895def6c9c81a1b8662242ba2f755b02a7e17b5c
[linux-2.6.git] / net / ipv4 / udp.c
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.
5  *
6  *              The User Datagram Protocol (UDP).
7  *
8  * Version:     $Id: udp.c,v 1.102 2002/02/01 22:01:04 davem Exp $
9  *
10  * Authors:     Ross Biro
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>
15  *
16  * Fixes:
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
30  *                                      does NOT close.
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
59  *                                      for connect. 
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
66  *                                      datagrams.
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
73  *
74  *
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.
79  */
80  
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>
90 #include <linux/in.h>
91 #include <linux/errno.h>
92 #include <linux/timer.h>
93 #include <linux/mm.h>
94 #include <linux/config.h>
95 #include <linux/inet.h>
96 #include <linux/ipv6.h>
97 #include <linux/netdevice.h>
98 #include <net/snmp.h>
99 #include <net/ip.h>
100 #include <net/tcp_states.h>
101 #include <net/protocol.h>
102 #include <linux/skbuff.h>
103 #include <linux/proc_fs.h>
104 #include <linux/seq_file.h>
105 #include <net/sock.h>
106 #include <net/udp.h>
107 #include <net/icmp.h>
108 #include <net/route.h>
109 #include <net/inet_common.h>
110 #include <net/checksum.h>
111 #include <net/xfrm.h>
112
113 /*
114  *      Snmp MIB for the UDP layer
115  */
116
117 DEFINE_SNMP_STAT(struct udp_mib, udp_statistics) __read_mostly;
118
119 struct hlist_head udp_hash[UDP_HTABLE_SIZE];
120 DEFINE_RWLOCK(udp_hash_lock);
121
122 /* Shared by v4/v6 udp. */
123 int udp_port_rover;
124
125 static int udp_v4_get_port(struct sock *sk, unsigned short snum)
126 {
127         struct hlist_node *node;
128         struct sock *sk2;
129         struct inet_sock *inet = inet_sk(sk);
130
131         write_lock_bh(&udp_hash_lock);
132         if (snum == 0) {
133                 int best_size_so_far, best, result, i;
134
135                 if (udp_port_rover > sysctl_local_port_range[1] ||
136                     udp_port_rover < sysctl_local_port_range[0])
137                         udp_port_rover = sysctl_local_port_range[0];
138                 best_size_so_far = 32767;
139                 best = result = udp_port_rover;
140                 for (i = 0; i < UDP_HTABLE_SIZE; i++, result++) {
141                         struct hlist_head *list;
142                         int size;
143
144                         list = &udp_hash[result & (UDP_HTABLE_SIZE - 1)];
145                         if (hlist_empty(list)) {
146                                 if (result > sysctl_local_port_range[1])
147                                         result = sysctl_local_port_range[0] +
148                                                 ((result - sysctl_local_port_range[0]) &
149                                                  (UDP_HTABLE_SIZE - 1));
150                                 goto gotit;
151                         }
152                         size = 0;
153                         sk_for_each(sk2, node, list)
154                                 if (++size >= best_size_so_far)
155                                         goto next;
156                         best_size_so_far = size;
157                         best = result;
158                 next:;
159                 }
160                 result = best;
161                 for(i = 0; i < (1 << 16) / UDP_HTABLE_SIZE; i++, result += UDP_HTABLE_SIZE) {
162                         if (result > sysctl_local_port_range[1])
163                                 result = sysctl_local_port_range[0]
164                                         + ((result - sysctl_local_port_range[0]) &
165                                            (UDP_HTABLE_SIZE - 1));
166                         if (!udp_lport_inuse(result))
167                                 break;
168                 }
169                 if (i >= (1 << 16) / UDP_HTABLE_SIZE)
170                         goto fail;
171 gotit:
172                 udp_port_rover = snum = result;
173         } else {
174                 sk_for_each(sk2, node,
175                             &udp_hash[snum & (UDP_HTABLE_SIZE - 1)]) {
176                         struct inet_sock *inet2 = inet_sk(sk2);
177
178                         if (inet2->num == snum &&
179                             sk2 != sk && !ipv6_only_sock(sk2) &&
180                             (!sk2->sk_bound_dev_if ||
181                              !sk->sk_bound_dev_if ||
182                              sk2->sk_bound_dev_if == sk->sk_bound_dev_if) &&
183                             nx_addr_conflict(sk->sk_nx_info,
184                              inet_rcv_saddr(sk), sk2) &&
185                             (!sk2->sk_reuse || !sk->sk_reuse))
186                                 goto fail;
187                 }
188         }
189         inet->num = snum;
190         if (sk_unhashed(sk)) {
191                 struct hlist_head *h = &udp_hash[snum & (UDP_HTABLE_SIZE - 1)];
192
193                 sk_add_node(sk, h);
194                 sock_prot_inc_use(sk->sk_prot);
195         }
196         write_unlock_bh(&udp_hash_lock);
197         return 0;
198
199 fail:
200         write_unlock_bh(&udp_hash_lock);
201         return 1;
202 }
203
204 static void udp_v4_hash(struct sock *sk)
205 {
206         BUG();
207 }
208
209 static void udp_v4_unhash(struct sock *sk)
210 {
211         write_lock_bh(&udp_hash_lock);
212         if (sk_del_node_init(sk)) {
213                 inet_sk(sk)->num = 0;
214                 sock_prot_dec_use(sk->sk_prot);
215         }
216         write_unlock_bh(&udp_hash_lock);
217 }
218
219
220 /* UDP is nearly always wildcards out the wazoo, it makes no sense to try
221  * harder than this. -DaveM
222  */
223 static struct sock *udp_v4_lookup_longway(u32 saddr, u16 sport,
224                                           u32 daddr, u16 dport, int dif)
225 {
226         struct sock *sk, *result = NULL;
227         struct hlist_node *node;
228         unsigned short hnum = ntohs(dport);
229         int badness = -1;
230
231         sk_for_each(sk, node, &udp_hash[hnum & (UDP_HTABLE_SIZE - 1)]) {
232                 struct inet_sock *inet = inet_sk(sk);
233
234                 if (inet->num == hnum && !ipv6_only_sock(sk)) {
235                         int score = (sk->sk_family == PF_INET ? 1 : 0);
236                         if (inet->rcv_saddr) {
237                                 if (inet->rcv_saddr != daddr)
238                                         continue;
239                                 score+=2;
240                         } else if (sk->sk_nx_info) {
241                                 if (addr_in_nx_info(sk->sk_nx_info, daddr))
242                                         score+=2;
243                                 else
244                                         continue;
245                         }
246                         if (inet->daddr) {
247                                 if (inet->daddr != saddr)
248                                         continue;
249                                 score+=2;
250                         }
251                         if (inet->dport) {
252                                 if (inet->dport != sport)
253                                         continue;
254                                 score+=2;
255                         }
256                         if (sk->sk_bound_dev_if) {
257                                 if (sk->sk_bound_dev_if != dif)
258                                         continue;
259                                 score+=2;
260                         }
261                         if(score == 9) {
262                                 result = sk;
263                                 break;
264                         } else if(score > badness) {
265                                 result = sk;
266                                 badness = score;
267                         }
268                 }
269         }
270         return result;
271 }
272
273 static __inline__ struct sock *udp_v4_lookup(u32 saddr, u16 sport,
274                                              u32 daddr, u16 dport, int dif)
275 {
276         struct sock *sk;
277
278         read_lock(&udp_hash_lock);
279         sk = udp_v4_lookup_longway(saddr, sport, daddr, dport, dif);
280         if (sk)
281                 sock_hold(sk);
282         read_unlock(&udp_hash_lock);
283         return sk;
284 }
285
286 static inline struct sock *udp_v4_mcast_next(struct sock *sk,
287                                              u16 loc_port, u32 loc_addr,
288                                              u16 rmt_port, u32 rmt_addr,
289                                              int dif)
290 {
291         struct hlist_node *node;
292         struct sock *s = sk;
293         unsigned short hnum = ntohs(loc_port);
294
295         sk_for_each_from(s, node) {
296                 struct inet_sock *inet = inet_sk(s);
297
298                 if (inet->num != hnum                                   ||
299                     (inet->daddr && inet->daddr != rmt_addr)            ||
300                     (inet->dport != rmt_port && inet->dport)            ||
301                     (inet->rcv_saddr && inet->rcv_saddr != loc_addr &&
302                      inet->rcv_saddr2 && inet->rcv_saddr2 != loc_addr)  ||
303                     ipv6_only_sock(s)                                   ||
304                     (s->sk_bound_dev_if && s->sk_bound_dev_if != dif))
305                         continue;
306                 if (!ip_mc_sf_allow(s, loc_addr, rmt_addr, dif))
307                         continue;
308                 goto found;
309         }
310         s = NULL;
311 found:
312         return s;
313 }
314
315 /*
316  * This routine is called by the ICMP module when it gets some
317  * sort of error condition.  If err < 0 then the socket should
318  * be closed and the error returned to the user.  If err > 0
319  * it's just the icmp type << 8 | icmp code.  
320  * Header points to the ip header of the error packet. We move
321  * on past this. Then (as it used to claim before adjustment)
322  * header points to the first 8 bytes of the udp header.  We need
323  * to find the appropriate port.
324  */
325
326 void udp_err(struct sk_buff *skb, u32 info)
327 {
328         struct inet_sock *inet;
329         struct iphdr *iph = (struct iphdr*)skb->data;
330         struct udphdr *uh = (struct udphdr*)(skb->data+(iph->ihl<<2));
331         int type = skb->h.icmph->type;
332         int code = skb->h.icmph->code;
333         struct sock *sk;
334         int harderr;
335         int err;
336
337         sk = udp_v4_lookup(iph->daddr, uh->dest, iph->saddr, uh->source, skb->dev->ifindex);
338         if (sk == NULL) {
339                 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
340                 return; /* No socket for error */
341         }
342
343         err = 0;
344         harderr = 0;
345         inet = inet_sk(sk);
346
347         switch (type) {
348         default:
349         case ICMP_TIME_EXCEEDED:
350                 err = EHOSTUNREACH;
351                 break;
352         case ICMP_SOURCE_QUENCH:
353                 goto out;
354         case ICMP_PARAMETERPROB:
355                 err = EPROTO;
356                 harderr = 1;
357                 break;
358         case ICMP_DEST_UNREACH:
359                 if (code == ICMP_FRAG_NEEDED) { /* Path MTU discovery */
360                         if (inet->pmtudisc != IP_PMTUDISC_DONT) {
361                                 err = EMSGSIZE;
362                                 harderr = 1;
363                                 break;
364                         }
365                         goto out;
366                 }
367                 err = EHOSTUNREACH;
368                 if (code <= NR_ICMP_UNREACH) {
369                         harderr = icmp_err_convert[code].fatal;
370                         err = icmp_err_convert[code].errno;
371                 }
372                 break;
373         }
374
375         /*
376          *      RFC1122: OK.  Passes ICMP errors back to application, as per 
377          *      4.1.3.3.
378          */
379         if (!inet->recverr) {
380                 if (!harderr || sk->sk_state != TCP_ESTABLISHED)
381                         goto out;
382         } else {
383                 ip_icmp_error(sk, skb, err, uh->dest, info, (u8*)(uh+1));
384         }
385         sk->sk_err = err;
386         sk->sk_error_report(sk);
387 out:
388         sock_put(sk);
389 }
390
391 /*
392  * Throw away all pending data and cancel the corking. Socket is locked.
393  */
394 static void udp_flush_pending_frames(struct sock *sk)
395 {
396         struct udp_sock *up = udp_sk(sk);
397
398         if (up->pending) {
399                 up->len = 0;
400                 up->pending = 0;
401                 ip_flush_pending_frames(sk);
402         }
403 }
404
405 /*
406  * Push out all pending data as one UDP datagram. Socket is locked.
407  */
408 static int udp_push_pending_frames(struct sock *sk, struct udp_sock *up)
409 {
410         struct inet_sock *inet = inet_sk(sk);
411         struct flowi *fl = &inet->cork.fl;
412         struct sk_buff *skb;
413         struct udphdr *uh;
414         int err = 0;
415
416         /* Grab the skbuff where UDP header space exists. */
417         if ((skb = skb_peek(&sk->sk_write_queue)) == NULL)
418                 goto out;
419
420         /*
421          * Create a UDP header
422          */
423         uh = skb->h.uh;
424         uh->source = fl->fl_ip_sport;
425         uh->dest = fl->fl_ip_dport;
426         uh->len = htons(up->len);
427         uh->check = 0;
428
429         if (sk->sk_no_check == UDP_CSUM_NOXMIT) {
430                 skb->ip_summed = CHECKSUM_NONE;
431                 goto send;
432         }
433
434         if (skb_queue_len(&sk->sk_write_queue) == 1) {
435                 /*
436                  * Only one fragment on the socket.
437                  */
438                 if (skb->ip_summed == CHECKSUM_HW) {
439                         skb->csum = offsetof(struct udphdr, check);
440                         uh->check = ~csum_tcpudp_magic(fl->fl4_src, fl->fl4_dst,
441                                         up->len, IPPROTO_UDP, 0);
442                 } else {
443                         skb->csum = csum_partial((char *)uh,
444                                         sizeof(struct udphdr), skb->csum);
445                         uh->check = csum_tcpudp_magic(fl->fl4_src, fl->fl4_dst,
446                                         up->len, IPPROTO_UDP, skb->csum);
447                         if (uh->check == 0)
448                                 uh->check = -1;
449                 }
450         } else {
451                 unsigned int csum = 0;
452                 /*
453                  * HW-checksum won't work as there are two or more 
454                  * fragments on the socket so that all csums of sk_buffs
455                  * should be together.
456                  */
457                 if (skb->ip_summed == CHECKSUM_HW) {
458                         int offset = (unsigned char *)uh - skb->data;
459                         skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
460
461                         skb->ip_summed = CHECKSUM_NONE;
462                 } else {
463                         skb->csum = csum_partial((char *)uh,
464                                         sizeof(struct udphdr), skb->csum);
465                 }
466
467                 skb_queue_walk(&sk->sk_write_queue, skb) {
468                         csum = csum_add(csum, skb->csum);
469                 }
470                 uh->check = csum_tcpudp_magic(fl->fl4_src, fl->fl4_dst,
471                                 up->len, IPPROTO_UDP, csum);
472                 if (uh->check == 0)
473                         uh->check = -1;
474         }
475 send:
476         err = ip_push_pending_frames(sk);
477 out:
478         up->len = 0;
479         up->pending = 0;
480         return err;
481 }
482
483
484 static unsigned short udp_check(struct udphdr *uh, int len, unsigned long saddr, unsigned long daddr, unsigned long base)
485 {
486         return(csum_tcpudp_magic(saddr, daddr, len, IPPROTO_UDP, base));
487 }
488
489 int udp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
490                 size_t len)
491 {
492         struct inet_sock *inet = inet_sk(sk);
493         struct udp_sock *up = udp_sk(sk);
494         int ulen = len;
495         struct ipcm_cookie ipc;
496         struct rtable *rt = NULL;
497         int free = 0;
498         int connected = 0;
499         u32 daddr, faddr, saddr;
500         u16 dport;
501         u8  tos;
502         int err;
503         int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
504
505         if (len > 0xFFFF)
506                 return -EMSGSIZE;
507
508         /* 
509          *      Check the flags.
510          */
511
512         if (msg->msg_flags&MSG_OOB)     /* Mirror BSD error message compatibility */
513                 return -EOPNOTSUPP;
514
515         ipc.opt = NULL;
516
517         if (up->pending) {
518                 /*
519                  * There are pending frames.
520                  * The socket lock must be held while it's corked.
521                  */
522                 lock_sock(sk);
523                 if (likely(up->pending)) {
524                         if (unlikely(up->pending != AF_INET)) {
525                                 release_sock(sk);
526                                 return -EINVAL;
527                         }
528                         goto do_append_data;
529                 }
530                 release_sock(sk);
531         }
532         ulen += sizeof(struct udphdr);
533
534         /*
535          *      Get and verify the address. 
536          */
537         if (msg->msg_name) {
538                 struct sockaddr_in * usin = (struct sockaddr_in*)msg->msg_name;
539                 if (msg->msg_namelen < sizeof(*usin))
540                         return -EINVAL;
541                 if (usin->sin_family != AF_INET) {
542                         if (usin->sin_family != AF_UNSPEC)
543                                 return -EAFNOSUPPORT;
544                 }
545
546                 daddr = usin->sin_addr.s_addr;
547                 dport = usin->sin_port;
548                 if (dport == 0)
549                         return -EINVAL;
550         } else {
551                 if (sk->sk_state != TCP_ESTABLISHED)
552                         return -EDESTADDRREQ;
553                 daddr = inet->daddr;
554                 dport = inet->dport;
555                 /* Open fast path for connected socket.
556                    Route will not be used, if at least one option is set.
557                  */
558                 connected = 1;
559         }
560         ipc.addr = inet->saddr;
561
562         ipc.oif = sk->sk_bound_dev_if;
563         if (msg->msg_controllen) {
564                 err = ip_cmsg_send(msg, &ipc);
565                 if (err)
566                         return err;
567                 if (ipc.opt)
568                         free = 1;
569                 connected = 0;
570         }
571         if (!ipc.opt)
572                 ipc.opt = inet->opt;
573
574         saddr = ipc.addr;
575         ipc.addr = faddr = daddr;
576
577         if (ipc.opt && ipc.opt->srr) {
578                 if (!daddr)
579                         return -EINVAL;
580                 faddr = ipc.opt->faddr;
581                 connected = 0;
582         }
583         tos = RT_TOS(inet->tos);
584         if (sock_flag(sk, SOCK_LOCALROUTE) ||
585             (msg->msg_flags & MSG_DONTROUTE) || 
586             (ipc.opt && ipc.opt->is_strictroute)) {
587                 tos |= RTO_ONLINK;
588                 connected = 0;
589         }
590
591         if (MULTICAST(daddr)) {
592                 if (!ipc.oif)
593                         ipc.oif = inet->mc_index;
594                 if (!saddr)
595                         saddr = inet->mc_addr;
596                 connected = 0;
597         }
598
599         if (connected)
600                 rt = (struct rtable*)sk_dst_check(sk, 0);
601
602         if (rt == NULL) {
603                 struct flowi fl = { .oif = ipc.oif,
604                                     .nl_u = { .ip4_u =
605                                               { .daddr = faddr,
606                                                 .saddr = saddr,
607                                                 .tos = tos } },
608                                     .proto = IPPROTO_UDP,
609                                     .uli_u = { .ports =
610                                                { .sport = inet->sport,
611                                                  .dport = dport } } };
612                 struct nx_info *nxi = sk->sk_nx_info;
613
614                 if (nxi) {
615                         err = ip_find_src(nxi, &rt, &fl);
616                         if (err)
617                                 goto out;
618                         if (daddr == IPI_LOOPBACK && !vx_check(0, VX_ADMIN))
619                                 daddr = fl.fl4_dst = nxi->ipv4[0];
620 #ifdef CONFIG_VSERVER_REMAP_SADDR
621                         if (saddr == IPI_LOOPBACK && !vx_check(0, VX_ADMIN))
622                                 saddr = fl.fl4_src = nxi->ipv4[0];
623 #endif
624                 }
625                 err = ip_route_output_flow(&rt, &fl, sk, !(msg->msg_flags&MSG_DONTWAIT));
626                 if (err)
627                         goto out;
628
629                 err = -EACCES;
630                 if ((rt->rt_flags & RTCF_BROADCAST) &&
631                     !sock_flag(sk, SOCK_BROADCAST))
632                         goto out;
633                 if (connected)
634                         sk_dst_set(sk, dst_clone(&rt->u.dst));
635         }
636
637         if (msg->msg_flags&MSG_CONFIRM)
638                 goto do_confirm;
639 back_from_confirm:
640
641         saddr = rt->rt_src;
642         if (!ipc.addr)
643                 daddr = ipc.addr = rt->rt_dst;
644
645         lock_sock(sk);
646         if (unlikely(up->pending)) {
647                 /* The socket is already corked while preparing it. */
648                 /* ... which is an evident application bug. --ANK */
649                 release_sock(sk);
650
651                 LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 2\n");
652                 err = -EINVAL;
653                 goto out;
654         }
655         /*
656          *      Now cork the socket to pend data.
657          */
658         inet->cork.fl.fl4_dst = daddr;
659         inet->cork.fl.fl_ip_dport = dport;
660         inet->cork.fl.fl4_src = saddr;
661         inet->cork.fl.fl_ip_sport = inet->sport;
662         up->pending = AF_INET;
663
664 do_append_data:
665         up->len += ulen;
666         err = ip_append_data(sk, ip_generic_getfrag, msg->msg_iov, ulen, 
667                         sizeof(struct udphdr), &ipc, rt, 
668                         corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags);
669         if (err)
670                 udp_flush_pending_frames(sk);
671         else if (!corkreq)
672                 err = udp_push_pending_frames(sk, up);
673         release_sock(sk);
674
675 out:
676         ip_rt_put(rt);
677         if (free)
678                 kfree(ipc.opt);
679         if (!err) {
680                 UDP_INC_STATS_USER(UDP_MIB_OUTDATAGRAMS);
681                 return len;
682         }
683         return err;
684
685 do_confirm:
686         dst_confirm(&rt->u.dst);
687         if (!(msg->msg_flags&MSG_PROBE) || len)
688                 goto back_from_confirm;
689         err = 0;
690         goto out;
691 }
692
693 static int udp_sendpage(struct sock *sk, struct page *page, int offset,
694                         size_t size, int flags)
695 {
696         struct udp_sock *up = udp_sk(sk);
697         int ret;
698
699         if (!up->pending) {
700                 struct msghdr msg = {   .msg_flags = flags|MSG_MORE };
701
702                 /* Call udp_sendmsg to specify destination address which
703                  * sendpage interface can't pass.
704                  * This will succeed only when the socket is connected.
705                  */
706                 ret = udp_sendmsg(NULL, sk, &msg, 0);
707                 if (ret < 0)
708                         return ret;
709         }
710
711         lock_sock(sk);
712
713         if (unlikely(!up->pending)) {
714                 release_sock(sk);
715
716                 LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 3\n");
717                 return -EINVAL;
718         }
719
720         ret = ip_append_page(sk, page, offset, size, flags);
721         if (ret == -EOPNOTSUPP) {
722                 release_sock(sk);
723                 return sock_no_sendpage(sk->sk_socket, page, offset,
724                                         size, flags);
725         }
726         if (ret < 0) {
727                 udp_flush_pending_frames(sk);
728                 goto out;
729         }
730
731         up->len += size;
732         if (!(up->corkflag || (flags&MSG_MORE)))
733                 ret = udp_push_pending_frames(sk, up);
734         if (!ret)
735                 ret = size;
736 out:
737         release_sock(sk);
738         return ret;
739 }
740
741 /*
742  *      IOCTL requests applicable to the UDP protocol
743  */
744  
745 int udp_ioctl(struct sock *sk, int cmd, unsigned long arg)
746 {
747         switch(cmd) 
748         {
749                 case SIOCOUTQ:
750                 {
751                         int amount = atomic_read(&sk->sk_wmem_alloc);
752                         return put_user(amount, (int __user *)arg);
753                 }
754
755                 case SIOCINQ:
756                 {
757                         struct sk_buff *skb;
758                         unsigned long amount;
759
760                         amount = 0;
761                         spin_lock_bh(&sk->sk_receive_queue.lock);
762                         skb = skb_peek(&sk->sk_receive_queue);
763                         if (skb != NULL) {
764                                 /*
765                                  * We will only return the amount
766                                  * of this packet since that is all
767                                  * that will be read.
768                                  */
769                                 amount = skb->len - sizeof(struct udphdr);
770                         }
771                         spin_unlock_bh(&sk->sk_receive_queue.lock);
772                         return put_user(amount, (int __user *)arg);
773                 }
774
775                 default:
776                         return -ENOIOCTLCMD;
777         }
778         return(0);
779 }
780
781 static __inline__ int __udp_checksum_complete(struct sk_buff *skb)
782 {
783         return __skb_checksum_complete(skb);
784 }
785
786 static __inline__ int udp_checksum_complete(struct sk_buff *skb)
787 {
788         return skb->ip_summed != CHECKSUM_UNNECESSARY &&
789                 __udp_checksum_complete(skb);
790 }
791
792 /*
793  *      This should be easy, if there is something there we
794  *      return it, otherwise we block.
795  */
796
797 static int udp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
798                        size_t len, int noblock, int flags, int *addr_len)
799 {
800         struct inet_sock *inet = inet_sk(sk);
801         struct sockaddr_in *sin = (struct sockaddr_in *)msg->msg_name;
802         struct sk_buff *skb;
803         int copied, err;
804
805         /*
806          *      Check any passed addresses
807          */
808         if (addr_len)
809                 *addr_len=sizeof(*sin);
810
811         if (flags & MSG_ERRQUEUE)
812                 return ip_recv_error(sk, msg, len);
813
814 try_again:
815         skb = skb_recv_datagram(sk, flags, noblock, &err);
816         if (!skb)
817                 goto out;
818   
819         copied = skb->len - sizeof(struct udphdr);
820         if (copied > len) {
821                 copied = len;
822                 msg->msg_flags |= MSG_TRUNC;
823         }
824
825         if (skb->ip_summed==CHECKSUM_UNNECESSARY) {
826                 err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov,
827                                               copied);
828         } else if (msg->msg_flags&MSG_TRUNC) {
829                 if (__udp_checksum_complete(skb))
830                         goto csum_copy_err;
831                 err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov,
832                                               copied);
833         } else {
834                 err = skb_copy_and_csum_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov);
835
836                 if (err == -EINVAL)
837                         goto csum_copy_err;
838         }
839
840         if (err)
841                 goto out_free;
842
843         sock_recv_timestamp(msg, sk, skb);
844
845         /* Copy the address. */
846         if (sin)
847         {
848                 sin->sin_family = AF_INET;
849                 sin->sin_port = skb->h.uh->source;
850                 sin->sin_addr.s_addr = skb->nh.iph->saddr;
851                 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
852         }
853         if (inet->cmsg_flags)
854                 ip_cmsg_recv(msg, skb);
855
856         err = copied;
857         if (flags & MSG_TRUNC)
858                 err = skb->len - sizeof(struct udphdr);
859   
860 out_free:
861         skb_free_datagram(sk, skb);
862 out:
863         return err;
864
865 csum_copy_err:
866         UDP_INC_STATS_BH(UDP_MIB_INERRORS);
867
868         skb_kill_datagram(sk, skb, flags);
869
870         if (noblock)
871                 return -EAGAIN; 
872         goto try_again;
873 }
874
875
876 int udp_disconnect(struct sock *sk, int flags)
877 {
878         struct inet_sock *inet = inet_sk(sk);
879         /*
880          *      1003.1g - break association.
881          */
882          
883         sk->sk_state = TCP_CLOSE;
884         inet->daddr = 0;
885         inet->dport = 0;
886         sk->sk_bound_dev_if = 0;
887         if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK))
888                 inet_reset_saddr(sk);
889
890         if (!(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) {
891                 sk->sk_prot->unhash(sk);
892                 inet->sport = 0;
893         }
894         sk_dst_reset(sk);
895         return 0;
896 }
897
898 static void udp_close(struct sock *sk, long timeout)
899 {
900         sk_common_release(sk);
901 }
902
903 /* return:
904  *      1  if the the UDP system should process it
905  *      0  if we should drop this packet
906  *      -1 if it should get processed by xfrm4_rcv_encap
907  */
908 static int udp_encap_rcv(struct sock * sk, struct sk_buff *skb)
909 {
910 #ifndef CONFIG_XFRM
911         return 1; 
912 #else
913         struct udp_sock *up = udp_sk(sk);
914         struct udphdr *uh = skb->h.uh;
915         struct iphdr *iph;
916         int iphlen, len;
917   
918         __u8 *udpdata = (__u8 *)uh + sizeof(struct udphdr);
919         __u32 *udpdata32 = (__u32 *)udpdata;
920         __u16 encap_type = up->encap_type;
921
922         /* if we're overly short, let UDP handle it */
923         if (udpdata > skb->tail)
924                 return 1;
925
926         /* if this is not encapsulated socket, then just return now */
927         if (!encap_type)
928                 return 1;
929
930         len = skb->tail - udpdata;
931
932         switch (encap_type) {
933         default:
934         case UDP_ENCAP_ESPINUDP:
935                 /* Check if this is a keepalive packet.  If so, eat it. */
936                 if (len == 1 && udpdata[0] == 0xff) {
937                         return 0;
938                 } else if (len > sizeof(struct ip_esp_hdr) && udpdata32[0] != 0 ) {
939                         /* ESP Packet without Non-ESP header */
940                         len = sizeof(struct udphdr);
941                 } else
942                         /* Must be an IKE packet.. pass it through */
943                         return 1;
944                 break;
945         case UDP_ENCAP_ESPINUDP_NON_IKE:
946                 /* Check if this is a keepalive packet.  If so, eat it. */
947                 if (len == 1 && udpdata[0] == 0xff) {
948                         return 0;
949                 } else if (len > 2 * sizeof(u32) + sizeof(struct ip_esp_hdr) &&
950                            udpdata32[0] == 0 && udpdata32[1] == 0) {
951                         
952                         /* ESP Packet with Non-IKE marker */
953                         len = sizeof(struct udphdr) + 2 * sizeof(u32);
954                 } else
955                         /* Must be an IKE packet.. pass it through */
956                         return 1;
957                 break;
958         }
959
960         /* At this point we are sure that this is an ESPinUDP packet,
961          * so we need to remove 'len' bytes from the packet (the UDP
962          * header and optional ESP marker bytes) and then modify the
963          * protocol to ESP, and then call into the transform receiver.
964          */
965         if (skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
966                 return 0;
967
968         /* Now we can update and verify the packet length... */
969         iph = skb->nh.iph;
970         iphlen = iph->ihl << 2;
971         iph->tot_len = htons(ntohs(iph->tot_len) - len);
972         if (skb->len < iphlen + len) {
973                 /* packet is too small!?! */
974                 return 0;
975         }
976
977         /* pull the data buffer up to the ESP header and set the
978          * transport header to point to ESP.  Keep UDP on the stack
979          * for later.
980          */
981         skb->h.raw = skb_pull(skb, len);
982
983         /* modify the protocol (it's ESP!) */
984         iph->protocol = IPPROTO_ESP;
985
986         /* and let the caller know to send this into the ESP processor... */
987         return -1;
988 #endif
989 }
990
991 /* returns:
992  *  -1: error
993  *   0: success
994  *  >0: "udp encap" protocol resubmission
995  *
996  * Note that in the success and error cases, the skb is assumed to
997  * have either been requeued or freed.
998  */
999 static int udp_queue_rcv_skb(struct sock * sk, struct sk_buff *skb)
1000 {
1001         struct udp_sock *up = udp_sk(sk);
1002
1003         /*
1004          *      Charge it to the socket, dropping if the queue is full.
1005          */
1006         if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb)) {
1007                 kfree_skb(skb);
1008                 return -1;
1009         }
1010         nf_reset(skb);
1011
1012         if (up->encap_type) {
1013                 /*
1014                  * This is an encapsulation socket, so let's see if this is
1015                  * an encapsulated packet.
1016                  * If it's a keepalive packet, then just eat it.
1017                  * If it's an encapsulateed packet, then pass it to the
1018                  * IPsec xfrm input and return the response
1019                  * appropriately.  Otherwise, just fall through and
1020                  * pass this up the UDP socket.
1021                  */
1022                 int ret;
1023
1024                 ret = udp_encap_rcv(sk, skb);
1025                 if (ret == 0) {
1026                         /* Eat the packet .. */
1027                         kfree_skb(skb);
1028                         return 0;
1029                 }
1030                 if (ret < 0) {
1031                         /* process the ESP packet */
1032                         ret = xfrm4_rcv_encap(skb, up->encap_type);
1033                         UDP_INC_STATS_BH(UDP_MIB_INDATAGRAMS);
1034                         return -ret;
1035                 }
1036                 /* FALLTHROUGH -- it's a UDP Packet */
1037         }
1038
1039         if (sk->sk_filter && skb->ip_summed != CHECKSUM_UNNECESSARY) {
1040                 if (__udp_checksum_complete(skb)) {
1041                         UDP_INC_STATS_BH(UDP_MIB_INERRORS);
1042                         kfree_skb(skb);
1043                         return -1;
1044                 }
1045                 skb->ip_summed = CHECKSUM_UNNECESSARY;
1046         }
1047
1048         if (sock_queue_rcv_skb(sk,skb)<0) {
1049                 UDP_INC_STATS_BH(UDP_MIB_INERRORS);
1050                 kfree_skb(skb);
1051                 return -1;
1052         }
1053         UDP_INC_STATS_BH(UDP_MIB_INDATAGRAMS);
1054         return 0;
1055 }
1056
1057 /*
1058  *      Multicasts and broadcasts go to each listener.
1059  *
1060  *      Note: called only from the BH handler context,
1061  *      so we don't need to lock the hashes.
1062  */
1063 static int udp_v4_mcast_deliver(struct sk_buff *skb, struct udphdr *uh,
1064                                  u32 saddr, u32 daddr)
1065 {
1066         struct sock *sk;
1067         int dif;
1068
1069         read_lock(&udp_hash_lock);
1070         sk = sk_head(&udp_hash[ntohs(uh->dest) & (UDP_HTABLE_SIZE - 1)]);
1071         dif = skb->dev->ifindex;
1072         sk = udp_v4_mcast_next(sk, uh->dest, daddr, uh->source, saddr, dif);
1073         if (sk) {
1074                 struct sock *sknext = NULL;
1075
1076                 do {
1077                         struct sk_buff *skb1 = skb;
1078
1079                         sknext = udp_v4_mcast_next(sk_next(sk), uh->dest, daddr,
1080                                                    uh->source, saddr, dif);
1081                         if(sknext)
1082                                 skb1 = skb_clone(skb, GFP_ATOMIC);
1083
1084                         if(skb1) {
1085                                 int ret = udp_queue_rcv_skb(sk, skb1);
1086                                 if (ret > 0)
1087                                         /* we should probably re-process instead
1088                                          * of dropping packets here. */
1089                                         kfree_skb(skb1);
1090                         }
1091                         sk = sknext;
1092                 } while(sknext);
1093         } else
1094                 kfree_skb(skb);
1095         read_unlock(&udp_hash_lock);
1096         return 0;
1097 }
1098
1099 /* Initialize UDP checksum. If exited with zero value (success),
1100  * CHECKSUM_UNNECESSARY means, that no more checks are required.
1101  * Otherwise, csum completion requires chacksumming packet body,
1102  * including udp header and folding it to skb->csum.
1103  */
1104 static void udp_checksum_init(struct sk_buff *skb, struct udphdr *uh,
1105                              unsigned short ulen, u32 saddr, u32 daddr)
1106 {
1107         if (uh->check == 0) {
1108                 skb->ip_summed = CHECKSUM_UNNECESSARY;
1109         } else if (skb->ip_summed == CHECKSUM_HW) {
1110                 if (!udp_check(uh, ulen, saddr, daddr, skb->csum))
1111                         skb->ip_summed = CHECKSUM_UNNECESSARY;
1112         }
1113         if (skb->ip_summed != CHECKSUM_UNNECESSARY)
1114                 skb->csum = csum_tcpudp_nofold(saddr, daddr, ulen, IPPROTO_UDP, 0);
1115         /* Probably, we should checksum udp header (it should be in cache
1116          * in any case) and data in tiny packets (< rx copybreak).
1117          */
1118 }
1119
1120 /*
1121  *      All we need to do is get the socket, and then do a checksum. 
1122  */
1123  
1124 int udp_rcv(struct sk_buff *skb)
1125 {
1126         struct sock *sk;
1127         struct udphdr *uh;
1128         unsigned short ulen;
1129         struct rtable *rt = (struct rtable*)skb->dst;
1130         u32 saddr = skb->nh.iph->saddr;
1131         u32 daddr = skb->nh.iph->daddr;
1132         int len = skb->len;
1133
1134         /*
1135          *      Validate the packet and the UDP length.
1136          */
1137         if (!pskb_may_pull(skb, sizeof(struct udphdr)))
1138                 goto no_header;
1139
1140         uh = skb->h.uh;
1141
1142         ulen = ntohs(uh->len);
1143
1144         if (ulen > len || ulen < sizeof(*uh))
1145                 goto short_packet;
1146
1147         if (pskb_trim_rcsum(skb, ulen))
1148                 goto short_packet;
1149
1150         udp_checksum_init(skb, uh, ulen, saddr, daddr);
1151
1152         if(rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST))
1153                 return udp_v4_mcast_deliver(skb, uh, saddr, daddr);
1154
1155         sk = udp_v4_lookup(saddr, uh->source, daddr, uh->dest, skb->dev->ifindex);
1156
1157         if (sk != NULL) {
1158                 int ret = udp_queue_rcv_skb(sk, skb);
1159                 sock_put(sk);
1160
1161                 /* a return value > 0 means to resubmit the input, but
1162                  * it it wants the return to be -protocol, or 0
1163                  */
1164                 if (ret > 0)
1165                         return -ret;
1166                 return 0;
1167         }
1168
1169         if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
1170                 goto drop;
1171         nf_reset(skb);
1172
1173         /* No socket. Drop packet silently, if checksum is wrong */
1174         if (udp_checksum_complete(skb))
1175                 goto csum_error;
1176
1177         UDP_INC_STATS_BH(UDP_MIB_NOPORTS);
1178         icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
1179
1180         /*
1181          * Hmm.  We got an UDP packet to a port to which we
1182          * don't wanna listen.  Ignore it.
1183          */
1184         kfree_skb(skb);
1185         return(0);
1186
1187 short_packet:
1188         LIMIT_NETDEBUG(KERN_DEBUG "UDP: short packet: From %u.%u.%u.%u:%u %d/%d to %u.%u.%u.%u:%u\n",
1189                        NIPQUAD(saddr),
1190                        ntohs(uh->source),
1191                        ulen,
1192                        len,
1193                        NIPQUAD(daddr),
1194                        ntohs(uh->dest));
1195 no_header:
1196         UDP_INC_STATS_BH(UDP_MIB_INERRORS);
1197         kfree_skb(skb);
1198         return(0);
1199
1200 csum_error:
1201         /* 
1202          * RFC1122: OK.  Discards the bad packet silently (as far as 
1203          * the network is concerned, anyway) as per 4.1.3.4 (MUST). 
1204          */
1205         LIMIT_NETDEBUG(KERN_DEBUG "UDP: bad checksum. From %d.%d.%d.%d:%d to %d.%d.%d.%d:%d ulen %d\n",
1206                        NIPQUAD(saddr),
1207                        ntohs(uh->source),
1208                        NIPQUAD(daddr),
1209                        ntohs(uh->dest),
1210                        ulen);
1211 drop:
1212         UDP_INC_STATS_BH(UDP_MIB_INERRORS);
1213         kfree_skb(skb);
1214         return(0);
1215 }
1216
1217 static int udp_destroy_sock(struct sock *sk)
1218 {
1219         lock_sock(sk);
1220         udp_flush_pending_frames(sk);
1221         release_sock(sk);
1222         return 0;
1223 }
1224
1225 /*
1226  *      Socket option code for UDP
1227  */
1228 static int do_udp_setsockopt(struct sock *sk, int level, int optname,
1229                           char __user *optval, int optlen)
1230 {
1231         struct udp_sock *up = udp_sk(sk);
1232         int val;
1233         int err = 0;
1234
1235         if(optlen<sizeof(int))
1236                 return -EINVAL;
1237
1238         if (get_user(val, (int __user *)optval))
1239                 return -EFAULT;
1240
1241         switch(optname) {
1242         case UDP_CORK:
1243                 if (val != 0) {
1244                         up->corkflag = 1;
1245                 } else {
1246                         up->corkflag = 0;
1247                         lock_sock(sk);
1248                         udp_push_pending_frames(sk, up);
1249                         release_sock(sk);
1250                 }
1251                 break;
1252                 
1253         case UDP_ENCAP:
1254                 switch (val) {
1255                 case 0:
1256                 case UDP_ENCAP_ESPINUDP:
1257                 case UDP_ENCAP_ESPINUDP_NON_IKE:
1258                         up->encap_type = val;
1259                         break;
1260                 default:
1261                         err = -ENOPROTOOPT;
1262                         break;
1263                 }
1264                 break;
1265
1266         default:
1267                 err = -ENOPROTOOPT;
1268                 break;
1269         };
1270
1271         return err;
1272 }
1273
1274 static int udp_setsockopt(struct sock *sk, int level, int optname,
1275                           char __user *optval, int optlen)
1276 {
1277         if (level != SOL_UDP)
1278                 return ip_setsockopt(sk, level, optname, optval, optlen);
1279         return do_udp_setsockopt(sk, level, optname, optval, optlen);
1280 }
1281
1282 #ifdef CONFIG_COMPAT
1283 static int compat_udp_setsockopt(struct sock *sk, int level, int optname,
1284                                  char __user *optval, int optlen)
1285 {
1286         if (level != SOL_UDP)
1287                 return compat_ip_setsockopt(sk, level, optname, optval, optlen);
1288         return do_udp_setsockopt(sk, level, optname, optval, optlen);
1289 }
1290 #endif
1291
1292 static int do_udp_getsockopt(struct sock *sk, int level, int optname,
1293                           char __user *optval, int __user *optlen)
1294 {
1295         struct udp_sock *up = udp_sk(sk);
1296         int val, len;
1297
1298         if(get_user(len,optlen))
1299                 return -EFAULT;
1300
1301         len = min_t(unsigned int, len, sizeof(int));
1302         
1303         if(len < 0)
1304                 return -EINVAL;
1305
1306         switch(optname) {
1307         case UDP_CORK:
1308                 val = up->corkflag;
1309                 break;
1310
1311         case UDP_ENCAP:
1312                 val = up->encap_type;
1313                 break;
1314
1315         default:
1316                 return -ENOPROTOOPT;
1317         };
1318
1319         if(put_user(len, optlen))
1320                 return -EFAULT;
1321         if(copy_to_user(optval, &val,len))
1322                 return -EFAULT;
1323         return 0;
1324 }
1325
1326 static int udp_getsockopt(struct sock *sk, int level, int optname,
1327                           char __user *optval, int __user *optlen)
1328 {
1329         if (level != SOL_UDP)
1330                 return ip_getsockopt(sk, level, optname, optval, optlen);
1331         return do_udp_getsockopt(sk, level, optname, optval, optlen);
1332 }
1333
1334 #ifdef CONFIG_COMPAT
1335 static int compat_udp_getsockopt(struct sock *sk, int level, int optname,
1336                                  char __user *optval, int __user *optlen)
1337 {
1338         if (level != SOL_UDP)
1339                 return compat_ip_getsockopt(sk, level, optname, optval, optlen);
1340         return do_udp_getsockopt(sk, level, optname, optval, optlen);
1341 }
1342 #endif
1343 /**
1344  *      udp_poll - wait for a UDP event.
1345  *      @file - file struct
1346  *      @sock - socket
1347  *      @wait - poll table
1348  *
1349  *      This is same as datagram poll, except for the special case of 
1350  *      blocking sockets. If application is using a blocking fd
1351  *      and a packet with checksum error is in the queue;
1352  *      then it could get return from select indicating data available
1353  *      but then block when reading it. Add special case code
1354  *      to work around these arguably broken applications.
1355  */
1356 unsigned int udp_poll(struct file *file, struct socket *sock, poll_table *wait)
1357 {
1358         unsigned int mask = datagram_poll(file, sock, wait);
1359         struct sock *sk = sock->sk;
1360         
1361         /* Check for false positives due to checksum errors */
1362         if ( (mask & POLLRDNORM) &&
1363              !(file->f_flags & O_NONBLOCK) &&
1364              !(sk->sk_shutdown & RCV_SHUTDOWN)){
1365                 struct sk_buff_head *rcvq = &sk->sk_receive_queue;
1366                 struct sk_buff *skb;
1367
1368                 spin_lock_bh(&rcvq->lock);
1369                 while ((skb = skb_peek(rcvq)) != NULL) {
1370                         if (udp_checksum_complete(skb)) {
1371                                 UDP_INC_STATS_BH(UDP_MIB_INERRORS);
1372                                 __skb_unlink(skb, rcvq);
1373                                 kfree_skb(skb);
1374                         } else {
1375                                 skb->ip_summed = CHECKSUM_UNNECESSARY;
1376                                 break;
1377                         }
1378                 }
1379                 spin_unlock_bh(&rcvq->lock);
1380
1381                 /* nothing to see, move along */
1382                 if (skb == NULL)
1383                         mask &= ~(POLLIN | POLLRDNORM);
1384         }
1385
1386         return mask;
1387         
1388 }
1389
1390 struct proto udp_prot = {
1391         .name              = "UDP",
1392         .owner             = THIS_MODULE,
1393         .close             = udp_close,
1394         .connect           = ip4_datagram_connect,
1395         .disconnect        = udp_disconnect,
1396         .ioctl             = udp_ioctl,
1397         .destroy           = udp_destroy_sock,
1398         .setsockopt        = udp_setsockopt,
1399         .getsockopt        = udp_getsockopt,
1400         .sendmsg           = udp_sendmsg,
1401         .recvmsg           = udp_recvmsg,
1402         .sendpage          = udp_sendpage,
1403         .backlog_rcv       = udp_queue_rcv_skb,
1404         .hash              = udp_v4_hash,
1405         .unhash            = udp_v4_unhash,
1406         .get_port          = udp_v4_get_port,
1407         .obj_size          = sizeof(struct udp_sock),
1408 #ifdef CONFIG_COMPAT
1409         .compat_setsockopt = compat_udp_setsockopt,
1410         .compat_getsockopt = compat_udp_getsockopt,
1411 #endif
1412 };
1413
1414 /* ------------------------------------------------------------------------ */
1415 #ifdef CONFIG_PROC_FS
1416
1417 static struct sock *udp_get_first(struct seq_file *seq)
1418 {
1419         struct sock *sk;
1420         struct udp_iter_state *state = seq->private;
1421
1422         for (state->bucket = 0; state->bucket < UDP_HTABLE_SIZE; ++state->bucket) {
1423                 struct hlist_node *node;
1424
1425                 sk_for_each(sk, node, &udp_hash[state->bucket]) {
1426                         if (sk->sk_family == state->family &&
1427                                 vx_check(sk->sk_xid, VX_IDENT|VX_WATCH))
1428                                 goto found;
1429                 }
1430         }
1431         sk = NULL;
1432 found:
1433         return sk;
1434 }
1435
1436 static struct sock *udp_get_next(struct seq_file *seq, struct sock *sk)
1437 {
1438         struct udp_iter_state *state = seq->private;
1439
1440         do {
1441                 sk = sk_next(sk);
1442 try_again:
1443                 ;
1444         } while (sk && (sk->sk_family != state->family ||
1445                 !vx_check(sk->sk_xid, VX_IDENT|VX_WATCH)));
1446
1447         if (!sk && ++state->bucket < UDP_HTABLE_SIZE) {
1448                 sk = sk_head(&udp_hash[state->bucket]);
1449                 goto try_again;
1450         }
1451         return sk;
1452 }
1453
1454 static struct sock *udp_get_idx(struct seq_file *seq, loff_t pos)
1455 {
1456         struct sock *sk = udp_get_first(seq);
1457
1458         if (sk)
1459                 while(pos && (sk = udp_get_next(seq, sk)) != NULL)
1460                         --pos;
1461         return pos ? NULL : sk;
1462 }
1463
1464 static void *udp_seq_start(struct seq_file *seq, loff_t *pos)
1465 {
1466         read_lock(&udp_hash_lock);
1467         return *pos ? udp_get_idx(seq, *pos-1) : (void *)1;
1468 }
1469
1470 static void *udp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1471 {
1472         struct sock *sk;
1473
1474         if (v == (void *)1)
1475                 sk = udp_get_idx(seq, 0);
1476         else
1477                 sk = udp_get_next(seq, v);
1478
1479         ++*pos;
1480         return sk;
1481 }
1482
1483 static void udp_seq_stop(struct seq_file *seq, void *v)
1484 {
1485         read_unlock(&udp_hash_lock);
1486 }
1487
1488 static int udp_seq_open(struct inode *inode, struct file *file)
1489 {
1490         struct udp_seq_afinfo *afinfo = PDE(inode)->data;
1491         struct seq_file *seq;
1492         int rc = -ENOMEM;
1493         struct udp_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
1494
1495         if (!s)
1496                 goto out;
1497         memset(s, 0, sizeof(*s));
1498         s->family               = afinfo->family;
1499         s->seq_ops.start        = udp_seq_start;
1500         s->seq_ops.next         = udp_seq_next;
1501         s->seq_ops.show         = afinfo->seq_show;
1502         s->seq_ops.stop         = udp_seq_stop;
1503
1504         rc = seq_open(file, &s->seq_ops);
1505         if (rc)
1506                 goto out_kfree;
1507
1508         seq          = file->private_data;
1509         seq->private = s;
1510 out:
1511         return rc;
1512 out_kfree:
1513         kfree(s);
1514         goto out;
1515 }
1516
1517 /* ------------------------------------------------------------------------ */
1518 int udp_proc_register(struct udp_seq_afinfo *afinfo)
1519 {
1520         struct proc_dir_entry *p;
1521         int rc = 0;
1522
1523         if (!afinfo)
1524                 return -EINVAL;
1525         afinfo->seq_fops->owner         = afinfo->owner;
1526         afinfo->seq_fops->open          = udp_seq_open;
1527         afinfo->seq_fops->read          = seq_read;
1528         afinfo->seq_fops->llseek        = seq_lseek;
1529         afinfo->seq_fops->release       = seq_release_private;
1530
1531         p = proc_net_fops_create(afinfo->name, S_IRUGO, afinfo->seq_fops);
1532         if (p)
1533                 p->data = afinfo;
1534         else
1535                 rc = -ENOMEM;
1536         return rc;
1537 }
1538
1539 void udp_proc_unregister(struct udp_seq_afinfo *afinfo)
1540 {
1541         if (!afinfo)
1542                 return;
1543         proc_net_remove(afinfo->name);
1544         memset(afinfo->seq_fops, 0, sizeof(*afinfo->seq_fops));
1545 }
1546
1547 /* ------------------------------------------------------------------------ */
1548 static void udp4_format_sock(struct sock *sp, char *tmpbuf, int bucket)
1549 {
1550         struct inet_sock *inet = inet_sk(sp);
1551         unsigned int dest = inet->daddr;
1552         unsigned int src  = inet->rcv_saddr;
1553         __u16 destp       = ntohs(inet->dport);
1554         __u16 srcp        = ntohs(inet->sport);
1555
1556         sprintf(tmpbuf, "%4d: %08X:%04X %08X:%04X"
1557                 " %02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p",
1558                 bucket, src, srcp, dest, destp, sp->sk_state, 
1559                 atomic_read(&sp->sk_wmem_alloc),
1560                 atomic_read(&sp->sk_rmem_alloc),
1561                 0, 0L, 0, sock_i_uid(sp), 0, sock_i_ino(sp),
1562                 atomic_read(&sp->sk_refcnt), sp);
1563 }
1564
1565 static int udp4_seq_show(struct seq_file *seq, void *v)
1566 {
1567         if (v == SEQ_START_TOKEN)
1568                 seq_printf(seq, "%-127s\n",
1569                            "  sl  local_address rem_address   st tx_queue "
1570                            "rx_queue tr tm->when retrnsmt   uid  timeout "
1571                            "inode");
1572         else {
1573                 char tmpbuf[129];
1574                 struct udp_iter_state *state = seq->private;
1575
1576                 udp4_format_sock(v, tmpbuf, state->bucket);
1577                 seq_printf(seq, "%-127s\n", tmpbuf);
1578         }
1579         return 0;
1580 }
1581
1582 /* ------------------------------------------------------------------------ */
1583 static struct file_operations udp4_seq_fops;
1584 static struct udp_seq_afinfo udp4_seq_afinfo = {
1585         .owner          = THIS_MODULE,
1586         .name           = "udp",
1587         .family         = AF_INET,
1588         .seq_show       = udp4_seq_show,
1589         .seq_fops       = &udp4_seq_fops,
1590 };
1591
1592 int __init udp4_proc_init(void)
1593 {
1594         return udp_proc_register(&udp4_seq_afinfo);
1595 }
1596
1597 void udp4_proc_exit(void)
1598 {
1599         udp_proc_unregister(&udp4_seq_afinfo);
1600 }
1601 #endif /* CONFIG_PROC_FS */
1602
1603 EXPORT_SYMBOL(udp_disconnect);
1604 EXPORT_SYMBOL(udp_hash);
1605 EXPORT_SYMBOL(udp_hash_lock);
1606 EXPORT_SYMBOL(udp_ioctl);
1607 EXPORT_SYMBOL(udp_port_rover);
1608 EXPORT_SYMBOL(udp_prot);
1609 EXPORT_SYMBOL(udp_sendmsg);
1610 EXPORT_SYMBOL(udp_poll);
1611
1612 #ifdef CONFIG_PROC_FS
1613 EXPORT_SYMBOL(udp_proc_register);
1614 EXPORT_SYMBOL(udp_proc_unregister);
1615 #endif