ALSA: seq: Use bool for snd_seq_queue internal flags
[linux/fpc-iii.git] / net / ipv6 / datagram.c
blobfa4987827cda3d93c53e950351ea0e09bf0ef0ed
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>
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;
58 if (!fl6->flowi6_oif)
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);
75 struct flowi6 fl6;
76 int err = 0;
78 if (np->sndflow && (np->flow_label & IPV6_FLOWLABEL_MASK)) {
79 flowlabel = fl6_sock_lookup(sk, np->flow_label);
80 if (!flowlabel)
81 return -EINVAL;
83 ip6_datagram_flow_key_init(&fl6, sk);
85 rcu_read_lock();
86 opt = flowlabel ? flowlabel->opt : rcu_dereference(np->opt);
87 final_p = fl6_update_dst(&fl6, opt, &final);
88 rcu_read_unlock();
90 dst = ip6_dst_lookup_flow(sock_net(sk), sk, &fl6, final_p);
91 if (IS_ERR(dst)) {
92 err = PTR_ERR(dst);
93 goto out;
96 if (fix_sk_saddr) {
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) ?
113 &np->saddr :
114 #endif
115 NULL);
117 out:
118 fl6_sock_release(flowlabel);
119 return err;
122 void ip6_datagram_release_cb(struct sock *sk)
124 struct dst_entry *dst;
126 if (ipv6_addr_v4mapped(&sk->sk_v6_daddr))
127 return;
129 rcu_read_lock();
130 dst = __sk_dst_get(sk);
131 if (!dst || !dst->obsolete ||
132 dst->ops->check(dst, inet6_sk(sk)->dst_cookie)) {
133 rcu_read_unlock();
134 return;
136 rcu_read_unlock();
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,
143 int addr_len)
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;
151 __be32 old_dport;
152 int addr_type;
153 int err;
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);
159 goto ipv4_connected;
162 if (addr_len < SIN6_LEN_RFC2133)
163 return -EINVAL;
165 if (usin->sin6_family != AF_INET6)
166 return -EAFNOSUPPORT;
168 if (np->sndflow)
169 fl6_flowlabel = usin->sin6_flowinfo & IPV6_FLOWINFO_MASK;
171 if (ipv6_addr_any(&usin->sin6_addr)) {
173 * connect to self
175 if (ipv6_addr_v4mapped(&sk->sk_v6_rcv_saddr))
176 ipv6_addr_set_v4mapped(htonl(INADDR_LOOPBACK),
177 &usin->sin6_addr);
178 else
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)) {
190 err = -ENETUNREACH;
191 goto out;
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,
199 sizeof(sin));
201 ipv4_connected:
202 if (err)
203 goto out;
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);
219 goto out;
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) {
227 err = -EINVAL;
228 goto out;
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) {
238 err = -EINVAL;
239 goto out;
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);
258 if (err) {
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;
265 goto out;
268 sk->sk_state = TCP_ESTABLISHED;
269 sk_set_txhash(sk);
270 out:
271 return err;
273 EXPORT_SYMBOL_GPL(__ip6_datagram_connect);
275 int ip6_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
277 int res;
279 lock_sock(sk);
280 res = __ip6_datagram_connect(sk, uaddr, addr_len);
281 release_sock(sk);
282 return res;
284 EXPORT_SYMBOL_GPL(ip6_datagram_connect);
286 int ip6_datagram_connect_v6_only(struct sock *sk, struct sockaddr *uaddr,
287 int addr_len)
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;
303 if (!np->recverr)
304 return;
306 skb = skb_clone(skb, GFP_ATOMIC);
307 if (!skb)
308 return;
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;
317 serr->ee.ee_pad = 0;
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);
322 serr->port = port;
324 __skb_pull(skb, payload - skb->data);
325 skb_reset_transport_header(skb);
327 if (sock_queue_err_skb(sk, skb))
328 kfree_skb(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;
335 struct ipv6hdr *iph;
336 struct sk_buff *skb;
338 if (!np->recverr)
339 return;
341 skb = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
342 if (!skb)
343 return;
345 skb->protocol = htons(ETH_P_IPV6);
347 skb_put(skb, sizeof(struct ipv6hdr));
348 skb_reset_network_header(skb);
349 iph = ipv6_hdr(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;
358 serr->ee.ee_pad = 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))
368 kfree_skb(skb);
371 void ipv6_local_rxpmtu(struct sock *sk, struct flowi6 *fl6, u32 mtu)
373 struct ipv6_pinfo *np = inet6_sk(sk);
374 struct ipv6hdr *iph;
375 struct sk_buff *skb;
376 struct ip6_mtuinfo *mtu_info;
378 if (!np->rxopt.bits.rxpmtu)
379 return;
381 skb = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
382 if (!skb)
383 return;
385 skb_put(skb, sizeof(struct ipv6hdr));
386 skb_reset_network_header(skb);
387 iph = ipv6_hdr(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);
403 kfree_skb(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)
427 return true;
429 if (serr->ee.ee_origin == SO_EE_ORIGIN_LOCAL)
430 return false;
432 if (!IP6CB(skb)->iif)
433 return false;
435 return true;
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;
445 struct sk_buff *skb;
446 DECLARE_SOCKADDR(struct sockaddr_in6 *, sin, msg->msg_name);
447 struct {
448 struct sock_extended_err ee;
449 struct sockaddr_in6 offender;
450 } errhdr;
451 int err;
452 int copied;
454 err = -EAGAIN;
455 skb = sock_dequeue_err_skb(sk);
456 if (!skb)
457 goto out;
459 copied = skb->len;
460 if (copied > len) {
461 msg->msg_flags |= MSG_TRUNC;
462 copied = len;
464 err = skb_copy_datagram_msg(skb, 0, msg, copied);
465 if (unlikely(err)) {
466 kfree_skb(skb);
467 return err;
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;
482 if (np->sndflow)
483 sin->sin6_flowinfo = ip6_flowinfo(ip6h);
484 sin->sin6_scope_id =
485 ipv6_iface_scope_id(&sin->sin6_addr,
486 IP6CB(skb)->iif);
487 } else {
488 ipv6_addr_set_v4mapped(*(__be32 *)(nh + serr->addr_offset),
489 &sin->sin6_addr);
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;
501 if (np->rxopt.all)
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;
505 if (np->rxopt.all)
506 ip6_datagram_recv_specific_ctl(sk, msg, skb);
507 sin->sin6_scope_id =
508 ipv6_iface_scope_id(&sin->sin6_addr,
509 IP6CB(skb)->iif);
510 } else {
511 ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr,
512 &sin->sin6_addr);
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;
523 err = copied;
525 consume_skb(skb);
526 out:
527 return err;
529 EXPORT_SYMBOL_GPL(ipv6_recv_error);
532 * Handle IPV6_RECVPATHMTU
534 int ipv6_recv_rxpmtu(struct sock *sk, struct msghdr *msg, int len,
535 int *addr_len)
537 struct ipv6_pinfo *np = inet6_sk(sk);
538 struct sk_buff *skb;
539 struct ip6_mtuinfo mtu_info;
540 DECLARE_SOCKADDR(struct sockaddr_in6 *, sin, msg->msg_name);
541 int err;
542 int copied;
544 err = -EAGAIN;
545 skb = xchg(&np->rxpmtu, NULL);
546 if (!skb)
547 goto out;
549 copied = skb->len;
550 if (copied > len) {
551 msg->msg_flags |= MSG_TRUNC;
552 copied = len;
554 err = skb_copy_datagram_msg(skb, 0, msg, copied);
555 if (err)
556 goto out_free_skb;
558 sock_recv_timestamp(msg, sk, skb);
560 memcpy(&mtu_info, IP6CBMTU(skb), sizeof(mtu_info));
562 if (sin) {
563 sin->sin6_family = AF_INET6;
564 sin->sin6_flowinfo = 0;
565 sin->sin6_port = 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);
573 err = copied;
575 out_free_skb:
576 kfree_skb(skb);
577 out:
578 return err;
582 void ip6_datagram_recv_common_ctl(struct sock *sk, struct msghdr *msg,
583 struct sk_buff *skb)
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;
591 if (is_ipv6) {
592 src_info.ipi6_ifindex = IP6CB(skb)->iif;
593 src_info.ipi6_addr = ipv6_hdr(skb)->daddr;
594 } else {
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,
608 struct sk_buff *skb)
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);
626 if (flowinfo)
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);
636 if (opt->lastopt &&
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)
641 * in order.
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) {
651 unsigned int len;
652 u8 *ptr = nh + off;
654 switch (nexthdr) {
655 case IPPROTO_DSTOPTS:
656 nexthdr = ptr[0];
657 len = (ptr[1] + 1) << 3;
658 if (np->rxopt.bits.dstopts)
659 put_cmsg(msg, SOL_IPV6, IPV6_DSTOPTS, len, ptr);
660 break;
661 case IPPROTO_ROUTING:
662 nexthdr = ptr[0];
663 len = (ptr[1] + 1) << 3;
664 if (np->rxopt.bits.srcrt)
665 put_cmsg(msg, SOL_IPV6, IPV6_RTHDR, len, ptr);
666 break;
667 case IPPROTO_AH:
668 nexthdr = ptr[0];
669 len = (ptr[1] + 2) << 2;
670 break;
671 default:
672 nexthdr = ptr[0];
673 len = (ptr[1] + 1) << 3;
674 break;
677 off += len;
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);
715 if (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;
724 sin6.sin6_scope_id =
725 ipv6_iface_scope_id(&ipv6_hdr(skb)->daddr,
726 opt->iif);
728 put_cmsg(msg, SOL_IPV6, IPV6_ORIGDSTADDR, sizeof(sin6), &sin6);
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, 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;
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, 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 (!(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))
812 err = -EINVAL;
813 else
814 fl6->saddr = src_info->ipi6_addr;
817 rcu_read_unlock();
819 if (err)
820 goto exit_f;
822 break;
825 case IPV6_FLOWINFO:
826 if (cmsg->cmsg_len < CMSG_LEN(4)) {
827 err = -EINVAL;
828 goto exit_f;
831 if (fl6->flowlabel&IPV6_FLOWINFO_MASK) {
832 if ((fl6->flowlabel^*(__be32 *)CMSG_DATA(cmsg))&~IPV6_FLOWINFO_MASK) {
833 err = -EINVAL;
834 goto exit_f;
837 fl6->flowlabel = IPV6_FLOWINFO_MASK & *(__be32 *)CMSG_DATA(cmsg);
838 break;
840 case IPV6_2292HOPOPTS:
841 case IPV6_HOPOPTS:
842 if (opt->hopopt || cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
843 err = -EINVAL;
844 goto exit_f;
847 hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
848 len = ((hdr->hdrlen + 1) << 3);
849 if (cmsg->cmsg_len < CMSG_LEN(len)) {
850 err = -EINVAL;
851 goto exit_f;
853 if (!ns_capable(net->user_ns, CAP_NET_RAW)) {
854 err = -EPERM;
855 goto exit_f;
857 opt->opt_nflen += len;
858 opt->hopopt = hdr;
859 break;
861 case IPV6_2292DSTOPTS:
862 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
863 err = -EINVAL;
864 goto exit_f;
867 hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
868 len = ((hdr->hdrlen + 1) << 3);
869 if (cmsg->cmsg_len < CMSG_LEN(len)) {
870 err = -EINVAL;
871 goto exit_f;
873 if (!ns_capable(net->user_ns, CAP_NET_RAW)) {
874 err = -EPERM;
875 goto exit_f;
877 if (opt->dst1opt) {
878 err = -EINVAL;
879 goto exit_f;
881 opt->opt_flen += len;
882 opt->dst1opt = hdr;
883 break;
885 case IPV6_DSTOPTS:
886 case IPV6_RTHDRDSTOPTS:
887 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
888 err = -EINVAL;
889 goto exit_f;
892 hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
893 len = ((hdr->hdrlen + 1) << 3);
894 if (cmsg->cmsg_len < CMSG_LEN(len)) {
895 err = -EINVAL;
896 goto exit_f;
898 if (!ns_capable(net->user_ns, CAP_NET_RAW)) {
899 err = -EPERM;
900 goto exit_f;
902 if (cmsg->cmsg_type == IPV6_DSTOPTS) {
903 opt->opt_flen += len;
904 opt->dst1opt = hdr;
905 } else {
906 opt->opt_nflen += len;
907 opt->dst0opt = hdr;
909 break;
911 case IPV6_2292RTHDR:
912 case IPV6_RTHDR:
913 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_rt_hdr))) {
914 err = -EINVAL;
915 goto exit_f;
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) {
925 err = -EINVAL;
926 goto exit_f;
928 break;
929 #endif
930 default:
931 err = -EINVAL;
932 goto exit_f;
935 len = ((rthdr->hdrlen + 1) << 3);
937 if (cmsg->cmsg_len < CMSG_LEN(len)) {
938 err = -EINVAL;
939 goto exit_f;
942 /* segments left must also match */
943 if ((rthdr->hdrlen >> 1) != rthdr->segments_left) {
944 err = -EINVAL;
945 goto exit_f;
948 opt->opt_nflen += len;
949 opt->srcrt = rthdr;
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;
956 opt->dst1opt = NULL;
957 opt->opt_flen -= dsthdrlen;
960 break;
962 case IPV6_2292HOPLIMIT:
963 case IPV6_HOPLIMIT:
964 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) {
965 err = -EINVAL;
966 goto exit_f;
969 ipc6->hlimit = *(int *)CMSG_DATA(cmsg);
970 if (ipc6->hlimit < -1 || ipc6->hlimit > 0xff) {
971 err = -EINVAL;
972 goto exit_f;
975 break;
977 case IPV6_TCLASS:
979 int tc;
981 err = -EINVAL;
982 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
983 goto exit_f;
985 tc = *(int *)CMSG_DATA(cmsg);
986 if (tc < -1 || tc > 0xff)
987 goto exit_f;
989 err = 0;
990 ipc6->tclass = tc;
992 break;
995 case IPV6_DONTFRAG:
997 int df;
999 err = -EINVAL;
1000 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
1001 goto exit_f;
1003 df = *(int *)CMSG_DATA(cmsg);
1004 if (df < 0 || df > 1)
1005 goto exit_f;
1007 err = 0;
1008 ipc6->dontfrag = df;
1010 break;
1012 default:
1013 net_dbg_ratelimited("invalid cmsg type: %d\n",
1014 cmsg->cmsg_type);
1015 err = -EINVAL;
1016 goto exit_f;
1020 exit_f:
1021 return err;
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;
1032 seq_printf(seq,
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",
1035 bucket,
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,
1040 sp->sk_state,
1041 sk_wmem_alloc_get(sp),
1042 sk_rmem_alloc_get(sp),
1043 0, 0L, 0,
1044 from_kuid_munged(seq_user_ns(seq), sock_i_uid(sp)),
1046 sock_i_ino(sp),
1047 atomic_read(&sp->sk_refcnt), sp,
1048 atomic_read(&sp->sk_drops));