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