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