1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * INET An implementation of the TCP/IP protocol suite for the LINUX 4 * operating system. INET is implemented using the BSD Socket 5 * interface as the means of communication with the user level. 6 * 7 * RAW - implementation of IP "raw" sockets. 8 * 9 * Authors: Ross Biro 10 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG> 11 * 12 * Fixes: 13 * Alan Cox : verify_area() fixed up 14 * Alan Cox : ICMP error handling 15 * Alan Cox : EMSGSIZE if you send too big a packet 16 * Alan Cox : Now uses generic datagrams and shared 17 * skbuff library. No more peek crashes, 18 * no more backlogs 19 * Alan Cox : Checks sk->broadcast. 20 * Alan Cox : Uses skb_free_datagram/skb_copy_datagram 21 * Alan Cox : Raw passes ip options too 22 * Alan Cox : Setsocketopt added 23 * Alan Cox : Fixed error return for broadcasts 24 * Alan Cox : Removed wake_up calls 25 * Alan Cox : Use ttl/tos 26 * Alan Cox : Cleaned up old debugging 27 * Alan Cox : Use new kernel side addresses 28 * Arnt Gulbrandsen : Fixed MSG_DONTROUTE in raw sockets. 29 * Alan Cox : BSD style RAW socket demultiplexing. 30 * Alan Cox : Beginnings of mrouted support. 31 * Alan Cox : Added IP_HDRINCL option. 32 * Alan Cox : Skip broadcast check if BSDism set. 33 * David S. Miller : New socket lookup architecture. 34 */ 35 36 #include <linux/types.h> 37 #include <linux/atomic.h> 38 #include <asm/byteorder.h> 39 #include <asm/current.h> 40 #include <linux/uaccess.h> 41 #include <asm/ioctls.h> 42 #include <linux/stddef.h> 43 #include <linux/slab.h> 44 #include <linux/errno.h> 45 #include <linux/kernel.h> 46 #include <linux/export.h> 47 #include <linux/spinlock.h> 48 #include <linux/sockios.h> 49 #include <linux/socket.h> 50 #include <linux/in.h> 51 #include <linux/mroute.h> 52 #include <linux/netdevice.h> 53 #include <linux/in_route.h> 54 #include <linux/route.h> 55 #include <linux/skbuff.h> 56 #include <linux/igmp.h> 57 #include <net/net_namespace.h> 58 #include <net/dst.h> 59 #include <net/sock.h> 60 #include <linux/ip.h> 61 #include <linux/net.h> 62 #include <net/ip.h> 63 #include <net/icmp.h> 64 #include <net/udp.h> 65 #include <net/raw.h> 66 #include <net/snmp.h> 67 #include <net/tcp_states.h> 68 #include <net/inet_common.h> 69 #include <net/checksum.h> 70 #include <net/xfrm.h> 71 #include <linux/rtnetlink.h> 72 #include <linux/proc_fs.h> 73 #include <linux/seq_file.h> 74 #include <linux/netfilter.h> 75 #include <linux/netfilter_ipv4.h> 76 #include <linux/compat.h> 77 #include <linux/uio.h> 78 79 struct raw_frag_vec { 80 struct msghdr *msg; 81 union { 82 struct icmphdr icmph; 83 char c[1]; 84 } hdr; 85 int hlen; 86 }; 87 88 struct raw_hashinfo raw_v4_hashinfo; 89 EXPORT_SYMBOL_GPL(raw_v4_hashinfo); 90 91 int raw_hash_sk(struct sock *sk) 92 { 93 struct raw_hashinfo *h = sk->sk_prot->h.raw_hash; 94 struct hlist_head *hlist; 95 96 hlist = &h->ht[raw_hashfunc(sock_net(sk), inet_sk(sk)->inet_num)]; 97 98 spin_lock(&h->lock); 99 sk_add_node_rcu(sk, hlist); 100 sock_set_flag(sk, SOCK_RCU_FREE); 101 spin_unlock(&h->lock); 102 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1); 103 104 return 0; 105 } 106 EXPORT_SYMBOL_GPL(raw_hash_sk); 107 108 void raw_unhash_sk(struct sock *sk) 109 { 110 struct raw_hashinfo *h = sk->sk_prot->h.raw_hash; 111 112 spin_lock(&h->lock); 113 if (sk_del_node_init_rcu(sk)) 114 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1); 115 spin_unlock(&h->lock); 116 } 117 EXPORT_SYMBOL_GPL(raw_unhash_sk); 118 119 bool raw_v4_match(struct net *net, const struct sock *sk, unsigned short num, 120 __be32 raddr, __be32 laddr, int dif, int sdif) 121 { 122 const struct inet_sock *inet = inet_sk(sk); 123 124 if (net_eq(sock_net(sk), net) && inet->inet_num == num && 125 !(inet->inet_daddr && inet->inet_daddr != raddr) && 126 !(inet->inet_rcv_saddr && inet->inet_rcv_saddr != laddr) && 127 raw_sk_bound_dev_eq(net, sk->sk_bound_dev_if, dif, sdif)) 128 return true; 129 return false; 130 } 131 EXPORT_SYMBOL_GPL(raw_v4_match); 132 133 /* 134 * 0 - deliver 135 * 1 - block 136 */ 137 static int icmp_filter(const struct sock *sk, const struct sk_buff *skb) 138 { 139 struct icmphdr _hdr; 140 const struct icmphdr *hdr; 141 142 hdr = skb_header_pointer(skb, skb_transport_offset(skb), 143 sizeof(_hdr), &_hdr); 144 if (!hdr) 145 return 1; 146 147 if (hdr->type < 32) { 148 __u32 data = raw_sk(sk)->filter.data; 149 150 return ((1U << hdr->type) & data) != 0; 151 } 152 153 /* Do not block unknown ICMP types */ 154 return 0; 155 } 156 157 /* IP input processing comes here for RAW socket delivery. 158 * Caller owns SKB, so we must make clones. 159 * 160 * RFC 1122: SHOULD pass TOS value up to the transport layer. 161 * -> It does. And not only TOS, but all IP header. 162 */ 163 static int raw_v4_input(struct net *net, struct sk_buff *skb, 164 const struct iphdr *iph, int hash) 165 { 166 int sdif = inet_sdif(skb); 167 struct hlist_head *hlist; 168 int dif = inet_iif(skb); 169 int delivered = 0; 170 struct sock *sk; 171 172 hlist = &raw_v4_hashinfo.ht[hash]; 173 rcu_read_lock(); 174 sk_for_each_rcu(sk, hlist) { 175 if (!raw_v4_match(net, sk, iph->protocol, 176 iph->saddr, iph->daddr, dif, sdif)) 177 continue; 178 delivered = 1; 179 if ((iph->protocol != IPPROTO_ICMP || !icmp_filter(sk, skb)) && 180 ip_mc_sf_allow(sk, iph->daddr, iph->saddr, 181 skb->dev->ifindex, sdif)) { 182 struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC); 183 184 /* Not releasing hash table! */ 185 if (clone) 186 raw_rcv(sk, clone); 187 } 188 } 189 rcu_read_unlock(); 190 return delivered; 191 } 192 193 int raw_local_deliver(struct sk_buff *skb, int protocol) 194 { 195 struct net *net = dev_net(skb->dev); 196 197 return raw_v4_input(net, skb, ip_hdr(skb), 198 raw_hashfunc(net, protocol)); 199 } 200 201 static void raw_err(struct sock *sk, struct sk_buff *skb, u32 info) 202 { 203 struct inet_sock *inet = inet_sk(sk); 204 const int type = icmp_hdr(skb)->type; 205 const int code = icmp_hdr(skb)->code; 206 int err = 0; 207 int harderr = 0; 208 209 if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED) 210 ipv4_sk_update_pmtu(skb, sk, info); 211 else if (type == ICMP_REDIRECT) { 212 ipv4_sk_redirect(skb, sk); 213 return; 214 } 215 216 /* Report error on raw socket, if: 217 1. User requested ip_recverr. 218 2. Socket is connected (otherwise the error indication 219 is useless without ip_recverr and error is hard. 220 */ 221 if (!inet->recverr && sk->sk_state != TCP_ESTABLISHED) 222 return; 223 224 switch (type) { 225 default: 226 case ICMP_TIME_EXCEEDED: 227 err = EHOSTUNREACH; 228 break; 229 case ICMP_SOURCE_QUENCH: 230 return; 231 case ICMP_PARAMETERPROB: 232 err = EPROTO; 233 harderr = 1; 234 break; 235 case ICMP_DEST_UNREACH: 236 err = EHOSTUNREACH; 237 if (code > NR_ICMP_UNREACH) 238 break; 239 if (code == ICMP_FRAG_NEEDED) { 240 harderr = inet->pmtudisc != IP_PMTUDISC_DONT; 241 err = EMSGSIZE; 242 } else { 243 err = icmp_err_convert[code].errno; 244 harderr = icmp_err_convert[code].fatal; 245 } 246 } 247 248 if (inet->recverr) { 249 const struct iphdr *iph = (const struct iphdr *)skb->data; 250 u8 *payload = skb->data + (iph->ihl << 2); 251 252 if (inet->hdrincl) 253 payload = skb->data; 254 ip_icmp_error(sk, skb, err, 0, info, payload); 255 } 256 257 if (inet->recverr || harderr) { 258 sk->sk_err = err; 259 sk_error_report(sk); 260 } 261 } 262 263 void raw_icmp_error(struct sk_buff *skb, int protocol, u32 info) 264 { 265 struct net *net = dev_net(skb->dev); 266 int dif = skb->dev->ifindex; 267 int sdif = inet_sdif(skb); 268 struct hlist_head *hlist; 269 const struct iphdr *iph; 270 struct sock *sk; 271 int hash; 272 273 hash = raw_hashfunc(net, protocol); 274 hlist = &raw_v4_hashinfo.ht[hash]; 275 276 rcu_read_lock(); 277 sk_for_each_rcu(sk, hlist) { 278 iph = (const struct iphdr *)skb->data; 279 if (!raw_v4_match(net, sk, iph->protocol, 280 iph->daddr, iph->saddr, dif, sdif)) 281 continue; 282 raw_err(sk, skb, info); 283 } 284 rcu_read_unlock(); 285 } 286 287 static int raw_rcv_skb(struct sock *sk, struct sk_buff *skb) 288 { 289 enum skb_drop_reason reason; 290 291 /* Charge it to the socket. */ 292 293 ipv4_pktinfo_prepare(sk, skb); 294 if (sock_queue_rcv_skb_reason(sk, skb, &reason) < 0) { 295 kfree_skb_reason(skb, reason); 296 return NET_RX_DROP; 297 } 298 299 return NET_RX_SUCCESS; 300 } 301 302 int raw_rcv(struct sock *sk, struct sk_buff *skb) 303 { 304 if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb)) { 305 atomic_inc(&sk->sk_drops); 306 kfree_skb_reason(skb, SKB_DROP_REASON_XFRM_POLICY); 307 return NET_RX_DROP; 308 } 309 nf_reset_ct(skb); 310 311 skb_push(skb, skb->data - skb_network_header(skb)); 312 313 raw_rcv_skb(sk, skb); 314 return 0; 315 } 316 317 static int raw_send_hdrinc(struct sock *sk, struct flowi4 *fl4, 318 struct msghdr *msg, size_t length, 319 struct rtable **rtp, unsigned int flags, 320 const struct sockcm_cookie *sockc) 321 { 322 struct inet_sock *inet = inet_sk(sk); 323 struct net *net = sock_net(sk); 324 struct iphdr *iph; 325 struct sk_buff *skb; 326 unsigned int iphlen; 327 int err; 328 struct rtable *rt = *rtp; 329 int hlen, tlen; 330 331 if (length > rt->dst.dev->mtu) { 332 ip_local_error(sk, EMSGSIZE, fl4->daddr, inet->inet_dport, 333 rt->dst.dev->mtu); 334 return -EMSGSIZE; 335 } 336 if (length < sizeof(struct iphdr)) 337 return -EINVAL; 338 339 if (flags&MSG_PROBE) 340 goto out; 341 342 hlen = LL_RESERVED_SPACE(rt->dst.dev); 343 tlen = rt->dst.dev->needed_tailroom; 344 skb = sock_alloc_send_skb(sk, 345 length + hlen + tlen + 15, 346 flags & MSG_DONTWAIT, &err); 347 if (!skb) 348 goto error; 349 skb_reserve(skb, hlen); 350 351 skb->priority = sk->sk_priority; 352 skb->mark = sockc->mark; 353 skb->tstamp = sockc->transmit_time; 354 skb_dst_set(skb, &rt->dst); 355 *rtp = NULL; 356 357 skb_reset_network_header(skb); 358 iph = ip_hdr(skb); 359 skb_put(skb, length); 360 361 skb->ip_summed = CHECKSUM_NONE; 362 363 skb_setup_tx_timestamp(skb, sockc->tsflags); 364 365 if (flags & MSG_CONFIRM) 366 skb_set_dst_pending_confirm(skb, 1); 367 368 skb->transport_header = skb->network_header; 369 err = -EFAULT; 370 if (memcpy_from_msg(iph, msg, length)) 371 goto error_free; 372 373 iphlen = iph->ihl * 4; 374 375 /* 376 * We don't want to modify the ip header, but we do need to 377 * be sure that it won't cause problems later along the network 378 * stack. Specifically we want to make sure that iph->ihl is a 379 * sane value. If ihl points beyond the length of the buffer passed 380 * in, reject the frame as invalid 381 */ 382 err = -EINVAL; 383 if (iphlen > length) 384 goto error_free; 385 386 if (iphlen >= sizeof(*iph)) { 387 if (!iph->saddr) 388 iph->saddr = fl4->saddr; 389 iph->check = 0; 390 iph->tot_len = htons(length); 391 if (!iph->id) 392 ip_select_ident(net, skb, NULL); 393 394 iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl); 395 skb->transport_header += iphlen; 396 if (iph->protocol == IPPROTO_ICMP && 397 length >= iphlen + sizeof(struct icmphdr)) 398 icmp_out_count(net, ((struct icmphdr *) 399 skb_transport_header(skb))->type); 400 } 401 402 err = NF_HOOK(NFPROTO_IPV4, NF_INET_LOCAL_OUT, 403 net, sk, skb, NULL, rt->dst.dev, 404 dst_output); 405 if (err > 0) 406 err = net_xmit_errno(err); 407 if (err) 408 goto error; 409 out: 410 return 0; 411 412 error_free: 413 kfree_skb(skb); 414 error: 415 IP_INC_STATS(net, IPSTATS_MIB_OUTDISCARDS); 416 if (err == -ENOBUFS && !inet->recverr) 417 err = 0; 418 return err; 419 } 420 421 static int raw_probe_proto_opt(struct raw_frag_vec *rfv, struct flowi4 *fl4) 422 { 423 int err; 424 425 if (fl4->flowi4_proto != IPPROTO_ICMP) 426 return 0; 427 428 /* We only need the first two bytes. */ 429 rfv->hlen = 2; 430 431 err = memcpy_from_msg(rfv->hdr.c, rfv->msg, rfv->hlen); 432 if (err) 433 return err; 434 435 fl4->fl4_icmp_type = rfv->hdr.icmph.type; 436 fl4->fl4_icmp_code = rfv->hdr.icmph.code; 437 438 return 0; 439 } 440 441 static int raw_getfrag(void *from, char *to, int offset, int len, int odd, 442 struct sk_buff *skb) 443 { 444 struct raw_frag_vec *rfv = from; 445 446 if (offset < rfv->hlen) { 447 int copy = min(rfv->hlen - offset, len); 448 449 if (skb->ip_summed == CHECKSUM_PARTIAL) 450 memcpy(to, rfv->hdr.c + offset, copy); 451 else 452 skb->csum = csum_block_add( 453 skb->csum, 454 csum_partial_copy_nocheck(rfv->hdr.c + offset, 455 to, copy), 456 odd); 457 458 odd = 0; 459 offset += copy; 460 to += copy; 461 len -= copy; 462 463 if (!len) 464 return 0; 465 } 466 467 offset -= rfv->hlen; 468 469 return ip_generic_getfrag(rfv->msg, to, offset, len, odd, skb); 470 } 471 472 static int raw_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) 473 { 474 struct inet_sock *inet = inet_sk(sk); 475 struct net *net = sock_net(sk); 476 struct ipcm_cookie ipc; 477 struct rtable *rt = NULL; 478 struct flowi4 fl4; 479 u8 tos, scope; 480 int free = 0; 481 __be32 daddr; 482 __be32 saddr; 483 int err; 484 struct ip_options_data opt_copy; 485 struct raw_frag_vec rfv; 486 int hdrincl; 487 488 err = -EMSGSIZE; 489 if (len > 0xFFFF) 490 goto out; 491 492 /* hdrincl should be READ_ONCE(inet->hdrincl) 493 * but READ_ONCE() doesn't work with bit fields. 494 * Doing this indirectly yields the same result. 495 */ 496 hdrincl = inet->hdrincl; 497 hdrincl = READ_ONCE(hdrincl); 498 /* 499 * Check the flags. 500 */ 501 502 err = -EOPNOTSUPP; 503 if (msg->msg_flags & MSG_OOB) /* Mirror BSD error message */ 504 goto out; /* compatibility */ 505 506 /* 507 * Get and verify the address. 508 */ 509 510 if (msg->msg_namelen) { 511 DECLARE_SOCKADDR(struct sockaddr_in *, usin, msg->msg_name); 512 err = -EINVAL; 513 if (msg->msg_namelen < sizeof(*usin)) 514 goto out; 515 if (usin->sin_family != AF_INET) { 516 pr_info_once("%s: %s forgot to set AF_INET. Fix it!\n", 517 __func__, current->comm); 518 err = -EAFNOSUPPORT; 519 if (usin->sin_family) 520 goto out; 521 } 522 daddr = usin->sin_addr.s_addr; 523 /* ANK: I did not forget to get protocol from port field. 524 * I just do not know, who uses this weirdness. 525 * IP_HDRINCL is much more convenient. 526 */ 527 } else { 528 err = -EDESTADDRREQ; 529 if (sk->sk_state != TCP_ESTABLISHED) 530 goto out; 531 daddr = inet->inet_daddr; 532 } 533 534 ipcm_init_sk(&ipc, inet); 535 /* Keep backward compat */ 536 if (hdrincl) 537 ipc.protocol = IPPROTO_RAW; 538 539 if (msg->msg_controllen) { 540 err = ip_cmsg_send(sk, msg, &ipc, false); 541 if (unlikely(err)) { 542 kfree(ipc.opt); 543 goto out; 544 } 545 if (ipc.opt) 546 free = 1; 547 } 548 549 saddr = ipc.addr; 550 ipc.addr = daddr; 551 552 if (!ipc.opt) { 553 struct ip_options_rcu *inet_opt; 554 555 rcu_read_lock(); 556 inet_opt = rcu_dereference(inet->inet_opt); 557 if (inet_opt) { 558 memcpy(&opt_copy, inet_opt, 559 sizeof(*inet_opt) + inet_opt->opt.optlen); 560 ipc.opt = &opt_copy.opt; 561 } 562 rcu_read_unlock(); 563 } 564 565 if (ipc.opt) { 566 err = -EINVAL; 567 /* Linux does not mangle headers on raw sockets, 568 * so that IP options + IP_HDRINCL is non-sense. 569 */ 570 if (hdrincl) 571 goto done; 572 if (ipc.opt->opt.srr) { 573 if (!daddr) 574 goto done; 575 daddr = ipc.opt->opt.faddr; 576 } 577 } 578 tos = get_rttos(&ipc, inet); 579 scope = ip_sendmsg_scope(inet, &ipc, msg); 580 581 if (ipv4_is_multicast(daddr)) { 582 if (!ipc.oif || netif_index_is_l3_master(sock_net(sk), ipc.oif)) 583 ipc.oif = inet->mc_index; 584 if (!saddr) 585 saddr = inet->mc_addr; 586 } else if (!ipc.oif) { 587 ipc.oif = inet->uc_index; 588 } else if (ipv4_is_lbcast(daddr) && inet->uc_index) { 589 /* oif is set, packet is to local broadcast 590 * and uc_index is set. oif is most likely set 591 * by sk_bound_dev_if. If uc_index != oif check if the 592 * oif is an L3 master and uc_index is an L3 slave. 593 * If so, we want to allow the send using the uc_index. 594 */ 595 if (ipc.oif != inet->uc_index && 596 ipc.oif == l3mdev_master_ifindex_by_index(sock_net(sk), 597 inet->uc_index)) { 598 ipc.oif = inet->uc_index; 599 } 600 } 601 602 flowi4_init_output(&fl4, ipc.oif, ipc.sockc.mark, tos, scope, 603 hdrincl ? ipc.protocol : sk->sk_protocol, 604 inet_sk_flowi_flags(sk) | 605 (hdrincl ? FLOWI_FLAG_KNOWN_NH : 0), 606 daddr, saddr, 0, 0, sk->sk_uid); 607 608 if (!hdrincl) { 609 rfv.msg = msg; 610 rfv.hlen = 0; 611 612 err = raw_probe_proto_opt(&rfv, &fl4); 613 if (err) 614 goto done; 615 } 616 617 security_sk_classify_flow(sk, flowi4_to_flowi_common(&fl4)); 618 rt = ip_route_output_flow(net, &fl4, sk); 619 if (IS_ERR(rt)) { 620 err = PTR_ERR(rt); 621 rt = NULL; 622 goto done; 623 } 624 625 err = -EACCES; 626 if (rt->rt_flags & RTCF_BROADCAST && !sock_flag(sk, SOCK_BROADCAST)) 627 goto done; 628 629 if (msg->msg_flags & MSG_CONFIRM) 630 goto do_confirm; 631 back_from_confirm: 632 633 if (hdrincl) 634 err = raw_send_hdrinc(sk, &fl4, msg, len, 635 &rt, msg->msg_flags, &ipc.sockc); 636 637 else { 638 if (!ipc.addr) 639 ipc.addr = fl4.daddr; 640 lock_sock(sk); 641 err = ip_append_data(sk, &fl4, raw_getfrag, 642 &rfv, len, 0, 643 &ipc, &rt, msg->msg_flags); 644 if (err) 645 ip_flush_pending_frames(sk); 646 else if (!(msg->msg_flags & MSG_MORE)) { 647 err = ip_push_pending_frames(sk, &fl4); 648 if (err == -ENOBUFS && !inet->recverr) 649 err = 0; 650 } 651 release_sock(sk); 652 } 653 done: 654 if (free) 655 kfree(ipc.opt); 656 ip_rt_put(rt); 657 658 out: 659 if (err < 0) 660 return err; 661 return len; 662 663 do_confirm: 664 if (msg->msg_flags & MSG_PROBE) 665 dst_confirm_neigh(&rt->dst, &fl4.daddr); 666 if (!(msg->msg_flags & MSG_PROBE) || len) 667 goto back_from_confirm; 668 err = 0; 669 goto done; 670 } 671 672 static void raw_close(struct sock *sk, long timeout) 673 { 674 /* 675 * Raw sockets may have direct kernel references. Kill them. 676 */ 677 ip_ra_control(sk, 0, NULL); 678 679 sk_common_release(sk); 680 } 681 682 static void raw_destroy(struct sock *sk) 683 { 684 lock_sock(sk); 685 ip_flush_pending_frames(sk); 686 release_sock(sk); 687 } 688 689 /* This gets rid of all the nasties in af_inet. -DaveM */ 690 static int raw_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len) 691 { 692 struct inet_sock *inet = inet_sk(sk); 693 struct sockaddr_in *addr = (struct sockaddr_in *) uaddr; 694 struct net *net = sock_net(sk); 695 u32 tb_id = RT_TABLE_LOCAL; 696 int ret = -EINVAL; 697 int chk_addr_ret; 698 699 lock_sock(sk); 700 if (sk->sk_state != TCP_CLOSE || addr_len < sizeof(struct sockaddr_in)) 701 goto out; 702 703 if (sk->sk_bound_dev_if) 704 tb_id = l3mdev_fib_table_by_index(net, 705 sk->sk_bound_dev_if) ? : tb_id; 706 707 chk_addr_ret = inet_addr_type_table(net, addr->sin_addr.s_addr, tb_id); 708 709 ret = -EADDRNOTAVAIL; 710 if (!inet_addr_valid_or_nonlocal(net, inet, addr->sin_addr.s_addr, 711 chk_addr_ret)) 712 goto out; 713 714 inet->inet_rcv_saddr = inet->inet_saddr = addr->sin_addr.s_addr; 715 if (chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST) 716 inet->inet_saddr = 0; /* Use device */ 717 sk_dst_reset(sk); 718 ret = 0; 719 out: 720 release_sock(sk); 721 return ret; 722 } 723 724 /* 725 * This should be easy, if there is something there 726 * we return it, otherwise we block. 727 */ 728 729 static int raw_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, 730 int flags, int *addr_len) 731 { 732 struct inet_sock *inet = inet_sk(sk); 733 size_t copied = 0; 734 int err = -EOPNOTSUPP; 735 DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name); 736 struct sk_buff *skb; 737 738 if (flags & MSG_OOB) 739 goto out; 740 741 if (flags & MSG_ERRQUEUE) { 742 err = ip_recv_error(sk, msg, len, addr_len); 743 goto out; 744 } 745 746 skb = skb_recv_datagram(sk, flags, &err); 747 if (!skb) 748 goto out; 749 750 copied = skb->len; 751 if (len < copied) { 752 msg->msg_flags |= MSG_TRUNC; 753 copied = len; 754 } 755 756 err = skb_copy_datagram_msg(skb, 0, msg, copied); 757 if (err) 758 goto done; 759 760 sock_recv_cmsgs(msg, sk, skb); 761 762 /* Copy the address. */ 763 if (sin) { 764 sin->sin_family = AF_INET; 765 sin->sin_addr.s_addr = ip_hdr(skb)->saddr; 766 sin->sin_port = 0; 767 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero)); 768 *addr_len = sizeof(*sin); 769 } 770 if (inet->cmsg_flags) 771 ip_cmsg_recv(msg, skb); 772 if (flags & MSG_TRUNC) 773 copied = skb->len; 774 done: 775 skb_free_datagram(sk, skb); 776 out: 777 if (err) 778 return err; 779 return copied; 780 } 781 782 static int raw_sk_init(struct sock *sk) 783 { 784 struct raw_sock *rp = raw_sk(sk); 785 786 if (inet_sk(sk)->inet_num == IPPROTO_ICMP) 787 memset(&rp->filter, 0, sizeof(rp->filter)); 788 return 0; 789 } 790 791 static int raw_seticmpfilter(struct sock *sk, sockptr_t optval, int optlen) 792 { 793 if (optlen > sizeof(struct icmp_filter)) 794 optlen = sizeof(struct icmp_filter); 795 if (copy_from_sockptr(&raw_sk(sk)->filter, optval, optlen)) 796 return -EFAULT; 797 return 0; 798 } 799 800 static int raw_geticmpfilter(struct sock *sk, char __user *optval, int __user *optlen) 801 { 802 int len, ret = -EFAULT; 803 804 if (get_user(len, optlen)) 805 goto out; 806 ret = -EINVAL; 807 if (len < 0) 808 goto out; 809 if (len > sizeof(struct icmp_filter)) 810 len = sizeof(struct icmp_filter); 811 ret = -EFAULT; 812 if (put_user(len, optlen) || 813 copy_to_user(optval, &raw_sk(sk)->filter, len)) 814 goto out; 815 ret = 0; 816 out: return ret; 817 } 818 819 static int do_raw_setsockopt(struct sock *sk, int level, int optname, 820 sockptr_t optval, unsigned int optlen) 821 { 822 if (optname == ICMP_FILTER) { 823 if (inet_sk(sk)->inet_num != IPPROTO_ICMP) 824 return -EOPNOTSUPP; 825 else 826 return raw_seticmpfilter(sk, optval, optlen); 827 } 828 return -ENOPROTOOPT; 829 } 830 831 static int raw_setsockopt(struct sock *sk, int level, int optname, 832 sockptr_t optval, unsigned int optlen) 833 { 834 if (level != SOL_RAW) 835 return ip_setsockopt(sk, level, optname, optval, optlen); 836 return do_raw_setsockopt(sk, level, optname, optval, optlen); 837 } 838 839 static int do_raw_getsockopt(struct sock *sk, int level, int optname, 840 char __user *optval, int __user *optlen) 841 { 842 if (optname == ICMP_FILTER) { 843 if (inet_sk(sk)->inet_num != IPPROTO_ICMP) 844 return -EOPNOTSUPP; 845 else 846 return raw_geticmpfilter(sk, optval, optlen); 847 } 848 return -ENOPROTOOPT; 849 } 850 851 static int raw_getsockopt(struct sock *sk, int level, int optname, 852 char __user *optval, int __user *optlen) 853 { 854 if (level != SOL_RAW) 855 return ip_getsockopt(sk, level, optname, optval, optlen); 856 return do_raw_getsockopt(sk, level, optname, optval, optlen); 857 } 858 859 static int raw_ioctl(struct sock *sk, int cmd, int *karg) 860 { 861 switch (cmd) { 862 case SIOCOUTQ: { 863 *karg = sk_wmem_alloc_get(sk); 864 return 0; 865 } 866 case SIOCINQ: { 867 struct sk_buff *skb; 868 869 spin_lock_bh(&sk->sk_receive_queue.lock); 870 skb = skb_peek(&sk->sk_receive_queue); 871 if (skb) 872 *karg = skb->len; 873 else 874 *karg = 0; 875 spin_unlock_bh(&sk->sk_receive_queue.lock); 876 return 0; 877 } 878 879 default: 880 #ifdef CONFIG_IP_MROUTE 881 return ipmr_ioctl(sk, cmd, karg); 882 #else 883 return -ENOIOCTLCMD; 884 #endif 885 } 886 } 887 888 #ifdef CONFIG_COMPAT 889 static int compat_raw_ioctl(struct sock *sk, unsigned int cmd, unsigned long arg) 890 { 891 switch (cmd) { 892 case SIOCOUTQ: 893 case SIOCINQ: 894 return -ENOIOCTLCMD; 895 default: 896 #ifdef CONFIG_IP_MROUTE 897 return ipmr_compat_ioctl(sk, cmd, compat_ptr(arg)); 898 #else 899 return -ENOIOCTLCMD; 900 #endif 901 } 902 } 903 #endif 904 905 int raw_abort(struct sock *sk, int err) 906 { 907 lock_sock(sk); 908 909 sk->sk_err = err; 910 sk_error_report(sk); 911 __udp_disconnect(sk, 0); 912 913 release_sock(sk); 914 915 return 0; 916 } 917 EXPORT_SYMBOL_GPL(raw_abort); 918 919 struct proto raw_prot = { 920 .name = "RAW", 921 .owner = THIS_MODULE, 922 .close = raw_close, 923 .destroy = raw_destroy, 924 .connect = ip4_datagram_connect, 925 .disconnect = __udp_disconnect, 926 .ioctl = raw_ioctl, 927 .init = raw_sk_init, 928 .setsockopt = raw_setsockopt, 929 .getsockopt = raw_getsockopt, 930 .sendmsg = raw_sendmsg, 931 .recvmsg = raw_recvmsg, 932 .bind = raw_bind, 933 .backlog_rcv = raw_rcv_skb, 934 .release_cb = ip4_datagram_release_cb, 935 .hash = raw_hash_sk, 936 .unhash = raw_unhash_sk, 937 .obj_size = sizeof(struct raw_sock), 938 .useroffset = offsetof(struct raw_sock, filter), 939 .usersize = sizeof_field(struct raw_sock, filter), 940 .h.raw_hash = &raw_v4_hashinfo, 941 #ifdef CONFIG_COMPAT 942 .compat_ioctl = compat_raw_ioctl, 943 #endif 944 .diag_destroy = raw_abort, 945 }; 946 947 #ifdef CONFIG_PROC_FS 948 static struct sock *raw_get_first(struct seq_file *seq, int bucket) 949 { 950 struct raw_hashinfo *h = pde_data(file_inode(seq->file)); 951 struct raw_iter_state *state = raw_seq_private(seq); 952 struct hlist_head *hlist; 953 struct sock *sk; 954 955 for (state->bucket = bucket; state->bucket < RAW_HTABLE_SIZE; 956 ++state->bucket) { 957 hlist = &h->ht[state->bucket]; 958 sk_for_each(sk, hlist) { 959 if (sock_net(sk) == seq_file_net(seq)) 960 return sk; 961 } 962 } 963 return NULL; 964 } 965 966 static struct sock *raw_get_next(struct seq_file *seq, struct sock *sk) 967 { 968 struct raw_iter_state *state = raw_seq_private(seq); 969 970 do { 971 sk = sk_next(sk); 972 } while (sk && sock_net(sk) != seq_file_net(seq)); 973 974 if (!sk) 975 return raw_get_first(seq, state->bucket + 1); 976 return sk; 977 } 978 979 static struct sock *raw_get_idx(struct seq_file *seq, loff_t pos) 980 { 981 struct sock *sk = raw_get_first(seq, 0); 982 983 if (sk) 984 while (pos && (sk = raw_get_next(seq, sk)) != NULL) 985 --pos; 986 return pos ? NULL : sk; 987 } 988 989 void *raw_seq_start(struct seq_file *seq, loff_t *pos) 990 __acquires(&h->lock) 991 { 992 struct raw_hashinfo *h = pde_data(file_inode(seq->file)); 993 994 spin_lock(&h->lock); 995 996 return *pos ? raw_get_idx(seq, *pos - 1) : SEQ_START_TOKEN; 997 } 998 EXPORT_SYMBOL_GPL(raw_seq_start); 999 1000 void *raw_seq_next(struct seq_file *seq, void *v, loff_t *pos) 1001 { 1002 struct sock *sk; 1003 1004 if (v == SEQ_START_TOKEN) 1005 sk = raw_get_first(seq, 0); 1006 else 1007 sk = raw_get_next(seq, v); 1008 ++*pos; 1009 return sk; 1010 } 1011 EXPORT_SYMBOL_GPL(raw_seq_next); 1012 1013 void raw_seq_stop(struct seq_file *seq, void *v) 1014 __releases(&h->lock) 1015 { 1016 struct raw_hashinfo *h = pde_data(file_inode(seq->file)); 1017 1018 spin_unlock(&h->lock); 1019 } 1020 EXPORT_SYMBOL_GPL(raw_seq_stop); 1021 1022 static void raw_sock_seq_show(struct seq_file *seq, struct sock *sp, int i) 1023 { 1024 struct inet_sock *inet = inet_sk(sp); 1025 __be32 dest = inet->inet_daddr, 1026 src = inet->inet_rcv_saddr; 1027 __u16 destp = 0, 1028 srcp = inet->inet_num; 1029 1030 seq_printf(seq, "%4d: %08X:%04X %08X:%04X" 1031 " %02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %u\n", 1032 i, src, srcp, dest, destp, sp->sk_state, 1033 sk_wmem_alloc_get(sp), 1034 sk_rmem_alloc_get(sp), 1035 0, 0L, 0, 1036 from_kuid_munged(seq_user_ns(seq), sock_i_uid(sp)), 1037 0, sock_i_ino(sp), 1038 refcount_read(&sp->sk_refcnt), sp, atomic_read(&sp->sk_drops)); 1039 } 1040 1041 static int raw_seq_show(struct seq_file *seq, void *v) 1042 { 1043 if (v == SEQ_START_TOKEN) 1044 seq_printf(seq, " sl local_address rem_address st tx_queue " 1045 "rx_queue tr tm->when retrnsmt uid timeout " 1046 "inode ref pointer drops\n"); 1047 else 1048 raw_sock_seq_show(seq, v, raw_seq_private(seq)->bucket); 1049 return 0; 1050 } 1051 1052 static const struct seq_operations raw_seq_ops = { 1053 .start = raw_seq_start, 1054 .next = raw_seq_next, 1055 .stop = raw_seq_stop, 1056 .show = raw_seq_show, 1057 }; 1058 1059 static __net_init int raw_init_net(struct net *net) 1060 { 1061 if (!proc_create_net_data("raw", 0444, net->proc_net, &raw_seq_ops, 1062 sizeof(struct raw_iter_state), &raw_v4_hashinfo)) 1063 return -ENOMEM; 1064 1065 return 0; 1066 } 1067 1068 static __net_exit void raw_exit_net(struct net *net) 1069 { 1070 remove_proc_entry("raw", net->proc_net); 1071 } 1072 1073 static __net_initdata struct pernet_operations raw_net_ops = { 1074 .init = raw_init_net, 1075 .exit = raw_exit_net, 1076 }; 1077 1078 int __init raw_proc_init(void) 1079 { 1080 1081 return register_pernet_subsys(&raw_net_ops); 1082 } 1083 1084 void __init raw_proc_exit(void) 1085 { 1086 unregister_pernet_subsys(&raw_net_ops); 1087 } 1088 #endif /* CONFIG_PROC_FS */ 1089 1090 static void raw_sysctl_init_net(struct net *net) 1091 { 1092 #ifdef CONFIG_NET_L3_MASTER_DEV 1093 net->ipv4.sysctl_raw_l3mdev_accept = 1; 1094 #endif 1095 } 1096 1097 static int __net_init raw_sysctl_init(struct net *net) 1098 { 1099 raw_sysctl_init_net(net); 1100 return 0; 1101 } 1102 1103 static struct pernet_operations __net_initdata raw_sysctl_ops = { 1104 .init = raw_sysctl_init, 1105 }; 1106 1107 void __init raw_init(void) 1108 { 1109 raw_sysctl_init_net(&init_net); 1110 if (register_pernet_subsys(&raw_sysctl_ops)) 1111 panic("RAW: failed to init sysctl parameters.\n"); 1112 } 1113