3 * Linux INET6 implementation
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 $
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>
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>
52 #include <net/inet_common.h>
54 #include <net/checksum.h>
57 #include <linux/proc_fs.h>
58 #include <linux/seq_file.h>
60 DEFINE_SNMP_STAT(struct udp_mib
, udp_stats_in6
);
62 /* Grrr, addr_type already calculated by caller, but I don't want
63 * to add some silly "cookie" argument to this method just for that.
65 static int udp_v6_get_port(struct sock
*sk
, unsigned short snum
)
68 struct hlist_node
*node
;
70 write_lock_bh(&udp_hash_lock
);
72 int best_size_so_far
, best
, result
, i
;
74 if (udp_port_rover
> sysctl_local_port_range
[1] ||
75 udp_port_rover
< sysctl_local_port_range
[0])
76 udp_port_rover
= sysctl_local_port_range
[0];
77 best_size_so_far
= 32767;
78 best
= result
= udp_port_rover
;
79 for (i
= 0; i
< UDP_HTABLE_SIZE
; i
++, result
++) {
81 struct hlist_head
*list
;
83 list
= &udp_hash
[result
& (UDP_HTABLE_SIZE
- 1)];
84 if (hlist_empty(list
)) {
85 if (result
> sysctl_local_port_range
[1])
86 result
= sysctl_local_port_range
[0] +
87 ((result
- sysctl_local_port_range
[0]) &
88 (UDP_HTABLE_SIZE
- 1));
92 sk_for_each(sk2
, node
, list
)
93 if (++size
>= best_size_so_far
)
95 best_size_so_far
= size
;
100 for(;; result
+= UDP_HTABLE_SIZE
) {
101 if (result
> sysctl_local_port_range
[1])
102 result
= sysctl_local_port_range
[0]
103 + ((result
- sysctl_local_port_range
[0]) &
104 (UDP_HTABLE_SIZE
- 1));
105 if (!udp_lport_inuse(result
))
109 udp_port_rover
= snum
= result
;
111 sk_for_each(sk2
, node
,
112 &udp_hash
[snum
& (UDP_HTABLE_SIZE
- 1)]) {
113 if (inet_sk(sk2
)->num
== snum
&&
115 (!sk2
->sk_bound_dev_if
||
116 !sk
->sk_bound_dev_if
||
117 sk2
->sk_bound_dev_if
== sk
->sk_bound_dev_if
) &&
118 (!sk2
->sk_reuse
|| !sk
->sk_reuse
) &&
119 ipv6_rcv_saddr_equal(sk
, sk2
))
124 inet_sk(sk
)->num
= snum
;
125 if (sk_unhashed(sk
)) {
126 sk_add_node(sk
, &udp_hash
[snum
& (UDP_HTABLE_SIZE
- 1)]);
127 sock_prot_inc_use(sk
->sk_prot
);
129 write_unlock_bh(&udp_hash_lock
);
133 write_unlock_bh(&udp_hash_lock
);
137 static void udp_v6_hash(struct sock
*sk
)
142 static void udp_v6_unhash(struct sock
*sk
)
144 write_lock_bh(&udp_hash_lock
);
145 if (sk_del_node_init(sk
)) {
146 inet_sk(sk
)->num
= 0;
147 sock_prot_dec_use(sk
->sk_prot
);
149 write_unlock_bh(&udp_hash_lock
);
152 static struct sock
*udp_v6_lookup(struct in6_addr
*saddr
, u16 sport
,
153 struct in6_addr
*daddr
, u16 dport
, int dif
)
155 struct sock
*sk
, *result
= NULL
;
156 struct hlist_node
*node
;
157 unsigned short hnum
= ntohs(dport
);
160 read_lock(&udp_hash_lock
);
161 sk_for_each(sk
, node
, &udp_hash
[hnum
& (UDP_HTABLE_SIZE
- 1)]) {
162 struct inet_opt
*inet
= inet_sk(sk
);
164 if (inet
->num
== hnum
&& sk
->sk_family
== PF_INET6
) {
165 struct ipv6_pinfo
*np
= inet6_sk(sk
);
168 if (inet
->dport
!= sport
)
172 if (!ipv6_addr_any(&np
->rcv_saddr
)) {
173 if (ipv6_addr_cmp(&np
->rcv_saddr
, daddr
))
177 if (!ipv6_addr_any(&np
->daddr
)) {
178 if (ipv6_addr_cmp(&np
->daddr
, saddr
))
182 if (sk
->sk_bound_dev_if
) {
183 if (sk
->sk_bound_dev_if
!= dif
)
190 } else if(score
> badness
) {
198 read_unlock(&udp_hash_lock
);
206 int udpv6_connect(struct sock
*sk
, struct sockaddr
*uaddr
, int addr_len
)
208 struct sockaddr_in6
*usin
= (struct sockaddr_in6
*) uaddr
;
209 struct inet_opt
*inet
= inet_sk(sk
);
210 struct ipv6_pinfo
*np
= inet6_sk(sk
);
211 struct in6_addr
*daddr
;
212 struct dst_entry
*dst
;
214 struct ip6_flowlabel
*flowlabel
= NULL
;
218 if (usin
->sin6_family
== AF_INET
) {
219 if (__ipv6_only_sock(sk
))
220 return -EAFNOSUPPORT
;
221 err
= udp_connect(sk
, uaddr
, addr_len
);
225 if (addr_len
< SIN6_LEN_RFC2133
)
228 if (usin
->sin6_family
!= AF_INET6
)
229 return -EAFNOSUPPORT
;
231 memset(&fl
, 0, sizeof(fl
));
233 fl
.fl6_flowlabel
= usin
->sin6_flowinfo
&IPV6_FLOWINFO_MASK
;
234 if (fl
.fl6_flowlabel
&IPV6_FLOWLABEL_MASK
) {
235 flowlabel
= fl6_sock_lookup(sk
, fl
.fl6_flowlabel
);
236 if (flowlabel
== NULL
)
238 ipv6_addr_copy(&usin
->sin6_addr
, &flowlabel
->dst
);
242 addr_type
= ipv6_addr_type(&usin
->sin6_addr
);
244 if (addr_type
== IPV6_ADDR_ANY
) {
248 usin
->sin6_addr
.s6_addr
[15] = 0x01;
251 daddr
= &usin
->sin6_addr
;
253 if (addr_type
== IPV6_ADDR_MAPPED
) {
254 struct sockaddr_in sin
;
256 if (__ipv6_only_sock(sk
)) {
260 sin
.sin_family
= AF_INET
;
261 sin
.sin_addr
.s_addr
= daddr
->s6_addr32
[3];
262 sin
.sin_port
= usin
->sin6_port
;
264 err
= udp_connect(sk
, (struct sockaddr
*) &sin
, sizeof(sin
));
270 ipv6_addr_set(&np
->daddr
, 0, 0, htonl(0x0000ffff), inet
->daddr
);
272 if (ipv6_addr_any(&np
->saddr
)) {
273 ipv6_addr_set(&np
->saddr
, 0, 0, htonl(0x0000ffff),
277 if (ipv6_addr_any(&np
->rcv_saddr
)) {
278 ipv6_addr_set(&np
->rcv_saddr
, 0, 0, htonl(0x0000ffff),
284 if (addr_type
&IPV6_ADDR_LINKLOCAL
) {
285 if (addr_len
>= sizeof(struct sockaddr_in6
) &&
286 usin
->sin6_scope_id
) {
287 if (sk
->sk_bound_dev_if
&&
288 sk
->sk_bound_dev_if
!= usin
->sin6_scope_id
) {
292 sk
->sk_bound_dev_if
= usin
->sin6_scope_id
;
293 if (!sk
->sk_bound_dev_if
&&
294 (addr_type
& IPV6_ADDR_MULTICAST
))
295 fl
.oif
= np
->mcast_oif
;
298 /* Connect to link-local address requires an interface */
299 if (!sk
->sk_bound_dev_if
) {
305 ipv6_addr_copy(&np
->daddr
, daddr
);
306 np
->flow_label
= fl
.fl6_flowlabel
;
308 inet
->dport
= usin
->sin6_port
;
311 * Check for a route to destination an obtain the
312 * destination cache for it.
315 fl
.proto
= IPPROTO_UDP
;
316 ipv6_addr_copy(&fl
.fl6_dst
, &np
->daddr
);
317 ipv6_addr_copy(&fl
.fl6_src
, &np
->saddr
);
318 fl
.oif
= sk
->sk_bound_dev_if
;
319 fl
.fl_ip_dport
= inet
->dport
;
320 fl
.fl_ip_sport
= inet
->sport
;
322 if (!fl
.oif
&& (addr_type
&IPV6_ADDR_MULTICAST
))
323 fl
.oif
= np
->mcast_oif
;
326 if (flowlabel
->opt
&& flowlabel
->opt
->srcrt
) {
327 struct rt0_hdr
*rt0
= (struct rt0_hdr
*) flowlabel
->opt
->srcrt
;
328 ipv6_addr_copy(&fl
.fl6_dst
, rt0
->addr
);
330 } else if (np
->opt
&& np
->opt
->srcrt
) {
331 struct rt0_hdr
*rt0
= (struct rt0_hdr
*)np
->opt
->srcrt
;
332 ipv6_addr_copy(&fl
.fl6_dst
, rt0
->addr
);
335 err
= ip6_dst_lookup(sk
, &dst
, &fl
);
339 /* source address lookup done in ip6_dst_lookup */
341 if (ipv6_addr_any(&np
->saddr
))
342 ipv6_addr_copy(&np
->saddr
, &fl
.fl6_src
);
344 if (ipv6_addr_any(&np
->rcv_saddr
)) {
345 ipv6_addr_copy(&np
->rcv_saddr
, &fl
.fl6_src
);
346 inet
->rcv_saddr
= LOOPBACK4_IPV6
;
349 ip6_dst_store(sk
, dst
,
350 !ipv6_addr_cmp(&fl
.fl6_dst
, &np
->daddr
) ?
353 sk
->sk_state
= TCP_ESTABLISHED
;
355 fl6_sock_release(flowlabel
);
359 static void udpv6_close(struct sock
*sk
, long timeout
)
361 inet_sock_release(sk
);
365 * This should be easy, if there is something there we
366 * return it, otherwise we block.
369 static int udpv6_recvmsg(struct kiocb
*iocb
, struct sock
*sk
,
370 struct msghdr
*msg
, size_t len
,
371 int noblock
, int flags
, int *addr_len
)
373 struct ipv6_pinfo
*np
= inet6_sk(sk
);
379 *addr_len
=sizeof(struct sockaddr_in6
);
381 if (flags
& MSG_ERRQUEUE
)
382 return ipv6_recv_error(sk
, msg
, len
);
385 skb
= skb_recv_datagram(sk
, flags
, noblock
, &err
);
389 copied
= skb
->len
- sizeof(struct udphdr
);
392 msg
->msg_flags
|= MSG_TRUNC
;
395 if (skb
->ip_summed
==CHECKSUM_UNNECESSARY
) {
396 err
= skb_copy_datagram_iovec(skb
, sizeof(struct udphdr
), msg
->msg_iov
,
398 } else if (msg
->msg_flags
&MSG_TRUNC
) {
399 if ((unsigned short)csum_fold(skb_checksum(skb
, 0, skb
->len
, skb
->csum
)))
401 err
= skb_copy_datagram_iovec(skb
, sizeof(struct udphdr
), msg
->msg_iov
,
404 err
= skb_copy_and_csum_datagram_iovec(skb
, sizeof(struct udphdr
), msg
->msg_iov
);
411 sock_recv_timestamp(msg
, sk
, skb
);
413 /* Copy the address. */
415 struct sockaddr_in6
*sin6
;
417 sin6
= (struct sockaddr_in6
*) msg
->msg_name
;
418 sin6
->sin6_family
= AF_INET6
;
419 sin6
->sin6_port
= skb
->h
.uh
->source
;
420 sin6
->sin6_flowinfo
= 0;
421 sin6
->sin6_scope_id
= 0;
423 if (skb
->protocol
== htons(ETH_P_IP
)) {
424 struct inet_opt
*inet
= inet_sk(sk
);
426 ipv6_addr_set(&sin6
->sin6_addr
, 0, 0,
427 htonl(0xffff), skb
->nh
.iph
->saddr
);
428 if (inet
->cmsg_flags
)
429 ip_cmsg_recv(msg
, skb
);
431 ipv6_addr_copy(&sin6
->sin6_addr
, &skb
->nh
.ipv6h
->saddr
);
434 datagram_recv_ctl(sk
, msg
, skb
);
435 if (ipv6_addr_type(&sin6
->sin6_addr
) & IPV6_ADDR_LINKLOCAL
) {
436 struct inet6_skb_parm
*opt
= (struct inet6_skb_parm
*) skb
->cb
;
437 sin6
->sin6_scope_id
= opt
->iif
;
444 skb_free_datagram(sk
, skb
);
450 if (flags
&MSG_PEEK
) {
452 spin_lock_irq(&sk
->sk_receive_queue
.lock
);
453 if (skb
== skb_peek(&sk
->sk_receive_queue
)) {
454 __skb_unlink(skb
, &sk
->sk_receive_queue
);
457 spin_unlock_irq(&sk
->sk_receive_queue
.lock
);
462 skb_free_datagram(sk
, skb
);
464 if (flags
& MSG_DONTWAIT
) {
465 UDP6_INC_STATS_USER(UdpInErrors
);
471 static void udpv6_err(struct sk_buff
*skb
, struct inet6_skb_parm
*opt
,
472 int type
, int code
, int offset
, __u32 info
)
474 struct ipv6_pinfo
*np
;
475 struct ipv6hdr
*hdr
= (struct ipv6hdr
*)skb
->data
;
476 struct net_device
*dev
= skb
->dev
;
477 struct in6_addr
*saddr
= &hdr
->saddr
;
478 struct in6_addr
*daddr
= &hdr
->daddr
;
479 struct udphdr
*uh
= (struct udphdr
*)(skb
->data
+offset
);
483 sk
= udp_v6_lookup(daddr
, uh
->dest
, saddr
, uh
->source
, dev
->ifindex
);
490 if (!icmpv6_err_convert(type
, code
, &err
) && !np
->recverr
)
493 if (sk
->sk_state
!= TCP_ESTABLISHED
&& !np
->recverr
)
497 ipv6_icmp_error(sk
, skb
, err
, uh
->dest
, ntohl(info
), (u8
*)(uh
+1));
500 sk
->sk_error_report(sk
);
505 static inline int udpv6_queue_rcv_skb(struct sock
* sk
, struct sk_buff
*skb
)
507 if (!xfrm6_policy_check(sk
, XFRM_POLICY_IN
, skb
)) {
512 if (sk
->sk_filter
&& skb
->ip_summed
!= CHECKSUM_UNNECESSARY
) {
513 if ((unsigned short)csum_fold(skb_checksum(skb
, 0, skb
->len
, skb
->csum
))) {
514 UDP6_INC_STATS_BH(UdpInErrors
);
518 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
521 if (sock_queue_rcv_skb(sk
,skb
)<0) {
522 UDP6_INC_STATS_BH(UdpInErrors
);
526 UDP6_INC_STATS_BH(UdpInDatagrams
);
530 static struct sock
*udp_v6_mcast_next(struct sock
*sk
,
531 u16 loc_port
, struct in6_addr
*loc_addr
,
532 u16 rmt_port
, struct in6_addr
*rmt_addr
,
535 struct hlist_node
*node
;
537 unsigned short num
= ntohs(loc_port
);
539 sk_for_each_from(s
, node
) {
540 struct inet_opt
*inet
= inet_sk(s
);
542 if (inet
->num
== num
&& s
->sk_family
== PF_INET6
) {
543 struct ipv6_pinfo
*np
= inet6_sk(s
);
545 if (inet
->dport
!= rmt_port
)
548 if (!ipv6_addr_any(&np
->daddr
) &&
549 ipv6_addr_cmp(&np
->daddr
, rmt_addr
))
552 if (s
->sk_bound_dev_if
&& s
->sk_bound_dev_if
!= dif
)
555 if (!ipv6_addr_any(&np
->rcv_saddr
)) {
556 if (!ipv6_addr_cmp(&np
->rcv_saddr
, loc_addr
))
560 if(!inet6_mc_check(s
, loc_addr
, rmt_addr
))
569 * Note: called only from the BH handler context,
570 * so we don't need to lock the hashes.
572 static void udpv6_mcast_deliver(struct udphdr
*uh
,
573 struct in6_addr
*saddr
, struct in6_addr
*daddr
,
576 struct sock
*sk
, *sk2
;
577 struct sk_buff
*buff
;
580 read_lock(&udp_hash_lock
);
581 sk
= sk_head(&udp_hash
[ntohs(uh
->dest
) & (UDP_HTABLE_SIZE
- 1)]);
582 dif
= skb
->dev
->ifindex
;
583 sk
= udp_v6_mcast_next(sk
, uh
->dest
, daddr
, uh
->source
, saddr
, dif
);
589 while ((sk2
= udp_v6_mcast_next(sk_next(sk2
), uh
->dest
, daddr
,
590 uh
->source
, saddr
, dif
))) {
592 buff
= skb_clone(skb
, GFP_ATOMIC
);
596 if (udpv6_queue_rcv_skb(sk2
, buff
) >= 0)
601 if (udpv6_queue_rcv_skb(sk
, skb
) < 0) {
605 read_unlock(&udp_hash_lock
);
608 static int udpv6_rcv(struct sk_buff
**pskb
, unsigned int *nhoffp
)
610 struct sk_buff
*skb
= *pskb
;
613 struct net_device
*dev
= skb
->dev
;
614 struct in6_addr
*saddr
, *daddr
;
617 if (!pskb_may_pull(skb
, sizeof(struct udphdr
)))
620 saddr
= &skb
->nh
.ipv6h
->saddr
;
621 daddr
= &skb
->nh
.ipv6h
->daddr
;
624 ulen
= ntohs(uh
->len
);
626 /* Check for jumbo payload */
630 if (ulen
> skb
->len
|| ulen
< sizeof(*uh
))
633 if (uh
->check
== 0) {
634 /* RFC 2460 section 8.1 says that we SHOULD log
635 this error. Well, it is reasonable.
638 printk(KERN_INFO
"IPv6: udp checksum is 0\n"));
642 if (ulen
< skb
->len
) {
643 if (__pskb_trim(skb
, ulen
))
645 saddr
= &skb
->nh
.ipv6h
->saddr
;
646 daddr
= &skb
->nh
.ipv6h
->daddr
;
650 if (skb
->ip_summed
==CHECKSUM_HW
) {
651 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
652 if (csum_ipv6_magic(saddr
, daddr
, ulen
, IPPROTO_UDP
, skb
->csum
)) {
653 LIMIT_NETDEBUG(printk(KERN_DEBUG
"udp v6 hw csum failure.\n"));
654 skb
->ip_summed
= CHECKSUM_NONE
;
657 if (skb
->ip_summed
!= CHECKSUM_UNNECESSARY
)
658 skb
->csum
= ~csum_ipv6_magic(saddr
, daddr
, ulen
, IPPROTO_UDP
, 0);
661 * Multicast receive code
663 if (ipv6_addr_is_multicast(daddr
)) {
664 udpv6_mcast_deliver(uh
, saddr
, daddr
, skb
);
671 * check socket cache ... must talk to Alan about his plans
672 * for sock caches... i'll skip this for now.
674 sk
= udp_v6_lookup(saddr
, uh
->source
, daddr
, uh
->dest
, dev
->ifindex
);
677 if (!xfrm6_policy_check(NULL
, XFRM_POLICY_IN
, skb
))
680 if (skb
->ip_summed
!= CHECKSUM_UNNECESSARY
&&
681 (unsigned short)csum_fold(skb_checksum(skb
, 0, skb
->len
, skb
->csum
)))
683 UDP6_INC_STATS_BH(UdpNoPorts
);
685 icmpv6_send(skb
, ICMPV6_DEST_UNREACH
, ICMPV6_PORT_UNREACH
, 0, dev
);
693 udpv6_queue_rcv_skb(sk
, skb
);
699 printk(KERN_DEBUG
"UDP: short packet: %d/%u\n", ulen
, skb
->len
);
702 UDP6_INC_STATS_BH(UdpInErrors
);
707 * Throw away all pending data and cancel the corking. Socket is locked.
709 static void udp_v6_flush_pending_frames(struct sock
*sk
)
711 struct udp_opt
*up
= udp_sk(sk
);
716 ip6_flush_pending_frames(sk
);
724 static int udp_v6_push_pending_frames(struct sock
*sk
, struct udp_opt
*up
)
728 struct inet_opt
*inet
= inet_sk(sk
);
729 struct flowi
*fl
= &inet
->cork
.fl
;
732 /* Grab the skbuff where UDP header space exists. */
733 if ((skb
= skb_peek(&sk
->sk_write_queue
)) == NULL
)
737 * Create a UDP header
740 uh
->source
= fl
->fl_ip_sport
;
741 uh
->dest
= fl
->fl_ip_dport
;
742 uh
->len
= htons(up
->len
);
745 if (sk
->sk_no_check
== UDP_CSUM_NOXMIT
) {
746 skb
->ip_summed
= CHECKSUM_NONE
;
750 if (skb_queue_len(&sk
->sk_write_queue
) == 1) {
751 skb
->csum
= csum_partial((char *)uh
,
752 sizeof(struct udphdr
), skb
->csum
);
753 uh
->check
= csum_ipv6_magic(&fl
->fl6_src
,
755 up
->len
, fl
->proto
, skb
->csum
);
759 skb_queue_walk(&sk
->sk_write_queue
, skb
) {
760 tmp_csum
= csum_add(tmp_csum
, skb
->csum
);
762 tmp_csum
= csum_partial((char *)uh
,
763 sizeof(struct udphdr
), tmp_csum
);
764 tmp_csum
= csum_ipv6_magic(&fl
->fl6_src
,
766 up
->len
, fl
->proto
, tmp_csum
);
767 uh
->check
= tmp_csum
;
774 err
= ip6_push_pending_frames(sk
);
781 static int udpv6_sendmsg(struct kiocb
*iocb
, struct sock
*sk
,
782 struct msghdr
*msg
, size_t len
)
784 struct ipv6_txoptions opt_space
;
785 struct udp_opt
*up
= udp_sk(sk
);
786 struct inet_opt
*inet
= inet_sk(sk
);
787 struct ipv6_pinfo
*np
= inet6_sk(sk
);
788 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*) msg
->msg_name
;
789 struct in6_addr
*daddr
;
790 struct ipv6_txoptions
*opt
= NULL
;
791 struct ip6_flowlabel
*flowlabel
= NULL
;
792 struct flowi
*fl
= &inet
->cork
.fl
;
793 struct dst_entry
*dst
;
794 int addr_len
= msg
->msg_namelen
;
797 int corkreq
= up
->corkflag
|| msg
->msg_flags
&MSG_MORE
;
800 /* destination address check */
802 if (addr_len
< offsetof(struct sockaddr
, sa_data
))
805 switch (sin6
->sin6_family
) {
807 if (addr_len
< SIN6_LEN_RFC2133
)
809 daddr
= &sin6
->sin6_addr
;
814 msg
->msg_name
= sin6
= NULL
;
815 msg
->msg_namelen
= addr_len
= 0;
821 } else if (!up
->pending
) {
822 if (sk
->sk_state
!= TCP_ESTABLISHED
)
823 return -EDESTADDRREQ
;
829 if (ipv6_addr_type(daddr
) == IPV6_ADDR_MAPPED
) {
830 struct sockaddr_in sin
;
831 sin
.sin_family
= AF_INET
;
832 sin
.sin_port
= sin6
? sin6
->sin6_port
: inet
->dport
;
833 sin
.sin_addr
.s_addr
= daddr
->s6_addr32
[3];
834 msg
->msg_name
= &sin
;
835 msg
->msg_namelen
= sizeof(sin
);
837 if (__ipv6_only_sock(sk
))
839 return udp_sendmsg(iocb
, sk
, msg
, len
);
843 if (up
->pending
== AF_INET
)
844 return udp_sendmsg(iocb
, sk
, msg
, len
);
846 /* Rough check on arithmetic overflow,
847 better check is made in ip6_build_xmit
849 if (len
> INT_MAX
- sizeof(struct udphdr
))
854 * There are pending frames.
855 * The socket lock must be held while it's corked.
858 if (likely(up
->pending
)) {
859 if (unlikely(up
->pending
!= AF_INET6
)) {
868 ulen
+= sizeof(struct udphdr
);
870 memset(fl
, 0, sizeof(*fl
));
873 if (sin6
->sin6_port
== 0)
876 fl
->fl_ip_dport
= sin6
->sin6_port
;
877 daddr
= &sin6
->sin6_addr
;
880 fl
->fl6_flowlabel
= sin6
->sin6_flowinfo
&IPV6_FLOWINFO_MASK
;
881 if (fl
->fl6_flowlabel
&IPV6_FLOWLABEL_MASK
) {
882 flowlabel
= fl6_sock_lookup(sk
, fl
->fl6_flowlabel
);
883 if (flowlabel
== NULL
)
885 daddr
= &flowlabel
->dst
;
890 * Otherwise it will be difficult to maintain
893 if (sk
->sk_state
== TCP_ESTABLISHED
&&
894 !ipv6_addr_cmp(daddr
, &np
->daddr
))
897 if (addr_len
>= sizeof(struct sockaddr_in6
) &&
898 sin6
->sin6_scope_id
&&
899 ipv6_addr_type(daddr
)&IPV6_ADDR_LINKLOCAL
)
900 fl
->oif
= sin6
->sin6_scope_id
;
902 if (sk
->sk_state
!= TCP_ESTABLISHED
)
903 return -EDESTADDRREQ
;
905 fl
->fl_ip_dport
= inet
->dport
;
907 fl
->fl6_flowlabel
= np
->flow_label
;
911 fl
->oif
= sk
->sk_bound_dev_if
;
913 if (msg
->msg_controllen
) {
915 memset(opt
, 0, sizeof(struct ipv6_txoptions
));
917 err
= datagram_send_ctl(msg
, fl
, opt
, &hlimit
);
919 fl6_sock_release(flowlabel
);
922 if ((fl
->fl6_flowlabel
&IPV6_FLOWLABEL_MASK
) && !flowlabel
) {
923 flowlabel
= fl6_sock_lookup(sk
, fl
->fl6_flowlabel
);
924 if (flowlabel
== NULL
)
927 if (!(opt
->opt_nflen
|opt
->opt_flen
))
933 opt
= fl6_merge_options(&opt_space
, flowlabel
, opt
);
935 fl
->proto
= IPPROTO_UDP
;
936 ipv6_addr_copy(&fl
->fl6_dst
, daddr
);
937 if (ipv6_addr_any(&fl
->fl6_src
) && !ipv6_addr_any(&np
->saddr
))
938 ipv6_addr_copy(&fl
->fl6_src
, &np
->saddr
);
939 fl
->fl_ip_sport
= inet
->sport
;
941 /* merge ip6_build_xmit from ip6_output */
942 if (opt
&& opt
->srcrt
) {
943 struct rt0_hdr
*rt0
= (struct rt0_hdr
*) opt
->srcrt
;
944 ipv6_addr_copy(&fl
->fl6_dst
, rt0
->addr
);
947 if (!fl
->oif
&& ipv6_addr_is_multicast(&fl
->fl6_dst
))
948 fl
->oif
= np
->mcast_oif
;
950 err
= ip6_dst_lookup(sk
, &dst
, fl
);
955 if (ipv6_addr_is_multicast(&fl
->fl6_dst
))
956 hlimit
= np
->mcast_hops
;
958 hlimit
= np
->hop_limit
;
960 hlimit
= dst_metric(dst
, RTAX_HOPLIMIT
);
963 if (msg
->msg_flags
&MSG_CONFIRM
)
968 if (unlikely(up
->pending
)) {
969 /* The socket is already corked while preparing it. */
970 /* ... which is an evident application bug. --ANK */
973 LIMIT_NETDEBUG(printk(KERN_DEBUG
"udp cork app bug 2\n"));
978 up
->pending
= AF_INET6
;
982 err
= ip6_append_data(sk
, ip_generic_getfrag
, msg
->msg_iov
, ulen
, sizeof(struct udphdr
),
983 hlimit
, opt
, fl
, (struct rt6_info
*)dst
,
984 corkreq
? msg
->msg_flags
|MSG_MORE
: msg
->msg_flags
);
986 udp_v6_flush_pending_frames(sk
);
988 err
= udp_v6_push_pending_frames(sk
, up
);
991 ip6_dst_store(sk
, dst
,
992 !ipv6_addr_cmp(&fl
->fl6_dst
, &np
->daddr
) ?
995 err
= np
->recverr
? net_xmit_errno(err
) : 0;
998 fl6_sock_release(flowlabel
);
1000 UDP6_INC_STATS_USER(UdpOutDatagrams
);
1007 if (!(msg
->msg_flags
&MSG_PROBE
) || len
)
1008 goto back_from_confirm
;
1013 static int udpv6_destroy_sock(struct sock
*sk
)
1016 udp_v6_flush_pending_frames(sk
);
1019 inet6_destroy_sock(sk
);
1025 * Socket option code for UDP
1027 static int udpv6_setsockopt(struct sock
*sk
, int level
, int optname
,
1028 char *optval
, int optlen
)
1030 struct udp_opt
*up
= udp_sk(sk
);
1034 if (level
!= SOL_UDP
)
1035 return ipv6_setsockopt(sk
, level
, optname
, optval
, optlen
);
1037 if(optlen
<sizeof(int))
1040 if (get_user(val
, (int *)optval
))
1050 udp_v6_push_pending_frames(sk
, up
);
1056 up
->encap_type
= val
;
1067 static int udpv6_getsockopt(struct sock
*sk
, int level
, int optname
,
1068 char *optval
, int *optlen
)
1070 struct udp_opt
*up
= udp_sk(sk
);
1073 if (level
!= SOL_UDP
)
1074 return ipv6_getsockopt(sk
, level
, optname
, optval
, optlen
);
1076 if(get_user(len
,optlen
))
1079 len
= min_t(unsigned int, len
, sizeof(int));
1090 val
= up
->encap_type
;
1094 return -ENOPROTOOPT
;
1097 if(put_user(len
, optlen
))
1099 if(copy_to_user(optval
, &val
,len
))
1104 static struct inet6_protocol udpv6_protocol
= {
1105 .handler
= udpv6_rcv
,
1106 .err_handler
= udpv6_err
,
1107 .flags
= INET6_PROTO_NOPOLICY
|INET6_PROTO_FINAL
,
1110 /* ------------------------------------------------------------------------ */
1111 #ifdef CONFIG_PROC_FS
1113 static void udp6_sock_seq_show(struct seq_file
*seq
, struct sock
*sp
, int bucket
)
1115 struct inet_opt
*inet
= inet_sk(sp
);
1116 struct ipv6_pinfo
*np
= inet6_sk(sp
);
1117 struct in6_addr
*dest
, *src
;
1121 src
= &np
->rcv_saddr
;
1122 destp
= ntohs(inet
->dport
);
1123 srcp
= ntohs(inet
->sport
);
1125 "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
1126 "%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p\n",
1128 src
->s6_addr32
[0], src
->s6_addr32
[1],
1129 src
->s6_addr32
[2], src
->s6_addr32
[3], srcp
,
1130 dest
->s6_addr32
[0], dest
->s6_addr32
[1],
1131 dest
->s6_addr32
[2], dest
->s6_addr32
[3], destp
,
1133 atomic_read(&sp
->sk_wmem_alloc
),
1134 atomic_read(&sp
->sk_rmem_alloc
),
1138 atomic_read(&sp
->sk_refcnt
), sp
);
1141 static int udp6_seq_show(struct seq_file
*seq
, void *v
)
1143 if (v
== SEQ_START_TOKEN
)
1148 "st tx_queue rx_queue tr tm->when retrnsmt"
1149 " uid timeout inode\n");
1151 udp6_sock_seq_show(seq
, v
, ((struct udp_iter_state
*)seq
->private)->bucket
);
1155 static struct file_operations udp6_seq_fops
;
1156 static struct udp_seq_afinfo udp6_seq_afinfo
= {
1157 .owner
= THIS_MODULE
,
1160 .seq_show
= udp6_seq_show
,
1161 .seq_fops
= &udp6_seq_fops
,
1164 int __init
udp6_proc_init(void)
1166 return udp_proc_register(&udp6_seq_afinfo
);
1169 void udp6_proc_exit(void) {
1170 udp_proc_unregister(&udp6_seq_afinfo
);
1172 #endif /* CONFIG_PROC_FS */
1174 /* ------------------------------------------------------------------------ */
1176 struct proto udpv6_prot
= {
1178 .close
= udpv6_close
,
1179 .connect
= udpv6_connect
,
1180 .disconnect
= udp_disconnect
,
1182 .destroy
= udpv6_destroy_sock
,
1183 .setsockopt
= udpv6_setsockopt
,
1184 .getsockopt
= udpv6_getsockopt
,
1185 .sendmsg
= udpv6_sendmsg
,
1186 .recvmsg
= udpv6_recvmsg
,
1187 .backlog_rcv
= udpv6_queue_rcv_skb
,
1188 .hash
= udp_v6_hash
,
1189 .unhash
= udp_v6_unhash
,
1190 .get_port
= udp_v6_get_port
,
1193 extern struct proto_ops inet6_dgram_ops
;
1195 static struct inet_protosw udpv6_protosw
= {
1197 .protocol
= IPPROTO_UDP
,
1198 .prot
= &udpv6_prot
,
1199 .ops
= &inet6_dgram_ops
,
1201 .no_check
= UDP_CSUM_DEFAULT
,
1202 .flags
= INET_PROTOSW_PERMANENT
,
1206 void __init
udpv6_init(void)
1208 if (inet6_add_protocol(&udpv6_protocol
, IPPROTO_UDP
) < 0)
1209 printk(KERN_ERR
"udpv6_init: Could not register protocol\n");
1210 inet6_register_protosw(&udpv6_protosw
);