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 <linux/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
;
57 fl6
->flowi6_uid
= sk
->sk_uid
;
60 fl6
->flowi6_oif
= np
->sticky_pktinfo
.ipi6_ifindex
;
62 if (!fl6
->flowi6_oif
&& ipv6_addr_is_multicast(&fl6
->daddr
))
63 fl6
->flowi6_oif
= np
->mcast_oif
;
65 security_sk_classify_flow(sk
, flowi6_to_flowi(fl6
));
68 int ip6_datagram_dst_update(struct sock
*sk
, bool fix_sk_saddr
)
70 struct ip6_flowlabel
*flowlabel
= NULL
;
71 struct in6_addr
*final_p
, final
;
72 struct ipv6_txoptions
*opt
;
73 struct dst_entry
*dst
;
74 struct inet_sock
*inet
= inet_sk(sk
);
75 struct ipv6_pinfo
*np
= inet6_sk(sk
);
79 if (np
->sndflow
&& (np
->flow_label
& IPV6_FLOWLABEL_MASK
)) {
80 flowlabel
= fl6_sock_lookup(sk
, np
->flow_label
);
84 ip6_datagram_flow_key_init(&fl6
, sk
);
87 opt
= flowlabel
? flowlabel
->opt
: rcu_dereference(np
->opt
);
88 final_p
= fl6_update_dst(&fl6
, opt
, &final
);
91 dst
= ip6_dst_lookup_flow(sk
, &fl6
, final_p
);
98 if (ipv6_addr_any(&np
->saddr
))
99 np
->saddr
= fl6
.saddr
;
101 if (ipv6_addr_any(&sk
->sk_v6_rcv_saddr
)) {
102 sk
->sk_v6_rcv_saddr
= fl6
.saddr
;
103 inet
->inet_rcv_saddr
= LOOPBACK4_IPV6
;
104 if (sk
->sk_prot
->rehash
)
105 sk
->sk_prot
->rehash(sk
);
109 ip6_dst_store(sk
, dst
,
110 ipv6_addr_equal(&fl6
.daddr
, &sk
->sk_v6_daddr
) ?
111 &sk
->sk_v6_daddr
: NULL
,
112 #ifdef CONFIG_IPV6_SUBTREES
113 ipv6_addr_equal(&fl6
.saddr
, &np
->saddr
) ?
119 fl6_sock_release(flowlabel
);
123 void ip6_datagram_release_cb(struct sock
*sk
)
125 struct dst_entry
*dst
;
127 if (ipv6_addr_v4mapped(&sk
->sk_v6_daddr
))
131 dst
= __sk_dst_get(sk
);
132 if (!dst
|| !dst
->obsolete
||
133 dst
->ops
->check(dst
, inet6_sk(sk
)->dst_cookie
)) {
139 ip6_datagram_dst_update(sk
, false);
141 EXPORT_SYMBOL_GPL(ip6_datagram_release_cb
);
143 int __ip6_datagram_connect(struct sock
*sk
, struct sockaddr
*uaddr
,
146 struct sockaddr_in6
*usin
= (struct sockaddr_in6
*) uaddr
;
147 struct inet_sock
*inet
= inet_sk(sk
);
148 struct ipv6_pinfo
*np
= inet6_sk(sk
);
149 struct in6_addr
*daddr
;
152 __be32 fl6_flowlabel
= 0;
154 if (usin
->sin6_family
== AF_INET
) {
155 if (__ipv6_only_sock(sk
))
156 return -EAFNOSUPPORT
;
157 err
= __ip4_datagram_connect(sk
, uaddr
, addr_len
);
161 if (addr_len
< SIN6_LEN_RFC2133
)
164 if (usin
->sin6_family
!= AF_INET6
)
165 return -EAFNOSUPPORT
;
168 fl6_flowlabel
= usin
->sin6_flowinfo
& IPV6_FLOWINFO_MASK
;
170 if (ipv6_addr_any(&usin
->sin6_addr
)) {
174 if (ipv6_addr_v4mapped(&sk
->sk_v6_rcv_saddr
))
175 ipv6_addr_set_v4mapped(htonl(INADDR_LOOPBACK
),
178 usin
->sin6_addr
= in6addr_loopback
;
181 addr_type
= ipv6_addr_type(&usin
->sin6_addr
);
183 daddr
= &usin
->sin6_addr
;
185 if (addr_type
& IPV6_ADDR_MAPPED
) {
186 struct sockaddr_in sin
;
188 if (__ipv6_only_sock(sk
)) {
192 sin
.sin_family
= AF_INET
;
193 sin
.sin_addr
.s_addr
= daddr
->s6_addr32
[3];
194 sin
.sin_port
= usin
->sin6_port
;
196 err
= __ip4_datagram_connect(sk
,
197 (struct sockaddr
*) &sin
,
204 ipv6_addr_set_v4mapped(inet
->inet_daddr
, &sk
->sk_v6_daddr
);
206 if (ipv6_addr_any(&np
->saddr
) ||
207 ipv6_mapped_addr_any(&np
->saddr
))
208 ipv6_addr_set_v4mapped(inet
->inet_saddr
, &np
->saddr
);
210 if (ipv6_addr_any(&sk
->sk_v6_rcv_saddr
) ||
211 ipv6_mapped_addr_any(&sk
->sk_v6_rcv_saddr
)) {
212 ipv6_addr_set_v4mapped(inet
->inet_rcv_saddr
,
213 &sk
->sk_v6_rcv_saddr
);
214 if (sk
->sk_prot
->rehash
)
215 sk
->sk_prot
->rehash(sk
);
221 if (__ipv6_addr_needs_scope_id(addr_type
)) {
222 if (addr_len
>= sizeof(struct sockaddr_in6
) &&
223 usin
->sin6_scope_id
) {
224 if (sk
->sk_bound_dev_if
&&
225 sk
->sk_bound_dev_if
!= usin
->sin6_scope_id
) {
229 sk
->sk_bound_dev_if
= usin
->sin6_scope_id
;
232 if (!sk
->sk_bound_dev_if
&& (addr_type
& IPV6_ADDR_MULTICAST
))
233 sk
->sk_bound_dev_if
= np
->mcast_oif
;
235 /* Connect to link-local address requires an interface */
236 if (!sk
->sk_bound_dev_if
) {
242 sk
->sk_v6_daddr
= *daddr
;
243 np
->flow_label
= fl6_flowlabel
;
245 inet
->inet_dport
= usin
->sin6_port
;
248 * Check for a route to destination an obtain the
249 * destination cache for it.
252 err
= ip6_datagram_dst_update(sk
, true);
254 /* Reset daddr and dport so that udp_v6_early_demux()
255 * fails to find this socket
257 memset(&sk
->sk_v6_daddr
, 0, sizeof(sk
->sk_v6_daddr
));
258 inet
->inet_dport
= 0;
262 sk
->sk_state
= TCP_ESTABLISHED
;
267 EXPORT_SYMBOL_GPL(__ip6_datagram_connect
);
269 int ip6_datagram_connect(struct sock
*sk
, struct sockaddr
*uaddr
, int addr_len
)
274 res
= __ip6_datagram_connect(sk
, uaddr
, addr_len
);
278 EXPORT_SYMBOL_GPL(ip6_datagram_connect
);
280 int ip6_datagram_connect_v6_only(struct sock
*sk
, struct sockaddr
*uaddr
,
283 DECLARE_SOCKADDR(struct sockaddr_in6
*, sin6
, uaddr
);
284 if (sin6
->sin6_family
!= AF_INET6
)
285 return -EAFNOSUPPORT
;
286 return ip6_datagram_connect(sk
, uaddr
, addr_len
);
288 EXPORT_SYMBOL_GPL(ip6_datagram_connect_v6_only
);
290 void ipv6_icmp_error(struct sock
*sk
, struct sk_buff
*skb
, int err
,
291 __be16 port
, u32 info
, u8
*payload
)
293 struct ipv6_pinfo
*np
= inet6_sk(sk
);
294 struct icmp6hdr
*icmph
= icmp6_hdr(skb
);
295 struct sock_exterr_skb
*serr
;
300 skb
= skb_clone(skb
, GFP_ATOMIC
);
304 skb
->protocol
= htons(ETH_P_IPV6
);
306 serr
= SKB_EXT_ERR(skb
);
307 serr
->ee
.ee_errno
= err
;
308 serr
->ee
.ee_origin
= SO_EE_ORIGIN_ICMP6
;
309 serr
->ee
.ee_type
= icmph
->icmp6_type
;
310 serr
->ee
.ee_code
= icmph
->icmp6_code
;
312 serr
->ee
.ee_info
= info
;
313 serr
->ee
.ee_data
= 0;
314 serr
->addr_offset
= (u8
*)&(((struct ipv6hdr
*)(icmph
+ 1))->daddr
) -
315 skb_network_header(skb
);
318 __skb_pull(skb
, payload
- skb
->data
);
319 skb_reset_transport_header(skb
);
321 if (sock_queue_err_skb(sk
, skb
))
325 void ipv6_local_error(struct sock
*sk
, int err
, struct flowi6
*fl6
, u32 info
)
327 const struct ipv6_pinfo
*np
= inet6_sk(sk
);
328 struct sock_exterr_skb
*serr
;
335 skb
= alloc_skb(sizeof(struct ipv6hdr
), GFP_ATOMIC
);
339 skb
->protocol
= htons(ETH_P_IPV6
);
341 skb_put(skb
, sizeof(struct ipv6hdr
));
342 skb_reset_network_header(skb
);
344 iph
->daddr
= fl6
->daddr
;
346 serr
= SKB_EXT_ERR(skb
);
347 serr
->ee
.ee_errno
= err
;
348 serr
->ee
.ee_origin
= SO_EE_ORIGIN_LOCAL
;
349 serr
->ee
.ee_type
= 0;
350 serr
->ee
.ee_code
= 0;
352 serr
->ee
.ee_info
= info
;
353 serr
->ee
.ee_data
= 0;
354 serr
->addr_offset
= (u8
*)&iph
->daddr
- skb_network_header(skb
);
355 serr
->port
= fl6
->fl6_dport
;
357 __skb_pull(skb
, skb_tail_pointer(skb
) - skb
->data
);
358 skb_reset_transport_header(skb
);
360 if (sock_queue_err_skb(sk
, skb
))
364 void ipv6_local_rxpmtu(struct sock
*sk
, struct flowi6
*fl6
, u32 mtu
)
366 struct ipv6_pinfo
*np
= inet6_sk(sk
);
369 struct ip6_mtuinfo
*mtu_info
;
371 if (!np
->rxopt
.bits
.rxpmtu
)
374 skb
= alloc_skb(sizeof(struct ipv6hdr
), GFP_ATOMIC
);
378 skb_put(skb
, sizeof(struct ipv6hdr
));
379 skb_reset_network_header(skb
);
381 iph
->daddr
= fl6
->daddr
;
383 mtu_info
= IP6CBMTU(skb
);
385 mtu_info
->ip6m_mtu
= mtu
;
386 mtu_info
->ip6m_addr
.sin6_family
= AF_INET6
;
387 mtu_info
->ip6m_addr
.sin6_port
= 0;
388 mtu_info
->ip6m_addr
.sin6_flowinfo
= 0;
389 mtu_info
->ip6m_addr
.sin6_scope_id
= fl6
->flowi6_oif
;
390 mtu_info
->ip6m_addr
.sin6_addr
= ipv6_hdr(skb
)->daddr
;
392 __skb_pull(skb
, skb_tail_pointer(skb
) - skb
->data
);
393 skb_reset_transport_header(skb
);
395 skb
= xchg(&np
->rxpmtu
, skb
);
399 /* For some errors we have valid addr_offset even with zero payload and
400 * zero port. Also, addr_offset should be supported if port is set.
402 static inline bool ipv6_datagram_support_addr(struct sock_exterr_skb
*serr
)
404 return serr
->ee
.ee_origin
== SO_EE_ORIGIN_ICMP6
||
405 serr
->ee
.ee_origin
== SO_EE_ORIGIN_ICMP
||
406 serr
->ee
.ee_origin
== SO_EE_ORIGIN_LOCAL
|| serr
->port
;
409 /* IPv6 supports cmsg on all origins aside from SO_EE_ORIGIN_LOCAL.
411 * At one point, excluding local errors was a quick test to identify icmp/icmp6
412 * errors. This is no longer true, but the test remained, so the v6 stack,
413 * unlike v4, also honors cmsg requests on all wifi and timestamp errors.
415 static bool ip6_datagram_support_cmsg(struct sk_buff
*skb
,
416 struct sock_exterr_skb
*serr
)
418 if (serr
->ee
.ee_origin
== SO_EE_ORIGIN_ICMP
||
419 serr
->ee
.ee_origin
== SO_EE_ORIGIN_ICMP6
)
422 if (serr
->ee
.ee_origin
== SO_EE_ORIGIN_LOCAL
)
425 if (!IP6CB(skb
)->iif
)
432 * Handle MSG_ERRQUEUE
434 int ipv6_recv_error(struct sock
*sk
, struct msghdr
*msg
, int len
, int *addr_len
)
436 struct ipv6_pinfo
*np
= inet6_sk(sk
);
437 struct sock_exterr_skb
*serr
;
439 DECLARE_SOCKADDR(struct sockaddr_in6
*, sin
, msg
->msg_name
);
441 struct sock_extended_err ee
;
442 struct sockaddr_in6 offender
;
448 skb
= sock_dequeue_err_skb(sk
);
454 msg
->msg_flags
|= MSG_TRUNC
;
457 err
= skb_copy_datagram_msg(skb
, 0, msg
, copied
);
462 sock_recv_timestamp(msg
, sk
, skb
);
464 serr
= SKB_EXT_ERR(skb
);
466 if (sin
&& ipv6_datagram_support_addr(serr
)) {
467 const unsigned char *nh
= skb_network_header(skb
);
468 sin
->sin6_family
= AF_INET6
;
469 sin
->sin6_flowinfo
= 0;
470 sin
->sin6_port
= serr
->port
;
471 if (skb
->protocol
== htons(ETH_P_IPV6
)) {
472 const struct ipv6hdr
*ip6h
= container_of((struct in6_addr
*)(nh
+ serr
->addr_offset
),
473 struct ipv6hdr
, daddr
);
474 sin
->sin6_addr
= ip6h
->daddr
;
476 sin
->sin6_flowinfo
= ip6_flowinfo(ip6h
);
478 ipv6_iface_scope_id(&sin
->sin6_addr
,
481 ipv6_addr_set_v4mapped(*(__be32
*)(nh
+ serr
->addr_offset
),
483 sin
->sin6_scope_id
= 0;
485 *addr_len
= sizeof(*sin
);
488 memcpy(&errhdr
.ee
, &serr
->ee
, sizeof(struct sock_extended_err
));
489 sin
= &errhdr
.offender
;
490 memset(sin
, 0, sizeof(*sin
));
492 if (ip6_datagram_support_cmsg(skb
, serr
)) {
493 sin
->sin6_family
= AF_INET6
;
495 ip6_datagram_recv_common_ctl(sk
, msg
, skb
);
496 if (skb
->protocol
== htons(ETH_P_IPV6
)) {
497 sin
->sin6_addr
= ipv6_hdr(skb
)->saddr
;
499 ip6_datagram_recv_specific_ctl(sk
, msg
, skb
);
501 ipv6_iface_scope_id(&sin
->sin6_addr
,
504 ipv6_addr_set_v4mapped(ip_hdr(skb
)->saddr
,
506 if (inet_sk(sk
)->cmsg_flags
)
507 ip_cmsg_recv(msg
, skb
);
511 put_cmsg(msg
, SOL_IPV6
, IPV6_RECVERR
, sizeof(errhdr
), &errhdr
);
513 /* Now we could try to dump offended packet options */
515 msg
->msg_flags
|= MSG_ERRQUEUE
;
522 EXPORT_SYMBOL_GPL(ipv6_recv_error
);
525 * Handle IPV6_RECVPATHMTU
527 int ipv6_recv_rxpmtu(struct sock
*sk
, struct msghdr
*msg
, int len
,
530 struct ipv6_pinfo
*np
= inet6_sk(sk
);
532 struct ip6_mtuinfo mtu_info
;
533 DECLARE_SOCKADDR(struct sockaddr_in6
*, sin
, msg
->msg_name
);
538 skb
= xchg(&np
->rxpmtu
, NULL
);
544 msg
->msg_flags
|= MSG_TRUNC
;
547 err
= skb_copy_datagram_msg(skb
, 0, msg
, copied
);
551 sock_recv_timestamp(msg
, sk
, skb
);
553 memcpy(&mtu_info
, IP6CBMTU(skb
), sizeof(mtu_info
));
556 sin
->sin6_family
= AF_INET6
;
557 sin
->sin6_flowinfo
= 0;
559 sin
->sin6_scope_id
= mtu_info
.ip6m_addr
.sin6_scope_id
;
560 sin
->sin6_addr
= mtu_info
.ip6m_addr
.sin6_addr
;
561 *addr_len
= sizeof(*sin
);
564 put_cmsg(msg
, SOL_IPV6
, IPV6_PATHMTU
, sizeof(mtu_info
), &mtu_info
);
575 void ip6_datagram_recv_common_ctl(struct sock
*sk
, struct msghdr
*msg
,
578 struct ipv6_pinfo
*np
= inet6_sk(sk
);
579 bool is_ipv6
= skb
->protocol
== htons(ETH_P_IPV6
);
581 if (np
->rxopt
.bits
.rxinfo
) {
582 struct in6_pktinfo src_info
;
585 src_info
.ipi6_ifindex
= IP6CB(skb
)->iif
;
586 src_info
.ipi6_addr
= ipv6_hdr(skb
)->daddr
;
588 src_info
.ipi6_ifindex
=
589 PKTINFO_SKB_CB(skb
)->ipi_ifindex
;
590 ipv6_addr_set_v4mapped(ip_hdr(skb
)->daddr
,
591 &src_info
.ipi6_addr
);
594 if (src_info
.ipi6_ifindex
>= 0)
595 put_cmsg(msg
, SOL_IPV6
, IPV6_PKTINFO
,
596 sizeof(src_info
), &src_info
);
600 void ip6_datagram_recv_specific_ctl(struct sock
*sk
, struct msghdr
*msg
,
603 struct ipv6_pinfo
*np
= inet6_sk(sk
);
604 struct inet6_skb_parm
*opt
= IP6CB(skb
);
605 unsigned char *nh
= skb_network_header(skb
);
607 if (np
->rxopt
.bits
.rxhlim
) {
608 int hlim
= ipv6_hdr(skb
)->hop_limit
;
609 put_cmsg(msg
, SOL_IPV6
, IPV6_HOPLIMIT
, sizeof(hlim
), &hlim
);
612 if (np
->rxopt
.bits
.rxtclass
) {
613 int tclass
= ipv6_get_dsfield(ipv6_hdr(skb
));
614 put_cmsg(msg
, SOL_IPV6
, IPV6_TCLASS
, sizeof(tclass
), &tclass
);
617 if (np
->rxopt
.bits
.rxflow
) {
618 __be32 flowinfo
= ip6_flowinfo((struct ipv6hdr
*)nh
);
620 put_cmsg(msg
, SOL_IPV6
, IPV6_FLOWINFO
, sizeof(flowinfo
), &flowinfo
);
623 /* HbH is allowed only once */
624 if (np
->rxopt
.bits
.hopopts
&& (opt
->flags
& IP6SKB_HOPBYHOP
)) {
625 u8
*ptr
= nh
+ sizeof(struct ipv6hdr
);
626 put_cmsg(msg
, SOL_IPV6
, IPV6_HOPOPTS
, (ptr
[1]+1)<<3, ptr
);
630 (np
->rxopt
.bits
.dstopts
|| np
->rxopt
.bits
.srcrt
)) {
632 * Silly enough, but we need to reparse in order to
633 * report extension headers (except for HbH)
636 * Also note that IPV6_RECVRTHDRDSTOPTS is NOT
637 * (and WILL NOT be) defined because
638 * IPV6_RECVDSTOPTS is more generic. --yoshfuji
640 unsigned int off
= sizeof(struct ipv6hdr
);
641 u8 nexthdr
= ipv6_hdr(skb
)->nexthdr
;
643 while (off
<= opt
->lastopt
) {
648 case IPPROTO_DSTOPTS
:
650 len
= (ptr
[1] + 1) << 3;
651 if (np
->rxopt
.bits
.dstopts
)
652 put_cmsg(msg
, SOL_IPV6
, IPV6_DSTOPTS
, len
, ptr
);
654 case IPPROTO_ROUTING
:
656 len
= (ptr
[1] + 1) << 3;
657 if (np
->rxopt
.bits
.srcrt
)
658 put_cmsg(msg
, SOL_IPV6
, IPV6_RTHDR
, len
, ptr
);
662 len
= (ptr
[1] + 2) << 2;
666 len
= (ptr
[1] + 1) << 3;
674 /* socket options in old style */
675 if (np
->rxopt
.bits
.rxoinfo
) {
676 struct in6_pktinfo src_info
;
678 src_info
.ipi6_ifindex
= opt
->iif
;
679 src_info
.ipi6_addr
= ipv6_hdr(skb
)->daddr
;
680 put_cmsg(msg
, SOL_IPV6
, IPV6_2292PKTINFO
, sizeof(src_info
), &src_info
);
682 if (np
->rxopt
.bits
.rxohlim
) {
683 int hlim
= ipv6_hdr(skb
)->hop_limit
;
684 put_cmsg(msg
, SOL_IPV6
, IPV6_2292HOPLIMIT
, sizeof(hlim
), &hlim
);
686 if (np
->rxopt
.bits
.ohopopts
&& (opt
->flags
& IP6SKB_HOPBYHOP
)) {
687 u8
*ptr
= nh
+ sizeof(struct ipv6hdr
);
688 put_cmsg(msg
, SOL_IPV6
, IPV6_2292HOPOPTS
, (ptr
[1]+1)<<3, ptr
);
690 if (np
->rxopt
.bits
.odstopts
&& opt
->dst0
) {
691 u8
*ptr
= nh
+ opt
->dst0
;
692 put_cmsg(msg
, SOL_IPV6
, IPV6_2292DSTOPTS
, (ptr
[1]+1)<<3, ptr
);
694 if (np
->rxopt
.bits
.osrcrt
&& opt
->srcrt
) {
695 struct ipv6_rt_hdr
*rthdr
= (struct ipv6_rt_hdr
*)(nh
+ opt
->srcrt
);
696 put_cmsg(msg
, SOL_IPV6
, IPV6_2292RTHDR
, (rthdr
->hdrlen
+1) << 3, rthdr
);
698 if (np
->rxopt
.bits
.odstopts
&& opt
->dst1
) {
699 u8
*ptr
= nh
+ opt
->dst1
;
700 put_cmsg(msg
, SOL_IPV6
, IPV6_2292DSTOPTS
, (ptr
[1]+1)<<3, ptr
);
702 if (np
->rxopt
.bits
.rxorigdstaddr
) {
703 struct sockaddr_in6 sin6
;
704 __be16
*ports
= (__be16
*) skb_transport_header(skb
);
706 if (skb_transport_offset(skb
) + 4 <= (int)skb
->len
) {
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.
712 sin6
.sin6_family
= AF_INET6
;
713 sin6
.sin6_addr
= ipv6_hdr(skb
)->daddr
;
714 sin6
.sin6_port
= ports
[1];
715 sin6
.sin6_flowinfo
= 0;
717 ipv6_iface_scope_id(&ipv6_hdr(skb
)->daddr
,
720 put_cmsg(msg
, SOL_IPV6
, IPV6_ORIGDSTADDR
, sizeof(sin6
), &sin6
);
723 if (np
->rxopt
.bits
.recvfragsize
&& opt
->frag_max_size
) {
724 int val
= opt
->frag_max_size
;
726 put_cmsg(msg
, SOL_IPV6
, IPV6_RECVFRAGSIZE
, sizeof(val
), &val
);
730 void ip6_datagram_recv_ctl(struct sock
*sk
, struct msghdr
*msg
,
733 ip6_datagram_recv_common_ctl(sk
, msg
, skb
);
734 ip6_datagram_recv_specific_ctl(sk
, msg
, skb
);
736 EXPORT_SYMBOL_GPL(ip6_datagram_recv_ctl
);
738 int ip6_datagram_send_ctl(struct net
*net
, struct sock
*sk
,
739 struct msghdr
*msg
, struct flowi6
*fl6
,
740 struct ipcm6_cookie
*ipc6
, struct sockcm_cookie
*sockc
)
742 struct in6_pktinfo
*src_info
;
743 struct cmsghdr
*cmsg
;
744 struct ipv6_rt_hdr
*rthdr
;
745 struct ipv6_opt_hdr
*hdr
;
746 struct ipv6_txoptions
*opt
= ipc6
->opt
;
750 for_each_cmsghdr(cmsg
, msg
) {
753 if (!CMSG_OK(msg
, cmsg
)) {
758 if (cmsg
->cmsg_level
== SOL_SOCKET
) {
759 err
= __sock_cmsg_send(sk
, msg
, cmsg
, sockc
);
765 if (cmsg
->cmsg_level
!= SOL_IPV6
)
768 switch (cmsg
->cmsg_type
) {
770 case IPV6_2292PKTINFO
:
772 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
);
781 if (src_info
->ipi6_ifindex
) {
782 if (fl6
->flowi6_oif
&&
783 src_info
->ipi6_ifindex
!= fl6
->flowi6_oif
)
785 fl6
->flowi6_oif
= src_info
->ipi6_ifindex
;
788 addr_type
= __ipv6_addr_type(&src_info
->ipi6_addr
);
791 if (fl6
->flowi6_oif
) {
792 dev
= dev_get_by_index_rcu(net
, fl6
->flowi6_oif
);
797 } else if (addr_type
& IPV6_ADDR_LINKLOCAL
) {
802 if (addr_type
!= IPV6_ADDR_ANY
) {
803 int strict
= __ipv6_addr_src_scope(addr_type
) <= IPV6_ADDR_SCOPE_LINKLOCAL
;
804 if (!(inet_sk(sk
)->freebind
|| inet_sk(sk
)->transparent
) &&
805 !ipv6_chk_addr(net
, &src_info
->ipi6_addr
,
806 strict
? dev
: NULL
, 0) &&
807 !ipv6_chk_acast_addr_src(net
, dev
,
808 &src_info
->ipi6_addr
))
811 fl6
->saddr
= src_info
->ipi6_addr
;
823 if (cmsg
->cmsg_len
< CMSG_LEN(4)) {
828 if (fl6
->flowlabel
&IPV6_FLOWINFO_MASK
) {
829 if ((fl6
->flowlabel
^*(__be32
*)CMSG_DATA(cmsg
))&~IPV6_FLOWINFO_MASK
) {
834 fl6
->flowlabel
= IPV6_FLOWINFO_MASK
& *(__be32
*)CMSG_DATA(cmsg
);
837 case IPV6_2292HOPOPTS
:
839 if (opt
->hopopt
|| cmsg
->cmsg_len
< CMSG_LEN(sizeof(struct ipv6_opt_hdr
))) {
844 hdr
= (struct ipv6_opt_hdr
*)CMSG_DATA(cmsg
);
845 len
= ((hdr
->hdrlen
+ 1) << 3);
846 if (cmsg
->cmsg_len
< CMSG_LEN(len
)) {
850 if (!ns_capable(net
->user_ns
, CAP_NET_RAW
)) {
854 opt
->opt_nflen
+= len
;
858 case IPV6_2292DSTOPTS
:
859 if (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
)) {
878 opt
->opt_flen
+= len
;
883 case IPV6_RTHDRDSTOPTS
:
884 if (cmsg
->cmsg_len
< CMSG_LEN(sizeof(struct ipv6_opt_hdr
))) {
889 hdr
= (struct ipv6_opt_hdr
*)CMSG_DATA(cmsg
);
890 len
= ((hdr
->hdrlen
+ 1) << 3);
891 if (cmsg
->cmsg_len
< CMSG_LEN(len
)) {
895 if (!ns_capable(net
->user_ns
, CAP_NET_RAW
)) {
899 if (cmsg
->cmsg_type
== IPV6_DSTOPTS
) {
900 opt
->opt_flen
+= len
;
903 opt
->opt_nflen
+= len
;
910 if (cmsg
->cmsg_len
< CMSG_LEN(sizeof(struct ipv6_rt_hdr
))) {
915 rthdr
= (struct ipv6_rt_hdr
*)CMSG_DATA(cmsg
);
917 switch (rthdr
->type
) {
918 #if IS_ENABLED(CONFIG_IPV6_MIP6)
919 case IPV6_SRCRT_TYPE_2
:
920 if (rthdr
->hdrlen
!= 2 ||
921 rthdr
->segments_left
!= 1) {
932 len
= ((rthdr
->hdrlen
+ 1) << 3);
934 if (cmsg
->cmsg_len
< CMSG_LEN(len
)) {
939 /* segments left must also match */
940 if ((rthdr
->hdrlen
>> 1) != rthdr
->segments_left
) {
945 opt
->opt_nflen
+= len
;
948 if (cmsg
->cmsg_type
== IPV6_2292RTHDR
&& opt
->dst1opt
) {
949 int dsthdrlen
= ((opt
->dst1opt
->hdrlen
+1)<<3);
951 opt
->opt_nflen
+= dsthdrlen
;
952 opt
->dst0opt
= opt
->dst1opt
;
954 opt
->opt_flen
-= dsthdrlen
;
959 case IPV6_2292HOPLIMIT
:
961 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(int))) {
966 ipc6
->hlimit
= *(int *)CMSG_DATA(cmsg
);
967 if (ipc6
->hlimit
< -1 || ipc6
->hlimit
> 0xff) {
979 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(int)))
982 tc
= *(int *)CMSG_DATA(cmsg
);
983 if (tc
< -1 || tc
> 0xff)
997 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(int)))
1000 df
= *(int *)CMSG_DATA(cmsg
);
1001 if (df
< 0 || df
> 1)
1005 ipc6
->dontfrag
= df
;
1010 net_dbg_ratelimited("invalid cmsg type: %d\n",
1020 EXPORT_SYMBOL_GPL(ip6_datagram_send_ctl
);
1022 void ip6_dgram_sock_seq_show(struct seq_file
*seq
, struct sock
*sp
,
1023 __u16 srcp
, __u16 destp
, int bucket
)
1025 const struct in6_addr
*dest
, *src
;
1027 dest
= &sp
->sk_v6_daddr
;
1028 src
= &sp
->sk_v6_rcv_saddr
;
1030 "%5d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
1031 "%02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %d\n",
1033 src
->s6_addr32
[0], src
->s6_addr32
[1],
1034 src
->s6_addr32
[2], src
->s6_addr32
[3], srcp
,
1035 dest
->s6_addr32
[0], dest
->s6_addr32
[1],
1036 dest
->s6_addr32
[2], dest
->s6_addr32
[3], destp
,
1038 sk_wmem_alloc_get(sp
),
1039 sk_rmem_alloc_get(sp
),
1041 from_kuid_munged(seq_user_ns(seq
), sock_i_uid(sp
)),
1044 refcount_read(&sp
->sk_refcnt
), sp
,
1045 atomic_read(&sp
->sk_drops
));