Linux 4.19.133
[linux/fpc-iii.git] / net / ipv6 / datagram.c
blob727f958dd869542ab31b3dd76908537446505245
1 /*
2 * common UDP/RAW code
3 * Linux INET6 implementation
5 * Authors:
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>
27 #include <net/ipv6.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>
34 #include <net/sock_reuseport.h>
36 #include <linux/errqueue.h>
37 #include <linux/uaccess.h>
39 static bool ipv6_mapped_addr_any(const struct in6_addr *a)
41 return ipv6_addr_v4mapped(a) && (a->s6_addr32[3] == 0);
44 static void ip6_datagram_flow_key_init(struct flowi6 *fl6, struct sock *sk)
46 struct inet_sock *inet = inet_sk(sk);
47 struct ipv6_pinfo *np = inet6_sk(sk);
49 memset(fl6, 0, sizeof(*fl6));
50 fl6->flowi6_proto = sk->sk_protocol;
51 fl6->daddr = sk->sk_v6_daddr;
52 fl6->saddr = np->saddr;
53 fl6->flowi6_oif = sk->sk_bound_dev_if;
54 fl6->flowi6_mark = sk->sk_mark;
55 fl6->fl6_dport = inet->inet_dport;
56 fl6->fl6_sport = inet->inet_sport;
57 fl6->flowlabel = np->flow_label;
58 fl6->flowi6_uid = sk->sk_uid;
60 if (!fl6->flowi6_oif)
61 fl6->flowi6_oif = np->sticky_pktinfo.ipi6_ifindex;
63 if (!fl6->flowi6_oif && ipv6_addr_is_multicast(&fl6->daddr))
64 fl6->flowi6_oif = np->mcast_oif;
66 security_sk_classify_flow(sk, flowi6_to_flowi(fl6));
69 int ip6_datagram_dst_update(struct sock *sk, bool fix_sk_saddr)
71 struct ip6_flowlabel *flowlabel = NULL;
72 struct in6_addr *final_p, final;
73 struct ipv6_txoptions *opt;
74 struct dst_entry *dst;
75 struct inet_sock *inet = inet_sk(sk);
76 struct ipv6_pinfo *np = inet6_sk(sk);
77 struct flowi6 fl6;
78 int err = 0;
80 if (np->sndflow && (np->flow_label & IPV6_FLOWLABEL_MASK)) {
81 flowlabel = fl6_sock_lookup(sk, np->flow_label);
82 if (!flowlabel)
83 return -EINVAL;
85 ip6_datagram_flow_key_init(&fl6, sk);
87 rcu_read_lock();
88 opt = flowlabel ? flowlabel->opt : rcu_dereference(np->opt);
89 final_p = fl6_update_dst(&fl6, opt, &final);
90 rcu_read_unlock();
92 dst = ip6_dst_lookup_flow(sock_net(sk), sk, &fl6, final_p);
93 if (IS_ERR(dst)) {
94 err = PTR_ERR(dst);
95 goto out;
98 if (fix_sk_saddr) {
99 if (ipv6_addr_any(&np->saddr))
100 np->saddr = fl6.saddr;
102 if (ipv6_addr_any(&sk->sk_v6_rcv_saddr)) {
103 sk->sk_v6_rcv_saddr = fl6.saddr;
104 inet->inet_rcv_saddr = LOOPBACK4_IPV6;
105 if (sk->sk_prot->rehash)
106 sk->sk_prot->rehash(sk);
110 ip6_sk_dst_store_flow(sk, dst, &fl6);
112 out:
113 fl6_sock_release(flowlabel);
114 return err;
117 void ip6_datagram_release_cb(struct sock *sk)
119 struct dst_entry *dst;
121 if (ipv6_addr_v4mapped(&sk->sk_v6_daddr))
122 return;
124 rcu_read_lock();
125 dst = __sk_dst_get(sk);
126 if (!dst || !dst->obsolete ||
127 dst->ops->check(dst, inet6_sk(sk)->dst_cookie)) {
128 rcu_read_unlock();
129 return;
131 rcu_read_unlock();
133 ip6_datagram_dst_update(sk, false);
135 EXPORT_SYMBOL_GPL(ip6_datagram_release_cb);
137 int __ip6_datagram_connect(struct sock *sk, struct sockaddr *uaddr,
138 int addr_len)
140 struct sockaddr_in6 *usin = (struct sockaddr_in6 *) uaddr;
141 struct inet_sock *inet = inet_sk(sk);
142 struct ipv6_pinfo *np = inet6_sk(sk);
143 struct in6_addr *daddr, old_daddr;
144 __be32 fl6_flowlabel = 0;
145 __be32 old_fl6_flowlabel;
146 __be16 old_dport;
147 int addr_type;
148 int err;
150 if (usin->sin6_family == AF_INET) {
151 if (__ipv6_only_sock(sk))
152 return -EAFNOSUPPORT;
153 err = __ip4_datagram_connect(sk, uaddr, addr_len);
154 goto ipv4_connected;
157 if (addr_len < SIN6_LEN_RFC2133)
158 return -EINVAL;
160 if (usin->sin6_family != AF_INET6)
161 return -EAFNOSUPPORT;
163 if (np->sndflow)
164 fl6_flowlabel = usin->sin6_flowinfo & IPV6_FLOWINFO_MASK;
166 if (ipv6_addr_any(&usin->sin6_addr)) {
168 * connect to self
170 if (ipv6_addr_v4mapped(&sk->sk_v6_rcv_saddr))
171 ipv6_addr_set_v4mapped(htonl(INADDR_LOOPBACK),
172 &usin->sin6_addr);
173 else
174 usin->sin6_addr = in6addr_loopback;
177 addr_type = ipv6_addr_type(&usin->sin6_addr);
179 daddr = &usin->sin6_addr;
181 if (addr_type & IPV6_ADDR_MAPPED) {
182 struct sockaddr_in sin;
184 if (__ipv6_only_sock(sk)) {
185 err = -ENETUNREACH;
186 goto out;
188 sin.sin_family = AF_INET;
189 sin.sin_addr.s_addr = daddr->s6_addr32[3];
190 sin.sin_port = usin->sin6_port;
192 err = __ip4_datagram_connect(sk,
193 (struct sockaddr *) &sin,
194 sizeof(sin));
196 ipv4_connected:
197 if (err)
198 goto out;
200 ipv6_addr_set_v4mapped(inet->inet_daddr, &sk->sk_v6_daddr);
202 if (ipv6_addr_any(&np->saddr) ||
203 ipv6_mapped_addr_any(&np->saddr))
204 ipv6_addr_set_v4mapped(inet->inet_saddr, &np->saddr);
206 if (ipv6_addr_any(&sk->sk_v6_rcv_saddr) ||
207 ipv6_mapped_addr_any(&sk->sk_v6_rcv_saddr)) {
208 ipv6_addr_set_v4mapped(inet->inet_rcv_saddr,
209 &sk->sk_v6_rcv_saddr);
210 if (sk->sk_prot->rehash)
211 sk->sk_prot->rehash(sk);
214 goto out;
217 if (__ipv6_addr_needs_scope_id(addr_type)) {
218 if (addr_len >= sizeof(struct sockaddr_in6) &&
219 usin->sin6_scope_id) {
220 if (!sk_dev_equal_l3scope(sk, usin->sin6_scope_id)) {
221 err = -EINVAL;
222 goto out;
224 sk->sk_bound_dev_if = usin->sin6_scope_id;
227 if (!sk->sk_bound_dev_if && (addr_type & IPV6_ADDR_MULTICAST))
228 sk->sk_bound_dev_if = np->mcast_oif;
230 /* Connect to link-local address requires an interface */
231 if (!sk->sk_bound_dev_if) {
232 err = -EINVAL;
233 goto out;
237 /* save the current peer information before updating it */
238 old_daddr = sk->sk_v6_daddr;
239 old_fl6_flowlabel = np->flow_label;
240 old_dport = inet->inet_dport;
242 sk->sk_v6_daddr = *daddr;
243 np->flow_label = fl6_flowlabel;
244 inet->inet_dport = usin->sin6_port;
247 * Check for a route to destination an obtain the
248 * destination cache for it.
251 err = ip6_datagram_dst_update(sk, true);
252 if (err) {
253 /* Restore the socket peer info, to keep it consistent with
254 * the old socket state
256 sk->sk_v6_daddr = old_daddr;
257 np->flow_label = old_fl6_flowlabel;
258 inet->inet_dport = old_dport;
259 goto out;
262 reuseport_has_conns(sk, true);
263 sk->sk_state = TCP_ESTABLISHED;
264 sk_set_txhash(sk);
265 out:
266 return err;
268 EXPORT_SYMBOL_GPL(__ip6_datagram_connect);
270 int ip6_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
272 int res;
274 lock_sock(sk);
275 res = __ip6_datagram_connect(sk, uaddr, addr_len);
276 release_sock(sk);
277 return res;
279 EXPORT_SYMBOL_GPL(ip6_datagram_connect);
281 int ip6_datagram_connect_v6_only(struct sock *sk, struct sockaddr *uaddr,
282 int addr_len)
284 DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, uaddr);
285 if (sin6->sin6_family != AF_INET6)
286 return -EAFNOSUPPORT;
287 return ip6_datagram_connect(sk, uaddr, addr_len);
289 EXPORT_SYMBOL_GPL(ip6_datagram_connect_v6_only);
291 void ipv6_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
292 __be16 port, u32 info, u8 *payload)
294 struct ipv6_pinfo *np = inet6_sk(sk);
295 struct icmp6hdr *icmph = icmp6_hdr(skb);
296 struct sock_exterr_skb *serr;
298 if (!np->recverr)
299 return;
301 skb = skb_clone(skb, GFP_ATOMIC);
302 if (!skb)
303 return;
305 skb->protocol = htons(ETH_P_IPV6);
307 serr = SKB_EXT_ERR(skb);
308 serr->ee.ee_errno = err;
309 serr->ee.ee_origin = SO_EE_ORIGIN_ICMP6;
310 serr->ee.ee_type = icmph->icmp6_type;
311 serr->ee.ee_code = icmph->icmp6_code;
312 serr->ee.ee_pad = 0;
313 serr->ee.ee_info = info;
314 serr->ee.ee_data = 0;
315 serr->addr_offset = (u8 *)&(((struct ipv6hdr *)(icmph + 1))->daddr) -
316 skb_network_header(skb);
317 serr->port = port;
319 __skb_pull(skb, payload - skb->data);
320 skb_reset_transport_header(skb);
322 if (sock_queue_err_skb(sk, skb))
323 kfree_skb(skb);
326 void ipv6_local_error(struct sock *sk, int err, struct flowi6 *fl6, u32 info)
328 const struct ipv6_pinfo *np = inet6_sk(sk);
329 struct sock_exterr_skb *serr;
330 struct ipv6hdr *iph;
331 struct sk_buff *skb;
333 if (!np->recverr)
334 return;
336 skb = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
337 if (!skb)
338 return;
340 skb->protocol = htons(ETH_P_IPV6);
342 skb_put(skb, sizeof(struct ipv6hdr));
343 skb_reset_network_header(skb);
344 iph = ipv6_hdr(skb);
345 iph->daddr = fl6->daddr;
346 ip6_flow_hdr(iph, 0, 0);
348 serr = SKB_EXT_ERR(skb);
349 serr->ee.ee_errno = err;
350 serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
351 serr->ee.ee_type = 0;
352 serr->ee.ee_code = 0;
353 serr->ee.ee_pad = 0;
354 serr->ee.ee_info = info;
355 serr->ee.ee_data = 0;
356 serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
357 serr->port = fl6->fl6_dport;
359 __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
360 skb_reset_transport_header(skb);
362 if (sock_queue_err_skb(sk, skb))
363 kfree_skb(skb);
366 void ipv6_local_rxpmtu(struct sock *sk, struct flowi6 *fl6, u32 mtu)
368 struct ipv6_pinfo *np = inet6_sk(sk);
369 struct ipv6hdr *iph;
370 struct sk_buff *skb;
371 struct ip6_mtuinfo *mtu_info;
373 if (!np->rxopt.bits.rxpmtu)
374 return;
376 skb = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
377 if (!skb)
378 return;
380 skb_put(skb, sizeof(struct ipv6hdr));
381 skb_reset_network_header(skb);
382 iph = ipv6_hdr(skb);
383 iph->daddr = fl6->daddr;
385 mtu_info = IP6CBMTU(skb);
387 mtu_info->ip6m_mtu = mtu;
388 mtu_info->ip6m_addr.sin6_family = AF_INET6;
389 mtu_info->ip6m_addr.sin6_port = 0;
390 mtu_info->ip6m_addr.sin6_flowinfo = 0;
391 mtu_info->ip6m_addr.sin6_scope_id = fl6->flowi6_oif;
392 mtu_info->ip6m_addr.sin6_addr = ipv6_hdr(skb)->daddr;
394 __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
395 skb_reset_transport_header(skb);
397 skb = xchg(&np->rxpmtu, skb);
398 kfree_skb(skb);
401 /* For some errors we have valid addr_offset even with zero payload and
402 * zero port. Also, addr_offset should be supported if port is set.
404 static inline bool ipv6_datagram_support_addr(struct sock_exterr_skb *serr)
406 return serr->ee.ee_origin == SO_EE_ORIGIN_ICMP6 ||
407 serr->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
408 serr->ee.ee_origin == SO_EE_ORIGIN_LOCAL || serr->port;
411 /* IPv6 supports cmsg on all origins aside from SO_EE_ORIGIN_LOCAL.
413 * At one point, excluding local errors was a quick test to identify icmp/icmp6
414 * errors. This is no longer true, but the test remained, so the v6 stack,
415 * unlike v4, also honors cmsg requests on all wifi and timestamp errors.
417 static bool ip6_datagram_support_cmsg(struct sk_buff *skb,
418 struct sock_exterr_skb *serr)
420 if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
421 serr->ee.ee_origin == SO_EE_ORIGIN_ICMP6)
422 return true;
424 if (serr->ee.ee_origin == SO_EE_ORIGIN_LOCAL)
425 return false;
427 if (!IP6CB(skb)->iif)
428 return false;
430 return true;
434 * Handle MSG_ERRQUEUE
436 int ipv6_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len)
438 struct ipv6_pinfo *np = inet6_sk(sk);
439 struct sock_exterr_skb *serr;
440 struct sk_buff *skb;
441 DECLARE_SOCKADDR(struct sockaddr_in6 *, sin, msg->msg_name);
442 struct {
443 struct sock_extended_err ee;
444 struct sockaddr_in6 offender;
445 } errhdr;
446 int err;
447 int copied;
449 err = -EAGAIN;
450 skb = sock_dequeue_err_skb(sk);
451 if (!skb)
452 goto out;
454 copied = skb->len;
455 if (copied > len) {
456 msg->msg_flags |= MSG_TRUNC;
457 copied = len;
459 err = skb_copy_datagram_msg(skb, 0, msg, copied);
460 if (unlikely(err)) {
461 kfree_skb(skb);
462 return err;
464 sock_recv_timestamp(msg, sk, skb);
466 serr = SKB_EXT_ERR(skb);
468 if (sin && ipv6_datagram_support_addr(serr)) {
469 const unsigned char *nh = skb_network_header(skb);
470 sin->sin6_family = AF_INET6;
471 sin->sin6_flowinfo = 0;
472 sin->sin6_port = serr->port;
473 if (skb->protocol == htons(ETH_P_IPV6)) {
474 const struct ipv6hdr *ip6h = container_of((struct in6_addr *)(nh + serr->addr_offset),
475 struct ipv6hdr, daddr);
476 sin->sin6_addr = ip6h->daddr;
477 if (np->sndflow)
478 sin->sin6_flowinfo = ip6_flowinfo(ip6h);
479 sin->sin6_scope_id =
480 ipv6_iface_scope_id(&sin->sin6_addr,
481 IP6CB(skb)->iif);
482 } else {
483 ipv6_addr_set_v4mapped(*(__be32 *)(nh + serr->addr_offset),
484 &sin->sin6_addr);
485 sin->sin6_scope_id = 0;
487 *addr_len = sizeof(*sin);
490 memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
491 sin = &errhdr.offender;
492 memset(sin, 0, sizeof(*sin));
494 if (ip6_datagram_support_cmsg(skb, serr)) {
495 sin->sin6_family = AF_INET6;
496 if (np->rxopt.all)
497 ip6_datagram_recv_common_ctl(sk, msg, skb);
498 if (skb->protocol == htons(ETH_P_IPV6)) {
499 sin->sin6_addr = ipv6_hdr(skb)->saddr;
500 if (np->rxopt.all)
501 ip6_datagram_recv_specific_ctl(sk, msg, skb);
502 sin->sin6_scope_id =
503 ipv6_iface_scope_id(&sin->sin6_addr,
504 IP6CB(skb)->iif);
505 } else {
506 ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr,
507 &sin->sin6_addr);
508 if (inet_sk(sk)->cmsg_flags)
509 ip_cmsg_recv(msg, skb);
513 put_cmsg(msg, SOL_IPV6, IPV6_RECVERR, sizeof(errhdr), &errhdr);
515 /* Now we could try to dump offended packet options */
517 msg->msg_flags |= MSG_ERRQUEUE;
518 err = copied;
520 consume_skb(skb);
521 out:
522 return err;
524 EXPORT_SYMBOL_GPL(ipv6_recv_error);
527 * Handle IPV6_RECVPATHMTU
529 int ipv6_recv_rxpmtu(struct sock *sk, struct msghdr *msg, int len,
530 int *addr_len)
532 struct ipv6_pinfo *np = inet6_sk(sk);
533 struct sk_buff *skb;
534 struct ip6_mtuinfo mtu_info;
535 DECLARE_SOCKADDR(struct sockaddr_in6 *, sin, msg->msg_name);
536 int err;
537 int copied;
539 err = -EAGAIN;
540 skb = xchg(&np->rxpmtu, NULL);
541 if (!skb)
542 goto out;
544 copied = skb->len;
545 if (copied > len) {
546 msg->msg_flags |= MSG_TRUNC;
547 copied = len;
549 err = skb_copy_datagram_msg(skb, 0, msg, copied);
550 if (err)
551 goto out_free_skb;
553 sock_recv_timestamp(msg, sk, skb);
555 memcpy(&mtu_info, IP6CBMTU(skb), sizeof(mtu_info));
557 if (sin) {
558 sin->sin6_family = AF_INET6;
559 sin->sin6_flowinfo = 0;
560 sin->sin6_port = 0;
561 sin->sin6_scope_id = mtu_info.ip6m_addr.sin6_scope_id;
562 sin->sin6_addr = mtu_info.ip6m_addr.sin6_addr;
563 *addr_len = sizeof(*sin);
566 put_cmsg(msg, SOL_IPV6, IPV6_PATHMTU, sizeof(mtu_info), &mtu_info);
568 err = copied;
570 out_free_skb:
571 kfree_skb(skb);
572 out:
573 return err;
577 void ip6_datagram_recv_common_ctl(struct sock *sk, struct msghdr *msg,
578 struct sk_buff *skb)
580 struct ipv6_pinfo *np = inet6_sk(sk);
581 bool is_ipv6 = skb->protocol == htons(ETH_P_IPV6);
583 if (np->rxopt.bits.rxinfo) {
584 struct in6_pktinfo src_info;
586 if (is_ipv6) {
587 src_info.ipi6_ifindex = IP6CB(skb)->iif;
588 src_info.ipi6_addr = ipv6_hdr(skb)->daddr;
589 } else {
590 src_info.ipi6_ifindex =
591 PKTINFO_SKB_CB(skb)->ipi_ifindex;
592 ipv6_addr_set_v4mapped(ip_hdr(skb)->daddr,
593 &src_info.ipi6_addr);
596 if (src_info.ipi6_ifindex >= 0)
597 put_cmsg(msg, SOL_IPV6, IPV6_PKTINFO,
598 sizeof(src_info), &src_info);
602 void ip6_datagram_recv_specific_ctl(struct sock *sk, struct msghdr *msg,
603 struct sk_buff *skb)
605 struct ipv6_pinfo *np = inet6_sk(sk);
606 struct inet6_skb_parm *opt = IP6CB(skb);
607 unsigned char *nh = skb_network_header(skb);
609 if (np->rxopt.bits.rxhlim) {
610 int hlim = ipv6_hdr(skb)->hop_limit;
611 put_cmsg(msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
614 if (np->rxopt.bits.rxtclass) {
615 int tclass = ipv6_get_dsfield(ipv6_hdr(skb));
616 put_cmsg(msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass);
619 if (np->rxopt.bits.rxflow) {
620 __be32 flowinfo = ip6_flowinfo((struct ipv6hdr *)nh);
621 if (flowinfo)
622 put_cmsg(msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo);
625 /* HbH is allowed only once */
626 if (np->rxopt.bits.hopopts && (opt->flags & IP6SKB_HOPBYHOP)) {
627 u8 *ptr = nh + sizeof(struct ipv6hdr);
628 put_cmsg(msg, SOL_IPV6, IPV6_HOPOPTS, (ptr[1]+1)<<3, ptr);
631 if (opt->lastopt &&
632 (np->rxopt.bits.dstopts || np->rxopt.bits.srcrt)) {
634 * Silly enough, but we need to reparse in order to
635 * report extension headers (except for HbH)
636 * in order.
638 * Also note that IPV6_RECVRTHDRDSTOPTS is NOT
639 * (and WILL NOT be) defined because
640 * IPV6_RECVDSTOPTS is more generic. --yoshfuji
642 unsigned int off = sizeof(struct ipv6hdr);
643 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
645 while (off <= opt->lastopt) {
646 unsigned int len;
647 u8 *ptr = nh + off;
649 switch (nexthdr) {
650 case IPPROTO_DSTOPTS:
651 nexthdr = ptr[0];
652 len = (ptr[1] + 1) << 3;
653 if (np->rxopt.bits.dstopts)
654 put_cmsg(msg, SOL_IPV6, IPV6_DSTOPTS, len, ptr);
655 break;
656 case IPPROTO_ROUTING:
657 nexthdr = ptr[0];
658 len = (ptr[1] + 1) << 3;
659 if (np->rxopt.bits.srcrt)
660 put_cmsg(msg, SOL_IPV6, IPV6_RTHDR, len, ptr);
661 break;
662 case IPPROTO_AH:
663 nexthdr = ptr[0];
664 len = (ptr[1] + 2) << 2;
665 break;
666 default:
667 nexthdr = ptr[0];
668 len = (ptr[1] + 1) << 3;
669 break;
672 off += len;
676 /* socket options in old style */
677 if (np->rxopt.bits.rxoinfo) {
678 struct in6_pktinfo src_info;
680 src_info.ipi6_ifindex = opt->iif;
681 src_info.ipi6_addr = ipv6_hdr(skb)->daddr;
682 put_cmsg(msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
684 if (np->rxopt.bits.rxohlim) {
685 int hlim = ipv6_hdr(skb)->hop_limit;
686 put_cmsg(msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
688 if (np->rxopt.bits.ohopopts && (opt->flags & IP6SKB_HOPBYHOP)) {
689 u8 *ptr = nh + sizeof(struct ipv6hdr);
690 put_cmsg(msg, SOL_IPV6, IPV6_2292HOPOPTS, (ptr[1]+1)<<3, ptr);
692 if (np->rxopt.bits.odstopts && opt->dst0) {
693 u8 *ptr = nh + opt->dst0;
694 put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr);
696 if (np->rxopt.bits.osrcrt && opt->srcrt) {
697 struct ipv6_rt_hdr *rthdr = (struct ipv6_rt_hdr *)(nh + opt->srcrt);
698 put_cmsg(msg, SOL_IPV6, IPV6_2292RTHDR, (rthdr->hdrlen+1) << 3, rthdr);
700 if (np->rxopt.bits.odstopts && opt->dst1) {
701 u8 *ptr = nh + opt->dst1;
702 put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr);
704 if (np->rxopt.bits.rxorigdstaddr) {
705 struct sockaddr_in6 sin6;
706 __be16 _ports[2], *ports;
708 ports = skb_header_pointer(skb, skb_transport_offset(skb),
709 sizeof(_ports), &_ports);
710 if (ports) {
711 /* All current transport protocols have the port numbers in the
712 * first four bytes of the transport header and this function is
713 * written with this assumption in mind.
715 sin6.sin6_family = AF_INET6;
716 sin6.sin6_addr = ipv6_hdr(skb)->daddr;
717 sin6.sin6_port = ports[1];
718 sin6.sin6_flowinfo = 0;
719 sin6.sin6_scope_id =
720 ipv6_iface_scope_id(&ipv6_hdr(skb)->daddr,
721 opt->iif);
723 put_cmsg(msg, SOL_IPV6, IPV6_ORIGDSTADDR, sizeof(sin6), &sin6);
726 if (np->rxopt.bits.recvfragsize && opt->frag_max_size) {
727 int val = opt->frag_max_size;
729 put_cmsg(msg, SOL_IPV6, IPV6_RECVFRAGSIZE, sizeof(val), &val);
733 void ip6_datagram_recv_ctl(struct sock *sk, struct msghdr *msg,
734 struct sk_buff *skb)
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)
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;
750 int len;
751 int err = 0;
753 for_each_cmsghdr(cmsg, msg) {
754 int addr_type;
756 if (!CMSG_OK(msg, cmsg)) {
757 err = -EINVAL;
758 goto exit_f;
761 if (cmsg->cmsg_level == SOL_SOCKET) {
762 err = __sock_cmsg_send(sk, msg, cmsg, &ipc6->sockc);
763 if (err)
764 return err;
765 continue;
768 if (cmsg->cmsg_level != SOL_IPV6)
769 continue;
771 switch (cmsg->cmsg_type) {
772 case IPV6_PKTINFO:
773 case IPV6_2292PKTINFO:
775 struct net_device *dev = NULL;
777 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct in6_pktinfo))) {
778 err = -EINVAL;
779 goto exit_f;
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)
787 return -EINVAL;
788 fl6->flowi6_oif = src_info->ipi6_ifindex;
791 addr_type = __ipv6_addr_type(&src_info->ipi6_addr);
793 rcu_read_lock();
794 if (fl6->flowi6_oif) {
795 dev = dev_get_by_index_rcu(net, fl6->flowi6_oif);
796 if (!dev) {
797 rcu_read_unlock();
798 return -ENODEV;
800 } else if (addr_type & IPV6_ADDR_LINKLOCAL) {
801 rcu_read_unlock();
802 return -EINVAL;
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,
809 dev, !strict, 0,
810 IFA_F_TENTATIVE) &&
811 !ipv6_chk_acast_addr_src(net, dev,
812 &src_info->ipi6_addr))
813 err = -EINVAL;
814 else
815 fl6->saddr = src_info->ipi6_addr;
818 rcu_read_unlock();
820 if (err)
821 goto exit_f;
823 break;
826 case IPV6_FLOWINFO:
827 if (cmsg->cmsg_len < CMSG_LEN(4)) {
828 err = -EINVAL;
829 goto exit_f;
832 if (fl6->flowlabel&IPV6_FLOWINFO_MASK) {
833 if ((fl6->flowlabel^*(__be32 *)CMSG_DATA(cmsg))&~IPV6_FLOWINFO_MASK) {
834 err = -EINVAL;
835 goto exit_f;
838 fl6->flowlabel = IPV6_FLOWINFO_MASK & *(__be32 *)CMSG_DATA(cmsg);
839 break;
841 case IPV6_2292HOPOPTS:
842 case IPV6_HOPOPTS:
843 if (opt->hopopt || cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
844 err = -EINVAL;
845 goto exit_f;
848 hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
849 len = ((hdr->hdrlen + 1) << 3);
850 if (cmsg->cmsg_len < CMSG_LEN(len)) {
851 err = -EINVAL;
852 goto exit_f;
854 if (!ns_capable(net->user_ns, CAP_NET_RAW)) {
855 err = -EPERM;
856 goto exit_f;
858 opt->opt_nflen += len;
859 opt->hopopt = hdr;
860 break;
862 case IPV6_2292DSTOPTS:
863 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
864 err = -EINVAL;
865 goto exit_f;
868 hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
869 len = ((hdr->hdrlen + 1) << 3);
870 if (cmsg->cmsg_len < CMSG_LEN(len)) {
871 err = -EINVAL;
872 goto exit_f;
874 if (!ns_capable(net->user_ns, CAP_NET_RAW)) {
875 err = -EPERM;
876 goto exit_f;
878 if (opt->dst1opt) {
879 err = -EINVAL;
880 goto exit_f;
882 opt->opt_flen += len;
883 opt->dst1opt = hdr;
884 break;
886 case IPV6_DSTOPTS:
887 case IPV6_RTHDRDSTOPTS:
888 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
889 err = -EINVAL;
890 goto exit_f;
893 hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
894 len = ((hdr->hdrlen + 1) << 3);
895 if (cmsg->cmsg_len < CMSG_LEN(len)) {
896 err = -EINVAL;
897 goto exit_f;
899 if (!ns_capable(net->user_ns, CAP_NET_RAW)) {
900 err = -EPERM;
901 goto exit_f;
903 if (cmsg->cmsg_type == IPV6_DSTOPTS) {
904 opt->opt_flen += len;
905 opt->dst1opt = hdr;
906 } else {
907 opt->opt_nflen += len;
908 opt->dst0opt = hdr;
910 break;
912 case IPV6_2292RTHDR:
913 case IPV6_RTHDR:
914 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_rt_hdr))) {
915 err = -EINVAL;
916 goto exit_f;
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) {
926 err = -EINVAL;
927 goto exit_f;
929 break;
930 #endif
931 default:
932 err = -EINVAL;
933 goto exit_f;
936 len = ((rthdr->hdrlen + 1) << 3);
938 if (cmsg->cmsg_len < CMSG_LEN(len)) {
939 err = -EINVAL;
940 goto exit_f;
943 /* segments left must also match */
944 if ((rthdr->hdrlen >> 1) != rthdr->segments_left) {
945 err = -EINVAL;
946 goto exit_f;
949 opt->opt_nflen += len;
950 opt->srcrt = rthdr;
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;
957 opt->dst1opt = NULL;
958 opt->opt_flen -= dsthdrlen;
961 break;
963 case IPV6_2292HOPLIMIT:
964 case IPV6_HOPLIMIT:
965 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) {
966 err = -EINVAL;
967 goto exit_f;
970 ipc6->hlimit = *(int *)CMSG_DATA(cmsg);
971 if (ipc6->hlimit < -1 || ipc6->hlimit > 0xff) {
972 err = -EINVAL;
973 goto exit_f;
976 break;
978 case IPV6_TCLASS:
980 int tc;
982 err = -EINVAL;
983 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
984 goto exit_f;
986 tc = *(int *)CMSG_DATA(cmsg);
987 if (tc < -1 || tc > 0xff)
988 goto exit_f;
990 err = 0;
991 ipc6->tclass = tc;
993 break;
996 case IPV6_DONTFRAG:
998 int df;
1000 err = -EINVAL;
1001 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
1002 goto exit_f;
1004 df = *(int *)CMSG_DATA(cmsg);
1005 if (df < 0 || df > 1)
1006 goto exit_f;
1008 err = 0;
1009 ipc6->dontfrag = df;
1011 break;
1013 default:
1014 net_dbg_ratelimited("invalid cmsg type: %d\n",
1015 cmsg->cmsg_type);
1016 err = -EINVAL;
1017 goto exit_f;
1021 exit_f:
1022 return err;
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;
1033 seq_printf(seq,
1034 "%5d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
1035 "%02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %d\n",
1036 bucket,
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,
1041 sp->sk_state,
1042 sk_wmem_alloc_get(sp),
1043 rqueue,
1044 0, 0L, 0,
1045 from_kuid_munged(seq_user_ns(seq), sock_i_uid(sp)),
1047 sock_i_ino(sp),
1048 refcount_read(&sp->sk_refcnt), sp,
1049 atomic_read(&sp->sk_drops));