1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Linux INET6 implementation
7 * Pedro Roque <roque@di.fc.ul.pt>
10 #include <linux/capability.h>
11 #include <linux/errno.h>
12 #include <linux/types.h>
13 #include <linux/kernel.h>
14 #include <linux/interrupt.h>
15 #include <linux/socket.h>
16 #include <linux/sockios.h>
17 #include <linux/in6.h>
18 #include <linux/ipv6.h>
19 #include <linux/route.h>
20 #include <linux/slab.h>
21 #include <linux/export.h>
24 #include <net/ndisc.h>
25 #include <net/addrconf.h>
26 #include <net/transp_v6.h>
27 #include <net/ip6_route.h>
28 #include <net/tcp_states.h>
29 #include <net/dsfield.h>
30 #include <net/sock_reuseport.h>
32 #include <linux/errqueue.h>
33 #include <linux/uaccess.h>
35 static bool ipv6_mapped_addr_any(const struct in6_addr
*a
)
37 return ipv6_addr_v4mapped(a
) && (a
->s6_addr32
[3] == 0);
40 static void ip6_datagram_flow_key_init(struct flowi6
*fl6
, struct sock
*sk
)
42 struct inet_sock
*inet
= inet_sk(sk
);
43 struct ipv6_pinfo
*np
= inet6_sk(sk
);
45 memset(fl6
, 0, sizeof(*fl6
));
46 fl6
->flowi6_proto
= sk
->sk_protocol
;
47 fl6
->daddr
= sk
->sk_v6_daddr
;
48 fl6
->saddr
= np
->saddr
;
49 fl6
->flowi6_oif
= sk
->sk_bound_dev_if
;
50 fl6
->flowi6_mark
= sk
->sk_mark
;
51 fl6
->fl6_dport
= inet
->inet_dport
;
52 fl6
->fl6_sport
= inet
->inet_sport
;
53 fl6
->flowlabel
= np
->flow_label
;
54 fl6
->flowi6_uid
= sk
->sk_uid
;
57 fl6
->flowi6_oif
= np
->sticky_pktinfo
.ipi6_ifindex
;
59 if (!fl6
->flowi6_oif
&& ipv6_addr_is_multicast(&fl6
->daddr
))
60 fl6
->flowi6_oif
= np
->mcast_oif
;
62 security_sk_classify_flow(sk
, flowi6_to_flowi(fl6
));
65 int ip6_datagram_dst_update(struct sock
*sk
, bool fix_sk_saddr
)
67 struct ip6_flowlabel
*flowlabel
= NULL
;
68 struct in6_addr
*final_p
, final
;
69 struct ipv6_txoptions
*opt
;
70 struct dst_entry
*dst
;
71 struct inet_sock
*inet
= inet_sk(sk
);
72 struct ipv6_pinfo
*np
= inet6_sk(sk
);
76 if (np
->sndflow
&& (np
->flow_label
& IPV6_FLOWLABEL_MASK
)) {
77 flowlabel
= fl6_sock_lookup(sk
, np
->flow_label
);
78 if (IS_ERR(flowlabel
))
81 ip6_datagram_flow_key_init(&fl6
, sk
);
84 opt
= flowlabel
? flowlabel
->opt
: rcu_dereference(np
->opt
);
85 final_p
= fl6_update_dst(&fl6
, opt
, &final
);
88 dst
= ip6_dst_lookup_flow(sock_net(sk
), sk
, &fl6
, final_p
);
95 if (ipv6_addr_any(&np
->saddr
))
96 np
->saddr
= fl6
.saddr
;
98 if (ipv6_addr_any(&sk
->sk_v6_rcv_saddr
)) {
99 sk
->sk_v6_rcv_saddr
= fl6
.saddr
;
100 inet
->inet_rcv_saddr
= LOOPBACK4_IPV6
;
101 if (sk
->sk_prot
->rehash
)
102 sk
->sk_prot
->rehash(sk
);
106 ip6_sk_dst_store_flow(sk
, dst
, &fl6
);
109 fl6_sock_release(flowlabel
);
113 void ip6_datagram_release_cb(struct sock
*sk
)
115 struct dst_entry
*dst
;
117 if (ipv6_addr_v4mapped(&sk
->sk_v6_daddr
))
121 dst
= __sk_dst_get(sk
);
122 if (!dst
|| !dst
->obsolete
||
123 dst
->ops
->check(dst
, inet6_sk(sk
)->dst_cookie
)) {
129 ip6_datagram_dst_update(sk
, false);
131 EXPORT_SYMBOL_GPL(ip6_datagram_release_cb
);
133 int __ip6_datagram_connect(struct sock
*sk
, struct sockaddr
*uaddr
,
136 struct sockaddr_in6
*usin
= (struct sockaddr_in6
*) uaddr
;
137 struct inet_sock
*inet
= inet_sk(sk
);
138 struct ipv6_pinfo
*np
= inet6_sk(sk
);
139 struct in6_addr
*daddr
, old_daddr
;
140 __be32 fl6_flowlabel
= 0;
141 __be32 old_fl6_flowlabel
;
146 if (usin
->sin6_family
== AF_INET
) {
147 if (__ipv6_only_sock(sk
))
148 return -EAFNOSUPPORT
;
149 err
= __ip4_datagram_connect(sk
, uaddr
, addr_len
);
153 if (addr_len
< SIN6_LEN_RFC2133
)
156 if (usin
->sin6_family
!= AF_INET6
)
157 return -EAFNOSUPPORT
;
160 fl6_flowlabel
= usin
->sin6_flowinfo
& IPV6_FLOWINFO_MASK
;
162 if (ipv6_addr_any(&usin
->sin6_addr
)) {
166 if (ipv6_addr_v4mapped(&sk
->sk_v6_rcv_saddr
))
167 ipv6_addr_set_v4mapped(htonl(INADDR_LOOPBACK
),
170 usin
->sin6_addr
= in6addr_loopback
;
173 addr_type
= ipv6_addr_type(&usin
->sin6_addr
);
175 daddr
= &usin
->sin6_addr
;
177 if (addr_type
& IPV6_ADDR_MAPPED
) {
178 struct sockaddr_in sin
;
180 if (__ipv6_only_sock(sk
)) {
184 sin
.sin_family
= AF_INET
;
185 sin
.sin_addr
.s_addr
= daddr
->s6_addr32
[3];
186 sin
.sin_port
= usin
->sin6_port
;
188 err
= __ip4_datagram_connect(sk
,
189 (struct sockaddr
*) &sin
,
196 ipv6_addr_set_v4mapped(inet
->inet_daddr
, &sk
->sk_v6_daddr
);
198 if (ipv6_addr_any(&np
->saddr
) ||
199 ipv6_mapped_addr_any(&np
->saddr
))
200 ipv6_addr_set_v4mapped(inet
->inet_saddr
, &np
->saddr
);
202 if (ipv6_addr_any(&sk
->sk_v6_rcv_saddr
) ||
203 ipv6_mapped_addr_any(&sk
->sk_v6_rcv_saddr
)) {
204 ipv6_addr_set_v4mapped(inet
->inet_rcv_saddr
,
205 &sk
->sk_v6_rcv_saddr
);
206 if (sk
->sk_prot
->rehash
)
207 sk
->sk_prot
->rehash(sk
);
213 if (__ipv6_addr_needs_scope_id(addr_type
)) {
214 if (addr_len
>= sizeof(struct sockaddr_in6
) &&
215 usin
->sin6_scope_id
) {
216 if (!sk_dev_equal_l3scope(sk
, usin
->sin6_scope_id
)) {
220 sk
->sk_bound_dev_if
= usin
->sin6_scope_id
;
223 if (!sk
->sk_bound_dev_if
&& (addr_type
& IPV6_ADDR_MULTICAST
))
224 sk
->sk_bound_dev_if
= np
->mcast_oif
;
226 /* Connect to link-local address requires an interface */
227 if (!sk
->sk_bound_dev_if
) {
233 /* save the current peer information before updating it */
234 old_daddr
= sk
->sk_v6_daddr
;
235 old_fl6_flowlabel
= np
->flow_label
;
236 old_dport
= inet
->inet_dport
;
238 sk
->sk_v6_daddr
= *daddr
;
239 np
->flow_label
= fl6_flowlabel
;
240 inet
->inet_dport
= usin
->sin6_port
;
243 * Check for a route to destination an obtain the
244 * destination cache for it.
247 err
= ip6_datagram_dst_update(sk
, true);
249 /* Restore the socket peer info, to keep it consistent with
250 * the old socket state
252 sk
->sk_v6_daddr
= old_daddr
;
253 np
->flow_label
= old_fl6_flowlabel
;
254 inet
->inet_dport
= old_dport
;
258 reuseport_has_conns(sk
, true);
259 sk
->sk_state
= TCP_ESTABLISHED
;
264 EXPORT_SYMBOL_GPL(__ip6_datagram_connect
);
266 int ip6_datagram_connect(struct sock
*sk
, struct sockaddr
*uaddr
, int addr_len
)
271 res
= __ip6_datagram_connect(sk
, uaddr
, addr_len
);
275 EXPORT_SYMBOL_GPL(ip6_datagram_connect
);
277 int ip6_datagram_connect_v6_only(struct sock
*sk
, struct sockaddr
*uaddr
,
280 DECLARE_SOCKADDR(struct sockaddr_in6
*, sin6
, uaddr
);
281 if (sin6
->sin6_family
!= AF_INET6
)
282 return -EAFNOSUPPORT
;
283 return ip6_datagram_connect(sk
, uaddr
, addr_len
);
285 EXPORT_SYMBOL_GPL(ip6_datagram_connect_v6_only
);
287 void ipv6_icmp_error(struct sock
*sk
, struct sk_buff
*skb
, int err
,
288 __be16 port
, u32 info
, u8
*payload
)
290 struct ipv6_pinfo
*np
= inet6_sk(sk
);
291 struct icmp6hdr
*icmph
= icmp6_hdr(skb
);
292 struct sock_exterr_skb
*serr
;
297 skb
= skb_clone(skb
, GFP_ATOMIC
);
301 skb
->protocol
= htons(ETH_P_IPV6
);
303 serr
= SKB_EXT_ERR(skb
);
304 serr
->ee
.ee_errno
= err
;
305 serr
->ee
.ee_origin
= SO_EE_ORIGIN_ICMP6
;
306 serr
->ee
.ee_type
= icmph
->icmp6_type
;
307 serr
->ee
.ee_code
= icmph
->icmp6_code
;
309 serr
->ee
.ee_info
= info
;
310 serr
->ee
.ee_data
= 0;
311 serr
->addr_offset
= (u8
*)&(((struct ipv6hdr
*)(icmph
+ 1))->daddr
) -
312 skb_network_header(skb
);
315 __skb_pull(skb
, payload
- skb
->data
);
316 skb_reset_transport_header(skb
);
318 if (sock_queue_err_skb(sk
, skb
))
322 void ipv6_local_error(struct sock
*sk
, int err
, struct flowi6
*fl6
, u32 info
)
324 const struct ipv6_pinfo
*np
= inet6_sk(sk
);
325 struct sock_exterr_skb
*serr
;
332 skb
= alloc_skb(sizeof(struct ipv6hdr
), GFP_ATOMIC
);
336 skb
->protocol
= htons(ETH_P_IPV6
);
338 skb_put(skb
, sizeof(struct ipv6hdr
));
339 skb_reset_network_header(skb
);
341 iph
->daddr
= fl6
->daddr
;
342 ip6_flow_hdr(iph
, 0, 0);
344 serr
= SKB_EXT_ERR(skb
);
345 serr
->ee
.ee_errno
= err
;
346 serr
->ee
.ee_origin
= SO_EE_ORIGIN_LOCAL
;
347 serr
->ee
.ee_type
= 0;
348 serr
->ee
.ee_code
= 0;
350 serr
->ee
.ee_info
= info
;
351 serr
->ee
.ee_data
= 0;
352 serr
->addr_offset
= (u8
*)&iph
->daddr
- skb_network_header(skb
);
353 serr
->port
= fl6
->fl6_dport
;
355 __skb_pull(skb
, skb_tail_pointer(skb
) - skb
->data
);
356 skb_reset_transport_header(skb
);
358 if (sock_queue_err_skb(sk
, skb
))
362 void ipv6_local_rxpmtu(struct sock
*sk
, struct flowi6
*fl6
, u32 mtu
)
364 struct ipv6_pinfo
*np
= inet6_sk(sk
);
367 struct ip6_mtuinfo
*mtu_info
;
369 if (!np
->rxopt
.bits
.rxpmtu
)
372 skb
= alloc_skb(sizeof(struct ipv6hdr
), GFP_ATOMIC
);
376 skb_put(skb
, sizeof(struct ipv6hdr
));
377 skb_reset_network_header(skb
);
379 iph
->daddr
= fl6
->daddr
;
381 mtu_info
= IP6CBMTU(skb
);
383 mtu_info
->ip6m_mtu
= mtu
;
384 mtu_info
->ip6m_addr
.sin6_family
= AF_INET6
;
385 mtu_info
->ip6m_addr
.sin6_port
= 0;
386 mtu_info
->ip6m_addr
.sin6_flowinfo
= 0;
387 mtu_info
->ip6m_addr
.sin6_scope_id
= fl6
->flowi6_oif
;
388 mtu_info
->ip6m_addr
.sin6_addr
= ipv6_hdr(skb
)->daddr
;
390 __skb_pull(skb
, skb_tail_pointer(skb
) - skb
->data
);
391 skb_reset_transport_header(skb
);
393 skb
= xchg(&np
->rxpmtu
, skb
);
397 /* For some errors we have valid addr_offset even with zero payload and
398 * zero port. Also, addr_offset should be supported if port is set.
400 static inline bool ipv6_datagram_support_addr(struct sock_exterr_skb
*serr
)
402 return serr
->ee
.ee_origin
== SO_EE_ORIGIN_ICMP6
||
403 serr
->ee
.ee_origin
== SO_EE_ORIGIN_ICMP
||
404 serr
->ee
.ee_origin
== SO_EE_ORIGIN_LOCAL
|| serr
->port
;
407 /* IPv6 supports cmsg on all origins aside from SO_EE_ORIGIN_LOCAL.
409 * At one point, excluding local errors was a quick test to identify icmp/icmp6
410 * errors. This is no longer true, but the test remained, so the v6 stack,
411 * unlike v4, also honors cmsg requests on all wifi and timestamp errors.
413 static bool ip6_datagram_support_cmsg(struct sk_buff
*skb
,
414 struct sock_exterr_skb
*serr
)
416 if (serr
->ee
.ee_origin
== SO_EE_ORIGIN_ICMP
||
417 serr
->ee
.ee_origin
== SO_EE_ORIGIN_ICMP6
)
420 if (serr
->ee
.ee_origin
== SO_EE_ORIGIN_LOCAL
)
423 if (!IP6CB(skb
)->iif
)
430 * Handle MSG_ERRQUEUE
432 int ipv6_recv_error(struct sock
*sk
, struct msghdr
*msg
, int len
, int *addr_len
)
434 struct ipv6_pinfo
*np
= inet6_sk(sk
);
435 struct sock_exterr_skb
*serr
;
437 DECLARE_SOCKADDR(struct sockaddr_in6
*, sin
, msg
->msg_name
);
439 struct sock_extended_err ee
;
440 struct sockaddr_in6 offender
;
446 skb
= sock_dequeue_err_skb(sk
);
452 msg
->msg_flags
|= MSG_TRUNC
;
455 err
= skb_copy_datagram_msg(skb
, 0, msg
, copied
);
460 sock_recv_timestamp(msg
, sk
, skb
);
462 serr
= SKB_EXT_ERR(skb
);
464 if (sin
&& ipv6_datagram_support_addr(serr
)) {
465 const unsigned char *nh
= skb_network_header(skb
);
466 sin
->sin6_family
= AF_INET6
;
467 sin
->sin6_flowinfo
= 0;
468 sin
->sin6_port
= serr
->port
;
469 if (skb
->protocol
== htons(ETH_P_IPV6
)) {
470 const struct ipv6hdr
*ip6h
= container_of((struct in6_addr
*)(nh
+ serr
->addr_offset
),
471 struct ipv6hdr
, daddr
);
472 sin
->sin6_addr
= ip6h
->daddr
;
474 sin
->sin6_flowinfo
= ip6_flowinfo(ip6h
);
476 ipv6_iface_scope_id(&sin
->sin6_addr
,
479 ipv6_addr_set_v4mapped(*(__be32
*)(nh
+ serr
->addr_offset
),
481 sin
->sin6_scope_id
= 0;
483 *addr_len
= sizeof(*sin
);
486 memcpy(&errhdr
.ee
, &serr
->ee
, sizeof(struct sock_extended_err
));
487 sin
= &errhdr
.offender
;
488 memset(sin
, 0, sizeof(*sin
));
490 if (ip6_datagram_support_cmsg(skb
, serr
)) {
491 sin
->sin6_family
= AF_INET6
;
493 ip6_datagram_recv_common_ctl(sk
, msg
, skb
);
494 if (skb
->protocol
== htons(ETH_P_IPV6
)) {
495 sin
->sin6_addr
= ipv6_hdr(skb
)->saddr
;
497 ip6_datagram_recv_specific_ctl(sk
, msg
, skb
);
499 ipv6_iface_scope_id(&sin
->sin6_addr
,
502 ipv6_addr_set_v4mapped(ip_hdr(skb
)->saddr
,
504 if (inet_sk(sk
)->cmsg_flags
)
505 ip_cmsg_recv(msg
, skb
);
509 put_cmsg(msg
, SOL_IPV6
, IPV6_RECVERR
, sizeof(errhdr
), &errhdr
);
511 /* Now we could try to dump offended packet options */
513 msg
->msg_flags
|= MSG_ERRQUEUE
;
520 EXPORT_SYMBOL_GPL(ipv6_recv_error
);
523 * Handle IPV6_RECVPATHMTU
525 int ipv6_recv_rxpmtu(struct sock
*sk
, struct msghdr
*msg
, int len
,
528 struct ipv6_pinfo
*np
= inet6_sk(sk
);
530 struct ip6_mtuinfo mtu_info
;
531 DECLARE_SOCKADDR(struct sockaddr_in6
*, sin
, msg
->msg_name
);
536 skb
= xchg(&np
->rxpmtu
, NULL
);
542 msg
->msg_flags
|= MSG_TRUNC
;
545 err
= skb_copy_datagram_msg(skb
, 0, msg
, copied
);
549 sock_recv_timestamp(msg
, sk
, skb
);
551 memcpy(&mtu_info
, IP6CBMTU(skb
), sizeof(mtu_info
));
554 sin
->sin6_family
= AF_INET6
;
555 sin
->sin6_flowinfo
= 0;
557 sin
->sin6_scope_id
= mtu_info
.ip6m_addr
.sin6_scope_id
;
558 sin
->sin6_addr
= mtu_info
.ip6m_addr
.sin6_addr
;
559 *addr_len
= sizeof(*sin
);
562 put_cmsg(msg
, SOL_IPV6
, IPV6_PATHMTU
, sizeof(mtu_info
), &mtu_info
);
573 void ip6_datagram_recv_common_ctl(struct sock
*sk
, struct msghdr
*msg
,
576 struct ipv6_pinfo
*np
= inet6_sk(sk
);
577 bool is_ipv6
= skb
->protocol
== htons(ETH_P_IPV6
);
579 if (np
->rxopt
.bits
.rxinfo
) {
580 struct in6_pktinfo src_info
;
583 src_info
.ipi6_ifindex
= IP6CB(skb
)->iif
;
584 src_info
.ipi6_addr
= ipv6_hdr(skb
)->daddr
;
586 src_info
.ipi6_ifindex
=
587 PKTINFO_SKB_CB(skb
)->ipi_ifindex
;
588 ipv6_addr_set_v4mapped(ip_hdr(skb
)->daddr
,
589 &src_info
.ipi6_addr
);
592 if (src_info
.ipi6_ifindex
>= 0)
593 put_cmsg(msg
, SOL_IPV6
, IPV6_PKTINFO
,
594 sizeof(src_info
), &src_info
);
598 void ip6_datagram_recv_specific_ctl(struct sock
*sk
, struct msghdr
*msg
,
601 struct ipv6_pinfo
*np
= inet6_sk(sk
);
602 struct inet6_skb_parm
*opt
= IP6CB(skb
);
603 unsigned char *nh
= skb_network_header(skb
);
605 if (np
->rxopt
.bits
.rxhlim
) {
606 int hlim
= ipv6_hdr(skb
)->hop_limit
;
607 put_cmsg(msg
, SOL_IPV6
, IPV6_HOPLIMIT
, sizeof(hlim
), &hlim
);
610 if (np
->rxopt
.bits
.rxtclass
) {
611 int tclass
= ipv6_get_dsfield(ipv6_hdr(skb
));
612 put_cmsg(msg
, SOL_IPV6
, IPV6_TCLASS
, sizeof(tclass
), &tclass
);
615 if (np
->rxopt
.bits
.rxflow
) {
616 __be32 flowinfo
= ip6_flowinfo((struct ipv6hdr
*)nh
);
618 put_cmsg(msg
, SOL_IPV6
, IPV6_FLOWINFO
, sizeof(flowinfo
), &flowinfo
);
621 /* HbH is allowed only once */
622 if (np
->rxopt
.bits
.hopopts
&& (opt
->flags
& IP6SKB_HOPBYHOP
)) {
623 u8
*ptr
= nh
+ sizeof(struct ipv6hdr
);
624 put_cmsg(msg
, SOL_IPV6
, IPV6_HOPOPTS
, (ptr
[1]+1)<<3, ptr
);
628 (np
->rxopt
.bits
.dstopts
|| np
->rxopt
.bits
.srcrt
)) {
630 * Silly enough, but we need to reparse in order to
631 * report extension headers (except for HbH)
634 * Also note that IPV6_RECVRTHDRDSTOPTS is NOT
635 * (and WILL NOT be) defined because
636 * IPV6_RECVDSTOPTS is more generic. --yoshfuji
638 unsigned int off
= sizeof(struct ipv6hdr
);
639 u8 nexthdr
= ipv6_hdr(skb
)->nexthdr
;
641 while (off
<= opt
->lastopt
) {
646 case IPPROTO_DSTOPTS
:
648 len
= (ptr
[1] + 1) << 3;
649 if (np
->rxopt
.bits
.dstopts
)
650 put_cmsg(msg
, SOL_IPV6
, IPV6_DSTOPTS
, len
, ptr
);
652 case IPPROTO_ROUTING
:
654 len
= (ptr
[1] + 1) << 3;
655 if (np
->rxopt
.bits
.srcrt
)
656 put_cmsg(msg
, SOL_IPV6
, IPV6_RTHDR
, len
, ptr
);
660 len
= (ptr
[1] + 2) << 2;
664 len
= (ptr
[1] + 1) << 3;
672 /* socket options in old style */
673 if (np
->rxopt
.bits
.rxoinfo
) {
674 struct in6_pktinfo src_info
;
676 src_info
.ipi6_ifindex
= opt
->iif
;
677 src_info
.ipi6_addr
= ipv6_hdr(skb
)->daddr
;
678 put_cmsg(msg
, SOL_IPV6
, IPV6_2292PKTINFO
, sizeof(src_info
), &src_info
);
680 if (np
->rxopt
.bits
.rxohlim
) {
681 int hlim
= ipv6_hdr(skb
)->hop_limit
;
682 put_cmsg(msg
, SOL_IPV6
, IPV6_2292HOPLIMIT
, sizeof(hlim
), &hlim
);
684 if (np
->rxopt
.bits
.ohopopts
&& (opt
->flags
& IP6SKB_HOPBYHOP
)) {
685 u8
*ptr
= nh
+ sizeof(struct ipv6hdr
);
686 put_cmsg(msg
, SOL_IPV6
, IPV6_2292HOPOPTS
, (ptr
[1]+1)<<3, ptr
);
688 if (np
->rxopt
.bits
.odstopts
&& opt
->dst0
) {
689 u8
*ptr
= nh
+ opt
->dst0
;
690 put_cmsg(msg
, SOL_IPV6
, IPV6_2292DSTOPTS
, (ptr
[1]+1)<<3, ptr
);
692 if (np
->rxopt
.bits
.osrcrt
&& opt
->srcrt
) {
693 struct ipv6_rt_hdr
*rthdr
= (struct ipv6_rt_hdr
*)(nh
+ opt
->srcrt
);
694 put_cmsg(msg
, SOL_IPV6
, IPV6_2292RTHDR
, (rthdr
->hdrlen
+1) << 3, rthdr
);
696 if (np
->rxopt
.bits
.odstopts
&& opt
->dst1
) {
697 u8
*ptr
= nh
+ opt
->dst1
;
698 put_cmsg(msg
, SOL_IPV6
, IPV6_2292DSTOPTS
, (ptr
[1]+1)<<3, ptr
);
700 if (np
->rxopt
.bits
.rxorigdstaddr
) {
701 struct sockaddr_in6 sin6
;
702 __be16 _ports
[2], *ports
;
704 ports
= skb_header_pointer(skb
, skb_transport_offset(skb
),
705 sizeof(_ports
), &_ports
);
707 /* All current transport protocols have the port numbers in the
708 * first four bytes of the transport header and this function is
709 * written with this assumption in mind.
711 sin6
.sin6_family
= AF_INET6
;
712 sin6
.sin6_addr
= ipv6_hdr(skb
)->daddr
;
713 sin6
.sin6_port
= ports
[1];
714 sin6
.sin6_flowinfo
= 0;
716 ipv6_iface_scope_id(&ipv6_hdr(skb
)->daddr
,
719 put_cmsg(msg
, SOL_IPV6
, IPV6_ORIGDSTADDR
, sizeof(sin6
), &sin6
);
722 if (np
->rxopt
.bits
.recvfragsize
&& opt
->frag_max_size
) {
723 int val
= opt
->frag_max_size
;
725 put_cmsg(msg
, SOL_IPV6
, IPV6_RECVFRAGSIZE
, sizeof(val
), &val
);
729 void ip6_datagram_recv_ctl(struct sock
*sk
, struct msghdr
*msg
,
732 ip6_datagram_recv_common_ctl(sk
, msg
, skb
);
733 ip6_datagram_recv_specific_ctl(sk
, msg
, skb
);
735 EXPORT_SYMBOL_GPL(ip6_datagram_recv_ctl
);
737 int ip6_datagram_send_ctl(struct net
*net
, struct sock
*sk
,
738 struct msghdr
*msg
, struct flowi6
*fl6
,
739 struct ipcm6_cookie
*ipc6
)
741 struct in6_pktinfo
*src_info
;
742 struct cmsghdr
*cmsg
;
743 struct ipv6_rt_hdr
*rthdr
;
744 struct ipv6_opt_hdr
*hdr
;
745 struct ipv6_txoptions
*opt
= ipc6
->opt
;
749 for_each_cmsghdr(cmsg
, msg
) {
752 if (!CMSG_OK(msg
, cmsg
)) {
757 if (cmsg
->cmsg_level
== SOL_SOCKET
) {
758 err
= __sock_cmsg_send(sk
, msg
, cmsg
, &ipc6
->sockc
);
764 if (cmsg
->cmsg_level
!= SOL_IPV6
)
767 switch (cmsg
->cmsg_type
) {
769 case IPV6_2292PKTINFO
:
771 struct net_device
*dev
= NULL
;
774 if (cmsg
->cmsg_len
< CMSG_LEN(sizeof(struct in6_pktinfo
))) {
779 src_info
= (struct in6_pktinfo
*)CMSG_DATA(cmsg
);
780 src_idx
= src_info
->ipi6_ifindex
;
783 if (fl6
->flowi6_oif
&&
784 src_idx
!= fl6
->flowi6_oif
&&
785 (sk
->sk_bound_dev_if
!= fl6
->flowi6_oif
||
786 !sk_dev_equal_l3scope(sk
, src_idx
)))
788 fl6
->flowi6_oif
= src_idx
;
791 addr_type
= __ipv6_addr_type(&src_info
->ipi6_addr
);
794 if (fl6
->flowi6_oif
) {
795 dev
= dev_get_by_index_rcu(net
, fl6
->flowi6_oif
);
800 } else if (addr_type
& IPV6_ADDR_LINKLOCAL
) {
805 if (addr_type
!= IPV6_ADDR_ANY
) {
806 int strict
= __ipv6_addr_src_scope(addr_type
) <= IPV6_ADDR_SCOPE_LINKLOCAL
;
807 if (!ipv6_can_nonlocal_bind(net
, inet_sk(sk
)) &&
808 !ipv6_chk_addr_and_flags(net
, &src_info
->ipi6_addr
,
811 !ipv6_chk_acast_addr_src(net
, dev
,
812 &src_info
->ipi6_addr
))
815 fl6
->saddr
= src_info
->ipi6_addr
;
827 if (cmsg
->cmsg_len
< CMSG_LEN(4)) {
832 if (fl6
->flowlabel
&IPV6_FLOWINFO_MASK
) {
833 if ((fl6
->flowlabel
^*(__be32
*)CMSG_DATA(cmsg
))&~IPV6_FLOWINFO_MASK
) {
838 fl6
->flowlabel
= IPV6_FLOWINFO_MASK
& *(__be32
*)CMSG_DATA(cmsg
);
841 case IPV6_2292HOPOPTS
:
843 if (opt
->hopopt
|| cmsg
->cmsg_len
< CMSG_LEN(sizeof(struct ipv6_opt_hdr
))) {
848 hdr
= (struct ipv6_opt_hdr
*)CMSG_DATA(cmsg
);
849 len
= ((hdr
->hdrlen
+ 1) << 3);
850 if (cmsg
->cmsg_len
< CMSG_LEN(len
)) {
854 if (!ns_capable(net
->user_ns
, CAP_NET_RAW
)) {
858 opt
->opt_nflen
+= len
;
862 case IPV6_2292DSTOPTS
:
863 if (cmsg
->cmsg_len
< CMSG_LEN(sizeof(struct ipv6_opt_hdr
))) {
868 hdr
= (struct ipv6_opt_hdr
*)CMSG_DATA(cmsg
);
869 len
= ((hdr
->hdrlen
+ 1) << 3);
870 if (cmsg
->cmsg_len
< CMSG_LEN(len
)) {
874 if (!ns_capable(net
->user_ns
, CAP_NET_RAW
)) {
882 opt
->opt_flen
+= len
;
887 case IPV6_RTHDRDSTOPTS
:
888 if (cmsg
->cmsg_len
< CMSG_LEN(sizeof(struct ipv6_opt_hdr
))) {
893 hdr
= (struct ipv6_opt_hdr
*)CMSG_DATA(cmsg
);
894 len
= ((hdr
->hdrlen
+ 1) << 3);
895 if (cmsg
->cmsg_len
< CMSG_LEN(len
)) {
899 if (!ns_capable(net
->user_ns
, CAP_NET_RAW
)) {
903 if (cmsg
->cmsg_type
== IPV6_DSTOPTS
) {
904 opt
->opt_flen
+= len
;
907 opt
->opt_nflen
+= len
;
914 if (cmsg
->cmsg_len
< CMSG_LEN(sizeof(struct ipv6_rt_hdr
))) {
919 rthdr
= (struct ipv6_rt_hdr
*)CMSG_DATA(cmsg
);
921 switch (rthdr
->type
) {
922 #if IS_ENABLED(CONFIG_IPV6_MIP6)
923 case IPV6_SRCRT_TYPE_2
:
924 if (rthdr
->hdrlen
!= 2 ||
925 rthdr
->segments_left
!= 1) {
936 len
= ((rthdr
->hdrlen
+ 1) << 3);
938 if (cmsg
->cmsg_len
< CMSG_LEN(len
)) {
943 /* segments left must also match */
944 if ((rthdr
->hdrlen
>> 1) != rthdr
->segments_left
) {
949 opt
->opt_nflen
+= len
;
952 if (cmsg
->cmsg_type
== IPV6_2292RTHDR
&& opt
->dst1opt
) {
953 int dsthdrlen
= ((opt
->dst1opt
->hdrlen
+1)<<3);
955 opt
->opt_nflen
+= dsthdrlen
;
956 opt
->dst0opt
= opt
->dst1opt
;
958 opt
->opt_flen
-= dsthdrlen
;
963 case IPV6_2292HOPLIMIT
:
965 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(int))) {
970 ipc6
->hlimit
= *(int *)CMSG_DATA(cmsg
);
971 if (ipc6
->hlimit
< -1 || ipc6
->hlimit
> 0xff) {
983 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(int)))
986 tc
= *(int *)CMSG_DATA(cmsg
);
987 if (tc
< -1 || tc
> 0xff)
1001 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(int)))
1004 df
= *(int *)CMSG_DATA(cmsg
);
1005 if (df
< 0 || df
> 1)
1009 ipc6
->dontfrag
= df
;
1014 net_dbg_ratelimited("invalid cmsg type: %d\n",
1024 EXPORT_SYMBOL_GPL(ip6_datagram_send_ctl
);
1026 void __ip6_dgram_sock_seq_show(struct seq_file
*seq
, struct sock
*sp
,
1027 __u16 srcp
, __u16 destp
, int rqueue
, int bucket
)
1029 const struct in6_addr
*dest
, *src
;
1031 dest
= &sp
->sk_v6_daddr
;
1032 src
= &sp
->sk_v6_rcv_saddr
;
1034 "%5d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
1035 "%02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %u\n",
1037 src
->s6_addr32
[0], src
->s6_addr32
[1],
1038 src
->s6_addr32
[2], src
->s6_addr32
[3], srcp
,
1039 dest
->s6_addr32
[0], dest
->s6_addr32
[1],
1040 dest
->s6_addr32
[2], dest
->s6_addr32
[3], destp
,
1042 sk_wmem_alloc_get(sp
),
1045 from_kuid_munged(seq_user_ns(seq
), sock_i_uid(sp
)),
1048 refcount_read(&sp
->sk_refcnt
), sp
,
1049 atomic_read(&sp
->sk_drops
));