1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * common UDP/RAW code 4 * Linux INET6 implementation 5 * 6 * Authors: 7 * Pedro Roque <roque@di.fc.ul.pt> 8 */ 9 10 #include <linux/capability.h> 11 #include <linux/errno.h> 12 #include <linux/types.h> 13 #include <linux/kernel.h> 14 #include <linux/interrupt.h> 15 #include <linux/socket.h> 16 #include <linux/sockios.h> 17 #include <linux/in6.h> 18 #include <linux/ipv6.h> 19 #include <linux/route.h> 20 #include <linux/slab.h> 21 #include <linux/export.h> 22 23 #include <net/ipv6.h> 24 #include <net/ndisc.h> 25 #include <net/addrconf.h> 26 #include <net/transp_v6.h> 27 #include <net/ip6_route.h> 28 #include <net/tcp_states.h> 29 #include <net/dsfield.h> 30 31 #include <linux/errqueue.h> 32 #include <linux/uaccess.h> 33 34 static bool ipv6_mapped_addr_any(const struct in6_addr *a) 35 { 36 return ipv6_addr_v4mapped(a) && (a->s6_addr32[3] == 0); 37 } 38 39 static void ip6_datagram_flow_key_init(struct flowi6 *fl6, struct sock *sk) 40 { 41 struct inet_sock *inet = inet_sk(sk); 42 struct ipv6_pinfo *np = inet6_sk(sk); 43 44 memset(fl6, 0, sizeof(*fl6)); 45 fl6->flowi6_proto = sk->sk_protocol; 46 fl6->daddr = sk->sk_v6_daddr; 47 fl6->saddr = np->saddr; 48 fl6->flowi6_oif = sk->sk_bound_dev_if; 49 fl6->flowi6_mark = sk->sk_mark; 50 fl6->fl6_dport = inet->inet_dport; 51 fl6->fl6_sport = inet->inet_sport; 52 fl6->flowlabel = np->flow_label; 53 fl6->flowi6_uid = sk->sk_uid; 54 55 if (!fl6->flowi6_oif) 56 fl6->flowi6_oif = np->sticky_pktinfo.ipi6_ifindex; 57 58 if (!fl6->flowi6_oif && ipv6_addr_is_multicast(&fl6->daddr)) 59 fl6->flowi6_oif = np->mcast_oif; 60 61 security_sk_classify_flow(sk, flowi6_to_flowi(fl6)); 62 } 63 64 int ip6_datagram_dst_update(struct sock *sk, bool fix_sk_saddr) 65 { 66 struct ip6_flowlabel *flowlabel = NULL; 67 struct in6_addr *final_p, final; 68 struct ipv6_txoptions *opt; 69 struct dst_entry *dst; 70 struct inet_sock *inet = inet_sk(sk); 71 struct ipv6_pinfo *np = inet6_sk(sk); 72 struct flowi6 fl6; 73 int err = 0; 74 75 if (np->sndflow && (np->flow_label & IPV6_FLOWLABEL_MASK)) { 76 flowlabel = fl6_sock_lookup(sk, np->flow_label); 77 if (IS_ERR(flowlabel)) 78 return -EINVAL; 79 } 80 ip6_datagram_flow_key_init(&fl6, sk); 81 82 rcu_read_lock(); 83 opt = flowlabel ? flowlabel->opt : rcu_dereference(np->opt); 84 final_p = fl6_update_dst(&fl6, opt, &final); 85 rcu_read_unlock(); 86 87 dst = ip6_dst_lookup_flow(sk, &fl6, final_p); 88 if (IS_ERR(dst)) { 89 err = PTR_ERR(dst); 90 goto out; 91 } 92 93 if (fix_sk_saddr) { 94 if (ipv6_addr_any(&np->saddr)) 95 np->saddr = fl6.saddr; 96 97 if (ipv6_addr_any(&sk->sk_v6_rcv_saddr)) { 98 sk->sk_v6_rcv_saddr = fl6.saddr; 99 inet->inet_rcv_saddr = LOOPBACK4_IPV6; 100 if (sk->sk_prot->rehash) 101 sk->sk_prot->rehash(sk); 102 } 103 } 104 105 ip6_sk_dst_store_flow(sk, dst, &fl6); 106 107 out: 108 fl6_sock_release(flowlabel); 109 return err; 110 } 111 112 void ip6_datagram_release_cb(struct sock *sk) 113 { 114 struct dst_entry *dst; 115 116 if (ipv6_addr_v4mapped(&sk->sk_v6_daddr)) 117 return; 118 119 rcu_read_lock(); 120 dst = __sk_dst_get(sk); 121 if (!dst || !dst->obsolete || 122 dst->ops->check(dst, inet6_sk(sk)->dst_cookie)) { 123 rcu_read_unlock(); 124 return; 125 } 126 rcu_read_unlock(); 127 128 ip6_datagram_dst_update(sk, false); 129 } 130 EXPORT_SYMBOL_GPL(ip6_datagram_release_cb); 131 132 int __ip6_datagram_connect(struct sock *sk, struct sockaddr *uaddr, 133 int addr_len) 134 { 135 struct sockaddr_in6 *usin = (struct sockaddr_in6 *) uaddr; 136 struct inet_sock *inet = inet_sk(sk); 137 struct ipv6_pinfo *np = inet6_sk(sk); 138 struct in6_addr *daddr, old_daddr; 139 __be32 fl6_flowlabel = 0; 140 __be32 old_fl6_flowlabel; 141 __be16 old_dport; 142 int addr_type; 143 int err; 144 145 if (usin->sin6_family == AF_INET) { 146 if (__ipv6_only_sock(sk)) 147 return -EAFNOSUPPORT; 148 err = __ip4_datagram_connect(sk, uaddr, addr_len); 149 goto ipv4_connected; 150 } 151 152 if (addr_len < SIN6_LEN_RFC2133) 153 return -EINVAL; 154 155 if (usin->sin6_family != AF_INET6) 156 return -EAFNOSUPPORT; 157 158 if (np->sndflow) 159 fl6_flowlabel = usin->sin6_flowinfo & IPV6_FLOWINFO_MASK; 160 161 if (ipv6_addr_any(&usin->sin6_addr)) { 162 /* 163 * connect to self 164 */ 165 if (ipv6_addr_v4mapped(&sk->sk_v6_rcv_saddr)) 166 ipv6_addr_set_v4mapped(htonl(INADDR_LOOPBACK), 167 &usin->sin6_addr); 168 else 169 usin->sin6_addr = in6addr_loopback; 170 } 171 172 addr_type = ipv6_addr_type(&usin->sin6_addr); 173 174 daddr = &usin->sin6_addr; 175 176 if (addr_type & IPV6_ADDR_MAPPED) { 177 struct sockaddr_in sin; 178 179 if (__ipv6_only_sock(sk)) { 180 err = -ENETUNREACH; 181 goto out; 182 } 183 sin.sin_family = AF_INET; 184 sin.sin_addr.s_addr = daddr->s6_addr32[3]; 185 sin.sin_port = usin->sin6_port; 186 187 err = __ip4_datagram_connect(sk, 188 (struct sockaddr *) &sin, 189 sizeof(sin)); 190 191 ipv4_connected: 192 if (err) 193 goto out; 194 195 ipv6_addr_set_v4mapped(inet->inet_daddr, &sk->sk_v6_daddr); 196 197 if (ipv6_addr_any(&np->saddr) || 198 ipv6_mapped_addr_any(&np->saddr)) 199 ipv6_addr_set_v4mapped(inet->inet_saddr, &np->saddr); 200 201 if (ipv6_addr_any(&sk->sk_v6_rcv_saddr) || 202 ipv6_mapped_addr_any(&sk->sk_v6_rcv_saddr)) { 203 ipv6_addr_set_v4mapped(inet->inet_rcv_saddr, 204 &sk->sk_v6_rcv_saddr); 205 if (sk->sk_prot->rehash) 206 sk->sk_prot->rehash(sk); 207 } 208 209 goto out; 210 } 211 212 if (__ipv6_addr_needs_scope_id(addr_type)) { 213 if (addr_len >= sizeof(struct sockaddr_in6) && 214 usin->sin6_scope_id) { 215 if (!sk_dev_equal_l3scope(sk, usin->sin6_scope_id)) { 216 err = -EINVAL; 217 goto out; 218 } 219 sk->sk_bound_dev_if = usin->sin6_scope_id; 220 } 221 222 if (!sk->sk_bound_dev_if && (addr_type & IPV6_ADDR_MULTICAST)) 223 sk->sk_bound_dev_if = np->mcast_oif; 224 225 /* Connect to link-local address requires an interface */ 226 if (!sk->sk_bound_dev_if) { 227 err = -EINVAL; 228 goto out; 229 } 230 } 231 232 /* save the current peer information before updating it */ 233 old_daddr = sk->sk_v6_daddr; 234 old_fl6_flowlabel = np->flow_label; 235 old_dport = inet->inet_dport; 236 237 sk->sk_v6_daddr = *daddr; 238 np->flow_label = fl6_flowlabel; 239 inet->inet_dport = usin->sin6_port; 240 241 /* 242 * Check for a route to destination an obtain the 243 * destination cache for it. 244 */ 245 246 err = ip6_datagram_dst_update(sk, true); 247 if (err) { 248 /* Restore the socket peer info, to keep it consistent with 249 * the old socket state 250 */ 251 sk->sk_v6_daddr = old_daddr; 252 np->flow_label = old_fl6_flowlabel; 253 inet->inet_dport = old_dport; 254 goto out; 255 } 256 257 sk->sk_state = TCP_ESTABLISHED; 258 sk_set_txhash(sk); 259 out: 260 return err; 261 } 262 EXPORT_SYMBOL_GPL(__ip6_datagram_connect); 263 264 int ip6_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len) 265 { 266 int res; 267 268 lock_sock(sk); 269 res = __ip6_datagram_connect(sk, uaddr, addr_len); 270 release_sock(sk); 271 return res; 272 } 273 EXPORT_SYMBOL_GPL(ip6_datagram_connect); 274 275 int ip6_datagram_connect_v6_only(struct sock *sk, struct sockaddr *uaddr, 276 int addr_len) 277 { 278 DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, uaddr); 279 if (sin6->sin6_family != AF_INET6) 280 return -EAFNOSUPPORT; 281 return ip6_datagram_connect(sk, uaddr, addr_len); 282 } 283 EXPORT_SYMBOL_GPL(ip6_datagram_connect_v6_only); 284 285 void ipv6_icmp_error(struct sock *sk, struct sk_buff *skb, int err, 286 __be16 port, u32 info, u8 *payload) 287 { 288 struct ipv6_pinfo *np = inet6_sk(sk); 289 struct icmp6hdr *icmph = icmp6_hdr(skb); 290 struct sock_exterr_skb *serr; 291 292 if (!np->recverr) 293 return; 294 295 skb = skb_clone(skb, GFP_ATOMIC); 296 if (!skb) 297 return; 298 299 skb->protocol = htons(ETH_P_IPV6); 300 301 serr = SKB_EXT_ERR(skb); 302 serr->ee.ee_errno = err; 303 serr->ee.ee_origin = SO_EE_ORIGIN_ICMP6; 304 serr->ee.ee_type = icmph->icmp6_type; 305 serr->ee.ee_code = icmph->icmp6_code; 306 serr->ee.ee_pad = 0; 307 serr->ee.ee_info = info; 308 serr->ee.ee_data = 0; 309 serr->addr_offset = (u8 *)&(((struct ipv6hdr *)(icmph + 1))->daddr) - 310 skb_network_header(skb); 311 serr->port = port; 312 313 __skb_pull(skb, payload - skb->data); 314 skb_reset_transport_header(skb); 315 316 if (sock_queue_err_skb(sk, skb)) 317 kfree_skb(skb); 318 } 319 320 void ipv6_local_error(struct sock *sk, int err, struct flowi6 *fl6, u32 info) 321 { 322 const struct ipv6_pinfo *np = inet6_sk(sk); 323 struct sock_exterr_skb *serr; 324 struct ipv6hdr *iph; 325 struct sk_buff *skb; 326 327 if (!np->recverr) 328 return; 329 330 skb = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC); 331 if (!skb) 332 return; 333 334 skb->protocol = htons(ETH_P_IPV6); 335 336 skb_put(skb, sizeof(struct ipv6hdr)); 337 skb_reset_network_header(skb); 338 iph = ipv6_hdr(skb); 339 iph->daddr = fl6->daddr; 340 ip6_flow_hdr(iph, 0, 0); 341 342 serr = SKB_EXT_ERR(skb); 343 serr->ee.ee_errno = err; 344 serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL; 345 serr->ee.ee_type = 0; 346 serr->ee.ee_code = 0; 347 serr->ee.ee_pad = 0; 348 serr->ee.ee_info = info; 349 serr->ee.ee_data = 0; 350 serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb); 351 serr->port = fl6->fl6_dport; 352 353 __skb_pull(skb, skb_tail_pointer(skb) - skb->data); 354 skb_reset_transport_header(skb); 355 356 if (sock_queue_err_skb(sk, skb)) 357 kfree_skb(skb); 358 } 359 360 void ipv6_local_rxpmtu(struct sock *sk, struct flowi6 *fl6, u32 mtu) 361 { 362 struct ipv6_pinfo *np = inet6_sk(sk); 363 struct ipv6hdr *iph; 364 struct sk_buff *skb; 365 struct ip6_mtuinfo *mtu_info; 366 367 if (!np->rxopt.bits.rxpmtu) 368 return; 369 370 skb = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC); 371 if (!skb) 372 return; 373 374 skb_put(skb, sizeof(struct ipv6hdr)); 375 skb_reset_network_header(skb); 376 iph = ipv6_hdr(skb); 377 iph->daddr = fl6->daddr; 378 379 mtu_info = IP6CBMTU(skb); 380 381 mtu_info->ip6m_mtu = mtu; 382 mtu_info->ip6m_addr.sin6_family = AF_INET6; 383 mtu_info->ip6m_addr.sin6_port = 0; 384 mtu_info->ip6m_addr.sin6_flowinfo = 0; 385 mtu_info->ip6m_addr.sin6_scope_id = fl6->flowi6_oif; 386 mtu_info->ip6m_addr.sin6_addr = ipv6_hdr(skb)->daddr; 387 388 __skb_pull(skb, skb_tail_pointer(skb) - skb->data); 389 skb_reset_transport_header(skb); 390 391 skb = xchg(&np->rxpmtu, skb); 392 kfree_skb(skb); 393 } 394 395 /* For some errors we have valid addr_offset even with zero payload and 396 * zero port. Also, addr_offset should be supported if port is set. 397 */ 398 static inline bool ipv6_datagram_support_addr(struct sock_exterr_skb *serr) 399 { 400 return serr->ee.ee_origin == SO_EE_ORIGIN_ICMP6 || 401 serr->ee.ee_origin == SO_EE_ORIGIN_ICMP || 402 serr->ee.ee_origin == SO_EE_ORIGIN_LOCAL || serr->port; 403 } 404 405 /* IPv6 supports cmsg on all origins aside from SO_EE_ORIGIN_LOCAL. 406 * 407 * At one point, excluding local errors was a quick test to identify icmp/icmp6 408 * errors. This is no longer true, but the test remained, so the v6 stack, 409 * unlike v4, also honors cmsg requests on all wifi and timestamp errors. 410 */ 411 static bool ip6_datagram_support_cmsg(struct sk_buff *skb, 412 struct sock_exterr_skb *serr) 413 { 414 if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP || 415 serr->ee.ee_origin == SO_EE_ORIGIN_ICMP6) 416 return true; 417 418 if (serr->ee.ee_origin == SO_EE_ORIGIN_LOCAL) 419 return false; 420 421 if (!IP6CB(skb)->iif) 422 return false; 423 424 return true; 425 } 426 427 /* 428 * Handle MSG_ERRQUEUE 429 */ 430 int ipv6_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len) 431 { 432 struct ipv6_pinfo *np = inet6_sk(sk); 433 struct sock_exterr_skb *serr; 434 struct sk_buff *skb; 435 DECLARE_SOCKADDR(struct sockaddr_in6 *, sin, msg->msg_name); 436 struct { 437 struct sock_extended_err ee; 438 struct sockaddr_in6 offender; 439 } errhdr; 440 int err; 441 int copied; 442 443 err = -EAGAIN; 444 skb = sock_dequeue_err_skb(sk); 445 if (!skb) 446 goto out; 447 448 copied = skb->len; 449 if (copied > len) { 450 msg->msg_flags |= MSG_TRUNC; 451 copied = len; 452 } 453 err = skb_copy_datagram_msg(skb, 0, msg, copied); 454 if (unlikely(err)) { 455 kfree_skb(skb); 456 return err; 457 } 458 sock_recv_timestamp(msg, sk, skb); 459 460 serr = SKB_EXT_ERR(skb); 461 462 if (sin && ipv6_datagram_support_addr(serr)) { 463 const unsigned char *nh = skb_network_header(skb); 464 sin->sin6_family = AF_INET6; 465 sin->sin6_flowinfo = 0; 466 sin->sin6_port = serr->port; 467 if (skb->protocol == htons(ETH_P_IPV6)) { 468 const struct ipv6hdr *ip6h = container_of((struct in6_addr *)(nh + serr->addr_offset), 469 struct ipv6hdr, daddr); 470 sin->sin6_addr = ip6h->daddr; 471 if (np->sndflow) 472 sin->sin6_flowinfo = ip6_flowinfo(ip6h); 473 sin->sin6_scope_id = 474 ipv6_iface_scope_id(&sin->sin6_addr, 475 IP6CB(skb)->iif); 476 } else { 477 ipv6_addr_set_v4mapped(*(__be32 *)(nh + serr->addr_offset), 478 &sin->sin6_addr); 479 sin->sin6_scope_id = 0; 480 } 481 *addr_len = sizeof(*sin); 482 } 483 484 memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err)); 485 sin = &errhdr.offender; 486 memset(sin, 0, sizeof(*sin)); 487 488 if (ip6_datagram_support_cmsg(skb, serr)) { 489 sin->sin6_family = AF_INET6; 490 if (np->rxopt.all) 491 ip6_datagram_recv_common_ctl(sk, msg, skb); 492 if (skb->protocol == htons(ETH_P_IPV6)) { 493 sin->sin6_addr = ipv6_hdr(skb)->saddr; 494 if (np->rxopt.all) 495 ip6_datagram_recv_specific_ctl(sk, msg, skb); 496 sin->sin6_scope_id = 497 ipv6_iface_scope_id(&sin->sin6_addr, 498 IP6CB(skb)->iif); 499 } else { 500 ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr, 501 &sin->sin6_addr); 502 if (inet_sk(sk)->cmsg_flags) 503 ip_cmsg_recv(msg, skb); 504 } 505 } 506 507 put_cmsg(msg, SOL_IPV6, IPV6_RECVERR, sizeof(errhdr), &errhdr); 508 509 /* Now we could try to dump offended packet options */ 510 511 msg->msg_flags |= MSG_ERRQUEUE; 512 err = copied; 513 514 consume_skb(skb); 515 out: 516 return err; 517 } 518 EXPORT_SYMBOL_GPL(ipv6_recv_error); 519 520 /* 521 * Handle IPV6_RECVPATHMTU 522 */ 523 int ipv6_recv_rxpmtu(struct sock *sk, struct msghdr *msg, int len, 524 int *addr_len) 525 { 526 struct ipv6_pinfo *np = inet6_sk(sk); 527 struct sk_buff *skb; 528 struct ip6_mtuinfo mtu_info; 529 DECLARE_SOCKADDR(struct sockaddr_in6 *, sin, msg->msg_name); 530 int err; 531 int copied; 532 533 err = -EAGAIN; 534 skb = xchg(&np->rxpmtu, NULL); 535 if (!skb) 536 goto out; 537 538 copied = skb->len; 539 if (copied > len) { 540 msg->msg_flags |= MSG_TRUNC; 541 copied = len; 542 } 543 err = skb_copy_datagram_msg(skb, 0, msg, copied); 544 if (err) 545 goto out_free_skb; 546 547 sock_recv_timestamp(msg, sk, skb); 548 549 memcpy(&mtu_info, IP6CBMTU(skb), sizeof(mtu_info)); 550 551 if (sin) { 552 sin->sin6_family = AF_INET6; 553 sin->sin6_flowinfo = 0; 554 sin->sin6_port = 0; 555 sin->sin6_scope_id = mtu_info.ip6m_addr.sin6_scope_id; 556 sin->sin6_addr = mtu_info.ip6m_addr.sin6_addr; 557 *addr_len = sizeof(*sin); 558 } 559 560 put_cmsg(msg, SOL_IPV6, IPV6_PATHMTU, sizeof(mtu_info), &mtu_info); 561 562 err = copied; 563 564 out_free_skb: 565 kfree_skb(skb); 566 out: 567 return err; 568 } 569 570 571 void ip6_datagram_recv_common_ctl(struct sock *sk, struct msghdr *msg, 572 struct sk_buff *skb) 573 { 574 struct ipv6_pinfo *np = inet6_sk(sk); 575 bool is_ipv6 = skb->protocol == htons(ETH_P_IPV6); 576 577 if (np->rxopt.bits.rxinfo) { 578 struct in6_pktinfo src_info; 579 580 if (is_ipv6) { 581 src_info.ipi6_ifindex = IP6CB(skb)->iif; 582 src_info.ipi6_addr = ipv6_hdr(skb)->daddr; 583 } else { 584 src_info.ipi6_ifindex = 585 PKTINFO_SKB_CB(skb)->ipi_ifindex; 586 ipv6_addr_set_v4mapped(ip_hdr(skb)->daddr, 587 &src_info.ipi6_addr); 588 } 589 590 if (src_info.ipi6_ifindex >= 0) 591 put_cmsg(msg, SOL_IPV6, IPV6_PKTINFO, 592 sizeof(src_info), &src_info); 593 } 594 } 595 596 void ip6_datagram_recv_specific_ctl(struct sock *sk, struct msghdr *msg, 597 struct sk_buff *skb) 598 { 599 struct ipv6_pinfo *np = inet6_sk(sk); 600 struct inet6_skb_parm *opt = IP6CB(skb); 601 unsigned char *nh = skb_network_header(skb); 602 603 if (np->rxopt.bits.rxhlim) { 604 int hlim = ipv6_hdr(skb)->hop_limit; 605 put_cmsg(msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim); 606 } 607 608 if (np->rxopt.bits.rxtclass) { 609 int tclass = ipv6_get_dsfield(ipv6_hdr(skb)); 610 put_cmsg(msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass); 611 } 612 613 if (np->rxopt.bits.rxflow) { 614 __be32 flowinfo = ip6_flowinfo((struct ipv6hdr *)nh); 615 if (flowinfo) 616 put_cmsg(msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo); 617 } 618 619 /* HbH is allowed only once */ 620 if (np->rxopt.bits.hopopts && (opt->flags & IP6SKB_HOPBYHOP)) { 621 u8 *ptr = nh + sizeof(struct ipv6hdr); 622 put_cmsg(msg, SOL_IPV6, IPV6_HOPOPTS, (ptr[1]+1)<<3, ptr); 623 } 624 625 if (opt->lastopt && 626 (np->rxopt.bits.dstopts || np->rxopt.bits.srcrt)) { 627 /* 628 * Silly enough, but we need to reparse in order to 629 * report extension headers (except for HbH) 630 * in order. 631 * 632 * Also note that IPV6_RECVRTHDRDSTOPTS is NOT 633 * (and WILL NOT be) defined because 634 * IPV6_RECVDSTOPTS is more generic. --yoshfuji 635 */ 636 unsigned int off = sizeof(struct ipv6hdr); 637 u8 nexthdr = ipv6_hdr(skb)->nexthdr; 638 639 while (off <= opt->lastopt) { 640 unsigned int len; 641 u8 *ptr = nh + off; 642 643 switch (nexthdr) { 644 case IPPROTO_DSTOPTS: 645 nexthdr = ptr[0]; 646 len = (ptr[1] + 1) << 3; 647 if (np->rxopt.bits.dstopts) 648 put_cmsg(msg, SOL_IPV6, IPV6_DSTOPTS, len, ptr); 649 break; 650 case IPPROTO_ROUTING: 651 nexthdr = ptr[0]; 652 len = (ptr[1] + 1) << 3; 653 if (np->rxopt.bits.srcrt) 654 put_cmsg(msg, SOL_IPV6, IPV6_RTHDR, len, ptr); 655 break; 656 case IPPROTO_AH: 657 nexthdr = ptr[0]; 658 len = (ptr[1] + 2) << 2; 659 break; 660 default: 661 nexthdr = ptr[0]; 662 len = (ptr[1] + 1) << 3; 663 break; 664 } 665 666 off += len; 667 } 668 } 669 670 /* socket options in old style */ 671 if (np->rxopt.bits.rxoinfo) { 672 struct in6_pktinfo src_info; 673 674 src_info.ipi6_ifindex = opt->iif; 675 src_info.ipi6_addr = ipv6_hdr(skb)->daddr; 676 put_cmsg(msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info); 677 } 678 if (np->rxopt.bits.rxohlim) { 679 int hlim = ipv6_hdr(skb)->hop_limit; 680 put_cmsg(msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim); 681 } 682 if (np->rxopt.bits.ohopopts && (opt->flags & IP6SKB_HOPBYHOP)) { 683 u8 *ptr = nh + sizeof(struct ipv6hdr); 684 put_cmsg(msg, SOL_IPV6, IPV6_2292HOPOPTS, (ptr[1]+1)<<3, ptr); 685 } 686 if (np->rxopt.bits.odstopts && opt->dst0) { 687 u8 *ptr = nh + opt->dst0; 688 put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr); 689 } 690 if (np->rxopt.bits.osrcrt && opt->srcrt) { 691 struct ipv6_rt_hdr *rthdr = (struct ipv6_rt_hdr *)(nh + opt->srcrt); 692 put_cmsg(msg, SOL_IPV6, IPV6_2292RTHDR, (rthdr->hdrlen+1) << 3, rthdr); 693 } 694 if (np->rxopt.bits.odstopts && opt->dst1) { 695 u8 *ptr = nh + opt->dst1; 696 put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr); 697 } 698 if (np->rxopt.bits.rxorigdstaddr) { 699 struct sockaddr_in6 sin6; 700 __be16 _ports[2], *ports; 701 702 ports = skb_header_pointer(skb, skb_transport_offset(skb), 703 sizeof(_ports), &_ports); 704 if (ports) { 705 /* All current transport protocols have the port numbers in the 706 * first four bytes of the transport header and this function is 707 * written with this assumption in mind. 708 */ 709 sin6.sin6_family = AF_INET6; 710 sin6.sin6_addr = ipv6_hdr(skb)->daddr; 711 sin6.sin6_port = ports[1]; 712 sin6.sin6_flowinfo = 0; 713 sin6.sin6_scope_id = 714 ipv6_iface_scope_id(&ipv6_hdr(skb)->daddr, 715 opt->iif); 716 717 put_cmsg(msg, SOL_IPV6, IPV6_ORIGDSTADDR, sizeof(sin6), &sin6); 718 } 719 } 720 if (np->rxopt.bits.recvfragsize && opt->frag_max_size) { 721 int val = opt->frag_max_size; 722 723 put_cmsg(msg, SOL_IPV6, IPV6_RECVFRAGSIZE, sizeof(val), &val); 724 } 725 } 726 727 void ip6_datagram_recv_ctl(struct sock *sk, struct msghdr *msg, 728 struct sk_buff *skb) 729 { 730 ip6_datagram_recv_common_ctl(sk, msg, skb); 731 ip6_datagram_recv_specific_ctl(sk, msg, skb); 732 } 733 EXPORT_SYMBOL_GPL(ip6_datagram_recv_ctl); 734 735 int ip6_datagram_send_ctl(struct net *net, struct sock *sk, 736 struct msghdr *msg, struct flowi6 *fl6, 737 struct ipcm6_cookie *ipc6) 738 { 739 struct in6_pktinfo *src_info; 740 struct cmsghdr *cmsg; 741 struct ipv6_rt_hdr *rthdr; 742 struct ipv6_opt_hdr *hdr; 743 struct ipv6_txoptions *opt = ipc6->opt; 744 int len; 745 int err = 0; 746 747 for_each_cmsghdr(cmsg, msg) { 748 int addr_type; 749 750 if (!CMSG_OK(msg, cmsg)) { 751 err = -EINVAL; 752 goto exit_f; 753 } 754 755 if (cmsg->cmsg_level == SOL_SOCKET) { 756 err = __sock_cmsg_send(sk, msg, cmsg, &ipc6->sockc); 757 if (err) 758 return err; 759 continue; 760 } 761 762 if (cmsg->cmsg_level != SOL_IPV6) 763 continue; 764 765 switch (cmsg->cmsg_type) { 766 case IPV6_PKTINFO: 767 case IPV6_2292PKTINFO: 768 { 769 struct net_device *dev = NULL; 770 int src_idx; 771 772 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct in6_pktinfo))) { 773 err = -EINVAL; 774 goto exit_f; 775 } 776 777 src_info = (struct in6_pktinfo *)CMSG_DATA(cmsg); 778 src_idx = src_info->ipi6_ifindex; 779 780 if (src_idx) { 781 if (fl6->flowi6_oif && 782 src_idx != fl6->flowi6_oif && 783 (sk->sk_bound_dev_if != fl6->flowi6_oif || 784 !sk_dev_equal_l3scope(sk, src_idx))) 785 return -EINVAL; 786 fl6->flowi6_oif = src_idx; 787 } 788 789 addr_type = __ipv6_addr_type(&src_info->ipi6_addr); 790 791 rcu_read_lock(); 792 if (fl6->flowi6_oif) { 793 dev = dev_get_by_index_rcu(net, fl6->flowi6_oif); 794 if (!dev) { 795 rcu_read_unlock(); 796 return -ENODEV; 797 } 798 } else if (addr_type & IPV6_ADDR_LINKLOCAL) { 799 rcu_read_unlock(); 800 return -EINVAL; 801 } 802 803 if (addr_type != IPV6_ADDR_ANY) { 804 int strict = __ipv6_addr_src_scope(addr_type) <= IPV6_ADDR_SCOPE_LINKLOCAL; 805 if (!ipv6_can_nonlocal_bind(net, inet_sk(sk)) && 806 !ipv6_chk_addr_and_flags(net, &src_info->ipi6_addr, 807 dev, !strict, 0, 808 IFA_F_TENTATIVE) && 809 !ipv6_chk_acast_addr_src(net, dev, 810 &src_info->ipi6_addr)) 811 err = -EINVAL; 812 else 813 fl6->saddr = src_info->ipi6_addr; 814 } 815 816 rcu_read_unlock(); 817 818 if (err) 819 goto exit_f; 820 821 break; 822 } 823 824 case IPV6_FLOWINFO: 825 if (cmsg->cmsg_len < CMSG_LEN(4)) { 826 err = -EINVAL; 827 goto exit_f; 828 } 829 830 if (fl6->flowlabel&IPV6_FLOWINFO_MASK) { 831 if ((fl6->flowlabel^*(__be32 *)CMSG_DATA(cmsg))&~IPV6_FLOWINFO_MASK) { 832 err = -EINVAL; 833 goto exit_f; 834 } 835 } 836 fl6->flowlabel = IPV6_FLOWINFO_MASK & *(__be32 *)CMSG_DATA(cmsg); 837 break; 838 839 case IPV6_2292HOPOPTS: 840 case IPV6_HOPOPTS: 841 if (opt->hopopt || cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) { 842 err = -EINVAL; 843 goto exit_f; 844 } 845 846 hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg); 847 len = ((hdr->hdrlen + 1) << 3); 848 if (cmsg->cmsg_len < CMSG_LEN(len)) { 849 err = -EINVAL; 850 goto exit_f; 851 } 852 if (!ns_capable(net->user_ns, CAP_NET_RAW)) { 853 err = -EPERM; 854 goto exit_f; 855 } 856 opt->opt_nflen += len; 857 opt->hopopt = hdr; 858 break; 859 860 case IPV6_2292DSTOPTS: 861 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) { 862 err = -EINVAL; 863 goto exit_f; 864 } 865 866 hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg); 867 len = ((hdr->hdrlen + 1) << 3); 868 if (cmsg->cmsg_len < CMSG_LEN(len)) { 869 err = -EINVAL; 870 goto exit_f; 871 } 872 if (!ns_capable(net->user_ns, CAP_NET_RAW)) { 873 err = -EPERM; 874 goto exit_f; 875 } 876 if (opt->dst1opt) { 877 err = -EINVAL; 878 goto exit_f; 879 } 880 opt->opt_flen += len; 881 opt->dst1opt = hdr; 882 break; 883 884 case IPV6_DSTOPTS: 885 case IPV6_RTHDRDSTOPTS: 886 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) { 887 err = -EINVAL; 888 goto exit_f; 889 } 890 891 hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg); 892 len = ((hdr->hdrlen + 1) << 3); 893 if (cmsg->cmsg_len < CMSG_LEN(len)) { 894 err = -EINVAL; 895 goto exit_f; 896 } 897 if (!ns_capable(net->user_ns, CAP_NET_RAW)) { 898 err = -EPERM; 899 goto exit_f; 900 } 901 if (cmsg->cmsg_type == IPV6_DSTOPTS) { 902 opt->opt_flen += len; 903 opt->dst1opt = hdr; 904 } else { 905 opt->opt_nflen += len; 906 opt->dst0opt = hdr; 907 } 908 break; 909 910 case IPV6_2292RTHDR: 911 case IPV6_RTHDR: 912 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_rt_hdr))) { 913 err = -EINVAL; 914 goto exit_f; 915 } 916 917 rthdr = (struct ipv6_rt_hdr *)CMSG_DATA(cmsg); 918 919 switch (rthdr->type) { 920 #if IS_ENABLED(CONFIG_IPV6_MIP6) 921 case IPV6_SRCRT_TYPE_2: 922 if (rthdr->hdrlen != 2 || 923 rthdr->segments_left != 1) { 924 err = -EINVAL; 925 goto exit_f; 926 } 927 break; 928 #endif 929 default: 930 err = -EINVAL; 931 goto exit_f; 932 } 933 934 len = ((rthdr->hdrlen + 1) << 3); 935 936 if (cmsg->cmsg_len < CMSG_LEN(len)) { 937 err = -EINVAL; 938 goto exit_f; 939 } 940 941 /* segments left must also match */ 942 if ((rthdr->hdrlen >> 1) != rthdr->segments_left) { 943 err = -EINVAL; 944 goto exit_f; 945 } 946 947 opt->opt_nflen += len; 948 opt->srcrt = rthdr; 949 950 if (cmsg->cmsg_type == IPV6_2292RTHDR && opt->dst1opt) { 951 int dsthdrlen = ((opt->dst1opt->hdrlen+1)<<3); 952 953 opt->opt_nflen += dsthdrlen; 954 opt->dst0opt = opt->dst1opt; 955 opt->dst1opt = NULL; 956 opt->opt_flen -= dsthdrlen; 957 } 958 959 break; 960 961 case IPV6_2292HOPLIMIT: 962 case IPV6_HOPLIMIT: 963 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) { 964 err = -EINVAL; 965 goto exit_f; 966 } 967 968 ipc6->hlimit = *(int *)CMSG_DATA(cmsg); 969 if (ipc6->hlimit < -1 || ipc6->hlimit > 0xff) { 970 err = -EINVAL; 971 goto exit_f; 972 } 973 974 break; 975 976 case IPV6_TCLASS: 977 { 978 int tc; 979 980 err = -EINVAL; 981 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) 982 goto exit_f; 983 984 tc = *(int *)CMSG_DATA(cmsg); 985 if (tc < -1 || tc > 0xff) 986 goto exit_f; 987 988 err = 0; 989 ipc6->tclass = tc; 990 991 break; 992 } 993 994 case IPV6_DONTFRAG: 995 { 996 int df; 997 998 err = -EINVAL; 999 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) 1000 goto exit_f; 1001 1002 df = *(int *)CMSG_DATA(cmsg); 1003 if (df < 0 || df > 1) 1004 goto exit_f; 1005 1006 err = 0; 1007 ipc6->dontfrag = df; 1008 1009 break; 1010 } 1011 default: 1012 net_dbg_ratelimited("invalid cmsg type: %d\n", 1013 cmsg->cmsg_type); 1014 err = -EINVAL; 1015 goto exit_f; 1016 } 1017 } 1018 1019 exit_f: 1020 return err; 1021 } 1022 EXPORT_SYMBOL_GPL(ip6_datagram_send_ctl); 1023 1024 void __ip6_dgram_sock_seq_show(struct seq_file *seq, struct sock *sp, 1025 __u16 srcp, __u16 destp, int rqueue, int bucket) 1026 { 1027 const struct in6_addr *dest, *src; 1028 1029 dest = &sp->sk_v6_daddr; 1030 src = &sp->sk_v6_rcv_saddr; 1031 seq_printf(seq, 1032 "%5d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X " 1033 "%02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %u\n", 1034 bucket, 1035 src->s6_addr32[0], src->s6_addr32[1], 1036 src->s6_addr32[2], src->s6_addr32[3], srcp, 1037 dest->s6_addr32[0], dest->s6_addr32[1], 1038 dest->s6_addr32[2], dest->s6_addr32[3], destp, 1039 sp->sk_state, 1040 sk_wmem_alloc_get(sp), 1041 rqueue, 1042 0, 0L, 0, 1043 from_kuid_munged(seq_user_ns(seq), sock_i_uid(sp)), 1044 0, 1045 sock_i_ino(sp), 1046 refcount_read(&sp->sk_refcnt), sp, 1047 atomic_read(&sp->sk_drops)); 1048 } 1049