VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / net / ipv6 / udp.c
1 /*
2  *      UDP over IPv6
3  *      Linux INET6 implementation 
4  *
5  *      Authors:
6  *      Pedro Roque             <roque@di.fc.ul.pt>     
7  *
8  *      Based on linux/ipv4/udp.c
9  *
10  *      $Id: udp.c,v 1.65 2002/02/01 22:01:04 davem Exp $
11  *
12  *      Fixes:
13  *      Hideaki YOSHIFUJI       :       sin6_scope_id support
14  *      YOSHIFUJI Hideaki @USAGI and:   Support IPV6_V6ONLY socket option, which
15  *      Alexey Kuznetsov                allow both IPv4 and IPv6 sockets to bind
16  *                                      a single port at the same time.
17  *      Kazunori MIYAZAWA @USAGI:       change process style to use ip6_append_data
18  *      YOSHIFUJI Hideaki @USAGI:       convert /proc/net/udp6 to seq_file.
19  *
20  *      This program is free software; you can redistribute it and/or
21  *      modify it under the terms of the GNU General Public License
22  *      as published by the Free Software Foundation; either version
23  *      2 of the License, or (at your option) any later version.
24  */
25
26 #include <linux/config.h>
27 #include <linux/errno.h>
28 #include <linux/types.h>
29 #include <linux/socket.h>
30 #include <linux/sockios.h>
31 #include <linux/sched.h>
32 #include <linux/net.h>
33 #include <linux/in6.h>
34 #include <linux/netdevice.h>
35 #include <linux/if_arp.h>
36 #include <linux/ipv6.h>
37 #include <linux/icmpv6.h>
38 #include <linux/init.h>
39 #include <asm/uaccess.h>
40
41 #include <net/sock.h>
42 #include <net/snmp.h>
43
44 #include <net/ipv6.h>
45 #include <net/ndisc.h>
46 #include <net/protocol.h>
47 #include <net/transp_v6.h>
48 #include <net/ip6_route.h>
49 #include <net/addrconf.h>
50 #include <net/ip.h>
51 #include <net/udp.h>
52 #include <net/raw.h>
53 #include <net/inet_common.h>
54
55 #include <net/ip6_checksum.h>
56 #include <net/xfrm.h>
57
58 #include <linux/proc_fs.h>
59 #include <linux/seq_file.h>
60
61 DEFINE_SNMP_STAT(struct udp_mib, udp_stats_in6);
62
63 /* Grrr, addr_type already calculated by caller, but I don't want
64  * to add some silly "cookie" argument to this method just for that.
65  */
66 static int udp_v6_get_port(struct sock *sk, unsigned short snum)
67 {
68         struct sock *sk2;
69         struct hlist_node *node;
70
71         write_lock_bh(&udp_hash_lock);
72         if (snum == 0) {
73                 int best_size_so_far, best, result, i;
74
75                 if (udp_port_rover > sysctl_local_port_range[1] ||
76                     udp_port_rover < sysctl_local_port_range[0])
77                         udp_port_rover = sysctl_local_port_range[0];
78                 best_size_so_far = 32767;
79                 best = result = udp_port_rover;
80                 for (i = 0; i < UDP_HTABLE_SIZE; i++, result++) {
81                         int size;
82                         struct hlist_head *list;
83
84                         list = &udp_hash[result & (UDP_HTABLE_SIZE - 1)];
85                         if (hlist_empty(list)) {
86                                 if (result > sysctl_local_port_range[1])
87                                         result = sysctl_local_port_range[0] +
88                                                 ((result - sysctl_local_port_range[0]) &
89                                                  (UDP_HTABLE_SIZE - 1));
90                                 goto gotit;
91                         }
92                         size = 0;
93                         sk_for_each(sk2, node, list)
94                                 if (++size >= best_size_so_far)
95                                         goto next;
96                         best_size_so_far = size;
97                         best = result;
98                 next:;
99                 }
100                 result = best;
101                 for(;; result += UDP_HTABLE_SIZE) {
102                         if (result > sysctl_local_port_range[1])
103                                 result = sysctl_local_port_range[0]
104                                         + ((result - sysctl_local_port_range[0]) &
105                                            (UDP_HTABLE_SIZE - 1));
106                         if (!udp_lport_inuse(result))
107                                 break;
108                 }
109 gotit:
110                 udp_port_rover = snum = result;
111         } else {
112                 sk_for_each(sk2, node,
113                             &udp_hash[snum & (UDP_HTABLE_SIZE - 1)]) {
114                         if (inet_sk(sk2)->num == snum &&
115                             sk2 != sk &&
116                             (!sk2->sk_bound_dev_if ||
117                              !sk->sk_bound_dev_if ||
118                              sk2->sk_bound_dev_if == sk->sk_bound_dev_if) &&
119                             (!sk2->sk_reuse || !sk->sk_reuse) &&
120                             ipv6_rcv_saddr_equal(sk, sk2))
121                                 goto fail;
122                 }
123         }
124
125         inet_sk(sk)->num = snum;
126         if (sk_unhashed(sk)) {
127                 sk_add_node(sk, &udp_hash[snum & (UDP_HTABLE_SIZE - 1)]);
128                 sock_prot_inc_use(sk->sk_prot);
129         }
130         write_unlock_bh(&udp_hash_lock);
131         return 0;
132
133 fail:
134         write_unlock_bh(&udp_hash_lock);
135         return 1;
136 }
137
138 static void udp_v6_hash(struct sock *sk)
139 {
140         BUG();
141 }
142
143 static void udp_v6_unhash(struct sock *sk)
144 {
145         write_lock_bh(&udp_hash_lock);
146         if (sk_del_node_init(sk)) {
147                 inet_sk(sk)->num = 0;
148                 sock_prot_dec_use(sk->sk_prot);
149         }
150         write_unlock_bh(&udp_hash_lock);
151 }
152
153 static struct sock *udp_v6_lookup(struct in6_addr *saddr, u16 sport,
154                                   struct in6_addr *daddr, u16 dport, int dif)
155 {
156         struct sock *sk, *result = NULL;
157         struct hlist_node *node;
158         unsigned short hnum = ntohs(dport);
159         int badness = -1;
160
161         read_lock(&udp_hash_lock);
162         sk_for_each(sk, node, &udp_hash[hnum & (UDP_HTABLE_SIZE - 1)]) {
163                 struct inet_opt *inet = inet_sk(sk);
164
165                 if (inet->num == hnum && sk->sk_family == PF_INET6) {
166                         struct ipv6_pinfo *np = inet6_sk(sk);
167                         int score = 0;
168                         if (inet->dport) {
169                                 if (inet->dport != sport)
170                                         continue;
171                                 score++;
172                         }
173                         if (!ipv6_addr_any(&np->rcv_saddr)) {
174                                 if (ipv6_addr_cmp(&np->rcv_saddr, daddr))
175                                         continue;
176                                 score++;
177                         }
178                         if (!ipv6_addr_any(&np->daddr)) {
179                                 if (ipv6_addr_cmp(&np->daddr, saddr))
180                                         continue;
181                                 score++;
182                         }
183                         if (sk->sk_bound_dev_if) {
184                                 if (sk->sk_bound_dev_if != dif)
185                                         continue;
186                                 score++;
187                         }
188                         if(score == 4) {
189                                 result = sk;
190                                 break;
191                         } else if(score > badness) {
192                                 result = sk;
193                                 badness = score;
194                         }
195                 }
196         }
197         if (result)
198                 sock_hold(result);
199         read_unlock(&udp_hash_lock);
200         return result;
201 }
202
203 /*
204  *
205  */
206
207 static void udpv6_close(struct sock *sk, long timeout)
208 {
209         sk_common_release(sk);
210 }
211
212 /*
213  *      This should be easy, if there is something there we
214  *      return it, otherwise we block.
215  */
216
217 static int udpv6_recvmsg(struct kiocb *iocb, struct sock *sk, 
218                   struct msghdr *msg, size_t len,
219                   int noblock, int flags, int *addr_len)
220 {
221         struct ipv6_pinfo *np = inet6_sk(sk);
222         struct sk_buff *skb;
223         size_t copied;
224         int err;
225
226         if (addr_len)
227                 *addr_len=sizeof(struct sockaddr_in6);
228   
229         if (flags & MSG_ERRQUEUE)
230                 return ipv6_recv_error(sk, msg, len);
231
232 try_again:
233         skb = skb_recv_datagram(sk, flags, noblock, &err);
234         if (!skb)
235                 goto out;
236
237         copied = skb->len - sizeof(struct udphdr);
238         if (copied > len) {
239                 copied = len;
240                 msg->msg_flags |= MSG_TRUNC;
241         }
242
243         if (skb->ip_summed==CHECKSUM_UNNECESSARY) {
244                 err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov,
245                                               copied);
246         } else if (msg->msg_flags&MSG_TRUNC) {
247                 if ((unsigned short)csum_fold(skb_checksum(skb, 0, skb->len, skb->csum)))
248                         goto csum_copy_err;
249                 err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov,
250                                               copied);
251         } else {
252                 err = skb_copy_and_csum_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov);
253                 if (err == -EINVAL)
254                         goto csum_copy_err;
255         }
256         if (err)
257                 goto out_free;
258
259         sock_recv_timestamp(msg, sk, skb);
260
261         /* Copy the address. */
262         if (msg->msg_name) {
263                 struct sockaddr_in6 *sin6;
264           
265                 sin6 = (struct sockaddr_in6 *) msg->msg_name;
266                 sin6->sin6_family = AF_INET6;
267                 sin6->sin6_port = skb->h.uh->source;
268                 sin6->sin6_flowinfo = 0;
269                 sin6->sin6_scope_id = 0;
270
271                 if (skb->protocol == htons(ETH_P_IP)) {
272                         struct inet_opt *inet = inet_sk(sk);
273
274                         ipv6_addr_set(&sin6->sin6_addr, 0, 0,
275                                       htonl(0xffff), skb->nh.iph->saddr);
276                         if (inet->cmsg_flags)
277                                 ip_cmsg_recv(msg, skb);
278                 } else {
279                         ipv6_addr_copy(&sin6->sin6_addr, &skb->nh.ipv6h->saddr);
280
281                         if (np->rxopt.all)
282                                 datagram_recv_ctl(sk, msg, skb);
283                         if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
284                                 sin6->sin6_scope_id = IP6CB(skb)->iif;
285                 }
286         }
287
288         err = copied;
289         if (flags & MSG_TRUNC)
290                 err = skb->len - sizeof(struct udphdr);
291
292 out_free:
293         skb_free_datagram(sk, skb);
294 out:
295         return err;
296
297 csum_copy_err:
298         /* Clear queue. */
299         if (flags&MSG_PEEK) {
300                 int clear = 0;
301                 spin_lock_irq(&sk->sk_receive_queue.lock);
302                 if (skb == skb_peek(&sk->sk_receive_queue)) {
303                         __skb_unlink(skb, &sk->sk_receive_queue);
304                         clear = 1;
305                 }
306                 spin_unlock_irq(&sk->sk_receive_queue.lock);
307                 if (clear)
308                         kfree_skb(skb);
309         }
310
311         skb_free_datagram(sk, skb);
312
313         if (flags & MSG_DONTWAIT) {
314                 UDP6_INC_STATS_USER(UDP_MIB_INERRORS);
315                 return -EAGAIN;
316         }
317         goto try_again;
318 }
319
320 static void udpv6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
321                int type, int code, int offset, __u32 info)
322 {
323         struct ipv6_pinfo *np;
324         struct ipv6hdr *hdr = (struct ipv6hdr*)skb->data;
325         struct net_device *dev = skb->dev;
326         struct in6_addr *saddr = &hdr->saddr;
327         struct in6_addr *daddr = &hdr->daddr;
328         struct udphdr *uh = (struct udphdr*)(skb->data+offset);
329         struct sock *sk;
330         int err;
331
332         sk = udp_v6_lookup(daddr, uh->dest, saddr, uh->source, dev->ifindex);
333    
334         if (sk == NULL)
335                 return;
336
337         np = inet6_sk(sk);
338
339         if (!icmpv6_err_convert(type, code, &err) && !np->recverr)
340                 goto out;
341
342         if (sk->sk_state != TCP_ESTABLISHED && !np->recverr)
343                 goto out;
344
345         if (np->recverr)
346                 ipv6_icmp_error(sk, skb, err, uh->dest, ntohl(info), (u8 *)(uh+1));
347
348         sk->sk_err = err;
349         sk->sk_error_report(sk);
350 out:
351         sock_put(sk);
352 }
353
354 static inline int udpv6_queue_rcv_skb(struct sock * sk, struct sk_buff *skb)
355 {
356         if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb)) {
357                 kfree_skb(skb);
358                 return -1;
359         }
360
361         if (skb->ip_summed != CHECKSUM_UNNECESSARY) {
362                 if ((unsigned short)csum_fold(skb_checksum(skb, 0, skb->len, skb->csum))) {
363                         UDP6_INC_STATS_BH(UDP_MIB_INERRORS);
364                         kfree_skb(skb);
365                         return 0;
366                 }
367                 skb->ip_summed = CHECKSUM_UNNECESSARY;
368         }
369
370         if (sock_queue_rcv_skb(sk,skb)<0) {
371                 UDP6_INC_STATS_BH(UDP_MIB_INERRORS);
372                 kfree_skb(skb);
373                 return 0;
374         }
375         UDP6_INC_STATS_BH(UDP_MIB_INDATAGRAMS);
376         return 0;
377 }
378
379 static struct sock *udp_v6_mcast_next(struct sock *sk,
380                                       u16 loc_port, struct in6_addr *loc_addr,
381                                       u16 rmt_port, struct in6_addr *rmt_addr,
382                                       int dif)
383 {
384         struct hlist_node *node;
385         struct sock *s = sk;
386         unsigned short num = ntohs(loc_port);
387
388         sk_for_each_from(s, node) {
389                 struct inet_opt *inet = inet_sk(s);
390
391                 if (inet->num == num && s->sk_family == PF_INET6) {
392                         struct ipv6_pinfo *np = inet6_sk(s);
393                         if (inet->dport) {
394                                 if (inet->dport != rmt_port)
395                                         continue;
396                         }
397                         if (!ipv6_addr_any(&np->daddr) &&
398                             ipv6_addr_cmp(&np->daddr, rmt_addr))
399                                 continue;
400
401                         if (s->sk_bound_dev_if && s->sk_bound_dev_if != dif)
402                                 continue;
403
404                         if (!ipv6_addr_any(&np->rcv_saddr)) {
405                                 if (!ipv6_addr_cmp(&np->rcv_saddr, loc_addr))
406                                         return s;
407                                 continue;
408                         }
409                         if(!inet6_mc_check(s, loc_addr, rmt_addr))
410                                 continue;
411                         return s;
412                 }
413         }
414         return NULL;
415 }
416
417 /*
418  * Note: called only from the BH handler context,
419  * so we don't need to lock the hashes.
420  */
421 static void udpv6_mcast_deliver(struct udphdr *uh,
422                                 struct in6_addr *saddr, struct in6_addr *daddr,
423                                 struct sk_buff *skb)
424 {
425         struct sock *sk, *sk2;
426         int dif;
427
428         read_lock(&udp_hash_lock);
429         sk = sk_head(&udp_hash[ntohs(uh->dest) & (UDP_HTABLE_SIZE - 1)]);
430         dif = skb->dev->ifindex;
431         sk = udp_v6_mcast_next(sk, uh->dest, daddr, uh->source, saddr, dif);
432         if (!sk) {
433                 kfree_skb(skb);
434                 goto out;
435         }
436
437         sk2 = sk;
438         while ((sk2 = udp_v6_mcast_next(sk_next(sk2), uh->dest, daddr,
439                                         uh->source, saddr, dif))) {
440                 struct sk_buff *buff = skb_clone(skb, GFP_ATOMIC);
441                 if (buff)
442                         udpv6_queue_rcv_skb(sk2, buff);
443         }
444         udpv6_queue_rcv_skb(sk, skb);
445 out:
446         read_unlock(&udp_hash_lock);
447 }
448
449 static int udpv6_rcv(struct sk_buff **pskb, unsigned int *nhoffp)
450 {
451         struct sk_buff *skb = *pskb;
452         struct sock *sk;
453         struct udphdr *uh;
454         struct net_device *dev = skb->dev;
455         struct in6_addr *saddr, *daddr;
456         u32 ulen = 0;
457
458         if (!pskb_may_pull(skb, sizeof(struct udphdr)))
459                 goto short_packet;
460
461         saddr = &skb->nh.ipv6h->saddr;
462         daddr = &skb->nh.ipv6h->daddr;
463         uh = skb->h.uh;
464
465         ulen = ntohs(uh->len);
466
467         /* Check for jumbo payload */
468         if (ulen == 0)
469                 ulen = skb->len;
470
471         if (ulen > skb->len || ulen < sizeof(*uh))
472                 goto short_packet;
473
474         if (uh->check == 0) {
475                 /* RFC 2460 section 8.1 says that we SHOULD log
476                    this error. Well, it is reasonable.
477                  */
478                 LIMIT_NETDEBUG(
479                         printk(KERN_INFO "IPv6: udp checksum is 0\n"));
480                 goto discard;
481         }
482
483         if (ulen < skb->len) {
484                 if (__pskb_trim(skb, ulen))
485                         goto discard;
486                 saddr = &skb->nh.ipv6h->saddr;
487                 daddr = &skb->nh.ipv6h->daddr;
488                 uh = skb->h.uh;
489         }
490
491         if (skb->ip_summed==CHECKSUM_HW) {
492                 skb->ip_summed = CHECKSUM_UNNECESSARY;
493                 if (csum_ipv6_magic(saddr, daddr, ulen, IPPROTO_UDP, skb->csum)) {
494                         LIMIT_NETDEBUG(printk(KERN_DEBUG "udp v6 hw csum failure.\n"));
495                         skb->ip_summed = CHECKSUM_NONE;
496                 }
497         }
498         if (skb->ip_summed != CHECKSUM_UNNECESSARY)
499                 skb->csum = ~csum_ipv6_magic(saddr, daddr, ulen, IPPROTO_UDP, 0);
500
501         /* 
502          *      Multicast receive code 
503          */
504         if (ipv6_addr_is_multicast(daddr)) {
505                 udpv6_mcast_deliver(uh, saddr, daddr, skb);
506                 return 0;
507         }
508
509         /* Unicast */
510
511         /* 
512          * check socket cache ... must talk to Alan about his plans
513          * for sock caches... i'll skip this for now.
514          */
515         sk = udp_v6_lookup(saddr, uh->source, daddr, uh->dest, dev->ifindex);
516
517         if (sk == NULL) {
518                 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
519                         goto discard;
520
521                 if (skb->ip_summed != CHECKSUM_UNNECESSARY &&
522                     (unsigned short)csum_fold(skb_checksum(skb, 0, skb->len, skb->csum)))
523                         goto discard;
524                 UDP6_INC_STATS_BH(UDP_MIB_NOPORTS);
525
526                 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0, dev);
527
528                 kfree_skb(skb);
529                 return(0);
530         }
531         
532         /* deliver */
533         
534         udpv6_queue_rcv_skb(sk, skb);
535         sock_put(sk);
536         return(0);
537
538 short_packet:   
539         if (net_ratelimit())
540                 printk(KERN_DEBUG "UDP: short packet: %d/%u\n", ulen, skb->len);
541
542 discard:
543         UDP6_INC_STATS_BH(UDP_MIB_INERRORS);
544         kfree_skb(skb);
545         return(0);      
546 }
547 /*
548  * Throw away all pending data and cancel the corking. Socket is locked.
549  */
550 static void udp_v6_flush_pending_frames(struct sock *sk)
551 {
552         struct udp_opt *up = udp_sk(sk);
553
554         if (up->pending) {
555                 up->len = 0;
556                 up->pending = 0;
557                 ip6_flush_pending_frames(sk);
558         }
559 }
560
561 /*
562  *      Sending
563  */
564
565 static int udp_v6_push_pending_frames(struct sock *sk, struct udp_opt *up)
566 {
567         struct sk_buff *skb;
568         struct udphdr *uh;
569         struct inet_opt *inet = inet_sk(sk);
570         struct flowi *fl = &inet->cork.fl;
571         int err = 0;
572
573         /* Grab the skbuff where UDP header space exists. */
574         if ((skb = skb_peek(&sk->sk_write_queue)) == NULL)
575                 goto out;
576
577         /*
578          * Create a UDP header
579          */
580         uh = skb->h.uh;
581         uh->source = fl->fl_ip_sport;
582         uh->dest = fl->fl_ip_dport;
583         uh->len = htons(up->len);
584         uh->check = 0;
585
586         if (sk->sk_no_check == UDP_CSUM_NOXMIT) {
587                 skb->ip_summed = CHECKSUM_NONE;
588                 goto send;
589         }
590
591         if (skb_queue_len(&sk->sk_write_queue) == 1) {
592                 skb->csum = csum_partial((char *)uh,
593                                 sizeof(struct udphdr), skb->csum);
594                 uh->check = csum_ipv6_magic(&fl->fl6_src,
595                                             &fl->fl6_dst,
596                                             up->len, fl->proto, skb->csum);
597         } else {
598                 u32 tmp_csum = 0;
599
600                 skb_queue_walk(&sk->sk_write_queue, skb) {
601                         tmp_csum = csum_add(tmp_csum, skb->csum);
602                 }
603                 tmp_csum = csum_partial((char *)uh,
604                                 sizeof(struct udphdr), tmp_csum);
605                 tmp_csum = csum_ipv6_magic(&fl->fl6_src,
606                                            &fl->fl6_dst,
607                                            up->len, fl->proto, tmp_csum);
608                 uh->check = tmp_csum;
609
610         }
611         if (uh->check == 0)
612                 uh->check = -1;
613
614 send:
615         err = ip6_push_pending_frames(sk);
616 out:
617         up->len = 0;
618         up->pending = 0;
619         return err;
620 }
621
622 static int udpv6_sendmsg(struct kiocb *iocb, struct sock *sk, 
623                   struct msghdr *msg, size_t len)
624 {
625         struct ipv6_txoptions opt_space;
626         struct udp_opt *up = udp_sk(sk);
627         struct inet_opt *inet = inet_sk(sk);
628         struct ipv6_pinfo *np = inet6_sk(sk);
629         struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) msg->msg_name;
630         struct in6_addr *daddr;
631         struct ipv6_txoptions *opt = NULL;
632         struct ip6_flowlabel *flowlabel = NULL;
633         struct flowi *fl = &inet->cork.fl;
634         struct dst_entry *dst;
635         int addr_len = msg->msg_namelen;
636         int ulen = len;
637         int hlimit = -1;
638         int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
639         int err;
640
641         /* destination address check */
642         if (sin6) {
643                 if (addr_len < offsetof(struct sockaddr, sa_data))
644                         return -EINVAL;
645
646                 switch (sin6->sin6_family) {
647                 case AF_INET6:
648                         if (addr_len < SIN6_LEN_RFC2133)
649                                 return -EINVAL;
650                         daddr = &sin6->sin6_addr;
651                         break;
652                 case AF_INET:
653                         goto do_udp_sendmsg;
654                 case AF_UNSPEC:
655                         msg->msg_name = sin6 = NULL;
656                         msg->msg_namelen = addr_len = 0;
657                         daddr = NULL;
658                         break;
659                 default:
660                         return -EINVAL;
661                 }
662         } else if (!up->pending) {
663                 if (sk->sk_state != TCP_ESTABLISHED)
664                         return -EDESTADDRREQ;
665                 daddr = &np->daddr;
666         } else 
667                 daddr = NULL;
668
669         if (daddr) {
670                 if (ipv6_addr_type(daddr) == IPV6_ADDR_MAPPED) {
671                         struct sockaddr_in sin;
672                         sin.sin_family = AF_INET;
673                         sin.sin_port = sin6 ? sin6->sin6_port : inet->dport;
674                         sin.sin_addr.s_addr = daddr->s6_addr32[3];
675                         msg->msg_name = &sin;
676                         msg->msg_namelen = sizeof(sin);
677 do_udp_sendmsg:
678                         if (__ipv6_only_sock(sk))
679                                 return -ENETUNREACH;
680                         return udp_sendmsg(iocb, sk, msg, len);
681                 }
682         }
683
684         if (up->pending == AF_INET)
685                 return udp_sendmsg(iocb, sk, msg, len);
686
687         /* Rough check on arithmetic overflow,
688            better check is made in ip6_build_xmit
689            */
690         if (len > INT_MAX - sizeof(struct udphdr))
691                 return -EMSGSIZE;
692         
693         if (up->pending) {
694                 /*
695                  * There are pending frames.
696                  * The socket lock must be held while it's corked.
697                  */
698                 lock_sock(sk);
699                 if (likely(up->pending)) {
700                         if (unlikely(up->pending != AF_INET6)) {
701                                 release_sock(sk);
702                                 return -EINVAL;
703                         }
704                         dst = NULL;
705                         goto do_append_data;
706                 }
707                 release_sock(sk);
708         }
709         ulen += sizeof(struct udphdr);
710
711         memset(fl, 0, sizeof(*fl));
712
713         if (sin6) {
714                 if (sin6->sin6_port == 0)
715                         return -EINVAL;
716
717                 fl->fl_ip_dport = sin6->sin6_port;
718                 daddr = &sin6->sin6_addr;
719
720                 if (np->sndflow) {
721                         fl->fl6_flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
722                         if (fl->fl6_flowlabel&IPV6_FLOWLABEL_MASK) {
723                                 flowlabel = fl6_sock_lookup(sk, fl->fl6_flowlabel);
724                                 if (flowlabel == NULL)
725                                         return -EINVAL;
726                                 daddr = &flowlabel->dst;
727                         }
728                 }
729
730                 /*
731                  * Otherwise it will be difficult to maintain
732                  * sk->sk_dst_cache.
733                  */
734                 if (sk->sk_state == TCP_ESTABLISHED &&
735                     !ipv6_addr_cmp(daddr, &np->daddr))
736                         daddr = &np->daddr;
737
738                 if (addr_len >= sizeof(struct sockaddr_in6) &&
739                     sin6->sin6_scope_id &&
740                     ipv6_addr_type(daddr)&IPV6_ADDR_LINKLOCAL)
741                         fl->oif = sin6->sin6_scope_id;
742         } else {
743                 if (sk->sk_state != TCP_ESTABLISHED)
744                         return -EDESTADDRREQ;
745
746                 fl->fl_ip_dport = inet->dport;
747                 daddr = &np->daddr;
748                 fl->fl6_flowlabel = np->flow_label;
749         }
750
751         if (!fl->oif)
752                 fl->oif = sk->sk_bound_dev_if;
753
754         if (msg->msg_controllen) {
755                 opt = &opt_space;
756                 memset(opt, 0, sizeof(struct ipv6_txoptions));
757                 opt->tot_len = sizeof(*opt);
758
759                 err = datagram_send_ctl(msg, fl, opt, &hlimit);
760                 if (err < 0) {
761                         fl6_sock_release(flowlabel);
762                         return err;
763                 }
764                 if ((fl->fl6_flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
765                         flowlabel = fl6_sock_lookup(sk, fl->fl6_flowlabel);
766                         if (flowlabel == NULL)
767                                 return -EINVAL;
768                 }
769                 if (!(opt->opt_nflen|opt->opt_flen))
770                         opt = NULL;
771         }
772         if (opt == NULL)
773                 opt = np->opt;
774         if (flowlabel)
775                 opt = fl6_merge_options(&opt_space, flowlabel, opt);
776
777         fl->proto = IPPROTO_UDP;
778         ipv6_addr_copy(&fl->fl6_dst, daddr);
779         if (ipv6_addr_any(&fl->fl6_src) && !ipv6_addr_any(&np->saddr))
780                 ipv6_addr_copy(&fl->fl6_src, &np->saddr);
781         fl->fl_ip_sport = inet->sport;
782         
783         /* merge ip6_build_xmit from ip6_output */
784         if (opt && opt->srcrt) {
785                 struct rt0_hdr *rt0 = (struct rt0_hdr *) opt->srcrt;
786                 ipv6_addr_copy(&fl->fl6_dst, rt0->addr);
787         }
788
789         if (!fl->oif && ipv6_addr_is_multicast(&fl->fl6_dst))
790                 fl->oif = np->mcast_oif;
791
792         err = ip6_dst_lookup(sk, &dst, fl);
793         if (err)
794                 goto out;
795
796         if (hlimit < 0) {
797                 if (ipv6_addr_is_multicast(&fl->fl6_dst))
798                         hlimit = np->mcast_hops;
799                 else
800                         hlimit = np->hop_limit;
801                 if (hlimit < 0)
802                         hlimit = dst_metric(dst, RTAX_HOPLIMIT);
803         }
804
805         if (msg->msg_flags&MSG_CONFIRM)
806                 goto do_confirm;
807 back_from_confirm:
808
809         lock_sock(sk);
810         if (unlikely(up->pending)) {
811                 /* The socket is already corked while preparing it. */
812                 /* ... which is an evident application bug. --ANK */
813                 release_sock(sk);
814
815                 LIMIT_NETDEBUG(printk(KERN_DEBUG "udp cork app bug 2\n"));
816                 err = -EINVAL;
817                 goto out;
818         }
819
820         up->pending = AF_INET6;
821
822 do_append_data:
823         up->len += ulen;
824         err = ip6_append_data(sk, ip_generic_getfrag, msg->msg_iov, ulen, sizeof(struct udphdr),
825                               hlimit, opt, fl, (struct rt6_info*)dst,
826                               corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags);
827         if (err)
828                 udp_v6_flush_pending_frames(sk);
829         else if (!corkreq)
830                 err = udp_v6_push_pending_frames(sk, up);
831
832         if (dst)
833                 ip6_dst_store(sk, dst,
834                               !ipv6_addr_cmp(&fl->fl6_dst, &np->daddr) ?
835                               &np->daddr : NULL);
836         if (err > 0)
837                 err = np->recverr ? net_xmit_errno(err) : 0;
838         release_sock(sk);
839 out:
840         fl6_sock_release(flowlabel);
841         if (!err) {
842                 UDP6_INC_STATS_USER(UDP_MIB_OUTDATAGRAMS);
843                 return len;
844         }
845         return err;
846
847 do_confirm:
848         dst_confirm(dst);
849         if (!(msg->msg_flags&MSG_PROBE) || len)
850                 goto back_from_confirm;
851         err = 0;
852         goto out;
853 }
854
855 static int udpv6_destroy_sock(struct sock *sk)
856 {
857         lock_sock(sk);
858         udp_v6_flush_pending_frames(sk);
859         release_sock(sk);
860
861         inet6_destroy_sock(sk);
862
863         return 0;
864 }
865
866 /*
867  *      Socket option code for UDP
868  */
869 static int udpv6_setsockopt(struct sock *sk, int level, int optname, 
870                           char __user *optval, int optlen)
871 {
872         struct udp_opt *up = udp_sk(sk);
873         int val;
874         int err = 0;
875
876         if (level != SOL_UDP)
877                 return ipv6_setsockopt(sk, level, optname, optval, optlen);
878
879         if(optlen<sizeof(int))
880                 return -EINVAL;
881
882         if (get_user(val, (int __user *)optval))
883                 return -EFAULT;
884
885         switch(optname) {
886         case UDP_CORK:
887                 if (val != 0) {
888                         up->corkflag = 1;
889                 } else {
890                         up->corkflag = 0;
891                         lock_sock(sk);
892                         udp_v6_push_pending_frames(sk, up);
893                         release_sock(sk);
894                 }
895                 break;
896                 
897         case UDP_ENCAP:
898                 switch (val) {
899                 case 0:
900                         up->encap_type = val;
901                         break;
902                 default:
903                         err = -ENOPROTOOPT;
904                         break;
905                 }
906                 break;
907
908         default:
909                 err = -ENOPROTOOPT;
910                 break;
911         };
912
913         return err;
914 }
915
916 static int udpv6_getsockopt(struct sock *sk, int level, int optname, 
917                           char __user *optval, int __user *optlen)
918 {
919         struct udp_opt *up = udp_sk(sk);
920         int val, len;
921
922         if (level != SOL_UDP)
923                 return ipv6_getsockopt(sk, level, optname, optval, optlen);
924
925         if(get_user(len,optlen))
926                 return -EFAULT;
927
928         len = min_t(unsigned int, len, sizeof(int));
929         
930         if(len < 0)
931                 return -EINVAL;
932
933         switch(optname) {
934         case UDP_CORK:
935                 val = up->corkflag;
936                 break;
937
938         case UDP_ENCAP:
939                 val = up->encap_type;
940                 break;
941
942         default:
943                 return -ENOPROTOOPT;
944         };
945
946         if(put_user(len, optlen))
947                 return -EFAULT;
948         if(copy_to_user(optval, &val,len))
949                 return -EFAULT;
950         return 0;
951 }
952
953 static struct inet6_protocol udpv6_protocol = {
954         .handler        =       udpv6_rcv,
955         .err_handler    =       udpv6_err,
956         .flags          =       INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
957 };
958
959 /* ------------------------------------------------------------------------ */
960 #ifdef CONFIG_PROC_FS
961
962 static void udp6_sock_seq_show(struct seq_file *seq, struct sock *sp, int bucket)
963 {
964         struct inet_opt *inet = inet_sk(sp);
965         struct ipv6_pinfo *np = inet6_sk(sp);
966         struct in6_addr *dest, *src;
967         __u16 destp, srcp;
968
969         dest  = &np->daddr;
970         src   = &np->rcv_saddr;
971         destp = ntohs(inet->dport);
972         srcp  = ntohs(inet->sport);
973         seq_printf(seq,
974                    "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
975                    "%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p\n",
976                    bucket,
977                    src->s6_addr32[0], src->s6_addr32[1],
978                    src->s6_addr32[2], src->s6_addr32[3], srcp,
979                    dest->s6_addr32[0], dest->s6_addr32[1],
980                    dest->s6_addr32[2], dest->s6_addr32[3], destp,
981                    sp->sk_state, 
982                    atomic_read(&sp->sk_wmem_alloc),
983                    atomic_read(&sp->sk_rmem_alloc),
984                    0, 0L, 0,
985                    sock_i_uid(sp), 0,
986                    sock_i_ino(sp),
987                    atomic_read(&sp->sk_refcnt), sp);
988 }
989
990 static int udp6_seq_show(struct seq_file *seq, void *v)
991 {
992         if (v == SEQ_START_TOKEN)
993                 seq_printf(seq,
994                            "  sl  "
995                            "local_address                         "
996                            "remote_address                        "
997                            "st tx_queue rx_queue tr tm->when retrnsmt"
998                            "   uid  timeout inode\n");
999         else
1000                 udp6_sock_seq_show(seq, v, ((struct udp_iter_state *)seq->private)->bucket);
1001         return 0;
1002 }
1003
1004 static struct file_operations udp6_seq_fops;
1005 static struct udp_seq_afinfo udp6_seq_afinfo = {
1006         .owner          = THIS_MODULE,
1007         .name           = "udp6",
1008         .family         = AF_INET6,
1009         .seq_show       = udp6_seq_show,
1010         .seq_fops       = &udp6_seq_fops,
1011 };
1012
1013 int __init udp6_proc_init(void)
1014 {
1015         return udp_proc_register(&udp6_seq_afinfo);
1016 }
1017
1018 void udp6_proc_exit(void) {
1019         udp_proc_unregister(&udp6_seq_afinfo);
1020 }
1021 #endif /* CONFIG_PROC_FS */
1022
1023 /* ------------------------------------------------------------------------ */
1024
1025 struct proto udpv6_prot = {
1026         .name =         "UDP",
1027         .close =        udpv6_close,
1028         .connect =      ip6_datagram_connect,
1029         .disconnect =   udp_disconnect,
1030         .ioctl =        udp_ioctl,
1031         .destroy =      udpv6_destroy_sock,
1032         .setsockopt =   udpv6_setsockopt,
1033         .getsockopt =   udpv6_getsockopt,
1034         .sendmsg =      udpv6_sendmsg,
1035         .recvmsg =      udpv6_recvmsg,
1036         .backlog_rcv =  udpv6_queue_rcv_skb,
1037         .hash =         udp_v6_hash,
1038         .unhash =       udp_v6_unhash,
1039         .get_port =     udp_v6_get_port,
1040 };
1041
1042 extern struct proto_ops inet6_dgram_ops;
1043
1044 static struct inet_protosw udpv6_protosw = {
1045         .type =      SOCK_DGRAM,
1046         .protocol =  IPPROTO_UDP,
1047         .prot =      &udpv6_prot,
1048         .ops =       &inet6_dgram_ops,
1049         .capability =-1,
1050         .no_check =  UDP_CSUM_DEFAULT,
1051         .flags =     INET_PROTOSW_PERMANENT,
1052 };
1053
1054
1055 void __init udpv6_init(void)
1056 {
1057         if (inet6_add_protocol(&udpv6_protocol, IPPROTO_UDP) < 0)
1058                 printk(KERN_ERR "udpv6_init: Could not register protocol\n");
1059         inet6_register_protosw(&udpv6_protosw);
1060 }