1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * RAW sockets for IPv6 4 * Linux INET6 implementation 5 * 6 * Authors: 7 * Pedro Roque <roque@di.fc.ul.pt> 8 * 9 * Adapted from linux/net/ipv4/raw.c 10 * 11 * Fixes: 12 * Hideaki YOSHIFUJI : sin6_scope_id support 13 * YOSHIFUJI,H.@USAGI : raw checksum (RFC2292(bis) compliance) 14 * Kazunori MIYAZAWA @USAGI: change process style to use ip6_append_data 15 */ 16 17 #include <linux/errno.h> 18 #include <linux/types.h> 19 #include <linux/socket.h> 20 #include <linux/slab.h> 21 #include <linux/sockios.h> 22 #include <linux/net.h> 23 #include <linux/in6.h> 24 #include <linux/netdevice.h> 25 #include <linux/if_arp.h> 26 #include <linux/icmpv6.h> 27 #include <linux/netfilter.h> 28 #include <linux/netfilter_ipv6.h> 29 #include <linux/skbuff.h> 30 #include <linux/compat.h> 31 #include <linux/uaccess.h> 32 #include <asm/ioctls.h> 33 34 #include <net/net_namespace.h> 35 #include <net/ip.h> 36 #include <net/sock.h> 37 #include <net/snmp.h> 38 39 #include <net/ipv6.h> 40 #include <net/ndisc.h> 41 #include <net/protocol.h> 42 #include <net/ip6_route.h> 43 #include <net/ip6_checksum.h> 44 #include <net/addrconf.h> 45 #include <net/transp_v6.h> 46 #include <net/udp.h> 47 #include <net/inet_common.h> 48 #include <net/tcp_states.h> 49 #if IS_ENABLED(CONFIG_IPV6_MIP6) 50 #include <net/mip6.h> 51 #endif 52 #include <linux/mroute6.h> 53 54 #include <net/raw.h> 55 #include <net/rawv6.h> 56 #include <net/xfrm.h> 57 58 #include <linux/proc_fs.h> 59 #include <linux/seq_file.h> 60 #include <linux/export.h> 61 62 #define ICMPV6_HDRLEN 4 /* ICMPv6 header, RFC 4443 Section 2.1 */ 63 64 struct raw_hashinfo raw_v6_hashinfo; 65 EXPORT_SYMBOL_GPL(raw_v6_hashinfo); 66 67 bool raw_v6_match(struct net *net, const struct sock *sk, unsigned short num, 68 const struct in6_addr *loc_addr, 69 const struct in6_addr *rmt_addr, int dif, int sdif) 70 { 71 if (inet_sk(sk)->inet_num != num || 72 !net_eq(sock_net(sk), net) || 73 (!ipv6_addr_any(&sk->sk_v6_daddr) && 74 !ipv6_addr_equal(&sk->sk_v6_daddr, rmt_addr)) || 75 !raw_sk_bound_dev_eq(net, sk->sk_bound_dev_if, 76 dif, sdif)) 77 return false; 78 79 if (ipv6_addr_any(&sk->sk_v6_rcv_saddr) || 80 ipv6_addr_equal(&sk->sk_v6_rcv_saddr, loc_addr) || 81 (ipv6_addr_is_multicast(loc_addr) && 82 inet6_mc_check(sk, loc_addr, rmt_addr))) 83 return true; 84 85 return false; 86 } 87 EXPORT_SYMBOL_GPL(raw_v6_match); 88 89 /* 90 * 0 - deliver 91 * 1 - block 92 */ 93 static int icmpv6_filter(const struct sock *sk, const struct sk_buff *skb) 94 { 95 struct icmp6hdr _hdr; 96 const struct icmp6hdr *hdr; 97 98 /* We require only the four bytes of the ICMPv6 header, not any 99 * additional bytes of message body in "struct icmp6hdr". 100 */ 101 hdr = skb_header_pointer(skb, skb_transport_offset(skb), 102 ICMPV6_HDRLEN, &_hdr); 103 if (hdr) { 104 const __u32 *data = &raw6_sk(sk)->filter.data[0]; 105 unsigned int type = hdr->icmp6_type; 106 107 return (data[type >> 5] & (1U << (type & 31))) != 0; 108 } 109 return 1; 110 } 111 112 #if IS_ENABLED(CONFIG_IPV6_MIP6) 113 typedef int mh_filter_t(struct sock *sock, struct sk_buff *skb); 114 115 static mh_filter_t __rcu *mh_filter __read_mostly; 116 117 int rawv6_mh_filter_register(mh_filter_t filter) 118 { 119 rcu_assign_pointer(mh_filter, filter); 120 return 0; 121 } 122 EXPORT_SYMBOL(rawv6_mh_filter_register); 123 124 int rawv6_mh_filter_unregister(mh_filter_t filter) 125 { 126 RCU_INIT_POINTER(mh_filter, NULL); 127 synchronize_rcu(); 128 return 0; 129 } 130 EXPORT_SYMBOL(rawv6_mh_filter_unregister); 131 132 #endif 133 134 /* 135 * demultiplex raw sockets. 136 * (should consider queueing the skb in the sock receive_queue 137 * without calling rawv6.c) 138 * 139 * Caller owns SKB so we must make clones. 140 */ 141 static bool ipv6_raw_deliver(struct sk_buff *skb, int nexthdr) 142 { 143 struct net *net = dev_net(skb->dev); 144 const struct in6_addr *saddr; 145 const struct in6_addr *daddr; 146 struct hlist_head *hlist; 147 struct sock *sk; 148 bool delivered = false; 149 __u8 hash; 150 151 saddr = &ipv6_hdr(skb)->saddr; 152 daddr = saddr + 1; 153 154 hash = raw_hashfunc(net, nexthdr); 155 hlist = &raw_v6_hashinfo.ht[hash]; 156 rcu_read_lock(); 157 sk_for_each_rcu(sk, hlist) { 158 int filtered; 159 160 if (!raw_v6_match(net, sk, nexthdr, daddr, saddr, 161 inet6_iif(skb), inet6_sdif(skb))) 162 continue; 163 164 if (atomic_read(&sk->sk_rmem_alloc) >= 165 READ_ONCE(sk->sk_rcvbuf)) { 166 atomic_inc(&sk->sk_drops); 167 continue; 168 } 169 170 delivered = true; 171 switch (nexthdr) { 172 case IPPROTO_ICMPV6: 173 filtered = icmpv6_filter(sk, skb); 174 break; 175 176 #if IS_ENABLED(CONFIG_IPV6_MIP6) 177 case IPPROTO_MH: 178 { 179 /* XXX: To validate MH only once for each packet, 180 * this is placed here. It should be after checking 181 * xfrm policy, however it doesn't. The checking xfrm 182 * policy is placed in rawv6_rcv() because it is 183 * required for each socket. 184 */ 185 mh_filter_t *filter; 186 187 filter = rcu_dereference(mh_filter); 188 filtered = filter ? (*filter)(sk, skb) : 0; 189 break; 190 } 191 #endif 192 default: 193 filtered = 0; 194 break; 195 } 196 197 if (filtered < 0) 198 break; 199 if (filtered == 0) { 200 struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC); 201 202 /* Not releasing hash table! */ 203 if (clone) 204 rawv6_rcv(sk, clone); 205 } 206 } 207 rcu_read_unlock(); 208 return delivered; 209 } 210 211 bool raw6_local_deliver(struct sk_buff *skb, int nexthdr) 212 { 213 return ipv6_raw_deliver(skb, nexthdr); 214 } 215 216 /* This cleans up af_inet6 a bit. -DaveM */ 217 static int rawv6_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len) 218 { 219 struct inet_sock *inet = inet_sk(sk); 220 struct ipv6_pinfo *np = inet6_sk(sk); 221 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) uaddr; 222 __be32 v4addr = 0; 223 int addr_type; 224 int err; 225 226 if (addr_len < SIN6_LEN_RFC2133) 227 return -EINVAL; 228 229 if (addr->sin6_family != AF_INET6) 230 return -EINVAL; 231 232 addr_type = ipv6_addr_type(&addr->sin6_addr); 233 234 /* Raw sockets are IPv6 only */ 235 if (addr_type == IPV6_ADDR_MAPPED) 236 return -EADDRNOTAVAIL; 237 238 lock_sock(sk); 239 240 err = -EINVAL; 241 if (sk->sk_state != TCP_CLOSE) 242 goto out; 243 244 rcu_read_lock(); 245 /* Check if the address belongs to the host. */ 246 if (addr_type != IPV6_ADDR_ANY) { 247 struct net_device *dev = NULL; 248 249 if (__ipv6_addr_needs_scope_id(addr_type)) { 250 if (addr_len >= sizeof(struct sockaddr_in6) && 251 addr->sin6_scope_id) { 252 /* Override any existing binding, if another 253 * one is supplied by user. 254 */ 255 sk->sk_bound_dev_if = addr->sin6_scope_id; 256 } 257 258 /* Binding to link-local address requires an interface */ 259 if (!sk->sk_bound_dev_if) 260 goto out_unlock; 261 } 262 263 if (sk->sk_bound_dev_if) { 264 err = -ENODEV; 265 dev = dev_get_by_index_rcu(sock_net(sk), 266 sk->sk_bound_dev_if); 267 if (!dev) 268 goto out_unlock; 269 } 270 271 /* ipv4 addr of the socket is invalid. Only the 272 * unspecified and mapped address have a v4 equivalent. 273 */ 274 v4addr = LOOPBACK4_IPV6; 275 if (!(addr_type & IPV6_ADDR_MULTICAST) && 276 !ipv6_can_nonlocal_bind(sock_net(sk), inet)) { 277 err = -EADDRNOTAVAIL; 278 if (!ipv6_chk_addr(sock_net(sk), &addr->sin6_addr, 279 dev, 0)) { 280 goto out_unlock; 281 } 282 } 283 } 284 285 inet->inet_rcv_saddr = inet->inet_saddr = v4addr; 286 sk->sk_v6_rcv_saddr = addr->sin6_addr; 287 if (!(addr_type & IPV6_ADDR_MULTICAST)) 288 np->saddr = addr->sin6_addr; 289 err = 0; 290 out_unlock: 291 rcu_read_unlock(); 292 out: 293 release_sock(sk); 294 return err; 295 } 296 297 static void rawv6_err(struct sock *sk, struct sk_buff *skb, 298 u8 type, u8 code, int offset, __be32 info) 299 { 300 bool recverr = inet6_test_bit(RECVERR6, sk); 301 struct ipv6_pinfo *np = inet6_sk(sk); 302 int err; 303 int harderr; 304 305 /* Report error on raw socket, if: 306 1. User requested recverr. 307 2. Socket is connected (otherwise the error indication 308 is useless without recverr and error is hard. 309 */ 310 if (!recverr && sk->sk_state != TCP_ESTABLISHED) 311 return; 312 313 harderr = icmpv6_err_convert(type, code, &err); 314 if (type == ICMPV6_PKT_TOOBIG) { 315 ip6_sk_update_pmtu(skb, sk, info); 316 harderr = (READ_ONCE(np->pmtudisc) == IPV6_PMTUDISC_DO); 317 } 318 if (type == NDISC_REDIRECT) { 319 ip6_sk_redirect(skb, sk); 320 return; 321 } 322 if (recverr) { 323 u8 *payload = skb->data; 324 if (!inet_test_bit(HDRINCL, sk)) 325 payload += offset; 326 ipv6_icmp_error(sk, skb, err, 0, ntohl(info), payload); 327 } 328 329 if (recverr || harderr) { 330 sk->sk_err = err; 331 sk_error_report(sk); 332 } 333 } 334 335 void raw6_icmp_error(struct sk_buff *skb, int nexthdr, 336 u8 type, u8 code, int inner_offset, __be32 info) 337 { 338 struct net *net = dev_net(skb->dev); 339 struct hlist_head *hlist; 340 struct sock *sk; 341 int hash; 342 343 hash = raw_hashfunc(net, nexthdr); 344 hlist = &raw_v6_hashinfo.ht[hash]; 345 rcu_read_lock(); 346 sk_for_each_rcu(sk, hlist) { 347 /* Note: ipv6_hdr(skb) != skb->data */ 348 const struct ipv6hdr *ip6h = (const struct ipv6hdr *)skb->data; 349 350 if (!raw_v6_match(net, sk, nexthdr, &ip6h->saddr, &ip6h->daddr, 351 inet6_iif(skb), inet6_iif(skb))) 352 continue; 353 rawv6_err(sk, skb, type, code, inner_offset, info); 354 } 355 rcu_read_unlock(); 356 } 357 358 static inline int rawv6_rcv_skb(struct sock *sk, struct sk_buff *skb) 359 { 360 enum skb_drop_reason reason; 361 362 if ((raw6_sk(sk)->checksum || rcu_access_pointer(sk->sk_filter)) && 363 skb_checksum_complete(skb)) { 364 atomic_inc(&sk->sk_drops); 365 sk_skb_reason_drop(sk, skb, SKB_DROP_REASON_SKB_CSUM); 366 return NET_RX_DROP; 367 } 368 369 /* Charge it to the socket. */ 370 skb_dst_drop(skb); 371 if (sock_queue_rcv_skb_reason(sk, skb, &reason) < 0) { 372 sk_skb_reason_drop(sk, skb, reason); 373 return NET_RX_DROP; 374 } 375 376 return 0; 377 } 378 379 /* 380 * This is next to useless... 381 * if we demultiplex in network layer we don't need the extra call 382 * just to queue the skb... 383 * maybe we could have the network decide upon a hint if it 384 * should call raw_rcv for demultiplexing 385 */ 386 int rawv6_rcv(struct sock *sk, struct sk_buff *skb) 387 { 388 struct inet_sock *inet = inet_sk(sk); 389 struct raw6_sock *rp = raw6_sk(sk); 390 391 if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb)) { 392 atomic_inc(&sk->sk_drops); 393 sk_skb_reason_drop(sk, skb, SKB_DROP_REASON_XFRM_POLICY); 394 return NET_RX_DROP; 395 } 396 nf_reset_ct(skb); 397 398 if (!rp->checksum) 399 skb->ip_summed = CHECKSUM_UNNECESSARY; 400 401 if (skb->ip_summed == CHECKSUM_COMPLETE) { 402 skb_postpull_rcsum(skb, skb_network_header(skb), 403 skb_network_header_len(skb)); 404 if (!csum_ipv6_magic(&ipv6_hdr(skb)->saddr, 405 &ipv6_hdr(skb)->daddr, 406 skb->len, inet->inet_num, skb->csum)) 407 skb->ip_summed = CHECKSUM_UNNECESSARY; 408 } 409 if (!skb_csum_unnecessary(skb)) 410 skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr, 411 &ipv6_hdr(skb)->daddr, 412 skb->len, 413 inet->inet_num, 0)); 414 415 if (inet_test_bit(HDRINCL, sk)) { 416 if (skb_checksum_complete(skb)) { 417 atomic_inc(&sk->sk_drops); 418 sk_skb_reason_drop(sk, skb, SKB_DROP_REASON_SKB_CSUM); 419 return NET_RX_DROP; 420 } 421 } 422 423 rawv6_rcv_skb(sk, skb); 424 return 0; 425 } 426 427 428 /* 429 * This should be easy, if there is something there 430 * we return it, otherwise we block. 431 */ 432 433 static int rawv6_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, 434 int flags, int *addr_len) 435 { 436 struct ipv6_pinfo *np = inet6_sk(sk); 437 DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name); 438 struct sk_buff *skb; 439 size_t copied; 440 int err; 441 442 if (flags & MSG_OOB) 443 return -EOPNOTSUPP; 444 445 if (flags & MSG_ERRQUEUE) 446 return ipv6_recv_error(sk, msg, len, addr_len); 447 448 if (np->rxpmtu && np->rxopt.bits.rxpmtu) 449 return ipv6_recv_rxpmtu(sk, msg, len, addr_len); 450 451 skb = skb_recv_datagram(sk, flags, &err); 452 if (!skb) 453 goto out; 454 if (ccs_socket_post_recvmsg_permission(sk, skb, flags)) { 455 err = -EAGAIN; /* Hope less harmful than -EPERM. */ 456 goto out; 457 } 458 459 copied = skb->len; 460 if (copied > len) { 461 copied = len; 462 msg->msg_flags |= MSG_TRUNC; 463 } 464 465 if (skb_csum_unnecessary(skb)) { 466 err = skb_copy_datagram_msg(skb, 0, msg, copied); 467 } else if (msg->msg_flags&MSG_TRUNC) { 468 if (__skb_checksum_complete(skb)) 469 goto csum_copy_err; 470 err = skb_copy_datagram_msg(skb, 0, msg, copied); 471 } else { 472 err = skb_copy_and_csum_datagram_msg(skb, 0, msg); 473 if (err == -EINVAL) 474 goto csum_copy_err; 475 } 476 if (err) 477 goto out_free; 478 479 /* Copy the address. */ 480 if (sin6) { 481 sin6->sin6_family = AF_INET6; 482 sin6->sin6_port = 0; 483 sin6->sin6_addr = ipv6_hdr(skb)->saddr; 484 sin6->sin6_flowinfo = 0; 485 sin6->sin6_scope_id = ipv6_iface_scope_id(&sin6->sin6_addr, 486 inet6_iif(skb)); 487 *addr_len = sizeof(*sin6); 488 } 489 490 sock_recv_cmsgs(msg, sk, skb); 491 492 if (np->rxopt.all) 493 ip6_datagram_recv_ctl(sk, msg, skb); 494 495 err = copied; 496 if (flags & MSG_TRUNC) 497 err = skb->len; 498 499 out_free: 500 skb_free_datagram(sk, skb); 501 out: 502 return err; 503 504 csum_copy_err: 505 skb_kill_datagram(sk, skb, flags); 506 507 /* Error for blocking case is chosen to masquerade 508 as some normal condition. 509 */ 510 err = (flags&MSG_DONTWAIT) ? -EAGAIN : -EHOSTUNREACH; 511 goto out; 512 } 513 514 static int rawv6_push_pending_frames(struct sock *sk, struct flowi6 *fl6, 515 struct raw6_sock *rp) 516 { 517 struct ipv6_txoptions *opt; 518 struct sk_buff *skb; 519 int err = 0; 520 int offset; 521 int len; 522 int total_len; 523 __wsum tmp_csum; 524 __sum16 csum; 525 526 if (!rp->checksum) 527 goto send; 528 529 skb = skb_peek(&sk->sk_write_queue); 530 if (!skb) 531 goto out; 532 533 offset = rp->offset; 534 total_len = inet_sk(sk)->cork.base.length; 535 opt = inet6_sk(sk)->cork.opt; 536 total_len -= opt ? opt->opt_flen : 0; 537 538 if (offset >= total_len - 1) { 539 err = -EINVAL; 540 ip6_flush_pending_frames(sk); 541 goto out; 542 } 543 544 /* should be check HW csum miyazawa */ 545 if (skb_queue_len(&sk->sk_write_queue) == 1) { 546 /* 547 * Only one fragment on the socket. 548 */ 549 tmp_csum = skb->csum; 550 } else { 551 struct sk_buff *csum_skb = NULL; 552 tmp_csum = 0; 553 554 skb_queue_walk(&sk->sk_write_queue, skb) { 555 tmp_csum = csum_add(tmp_csum, skb->csum); 556 557 if (csum_skb) 558 continue; 559 560 len = skb->len - skb_transport_offset(skb); 561 if (offset >= len) { 562 offset -= len; 563 continue; 564 } 565 566 csum_skb = skb; 567 } 568 569 skb = csum_skb; 570 } 571 572 offset += skb_transport_offset(skb); 573 err = skb_copy_bits(skb, offset, &csum, 2); 574 if (err < 0) { 575 ip6_flush_pending_frames(sk); 576 goto out; 577 } 578 579 /* in case cksum was not initialized */ 580 if (unlikely(csum)) 581 tmp_csum = csum_sub(tmp_csum, csum_unfold(csum)); 582 583 csum = csum_ipv6_magic(&fl6->saddr, &fl6->daddr, 584 total_len, fl6->flowi6_proto, tmp_csum); 585 586 if (csum == 0 && fl6->flowi6_proto == IPPROTO_UDP) 587 csum = CSUM_MANGLED_0; 588 589 BUG_ON(skb_store_bits(skb, offset, &csum, 2)); 590 591 send: 592 err = ip6_push_pending_frames(sk); 593 out: 594 return err; 595 } 596 597 static int rawv6_send_hdrinc(struct sock *sk, struct msghdr *msg, int length, 598 struct flowi6 *fl6, struct dst_entry **dstp, 599 unsigned int flags, const struct sockcm_cookie *sockc) 600 { 601 struct net *net = sock_net(sk); 602 struct ipv6hdr *iph; 603 struct sk_buff *skb; 604 int err; 605 struct rt6_info *rt = dst_rt6_info(*dstp); 606 int hlen = LL_RESERVED_SPACE(rt->dst.dev); 607 int tlen = rt->dst.dev->needed_tailroom; 608 609 if (length > rt->dst.dev->mtu) { 610 ipv6_local_error(sk, EMSGSIZE, fl6, rt->dst.dev->mtu); 611 return -EMSGSIZE; 612 } 613 if (length < sizeof(struct ipv6hdr)) 614 return -EINVAL; 615 if (flags&MSG_PROBE) 616 goto out; 617 618 skb = sock_alloc_send_skb(sk, 619 length + hlen + tlen + 15, 620 flags & MSG_DONTWAIT, &err); 621 if (!skb) 622 goto error; 623 skb_reserve(skb, hlen); 624 625 skb->protocol = htons(ETH_P_IPV6); 626 skb->priority = READ_ONCE(sk->sk_priority); 627 skb->mark = sockc->mark; 628 skb_set_delivery_type_by_clockid(skb, sockc->transmit_time, sk->sk_clockid); 629 630 skb_put(skb, length); 631 skb_reset_network_header(skb); 632 iph = ipv6_hdr(skb); 633 634 skb->ip_summed = CHECKSUM_NONE; 635 636 skb_setup_tx_timestamp(skb, sockc->tsflags); 637 638 if (flags & MSG_CONFIRM) 639 skb_set_dst_pending_confirm(skb, 1); 640 641 skb->transport_header = skb->network_header; 642 err = memcpy_from_msg(iph, msg, length); 643 if (err) { 644 err = -EFAULT; 645 kfree_skb(skb); 646 goto error; 647 } 648 649 skb_dst_set(skb, &rt->dst); 650 *dstp = NULL; 651 652 /* if egress device is enslaved to an L3 master device pass the 653 * skb to its handler for processing 654 */ 655 skb = l3mdev_ip6_out(sk, skb); 656 if (unlikely(!skb)) 657 return 0; 658 659 /* Acquire rcu_read_lock() in case we need to use rt->rt6i_idev 660 * in the error path. Since skb has been freed, the dst could 661 * have been queued for deletion. 662 */ 663 rcu_read_lock(); 664 IP6_INC_STATS(net, rt->rt6i_idev, IPSTATS_MIB_OUTREQUESTS); 665 err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, net, sk, skb, 666 NULL, rt->dst.dev, dst_output); 667 if (err > 0) 668 err = net_xmit_errno(err); 669 if (err) { 670 IP6_INC_STATS(net, rt->rt6i_idev, IPSTATS_MIB_OUTDISCARDS); 671 rcu_read_unlock(); 672 goto error_check; 673 } 674 rcu_read_unlock(); 675 out: 676 return 0; 677 678 error: 679 IP6_INC_STATS(net, rt->rt6i_idev, IPSTATS_MIB_OUTDISCARDS); 680 error_check: 681 if (err == -ENOBUFS && !inet6_test_bit(RECVERR6, sk)) 682 err = 0; 683 return err; 684 } 685 686 struct raw6_frag_vec { 687 struct msghdr *msg; 688 int hlen; 689 char c[4]; 690 }; 691 692 static int rawv6_probe_proto_opt(struct raw6_frag_vec *rfv, struct flowi6 *fl6) 693 { 694 int err = 0; 695 switch (fl6->flowi6_proto) { 696 case IPPROTO_ICMPV6: 697 rfv->hlen = 2; 698 err = memcpy_from_msg(rfv->c, rfv->msg, rfv->hlen); 699 if (!err) { 700 fl6->fl6_icmp_type = rfv->c[0]; 701 fl6->fl6_icmp_code = rfv->c[1]; 702 } 703 break; 704 case IPPROTO_MH: 705 rfv->hlen = 4; 706 err = memcpy_from_msg(rfv->c, rfv->msg, rfv->hlen); 707 if (!err) 708 fl6->fl6_mh_type = rfv->c[2]; 709 } 710 return err; 711 } 712 713 static int raw6_getfrag(void *from, char *to, int offset, int len, int odd, 714 struct sk_buff *skb) 715 { 716 struct raw6_frag_vec *rfv = from; 717 718 if (offset < rfv->hlen) { 719 int copy = min(rfv->hlen - offset, len); 720 721 if (skb->ip_summed == CHECKSUM_PARTIAL) 722 memcpy(to, rfv->c + offset, copy); 723 else 724 skb->csum = csum_block_add( 725 skb->csum, 726 csum_partial_copy_nocheck(rfv->c + offset, 727 to, copy), 728 odd); 729 730 odd = 0; 731 offset += copy; 732 to += copy; 733 len -= copy; 734 735 if (!len) 736 return 0; 737 } 738 739 offset -= rfv->hlen; 740 741 return ip_generic_getfrag(rfv->msg, to, offset, len, odd, skb); 742 } 743 744 static int rawv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) 745 { 746 struct ipv6_txoptions *opt_to_free = NULL; 747 struct ipv6_txoptions opt_space; 748 DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name); 749 struct in6_addr *daddr, *final_p, final; 750 struct inet_sock *inet = inet_sk(sk); 751 struct ipv6_pinfo *np = inet6_sk(sk); 752 struct raw6_sock *rp = raw6_sk(sk); 753 struct ipv6_txoptions *opt = NULL; 754 struct ip6_flowlabel *flowlabel = NULL; 755 struct dst_entry *dst = NULL; 756 struct raw6_frag_vec rfv; 757 struct flowi6 fl6; 758 struct ipcm6_cookie ipc6; 759 int addr_len = msg->msg_namelen; 760 int hdrincl; 761 u16 proto; 762 int err; 763 764 /* Rough check on arithmetic overflow, 765 better check is made in ip6_append_data(). 766 */ 767 if (len > INT_MAX) 768 return -EMSGSIZE; 769 770 /* Mirror BSD error message compatibility */ 771 if (msg->msg_flags & MSG_OOB) 772 return -EOPNOTSUPP; 773 774 hdrincl = inet_test_bit(HDRINCL, sk); 775 776 /* 777 * Get and verify the address. 778 */ 779 memset(&fl6, 0, sizeof(fl6)); 780 781 fl6.flowi6_mark = READ_ONCE(sk->sk_mark); 782 fl6.flowi6_uid = sk->sk_uid; 783 784 ipcm6_init(&ipc6); 785 ipc6.sockc.tsflags = READ_ONCE(sk->sk_tsflags); 786 ipc6.sockc.mark = fl6.flowi6_mark; 787 788 if (sin6) { 789 if (addr_len < SIN6_LEN_RFC2133) 790 return -EINVAL; 791 792 if (sin6->sin6_family && sin6->sin6_family != AF_INET6) 793 return -EAFNOSUPPORT; 794 795 /* port is the proto value [0..255] carried in nexthdr */ 796 proto = ntohs(sin6->sin6_port); 797 798 if (!proto) 799 proto = inet->inet_num; 800 else if (proto != inet->inet_num && 801 inet->inet_num != IPPROTO_RAW) 802 return -EINVAL; 803 804 if (proto > 255) 805 return -EINVAL; 806 807 daddr = &sin6->sin6_addr; 808 if (inet6_test_bit(SNDFLOW, sk)) { 809 fl6.flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK; 810 if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) { 811 flowlabel = fl6_sock_lookup(sk, fl6.flowlabel); 812 if (IS_ERR(flowlabel)) 813 return -EINVAL; 814 } 815 } 816 817 /* 818 * Otherwise it will be difficult to maintain 819 * sk->sk_dst_cache. 820 */ 821 if (sk->sk_state == TCP_ESTABLISHED && 822 ipv6_addr_equal(daddr, &sk->sk_v6_daddr)) 823 daddr = &sk->sk_v6_daddr; 824 825 if (addr_len >= sizeof(struct sockaddr_in6) && 826 sin6->sin6_scope_id && 827 __ipv6_addr_needs_scope_id(__ipv6_addr_type(daddr))) 828 fl6.flowi6_oif = sin6->sin6_scope_id; 829 } else { 830 if (sk->sk_state != TCP_ESTABLISHED) 831 return -EDESTADDRREQ; 832 833 proto = inet->inet_num; 834 daddr = &sk->sk_v6_daddr; 835 fl6.flowlabel = np->flow_label; 836 } 837 838 if (fl6.flowi6_oif == 0) 839 fl6.flowi6_oif = sk->sk_bound_dev_if; 840 841 if (msg->msg_controllen) { 842 opt = &opt_space; 843 memset(opt, 0, sizeof(struct ipv6_txoptions)); 844 opt->tot_len = sizeof(struct ipv6_txoptions); 845 ipc6.opt = opt; 846 847 err = ip6_datagram_send_ctl(sock_net(sk), sk, msg, &fl6, &ipc6); 848 if (err < 0) { 849 fl6_sock_release(flowlabel); 850 return err; 851 } 852 if ((fl6.flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) { 853 flowlabel = fl6_sock_lookup(sk, fl6.flowlabel); 854 if (IS_ERR(flowlabel)) 855 return -EINVAL; 856 } 857 if (!(opt->opt_nflen|opt->opt_flen)) 858 opt = NULL; 859 } 860 if (!opt) { 861 opt = txopt_get(np); 862 opt_to_free = opt; 863 } 864 if (flowlabel) 865 opt = fl6_merge_options(&opt_space, flowlabel, opt); 866 opt = ipv6_fixup_options(&opt_space, opt); 867 868 fl6.flowi6_proto = proto; 869 fl6.flowi6_mark = ipc6.sockc.mark; 870 871 if (!hdrincl) { 872 rfv.msg = msg; 873 rfv.hlen = 0; 874 err = rawv6_probe_proto_opt(&rfv, &fl6); 875 if (err) 876 goto out; 877 } 878 879 if (!ipv6_addr_any(daddr)) 880 fl6.daddr = *daddr; 881 else 882 fl6.daddr.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */ 883 if (ipv6_addr_any(&fl6.saddr) && !ipv6_addr_any(&np->saddr)) 884 fl6.saddr = np->saddr; 885 886 final_p = fl6_update_dst(&fl6, opt, &final); 887 888 if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr)) 889 fl6.flowi6_oif = READ_ONCE(np->mcast_oif); 890 else if (!fl6.flowi6_oif) 891 fl6.flowi6_oif = READ_ONCE(np->ucast_oif); 892 security_sk_classify_flow(sk, flowi6_to_flowi_common(&fl6)); 893 894 if (hdrincl) 895 fl6.flowi6_flags |= FLOWI_FLAG_KNOWN_NH; 896 897 if (ipc6.tclass < 0) 898 ipc6.tclass = np->tclass; 899 900 fl6.flowlabel = ip6_make_flowinfo(ipc6.tclass, fl6.flowlabel); 901 902 dst = ip6_dst_lookup_flow(sock_net(sk), sk, &fl6, final_p); 903 if (IS_ERR(dst)) { 904 err = PTR_ERR(dst); 905 goto out; 906 } 907 if (ipc6.hlimit < 0) 908 ipc6.hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst); 909 910 if (ipc6.dontfrag < 0) 911 ipc6.dontfrag = inet6_test_bit(DONTFRAG, sk); 912 913 if (msg->msg_flags&MSG_CONFIRM) 914 goto do_confirm; 915 916 back_from_confirm: 917 if (hdrincl) 918 err = rawv6_send_hdrinc(sk, msg, len, &fl6, &dst, 919 msg->msg_flags, &ipc6.sockc); 920 else { 921 ipc6.opt = opt; 922 lock_sock(sk); 923 err = ip6_append_data(sk, raw6_getfrag, &rfv, 924 len, 0, &ipc6, &fl6, dst_rt6_info(dst), 925 msg->msg_flags); 926 927 if (err) 928 ip6_flush_pending_frames(sk); 929 else if (!(msg->msg_flags & MSG_MORE)) 930 err = rawv6_push_pending_frames(sk, &fl6, rp); 931 release_sock(sk); 932 } 933 done: 934 dst_release(dst); 935 out: 936 fl6_sock_release(flowlabel); 937 txopt_put(opt_to_free); 938 return err < 0 ? err : len; 939 do_confirm: 940 if (msg->msg_flags & MSG_PROBE) 941 dst_confirm_neigh(dst, &fl6.daddr); 942 if (!(msg->msg_flags & MSG_PROBE) || len) 943 goto back_from_confirm; 944 err = 0; 945 goto done; 946 } 947 948 static int rawv6_seticmpfilter(struct sock *sk, int optname, 949 sockptr_t optval, int optlen) 950 { 951 switch (optname) { 952 case ICMPV6_FILTER: 953 if (optlen > sizeof(struct icmp6_filter)) 954 optlen = sizeof(struct icmp6_filter); 955 if (copy_from_sockptr(&raw6_sk(sk)->filter, optval, optlen)) 956 return -EFAULT; 957 return 0; 958 default: 959 return -ENOPROTOOPT; 960 } 961 962 return 0; 963 } 964 965 static int rawv6_geticmpfilter(struct sock *sk, int optname, 966 char __user *optval, int __user *optlen) 967 { 968 int len; 969 970 switch (optname) { 971 case ICMPV6_FILTER: 972 if (get_user(len, optlen)) 973 return -EFAULT; 974 if (len < 0) 975 return -EINVAL; 976 if (len > sizeof(struct icmp6_filter)) 977 len = sizeof(struct icmp6_filter); 978 if (put_user(len, optlen)) 979 return -EFAULT; 980 if (copy_to_user(optval, &raw6_sk(sk)->filter, len)) 981 return -EFAULT; 982 return 0; 983 default: 984 return -ENOPROTOOPT; 985 } 986 987 return 0; 988 } 989 990 991 static int do_rawv6_setsockopt(struct sock *sk, int level, int optname, 992 sockptr_t optval, unsigned int optlen) 993 { 994 struct raw6_sock *rp = raw6_sk(sk); 995 int val; 996 997 if (optlen < sizeof(val)) 998 return -EINVAL; 999 1000 if (copy_from_sockptr(&val, optval, sizeof(val))) 1001 return -EFAULT; 1002 1003 switch (optname) { 1004 case IPV6_HDRINCL: 1005 if (sk->sk_type != SOCK_RAW) 1006 return -EINVAL; 1007 inet_assign_bit(HDRINCL, sk, val); 1008 return 0; 1009 case IPV6_CHECKSUM: 1010 if (inet_sk(sk)->inet_num == IPPROTO_ICMPV6 && 1011 level == IPPROTO_IPV6) { 1012 /* 1013 * RFC3542 tells that IPV6_CHECKSUM socket 1014 * option in the IPPROTO_IPV6 level is not 1015 * allowed on ICMPv6 sockets. 1016 * If you want to set it, use IPPROTO_RAW 1017 * level IPV6_CHECKSUM socket option 1018 * (Linux extension). 1019 */ 1020 return -EINVAL; 1021 } 1022 1023 /* You may get strange result with a positive odd offset; 1024 RFC2292bis agrees with me. */ 1025 if (val > 0 && (val&1)) 1026 return -EINVAL; 1027 if (val < 0) { 1028 rp->checksum = 0; 1029 } else { 1030 rp->checksum = 1; 1031 rp->offset = val; 1032 } 1033 1034 return 0; 1035 1036 default: 1037 return -ENOPROTOOPT; 1038 } 1039 } 1040 1041 static int rawv6_setsockopt(struct sock *sk, int level, int optname, 1042 sockptr_t optval, unsigned int optlen) 1043 { 1044 switch (level) { 1045 case SOL_RAW: 1046 break; 1047 1048 case SOL_ICMPV6: 1049 if (inet_sk(sk)->inet_num != IPPROTO_ICMPV6) 1050 return -EOPNOTSUPP; 1051 return rawv6_seticmpfilter(sk, optname, optval, optlen); 1052 case SOL_IPV6: 1053 if (optname == IPV6_CHECKSUM || 1054 optname == IPV6_HDRINCL) 1055 break; 1056 fallthrough; 1057 default: 1058 return ipv6_setsockopt(sk, level, optname, optval, optlen); 1059 } 1060 1061 return do_rawv6_setsockopt(sk, level, optname, optval, optlen); 1062 } 1063 1064 static int do_rawv6_getsockopt(struct sock *sk, int level, int optname, 1065 char __user *optval, int __user *optlen) 1066 { 1067 struct raw6_sock *rp = raw6_sk(sk); 1068 int val, len; 1069 1070 if (get_user(len, optlen)) 1071 return -EFAULT; 1072 1073 switch (optname) { 1074 case IPV6_HDRINCL: 1075 val = inet_test_bit(HDRINCL, sk); 1076 break; 1077 case IPV6_CHECKSUM: 1078 /* 1079 * We allow getsockopt() for IPPROTO_IPV6-level 1080 * IPV6_CHECKSUM socket option on ICMPv6 sockets 1081 * since RFC3542 is silent about it. 1082 */ 1083 if (rp->checksum == 0) 1084 val = -1; 1085 else 1086 val = rp->offset; 1087 break; 1088 1089 default: 1090 return -ENOPROTOOPT; 1091 } 1092 1093 len = min_t(unsigned int, sizeof(int), len); 1094 1095 if (put_user(len, optlen)) 1096 return -EFAULT; 1097 if (copy_to_user(optval, &val, len)) 1098 return -EFAULT; 1099 return 0; 1100 } 1101 1102 static int rawv6_getsockopt(struct sock *sk, int level, int optname, 1103 char __user *optval, int __user *optlen) 1104 { 1105 switch (level) { 1106 case SOL_RAW: 1107 break; 1108 1109 case SOL_ICMPV6: 1110 if (inet_sk(sk)->inet_num != IPPROTO_ICMPV6) 1111 return -EOPNOTSUPP; 1112 return rawv6_geticmpfilter(sk, optname, optval, optlen); 1113 case SOL_IPV6: 1114 if (optname == IPV6_CHECKSUM || 1115 optname == IPV6_HDRINCL) 1116 break; 1117 fallthrough; 1118 default: 1119 return ipv6_getsockopt(sk, level, optname, optval, optlen); 1120 } 1121 1122 return do_rawv6_getsockopt(sk, level, optname, optval, optlen); 1123 } 1124 1125 static int rawv6_ioctl(struct sock *sk, int cmd, int *karg) 1126 { 1127 switch (cmd) { 1128 case SIOCOUTQ: { 1129 *karg = sk_wmem_alloc_get(sk); 1130 return 0; 1131 } 1132 case SIOCINQ: { 1133 struct sk_buff *skb; 1134 1135 spin_lock_bh(&sk->sk_receive_queue.lock); 1136 skb = skb_peek(&sk->sk_receive_queue); 1137 if (skb) 1138 *karg = skb->len; 1139 else 1140 *karg = 0; 1141 spin_unlock_bh(&sk->sk_receive_queue.lock); 1142 return 0; 1143 } 1144 1145 default: 1146 #ifdef CONFIG_IPV6_MROUTE 1147 return ip6mr_ioctl(sk, cmd, karg); 1148 #else 1149 return -ENOIOCTLCMD; 1150 #endif 1151 } 1152 } 1153 1154 #ifdef CONFIG_COMPAT 1155 static int compat_rawv6_ioctl(struct sock *sk, unsigned int cmd, unsigned long arg) 1156 { 1157 switch (cmd) { 1158 case SIOCOUTQ: 1159 case SIOCINQ: 1160 return -ENOIOCTLCMD; 1161 default: 1162 #ifdef CONFIG_IPV6_MROUTE 1163 return ip6mr_compat_ioctl(sk, cmd, compat_ptr(arg)); 1164 #else 1165 return -ENOIOCTLCMD; 1166 #endif 1167 } 1168 } 1169 #endif 1170 1171 static void rawv6_close(struct sock *sk, long timeout) 1172 { 1173 if (inet_sk(sk)->inet_num == IPPROTO_RAW) 1174 ip6_ra_control(sk, -1); 1175 ip6mr_sk_done(sk); 1176 sk_common_release(sk); 1177 } 1178 1179 static void raw6_destroy(struct sock *sk) 1180 { 1181 lock_sock(sk); 1182 ip6_flush_pending_frames(sk); 1183 release_sock(sk); 1184 } 1185 1186 static int rawv6_init_sk(struct sock *sk) 1187 { 1188 struct raw6_sock *rp = raw6_sk(sk); 1189 1190 switch (inet_sk(sk)->inet_num) { 1191 case IPPROTO_ICMPV6: 1192 rp->checksum = 1; 1193 rp->offset = 2; 1194 break; 1195 case IPPROTO_MH: 1196 rp->checksum = 1; 1197 rp->offset = 4; 1198 break; 1199 default: 1200 break; 1201 } 1202 return 0; 1203 } 1204 1205 struct proto rawv6_prot = { 1206 .name = "RAWv6", 1207 .owner = THIS_MODULE, 1208 .close = rawv6_close, 1209 .destroy = raw6_destroy, 1210 .connect = ip6_datagram_connect_v6_only, 1211 .disconnect = __udp_disconnect, 1212 .ioctl = rawv6_ioctl, 1213 .init = rawv6_init_sk, 1214 .setsockopt = rawv6_setsockopt, 1215 .getsockopt = rawv6_getsockopt, 1216 .sendmsg = rawv6_sendmsg, 1217 .recvmsg = rawv6_recvmsg, 1218 .bind = rawv6_bind, 1219 .backlog_rcv = rawv6_rcv_skb, 1220 .hash = raw_hash_sk, 1221 .unhash = raw_unhash_sk, 1222 .obj_size = sizeof(struct raw6_sock), 1223 .ipv6_pinfo_offset = offsetof(struct raw6_sock, inet6), 1224 .useroffset = offsetof(struct raw6_sock, filter), 1225 .usersize = sizeof_field(struct raw6_sock, filter), 1226 .h.raw_hash = &raw_v6_hashinfo, 1227 #ifdef CONFIG_COMPAT 1228 .compat_ioctl = compat_rawv6_ioctl, 1229 #endif 1230 .diag_destroy = raw_abort, 1231 }; 1232 1233 #ifdef CONFIG_PROC_FS 1234 static int raw6_seq_show(struct seq_file *seq, void *v) 1235 { 1236 if (v == SEQ_START_TOKEN) { 1237 seq_puts(seq, IPV6_SEQ_DGRAM_HEADER); 1238 } else { 1239 struct sock *sp = v; 1240 __u16 srcp = inet_sk(sp)->inet_num; 1241 ip6_dgram_sock_seq_show(seq, v, srcp, 0, 1242 raw_seq_private(seq)->bucket); 1243 } 1244 return 0; 1245 } 1246 1247 static const struct seq_operations raw6_seq_ops = { 1248 .start = raw_seq_start, 1249 .next = raw_seq_next, 1250 .stop = raw_seq_stop, 1251 .show = raw6_seq_show, 1252 }; 1253 1254 static int __net_init raw6_init_net(struct net *net) 1255 { 1256 if (!proc_create_net_data("raw6", 0444, net->proc_net, &raw6_seq_ops, 1257 sizeof(struct raw_iter_state), &raw_v6_hashinfo)) 1258 return -ENOMEM; 1259 1260 return 0; 1261 } 1262 1263 static void __net_exit raw6_exit_net(struct net *net) 1264 { 1265 remove_proc_entry("raw6", net->proc_net); 1266 } 1267 1268 static struct pernet_operations raw6_net_ops = { 1269 .init = raw6_init_net, 1270 .exit = raw6_exit_net, 1271 }; 1272 1273 int __init raw6_proc_init(void) 1274 { 1275 return register_pernet_subsys(&raw6_net_ops); 1276 } 1277 1278 void raw6_proc_exit(void) 1279 { 1280 unregister_pernet_subsys(&raw6_net_ops); 1281 } 1282 #endif /* CONFIG_PROC_FS */ 1283 1284 /* Same as inet6_dgram_ops, sans udp_poll. */ 1285 const struct proto_ops inet6_sockraw_ops = { 1286 .family = PF_INET6, 1287 .owner = THIS_MODULE, 1288 .release = inet6_release, 1289 .bind = inet6_bind, 1290 .connect = inet_dgram_connect, /* ok */ 1291 .socketpair = sock_no_socketpair, /* a do nothing */ 1292 .accept = sock_no_accept, /* a do nothing */ 1293 .getname = inet6_getname, 1294 .poll = datagram_poll, /* ok */ 1295 .ioctl = inet6_ioctl, /* must change */ 1296 .gettstamp = sock_gettstamp, 1297 .listen = sock_no_listen, /* ok */ 1298 .shutdown = inet_shutdown, /* ok */ 1299 .setsockopt = sock_common_setsockopt, /* ok */ 1300 .getsockopt = sock_common_getsockopt, /* ok */ 1301 .sendmsg = inet_sendmsg, /* ok */ 1302 .recvmsg = sock_common_recvmsg, /* ok */ 1303 .mmap = sock_no_mmap, 1304 #ifdef CONFIG_COMPAT 1305 .compat_ioctl = inet6_compat_ioctl, 1306 #endif 1307 }; 1308 1309 static struct inet_protosw rawv6_protosw = { 1310 .type = SOCK_RAW, 1311 .protocol = IPPROTO_IP, /* wild card */ 1312 .prot = &rawv6_prot, 1313 .ops = &inet6_sockraw_ops, 1314 .flags = INET_PROTOSW_REUSE, 1315 }; 1316 1317 int __init rawv6_init(void) 1318 { 1319 return inet6_register_protosw(&rawv6_protosw); 1320 } 1321 1322 void rawv6_exit(void) 1323 { 1324 inet6_unregister_protosw(&rawv6_protosw); 1325 } 1326
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.