linux-old/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 * Version:     $Id: raw.c,v 1.39 1998/11/08 11:17:04 davem Exp $
   9 *
  10 * Authors:     Ross Biro, <bir7@leland.Stanford.Edu>
  11 *              Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  12 *
  13 * Fixes:
  14 *              Alan Cox        :       verify_area() fixed up
  15 *              Alan Cox        :       ICMP error handling
  16 *              Alan Cox        :       EMSGSIZE if you send too big a packet
  17 *              Alan Cox        :       Now uses generic datagrams and shared skbuff
  18 *                                      library. No more peek crashes, 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 *              This program is free software; you can redistribute it and/or
  36 *              modify it under the terms of the GNU General Public License
  37 *              as published by the Free Software Foundation; either version
  38 *              2 of the License, or (at your option) any later version.
  39 */
  40 
  41#include <linux/config.h> 
  42#include <asm/system.h>
  43#include <asm/uaccess.h>
  44#include <linux/types.h>
  45#include <linux/sched.h>
  46#include <linux/errno.h>
  47#include <linux/timer.h>
  48#include <linux/mm.h>
  49#include <linux/kernel.h>
  50#include <linux/fcntl.h>
  51#include <linux/socket.h>
  52#include <linux/in.h>
  53#include <linux/inet.h>
  54#include <linux/netdevice.h>
  55#include <linux/mroute.h>
  56#include <net/ip.h>
  57#include <net/protocol.h>
  58#include <linux/skbuff.h>
  59#include <net/sock.h>
  60#include <net/icmp.h>
  61#include <net/udp.h>
  62#include <net/raw.h>
  63#include <net/checksum.h>
  64
  65#ifdef CONFIG_IP_MROUTE
  66struct sock *mroute_socket=NULL;
  67#endif
  68
  69struct sock *raw_v4_htable[RAWV4_HTABLE_SIZE];
  70
  71static void raw_v4_hash(struct sock *sk)
  72{
  73        struct sock **skp;
  74        int num = sk->num;
  75
  76        num &= (RAWV4_HTABLE_SIZE - 1);
  77        skp = &raw_v4_htable[num];
  78        SOCKHASH_LOCK();
  79        sk->next = *skp;
  80        *skp = sk;
  81        sk->hashent = num;
  82        SOCKHASH_UNLOCK();
  83}
  84
  85static void raw_v4_unhash(struct sock *sk)
  86{
  87        struct sock **skp;
  88        int num = sk->num;
  89
  90        num &= (RAWV4_HTABLE_SIZE - 1);
  91        skp = &raw_v4_htable[num];
  92
  93        SOCKHASH_LOCK();
  94        while(*skp != NULL) {
  95                if(*skp == sk) {
  96                        *skp = sk->next;
  97                        break;
  98                }
  99                skp = &((*skp)->next);
 100        }
 101        SOCKHASH_UNLOCK();
 102}
 103
 104static void raw_v4_rehash(struct sock *sk)
 105{
 106        struct sock **skp;
 107        int num = sk->num;
 108        int oldnum = sk->hashent;
 109
 110        num &= (RAWV4_HTABLE_SIZE - 1);
 111        skp = &raw_v4_htable[oldnum];
 112
 113        SOCKHASH_LOCK();
 114        while(*skp != NULL) {
 115                if(*skp == sk) {
 116                        *skp = sk->next;
 117                        break;
 118                }
 119                skp = &((*skp)->next);
 120        }
 121        sk->next = raw_v4_htable[num];
 122        raw_v4_htable[num] = sk;
 123        sk->hashent = num;
 124        SOCKHASH_UNLOCK();
 125}
 126
 127/* Grumble... icmp and ip_input want to get at this... */
 128struct sock *raw_v4_lookup(struct sock *sk, unsigned short num,
 129                           unsigned long raddr, unsigned long laddr, int dif)
 130{
 131        struct sock *s = sk;
 132
 133        SOCKHASH_LOCK();
 134        for(s = sk; s; s = s->next) {
 135                if((s->num == num)                              &&
 136                   !(s->dead && (s->state == TCP_CLOSE))        &&
 137                   !(s->daddr && s->daddr != raddr)             &&
 138                   !(s->rcv_saddr && s->rcv_saddr != laddr)     &&
 139                   !(s->bound_dev_if && s->bound_dev_if != dif))
 140                        break; /* gotcha */
 141        }
 142        SOCKHASH_UNLOCK();
 143        return s;
 144}
 145
 146void raw_err (struct sock *sk, struct sk_buff *skb)
 147{
 148        int type = skb->h.icmph->type;
 149        int code = skb->h.icmph->code;
 150        u32 info = 0;
 151        int err = 0;
 152        int harderr = 0;
 153
 154        /* Report error on raw socket, if:
 155           1. User requested ip_recverr.
 156           2. Socket is connected (otherwise the error indication
 157              is useless without ip_recverr and error is hard.
 158         */
 159        if (!sk->ip_recverr && sk->state != TCP_ESTABLISHED)
 160                return;
 161
 162        switch (type) {
 163        default:
 164        case ICMP_TIME_EXCEEDED:
 165                err = EHOSTUNREACH;
 166                break;
 167        case ICMP_SOURCE_QUENCH:
 168                return;
 169        case ICMP_PARAMETERPROB:
 170                err = EPROTO;
 171                info = ntohl(skb->h.icmph->un.gateway)>>24;
 172                harderr = 1;
 173                break;
 174        case ICMP_DEST_UNREACH:
 175                err = EHOSTUNREACH;
 176                if (code > NR_ICMP_UNREACH)
 177                        break;
 178                err = icmp_err_convert[code].errno;
 179                harderr = icmp_err_convert[code].fatal;
 180                if (code == ICMP_FRAG_NEEDED) {
 181                        harderr = (sk->ip_pmtudisc != IP_PMTUDISC_DONT);
 182                        err = EMSGSIZE;
 183                        info = ntohs(skb->h.icmph->un.frag.mtu);
 184                }
 185        }
 186
 187        if (sk->ip_recverr)
 188                ip_icmp_error(sk, skb, err, 0, info, (u8 *)(skb->h.icmph + 1));
 189                
 190        if (sk->ip_recverr || harderr) {
 191                sk->err = err;
 192                sk->error_report(sk);
 193        }
 194}
 195
 196static int raw_rcv_skb(struct sock * sk, struct sk_buff * skb)
 197{
 198        /* Charge it to the socket. */
 199        
 200        if (sock_queue_rcv_skb(sk,skb)<0)
 201        {
 202                ip_statistics.IpInDiscards++;
 203                kfree_skb(skb);
 204                return -1;
 205        }
 206
 207        ip_statistics.IpInDelivers++;
 208        return 0;
 209}
 210
 211/*
 212 *      This should be the easiest of all, all we do is
 213 *      copy it into a buffer. All demultiplexing is done
 214 *      in ip.c
 215 */
 216
 217int raw_rcv(struct sock *sk, struct sk_buff *skb)
 218{
 219        /* Now we need to copy this into memory. */
 220        skb_trim(skb, ntohs(skb->nh.iph->tot_len));
 221        
 222        skb->h.raw = skb->nh.raw;
 223
 224        raw_rcv_skb(sk, skb);
 225        return 0;
 226}
 227
 228struct rawfakehdr 
 229{
 230        struct  iovec *iov;
 231        u32     saddr;
 232};
 233
 234/*
 235 *      Send a RAW IP packet.
 236 */
 237
 238/*
 239 *      Callback support is trivial for SOCK_RAW
 240 */
 241  
 242static int raw_getfrag(const void *p, char *to, unsigned int offset, unsigned int fraglen)
 243{
 244        struct rawfakehdr *rfh = (struct rawfakehdr *) p;
 245        return memcpy_fromiovecend(to, rfh->iov, offset, fraglen);
 246}
 247
 248/*
 249 *      IPPROTO_RAW needs extra work.
 250 */
 251 
 252static int raw_getrawfrag(const void *p, char *to, unsigned int offset, unsigned int fraglen)
 253{
 254        struct rawfakehdr *rfh = (struct rawfakehdr *) p;
 255
 256        if (memcpy_fromiovecend(to, rfh->iov, offset, fraglen))
 257                return -EFAULT;
 258
 259        if (offset==0) {
 260                struct iphdr *iph = (struct iphdr *)to;
 261                if (!iph->saddr)
 262                        iph->saddr = rfh->saddr;
 263                iph->check=0;
 264                iph->tot_len=htons(fraglen);    /* This is right as you can't frag
 265                                                   RAW packets */
 266                /*
 267                 *      Deliberate breach of modularity to keep 
 268                 *      ip_build_xmit clean (well less messy).
 269                 */
 270                if (!iph->id)
 271                        iph->id = htons(ip_id_count++);
 272                iph->check=ip_fast_csum((unsigned char *)iph, iph->ihl);
 273        }
 274        return 0;
 275}
 276
 277static int raw_sendmsg(struct sock *sk, struct msghdr *msg, int len)
 278{
 279        struct ipcm_cookie ipc;
 280        struct rawfakehdr rfh;
 281        struct rtable *rt = NULL;
 282        int free = 0;
 283        u32 daddr;
 284        u8  tos;
 285        int err;
 286
 287        /* This check is ONLY to check for arithmetic overflow
 288           on integer(!) len. Not more! Real check will be made
 289           in ip_build_xmit --ANK
 290
 291           BTW socket.c -> af_*.c -> ... make multiple
 292           invalid conversions size_t -> int. We MUST repair it f.e.
 293           by replacing all of them with size_t and revise all
 294           the places sort of len += sizeof(struct iphdr)
 295           If len was ULONG_MAX-10 it would be cathastrophe  --ANK
 296         */
 297
 298        if (len < 0 || len > 0xFFFF)
 299                return -EMSGSIZE;
 300
 301        /*
 302         *      Check the flags.
 303         */
 304
 305        if (msg->msg_flags & MSG_OOB)           /* Mirror BSD error message compatibility */
 306                return -EOPNOTSUPP;
 307                         
 308        if (msg->msg_flags & ~(MSG_DONTROUTE|MSG_DONTWAIT))
 309                return(-EINVAL);
 310
 311        /*
 312         *      Get and verify the address. 
 313         */
 314
 315        if (msg->msg_namelen) {
 316                struct sockaddr_in *usin = (struct sockaddr_in*)msg->msg_name;
 317                if (msg->msg_namelen < sizeof(*usin))
 318                        return(-EINVAL);
 319                if (usin->sin_family != AF_INET) {
 320                        static int complained;
 321                        if (!complained++)
 322                                printk(KERN_INFO "%s forgot to set AF_INET in raw sendmsg. Fix it!\n", current->comm);
 323                        if (usin->sin_family)
 324                                return -EINVAL;
 325                }
 326                daddr = usin->sin_addr.s_addr;
 327                /* ANK: I did not forget to get protocol from port field.
 328                 * I just do not know, who uses this weirdness.
 329                 * IP_HDRINCL is much more convenient.
 330                 */
 331        } else {
 332                if (sk->state != TCP_ESTABLISHED) 
 333                        return(-EINVAL);
 334                daddr = sk->daddr;
 335        }
 336
 337        ipc.addr = sk->saddr;
 338        ipc.opt = NULL;
 339        ipc.oif = sk->bound_dev_if;
 340
 341        if (msg->msg_controllen) {
 342                int tmp = ip_cmsg_send(msg, &ipc);
 343                if (tmp)
 344                        return tmp;
 345                if (ipc.opt)
 346                        free=1;
 347        }
 348
 349        rfh.saddr = ipc.addr;
 350        ipc.addr = daddr;
 351
 352        if (!ipc.opt)
 353                ipc.opt = sk->opt;
 354
 355        if (ipc.opt) {
 356                err = -EINVAL;
 357                /* Linux does not mangle headers on raw sockets,
 358                 * so that IP options + IP_HDRINCL is non-sense.
 359                 */
 360                if (sk->ip_hdrincl)
 361                        goto done;
 362                if (ipc.opt->srr) {
 363                        if (!daddr)
 364                                goto done;
 365                        daddr = ipc.opt->faddr;
 366                }
 367        }
 368        tos = RT_TOS(sk->ip_tos) | sk->localroute;
 369        if (msg->msg_flags&MSG_DONTROUTE)
 370                tos |= RTO_ONLINK;
 371
 372        if (MULTICAST(daddr)) {
 373                if (!ipc.oif)
 374                        ipc.oif = sk->ip_mc_index;
 375                if (!rfh.saddr)
 376                        rfh.saddr = sk->ip_mc_addr;
 377        }
 378
 379        err = ip_route_output(&rt, daddr, rfh.saddr, tos, ipc.oif);
 380
 381        if (err)
 382                goto done;
 383
 384        err = -EACCES;
 385        if (rt->rt_flags&RTCF_BROADCAST && !sk->broadcast)
 386                goto done;
 387
 388        rfh.iov = msg->msg_iov;
 389        rfh.saddr = rt->rt_src;
 390        if (!ipc.addr)
 391                ipc.addr = rt->rt_dst;
 392        err=ip_build_xmit(sk, sk->ip_hdrincl ? raw_getrawfrag : raw_getfrag,
 393                          &rfh, len, &ipc, rt, msg->msg_flags);
 394
 395done:
 396        if (free)
 397                kfree(ipc.opt);
 398        ip_rt_put(rt);
 399
 400        return err<0 ? err : len;
 401}
 402
 403static void raw_close(struct sock *sk, long timeout)
 404{
 405        /* Observation: when raw_close is called, processes have
 406           no access to socket anymore. But net still has.
 407           Step one, detach it from networking:
 408
 409           A. Remove from hash tables.
 410         */
 411        sk->state = TCP_CLOSE;
 412        raw_v4_unhash(sk);
 413        /*
 414           B. Raw sockets may have direct kernel refereneces. Kill them.
 415         */
 416        ip_ra_control(sk, 0, NULL);
 417
 418        /* In this point socket cannot receive new packets anymore */
 419
 420
 421        /* But we still have packets pending on receive
 422           queue and probably, our own packets waiting in device queues.
 423           sock_destroy will drain receive queue, but transmitted
 424           packets will delay socket destruction.
 425           Set sk->dead=1 in order to prevent wakeups, when these
 426           packet will be freed.
 427         */
 428        sk->dead=1;
 429        destroy_sock(sk);
 430
 431        /* That's all. No races here. */
 432}
 433
 434/* This gets rid of all the nasties in af_inet. -DaveM */
 435static int raw_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
 436{
 437        struct sockaddr_in *addr = (struct sockaddr_in *) uaddr;
 438        int chk_addr_ret;
 439
 440        if((sk->state != TCP_CLOSE) || (addr_len < sizeof(struct sockaddr_in)))
 441                return -EINVAL;
 442        chk_addr_ret = inet_addr_type(addr->sin_addr.s_addr);
 443        if(addr->sin_addr.s_addr != 0 && chk_addr_ret != RTN_LOCAL &&
 444           chk_addr_ret != RTN_MULTICAST && chk_addr_ret != RTN_BROADCAST) {
 445#ifdef CONFIG_IP_TRANSPARENT_PROXY
 446                /* Superuser may bind to any address to allow transparent proxying. */
 447                if(chk_addr_ret != RTN_UNICAST || !capable(CAP_NET_ADMIN))
 448#endif
 449                        return -EADDRNOTAVAIL;
 450        }
 451        sk->rcv_saddr = sk->saddr = addr->sin_addr.s_addr;
 452        if(chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST)
 453                sk->saddr = 0;  /* Use device */
 454        dst_release(xchg(&sk->dst_cache, NULL));
 455        return 0;
 456}
 457
 458/*
 459 *      This should be easy, if there is something there
 460 *      we return it, otherwise we block.
 461 */
 462
 463int raw_recvmsg(struct sock *sk, struct msghdr *msg, int len,
 464                int noblock, int flags,int *addr_len)
 465{
 466        int copied=0;
 467        struct sk_buff *skb;
 468        int err;
 469        struct sockaddr_in *sin=(struct sockaddr_in *)msg->msg_name;
 470
 471        if (flags & MSG_OOB)
 472                return -EOPNOTSUPP;
 473
 474        if (addr_len)
 475                *addr_len=sizeof(*sin);
 476
 477        if (flags & MSG_ERRQUEUE)
 478                return ip_recv_error(sk, msg, len);
 479
 480        skb=skb_recv_datagram(sk,flags,noblock,&err);
 481        if(skb==NULL)
 482                return err;
 483
 484        copied = skb->len;
 485        if (len < copied)
 486        {
 487                msg->msg_flags |= MSG_TRUNC;
 488                copied = len;
 489        }
 490        
 491        err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
 492        if (err)
 493                goto done;
 494
 495        sk->stamp=skb->stamp;
 496
 497        /* Copy the address. */
 498        if (sin) {
 499                sin->sin_family = AF_INET;
 500                sin->sin_addr.s_addr = skb->nh.iph->saddr;
 501        }
 502        if (sk->ip_cmsg_flags)
 503                ip_cmsg_recv(msg, skb);
 504done:
 505        skb_free_datagram(sk, skb);
 506        return (err ? : copied);
 507}
 508
 509static int raw_init(struct sock *sk)
 510{
 511        struct raw_opt *tp = &(sk->tp_pinfo.tp_raw4);
 512        if (sk->num == IPPROTO_ICMP)
 513                memset(&tp->filter, 0, sizeof(tp->filter));
 514        return 0;
 515}
 516
 517static int raw_seticmpfilter(struct sock *sk, char *optval, int optlen)
 518{
 519        if (optlen > sizeof(struct icmp_filter))
 520                optlen = sizeof(struct icmp_filter);
 521        if (copy_from_user(&sk->tp_pinfo.tp_raw4.filter, optval, optlen))
 522                return -EFAULT;
 523        return 0;
 524}
 525
 526static int raw_geticmpfilter(struct sock *sk, char *optval, int *optlen)
 527{
 528        int len;
 529
 530        if (get_user(len,optlen))
 531                return -EFAULT;
 532        if (len > sizeof(struct icmp_filter))
 533                len = sizeof(struct icmp_filter);
 534        if (put_user(len, optlen))
 535                return -EFAULT;
 536        if (copy_to_user(optval, &sk->tp_pinfo.tp_raw4.filter, len))
 537                return -EFAULT;
 538        return 0;
 539}
 540
 541static int raw_setsockopt(struct sock *sk, int level, int optname, 
 542                          char *optval, int optlen)
 543{
 544        if (level != SOL_RAW)
 545                return ip_setsockopt(sk, level, optname, optval, optlen);
 546
 547        switch (optname) {
 548        case ICMP_FILTER:
 549                if (sk->num != IPPROTO_ICMP)
 550                        return -EOPNOTSUPP;
 551                return raw_seticmpfilter(sk, optval, optlen);
 552        };
 553
 554        return -ENOPROTOOPT;
 555}
 556
 557static int raw_getsockopt(struct sock *sk, int level, int optname, 
 558                          char *optval, int *optlen)
 559{
 560        if (level != SOL_RAW)
 561                return ip_getsockopt(sk, level, optname, optval, optlen);
 562
 563        switch (optname) {
 564        case ICMP_FILTER:
 565                if (sk->num != IPPROTO_ICMP)
 566                        return -EOPNOTSUPP;
 567                return raw_geticmpfilter(sk, optval, optlen);
 568        };
 569
 570        return -ENOPROTOOPT;
 571}
 572
 573struct proto raw_prot = {
 574        (struct sock *)&raw_prot,       /* sklist_next */
 575        (struct sock *)&raw_prot,       /* sklist_prev */
 576        raw_close,                      /* close */
 577        udp_connect,                    /* connect */
 578        NULL,                           /* accept */
 579        NULL,                           /* retransmit */
 580        NULL,                           /* write_wakeup */
 581        NULL,                           /* read_wakeup */
 582        datagram_poll,                  /* poll */
 583#ifdef CONFIG_IP_MROUTE
 584        ipmr_ioctl,                     /* ioctl */
 585#else
 586        NULL,                           /* ioctl */
 587#endif
 588        raw_init,                       /* init */
 589        NULL,                           /* destroy */
 590        NULL,                           /* shutdown */
 591        raw_setsockopt,                 /* setsockopt */
 592        raw_getsockopt,                 /* getsockopt */
 593        raw_sendmsg,                    /* sendmsg */
 594        raw_recvmsg,                    /* recvmsg */
 595        raw_bind,                       /* bind */
 596        raw_rcv_skb,                    /* backlog_rcv */
 597        raw_v4_hash,                    /* hash */
 598        raw_v4_unhash,                  /* unhash */
 599        raw_v4_rehash,                  /* rehash */
 600        NULL,                           /* good_socknum */
 601        NULL,                           /* verify_bind */
 602        128,                            /* max_header */
 603        0,                              /* retransmits */
 604        "RAW",                          /* name */
 605        0,                              /* inuse */
 606        0                               /* highestinuse */
 607};
 608
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.