3 * Linux INET6 implementation
6 * Pedro Roque <roque@di.fc.ul.pt>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
14 #include <linux/capability.h>
15 #include <linux/errno.h>
16 #include <linux/types.h>
17 #include <linux/kernel.h>
18 #include <linux/interrupt.h>
19 #include <linux/socket.h>
20 #include <linux/sockios.h>
21 #include <linux/in6.h>
22 #include <linux/ipv6.h>
23 #include <linux/route.h>
24 #include <linux/slab.h>
25 #include <linux/export.h>
28 #include <net/ndisc.h>
29 #include <net/addrconf.h>
30 #include <net/transp_v6.h>
31 #include <net/ip6_route.h>
32 #include <net/tcp_states.h>
33 #include <net/dsfield.h>
35 #include <linux/errqueue.h>
36 #include <asm/uaccess.h>
38 static bool ipv6_mapped_addr_any(const struct in6_addr
*a
)
40 return ipv6_addr_v4mapped(a
) && (a
->s6_addr32
[3] == 0);
43 static void ip6_datagram_flow_key_init(struct flowi6
*fl6
, struct sock
*sk
)
45 struct inet_sock
*inet
= inet_sk(sk
);
46 struct ipv6_pinfo
*np
= inet6_sk(sk
);
48 memset(fl6
, 0, sizeof(*fl6
));
49 fl6
->flowi6_proto
= sk
->sk_protocol
;
50 fl6
->daddr
= sk
->sk_v6_daddr
;
51 fl6
->saddr
= np
->saddr
;
52 fl6
->flowi6_oif
= sk
->sk_bound_dev_if
;
53 fl6
->flowi6_mark
= sk
->sk_mark
;
54 fl6
->fl6_dport
= inet
->inet_dport
;
55 fl6
->fl6_sport
= inet
->inet_sport
;
56 fl6
->flowlabel
= np
->flow_label
;
59 fl6
->flowi6_oif
= np
->sticky_pktinfo
.ipi6_ifindex
;
61 if (!fl6
->flowi6_oif
&& ipv6_addr_is_multicast(&fl6
->daddr
))
62 fl6
->flowi6_oif
= np
->mcast_oif
;
64 security_sk_classify_flow(sk
, flowi6_to_flowi(fl6
));
67 int ip6_datagram_dst_update(struct sock
*sk
, bool fix_sk_saddr
)
69 struct ip6_flowlabel
*flowlabel
= NULL
;
70 struct in6_addr
*final_p
, final
;
71 struct ipv6_txoptions
*opt
;
72 struct dst_entry
*dst
;
73 struct inet_sock
*inet
= inet_sk(sk
);
74 struct ipv6_pinfo
*np
= inet6_sk(sk
);
78 if (np
->sndflow
&& (np
->flow_label
& IPV6_FLOWLABEL_MASK
)) {
79 flowlabel
= fl6_sock_lookup(sk
, np
->flow_label
);
83 ip6_datagram_flow_key_init(&fl6
, sk
);
86 opt
= flowlabel
? flowlabel
->opt
: rcu_dereference(np
->opt
);
87 final_p
= fl6_update_dst(&fl6
, opt
, &final
);
90 dst
= ip6_dst_lookup_flow(sk
, &fl6
, final_p
);
97 if (ipv6_addr_any(&np
->saddr
))
98 np
->saddr
= fl6
.saddr
;
100 if (ipv6_addr_any(&sk
->sk_v6_rcv_saddr
)) {
101 sk
->sk_v6_rcv_saddr
= fl6
.saddr
;
102 inet
->inet_rcv_saddr
= LOOPBACK4_IPV6
;
103 if (sk
->sk_prot
->rehash
)
104 sk
->sk_prot
->rehash(sk
);
108 ip6_dst_store(sk
, dst
,
109 ipv6_addr_equal(&fl6
.daddr
, &sk
->sk_v6_daddr
) ?
110 &sk
->sk_v6_daddr
: NULL
,
111 #ifdef CONFIG_IPV6_SUBTREES
112 ipv6_addr_equal(&fl6
.saddr
, &np
->saddr
) ?
118 fl6_sock_release(flowlabel
);
122 void ip6_datagram_release_cb(struct sock
*sk
)
124 struct dst_entry
*dst
;
126 if (ipv6_addr_v4mapped(&sk
->sk_v6_daddr
))
130 dst
= __sk_dst_get(sk
);
131 if (!dst
|| !dst
->obsolete
||
132 dst
->ops
->check(dst
, inet6_sk(sk
)->dst_cookie
)) {
138 ip6_datagram_dst_update(sk
, false);
140 EXPORT_SYMBOL_GPL(ip6_datagram_release_cb
);
142 static int __ip6_datagram_connect(struct sock
*sk
, struct sockaddr
*uaddr
, int addr_len
)
144 struct sockaddr_in6
*usin
= (struct sockaddr_in6
*) uaddr
;
145 struct inet_sock
*inet
= inet_sk(sk
);
146 struct ipv6_pinfo
*np
= inet6_sk(sk
);
147 struct in6_addr
*daddr
;
150 __be32 fl6_flowlabel
= 0;
152 if (usin
->sin6_family
== AF_INET
) {
153 if (__ipv6_only_sock(sk
))
154 return -EAFNOSUPPORT
;
155 err
= __ip4_datagram_connect(sk
, uaddr
, addr_len
);
159 if (addr_len
< SIN6_LEN_RFC2133
)
162 if (usin
->sin6_family
!= AF_INET6
)
163 return -EAFNOSUPPORT
;
166 fl6_flowlabel
= usin
->sin6_flowinfo
& IPV6_FLOWINFO_MASK
;
168 addr_type
= ipv6_addr_type(&usin
->sin6_addr
);
170 if (addr_type
== IPV6_ADDR_ANY
) {
174 usin
->sin6_addr
.s6_addr
[15] = 0x01;
177 daddr
= &usin
->sin6_addr
;
179 if (addr_type
== IPV6_ADDR_MAPPED
) {
180 struct sockaddr_in sin
;
182 if (__ipv6_only_sock(sk
)) {
186 sin
.sin_family
= AF_INET
;
187 sin
.sin_addr
.s_addr
= daddr
->s6_addr32
[3];
188 sin
.sin_port
= usin
->sin6_port
;
190 err
= __ip4_datagram_connect(sk
,
191 (struct sockaddr
*) &sin
,
198 ipv6_addr_set_v4mapped(inet
->inet_daddr
, &sk
->sk_v6_daddr
);
200 if (ipv6_addr_any(&np
->saddr
) ||
201 ipv6_mapped_addr_any(&np
->saddr
))
202 ipv6_addr_set_v4mapped(inet
->inet_saddr
, &np
->saddr
);
204 if (ipv6_addr_any(&sk
->sk_v6_rcv_saddr
) ||
205 ipv6_mapped_addr_any(&sk
->sk_v6_rcv_saddr
)) {
206 ipv6_addr_set_v4mapped(inet
->inet_rcv_saddr
,
207 &sk
->sk_v6_rcv_saddr
);
208 if (sk
->sk_prot
->rehash
)
209 sk
->sk_prot
->rehash(sk
);
215 if (__ipv6_addr_needs_scope_id(addr_type
)) {
216 if (addr_len
>= sizeof(struct sockaddr_in6
) &&
217 usin
->sin6_scope_id
) {
218 if (sk
->sk_bound_dev_if
&&
219 sk
->sk_bound_dev_if
!= usin
->sin6_scope_id
) {
223 sk
->sk_bound_dev_if
= usin
->sin6_scope_id
;
226 if (!sk
->sk_bound_dev_if
&& (addr_type
& IPV6_ADDR_MULTICAST
))
227 sk
->sk_bound_dev_if
= np
->mcast_oif
;
229 /* Connect to link-local address requires an interface */
230 if (!sk
->sk_bound_dev_if
) {
236 sk
->sk_v6_daddr
= *daddr
;
237 np
->flow_label
= fl6_flowlabel
;
239 inet
->inet_dport
= usin
->sin6_port
;
242 * Check for a route to destination an obtain the
243 * destination cache for it.
246 err
= ip6_datagram_dst_update(sk
, true);
250 sk
->sk_state
= TCP_ESTABLISHED
;
256 int ip6_datagram_connect(struct sock
*sk
, struct sockaddr
*uaddr
, int addr_len
)
261 res
= __ip6_datagram_connect(sk
, uaddr
, addr_len
);
265 EXPORT_SYMBOL_GPL(ip6_datagram_connect
);
267 int ip6_datagram_connect_v6_only(struct sock
*sk
, struct sockaddr
*uaddr
,
270 DECLARE_SOCKADDR(struct sockaddr_in6
*, sin6
, uaddr
);
271 if (sin6
->sin6_family
!= AF_INET6
)
272 return -EAFNOSUPPORT
;
273 return ip6_datagram_connect(sk
, uaddr
, addr_len
);
275 EXPORT_SYMBOL_GPL(ip6_datagram_connect_v6_only
);
277 void ipv6_icmp_error(struct sock
*sk
, struct sk_buff
*skb
, int err
,
278 __be16 port
, u32 info
, u8
*payload
)
280 struct ipv6_pinfo
*np
= inet6_sk(sk
);
281 struct icmp6hdr
*icmph
= icmp6_hdr(skb
);
282 struct sock_exterr_skb
*serr
;
287 skb
= skb_clone(skb
, GFP_ATOMIC
);
291 skb
->protocol
= htons(ETH_P_IPV6
);
293 serr
= SKB_EXT_ERR(skb
);
294 serr
->ee
.ee_errno
= err
;
295 serr
->ee
.ee_origin
= SO_EE_ORIGIN_ICMP6
;
296 serr
->ee
.ee_type
= icmph
->icmp6_type
;
297 serr
->ee
.ee_code
= icmph
->icmp6_code
;
299 serr
->ee
.ee_info
= info
;
300 serr
->ee
.ee_data
= 0;
301 serr
->addr_offset
= (u8
*)&(((struct ipv6hdr
*)(icmph
+ 1))->daddr
) -
302 skb_network_header(skb
);
305 __skb_pull(skb
, payload
- skb
->data
);
306 skb_reset_transport_header(skb
);
308 if (sock_queue_err_skb(sk
, skb
))
312 void ipv6_local_error(struct sock
*sk
, int err
, struct flowi6
*fl6
, u32 info
)
314 const struct ipv6_pinfo
*np
= inet6_sk(sk
);
315 struct sock_exterr_skb
*serr
;
322 skb
= alloc_skb(sizeof(struct ipv6hdr
), GFP_ATOMIC
);
326 skb
->protocol
= htons(ETH_P_IPV6
);
328 skb_put(skb
, sizeof(struct ipv6hdr
));
329 skb_reset_network_header(skb
);
331 iph
->daddr
= fl6
->daddr
;
333 serr
= SKB_EXT_ERR(skb
);
334 serr
->ee
.ee_errno
= err
;
335 serr
->ee
.ee_origin
= SO_EE_ORIGIN_LOCAL
;
336 serr
->ee
.ee_type
= 0;
337 serr
->ee
.ee_code
= 0;
339 serr
->ee
.ee_info
= info
;
340 serr
->ee
.ee_data
= 0;
341 serr
->addr_offset
= (u8
*)&iph
->daddr
- skb_network_header(skb
);
342 serr
->port
= fl6
->fl6_dport
;
344 __skb_pull(skb
, skb_tail_pointer(skb
) - skb
->data
);
345 skb_reset_transport_header(skb
);
347 if (sock_queue_err_skb(sk
, skb
))
351 void ipv6_local_rxpmtu(struct sock
*sk
, struct flowi6
*fl6
, u32 mtu
)
353 struct ipv6_pinfo
*np
= inet6_sk(sk
);
356 struct ip6_mtuinfo
*mtu_info
;
358 if (!np
->rxopt
.bits
.rxpmtu
)
361 skb
= alloc_skb(sizeof(struct ipv6hdr
), GFP_ATOMIC
);
365 skb_put(skb
, sizeof(struct ipv6hdr
));
366 skb_reset_network_header(skb
);
368 iph
->daddr
= fl6
->daddr
;
370 mtu_info
= IP6CBMTU(skb
);
372 mtu_info
->ip6m_mtu
= mtu
;
373 mtu_info
->ip6m_addr
.sin6_family
= AF_INET6
;
374 mtu_info
->ip6m_addr
.sin6_port
= 0;
375 mtu_info
->ip6m_addr
.sin6_flowinfo
= 0;
376 mtu_info
->ip6m_addr
.sin6_scope_id
= fl6
->flowi6_oif
;
377 mtu_info
->ip6m_addr
.sin6_addr
= ipv6_hdr(skb
)->daddr
;
379 __skb_pull(skb
, skb_tail_pointer(skb
) - skb
->data
);
380 skb_reset_transport_header(skb
);
382 skb
= xchg(&np
->rxpmtu
, skb
);
386 /* For some errors we have valid addr_offset even with zero payload and
387 * zero port. Also, addr_offset should be supported if port is set.
389 static inline bool ipv6_datagram_support_addr(struct sock_exterr_skb
*serr
)
391 return serr
->ee
.ee_origin
== SO_EE_ORIGIN_ICMP6
||
392 serr
->ee
.ee_origin
== SO_EE_ORIGIN_ICMP
||
393 serr
->ee
.ee_origin
== SO_EE_ORIGIN_LOCAL
|| serr
->port
;
396 /* IPv6 supports cmsg on all origins aside from SO_EE_ORIGIN_LOCAL.
398 * At one point, excluding local errors was a quick test to identify icmp/icmp6
399 * errors. This is no longer true, but the test remained, so the v6 stack,
400 * unlike v4, also honors cmsg requests on all wifi and timestamp errors.
402 * Timestamp code paths do not initialize the fields expected by cmsg:
403 * the PKTINFO fields in skb->cb[]. Fill those in here.
405 static bool ip6_datagram_support_cmsg(struct sk_buff
*skb
,
406 struct sock_exterr_skb
*serr
)
408 if (serr
->ee
.ee_origin
== SO_EE_ORIGIN_ICMP
||
409 serr
->ee
.ee_origin
== SO_EE_ORIGIN_ICMP6
)
412 if (serr
->ee
.ee_origin
== SO_EE_ORIGIN_LOCAL
)
418 if (skb
->protocol
== htons(ETH_P_IPV6
))
419 IP6CB(skb
)->iif
= skb
->dev
->ifindex
;
421 PKTINFO_SKB_CB(skb
)->ipi_ifindex
= skb
->dev
->ifindex
;
427 * Handle MSG_ERRQUEUE
429 int ipv6_recv_error(struct sock
*sk
, struct msghdr
*msg
, int len
, int *addr_len
)
431 struct ipv6_pinfo
*np
= inet6_sk(sk
);
432 struct sock_exterr_skb
*serr
;
434 DECLARE_SOCKADDR(struct sockaddr_in6
*, sin
, msg
->msg_name
);
436 struct sock_extended_err ee
;
437 struct sockaddr_in6 offender
;
443 skb
= sock_dequeue_err_skb(sk
);
449 msg
->msg_flags
|= MSG_TRUNC
;
452 err
= skb_copy_datagram_msg(skb
, 0, msg
, copied
);
457 sock_recv_timestamp(msg
, sk
, skb
);
459 serr
= SKB_EXT_ERR(skb
);
461 if (sin
&& ipv6_datagram_support_addr(serr
)) {
462 const unsigned char *nh
= skb_network_header(skb
);
463 sin
->sin6_family
= AF_INET6
;
464 sin
->sin6_flowinfo
= 0;
465 sin
->sin6_port
= serr
->port
;
466 if (skb
->protocol
== htons(ETH_P_IPV6
)) {
467 const struct ipv6hdr
*ip6h
= container_of((struct in6_addr
*)(nh
+ serr
->addr_offset
),
468 struct ipv6hdr
, daddr
);
469 sin
->sin6_addr
= ip6h
->daddr
;
471 sin
->sin6_flowinfo
= ip6_flowinfo(ip6h
);
473 ipv6_iface_scope_id(&sin
->sin6_addr
,
476 ipv6_addr_set_v4mapped(*(__be32
*)(nh
+ serr
->addr_offset
),
478 sin
->sin6_scope_id
= 0;
480 *addr_len
= sizeof(*sin
);
483 memcpy(&errhdr
.ee
, &serr
->ee
, sizeof(struct sock_extended_err
));
484 sin
= &errhdr
.offender
;
485 memset(sin
, 0, sizeof(*sin
));
487 if (ip6_datagram_support_cmsg(skb
, serr
)) {
488 sin
->sin6_family
= AF_INET6
;
490 ip6_datagram_recv_common_ctl(sk
, msg
, skb
);
491 if (skb
->protocol
== htons(ETH_P_IPV6
)) {
492 sin
->sin6_addr
= ipv6_hdr(skb
)->saddr
;
494 ip6_datagram_recv_specific_ctl(sk
, msg
, skb
);
496 ipv6_iface_scope_id(&sin
->sin6_addr
,
499 ipv6_addr_set_v4mapped(ip_hdr(skb
)->saddr
,
501 if (inet_sk(sk
)->cmsg_flags
)
502 ip_cmsg_recv(msg
, skb
);
506 put_cmsg(msg
, SOL_IPV6
, IPV6_RECVERR
, sizeof(errhdr
), &errhdr
);
508 /* Now we could try to dump offended packet options */
510 msg
->msg_flags
|= MSG_ERRQUEUE
;
517 EXPORT_SYMBOL_GPL(ipv6_recv_error
);
520 * Handle IPV6_RECVPATHMTU
522 int ipv6_recv_rxpmtu(struct sock
*sk
, struct msghdr
*msg
, int len
,
525 struct ipv6_pinfo
*np
= inet6_sk(sk
);
527 struct ip6_mtuinfo mtu_info
;
528 DECLARE_SOCKADDR(struct sockaddr_in6
*, sin
, msg
->msg_name
);
533 skb
= xchg(&np
->rxpmtu
, NULL
);
539 msg
->msg_flags
|= MSG_TRUNC
;
542 err
= skb_copy_datagram_msg(skb
, 0, msg
, copied
);
546 sock_recv_timestamp(msg
, sk
, skb
);
548 memcpy(&mtu_info
, IP6CBMTU(skb
), sizeof(mtu_info
));
551 sin
->sin6_family
= AF_INET6
;
552 sin
->sin6_flowinfo
= 0;
554 sin
->sin6_scope_id
= mtu_info
.ip6m_addr
.sin6_scope_id
;
555 sin
->sin6_addr
= mtu_info
.ip6m_addr
.sin6_addr
;
556 *addr_len
= sizeof(*sin
);
559 put_cmsg(msg
, SOL_IPV6
, IPV6_PATHMTU
, sizeof(mtu_info
), &mtu_info
);
570 void ip6_datagram_recv_common_ctl(struct sock
*sk
, struct msghdr
*msg
,
573 struct ipv6_pinfo
*np
= inet6_sk(sk
);
574 bool is_ipv6
= skb
->protocol
== htons(ETH_P_IPV6
);
576 if (np
->rxopt
.bits
.rxinfo
) {
577 struct in6_pktinfo src_info
;
580 src_info
.ipi6_ifindex
= IP6CB(skb
)->iif
;
581 src_info
.ipi6_addr
= ipv6_hdr(skb
)->daddr
;
583 src_info
.ipi6_ifindex
=
584 PKTINFO_SKB_CB(skb
)->ipi_ifindex
;
585 ipv6_addr_set_v4mapped(ip_hdr(skb
)->daddr
,
586 &src_info
.ipi6_addr
);
589 if (src_info
.ipi6_ifindex
>= 0)
590 put_cmsg(msg
, SOL_IPV6
, IPV6_PKTINFO
,
591 sizeof(src_info
), &src_info
);
595 void ip6_datagram_recv_specific_ctl(struct sock
*sk
, struct msghdr
*msg
,
598 struct ipv6_pinfo
*np
= inet6_sk(sk
);
599 struct inet6_skb_parm
*opt
= IP6CB(skb
);
600 unsigned char *nh
= skb_network_header(skb
);
602 if (np
->rxopt
.bits
.rxhlim
) {
603 int hlim
= ipv6_hdr(skb
)->hop_limit
;
604 put_cmsg(msg
, SOL_IPV6
, IPV6_HOPLIMIT
, sizeof(hlim
), &hlim
);
607 if (np
->rxopt
.bits
.rxtclass
) {
608 int tclass
= ipv6_get_dsfield(ipv6_hdr(skb
));
609 put_cmsg(msg
, SOL_IPV6
, IPV6_TCLASS
, sizeof(tclass
), &tclass
);
612 if (np
->rxopt
.bits
.rxflow
) {
613 __be32 flowinfo
= ip6_flowinfo((struct ipv6hdr
*)nh
);
615 put_cmsg(msg
, SOL_IPV6
, IPV6_FLOWINFO
, sizeof(flowinfo
), &flowinfo
);
618 /* HbH is allowed only once */
619 if (np
->rxopt
.bits
.hopopts
&& (opt
->flags
& IP6SKB_HOPBYHOP
)) {
620 u8
*ptr
= nh
+ sizeof(struct ipv6hdr
);
621 put_cmsg(msg
, SOL_IPV6
, IPV6_HOPOPTS
, (ptr
[1]+1)<<3, ptr
);
625 (np
->rxopt
.bits
.dstopts
|| np
->rxopt
.bits
.srcrt
)) {
627 * Silly enough, but we need to reparse in order to
628 * report extension headers (except for HbH)
631 * Also note that IPV6_RECVRTHDRDSTOPTS is NOT
632 * (and WILL NOT be) defined because
633 * IPV6_RECVDSTOPTS is more generic. --yoshfuji
635 unsigned int off
= sizeof(struct ipv6hdr
);
636 u8 nexthdr
= ipv6_hdr(skb
)->nexthdr
;
638 while (off
<= opt
->lastopt
) {
643 case IPPROTO_DSTOPTS
:
645 len
= (ptr
[1] + 1) << 3;
646 if (np
->rxopt
.bits
.dstopts
)
647 put_cmsg(msg
, SOL_IPV6
, IPV6_DSTOPTS
, len
, ptr
);
649 case IPPROTO_ROUTING
:
651 len
= (ptr
[1] + 1) << 3;
652 if (np
->rxopt
.bits
.srcrt
)
653 put_cmsg(msg
, SOL_IPV6
, IPV6_RTHDR
, len
, ptr
);
657 len
= (ptr
[1] + 2) << 2;
661 len
= (ptr
[1] + 1) << 3;
669 /* socket options in old style */
670 if (np
->rxopt
.bits
.rxoinfo
) {
671 struct in6_pktinfo src_info
;
673 src_info
.ipi6_ifindex
= opt
->iif
;
674 src_info
.ipi6_addr
= ipv6_hdr(skb
)->daddr
;
675 put_cmsg(msg
, SOL_IPV6
, IPV6_2292PKTINFO
, sizeof(src_info
), &src_info
);
677 if (np
->rxopt
.bits
.rxohlim
) {
678 int hlim
= ipv6_hdr(skb
)->hop_limit
;
679 put_cmsg(msg
, SOL_IPV6
, IPV6_2292HOPLIMIT
, sizeof(hlim
), &hlim
);
681 if (np
->rxopt
.bits
.ohopopts
&& (opt
->flags
& IP6SKB_HOPBYHOP
)) {
682 u8
*ptr
= nh
+ sizeof(struct ipv6hdr
);
683 put_cmsg(msg
, SOL_IPV6
, IPV6_2292HOPOPTS
, (ptr
[1]+1)<<3, ptr
);
685 if (np
->rxopt
.bits
.odstopts
&& opt
->dst0
) {
686 u8
*ptr
= nh
+ opt
->dst0
;
687 put_cmsg(msg
, SOL_IPV6
, IPV6_2292DSTOPTS
, (ptr
[1]+1)<<3, ptr
);
689 if (np
->rxopt
.bits
.osrcrt
&& opt
->srcrt
) {
690 struct ipv6_rt_hdr
*rthdr
= (struct ipv6_rt_hdr
*)(nh
+ opt
->srcrt
);
691 put_cmsg(msg
, SOL_IPV6
, IPV6_2292RTHDR
, (rthdr
->hdrlen
+1) << 3, rthdr
);
693 if (np
->rxopt
.bits
.odstopts
&& opt
->dst1
) {
694 u8
*ptr
= nh
+ opt
->dst1
;
695 put_cmsg(msg
, SOL_IPV6
, IPV6_2292DSTOPTS
, (ptr
[1]+1)<<3, ptr
);
697 if (np
->rxopt
.bits
.rxorigdstaddr
) {
698 struct sockaddr_in6 sin6
;
699 __be16
*ports
= (__be16
*) skb_transport_header(skb
);
701 if (skb_transport_offset(skb
) + 4 <= skb
->len
) {
702 /* All current transport protocols have the port numbers in the
703 * first four bytes of the transport header and this function is
704 * written with this assumption in mind.
707 sin6
.sin6_family
= AF_INET6
;
708 sin6
.sin6_addr
= ipv6_hdr(skb
)->daddr
;
709 sin6
.sin6_port
= ports
[1];
710 sin6
.sin6_flowinfo
= 0;
712 ipv6_iface_scope_id(&ipv6_hdr(skb
)->daddr
,
715 put_cmsg(msg
, SOL_IPV6
, IPV6_ORIGDSTADDR
, sizeof(sin6
), &sin6
);
720 void ip6_datagram_recv_ctl(struct sock
*sk
, struct msghdr
*msg
,
723 ip6_datagram_recv_common_ctl(sk
, msg
, skb
);
724 ip6_datagram_recv_specific_ctl(sk
, msg
, skb
);
726 EXPORT_SYMBOL_GPL(ip6_datagram_recv_ctl
);
728 int ip6_datagram_send_ctl(struct net
*net
, struct sock
*sk
,
729 struct msghdr
*msg
, struct flowi6
*fl6
,
730 struct ipcm6_cookie
*ipc6
, struct sockcm_cookie
*sockc
)
732 struct in6_pktinfo
*src_info
;
733 struct cmsghdr
*cmsg
;
734 struct ipv6_rt_hdr
*rthdr
;
735 struct ipv6_opt_hdr
*hdr
;
736 struct ipv6_txoptions
*opt
= ipc6
->opt
;
740 for_each_cmsghdr(cmsg
, msg
) {
743 if (!CMSG_OK(msg
, cmsg
)) {
748 if (cmsg
->cmsg_level
== SOL_SOCKET
) {
749 err
= __sock_cmsg_send(sk
, msg
, cmsg
, sockc
);
755 if (cmsg
->cmsg_level
!= SOL_IPV6
)
758 switch (cmsg
->cmsg_type
) {
760 case IPV6_2292PKTINFO
:
762 struct net_device
*dev
= NULL
;
764 if (cmsg
->cmsg_len
< CMSG_LEN(sizeof(struct in6_pktinfo
))) {
769 src_info
= (struct in6_pktinfo
*)CMSG_DATA(cmsg
);
771 if (src_info
->ipi6_ifindex
) {
772 if (fl6
->flowi6_oif
&&
773 src_info
->ipi6_ifindex
!= fl6
->flowi6_oif
)
775 fl6
->flowi6_oif
= src_info
->ipi6_ifindex
;
778 addr_type
= __ipv6_addr_type(&src_info
->ipi6_addr
);
781 if (fl6
->flowi6_oif
) {
782 dev
= dev_get_by_index_rcu(net
, fl6
->flowi6_oif
);
787 } else if (addr_type
& IPV6_ADDR_LINKLOCAL
) {
792 if (addr_type
!= IPV6_ADDR_ANY
) {
793 int strict
= __ipv6_addr_src_scope(addr_type
) <= IPV6_ADDR_SCOPE_LINKLOCAL
;
794 if (!(inet_sk(sk
)->freebind
|| inet_sk(sk
)->transparent
) &&
795 !ipv6_chk_addr(net
, &src_info
->ipi6_addr
,
796 strict
? dev
: NULL
, 0) &&
797 !ipv6_chk_acast_addr_src(net
, dev
,
798 &src_info
->ipi6_addr
))
801 fl6
->saddr
= src_info
->ipi6_addr
;
813 if (cmsg
->cmsg_len
< CMSG_LEN(4)) {
818 if (fl6
->flowlabel
&IPV6_FLOWINFO_MASK
) {
819 if ((fl6
->flowlabel
^*(__be32
*)CMSG_DATA(cmsg
))&~IPV6_FLOWINFO_MASK
) {
824 fl6
->flowlabel
= IPV6_FLOWINFO_MASK
& *(__be32
*)CMSG_DATA(cmsg
);
827 case IPV6_2292HOPOPTS
:
829 if (opt
->hopopt
|| cmsg
->cmsg_len
< CMSG_LEN(sizeof(struct ipv6_opt_hdr
))) {
834 hdr
= (struct ipv6_opt_hdr
*)CMSG_DATA(cmsg
);
835 len
= ((hdr
->hdrlen
+ 1) << 3);
836 if (cmsg
->cmsg_len
< CMSG_LEN(len
)) {
840 if (!ns_capable(net
->user_ns
, CAP_NET_RAW
)) {
844 opt
->opt_nflen
+= len
;
848 case IPV6_2292DSTOPTS
:
849 if (cmsg
->cmsg_len
< CMSG_LEN(sizeof(struct ipv6_opt_hdr
))) {
854 hdr
= (struct ipv6_opt_hdr
*)CMSG_DATA(cmsg
);
855 len
= ((hdr
->hdrlen
+ 1) << 3);
856 if (cmsg
->cmsg_len
< CMSG_LEN(len
)) {
860 if (!ns_capable(net
->user_ns
, CAP_NET_RAW
)) {
868 opt
->opt_flen
+= len
;
873 case IPV6_RTHDRDSTOPTS
:
874 if (cmsg
->cmsg_len
< CMSG_LEN(sizeof(struct ipv6_opt_hdr
))) {
879 hdr
= (struct ipv6_opt_hdr
*)CMSG_DATA(cmsg
);
880 len
= ((hdr
->hdrlen
+ 1) << 3);
881 if (cmsg
->cmsg_len
< CMSG_LEN(len
)) {
885 if (!ns_capable(net
->user_ns
, CAP_NET_RAW
)) {
889 if (cmsg
->cmsg_type
== IPV6_DSTOPTS
) {
890 opt
->opt_flen
+= len
;
893 opt
->opt_nflen
+= len
;
900 if (cmsg
->cmsg_len
< CMSG_LEN(sizeof(struct ipv6_rt_hdr
))) {
905 rthdr
= (struct ipv6_rt_hdr
*)CMSG_DATA(cmsg
);
907 switch (rthdr
->type
) {
908 #if IS_ENABLED(CONFIG_IPV6_MIP6)
909 case IPV6_SRCRT_TYPE_2
:
910 if (rthdr
->hdrlen
!= 2 ||
911 rthdr
->segments_left
!= 1) {
922 len
= ((rthdr
->hdrlen
+ 1) << 3);
924 if (cmsg
->cmsg_len
< CMSG_LEN(len
)) {
929 /* segments left must also match */
930 if ((rthdr
->hdrlen
>> 1) != rthdr
->segments_left
) {
935 opt
->opt_nflen
+= len
;
938 if (cmsg
->cmsg_type
== IPV6_2292RTHDR
&& opt
->dst1opt
) {
939 int dsthdrlen
= ((opt
->dst1opt
->hdrlen
+1)<<3);
941 opt
->opt_nflen
+= dsthdrlen
;
942 opt
->dst0opt
= opt
->dst1opt
;
944 opt
->opt_flen
-= dsthdrlen
;
949 case IPV6_2292HOPLIMIT
:
951 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(int))) {
956 ipc6
->hlimit
= *(int *)CMSG_DATA(cmsg
);
957 if (ipc6
->hlimit
< -1 || ipc6
->hlimit
> 0xff) {
969 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(int)))
972 tc
= *(int *)CMSG_DATA(cmsg
);
973 if (tc
< -1 || tc
> 0xff)
987 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(int)))
990 df
= *(int *)CMSG_DATA(cmsg
);
991 if (df
< 0 || df
> 1)
1000 net_dbg_ratelimited("invalid cmsg type: %d\n",
1010 EXPORT_SYMBOL_GPL(ip6_datagram_send_ctl
);
1012 void ip6_dgram_sock_seq_show(struct seq_file
*seq
, struct sock
*sp
,
1013 __u16 srcp
, __u16 destp
, int bucket
)
1015 const struct in6_addr
*dest
, *src
;
1017 dest
= &sp
->sk_v6_daddr
;
1018 src
= &sp
->sk_v6_rcv_saddr
;
1020 "%5d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
1021 "%02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %d\n",
1023 src
->s6_addr32
[0], src
->s6_addr32
[1],
1024 src
->s6_addr32
[2], src
->s6_addr32
[3], srcp
,
1025 dest
->s6_addr32
[0], dest
->s6_addr32
[1],
1026 dest
->s6_addr32
[2], dest
->s6_addr32
[3], destp
,
1028 sk_wmem_alloc_get(sp
),
1029 sk_rmem_alloc_get(sp
),
1031 from_kuid_munged(seq_user_ns(seq
), sock_i_uid(sp
)),
1034 atomic_read(&sp
->sk_refcnt
), sp
,
1035 atomic_read(&sp
->sk_drops
));