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/inet.h>
95 #include <linux/ipv6.h>
96 #include <linux/netdevice.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>
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>
113 * Snmp MIB for the UDP layer
116 DEFINE_SNMP_STAT(struct udp_mib
, udp_statistics
) __read_mostly
;
118 struct hlist_head udp_hash
[UDP_HTABLE_SIZE
];
119 DEFINE_RWLOCK(udp_hash_lock
);
121 /* Shared by v4/v6 udp. */
124 static int udp_v4_get_port(struct sock
*sk
, unsigned short snum
)
126 struct hlist_node
*node
;
128 struct inet_sock
*inet
= inet_sk(sk
);
130 write_lock_bh(&udp_hash_lock
);
132 int best_size_so_far
, best
, result
, i
;
134 if (udp_port_rover
> sysctl_local_port_range
[1] ||
135 udp_port_rover
< sysctl_local_port_range
[0])
136 udp_port_rover
= sysctl_local_port_range
[0];
137 best_size_so_far
= 32767;
138 best
= result
= udp_port_rover
;
139 for (i
= 0; i
< UDP_HTABLE_SIZE
; i
++, result
++) {
140 struct hlist_head
*list
;
143 list
= &udp_hash
[result
& (UDP_HTABLE_SIZE
- 1)];
144 if (hlist_empty(list
)) {
145 if (result
> sysctl_local_port_range
[1])
146 result
= sysctl_local_port_range
[0] +
147 ((result
- sysctl_local_port_range
[0]) &
148 (UDP_HTABLE_SIZE
- 1));
152 sk_for_each(sk2
, node
, list
)
153 if (++size
>= best_size_so_far
)
155 best_size_so_far
= size
;
160 for(i
= 0; i
< (1 << 16) / UDP_HTABLE_SIZE
; i
++, result
+= UDP_HTABLE_SIZE
) {
161 if (result
> sysctl_local_port_range
[1])
162 result
= sysctl_local_port_range
[0]
163 + ((result
- sysctl_local_port_range
[0]) &
164 (UDP_HTABLE_SIZE
- 1));
165 if (!udp_lport_inuse(result
))
168 if (i
>= (1 << 16) / UDP_HTABLE_SIZE
)
171 udp_port_rover
= snum
= result
;
173 sk_for_each(sk2
, node
,
174 &udp_hash
[snum
& (UDP_HTABLE_SIZE
- 1)]) {
175 struct inet_sock
*inet2
= inet_sk(sk2
);
177 if (inet2
->num
== snum
&&
179 !ipv6_only_sock(sk2
) &&
180 (!sk2
->sk_bound_dev_if
||
181 !sk
->sk_bound_dev_if
||
182 sk2
->sk_bound_dev_if
== sk
->sk_bound_dev_if
) &&
183 (!inet2
->rcv_saddr
||
185 inet2
->rcv_saddr
== inet
->rcv_saddr
) &&
186 (!sk2
->sk_reuse
|| !sk
->sk_reuse
))
191 if (sk_unhashed(sk
)) {
192 struct hlist_head
*h
= &udp_hash
[snum
& (UDP_HTABLE_SIZE
- 1)];
195 sock_prot_inc_use(sk
->sk_prot
);
197 write_unlock_bh(&udp_hash_lock
);
201 write_unlock_bh(&udp_hash_lock
);
205 static void udp_v4_hash(struct sock
*sk
)
210 static void udp_v4_unhash(struct sock
*sk
)
212 write_lock_bh(&udp_hash_lock
);
213 if (sk_del_node_init(sk
)) {
214 inet_sk(sk
)->num
= 0;
215 sock_prot_dec_use(sk
->sk_prot
);
217 write_unlock_bh(&udp_hash_lock
);
220 /* UDP is nearly always wildcards out the wazoo, it makes no sense to try
221 * harder than this. -DaveM
223 static struct sock
*udp_v4_lookup_longway(u32 saddr
, u16 sport
,
224 u32 daddr
, u16 dport
, int dif
)
226 struct sock
*sk
, *result
= NULL
;
227 struct hlist_node
*node
;
228 unsigned short hnum
= ntohs(dport
);
231 sk_for_each(sk
, node
, &udp_hash
[hnum
& (UDP_HTABLE_SIZE
- 1)]) {
232 struct inet_sock
*inet
= inet_sk(sk
);
234 if (inet
->num
== hnum
&& !ipv6_only_sock(sk
)) {
235 int score
= (sk
->sk_family
== PF_INET
? 1 : 0);
236 if (inet
->rcv_saddr
) {
237 if (inet
->rcv_saddr
!= daddr
)
242 if (inet
->daddr
!= saddr
)
247 if (inet
->dport
!= sport
)
251 if (sk
->sk_bound_dev_if
) {
252 if (sk
->sk_bound_dev_if
!= dif
)
259 } else if(score
> badness
) {
268 static __inline__
struct sock
*udp_v4_lookup(u32 saddr
, u16 sport
,
269 u32 daddr
, u16 dport
, int dif
)
273 read_lock(&udp_hash_lock
);
274 sk
= udp_v4_lookup_longway(saddr
, sport
, daddr
, dport
, dif
);
277 read_unlock(&udp_hash_lock
);
281 static inline struct sock
*udp_v4_mcast_next(struct sock
*sk
,
282 u16 loc_port
, u32 loc_addr
,
283 u16 rmt_port
, u32 rmt_addr
,
286 struct hlist_node
*node
;
288 unsigned short hnum
= ntohs(loc_port
);
290 sk_for_each_from(s
, node
) {
291 struct inet_sock
*inet
= inet_sk(s
);
293 if (inet
->num
!= hnum
||
294 (inet
->daddr
&& inet
->daddr
!= rmt_addr
) ||
295 (inet
->dport
!= rmt_port
&& inet
->dport
) ||
296 (inet
->rcv_saddr
&& inet
->rcv_saddr
!= loc_addr
) ||
298 (s
->sk_bound_dev_if
&& s
->sk_bound_dev_if
!= dif
))
300 if (!ip_mc_sf_allow(s
, loc_addr
, rmt_addr
, dif
))
310 * This routine is called by the ICMP module when it gets some
311 * sort of error condition. If err < 0 then the socket should
312 * be closed and the error returned to the user. If err > 0
313 * it's just the icmp type << 8 | icmp code.
314 * Header points to the ip header of the error packet. We move
315 * on past this. Then (as it used to claim before adjustment)
316 * header points to the first 8 bytes of the udp header. We need
317 * to find the appropriate port.
320 void udp_err(struct sk_buff
*skb
, u32 info
)
322 struct inet_sock
*inet
;
323 struct iphdr
*iph
= (struct iphdr
*)skb
->data
;
324 struct udphdr
*uh
= (struct udphdr
*)(skb
->data
+(iph
->ihl
<<2));
325 int type
= skb
->h
.icmph
->type
;
326 int code
= skb
->h
.icmph
->code
;
331 sk
= udp_v4_lookup(iph
->daddr
, uh
->dest
, iph
->saddr
, uh
->source
, skb
->dev
->ifindex
);
333 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS
);
334 return; /* No socket for error */
343 case ICMP_TIME_EXCEEDED
:
346 case ICMP_SOURCE_QUENCH
:
348 case ICMP_PARAMETERPROB
:
352 case ICMP_DEST_UNREACH
:
353 if (code
== ICMP_FRAG_NEEDED
) { /* Path MTU discovery */
354 if (inet
->pmtudisc
!= IP_PMTUDISC_DONT
) {
362 if (code
<= NR_ICMP_UNREACH
) {
363 harderr
= icmp_err_convert
[code
].fatal
;
364 err
= icmp_err_convert
[code
].errno
;
370 * RFC1122: OK. Passes ICMP errors back to application, as per
373 if (!inet
->recverr
) {
374 if (!harderr
|| sk
->sk_state
!= TCP_ESTABLISHED
)
377 ip_icmp_error(sk
, skb
, err
, uh
->dest
, info
, (u8
*)(uh
+1));
380 sk
->sk_error_report(sk
);
386 * Throw away all pending data and cancel the corking. Socket is locked.
388 static void udp_flush_pending_frames(struct sock
*sk
)
390 struct udp_sock
*up
= udp_sk(sk
);
395 ip_flush_pending_frames(sk
);
400 * Push out all pending data as one UDP datagram. Socket is locked.
402 static int udp_push_pending_frames(struct sock
*sk
, struct udp_sock
*up
)
404 struct inet_sock
*inet
= inet_sk(sk
);
405 struct flowi
*fl
= &inet
->cork
.fl
;
410 /* Grab the skbuff where UDP header space exists. */
411 if ((skb
= skb_peek(&sk
->sk_write_queue
)) == NULL
)
415 * Create a UDP header
418 uh
->source
= fl
->fl_ip_sport
;
419 uh
->dest
= fl
->fl_ip_dport
;
420 uh
->len
= htons(up
->len
);
423 if (sk
->sk_no_check
== UDP_CSUM_NOXMIT
) {
424 skb
->ip_summed
= CHECKSUM_NONE
;
428 if (skb_queue_len(&sk
->sk_write_queue
) == 1) {
430 * Only one fragment on the socket.
432 if (skb
->ip_summed
== CHECKSUM_HW
) {
433 skb
->csum
= offsetof(struct udphdr
, check
);
434 uh
->check
= ~csum_tcpudp_magic(fl
->fl4_src
, fl
->fl4_dst
,
435 up
->len
, IPPROTO_UDP
, 0);
437 skb
->csum
= csum_partial((char *)uh
,
438 sizeof(struct udphdr
), skb
->csum
);
439 uh
->check
= csum_tcpudp_magic(fl
->fl4_src
, fl
->fl4_dst
,
440 up
->len
, IPPROTO_UDP
, skb
->csum
);
445 unsigned int csum
= 0;
447 * HW-checksum won't work as there are two or more
448 * fragments on the socket so that all csums of sk_buffs
449 * should be together.
451 if (skb
->ip_summed
== CHECKSUM_HW
) {
452 int offset
= (unsigned char *)uh
- skb
->data
;
453 skb
->csum
= skb_checksum(skb
, offset
, skb
->len
- offset
, 0);
455 skb
->ip_summed
= CHECKSUM_NONE
;
457 skb
->csum
= csum_partial((char *)uh
,
458 sizeof(struct udphdr
), skb
->csum
);
461 skb_queue_walk(&sk
->sk_write_queue
, skb
) {
462 csum
= csum_add(csum
, skb
->csum
);
464 uh
->check
= csum_tcpudp_magic(fl
->fl4_src
, fl
->fl4_dst
,
465 up
->len
, IPPROTO_UDP
, csum
);
470 err
= ip_push_pending_frames(sk
);
478 static unsigned short udp_check(struct udphdr
*uh
, int len
, unsigned long saddr
, unsigned long daddr
, unsigned long base
)
480 return(csum_tcpudp_magic(saddr
, daddr
, len
, IPPROTO_UDP
, base
));
483 int udp_sendmsg(struct kiocb
*iocb
, struct sock
*sk
, struct msghdr
*msg
,
486 struct inet_sock
*inet
= inet_sk(sk
);
487 struct udp_sock
*up
= udp_sk(sk
);
489 struct ipcm_cookie ipc
;
490 struct rtable
*rt
= NULL
;
493 u32 daddr
, faddr
, saddr
;
497 int corkreq
= up
->corkflag
|| msg
->msg_flags
&MSG_MORE
;
506 if (msg
->msg_flags
&MSG_OOB
) /* Mirror BSD error message compatibility */
513 * There are pending frames.
514 * The socket lock must be held while it's corked.
517 if (likely(up
->pending
)) {
518 if (unlikely(up
->pending
!= AF_INET
)) {
526 ulen
+= sizeof(struct udphdr
);
529 * Get and verify the address.
532 struct sockaddr_in
* usin
= (struct sockaddr_in
*)msg
->msg_name
;
533 if (msg
->msg_namelen
< sizeof(*usin
))
535 if (usin
->sin_family
!= AF_INET
) {
536 if (usin
->sin_family
!= AF_UNSPEC
)
537 return -EAFNOSUPPORT
;
540 daddr
= usin
->sin_addr
.s_addr
;
541 dport
= usin
->sin_port
;
545 if (sk
->sk_state
!= TCP_ESTABLISHED
)
546 return -EDESTADDRREQ
;
549 /* Open fast path for connected socket.
550 Route will not be used, if at least one option is set.
554 ipc
.addr
= inet
->saddr
;
556 ipc
.oif
= sk
->sk_bound_dev_if
;
557 if (msg
->msg_controllen
) {
558 err
= ip_cmsg_send(msg
, &ipc
);
569 ipc
.addr
= faddr
= daddr
;
571 if (ipc
.opt
&& ipc
.opt
->srr
) {
574 faddr
= ipc
.opt
->faddr
;
577 tos
= RT_TOS(inet
->tos
);
578 if (sock_flag(sk
, SOCK_LOCALROUTE
) ||
579 (msg
->msg_flags
& MSG_DONTROUTE
) ||
580 (ipc
.opt
&& ipc
.opt
->is_strictroute
)) {
585 if (MULTICAST(daddr
)) {
587 ipc
.oif
= inet
->mc_index
;
589 saddr
= inet
->mc_addr
;
594 rt
= (struct rtable
*)sk_dst_check(sk
, 0);
597 struct flowi fl
= { .oif
= ipc
.oif
,
602 .proto
= IPPROTO_UDP
,
604 { .sport
= inet
->sport
,
605 .dport
= dport
} } };
606 err
= ip_route_output_flow(&rt
, &fl
, sk
, !(msg
->msg_flags
&MSG_DONTWAIT
));
611 if ((rt
->rt_flags
& RTCF_BROADCAST
) &&
612 !sock_flag(sk
, SOCK_BROADCAST
))
615 sk_dst_set(sk
, dst_clone(&rt
->u
.dst
));
618 if (msg
->msg_flags
&MSG_CONFIRM
)
624 daddr
= ipc
.addr
= rt
->rt_dst
;
627 if (unlikely(up
->pending
)) {
628 /* The socket is already corked while preparing it. */
629 /* ... which is an evident application bug. --ANK */
632 LIMIT_NETDEBUG(KERN_DEBUG
"udp cork app bug 2\n");
637 * Now cork the socket to pend data.
639 inet
->cork
.fl
.fl4_dst
= daddr
;
640 inet
->cork
.fl
.fl_ip_dport
= dport
;
641 inet
->cork
.fl
.fl4_src
= saddr
;
642 inet
->cork
.fl
.fl_ip_sport
= inet
->sport
;
643 up
->pending
= AF_INET
;
647 err
= ip_append_data(sk
, ip_generic_getfrag
, msg
->msg_iov
, ulen
,
648 sizeof(struct udphdr
), &ipc
, rt
,
649 corkreq
? msg
->msg_flags
|MSG_MORE
: msg
->msg_flags
);
651 udp_flush_pending_frames(sk
);
653 err
= udp_push_pending_frames(sk
, up
);
661 UDP_INC_STATS_USER(UDP_MIB_OUTDATAGRAMS
);
667 dst_confirm(&rt
->u
.dst
);
668 if (!(msg
->msg_flags
&MSG_PROBE
) || len
)
669 goto back_from_confirm
;
674 static int udp_sendpage(struct sock
*sk
, struct page
*page
, int offset
,
675 size_t size
, int flags
)
677 struct udp_sock
*up
= udp_sk(sk
);
681 struct msghdr msg
= { .msg_flags
= flags
|MSG_MORE
};
683 /* Call udp_sendmsg to specify destination address which
684 * sendpage interface can't pass.
685 * This will succeed only when the socket is connected.
687 ret
= udp_sendmsg(NULL
, sk
, &msg
, 0);
694 if (unlikely(!up
->pending
)) {
697 LIMIT_NETDEBUG(KERN_DEBUG
"udp cork app bug 3\n");
701 ret
= ip_append_page(sk
, page
, offset
, size
, flags
);
702 if (ret
== -EOPNOTSUPP
) {
704 return sock_no_sendpage(sk
->sk_socket
, page
, offset
,
708 udp_flush_pending_frames(sk
);
713 if (!(up
->corkflag
|| (flags
&MSG_MORE
)))
714 ret
= udp_push_pending_frames(sk
, up
);
723 * IOCTL requests applicable to the UDP protocol
726 int udp_ioctl(struct sock
*sk
, int cmd
, unsigned long arg
)
732 int amount
= atomic_read(&sk
->sk_wmem_alloc
);
733 return put_user(amount
, (int __user
*)arg
);
739 unsigned long amount
;
742 spin_lock_bh(&sk
->sk_receive_queue
.lock
);
743 skb
= skb_peek(&sk
->sk_receive_queue
);
746 * We will only return the amount
747 * of this packet since that is all
750 amount
= skb
->len
- sizeof(struct udphdr
);
752 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
753 return put_user(amount
, (int __user
*)arg
);
762 static __inline__
int __udp_checksum_complete(struct sk_buff
*skb
)
764 return __skb_checksum_complete(skb
);
767 static __inline__
int udp_checksum_complete(struct sk_buff
*skb
)
769 return skb
->ip_summed
!= CHECKSUM_UNNECESSARY
&&
770 __udp_checksum_complete(skb
);
774 * This should be easy, if there is something there we
775 * return it, otherwise we block.
778 static int udp_recvmsg(struct kiocb
*iocb
, struct sock
*sk
, struct msghdr
*msg
,
779 size_t len
, int noblock
, int flags
, int *addr_len
)
781 struct inet_sock
*inet
= inet_sk(sk
);
782 struct sockaddr_in
*sin
= (struct sockaddr_in
*)msg
->msg_name
;
787 * Check any passed addresses
790 *addr_len
=sizeof(*sin
);
792 if (flags
& MSG_ERRQUEUE
)
793 return ip_recv_error(sk
, msg
, len
);
796 skb
= skb_recv_datagram(sk
, flags
, noblock
, &err
);
800 copied
= skb
->len
- sizeof(struct udphdr
);
803 msg
->msg_flags
|= MSG_TRUNC
;
806 if (skb
->ip_summed
==CHECKSUM_UNNECESSARY
) {
807 err
= skb_copy_datagram_iovec(skb
, sizeof(struct udphdr
), msg
->msg_iov
,
809 } else if (msg
->msg_flags
&MSG_TRUNC
) {
810 if (__udp_checksum_complete(skb
))
812 err
= skb_copy_datagram_iovec(skb
, sizeof(struct udphdr
), msg
->msg_iov
,
815 err
= skb_copy_and_csum_datagram_iovec(skb
, sizeof(struct udphdr
), msg
->msg_iov
);
824 sock_recv_timestamp(msg
, sk
, skb
);
826 /* Copy the address. */
829 sin
->sin_family
= AF_INET
;
830 sin
->sin_port
= skb
->h
.uh
->source
;
831 sin
->sin_addr
.s_addr
= skb
->nh
.iph
->saddr
;
832 memset(sin
->sin_zero
, 0, sizeof(sin
->sin_zero
));
834 if (inet
->cmsg_flags
)
835 ip_cmsg_recv(msg
, skb
);
838 if (flags
& MSG_TRUNC
)
839 err
= skb
->len
- sizeof(struct udphdr
);
842 skb_free_datagram(sk
, skb
);
847 UDP_INC_STATS_BH(UDP_MIB_INERRORS
);
849 skb_kill_datagram(sk
, skb
, flags
);
857 int udp_disconnect(struct sock
*sk
, int flags
)
859 struct inet_sock
*inet
= inet_sk(sk
);
861 * 1003.1g - break association.
864 sk
->sk_state
= TCP_CLOSE
;
867 sk
->sk_bound_dev_if
= 0;
868 if (!(sk
->sk_userlocks
& SOCK_BINDADDR_LOCK
))
869 inet_reset_saddr(sk
);
871 if (!(sk
->sk_userlocks
& SOCK_BINDPORT_LOCK
)) {
872 sk
->sk_prot
->unhash(sk
);
879 static void udp_close(struct sock
*sk
, long timeout
)
881 sk_common_release(sk
);
885 * 1 if the the UDP system should process it
886 * 0 if we should drop this packet
887 * -1 if it should get processed by xfrm4_rcv_encap
889 static int udp_encap_rcv(struct sock
* sk
, struct sk_buff
*skb
)
894 struct udp_sock
*up
= udp_sk(sk
);
895 struct udphdr
*uh
= skb
->h
.uh
;
899 __u8
*udpdata
= (__u8
*)uh
+ sizeof(struct udphdr
);
900 __u32
*udpdata32
= (__u32
*)udpdata
;
901 __u16 encap_type
= up
->encap_type
;
903 /* if we're overly short, let UDP handle it */
904 if (udpdata
> skb
->tail
)
907 /* if this is not encapsulated socket, then just return now */
911 len
= skb
->tail
- udpdata
;
913 switch (encap_type
) {
915 case UDP_ENCAP_ESPINUDP
:
916 /* Check if this is a keepalive packet. If so, eat it. */
917 if (len
== 1 && udpdata
[0] == 0xff) {
919 } else if (len
> sizeof(struct ip_esp_hdr
) && udpdata32
[0] != 0 ) {
920 /* ESP Packet without Non-ESP header */
921 len
= sizeof(struct udphdr
);
923 /* Must be an IKE packet.. pass it through */
926 case UDP_ENCAP_ESPINUDP_NON_IKE
:
927 /* Check if this is a keepalive packet. If so, eat it. */
928 if (len
== 1 && udpdata
[0] == 0xff) {
930 } else if (len
> 2 * sizeof(u32
) + sizeof(struct ip_esp_hdr
) &&
931 udpdata32
[0] == 0 && udpdata32
[1] == 0) {
933 /* ESP Packet with Non-IKE marker */
934 len
= sizeof(struct udphdr
) + 2 * sizeof(u32
);
936 /* Must be an IKE packet.. pass it through */
941 /* At this point we are sure that this is an ESPinUDP packet,
942 * so we need to remove 'len' bytes from the packet (the UDP
943 * header and optional ESP marker bytes) and then modify the
944 * protocol to ESP, and then call into the transform receiver.
946 if (skb_cloned(skb
) && pskb_expand_head(skb
, 0, 0, GFP_ATOMIC
))
949 /* Now we can update and verify the packet length... */
951 iphlen
= iph
->ihl
<< 2;
952 iph
->tot_len
= htons(ntohs(iph
->tot_len
) - len
);
953 if (skb
->len
< iphlen
+ len
) {
954 /* packet is too small!?! */
958 /* pull the data buffer up to the ESP header and set the
959 * transport header to point to ESP. Keep UDP on the stack
962 skb
->h
.raw
= skb_pull(skb
, len
);
964 /* modify the protocol (it's ESP!) */
965 iph
->protocol
= IPPROTO_ESP
;
967 /* and let the caller know to send this into the ESP processor... */
975 * >0: "udp encap" protocol resubmission
977 * Note that in the success and error cases, the skb is assumed to
978 * have either been requeued or freed.
980 static int udp_queue_rcv_skb(struct sock
* sk
, struct sk_buff
*skb
)
982 struct udp_sock
*up
= udp_sk(sk
);
985 * Charge it to the socket, dropping if the queue is full.
987 if (!xfrm4_policy_check(sk
, XFRM_POLICY_IN
, skb
)) {
993 if (up
->encap_type
) {
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.
1005 ret
= udp_encap_rcv(sk
, skb
);
1007 /* Eat the packet .. */
1012 /* process the ESP packet */
1013 ret
= xfrm4_rcv_encap(skb
, up
->encap_type
);
1014 UDP_INC_STATS_BH(UDP_MIB_INDATAGRAMS
);
1017 /* FALLTHROUGH -- it's a UDP Packet */
1020 if (sk
->sk_filter
&& skb
->ip_summed
!= CHECKSUM_UNNECESSARY
) {
1021 if (__udp_checksum_complete(skb
)) {
1022 UDP_INC_STATS_BH(UDP_MIB_INERRORS
);
1026 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
1029 if (sock_queue_rcv_skb(sk
,skb
)<0) {
1030 UDP_INC_STATS_BH(UDP_MIB_INERRORS
);
1034 UDP_INC_STATS_BH(UDP_MIB_INDATAGRAMS
);
1039 * Multicasts and broadcasts go to each listener.
1041 * Note: called only from the BH handler context,
1042 * so we don't need to lock the hashes.
1044 static int udp_v4_mcast_deliver(struct sk_buff
*skb
, struct udphdr
*uh
,
1045 u32 saddr
, u32 daddr
)
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
);
1055 struct sock
*sknext
= NULL
;
1058 struct sk_buff
*skb1
= skb
;
1060 sknext
= udp_v4_mcast_next(sk_next(sk
), uh
->dest
, daddr
,
1061 uh
->source
, saddr
, dif
);
1063 skb1
= skb_clone(skb
, GFP_ATOMIC
);
1066 int ret
= udp_queue_rcv_skb(sk
, skb1
);
1068 /* we should probably re-process instead
1069 * of dropping packets here. */
1076 read_unlock(&udp_hash_lock
);
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.
1085 static void udp_checksum_init(struct sk_buff
*skb
, struct udphdr
*uh
,
1086 unsigned short ulen
, u32 saddr
, u32 daddr
)
1088 if (uh
->check
== 0) {
1089 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
1090 } else if (skb
->ip_summed
== CHECKSUM_HW
) {
1091 if (!udp_check(uh
, ulen
, saddr
, daddr
, skb
->csum
))
1092 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
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).
1102 * All we need to do is get the socket, and then do a checksum.
1105 int udp_rcv(struct sk_buff
*skb
)
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
;
1116 * Validate the packet and the UDP length.
1118 if (!pskb_may_pull(skb
, sizeof(struct udphdr
)))
1123 ulen
= ntohs(uh
->len
);
1125 if (ulen
> len
|| ulen
< sizeof(*uh
))
1128 if (pskb_trim_rcsum(skb
, ulen
))
1131 udp_checksum_init(skb
, uh
, ulen
, saddr
, daddr
);
1133 if(rt
->rt_flags
& (RTCF_BROADCAST
|RTCF_MULTICAST
))
1134 return udp_v4_mcast_deliver(skb
, uh
, saddr
, daddr
);
1136 sk
= udp_v4_lookup(saddr
, uh
->source
, daddr
, uh
->dest
, skb
->dev
->ifindex
);
1139 int ret
= udp_queue_rcv_skb(sk
, skb
);
1142 /* a return value > 0 means to resubmit the input, but
1143 * it it wants the return to be -protocol, or 0
1150 if (!xfrm4_policy_check(NULL
, XFRM_POLICY_IN
, skb
))
1154 /* No socket. Drop packet silently, if checksum is wrong */
1155 if (udp_checksum_complete(skb
))
1158 UDP_INC_STATS_BH(UDP_MIB_NOPORTS
);
1159 icmp_send(skb
, ICMP_DEST_UNREACH
, ICMP_PORT_UNREACH
, 0);
1162 * Hmm. We got an UDP packet to a port to which we
1163 * don't wanna listen. Ignore it.
1169 LIMIT_NETDEBUG(KERN_DEBUG
"UDP: short packet: From %u.%u.%u.%u:%u %d/%d to %u.%u.%u.%u:%u\n",
1177 UDP_INC_STATS_BH(UDP_MIB_INERRORS
);
1183 * RFC1122: OK. Discards the bad packet silently (as far as
1184 * the network is concerned, anyway) as per 4.1.3.4 (MUST).
1186 LIMIT_NETDEBUG(KERN_DEBUG
"UDP: bad checksum. From %d.%d.%d.%d:%d to %d.%d.%d.%d:%d ulen %d\n",
1193 UDP_INC_STATS_BH(UDP_MIB_INERRORS
);
1198 static int udp_destroy_sock(struct sock
*sk
)
1201 udp_flush_pending_frames(sk
);
1207 * Socket option code for UDP
1209 static int do_udp_setsockopt(struct sock
*sk
, int level
, int optname
,
1210 char __user
*optval
, int optlen
)
1212 struct udp_sock
*up
= udp_sk(sk
);
1216 if(optlen
<sizeof(int))
1219 if (get_user(val
, (int __user
*)optval
))
1229 udp_push_pending_frames(sk
, up
);
1237 case UDP_ENCAP_ESPINUDP
:
1238 case UDP_ENCAP_ESPINUDP_NON_IKE
:
1239 up
->encap_type
= val
;
1255 static int udp_setsockopt(struct sock
*sk
, int level
, int optname
,
1256 char __user
*optval
, int optlen
)
1258 if (level
!= SOL_UDP
)
1259 return ip_setsockopt(sk
, level
, optname
, optval
, optlen
);
1260 return do_udp_setsockopt(sk
, level
, optname
, optval
, optlen
);
1263 #ifdef CONFIG_COMPAT
1264 static int compat_udp_setsockopt(struct sock
*sk
, int level
, int optname
,
1265 char __user
*optval
, int optlen
)
1267 if (level
!= SOL_UDP
)
1268 return compat_ip_setsockopt(sk
, level
, optname
, optval
, optlen
);
1269 return do_udp_setsockopt(sk
, level
, optname
, optval
, optlen
);
1273 static int do_udp_getsockopt(struct sock
*sk
, int level
, int optname
,
1274 char __user
*optval
, int __user
*optlen
)
1276 struct udp_sock
*up
= udp_sk(sk
);
1279 if(get_user(len
,optlen
))
1282 len
= min_t(unsigned int, len
, sizeof(int));
1293 val
= up
->encap_type
;
1297 return -ENOPROTOOPT
;
1300 if(put_user(len
, optlen
))
1302 if(copy_to_user(optval
, &val
,len
))
1307 static int udp_getsockopt(struct sock
*sk
, int level
, int optname
,
1308 char __user
*optval
, int __user
*optlen
)
1310 if (level
!= SOL_UDP
)
1311 return ip_getsockopt(sk
, level
, optname
, optval
, optlen
);
1312 return do_udp_getsockopt(sk
, level
, optname
, optval
, optlen
);
1315 #ifdef CONFIG_COMPAT
1316 static int compat_udp_getsockopt(struct sock
*sk
, int level
, int optname
,
1317 char __user
*optval
, int __user
*optlen
)
1319 if (level
!= SOL_UDP
)
1320 return compat_ip_getsockopt(sk
, level
, optname
, optval
, optlen
);
1321 return do_udp_getsockopt(sk
, level
, optname
, optval
, optlen
);
1325 * udp_poll - wait for a UDP event.
1326 * @file - file struct
1328 * @wait - poll table
1330 * This is same as datagram poll, except for the special case of
1331 * blocking sockets. If application is using a blocking fd
1332 * and a packet with checksum error is in the queue;
1333 * then it could get return from select indicating data available
1334 * but then block when reading it. Add special case code
1335 * to work around these arguably broken applications.
1337 unsigned int udp_poll(struct file
*file
, struct socket
*sock
, poll_table
*wait
)
1339 unsigned int mask
= datagram_poll(file
, sock
, wait
);
1340 struct sock
*sk
= sock
->sk
;
1342 /* Check for false positives due to checksum errors */
1343 if ( (mask
& POLLRDNORM
) &&
1344 !(file
->f_flags
& O_NONBLOCK
) &&
1345 !(sk
->sk_shutdown
& RCV_SHUTDOWN
)){
1346 struct sk_buff_head
*rcvq
= &sk
->sk_receive_queue
;
1347 struct sk_buff
*skb
;
1349 spin_lock_bh(&rcvq
->lock
);
1350 while ((skb
= skb_peek(rcvq
)) != NULL
) {
1351 if (udp_checksum_complete(skb
)) {
1352 UDP_INC_STATS_BH(UDP_MIB_INERRORS
);
1353 __skb_unlink(skb
, rcvq
);
1356 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
1360 spin_unlock_bh(&rcvq
->lock
);
1362 /* nothing to see, move along */
1364 mask
&= ~(POLLIN
| POLLRDNORM
);
1371 struct proto udp_prot
= {
1373 .owner
= THIS_MODULE
,
1375 .connect
= ip4_datagram_connect
,
1376 .disconnect
= udp_disconnect
,
1378 .destroy
= udp_destroy_sock
,
1379 .setsockopt
= udp_setsockopt
,
1380 .getsockopt
= udp_getsockopt
,
1381 .sendmsg
= udp_sendmsg
,
1382 .recvmsg
= udp_recvmsg
,
1383 .sendpage
= udp_sendpage
,
1384 .backlog_rcv
= udp_queue_rcv_skb
,
1385 .hash
= udp_v4_hash
,
1386 .unhash
= udp_v4_unhash
,
1387 .get_port
= udp_v4_get_port
,
1388 .obj_size
= sizeof(struct udp_sock
),
1389 #ifdef CONFIG_COMPAT
1390 .compat_setsockopt
= compat_udp_setsockopt
,
1391 .compat_getsockopt
= compat_udp_getsockopt
,
1395 /* ------------------------------------------------------------------------ */
1396 #ifdef CONFIG_PROC_FS
1398 static struct sock
*udp_get_first(struct seq_file
*seq
)
1401 struct udp_iter_state
*state
= seq
->private;
1403 for (state
->bucket
= 0; state
->bucket
< UDP_HTABLE_SIZE
; ++state
->bucket
) {
1404 struct hlist_node
*node
;
1405 sk_for_each(sk
, node
, &udp_hash
[state
->bucket
]) {
1406 if (sk
->sk_family
== state
->family
)
1415 static struct sock
*udp_get_next(struct seq_file
*seq
, struct sock
*sk
)
1417 struct udp_iter_state
*state
= seq
->private;
1423 } while (sk
&& sk
->sk_family
!= state
->family
);
1425 if (!sk
&& ++state
->bucket
< UDP_HTABLE_SIZE
) {
1426 sk
= sk_head(&udp_hash
[state
->bucket
]);
1432 static struct sock
*udp_get_idx(struct seq_file
*seq
, loff_t pos
)
1434 struct sock
*sk
= udp_get_first(seq
);
1437 while(pos
&& (sk
= udp_get_next(seq
, sk
)) != NULL
)
1439 return pos
? NULL
: sk
;
1442 static void *udp_seq_start(struct seq_file
*seq
, loff_t
*pos
)
1444 read_lock(&udp_hash_lock
);
1445 return *pos
? udp_get_idx(seq
, *pos
-1) : (void *)1;
1448 static void *udp_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
1453 sk
= udp_get_idx(seq
, 0);
1455 sk
= udp_get_next(seq
, v
);
1461 static void udp_seq_stop(struct seq_file
*seq
, void *v
)
1463 read_unlock(&udp_hash_lock
);
1466 static int udp_seq_open(struct inode
*inode
, struct file
*file
)
1468 struct udp_seq_afinfo
*afinfo
= PDE(inode
)->data
;
1469 struct seq_file
*seq
;
1471 struct udp_iter_state
*s
= kzalloc(sizeof(*s
), GFP_KERNEL
);
1475 s
->family
= afinfo
->family
;
1476 s
->seq_ops
.start
= udp_seq_start
;
1477 s
->seq_ops
.next
= udp_seq_next
;
1478 s
->seq_ops
.show
= afinfo
->seq_show
;
1479 s
->seq_ops
.stop
= udp_seq_stop
;
1481 rc
= seq_open(file
, &s
->seq_ops
);
1485 seq
= file
->private_data
;
1494 /* ------------------------------------------------------------------------ */
1495 int udp_proc_register(struct udp_seq_afinfo
*afinfo
)
1497 struct proc_dir_entry
*p
;
1502 afinfo
->seq_fops
->owner
= afinfo
->owner
;
1503 afinfo
->seq_fops
->open
= udp_seq_open
;
1504 afinfo
->seq_fops
->read
= seq_read
;
1505 afinfo
->seq_fops
->llseek
= seq_lseek
;
1506 afinfo
->seq_fops
->release
= seq_release_private
;
1508 p
= proc_net_fops_create(afinfo
->name
, S_IRUGO
, afinfo
->seq_fops
);
1516 void udp_proc_unregister(struct udp_seq_afinfo
*afinfo
)
1520 proc_net_remove(afinfo
->name
);
1521 memset(afinfo
->seq_fops
, 0, sizeof(*afinfo
->seq_fops
));
1524 /* ------------------------------------------------------------------------ */
1525 static void udp4_format_sock(struct sock
*sp
, char *tmpbuf
, int bucket
)
1527 struct inet_sock
*inet
= inet_sk(sp
);
1528 unsigned int dest
= inet
->daddr
;
1529 unsigned int src
= inet
->rcv_saddr
;
1530 __u16 destp
= ntohs(inet
->dport
);
1531 __u16 srcp
= ntohs(inet
->sport
);
1533 sprintf(tmpbuf
, "%4d: %08X:%04X %08X:%04X"
1534 " %02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p",
1535 bucket
, src
, srcp
, dest
, destp
, sp
->sk_state
,
1536 atomic_read(&sp
->sk_wmem_alloc
),
1537 atomic_read(&sp
->sk_rmem_alloc
),
1538 0, 0L, 0, sock_i_uid(sp
), 0, sock_i_ino(sp
),
1539 atomic_read(&sp
->sk_refcnt
), sp
);
1542 static int udp4_seq_show(struct seq_file
*seq
, void *v
)
1544 if (v
== SEQ_START_TOKEN
)
1545 seq_printf(seq
, "%-127s\n",
1546 " sl local_address rem_address st tx_queue "
1547 "rx_queue tr tm->when retrnsmt uid timeout "
1551 struct udp_iter_state
*state
= seq
->private;
1553 udp4_format_sock(v
, tmpbuf
, state
->bucket
);
1554 seq_printf(seq
, "%-127s\n", tmpbuf
);
1559 /* ------------------------------------------------------------------------ */
1560 static struct file_operations udp4_seq_fops
;
1561 static struct udp_seq_afinfo udp4_seq_afinfo
= {
1562 .owner
= THIS_MODULE
,
1565 .seq_show
= udp4_seq_show
,
1566 .seq_fops
= &udp4_seq_fops
,
1569 int __init
udp4_proc_init(void)
1571 return udp_proc_register(&udp4_seq_afinfo
);
1574 void udp4_proc_exit(void)
1576 udp_proc_unregister(&udp4_seq_afinfo
);
1578 #endif /* CONFIG_PROC_FS */
1580 EXPORT_SYMBOL(udp_disconnect
);
1581 EXPORT_SYMBOL(udp_hash
);
1582 EXPORT_SYMBOL(udp_hash_lock
);
1583 EXPORT_SYMBOL(udp_ioctl
);
1584 EXPORT_SYMBOL(udp_port_rover
);
1585 EXPORT_SYMBOL(udp_prot
);
1586 EXPORT_SYMBOL(udp_sendmsg
);
1587 EXPORT_SYMBOL(udp_poll
);
1589 #ifdef CONFIG_PROC_FS
1590 EXPORT_SYMBOL(udp_proc_register
);
1591 EXPORT_SYMBOL(udp_proc_unregister
);