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 skb_dst_set(skb, &rt->dst); 655 *dstp = NULL; 656 657 skb_put(skb, length); 658 skb_reset_network_header(skb); 659 iph = ipv6_hdr(skb); 660 661 skb->ip_summed = CHECKSUM_NONE; 662 663 if (flags & MSG_CONFIRM) 664 skb_set_dst_pending_confirm(skb, 1); 665 666 skb->transport_header = skb->network_header; 667 err = memcpy_from_msg(iph, msg, length); 668 if (err) 669 goto error_fault; 670 671 /* if egress device is enslaved to an L3 master device pass the 672 * skb to its handler for processing 673 */ 674 skb = l3mdev_ip6_out(sk, skb); 675 if (unlikely(!skb)) 676 return 0; 677 678 IP6_UPD_PO_STATS(net, rt->rt6i_idev, IPSTATS_MIB_OUT, skb->len); 679 err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, net, sk, skb, 680 NULL, rt->dst.dev, dst_output); 681 if (err > 0) 682 err = net_xmit_errno(err); 683 if (err) 684 goto error; 685 out: 686 return 0; 687 688 error_fault: 689 err = -EFAULT; 690 kfree_skb(skb); 691 error: 692 IP6_INC_STATS(net, rt->rt6i_idev, IPSTATS_MIB_OUTDISCARDS); 693 if (err == -ENOBUFS && !np->recverr) 694 err = 0; 695 return err; 696 } 697 698 struct raw6_frag_vec { 699 struct msghdr *msg; 700 int hlen; 701 char c[4]; 702 }; 703 704 static int rawv6_probe_proto_opt(struct raw6_frag_vec *rfv, struct flowi6 *fl6) 705 { 706 int err = 0; 707 switch (fl6->flowi6_proto) { 708 case IPPROTO_ICMPV6: 709 rfv->hlen = 2; 710 err = memcpy_from_msg(rfv->c, rfv->msg, rfv->hlen); 711 if (!err) { 712 fl6->fl6_icmp_type = rfv->c[0]; 713 fl6->fl6_icmp_code = rfv->c[1]; 714 } 715 break; 716 case IPPROTO_MH: 717 rfv->hlen = 4; 718 err = memcpy_from_msg(rfv->c, rfv->msg, rfv->hlen); 719 if (!err) 720 fl6->fl6_mh_type = rfv->c[2]; 721 } 722 return err; 723 } 724 725 static int raw6_getfrag(void *from, char *to, int offset, int len, int odd, 726 struct sk_buff *skb) 727 { 728 struct raw6_frag_vec *rfv = from; 729 730 if (offset < rfv->hlen) { 731 int copy = min(rfv->hlen - offset, len); 732 733 if (skb->ip_summed == CHECKSUM_PARTIAL) 734 memcpy(to, rfv->c + offset, copy); 735 else 736 skb->csum = csum_block_add( 737 skb->csum, 738 csum_partial_copy_nocheck(rfv->c + offset, 739 to, copy, 0), 740 odd); 741 742 odd = 0; 743 offset += copy; 744 to += copy; 745 len -= copy; 746 747 if (!len) 748 return 0; 749 } 750 751 offset -= rfv->hlen; 752 753 return ip_generic_getfrag(rfv->msg, to, offset, len, odd, skb); 754 } 755 756 static int rawv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) 757 { 758 struct ipv6_txoptions *opt_to_free = NULL; 759 struct ipv6_txoptions opt_space; 760 DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name); 761 struct in6_addr *daddr, *final_p, final; 762 struct inet_sock *inet = inet_sk(sk); 763 struct ipv6_pinfo *np = inet6_sk(sk); 764 struct raw6_sock *rp = raw6_sk(sk); 765 struct ipv6_txoptions *opt = NULL; 766 struct ip6_flowlabel *flowlabel = NULL; 767 struct dst_entry *dst = NULL; 768 struct raw6_frag_vec rfv; 769 struct flowi6 fl6; 770 struct ipcm6_cookie ipc6; 771 int addr_len = msg->msg_namelen; 772 u16 proto; 773 int err; 774 775 /* Rough check on arithmetic overflow, 776 better check is made in ip6_append_data(). 777 */ 778 if (len > INT_MAX) 779 return -EMSGSIZE; 780 781 /* Mirror BSD error message compatibility */ 782 if (msg->msg_flags & MSG_OOB) 783 return -EOPNOTSUPP; 784 785 /* 786 * Get and verify the address. 787 */ 788 memset(&fl6, 0, sizeof(fl6)); 789 790 fl6.flowi6_mark = sk->sk_mark; 791 fl6.flowi6_uid = sk->sk_uid; 792 793 ipcm6_init(&ipc6); 794 ipc6.sockc.tsflags = sk->sk_tsflags; 795 796 if (sin6) { 797 if (addr_len < SIN6_LEN_RFC2133) 798 return -EINVAL; 799 800 if (sin6->sin6_family && sin6->sin6_family != AF_INET6) 801 return -EAFNOSUPPORT; 802 803 /* port is the proto value [0..255] carried in nexthdr */ 804 proto = ntohs(sin6->sin6_port); 805 806 if (!proto) 807 proto = inet->inet_num; 808 else if (proto != inet->inet_num) 809 return -EINVAL; 810 811 if (proto > 255) 812 return -EINVAL; 813 814 daddr = &sin6->sin6_addr; 815 if (np->sndflow) { 816 fl6.flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK; 817 if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) { 818 flowlabel = fl6_sock_lookup(sk, fl6.flowlabel); 819 if (!flowlabel) 820 return -EINVAL; 821 } 822 } 823 824 /* 825 * Otherwise it will be difficult to maintain 826 * sk->sk_dst_cache. 827 */ 828 if (sk->sk_state == TCP_ESTABLISHED && 829 ipv6_addr_equal(daddr, &sk->sk_v6_daddr)) 830 daddr = &sk->sk_v6_daddr; 831 832 if (addr_len >= sizeof(struct sockaddr_in6) && 833 sin6->sin6_scope_id && 834 __ipv6_addr_needs_scope_id(__ipv6_addr_type(daddr))) 835 fl6.flowi6_oif = sin6->sin6_scope_id; 836 } else { 837 if (sk->sk_state != TCP_ESTABLISHED) 838 return -EDESTADDRREQ; 839 840 proto = inet->inet_num; 841 daddr = &sk->sk_v6_daddr; 842 fl6.flowlabel = np->flow_label; 843 } 844 845 if (fl6.flowi6_oif == 0) 846 fl6.flowi6_oif = sk->sk_bound_dev_if; 847 848 if (msg->msg_controllen) { 849 opt = &opt_space; 850 memset(opt, 0, sizeof(struct ipv6_txoptions)); 851 opt->tot_len = sizeof(struct ipv6_txoptions); 852 ipc6.opt = opt; 853 854 err = ip6_datagram_send_ctl(sock_net(sk), sk, msg, &fl6, &ipc6); 855 if (err < 0) { 856 fl6_sock_release(flowlabel); 857 return err; 858 } 859 if ((fl6.flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) { 860 flowlabel = fl6_sock_lookup(sk, fl6.flowlabel); 861 if (!flowlabel) 862 return -EINVAL; 863 } 864 if (!(opt->opt_nflen|opt->opt_flen)) 865 opt = NULL; 866 } 867 if (!opt) { 868 opt = txopt_get(np); 869 opt_to_free = opt; 870 } 871 if (flowlabel) 872 opt = fl6_merge_options(&opt_space, flowlabel, opt); 873 opt = ipv6_fixup_options(&opt_space, opt); 874 875 fl6.flowi6_proto = proto; 876 rfv.msg = msg; 877 rfv.hlen = 0; 878 err = rawv6_probe_proto_opt(&rfv, &fl6); 879 if (err) 880 goto out; 881 882 if (!ipv6_addr_any(daddr)) 883 fl6.daddr = *daddr; 884 else 885 fl6.daddr.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */ 886 if (ipv6_addr_any(&fl6.saddr) && !ipv6_addr_any(&np->saddr)) 887 fl6.saddr = np->saddr; 888 889 final_p = fl6_update_dst(&fl6, opt, &final); 890 891 if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr)) 892 fl6.flowi6_oif = np->mcast_oif; 893 else if (!fl6.flowi6_oif) 894 fl6.flowi6_oif = np->ucast_oif; 895 security_sk_classify_flow(sk, flowi6_to_flowi(&fl6)); 896 897 if (inet->hdrincl) 898 fl6.flowi6_flags |= FLOWI_FLAG_KNOWN_NH; 899 900 if (ipc6.tclass < 0) 901 ipc6.tclass = np->tclass; 902 903 fl6.flowlabel = ip6_make_flowinfo(ipc6.tclass, fl6.flowlabel); 904 905 dst = ip6_dst_lookup_flow(sk, &fl6, final_p); 906 if (IS_ERR(dst)) { 907 err = PTR_ERR(dst); 908 goto out; 909 } 910 if (ipc6.hlimit < 0) 911 ipc6.hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst); 912 913 if (ipc6.dontfrag < 0) 914 ipc6.dontfrag = np->dontfrag; 915 916 if (msg->msg_flags&MSG_CONFIRM) 917 goto do_confirm; 918 919 back_from_confirm: 920 if (inet->hdrincl) 921 err = rawv6_send_hdrinc(sk, msg, len, &fl6, &dst, 922 msg->msg_flags, &ipc6.sockc); 923 else { 924 ipc6.opt = opt; 925 lock_sock(sk); 926 err = ip6_append_data(sk, raw6_getfrag, &rfv, 927 len, 0, &ipc6, &fl6, (struct rt6_info *)dst, 928 msg->msg_flags); 929 930 if (err) 931 ip6_flush_pending_frames(sk); 932 else if (!(msg->msg_flags & MSG_MORE)) 933 err = rawv6_push_pending_frames(sk, &fl6, rp); 934 release_sock(sk); 935 } 936 done: 937 dst_release(dst); 938 out: 939 fl6_sock_release(flowlabel); 940 txopt_put(opt_to_free); 941 return err < 0 ? err : len; 942 do_confirm: 943 if (msg->msg_flags & MSG_PROBE) 944 dst_confirm_neigh(dst, &fl6.daddr); 945 if (!(msg->msg_flags & MSG_PROBE) || len) 946 goto back_from_confirm; 947 err = 0; 948 goto done; 949 } 950 951 static int rawv6_seticmpfilter(struct sock *sk, int level, int optname, 952 char __user *optval, int optlen) 953 { 954 switch (optname) { 955 case ICMPV6_FILTER: 956 if (optlen > sizeof(struct icmp6_filter)) 957 optlen = sizeof(struct icmp6_filter); 958 if (copy_from_user(&raw6_sk(sk)->filter, optval, optlen)) 959 return -EFAULT; 960 return 0; 961 default: 962 return -ENOPROTOOPT; 963 } 964 965 return 0; 966 } 967 968 static int rawv6_geticmpfilter(struct sock *sk, int level, int optname, 969 char __user *optval, int __user *optlen) 970 { 971 int len; 972 973 switch (optname) { 974 case ICMPV6_FILTER: 975 if (get_user(len, optlen)) 976 return -EFAULT; 977 if (len < 0) 978 return -EINVAL; 979 if (len > sizeof(struct icmp6_filter)) 980 len = sizeof(struct icmp6_filter); 981 if (put_user(len, optlen)) 982 return -EFAULT; 983 if (copy_to_user(optval, &raw6_sk(sk)->filter, len)) 984 return -EFAULT; 985 return 0; 986 default: 987 return -ENOPROTOOPT; 988 } 989 990 return 0; 991 } 992 993 994 static int do_rawv6_setsockopt(struct sock *sk, int level, int optname, 995 char __user *optval, unsigned int optlen) 996 { 997 struct raw6_sock *rp = raw6_sk(sk); 998 int val; 999 1000 if (get_user(val, (int __user *)optval)) 1001 return -EFAULT; 1002 1003 switch (optname) { 1004 case IPV6_HDRINCL: 1005 if (sk->sk_type != SOCK_RAW) 1006 return -EINVAL; 1007 inet_sk(sk)->hdrincl = !!val; 1008 return 0; 1009 case IPV6_CHECKSUM: 1010 if (inet_sk(sk)->inet_num == IPPROTO_ICMPV6 && 1011 level == IPPROTO_IPV6) { 1012 /* 1013 * RFC3542 tells that IPV6_CHECKSUM socket 1014 * option in the IPPROTO_IPV6 level is not 1015 * allowed on ICMPv6 sockets. 1016 * If you want to set it, use IPPROTO_RAW 1017 * level IPV6_CHECKSUM socket option 1018 * (Linux extension). 1019 */ 1020 return -EINVAL; 1021 } 1022 1023 /* You may get strange result with a positive odd offset; 1024 RFC2292bis agrees with me. */ 1025 if (val > 0 && (val&1)) 1026 return -EINVAL; 1027 if (val < 0) { 1028 rp->checksum = 0; 1029 } else { 1030 rp->checksum = 1; 1031 rp->offset = val; 1032 } 1033 1034 return 0; 1035 1036 default: 1037 return -ENOPROTOOPT; 1038 } 1039 } 1040 1041 static int rawv6_setsockopt(struct sock *sk, int level, int optname, 1042 char __user *optval, unsigned int optlen) 1043 { 1044 switch (level) { 1045 case SOL_RAW: 1046 break; 1047 1048 case SOL_ICMPV6: 1049 if (inet_sk(sk)->inet_num != IPPROTO_ICMPV6) 1050 return -EOPNOTSUPP; 1051 return rawv6_seticmpfilter(sk, level, optname, optval, optlen); 1052 case SOL_IPV6: 1053 if (optname == IPV6_CHECKSUM || 1054 optname == IPV6_HDRINCL) 1055 break; 1056 /* fall through */ 1057 default: 1058 return ipv6_setsockopt(sk, level, optname, optval, optlen); 1059 } 1060 1061 return do_rawv6_setsockopt(sk, level, optname, optval, optlen); 1062 } 1063 1064 #ifdef CONFIG_COMPAT 1065 static int compat_rawv6_setsockopt(struct sock *sk, int level, int optname, 1066 char __user *optval, unsigned int optlen) 1067 { 1068 switch (level) { 1069 case SOL_RAW: 1070 break; 1071 case SOL_ICMPV6: 1072 if (inet_sk(sk)->inet_num != IPPROTO_ICMPV6) 1073 return -EOPNOTSUPP; 1074 return rawv6_seticmpfilter(sk, level, optname, optval, optlen); 1075 case SOL_IPV6: 1076 if (optname == IPV6_CHECKSUM || 1077 optname == IPV6_HDRINCL) 1078 break; 1079 /* fall through */ 1080 default: 1081 return compat_ipv6_setsockopt(sk, level, optname, 1082 optval, optlen); 1083 } 1084 return do_rawv6_setsockopt(sk, level, optname, optval, optlen); 1085 } 1086 #endif 1087 1088 static int do_rawv6_getsockopt(struct sock *sk, int level, int optname, 1089 char __user *optval, int __user *optlen) 1090 { 1091 struct raw6_sock *rp = raw6_sk(sk); 1092 int val, len; 1093 1094 if (get_user(len, optlen)) 1095 return -EFAULT; 1096 1097 switch (optname) { 1098 case IPV6_HDRINCL: 1099 val = inet_sk(sk)->hdrincl; 1100 break; 1101 case IPV6_CHECKSUM: 1102 /* 1103 * We allow getsockopt() for IPPROTO_IPV6-level 1104 * IPV6_CHECKSUM socket option on ICMPv6 sockets 1105 * since RFC3542 is silent about it. 1106 */ 1107 if (rp->checksum == 0) 1108 val = -1; 1109 else 1110 val = rp->offset; 1111 break; 1112 1113 default: 1114 return -ENOPROTOOPT; 1115 } 1116 1117 len = min_t(unsigned int, sizeof(int), len); 1118 1119 if (put_user(len, optlen)) 1120 return -EFAULT; 1121 if (copy_to_user(optval, &val, len)) 1122 return -EFAULT; 1123 return 0; 1124 } 1125 1126 static int rawv6_getsockopt(struct sock *sk, int level, int optname, 1127 char __user *optval, int __user *optlen) 1128 { 1129 switch (level) { 1130 case SOL_RAW: 1131 break; 1132 1133 case SOL_ICMPV6: 1134 if (inet_sk(sk)->inet_num != IPPROTO_ICMPV6) 1135 return -EOPNOTSUPP; 1136 return rawv6_geticmpfilter(sk, level, optname, optval, optlen); 1137 case SOL_IPV6: 1138 if (optname == IPV6_CHECKSUM || 1139 optname == IPV6_HDRINCL) 1140 break; 1141 /* fall through */ 1142 default: 1143 return ipv6_getsockopt(sk, level, optname, optval, optlen); 1144 } 1145 1146 return do_rawv6_getsockopt(sk, level, optname, optval, optlen); 1147 } 1148 1149 #ifdef CONFIG_COMPAT 1150 static int compat_rawv6_getsockopt(struct sock *sk, int level, int optname, 1151 char __user *optval, int __user *optlen) 1152 { 1153 switch (level) { 1154 case SOL_RAW: 1155 break; 1156 case SOL_ICMPV6: 1157 if (inet_sk(sk)->inet_num != IPPROTO_ICMPV6) 1158 return -EOPNOTSUPP; 1159 return rawv6_geticmpfilter(sk, level, optname, optval, optlen); 1160 case SOL_IPV6: 1161 if (optname == IPV6_CHECKSUM || 1162 optname == IPV6_HDRINCL) 1163 break; 1164 /* fall through */ 1165 default: 1166 return compat_ipv6_getsockopt(sk, level, optname, 1167 optval, optlen); 1168 } 1169 return do_rawv6_getsockopt(sk, level, optname, optval, optlen); 1170 } 1171 #endif 1172 1173 static int rawv6_ioctl(struct sock *sk, int cmd, unsigned long arg) 1174 { 1175 switch (cmd) { 1176 case SIOCOUTQ: { 1177 int amount = sk_wmem_alloc_get(sk); 1178 1179 return put_user(amount, (int __user *)arg); 1180 } 1181 case SIOCINQ: { 1182 struct sk_buff *skb; 1183 int amount = 0; 1184 1185 spin_lock_bh(&sk->sk_receive_queue.lock); 1186 skb = skb_peek(&sk->sk_receive_queue); 1187 if (skb) 1188 amount = skb->len; 1189 spin_unlock_bh(&sk->sk_receive_queue.lock); 1190 return put_user(amount, (int __user *)arg); 1191 } 1192 1193 default: 1194 #ifdef CONFIG_IPV6_MROUTE 1195 return ip6mr_ioctl(sk, cmd, (void __user *)arg); 1196 #else 1197 return -ENOIOCTLCMD; 1198 #endif 1199 } 1200 } 1201 1202 #ifdef CONFIG_COMPAT 1203 static int compat_rawv6_ioctl(struct sock *sk, unsigned int cmd, unsigned long arg) 1204 { 1205 switch (cmd) { 1206 case SIOCOUTQ: 1207 case SIOCINQ: 1208 return -ENOIOCTLCMD; 1209 default: 1210 #ifdef CONFIG_IPV6_MROUTE 1211 return ip6mr_compat_ioctl(sk, cmd, compat_ptr(arg)); 1212 #else 1213 return -ENOIOCTLCMD; 1214 #endif 1215 } 1216 } 1217 #endif 1218 1219 static void rawv6_close(struct sock *sk, long timeout) 1220 { 1221 if (inet_sk(sk)->inet_num == IPPROTO_RAW) 1222 ip6_ra_control(sk, -1); 1223 ip6mr_sk_done(sk); 1224 sk_common_release(sk); 1225 } 1226 1227 static void raw6_destroy(struct sock *sk) 1228 { 1229 lock_sock(sk); 1230 ip6_flush_pending_frames(sk); 1231 release_sock(sk); 1232 1233 inet6_destroy_sock(sk); 1234 } 1235 1236 static int rawv6_init_sk(struct sock *sk) 1237 { 1238 struct raw6_sock *rp = raw6_sk(sk); 1239 1240 switch (inet_sk(sk)->inet_num) { 1241 case IPPROTO_ICMPV6: 1242 rp->checksum = 1; 1243 rp->offset = 2; 1244 break; 1245 case IPPROTO_MH: 1246 rp->checksum = 1; 1247 rp->offset = 4; 1248 break; 1249 default: 1250 break; 1251 } 1252 return 0; 1253 } 1254 1255 struct proto rawv6_prot = { 1256 .name = "RAWv6", 1257 .owner = THIS_MODULE, 1258 .close = rawv6_close, 1259 .destroy = raw6_destroy, 1260 .connect = ip6_datagram_connect_v6_only, 1261 .disconnect = __udp_disconnect, 1262 .ioctl = rawv6_ioctl, 1263 .init = rawv6_init_sk, 1264 .setsockopt = rawv6_setsockopt, 1265 .getsockopt = rawv6_getsockopt, 1266 .sendmsg = rawv6_sendmsg, 1267 .recvmsg = rawv6_recvmsg, 1268 .bind = rawv6_bind, 1269 .backlog_rcv = rawv6_rcv_skb, 1270 .hash = raw_hash_sk, 1271 .unhash = raw_unhash_sk, 1272 .obj_size = sizeof(struct raw6_sock), 1273 .useroffset = offsetof(struct raw6_sock, filter), 1274 .usersize = sizeof_field(struct raw6_sock, filter), 1275 .h.raw_hash = &raw_v6_hashinfo, 1276 #ifdef CONFIG_COMPAT 1277 .compat_setsockopt = compat_rawv6_setsockopt, 1278 .compat_getsockopt = compat_rawv6_getsockopt, 1279 .compat_ioctl = compat_rawv6_ioctl, 1280 #endif 1281 .diag_destroy = raw_abort, 1282 }; 1283 1284 #ifdef CONFIG_PROC_FS 1285 static int raw6_seq_show(struct seq_file *seq, void *v) 1286 { 1287 if (v == SEQ_START_TOKEN) { 1288 seq_puts(seq, IPV6_SEQ_DGRAM_HEADER); 1289 } else { 1290 struct sock *sp = v; 1291 __u16 srcp = inet_sk(sp)->inet_num; 1292 ip6_dgram_sock_seq_show(seq, v, srcp, 0, 1293 raw_seq_private(seq)->bucket); 1294 } 1295 return 0; 1296 } 1297 1298 static const struct seq_operations raw6_seq_ops = { 1299 .start = raw_seq_start, 1300 .next = raw_seq_next, 1301 .stop = raw_seq_stop, 1302 .show = raw6_seq_show, 1303 }; 1304 1305 static int __net_init raw6_init_net(struct net *net) 1306 { 1307 if (!proc_create_net_data("raw6", 0444, net->proc_net, &raw6_seq_ops, 1308 sizeof(struct raw_iter_state), &raw_v6_hashinfo)) 1309 return -ENOMEM; 1310 1311 return 0; 1312 } 1313 1314 static void __net_exit raw6_exit_net(struct net *net) 1315 { 1316 remove_proc_entry("raw6", net->proc_net); 1317 } 1318 1319 static struct pernet_operations raw6_net_ops = { 1320 .init = raw6_init_net, 1321 .exit = raw6_exit_net, 1322 }; 1323 1324 int __init raw6_proc_init(void) 1325 { 1326 return register_pernet_subsys(&raw6_net_ops); 1327 } 1328 1329 void raw6_proc_exit(void) 1330 { 1331 unregister_pernet_subsys(&raw6_net_ops); 1332 } 1333 #endif /* CONFIG_PROC_FS */ 1334 1335 /* Same as inet6_dgram_ops, sans udp_poll. */ 1336 const struct proto_ops inet6_sockraw_ops = { 1337 .family = PF_INET6, 1338 .owner = THIS_MODULE, 1339 .release = inet6_release, 1340 .bind = inet6_bind, 1341 .connect = inet_dgram_connect, /* ok */ 1342 .socketpair = sock_no_socketpair, /* a do nothing */ 1343 .accept = sock_no_accept, /* a do nothing */ 1344 .getname = inet6_getname, 1345 .poll = datagram_poll, /* ok */ 1346 .ioctl = inet6_ioctl, /* must change */ 1347 .listen = sock_no_listen, /* ok */ 1348 .shutdown = inet_shutdown, /* ok */ 1349 .setsockopt = sock_common_setsockopt, /* ok */ 1350 .getsockopt = sock_common_getsockopt, /* ok */ 1351 .sendmsg = inet_sendmsg, /* ok */ 1352 .recvmsg = sock_common_recvmsg, /* ok */ 1353 .mmap = sock_no_mmap, 1354 .sendpage = sock_no_sendpage, 1355 #ifdef CONFIG_COMPAT 1356 .compat_setsockopt = compat_sock_common_setsockopt, 1357 .compat_getsockopt = compat_sock_common_getsockopt, 1358 #endif 1359 }; 1360 1361 static struct inet_protosw rawv6_protosw = { 1362 .type = SOCK_RAW, 1363 .protocol = IPPROTO_IP, /* wild card */ 1364 .prot = &rawv6_prot, 1365 .ops = &inet6_sockraw_ops, 1366 .flags = INET_PROTOSW_REUSE, 1367 }; 1368 1369 int __init rawv6_init(void) 1370 { 1371 return inet6_register_protosw(&rawv6_protosw); 1372 } 1373 1374 void rawv6_exit(void) 1375 { 1376 inet6_unregister_protosw(&rawv6_protosw); 1377 } 1378