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: @(#)udp.c 1.0.13 06/02/93 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 * David S. Miller : New socket lookup architecture. 53 * Last socket cache retained as it 54 * does have a high hit rate. 55 * Olaf Kirch : Don't linearise iovec on sendmsg. 58 * This program is free software; you can redistribute it and/or 59 * modify it under the terms of the GNU General Public License 60 * as published by the Free Software Foundation; either version 61 * 2 of the License, or (at your option) any later version. 66 SHOULD send ICMP_PORT_UNREACHABLE in response to datagrams to 67 an un-listened port. (OK) 69 MUST pass IP options from IP -> application (OK) 70 MUST allow application to specify IP options (OK) 71 4.1.3.3 (ICMP Messages) 72 MUST pass ICMP error messages to application (OK) 73 4.1.3.4 (UDP Checksums) 74 MUST provide facility for checksumming (OK) 75 MAY allow application to control checksumming (OK) 76 MUST default to checksumming on (OK) 77 MUST discard silently datagrams with bad csums (OK) 78 4.1.3.5 (UDP Multihoming) 79 MUST allow application to specify source address (OK) 80 SHOULD be able to communicate the chosen src addr up to application 81 when application doesn't choose (NOT YET - doesn't seem to be in the BSD API) 82 [Does opening a SOCK_PACKET and snooping your output count 8)] 83 4.1.3.6 (Invalid Addresses) 84 MUST discard invalid source addresses (NOT YET -- will be implemented 85 in IP, so UDP will eventually be OK. Right now it's a violation.) 86 MUST only send datagrams with one of our addresses (NOT YET - ought to be OK ) 90 #include <asm/system.h> 91 #include <asm/uaccess.h> 92 #include <linux/types.h> 93 #include <linux/sched.h> 94 #include <linux/fcntl.h> 95 #include <linux/socket.h> 96 #include <linux/sockios.h> 98 #include <linux/errno.h> 99 #include <linux/timer.h> 100 #include <linux/termios.h> 101 #include <linux/mm.h> 102 #include <linux/config.h> 103 #include <linux/inet.h> 104 #include <linux/netdevice.h> 105 #include <net/snmp.h> 107 #include <net/protocol.h> 109 #include <linux/skbuff.h> 110 #include <net/sock.h> 112 #include <net/icmp.h> 113 #include <net/route.h> 114 #include <net/checksum.h> 115 #include <linux/ipsec.h> 118 * Snmp MIB for the UDP layer 121 struct udp_mib udp_statistics
; 123 struct sock
*udp_hash
[UDP_HTABLE_SIZE
]; 125 static intudp_v4_verify_bind(struct sock
*sk
,unsigned short snum
) 128 int retval
=0, sk_reuse
= sk
->reuse
; 131 for(sk2
= udp_hash
[snum
& (UDP_HTABLE_SIZE
-1)]; sk2
!= NULL
; sk2
= sk2
->next
) { 132 if((sk2
->num
== snum
) && (sk2
!= sk
)) { 133 unsigned char state
= sk2
->state
; 134 int sk2_reuse
= sk2
->reuse
; 136 if(!sk2
->rcv_saddr
|| !sk
->rcv_saddr
) { 139 (state
== TCP_LISTEN
)) { 143 }else if(sk2
->rcv_saddr
== sk
->rcv_saddr
) { 146 (state
== TCP_LISTEN
)) { 157 staticinlineintudp_lport_inuse(int num
) 159 struct sock
*sk
= udp_hash
[num
& (UDP_HTABLE_SIZE
-1)]; 161 for(; sk
!= NULL
; sk
= sk
->next
) { 168 /* Shared by v4/v6 tcp. */ 169 unsigned shortudp_good_socknum(void) 173 int i
, best
=0, size
=32767;/* a big num. */ 176 base
= PROT_SOCK
+ (start
&1023) +1; 179 for(i
=0; i
< UDP_HTABLE_SIZE
; i
++) { 180 struct sock
*sk
= udp_hash
[i
]; 182 start
= (i
+1+ start
) &1023; 183 result
= i
+ base
+1; 190 }while((sk
= sk
->next
)); 197 while(udp_lport_inuse(base
+ best
+1)) 198 best
+= UDP_HTABLE_SIZE
; 199 result
= (best
+ base
+1); 205 /* Last hit UDP socket cache, this is ipv4 specific so make it static. */ 206 static u32 uh_cache_saddr
, uh_cache_daddr
; 207 static u16 uh_cache_dport
, uh_cache_sport
; 208 static struct sock
*uh_cache_sk
= NULL
; 210 static voidudp_v4_hash(struct sock
*sk
) 215 num
&= (UDP_HTABLE_SIZE
-1); 216 skp
= &udp_hash
[num
]; 225 static voidudp_v4_unhash(struct sock
*sk
) 230 num
&= (UDP_HTABLE_SIZE
-1); 231 skp
= &udp_hash
[num
]; 234 while(*skp
!= NULL
) { 239 skp
= &((*skp
)->next
); 241 if(uh_cache_sk
== sk
) 246 static voidudp_v4_rehash(struct sock
*sk
) 250 int oldnum
= sk
->hashent
; 252 num
&= (UDP_HTABLE_SIZE
-1); 253 skp
= &udp_hash
[oldnum
]; 256 while(*skp
!= NULL
) { 261 skp
= &((*skp
)->next
); 263 sk
->next
= udp_hash
[num
]; 266 if(uh_cache_sk
== sk
) 271 /* UDP is nearly always wildcards out the wazoo, it makes no sense to try 272 * harder than this here plus the last hit cache. -DaveM 274 struct sock
*udp_v4_lookup_longway(u32 saddr
, u16 sport
, u32 daddr
, u16 dport
) 276 struct sock
*sk
, *result
= NULL
; 277 unsigned short hnum
=ntohs(dport
); 280 for(sk
= udp_hash
[hnum
& (UDP_HTABLE_SIZE
-1)]; sk
!= NULL
; sk
= sk
->next
) { 281 if((sk
->num
== hnum
) && !(sk
->dead
&& (sk
->state
== TCP_CLOSE
))) { 284 if(sk
->rcv_saddr
!= daddr
) 289 if(sk
->daddr
!= saddr
) 293 if(sk
->dummy_th
.dest
) { 294 if(sk
->dummy_th
.dest
!= sport
) 301 }else if(score
> badness
) { 310 __inline__
struct sock
*udp_v4_lookup(u32 saddr
, u16 sport
, u32 daddr
, u16 dport
) 315 uh_cache_saddr
== saddr
&& 316 uh_cache_sport
== sport
&& 317 uh_cache_dport
== dport
&& 318 uh_cache_daddr
== daddr
) 321 sk
=udp_v4_lookup_longway(saddr
, sport
, daddr
, dport
); 323 uh_cache_saddr
= saddr
; 324 uh_cache_daddr
= daddr
; 325 uh_cache_sport
= sport
; 326 uh_cache_dport
= dport
; 330 #ifdef CONFIG_IP_TRANSPARENT_PROXY 331 #define secondlist(hpnum, sk, fpass) \ 332 ({ struct sock *s1; if(!(sk) && (fpass)--) \ 333 s1 = udp_hash[(hpnum) & (TCP_HTABLE_SIZE - 1)]; \ 339 #define udp_v4_proxy_loop_init(hnum, hpnum, sk, fpass) \ 340 secondlist((hpnum), udp_hash[(hnum)&(TCP_HTABLE_SIZE-1)],(fpass)) 342 #define udp_v4_proxy_loop_next(hnum, hpnum, sk, fpass) \ 343 secondlist((hpnum),(sk)->next,(fpass)) 345 struct sock
*udp_v4_proxy_lookup(unsigned short num
,unsigned long raddr
, 346 unsigned short rnum
,unsigned long laddr
, 347 unsigned long paddr
,unsigned short pnum
) 349 struct sock
*s
, *result
= NULL
; 351 unsigned short hnum
=ntohs(num
); 352 unsigned short hpnum
=ntohs(pnum
); 356 for(s
=udp_v4_proxy_loop_init(hnum
, hpnum
, s
, firstpass
); 358 s
=udp_v4_proxy_loop_next(hnum
, hpnum
, s
, firstpass
)) { 359 if(s
->num
== hnum
|| s
->num
== hpnum
) { 361 if(s
->dead
&& (s
->state
== TCP_CLOSE
)) 364 if((s
->num
!= hpnum
|| s
->rcv_saddr
!= paddr
) && 365 (s
->num
!= hnum
|| s
->rcv_saddr
!= laddr
)) 370 if(s
->daddr
!= raddr
) 374 if(s
->dummy_th
.dest
) { 375 if(s
->dummy_th
.dest
!= rnum
) 379 if(score
==3&& s
->num
== hnum
) { 382 }else if(score
> badness
&& (s
->num
== hpnum
|| s
->rcv_saddr
)) { 393 #undef udp_v4_proxy_loop_init 394 #undef udp_v4_proxy_loop_next 398 staticinlinestruct sock
*udp_v4_mcast_next(struct sock
*sk
, 405 unsigned short hnum
=ntohs(num
); 406 for(; s
; s
= s
->next
) { 407 if((s
->num
!= hnum
) || 408 (s
->dead
&& (s
->state
== TCP_CLOSE
)) || 409 (s
->daddr
&& s
->daddr
!=raddr
) || 410 (s
->dummy_th
.dest
!= rnum
&& s
->dummy_th
.dest
!=0) || 411 (s
->rcv_saddr
&& s
->rcv_saddr
!= laddr
)) 418 #define min(a,b) ((a)<(b)?(a):(b)) 421 * This routine is called by the ICMP module when it gets some 422 * sort of error condition. If err < 0 then the socket should 423 * be closed and the error returned to the user. If err > 0 424 * it's just the icmp type << 8 | icmp code. 425 * Header points to the ip header of the error packet. We move 426 * on past this. Then (as it used to claim before adjustment) 427 * header points to the first 8 bytes of the udp header. We need 428 * to find the appropriate port. 431 voidudp_err(struct sk_buff
*skb
,unsigned char*dp
) 433 struct iphdr
*iph
= (struct iphdr
*)dp
; 434 struct udphdr
*uh
= (struct udphdr
*)(dp
+(iph
->ihl
<<2)); 435 int type
= skb
->h
.icmph
->type
; 436 int code
= skb
->h
.icmph
->code
; 439 sk
=udp_v4_lookup(iph
->daddr
, uh
->dest
, iph
->saddr
, uh
->source
); 441 return;/* No socket for error */ 443 if(sk
->ip_recverr
&& !sk
->sock_readers
) { 444 struct sk_buff
*skb2
=skb_clone(skb
, GFP_ATOMIC
); 445 if(skb2
&&sock_queue_err_skb(sk
, skb2
)) 446 kfree_skb(skb2
, FREE_READ
); 449 if(type
== ICMP_SOURCE_QUENCH
) 451 if(sk
->cong_window
>1) 452 sk
->cong_window
= sk
->cong_window
/2; 456 if(type
== ICMP_PARAMETERPROB
) 459 sk
->error_report(sk
); 463 if(type
== ICMP_DEST_UNREACH
&& code
== ICMP_FRAG_NEEDED
) 465 if(sk
->ip_pmtudisc
!= IP_PMTUDISC_DONT
) { 467 sk
->error_report(sk
); 473 * Various people wanted BSD UDP semantics. Well they've come 474 * back out because they slow down response to stuff like dead 475 * or unreachable name servers and they screw term users something 476 * chronic. Oh and it violates RFC1122. So basically fix your 477 * client code people. 480 /* RFC1122: OK. Passes ICMP errors back to application, as per */ 482 /* After the comment above, that should be no surprise. */ 484 if(code
< NR_ICMP_UNREACH
&& icmp_err_convert
[code
].fatal
) 487 * 4.x BSD compatibility item. Break RFC1122 to 488 * get BSD socket semantics. 490 if(sk
->bsdism
&& sk
->state
!=TCP_ESTABLISHED
) 492 sk
->err
= icmp_err_convert
[code
].errno
; 493 sk
->error_report(sk
); 498 static unsigned shortudp_check(struct udphdr
*uh
,int len
,unsigned long saddr
,unsigned long daddr
,unsigned long base
) 500 return(csum_tcpudp_magic(saddr
, daddr
, len
, IPPROTO_UDP
, base
)); 515 * Copy and checksum a UDP packet from user space into a buffer. We still have 516 * to do the planning to get ip_build_xmit to spot direct transfer to network 517 * card and provide an additional callback mode for direct user->board I/O 518 * transfers. That one will be fun. 521 static intudp_getfrag(const void*p
,char* to
,unsigned int offset
,unsigned int fraglen
) 523 struct udpfakehdr
*ufh
= (struct udpfakehdr
*)p
; 530 fraglen
-=sizeof(struct udphdr
); 531 dst
+=sizeof(struct udphdr
); 536 if((len
= iov
->iov_len
) > fraglen
) 538 src
= (char*) iov
->iov_base
+ iov
->iov_len
- len
; 539 ufh
->wcheck
=csum_partial_copy_fromuser(src
, 540 dst
+ fraglen
- len
, len
, 542 if((iov
->iov_len
-= len
) ==0) { 543 if(--(ufh
->nriov
) <0) { 544 printk(KERN_NOTICE
"udp_getfrag: nriov = %d\n", 555 ufh
->wcheck
=csum_partial((char*)ufh
,sizeof(struct udphdr
), 557 ufh
->uh
.check
=csum_tcpudp_magic(ufh
->saddr
, ufh
->daddr
, 559 IPPROTO_UDP
, ufh
->wcheck
); 560 if(ufh
->uh
.check
==0) 562 memcpy(to
, ufh
,sizeof(struct udphdr
)); 568 * Unchecksummed UDP is sufficiently critical to stuff like ATM video conferencing 569 * that we use two routines for this for speed. Probably we ought to have a 570 * CONFIG_FAST_NET set for >10Mb/second boards to activate this sort of coding. 571 * Timing needed to verify if this is a valid decision. 574 static intudp_getfrag_nosum(const void*p
,char* to
,unsigned int offset
,unsigned int fraglen
) 576 struct udpfakehdr
*ufh
= (struct udpfakehdr
*)p
; 584 fraglen
-=sizeof(struct udphdr
); 585 dst
+=sizeof(struct udphdr
); 590 if((len
= iov
->iov_len
) > fraglen
) 592 src
= (char*) iov
->iov_base
+ iov
->iov_len
- len
; 593 err
=copy_from_user(dst
+ fraglen
- len
, src
, len
); 595 if((iov
->iov_len
-= len
) ==0) { 596 if(--(ufh
->nriov
) <0) { 597 printk(KERN_NOTICE
"udp_getfrag: nriov = %d\n", 603 }while(fraglen
&& err
>=0); 607 memcpy(to
, ufh
,sizeof(struct udphdr
)); 612 intudp_sendmsg(struct sock
*sk
,struct msghdr
*msg
,int len
) 614 int ulen
= len
+sizeof(struct udphdr
); 615 struct device
*dev
= NULL
; 616 struct ipcm_cookie ipc
; 617 struct udpfakehdr ufh
; 631 if(msg
->msg_flags
&MSG_OOB
)/* Mirror BSD error message compatibility */ 634 if(msg
->msg_flags
&~(MSG_DONTROUTE
|MSG_DONTWAIT
)) 638 * Get and verify the address. 641 if(msg
->msg_namelen
) { 642 struct sockaddr_in
* usin
= (struct sockaddr_in
*)msg
->msg_name
; 643 if(msg
->msg_namelen
<sizeof(*usin
)) 645 if(usin
->sin_family
!= AF_INET
) { 646 static int complained
; 648 printk(KERN_WARNING
"%s forgot to set AF_INET in udp sendmsg. Fix it!\n", current
->comm
); 652 ufh
.daddr
= usin
->sin_addr
.s_addr
; 653 ufh
.uh
.dest
= usin
->sin_port
; 657 if(sk
->state
!= TCP_ESTABLISHED
) 659 ufh
.daddr
= sk
->daddr
; 660 ufh
.uh
.dest
= sk
->dummy_th
.dest
; 663 ipc
.addr
= sk
->saddr
; 665 if(msg
->msg_controllen
) { 666 err
=ip_cmsg_send(msg
, &ipc
, &dev
); 675 ufh
.saddr
= ipc
.addr
; 676 ipc
.addr
= daddr
= ufh
.daddr
; 678 if(ipc
.opt
&& ipc
.opt
->srr
) { 681 daddr
= ipc
.opt
->faddr
; 683 tos
=RT_TOS(sk
->ip_tos
) | (sk
->localroute
|| (msg
->msg_flags
&MSG_DONTROUTE
) || 684 (ipc
.opt
&& ipc
.opt
->is_strictroute
)); 686 if(MULTICAST(daddr
) && sk
->ip_mc_index
&& dev
== NULL
) 687 err
=ip_route_output_dev(&rt
, daddr
, ufh
.saddr
, tos
, sk
->ip_mc_index
); 689 err
=ip_route_output(&rt
, daddr
, ufh
.saddr
, tos
, dev
); 692 if(free
)kfree(ipc
.opt
); 696 if(rt
->rt_flags
&RTF_BROADCAST
&& !sk
->broadcast
) { 697 if(free
)kfree(ipc
.opt
); 702 ufh
.saddr
= rt
->rt_src
; 704 ufh
.daddr
= ipc
.addr
= rt
->rt_dst
; 705 ufh
.uh
.source
= sk
->dummy_th
.source
; 706 ufh
.uh
.len
=htons(ulen
); 708 ufh
.other
= (htons(ulen
) <<16) + IPPROTO_UDP
*256; 709 ufh
.iov
= msg
->msg_iov
+ msg
->msg_iovlen
-1; 710 ufh
.nriov
= msg
->msg_iovlen
; 713 /* RFC1122: OK. Provides the checksumming facility (MUST) as per */ 714 /* 4.1.3.4. It's configurable by the application via setsockopt() */ 715 /* (MAY) and it defaults to on (MUST). Almost makes up for the */ 716 /* violation above. -- MS */ 720 err
=ip_build_xmit(sk
, udp_getfrag_nosum
, &ufh
, ulen
, 721 &ipc
, rt
, msg
->msg_flags
); 723 err
=ip_build_xmit(sk
, udp_getfrag
, &ufh
, ulen
, 724 &ipc
, rt
, msg
->msg_flags
); 731 udp_statistics
.UdpOutDatagrams
++; 738 * IOCTL requests applicable to the UDP protocol 741 intudp_ioctl(struct sock
*sk
,int cmd
,unsigned long arg
) 747 unsigned long amount
; 749 if(sk
->state
== TCP_LISTEN
)return(-EINVAL
); 750 amount
=sock_wspace(sk
); 751 returnput_user(amount
, (int*)arg
); 757 unsigned long amount
; 759 if(sk
->state
== TCP_LISTEN
)return(-EINVAL
); 761 skb
=skb_peek(&sk
->receive_queue
); 764 * We will only return the amount 765 * of this packet since that is all 768 amount
= skb
->len
-sizeof(struct udphdr
); 770 returnput_user(amount
, (int*)arg
); 781 * This should be easy, if there is something there we 782 * return it, otherwise we block. 785 intudp_recvmsg(struct sock
*sk
,struct msghdr
*msg
,int len
, 786 int noblock
,int flags
,int*addr_len
) 792 struct sockaddr_in
*sin
=(struct sockaddr_in
*)msg
->msg_name
; 795 * Check any passed addresses 799 *addr_len
=sizeof(*sin
); 801 if(sk
->ip_recverr
&& (skb
=skb_dequeue(&sk
->error_queue
)) != NULL
) { 803 if(msg
->msg_controllen
==0) { 804 skb_free_datagram(sk
, skb
); 807 put_cmsg(msg
, SOL_IP
, IP_RECVERR
, skb
->len
, skb
->data
); 808 skb_free_datagram(sk
, skb
); 813 * From here the generic datagram does a lot of the work. Come 814 * the finished NET3, it will do _ALL_ the work! 817 skb
=skb_recv_datagram(sk
,flags
,noblock
,&er
); 821 truesize
= skb
->len
-sizeof(struct udphdr
); 825 msg
->msg_flags
|= MSG_TRUNC
; 830 * FIXME : should use udp header size info value 833 er
=skb_copy_datagram_iovec(skb
,sizeof(struct udphdr
),msg
->msg_iov
,copied
); 836 sk
->stamp
=skb
->stamp
; 838 /* Copy the address. */ 841 sin
->sin_family
= AF_INET
; 842 sin
->sin_port
= skb
->h
.uh
->source
; 843 sin
->sin_addr
.s_addr
= skb
->nh
.iph
->saddr
; 844 #ifdef CONFIG_IP_TRANSPARENT_PROXY 848 * We map the first 8 bytes of a second sockaddr_in 849 * into the last 8 (unused) bytes of a sockaddr_in. 850 * This _is_ ugly, but it's the only way to do it 851 * easily, without adding system calls. 853 struct sockaddr_in
*sinto
= 854 (struct sockaddr_in
*) sin
->sin_zero
; 856 sinto
->sin_family
= AF_INET
; 857 sinto
->sin_port
= skb
->h
.uh
->dest
; 858 sinto
->sin_addr
.s_addr
= skb
->nh
.iph
->daddr
; 862 if(sk
->ip_cmsg_flags
) 863 ip_cmsg_recv(msg
, skb
); 865 skb_free_datagram(sk
, skb
); 869 intudp_connect(struct sock
*sk
,struct sockaddr
*uaddr
,int addr_len
) 871 struct sockaddr_in
*usin
= (struct sockaddr_in
*) uaddr
; 876 if(addr_len
<sizeof(*usin
)) 880 * 1003.1g - break association. 883 if(usin
->sin_family
==AF_UNSPEC
) 885 sk
->saddr
=INADDR_ANY
; 886 sk
->rcv_saddr
=INADDR_ANY
; 887 sk
->daddr
=INADDR_ANY
; 888 sk
->state
= TCP_CLOSE
; 889 if(uh_cache_sk
== sk
) 894 if(usin
->sin_family
&& usin
->sin_family
!= AF_INET
) 895 return(-EAFNOSUPPORT
); 897 err
=ip_route_connect(&rt
, usin
->sin_addr
.s_addr
, sk
->saddr
, 898 sk
->ip_tos
|sk
->localroute
); 901 if((rt
->rt_flags
&RTF_BROADCAST
) && !sk
->broadcast
) { 906 sk
->saddr
= rt
->rt_src
;/* Update source address */ 908 sk
->rcv_saddr
= rt
->rt_src
; 909 sk
->daddr
= rt
->rt_dst
; 910 sk
->dummy_th
.dest
= usin
->sin_port
; 911 sk
->state
= TCP_ESTABLISHED
; 912 if(uh_cache_sk
== sk
) 919 static voidudp_close(struct sock
*sk
,unsigned long timeout
) 922 sk
->state
= TCP_CLOSE
; 923 if(uh_cache_sk
== sk
) 931 static intudp_queue_rcv_skb(struct sock
* sk
,struct sk_buff
*skb
) 934 * Check the security clearance 937 if(!ipsec_sk_policy(sk
,skb
)) 939 kfree_skb(skb
, FREE_WRITE
); 944 * Charge it to the socket, dropping if the queue is full. 947 if(__sock_queue_rcv_skb(sk
,skb
)<0) { 948 udp_statistics
.UdpInErrors
++; 949 ip_statistics
.IpInDiscards
++; 950 ip_statistics
.IpInDelivers
--; 951 kfree_skb(skb
, FREE_WRITE
); 954 udp_statistics
.UdpInDatagrams
++; 959 staticinlinevoidudp_deliver(struct sock
*sk
,struct sk_buff
*skb
) 961 if(sk
->sock_readers
) { 962 __skb_queue_tail(&sk
->back_log
, skb
); 965 udp_queue_rcv_skb(sk
, skb
); 969 * Multicasts and broadcasts go to each listener. 971 static intudp_v4_mcast_deliver(struct sk_buff
*skb
,struct udphdr
*uh
, 972 u32 saddr
, u32 daddr
) 978 sk
= udp_hash
[ntohs(uh
->dest
) & (UDP_HTABLE_SIZE
-1)]; 979 sk
=udp_v4_mcast_next(sk
, uh
->dest
, saddr
, uh
->source
, daddr
); 981 struct sock
*sknext
= NULL
; 984 struct sk_buff
*skb1
= skb
; 986 sknext
=udp_v4_mcast_next(sk
->next
, uh
->dest
, saddr
, 989 skb1
=skb_clone(skb
, GFP_ATOMIC
); 992 udp_deliver(sk
, skb1
); 999 kfree_skb(skb
, FREE_READ
); 1003 #ifdef CONFIG_IP_TRANSPARENT_PROXY 1005 * Check whether a received UDP packet might be for one of our 1009 intudp_chkaddr(struct sk_buff
*skb
) 1011 struct iphdr
*iph
= skb
->nh
.iph
; 1012 struct udphdr
*uh
= (struct udphdr
*)(skb
->nh
.raw
+ iph
->ihl
*4); 1015 sk
=udp_v4_lookup(iph
->saddr
, uh
->source
, iph
->daddr
, uh
->dest
); 1019 /* 0 means accept all LOCAL addresses here, not all the world... */ 1020 if(sk
->rcv_saddr
==0) 1028 * All we need to do is get the socket, and then do a checksum. 1031 intudp_rcv(struct sk_buff
*skb
,unsigned short len
) 1035 unsigned short ulen
; 1036 struct rtable
*rt
= (struct rtable
*)skb
->dst
; 1037 u32 saddr
= skb
->nh
.iph
->saddr
; 1038 u32 daddr
= skb
->nh
.iph
->daddr
; 1041 * First time through the loop.. Do all the setup stuff 1042 * (including finding out the socket we go to etc) 1051 ip_statistics
.IpInDelivers
++; 1054 * Validate the packet and the UDP length. 1057 ulen
=ntohs(uh
->len
); 1059 if(ulen
> len
|| len
<sizeof(*uh
) || ulen
<sizeof(*uh
)) { 1060 NETDEBUG(printk(KERN_DEBUG
"UDP: short packet: %d/%d\n", ulen
, len
)); 1061 udp_statistics
.UdpInErrors
++; 1062 kfree_skb(skb
, FREE_WRITE
); 1066 /* RFC1122 warning: According to 4.1.3.6, we MUST discard any */ 1067 /* datagram which has an invalid source address, either here or */ 1069 /* Right now, IP isn't doing it, and neither is UDP. It's on the */ 1070 /* FIXME list for IP, though, so I wouldn't worry about it. */ 1071 /* (That's the Right Place to do it, IMHO.) -- MS */ 1074 (((skb
->ip_summed
==CHECKSUM_HW
)&&udp_check(uh
,len
,saddr
,daddr
,skb
->csum
)) || 1075 ((skb
->ip_summed
==CHECKSUM_NONE
) && 1076 (udp_check(uh
,len
,saddr
,daddr
,csum_partial((char*)uh
, len
,0)))))) { 1077 /* <mea@utu.fi> wants to know, who sent it, to 1078 go and stomp on the garbage sender... */ 1080 /* RFC1122: OK. Discards the bad packet silently (as far as */ 1081 /* the network is concerned, anyway) as per 4.1.3.4 (MUST). */ 1083 NETDEBUG(printk(KERN_DEBUG
"UDP: bad checksum. From %08lX:%d to %08lX:%d ulen %d\n", 1084 ntohl(saddr
),ntohs(uh
->source
), 1085 ntohl(daddr
),ntohs(uh
->dest
), 1087 udp_statistics
.UdpInErrors
++; 1088 kfree_skb(skb
, FREE_WRITE
); 1097 * Trimming things wrongly. We must adjust the base/end to allow 1098 * for the headers we keep! 1104 if(rt
->rt_flags
& (RTF_BROADCAST
|RTF_MULTICAST
)) 1105 returnudp_v4_mcast_deliver(skb
, uh
, saddr
, daddr
); 1107 #ifdef CONFIG_IP_TRANSPARENT_PROXY 1108 if(IPCB(skb
)->redirport
) 1109 sk
=udp_v4_proxy_lookup(uh
->dest
, saddr
, uh
->source
, 1110 daddr
, skb
->dev
->pa_addr
, 1111 IPCB(skb
)->redirport
); 1114 sk
=udp_v4_lookup(saddr
, uh
->source
, daddr
, uh
->dest
); 1117 udp_statistics
.UdpNoPorts
++; 1118 icmp_send(skb
, ICMP_DEST_UNREACH
, ICMP_PORT_UNREACH
,0); 1121 * Hmm. We got an UDP broadcast to a port to which we 1122 * don't wanna listen. Ignore it. 1124 kfree_skb(skb
, FREE_WRITE
); 1127 udp_deliver(sk
, skb
); 1131 struct proto udp_prot
= { 1132 (struct sock
*)&udp_prot
,/* sklist_next */ 1133 (struct sock
*)&udp_prot
,/* sklist_prev */ 1134 udp_close
,/* close */ 1135 udp_connect
,/* connect */ 1137 NULL
,/* retransmit */ 1138 NULL
,/* write_wakeup */ 1139 NULL
,/* read_wakeup */ 1140 datagram_poll
,/* poll */ 1141 udp_ioctl
,/* ioctl */ 1145 ip_setsockopt
,/* setsockopt */ 1146 ip_getsockopt
,/* getsockopt */ 1147 udp_sendmsg
,/* sendmsg */ 1148 udp_recvmsg
,/* recvmsg */ 1150 udp_queue_rcv_skb
,/* backlog_rcv */ 1151 udp_v4_hash
,/* hash */ 1152 udp_v4_unhash
,/* unhash */ 1153 udp_v4_rehash
,/* rehash */ 1154 udp_good_socknum
,/* good_socknum */ 1155 udp_v4_verify_bind
,/* verify_bind */ 1156 128,/* max_header */