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