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>
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
)
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
)
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
;
152 write_lock_bh(&udp_hash_lock
);
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
++) {
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));
173 sk_for_each(sk2
, node
, head
)
174 if (++size
< best_size_so_far
) {
175 best_size_so_far
= size
;
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
))
188 if (i
>= (1 << 16) / UDP_HTABLE_SIZE
)
191 udp_port_rover
= snum
= result
;
193 head
= &udp_hash
[snum
& (UDP_HTABLE_SIZE
- 1)];
195 sk_for_each(sk2
, node
, head
)
196 if (inet_sk(sk2
)->num
== snum
&&
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
) )
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
);
212 write_unlock_bh(&udp_hash_lock
);
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
)
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
);
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
)
268 if (inet
->daddr
!= saddr
)
273 if (inet
->dport
!= sport
)
277 if (sk
->sk_bound_dev_if
) {
278 if (sk
->sk_bound_dev_if
!= dif
)
285 } else if(score
> badness
) {
294 static __inline__
struct sock
*udp_v4_lookup(__be32 saddr
, __be16 sport
,
295 __be32 daddr
, __be16 dport
, int dif
)
299 read_lock(&udp_hash_lock
);
300 sk
= udp_v4_lookup_longway(saddr
, sport
, daddr
, dport
, dif
);
303 read_unlock(&udp_hash_lock
);
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
,
312 struct hlist_node
*node
;
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
) ||
324 (s
->sk_bound_dev_if
&& s
->sk_bound_dev_if
!= dif
))
326 if (!ip_mc_sf_allow(s
, loc_addr
, rmt_addr
, dif
))
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
;
357 sk
= udp_v4_lookup(iph
->daddr
, uh
->dest
, iph
->saddr
, uh
->source
, skb
->dev
->ifindex
);
359 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS
);
360 return; /* No socket for error */
369 case ICMP_TIME_EXCEEDED
:
372 case ICMP_SOURCE_QUENCH
:
374 case ICMP_PARAMETERPROB
:
378 case ICMP_DEST_UNREACH
:
379 if (code
== ICMP_FRAG_NEEDED
) { /* Path MTU discovery */
380 if (inet
->pmtudisc
!= IP_PMTUDISC_DONT
) {
388 if (code
<= NR_ICMP_UNREACH
) {
389 harderr
= icmp_err_convert
[code
].fatal
;
390 err
= icmp_err_convert
[code
].errno
;
396 * RFC1122: OK. Passes ICMP errors back to application, as per
399 if (!inet
->recverr
) {
400 if (!harderr
|| sk
->sk_state
!= TCP_ESTABLISHED
)
403 ip_icmp_error(sk
, skb
, err
, uh
->dest
, info
, (u8
*)(uh
+1));
406 sk
->sk_error_report(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
);
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
;
436 /* Grab the skbuff where UDP header space exists. */
437 if ((skb
= skb_peek(&sk
->sk_write_queue
)) == NULL
)
441 * Create a UDP header
444 uh
->source
= fl
->fl_ip_sport
;
445 uh
->dest
= fl
->fl_ip_dport
;
446 uh
->len
= htons(up
->len
);
449 if (sk
->sk_no_check
== UDP_CSUM_NOXMIT
) {
450 skb
->ip_summed
= CHECKSUM_NONE
;
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);
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
);
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
;
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
);
496 err
= ip_push_pending_frames(sk
);
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
,
512 struct inet_sock
*inet
= inet_sk(sk
);
513 struct udp_sock
*up
= udp_sk(sk
);
515 struct ipcm_cookie ipc
;
516 struct rtable
*rt
= NULL
;
519 __be32 daddr
, faddr
, saddr
;
523 int corkreq
= up
->corkflag
|| msg
->msg_flags
&MSG_MORE
;
532 if (msg
->msg_flags
&MSG_OOB
) /* Mirror BSD error message compatibility */
539 * There are pending frames.
540 * The socket lock must be held while it's corked.
543 if (likely(up
->pending
)) {
544 if (unlikely(up
->pending
!= AF_INET
)) {
552 ulen
+= sizeof(struct udphdr
);
555 * Get and verify the address.
558 struct sockaddr_in
* usin
= (struct sockaddr_in
*)msg
->msg_name
;
559 if (msg
->msg_namelen
< sizeof(*usin
))
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
;
571 if (sk
->sk_state
!= TCP_ESTABLISHED
)
572 return -EDESTADDRREQ
;
575 /* Open fast path for connected socket.
576 Route will not be used, if at least one option is set.
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
);
595 ipc
.addr
= faddr
= daddr
;
597 if (ipc
.opt
&& ipc
.opt
->srr
) {
600 faddr
= ipc
.opt
->faddr
;
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
)) {
611 if (MULTICAST(daddr
)) {
613 ipc
.oif
= inet
->mc_index
;
615 saddr
= inet
->mc_addr
;
620 rt
= (struct rtable
*)sk_dst_check(sk
, 0);
623 struct flowi fl
= { .oif
= ipc
.oif
,
628 .proto
= IPPROTO_UDP
,
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
));
638 if ((rt
->rt_flags
& RTCF_BROADCAST
) &&
639 !sock_flag(sk
, SOCK_BROADCAST
))
642 sk_dst_set(sk
, dst_clone(&rt
->u
.dst
));
645 if (msg
->msg_flags
&MSG_CONFIRM
)
651 daddr
= ipc
.addr
= rt
->rt_dst
;
654 if (unlikely(up
->pending
)) {
655 /* The socket is already corked while preparing it. */
656 /* ... which is an evident application bug. --ANK */
659 LIMIT_NETDEBUG(KERN_DEBUG
"udp cork app bug 2\n");
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
;
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
);
678 udp_flush_pending_frames(sk
);
680 err
= udp_push_pending_frames(sk
, up
);
681 else if (unlikely(skb_queue_empty(&sk
->sk_write_queue
)))
690 UDP_INC_STATS_USER(UDP_MIB_OUTDATAGRAMS
);
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
);
706 dst_confirm(&rt
->u
.dst
);
707 if (!(msg
->msg_flags
&MSG_PROBE
) || len
)
708 goto back_from_confirm
;
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
);
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);
733 if (unlikely(!up
->pending
)) {
736 LIMIT_NETDEBUG(KERN_DEBUG
"udp cork app bug 3\n");
740 ret
= ip_append_page(sk
, page
, offset
, size
, flags
);
741 if (ret
== -EOPNOTSUPP
) {
743 return sock_no_sendpage(sk
->sk_socket
, page
, offset
,
747 udp_flush_pending_frames(sk
);
752 if (!(up
->corkflag
|| (flags
&MSG_MORE
)))
753 ret
= udp_push_pending_frames(sk
, up
);
762 * IOCTL requests applicable to the UDP protocol
765 int udp_ioctl(struct sock
*sk
, int cmd
, unsigned long arg
)
771 int amount
= atomic_read(&sk
->sk_wmem_alloc
);
772 return put_user(amount
, (int __user
*)arg
);
778 unsigned long amount
;
781 spin_lock_bh(&sk
->sk_receive_queue
.lock
);
782 skb
= skb_peek(&sk
->sk_receive_queue
);
785 * We will only return the amount
786 * of this packet since that is all
789 amount
= skb
->len
- sizeof(struct udphdr
);
791 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
792 return put_user(amount
, (int __user
*)arg
);
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
;
826 * Check any passed addresses
829 *addr_len
=sizeof(*sin
);
831 if (flags
& MSG_ERRQUEUE
)
832 return ip_recv_error(sk
, msg
, len
);
835 skb
= skb_recv_datagram(sk
, flags
, noblock
, &err
);
839 copied
= skb
->len
- sizeof(struct udphdr
);
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
,
848 } else if (msg
->msg_flags
&MSG_TRUNC
) {
849 if (__udp_checksum_complete(skb
))
851 err
= skb_copy_datagram_iovec(skb
, sizeof(struct udphdr
), msg
->msg_iov
,
854 err
= skb_copy_and_csum_datagram_iovec(skb
, sizeof(struct udphdr
), msg
->msg_iov
);
863 sock_recv_timestamp(msg
, sk
, skb
);
865 /* Copy the address. */
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
);
877 if (flags
& MSG_TRUNC
)
878 err
= skb
->len
- sizeof(struct udphdr
);
881 skb_free_datagram(sk
, skb
);
886 UDP_INC_STATS_BH(UDP_MIB_INERRORS
);
888 skb_kill_datagram(sk
, skb
, flags
);
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
;
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
);
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
;
929 static xfrm4_rcv_encap_t xfrm4_rcv_encap_func
= NULL
;
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
;
940 if(xfrm4_rcv_encap_func
!= NULL
)
944 xfrm4_rcv_encap_func
= func
;
948 int udp4_unregister_esp_rcvencap(xfrm4_rcv_encap_t func
)
950 if(xfrm4_rcv_encap_func
!= func
)
953 xfrm4_rcv_encap_func
= NULL
;
956 #endif /* CONFIG_XFRM_MODULE || CONFIG_IPSEC_NAT_TRAVERSAL */
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)
968 #else /* either CONFIG_XFRM or CONFIG_IPSEC_NAT_TRAVERSAL */
969 struct udp_sock
*up
= udp_sk(sk
);
976 __u16 encap_type
= up
->encap_type
;
978 /* if we're overly short, let UDP handle it */
979 len
= skb
->len
- sizeof(struct udphdr
);
983 /* if this is not encapsulated socket, then just return now */
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)))
992 /* Now we can get the pointers */
994 udpdata
= (__u8
*)uh
+ sizeof(struct udphdr
);
995 udpdata32
= (__be32
*)udpdata
;
997 switch (encap_type
) {
999 case UDP_ENCAP_ESPINUDP
:
1000 /* Check if this is a keepalive packet. If so, eat it. */
1001 if (len
== 1 && udpdata
[0] == 0xff) {
1003 } else if (len
> sizeof(struct ip_esp_hdr
) && udpdata32
[0] != 0 ) {
1004 /* ESP Packet without Non-ESP header */
1005 len
= sizeof(struct udphdr
);
1007 /* Must be an IKE packet.. pass it through */
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) {
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
);
1020 /* Must be an IKE packet.. pass it through */
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
))
1032 if (skb_cloned(skb
) && pskb_expand_head(skb
, 0, 0, GFP_ATOMIC
))
1035 /* Now we can update and verify the packet length... */
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!?! */
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
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... */
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
);
1072 * Charge it to the socket, dropping if the queue is full.
1074 if (!xfrm4_policy_check(sk
, XFRM_POLICY_IN
, 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.
1092 ret
= udp_encap_rcv(sk
, skb
);
1094 /* Eat the packet .. */
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
);
1103 UDP_INC_STATS_BH(UDP_MIB_INERRORS
);
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
);
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 */
1123 UDP_INC_STATS_BH(UDP_MIB_RCVBUFERRORS
);
1124 UDP_INC_STATS_BH(UDP_MIB_INERRORS
);
1128 UDP_INC_STATS_BH(UDP_MIB_INDATAGRAMS
);
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
)
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
);
1149 struct sock
*sknext
= NULL
;
1152 struct sk_buff
*skb1
= skb
;
1154 sknext
= udp_v4_mcast_next(sk_next(sk
), uh
->dest
, daddr
,
1155 uh
->source
, saddr
, dif
);
1157 skb1
= skb_clone(skb
, GFP_ATOMIC
);
1160 int ret
= udp_queue_rcv_skb(sk
, skb1
);
1162 /* we should probably re-process instead
1163 * of dropping packets here. */
1170 read_unlock(&udp_hash_lock
);
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
)
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
;
1210 * Validate the packet and the UDP length.
1212 if (!pskb_may_pull(skb
, sizeof(struct udphdr
)))
1217 ulen
= ntohs(uh
->len
);
1219 if (ulen
> len
|| ulen
< sizeof(*uh
))
1222 if (pskb_trim_rcsum(skb
, ulen
))
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
);
1233 int ret
= udp_queue_rcv_skb(sk
, skb
);
1236 /* a return value > 0 means to resubmit the input, but
1237 * it it wants the return to be -protocol, or 0
1244 if (!xfrm4_policy_check(NULL
, XFRM_POLICY_IN
, skb
))
1248 /* No socket. Drop packet silently, if checksum is wrong */
1249 if (udp_checksum_complete(skb
))
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.
1263 LIMIT_NETDEBUG(KERN_DEBUG
"UDP: short packet: From %u.%u.%u.%u:%u %d/%d to %u.%u.%u.%u:%u\n",
1271 UDP_INC_STATS_BH(UDP_MIB_INERRORS
);
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",
1287 UDP_INC_STATS_BH(UDP_MIB_INERRORS
);
1292 static int udp_destroy_sock(struct sock
*sk
)
1295 udp_flush_pending_frames(sk
);
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
);
1310 if(optlen
<sizeof(int))
1313 if (get_user(val
, (int __user
*)optval
))
1323 udp_push_pending_frames(sk
, up
);
1331 case UDP_ENCAP_ESPINUDP
:
1332 case UDP_ENCAP_ESPINUDP_NON_IKE
:
1333 up
->encap_type
= val
;
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
);
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
);
1373 if(get_user(len
,optlen
))
1376 len
= min_t(unsigned int, len
, sizeof(int));
1387 val
= up
->encap_type
;
1391 return -ENOPROTOOPT
;
1394 if(put_user(len
, optlen
))
1396 if(copy_to_user(optval
, &val
,len
))
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
);
1419 * udp_poll - wait for a UDP event.
1420 * @file - file struct
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
);
1450 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
1454 spin_unlock_bh(&rcvq
->lock
);
1456 /* nothing to see, move along */
1458 mask
&= ~(POLLIN
| POLLRDNORM
);
1465 struct proto udp_prot
= {
1467 .owner
= THIS_MODULE
,
1469 .connect
= ip4_datagram_connect
,
1470 .disconnect
= udp_disconnect
,
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
,
1489 /* ------------------------------------------------------------------------ */
1490 #ifdef CONFIG_PROC_FS
1492 static struct sock
*udp_get_first(struct seq_file
*seq
)
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
)
1509 static struct sock
*udp_get_next(struct seq_file
*seq
, struct sock
*sk
)
1511 struct udp_iter_state
*state
= seq
->private;
1517 } while (sk
&& sk
->sk_family
!= state
->family
);
1519 if (!sk
&& ++state
->bucket
< UDP_HTABLE_SIZE
) {
1520 sk
= sk_head(&udp_hash
[state
->bucket
]);
1526 static struct sock
*udp_get_idx(struct seq_file
*seq
, loff_t pos
)
1528 struct sock
*sk
= udp_get_first(seq
);
1531 while(pos
&& (sk
= udp_get_next(seq
, sk
)) != NULL
)
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
)
1547 sk
= udp_get_idx(seq
, 0);
1549 sk
= udp_get_next(seq
, v
);
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
;
1565 struct udp_iter_state
*s
= kzalloc(sizeof(*s
), GFP_KERNEL
);
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
);
1579 seq
= file
->private_data
;
1588 /* ------------------------------------------------------------------------ */
1589 int udp_proc_register(struct udp_seq_afinfo
*afinfo
)
1591 struct proc_dir_entry
*p
;
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
);
1610 void udp_proc_unregister(struct udp_seq_afinfo
*afinfo
)
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 "
1645 struct udp_iter_state
*state
= seq
->private;
1647 udp4_format_sock(v
, tmpbuf
, state
->bucket
);
1648 seq_printf(seq
, "%-127s\n", tmpbuf
);
1653 /* ------------------------------------------------------------------------ */
1654 static struct file_operations udp4_seq_fops
;
1655 static struct udp_seq_afinfo udp4_seq_afinfo
= {
1656 .owner
= THIS_MODULE
,
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
);
1688 #if defined(CONFIG_IPSEC_NAT_TRAVERSAL)
1689 EXPORT_SYMBOL(udp4_register_esp_rcvencap
);
1690 EXPORT_SYMBOL(udp4_unregister_esp_rcvencap
);