Merge /spare/repo/linux-2.6/
[linux-2.6] / 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_sock *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_equal(&np->rcv_saddr, daddr))
175                                         continue;
176                                 score++;
177                         }
178                         if (!ipv6_addr_any(&np->daddr)) {
179                                 if (!ipv6_addr_equal(&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 inet_sock *inet = inet_sk(sk);
223         struct sk_buff *skb;
224         size_t copied;
225         int err;
226
227         if (addr_len)
228                 *addr_len=sizeof(struct sockaddr_in6);
229   
230         if (flags & MSG_ERRQUEUE)
231                 return ipv6_recv_error(sk, msg, len);
232
233 try_again:
234         skb = skb_recv_datagram(sk, flags, noblock, &err);
235         if (!skb)
236                 goto out;
237
238         copied = skb->len - sizeof(struct udphdr);
239         if (copied > len) {
240                 copied = len;
241                 msg->msg_flags |= MSG_TRUNC;
242         }
243
244         if (skb->ip_summed==CHECKSUM_UNNECESSARY) {
245                 err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov,
246                                               copied);
247         } else if (msg->msg_flags&MSG_TRUNC) {
248                 if ((unsigned short)csum_fold(skb_checksum(skb, 0, skb->len, skb->csum)))
249                         goto csum_copy_err;
250                 err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov,
251                                               copied);
252         } else {
253                 err = skb_copy_and_csum_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov);
254                 if (err == -EINVAL)
255                         goto csum_copy_err;
256         }
257         if (err)
258                 goto out_free;
259
260         sock_recv_timestamp(msg, sk, skb);
261
262         /* Copy the address. */
263         if (msg->msg_name) {
264                 struct sockaddr_in6 *sin6;
265           
266                 sin6 = (struct sockaddr_in6 *) msg->msg_name;
267                 sin6->sin6_family = AF_INET6;
268                 sin6->sin6_port = skb->h.uh->source;
269                 sin6->sin6_flowinfo = 0;
270                 sin6->sin6_scope_id = 0;
271
272                 if (skb->protocol == htons(ETH_P_IP))
273                         ipv6_addr_set(&sin6->sin6_addr, 0, 0,
274                                       htonl(0xffff), skb->nh.iph->saddr);
275                 else {
276                         ipv6_addr_copy(&sin6->sin6_addr, &skb->nh.ipv6h->saddr);
277                         if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
278                                 sin6->sin6_scope_id = IP6CB(skb)->iif;
279                 }
280
281         }
282         if (skb->protocol == htons(ETH_P_IP)) {
283                 if (inet->cmsg_flags)
284                         ip_cmsg_recv(msg, skb);
285         } else {
286                 if (np->rxopt.all)
287                         datagram_recv_ctl(sk, msg, skb);
288         }
289
290         err = copied;
291         if (flags & MSG_TRUNC)
292                 err = skb->len - sizeof(struct udphdr);
293
294 out_free:
295         skb_free_datagram(sk, skb);
296 out:
297         return err;
298
299 csum_copy_err:
300         /* Clear queue. */
301         if (flags&MSG_PEEK) {
302                 int clear = 0;
303                 spin_lock_bh(&sk->sk_receive_queue.lock);
304                 if (skb == skb_peek(&sk->sk_receive_queue)) {
305                         __skb_unlink(skb, &sk->sk_receive_queue);
306                         clear = 1;
307                 }
308                 spin_unlock_bh(&sk->sk_receive_queue.lock);
309                 if (clear)
310                         kfree_skb(skb);
311         }
312
313         skb_free_datagram(sk, skb);
314
315         if (flags & MSG_DONTWAIT) {
316                 UDP6_INC_STATS_USER(UDP_MIB_INERRORS);
317                 return -EAGAIN;
318         }
319         goto try_again;
320 }
321
322 static void udpv6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
323                int type, int code, int offset, __u32 info)
324 {
325         struct ipv6_pinfo *np;
326         struct ipv6hdr *hdr = (struct ipv6hdr*)skb->data;
327         struct net_device *dev = skb->dev;
328         struct in6_addr *saddr = &hdr->saddr;
329         struct in6_addr *daddr = &hdr->daddr;
330         struct udphdr *uh = (struct udphdr*)(skb->data+offset);
331         struct sock *sk;
332         int err;
333
334         sk = udp_v6_lookup(daddr, uh->dest, saddr, uh->source, dev->ifindex);
335    
336         if (sk == NULL)
337                 return;
338
339         np = inet6_sk(sk);
340
341         if (!icmpv6_err_convert(type, code, &err) && !np->recverr)
342                 goto out;
343
344         if (sk->sk_state != TCP_ESTABLISHED && !np->recverr)
345                 goto out;
346
347         if (np->recverr)
348                 ipv6_icmp_error(sk, skb, err, uh->dest, ntohl(info), (u8 *)(uh+1));
349
350         sk->sk_err = err;
351         sk->sk_error_report(sk);
352 out:
353         sock_put(sk);
354 }
355
356 static inline int udpv6_queue_rcv_skb(struct sock * sk, struct sk_buff *skb)
357 {
358         if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb)) {
359                 kfree_skb(skb);
360                 return -1;
361         }
362
363         if (skb->ip_summed != CHECKSUM_UNNECESSARY) {
364                 if ((unsigned short)csum_fold(skb_checksum(skb, 0, skb->len, skb->csum))) {
365                         UDP6_INC_STATS_BH(UDP_MIB_INERRORS);
366                         kfree_skb(skb);
367                         return 0;
368                 }
369                 skb->ip_summed = CHECKSUM_UNNECESSARY;
370         }
371
372         if (sock_queue_rcv_skb(sk,skb)<0) {
373                 UDP6_INC_STATS_BH(UDP_MIB_INERRORS);
374                 kfree_skb(skb);
375                 return 0;
376         }
377         UDP6_INC_STATS_BH(UDP_MIB_INDATAGRAMS);
378         return 0;
379 }
380
381 static struct sock *udp_v6_mcast_next(struct sock *sk,
382                                       u16 loc_port, struct in6_addr *loc_addr,
383                                       u16 rmt_port, struct in6_addr *rmt_addr,
384                                       int dif)
385 {
386         struct hlist_node *node;
387         struct sock *s = sk;
388         unsigned short num = ntohs(loc_port);
389
390         sk_for_each_from(s, node) {
391                 struct inet_sock *inet = inet_sk(s);
392
393                 if (inet->num == num && s->sk_family == PF_INET6) {
394                         struct ipv6_pinfo *np = inet6_sk(s);
395                         if (inet->dport) {
396                                 if (inet->dport != rmt_port)
397                                         continue;
398                         }
399                         if (!ipv6_addr_any(&np->daddr) &&
400                             !ipv6_addr_equal(&np->daddr, rmt_addr))
401                                 continue;
402
403                         if (s->sk_bound_dev_if && s->sk_bound_dev_if != dif)
404                                 continue;
405
406                         if (!ipv6_addr_any(&np->rcv_saddr)) {
407                                 if (ipv6_addr_equal(&np->rcv_saddr, loc_addr))
408                                         return s;
409                                 continue;
410                         }
411                         if(!inet6_mc_check(s, loc_addr, rmt_addr))
412                                 continue;
413                         return s;
414                 }
415         }
416         return NULL;
417 }
418
419 /*
420  * Note: called only from the BH handler context,
421  * so we don't need to lock the hashes.
422  */
423 static void udpv6_mcast_deliver(struct udphdr *uh,
424                                 struct in6_addr *saddr, struct in6_addr *daddr,
425                                 struct sk_buff *skb)
426 {
427         struct sock *sk, *sk2;
428         int dif;
429
430         read_lock(&udp_hash_lock);
431         sk = sk_head(&udp_hash[ntohs(uh->dest) & (UDP_HTABLE_SIZE - 1)]);
432         dif = skb->dev->ifindex;
433         sk = udp_v6_mcast_next(sk, uh->dest, daddr, uh->source, saddr, dif);
434         if (!sk) {
435                 kfree_skb(skb);
436                 goto out;
437         }
438
439         sk2 = sk;
440         while ((sk2 = udp_v6_mcast_next(sk_next(sk2), uh->dest, daddr,
441                                         uh->source, saddr, dif))) {
442                 struct sk_buff *buff = skb_clone(skb, GFP_ATOMIC);
443                 if (buff)
444                         udpv6_queue_rcv_skb(sk2, buff);
445         }
446         udpv6_queue_rcv_skb(sk, skb);
447 out:
448         read_unlock(&udp_hash_lock);
449 }
450
451 static int udpv6_rcv(struct sk_buff **pskb, unsigned int *nhoffp)
452 {
453         struct sk_buff *skb = *pskb;
454         struct sock *sk;
455         struct udphdr *uh;
456         struct net_device *dev = skb->dev;
457         struct in6_addr *saddr, *daddr;
458         u32 ulen = 0;
459
460         if (!pskb_may_pull(skb, sizeof(struct udphdr)))
461                 goto short_packet;
462
463         saddr = &skb->nh.ipv6h->saddr;
464         daddr = &skb->nh.ipv6h->daddr;
465         uh = skb->h.uh;
466
467         ulen = ntohs(uh->len);
468
469         /* Check for jumbo payload */
470         if (ulen == 0)
471                 ulen = skb->len;
472
473         if (ulen > skb->len || ulen < sizeof(*uh))
474                 goto short_packet;
475
476         if (uh->check == 0) {
477                 /* RFC 2460 section 8.1 says that we SHOULD log
478                    this error. Well, it is reasonable.
479                  */
480                 LIMIT_NETDEBUG(
481                         printk(KERN_INFO "IPv6: udp checksum is 0\n"));
482                 goto discard;
483         }
484
485         if (ulen < skb->len) {
486                 if (__pskb_trim(skb, ulen))
487                         goto discard;
488                 saddr = &skb->nh.ipv6h->saddr;
489                 daddr = &skb->nh.ipv6h->daddr;
490                 uh = skb->h.uh;
491         }
492
493         if (skb->ip_summed==CHECKSUM_HW) {
494                 skb->ip_summed = CHECKSUM_UNNECESSARY;
495                 if (csum_ipv6_magic(saddr, daddr, ulen, IPPROTO_UDP, skb->csum)) {
496                         LIMIT_NETDEBUG(printk(KERN_DEBUG "udp v6 hw csum failure.\n"));
497                         skb->ip_summed = CHECKSUM_NONE;
498                 }
499         }
500         if (skb->ip_summed != CHECKSUM_UNNECESSARY)
501                 skb->csum = ~csum_ipv6_magic(saddr, daddr, ulen, IPPROTO_UDP, 0);
502
503         /* 
504          *      Multicast receive code 
505          */
506         if (ipv6_addr_is_multicast(daddr)) {
507                 udpv6_mcast_deliver(uh, saddr, daddr, skb);
508                 return 0;
509         }
510
511         /* Unicast */
512
513         /* 
514          * check socket cache ... must talk to Alan about his plans
515          * for sock caches... i'll skip this for now.
516          */
517         sk = udp_v6_lookup(saddr, uh->source, daddr, uh->dest, dev->ifindex);
518
519         if (sk == NULL) {
520                 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
521                         goto discard;
522
523                 if (skb->ip_summed != CHECKSUM_UNNECESSARY &&
524                     (unsigned short)csum_fold(skb_checksum(skb, 0, skb->len, skb->csum)))
525                         goto discard;
526                 UDP6_INC_STATS_BH(UDP_MIB_NOPORTS);
527
528                 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0, dev);
529
530                 kfree_skb(skb);
531                 return(0);
532         }
533         
534         /* deliver */
535         
536         udpv6_queue_rcv_skb(sk, skb);
537         sock_put(sk);
538         return(0);
539
540 short_packet:   
541         if (net_ratelimit())
542                 printk(KERN_DEBUG "UDP: short packet: %d/%u\n", ulen, skb->len);
543
544 discard:
545         UDP6_INC_STATS_BH(UDP_MIB_INERRORS);
546         kfree_skb(skb);
547         return(0);      
548 }
549 /*
550  * Throw away all pending data and cancel the corking. Socket is locked.
551  */
552 static void udp_v6_flush_pending_frames(struct sock *sk)
553 {
554         struct udp_sock *up = udp_sk(sk);
555
556         if (up->pending) {
557                 up->len = 0;
558                 up->pending = 0;
559                 ip6_flush_pending_frames(sk);
560         }
561 }
562
563 /*
564  *      Sending
565  */
566
567 static int udp_v6_push_pending_frames(struct sock *sk, struct udp_sock *up)
568 {
569         struct sk_buff *skb;
570         struct udphdr *uh;
571         struct inet_sock *inet = inet_sk(sk);
572         struct flowi *fl = &inet->cork.fl;
573         int err = 0;
574
575         /* Grab the skbuff where UDP header space exists. */
576         if ((skb = skb_peek(&sk->sk_write_queue)) == NULL)
577                 goto out;
578
579         /*
580          * Create a UDP header
581          */
582         uh = skb->h.uh;
583         uh->source = fl->fl_ip_sport;
584         uh->dest = fl->fl_ip_dport;
585         uh->len = htons(up->len);
586         uh->check = 0;
587
588         if (sk->sk_no_check == UDP_CSUM_NOXMIT) {
589                 skb->ip_summed = CHECKSUM_NONE;
590                 goto send;
591         }
592
593         if (skb_queue_len(&sk->sk_write_queue) == 1) {
594                 skb->csum = csum_partial((char *)uh,
595                                 sizeof(struct udphdr), skb->csum);
596                 uh->check = csum_ipv6_magic(&fl->fl6_src,
597                                             &fl->fl6_dst,
598                                             up->len, fl->proto, skb->csum);
599         } else {
600                 u32 tmp_csum = 0;
601
602                 skb_queue_walk(&sk->sk_write_queue, skb) {
603                         tmp_csum = csum_add(tmp_csum, skb->csum);
604                 }
605                 tmp_csum = csum_partial((char *)uh,
606                                 sizeof(struct udphdr), tmp_csum);
607                 tmp_csum = csum_ipv6_magic(&fl->fl6_src,
608                                            &fl->fl6_dst,
609                                            up->len, fl->proto, tmp_csum);
610                 uh->check = tmp_csum;
611
612         }
613         if (uh->check == 0)
614                 uh->check = -1;
615
616 send:
617         err = ip6_push_pending_frames(sk);
618 out:
619         up->len = 0;
620         up->pending = 0;
621         return err;
622 }
623
624 static int udpv6_sendmsg(struct kiocb *iocb, struct sock *sk, 
625                   struct msghdr *msg, size_t len)
626 {
627         struct ipv6_txoptions opt_space;
628         struct udp_sock *up = udp_sk(sk);
629         struct inet_sock *inet = inet_sk(sk);
630         struct ipv6_pinfo *np = inet6_sk(sk);
631         struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) msg->msg_name;
632         struct in6_addr *daddr, *final_p = NULL, final;
633         struct ipv6_txoptions *opt = NULL;
634         struct ip6_flowlabel *flowlabel = NULL;
635         struct flowi *fl = &inet->cork.fl;
636         struct dst_entry *dst;
637         int addr_len = msg->msg_namelen;
638         int ulen = len;
639         int hlimit = -1;
640         int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
641         int err;
642
643         /* destination address check */
644         if (sin6) {
645                 if (addr_len < offsetof(struct sockaddr, sa_data))
646                         return -EINVAL;
647
648                 switch (sin6->sin6_family) {
649                 case AF_INET6:
650                         if (addr_len < SIN6_LEN_RFC2133)
651                                 return -EINVAL;
652                         daddr = &sin6->sin6_addr;
653                         break;
654                 case AF_INET:
655                         goto do_udp_sendmsg;
656                 case AF_UNSPEC:
657                         msg->msg_name = sin6 = NULL;
658                         msg->msg_namelen = addr_len = 0;
659                         daddr = NULL;
660                         break;
661                 default:
662                         return -EINVAL;
663                 }
664         } else if (!up->pending) {
665                 if (sk->sk_state != TCP_ESTABLISHED)
666                         return -EDESTADDRREQ;
667                 daddr = &np->daddr;
668         } else 
669                 daddr = NULL;
670
671         if (daddr) {
672                 if (ipv6_addr_type(daddr) == IPV6_ADDR_MAPPED) {
673                         struct sockaddr_in sin;
674                         sin.sin_family = AF_INET;
675                         sin.sin_port = sin6 ? sin6->sin6_port : inet->dport;
676                         sin.sin_addr.s_addr = daddr->s6_addr32[3];
677                         msg->msg_name = &sin;
678                         msg->msg_namelen = sizeof(sin);
679 do_udp_sendmsg:
680                         if (__ipv6_only_sock(sk))
681                                 return -ENETUNREACH;
682                         return udp_sendmsg(iocb, sk, msg, len);
683                 }
684         }
685
686         if (up->pending == AF_INET)
687                 return udp_sendmsg(iocb, sk, msg, len);
688
689         /* Rough check on arithmetic overflow,
690            better check is made in ip6_build_xmit
691            */
692         if (len > INT_MAX - sizeof(struct udphdr))
693                 return -EMSGSIZE;
694         
695         if (up->pending) {
696                 /*
697                  * There are pending frames.
698                  * The socket lock must be held while it's corked.
699                  */
700                 lock_sock(sk);
701                 if (likely(up->pending)) {
702                         if (unlikely(up->pending != AF_INET6)) {
703                                 release_sock(sk);
704                                 return -EAFNOSUPPORT;
705                         }
706                         dst = NULL;
707                         goto do_append_data;
708                 }
709                 release_sock(sk);
710         }
711         ulen += sizeof(struct udphdr);
712
713         memset(fl, 0, sizeof(*fl));
714
715         if (sin6) {
716                 if (sin6->sin6_port == 0)
717                         return -EINVAL;
718
719                 fl->fl_ip_dport = sin6->sin6_port;
720                 daddr = &sin6->sin6_addr;
721
722                 if (np->sndflow) {
723                         fl->fl6_flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
724                         if (fl->fl6_flowlabel&IPV6_FLOWLABEL_MASK) {
725                                 flowlabel = fl6_sock_lookup(sk, fl->fl6_flowlabel);
726                                 if (flowlabel == NULL)
727                                         return -EINVAL;
728                                 daddr = &flowlabel->dst;
729                         }
730                 }
731
732                 /*
733                  * Otherwise it will be difficult to maintain
734                  * sk->sk_dst_cache.
735                  */
736                 if (sk->sk_state == TCP_ESTABLISHED &&
737                     ipv6_addr_equal(daddr, &np->daddr))
738                         daddr = &np->daddr;
739
740                 if (addr_len >= sizeof(struct sockaddr_in6) &&
741                     sin6->sin6_scope_id &&
742                     ipv6_addr_type(daddr)&IPV6_ADDR_LINKLOCAL)
743                         fl->oif = sin6->sin6_scope_id;
744         } else {
745                 if (sk->sk_state != TCP_ESTABLISHED)
746                         return -EDESTADDRREQ;
747
748                 fl->fl_ip_dport = inet->dport;
749                 daddr = &np->daddr;
750                 fl->fl6_flowlabel = np->flow_label;
751         }
752
753         if (!fl->oif)
754                 fl->oif = sk->sk_bound_dev_if;
755
756         if (msg->msg_controllen) {
757                 opt = &opt_space;
758                 memset(opt, 0, sizeof(struct ipv6_txoptions));
759                 opt->tot_len = sizeof(*opt);
760
761                 err = datagram_send_ctl(msg, fl, opt, &hlimit);
762                 if (err < 0) {
763                         fl6_sock_release(flowlabel);
764                         return err;
765                 }
766                 if ((fl->fl6_flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
767                         flowlabel = fl6_sock_lookup(sk, fl->fl6_flowlabel);
768                         if (flowlabel == NULL)
769                                 return -EINVAL;
770                 }
771                 if (!(opt->opt_nflen|opt->opt_flen))
772                         opt = NULL;
773         }
774         if (opt == NULL)
775                 opt = np->opt;
776         if (flowlabel)
777                 opt = fl6_merge_options(&opt_space, flowlabel, opt);
778
779         fl->proto = IPPROTO_UDP;
780         ipv6_addr_copy(&fl->fl6_dst, daddr);
781         if (ipv6_addr_any(&fl->fl6_src) && !ipv6_addr_any(&np->saddr))
782                 ipv6_addr_copy(&fl->fl6_src, &np->saddr);
783         fl->fl_ip_sport = inet->sport;
784         
785         /* merge ip6_build_xmit from ip6_output */
786         if (opt && opt->srcrt) {
787                 struct rt0_hdr *rt0 = (struct rt0_hdr *) opt->srcrt;
788                 ipv6_addr_copy(&final, &fl->fl6_dst);
789                 ipv6_addr_copy(&fl->fl6_dst, rt0->addr);
790                 final_p = &final;
791         }
792
793         if (!fl->oif && ipv6_addr_is_multicast(&fl->fl6_dst))
794                 fl->oif = np->mcast_oif;
795
796         err = ip6_dst_lookup(sk, &dst, fl);
797         if (err)
798                 goto out;
799         if (final_p)
800                 ipv6_addr_copy(&fl->fl6_dst, final_p);
801
802         if ((err = xfrm_lookup(&dst, fl, sk, 0)) < 0) {
803                 dst_release(dst);
804                 goto out;
805         }
806
807         if (hlimit < 0) {
808                 if (ipv6_addr_is_multicast(&fl->fl6_dst))
809                         hlimit = np->mcast_hops;
810                 else
811                         hlimit = np->hop_limit;
812                 if (hlimit < 0)
813                         hlimit = dst_metric(dst, RTAX_HOPLIMIT);
814                 if (hlimit < 0)
815                         hlimit = ipv6_get_hoplimit(dst->dev);
816         }
817
818         if (msg->msg_flags&MSG_CONFIRM)
819                 goto do_confirm;
820 back_from_confirm:
821
822         lock_sock(sk);
823         if (unlikely(up->pending)) {
824                 /* The socket is already corked while preparing it. */
825                 /* ... which is an evident application bug. --ANK */
826                 release_sock(sk);
827
828                 LIMIT_NETDEBUG(printk(KERN_DEBUG "udp cork app bug 2\n"));
829                 err = -EINVAL;
830                 goto out;
831         }
832
833         up->pending = AF_INET6;
834
835 do_append_data:
836         up->len += ulen;
837         err = ip6_append_data(sk, ip_generic_getfrag, msg->msg_iov, ulen, sizeof(struct udphdr),
838                               hlimit, opt, fl, (struct rt6_info*)dst,
839                               corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags);
840         if (err)
841                 udp_v6_flush_pending_frames(sk);
842         else if (!corkreq)
843                 err = udp_v6_push_pending_frames(sk, up);
844
845         if (dst)
846                 ip6_dst_store(sk, dst,
847                               ipv6_addr_equal(&fl->fl6_dst, &np->daddr) ?
848                               &np->daddr : NULL);
849         if (err > 0)
850                 err = np->recverr ? net_xmit_errno(err) : 0;
851         release_sock(sk);
852 out:
853         fl6_sock_release(flowlabel);
854         if (!err) {
855                 UDP6_INC_STATS_USER(UDP_MIB_OUTDATAGRAMS);
856                 return len;
857         }
858         return err;
859
860 do_confirm:
861         dst_confirm(dst);
862         if (!(msg->msg_flags&MSG_PROBE) || len)
863                 goto back_from_confirm;
864         err = 0;
865         goto out;
866 }
867
868 static int udpv6_destroy_sock(struct sock *sk)
869 {
870         lock_sock(sk);
871         udp_v6_flush_pending_frames(sk);
872         release_sock(sk);
873
874         inet6_destroy_sock(sk);
875
876         return 0;
877 }
878
879 /*
880  *      Socket option code for UDP
881  */
882 static int udpv6_setsockopt(struct sock *sk, int level, int optname, 
883                           char __user *optval, int optlen)
884 {
885         struct udp_sock *up = udp_sk(sk);
886         int val;
887         int err = 0;
888
889         if (level != SOL_UDP)
890                 return ipv6_setsockopt(sk, level, optname, optval, optlen);
891
892         if(optlen<sizeof(int))
893                 return -EINVAL;
894
895         if (get_user(val, (int __user *)optval))
896                 return -EFAULT;
897
898         switch(optname) {
899         case UDP_CORK:
900                 if (val != 0) {
901                         up->corkflag = 1;
902                 } else {
903                         up->corkflag = 0;
904                         lock_sock(sk);
905                         udp_v6_push_pending_frames(sk, up);
906                         release_sock(sk);
907                 }
908                 break;
909                 
910         case UDP_ENCAP:
911                 switch (val) {
912                 case 0:
913                         up->encap_type = val;
914                         break;
915                 default:
916                         err = -ENOPROTOOPT;
917                         break;
918                 }
919                 break;
920
921         default:
922                 err = -ENOPROTOOPT;
923                 break;
924         };
925
926         return err;
927 }
928
929 static int udpv6_getsockopt(struct sock *sk, int level, int optname, 
930                           char __user *optval, int __user *optlen)
931 {
932         struct udp_sock *up = udp_sk(sk);
933         int val, len;
934
935         if (level != SOL_UDP)
936                 return ipv6_getsockopt(sk, level, optname, optval, optlen);
937
938         if(get_user(len,optlen))
939                 return -EFAULT;
940
941         len = min_t(unsigned int, len, sizeof(int));
942         
943         if(len < 0)
944                 return -EINVAL;
945
946         switch(optname) {
947         case UDP_CORK:
948                 val = up->corkflag;
949                 break;
950
951         case UDP_ENCAP:
952                 val = up->encap_type;
953                 break;
954
955         default:
956                 return -ENOPROTOOPT;
957         };
958
959         if(put_user(len, optlen))
960                 return -EFAULT;
961         if(copy_to_user(optval, &val,len))
962                 return -EFAULT;
963         return 0;
964 }
965
966 static struct inet6_protocol udpv6_protocol = {
967         .handler        =       udpv6_rcv,
968         .err_handler    =       udpv6_err,
969         .flags          =       INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
970 };
971
972 /* ------------------------------------------------------------------------ */
973 #ifdef CONFIG_PROC_FS
974
975 static void udp6_sock_seq_show(struct seq_file *seq, struct sock *sp, int bucket)
976 {
977         struct inet_sock *inet = inet_sk(sp);
978         struct ipv6_pinfo *np = inet6_sk(sp);
979         struct in6_addr *dest, *src;
980         __u16 destp, srcp;
981
982         dest  = &np->daddr;
983         src   = &np->rcv_saddr;
984         destp = ntohs(inet->dport);
985         srcp  = ntohs(inet->sport);
986         seq_printf(seq,
987                    "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
988                    "%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p\n",
989                    bucket,
990                    src->s6_addr32[0], src->s6_addr32[1],
991                    src->s6_addr32[2], src->s6_addr32[3], srcp,
992                    dest->s6_addr32[0], dest->s6_addr32[1],
993                    dest->s6_addr32[2], dest->s6_addr32[3], destp,
994                    sp->sk_state, 
995                    atomic_read(&sp->sk_wmem_alloc),
996                    atomic_read(&sp->sk_rmem_alloc),
997                    0, 0L, 0,
998                    sock_i_uid(sp), 0,
999                    sock_i_ino(sp),
1000                    atomic_read(&sp->sk_refcnt), sp);
1001 }
1002
1003 static int udp6_seq_show(struct seq_file *seq, void *v)
1004 {
1005         if (v == SEQ_START_TOKEN)
1006                 seq_printf(seq,
1007                            "  sl  "
1008                            "local_address                         "
1009                            "remote_address                        "
1010                            "st tx_queue rx_queue tr tm->when retrnsmt"
1011                            "   uid  timeout inode\n");
1012         else
1013                 udp6_sock_seq_show(seq, v, ((struct udp_iter_state *)seq->private)->bucket);
1014         return 0;
1015 }
1016
1017 static struct file_operations udp6_seq_fops;
1018 static struct udp_seq_afinfo udp6_seq_afinfo = {
1019         .owner          = THIS_MODULE,
1020         .name           = "udp6",
1021         .family         = AF_INET6,
1022         .seq_show       = udp6_seq_show,
1023         .seq_fops       = &udp6_seq_fops,
1024 };
1025
1026 int __init udp6_proc_init(void)
1027 {
1028         return udp_proc_register(&udp6_seq_afinfo);
1029 }
1030
1031 void udp6_proc_exit(void) {
1032         udp_proc_unregister(&udp6_seq_afinfo);
1033 }
1034 #endif /* CONFIG_PROC_FS */
1035
1036 /* ------------------------------------------------------------------------ */
1037
1038 struct proto udpv6_prot = {
1039         .name =         "UDPv6",
1040         .owner =        THIS_MODULE,
1041         .close =        udpv6_close,
1042         .connect =      ip6_datagram_connect,
1043         .disconnect =   udp_disconnect,
1044         .ioctl =        udp_ioctl,
1045         .destroy =      udpv6_destroy_sock,
1046         .setsockopt =   udpv6_setsockopt,
1047         .getsockopt =   udpv6_getsockopt,
1048         .sendmsg =      udpv6_sendmsg,
1049         .recvmsg =      udpv6_recvmsg,
1050         .backlog_rcv =  udpv6_queue_rcv_skb,
1051         .hash =         udp_v6_hash,
1052         .unhash =       udp_v6_unhash,
1053         .get_port =     udp_v6_get_port,
1054         .obj_size =     sizeof(struct udp6_sock),
1055 };
1056
1057 extern struct proto_ops inet6_dgram_ops;
1058
1059 static struct inet_protosw udpv6_protosw = {
1060         .type =      SOCK_DGRAM,
1061         .protocol =  IPPROTO_UDP,
1062         .prot =      &udpv6_prot,
1063         .ops =       &inet6_dgram_ops,
1064         .capability =-1,
1065         .no_check =  UDP_CSUM_DEFAULT,
1066         .flags =     INET_PROTOSW_PERMANENT,
1067 };
1068
1069
1070 void __init udpv6_init(void)
1071 {
1072         if (inet6_add_protocol(&udpv6_protocol, IPPROTO_UDP) < 0)
1073                 printk(KERN_ERR "udpv6_init: Could not register protocol\n");
1074         inet6_register_protosw(&udpv6_protosw);
1075 }