Import 2.1.34
[davej-history.git] / net / ipv4 / udp.c
blobde14fec113e82355bee0ec5e64c0b981980521ba
1 /*
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>
15 * Fixes:
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
29 * does NOT close.
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.
64 /* RFC1122 Status:
65 4.1.3.1 (Ports):
66 SHOULD send ICMP_PORT_UNREACHABLE in response to datagrams to
67 an un-listened port. (OK)
68 4.1.3.2 (IP Options)
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 )
87 950728 -- MS
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>
97 #include <linux/in.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>
106 #include <net/ip.h>
107 #include <net/protocol.h>
108 #include <net/tcp.h>
109 #include <linux/skbuff.h>
110 #include <net/sock.h>
111 #include <net/udp.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)
127 struct sock *sk2;
128 int retval =0, sk_reuse = sk->reuse;
130 SOCKHASH_LOCK();
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) {
137 if((!sk2_reuse) ||
138 (!sk_reuse) ||
139 (state == TCP_LISTEN)) {
140 retval =1;
141 break;
143 }else if(sk2->rcv_saddr == sk->rcv_saddr) {
144 if((!sk_reuse) ||
145 (!sk2_reuse) ||
146 (state == TCP_LISTEN)) {
147 retval =1;
148 break;
153 SOCKHASH_UNLOCK();
154 return retval;
157 staticinlineintudp_lport_inuse(int num)
159 struct sock *sk = udp_hash[num & (UDP_HTABLE_SIZE -1)];
161 for(; sk != NULL; sk = sk->next) {
162 if(sk->num == num)
163 return1;
165 return0;
168 /* Shared by v4/v6 tcp. */
169 unsigned shortudp_good_socknum(void)
171 static int start =0;
172 unsigned short base;
173 int i, best =0, size =32767;/* a big num. */
174 int result;
176 base = PROT_SOCK + (start &1023) +1;
178 SOCKHASH_LOCK();
179 for(i =0; i < UDP_HTABLE_SIZE; i++) {
180 struct sock *sk = udp_hash[i];
181 if(!sk) {
182 start = (i +1+ start) &1023;
183 result = i + base +1;
184 goto out;
185 }else{
186 int j =0;
188 if(++j >= size)
189 goto next;
190 }while((sk = sk->next));
191 best = i;
192 size = j;
194 next:
197 while(udp_lport_inuse(base + best +1))
198 best += UDP_HTABLE_SIZE;
199 result = (best + base +1);
200 out:
201 SOCKHASH_UNLOCK();
202 return result;
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)
212 struct sock **skp;
213 int num = sk->num;
215 num &= (UDP_HTABLE_SIZE -1);
216 skp = &udp_hash[num];
218 SOCKHASH_LOCK();
219 sk->next = *skp;
220 *skp = sk;
221 sk->hashent = num;
222 SOCKHASH_UNLOCK();
225 static voidudp_v4_unhash(struct sock *sk)
227 struct sock **skp;
228 int num = sk->num;
230 num &= (UDP_HTABLE_SIZE -1);
231 skp = &udp_hash[num];
233 SOCKHASH_LOCK();
234 while(*skp != NULL) {
235 if(*skp == sk) {
236 *skp = sk->next;
237 break;
239 skp = &((*skp)->next);
241 if(uh_cache_sk == sk)
242 uh_cache_sk = NULL;
243 SOCKHASH_UNLOCK();
246 static voidudp_v4_rehash(struct sock *sk)
248 struct sock **skp;
249 int num = sk->num;
250 int oldnum = sk->hashent;
252 num &= (UDP_HTABLE_SIZE -1);
253 skp = &udp_hash[oldnum];
255 SOCKHASH_LOCK();
256 while(*skp != NULL) {
257 if(*skp == sk) {
258 *skp = sk->next;
259 break;
261 skp = &((*skp)->next);
263 sk->next = udp_hash[num];
264 udp_hash[num] = sk;
265 sk->hashent = num;
266 if(uh_cache_sk == sk)
267 uh_cache_sk = NULL;
268 SOCKHASH_UNLOCK();
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);
278 int badness = -1;
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))) {
282 int score =0;
283 if(sk->rcv_saddr) {
284 if(sk->rcv_saddr != daddr)
285 continue;
286 score++;
288 if(sk->daddr) {
289 if(sk->daddr != saddr)
290 continue;
291 score++;
293 if(sk->dummy_th.dest) {
294 if(sk->dummy_th.dest != sport)
295 continue;
296 score++;
298 if(score ==3) {
299 result = sk;
300 break;
301 }else if(score > badness) {
302 result = sk;
303 badness = score;
307 return result;
310 __inline__ struct sock *udp_v4_lookup(u32 saddr, u16 sport, u32 daddr, u16 dport)
312 struct sock *sk;
314 if(uh_cache_sk &&
315 uh_cache_saddr == saddr &&
316 uh_cache_sport == sport &&
317 uh_cache_dport == dport &&
318 uh_cache_daddr == daddr)
319 return uh_cache_sk;
321 sk =udp_v4_lookup_longway(saddr, sport, daddr, dport);
322 uh_cache_sk = sk;
323 uh_cache_saddr = saddr;
324 uh_cache_daddr = daddr;
325 uh_cache_sport = sport;
326 uh_cache_dport = dport;
327 return sk;
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)]; \
334 else \
335 s1 = (sk); \
336 s1; \
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;
350 int badness = -1;
351 unsigned short hnum =ntohs(num);
352 unsigned short hpnum =ntohs(pnum);
353 int firstpass =1;
355 SOCKHASH_LOCK();
356 for(s =udp_v4_proxy_loop_init(hnum, hpnum, s, firstpass);
357 s != NULL;
358 s =udp_v4_proxy_loop_next(hnum, hpnum, s, firstpass)) {
359 if(s->num == hnum || s->num == hpnum) {
360 int score =0;
361 if(s->dead && (s->state == TCP_CLOSE))
362 continue;
363 if(s->rcv_saddr) {
364 if((s->num != hpnum || s->rcv_saddr != paddr) &&
365 (s->num != hnum || s->rcv_saddr != laddr))
366 continue;
367 score++;
369 if(s->daddr) {
370 if(s->daddr != raddr)
371 continue;
372 score++;
374 if(s->dummy_th.dest) {
375 if(s->dummy_th.dest != rnum)
376 continue;
377 score++;
379 if(score ==3&& s->num == hnum) {
380 result = s;
381 break;
382 }else if(score > badness && (s->num == hpnum || s->rcv_saddr)) {
383 result = s;
384 badness = score;
388 SOCKHASH_UNLOCK();
389 return result;
392 #undef secondlist
393 #undef udp_v4_proxy_loop_init
394 #undef udp_v4_proxy_loop_next
396 #endif
398 staticinlinestruct sock *udp_v4_mcast_next(struct sock *sk,
399 unsigned short num,
400 unsigned long raddr,
401 unsigned short rnum,
402 unsigned long laddr)
404 struct sock *s = 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))
412 continue;
413 break;
415 return s;
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;
437 struct sock *sk;
439 sk =udp_v4_lookup(iph->daddr, uh->dest, iph->saddr, uh->source);
440 if(sk == NULL)
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)
450 {/* Slow down! */
451 if(sk->cong_window >1)
452 sk->cong_window = sk->cong_window/2;
453 return;
456 if(type == ICMP_PARAMETERPROB)
458 sk->err = EPROTO;
459 sk->error_report(sk);
460 return;
463 if(type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
465 if(sk->ip_pmtudisc != IP_PMTUDISC_DONT) {
466 sk->err = EMSGSIZE;
467 sk->error_report(sk);
469 return;
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 */
481 /* 4.1.3.3. */
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)
491 return;
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));
503 struct udpfakehdr
505 struct udphdr uh;
506 u32 saddr;
507 u32 daddr;
508 u32 other;
509 struct iovec *iov;
510 int nriov;
511 u32 wcheck;
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;
524 struct iovec *iov;
525 char*src;
526 char*dst = to;
527 unsigned int len;
529 if(offset ==0) {
530 fraglen -=sizeof(struct udphdr);
531 dst +=sizeof(struct udphdr);
534 iov = ufh->iov;
536 if((len = iov->iov_len) > fraglen)
537 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,
541 ufh->wcheck);
542 if((iov->iov_len -= len) ==0) {
543 if(--(ufh->nriov) <0) {
544 printk(KERN_NOTICE "udp_getfrag: nriov = %d\n",
545 ufh->nriov);
546 return-EINVAL;
548 iov--;
550 fraglen -= len;
551 }while(fraglen);
552 ufh->iov = iov;
554 if(offset ==0) {
555 ufh->wcheck =csum_partial((char*)ufh,sizeof(struct udphdr),
556 ufh->wcheck);
557 ufh->uh.check =csum_tcpudp_magic(ufh->saddr, ufh->daddr,
558 ntohs(ufh->uh.len),
559 IPPROTO_UDP, ufh->wcheck);
560 if(ufh->uh.check ==0)
561 ufh->uh.check = -1;
562 memcpy(to, ufh,sizeof(struct udphdr));
564 return0;
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;
577 struct iovec *iov;
578 char*src;
579 char*dst = to;
580 int err;
581 unsigned int len;
583 if(offset ==0) {
584 fraglen -=sizeof(struct udphdr);
585 dst +=sizeof(struct udphdr);
588 iov = ufh->iov;
590 if((len = iov->iov_len) > fraglen)
591 len = fraglen;
592 src = (char*) iov->iov_base + iov->iov_len - len;
593 err =copy_from_user(dst + fraglen - len, src, len);
594 fraglen -= len;
595 if((iov->iov_len -= len) ==0) {
596 if(--(ufh->nriov) <0) {
597 printk(KERN_NOTICE "udp_getfrag: nriov = %d\n",
598 ufh->nriov);
599 return-EINVAL;
601 iov--;
603 }while(fraglen && err >=0);
604 ufh->iov = iov;
606 if(offset ==0)
607 memcpy(to, ufh,sizeof(struct udphdr));
608 return err;
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;
618 struct rtable *rt;
619 int free =0;
620 u32 daddr;
621 u8 tos;
622 int err;
624 if(len>65535)
625 return-EMSGSIZE;
628 * Check the flags.
631 if(msg->msg_flags&MSG_OOB)/* Mirror BSD error message compatibility */
632 return-EOPNOTSUPP;
634 if(msg->msg_flags&~(MSG_DONTROUTE|MSG_DONTWAIT))
635 return-EINVAL;
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))
644 return(-EINVAL);
645 if(usin->sin_family != AF_INET) {
646 static int complained;
647 if(!complained++)
648 printk(KERN_WARNING "%s forgot to set AF_INET in udp sendmsg. Fix it!\n", current->comm);
649 if(usin->sin_family)
650 return-EINVAL;
652 ufh.daddr = usin->sin_addr.s_addr;
653 ufh.uh.dest = usin->sin_port;
654 if(ufh.uh.dest ==0)
655 return-EINVAL;
656 }else{
657 if(sk->state != TCP_ESTABLISHED)
658 return-EINVAL;
659 ufh.daddr = sk->daddr;
660 ufh.uh.dest = sk->dummy_th.dest;
663 ipc.addr = sk->saddr;
664 ipc.opt = NULL;
665 if(msg->msg_controllen) {
666 err =ip_cmsg_send(msg, &ipc, &dev);
667 if(err)
668 return err;
669 if(ipc.opt)
670 free =1;
672 if(!ipc.opt)
673 ipc.opt = sk->opt;
675 ufh.saddr = ipc.addr;
676 ipc.addr = daddr = ufh.daddr;
678 if(ipc.opt && ipc.opt->srr) {
679 if(!daddr)
680 return-EINVAL;
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);
688 else
689 err =ip_route_output(&rt, daddr, ufh.saddr, tos, dev);
691 if(err) {
692 if(free)kfree(ipc.opt);
693 return err;
696 if(rt->rt_flags&RTF_BROADCAST && !sk->broadcast) {
697 if(free)kfree(ipc.opt);
698 ip_rt_put(rt);
699 return-EACCES;
702 ufh.saddr = rt->rt_src;
703 if(!ipc.addr)
704 ufh.daddr = ipc.addr = rt->rt_dst;
705 ufh.uh.source = sk->dummy_th.source;
706 ufh.uh.len =htons(ulen);
707 ufh.uh.check =0;
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;
711 ufh.wcheck =0;
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 */
718 lock_sock(sk);
719 if(sk->no_check)
720 err =ip_build_xmit(sk, udp_getfrag_nosum, &ufh, ulen,
721 &ipc, rt, msg->msg_flags);
722 else
723 err =ip_build_xmit(sk, udp_getfrag, &ufh, ulen,
724 &ipc, rt, msg->msg_flags);
725 ip_rt_put(rt);
726 release_sock(sk);
728 if(free)
729 kfree(ipc.opt);
730 if(!err) {
731 udp_statistics.UdpOutDatagrams++;
732 return len;
734 return err;
738 * IOCTL requests applicable to the UDP protocol
741 intudp_ioctl(struct sock *sk,int cmd,unsigned long arg)
743 switch(cmd)
745 case TIOCOUTQ:
747 unsigned long amount;
749 if(sk->state == TCP_LISTEN)return(-EINVAL);
750 amount =sock_wspace(sk);
751 returnput_user(amount, (int*)arg);
754 case TIOCINQ:
756 struct sk_buff *skb;
757 unsigned long amount;
759 if(sk->state == TCP_LISTEN)return(-EINVAL);
760 amount =0;
761 skb =skb_peek(&sk->receive_queue);
762 if(skb != NULL) {
764 * We will only return the amount
765 * of this packet since that is all
766 * that will be read.
768 amount = skb->len-sizeof(struct udphdr);
770 returnput_user(amount, (int*)arg);
773 default:
774 return(-EINVAL);
776 return(0);
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)
788 int copied =0;
789 int truesize;
790 struct sk_buff *skb;
791 int er;
792 struct sockaddr_in *sin=(struct sockaddr_in *)msg->msg_name;
795 * Check any passed addresses
798 if(addr_len)
799 *addr_len=sizeof(*sin);
801 if(sk->ip_recverr && (skb =skb_dequeue(&sk->error_queue)) != NULL) {
802 er =sock_error(sk);
803 if(msg->msg_controllen ==0) {
804 skb_free_datagram(sk, skb);
805 return er;
807 put_cmsg(msg, SOL_IP, IP_RECVERR, skb->len, skb->data);
808 skb_free_datagram(sk, skb);
809 return0;
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);
818 if(skb==NULL)
819 return er;
821 truesize = skb->len -sizeof(struct udphdr);
822 copied = truesize;
823 if(len < truesize)
825 msg->msg_flags |= MSG_TRUNC;
826 copied = len;
830 * FIXME : should use udp header size info value
833 er =skb_copy_datagram_iovec(skb,sizeof(struct udphdr),msg->msg_iov,copied);
834 if(er)
835 return er;
836 sk->stamp=skb->stamp;
838 /* Copy the address. */
839 if(sin)
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
845 if(flags&MSG_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;
860 #endif
862 if(sk->ip_cmsg_flags)
863 ip_cmsg_recv(msg, skb);
865 skb_free_datagram(sk, skb);
866 return(copied);
869 intudp_connect(struct sock *sk,struct sockaddr *uaddr,int addr_len)
871 struct sockaddr_in *usin = (struct sockaddr_in *) uaddr;
872 struct rtable *rt;
873 int err;
876 if(addr_len <sizeof(*usin))
877 return(-EINVAL);
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)
890 uh_cache_sk = NULL;
891 return0;
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);
899 if(err)
900 return err;
901 if((rt->rt_flags&RTF_BROADCAST) && !sk->broadcast) {
902 ip_rt_put(rt);
903 return-EACCES;
905 if(!sk->saddr)
906 sk->saddr = rt->rt_src;/* Update source address */
907 if(!sk->rcv_saddr)
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)
913 uh_cache_sk = NULL;
914 ip_rt_put(rt);
915 return(0);
919 static voidudp_close(struct sock *sk,unsigned long timeout)
921 lock_sock(sk);
922 sk->state = TCP_CLOSE;
923 if(uh_cache_sk == sk)
924 uh_cache_sk = NULL;
925 sk->dead =1;
926 release_sock(sk);
927 udp_v4_unhash(sk);
928 destroy_sock(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);
940 return(0);
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);
952 return-1;
954 udp_statistics.UdpInDatagrams++;
955 return0;
959 staticinlinevoidudp_deliver(struct sock *sk,struct sk_buff *skb)
961 if(sk->sock_readers) {
962 __skb_queue_tail(&sk->back_log, skb);
963 return;
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)
974 struct sock *sk;
975 int given =0;
977 SOCKHASH_LOCK();
978 sk = udp_hash[ntohs(uh->dest) & (UDP_HTABLE_SIZE -1)];
979 sk =udp_v4_mcast_next(sk, uh->dest, saddr, uh->source, daddr);
980 if(sk) {
981 struct sock *sknext = NULL;
984 struct sk_buff *skb1 = skb;
986 sknext =udp_v4_mcast_next(sk->next, uh->dest, saddr,
987 uh->source, daddr);
988 if(sknext)
989 skb1 =skb_clone(skb, GFP_ATOMIC);
991 if(skb1)
992 udp_deliver(sk, skb1);
993 sk = sknext;
994 }while(sknext);
995 given =1;
997 SOCKHASH_UNLOCK();
998 if(!given)
999 kfree_skb(skb, FREE_READ);
1000 return0;
1003 #ifdef CONFIG_IP_TRANSPARENT_PROXY
1005 * Check whether a received UDP packet might be for one of our
1006 * sockets.
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);
1013 struct sock *sk;
1015 sk =udp_v4_lookup(iph->saddr, uh->source, iph->daddr, uh->dest);
1016 if(!sk)
1017 return0;
1019 /* 0 means accept all LOCAL addresses here, not all the world... */
1020 if(sk->rcv_saddr ==0)
1021 return0;
1023 return1;
1025 #endif
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)
1033 struct sock *sk;
1034 struct udphdr *uh;
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)
1046 * Get the header.
1049 uh = skb->h.uh;
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);
1063 return(0);
1066 /* RFC1122 warning: According to 4.1.3.6, we MUST discard any */
1067 /* datagram which has an invalid source address, either here or */
1068 /* in IP. */
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 */
1073 if(uh->check &&
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),
1086 ulen));
1087 udp_statistics.UdpInErrors++;
1088 kfree_skb(skb, FREE_WRITE);
1089 return(0);
1093 len = ulen;
1096 * FIXME:
1097 * Trimming things wrongly. We must adjust the base/end to allow
1098 * for the headers we keep!
1099 * --ANK
1101 skb_trim(skb,len);
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);
1112 else
1113 #endif
1114 sk =udp_v4_lookup(saddr, uh->source, daddr, uh->dest);
1116 if(sk == NULL) {
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);
1125 return(0);
1127 udp_deliver(sk, skb);
1128 return0;
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 */
1136 NULL,/* accept */
1137 NULL,/* retransmit */
1138 NULL,/* write_wakeup */
1139 NULL,/* read_wakeup */
1140 datagram_poll,/* poll */
1141 udp_ioctl,/* ioctl */
1142 NULL,/* init */
1143 NULL,/* destroy */
1144 NULL,/* shutdown */
1145 ip_setsockopt,/* setsockopt */
1146 ip_getsockopt,/* getsockopt */
1147 udp_sendmsg,/* sendmsg */
1148 udp_recvmsg,/* recvmsg */
1149 NULL,/* bind */
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 */
1157 0,/* retransmits */
1158 "UDP",/* name */
1159 0,/* inuse */
1160 0/* highestinuse */
close