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.83 2000/06/09 07:35:49 davem Exp $
10 * Authors: Ross Biro, <bir7@leland.Stanford.Edu>
11 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
12 * Arnt Gulbrandsen, <agulbra@nvg.unit.no>
13 * Alan Cox, <Alan.Cox@linux.org>
16 * Alan Cox : verify_area() calls
17 * Alan Cox : stopped close while in use off icmp
18 * messages. Not a fix but a botch that
19 * for udp at least is 'valid'.
20 * Alan Cox : Fixed icmp handling properly
21 * Alan Cox : Correct error for oversized datagrams
22 * Alan Cox : Tidied select() semantics.
23 * Alan Cox : udp_err() fixed properly, also now
24 * select and read wake correctly on errors
25 * Alan Cox : udp_send verify_area moved to avoid mem leak
26 * Alan Cox : UDP can count its memory
27 * Alan Cox : send to an unknown connection causes
28 * an ECONNREFUSED off the icmp, but
30 * Alan Cox : Switched to new sk_buff handlers. No more backlog!
31 * Alan Cox : Using generic datagram code. Even smaller and the PEEK
32 * bug no longer crashes it.
33 * Fred Van Kempen : Net2e support for sk->broadcast.
34 * Alan Cox : Uses skb_free_datagram
35 * Alan Cox : Added get/set sockopt support.
36 * Alan Cox : Broadcasting without option set returns EACCES.
37 * Alan Cox : No wakeup calls. Instead we now use the callbacks.
38 * Alan Cox : Use ip_tos and ip_ttl
39 * Alan Cox : SNMP Mibs
40 * Alan Cox : MSG_DONTROUTE, and 0.0.0.0 support.
41 * Matt Dillon : UDP length checks.
42 * Alan Cox : Smarter af_inet used properly.
43 * Alan Cox : Use new kernel side addressing.
44 * Alan Cox : Incorrect return on truncated datagram receive.
45 * Arnt Gulbrandsen : New udp_send and stuff
46 * Alan Cox : Cache last socket
47 * Alan Cox : Route cache
48 * Jon Peatfield : Minor efficiency fix to sendto().
49 * Mike Shaver : RFC1122 checks.
50 * Alan Cox : Nonblocking error fix.
51 * Willy Konynenberg : Transparent proxying support.
52 * Mike McLagan : Routing by source
53 * David S. Miller : New socket lookup architecture.
54 * Last socket cache retained as it
55 * does have a high hit rate.
56 * Olaf Kirch : Don't linearise iovec on sendmsg.
57 * Andi Kleen : Some cleanups, cache destination entry
59 * Vitaly E. Lavrov : Transparent proxy revived after year coma.
60 * Melvin Smith : Check msg_name not msg_namelen in sendto(),
61 * return ENOTCONN for unconnected sockets (POSIX)
62 * Janos Farkas : don't deliver multi/broadcasts to a different
63 * bound-to-device socket
66 * This program is free software; you can redistribute it and/or
67 * modify it under the terms of the GNU General Public License
68 * as published by the Free Software Foundation; either version
69 * 2 of the License, or (at your option) any later version.
74 SHOULD send ICMP_PORT_UNREACHABLE in response to datagrams to
75 an un-listened port. (OK)
77 MUST pass IP options from IP -> application (OK)
78 MUST allow application to specify IP options (OK)
79 4.1.3.3 (ICMP Messages)
80 MUST pass ICMP error messages to application (OK -- except when SO_BSDCOMPAT is set)
81 4.1.3.4 (UDP Checksums)
82 MUST provide facility for checksumming (OK)
83 MAY allow application to control checksumming (OK)
84 MUST default to checksumming on (OK)
85 MUST discard silently datagrams with bad csums (OK, except during debugging)
86 4.1.3.5 (UDP Multihoming)
87 MUST allow application to specify source address (OK)
88 SHOULD be able to communicate the chosen src addr up to application
89 when application doesn't choose (DOES - use recvmsg cmsgs)
90 4.1.3.6 (Invalid Addresses)
91 MUST discard invalid source addresses (OK -- done in the new routing code)
92 MUST only send datagrams with one of our addresses (OK)
95 #include <asm/system.h>
96 #include <asm/uaccess.h>
97 #include <linux/types.h>
98 #include <linux/fcntl.h>
99 #include <linux/socket.h>
100 #include <linux/sockios.h>
101 #include <linux/in.h>
102 #include <linux/errno.h>
103 #include <linux/timer.h>
104 #include <linux/mm.h>
105 #include <linux/config.h>
106 #include <linux/inet.h>
107 #include <linux/netdevice.h>
108 #include <net/snmp.h>
110 #include <net/protocol.h>
111 #include <linux/skbuff.h>
112 #include <net/sock.h>
114 #include <net/icmp.h>
115 #include <net/route.h>
116 #include <net/inet_common.h>
117 #include <net/checksum.h>
120 * Snmp MIB for the UDP layer
123 struct udp_mib udp_statistics
[NR_CPUS
*2];
125 struct sock
*udp_hash
[UDP_HTABLE_SIZE
];
126 rwlock_t udp_hash_lock
= RW_LOCK_UNLOCKED
;
128 /* Shared by v4/v6 udp. */
129 int udp_port_rover
= 0;
131 static int udp_v4_get_port(struct sock
*sk
, unsigned short snum
)
133 write_lock_bh(&udp_hash_lock
);
135 int best_size_so_far
, best
, result
, i
;
137 if (udp_port_rover
> sysctl_local_port_range
[1] ||
138 udp_port_rover
< sysctl_local_port_range
[0])
139 udp_port_rover
= sysctl_local_port_range
[0];
140 best_size_so_far
= 32767;
141 best
= result
= udp_port_rover
;
142 for (i
= 0; i
< UDP_HTABLE_SIZE
; i
++, result
++) {
146 sk
= udp_hash
[result
& (UDP_HTABLE_SIZE
- 1)];
148 if (result
> sysctl_local_port_range
[1])
149 result
= sysctl_local_port_range
[0] +
150 ((result
- sysctl_local_port_range
[0]) &
151 (UDP_HTABLE_SIZE
- 1));
156 if (++size
>= best_size_so_far
)
158 } while ((sk
= sk
->next
) != NULL
);
159 best_size_so_far
= size
;
164 for(;; result
+= UDP_HTABLE_SIZE
) {
165 if (result
> sysctl_local_port_range
[1])
166 result
= sysctl_local_port_range
[0]
167 + ((result
- sysctl_local_port_range
[0]) &
168 (UDP_HTABLE_SIZE
- 1));
169 if (!udp_lport_inuse(result
))
173 udp_port_rover
= snum
= result
;
177 for (sk2
= udp_hash
[snum
& (UDP_HTABLE_SIZE
- 1)];
180 if (sk2
->num
== snum
&&
182 sk2
->bound_dev_if
== sk
->bound_dev_if
&&
185 sk2
->rcv_saddr
== sk
->rcv_saddr
) &&
186 (!sk2
->reuse
|| !sk
->reuse
))
191 write_unlock_bh(&udp_hash_lock
);
195 write_unlock_bh(&udp_hash_lock
);
199 static void udp_v4_hash(struct sock
*sk
)
201 struct sock
**skp
= &udp_hash
[sk
->num
& (UDP_HTABLE_SIZE
- 1)];
203 write_lock_bh(&udp_hash_lock
);
204 if ((sk
->next
= *skp
) != NULL
)
205 (*skp
)->pprev
= &sk
->next
;
208 sock_prot_inc_use(sk
->prot
);
210 write_unlock_bh(&udp_hash_lock
);
213 static void udp_v4_unhash(struct sock
*sk
)
215 write_lock_bh(&udp_hash_lock
);
218 sk
->next
->pprev
= sk
->pprev
;
219 *sk
->pprev
= sk
->next
;
221 sock_prot_dec_use(sk
->prot
);
224 write_unlock_bh(&udp_hash_lock
);
227 /* UDP is nearly always wildcards out the wazoo, it makes no sense to try
228 * harder than this. -DaveM
230 struct sock
*udp_v4_lookup_longway(u32 saddr
, u16 sport
, u32 daddr
, u16 dport
, int dif
)
232 struct sock
*sk
, *result
= NULL
;
233 unsigned short hnum
= ntohs(dport
);
236 for(sk
= udp_hash
[hnum
& (UDP_HTABLE_SIZE
- 1)]; sk
!= NULL
; sk
= sk
->next
) {
237 if(sk
->num
== hnum
) {
240 if(sk
->rcv_saddr
!= daddr
)
245 if(sk
->daddr
!= saddr
)
250 if(sk
->dport
!= sport
)
254 if(sk
->bound_dev_if
) {
255 if(sk
->bound_dev_if
!= dif
)
262 } else if(score
> badness
) {
271 __inline__
struct sock
*udp_v4_lookup(u32 saddr
, u16 sport
, u32 daddr
, u16 dport
, int dif
)
275 read_lock(&udp_hash_lock
);
276 sk
= udp_v4_lookup_longway(saddr
, sport
, daddr
, dport
, dif
);
279 read_unlock(&udp_hash_lock
);
283 static inline struct sock
*udp_v4_mcast_next(struct sock
*sk
,
291 unsigned short hnum
= ntohs(num
);
292 for(; s
; s
= s
->next
) {
293 if ((s
->num
!= hnum
) ||
294 (s
->daddr
&& s
->daddr
!=raddr
) ||
295 (s
->dport
!= rnum
&& s
->dport
!= 0) ||
296 (s
->rcv_saddr
&& s
->rcv_saddr
!= laddr
) ||
297 (s
->bound_dev_if
&& s
->bound_dev_if
!= dif
))
305 * This routine is called by the ICMP module when it gets some
306 * sort of error condition. If err < 0 then the socket should
307 * be closed and the error returned to the user. If err > 0
308 * it's just the icmp type << 8 | icmp code.
309 * Header points to the ip header of the error packet. We move
310 * on past this. Then (as it used to claim before adjustment)
311 * header points to the first 8 bytes of the udp header. We need
312 * to find the appropriate port.
315 void udp_err(struct sk_buff
*skb
, unsigned char *dp
, int len
)
317 struct iphdr
*iph
= (struct iphdr
*)dp
;
318 struct udphdr
*uh
= (struct udphdr
*)(dp
+(iph
->ihl
<<2));
319 int type
= skb
->h
.icmph
->type
;
320 int code
= skb
->h
.icmph
->code
;
326 if (len
< (iph
->ihl
<<2)+sizeof(struct udphdr
)) {
327 ICMP_INC_STATS_BH(IcmpInErrors
);
331 sk
= udp_v4_lookup(iph
->daddr
, uh
->dest
, iph
->saddr
, uh
->source
, skb
->dev
->ifindex
);
333 ICMP_INC_STATS_BH(IcmpInErrors
);
334 return; /* No socket for error */
343 case ICMP_TIME_EXCEEDED
:
346 case ICMP_SOURCE_QUENCH
:
348 case ICMP_PARAMETERPROB
:
350 info
= ntohl(skb
->h
.icmph
->un
.gateway
)>>24;
353 case ICMP_DEST_UNREACH
:
354 if (code
== ICMP_FRAG_NEEDED
) { /* Path MTU discovery */
355 if (sk
->protinfo
.af_inet
.pmtudisc
!= IP_PMTUDISC_DONT
) {
357 info
= ntohs(skb
->h
.icmph
->un
.frag
.mtu
);
364 if (code
<= NR_ICMP_UNREACH
) {
365 harderr
= icmp_err_convert
[code
].fatal
;
366 err
= icmp_err_convert
[code
].errno
;
372 * RFC1122: OK. Passes ICMP errors back to application, as per
375 if (!sk
->protinfo
.af_inet
.recverr
) {
376 if (!harderr
|| sk
->state
!= TCP_ESTABLISHED
)
379 ip_icmp_error(sk
, skb
, err
, uh
->dest
, info
, (u8
*)(uh
+1));
382 sk
->error_report(sk
);
388 static unsigned short udp_check(struct udphdr
*uh
, int len
, unsigned long saddr
, unsigned long daddr
, unsigned long base
)
390 return(csum_tcpudp_magic(saddr
, daddr
, len
, IPPROTO_UDP
, base
));
403 * Copy and checksum a UDP packet from user space into a buffer. We still have
404 * to do the planning to get ip_build_xmit to spot direct transfer to network
405 * card and provide an additional callback mode for direct user->board I/O
406 * transfers. That one will be fun.
409 static int udp_getfrag(const void *p
, char * to
, unsigned int offset
, unsigned int fraglen
)
411 struct udpfakehdr
*ufh
= (struct udpfakehdr
*)p
;
413 if (csum_partial_copy_fromiovecend(to
+sizeof(struct udphdr
), ufh
->iov
, offset
,
414 fraglen
-sizeof(struct udphdr
), &ufh
->wcheck
))
416 ufh
->wcheck
= csum_partial((char *)ufh
, sizeof(struct udphdr
),
418 ufh
->uh
.check
= csum_tcpudp_magic(ufh
->saddr
, ufh
->daddr
,
420 IPPROTO_UDP
, ufh
->wcheck
);
421 if (ufh
->uh
.check
== 0)
423 memcpy(to
, ufh
, sizeof(struct udphdr
));
426 if (csum_partial_copy_fromiovecend(to
, ufh
->iov
, offset
-sizeof(struct udphdr
),
427 fraglen
, &ufh
->wcheck
))
433 * Unchecksummed UDP is sufficiently critical to stuff like ATM video conferencing
434 * that we use two routines for this for speed. Probably we ought to have a
435 * CONFIG_FAST_NET set for >10Mb/second boards to activate this sort of coding.
436 * Timing needed to verify if this is a valid decision.
439 static int udp_getfrag_nosum(const void *p
, char * to
, unsigned int offset
, unsigned int fraglen
)
441 struct udpfakehdr
*ufh
= (struct udpfakehdr
*)p
;
444 memcpy(to
, ufh
, sizeof(struct udphdr
));
445 return memcpy_fromiovecend(to
+sizeof(struct udphdr
), ufh
->iov
, offset
,
446 fraglen
-sizeof(struct udphdr
));
448 return memcpy_fromiovecend(to
, ufh
->iov
, offset
-sizeof(struct udphdr
),
452 int udp_sendmsg(struct sock
*sk
, struct msghdr
*msg
, int len
)
454 int ulen
= len
+ sizeof(struct udphdr
);
455 struct ipcm_cookie ipc
;
456 struct udpfakehdr ufh
;
457 struct rtable
*rt
= NULL
;
464 /* This check is ONLY to check for arithmetic overflow
465 on integer(!) len. Not more! Real check will be made
466 in ip_build_xmit --ANK
468 BTW socket.c -> af_*.c -> ... make multiple
469 invalid conversions size_t -> int. We MUST repair it f.e.
470 by replacing all of them with size_t and revise all
471 the places sort of len += sizeof(struct iphdr)
472 If len was ULONG_MAX-10 it would be cathastrophe --ANK
475 if (len
< 0 || len
> 0xFFFF)
482 if (msg
->msg_flags
&MSG_OOB
) /* Mirror BSD error message compatibility */
486 * Get and verify the address.
490 struct sockaddr_in
* usin
= (struct sockaddr_in
*)msg
->msg_name
;
491 if (msg
->msg_namelen
< sizeof(*usin
))
493 if (usin
->sin_family
!= AF_INET
) {
494 if (usin
->sin_family
!= AF_UNSPEC
)
497 printk("Remind Kuznetsov, he has to repair %s eventually\n", current
->comm
);
500 ufh
.daddr
= usin
->sin_addr
.s_addr
;
501 ufh
.uh
.dest
= usin
->sin_port
;
502 if (ufh
.uh
.dest
== 0)
505 if (sk
->state
!= TCP_ESTABLISHED
)
507 ufh
.daddr
= sk
->daddr
;
508 ufh
.uh
.dest
= sk
->dport
;
509 /* Open fast path for connected socket.
510 Route will not be used, if at least one option is set.
514 ipc
.addr
= sk
->saddr
;
515 ufh
.uh
.source
= sk
->sport
;
518 ipc
.oif
= sk
->bound_dev_if
;
519 if (msg
->msg_controllen
) {
520 err
= ip_cmsg_send(msg
, &ipc
);
528 ipc
.opt
= sk
->protinfo
.af_inet
.opt
;
530 ufh
.saddr
= ipc
.addr
;
531 ipc
.addr
= daddr
= ufh
.daddr
;
533 if (ipc
.opt
&& ipc
.opt
->srr
) {
536 daddr
= ipc
.opt
->faddr
;
539 tos
= RT_TOS(sk
->protinfo
.af_inet
.tos
);
540 if (sk
->localroute
|| (msg
->msg_flags
&MSG_DONTROUTE
) ||
541 (ipc
.opt
&& ipc
.opt
->is_strictroute
)) {
546 if (MULTICAST(daddr
)) {
548 ipc
.oif
= sk
->protinfo
.af_inet
.mc_index
;
550 ufh
.saddr
= sk
->protinfo
.af_inet
.mc_addr
;
555 rt
= (struct rtable
*)sk_dst_check(sk
, 0);
558 err
= ip_route_output(&rt
, daddr
, ufh
.saddr
, tos
, ipc
.oif
);
563 if (rt
->rt_flags
&RTCF_BROADCAST
&& !sk
->broadcast
)
566 sk_dst_set(sk
, dst_clone(&rt
->u
.dst
));
569 if (msg
->msg_flags
&MSG_CONFIRM
)
573 ufh
.saddr
= rt
->rt_src
;
575 ufh
.daddr
= ipc
.addr
= rt
->rt_dst
;
576 ufh
.uh
.len
= htons(ulen
);
578 ufh
.iov
= msg
->msg_iov
;
581 /* RFC1122: OK. Provides the checksumming facility (MUST) as per */
582 /* 4.1.3.4. It's configurable by the application via setsockopt() */
583 /* (MAY) and it defaults to on (MUST). */
585 err
= ip_build_xmit(sk
,
586 (sk
->no_check
== UDP_CSUM_NOXMIT
?
589 &ufh
, ulen
, &ipc
, rt
, msg
->msg_flags
);
596 UDP_INC_STATS_USER(UdpOutDatagrams
);
602 dst_confirm(&rt
->u
.dst
);
603 if (!(msg
->msg_flags
&MSG_PROBE
) || len
)
604 goto back_from_confirm
;
610 * IOCTL requests applicable to the UDP protocol
613 int udp_ioctl(struct sock
*sk
, int cmd
, unsigned long arg
)
619 int amount
= atomic_read(&sk
->wmem_alloc
);
620 return put_user(amount
, (int *)arg
);
626 unsigned long amount
;
629 spin_lock_irq(&sk
->receive_queue
.lock
);
630 skb
= skb_peek(&sk
->receive_queue
);
633 * We will only return the amount
634 * of this packet since that is all
637 amount
= skb
->len
- sizeof(struct udphdr
);
639 spin_unlock_irq(&sk
->receive_queue
.lock
);
640 return put_user(amount
, (int *)arg
);
649 static __inline__
int __udp_checksum_complete(struct sk_buff
*skb
)
651 return (unsigned short)csum_fold(csum_partial(skb
->h
.raw
, skb
->len
, skb
->csum
));
654 static __inline__
int udp_checksum_complete(struct sk_buff
*skb
)
656 return skb
->ip_summed
!= CHECKSUM_UNNECESSARY
&&
657 __udp_checksum_complete(skb
);
661 * This should be easy, if there is something there we
662 * return it, otherwise we block.
665 int udp_recvmsg(struct sock
*sk
, struct msghdr
*msg
, int len
,
666 int noblock
, int flags
, int *addr_len
)
668 struct sockaddr_in
*sin
= (struct sockaddr_in
*)msg
->msg_name
;
673 * Check any passed addresses
676 *addr_len
=sizeof(*sin
);
678 if (flags
& MSG_ERRQUEUE
)
679 return ip_recv_error(sk
, msg
, len
);
682 * From here the generic datagram does a lot of the work. Come
683 * the finished NET3, it will do _ALL_ the work!
686 skb
= skb_recv_datagram(sk
, flags
, noblock
, &err
);
690 copied
= skb
->len
- sizeof(struct udphdr
);
693 msg
->msg_flags
|= MSG_TRUNC
;
696 if (skb
->ip_summed
==CHECKSUM_UNNECESSARY
) {
697 err
= skb_copy_datagram_iovec(skb
, sizeof(struct udphdr
), msg
->msg_iov
,
699 } else if (msg
->msg_flags
&MSG_TRUNC
) {
700 if (__udp_checksum_complete(skb
))
702 err
= skb_copy_datagram_iovec(skb
, sizeof(struct udphdr
), msg
->msg_iov
,
705 err
= copy_and_csum_toiovec(msg
->msg_iov
, skb
, sizeof(struct udphdr
));
713 sk
->stamp
=skb
->stamp
;
715 /* Copy the address. */
718 sin
->sin_family
= AF_INET
;
719 sin
->sin_port
= skb
->h
.uh
->source
;
720 sin
->sin_addr
.s_addr
= skb
->nh
.iph
->saddr
;
721 memset(sin
->sin_zero
, 0, sizeof(sin
->sin_zero
));
723 if (sk
->protinfo
.af_inet
.cmsg_flags
)
724 ip_cmsg_recv(msg
, skb
);
728 skb_free_datagram(sk
, skb
);
733 UDP_INC_STATS_BH(UdpInErrors
);
736 if (flags
&MSG_PEEK
) {
738 spin_lock_irq(&sk
->receive_queue
.lock
);
739 if (skb
== skb_peek(&sk
->receive_queue
)) {
740 __skb_unlink(skb
, &sk
->receive_queue
);
743 spin_unlock_irq(&sk
->receive_queue
.lock
);
748 skb_free_datagram(sk
, skb
);
751 * Error for blocking case is chosen to masquerade
752 * as some normal condition.
754 return (flags
&MSG_DONTWAIT
) ? -EAGAIN
: -EHOSTUNREACH
;
757 int udp_connect(struct sock
*sk
, struct sockaddr
*uaddr
, int addr_len
)
759 struct sockaddr_in
*usin
= (struct sockaddr_in
*) uaddr
;
764 if (addr_len
< sizeof(*usin
))
767 if (usin
->sin_family
!= AF_INET
)
768 return -EAFNOSUPPORT
;
772 err
= ip_route_connect(&rt
, usin
->sin_addr
.s_addr
, sk
->saddr
,
773 sk
->protinfo
.af_inet
.tos
|sk
->localroute
, sk
->bound_dev_if
);
776 if ((rt
->rt_flags
&RTCF_BROADCAST
) && !sk
->broadcast
) {
781 sk
->saddr
= rt
->rt_src
; /* Update source address */
783 sk
->rcv_saddr
= rt
->rt_src
;
784 sk
->daddr
= rt
->rt_dst
;
785 sk
->dport
= usin
->sin_port
;
786 sk
->state
= TCP_ESTABLISHED
;
788 sk_dst_set(sk
, &rt
->u
.dst
);
792 int udp_disconnect(struct sock
*sk
, int flags
)
795 * 1003.1g - break association.
798 sk
->state
= TCP_CLOSE
;
802 sk
->bound_dev_if
= 0;
807 static void udp_close(struct sock
*sk
, long timeout
)
809 inet_sock_release(sk
);
812 static int udp_queue_rcv_skb(struct sock
* sk
, struct sk_buff
*skb
)
815 * Charge it to the socket, dropping if the queue is full.
818 #if defined(CONFIG_FILTER)
819 if (sk
->filter
&& skb
->ip_summed
!= CHECKSUM_UNNECESSARY
) {
820 if (__udp_checksum_complete(skb
)) {
821 UDP_INC_STATS_BH(UdpInErrors
);
822 IP_INC_STATS_BH(IpInDiscards
);
823 ip_statistics
[smp_processor_id()*2].IpInDelivers
--;
827 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
831 if (sock_queue_rcv_skb(sk
,skb
)<0) {
832 UDP_INC_STATS_BH(UdpInErrors
);
833 IP_INC_STATS_BH(IpInDiscards
);
834 ip_statistics
[smp_processor_id()*2].IpInDelivers
--;
838 UDP_INC_STATS_BH(UdpInDatagrams
);
843 * Multicasts and broadcasts go to each listener.
845 * Note: called only from the BH handler context,
846 * so we don't need to lock the hashes.
848 static int udp_v4_mcast_deliver(struct sk_buff
*skb
, struct udphdr
*uh
,
849 u32 saddr
, u32 daddr
)
854 read_lock(&udp_hash_lock
);
855 sk
= udp_hash
[ntohs(uh
->dest
) & (UDP_HTABLE_SIZE
- 1)];
856 dif
= skb
->dev
->ifindex
;
857 sk
= udp_v4_mcast_next(sk
, uh
->dest
, saddr
, uh
->source
, daddr
, dif
);
859 struct sock
*sknext
= NULL
;
862 struct sk_buff
*skb1
= skb
;
864 sknext
= udp_v4_mcast_next(sk
->next
, uh
->dest
, saddr
,
865 uh
->source
, daddr
, dif
);
867 skb1
= skb_clone(skb
, GFP_ATOMIC
);
870 udp_queue_rcv_skb(sk
, skb1
);
875 read_unlock(&udp_hash_lock
);
879 /* Initialize UDP checksum. If exited with zero value (success),
880 * CHECKSUM_UNNECESSARY means, that no more checks are required.
881 * Otherwise, csum completion requires chacksumming packet body,
882 * including udp header and folding it to skb->csum.
884 static int udp_checksum_init(struct sk_buff
*skb
, struct udphdr
*uh
,
885 unsigned short ulen
, u32 saddr
, u32 daddr
)
887 if (uh
->check
== 0) {
888 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
889 } else if (skb
->ip_summed
== CHECKSUM_HW
) {
890 if (udp_check(uh
, ulen
, saddr
, daddr
, skb
->csum
))
892 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
893 } else if (skb
->ip_summed
!= CHECKSUM_UNNECESSARY
)
894 skb
->csum
= csum_tcpudp_nofold(saddr
, daddr
, ulen
, IPPROTO_UDP
, 0);
895 /* Probably, we should checksum udp header (it should be in cache
896 * in any case) and data in tiny packets (< rx copybreak).
902 * All we need to do is get the socket, and then do a checksum.
905 int udp_rcv(struct sk_buff
*skb
, unsigned short len
)
910 struct rtable
*rt
= (struct rtable
*)skb
->dst
;
911 u32 saddr
= skb
->nh
.iph
->saddr
;
912 u32 daddr
= skb
->nh
.iph
->daddr
;
919 __skb_pull(skb
, skb
->h
.raw
- skb
->data
);
921 IP_INC_STATS_BH(IpInDelivers
);
924 * Validate the packet and the UDP length.
927 ulen
= ntohs(uh
->len
);
929 if (ulen
> len
|| ulen
< sizeof(*uh
)) {
930 NETDEBUG(printk(KERN_DEBUG
"UDP: short packet: %d/%d\n", ulen
, len
));
931 UDP_INC_STATS_BH(UdpInErrors
);
937 if (udp_checksum_init(skb
, uh
, ulen
, saddr
, daddr
) < 0)
940 if(rt
->rt_flags
& (RTCF_BROADCAST
|RTCF_MULTICAST
))
941 return udp_v4_mcast_deliver(skb
, uh
, saddr
, daddr
);
943 sk
= udp_v4_lookup(saddr
, uh
->source
, daddr
, uh
->dest
, skb
->dev
->ifindex
);
946 udp_queue_rcv_skb(sk
, skb
);
951 /* No socket. Drop packet silently, if checksum is wrong */
952 if (udp_checksum_complete(skb
))
955 UDP_INC_STATS_BH(UdpNoPorts
);
956 icmp_send(skb
, ICMP_DEST_UNREACH
, ICMP_PORT_UNREACH
, 0);
959 * Hmm. We got an UDP packet to a port to which we
960 * don't wanna listen. Ignore it.
967 * RFC1122: OK. Discards the bad packet silently (as far as
968 * the network is concerned, anyway) as per 4.1.3.4 (MUST).
970 NETDEBUG(printk(KERN_DEBUG
"UDP: bad checksum. From %d.%d.%d.%d:%d to %d.%d.%d.%d:%d ulen %d\n",
976 UDP_INC_STATS_BH(UdpInErrors
);
981 static void get_udp_sock(struct sock
*sp
, char *tmpbuf
, int i
)
983 unsigned int dest
, src
;
986 unsigned long timer_expires
;
990 destp
= ntohs(sp
->dport
);
991 srcp
= ntohs(sp
->sport
);
992 timer_active
= timer_pending(&sp
->timer
) ? 2 : 0;
993 timer_expires
= (timer_active
== 2 ? sp
->timer
.expires
: jiffies
);
994 sprintf(tmpbuf
, "%4d: %08X:%04X %08X:%04X"
995 " %02X %08X:%08X %02X:%08lX %08X %5d %8d %ld %d %p",
996 i
, src
, srcp
, dest
, destp
, sp
->state
,
997 atomic_read(&sp
->wmem_alloc
), atomic_read(&sp
->rmem_alloc
),
998 timer_active
, timer_expires
-jiffies
, 0,
999 sp
->socket
->inode
->i_uid
, 0,
1000 sp
->socket
? sp
->socket
->inode
->i_ino
: 0,
1001 atomic_read(&sp
->refcnt
), sp
);
1004 int udp_get_info(char *buffer
, char **start
, off_t offset
, int length
)
1006 int len
= 0, num
= 0, i
;
1012 len
+= sprintf(buffer
, "%-127s\n",
1013 " sl local_address rem_address st tx_queue "
1014 "rx_queue tr tm->when retrnsmt uid timeout inode");
1016 read_lock(&udp_hash_lock
);
1017 for (i
= 0; i
< UDP_HTABLE_SIZE
; i
++) {
1020 for (sk
= udp_hash
[i
]; sk
; sk
= sk
->next
, num
++) {
1021 if (sk
->family
!= PF_INET
)
1026 get_udp_sock(sk
, tmpbuf
, i
);
1027 len
+= sprintf(buffer
+len
, "%-127s\n", tmpbuf
);
1033 read_unlock(&udp_hash_lock
);
1034 begin
= len
- (pos
- offset
);
1035 *start
= buffer
+ begin
;
1044 struct proto udp_prot
= {
1047 connect
: udp_connect
,
1048 disconnect
: udp_disconnect
,
1050 setsockopt
: ip_setsockopt
,
1051 getsockopt
: ip_getsockopt
,
1052 sendmsg
: udp_sendmsg
,
1053 recvmsg
: udp_recvmsg
,
1054 backlog_rcv
: udp_queue_rcv_skb
,
1056 unhash
: udp_v4_unhash
,
1057 get_port
: udp_v4_get_port
,