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