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(sock_net(sk
), 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 int __ip6_datagram_connect(struct sock
*sk
, struct sockaddr
*uaddr
,
145 struct sockaddr_in6
*usin
= (struct sockaddr_in6
*) uaddr
;
146 struct inet_sock
*inet
= inet_sk(sk
);
147 struct ipv6_pinfo
*np
= inet6_sk(sk
);
148 struct in6_addr
*daddr
, old_daddr
;
149 __be32 fl6_flowlabel
= 0;
150 __be32 old_fl6_flowlabel
;
155 if (usin
->sin6_family
== AF_INET
) {
156 if (__ipv6_only_sock(sk
))
157 return -EAFNOSUPPORT
;
158 err
= __ip4_datagram_connect(sk
, uaddr
, addr_len
);
162 if (addr_len
< SIN6_LEN_RFC2133
)
165 if (usin
->sin6_family
!= AF_INET6
)
166 return -EAFNOSUPPORT
;
169 fl6_flowlabel
= usin
->sin6_flowinfo
& IPV6_FLOWINFO_MASK
;
171 if (ipv6_addr_any(&usin
->sin6_addr
)) {
175 if (ipv6_addr_v4mapped(&sk
->sk_v6_rcv_saddr
))
176 ipv6_addr_set_v4mapped(htonl(INADDR_LOOPBACK
),
179 usin
->sin6_addr
= in6addr_loopback
;
182 addr_type
= ipv6_addr_type(&usin
->sin6_addr
);
184 daddr
= &usin
->sin6_addr
;
186 if (addr_type
& IPV6_ADDR_MAPPED
) {
187 struct sockaddr_in sin
;
189 if (__ipv6_only_sock(sk
)) {
193 sin
.sin_family
= AF_INET
;
194 sin
.sin_addr
.s_addr
= daddr
->s6_addr32
[3];
195 sin
.sin_port
= usin
->sin6_port
;
197 err
= __ip4_datagram_connect(sk
,
198 (struct sockaddr
*) &sin
,
205 ipv6_addr_set_v4mapped(inet
->inet_daddr
, &sk
->sk_v6_daddr
);
207 if (ipv6_addr_any(&np
->saddr
) ||
208 ipv6_mapped_addr_any(&np
->saddr
))
209 ipv6_addr_set_v4mapped(inet
->inet_saddr
, &np
->saddr
);
211 if (ipv6_addr_any(&sk
->sk_v6_rcv_saddr
) ||
212 ipv6_mapped_addr_any(&sk
->sk_v6_rcv_saddr
)) {
213 ipv6_addr_set_v4mapped(inet
->inet_rcv_saddr
,
214 &sk
->sk_v6_rcv_saddr
);
215 if (sk
->sk_prot
->rehash
)
216 sk
->sk_prot
->rehash(sk
);
222 if (__ipv6_addr_needs_scope_id(addr_type
)) {
223 if (addr_len
>= sizeof(struct sockaddr_in6
) &&
224 usin
->sin6_scope_id
) {
225 if (sk
->sk_bound_dev_if
&&
226 sk
->sk_bound_dev_if
!= usin
->sin6_scope_id
) {
230 sk
->sk_bound_dev_if
= usin
->sin6_scope_id
;
233 if (!sk
->sk_bound_dev_if
&& (addr_type
& IPV6_ADDR_MULTICAST
))
234 sk
->sk_bound_dev_if
= np
->mcast_oif
;
236 /* Connect to link-local address requires an interface */
237 if (!sk
->sk_bound_dev_if
) {
243 /* save the current peer information before updating it */
244 old_daddr
= sk
->sk_v6_daddr
;
245 old_fl6_flowlabel
= np
->flow_label
;
246 old_dport
= inet
->inet_dport
;
248 sk
->sk_v6_daddr
= *daddr
;
249 np
->flow_label
= fl6_flowlabel
;
250 inet
->inet_dport
= usin
->sin6_port
;
253 * Check for a route to destination an obtain the
254 * destination cache for it.
257 err
= ip6_datagram_dst_update(sk
, true);
259 /* Restore the socket peer info, to keep it consistent with
260 * the old socket state
262 sk
->sk_v6_daddr
= old_daddr
;
263 np
->flow_label
= old_fl6_flowlabel
;
264 inet
->inet_dport
= old_dport
;
268 sk
->sk_state
= TCP_ESTABLISHED
;
273 EXPORT_SYMBOL_GPL(__ip6_datagram_connect
);
275 int ip6_datagram_connect(struct sock
*sk
, struct sockaddr
*uaddr
, int addr_len
)
280 res
= __ip6_datagram_connect(sk
, uaddr
, addr_len
);
284 EXPORT_SYMBOL_GPL(ip6_datagram_connect
);
286 int ip6_datagram_connect_v6_only(struct sock
*sk
, struct sockaddr
*uaddr
,
289 DECLARE_SOCKADDR(struct sockaddr_in6
*, sin6
, uaddr
);
290 if (sin6
->sin6_family
!= AF_INET6
)
291 return -EAFNOSUPPORT
;
292 return ip6_datagram_connect(sk
, uaddr
, addr_len
);
294 EXPORT_SYMBOL_GPL(ip6_datagram_connect_v6_only
);
296 void ipv6_icmp_error(struct sock
*sk
, struct sk_buff
*skb
, int err
,
297 __be16 port
, u32 info
, u8
*payload
)
299 struct ipv6_pinfo
*np
= inet6_sk(sk
);
300 struct icmp6hdr
*icmph
= icmp6_hdr(skb
);
301 struct sock_exterr_skb
*serr
;
306 skb
= skb_clone(skb
, GFP_ATOMIC
);
310 skb
->protocol
= htons(ETH_P_IPV6
);
312 serr
= SKB_EXT_ERR(skb
);
313 serr
->ee
.ee_errno
= err
;
314 serr
->ee
.ee_origin
= SO_EE_ORIGIN_ICMP6
;
315 serr
->ee
.ee_type
= icmph
->icmp6_type
;
316 serr
->ee
.ee_code
= icmph
->icmp6_code
;
318 serr
->ee
.ee_info
= info
;
319 serr
->ee
.ee_data
= 0;
320 serr
->addr_offset
= (u8
*)&(((struct ipv6hdr
*)(icmph
+ 1))->daddr
) -
321 skb_network_header(skb
);
324 __skb_pull(skb
, payload
- skb
->data
);
325 skb_reset_transport_header(skb
);
327 if (sock_queue_err_skb(sk
, skb
))
331 void ipv6_local_error(struct sock
*sk
, int err
, struct flowi6
*fl6
, u32 info
)
333 const struct ipv6_pinfo
*np
= inet6_sk(sk
);
334 struct sock_exterr_skb
*serr
;
341 skb
= alloc_skb(sizeof(struct ipv6hdr
), GFP_ATOMIC
);
345 skb
->protocol
= htons(ETH_P_IPV6
);
347 skb_put(skb
, sizeof(struct ipv6hdr
));
348 skb_reset_network_header(skb
);
350 iph
->daddr
= fl6
->daddr
;
351 ip6_flow_hdr(iph
, 0, 0);
353 serr
= SKB_EXT_ERR(skb
);
354 serr
->ee
.ee_errno
= err
;
355 serr
->ee
.ee_origin
= SO_EE_ORIGIN_LOCAL
;
356 serr
->ee
.ee_type
= 0;
357 serr
->ee
.ee_code
= 0;
359 serr
->ee
.ee_info
= info
;
360 serr
->ee
.ee_data
= 0;
361 serr
->addr_offset
= (u8
*)&iph
->daddr
- skb_network_header(skb
);
362 serr
->port
= fl6
->fl6_dport
;
364 __skb_pull(skb
, skb_tail_pointer(skb
) - skb
->data
);
365 skb_reset_transport_header(skb
);
367 if (sock_queue_err_skb(sk
, skb
))
371 void ipv6_local_rxpmtu(struct sock
*sk
, struct flowi6
*fl6
, u32 mtu
)
373 struct ipv6_pinfo
*np
= inet6_sk(sk
);
376 struct ip6_mtuinfo
*mtu_info
;
378 if (!np
->rxopt
.bits
.rxpmtu
)
381 skb
= alloc_skb(sizeof(struct ipv6hdr
), GFP_ATOMIC
);
385 skb_put(skb
, sizeof(struct ipv6hdr
));
386 skb_reset_network_header(skb
);
388 iph
->daddr
= fl6
->daddr
;
390 mtu_info
= IP6CBMTU(skb
);
392 mtu_info
->ip6m_mtu
= mtu
;
393 mtu_info
->ip6m_addr
.sin6_family
= AF_INET6
;
394 mtu_info
->ip6m_addr
.sin6_port
= 0;
395 mtu_info
->ip6m_addr
.sin6_flowinfo
= 0;
396 mtu_info
->ip6m_addr
.sin6_scope_id
= fl6
->flowi6_oif
;
397 mtu_info
->ip6m_addr
.sin6_addr
= ipv6_hdr(skb
)->daddr
;
399 __skb_pull(skb
, skb_tail_pointer(skb
) - skb
->data
);
400 skb_reset_transport_header(skb
);
402 skb
= xchg(&np
->rxpmtu
, skb
);
406 /* For some errors we have valid addr_offset even with zero payload and
407 * zero port. Also, addr_offset should be supported if port is set.
409 static inline bool ipv6_datagram_support_addr(struct sock_exterr_skb
*serr
)
411 return serr
->ee
.ee_origin
== SO_EE_ORIGIN_ICMP6
||
412 serr
->ee
.ee_origin
== SO_EE_ORIGIN_ICMP
||
413 serr
->ee
.ee_origin
== SO_EE_ORIGIN_LOCAL
|| serr
->port
;
416 /* IPv6 supports cmsg on all origins aside from SO_EE_ORIGIN_LOCAL.
418 * At one point, excluding local errors was a quick test to identify icmp/icmp6
419 * errors. This is no longer true, but the test remained, so the v6 stack,
420 * unlike v4, also honors cmsg requests on all wifi and timestamp errors.
422 static bool ip6_datagram_support_cmsg(struct sk_buff
*skb
,
423 struct sock_exterr_skb
*serr
)
425 if (serr
->ee
.ee_origin
== SO_EE_ORIGIN_ICMP
||
426 serr
->ee
.ee_origin
== SO_EE_ORIGIN_ICMP6
)
429 if (serr
->ee
.ee_origin
== SO_EE_ORIGIN_LOCAL
)
432 if (!IP6CB(skb
)->iif
)
439 * Handle MSG_ERRQUEUE
441 int ipv6_recv_error(struct sock
*sk
, struct msghdr
*msg
, int len
, int *addr_len
)
443 struct ipv6_pinfo
*np
= inet6_sk(sk
);
444 struct sock_exterr_skb
*serr
;
446 DECLARE_SOCKADDR(struct sockaddr_in6
*, sin
, msg
->msg_name
);
448 struct sock_extended_err ee
;
449 struct sockaddr_in6 offender
;
455 skb
= sock_dequeue_err_skb(sk
);
461 msg
->msg_flags
|= MSG_TRUNC
;
464 err
= skb_copy_datagram_msg(skb
, 0, msg
, copied
);
469 sock_recv_timestamp(msg
, sk
, skb
);
471 serr
= SKB_EXT_ERR(skb
);
473 if (sin
&& ipv6_datagram_support_addr(serr
)) {
474 const unsigned char *nh
= skb_network_header(skb
);
475 sin
->sin6_family
= AF_INET6
;
476 sin
->sin6_flowinfo
= 0;
477 sin
->sin6_port
= serr
->port
;
478 if (skb
->protocol
== htons(ETH_P_IPV6
)) {
479 const struct ipv6hdr
*ip6h
= container_of((struct in6_addr
*)(nh
+ serr
->addr_offset
),
480 struct ipv6hdr
, daddr
);
481 sin
->sin6_addr
= ip6h
->daddr
;
483 sin
->sin6_flowinfo
= ip6_flowinfo(ip6h
);
485 ipv6_iface_scope_id(&sin
->sin6_addr
,
488 ipv6_addr_set_v4mapped(*(__be32
*)(nh
+ serr
->addr_offset
),
490 sin
->sin6_scope_id
= 0;
492 *addr_len
= sizeof(*sin
);
495 memcpy(&errhdr
.ee
, &serr
->ee
, sizeof(struct sock_extended_err
));
496 sin
= &errhdr
.offender
;
497 memset(sin
, 0, sizeof(*sin
));
499 if (ip6_datagram_support_cmsg(skb
, serr
)) {
500 sin
->sin6_family
= AF_INET6
;
502 ip6_datagram_recv_common_ctl(sk
, msg
, skb
);
503 if (skb
->protocol
== htons(ETH_P_IPV6
)) {
504 sin
->sin6_addr
= ipv6_hdr(skb
)->saddr
;
506 ip6_datagram_recv_specific_ctl(sk
, msg
, skb
);
508 ipv6_iface_scope_id(&sin
->sin6_addr
,
511 ipv6_addr_set_v4mapped(ip_hdr(skb
)->saddr
,
513 if (inet_sk(sk
)->cmsg_flags
)
514 ip_cmsg_recv(msg
, skb
);
518 put_cmsg(msg
, SOL_IPV6
, IPV6_RECVERR
, sizeof(errhdr
), &errhdr
);
520 /* Now we could try to dump offended packet options */
522 msg
->msg_flags
|= MSG_ERRQUEUE
;
529 EXPORT_SYMBOL_GPL(ipv6_recv_error
);
532 * Handle IPV6_RECVPATHMTU
534 int ipv6_recv_rxpmtu(struct sock
*sk
, struct msghdr
*msg
, int len
,
537 struct ipv6_pinfo
*np
= inet6_sk(sk
);
539 struct ip6_mtuinfo mtu_info
;
540 DECLARE_SOCKADDR(struct sockaddr_in6
*, sin
, msg
->msg_name
);
545 skb
= xchg(&np
->rxpmtu
, NULL
);
551 msg
->msg_flags
|= MSG_TRUNC
;
554 err
= skb_copy_datagram_msg(skb
, 0, msg
, copied
);
558 sock_recv_timestamp(msg
, sk
, skb
);
560 memcpy(&mtu_info
, IP6CBMTU(skb
), sizeof(mtu_info
));
563 sin
->sin6_family
= AF_INET6
;
564 sin
->sin6_flowinfo
= 0;
566 sin
->sin6_scope_id
= mtu_info
.ip6m_addr
.sin6_scope_id
;
567 sin
->sin6_addr
= mtu_info
.ip6m_addr
.sin6_addr
;
568 *addr_len
= sizeof(*sin
);
571 put_cmsg(msg
, SOL_IPV6
, IPV6_PATHMTU
, sizeof(mtu_info
), &mtu_info
);
582 void ip6_datagram_recv_common_ctl(struct sock
*sk
, struct msghdr
*msg
,
585 struct ipv6_pinfo
*np
= inet6_sk(sk
);
586 bool is_ipv6
= skb
->protocol
== htons(ETH_P_IPV6
);
588 if (np
->rxopt
.bits
.rxinfo
) {
589 struct in6_pktinfo src_info
;
592 src_info
.ipi6_ifindex
= IP6CB(skb
)->iif
;
593 src_info
.ipi6_addr
= ipv6_hdr(skb
)->daddr
;
595 src_info
.ipi6_ifindex
=
596 PKTINFO_SKB_CB(skb
)->ipi_ifindex
;
597 ipv6_addr_set_v4mapped(ip_hdr(skb
)->daddr
,
598 &src_info
.ipi6_addr
);
601 if (src_info
.ipi6_ifindex
>= 0)
602 put_cmsg(msg
, SOL_IPV6
, IPV6_PKTINFO
,
603 sizeof(src_info
), &src_info
);
607 void ip6_datagram_recv_specific_ctl(struct sock
*sk
, struct msghdr
*msg
,
610 struct ipv6_pinfo
*np
= inet6_sk(sk
);
611 struct inet6_skb_parm
*opt
= IP6CB(skb
);
612 unsigned char *nh
= skb_network_header(skb
);
614 if (np
->rxopt
.bits
.rxhlim
) {
615 int hlim
= ipv6_hdr(skb
)->hop_limit
;
616 put_cmsg(msg
, SOL_IPV6
, IPV6_HOPLIMIT
, sizeof(hlim
), &hlim
);
619 if (np
->rxopt
.bits
.rxtclass
) {
620 int tclass
= ipv6_get_dsfield(ipv6_hdr(skb
));
621 put_cmsg(msg
, SOL_IPV6
, IPV6_TCLASS
, sizeof(tclass
), &tclass
);
624 if (np
->rxopt
.bits
.rxflow
) {
625 __be32 flowinfo
= ip6_flowinfo((struct ipv6hdr
*)nh
);
627 put_cmsg(msg
, SOL_IPV6
, IPV6_FLOWINFO
, sizeof(flowinfo
), &flowinfo
);
630 /* HbH is allowed only once */
631 if (np
->rxopt
.bits
.hopopts
&& (opt
->flags
& IP6SKB_HOPBYHOP
)) {
632 u8
*ptr
= nh
+ sizeof(struct ipv6hdr
);
633 put_cmsg(msg
, SOL_IPV6
, IPV6_HOPOPTS
, (ptr
[1]+1)<<3, ptr
);
637 (np
->rxopt
.bits
.dstopts
|| np
->rxopt
.bits
.srcrt
)) {
639 * Silly enough, but we need to reparse in order to
640 * report extension headers (except for HbH)
643 * Also note that IPV6_RECVRTHDRDSTOPTS is NOT
644 * (and WILL NOT be) defined because
645 * IPV6_RECVDSTOPTS is more generic. --yoshfuji
647 unsigned int off
= sizeof(struct ipv6hdr
);
648 u8 nexthdr
= ipv6_hdr(skb
)->nexthdr
;
650 while (off
<= opt
->lastopt
) {
655 case IPPROTO_DSTOPTS
:
657 len
= (ptr
[1] + 1) << 3;
658 if (np
->rxopt
.bits
.dstopts
)
659 put_cmsg(msg
, SOL_IPV6
, IPV6_DSTOPTS
, len
, ptr
);
661 case IPPROTO_ROUTING
:
663 len
= (ptr
[1] + 1) << 3;
664 if (np
->rxopt
.bits
.srcrt
)
665 put_cmsg(msg
, SOL_IPV6
, IPV6_RTHDR
, len
, ptr
);
669 len
= (ptr
[1] + 2) << 2;
673 len
= (ptr
[1] + 1) << 3;
681 /* socket options in old style */
682 if (np
->rxopt
.bits
.rxoinfo
) {
683 struct in6_pktinfo src_info
;
685 src_info
.ipi6_ifindex
= opt
->iif
;
686 src_info
.ipi6_addr
= ipv6_hdr(skb
)->daddr
;
687 put_cmsg(msg
, SOL_IPV6
, IPV6_2292PKTINFO
, sizeof(src_info
), &src_info
);
689 if (np
->rxopt
.bits
.rxohlim
) {
690 int hlim
= ipv6_hdr(skb
)->hop_limit
;
691 put_cmsg(msg
, SOL_IPV6
, IPV6_2292HOPLIMIT
, sizeof(hlim
), &hlim
);
693 if (np
->rxopt
.bits
.ohopopts
&& (opt
->flags
& IP6SKB_HOPBYHOP
)) {
694 u8
*ptr
= nh
+ sizeof(struct ipv6hdr
);
695 put_cmsg(msg
, SOL_IPV6
, IPV6_2292HOPOPTS
, (ptr
[1]+1)<<3, ptr
);
697 if (np
->rxopt
.bits
.odstopts
&& opt
->dst0
) {
698 u8
*ptr
= nh
+ opt
->dst0
;
699 put_cmsg(msg
, SOL_IPV6
, IPV6_2292DSTOPTS
, (ptr
[1]+1)<<3, ptr
);
701 if (np
->rxopt
.bits
.osrcrt
&& opt
->srcrt
) {
702 struct ipv6_rt_hdr
*rthdr
= (struct ipv6_rt_hdr
*)(nh
+ opt
->srcrt
);
703 put_cmsg(msg
, SOL_IPV6
, IPV6_2292RTHDR
, (rthdr
->hdrlen
+1) << 3, rthdr
);
705 if (np
->rxopt
.bits
.odstopts
&& opt
->dst1
) {
706 u8
*ptr
= nh
+ opt
->dst1
;
707 put_cmsg(msg
, SOL_IPV6
, IPV6_2292DSTOPTS
, (ptr
[1]+1)<<3, ptr
);
709 if (np
->rxopt
.bits
.rxorigdstaddr
) {
710 struct sockaddr_in6 sin6
;
711 __be16 _ports
[2], *ports
;
713 ports
= skb_header_pointer(skb
, skb_transport_offset(skb
),
714 sizeof(_ports
), &_ports
);
716 /* All current transport protocols have the port numbers in the
717 * first four bytes of the transport header and this function is
718 * written with this assumption in mind.
720 sin6
.sin6_family
= AF_INET6
;
721 sin6
.sin6_addr
= ipv6_hdr(skb
)->daddr
;
722 sin6
.sin6_port
= ports
[1];
723 sin6
.sin6_flowinfo
= 0;
725 ipv6_iface_scope_id(&ipv6_hdr(skb
)->daddr
,
728 put_cmsg(msg
, SOL_IPV6
, IPV6_ORIGDSTADDR
, sizeof(sin6
), &sin6
);
733 void ip6_datagram_recv_ctl(struct sock
*sk
, struct msghdr
*msg
,
736 ip6_datagram_recv_common_ctl(sk
, msg
, skb
);
737 ip6_datagram_recv_specific_ctl(sk
, msg
, skb
);
739 EXPORT_SYMBOL_GPL(ip6_datagram_recv_ctl
);
741 int ip6_datagram_send_ctl(struct net
*net
, struct sock
*sk
,
742 struct msghdr
*msg
, struct flowi6
*fl6
,
743 struct ipcm6_cookie
*ipc6
, struct sockcm_cookie
*sockc
)
745 struct in6_pktinfo
*src_info
;
746 struct cmsghdr
*cmsg
;
747 struct ipv6_rt_hdr
*rthdr
;
748 struct ipv6_opt_hdr
*hdr
;
749 struct ipv6_txoptions
*opt
= ipc6
->opt
;
753 for_each_cmsghdr(cmsg
, msg
) {
756 if (!CMSG_OK(msg
, cmsg
)) {
761 if (cmsg
->cmsg_level
== SOL_SOCKET
) {
762 err
= __sock_cmsg_send(sk
, msg
, cmsg
, sockc
);
768 if (cmsg
->cmsg_level
!= SOL_IPV6
)
771 switch (cmsg
->cmsg_type
) {
773 case IPV6_2292PKTINFO
:
775 struct net_device
*dev
= NULL
;
777 if (cmsg
->cmsg_len
< CMSG_LEN(sizeof(struct in6_pktinfo
))) {
782 src_info
= (struct in6_pktinfo
*)CMSG_DATA(cmsg
);
784 if (src_info
->ipi6_ifindex
) {
785 if (fl6
->flowi6_oif
&&
786 src_info
->ipi6_ifindex
!= fl6
->flowi6_oif
)
788 fl6
->flowi6_oif
= src_info
->ipi6_ifindex
;
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 (!(inet_sk(sk
)->freebind
|| inet_sk(sk
)->transparent
) &&
808 !ipv6_chk_addr(net
, &src_info
->ipi6_addr
,
809 strict
? dev
: NULL
, 0) &&
810 !ipv6_chk_acast_addr_src(net
, dev
,
811 &src_info
->ipi6_addr
))
814 fl6
->saddr
= src_info
->ipi6_addr
;
826 if (cmsg
->cmsg_len
< CMSG_LEN(4)) {
831 if (fl6
->flowlabel
&IPV6_FLOWINFO_MASK
) {
832 if ((fl6
->flowlabel
^*(__be32
*)CMSG_DATA(cmsg
))&~IPV6_FLOWINFO_MASK
) {
837 fl6
->flowlabel
= IPV6_FLOWINFO_MASK
& *(__be32
*)CMSG_DATA(cmsg
);
840 case IPV6_2292HOPOPTS
:
842 if (opt
->hopopt
|| cmsg
->cmsg_len
< CMSG_LEN(sizeof(struct ipv6_opt_hdr
))) {
847 hdr
= (struct ipv6_opt_hdr
*)CMSG_DATA(cmsg
);
848 len
= ((hdr
->hdrlen
+ 1) << 3);
849 if (cmsg
->cmsg_len
< CMSG_LEN(len
)) {
853 if (!ns_capable(net
->user_ns
, CAP_NET_RAW
)) {
857 opt
->opt_nflen
+= len
;
861 case IPV6_2292DSTOPTS
:
862 if (cmsg
->cmsg_len
< CMSG_LEN(sizeof(struct ipv6_opt_hdr
))) {
867 hdr
= (struct ipv6_opt_hdr
*)CMSG_DATA(cmsg
);
868 len
= ((hdr
->hdrlen
+ 1) << 3);
869 if (cmsg
->cmsg_len
< CMSG_LEN(len
)) {
873 if (!ns_capable(net
->user_ns
, CAP_NET_RAW
)) {
881 opt
->opt_flen
+= len
;
886 case IPV6_RTHDRDSTOPTS
:
887 if (cmsg
->cmsg_len
< CMSG_LEN(sizeof(struct ipv6_opt_hdr
))) {
892 hdr
= (struct ipv6_opt_hdr
*)CMSG_DATA(cmsg
);
893 len
= ((hdr
->hdrlen
+ 1) << 3);
894 if (cmsg
->cmsg_len
< CMSG_LEN(len
)) {
898 if (!ns_capable(net
->user_ns
, CAP_NET_RAW
)) {
902 if (cmsg
->cmsg_type
== IPV6_DSTOPTS
) {
903 opt
->opt_flen
+= len
;
906 opt
->opt_nflen
+= len
;
913 if (cmsg
->cmsg_len
< CMSG_LEN(sizeof(struct ipv6_rt_hdr
))) {
918 rthdr
= (struct ipv6_rt_hdr
*)CMSG_DATA(cmsg
);
920 switch (rthdr
->type
) {
921 #if IS_ENABLED(CONFIG_IPV6_MIP6)
922 case IPV6_SRCRT_TYPE_2
:
923 if (rthdr
->hdrlen
!= 2 ||
924 rthdr
->segments_left
!= 1) {
935 len
= ((rthdr
->hdrlen
+ 1) << 3);
937 if (cmsg
->cmsg_len
< CMSG_LEN(len
)) {
942 /* segments left must also match */
943 if ((rthdr
->hdrlen
>> 1) != rthdr
->segments_left
) {
948 opt
->opt_nflen
+= len
;
951 if (cmsg
->cmsg_type
== IPV6_2292RTHDR
&& opt
->dst1opt
) {
952 int dsthdrlen
= ((opt
->dst1opt
->hdrlen
+1)<<3);
954 opt
->opt_nflen
+= dsthdrlen
;
955 opt
->dst0opt
= opt
->dst1opt
;
957 opt
->opt_flen
-= dsthdrlen
;
962 case IPV6_2292HOPLIMIT
:
964 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(int))) {
969 ipc6
->hlimit
= *(int *)CMSG_DATA(cmsg
);
970 if (ipc6
->hlimit
< -1 || ipc6
->hlimit
> 0xff) {
982 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(int)))
985 tc
= *(int *)CMSG_DATA(cmsg
);
986 if (tc
< -1 || tc
> 0xff)
1000 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(int)))
1003 df
= *(int *)CMSG_DATA(cmsg
);
1004 if (df
< 0 || df
> 1)
1008 ipc6
->dontfrag
= df
;
1013 net_dbg_ratelimited("invalid cmsg type: %d\n",
1023 EXPORT_SYMBOL_GPL(ip6_datagram_send_ctl
);
1025 void ip6_dgram_sock_seq_show(struct seq_file
*seq
, struct sock
*sp
,
1026 __u16 srcp
, __u16 destp
, int bucket
)
1028 const struct in6_addr
*dest
, *src
;
1030 dest
= &sp
->sk_v6_daddr
;
1031 src
= &sp
->sk_v6_rcv_saddr
;
1033 "%5d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
1034 "%02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %d\n",
1036 src
->s6_addr32
[0], src
->s6_addr32
[1],
1037 src
->s6_addr32
[2], src
->s6_addr32
[3], srcp
,
1038 dest
->s6_addr32
[0], dest
->s6_addr32
[1],
1039 dest
->s6_addr32
[2], dest
->s6_addr32
[3], destp
,
1041 sk_wmem_alloc_get(sp
),
1042 sk_rmem_alloc_get(sp
),
1044 from_kuid_munged(seq_user_ns(seq
), sock_i_uid(sp
)),
1047 atomic_read(&sp
->sk_refcnt
), sp
,
1048 atomic_read(&sp
->sk_drops
));