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