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