MOXA linux-2.6.x / linux-2.6.19-uc1 from UC-7110-LX-BOOTLOADER-1.9_VERSION-4.2.tgz
[linux-2.6.19-moxart.git] / net / ipv4 / udp.c
blob79939522968cc9e87c6a374e2c578bd747f7c00f
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.
6 * The User Datagram Protocol (UDP).
8 * Version: $Id: udp.c,v 1.102 2002/02/01 22:01:04 davem Exp $
10 * Authors: Ross Biro
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>
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
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.
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>
89 #include <linux/igmp.h>
90 #include <linux/in.h>
91 #include <linux/errno.h>
92 #include <linux/timer.h>
93 #include <linux/mm.h>
94 #include <linux/inet.h>
95 #include <linux/ipv6.h>
96 #include <linux/netdevice.h>
97 #include <net/snmp.h>
98 #include <net/ip.h>
99 #include <net/tcp_states.h>
100 #include <net/protocol.h>
101 #include <linux/skbuff.h>
102 #include <linux/proc_fs.h>
103 #include <linux/seq_file.h>
104 #include <net/sock.h>
105 #include <net/udp.h>
106 #include <net/icmp.h>
107 #include <net/route.h>
108 #include <net/inet_common.h>
109 #include <net/checksum.h>
110 #include <net/xfrm.h>
111 #include <net/xfrmudp.h>
114 * Snmp MIB for the UDP layer
117 static xfrm4_rcv_encap_t xfrm4_rcv_encap_func;
119 DEFINE_SNMP_STAT(struct udp_mib, udp_statistics) __read_mostly;
121 struct hlist_head udp_hash[UDP_HTABLE_SIZE];
122 DEFINE_RWLOCK(udp_hash_lock);
124 static int udp_port_rover;
126 static inline int udp_lport_inuse(u16 num)
128 struct sock *sk;
129 struct hlist_node *node;
131 sk_for_each(sk, node, &udp_hash[num & (UDP_HTABLE_SIZE - 1)])
132 if (inet_sk(sk)->num == num)
133 return 1;
134 return 0;
138 * udp_get_port - common port lookup for IPv4 and IPv6
140 * @sk: socket struct in question
141 * @snum: port number to look up
142 * @saddr_comp: AF-dependent comparison of bound local IP addresses
144 int udp_get_port(struct sock *sk, unsigned short snum,
145 int (*saddr_cmp)(const struct sock *sk1, const struct sock *sk2))
147 struct hlist_node *node;
148 struct hlist_head *head;
149 struct sock *sk2;
150 int error = 1;
152 write_lock_bh(&udp_hash_lock);
153 if (snum == 0) {
154 int best_size_so_far, best, result, i;
156 if (udp_port_rover > sysctl_local_port_range[1] ||
157 udp_port_rover < sysctl_local_port_range[0])
158 udp_port_rover = sysctl_local_port_range[0];
159 best_size_so_far = 32767;
160 best = result = udp_port_rover;
161 for (i = 0; i < UDP_HTABLE_SIZE; i++, result++) {
162 int size;
164 head = &udp_hash[result & (UDP_HTABLE_SIZE - 1)];
165 if (hlist_empty(head)) {
166 if (result > sysctl_local_port_range[1])
167 result = sysctl_local_port_range[0] +
168 ((result - sysctl_local_port_range[0]) &
169 (UDP_HTABLE_SIZE - 1));
170 goto gotit;
172 size = 0;
173 sk_for_each(sk2, node, head)
174 if (++size < best_size_so_far) {
175 best_size_so_far = size;
176 best = result;
179 result = best;
180 for(i = 0; i < (1 << 16) / UDP_HTABLE_SIZE; i++, result += UDP_HTABLE_SIZE) {
181 if (result > sysctl_local_port_range[1])
182 result = sysctl_local_port_range[0]
183 + ((result - sysctl_local_port_range[0]) &
184 (UDP_HTABLE_SIZE - 1));
185 if (!udp_lport_inuse(result))
186 break;
188 if (i >= (1 << 16) / UDP_HTABLE_SIZE)
189 goto fail;
190 gotit:
191 udp_port_rover = snum = result;
192 } else {
193 head = &udp_hash[snum & (UDP_HTABLE_SIZE - 1)];
195 sk_for_each(sk2, node, head)
196 if (inet_sk(sk2)->num == snum &&
197 sk2 != sk &&
198 (!sk2->sk_reuse || !sk->sk_reuse) &&
199 (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if
200 || sk2->sk_bound_dev_if == sk->sk_bound_dev_if) &&
201 (*saddr_cmp)(sk, sk2) )
202 goto fail;
204 inet_sk(sk)->num = snum;
205 if (sk_unhashed(sk)) {
206 head = &udp_hash[snum & (UDP_HTABLE_SIZE - 1)];
207 sk_add_node(sk, head);
208 sock_prot_inc_use(sk->sk_prot);
210 error = 0;
211 fail:
212 write_unlock_bh(&udp_hash_lock);
213 return error;
216 static inline int ipv4_rcv_saddr_equal(const struct sock *sk1, const struct sock *sk2)
218 struct inet_sock *inet1 = inet_sk(sk1), *inet2 = inet_sk(sk2);
220 return ( !ipv6_only_sock(sk2) &&
221 (!inet1->rcv_saddr || !inet2->rcv_saddr ||
222 inet1->rcv_saddr == inet2->rcv_saddr ));
225 static inline int udp_v4_get_port(struct sock *sk, unsigned short snum)
227 return udp_get_port(sk, snum, ipv4_rcv_saddr_equal);
231 static void udp_v4_hash(struct sock *sk)
233 BUG();
236 static void udp_v4_unhash(struct sock *sk)
238 write_lock_bh(&udp_hash_lock);
239 if (sk_del_node_init(sk)) {
240 inet_sk(sk)->num = 0;
241 sock_prot_dec_use(sk->sk_prot);
243 write_unlock_bh(&udp_hash_lock);
246 /* UDP is nearly always wildcards out the wazoo, it makes no sense to try
247 * harder than this. -DaveM
249 static struct sock *udp_v4_lookup_longway(__be32 saddr, __be16 sport,
250 __be32 daddr, __be16 dport, int dif)
252 struct sock *sk, *result = NULL;
253 struct hlist_node *node;
254 unsigned short hnum = ntohs(dport);
255 int badness = -1;
257 sk_for_each(sk, node, &udp_hash[hnum & (UDP_HTABLE_SIZE - 1)]) {
258 struct inet_sock *inet = inet_sk(sk);
260 if (inet->num == hnum && !ipv6_only_sock(sk)) {
261 int score = (sk->sk_family == PF_INET ? 1 : 0);
262 if (inet->rcv_saddr) {
263 if (inet->rcv_saddr != daddr)
264 continue;
265 score+=2;
267 if (inet->daddr) {
268 if (inet->daddr != saddr)
269 continue;
270 score+=2;
272 if (inet->dport) {
273 if (inet->dport != sport)
274 continue;
275 score+=2;
277 if (sk->sk_bound_dev_if) {
278 if (sk->sk_bound_dev_if != dif)
279 continue;
280 score+=2;
282 if(score == 9) {
283 result = sk;
284 break;
285 } else if(score > badness) {
286 result = sk;
287 badness = score;
291 return result;
294 static __inline__ struct sock *udp_v4_lookup(__be32 saddr, __be16 sport,
295 __be32 daddr, __be16 dport, int dif)
297 struct sock *sk;
299 read_lock(&udp_hash_lock);
300 sk = udp_v4_lookup_longway(saddr, sport, daddr, dport, dif);
301 if (sk)
302 sock_hold(sk);
303 read_unlock(&udp_hash_lock);
304 return sk;
307 static inline struct sock *udp_v4_mcast_next(struct sock *sk,
308 __be16 loc_port, __be32 loc_addr,
309 __be16 rmt_port, __be32 rmt_addr,
310 int dif)
312 struct hlist_node *node;
313 struct sock *s = sk;
314 unsigned short hnum = ntohs(loc_port);
316 sk_for_each_from(s, node) {
317 struct inet_sock *inet = inet_sk(s);
319 if (inet->num != hnum ||
320 (inet->daddr && inet->daddr != rmt_addr) ||
321 (inet->dport != rmt_port && inet->dport) ||
322 (inet->rcv_saddr && inet->rcv_saddr != loc_addr) ||
323 ipv6_only_sock(s) ||
324 (s->sk_bound_dev_if && s->sk_bound_dev_if != dif))
325 continue;
326 if (!ip_mc_sf_allow(s, loc_addr, rmt_addr, dif))
327 continue;
328 goto found;
330 s = NULL;
331 found:
332 return s;
336 * This routine is called by the ICMP module when it gets some
337 * sort of error condition. If err < 0 then the socket should
338 * be closed and the error returned to the user. If err > 0
339 * it's just the icmp type << 8 | icmp code.
340 * Header points to the ip header of the error packet. We move
341 * on past this. Then (as it used to claim before adjustment)
342 * header points to the first 8 bytes of the udp header. We need
343 * to find the appropriate port.
346 void udp_err(struct sk_buff *skb, u32 info)
348 struct inet_sock *inet;
349 struct iphdr *iph = (struct iphdr*)skb->data;
350 struct udphdr *uh = (struct udphdr*)(skb->data+(iph->ihl<<2));
351 int type = skb->h.icmph->type;
352 int code = skb->h.icmph->code;
353 struct sock *sk;
354 int harderr;
355 int err;
357 sk = udp_v4_lookup(iph->daddr, uh->dest, iph->saddr, uh->source, skb->dev->ifindex);
358 if (sk == NULL) {
359 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
360 return; /* No socket for error */
363 err = 0;
364 harderr = 0;
365 inet = inet_sk(sk);
367 switch (type) {
368 default:
369 case ICMP_TIME_EXCEEDED:
370 err = EHOSTUNREACH;
371 break;
372 case ICMP_SOURCE_QUENCH:
373 goto out;
374 case ICMP_PARAMETERPROB:
375 err = EPROTO;
376 harderr = 1;
377 break;
378 case ICMP_DEST_UNREACH:
379 if (code == ICMP_FRAG_NEEDED) { /* Path MTU discovery */
380 if (inet->pmtudisc != IP_PMTUDISC_DONT) {
381 err = EMSGSIZE;
382 harderr = 1;
383 break;
385 goto out;
387 err = EHOSTUNREACH;
388 if (code <= NR_ICMP_UNREACH) {
389 harderr = icmp_err_convert[code].fatal;
390 err = icmp_err_convert[code].errno;
392 break;
396 * RFC1122: OK. Passes ICMP errors back to application, as per
397 * 4.1.3.3.
399 if (!inet->recverr) {
400 if (!harderr || sk->sk_state != TCP_ESTABLISHED)
401 goto out;
402 } else {
403 ip_icmp_error(sk, skb, err, uh->dest, info, (u8*)(uh+1));
405 sk->sk_err = err;
406 sk->sk_error_report(sk);
407 out:
408 sock_put(sk);
412 * Throw away all pending data and cancel the corking. Socket is locked.
414 static void udp_flush_pending_frames(struct sock *sk)
416 struct udp_sock *up = udp_sk(sk);
418 if (up->pending) {
419 up->len = 0;
420 up->pending = 0;
421 ip_flush_pending_frames(sk);
426 * Push out all pending data as one UDP datagram. Socket is locked.
428 static int udp_push_pending_frames(struct sock *sk, struct udp_sock *up)
430 struct inet_sock *inet = inet_sk(sk);
431 struct flowi *fl = &inet->cork.fl;
432 struct sk_buff *skb;
433 struct udphdr *uh;
434 int err = 0;
436 /* Grab the skbuff where UDP header space exists. */
437 if ((skb = skb_peek(&sk->sk_write_queue)) == NULL)
438 goto out;
441 * Create a UDP header
443 uh = skb->h.uh;
444 uh->source = fl->fl_ip_sport;
445 uh->dest = fl->fl_ip_dport;
446 uh->len = htons(up->len);
447 uh->check = 0;
449 if (sk->sk_no_check == UDP_CSUM_NOXMIT) {
450 skb->ip_summed = CHECKSUM_NONE;
451 goto send;
454 if (skb_queue_len(&sk->sk_write_queue) == 1) {
456 * Only one fragment on the socket.
458 if (skb->ip_summed == CHECKSUM_PARTIAL) {
459 skb->csum = offsetof(struct udphdr, check);
460 uh->check = ~csum_tcpudp_magic(fl->fl4_src, fl->fl4_dst,
461 up->len, IPPROTO_UDP, 0);
462 } else {
463 skb->csum = csum_partial((char *)uh,
464 sizeof(struct udphdr), skb->csum);
465 uh->check = csum_tcpudp_magic(fl->fl4_src, fl->fl4_dst,
466 up->len, IPPROTO_UDP, skb->csum);
467 if (uh->check == 0)
468 uh->check = -1;
470 } else {
471 unsigned int csum = 0;
473 * HW-checksum won't work as there are two or more
474 * fragments on the socket so that all csums of sk_buffs
475 * should be together.
477 if (skb->ip_summed == CHECKSUM_PARTIAL) {
478 int offset = (unsigned char *)uh - skb->data;
479 skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
481 skb->ip_summed = CHECKSUM_NONE;
482 } else {
483 skb->csum = csum_partial((char *)uh,
484 sizeof(struct udphdr), skb->csum);
487 skb_queue_walk(&sk->sk_write_queue, skb) {
488 csum = csum_add(csum, skb->csum);
490 uh->check = csum_tcpudp_magic(fl->fl4_src, fl->fl4_dst,
491 up->len, IPPROTO_UDP, csum);
492 if (uh->check == 0)
493 uh->check = -1;
495 send:
496 err = ip_push_pending_frames(sk);
497 out:
498 up->len = 0;
499 up->pending = 0;
500 return err;
504 static unsigned short udp_check(struct udphdr *uh, int len, __be32 saddr, __be32 daddr, unsigned long base)
506 return(csum_tcpudp_magic(saddr, daddr, len, IPPROTO_UDP, base));
509 int udp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
510 size_t len)
512 struct inet_sock *inet = inet_sk(sk);
513 struct udp_sock *up = udp_sk(sk);
514 int ulen = len;
515 struct ipcm_cookie ipc;
516 struct rtable *rt = NULL;
517 int free = 0;
518 int connected = 0;
519 __be32 daddr, faddr, saddr;
520 __be16 dport;
521 u8 tos;
522 int err;
523 int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
525 if (len > 0xFFFF)
526 return -EMSGSIZE;
529 * Check the flags.
532 if (msg->msg_flags&MSG_OOB) /* Mirror BSD error message compatibility */
533 return -EOPNOTSUPP;
535 ipc.opt = NULL;
537 if (up->pending) {
539 * There are pending frames.
540 * The socket lock must be held while it's corked.
542 lock_sock(sk);
543 if (likely(up->pending)) {
544 if (unlikely(up->pending != AF_INET)) {
545 release_sock(sk);
546 return -EINVAL;
548 goto do_append_data;
550 release_sock(sk);
552 ulen += sizeof(struct udphdr);
555 * Get and verify the address.
557 if (msg->msg_name) {
558 struct sockaddr_in * usin = (struct sockaddr_in*)msg->msg_name;
559 if (msg->msg_namelen < sizeof(*usin))
560 return -EINVAL;
561 if (usin->sin_family != AF_INET) {
562 if (usin->sin_family != AF_UNSPEC)
563 return -EAFNOSUPPORT;
566 daddr = usin->sin_addr.s_addr;
567 dport = usin->sin_port;
568 if (dport == 0)
569 return -EINVAL;
570 } else {
571 if (sk->sk_state != TCP_ESTABLISHED)
572 return -EDESTADDRREQ;
573 daddr = inet->daddr;
574 dport = inet->dport;
575 /* Open fast path for connected socket.
576 Route will not be used, if at least one option is set.
578 connected = 1;
580 ipc.addr = inet->saddr;
582 ipc.oif = sk->sk_bound_dev_if;
583 if (msg->msg_controllen) {
584 err = ip_cmsg_send(msg, &ipc);
585 if (err)
586 return err;
587 if (ipc.opt)
588 free = 1;
589 connected = 0;
591 if (!ipc.opt)
592 ipc.opt = inet->opt;
594 saddr = ipc.addr;
595 ipc.addr = faddr = daddr;
597 if (ipc.opt && ipc.opt->srr) {
598 if (!daddr)
599 return -EINVAL;
600 faddr = ipc.opt->faddr;
601 connected = 0;
603 tos = RT_TOS(inet->tos);
604 if (sock_flag(sk, SOCK_LOCALROUTE) ||
605 (msg->msg_flags & MSG_DONTROUTE) ||
606 (ipc.opt && ipc.opt->is_strictroute)) {
607 tos |= RTO_ONLINK;
608 connected = 0;
611 if (MULTICAST(daddr)) {
612 if (!ipc.oif)
613 ipc.oif = inet->mc_index;
614 if (!saddr)
615 saddr = inet->mc_addr;
616 connected = 0;
619 if (connected)
620 rt = (struct rtable*)sk_dst_check(sk, 0);
622 if (rt == NULL) {
623 struct flowi fl = { .oif = ipc.oif,
624 .nl_u = { .ip4_u =
625 { .daddr = faddr,
626 .saddr = saddr,
627 .tos = tos } },
628 .proto = IPPROTO_UDP,
629 .uli_u = { .ports =
630 { .sport = inet->sport,
631 .dport = dport } } };
632 security_sk_classify_flow(sk, &fl);
633 err = ip_route_output_flow(&rt, &fl, sk, !(msg->msg_flags&MSG_DONTWAIT));
634 if (err)
635 goto out;
637 err = -EACCES;
638 if ((rt->rt_flags & RTCF_BROADCAST) &&
639 !sock_flag(sk, SOCK_BROADCAST))
640 goto out;
641 if (connected)
642 sk_dst_set(sk, dst_clone(&rt->u.dst));
645 if (msg->msg_flags&MSG_CONFIRM)
646 goto do_confirm;
647 back_from_confirm:
649 saddr = rt->rt_src;
650 if (!ipc.addr)
651 daddr = ipc.addr = rt->rt_dst;
653 lock_sock(sk);
654 if (unlikely(up->pending)) {
655 /* The socket is already corked while preparing it. */
656 /* ... which is an evident application bug. --ANK */
657 release_sock(sk);
659 LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 2\n");
660 err = -EINVAL;
661 goto out;
664 * Now cork the socket to pend data.
666 inet->cork.fl.fl4_dst = daddr;
667 inet->cork.fl.fl_ip_dport = dport;
668 inet->cork.fl.fl4_src = saddr;
669 inet->cork.fl.fl_ip_sport = inet->sport;
670 up->pending = AF_INET;
672 do_append_data:
673 up->len += ulen;
674 err = ip_append_data(sk, ip_generic_getfrag, msg->msg_iov, ulen,
675 sizeof(struct udphdr), &ipc, rt,
676 corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags);
677 if (err)
678 udp_flush_pending_frames(sk);
679 else if (!corkreq)
680 err = udp_push_pending_frames(sk, up);
681 else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
682 up->pending = 0;
683 release_sock(sk);
685 out:
686 ip_rt_put(rt);
687 if (free)
688 kfree(ipc.opt);
689 if (!err) {
690 UDP_INC_STATS_USER(UDP_MIB_OUTDATAGRAMS);
691 return len;
694 * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting
695 * ENOBUFS might not be good (it's not tunable per se), but otherwise
696 * we don't have a good statistic (IpOutDiscards but it can be too many
697 * things). We could add another new stat but at least for now that
698 * seems like overkill.
700 if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
701 UDP_INC_STATS_USER(UDP_MIB_SNDBUFERRORS);
703 return err;
705 do_confirm:
706 dst_confirm(&rt->u.dst);
707 if (!(msg->msg_flags&MSG_PROBE) || len)
708 goto back_from_confirm;
709 err = 0;
710 goto out;
713 static int udp_sendpage(struct sock *sk, struct page *page, int offset,
714 size_t size, int flags)
716 struct udp_sock *up = udp_sk(sk);
717 int ret;
719 if (!up->pending) {
720 struct msghdr msg = { .msg_flags = flags|MSG_MORE };
722 /* Call udp_sendmsg to specify destination address which
723 * sendpage interface can't pass.
724 * This will succeed only when the socket is connected.
726 ret = udp_sendmsg(NULL, sk, &msg, 0);
727 if (ret < 0)
728 return ret;
731 lock_sock(sk);
733 if (unlikely(!up->pending)) {
734 release_sock(sk);
736 LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 3\n");
737 return -EINVAL;
740 ret = ip_append_page(sk, page, offset, size, flags);
741 if (ret == -EOPNOTSUPP) {
742 release_sock(sk);
743 return sock_no_sendpage(sk->sk_socket, page, offset,
744 size, flags);
746 if (ret < 0) {
747 udp_flush_pending_frames(sk);
748 goto out;
751 up->len += size;
752 if (!(up->corkflag || (flags&MSG_MORE)))
753 ret = udp_push_pending_frames(sk, up);
754 if (!ret)
755 ret = size;
756 out:
757 release_sock(sk);
758 return ret;
762 * IOCTL requests applicable to the UDP protocol
765 int udp_ioctl(struct sock *sk, int cmd, unsigned long arg)
767 switch(cmd)
769 case SIOCOUTQ:
771 int amount = atomic_read(&sk->sk_wmem_alloc);
772 return put_user(amount, (int __user *)arg);
775 case SIOCINQ:
777 struct sk_buff *skb;
778 unsigned long amount;
780 amount = 0;
781 spin_lock_bh(&sk->sk_receive_queue.lock);
782 skb = skb_peek(&sk->sk_receive_queue);
783 if (skb != NULL) {
785 * We will only return the amount
786 * of this packet since that is all
787 * that will be read.
789 amount = skb->len - sizeof(struct udphdr);
791 spin_unlock_bh(&sk->sk_receive_queue.lock);
792 return put_user(amount, (int __user *)arg);
795 default:
796 return -ENOIOCTLCMD;
798 return(0);
801 static __inline__ int __udp_checksum_complete(struct sk_buff *skb)
803 return __skb_checksum_complete(skb);
806 static __inline__ int udp_checksum_complete(struct sk_buff *skb)
808 return skb->ip_summed != CHECKSUM_UNNECESSARY &&
809 __udp_checksum_complete(skb);
813 * This should be easy, if there is something there we
814 * return it, otherwise we block.
817 static int udp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
818 size_t len, int noblock, int flags, int *addr_len)
820 struct inet_sock *inet = inet_sk(sk);
821 struct sockaddr_in *sin = (struct sockaddr_in *)msg->msg_name;
822 struct sk_buff *skb;
823 int copied, err;
826 * Check any passed addresses
828 if (addr_len)
829 *addr_len=sizeof(*sin);
831 if (flags & MSG_ERRQUEUE)
832 return ip_recv_error(sk, msg, len);
834 try_again:
835 skb = skb_recv_datagram(sk, flags, noblock, &err);
836 if (!skb)
837 goto out;
839 copied = skb->len - sizeof(struct udphdr);
840 if (copied > len) {
841 copied = len;
842 msg->msg_flags |= MSG_TRUNC;
845 if (skb->ip_summed==CHECKSUM_UNNECESSARY) {
846 err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov,
847 copied);
848 } else if (msg->msg_flags&MSG_TRUNC) {
849 if (__udp_checksum_complete(skb))
850 goto csum_copy_err;
851 err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov,
852 copied);
853 } else {
854 err = skb_copy_and_csum_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov);
856 if (err == -EINVAL)
857 goto csum_copy_err;
860 if (err)
861 goto out_free;
863 sock_recv_timestamp(msg, sk, skb);
865 /* Copy the address. */
866 if (sin)
868 sin->sin_family = AF_INET;
869 sin->sin_port = skb->h.uh->source;
870 sin->sin_addr.s_addr = skb->nh.iph->saddr;
871 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
873 if (inet->cmsg_flags)
874 ip_cmsg_recv(msg, skb);
876 err = copied;
877 if (flags & MSG_TRUNC)
878 err = skb->len - sizeof(struct udphdr);
880 out_free:
881 skb_free_datagram(sk, skb);
882 out:
883 return err;
885 csum_copy_err:
886 UDP_INC_STATS_BH(UDP_MIB_INERRORS);
888 skb_kill_datagram(sk, skb, flags);
890 if (noblock)
891 return -EAGAIN;
892 goto try_again;
896 int udp_disconnect(struct sock *sk, int flags)
898 struct inet_sock *inet = inet_sk(sk);
900 * 1003.1g - break association.
903 sk->sk_state = TCP_CLOSE;
904 inet->daddr = 0;
905 inet->dport = 0;
906 sk->sk_bound_dev_if = 0;
907 if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK))
908 inet_reset_saddr(sk);
910 if (!(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) {
911 sk->sk_prot->unhash(sk);
912 inet->sport = 0;
914 sk_dst_reset(sk);
915 return 0;
918 static void udp_close(struct sock *sk, long timeout)
920 sk_common_release(sk);
923 #if defined(CONFIG_XFRM) || defined(CONFIG_IPSEC_NAT_TRAVERSAL)
925 /* if XFRM isn't a module, then register it directly. */
926 #if 0 && !defined(CONFIG_XFRM_MODULE) && !defined(CONFIG_IPSEC_NAT_TRAVERSAL)
927 static xfrm4_rcv_encap_t xfrm4_rcv_encap_func = xfrm4_rcv_encap;
928 #else
929 static xfrm4_rcv_encap_t xfrm4_rcv_encap_func = NULL;
930 #endif
932 int udp4_register_esp_rcvencap(xfrm4_rcv_encap_t func
933 , xfrm4_rcv_encap_t *oldfunc)
935 if(oldfunc != NULL) {
936 *oldfunc = xfrm4_rcv_encap_func;
939 #if 0
940 if(xfrm4_rcv_encap_func != NULL)
941 return -1;
942 #endif
944 xfrm4_rcv_encap_func = func;
945 return 0;
948 int udp4_unregister_esp_rcvencap(xfrm4_rcv_encap_t func)
950 if(xfrm4_rcv_encap_func != func)
951 return -1;
953 xfrm4_rcv_encap_func = NULL;
954 return 0;
956 #endif /* CONFIG_XFRM_MODULE || CONFIG_IPSEC_NAT_TRAVERSAL */
959 /* return:
960 * 1 if the the UDP system should process it
961 * 0 if we should drop this packet
962 * -1 if it should get processed by xfrm4_rcv_encap
964 static int udp_encap_rcv(struct sock * sk, struct sk_buff *skb)
966 #if !defined(CONFIG_XFRM) && !defined(CONFIG_IPSEC_NAT_TRAVERSAL)
967 return 1;
968 #else /* either CONFIG_XFRM or CONFIG_IPSEC_NAT_TRAVERSAL */
969 struct udp_sock *up = udp_sk(sk);
970 struct udphdr *uh;
971 struct iphdr *iph;
972 int iphlen, len;
974 __u8 *udpdata;
975 __be32 *udpdata32;
976 __u16 encap_type = up->encap_type;
978 /* if we're overly short, let UDP handle it */
979 len = skb->len - sizeof(struct udphdr);
980 if (len <= 0)
981 return 1;
983 /* if this is not encapsulated socket, then just return now */
984 if (!encap_type)
985 return 3;
987 /* If this is a paged skb, make sure we pull up
988 * whatever data we need to look at. */
989 if (!pskb_may_pull(skb, sizeof(struct udphdr) + min(len, 8)))
990 return 1;
992 /* Now we can get the pointers */
993 uh = skb->h.uh;
994 udpdata = (__u8 *)uh + sizeof(struct udphdr);
995 udpdata32 = (__be32 *)udpdata;
997 switch (encap_type) {
998 default:
999 case UDP_ENCAP_ESPINUDP:
1000 /* Check if this is a keepalive packet. If so, eat it. */
1001 if (len == 1 && udpdata[0] == 0xff) {
1002 return 0;
1003 } else if (len > sizeof(struct ip_esp_hdr) && udpdata32[0] != 0 ) {
1004 /* ESP Packet without Non-ESP header */
1005 len = sizeof(struct udphdr);
1006 } else
1007 /* Must be an IKE packet.. pass it through */
1008 return 4;
1009 break;
1010 case UDP_ENCAP_ESPINUDP_NON_IKE:
1011 /* Check if this is a keepalive packet. If so, eat it. */
1012 if (len == 1 && udpdata[0] == 0xff) {
1013 return 0;
1014 } else if (len > 2 * sizeof(u32) + sizeof(struct ip_esp_hdr) &&
1015 udpdata32[0] == 0 && udpdata32[1] == 0) {
1017 /* ESP Packet with Non-IKE marker */
1018 len = sizeof(struct udphdr) + 2 * sizeof(u32);
1019 } else
1020 /* Must be an IKE packet.. pass it through */
1021 return 5;
1022 break;
1025 /* At this point we are sure that this is an ESPinUDP packet,
1026 * so we need to remove 'len' bytes from the packet (the UDP
1027 * header and optional ESP marker bytes) and then modify the
1028 * protocol to ESP, and then call into the transform receiver.
1030 if (skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
1031 return 0;
1032 if (skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
1033 return 0;
1035 /* Now we can update and verify the packet length... */
1036 iph = skb->nh.iph;
1037 iphlen = iph->ihl << 2;
1038 iph->tot_len = htons(ntohs(iph->tot_len) - len);
1039 if (skb->len < iphlen + len) {
1040 /* packet is too small!?! */
1041 return 0;
1044 /* pull the data buffer up to the ESP header and set the
1045 * transport header to point to ESP. Keep UDP on the stack
1046 * for later.
1048 skb->h.raw = skb_pull(skb, len);
1050 /* modify the protocol (it's ESP!) */
1051 iph->protocol = IPPROTO_ESP;
1053 /* and let the caller know to send this into the ESP processor... */
1054 return -1;
1055 #endif
1058 /* returns:
1059 * -1: error
1060 * 0: success
1061 * >0: "udp encap" protocol resubmission
1063 * Note that in the success and error cases, the skb is assumed to
1064 * have either been requeued or freed.
1066 static int udp_queue_rcv_skb(struct sock * sk, struct sk_buff *skb)
1068 struct udp_sock *up = udp_sk(sk);
1069 int rc;
1072 * Charge it to the socket, dropping if the queue is full.
1074 if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb)) {
1075 kfree_skb(skb);
1076 return -1;
1078 nf_reset(skb);
1080 if (up->encap_type) {
1082 * This is an encapsulation socket, so let's see if this is
1083 * an encapsulated packet.
1084 * If it's a keepalive packet, then just eat it.
1085 * If it's an encapsulateed packet, then pass it to the
1086 * IPsec xfrm input and return the response
1087 * appropriately. Otherwise, just fall through and
1088 * pass this up the UDP socket.
1090 int ret;
1092 ret = udp_encap_rcv(sk, skb);
1093 if (ret == 0) {
1094 /* Eat the packet .. */
1095 kfree_skb(skb);
1096 return 0;
1098 if (ret < 0) {
1099 if(xfrm4_rcv_encap_func != NULL) {
1100 ret = (*xfrm4_rcv_encap_func)(skb, up->encap_type);
1101 UDP_INC_STATS_BH(UDP_MIB_INDATAGRAMS);
1102 } else {
1103 UDP_INC_STATS_BH(UDP_MIB_INERRORS);
1104 ret = 1;
1106 return -ret;
1108 /* FALLTHROUGH -- it's a UDP Packet */
1111 if (sk->sk_filter && skb->ip_summed != CHECKSUM_UNNECESSARY) {
1112 if (__udp_checksum_complete(skb)) {
1113 UDP_INC_STATS_BH(UDP_MIB_INERRORS);
1114 kfree_skb(skb);
1115 return -1;
1117 skb->ip_summed = CHECKSUM_UNNECESSARY;
1120 if ((rc = sock_queue_rcv_skb(sk,skb)) < 0) {
1121 /* Note that an ENOMEM error is charged twice */
1122 if (rc == -ENOMEM)
1123 UDP_INC_STATS_BH(UDP_MIB_RCVBUFERRORS);
1124 UDP_INC_STATS_BH(UDP_MIB_INERRORS);
1125 kfree_skb(skb);
1126 return -1;
1128 UDP_INC_STATS_BH(UDP_MIB_INDATAGRAMS);
1129 return 0;
1133 * Multicasts and broadcasts go to each listener.
1135 * Note: called only from the BH handler context,
1136 * so we don't need to lock the hashes.
1138 static int udp_v4_mcast_deliver(struct sk_buff *skb, struct udphdr *uh,
1139 __be32 saddr, __be32 daddr)
1141 struct sock *sk;
1142 int dif;
1144 read_lock(&udp_hash_lock);
1145 sk = sk_head(&udp_hash[ntohs(uh->dest) & (UDP_HTABLE_SIZE - 1)]);
1146 dif = skb->dev->ifindex;
1147 sk = udp_v4_mcast_next(sk, uh->dest, daddr, uh->source, saddr, dif);
1148 if (sk) {
1149 struct sock *sknext = NULL;
1151 do {
1152 struct sk_buff *skb1 = skb;
1154 sknext = udp_v4_mcast_next(sk_next(sk), uh->dest, daddr,
1155 uh->source, saddr, dif);
1156 if(sknext)
1157 skb1 = skb_clone(skb, GFP_ATOMIC);
1159 if(skb1) {
1160 int ret = udp_queue_rcv_skb(sk, skb1);
1161 if (ret > 0)
1162 /* we should probably re-process instead
1163 * of dropping packets here. */
1164 kfree_skb(skb1);
1166 sk = sknext;
1167 } while(sknext);
1168 } else
1169 kfree_skb(skb);
1170 read_unlock(&udp_hash_lock);
1171 return 0;
1174 /* Initialize UDP checksum. If exited with zero value (success),
1175 * CHECKSUM_UNNECESSARY means, that no more checks are required.
1176 * Otherwise, csum completion requires chacksumming packet body,
1177 * including udp header and folding it to skb->csum.
1179 static void udp_checksum_init(struct sk_buff *skb, struct udphdr *uh,
1180 unsigned short ulen, __be32 saddr, __be32 daddr)
1182 if (uh->check == 0) {
1183 skb->ip_summed = CHECKSUM_UNNECESSARY;
1184 } else if (skb->ip_summed == CHECKSUM_COMPLETE) {
1185 if (!udp_check(uh, ulen, saddr, daddr, skb->csum))
1186 skb->ip_summed = CHECKSUM_UNNECESSARY;
1188 if (skb->ip_summed != CHECKSUM_UNNECESSARY)
1189 skb->csum = csum_tcpudp_nofold(saddr, daddr, ulen, IPPROTO_UDP, 0);
1190 /* Probably, we should checksum udp header (it should be in cache
1191 * in any case) and data in tiny packets (< rx copybreak).
1196 * All we need to do is get the socket, and then do a checksum.
1199 int udp_rcv(struct sk_buff *skb)
1201 struct sock *sk;
1202 struct udphdr *uh;
1203 unsigned short ulen;
1204 struct rtable *rt = (struct rtable*)skb->dst;
1205 __be32 saddr = skb->nh.iph->saddr;
1206 __be32 daddr = skb->nh.iph->daddr;
1207 int len = skb->len;
1210 * Validate the packet and the UDP length.
1212 if (!pskb_may_pull(skb, sizeof(struct udphdr)))
1213 goto no_header;
1215 uh = skb->h.uh;
1217 ulen = ntohs(uh->len);
1219 if (ulen > len || ulen < sizeof(*uh))
1220 goto short_packet;
1222 if (pskb_trim_rcsum(skb, ulen))
1223 goto short_packet;
1225 udp_checksum_init(skb, uh, ulen, saddr, daddr);
1227 if(rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST))
1228 return udp_v4_mcast_deliver(skb, uh, saddr, daddr);
1230 sk = udp_v4_lookup(saddr, uh->source, daddr, uh->dest, skb->dev->ifindex);
1232 if (sk != NULL) {
1233 int ret = udp_queue_rcv_skb(sk, skb);
1234 sock_put(sk);
1236 /* a return value > 0 means to resubmit the input, but
1237 * it it wants the return to be -protocol, or 0
1239 if (ret > 0)
1240 return -ret;
1241 return 0;
1244 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
1245 goto drop;
1246 nf_reset(skb);
1248 /* No socket. Drop packet silently, if checksum is wrong */
1249 if (udp_checksum_complete(skb))
1250 goto csum_error;
1252 UDP_INC_STATS_BH(UDP_MIB_NOPORTS);
1253 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
1256 * Hmm. We got an UDP packet to a port to which we
1257 * don't wanna listen. Ignore it.
1259 kfree_skb(skb);
1260 return(0);
1262 short_packet:
1263 LIMIT_NETDEBUG(KERN_DEBUG "UDP: short packet: From %u.%u.%u.%u:%u %d/%d to %u.%u.%u.%u:%u\n",
1264 NIPQUAD(saddr),
1265 ntohs(uh->source),
1266 ulen,
1267 len,
1268 NIPQUAD(daddr),
1269 ntohs(uh->dest));
1270 no_header:
1271 UDP_INC_STATS_BH(UDP_MIB_INERRORS);
1272 kfree_skb(skb);
1273 return(0);
1275 csum_error:
1277 * RFC1122: OK. Discards the bad packet silently (as far as
1278 * the network is concerned, anyway) as per 4.1.3.4 (MUST).
1280 LIMIT_NETDEBUG(KERN_DEBUG "UDP: bad checksum. From %d.%d.%d.%d:%d to %d.%d.%d.%d:%d ulen %d\n",
1281 NIPQUAD(saddr),
1282 ntohs(uh->source),
1283 NIPQUAD(daddr),
1284 ntohs(uh->dest),
1285 ulen);
1286 drop:
1287 UDP_INC_STATS_BH(UDP_MIB_INERRORS);
1288 kfree_skb(skb);
1289 return(0);
1292 static int udp_destroy_sock(struct sock *sk)
1294 lock_sock(sk);
1295 udp_flush_pending_frames(sk);
1296 release_sock(sk);
1297 return 0;
1301 * Socket option code for UDP
1303 static int do_udp_setsockopt(struct sock *sk, int level, int optname,
1304 char __user *optval, int optlen)
1306 struct udp_sock *up = udp_sk(sk);
1307 int val;
1308 int err = 0;
1310 if(optlen<sizeof(int))
1311 return -EINVAL;
1313 if (get_user(val, (int __user *)optval))
1314 return -EFAULT;
1316 switch(optname) {
1317 case UDP_CORK:
1318 if (val != 0) {
1319 up->corkflag = 1;
1320 } else {
1321 up->corkflag = 0;
1322 lock_sock(sk);
1323 udp_push_pending_frames(sk, up);
1324 release_sock(sk);
1326 break;
1328 case UDP_ENCAP:
1329 switch (val) {
1330 case 0:
1331 case UDP_ENCAP_ESPINUDP:
1332 case UDP_ENCAP_ESPINUDP_NON_IKE:
1333 up->encap_type = val;
1334 break;
1335 default:
1336 err = -ENOPROTOOPT;
1337 break;
1339 break;
1341 default:
1342 err = -ENOPROTOOPT;
1343 break;
1346 return err;
1349 static int udp_setsockopt(struct sock *sk, int level, int optname,
1350 char __user *optval, int optlen)
1352 if (level != SOL_UDP)
1353 return ip_setsockopt(sk, level, optname, optval, optlen);
1354 return do_udp_setsockopt(sk, level, optname, optval, optlen);
1357 #ifdef CONFIG_COMPAT
1358 static int compat_udp_setsockopt(struct sock *sk, int level, int optname,
1359 char __user *optval, int optlen)
1361 if (level != SOL_UDP)
1362 return compat_ip_setsockopt(sk, level, optname, optval, optlen);
1363 return do_udp_setsockopt(sk, level, optname, optval, optlen);
1365 #endif
1367 static int do_udp_getsockopt(struct sock *sk, int level, int optname,
1368 char __user *optval, int __user *optlen)
1370 struct udp_sock *up = udp_sk(sk);
1371 int val, len;
1373 if(get_user(len,optlen))
1374 return -EFAULT;
1376 len = min_t(unsigned int, len, sizeof(int));
1378 if(len < 0)
1379 return -EINVAL;
1381 switch(optname) {
1382 case UDP_CORK:
1383 val = up->corkflag;
1384 break;
1386 case UDP_ENCAP:
1387 val = up->encap_type;
1388 break;
1390 default:
1391 return -ENOPROTOOPT;
1394 if(put_user(len, optlen))
1395 return -EFAULT;
1396 if(copy_to_user(optval, &val,len))
1397 return -EFAULT;
1398 return 0;
1401 static int udp_getsockopt(struct sock *sk, int level, int optname,
1402 char __user *optval, int __user *optlen)
1404 if (level != SOL_UDP)
1405 return ip_getsockopt(sk, level, optname, optval, optlen);
1406 return do_udp_getsockopt(sk, level, optname, optval, optlen);
1409 #ifdef CONFIG_COMPAT
1410 static int compat_udp_getsockopt(struct sock *sk, int level, int optname,
1411 char __user *optval, int __user *optlen)
1413 if (level != SOL_UDP)
1414 return compat_ip_getsockopt(sk, level, optname, optval, optlen);
1415 return do_udp_getsockopt(sk, level, optname, optval, optlen);
1417 #endif
1419 * udp_poll - wait for a UDP event.
1420 * @file - file struct
1421 * @sock - socket
1422 * @wait - poll table
1424 * This is same as datagram poll, except for the special case of
1425 * blocking sockets. If application is using a blocking fd
1426 * and a packet with checksum error is in the queue;
1427 * then it could get return from select indicating data available
1428 * but then block when reading it. Add special case code
1429 * to work around these arguably broken applications.
1431 unsigned int udp_poll(struct file *file, struct socket *sock, poll_table *wait)
1433 unsigned int mask = datagram_poll(file, sock, wait);
1434 struct sock *sk = sock->sk;
1436 /* Check for false positives due to checksum errors */
1437 if ( (mask & POLLRDNORM) &&
1438 !(file->f_flags & O_NONBLOCK) &&
1439 !(sk->sk_shutdown & RCV_SHUTDOWN)){
1440 struct sk_buff_head *rcvq = &sk->sk_receive_queue;
1441 struct sk_buff *skb;
1443 spin_lock_bh(&rcvq->lock);
1444 while ((skb = skb_peek(rcvq)) != NULL) {
1445 if (udp_checksum_complete(skb)) {
1446 UDP_INC_STATS_BH(UDP_MIB_INERRORS);
1447 __skb_unlink(skb, rcvq);
1448 kfree_skb(skb);
1449 } else {
1450 skb->ip_summed = CHECKSUM_UNNECESSARY;
1451 break;
1454 spin_unlock_bh(&rcvq->lock);
1456 /* nothing to see, move along */
1457 if (skb == NULL)
1458 mask &= ~(POLLIN | POLLRDNORM);
1461 return mask;
1465 struct proto udp_prot = {
1466 .name = "UDP",
1467 .owner = THIS_MODULE,
1468 .close = udp_close,
1469 .connect = ip4_datagram_connect,
1470 .disconnect = udp_disconnect,
1471 .ioctl = udp_ioctl,
1472 .destroy = udp_destroy_sock,
1473 .setsockopt = udp_setsockopt,
1474 .getsockopt = udp_getsockopt,
1475 .sendmsg = udp_sendmsg,
1476 .recvmsg = udp_recvmsg,
1477 .sendpage = udp_sendpage,
1478 .backlog_rcv = udp_queue_rcv_skb,
1479 .hash = udp_v4_hash,
1480 .unhash = udp_v4_unhash,
1481 .get_port = udp_v4_get_port,
1482 .obj_size = sizeof(struct udp_sock),
1483 #ifdef CONFIG_COMPAT
1484 .compat_setsockopt = compat_udp_setsockopt,
1485 .compat_getsockopt = compat_udp_getsockopt,
1486 #endif
1489 /* ------------------------------------------------------------------------ */
1490 #ifdef CONFIG_PROC_FS
1492 static struct sock *udp_get_first(struct seq_file *seq)
1494 struct sock *sk;
1495 struct udp_iter_state *state = seq->private;
1497 for (state->bucket = 0; state->bucket < UDP_HTABLE_SIZE; ++state->bucket) {
1498 struct hlist_node *node;
1499 sk_for_each(sk, node, &udp_hash[state->bucket]) {
1500 if (sk->sk_family == state->family)
1501 goto found;
1504 sk = NULL;
1505 found:
1506 return sk;
1509 static struct sock *udp_get_next(struct seq_file *seq, struct sock *sk)
1511 struct udp_iter_state *state = seq->private;
1513 do {
1514 sk = sk_next(sk);
1515 try_again:
1517 } while (sk && sk->sk_family != state->family);
1519 if (!sk && ++state->bucket < UDP_HTABLE_SIZE) {
1520 sk = sk_head(&udp_hash[state->bucket]);
1521 goto try_again;
1523 return sk;
1526 static struct sock *udp_get_idx(struct seq_file *seq, loff_t pos)
1528 struct sock *sk = udp_get_first(seq);
1530 if (sk)
1531 while(pos && (sk = udp_get_next(seq, sk)) != NULL)
1532 --pos;
1533 return pos ? NULL : sk;
1536 static void *udp_seq_start(struct seq_file *seq, loff_t *pos)
1538 read_lock(&udp_hash_lock);
1539 return *pos ? udp_get_idx(seq, *pos-1) : (void *)1;
1542 static void *udp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1544 struct sock *sk;
1546 if (v == (void *)1)
1547 sk = udp_get_idx(seq, 0);
1548 else
1549 sk = udp_get_next(seq, v);
1551 ++*pos;
1552 return sk;
1555 static void udp_seq_stop(struct seq_file *seq, void *v)
1557 read_unlock(&udp_hash_lock);
1560 static int udp_seq_open(struct inode *inode, struct file *file)
1562 struct udp_seq_afinfo *afinfo = PDE(inode)->data;
1563 struct seq_file *seq;
1564 int rc = -ENOMEM;
1565 struct udp_iter_state *s = kzalloc(sizeof(*s), GFP_KERNEL);
1567 if (!s)
1568 goto out;
1569 s->family = afinfo->family;
1570 s->seq_ops.start = udp_seq_start;
1571 s->seq_ops.next = udp_seq_next;
1572 s->seq_ops.show = afinfo->seq_show;
1573 s->seq_ops.stop = udp_seq_stop;
1575 rc = seq_open(file, &s->seq_ops);
1576 if (rc)
1577 goto out_kfree;
1579 seq = file->private_data;
1580 seq->private = s;
1581 out:
1582 return rc;
1583 out_kfree:
1584 kfree(s);
1585 goto out;
1588 /* ------------------------------------------------------------------------ */
1589 int udp_proc_register(struct udp_seq_afinfo *afinfo)
1591 struct proc_dir_entry *p;
1592 int rc = 0;
1594 if (!afinfo)
1595 return -EINVAL;
1596 afinfo->seq_fops->owner = afinfo->owner;
1597 afinfo->seq_fops->open = udp_seq_open;
1598 afinfo->seq_fops->read = seq_read;
1599 afinfo->seq_fops->llseek = seq_lseek;
1600 afinfo->seq_fops->release = seq_release_private;
1602 p = proc_net_fops_create(afinfo->name, S_IRUGO, afinfo->seq_fops);
1603 if (p)
1604 p->data = afinfo;
1605 else
1606 rc = -ENOMEM;
1607 return rc;
1610 void udp_proc_unregister(struct udp_seq_afinfo *afinfo)
1612 if (!afinfo)
1613 return;
1614 proc_net_remove(afinfo->name);
1615 memset(afinfo->seq_fops, 0, sizeof(*afinfo->seq_fops));
1618 /* ------------------------------------------------------------------------ */
1619 static void udp4_format_sock(struct sock *sp, char *tmpbuf, int bucket)
1621 struct inet_sock *inet = inet_sk(sp);
1622 __be32 dest = inet->daddr;
1623 __be32 src = inet->rcv_saddr;
1624 __u16 destp = ntohs(inet->dport);
1625 __u16 srcp = ntohs(inet->sport);
1627 sprintf(tmpbuf, "%4d: %08X:%04X %08X:%04X"
1628 " %02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p",
1629 bucket, src, srcp, dest, destp, sp->sk_state,
1630 atomic_read(&sp->sk_wmem_alloc),
1631 atomic_read(&sp->sk_rmem_alloc),
1632 0, 0L, 0, sock_i_uid(sp), 0, sock_i_ino(sp),
1633 atomic_read(&sp->sk_refcnt), sp);
1636 static int udp4_seq_show(struct seq_file *seq, void *v)
1638 if (v == SEQ_START_TOKEN)
1639 seq_printf(seq, "%-127s\n",
1640 " sl local_address rem_address st tx_queue "
1641 "rx_queue tr tm->when retrnsmt uid timeout "
1642 "inode");
1643 else {
1644 char tmpbuf[129];
1645 struct udp_iter_state *state = seq->private;
1647 udp4_format_sock(v, tmpbuf, state->bucket);
1648 seq_printf(seq, "%-127s\n", tmpbuf);
1650 return 0;
1653 /* ------------------------------------------------------------------------ */
1654 static struct file_operations udp4_seq_fops;
1655 static struct udp_seq_afinfo udp4_seq_afinfo = {
1656 .owner = THIS_MODULE,
1657 .name = "udp",
1658 .family = AF_INET,
1659 .seq_show = udp4_seq_show,
1660 .seq_fops = &udp4_seq_fops,
1663 int __init udp4_proc_init(void)
1665 return udp_proc_register(&udp4_seq_afinfo);
1668 void udp4_proc_exit(void)
1670 udp_proc_unregister(&udp4_seq_afinfo);
1672 #endif /* CONFIG_PROC_FS */
1674 EXPORT_SYMBOL(udp_disconnect);
1675 EXPORT_SYMBOL(udp_hash);
1676 EXPORT_SYMBOL(udp_hash_lock);
1677 EXPORT_SYMBOL(udp_ioctl);
1678 EXPORT_SYMBOL(udp_get_port);
1679 EXPORT_SYMBOL(udp_prot);
1680 EXPORT_SYMBOL(udp_sendmsg);
1681 EXPORT_SYMBOL(udp_poll);
1683 #ifdef CONFIG_PROC_FS
1684 EXPORT_SYMBOL(udp_proc_register);
1685 EXPORT_SYMBOL(udp_proc_unregister);
1686 #endif
1688 #if defined(CONFIG_IPSEC_NAT_TRAVERSAL)
1689 EXPORT_SYMBOL(udp4_register_esp_rcvencap);
1690 EXPORT_SYMBOL(udp4_unregister_esp_rcvencap);
1691 #endif