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>
22 #include <linux/icmp.h>
25 #include <net/ndisc.h>
26 #include <net/addrconf.h>
27 #include <net/transp_v6.h>
28 #include <net/ip6_route.h>
29 #include <net/tcp_states.h>
30 #include <net/dsfield.h>
31 #include <net/sock_reuseport.h>
33 #include <linux/errqueue.h>
34 #include <linux/uaccess.h>
36 static bool ipv6_mapped_addr_any(const struct in6_addr
*a
)
38 return ipv6_addr_v4mapped(a
) && (a
->s6_addr32
[3] == 0);
41 static void ip6_datagram_flow_key_init(struct flowi6
*fl6
, struct sock
*sk
)
43 struct inet_sock
*inet
= inet_sk(sk
);
44 struct ipv6_pinfo
*np
= inet6_sk(sk
);
46 memset(fl6
, 0, sizeof(*fl6
));
47 fl6
->flowi6_proto
= sk
->sk_protocol
;
48 fl6
->daddr
= sk
->sk_v6_daddr
;
49 fl6
->saddr
= np
->saddr
;
50 fl6
->flowi6_oif
= sk
->sk_bound_dev_if
;
51 fl6
->flowi6_mark
= sk
->sk_mark
;
52 fl6
->fl6_dport
= inet
->inet_dport
;
53 fl6
->fl6_sport
= inet
->inet_sport
;
54 fl6
->flowlabel
= np
->flow_label
;
55 fl6
->flowi6_uid
= sk
->sk_uid
;
58 fl6
->flowi6_oif
= np
->sticky_pktinfo
.ipi6_ifindex
;
60 if (!fl6
->flowi6_oif
&& ipv6_addr_is_multicast(&fl6
->daddr
))
61 fl6
->flowi6_oif
= np
->mcast_oif
;
63 security_sk_classify_flow(sk
, flowi6_to_flowi_common(fl6
));
66 int ip6_datagram_dst_update(struct sock
*sk
, bool fix_sk_saddr
)
68 struct ip6_flowlabel
*flowlabel
= NULL
;
69 struct in6_addr
*final_p
, final
;
70 struct ipv6_txoptions
*opt
;
71 struct dst_entry
*dst
;
72 struct inet_sock
*inet
= inet_sk(sk
);
73 struct ipv6_pinfo
*np
= inet6_sk(sk
);
77 if (np
->sndflow
&& (np
->flow_label
& IPV6_FLOWLABEL_MASK
)) {
78 flowlabel
= fl6_sock_lookup(sk
, np
->flow_label
);
79 if (IS_ERR(flowlabel
))
82 ip6_datagram_flow_key_init(&fl6
, sk
);
85 opt
= flowlabel
? flowlabel
->opt
: rcu_dereference(np
->opt
);
86 final_p
= fl6_update_dst(&fl6
, opt
, &final
);
89 dst
= ip6_dst_lookup_flow(sock_net(sk
), sk
, &fl6
, final_p
);
96 if (ipv6_addr_any(&np
->saddr
))
97 np
->saddr
= fl6
.saddr
;
99 if (ipv6_addr_any(&sk
->sk_v6_rcv_saddr
)) {
100 sk
->sk_v6_rcv_saddr
= fl6
.saddr
;
101 inet
->inet_rcv_saddr
= LOOPBACK4_IPV6
;
102 if (sk
->sk_prot
->rehash
)
103 sk
->sk_prot
->rehash(sk
);
107 ip6_sk_dst_store_flow(sk
, dst
, &fl6
);
110 fl6_sock_release(flowlabel
);
114 void ip6_datagram_release_cb(struct sock
*sk
)
116 struct dst_entry
*dst
;
118 if (ipv6_addr_v4mapped(&sk
->sk_v6_daddr
))
122 dst
= __sk_dst_get(sk
);
123 if (!dst
|| !dst
->obsolete
||
124 dst
->ops
->check(dst
, inet6_sk(sk
)->dst_cookie
)) {
130 ip6_datagram_dst_update(sk
, false);
132 EXPORT_SYMBOL_GPL(ip6_datagram_release_cb
);
134 int __ip6_datagram_connect(struct sock
*sk
, struct sockaddr
*uaddr
,
137 struct sockaddr_in6
*usin
= (struct sockaddr_in6
*) uaddr
;
138 struct inet_sock
*inet
= inet_sk(sk
);
139 struct ipv6_pinfo
*np
= inet6_sk(sk
);
140 struct in6_addr
*daddr
, old_daddr
;
141 __be32 fl6_flowlabel
= 0;
142 __be32 old_fl6_flowlabel
;
147 if (usin
->sin6_family
== AF_INET
) {
148 if (__ipv6_only_sock(sk
))
149 return -EAFNOSUPPORT
;
150 err
= __ip4_datagram_connect(sk
, uaddr
, addr_len
);
154 if (addr_len
< SIN6_LEN_RFC2133
)
157 if (usin
->sin6_family
!= AF_INET6
)
158 return -EAFNOSUPPORT
;
161 fl6_flowlabel
= usin
->sin6_flowinfo
& IPV6_FLOWINFO_MASK
;
163 if (ipv6_addr_any(&usin
->sin6_addr
)) {
167 if (ipv6_addr_v4mapped(&sk
->sk_v6_rcv_saddr
))
168 ipv6_addr_set_v4mapped(htonl(INADDR_LOOPBACK
),
171 usin
->sin6_addr
= in6addr_loopback
;
174 addr_type
= ipv6_addr_type(&usin
->sin6_addr
);
176 daddr
= &usin
->sin6_addr
;
178 if (addr_type
& IPV6_ADDR_MAPPED
) {
179 struct sockaddr_in sin
;
181 if (__ipv6_only_sock(sk
)) {
185 sin
.sin_family
= AF_INET
;
186 sin
.sin_addr
.s_addr
= daddr
->s6_addr32
[3];
187 sin
.sin_port
= usin
->sin6_port
;
189 err
= __ip4_datagram_connect(sk
,
190 (struct sockaddr
*) &sin
,
197 ipv6_addr_set_v4mapped(inet
->inet_daddr
, &sk
->sk_v6_daddr
);
199 if (ipv6_addr_any(&np
->saddr
) ||
200 ipv6_mapped_addr_any(&np
->saddr
))
201 ipv6_addr_set_v4mapped(inet
->inet_saddr
, &np
->saddr
);
203 if (ipv6_addr_any(&sk
->sk_v6_rcv_saddr
) ||
204 ipv6_mapped_addr_any(&sk
->sk_v6_rcv_saddr
)) {
205 ipv6_addr_set_v4mapped(inet
->inet_rcv_saddr
,
206 &sk
->sk_v6_rcv_saddr
);
207 if (sk
->sk_prot
->rehash
)
208 sk
->sk_prot
->rehash(sk
);
214 if (__ipv6_addr_needs_scope_id(addr_type
)) {
215 if (addr_len
>= sizeof(struct sockaddr_in6
) &&
216 usin
->sin6_scope_id
) {
217 if (!sk_dev_equal_l3scope(sk
, usin
->sin6_scope_id
)) {
221 sk
->sk_bound_dev_if
= usin
->sin6_scope_id
;
224 if (!sk
->sk_bound_dev_if
&& (addr_type
& IPV6_ADDR_MULTICAST
))
225 sk
->sk_bound_dev_if
= np
->mcast_oif
;
227 /* Connect to link-local address requires an interface */
228 if (!sk
->sk_bound_dev_if
) {
234 /* save the current peer information before updating it */
235 old_daddr
= sk
->sk_v6_daddr
;
236 old_fl6_flowlabel
= np
->flow_label
;
237 old_dport
= inet
->inet_dport
;
239 sk
->sk_v6_daddr
= *daddr
;
240 np
->flow_label
= fl6_flowlabel
;
241 inet
->inet_dport
= usin
->sin6_port
;
244 * Check for a route to destination an obtain the
245 * destination cache for it.
248 err
= ip6_datagram_dst_update(sk
, true);
250 /* Restore the socket peer info, to keep it consistent with
251 * the old socket state
253 sk
->sk_v6_daddr
= old_daddr
;
254 np
->flow_label
= old_fl6_flowlabel
;
255 inet
->inet_dport
= old_dport
;
259 reuseport_has_conns(sk
, true);
260 sk
->sk_state
= TCP_ESTABLISHED
;
265 EXPORT_SYMBOL_GPL(__ip6_datagram_connect
);
267 int ip6_datagram_connect(struct sock
*sk
, struct sockaddr
*uaddr
, int addr_len
)
272 res
= __ip6_datagram_connect(sk
, uaddr
, addr_len
);
276 EXPORT_SYMBOL_GPL(ip6_datagram_connect
);
278 int ip6_datagram_connect_v6_only(struct sock
*sk
, struct sockaddr
*uaddr
,
281 DECLARE_SOCKADDR(struct sockaddr_in6
*, sin6
, uaddr
);
282 if (sin6
->sin6_family
!= AF_INET6
)
283 return -EAFNOSUPPORT
;
284 return ip6_datagram_connect(sk
, uaddr
, addr_len
);
286 EXPORT_SYMBOL_GPL(ip6_datagram_connect_v6_only
);
288 static void ipv6_icmp_error_rfc4884(const struct sk_buff
*skb
,
289 struct sock_ee_data_rfc4884
*out
)
291 switch (icmp6_hdr(skb
)->icmp6_type
) {
292 case ICMPV6_TIME_EXCEED
:
293 case ICMPV6_DEST_UNREACH
:
294 ip_icmp_error_rfc4884(skb
, out
, sizeof(struct icmp6hdr
),
295 icmp6_hdr(skb
)->icmp6_datagram_len
* 8);
299 void ipv6_icmp_error(struct sock
*sk
, struct sk_buff
*skb
, int err
,
300 __be16 port
, u32 info
, u8
*payload
)
302 struct ipv6_pinfo
*np
= inet6_sk(sk
);
303 struct icmp6hdr
*icmph
= icmp6_hdr(skb
);
304 struct sock_exterr_skb
*serr
;
309 skb
= skb_clone(skb
, GFP_ATOMIC
);
313 skb
->protocol
= htons(ETH_P_IPV6
);
315 serr
= SKB_EXT_ERR(skb
);
316 serr
->ee
.ee_errno
= err
;
317 serr
->ee
.ee_origin
= SO_EE_ORIGIN_ICMP6
;
318 serr
->ee
.ee_type
= icmph
->icmp6_type
;
319 serr
->ee
.ee_code
= icmph
->icmp6_code
;
321 serr
->ee
.ee_info
= info
;
322 serr
->ee
.ee_data
= 0;
323 serr
->addr_offset
= (u8
*)&(((struct ipv6hdr
*)(icmph
+ 1))->daddr
) -
324 skb_network_header(skb
);
327 __skb_pull(skb
, payload
- skb
->data
);
329 if (inet6_sk(sk
)->recverr_rfc4884
)
330 ipv6_icmp_error_rfc4884(skb
, &serr
->ee
.ee_rfc4884
);
332 skb_reset_transport_header(skb
);
334 if (sock_queue_err_skb(sk
, skb
))
338 void ipv6_local_error(struct sock
*sk
, int err
, struct flowi6
*fl6
, u32 info
)
340 const struct ipv6_pinfo
*np
= inet6_sk(sk
);
341 struct sock_exterr_skb
*serr
;
348 skb
= alloc_skb(sizeof(struct ipv6hdr
), GFP_ATOMIC
);
352 skb
->protocol
= htons(ETH_P_IPV6
);
354 skb_put(skb
, sizeof(struct ipv6hdr
));
355 skb_reset_network_header(skb
);
357 iph
->daddr
= fl6
->daddr
;
358 ip6_flow_hdr(iph
, 0, 0);
360 serr
= SKB_EXT_ERR(skb
);
361 serr
->ee
.ee_errno
= err
;
362 serr
->ee
.ee_origin
= SO_EE_ORIGIN_LOCAL
;
363 serr
->ee
.ee_type
= 0;
364 serr
->ee
.ee_code
= 0;
366 serr
->ee
.ee_info
= info
;
367 serr
->ee
.ee_data
= 0;
368 serr
->addr_offset
= (u8
*)&iph
->daddr
- skb_network_header(skb
);
369 serr
->port
= fl6
->fl6_dport
;
371 __skb_pull(skb
, skb_tail_pointer(skb
) - skb
->data
);
372 skb_reset_transport_header(skb
);
374 if (sock_queue_err_skb(sk
, skb
))
378 void ipv6_local_rxpmtu(struct sock
*sk
, struct flowi6
*fl6
, u32 mtu
)
380 struct ipv6_pinfo
*np
= inet6_sk(sk
);
383 struct ip6_mtuinfo
*mtu_info
;
385 if (!np
->rxopt
.bits
.rxpmtu
)
388 skb
= alloc_skb(sizeof(struct ipv6hdr
), GFP_ATOMIC
);
392 skb_put(skb
, sizeof(struct ipv6hdr
));
393 skb_reset_network_header(skb
);
395 iph
->daddr
= fl6
->daddr
;
397 mtu_info
= IP6CBMTU(skb
);
399 mtu_info
->ip6m_mtu
= mtu
;
400 mtu_info
->ip6m_addr
.sin6_family
= AF_INET6
;
401 mtu_info
->ip6m_addr
.sin6_port
= 0;
402 mtu_info
->ip6m_addr
.sin6_flowinfo
= 0;
403 mtu_info
->ip6m_addr
.sin6_scope_id
= fl6
->flowi6_oif
;
404 mtu_info
->ip6m_addr
.sin6_addr
= ipv6_hdr(skb
)->daddr
;
406 __skb_pull(skb
, skb_tail_pointer(skb
) - skb
->data
);
407 skb_reset_transport_header(skb
);
409 skb
= xchg(&np
->rxpmtu
, skb
);
413 /* For some errors we have valid addr_offset even with zero payload and
414 * zero port. Also, addr_offset should be supported if port is set.
416 static inline bool ipv6_datagram_support_addr(struct sock_exterr_skb
*serr
)
418 return serr
->ee
.ee_origin
== SO_EE_ORIGIN_ICMP6
||
419 serr
->ee
.ee_origin
== SO_EE_ORIGIN_ICMP
||
420 serr
->ee
.ee_origin
== SO_EE_ORIGIN_LOCAL
|| serr
->port
;
423 /* IPv6 supports cmsg on all origins aside from SO_EE_ORIGIN_LOCAL.
425 * At one point, excluding local errors was a quick test to identify icmp/icmp6
426 * errors. This is no longer true, but the test remained, so the v6 stack,
427 * unlike v4, also honors cmsg requests on all wifi and timestamp errors.
429 static bool ip6_datagram_support_cmsg(struct sk_buff
*skb
,
430 struct sock_exterr_skb
*serr
)
432 if (serr
->ee
.ee_origin
== SO_EE_ORIGIN_ICMP
||
433 serr
->ee
.ee_origin
== SO_EE_ORIGIN_ICMP6
)
436 if (serr
->ee
.ee_origin
== SO_EE_ORIGIN_LOCAL
)
439 if (!IP6CB(skb
)->iif
)
446 * Handle MSG_ERRQUEUE
448 int ipv6_recv_error(struct sock
*sk
, struct msghdr
*msg
, int len
, int *addr_len
)
450 struct ipv6_pinfo
*np
= inet6_sk(sk
);
451 struct sock_exterr_skb
*serr
;
453 DECLARE_SOCKADDR(struct sockaddr_in6
*, sin
, msg
->msg_name
);
455 struct sock_extended_err ee
;
456 struct sockaddr_in6 offender
;
462 skb
= sock_dequeue_err_skb(sk
);
468 msg
->msg_flags
|= MSG_TRUNC
;
471 err
= skb_copy_datagram_msg(skb
, 0, msg
, copied
);
476 sock_recv_timestamp(msg
, sk
, skb
);
478 serr
= SKB_EXT_ERR(skb
);
480 if (sin
&& ipv6_datagram_support_addr(serr
)) {
481 const unsigned char *nh
= skb_network_header(skb
);
482 sin
->sin6_family
= AF_INET6
;
483 sin
->sin6_flowinfo
= 0;
484 sin
->sin6_port
= serr
->port
;
485 if (skb
->protocol
== htons(ETH_P_IPV6
)) {
486 const struct ipv6hdr
*ip6h
= container_of((struct in6_addr
*)(nh
+ serr
->addr_offset
),
487 struct ipv6hdr
, daddr
);
488 sin
->sin6_addr
= ip6h
->daddr
;
490 sin
->sin6_flowinfo
= ip6_flowinfo(ip6h
);
492 ipv6_iface_scope_id(&sin
->sin6_addr
,
495 ipv6_addr_set_v4mapped(*(__be32
*)(nh
+ serr
->addr_offset
),
497 sin
->sin6_scope_id
= 0;
499 *addr_len
= sizeof(*sin
);
502 memcpy(&errhdr
.ee
, &serr
->ee
, sizeof(struct sock_extended_err
));
503 sin
= &errhdr
.offender
;
504 memset(sin
, 0, sizeof(*sin
));
506 if (ip6_datagram_support_cmsg(skb
, serr
)) {
507 sin
->sin6_family
= AF_INET6
;
509 ip6_datagram_recv_common_ctl(sk
, msg
, skb
);
510 if (skb
->protocol
== htons(ETH_P_IPV6
)) {
511 sin
->sin6_addr
= ipv6_hdr(skb
)->saddr
;
513 ip6_datagram_recv_specific_ctl(sk
, msg
, skb
);
515 ipv6_iface_scope_id(&sin
->sin6_addr
,
518 ipv6_addr_set_v4mapped(ip_hdr(skb
)->saddr
,
520 if (inet_sk(sk
)->cmsg_flags
)
521 ip_cmsg_recv(msg
, skb
);
525 put_cmsg(msg
, SOL_IPV6
, IPV6_RECVERR
, sizeof(errhdr
), &errhdr
);
527 /* Now we could try to dump offended packet options */
529 msg
->msg_flags
|= MSG_ERRQUEUE
;
536 EXPORT_SYMBOL_GPL(ipv6_recv_error
);
539 * Handle IPV6_RECVPATHMTU
541 int ipv6_recv_rxpmtu(struct sock
*sk
, struct msghdr
*msg
, int len
,
544 struct ipv6_pinfo
*np
= inet6_sk(sk
);
546 struct ip6_mtuinfo mtu_info
;
547 DECLARE_SOCKADDR(struct sockaddr_in6
*, sin
, msg
->msg_name
);
552 skb
= xchg(&np
->rxpmtu
, NULL
);
558 msg
->msg_flags
|= MSG_TRUNC
;
561 err
= skb_copy_datagram_msg(skb
, 0, msg
, copied
);
565 sock_recv_timestamp(msg
, sk
, skb
);
567 memcpy(&mtu_info
, IP6CBMTU(skb
), sizeof(mtu_info
));
570 sin
->sin6_family
= AF_INET6
;
571 sin
->sin6_flowinfo
= 0;
573 sin
->sin6_scope_id
= mtu_info
.ip6m_addr
.sin6_scope_id
;
574 sin
->sin6_addr
= mtu_info
.ip6m_addr
.sin6_addr
;
575 *addr_len
= sizeof(*sin
);
578 put_cmsg(msg
, SOL_IPV6
, IPV6_PATHMTU
, sizeof(mtu_info
), &mtu_info
);
589 void ip6_datagram_recv_common_ctl(struct sock
*sk
, struct msghdr
*msg
,
592 struct ipv6_pinfo
*np
= inet6_sk(sk
);
593 bool is_ipv6
= skb
->protocol
== htons(ETH_P_IPV6
);
595 if (np
->rxopt
.bits
.rxinfo
) {
596 struct in6_pktinfo src_info
;
599 src_info
.ipi6_ifindex
= IP6CB(skb
)->iif
;
600 src_info
.ipi6_addr
= ipv6_hdr(skb
)->daddr
;
602 src_info
.ipi6_ifindex
=
603 PKTINFO_SKB_CB(skb
)->ipi_ifindex
;
604 ipv6_addr_set_v4mapped(ip_hdr(skb
)->daddr
,
605 &src_info
.ipi6_addr
);
608 if (src_info
.ipi6_ifindex
>= 0)
609 put_cmsg(msg
, SOL_IPV6
, IPV6_PKTINFO
,
610 sizeof(src_info
), &src_info
);
614 void ip6_datagram_recv_specific_ctl(struct sock
*sk
, struct msghdr
*msg
,
617 struct ipv6_pinfo
*np
= inet6_sk(sk
);
618 struct inet6_skb_parm
*opt
= IP6CB(skb
);
619 unsigned char *nh
= skb_network_header(skb
);
621 if (np
->rxopt
.bits
.rxhlim
) {
622 int hlim
= ipv6_hdr(skb
)->hop_limit
;
623 put_cmsg(msg
, SOL_IPV6
, IPV6_HOPLIMIT
, sizeof(hlim
), &hlim
);
626 if (np
->rxopt
.bits
.rxtclass
) {
627 int tclass
= ipv6_get_dsfield(ipv6_hdr(skb
));
628 put_cmsg(msg
, SOL_IPV6
, IPV6_TCLASS
, sizeof(tclass
), &tclass
);
631 if (np
->rxopt
.bits
.rxflow
) {
632 __be32 flowinfo
= ip6_flowinfo((struct ipv6hdr
*)nh
);
634 put_cmsg(msg
, SOL_IPV6
, IPV6_FLOWINFO
, sizeof(flowinfo
), &flowinfo
);
637 /* HbH is allowed only once */
638 if (np
->rxopt
.bits
.hopopts
&& (opt
->flags
& IP6SKB_HOPBYHOP
)) {
639 u8
*ptr
= nh
+ sizeof(struct ipv6hdr
);
640 put_cmsg(msg
, SOL_IPV6
, IPV6_HOPOPTS
, (ptr
[1]+1)<<3, ptr
);
644 (np
->rxopt
.bits
.dstopts
|| np
->rxopt
.bits
.srcrt
)) {
646 * Silly enough, but we need to reparse in order to
647 * report extension headers (except for HbH)
650 * Also note that IPV6_RECVRTHDRDSTOPTS is NOT
651 * (and WILL NOT be) defined because
652 * IPV6_RECVDSTOPTS is more generic. --yoshfuji
654 unsigned int off
= sizeof(struct ipv6hdr
);
655 u8 nexthdr
= ipv6_hdr(skb
)->nexthdr
;
657 while (off
<= opt
->lastopt
) {
662 case IPPROTO_DSTOPTS
:
664 len
= (ptr
[1] + 1) << 3;
665 if (np
->rxopt
.bits
.dstopts
)
666 put_cmsg(msg
, SOL_IPV6
, IPV6_DSTOPTS
, len
, ptr
);
668 case IPPROTO_ROUTING
:
670 len
= (ptr
[1] + 1) << 3;
671 if (np
->rxopt
.bits
.srcrt
)
672 put_cmsg(msg
, SOL_IPV6
, IPV6_RTHDR
, len
, ptr
);
676 len
= (ptr
[1] + 2) << 2;
680 len
= (ptr
[1] + 1) << 3;
688 /* socket options in old style */
689 if (np
->rxopt
.bits
.rxoinfo
) {
690 struct in6_pktinfo src_info
;
692 src_info
.ipi6_ifindex
= opt
->iif
;
693 src_info
.ipi6_addr
= ipv6_hdr(skb
)->daddr
;
694 put_cmsg(msg
, SOL_IPV6
, IPV6_2292PKTINFO
, sizeof(src_info
), &src_info
);
696 if (np
->rxopt
.bits
.rxohlim
) {
697 int hlim
= ipv6_hdr(skb
)->hop_limit
;
698 put_cmsg(msg
, SOL_IPV6
, IPV6_2292HOPLIMIT
, sizeof(hlim
), &hlim
);
700 if (np
->rxopt
.bits
.ohopopts
&& (opt
->flags
& IP6SKB_HOPBYHOP
)) {
701 u8
*ptr
= nh
+ sizeof(struct ipv6hdr
);
702 put_cmsg(msg
, SOL_IPV6
, IPV6_2292HOPOPTS
, (ptr
[1]+1)<<3, ptr
);
704 if (np
->rxopt
.bits
.odstopts
&& opt
->dst0
) {
705 u8
*ptr
= nh
+ opt
->dst0
;
706 put_cmsg(msg
, SOL_IPV6
, IPV6_2292DSTOPTS
, (ptr
[1]+1)<<3, ptr
);
708 if (np
->rxopt
.bits
.osrcrt
&& opt
->srcrt
) {
709 struct ipv6_rt_hdr
*rthdr
= (struct ipv6_rt_hdr
*)(nh
+ opt
->srcrt
);
710 put_cmsg(msg
, SOL_IPV6
, IPV6_2292RTHDR
, (rthdr
->hdrlen
+1) << 3, rthdr
);
712 if (np
->rxopt
.bits
.odstopts
&& opt
->dst1
) {
713 u8
*ptr
= nh
+ opt
->dst1
;
714 put_cmsg(msg
, SOL_IPV6
, IPV6_2292DSTOPTS
, (ptr
[1]+1)<<3, ptr
);
716 if (np
->rxopt
.bits
.rxorigdstaddr
) {
717 struct sockaddr_in6 sin6
;
718 __be16 _ports
[2], *ports
;
720 ports
= skb_header_pointer(skb
, skb_transport_offset(skb
),
721 sizeof(_ports
), &_ports
);
723 /* All current transport protocols have the port numbers in the
724 * first four bytes of the transport header and this function is
725 * written with this assumption in mind.
727 sin6
.sin6_family
= AF_INET6
;
728 sin6
.sin6_addr
= ipv6_hdr(skb
)->daddr
;
729 sin6
.sin6_port
= ports
[1];
730 sin6
.sin6_flowinfo
= 0;
732 ipv6_iface_scope_id(&ipv6_hdr(skb
)->daddr
,
735 put_cmsg(msg
, SOL_IPV6
, IPV6_ORIGDSTADDR
, sizeof(sin6
), &sin6
);
738 if (np
->rxopt
.bits
.recvfragsize
&& opt
->frag_max_size
) {
739 int val
= opt
->frag_max_size
;
741 put_cmsg(msg
, SOL_IPV6
, IPV6_RECVFRAGSIZE
, sizeof(val
), &val
);
745 void ip6_datagram_recv_ctl(struct sock
*sk
, struct msghdr
*msg
,
748 ip6_datagram_recv_common_ctl(sk
, msg
, skb
);
749 ip6_datagram_recv_specific_ctl(sk
, msg
, skb
);
751 EXPORT_SYMBOL_GPL(ip6_datagram_recv_ctl
);
753 int ip6_datagram_send_ctl(struct net
*net
, struct sock
*sk
,
754 struct msghdr
*msg
, struct flowi6
*fl6
,
755 struct ipcm6_cookie
*ipc6
)
757 struct in6_pktinfo
*src_info
;
758 struct cmsghdr
*cmsg
;
759 struct ipv6_rt_hdr
*rthdr
;
760 struct ipv6_opt_hdr
*hdr
;
761 struct ipv6_txoptions
*opt
= ipc6
->opt
;
765 for_each_cmsghdr(cmsg
, msg
) {
768 if (!CMSG_OK(msg
, cmsg
)) {
773 if (cmsg
->cmsg_level
== SOL_SOCKET
) {
774 err
= __sock_cmsg_send(sk
, msg
, cmsg
, &ipc6
->sockc
);
780 if (cmsg
->cmsg_level
!= SOL_IPV6
)
783 switch (cmsg
->cmsg_type
) {
785 case IPV6_2292PKTINFO
:
787 struct net_device
*dev
= NULL
;
790 if (cmsg
->cmsg_len
< CMSG_LEN(sizeof(struct in6_pktinfo
))) {
795 src_info
= (struct in6_pktinfo
*)CMSG_DATA(cmsg
);
796 src_idx
= src_info
->ipi6_ifindex
;
799 if (fl6
->flowi6_oif
&&
800 src_idx
!= fl6
->flowi6_oif
&&
801 (sk
->sk_bound_dev_if
!= fl6
->flowi6_oif
||
802 !sk_dev_equal_l3scope(sk
, src_idx
)))
804 fl6
->flowi6_oif
= src_idx
;
807 addr_type
= __ipv6_addr_type(&src_info
->ipi6_addr
);
810 if (fl6
->flowi6_oif
) {
811 dev
= dev_get_by_index_rcu(net
, fl6
->flowi6_oif
);
816 } else if (addr_type
& IPV6_ADDR_LINKLOCAL
) {
821 if (addr_type
!= IPV6_ADDR_ANY
) {
822 int strict
= __ipv6_addr_src_scope(addr_type
) <= IPV6_ADDR_SCOPE_LINKLOCAL
;
823 if (!ipv6_can_nonlocal_bind(net
, inet_sk(sk
)) &&
824 !ipv6_chk_addr_and_flags(net
, &src_info
->ipi6_addr
,
827 !ipv6_chk_acast_addr_src(net
, dev
,
828 &src_info
->ipi6_addr
))
831 fl6
->saddr
= src_info
->ipi6_addr
;
843 if (cmsg
->cmsg_len
< CMSG_LEN(4)) {
848 if (fl6
->flowlabel
&IPV6_FLOWINFO_MASK
) {
849 if ((fl6
->flowlabel
^*(__be32
*)CMSG_DATA(cmsg
))&~IPV6_FLOWINFO_MASK
) {
854 fl6
->flowlabel
= IPV6_FLOWINFO_MASK
& *(__be32
*)CMSG_DATA(cmsg
);
857 case IPV6_2292HOPOPTS
:
859 if (opt
->hopopt
|| cmsg
->cmsg_len
< CMSG_LEN(sizeof(struct ipv6_opt_hdr
))) {
864 hdr
= (struct ipv6_opt_hdr
*)CMSG_DATA(cmsg
);
865 len
= ((hdr
->hdrlen
+ 1) << 3);
866 if (cmsg
->cmsg_len
< CMSG_LEN(len
)) {
870 if (!ns_capable(net
->user_ns
, CAP_NET_RAW
)) {
874 opt
->opt_nflen
+= len
;
878 case IPV6_2292DSTOPTS
:
879 if (cmsg
->cmsg_len
< CMSG_LEN(sizeof(struct ipv6_opt_hdr
))) {
884 hdr
= (struct ipv6_opt_hdr
*)CMSG_DATA(cmsg
);
885 len
= ((hdr
->hdrlen
+ 1) << 3);
886 if (cmsg
->cmsg_len
< CMSG_LEN(len
)) {
890 if (!ns_capable(net
->user_ns
, CAP_NET_RAW
)) {
898 opt
->opt_flen
+= len
;
903 case IPV6_RTHDRDSTOPTS
:
904 if (cmsg
->cmsg_len
< CMSG_LEN(sizeof(struct ipv6_opt_hdr
))) {
909 hdr
= (struct ipv6_opt_hdr
*)CMSG_DATA(cmsg
);
910 len
= ((hdr
->hdrlen
+ 1) << 3);
911 if (cmsg
->cmsg_len
< CMSG_LEN(len
)) {
915 if (!ns_capable(net
->user_ns
, CAP_NET_RAW
)) {
919 if (cmsg
->cmsg_type
== IPV6_DSTOPTS
) {
920 opt
->opt_flen
+= len
;
923 opt
->opt_nflen
+= len
;
930 if (cmsg
->cmsg_len
< CMSG_LEN(sizeof(struct ipv6_rt_hdr
))) {
935 rthdr
= (struct ipv6_rt_hdr
*)CMSG_DATA(cmsg
);
937 switch (rthdr
->type
) {
938 #if IS_ENABLED(CONFIG_IPV6_MIP6)
939 case IPV6_SRCRT_TYPE_2
:
940 if (rthdr
->hdrlen
!= 2 ||
941 rthdr
->segments_left
!= 1) {
952 len
= ((rthdr
->hdrlen
+ 1) << 3);
954 if (cmsg
->cmsg_len
< CMSG_LEN(len
)) {
959 /* segments left must also match */
960 if ((rthdr
->hdrlen
>> 1) != rthdr
->segments_left
) {
965 opt
->opt_nflen
+= len
;
968 if (cmsg
->cmsg_type
== IPV6_2292RTHDR
&& opt
->dst1opt
) {
969 int dsthdrlen
= ((opt
->dst1opt
->hdrlen
+1)<<3);
971 opt
->opt_nflen
+= dsthdrlen
;
972 opt
->dst0opt
= opt
->dst1opt
;
974 opt
->opt_flen
-= dsthdrlen
;
979 case IPV6_2292HOPLIMIT
:
981 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(int))) {
986 ipc6
->hlimit
= *(int *)CMSG_DATA(cmsg
);
987 if (ipc6
->hlimit
< -1 || ipc6
->hlimit
> 0xff) {
999 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(int)))
1002 tc
= *(int *)CMSG_DATA(cmsg
);
1003 if (tc
< -1 || tc
> 0xff)
1017 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(int)))
1020 df
= *(int *)CMSG_DATA(cmsg
);
1021 if (df
< 0 || df
> 1)
1025 ipc6
->dontfrag
= df
;
1030 net_dbg_ratelimited("invalid cmsg type: %d\n",
1040 EXPORT_SYMBOL_GPL(ip6_datagram_send_ctl
);
1042 void __ip6_dgram_sock_seq_show(struct seq_file
*seq
, struct sock
*sp
,
1043 __u16 srcp
, __u16 destp
, int rqueue
, int bucket
)
1045 const struct in6_addr
*dest
, *src
;
1047 dest
= &sp
->sk_v6_daddr
;
1048 src
= &sp
->sk_v6_rcv_saddr
;
1050 "%5d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
1051 "%02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %u\n",
1053 src
->s6_addr32
[0], src
->s6_addr32
[1],
1054 src
->s6_addr32
[2], src
->s6_addr32
[3], srcp
,
1055 dest
->s6_addr32
[0], dest
->s6_addr32
[1],
1056 dest
->s6_addr32
[2], dest
->s6_addr32
[3], destp
,
1058 sk_wmem_alloc_get(sp
),
1061 from_kuid_munged(seq_user_ns(seq
), sock_i_uid(sp
)),
1064 refcount_read(&sp
->sk_refcnt
), sp
,
1065 atomic_read(&sp
->sk_drops
));