]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/ipv4/udp.c
[INET_SOCK]: Move struct inet_sock & helper functions to net/inet_sock.h
[net-next-2.6.git] / net / ipv4 / udp.c
CommitLineData
1da177e4
LT
1/*
2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
5 *
6 * The User Datagram Protocol (UDP).
7 *
8 * Version: $Id: udp.c,v 1.102 2002/02/01 22:01:04 davem Exp $
9 *
02c30a84 10 * Authors: Ross Biro
1da177e4
LT
11 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
12 * Arnt Gulbrandsen, <agulbra@nvg.unit.no>
13 * Alan Cox, <Alan.Cox@linux.org>
14 * Hirokazu Takahashi, <taka@valinux.co.jp>
15 *
16 * Fixes:
17 * Alan Cox : verify_area() calls
18 * Alan Cox : stopped close while in use off icmp
19 * messages. Not a fix but a botch that
20 * for udp at least is 'valid'.
21 * Alan Cox : Fixed icmp handling properly
22 * Alan Cox : Correct error for oversized datagrams
23 * Alan Cox : Tidied select() semantics.
24 * Alan Cox : udp_err() fixed properly, also now
25 * select and read wake correctly on errors
26 * Alan Cox : udp_send verify_area moved to avoid mem leak
27 * Alan Cox : UDP can count its memory
28 * Alan Cox : send to an unknown connection causes
29 * an ECONNREFUSED off the icmp, but
30 * does NOT close.
31 * Alan Cox : Switched to new sk_buff handlers. No more backlog!
32 * Alan Cox : Using generic datagram code. Even smaller and the PEEK
33 * bug no longer crashes it.
34 * Fred Van Kempen : Net2e support for sk->broadcast.
35 * Alan Cox : Uses skb_free_datagram
36 * Alan Cox : Added get/set sockopt support.
37 * Alan Cox : Broadcasting without option set returns EACCES.
38 * Alan Cox : No wakeup calls. Instead we now use the callbacks.
39 * Alan Cox : Use ip_tos and ip_ttl
40 * Alan Cox : SNMP Mibs
41 * Alan Cox : MSG_DONTROUTE, and 0.0.0.0 support.
42 * Matt Dillon : UDP length checks.
43 * Alan Cox : Smarter af_inet used properly.
44 * Alan Cox : Use new kernel side addressing.
45 * Alan Cox : Incorrect return on truncated datagram receive.
46 * Arnt Gulbrandsen : New udp_send and stuff
47 * Alan Cox : Cache last socket
48 * Alan Cox : Route cache
49 * Jon Peatfield : Minor efficiency fix to sendto().
50 * Mike Shaver : RFC1122 checks.
51 * Alan Cox : Nonblocking error fix.
52 * Willy Konynenberg : Transparent proxying support.
53 * Mike McLagan : Routing by source
54 * David S. Miller : New socket lookup architecture.
55 * Last socket cache retained as it
56 * does have a high hit rate.
57 * Olaf Kirch : Don't linearise iovec on sendmsg.
58 * Andi Kleen : Some cleanups, cache destination entry
59 * for connect.
60 * Vitaly E. Lavrov : Transparent proxy revived after year coma.
61 * Melvin Smith : Check msg_name not msg_namelen in sendto(),
62 * return ENOTCONN for unconnected sockets (POSIX)
63 * Janos Farkas : don't deliver multi/broadcasts to a different
64 * bound-to-device socket
65 * Hirokazu Takahashi : HW checksumming for outgoing UDP
66 * datagrams.
67 * Hirokazu Takahashi : sendfile() on UDP works now.
68 * Arnaldo C. Melo : convert /proc/net/udp to seq_file
69 * YOSHIFUJI Hideaki @USAGI and: Support IPV6_V6ONLY socket option, which
70 * Alexey Kuznetsov: allow both IPv4 and IPv6 sockets to bind
71 * a single port at the same time.
72 * Derek Atkins <derek@ihtfp.com>: Add Encapulation Support
73 *
74 *
75 * This program is free software; you can redistribute it and/or
76 * modify it under the terms of the GNU General Public License
77 * as published by the Free Software Foundation; either version
78 * 2 of the License, or (at your option) any later version.
79 */
80
81#include <asm/system.h>
82#include <asm/uaccess.h>
83#include <asm/ioctls.h>
84#include <linux/types.h>
85#include <linux/fcntl.h>
86#include <linux/module.h>
87#include <linux/socket.h>
88#include <linux/sockios.h>
14c85021 89#include <linux/igmp.h>
1da177e4
LT
90#include <linux/in.h>
91#include <linux/errno.h>
92#include <linux/timer.h>
93#include <linux/mm.h>
94#include <linux/config.h>
95#include <linux/inet.h>
96#include <linux/ipv6.h>
97#include <linux/netdevice.h>
98#include <net/snmp.h>
c752f073
ACM
99#include <net/ip.h>
100#include <net/tcp_states.h>
1da177e4
LT
101#include <net/protocol.h>
102#include <linux/skbuff.h>
103#include <linux/proc_fs.h>
104#include <linux/seq_file.h>
105#include <net/sock.h>
106#include <net/udp.h>
107#include <net/icmp.h>
108#include <net/route.h>
109#include <net/inet_common.h>
110#include <net/checksum.h>
111#include <net/xfrm.h>
112
113/*
114 * Snmp MIB for the UDP layer
115 */
116
ba89966c 117DEFINE_SNMP_STAT(struct udp_mib, udp_statistics) __read_mostly;
1da177e4
LT
118
119struct hlist_head udp_hash[UDP_HTABLE_SIZE];
120DEFINE_RWLOCK(udp_hash_lock);
121
122/* Shared by v4/v6 udp. */
123int udp_port_rover;
124
125static int udp_v4_get_port(struct sock *sk, unsigned short snum)
126{
127 struct hlist_node *node;
128 struct sock *sk2;
129 struct inet_sock *inet = inet_sk(sk);
130
131 write_lock_bh(&udp_hash_lock);
132 if (snum == 0) {
133 int best_size_so_far, best, result, i;
134
135 if (udp_port_rover > sysctl_local_port_range[1] ||
136 udp_port_rover < sysctl_local_port_range[0])
137 udp_port_rover = sysctl_local_port_range[0];
138 best_size_so_far = 32767;
139 best = result = udp_port_rover;
140 for (i = 0; i < UDP_HTABLE_SIZE; i++, result++) {
141 struct hlist_head *list;
142 int size;
143
144 list = &udp_hash[result & (UDP_HTABLE_SIZE - 1)];
145 if (hlist_empty(list)) {
146 if (result > sysctl_local_port_range[1])
147 result = sysctl_local_port_range[0] +
148 ((result - sysctl_local_port_range[0]) &
149 (UDP_HTABLE_SIZE - 1));
150 goto gotit;
151 }
152 size = 0;
153 sk_for_each(sk2, node, list)
154 if (++size >= best_size_so_far)
155 goto next;
156 best_size_so_far = size;
157 best = result;
158 next:;
159 }
160 result = best;
161 for(i = 0; i < (1 << 16) / UDP_HTABLE_SIZE; i++, result += UDP_HTABLE_SIZE) {
162 if (result > sysctl_local_port_range[1])
163 result = sysctl_local_port_range[0]
164 + ((result - sysctl_local_port_range[0]) &
165 (UDP_HTABLE_SIZE - 1));
166 if (!udp_lport_inuse(result))
167 break;
168 }
169 if (i >= (1 << 16) / UDP_HTABLE_SIZE)
170 goto fail;
171gotit:
172 udp_port_rover = snum = result;
173 } else {
174 sk_for_each(sk2, node,
175 &udp_hash[snum & (UDP_HTABLE_SIZE - 1)]) {
176 struct inet_sock *inet2 = inet_sk(sk2);
177
178 if (inet2->num == snum &&
179 sk2 != sk &&
180 !ipv6_only_sock(sk2) &&
181 (!sk2->sk_bound_dev_if ||
182 !sk->sk_bound_dev_if ||
183 sk2->sk_bound_dev_if == sk->sk_bound_dev_if) &&
184 (!inet2->rcv_saddr ||
185 !inet->rcv_saddr ||
186 inet2->rcv_saddr == inet->rcv_saddr) &&
187 (!sk2->sk_reuse || !sk->sk_reuse))
188 goto fail;
189 }
190 }
191 inet->num = snum;
192 if (sk_unhashed(sk)) {
193 struct hlist_head *h = &udp_hash[snum & (UDP_HTABLE_SIZE - 1)];
194
195 sk_add_node(sk, h);
196 sock_prot_inc_use(sk->sk_prot);
197 }
198 write_unlock_bh(&udp_hash_lock);
199 return 0;
200
201fail:
202 write_unlock_bh(&udp_hash_lock);
203 return 1;
204}
205
206static void udp_v4_hash(struct sock *sk)
207{
208 BUG();
209}
210
211static void udp_v4_unhash(struct sock *sk)
212{
213 write_lock_bh(&udp_hash_lock);
214 if (sk_del_node_init(sk)) {
215 inet_sk(sk)->num = 0;
216 sock_prot_dec_use(sk->sk_prot);
217 }
218 write_unlock_bh(&udp_hash_lock);
219}
220
221/* UDP is nearly always wildcards out the wazoo, it makes no sense to try
222 * harder than this. -DaveM
223 */
224static struct sock *udp_v4_lookup_longway(u32 saddr, u16 sport,
225 u32 daddr, u16 dport, int dif)
226{
227 struct sock *sk, *result = NULL;
228 struct hlist_node *node;
229 unsigned short hnum = ntohs(dport);
230 int badness = -1;
231
232 sk_for_each(sk, node, &udp_hash[hnum & (UDP_HTABLE_SIZE - 1)]) {
233 struct inet_sock *inet = inet_sk(sk);
234
235 if (inet->num == hnum && !ipv6_only_sock(sk)) {
236 int score = (sk->sk_family == PF_INET ? 1 : 0);
237 if (inet->rcv_saddr) {
238 if (inet->rcv_saddr != daddr)
239 continue;
240 score+=2;
241 }
242 if (inet->daddr) {
243 if (inet->daddr != saddr)
244 continue;
245 score+=2;
246 }
247 if (inet->dport) {
248 if (inet->dport != sport)
249 continue;
250 score+=2;
251 }
252 if (sk->sk_bound_dev_if) {
253 if (sk->sk_bound_dev_if != dif)
254 continue;
255 score+=2;
256 }
257 if(score == 9) {
258 result = sk;
259 break;
260 } else if(score > badness) {
261 result = sk;
262 badness = score;
263 }
264 }
265 }
266 return result;
267}
268
269static __inline__ struct sock *udp_v4_lookup(u32 saddr, u16 sport,
270 u32 daddr, u16 dport, int dif)
271{
272 struct sock *sk;
273
274 read_lock(&udp_hash_lock);
275 sk = udp_v4_lookup_longway(saddr, sport, daddr, dport, dif);
276 if (sk)
277 sock_hold(sk);
278 read_unlock(&udp_hash_lock);
279 return sk;
280}
281
282static inline struct sock *udp_v4_mcast_next(struct sock *sk,
283 u16 loc_port, u32 loc_addr,
284 u16 rmt_port, u32 rmt_addr,
285 int dif)
286{
287 struct hlist_node *node;
288 struct sock *s = sk;
289 unsigned short hnum = ntohs(loc_port);
290
291 sk_for_each_from(s, node) {
292 struct inet_sock *inet = inet_sk(s);
293
294 if (inet->num != hnum ||
295 (inet->daddr && inet->daddr != rmt_addr) ||
296 (inet->dport != rmt_port && inet->dport) ||
297 (inet->rcv_saddr && inet->rcv_saddr != loc_addr) ||
298 ipv6_only_sock(s) ||
299 (s->sk_bound_dev_if && s->sk_bound_dev_if != dif))
300 continue;
301 if (!ip_mc_sf_allow(s, loc_addr, rmt_addr, dif))
302 continue;
303 goto found;
304 }
305 s = NULL;
306found:
307 return s;
308}
309
310/*
311 * This routine is called by the ICMP module when it gets some
312 * sort of error condition. If err < 0 then the socket should
313 * be closed and the error returned to the user. If err > 0
314 * it's just the icmp type << 8 | icmp code.
315 * Header points to the ip header of the error packet. We move
316 * on past this. Then (as it used to claim before adjustment)
317 * header points to the first 8 bytes of the udp header. We need
318 * to find the appropriate port.
319 */
320
321void udp_err(struct sk_buff *skb, u32 info)
322{
323 struct inet_sock *inet;
324 struct iphdr *iph = (struct iphdr*)skb->data;
325 struct udphdr *uh = (struct udphdr*)(skb->data+(iph->ihl<<2));
326 int type = skb->h.icmph->type;
327 int code = skb->h.icmph->code;
328 struct sock *sk;
329 int harderr;
330 int err;
331
332 sk = udp_v4_lookup(iph->daddr, uh->dest, iph->saddr, uh->source, skb->dev->ifindex);
333 if (sk == NULL) {
334 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
335 return; /* No socket for error */
336 }
337
338 err = 0;
339 harderr = 0;
340 inet = inet_sk(sk);
341
342 switch (type) {
343 default:
344 case ICMP_TIME_EXCEEDED:
345 err = EHOSTUNREACH;
346 break;
347 case ICMP_SOURCE_QUENCH:
348 goto out;
349 case ICMP_PARAMETERPROB:
350 err = EPROTO;
351 harderr = 1;
352 break;
353 case ICMP_DEST_UNREACH:
354 if (code == ICMP_FRAG_NEEDED) { /* Path MTU discovery */
355 if (inet->pmtudisc != IP_PMTUDISC_DONT) {
356 err = EMSGSIZE;
357 harderr = 1;
358 break;
359 }
360 goto out;
361 }
362 err = EHOSTUNREACH;
363 if (code <= NR_ICMP_UNREACH) {
364 harderr = icmp_err_convert[code].fatal;
365 err = icmp_err_convert[code].errno;
366 }
367 break;
368 }
369
370 /*
371 * RFC1122: OK. Passes ICMP errors back to application, as per
372 * 4.1.3.3.
373 */
374 if (!inet->recverr) {
375 if (!harderr || sk->sk_state != TCP_ESTABLISHED)
376 goto out;
377 } else {
378 ip_icmp_error(sk, skb, err, uh->dest, info, (u8*)(uh+1));
379 }
380 sk->sk_err = err;
381 sk->sk_error_report(sk);
382out:
383 sock_put(sk);
384}
385
386/*
387 * Throw away all pending data and cancel the corking. Socket is locked.
388 */
389static void udp_flush_pending_frames(struct sock *sk)
390{
391 struct udp_sock *up = udp_sk(sk);
392
393 if (up->pending) {
394 up->len = 0;
395 up->pending = 0;
396 ip_flush_pending_frames(sk);
397 }
398}
399
400/*
401 * Push out all pending data as one UDP datagram. Socket is locked.
402 */
403static int udp_push_pending_frames(struct sock *sk, struct udp_sock *up)
404{
405 struct inet_sock *inet = inet_sk(sk);
406 struct flowi *fl = &inet->cork.fl;
407 struct sk_buff *skb;
408 struct udphdr *uh;
409 int err = 0;
410
411 /* Grab the skbuff where UDP header space exists. */
412 if ((skb = skb_peek(&sk->sk_write_queue)) == NULL)
413 goto out;
414
415 /*
416 * Create a UDP header
417 */
418 uh = skb->h.uh;
419 uh->source = fl->fl_ip_sport;
420 uh->dest = fl->fl_ip_dport;
421 uh->len = htons(up->len);
422 uh->check = 0;
423
424 if (sk->sk_no_check == UDP_CSUM_NOXMIT) {
425 skb->ip_summed = CHECKSUM_NONE;
426 goto send;
427 }
428
429 if (skb_queue_len(&sk->sk_write_queue) == 1) {
430 /*
431 * Only one fragment on the socket.
432 */
433 if (skb->ip_summed == CHECKSUM_HW) {
434 skb->csum = offsetof(struct udphdr, check);
435 uh->check = ~csum_tcpudp_magic(fl->fl4_src, fl->fl4_dst,
436 up->len, IPPROTO_UDP, 0);
437 } else {
438 skb->csum = csum_partial((char *)uh,
439 sizeof(struct udphdr), skb->csum);
440 uh->check = csum_tcpudp_magic(fl->fl4_src, fl->fl4_dst,
441 up->len, IPPROTO_UDP, skb->csum);
442 if (uh->check == 0)
443 uh->check = -1;
444 }
445 } else {
446 unsigned int csum = 0;
447 /*
448 * HW-checksum won't work as there are two or more
449 * fragments on the socket so that all csums of sk_buffs
450 * should be together.
451 */
452 if (skb->ip_summed == CHECKSUM_HW) {
453 int offset = (unsigned char *)uh - skb->data;
454 skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
455
456 skb->ip_summed = CHECKSUM_NONE;
457 } else {
458 skb->csum = csum_partial((char *)uh,
459 sizeof(struct udphdr), skb->csum);
460 }
461
462 skb_queue_walk(&sk->sk_write_queue, skb) {
463 csum = csum_add(csum, skb->csum);
464 }
465 uh->check = csum_tcpudp_magic(fl->fl4_src, fl->fl4_dst,
466 up->len, IPPROTO_UDP, csum);
467 if (uh->check == 0)
468 uh->check = -1;
469 }
470send:
471 err = ip_push_pending_frames(sk);
472out:
473 up->len = 0;
474 up->pending = 0;
475 return err;
476}
477
478
479static unsigned short udp_check(struct udphdr *uh, int len, unsigned long saddr, unsigned long daddr, unsigned long base)
480{
481 return(csum_tcpudp_magic(saddr, daddr, len, IPPROTO_UDP, base));
482}
483
484int udp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
485 size_t len)
486{
487 struct inet_sock *inet = inet_sk(sk);
488 struct udp_sock *up = udp_sk(sk);
489 int ulen = len;
490 struct ipcm_cookie ipc;
491 struct rtable *rt = NULL;
492 int free = 0;
493 int connected = 0;
494 u32 daddr, faddr, saddr;
495 u16 dport;
496 u8 tos;
497 int err;
498 int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
499
500 if (len > 0xFFFF)
501 return -EMSGSIZE;
502
503 /*
504 * Check the flags.
505 */
506
507 if (msg->msg_flags&MSG_OOB) /* Mirror BSD error message compatibility */
508 return -EOPNOTSUPP;
509
510 ipc.opt = NULL;
511
512 if (up->pending) {
513 /*
514 * There are pending frames.
515 * The socket lock must be held while it's corked.
516 */
517 lock_sock(sk);
518 if (likely(up->pending)) {
519 if (unlikely(up->pending != AF_INET)) {
520 release_sock(sk);
521 return -EINVAL;
522 }
523 goto do_append_data;
524 }
525 release_sock(sk);
526 }
527 ulen += sizeof(struct udphdr);
528
529 /*
530 * Get and verify the address.
531 */
532 if (msg->msg_name) {
533 struct sockaddr_in * usin = (struct sockaddr_in*)msg->msg_name;
534 if (msg->msg_namelen < sizeof(*usin))
535 return -EINVAL;
536 if (usin->sin_family != AF_INET) {
537 if (usin->sin_family != AF_UNSPEC)
538 return -EAFNOSUPPORT;
539 }
540
541 daddr = usin->sin_addr.s_addr;
542 dport = usin->sin_port;
543 if (dport == 0)
544 return -EINVAL;
545 } else {
546 if (sk->sk_state != TCP_ESTABLISHED)
547 return -EDESTADDRREQ;
548 daddr = inet->daddr;
549 dport = inet->dport;
550 /* Open fast path for connected socket.
551 Route will not be used, if at least one option is set.
552 */
553 connected = 1;
554 }
555 ipc.addr = inet->saddr;
556
557 ipc.oif = sk->sk_bound_dev_if;
558 if (msg->msg_controllen) {
559 err = ip_cmsg_send(msg, &ipc);
560 if (err)
561 return err;
562 if (ipc.opt)
563 free = 1;
564 connected = 0;
565 }
566 if (!ipc.opt)
567 ipc.opt = inet->opt;
568
569 saddr = ipc.addr;
570 ipc.addr = faddr = daddr;
571
572 if (ipc.opt && ipc.opt->srr) {
573 if (!daddr)
574 return -EINVAL;
575 faddr = ipc.opt->faddr;
576 connected = 0;
577 }
578 tos = RT_TOS(inet->tos);
579 if (sock_flag(sk, SOCK_LOCALROUTE) ||
580 (msg->msg_flags & MSG_DONTROUTE) ||
581 (ipc.opt && ipc.opt->is_strictroute)) {
582 tos |= RTO_ONLINK;
583 connected = 0;
584 }
585
586 if (MULTICAST(daddr)) {
587 if (!ipc.oif)
588 ipc.oif = inet->mc_index;
589 if (!saddr)
590 saddr = inet->mc_addr;
591 connected = 0;
592 }
593
594 if (connected)
595 rt = (struct rtable*)sk_dst_check(sk, 0);
596
597 if (rt == NULL) {
598 struct flowi fl = { .oif = ipc.oif,
599 .nl_u = { .ip4_u =
600 { .daddr = faddr,
601 .saddr = saddr,
602 .tos = tos } },
603 .proto = IPPROTO_UDP,
604 .uli_u = { .ports =
605 { .sport = inet->sport,
606 .dport = dport } } };
607 err = ip_route_output_flow(&rt, &fl, sk, !(msg->msg_flags&MSG_DONTWAIT));
608 if (err)
609 goto out;
610
611 err = -EACCES;
612 if ((rt->rt_flags & RTCF_BROADCAST) &&
613 !sock_flag(sk, SOCK_BROADCAST))
614 goto out;
615 if (connected)
616 sk_dst_set(sk, dst_clone(&rt->u.dst));
617 }
618
619 if (msg->msg_flags&MSG_CONFIRM)
620 goto do_confirm;
621back_from_confirm:
622
623 saddr = rt->rt_src;
624 if (!ipc.addr)
625 daddr = ipc.addr = rt->rt_dst;
626
627 lock_sock(sk);
628 if (unlikely(up->pending)) {
629 /* The socket is already corked while preparing it. */
630 /* ... which is an evident application bug. --ANK */
631 release_sock(sk);
632
64ce2073 633 LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 2\n");
1da177e4
LT
634 err = -EINVAL;
635 goto out;
636 }
637 /*
638 * Now cork the socket to pend data.
639 */
640 inet->cork.fl.fl4_dst = daddr;
641 inet->cork.fl.fl_ip_dport = dport;
642 inet->cork.fl.fl4_src = saddr;
643 inet->cork.fl.fl_ip_sport = inet->sport;
644 up->pending = AF_INET;
645
646do_append_data:
647 up->len += ulen;
648 err = ip_append_data(sk, ip_generic_getfrag, msg->msg_iov, ulen,
649 sizeof(struct udphdr), &ipc, rt,
650 corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags);
651 if (err)
652 udp_flush_pending_frames(sk);
653 else if (!corkreq)
654 err = udp_push_pending_frames(sk, up);
655 release_sock(sk);
656
657out:
658 ip_rt_put(rt);
659 if (free)
660 kfree(ipc.opt);
661 if (!err) {
662 UDP_INC_STATS_USER(UDP_MIB_OUTDATAGRAMS);
663 return len;
664 }
665 return err;
666
667do_confirm:
668 dst_confirm(&rt->u.dst);
669 if (!(msg->msg_flags&MSG_PROBE) || len)
670 goto back_from_confirm;
671 err = 0;
672 goto out;
673}
674
675static int udp_sendpage(struct sock *sk, struct page *page, int offset,
676 size_t size, int flags)
677{
678 struct udp_sock *up = udp_sk(sk);
679 int ret;
680
681 if (!up->pending) {
682 struct msghdr msg = { .msg_flags = flags|MSG_MORE };
683
684 /* Call udp_sendmsg to specify destination address which
685 * sendpage interface can't pass.
686 * This will succeed only when the socket is connected.
687 */
688 ret = udp_sendmsg(NULL, sk, &msg, 0);
689 if (ret < 0)
690 return ret;
691 }
692
693 lock_sock(sk);
694
695 if (unlikely(!up->pending)) {
696 release_sock(sk);
697
64ce2073 698 LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 3\n");
1da177e4
LT
699 return -EINVAL;
700 }
701
702 ret = ip_append_page(sk, page, offset, size, flags);
703 if (ret == -EOPNOTSUPP) {
704 release_sock(sk);
705 return sock_no_sendpage(sk->sk_socket, page, offset,
706 size, flags);
707 }
708 if (ret < 0) {
709 udp_flush_pending_frames(sk);
710 goto out;
711 }
712
713 up->len += size;
714 if (!(up->corkflag || (flags&MSG_MORE)))
715 ret = udp_push_pending_frames(sk, up);
716 if (!ret)
717 ret = size;
718out:
719 release_sock(sk);
720 return ret;
721}
722
723/*
724 * IOCTL requests applicable to the UDP protocol
725 */
726
727int udp_ioctl(struct sock *sk, int cmd, unsigned long arg)
728{
729 switch(cmd)
730 {
731 case SIOCOUTQ:
732 {
733 int amount = atomic_read(&sk->sk_wmem_alloc);
734 return put_user(amount, (int __user *)arg);
735 }
736
737 case SIOCINQ:
738 {
739 struct sk_buff *skb;
740 unsigned long amount;
741
742 amount = 0;
208d8984 743 spin_lock_bh(&sk->sk_receive_queue.lock);
1da177e4
LT
744 skb = skb_peek(&sk->sk_receive_queue);
745 if (skb != NULL) {
746 /*
747 * We will only return the amount
748 * of this packet since that is all
749 * that will be read.
750 */
751 amount = skb->len - sizeof(struct udphdr);
752 }
208d8984 753 spin_unlock_bh(&sk->sk_receive_queue.lock);
1da177e4
LT
754 return put_user(amount, (int __user *)arg);
755 }
756
757 default:
758 return -ENOIOCTLCMD;
759 }
760 return(0);
761}
762
763static __inline__ int __udp_checksum_complete(struct sk_buff *skb)
764{
fb286bb2 765 return __skb_checksum_complete(skb);
1da177e4
LT
766}
767
768static __inline__ int udp_checksum_complete(struct sk_buff *skb)
769{
770 return skb->ip_summed != CHECKSUM_UNNECESSARY &&
771 __udp_checksum_complete(skb);
772}
773
774/*
775 * This should be easy, if there is something there we
776 * return it, otherwise we block.
777 */
778
779static int udp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
780 size_t len, int noblock, int flags, int *addr_len)
781{
782 struct inet_sock *inet = inet_sk(sk);
783 struct sockaddr_in *sin = (struct sockaddr_in *)msg->msg_name;
784 struct sk_buff *skb;
785 int copied, err;
786
787 /*
788 * Check any passed addresses
789 */
790 if (addr_len)
791 *addr_len=sizeof(*sin);
792
793 if (flags & MSG_ERRQUEUE)
794 return ip_recv_error(sk, msg, len);
795
796try_again:
797 skb = skb_recv_datagram(sk, flags, noblock, &err);
798 if (!skb)
799 goto out;
800
801 copied = skb->len - sizeof(struct udphdr);
802 if (copied > len) {
803 copied = len;
804 msg->msg_flags |= MSG_TRUNC;
805 }
806
807 if (skb->ip_summed==CHECKSUM_UNNECESSARY) {
808 err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov,
809 copied);
810 } else if (msg->msg_flags&MSG_TRUNC) {
811 if (__udp_checksum_complete(skb))
812 goto csum_copy_err;
813 err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov,
814 copied);
815 } else {
816 err = skb_copy_and_csum_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov);
817
818 if (err == -EINVAL)
819 goto csum_copy_err;
820 }
821
822 if (err)
823 goto out_free;
824
825 sock_recv_timestamp(msg, sk, skb);
826
827 /* Copy the address. */
828 if (sin)
829 {
830 sin->sin_family = AF_INET;
831 sin->sin_port = skb->h.uh->source;
832 sin->sin_addr.s_addr = skb->nh.iph->saddr;
833 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
834 }
835 if (inet->cmsg_flags)
836 ip_cmsg_recv(msg, skb);
837
838 err = copied;
839 if (flags & MSG_TRUNC)
840 err = skb->len - sizeof(struct udphdr);
841
842out_free:
843 skb_free_datagram(sk, skb);
844out:
845 return err;
846
847csum_copy_err:
848 UDP_INC_STATS_BH(UDP_MIB_INERRORS);
849
3305b80c 850 skb_kill_datagram(sk, skb, flags);
1da177e4
LT
851
852 if (noblock)
853 return -EAGAIN;
854 goto try_again;
855}
856
857
858int udp_disconnect(struct sock *sk, int flags)
859{
860 struct inet_sock *inet = inet_sk(sk);
861 /*
862 * 1003.1g - break association.
863 */
864
865 sk->sk_state = TCP_CLOSE;
866 inet->daddr = 0;
867 inet->dport = 0;
868 sk->sk_bound_dev_if = 0;
869 if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK))
870 inet_reset_saddr(sk);
871
872 if (!(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) {
873 sk->sk_prot->unhash(sk);
874 inet->sport = 0;
875 }
876 sk_dst_reset(sk);
877 return 0;
878}
879
880static void udp_close(struct sock *sk, long timeout)
881{
882 sk_common_release(sk);
883}
884
885/* return:
886 * 1 if the the UDP system should process it
887 * 0 if we should drop this packet
888 * -1 if it should get processed by xfrm4_rcv_encap
889 */
890static int udp_encap_rcv(struct sock * sk, struct sk_buff *skb)
891{
892#ifndef CONFIG_XFRM
893 return 1;
894#else
895 struct udp_sock *up = udp_sk(sk);
896 struct udphdr *uh = skb->h.uh;
897 struct iphdr *iph;
898 int iphlen, len;
899
900 __u8 *udpdata = (__u8 *)uh + sizeof(struct udphdr);
901 __u32 *udpdata32 = (__u32 *)udpdata;
902 __u16 encap_type = up->encap_type;
903
904 /* if we're overly short, let UDP handle it */
905 if (udpdata > skb->tail)
906 return 1;
907
908 /* if this is not encapsulated socket, then just return now */
909 if (!encap_type)
910 return 1;
911
912 len = skb->tail - udpdata;
913
914 switch (encap_type) {
915 default:
916 case UDP_ENCAP_ESPINUDP:
917 /* Check if this is a keepalive packet. If so, eat it. */
918 if (len == 1 && udpdata[0] == 0xff) {
919 return 0;
920 } else if (len > sizeof(struct ip_esp_hdr) && udpdata32[0] != 0 ) {
921 /* ESP Packet without Non-ESP header */
922 len = sizeof(struct udphdr);
923 } else
924 /* Must be an IKE packet.. pass it through */
925 return 1;
926 break;
927 case UDP_ENCAP_ESPINUDP_NON_IKE:
928 /* Check if this is a keepalive packet. If so, eat it. */
929 if (len == 1 && udpdata[0] == 0xff) {
930 return 0;
931 } else if (len > 2 * sizeof(u32) + sizeof(struct ip_esp_hdr) &&
932 udpdata32[0] == 0 && udpdata32[1] == 0) {
933
934 /* ESP Packet with Non-IKE marker */
935 len = sizeof(struct udphdr) + 2 * sizeof(u32);
936 } else
937 /* Must be an IKE packet.. pass it through */
938 return 1;
939 break;
940 }
941
942 /* At this point we are sure that this is an ESPinUDP packet,
943 * so we need to remove 'len' bytes from the packet (the UDP
944 * header and optional ESP marker bytes) and then modify the
945 * protocol to ESP, and then call into the transform receiver.
946 */
4d78b6c7
HX
947 if (skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
948 return 0;
1da177e4
LT
949
950 /* Now we can update and verify the packet length... */
951 iph = skb->nh.iph;
952 iphlen = iph->ihl << 2;
953 iph->tot_len = htons(ntohs(iph->tot_len) - len);
954 if (skb->len < iphlen + len) {
955 /* packet is too small!?! */
956 return 0;
957 }
958
959 /* pull the data buffer up to the ESP header and set the
960 * transport header to point to ESP. Keep UDP on the stack
961 * for later.
962 */
963 skb->h.raw = skb_pull(skb, len);
964
965 /* modify the protocol (it's ESP!) */
966 iph->protocol = IPPROTO_ESP;
967
968 /* and let the caller know to send this into the ESP processor... */
969 return -1;
970#endif
971}
972
973/* returns:
974 * -1: error
975 * 0: success
976 * >0: "udp encap" protocol resubmission
977 *
978 * Note that in the success and error cases, the skb is assumed to
979 * have either been requeued or freed.
980 */
981static int udp_queue_rcv_skb(struct sock * sk, struct sk_buff *skb)
982{
983 struct udp_sock *up = udp_sk(sk);
984
985 /*
986 * Charge it to the socket, dropping if the queue is full.
987 */
988 if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb)) {
989 kfree_skb(skb);
990 return -1;
991 }
992
993 if (up->encap_type) {
994 /*
995 * This is an encapsulation socket, so let's see if this is
996 * an encapsulated packet.
997 * If it's a keepalive packet, then just eat it.
998 * If it's an encapsulateed packet, then pass it to the
999 * IPsec xfrm input and return the response
1000 * appropriately. Otherwise, just fall through and
1001 * pass this up the UDP socket.
1002 */
1003 int ret;
1004
1005 ret = udp_encap_rcv(sk, skb);
1006 if (ret == 0) {
1007 /* Eat the packet .. */
1008 kfree_skb(skb);
1009 return 0;
1010 }
1011 if (ret < 0) {
1012 /* process the ESP packet */
1013 ret = xfrm4_rcv_encap(skb, up->encap_type);
1014 UDP_INC_STATS_BH(UDP_MIB_INDATAGRAMS);
1015 return -ret;
1016 }
1017 /* FALLTHROUGH -- it's a UDP Packet */
1018 }
1019
1020 if (sk->sk_filter && skb->ip_summed != CHECKSUM_UNNECESSARY) {
1021 if (__udp_checksum_complete(skb)) {
1022 UDP_INC_STATS_BH(UDP_MIB_INERRORS);
1023 kfree_skb(skb);
1024 return -1;
1025 }
1026 skb->ip_summed = CHECKSUM_UNNECESSARY;
1027 }
1028
1029 if (sock_queue_rcv_skb(sk,skb)<0) {
1030 UDP_INC_STATS_BH(UDP_MIB_INERRORS);
1031 kfree_skb(skb);
1032 return -1;
1033 }
1034 UDP_INC_STATS_BH(UDP_MIB_INDATAGRAMS);
1035 return 0;
1036}
1037
1038/*
1039 * Multicasts and broadcasts go to each listener.
1040 *
1041 * Note: called only from the BH handler context,
1042 * so we don't need to lock the hashes.
1043 */
1044static int udp_v4_mcast_deliver(struct sk_buff *skb, struct udphdr *uh,
1045 u32 saddr, u32 daddr)
1046{
1047 struct sock *sk;
1048 int dif;
1049
1050 read_lock(&udp_hash_lock);
1051 sk = sk_head(&udp_hash[ntohs(uh->dest) & (UDP_HTABLE_SIZE - 1)]);
1052 dif = skb->dev->ifindex;
1053 sk = udp_v4_mcast_next(sk, uh->dest, daddr, uh->source, saddr, dif);
1054 if (sk) {
1055 struct sock *sknext = NULL;
1056
1057 do {
1058 struct sk_buff *skb1 = skb;
1059
1060 sknext = udp_v4_mcast_next(sk_next(sk), uh->dest, daddr,
1061 uh->source, saddr, dif);
1062 if(sknext)
1063 skb1 = skb_clone(skb, GFP_ATOMIC);
1064
1065 if(skb1) {
1066 int ret = udp_queue_rcv_skb(sk, skb1);
1067 if (ret > 0)
1068 /* we should probably re-process instead
1069 * of dropping packets here. */
1070 kfree_skb(skb1);
1071 }
1072 sk = sknext;
1073 } while(sknext);
1074 } else
1075 kfree_skb(skb);
1076 read_unlock(&udp_hash_lock);
1077 return 0;
1078}
1079
1080/* Initialize UDP checksum. If exited with zero value (success),
1081 * CHECKSUM_UNNECESSARY means, that no more checks are required.
1082 * Otherwise, csum completion requires chacksumming packet body,
1083 * including udp header and folding it to skb->csum.
1084 */
65a45441 1085static void udp_checksum_init(struct sk_buff *skb, struct udphdr *uh,
1da177e4
LT
1086 unsigned short ulen, u32 saddr, u32 daddr)
1087{
1088 if (uh->check == 0) {
1089 skb->ip_summed = CHECKSUM_UNNECESSARY;
1090 } else if (skb->ip_summed == CHECKSUM_HW) {
1da177e4 1091 if (!udp_check(uh, ulen, saddr, daddr, skb->csum))
fb286bb2 1092 skb->ip_summed = CHECKSUM_UNNECESSARY;
1da177e4
LT
1093 }
1094 if (skb->ip_summed != CHECKSUM_UNNECESSARY)
1095 skb->csum = csum_tcpudp_nofold(saddr, daddr, ulen, IPPROTO_UDP, 0);
1096 /* Probably, we should checksum udp header (it should be in cache
1097 * in any case) and data in tiny packets (< rx copybreak).
1098 */
1da177e4
LT
1099}
1100
1101/*
1102 * All we need to do is get the socket, and then do a checksum.
1103 */
1104
1105int udp_rcv(struct sk_buff *skb)
1106{
1107 struct sock *sk;
1108 struct udphdr *uh;
1109 unsigned short ulen;
1110 struct rtable *rt = (struct rtable*)skb->dst;
1111 u32 saddr = skb->nh.iph->saddr;
1112 u32 daddr = skb->nh.iph->daddr;
1113 int len = skb->len;
1114
1115 /*
1116 * Validate the packet and the UDP length.
1117 */
1118 if (!pskb_may_pull(skb, sizeof(struct udphdr)))
1119 goto no_header;
1120
1121 uh = skb->h.uh;
1122
1123 ulen = ntohs(uh->len);
1124
1125 if (ulen > len || ulen < sizeof(*uh))
1126 goto short_packet;
1127
e308e25c 1128 if (pskb_trim_rcsum(skb, ulen))
1da177e4
LT
1129 goto short_packet;
1130
65a45441 1131 udp_checksum_init(skb, uh, ulen, saddr, daddr);
1da177e4
LT
1132
1133 if(rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST))
1134 return udp_v4_mcast_deliver(skb, uh, saddr, daddr);
1135
1136 sk = udp_v4_lookup(saddr, uh->source, daddr, uh->dest, skb->dev->ifindex);
1137
1138 if (sk != NULL) {
1139 int ret = udp_queue_rcv_skb(sk, skb);
1140 sock_put(sk);
1141
1142 /* a return value > 0 means to resubmit the input, but
1143 * it it wants the return to be -protocol, or 0
1144 */
1145 if (ret > 0)
1146 return -ret;
1147 return 0;
1148 }
1149
1150 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
1151 goto drop;
1152
1153 /* No socket. Drop packet silently, if checksum is wrong */
1154 if (udp_checksum_complete(skb))
1155 goto csum_error;
1156
1157 UDP_INC_STATS_BH(UDP_MIB_NOPORTS);
1158 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
1159
1160 /*
1161 * Hmm. We got an UDP packet to a port to which we
1162 * don't wanna listen. Ignore it.
1163 */
1164 kfree_skb(skb);
1165 return(0);
1166
1167short_packet:
64ce2073
PM
1168 LIMIT_NETDEBUG(KERN_DEBUG "UDP: short packet: From %u.%u.%u.%u:%u %d/%d to %u.%u.%u.%u:%u\n",
1169 NIPQUAD(saddr),
1170 ntohs(uh->source),
1171 ulen,
1172 len,
1173 NIPQUAD(daddr),
1174 ntohs(uh->dest));
1da177e4
LT
1175no_header:
1176 UDP_INC_STATS_BH(UDP_MIB_INERRORS);
1177 kfree_skb(skb);
1178 return(0);
1179
1180csum_error:
1181 /*
1182 * RFC1122: OK. Discards the bad packet silently (as far as
1183 * the network is concerned, anyway) as per 4.1.3.4 (MUST).
1184 */
64ce2073
PM
1185 LIMIT_NETDEBUG(KERN_DEBUG "UDP: bad checksum. From %d.%d.%d.%d:%d to %d.%d.%d.%d:%d ulen %d\n",
1186 NIPQUAD(saddr),
1187 ntohs(uh->source),
1188 NIPQUAD(daddr),
1189 ntohs(uh->dest),
1190 ulen);
1da177e4
LT
1191drop:
1192 UDP_INC_STATS_BH(UDP_MIB_INERRORS);
1193 kfree_skb(skb);
1194 return(0);
1195}
1196
1197static int udp_destroy_sock(struct sock *sk)
1198{
1199 lock_sock(sk);
1200 udp_flush_pending_frames(sk);
1201 release_sock(sk);
1202 return 0;
1203}
1204
1205/*
1206 * Socket option code for UDP
1207 */
1208static int udp_setsockopt(struct sock *sk, int level, int optname,
1209 char __user *optval, int optlen)
1210{
1211 struct udp_sock *up = udp_sk(sk);
1212 int val;
1213 int err = 0;
1214
1215 if (level != SOL_UDP)
1216 return ip_setsockopt(sk, level, optname, optval, optlen);
1217
1218 if(optlen<sizeof(int))
1219 return -EINVAL;
1220
1221 if (get_user(val, (int __user *)optval))
1222 return -EFAULT;
1223
1224 switch(optname) {
1225 case UDP_CORK:
1226 if (val != 0) {
1227 up->corkflag = 1;
1228 } else {
1229 up->corkflag = 0;
1230 lock_sock(sk);
1231 udp_push_pending_frames(sk, up);
1232 release_sock(sk);
1233 }
1234 break;
1235
1236 case UDP_ENCAP:
1237 switch (val) {
1238 case 0:
1239 case UDP_ENCAP_ESPINUDP:
1240 case UDP_ENCAP_ESPINUDP_NON_IKE:
1241 up->encap_type = val;
1242 break;
1243 default:
1244 err = -ENOPROTOOPT;
1245 break;
1246 }
1247 break;
1248
1249 default:
1250 err = -ENOPROTOOPT;
1251 break;
1252 };
1253
1254 return err;
1255}
1256
1257static int udp_getsockopt(struct sock *sk, int level, int optname,
1258 char __user *optval, int __user *optlen)
1259{
1260 struct udp_sock *up = udp_sk(sk);
1261 int val, len;
1262
1263 if (level != SOL_UDP)
1264 return ip_getsockopt(sk, level, optname, optval, optlen);
1265
1266 if(get_user(len,optlen))
1267 return -EFAULT;
1268
1269 len = min_t(unsigned int, len, sizeof(int));
1270
1271 if(len < 0)
1272 return -EINVAL;
1273
1274 switch(optname) {
1275 case UDP_CORK:
1276 val = up->corkflag;
1277 break;
1278
1279 case UDP_ENCAP:
1280 val = up->encap_type;
1281 break;
1282
1283 default:
1284 return -ENOPROTOOPT;
1285 };
1286
1287 if(put_user(len, optlen))
1288 return -EFAULT;
1289 if(copy_to_user(optval, &val,len))
1290 return -EFAULT;
1291 return 0;
1292}
1293
1294/**
1295 * udp_poll - wait for a UDP event.
1296 * @file - file struct
1297 * @sock - socket
1298 * @wait - poll table
1299 *
1300 * This is same as datagram poll, except for the special case of
1301 * blocking sockets. If application is using a blocking fd
1302 * and a packet with checksum error is in the queue;
1303 * then it could get return from select indicating data available
1304 * but then block when reading it. Add special case code
1305 * to work around these arguably broken applications.
1306 */
1307unsigned int udp_poll(struct file *file, struct socket *sock, poll_table *wait)
1308{
1309 unsigned int mask = datagram_poll(file, sock, wait);
1310 struct sock *sk = sock->sk;
1311
1312 /* Check for false positives due to checksum errors */
1313 if ( (mask & POLLRDNORM) &&
1314 !(file->f_flags & O_NONBLOCK) &&
1315 !(sk->sk_shutdown & RCV_SHUTDOWN)){
1316 struct sk_buff_head *rcvq = &sk->sk_receive_queue;
1317 struct sk_buff *skb;
1318
208d8984 1319 spin_lock_bh(&rcvq->lock);
1da177e4
LT
1320 while ((skb = skb_peek(rcvq)) != NULL) {
1321 if (udp_checksum_complete(skb)) {
1322 UDP_INC_STATS_BH(UDP_MIB_INERRORS);
1323 __skb_unlink(skb, rcvq);
1324 kfree_skb(skb);
1325 } else {
1326 skb->ip_summed = CHECKSUM_UNNECESSARY;
1327 break;
1328 }
1329 }
208d8984 1330 spin_unlock_bh(&rcvq->lock);
1da177e4
LT
1331
1332 /* nothing to see, move along */
1333 if (skb == NULL)
1334 mask &= ~(POLLIN | POLLRDNORM);
1335 }
1336
1337 return mask;
1338
1339}
1340
1341struct proto udp_prot = {
1342 .name = "UDP",
1343 .owner = THIS_MODULE,
1344 .close = udp_close,
1345 .connect = ip4_datagram_connect,
1346 .disconnect = udp_disconnect,
1347 .ioctl = udp_ioctl,
1348 .destroy = udp_destroy_sock,
1349 .setsockopt = udp_setsockopt,
1350 .getsockopt = udp_getsockopt,
1351 .sendmsg = udp_sendmsg,
1352 .recvmsg = udp_recvmsg,
1353 .sendpage = udp_sendpage,
1354 .backlog_rcv = udp_queue_rcv_skb,
1355 .hash = udp_v4_hash,
1356 .unhash = udp_v4_unhash,
1357 .get_port = udp_v4_get_port,
1358 .obj_size = sizeof(struct udp_sock),
1359};
1360
1361/* ------------------------------------------------------------------------ */
1362#ifdef CONFIG_PROC_FS
1363
1364static struct sock *udp_get_first(struct seq_file *seq)
1365{
1366 struct sock *sk;
1367 struct udp_iter_state *state = seq->private;
1368
1369 for (state->bucket = 0; state->bucket < UDP_HTABLE_SIZE; ++state->bucket) {
1370 struct hlist_node *node;
1371 sk_for_each(sk, node, &udp_hash[state->bucket]) {
1372 if (sk->sk_family == state->family)
1373 goto found;
1374 }
1375 }
1376 sk = NULL;
1377found:
1378 return sk;
1379}
1380
1381static struct sock *udp_get_next(struct seq_file *seq, struct sock *sk)
1382{
1383 struct udp_iter_state *state = seq->private;
1384
1385 do {
1386 sk = sk_next(sk);
1387try_again:
1388 ;
1389 } while (sk && sk->sk_family != state->family);
1390
1391 if (!sk && ++state->bucket < UDP_HTABLE_SIZE) {
1392 sk = sk_head(&udp_hash[state->bucket]);
1393 goto try_again;
1394 }
1395 return sk;
1396}
1397
1398static struct sock *udp_get_idx(struct seq_file *seq, loff_t pos)
1399{
1400 struct sock *sk = udp_get_first(seq);
1401
1402 if (sk)
1403 while(pos && (sk = udp_get_next(seq, sk)) != NULL)
1404 --pos;
1405 return pos ? NULL : sk;
1406}
1407
1408static void *udp_seq_start(struct seq_file *seq, loff_t *pos)
1409{
1410 read_lock(&udp_hash_lock);
1411 return *pos ? udp_get_idx(seq, *pos-1) : (void *)1;
1412}
1413
1414static void *udp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1415{
1416 struct sock *sk;
1417
1418 if (v == (void *)1)
1419 sk = udp_get_idx(seq, 0);
1420 else
1421 sk = udp_get_next(seq, v);
1422
1423 ++*pos;
1424 return sk;
1425}
1426
1427static void udp_seq_stop(struct seq_file *seq, void *v)
1428{
1429 read_unlock(&udp_hash_lock);
1430}
1431
1432static int udp_seq_open(struct inode *inode, struct file *file)
1433{
1434 struct udp_seq_afinfo *afinfo = PDE(inode)->data;
1435 struct seq_file *seq;
1436 int rc = -ENOMEM;
1437 struct udp_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
1438
1439 if (!s)
1440 goto out;
1441 memset(s, 0, sizeof(*s));
1442 s->family = afinfo->family;
1443 s->seq_ops.start = udp_seq_start;
1444 s->seq_ops.next = udp_seq_next;
1445 s->seq_ops.show = afinfo->seq_show;
1446 s->seq_ops.stop = udp_seq_stop;
1447
1448 rc = seq_open(file, &s->seq_ops);
1449 if (rc)
1450 goto out_kfree;
1451
1452 seq = file->private_data;
1453 seq->private = s;
1454out:
1455 return rc;
1456out_kfree:
1457 kfree(s);
1458 goto out;
1459}
1460
1461/* ------------------------------------------------------------------------ */
1462int udp_proc_register(struct udp_seq_afinfo *afinfo)
1463{
1464 struct proc_dir_entry *p;
1465 int rc = 0;
1466
1467 if (!afinfo)
1468 return -EINVAL;
1469 afinfo->seq_fops->owner = afinfo->owner;
1470 afinfo->seq_fops->open = udp_seq_open;
1471 afinfo->seq_fops->read = seq_read;
1472 afinfo->seq_fops->llseek = seq_lseek;
1473 afinfo->seq_fops->release = seq_release_private;
1474
1475 p = proc_net_fops_create(afinfo->name, S_IRUGO, afinfo->seq_fops);
1476 if (p)
1477 p->data = afinfo;
1478 else
1479 rc = -ENOMEM;
1480 return rc;
1481}
1482
1483void udp_proc_unregister(struct udp_seq_afinfo *afinfo)
1484{
1485 if (!afinfo)
1486 return;
1487 proc_net_remove(afinfo->name);
1488 memset(afinfo->seq_fops, 0, sizeof(*afinfo->seq_fops));
1489}
1490
1491/* ------------------------------------------------------------------------ */
1492static void udp4_format_sock(struct sock *sp, char *tmpbuf, int bucket)
1493{
1494 struct inet_sock *inet = inet_sk(sp);
1495 unsigned int dest = inet->daddr;
1496 unsigned int src = inet->rcv_saddr;
1497 __u16 destp = ntohs(inet->dport);
1498 __u16 srcp = ntohs(inet->sport);
1499
1500 sprintf(tmpbuf, "%4d: %08X:%04X %08X:%04X"
1501 " %02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p",
1502 bucket, src, srcp, dest, destp, sp->sk_state,
1503 atomic_read(&sp->sk_wmem_alloc),
1504 atomic_read(&sp->sk_rmem_alloc),
1505 0, 0L, 0, sock_i_uid(sp), 0, sock_i_ino(sp),
1506 atomic_read(&sp->sk_refcnt), sp);
1507}
1508
1509static int udp4_seq_show(struct seq_file *seq, void *v)
1510{
1511 if (v == SEQ_START_TOKEN)
1512 seq_printf(seq, "%-127s\n",
1513 " sl local_address rem_address st tx_queue "
1514 "rx_queue tr tm->when retrnsmt uid timeout "
1515 "inode");
1516 else {
1517 char tmpbuf[129];
1518 struct udp_iter_state *state = seq->private;
1519
1520 udp4_format_sock(v, tmpbuf, state->bucket);
1521 seq_printf(seq, "%-127s\n", tmpbuf);
1522 }
1523 return 0;
1524}
1525
1526/* ------------------------------------------------------------------------ */
1527static struct file_operations udp4_seq_fops;
1528static struct udp_seq_afinfo udp4_seq_afinfo = {
1529 .owner = THIS_MODULE,
1530 .name = "udp",
1531 .family = AF_INET,
1532 .seq_show = udp4_seq_show,
1533 .seq_fops = &udp4_seq_fops,
1534};
1535
1536int __init udp4_proc_init(void)
1537{
1538 return udp_proc_register(&udp4_seq_afinfo);
1539}
1540
1541void udp4_proc_exit(void)
1542{
1543 udp_proc_unregister(&udp4_seq_afinfo);
1544}
1545#endif /* CONFIG_PROC_FS */
1546
1547EXPORT_SYMBOL(udp_disconnect);
1548EXPORT_SYMBOL(udp_hash);
1549EXPORT_SYMBOL(udp_hash_lock);
1550EXPORT_SYMBOL(udp_ioctl);
1551EXPORT_SYMBOL(udp_port_rover);
1552EXPORT_SYMBOL(udp_prot);
1553EXPORT_SYMBOL(udp_sendmsg);
1554EXPORT_SYMBOL(udp_poll);
1555
1556#ifdef CONFIG_PROC_FS
1557EXPORT_SYMBOL(udp_proc_register);
1558EXPORT_SYMBOL(udp_proc_unregister);
1559#endif