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