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 $
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>
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
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
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
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>
91 #include <linux/errno.h>
92 #include <linux/timer.h>
94 #include <linux/config.h>
95 #include <linux/inet.h>
96 #include <linux/ipv6.h>
97 #include <linux/netdevice.h>
100 #include <net/tcp_states.h>
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>
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>
114 * Snmp MIB for the UDP layer
117 DEFINE_SNMP_STAT(struct udp_mib, udp_statistics) __read_mostly;
119 struct hlist_head udp_hash[UDP_HTABLE_SIZE];
120 DEFINE_RWLOCK(udp_hash_lock);
122 /* Shared by v4/v6 udp. */
125 static int udp_v4_get_port(struct sock *sk, unsigned short snum)
127 struct hlist_node *node;
129 struct inet_sock *inet = inet_sk(sk);
131 write_lock_bh(&udp_hash_lock);
133 int best_size_so_far, best, result, i;
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;
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));
153 sk_for_each(sk2, node, list)
154 if (++size >= best_size_so_far)
156 best_size_so_far = size;
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))
169 if (i >= (1 << 16) / UDP_HTABLE_SIZE)
172 udp_port_rover = snum = result;
174 sk_for_each(sk2, node,
175 &udp_hash[snum & (UDP_HTABLE_SIZE - 1)]) {
176 struct inet_sock *inet2 = inet_sk(sk2);
178 if (inet2->num == snum &&
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 ||
186 inet2->rcv_saddr == inet->rcv_saddr) &&
187 (!sk2->sk_reuse || !sk->sk_reuse))
192 if (sk_unhashed(sk)) {
193 struct hlist_head *h = &udp_hash[snum & (UDP_HTABLE_SIZE - 1)];
196 sock_prot_inc_use(sk->sk_prot);
198 write_unlock_bh(&udp_hash_lock);
202 write_unlock_bh(&udp_hash_lock);
206 static void udp_v4_hash(struct sock *sk)
211 static void udp_v4_unhash(struct sock *sk)
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);
218 write_unlock_bh(&udp_hash_lock);
221 /* UDP is nearly always wildcards out the wazoo, it makes no sense to try
222 * harder than this. -DaveM
224 static struct sock *udp_v4_lookup_longway(u32 saddr, u16 sport,
225 u32 daddr, u16 dport, int dif)
227 struct sock *sk, *result = NULL;
228 struct hlist_node *node;
229 unsigned short hnum = ntohs(dport);
232 sk_for_each(sk, node, &udp_hash[hnum & (UDP_HTABLE_SIZE - 1)]) {
233 struct inet_sock *inet = inet_sk(sk);
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)
243 if (inet->daddr != saddr)
248 if (inet->dport != sport)
252 if (sk->sk_bound_dev_if) {
253 if (sk->sk_bound_dev_if != dif)
260 } else if(score > badness) {
269 static __inline__ struct sock *udp_v4_lookup(u32 saddr, u16 sport,
270 u32 daddr, u16 dport, int dif)
274 read_lock(&udp_hash_lock);
275 sk = udp_v4_lookup_longway(saddr, sport, daddr, dport, dif);
278 read_unlock(&udp_hash_lock);
282 static inline struct sock *udp_v4_mcast_next(struct sock *sk,
283 u16 loc_port, u32 loc_addr,
284 u16 rmt_port, u32 rmt_addr,
287 struct hlist_node *node;
289 unsigned short hnum = ntohs(loc_port);
291 sk_for_each_from(s, node) {
292 struct inet_sock *inet = inet_sk(s);
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) ||
299 (s->sk_bound_dev_if && s->sk_bound_dev_if != dif))
301 if (!ip_mc_sf_allow(s, loc_addr, rmt_addr, dif))
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.
321 void udp_err(struct sk_buff *skb, u32 info)
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;
332 sk = udp_v4_lookup(iph->daddr, uh->dest, iph->saddr, uh->source, skb->dev->ifindex);
334 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
335 return; /* No socket for error */
344 case ICMP_TIME_EXCEEDED:
347 case ICMP_SOURCE_QUENCH:
349 case ICMP_PARAMETERPROB:
353 case ICMP_DEST_UNREACH:
354 if (code == ICMP_FRAG_NEEDED) { /* Path MTU discovery */
355 if (inet->pmtudisc != IP_PMTUDISC_DONT) {
363 if (code <= NR_ICMP_UNREACH) {
364 harderr = icmp_err_convert[code].fatal;
365 err = icmp_err_convert[code].errno;
371 * RFC1122: OK. Passes ICMP errors back to application, as per
374 if (!inet->recverr) {
375 if (!harderr || sk->sk_state != TCP_ESTABLISHED)
378 ip_icmp_error(sk, skb, err, uh->dest, info, (u8*)(uh+1));
381 sk->sk_error_report(sk);
387 * Throw away all pending data and cancel the corking. Socket is locked.
389 static void udp_flush_pending_frames(struct sock *sk)
391 struct udp_sock *up = udp_sk(sk);
396 ip_flush_pending_frames(sk);
401 * Push out all pending data as one UDP datagram. Socket is locked.
403 static int udp_push_pending_frames(struct sock *sk, struct udp_sock *up)
405 struct inet_sock *inet = inet_sk(sk);
406 struct flowi *fl = &inet->cork.fl;
411 /* Grab the skbuff where UDP header space exists. */
412 if ((skb = skb_peek(&sk->sk_write_queue)) == NULL)
416 * Create a UDP header
419 uh->source = fl->fl_ip_sport;
420 uh->dest = fl->fl_ip_dport;
421 uh->len = htons(up->len);
424 if (sk->sk_no_check == UDP_CSUM_NOXMIT) {
425 skb->ip_summed = CHECKSUM_NONE;
429 if (skb_queue_len(&sk->sk_write_queue) == 1) {
431 * Only one fragment on the socket.
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);
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);
446 unsigned int csum = 0;
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.
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);
456 skb->ip_summed = CHECKSUM_NONE;
458 skb->csum = csum_partial((char *)uh,
459 sizeof(struct udphdr), skb->csum);
462 skb_queue_walk(&sk->sk_write_queue, skb) {
463 csum = csum_add(csum, skb->csum);
465 uh->check = csum_tcpudp_magic(fl->fl4_src, fl->fl4_dst,
466 up->len, IPPROTO_UDP, csum);
471 err = ip_push_pending_frames(sk);
479 static unsigned short udp_check(struct udphdr *uh, int len, unsigned long saddr, unsigned long daddr, unsigned long base)
481 return(csum_tcpudp_magic(saddr, daddr, len, IPPROTO_UDP, base));
484 int udp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
487 struct inet_sock *inet = inet_sk(sk);
488 struct udp_sock *up = udp_sk(sk);
490 struct ipcm_cookie ipc;
491 struct rtable *rt = NULL;
494 u32 daddr, faddr, saddr;
498 int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
507 if (msg->msg_flags&MSG_OOB) /* Mirror BSD error message compatibility */
514 * There are pending frames.
515 * The socket lock must be held while it's corked.
518 if (likely(up->pending)) {
519 if (unlikely(up->pending != AF_INET)) {
527 ulen += sizeof(struct udphdr);
530 * Get and verify the address.
533 struct sockaddr_in * usin = (struct sockaddr_in*)msg->msg_name;
534 if (msg->msg_namelen < sizeof(*usin))
536 if (usin->sin_family != AF_INET) {
537 if (usin->sin_family != AF_UNSPEC)
538 return -EAFNOSUPPORT;
541 daddr = usin->sin_addr.s_addr;
542 dport = usin->sin_port;
546 if (sk->sk_state != TCP_ESTABLISHED)
547 return -EDESTADDRREQ;
550 /* Open fast path for connected socket.
551 Route will not be used, if at least one option is set.
555 ipc.addr = inet->saddr;
557 ipc.oif = sk->sk_bound_dev_if;
558 if (msg->msg_controllen) {
559 err = ip_cmsg_send(msg, &ipc);
570 ipc.addr = faddr = daddr;
572 if (ipc.opt && ipc.opt->srr) {
575 faddr = ipc.opt->faddr;
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)) {
586 if (MULTICAST(daddr)) {
588 ipc.oif = inet->mc_index;
590 saddr = inet->mc_addr;
595 rt = (struct rtable*)sk_dst_check(sk, 0);
598 struct flowi fl = { .oif = ipc.oif,
603 .proto = IPPROTO_UDP,
605 { .sport = inet->sport,
606 .dport = dport } } };
607 err = ip_route_output_flow(&rt, &fl, sk, !(msg->msg_flags&MSG_DONTWAIT));
612 if ((rt->rt_flags & RTCF_BROADCAST) &&
613 !sock_flag(sk, SOCK_BROADCAST))
616 sk_dst_set(sk, dst_clone(&rt->u.dst));
619 if (msg->msg_flags&MSG_CONFIRM)
625 daddr = ipc.addr = rt->rt_dst;
628 if (unlikely(up->pending)) {
629 /* The socket is already corked while preparing it. */
630 /* ... which is an evident application bug. --ANK */
633 LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 2\n");
638 * Now cork the socket to pend data.
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;
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);
652 udp_flush_pending_frames(sk);
654 err = udp_push_pending_frames(sk, up);
662 UDP_INC_STATS_USER(UDP_MIB_OUTDATAGRAMS);
668 dst_confirm(&rt->u.dst);
669 if (!(msg->msg_flags&MSG_PROBE) || len)
670 goto back_from_confirm;
675 static int udp_sendpage(struct sock *sk, struct page *page, int offset,
676 size_t size, int flags)
678 struct udp_sock *up = udp_sk(sk);
682 struct msghdr msg = { .msg_flags = flags|MSG_MORE };
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.
688 ret = udp_sendmsg(NULL, sk, &msg, 0);
695 if (unlikely(!up->pending)) {
698 LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 3\n");
702 ret = ip_append_page(sk, page, offset, size, flags);
703 if (ret == -EOPNOTSUPP) {
705 return sock_no_sendpage(sk->sk_socket, page, offset,
709 udp_flush_pending_frames(sk);
714 if (!(up->corkflag || (flags&MSG_MORE)))
715 ret = udp_push_pending_frames(sk, up);
724 * IOCTL requests applicable to the UDP protocol
727 int udp_ioctl(struct sock *sk, int cmd, unsigned long arg)
733 int amount = atomic_read(&sk->sk_wmem_alloc);
734 return put_user(amount, (int __user *)arg);
740 unsigned long amount;
743 spin_lock_bh(&sk->sk_receive_queue.lock);
744 skb = skb_peek(&sk->sk_receive_queue);
747 * We will only return the amount
748 * of this packet since that is all
751 amount = skb->len - sizeof(struct udphdr);
753 spin_unlock_bh(&sk->sk_receive_queue.lock);
754 return put_user(amount, (int __user *)arg);
763 static __inline__ int __udp_checksum_complete(struct sk_buff *skb)
765 return __skb_checksum_complete(skb);
768 static __inline__ int udp_checksum_complete(struct sk_buff *skb)
770 return skb->ip_summed != CHECKSUM_UNNECESSARY &&
771 __udp_checksum_complete(skb);
775 * This should be easy, if there is something there we
776 * return it, otherwise we block.
779 static int udp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
780 size_t len, int noblock, int flags, int *addr_len)
782 struct inet_sock *inet = inet_sk(sk);
783 struct sockaddr_in *sin = (struct sockaddr_in *)msg->msg_name;
788 * Check any passed addresses
791 *addr_len=sizeof(*sin);
793 if (flags & MSG_ERRQUEUE)
794 return ip_recv_error(sk, msg, len);
797 skb = skb_recv_datagram(sk, flags, noblock, &err);
801 copied = skb->len - sizeof(struct udphdr);
804 msg->msg_flags |= MSG_TRUNC;
807 if (skb->ip_summed==CHECKSUM_UNNECESSARY) {
808 err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov,
810 } else if (msg->msg_flags&MSG_TRUNC) {
811 if (__udp_checksum_complete(skb))
813 err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov,
816 err = skb_copy_and_csum_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov);
825 sock_recv_timestamp(msg, sk, skb);
827 /* Copy the address. */
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));
835 if (inet->cmsg_flags)
836 ip_cmsg_recv(msg, skb);
839 if (flags & MSG_TRUNC)
840 err = skb->len - sizeof(struct udphdr);
843 skb_free_datagram(sk, skb);
848 UDP_INC_STATS_BH(UDP_MIB_INERRORS);
850 skb_kill_datagram(sk, skb, flags);
858 int udp_disconnect(struct sock *sk, int flags)
860 struct inet_sock *inet = inet_sk(sk);
862 * 1003.1g - break association.
865 sk->sk_state = TCP_CLOSE;
868 sk->sk_bound_dev_if = 0;
869 if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK))
870 inet_reset_saddr(sk);
872 if (!(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) {
873 sk->sk_prot->unhash(sk);
880 static void udp_close(struct sock *sk, long timeout)
882 sk_common_release(sk);
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
890 static int udp_encap_rcv(struct sock * sk, struct sk_buff *skb)
895 struct udp_sock *up = udp_sk(sk);
896 struct udphdr *uh = skb->h.uh;
900 __u8 *udpdata = (__u8 *)uh + sizeof(struct udphdr);
901 __u32 *udpdata32 = (__u32 *)udpdata;
902 __u16 encap_type = up->encap_type;
904 /* if we're overly short, let UDP handle it */
905 if (udpdata > skb->tail)
908 /* if this is not encapsulated socket, then just return now */
912 len = skb->tail - udpdata;
914 switch (encap_type) {
916 case UDP_ENCAP_ESPINUDP:
917 /* Check if this is a keepalive packet. If so, eat it. */
918 if (len == 1 && udpdata[0] == 0xff) {
920 } else if (len > sizeof(struct ip_esp_hdr) && udpdata32[0] != 0 ) {
921 /* ESP Packet without Non-ESP header */
922 len = sizeof(struct udphdr);
924 /* Must be an IKE packet.. pass it through */
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) {
931 } else if (len > 2 * sizeof(u32) + sizeof(struct ip_esp_hdr) &&
932 udpdata32[0] == 0 && udpdata32[1] == 0) {
934 /* ESP Packet with Non-IKE marker */
935 len = sizeof(struct udphdr) + 2 * sizeof(u32);
937 /* Must be an IKE packet.. pass it through */
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.
947 if (skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
950 /* Now we can update and verify the packet length... */
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!?! */
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
963 skb->h.raw = skb_pull(skb, len);
965 /* modify the protocol (it's ESP!) */
966 iph->protocol = IPPROTO_ESP;
968 /* and let the caller know to send this into the ESP processor... */
976 * >0: "udp encap" protocol resubmission
978 * Note that in the success and error cases, the skb is assumed to
979 * have either been requeued or freed.
981 static int udp_queue_rcv_skb(struct sock * sk, struct sk_buff *skb)
983 struct udp_sock *up = udp_sk(sk);
986 * Charge it to the socket, dropping if the queue is full.
988 if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb)) {
994 if (up->encap_type) {
996 * This is an encapsulation socket, so let's see if this is
997 * an encapsulated packet.
998 * If it's a keepalive packet, then just eat it.
999 * If it's an encapsulateed packet, then pass it to the
1000 * IPsec xfrm input and return the response
1001 * appropriately. Otherwise, just fall through and
1002 * pass this up the UDP socket.
1006 ret = udp_encap_rcv(sk, skb);
1008 /* Eat the packet .. */
1013 /* process the ESP packet */
1014 ret = xfrm4_rcv_encap(skb, up->encap_type);
1015 UDP_INC_STATS_BH(UDP_MIB_INDATAGRAMS);
1018 /* FALLTHROUGH -- it's a UDP Packet */
1021 if (sk->sk_filter && skb->ip_summed != CHECKSUM_UNNECESSARY) {
1022 if (__udp_checksum_complete(skb)) {
1023 UDP_INC_STATS_BH(UDP_MIB_INERRORS);
1027 skb->ip_summed = CHECKSUM_UNNECESSARY;
1030 if (sock_queue_rcv_skb(sk,skb)<0) {
1031 UDP_INC_STATS_BH(UDP_MIB_INERRORS);
1035 UDP_INC_STATS_BH(UDP_MIB_INDATAGRAMS);
1040 * Multicasts and broadcasts go to each listener.
1042 * Note: called only from the BH handler context,
1043 * so we don't need to lock the hashes.
1045 static int udp_v4_mcast_deliver(struct sk_buff *skb, struct udphdr *uh,
1046 u32 saddr, u32 daddr)
1051 read_lock(&udp_hash_lock);
1052 sk = sk_head(&udp_hash[ntohs(uh->dest) & (UDP_HTABLE_SIZE - 1)]);
1053 dif = skb->dev->ifindex;
1054 sk = udp_v4_mcast_next(sk, uh->dest, daddr, uh->source, saddr, dif);
1056 struct sock *sknext = NULL;
1059 struct sk_buff *skb1 = skb;
1061 sknext = udp_v4_mcast_next(sk_next(sk), uh->dest, daddr,
1062 uh->source, saddr, dif);
1064 skb1 = skb_clone(skb, GFP_ATOMIC);
1067 int ret = udp_queue_rcv_skb(sk, skb1);
1069 /* we should probably re-process instead
1070 * of dropping packets here. */
1077 read_unlock(&udp_hash_lock);
1081 /* Initialize UDP checksum. If exited with zero value (success),
1082 * CHECKSUM_UNNECESSARY means, that no more checks are required.
1083 * Otherwise, csum completion requires chacksumming packet body,
1084 * including udp header and folding it to skb->csum.
1086 static void udp_checksum_init(struct sk_buff *skb, struct udphdr *uh,
1087 unsigned short ulen, u32 saddr, u32 daddr)
1089 if (uh->check == 0) {
1090 skb->ip_summed = CHECKSUM_UNNECESSARY;
1091 } else if (skb->ip_summed == CHECKSUM_HW) {
1092 if (!udp_check(uh, ulen, saddr, daddr, skb->csum))
1093 skb->ip_summed = CHECKSUM_UNNECESSARY;
1095 if (skb->ip_summed != CHECKSUM_UNNECESSARY)
1096 skb->csum = csum_tcpudp_nofold(saddr, daddr, ulen, IPPROTO_UDP, 0);
1097 /* Probably, we should checksum udp header (it should be in cache
1098 * in any case) and data in tiny packets (< rx copybreak).
1103 * All we need to do is get the socket, and then do a checksum.
1106 int udp_rcv(struct sk_buff *skb)
1110 unsigned short ulen;
1111 struct rtable *rt = (struct rtable*)skb->dst;
1112 u32 saddr = skb->nh.iph->saddr;
1113 u32 daddr = skb->nh.iph->daddr;
1117 * Validate the packet and the UDP length.
1119 if (!pskb_may_pull(skb, sizeof(struct udphdr)))
1124 ulen = ntohs(uh->len);
1126 if (ulen > len || ulen < sizeof(*uh))
1129 if (pskb_trim_rcsum(skb, ulen))
1132 udp_checksum_init(skb, uh, ulen, saddr, daddr);
1134 if(rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST))
1135 return udp_v4_mcast_deliver(skb, uh, saddr, daddr);
1137 sk = udp_v4_lookup(saddr, uh->source, daddr, uh->dest, skb->dev->ifindex);
1140 int ret = udp_queue_rcv_skb(sk, skb);
1143 /* a return value > 0 means to resubmit the input, but
1144 * it it wants the return to be -protocol, or 0
1151 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
1155 /* No socket. Drop packet silently, if checksum is wrong */
1156 if (udp_checksum_complete(skb))
1159 UDP_INC_STATS_BH(UDP_MIB_NOPORTS);
1160 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
1163 * Hmm. We got an UDP packet to a port to which we
1164 * don't wanna listen. Ignore it.
1170 LIMIT_NETDEBUG(KERN_DEBUG "UDP: short packet: From %u.%u.%u.%u:%u %d/%d to %u.%u.%u.%u:%u\n",
1178 UDP_INC_STATS_BH(UDP_MIB_INERRORS);
1184 * RFC1122: OK. Discards the bad packet silently (as far as
1185 * the network is concerned, anyway) as per 4.1.3.4 (MUST).
1187 LIMIT_NETDEBUG(KERN_DEBUG "UDP: bad checksum. From %d.%d.%d.%d:%d to %d.%d.%d.%d:%d ulen %d\n",
1194 UDP_INC_STATS_BH(UDP_MIB_INERRORS);
1199 static int udp_destroy_sock(struct sock *sk)
1202 udp_flush_pending_frames(sk);
1208 * Socket option code for UDP
1210 static int do_udp_setsockopt(struct sock *sk, int level, int optname,
1211 char __user *optval, int optlen)
1213 struct udp_sock *up = udp_sk(sk);
1217 if(optlen<sizeof(int))
1220 if (get_user(val, (int __user *)optval))
1230 udp_push_pending_frames(sk, up);
1238 case UDP_ENCAP_ESPINUDP:
1239 case UDP_ENCAP_ESPINUDP_NON_IKE:
1240 up->encap_type = val;
1256 static int udp_setsockopt(struct sock *sk, int level, int optname,
1257 char __user *optval, int optlen)
1259 if (level != SOL_UDP)
1260 return ip_setsockopt(sk, level, optname, optval, optlen);
1261 return do_udp_setsockopt(sk, level, optname, optval, optlen);
1264 #ifdef CONFIG_COMPAT
1265 static int compat_udp_setsockopt(struct sock *sk, int level, int optname,
1266 char __user *optval, int optlen)
1268 if (level != SOL_UDP)
1269 return compat_ip_setsockopt(sk, level, optname, optval, optlen);
1270 return do_udp_setsockopt(sk, level, optname, optval, optlen);
1274 static int do_udp_getsockopt(struct sock *sk, int level, int optname,
1275 char __user *optval, int __user *optlen)
1277 struct udp_sock *up = udp_sk(sk);
1280 if(get_user(len,optlen))
1283 len = min_t(unsigned int, len, sizeof(int));
1294 val = up->encap_type;
1298 return -ENOPROTOOPT;
1301 if(put_user(len, optlen))
1303 if(copy_to_user(optval, &val,len))
1308 static int udp_getsockopt(struct sock *sk, int level, int optname,
1309 char __user *optval, int __user *optlen)
1311 if (level != SOL_UDP)
1312 return ip_getsockopt(sk, level, optname, optval, optlen);
1313 return do_udp_getsockopt(sk, level, optname, optval, optlen);
1316 #ifdef CONFIG_COMPAT
1317 static int compat_udp_getsockopt(struct sock *sk, int level, int optname,
1318 char __user *optval, int __user *optlen)
1320 if (level != SOL_UDP)
1321 return compat_ip_getsockopt(sk, level, optname, optval, optlen);
1322 return do_udp_getsockopt(sk, level, optname, optval, optlen);
1326 * udp_poll - wait for a UDP event.
1327 * @file - file struct
1329 * @wait - poll table
1331 * This is same as datagram poll, except for the special case of
1332 * blocking sockets. If application is using a blocking fd
1333 * and a packet with checksum error is in the queue;
1334 * then it could get return from select indicating data available
1335 * but then block when reading it. Add special case code
1336 * to work around these arguably broken applications.
1338 unsigned int udp_poll(struct file *file, struct socket *sock, poll_table *wait)
1340 unsigned int mask = datagram_poll(file, sock, wait);
1341 struct sock *sk = sock->sk;
1343 /* Check for false positives due to checksum errors */
1344 if ( (mask & POLLRDNORM) &&
1345 !(file->f_flags & O_NONBLOCK) &&
1346 !(sk->sk_shutdown & RCV_SHUTDOWN)){
1347 struct sk_buff_head *rcvq = &sk->sk_receive_queue;
1348 struct sk_buff *skb;
1350 spin_lock_bh(&rcvq->lock);
1351 while ((skb = skb_peek(rcvq)) != NULL) {
1352 if (udp_checksum_complete(skb)) {
1353 UDP_INC_STATS_BH(UDP_MIB_INERRORS);
1354 __skb_unlink(skb, rcvq);
1357 skb->ip_summed = CHECKSUM_UNNECESSARY;
1361 spin_unlock_bh(&rcvq->lock);
1363 /* nothing to see, move along */
1365 mask &= ~(POLLIN | POLLRDNORM);
1372 struct proto udp_prot = {
1374 .owner = THIS_MODULE,
1376 .connect = ip4_datagram_connect,
1377 .disconnect = udp_disconnect,
1379 .destroy = udp_destroy_sock,
1380 .setsockopt = udp_setsockopt,
1381 .getsockopt = udp_getsockopt,
1382 .sendmsg = udp_sendmsg,
1383 .recvmsg = udp_recvmsg,
1384 .sendpage = udp_sendpage,
1385 .backlog_rcv = udp_queue_rcv_skb,
1386 .hash = udp_v4_hash,
1387 .unhash = udp_v4_unhash,
1388 .get_port = udp_v4_get_port,
1389 .obj_size = sizeof(struct udp_sock),
1390 #ifdef CONFIG_COMPAT
1391 .compat_setsockopt = compat_udp_setsockopt,
1392 .compat_getsockopt = compat_udp_getsockopt,
1396 /* ------------------------------------------------------------------------ */
1397 #ifdef CONFIG_PROC_FS
1399 static struct sock *udp_get_first(struct seq_file *seq)
1402 struct udp_iter_state *state = seq->private;
1404 for (state->bucket = 0; state->bucket < UDP_HTABLE_SIZE; ++state->bucket) {
1405 struct hlist_node *node;
1406 sk_for_each(sk, node, &udp_hash[state->bucket]) {
1407 if (sk->sk_family == state->family)
1416 static struct sock *udp_get_next(struct seq_file *seq, struct sock *sk)
1418 struct udp_iter_state *state = seq->private;
1424 } while (sk && sk->sk_family != state->family);
1426 if (!sk && ++state->bucket < UDP_HTABLE_SIZE) {
1427 sk = sk_head(&udp_hash[state->bucket]);
1433 static struct sock *udp_get_idx(struct seq_file *seq, loff_t pos)
1435 struct sock *sk = udp_get_first(seq);
1438 while(pos && (sk = udp_get_next(seq, sk)) != NULL)
1440 return pos ? NULL : sk;
1443 static void *udp_seq_start(struct seq_file *seq, loff_t *pos)
1445 read_lock(&udp_hash_lock);
1446 return *pos ? udp_get_idx(seq, *pos-1) : (void *)1;
1449 static void *udp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1454 sk = udp_get_idx(seq, 0);
1456 sk = udp_get_next(seq, v);
1462 static void udp_seq_stop(struct seq_file *seq, void *v)
1464 read_unlock(&udp_hash_lock);
1467 static int udp_seq_open(struct inode *inode, struct file *file)
1469 struct udp_seq_afinfo *afinfo = PDE(inode)->data;
1470 struct seq_file *seq;
1472 struct udp_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
1476 memset(s, 0, sizeof(*s));
1477 s->family = afinfo->family;
1478 s->seq_ops.start = udp_seq_start;
1479 s->seq_ops.next = udp_seq_next;
1480 s->seq_ops.show = afinfo->seq_show;
1481 s->seq_ops.stop = udp_seq_stop;
1483 rc = seq_open(file, &s->seq_ops);
1487 seq = file->private_data;
1496 /* ------------------------------------------------------------------------ */
1497 int udp_proc_register(struct udp_seq_afinfo *afinfo)
1499 struct proc_dir_entry *p;
1504 afinfo->seq_fops->owner = afinfo->owner;
1505 afinfo->seq_fops->open = udp_seq_open;
1506 afinfo->seq_fops->read = seq_read;
1507 afinfo->seq_fops->llseek = seq_lseek;
1508 afinfo->seq_fops->release = seq_release_private;
1510 p = proc_net_fops_create(afinfo->name, S_IRUGO, afinfo->seq_fops);
1518 void udp_proc_unregister(struct udp_seq_afinfo *afinfo)
1522 proc_net_remove(afinfo->name);
1523 memset(afinfo->seq_fops, 0, sizeof(*afinfo->seq_fops));
1526 /* ------------------------------------------------------------------------ */
1527 static void udp4_format_sock(struct sock *sp, char *tmpbuf, int bucket)
1529 struct inet_sock *inet = inet_sk(sp);
1530 unsigned int dest = inet->daddr;
1531 unsigned int src = inet->rcv_saddr;
1532 __u16 destp = ntohs(inet->dport);
1533 __u16 srcp = ntohs(inet->sport);
1535 sprintf(tmpbuf, "%4d: %08X:%04X %08X:%04X"
1536 " %02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p",
1537 bucket, src, srcp, dest, destp, sp->sk_state,
1538 atomic_read(&sp->sk_wmem_alloc),
1539 atomic_read(&sp->sk_rmem_alloc),
1540 0, 0L, 0, sock_i_uid(sp), 0, sock_i_ino(sp),
1541 atomic_read(&sp->sk_refcnt), sp);
1544 static int udp4_seq_show(struct seq_file *seq, void *v)
1546 if (v == SEQ_START_TOKEN)
1547 seq_printf(seq, "%-127s\n",
1548 " sl local_address rem_address st tx_queue "
1549 "rx_queue tr tm->when retrnsmt uid timeout "
1553 struct udp_iter_state *state = seq->private;
1555 udp4_format_sock(v, tmpbuf, state->bucket);
1556 seq_printf(seq, "%-127s\n", tmpbuf);
1561 /* ------------------------------------------------------------------------ */
1562 static struct file_operations udp4_seq_fops;
1563 static struct udp_seq_afinfo udp4_seq_afinfo = {
1564 .owner = THIS_MODULE,
1567 .seq_show = udp4_seq_show,
1568 .seq_fops = &udp4_seq_fops,
1571 int __init udp4_proc_init(void)
1573 return udp_proc_register(&udp4_seq_afinfo);
1576 void udp4_proc_exit(void)
1578 udp_proc_unregister(&udp4_seq_afinfo);
1580 #endif /* CONFIG_PROC_FS */
1582 EXPORT_SYMBOL(udp_disconnect);
1583 EXPORT_SYMBOL(udp_hash);
1584 EXPORT_SYMBOL(udp_hash_lock);
1585 EXPORT_SYMBOL(udp_ioctl);
1586 EXPORT_SYMBOL(udp_port_rover);
1587 EXPORT_SYMBOL(udp_prot);
1588 EXPORT_SYMBOL(udp_sendmsg);
1589 EXPORT_SYMBOL(udp_poll);
1591 #ifdef CONFIG_PROC_FS
1592 EXPORT_SYMBOL(udp_proc_register);
1593 EXPORT_SYMBOL(udp_proc_unregister);