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/netdevice.h>
96 #include <net/tcp_states.h>
97 #include <linux/skbuff.h>
98 #include <linux/proc_fs.h>
99 #include <linux/seq_file.h>
100 #include <net/icmp.h>
101 #include <net/route.h>
102 #include <net/checksum.h>
103 #include <net/xfrm.h>
104 #include "udp_impl.h"
107 * Snmp MIB for the UDP layer
110 DEFINE_SNMP_STAT(struct udp_mib
, udp_statistics
) __read_mostly
;
112 struct hlist_head udp_hash
[UDP_HTABLE_SIZE
];
113 DEFINE_RWLOCK(udp_hash_lock
);
115 static int udp_port_rover
;
118 * Note about this hash function :
119 * Typical use is probably daddr = 0, only dport is going to vary hash
121 static inline unsigned int udp_hash_port(__u16 port
)
126 static inline int __udp_lib_port_inuse(unsigned int hash
, int port
,
127 const struct sock
*this_sk
,
128 struct hlist_head udptable
[],
129 const struct udp_get_port_ops
*ops
)
132 struct hlist_node
*node
;
133 struct inet_sock
*inet
;
135 sk_for_each(sk
, node
, &udptable
[hash
& (UDP_HTABLE_SIZE
- 1)]) {
136 if (sk
->sk_hash
!= hash
)
139 if (inet
->num
!= port
)
142 if (ops
->saddr_cmp(sk
, this_sk
))
144 } else if (ops
->saddr_any(sk
))
151 * __udp_lib_get_port - UDP/-Lite port lookup for IPv4 and IPv6
153 * @sk: socket struct in question
154 * @snum: port number to look up
155 * @udptable: hash list table, must be of UDP_HTABLE_SIZE
156 * @port_rover: pointer to record of last unallocated port
157 * @ops: AF-dependent address operations
159 int __udp_lib_get_port(struct sock
*sk
, unsigned short snum
,
160 struct hlist_head udptable
[], int *port_rover
,
161 const struct udp_get_port_ops
*ops
)
163 struct hlist_node
*node
;
164 struct hlist_head
*head
;
169 write_lock_bh(&udp_hash_lock
);
171 int best_size_so_far
, best
, result
, i
;
173 if (*port_rover
> sysctl_local_port_range
[1] ||
174 *port_rover
< sysctl_local_port_range
[0])
175 *port_rover
= sysctl_local_port_range
[0];
176 best_size_so_far
= 32767;
177 best
= result
= *port_rover
;
178 for (i
= 0; i
< UDP_HTABLE_SIZE
; i
++, result
++) {
181 hash
= ops
->hash_port_and_rcv_saddr(result
, sk
);
182 head
= &udptable
[hash
& (UDP_HTABLE_SIZE
- 1)];
183 if (hlist_empty(head
)) {
184 if (result
> sysctl_local_port_range
[1])
185 result
= sysctl_local_port_range
[0] +
186 ((result
- sysctl_local_port_range
[0]) &
187 (UDP_HTABLE_SIZE
- 1));
191 sk_for_each(sk2
, node
, head
) {
192 if (++size
>= best_size_so_far
)
195 best_size_so_far
= size
;
201 for (i
= 0; i
< (1 << 16) / UDP_HTABLE_SIZE
;
202 i
++, result
+= UDP_HTABLE_SIZE
) {
203 if (result
> sysctl_local_port_range
[1])
204 result
= sysctl_local_port_range
[0]
205 + ((result
- sysctl_local_port_range
[0]) &
206 (UDP_HTABLE_SIZE
- 1));
207 hash
= udp_hash_port(result
);
208 if (__udp_lib_port_inuse(hash
, result
,
209 NULL
, udptable
, ops
))
211 if (ops
->saddr_any(sk
))
214 hash
= ops
->hash_port_and_rcv_saddr(result
, sk
);
215 if (! __udp_lib_port_inuse(hash
, result
,
219 if (i
>= (1 << 16) / UDP_HTABLE_SIZE
)
222 *port_rover
= snum
= result
;
224 hash
= udp_hash_port(snum
);
225 head
= &udptable
[hash
& (UDP_HTABLE_SIZE
- 1)];
227 sk_for_each(sk2
, node
, head
)
228 if (sk2
->sk_hash
== hash
&&
230 inet_sk(sk2
)->num
== snum
&&
231 (!sk2
->sk_reuse
|| !sk
->sk_reuse
) &&
232 (!sk2
->sk_bound_dev_if
|| !sk
->sk_bound_dev_if
||
233 sk2
->sk_bound_dev_if
== sk
->sk_bound_dev_if
) &&
234 ops
->saddr_cmp(sk
, sk2
))
237 if (!ops
->saddr_any(sk
)) {
238 hash
= ops
->hash_port_and_rcv_saddr(snum
, sk
);
239 head
= &udptable
[hash
& (UDP_HTABLE_SIZE
- 1)];
241 sk_for_each(sk2
, node
, head
)
242 if (sk2
->sk_hash
== hash
&&
244 inet_sk(sk2
)->num
== snum
&&
245 (!sk2
->sk_reuse
|| !sk
->sk_reuse
) &&
246 (!sk2
->sk_bound_dev_if
||
247 !sk
->sk_bound_dev_if
||
248 sk2
->sk_bound_dev_if
==
249 sk
->sk_bound_dev_if
) &&
250 ops
->saddr_cmp(sk
, sk2
))
254 inet_sk(sk
)->num
= snum
;
256 if (sk_unhashed(sk
)) {
257 head
= &udptable
[hash
& (UDP_HTABLE_SIZE
- 1)];
258 sk_add_node(sk
, head
);
259 sock_prot_inc_use(sk
->sk_prot
);
263 write_unlock_bh(&udp_hash_lock
);
267 int udp_get_port(struct sock
*sk
, unsigned short snum
,
268 const struct udp_get_port_ops
*ops
)
270 return __udp_lib_get_port(sk
, snum
, udp_hash
, &udp_port_rover
, ops
);
273 static int ipv4_rcv_saddr_equal(const struct sock
*sk1
, const struct sock
*sk2
)
275 struct inet_sock
*inet1
= inet_sk(sk1
), *inet2
= inet_sk(sk2
);
277 return ( !ipv6_only_sock(sk2
) &&
278 (!inet1
->rcv_saddr
|| !inet2
->rcv_saddr
||
279 inet1
->rcv_saddr
== inet2
->rcv_saddr
));
282 static int ipv4_rcv_saddr_any(const struct sock
*sk
)
284 return !inet_sk(sk
)->rcv_saddr
;
287 static inline unsigned int ipv4_hash_port_and_addr(__u16 port
, __be32 addr
)
294 static unsigned int ipv4_hash_port_and_rcv_saddr(__u16 port
,
295 const struct sock
*sk
)
297 return ipv4_hash_port_and_addr(port
, inet_sk(sk
)->rcv_saddr
);
300 const struct udp_get_port_ops udp_ipv4_ops
= {
301 .saddr_cmp
= ipv4_rcv_saddr_equal
,
302 .saddr_any
= ipv4_rcv_saddr_any
,
303 .hash_port_and_rcv_saddr
= ipv4_hash_port_and_rcv_saddr
,
306 static inline int udp_v4_get_port(struct sock
*sk
, unsigned short snum
)
308 return udp_get_port(sk
, snum
, &udp_ipv4_ops
);
311 /* UDP is nearly always wildcards out the wazoo, it makes no sense to try
312 * harder than this. -DaveM
314 static struct sock
*__udp4_lib_lookup(__be32 saddr
, __be16 sport
,
315 __be32 daddr
, __be16 dport
,
316 int dif
, struct hlist_head udptable
[])
318 struct sock
*sk
, *result
= NULL
;
319 struct hlist_node
*node
;
320 unsigned int hash
, hashwild
;
321 int score
, best
= -1, hport
= ntohs(dport
);
323 hash
= ipv4_hash_port_and_addr(hport
, daddr
);
324 hashwild
= udp_hash_port(hport
);
326 read_lock(&udp_hash_lock
);
330 sk_for_each(sk
, node
, &udptable
[hash
& (UDP_HTABLE_SIZE
- 1)]) {
331 struct inet_sock
*inet
= inet_sk(sk
);
333 if (sk
->sk_hash
!= hash
|| ipv6_only_sock(sk
) ||
337 score
= (sk
->sk_family
== PF_INET
? 1 : 0);
338 if (inet
->rcv_saddr
) {
339 if (inet
->rcv_saddr
!= daddr
)
344 if (inet
->daddr
!= saddr
)
349 if (inet
->dport
!= sport
)
353 if (sk
->sk_bound_dev_if
) {
354 if (sk
->sk_bound_dev_if
!= dif
)
361 } else if (score
> best
) {
367 if (hash
!= hashwild
) {
374 read_unlock(&udp_hash_lock
);
378 static inline struct sock
*udp_v4_mcast_next(struct sock
*sk
, unsigned int hnum
,
379 int hport
, __be32 loc_addr
,
380 __be16 rmt_port
, __be32 rmt_addr
,
383 struct hlist_node
*node
;
386 sk_for_each_from(s
, node
) {
387 struct inet_sock
*inet
= inet_sk(s
);
389 if (s
->sk_hash
!= hnum
||
390 inet
->num
!= hport
||
391 (inet
->daddr
&& inet
->daddr
!= rmt_addr
) ||
392 (inet
->dport
!= rmt_port
&& inet
->dport
) ||
393 (inet
->rcv_saddr
&& inet
->rcv_saddr
!= loc_addr
) ||
395 (s
->sk_bound_dev_if
&& s
->sk_bound_dev_if
!= dif
))
397 if (!ip_mc_sf_allow(s
, loc_addr
, rmt_addr
, dif
))
407 * This routine is called by the ICMP module when it gets some
408 * sort of error condition. If err < 0 then the socket should
409 * be closed and the error returned to the user. If err > 0
410 * it's just the icmp type << 8 | icmp code.
411 * Header points to the ip header of the error packet. We move
412 * on past this. Then (as it used to claim before adjustment)
413 * header points to the first 8 bytes of the udp header. We need
414 * to find the appropriate port.
417 void __udp4_lib_err(struct sk_buff
*skb
, u32 info
, struct hlist_head udptable
[])
419 struct inet_sock
*inet
;
420 struct iphdr
*iph
= (struct iphdr
*)skb
->data
;
421 struct udphdr
*uh
= (struct udphdr
*)(skb
->data
+(iph
->ihl
<<2));
422 const int type
= icmp_hdr(skb
)->type
;
423 const int code
= icmp_hdr(skb
)->code
;
428 sk
= __udp4_lib_lookup(iph
->daddr
, uh
->dest
, iph
->saddr
, uh
->source
,
429 skb
->dev
->ifindex
, udptable
);
431 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS
);
432 return; /* No socket for error */
441 case ICMP_TIME_EXCEEDED
:
444 case ICMP_SOURCE_QUENCH
:
446 case ICMP_PARAMETERPROB
:
450 case ICMP_DEST_UNREACH
:
451 if (code
== ICMP_FRAG_NEEDED
) { /* Path MTU discovery */
452 if (inet
->pmtudisc
!= IP_PMTUDISC_DONT
) {
460 if (code
<= NR_ICMP_UNREACH
) {
461 harderr
= icmp_err_convert
[code
].fatal
;
462 err
= icmp_err_convert
[code
].errno
;
468 * RFC1122: OK. Passes ICMP errors back to application, as per
471 if (!inet
->recverr
) {
472 if (!harderr
|| sk
->sk_state
!= TCP_ESTABLISHED
)
475 ip_icmp_error(sk
, skb
, err
, uh
->dest
, info
, (u8
*)(uh
+1));
478 sk
->sk_error_report(sk
);
483 void udp_err(struct sk_buff
*skb
, u32 info
)
485 return __udp4_lib_err(skb
, info
, udp_hash
);
489 * Throw away all pending data and cancel the corking. Socket is locked.
491 static void udp_flush_pending_frames(struct sock
*sk
)
493 struct udp_sock
*up
= udp_sk(sk
);
498 ip_flush_pending_frames(sk
);
503 * udp4_hwcsum_outgoing - handle outgoing HW checksumming
504 * @sk: socket we are sending on
505 * @skb: sk_buff containing the filled-in UDP header
506 * (checksum field must be zeroed out)
508 static void udp4_hwcsum_outgoing(struct sock
*sk
, struct sk_buff
*skb
,
509 __be32 src
, __be32 dst
, int len
)
512 struct udphdr
*uh
= udp_hdr(skb
);
515 if (skb_queue_len(&sk
->sk_write_queue
) == 1) {
517 * Only one fragment on the socket.
519 skb
->csum_start
= skb_transport_header(skb
) - skb
->head
;
520 skb
->csum_offset
= offsetof(struct udphdr
, check
);
521 uh
->check
= ~csum_tcpudp_magic(src
, dst
, len
, IPPROTO_UDP
, 0);
524 * HW-checksum won't work as there are two or more
525 * fragments on the socket so that all csums of sk_buffs
528 offset
= skb_transport_offset(skb
);
529 skb
->csum
= skb_checksum(skb
, offset
, skb
->len
- offset
, 0);
531 skb
->ip_summed
= CHECKSUM_NONE
;
533 skb_queue_walk(&sk
->sk_write_queue
, skb
) {
534 csum
= csum_add(csum
, skb
->csum
);
537 uh
->check
= csum_tcpudp_magic(src
, dst
, len
, IPPROTO_UDP
, csum
);
539 uh
->check
= CSUM_MANGLED_0
;
544 * Push out all pending data as one UDP datagram. Socket is locked.
546 static int udp_push_pending_frames(struct sock
*sk
)
548 struct udp_sock
*up
= udp_sk(sk
);
549 struct inet_sock
*inet
= inet_sk(sk
);
550 struct flowi
*fl
= &inet
->cork
.fl
;
556 /* Grab the skbuff where UDP header space exists. */
557 if ((skb
= skb_peek(&sk
->sk_write_queue
)) == NULL
)
561 * Create a UDP header
564 uh
->source
= fl
->fl_ip_sport
;
565 uh
->dest
= fl
->fl_ip_dport
;
566 uh
->len
= htons(up
->len
);
569 if (up
->pcflag
) /* UDP-Lite */
570 csum
= udplite_csum_outgoing(sk
, skb
);
572 else if (sk
->sk_no_check
== UDP_CSUM_NOXMIT
) { /* UDP csum disabled */
574 skb
->ip_summed
= CHECKSUM_NONE
;
577 } else if (skb
->ip_summed
== CHECKSUM_PARTIAL
) { /* UDP hardware csum */
579 udp4_hwcsum_outgoing(sk
, skb
, fl
->fl4_src
,fl
->fl4_dst
, up
->len
);
582 } else /* `normal' UDP */
583 csum
= udp_csum_outgoing(sk
, skb
);
585 /* add protocol-dependent pseudo-header */
586 uh
->check
= csum_tcpudp_magic(fl
->fl4_src
, fl
->fl4_dst
, up
->len
,
587 sk
->sk_protocol
, csum
);
589 uh
->check
= CSUM_MANGLED_0
;
592 err
= ip_push_pending_frames(sk
);
599 int udp_sendmsg(struct kiocb
*iocb
, struct sock
*sk
, struct msghdr
*msg
,
602 struct inet_sock
*inet
= inet_sk(sk
);
603 struct udp_sock
*up
= udp_sk(sk
);
605 struct ipcm_cookie ipc
;
606 struct rtable
*rt
= NULL
;
609 __be32 daddr
, faddr
, saddr
;
612 int err
, is_udplite
= up
->pcflag
;
613 int corkreq
= up
->corkflag
|| msg
->msg_flags
&MSG_MORE
;
614 int (*getfrag
)(void *, char *, int, int, int, struct sk_buff
*);
623 if (msg
->msg_flags
&MSG_OOB
) /* Mirror BSD error message compatibility */
630 * There are pending frames.
631 * The socket lock must be held while it's corked.
634 if (likely(up
->pending
)) {
635 if (unlikely(up
->pending
!= AF_INET
)) {
643 ulen
+= sizeof(struct udphdr
);
646 * Get and verify the address.
649 struct sockaddr_in
* usin
= (struct sockaddr_in
*)msg
->msg_name
;
650 if (msg
->msg_namelen
< sizeof(*usin
))
652 if (usin
->sin_family
!= AF_INET
) {
653 if (usin
->sin_family
!= AF_UNSPEC
)
654 return -EAFNOSUPPORT
;
657 daddr
= usin
->sin_addr
.s_addr
;
658 dport
= usin
->sin_port
;
662 if (sk
->sk_state
!= TCP_ESTABLISHED
)
663 return -EDESTADDRREQ
;
666 /* Open fast path for connected socket.
667 Route will not be used, if at least one option is set.
671 ipc
.addr
= inet
->saddr
;
673 ipc
.oif
= sk
->sk_bound_dev_if
;
674 if (msg
->msg_controllen
) {
675 err
= ip_cmsg_send(msg
, &ipc
);
686 ipc
.addr
= faddr
= daddr
;
688 if (ipc
.opt
&& ipc
.opt
->srr
) {
691 faddr
= ipc
.opt
->faddr
;
694 tos
= RT_TOS(inet
->tos
);
695 if (sock_flag(sk
, SOCK_LOCALROUTE
) ||
696 (msg
->msg_flags
& MSG_DONTROUTE
) ||
697 (ipc
.opt
&& ipc
.opt
->is_strictroute
)) {
702 if (MULTICAST(daddr
)) {
704 ipc
.oif
= inet
->mc_index
;
706 saddr
= inet
->mc_addr
;
711 rt
= (struct rtable
*)sk_dst_check(sk
, 0);
714 struct flowi fl
= { .oif
= ipc
.oif
,
719 .proto
= sk
->sk_protocol
,
721 { .sport
= inet
->sport
,
722 .dport
= dport
} } };
723 security_sk_classify_flow(sk
, &fl
);
724 err
= ip_route_output_flow(&rt
, &fl
, sk
, 1);
729 if ((rt
->rt_flags
& RTCF_BROADCAST
) &&
730 !sock_flag(sk
, SOCK_BROADCAST
))
733 sk_dst_set(sk
, dst_clone(&rt
->u
.dst
));
736 if (msg
->msg_flags
&MSG_CONFIRM
)
742 daddr
= ipc
.addr
= rt
->rt_dst
;
745 if (unlikely(up
->pending
)) {
746 /* The socket is already corked while preparing it. */
747 /* ... which is an evident application bug. --ANK */
750 LIMIT_NETDEBUG(KERN_DEBUG
"udp cork app bug 2\n");
755 * Now cork the socket to pend data.
757 inet
->cork
.fl
.fl4_dst
= daddr
;
758 inet
->cork
.fl
.fl_ip_dport
= dport
;
759 inet
->cork
.fl
.fl4_src
= saddr
;
760 inet
->cork
.fl
.fl_ip_sport
= inet
->sport
;
761 up
->pending
= AF_INET
;
765 getfrag
= is_udplite
? udplite_getfrag
: ip_generic_getfrag
;
766 err
= ip_append_data(sk
, getfrag
, msg
->msg_iov
, ulen
,
767 sizeof(struct udphdr
), &ipc
, rt
,
768 corkreq
? msg
->msg_flags
|MSG_MORE
: msg
->msg_flags
);
770 udp_flush_pending_frames(sk
);
772 err
= udp_push_pending_frames(sk
);
773 else if (unlikely(skb_queue_empty(&sk
->sk_write_queue
)))
782 UDP_INC_STATS_USER(UDP_MIB_OUTDATAGRAMS
, is_udplite
);
786 * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting
787 * ENOBUFS might not be good (it's not tunable per se), but otherwise
788 * we don't have a good statistic (IpOutDiscards but it can be too many
789 * things). We could add another new stat but at least for now that
790 * seems like overkill.
792 if (err
== -ENOBUFS
|| test_bit(SOCK_NOSPACE
, &sk
->sk_socket
->flags
)) {
793 UDP_INC_STATS_USER(UDP_MIB_SNDBUFERRORS
, is_udplite
);
798 dst_confirm(&rt
->u
.dst
);
799 if (!(msg
->msg_flags
&MSG_PROBE
) || len
)
800 goto back_from_confirm
;
805 int udp_sendpage(struct sock
*sk
, struct page
*page
, int offset
,
806 size_t size
, int flags
)
808 struct udp_sock
*up
= udp_sk(sk
);
812 struct msghdr msg
= { .msg_flags
= flags
|MSG_MORE
};
814 /* Call udp_sendmsg to specify destination address which
815 * sendpage interface can't pass.
816 * This will succeed only when the socket is connected.
818 ret
= udp_sendmsg(NULL
, sk
, &msg
, 0);
825 if (unlikely(!up
->pending
)) {
828 LIMIT_NETDEBUG(KERN_DEBUG
"udp cork app bug 3\n");
832 ret
= ip_append_page(sk
, page
, offset
, size
, flags
);
833 if (ret
== -EOPNOTSUPP
) {
835 return sock_no_sendpage(sk
->sk_socket
, page
, offset
,
839 udp_flush_pending_frames(sk
);
844 if (!(up
->corkflag
|| (flags
&MSG_MORE
)))
845 ret
= udp_push_pending_frames(sk
);
854 * IOCTL requests applicable to the UDP protocol
857 int udp_ioctl(struct sock
*sk
, int cmd
, unsigned long arg
)
862 int amount
= atomic_read(&sk
->sk_wmem_alloc
);
863 return put_user(amount
, (int __user
*)arg
);
869 unsigned long amount
;
872 spin_lock_bh(&sk
->sk_receive_queue
.lock
);
873 skb
= skb_peek(&sk
->sk_receive_queue
);
876 * We will only return the amount
877 * of this packet since that is all
880 amount
= skb
->len
- sizeof(struct udphdr
);
882 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
883 return put_user(amount
, (int __user
*)arg
);
894 * This should be easy, if there is something there we
895 * return it, otherwise we block.
898 int udp_recvmsg(struct kiocb
*iocb
, struct sock
*sk
, struct msghdr
*msg
,
899 size_t len
, int noblock
, int flags
, int *addr_len
)
901 struct inet_sock
*inet
= inet_sk(sk
);
902 struct sockaddr_in
*sin
= (struct sockaddr_in
*)msg
->msg_name
;
904 unsigned int ulen
, copied
;
906 int is_udplite
= IS_UDPLITE(sk
);
909 * Check any passed addresses
912 *addr_len
=sizeof(*sin
);
914 if (flags
& MSG_ERRQUEUE
)
915 return ip_recv_error(sk
, msg
, len
);
918 skb
= skb_recv_datagram(sk
, flags
, noblock
, &err
);
922 ulen
= skb
->len
- sizeof(struct udphdr
);
926 else if (copied
< ulen
)
927 msg
->msg_flags
|= MSG_TRUNC
;
930 * If checksum is needed at all, try to do it while copying the
931 * data. If the data is truncated, or if we only want a partial
932 * coverage checksum (UDP-Lite), do it before the copy.
935 if (copied
< ulen
|| UDP_SKB_CB(skb
)->partial_cov
) {
936 if (udp_lib_checksum_complete(skb
))
940 if (skb_csum_unnecessary(skb
))
941 err
= skb_copy_datagram_iovec(skb
, sizeof(struct udphdr
),
942 msg
->msg_iov
, copied
);
944 err
= skb_copy_and_csum_datagram_iovec(skb
, sizeof(struct udphdr
), msg
->msg_iov
);
953 sock_recv_timestamp(msg
, sk
, skb
);
955 /* Copy the address. */
958 sin
->sin_family
= AF_INET
;
959 sin
->sin_port
= udp_hdr(skb
)->source
;
960 sin
->sin_addr
.s_addr
= ip_hdr(skb
)->saddr
;
961 memset(sin
->sin_zero
, 0, sizeof(sin
->sin_zero
));
963 if (inet
->cmsg_flags
)
964 ip_cmsg_recv(msg
, skb
);
967 if (flags
& MSG_TRUNC
)
971 skb_free_datagram(sk
, skb
);
976 UDP_INC_STATS_BH(UDP_MIB_INERRORS
, is_udplite
);
978 skb_kill_datagram(sk
, skb
, flags
);
986 int udp_disconnect(struct sock
*sk
, int flags
)
988 struct inet_sock
*inet
= inet_sk(sk
);
990 * 1003.1g - break association.
993 sk
->sk_state
= TCP_CLOSE
;
996 sk
->sk_bound_dev_if
= 0;
997 if (!(sk
->sk_userlocks
& SOCK_BINDADDR_LOCK
))
998 inet_reset_saddr(sk
);
1000 if (!(sk
->sk_userlocks
& SOCK_BINDPORT_LOCK
)) {
1001 sk
->sk_prot
->unhash(sk
);
1009 * 1 if the UDP system should process it
1010 * 0 if we should drop this packet
1011 * -1 if it should get processed by xfrm4_rcv_encap
1013 static int udp_encap_rcv(struct sock
* sk
, struct sk_buff
*skb
)
1018 struct udp_sock
*up
= udp_sk(sk
);
1025 __u16 encap_type
= up
->encap_type
;
1027 /* if we're overly short, let UDP handle it */
1028 len
= skb
->len
- sizeof(struct udphdr
);
1032 /* if this is not encapsulated socket, then just return now */
1036 /* If this is a paged skb, make sure we pull up
1037 * whatever data we need to look at. */
1038 if (!pskb_may_pull(skb
, sizeof(struct udphdr
) + min(len
, 8)))
1041 /* Now we can get the pointers */
1043 udpdata
= (__u8
*)uh
+ sizeof(struct udphdr
);
1044 udpdata32
= (__be32
*)udpdata
;
1046 switch (encap_type
) {
1048 case UDP_ENCAP_ESPINUDP
:
1049 /* Check if this is a keepalive packet. If so, eat it. */
1050 if (len
== 1 && udpdata
[0] == 0xff) {
1052 } else if (len
> sizeof(struct ip_esp_hdr
) && udpdata32
[0] != 0) {
1053 /* ESP Packet without Non-ESP header */
1054 len
= sizeof(struct udphdr
);
1056 /* Must be an IKE packet.. pass it through */
1059 case UDP_ENCAP_ESPINUDP_NON_IKE
:
1060 /* Check if this is a keepalive packet. If so, eat it. */
1061 if (len
== 1 && udpdata
[0] == 0xff) {
1063 } else if (len
> 2 * sizeof(u32
) + sizeof(struct ip_esp_hdr
) &&
1064 udpdata32
[0] == 0 && udpdata32
[1] == 0) {
1066 /* ESP Packet with Non-IKE marker */
1067 len
= sizeof(struct udphdr
) + 2 * sizeof(u32
);
1069 /* Must be an IKE packet.. pass it through */
1074 /* At this point we are sure that this is an ESPinUDP packet,
1075 * so we need to remove 'len' bytes from the packet (the UDP
1076 * header and optional ESP marker bytes) and then modify the
1077 * protocol to ESP, and then call into the transform receiver.
1079 if (skb_cloned(skb
) && pskb_expand_head(skb
, 0, 0, GFP_ATOMIC
))
1082 /* Now we can update and verify the packet length... */
1084 iphlen
= iph
->ihl
<< 2;
1085 iph
->tot_len
= htons(ntohs(iph
->tot_len
) - len
);
1086 if (skb
->len
< iphlen
+ len
) {
1087 /* packet is too small!?! */
1091 /* pull the data buffer up to the ESP header and set the
1092 * transport header to point to ESP. Keep UDP on the stack
1095 __skb_pull(skb
, len
);
1096 skb_reset_transport_header(skb
);
1098 /* modify the protocol (it's ESP!) */
1099 iph
->protocol
= IPPROTO_ESP
;
1101 /* and let the caller know to send this into the ESP processor... */
1109 * >0: "udp encap" protocol resubmission
1111 * Note that in the success and error cases, the skb is assumed to
1112 * have either been requeued or freed.
1114 int udp_queue_rcv_skb(struct sock
* sk
, struct sk_buff
*skb
)
1116 struct udp_sock
*up
= udp_sk(sk
);
1120 * Charge it to the socket, dropping if the queue is full.
1122 if (!xfrm4_policy_check(sk
, XFRM_POLICY_IN
, skb
))
1126 if (up
->encap_type
) {
1128 * This is an encapsulation socket, so let's see if this is
1129 * an encapsulated packet.
1130 * If it's a keepalive packet, then just eat it.
1131 * If it's an encapsulateed packet, then pass it to the
1132 * IPsec xfrm input and return the response
1133 * appropriately. Otherwise, just fall through and
1134 * pass this up the UDP socket.
1138 ret
= udp_encap_rcv(sk
, skb
);
1140 /* Eat the packet .. */
1145 /* process the ESP packet */
1146 ret
= xfrm4_rcv_encap(skb
, up
->encap_type
);
1147 UDP_INC_STATS_BH(UDP_MIB_INDATAGRAMS
, up
->pcflag
);
1150 /* FALLTHROUGH -- it's a UDP Packet */
1154 * UDP-Lite specific tests, ignored on UDP sockets
1156 if ((up
->pcflag
& UDPLITE_RECV_CC
) && UDP_SKB_CB(skb
)->partial_cov
) {
1159 * MIB statistics other than incrementing the error count are
1160 * disabled for the following two types of errors: these depend
1161 * on the application settings, not on the functioning of the
1162 * protocol stack as such.
1164 * RFC 3828 here recommends (sec 3.3): "There should also be a
1165 * way ... to ... at least let the receiving application block
1166 * delivery of packets with coverage values less than a value
1167 * provided by the application."
1169 if (up
->pcrlen
== 0) { /* full coverage was set */
1170 LIMIT_NETDEBUG(KERN_WARNING
"UDPLITE: partial coverage "
1171 "%d while full coverage %d requested\n",
1172 UDP_SKB_CB(skb
)->cscov
, skb
->len
);
1175 /* The next case involves violating the min. coverage requested
1176 * by the receiver. This is subtle: if receiver wants x and x is
1177 * greater than the buffersize/MTU then receiver will complain
1178 * that it wants x while sender emits packets of smaller size y.
1179 * Therefore the above ...()->partial_cov statement is essential.
1181 if (UDP_SKB_CB(skb
)->cscov
< up
->pcrlen
) {
1182 LIMIT_NETDEBUG(KERN_WARNING
1183 "UDPLITE: coverage %d too small, need min %d\n",
1184 UDP_SKB_CB(skb
)->cscov
, up
->pcrlen
);
1189 if (sk
->sk_filter
) {
1190 if (udp_lib_checksum_complete(skb
))
1194 if ((rc
= sock_queue_rcv_skb(sk
,skb
)) < 0) {
1195 /* Note that an ENOMEM error is charged twice */
1197 UDP_INC_STATS_BH(UDP_MIB_RCVBUFERRORS
, up
->pcflag
);
1201 UDP_INC_STATS_BH(UDP_MIB_INDATAGRAMS
, up
->pcflag
);
1205 UDP_INC_STATS_BH(UDP_MIB_INERRORS
, up
->pcflag
);
1211 * Multicasts and broadcasts go to each listener.
1213 * Note: called only from the BH handler context,
1214 * so we don't need to lock the hashes.
1216 static int __udp4_lib_mcast_deliver(struct sk_buff
*skb
,
1218 __be32 saddr
, __be32 daddr
,
1219 struct hlist_head udptable
[])
1221 struct sock
*sk
, *skw
, *sknext
;
1223 int hport
= ntohs(uh
->dest
);
1224 unsigned int hash
= ipv4_hash_port_and_addr(hport
, daddr
);
1225 unsigned int hashwild
= udp_hash_port(hport
);
1227 dif
= skb
->dev
->ifindex
;
1229 read_lock(&udp_hash_lock
);
1231 sk
= sk_head(&udptable
[hash
& (UDP_HTABLE_SIZE
- 1)]);
1232 skw
= sk_head(&udptable
[hashwild
& (UDP_HTABLE_SIZE
- 1)]);
1234 sk
= udp_v4_mcast_next(sk
, hash
, hport
, daddr
, uh
->source
, saddr
, dif
);
1237 sk
= udp_v4_mcast_next(skw
, hash
, hport
, daddr
, uh
->source
,
1242 struct sk_buff
*skb1
= skb
;
1243 sknext
= udp_v4_mcast_next(sk_next(sk
), hash
, hport
,
1244 daddr
, uh
->source
, saddr
, dif
);
1245 if (!sknext
&& hash
!= hashwild
) {
1247 sknext
= udp_v4_mcast_next(skw
, hash
, hport
,
1248 daddr
, uh
->source
, saddr
, dif
);
1251 skb1
= skb_clone(skb
, GFP_ATOMIC
);
1254 int ret
= udp_queue_rcv_skb(sk
, skb1
);
1257 * we should probably re-process
1258 * instead of dropping packets here.
1266 read_unlock(&udp_hash_lock
);
1270 /* Initialize UDP checksum. If exited with zero value (success),
1271 * CHECKSUM_UNNECESSARY means, that no more checks are required.
1272 * Otherwise, csum completion requires chacksumming packet body,
1273 * including udp header and folding it to skb->csum.
1275 static inline int udp4_csum_init(struct sk_buff
*skb
, struct udphdr
*uh
,
1278 const struct iphdr
*iph
;
1281 UDP_SKB_CB(skb
)->partial_cov
= 0;
1282 UDP_SKB_CB(skb
)->cscov
= skb
->len
;
1284 if (proto
== IPPROTO_UDPLITE
) {
1285 err
= udplite_checksum_init(skb
, uh
);
1291 if (uh
->check
== 0) {
1292 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
1293 } else if (skb
->ip_summed
== CHECKSUM_COMPLETE
) {
1294 if (!csum_tcpudp_magic(iph
->saddr
, iph
->daddr
, skb
->len
,
1296 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
1298 if (!skb_csum_unnecessary(skb
))
1299 skb
->csum
= csum_tcpudp_nofold(iph
->saddr
, iph
->daddr
,
1300 skb
->len
, proto
, 0);
1301 /* Probably, we should checksum udp header (it should be in cache
1302 * in any case) and data in tiny packets (< rx copybreak).
1309 * All we need to do is get the socket, and then do a checksum.
1312 int __udp4_lib_rcv(struct sk_buff
*skb
, struct hlist_head udptable
[],
1316 struct udphdr
*uh
= udp_hdr(skb
);
1317 unsigned short ulen
;
1318 struct rtable
*rt
= (struct rtable
*)skb
->dst
;
1319 __be32 saddr
= ip_hdr(skb
)->saddr
;
1320 __be32 daddr
= ip_hdr(skb
)->daddr
;
1323 * Validate the packet.
1325 if (!pskb_may_pull(skb
, sizeof(struct udphdr
)))
1326 goto drop
; /* No space for header. */
1328 ulen
= ntohs(uh
->len
);
1329 if (ulen
> skb
->len
)
1332 if (proto
== IPPROTO_UDP
) {
1333 /* UDP validates ulen. */
1334 if (ulen
< sizeof(*uh
) || pskb_trim_rcsum(skb
, ulen
))
1339 if (udp4_csum_init(skb
, uh
, proto
))
1342 if (rt
->rt_flags
& (RTCF_BROADCAST
|RTCF_MULTICAST
))
1343 return __udp4_lib_mcast_deliver(skb
, uh
, saddr
, daddr
, udptable
);
1345 sk
= __udp4_lib_lookup(saddr
, uh
->source
, daddr
, uh
->dest
,
1346 skb
->dev
->ifindex
, udptable
);
1349 int ret
= udp_queue_rcv_skb(sk
, skb
);
1352 /* a return value > 0 means to resubmit the input, but
1353 * it wants the return to be -protocol, or 0
1360 if (!xfrm4_policy_check(NULL
, XFRM_POLICY_IN
, skb
))
1364 /* No socket. Drop packet silently, if checksum is wrong */
1365 if (udp_lib_checksum_complete(skb
))
1368 UDP_INC_STATS_BH(UDP_MIB_NOPORTS
, proto
== IPPROTO_UDPLITE
);
1369 icmp_send(skb
, ICMP_DEST_UNREACH
, ICMP_PORT_UNREACH
, 0);
1372 * Hmm. We got an UDP packet to a port to which we
1373 * don't wanna listen. Ignore it.
1379 LIMIT_NETDEBUG(KERN_DEBUG
"UDP%s: short packet: From %u.%u.%u.%u:%u %d/%d to %u.%u.%u.%u:%u\n",
1380 proto
== IPPROTO_UDPLITE
? "-Lite" : "",
1391 * RFC1122: OK. Discards the bad packet silently (as far as
1392 * the network is concerned, anyway) as per 4.1.3.4 (MUST).
1394 LIMIT_NETDEBUG(KERN_DEBUG
"UDP%s: bad checksum. From %d.%d.%d.%d:%d to %d.%d.%d.%d:%d ulen %d\n",
1395 proto
== IPPROTO_UDPLITE
? "-Lite" : "",
1402 UDP_INC_STATS_BH(UDP_MIB_INERRORS
, proto
== IPPROTO_UDPLITE
);
1407 int udp_rcv(struct sk_buff
*skb
)
1409 return __udp4_lib_rcv(skb
, udp_hash
, IPPROTO_UDP
);
1412 int udp_destroy_sock(struct sock
*sk
)
1415 udp_flush_pending_frames(sk
);
1421 * Socket option code for UDP
1423 int udp_lib_setsockopt(struct sock
*sk
, int level
, int optname
,
1424 char __user
*optval
, int optlen
,
1425 int (*push_pending_frames
)(struct sock
*))
1427 struct udp_sock
*up
= udp_sk(sk
);
1431 if (optlen
<sizeof(int))
1434 if (get_user(val
, (int __user
*)optval
))
1444 (*push_pending_frames
)(sk
);
1452 case UDP_ENCAP_ESPINUDP
:
1453 case UDP_ENCAP_ESPINUDP_NON_IKE
:
1454 up
->encap_type
= val
;
1463 * UDP-Lite's partial checksum coverage (RFC 3828).
1465 /* The sender sets actual checksum coverage length via this option.
1466 * The case coverage > packet length is handled by send module. */
1467 case UDPLITE_SEND_CSCOV
:
1468 if (!up
->pcflag
) /* Disable the option on UDP sockets */
1469 return -ENOPROTOOPT
;
1470 if (val
!= 0 && val
< 8) /* Illegal coverage: use default (8) */
1473 up
->pcflag
|= UDPLITE_SEND_CC
;
1476 /* The receiver specifies a minimum checksum coverage value. To make
1477 * sense, this should be set to at least 8 (as done below). If zero is
1478 * used, this again means full checksum coverage. */
1479 case UDPLITE_RECV_CSCOV
:
1480 if (!up
->pcflag
) /* Disable the option on UDP sockets */
1481 return -ENOPROTOOPT
;
1482 if (val
!= 0 && val
< 8) /* Avoid silly minimal values. */
1485 up
->pcflag
|= UDPLITE_RECV_CC
;
1496 int udp_setsockopt(struct sock
*sk
, int level
, int optname
,
1497 char __user
*optval
, int optlen
)
1499 if (level
== SOL_UDP
|| level
== SOL_UDPLITE
)
1500 return udp_lib_setsockopt(sk
, level
, optname
, optval
, optlen
,
1501 udp_push_pending_frames
);
1502 return ip_setsockopt(sk
, level
, optname
, optval
, optlen
);
1505 #ifdef CONFIG_COMPAT
1506 int compat_udp_setsockopt(struct sock
*sk
, int level
, int optname
,
1507 char __user
*optval
, int optlen
)
1509 if (level
== SOL_UDP
|| level
== SOL_UDPLITE
)
1510 return udp_lib_setsockopt(sk
, level
, optname
, optval
, optlen
,
1511 udp_push_pending_frames
);
1512 return compat_ip_setsockopt(sk
, level
, optname
, optval
, optlen
);
1516 int udp_lib_getsockopt(struct sock
*sk
, int level
, int optname
,
1517 char __user
*optval
, int __user
*optlen
)
1519 struct udp_sock
*up
= udp_sk(sk
);
1522 if (get_user(len
,optlen
))
1525 len
= min_t(unsigned int, len
, sizeof(int));
1536 val
= up
->encap_type
;
1539 /* The following two cannot be changed on UDP sockets, the return is
1540 * always 0 (which corresponds to the full checksum coverage of UDP). */
1541 case UDPLITE_SEND_CSCOV
:
1545 case UDPLITE_RECV_CSCOV
:
1550 return -ENOPROTOOPT
;
1553 if (put_user(len
, optlen
))
1555 if (copy_to_user(optval
, &val
,len
))
1560 int udp_getsockopt(struct sock
*sk
, int level
, int optname
,
1561 char __user
*optval
, int __user
*optlen
)
1563 if (level
== SOL_UDP
|| level
== SOL_UDPLITE
)
1564 return udp_lib_getsockopt(sk
, level
, optname
, optval
, optlen
);
1565 return ip_getsockopt(sk
, level
, optname
, optval
, optlen
);
1568 #ifdef CONFIG_COMPAT
1569 int compat_udp_getsockopt(struct sock
*sk
, int level
, int optname
,
1570 char __user
*optval
, int __user
*optlen
)
1572 if (level
== SOL_UDP
|| level
== SOL_UDPLITE
)
1573 return udp_lib_getsockopt(sk
, level
, optname
, optval
, optlen
);
1574 return compat_ip_getsockopt(sk
, level
, optname
, optval
, optlen
);
1578 * udp_poll - wait for a UDP event.
1579 * @file - file struct
1581 * @wait - poll table
1583 * This is same as datagram poll, except for the special case of
1584 * blocking sockets. If application is using a blocking fd
1585 * and a packet with checksum error is in the queue;
1586 * then it could get return from select indicating data available
1587 * but then block when reading it. Add special case code
1588 * to work around these arguably broken applications.
1590 unsigned int udp_poll(struct file
*file
, struct socket
*sock
, poll_table
*wait
)
1592 unsigned int mask
= datagram_poll(file
, sock
, wait
);
1593 struct sock
*sk
= sock
->sk
;
1594 int is_lite
= IS_UDPLITE(sk
);
1596 /* Check for false positives due to checksum errors */
1597 if ( (mask
& POLLRDNORM
) &&
1598 !(file
->f_flags
& O_NONBLOCK
) &&
1599 !(sk
->sk_shutdown
& RCV_SHUTDOWN
)){
1600 struct sk_buff_head
*rcvq
= &sk
->sk_receive_queue
;
1601 struct sk_buff
*skb
;
1603 spin_lock_bh(&rcvq
->lock
);
1604 while ((skb
= skb_peek(rcvq
)) != NULL
&&
1605 udp_lib_checksum_complete(skb
)) {
1606 UDP_INC_STATS_BH(UDP_MIB_INERRORS
, is_lite
);
1607 __skb_unlink(skb
, rcvq
);
1610 spin_unlock_bh(&rcvq
->lock
);
1612 /* nothing to see, move along */
1614 mask
&= ~(POLLIN
| POLLRDNORM
);
1621 struct proto udp_prot
= {
1623 .owner
= THIS_MODULE
,
1624 .close
= udp_lib_close
,
1625 .connect
= ip4_datagram_connect
,
1626 .disconnect
= udp_disconnect
,
1628 .destroy
= udp_destroy_sock
,
1629 .setsockopt
= udp_setsockopt
,
1630 .getsockopt
= udp_getsockopt
,
1631 .sendmsg
= udp_sendmsg
,
1632 .recvmsg
= udp_recvmsg
,
1633 .sendpage
= udp_sendpage
,
1634 .backlog_rcv
= udp_queue_rcv_skb
,
1635 .hash
= udp_lib_hash
,
1636 .unhash
= udp_lib_unhash
,
1637 .get_port
= udp_v4_get_port
,
1638 .obj_size
= sizeof(struct udp_sock
),
1639 #ifdef CONFIG_COMPAT
1640 .compat_setsockopt
= compat_udp_setsockopt
,
1641 .compat_getsockopt
= compat_udp_getsockopt
,
1645 /* ------------------------------------------------------------------------ */
1646 #ifdef CONFIG_PROC_FS
1648 static struct sock
*udp_get_first(struct seq_file
*seq
)
1651 struct udp_iter_state
*state
= seq
->private;
1653 for (state
->bucket
= 0; state
->bucket
< UDP_HTABLE_SIZE
; ++state
->bucket
) {
1654 struct hlist_node
*node
;
1655 sk_for_each(sk
, node
, state
->hashtable
+ state
->bucket
) {
1656 if (sk
->sk_family
== state
->family
)
1665 static struct sock
*udp_get_next(struct seq_file
*seq
, struct sock
*sk
)
1667 struct udp_iter_state
*state
= seq
->private;
1673 } while (sk
&& sk
->sk_family
!= state
->family
);
1675 if (!sk
&& ++state
->bucket
< UDP_HTABLE_SIZE
) {
1676 sk
= sk_head(state
->hashtable
+ state
->bucket
);
1682 static struct sock
*udp_get_idx(struct seq_file
*seq
, loff_t pos
)
1684 struct sock
*sk
= udp_get_first(seq
);
1687 while (pos
&& (sk
= udp_get_next(seq
, sk
)) != NULL
)
1689 return pos
? NULL
: sk
;
1692 static void *udp_seq_start(struct seq_file
*seq
, loff_t
*pos
)
1694 read_lock(&udp_hash_lock
);
1695 return *pos
? udp_get_idx(seq
, *pos
-1) : (void *)1;
1698 static void *udp_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
1703 sk
= udp_get_idx(seq
, 0);
1705 sk
= udp_get_next(seq
, v
);
1711 static void udp_seq_stop(struct seq_file
*seq
, void *v
)
1713 read_unlock(&udp_hash_lock
);
1716 static int udp_seq_open(struct inode
*inode
, struct file
*file
)
1718 struct udp_seq_afinfo
*afinfo
= PDE(inode
)->data
;
1719 struct seq_file
*seq
;
1721 struct udp_iter_state
*s
= kzalloc(sizeof(*s
), GFP_KERNEL
);
1725 s
->family
= afinfo
->family
;
1726 s
->hashtable
= afinfo
->hashtable
;
1727 s
->seq_ops
.start
= udp_seq_start
;
1728 s
->seq_ops
.next
= udp_seq_next
;
1729 s
->seq_ops
.show
= afinfo
->seq_show
;
1730 s
->seq_ops
.stop
= udp_seq_stop
;
1732 rc
= seq_open(file
, &s
->seq_ops
);
1736 seq
= file
->private_data
;
1745 /* ------------------------------------------------------------------------ */
1746 int udp_proc_register(struct udp_seq_afinfo
*afinfo
)
1748 struct proc_dir_entry
*p
;
1753 afinfo
->seq_fops
->owner
= afinfo
->owner
;
1754 afinfo
->seq_fops
->open
= udp_seq_open
;
1755 afinfo
->seq_fops
->read
= seq_read
;
1756 afinfo
->seq_fops
->llseek
= seq_lseek
;
1757 afinfo
->seq_fops
->release
= seq_release_private
;
1759 p
= proc_net_fops_create(afinfo
->name
, S_IRUGO
, afinfo
->seq_fops
);
1767 void udp_proc_unregister(struct udp_seq_afinfo
*afinfo
)
1771 proc_net_remove(afinfo
->name
);
1772 memset(afinfo
->seq_fops
, 0, sizeof(*afinfo
->seq_fops
));
1775 /* ------------------------------------------------------------------------ */
1776 static void udp4_format_sock(struct sock
*sp
, char *tmpbuf
, int bucket
)
1778 struct inet_sock
*inet
= inet_sk(sp
);
1779 __be32 dest
= inet
->daddr
;
1780 __be32 src
= inet
->rcv_saddr
;
1781 __u16 destp
= ntohs(inet
->dport
);
1782 __u16 srcp
= ntohs(inet
->sport
);
1784 sprintf(tmpbuf
, "%4d: %08X:%04X %08X:%04X"
1785 " %02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p",
1786 bucket
, src
, srcp
, dest
, destp
, sp
->sk_state
,
1787 atomic_read(&sp
->sk_wmem_alloc
),
1788 atomic_read(&sp
->sk_rmem_alloc
),
1789 0, 0L, 0, sock_i_uid(sp
), 0, sock_i_ino(sp
),
1790 atomic_read(&sp
->sk_refcnt
), sp
);
1793 int udp4_seq_show(struct seq_file
*seq
, void *v
)
1795 if (v
== SEQ_START_TOKEN
)
1796 seq_printf(seq
, "%-127s\n",
1797 " sl local_address rem_address st tx_queue "
1798 "rx_queue tr tm->when retrnsmt uid timeout "
1802 struct udp_iter_state
*state
= seq
->private;
1804 udp4_format_sock(v
, tmpbuf
, state
->bucket
);
1805 seq_printf(seq
, "%-127s\n", tmpbuf
);
1810 /* ------------------------------------------------------------------------ */
1811 static struct file_operations udp4_seq_fops
;
1812 static struct udp_seq_afinfo udp4_seq_afinfo
= {
1813 .owner
= THIS_MODULE
,
1816 .hashtable
= udp_hash
,
1817 .seq_show
= udp4_seq_show
,
1818 .seq_fops
= &udp4_seq_fops
,
1821 int __init
udp4_proc_init(void)
1823 return udp_proc_register(&udp4_seq_afinfo
);
1826 void udp4_proc_exit(void)
1828 udp_proc_unregister(&udp4_seq_afinfo
);
1830 #endif /* CONFIG_PROC_FS */
1832 EXPORT_SYMBOL(udp_disconnect
);
1833 EXPORT_SYMBOL(udp_hash
);
1834 EXPORT_SYMBOL(udp_hash_lock
);
1835 EXPORT_SYMBOL(udp_ioctl
);
1836 EXPORT_SYMBOL(udp_get_port
);
1837 EXPORT_SYMBOL(udp_prot
);
1838 EXPORT_SYMBOL(udp_sendmsg
);
1839 EXPORT_SYMBOL(udp_lib_getsockopt
);
1840 EXPORT_SYMBOL(udp_lib_setsockopt
);
1841 EXPORT_SYMBOL(udp_poll
);
1843 #ifdef CONFIG_PROC_FS
1844 EXPORT_SYMBOL(udp_proc_register
);
1845 EXPORT_SYMBOL(udp_proc_unregister
);