[PATCH] libata irq-pio: merge the ata_dataout_task workqueue with ata_pio_task workqueue
[pv_ops_mirror.git] / net / ipv6 / udp.c
blobbf9519341fd30ad28a0b9652d09bcd7628765dc4
1 /*
2 * UDP over IPv6
3 * Linux INET6 implementation
5 * Authors:
6 * Pedro Roque <roque@di.fc.ul.pt>
8 * Based on linux/ipv4/udp.c
10 * $Id: udp.c,v 1.65 2002/02/01 22:01:04 davem Exp $
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.
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.
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>
41 #include <net/sock.h>
42 #include <net/snmp.h>
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 #include <net/tcp_states.h>
56 #include <net/ip6_checksum.h>
57 #include <net/xfrm.h>
59 #include <linux/proc_fs.h>
60 #include <linux/seq_file.h>
62 DEFINE_SNMP_STAT(struct udp_mib, udp_stats_in6) __read_mostly;
64 /* Grrr, addr_type already calculated by caller, but I don't want
65 * to add some silly "cookie" argument to this method just for that.
67 static int udp_v6_get_port(struct sock *sk, unsigned short snum)
69 struct sock *sk2;
70 struct hlist_node *node;
72 write_lock_bh(&udp_hash_lock);
73 if (snum == 0) {
74 int best_size_so_far, best, result, i;
76 if (udp_port_rover > sysctl_local_port_range[1] ||
77 udp_port_rover < sysctl_local_port_range[0])
78 udp_port_rover = sysctl_local_port_range[0];
79 best_size_so_far = 32767;
80 best = result = udp_port_rover;
81 for (i = 0; i < UDP_HTABLE_SIZE; i++, result++) {
82 int size;
83 struct hlist_head *list;
85 list = &udp_hash[result & (UDP_HTABLE_SIZE - 1)];
86 if (hlist_empty(list)) {
87 if (result > sysctl_local_port_range[1])
88 result = sysctl_local_port_range[0] +
89 ((result - sysctl_local_port_range[0]) &
90 (UDP_HTABLE_SIZE - 1));
91 goto gotit;
93 size = 0;
94 sk_for_each(sk2, node, list)
95 if (++size >= best_size_so_far)
96 goto next;
97 best_size_so_far = size;
98 best = result;
99 next:;
101 result = best;
102 for(i = 0; i < (1 << 16) / UDP_HTABLE_SIZE; i++, result += UDP_HTABLE_SIZE) {
103 if (result > sysctl_local_port_range[1])
104 result = sysctl_local_port_range[0]
105 + ((result - sysctl_local_port_range[0]) &
106 (UDP_HTABLE_SIZE - 1));
107 if (!udp_lport_inuse(result))
108 break;
110 if (i >= (1 << 16) / UDP_HTABLE_SIZE)
111 goto fail;
112 gotit:
113 udp_port_rover = snum = result;
114 } else {
115 sk_for_each(sk2, node,
116 &udp_hash[snum & (UDP_HTABLE_SIZE - 1)]) {
117 if (inet_sk(sk2)->num == snum &&
118 sk2 != sk &&
119 (!sk2->sk_bound_dev_if ||
120 !sk->sk_bound_dev_if ||
121 sk2->sk_bound_dev_if == sk->sk_bound_dev_if) &&
122 (!sk2->sk_reuse || !sk->sk_reuse) &&
123 ipv6_rcv_saddr_equal(sk, sk2))
124 goto fail;
128 inet_sk(sk)->num = snum;
129 if (sk_unhashed(sk)) {
130 sk_add_node(sk, &udp_hash[snum & (UDP_HTABLE_SIZE - 1)]);
131 sock_prot_inc_use(sk->sk_prot);
133 write_unlock_bh(&udp_hash_lock);
134 return 0;
136 fail:
137 write_unlock_bh(&udp_hash_lock);
138 return 1;
141 static void udp_v6_hash(struct sock *sk)
143 BUG();
146 static void udp_v6_unhash(struct sock *sk)
148 write_lock_bh(&udp_hash_lock);
149 if (sk_del_node_init(sk)) {
150 inet_sk(sk)->num = 0;
151 sock_prot_dec_use(sk->sk_prot);
153 write_unlock_bh(&udp_hash_lock);
156 static struct sock *udp_v6_lookup(struct in6_addr *saddr, u16 sport,
157 struct in6_addr *daddr, u16 dport, int dif)
159 struct sock *sk, *result = NULL;
160 struct hlist_node *node;
161 unsigned short hnum = ntohs(dport);
162 int badness = -1;
164 read_lock(&udp_hash_lock);
165 sk_for_each(sk, node, &udp_hash[hnum & (UDP_HTABLE_SIZE - 1)]) {
166 struct inet_sock *inet = inet_sk(sk);
168 if (inet->num == hnum && sk->sk_family == PF_INET6) {
169 struct ipv6_pinfo *np = inet6_sk(sk);
170 int score = 0;
171 if (inet->dport) {
172 if (inet->dport != sport)
173 continue;
174 score++;
176 if (!ipv6_addr_any(&np->rcv_saddr)) {
177 if (!ipv6_addr_equal(&np->rcv_saddr, daddr))
178 continue;
179 score++;
181 if (!ipv6_addr_any(&np->daddr)) {
182 if (!ipv6_addr_equal(&np->daddr, saddr))
183 continue;
184 score++;
186 if (sk->sk_bound_dev_if) {
187 if (sk->sk_bound_dev_if != dif)
188 continue;
189 score++;
191 if(score == 4) {
192 result = sk;
193 break;
194 } else if(score > badness) {
195 result = sk;
196 badness = score;
200 if (result)
201 sock_hold(result);
202 read_unlock(&udp_hash_lock);
203 return result;
210 static void udpv6_close(struct sock *sk, long timeout)
212 sk_common_release(sk);
216 * This should be easy, if there is something there we
217 * return it, otherwise we block.
220 static int udpv6_recvmsg(struct kiocb *iocb, struct sock *sk,
221 struct msghdr *msg, size_t len,
222 int noblock, int flags, int *addr_len)
224 struct ipv6_pinfo *np = inet6_sk(sk);
225 struct inet_sock *inet = inet_sk(sk);
226 struct sk_buff *skb;
227 size_t copied;
228 int err;
230 if (addr_len)
231 *addr_len=sizeof(struct sockaddr_in6);
233 if (flags & MSG_ERRQUEUE)
234 return ipv6_recv_error(sk, msg, len);
236 try_again:
237 skb = skb_recv_datagram(sk, flags, noblock, &err);
238 if (!skb)
239 goto out;
241 copied = skb->len - sizeof(struct udphdr);
242 if (copied > len) {
243 copied = len;
244 msg->msg_flags |= MSG_TRUNC;
247 if (skb->ip_summed==CHECKSUM_UNNECESSARY) {
248 err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov,
249 copied);
250 } else if (msg->msg_flags&MSG_TRUNC) {
251 if ((unsigned short)csum_fold(skb_checksum(skb, 0, skb->len, skb->csum)))
252 goto csum_copy_err;
253 err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov,
254 copied);
255 } else {
256 err = skb_copy_and_csum_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov);
257 if (err == -EINVAL)
258 goto csum_copy_err;
260 if (err)
261 goto out_free;
263 sock_recv_timestamp(msg, sk, skb);
265 /* Copy the address. */
266 if (msg->msg_name) {
267 struct sockaddr_in6 *sin6;
269 sin6 = (struct sockaddr_in6 *) msg->msg_name;
270 sin6->sin6_family = AF_INET6;
271 sin6->sin6_port = skb->h.uh->source;
272 sin6->sin6_flowinfo = 0;
273 sin6->sin6_scope_id = 0;
275 if (skb->protocol == htons(ETH_P_IP))
276 ipv6_addr_set(&sin6->sin6_addr, 0, 0,
277 htonl(0xffff), skb->nh.iph->saddr);
278 else {
279 ipv6_addr_copy(&sin6->sin6_addr, &skb->nh.ipv6h->saddr);
280 if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
281 sin6->sin6_scope_id = IP6CB(skb)->iif;
285 if (skb->protocol == htons(ETH_P_IP)) {
286 if (inet->cmsg_flags)
287 ip_cmsg_recv(msg, skb);
288 } else {
289 if (np->rxopt.all)
290 datagram_recv_ctl(sk, msg, skb);
293 err = copied;
294 if (flags & MSG_TRUNC)
295 err = skb->len - sizeof(struct udphdr);
297 out_free:
298 skb_free_datagram(sk, skb);
299 out:
300 return err;
302 csum_copy_err:
303 /* Clear queue. */
304 if (flags&MSG_PEEK) {
305 int clear = 0;
306 spin_lock_bh(&sk->sk_receive_queue.lock);
307 if (skb == skb_peek(&sk->sk_receive_queue)) {
308 __skb_unlink(skb, &sk->sk_receive_queue);
309 clear = 1;
311 spin_unlock_bh(&sk->sk_receive_queue.lock);
312 if (clear)
313 kfree_skb(skb);
316 skb_free_datagram(sk, skb);
318 if (flags & MSG_DONTWAIT) {
319 UDP6_INC_STATS_USER(UDP_MIB_INERRORS);
320 return -EAGAIN;
322 goto try_again;
325 static void udpv6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
326 int type, int code, int offset, __u32 info)
328 struct ipv6_pinfo *np;
329 struct ipv6hdr *hdr = (struct ipv6hdr*)skb->data;
330 struct net_device *dev = skb->dev;
331 struct in6_addr *saddr = &hdr->saddr;
332 struct in6_addr *daddr = &hdr->daddr;
333 struct udphdr *uh = (struct udphdr*)(skb->data+offset);
334 struct sock *sk;
335 int err;
337 sk = udp_v6_lookup(daddr, uh->dest, saddr, uh->source, dev->ifindex);
339 if (sk == NULL)
340 return;
342 np = inet6_sk(sk);
344 if (!icmpv6_err_convert(type, code, &err) && !np->recverr)
345 goto out;
347 if (sk->sk_state != TCP_ESTABLISHED && !np->recverr)
348 goto out;
350 if (np->recverr)
351 ipv6_icmp_error(sk, skb, err, uh->dest, ntohl(info), (u8 *)(uh+1));
353 sk->sk_err = err;
354 sk->sk_error_report(sk);
355 out:
356 sock_put(sk);
359 static inline int udpv6_queue_rcv_skb(struct sock * sk, struct sk_buff *skb)
361 if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb)) {
362 kfree_skb(skb);
363 return -1;
366 if (skb->ip_summed != CHECKSUM_UNNECESSARY) {
367 if ((unsigned short)csum_fold(skb_checksum(skb, 0, skb->len, skb->csum))) {
368 UDP6_INC_STATS_BH(UDP_MIB_INERRORS);
369 kfree_skb(skb);
370 return 0;
372 skb->ip_summed = CHECKSUM_UNNECESSARY;
375 if (sock_queue_rcv_skb(sk,skb)<0) {
376 UDP6_INC_STATS_BH(UDP_MIB_INERRORS);
377 kfree_skb(skb);
378 return 0;
380 UDP6_INC_STATS_BH(UDP_MIB_INDATAGRAMS);
381 return 0;
384 static struct sock *udp_v6_mcast_next(struct sock *sk,
385 u16 loc_port, struct in6_addr *loc_addr,
386 u16 rmt_port, struct in6_addr *rmt_addr,
387 int dif)
389 struct hlist_node *node;
390 struct sock *s = sk;
391 unsigned short num = ntohs(loc_port);
393 sk_for_each_from(s, node) {
394 struct inet_sock *inet = inet_sk(s);
396 if (inet->num == num && s->sk_family == PF_INET6) {
397 struct ipv6_pinfo *np = inet6_sk(s);
398 if (inet->dport) {
399 if (inet->dport != rmt_port)
400 continue;
402 if (!ipv6_addr_any(&np->daddr) &&
403 !ipv6_addr_equal(&np->daddr, rmt_addr))
404 continue;
406 if (s->sk_bound_dev_if && s->sk_bound_dev_if != dif)
407 continue;
409 if (!ipv6_addr_any(&np->rcv_saddr)) {
410 if (!ipv6_addr_equal(&np->rcv_saddr, loc_addr))
411 continue;
413 if(!inet6_mc_check(s, loc_addr, rmt_addr))
414 continue;
415 return s;
418 return NULL;
422 * Note: called only from the BH handler context,
423 * so we don't need to lock the hashes.
425 static void udpv6_mcast_deliver(struct udphdr *uh,
426 struct in6_addr *saddr, struct in6_addr *daddr,
427 struct sk_buff *skb)
429 struct sock *sk, *sk2;
430 int dif;
432 read_lock(&udp_hash_lock);
433 sk = sk_head(&udp_hash[ntohs(uh->dest) & (UDP_HTABLE_SIZE - 1)]);
434 dif = skb->dev->ifindex;
435 sk = udp_v6_mcast_next(sk, uh->dest, daddr, uh->source, saddr, dif);
436 if (!sk) {
437 kfree_skb(skb);
438 goto out;
441 sk2 = sk;
442 while ((sk2 = udp_v6_mcast_next(sk_next(sk2), uh->dest, daddr,
443 uh->source, saddr, dif))) {
444 struct sk_buff *buff = skb_clone(skb, GFP_ATOMIC);
445 if (buff)
446 udpv6_queue_rcv_skb(sk2, buff);
448 udpv6_queue_rcv_skb(sk, skb);
449 out:
450 read_unlock(&udp_hash_lock);
453 static int udpv6_rcv(struct sk_buff **pskb, unsigned int *nhoffp)
455 struct sk_buff *skb = *pskb;
456 struct sock *sk;
457 struct udphdr *uh;
458 struct net_device *dev = skb->dev;
459 struct in6_addr *saddr, *daddr;
460 u32 ulen = 0;
462 if (!pskb_may_pull(skb, sizeof(struct udphdr)))
463 goto short_packet;
465 saddr = &skb->nh.ipv6h->saddr;
466 daddr = &skb->nh.ipv6h->daddr;
467 uh = skb->h.uh;
469 ulen = ntohs(uh->len);
471 /* Check for jumbo payload */
472 if (ulen == 0)
473 ulen = skb->len;
475 if (ulen > skb->len || ulen < sizeof(*uh))
476 goto short_packet;
478 if (uh->check == 0) {
479 /* RFC 2460 section 8.1 says that we SHOULD log
480 this error. Well, it is reasonable.
482 LIMIT_NETDEBUG(KERN_INFO "IPv6: udp checksum is 0\n");
483 goto discard;
486 if (ulen < skb->len) {
487 if (pskb_trim_rcsum(skb, ulen))
488 goto discard;
489 saddr = &skb->nh.ipv6h->saddr;
490 daddr = &skb->nh.ipv6h->daddr;
491 uh = skb->h.uh;
494 if (skb->ip_summed==CHECKSUM_HW) {
495 skb->ip_summed = CHECKSUM_UNNECESSARY;
496 if (csum_ipv6_magic(saddr, daddr, ulen, IPPROTO_UDP, skb->csum)) {
497 LIMIT_NETDEBUG(KERN_DEBUG "udp v6 hw csum failure.\n");
498 skb->ip_summed = CHECKSUM_NONE;
501 if (skb->ip_summed != CHECKSUM_UNNECESSARY)
502 skb->csum = ~csum_ipv6_magic(saddr, daddr, ulen, IPPROTO_UDP, 0);
505 * Multicast receive code
507 if (ipv6_addr_is_multicast(daddr)) {
508 udpv6_mcast_deliver(uh, saddr, daddr, skb);
509 return 0;
512 /* Unicast */
515 * check socket cache ... must talk to Alan about his plans
516 * for sock caches... i'll skip this for now.
518 sk = udp_v6_lookup(saddr, uh->source, daddr, uh->dest, dev->ifindex);
520 if (sk == NULL) {
521 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
522 goto discard;
524 if (skb->ip_summed != CHECKSUM_UNNECESSARY &&
525 (unsigned short)csum_fold(skb_checksum(skb, 0, skb->len, skb->csum)))
526 goto discard;
527 UDP6_INC_STATS_BH(UDP_MIB_NOPORTS);
529 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0, dev);
531 kfree_skb(skb);
532 return(0);
535 /* deliver */
537 udpv6_queue_rcv_skb(sk, skb);
538 sock_put(sk);
539 return(0);
541 short_packet:
542 if (net_ratelimit())
543 printk(KERN_DEBUG "UDP: short packet: %d/%u\n", ulen, skb->len);
545 discard:
546 UDP6_INC_STATS_BH(UDP_MIB_INERRORS);
547 kfree_skb(skb);
548 return(0);
551 * Throw away all pending data and cancel the corking. Socket is locked.
553 static void udp_v6_flush_pending_frames(struct sock *sk)
555 struct udp_sock *up = udp_sk(sk);
557 if (up->pending) {
558 up->len = 0;
559 up->pending = 0;
560 ip6_flush_pending_frames(sk);
565 * Sending
568 static int udp_v6_push_pending_frames(struct sock *sk, struct udp_sock *up)
570 struct sk_buff *skb;
571 struct udphdr *uh;
572 struct inet_sock *inet = inet_sk(sk);
573 struct flowi *fl = &inet->cork.fl;
574 int err = 0;
576 /* Grab the skbuff where UDP header space exists. */
577 if ((skb = skb_peek(&sk->sk_write_queue)) == NULL)
578 goto out;
581 * Create a UDP header
583 uh = skb->h.uh;
584 uh->source = fl->fl_ip_sport;
585 uh->dest = fl->fl_ip_dport;
586 uh->len = htons(up->len);
587 uh->check = 0;
589 if (sk->sk_no_check == UDP_CSUM_NOXMIT) {
590 skb->ip_summed = CHECKSUM_NONE;
591 goto send;
594 if (skb_queue_len(&sk->sk_write_queue) == 1) {
595 skb->csum = csum_partial((char *)uh,
596 sizeof(struct udphdr), skb->csum);
597 uh->check = csum_ipv6_magic(&fl->fl6_src,
598 &fl->fl6_dst,
599 up->len, fl->proto, skb->csum);
600 } else {
601 u32 tmp_csum = 0;
603 skb_queue_walk(&sk->sk_write_queue, skb) {
604 tmp_csum = csum_add(tmp_csum, skb->csum);
606 tmp_csum = csum_partial((char *)uh,
607 sizeof(struct udphdr), tmp_csum);
608 tmp_csum = csum_ipv6_magic(&fl->fl6_src,
609 &fl->fl6_dst,
610 up->len, fl->proto, tmp_csum);
611 uh->check = tmp_csum;
614 if (uh->check == 0)
615 uh->check = -1;
617 send:
618 err = ip6_push_pending_frames(sk);
619 out:
620 up->len = 0;
621 up->pending = 0;
622 return err;
625 static int udpv6_sendmsg(struct kiocb *iocb, struct sock *sk,
626 struct msghdr *msg, size_t len)
628 struct ipv6_txoptions opt_space;
629 struct udp_sock *up = udp_sk(sk);
630 struct inet_sock *inet = inet_sk(sk);
631 struct ipv6_pinfo *np = inet6_sk(sk);
632 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) msg->msg_name;
633 struct in6_addr *daddr, *final_p = NULL, final;
634 struct ipv6_txoptions *opt = NULL;
635 struct ip6_flowlabel *flowlabel = NULL;
636 struct flowi *fl = &inet->cork.fl;
637 struct dst_entry *dst;
638 int addr_len = msg->msg_namelen;
639 int ulen = len;
640 int hlimit = -1;
641 int tclass = -1;
642 int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
643 int err;
644 int connected = 0;
646 /* destination address check */
647 if (sin6) {
648 if (addr_len < offsetof(struct sockaddr, sa_data))
649 return -EINVAL;
651 switch (sin6->sin6_family) {
652 case AF_INET6:
653 if (addr_len < SIN6_LEN_RFC2133)
654 return -EINVAL;
655 daddr = &sin6->sin6_addr;
656 break;
657 case AF_INET:
658 goto do_udp_sendmsg;
659 case AF_UNSPEC:
660 msg->msg_name = sin6 = NULL;
661 msg->msg_namelen = addr_len = 0;
662 daddr = NULL;
663 break;
664 default:
665 return -EINVAL;
667 } else if (!up->pending) {
668 if (sk->sk_state != TCP_ESTABLISHED)
669 return -EDESTADDRREQ;
670 daddr = &np->daddr;
671 } else
672 daddr = NULL;
674 if (daddr) {
675 if (ipv6_addr_type(daddr) == IPV6_ADDR_MAPPED) {
676 struct sockaddr_in sin;
677 sin.sin_family = AF_INET;
678 sin.sin_port = sin6 ? sin6->sin6_port : inet->dport;
679 sin.sin_addr.s_addr = daddr->s6_addr32[3];
680 msg->msg_name = &sin;
681 msg->msg_namelen = sizeof(sin);
682 do_udp_sendmsg:
683 if (__ipv6_only_sock(sk))
684 return -ENETUNREACH;
685 return udp_sendmsg(iocb, sk, msg, len);
689 if (up->pending == AF_INET)
690 return udp_sendmsg(iocb, sk, msg, len);
692 /* Rough check on arithmetic overflow,
693 better check is made in ip6_build_xmit
695 if (len > INT_MAX - sizeof(struct udphdr))
696 return -EMSGSIZE;
698 if (up->pending) {
700 * There are pending frames.
701 * The socket lock must be held while it's corked.
703 lock_sock(sk);
704 if (likely(up->pending)) {
705 if (unlikely(up->pending != AF_INET6)) {
706 release_sock(sk);
707 return -EAFNOSUPPORT;
709 dst = NULL;
710 goto do_append_data;
712 release_sock(sk);
714 ulen += sizeof(struct udphdr);
716 memset(fl, 0, sizeof(*fl));
718 if (sin6) {
719 if (sin6->sin6_port == 0)
720 return -EINVAL;
722 fl->fl_ip_dport = sin6->sin6_port;
723 daddr = &sin6->sin6_addr;
725 if (np->sndflow) {
726 fl->fl6_flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
727 if (fl->fl6_flowlabel&IPV6_FLOWLABEL_MASK) {
728 flowlabel = fl6_sock_lookup(sk, fl->fl6_flowlabel);
729 if (flowlabel == NULL)
730 return -EINVAL;
731 daddr = &flowlabel->dst;
736 * Otherwise it will be difficult to maintain
737 * sk->sk_dst_cache.
739 if (sk->sk_state == TCP_ESTABLISHED &&
740 ipv6_addr_equal(daddr, &np->daddr))
741 daddr = &np->daddr;
743 if (addr_len >= sizeof(struct sockaddr_in6) &&
744 sin6->sin6_scope_id &&
745 ipv6_addr_type(daddr)&IPV6_ADDR_LINKLOCAL)
746 fl->oif = sin6->sin6_scope_id;
747 } else {
748 if (sk->sk_state != TCP_ESTABLISHED)
749 return -EDESTADDRREQ;
751 fl->fl_ip_dport = inet->dport;
752 daddr = &np->daddr;
753 fl->fl6_flowlabel = np->flow_label;
754 connected = 1;
757 if (!fl->oif)
758 fl->oif = sk->sk_bound_dev_if;
760 if (msg->msg_controllen) {
761 opt = &opt_space;
762 memset(opt, 0, sizeof(struct ipv6_txoptions));
763 opt->tot_len = sizeof(*opt);
765 err = datagram_send_ctl(msg, fl, opt, &hlimit, &tclass);
766 if (err < 0) {
767 fl6_sock_release(flowlabel);
768 return err;
770 if ((fl->fl6_flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
771 flowlabel = fl6_sock_lookup(sk, fl->fl6_flowlabel);
772 if (flowlabel == NULL)
773 return -EINVAL;
775 if (!(opt->opt_nflen|opt->opt_flen))
776 opt = NULL;
777 connected = 0;
779 if (opt == NULL)
780 opt = np->opt;
781 opt = fl6_merge_options(&opt_space, flowlabel, opt);
783 fl->proto = IPPROTO_UDP;
784 ipv6_addr_copy(&fl->fl6_dst, daddr);
785 if (ipv6_addr_any(&fl->fl6_src) && !ipv6_addr_any(&np->saddr))
786 ipv6_addr_copy(&fl->fl6_src, &np->saddr);
787 fl->fl_ip_sport = inet->sport;
789 /* merge ip6_build_xmit from ip6_output */
790 if (opt && opt->srcrt) {
791 struct rt0_hdr *rt0 = (struct rt0_hdr *) opt->srcrt;
792 ipv6_addr_copy(&final, &fl->fl6_dst);
793 ipv6_addr_copy(&fl->fl6_dst, rt0->addr);
794 final_p = &final;
795 connected = 0;
798 if (!fl->oif && ipv6_addr_is_multicast(&fl->fl6_dst)) {
799 fl->oif = np->mcast_oif;
800 connected = 0;
803 err = ip6_dst_lookup(sk, &dst, fl);
804 if (err)
805 goto out;
806 if (final_p)
807 ipv6_addr_copy(&fl->fl6_dst, final_p);
809 if ((err = xfrm_lookup(&dst, fl, sk, 0)) < 0)
810 goto out;
812 if (hlimit < 0) {
813 if (ipv6_addr_is_multicast(&fl->fl6_dst))
814 hlimit = np->mcast_hops;
815 else
816 hlimit = np->hop_limit;
817 if (hlimit < 0)
818 hlimit = dst_metric(dst, RTAX_HOPLIMIT);
819 if (hlimit < 0)
820 hlimit = ipv6_get_hoplimit(dst->dev);
823 if (tclass < 0) {
824 tclass = np->tclass;
825 if (tclass < 0)
826 tclass = 0;
829 if (msg->msg_flags&MSG_CONFIRM)
830 goto do_confirm;
831 back_from_confirm:
833 lock_sock(sk);
834 if (unlikely(up->pending)) {
835 /* The socket is already corked while preparing it. */
836 /* ... which is an evident application bug. --ANK */
837 release_sock(sk);
839 LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 2\n");
840 err = -EINVAL;
841 goto out;
844 up->pending = AF_INET6;
846 do_append_data:
847 up->len += ulen;
848 err = ip6_append_data(sk, ip_generic_getfrag, msg->msg_iov, ulen,
849 sizeof(struct udphdr), hlimit, tclass, opt, fl,
850 (struct rt6_info*)dst,
851 corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags);
852 if (err)
853 udp_v6_flush_pending_frames(sk);
854 else if (!corkreq)
855 err = udp_v6_push_pending_frames(sk, up);
857 if (dst) {
858 if (connected) {
859 ip6_dst_store(sk, dst,
860 ipv6_addr_equal(&fl->fl6_dst, &np->daddr) ?
861 &np->daddr : NULL);
862 } else {
863 dst_release(dst);
867 if (err > 0)
868 err = np->recverr ? net_xmit_errno(err) : 0;
869 release_sock(sk);
870 out:
871 fl6_sock_release(flowlabel);
872 if (!err) {
873 UDP6_INC_STATS_USER(UDP_MIB_OUTDATAGRAMS);
874 return len;
876 return err;
878 do_confirm:
879 dst_confirm(dst);
880 if (!(msg->msg_flags&MSG_PROBE) || len)
881 goto back_from_confirm;
882 err = 0;
883 goto out;
886 static int udpv6_destroy_sock(struct sock *sk)
888 lock_sock(sk);
889 udp_v6_flush_pending_frames(sk);
890 release_sock(sk);
892 inet6_destroy_sock(sk);
894 return 0;
898 * Socket option code for UDP
900 static int udpv6_setsockopt(struct sock *sk, int level, int optname,
901 char __user *optval, int optlen)
903 struct udp_sock *up = udp_sk(sk);
904 int val;
905 int err = 0;
907 if (level != SOL_UDP)
908 return ipv6_setsockopt(sk, level, optname, optval, optlen);
910 if(optlen<sizeof(int))
911 return -EINVAL;
913 if (get_user(val, (int __user *)optval))
914 return -EFAULT;
916 switch(optname) {
917 case UDP_CORK:
918 if (val != 0) {
919 up->corkflag = 1;
920 } else {
921 up->corkflag = 0;
922 lock_sock(sk);
923 udp_v6_push_pending_frames(sk, up);
924 release_sock(sk);
926 break;
928 case UDP_ENCAP:
929 switch (val) {
930 case 0:
931 up->encap_type = val;
932 break;
933 default:
934 err = -ENOPROTOOPT;
935 break;
937 break;
939 default:
940 err = -ENOPROTOOPT;
941 break;
944 return err;
947 static int udpv6_getsockopt(struct sock *sk, int level, int optname,
948 char __user *optval, int __user *optlen)
950 struct udp_sock *up = udp_sk(sk);
951 int val, len;
953 if (level != SOL_UDP)
954 return ipv6_getsockopt(sk, level, optname, optval, optlen);
956 if(get_user(len,optlen))
957 return -EFAULT;
959 len = min_t(unsigned int, len, sizeof(int));
961 if(len < 0)
962 return -EINVAL;
964 switch(optname) {
965 case UDP_CORK:
966 val = up->corkflag;
967 break;
969 case UDP_ENCAP:
970 val = up->encap_type;
971 break;
973 default:
974 return -ENOPROTOOPT;
977 if(put_user(len, optlen))
978 return -EFAULT;
979 if(copy_to_user(optval, &val,len))
980 return -EFAULT;
981 return 0;
984 static struct inet6_protocol udpv6_protocol = {
985 .handler = udpv6_rcv,
986 .err_handler = udpv6_err,
987 .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
990 /* ------------------------------------------------------------------------ */
991 #ifdef CONFIG_PROC_FS
993 static void udp6_sock_seq_show(struct seq_file *seq, struct sock *sp, int bucket)
995 struct inet_sock *inet = inet_sk(sp);
996 struct ipv6_pinfo *np = inet6_sk(sp);
997 struct in6_addr *dest, *src;
998 __u16 destp, srcp;
1000 dest = &np->daddr;
1001 src = &np->rcv_saddr;
1002 destp = ntohs(inet->dport);
1003 srcp = ntohs(inet->sport);
1004 seq_printf(seq,
1005 "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
1006 "%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p\n",
1007 bucket,
1008 src->s6_addr32[0], src->s6_addr32[1],
1009 src->s6_addr32[2], src->s6_addr32[3], srcp,
1010 dest->s6_addr32[0], dest->s6_addr32[1],
1011 dest->s6_addr32[2], dest->s6_addr32[3], destp,
1012 sp->sk_state,
1013 atomic_read(&sp->sk_wmem_alloc),
1014 atomic_read(&sp->sk_rmem_alloc),
1015 0, 0L, 0,
1016 sock_i_uid(sp), 0,
1017 sock_i_ino(sp),
1018 atomic_read(&sp->sk_refcnt), sp);
1021 static int udp6_seq_show(struct seq_file *seq, void *v)
1023 if (v == SEQ_START_TOKEN)
1024 seq_printf(seq,
1025 " sl "
1026 "local_address "
1027 "remote_address "
1028 "st tx_queue rx_queue tr tm->when retrnsmt"
1029 " uid timeout inode\n");
1030 else
1031 udp6_sock_seq_show(seq, v, ((struct udp_iter_state *)seq->private)->bucket);
1032 return 0;
1035 static struct file_operations udp6_seq_fops;
1036 static struct udp_seq_afinfo udp6_seq_afinfo = {
1037 .owner = THIS_MODULE,
1038 .name = "udp6",
1039 .family = AF_INET6,
1040 .seq_show = udp6_seq_show,
1041 .seq_fops = &udp6_seq_fops,
1044 int __init udp6_proc_init(void)
1046 return udp_proc_register(&udp6_seq_afinfo);
1049 void udp6_proc_exit(void) {
1050 udp_proc_unregister(&udp6_seq_afinfo);
1052 #endif /* CONFIG_PROC_FS */
1054 /* ------------------------------------------------------------------------ */
1056 struct proto udpv6_prot = {
1057 .name = "UDPv6",
1058 .owner = THIS_MODULE,
1059 .close = udpv6_close,
1060 .connect = ip6_datagram_connect,
1061 .disconnect = udp_disconnect,
1062 .ioctl = udp_ioctl,
1063 .destroy = udpv6_destroy_sock,
1064 .setsockopt = udpv6_setsockopt,
1065 .getsockopt = udpv6_getsockopt,
1066 .sendmsg = udpv6_sendmsg,
1067 .recvmsg = udpv6_recvmsg,
1068 .backlog_rcv = udpv6_queue_rcv_skb,
1069 .hash = udp_v6_hash,
1070 .unhash = udp_v6_unhash,
1071 .get_port = udp_v6_get_port,
1072 .obj_size = sizeof(struct udp6_sock),
1075 static struct inet_protosw udpv6_protosw = {
1076 .type = SOCK_DGRAM,
1077 .protocol = IPPROTO_UDP,
1078 .prot = &udpv6_prot,
1079 .ops = &inet6_dgram_ops,
1080 .capability =-1,
1081 .no_check = UDP_CSUM_DEFAULT,
1082 .flags = INET_PROTOSW_PERMANENT,
1086 void __init udpv6_init(void)
1088 if (inet6_add_protocol(&udpv6_protocol, IPPROTO_UDP) < 0)
1089 printk(KERN_ERR "udpv6_init: Could not register protocol\n");
1090 inet6_register_protosw(&udpv6_protosw);