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