root/net/ipv4/raw.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. min
  2. raw_err
  3. raw_rcv
  4. raw_getfrag
  5. raw_getrawfrag
  6. raw_sendto
  7. raw_sendmsg
  8. raw_close
  9. raw_init
  10. raw_recvmsg

   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:     @(#)raw.c       1.0.4   05/25/93
   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  *
  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/config.h> 
  41 #include <asm/system.h>
  42 #include <asm/segment.h>
  43 #include <linux/types.h>
  44 #include <linux/sched.h>
  45 #include <linux/errno.h>
  46 #include <linux/timer.h>
  47 #include <linux/mm.h>
  48 #include <linux/kernel.h>
  49 #include <linux/fcntl.h>
  50 #include <linux/socket.h>
  51 #include <linux/in.h>
  52 #include <linux/inet.h>
  53 #include <linux/netdevice.h>
  54 #include <linux/mroute.h>
  55 #include <net/ip.h>
  56 #include <net/protocol.h>
  57 #include <linux/skbuff.h>
  58 #include <net/sock.h>
  59 #include <net/icmp.h>
  60 #include <net/udp.h>
  61 #include <net/checksum.h>
  62 
  63 #ifdef CONFIG_IP_MROUTE
  64 struct sock *mroute_socket=NULL;
  65 #endif
  66 
  67 static inline unsigned long min(unsigned long a, unsigned long b)
     /* [previous][next][first][last][top][bottom][index][help] */
  68 {
  69         if (a < b) 
  70                 return(a);
  71         return(b);
  72 }
  73 
  74 
  75 /*
  76  *      Raw_err does not currently get called by the icmp module - FIXME:
  77  */
  78  
  79 void raw_err (int type, int code, unsigned char *header, __u32 daddr,
     /* [previous][next][first][last][top][bottom][index][help] */
  80          __u32 saddr, struct inet_protocol *protocol)
  81 {
  82         struct sock *sk;
  83    
  84         if (protocol == NULL) 
  85                 return;
  86         sk = (struct sock *) protocol->data;
  87         if (sk == NULL) 
  88                 return;
  89 
  90         /* This is meaningless in raw sockets. */
  91         if (type == ICMP_SOURCE_QUENCH) 
  92         {
  93                 if (sk->cong_window > 1) sk->cong_window = sk->cong_window/2;
  94                 return;
  95         }
  96         
  97         if(type == ICMP_PARAMETERPROB)
  98         {
  99                 sk->err = EPROTO;
 100                 sk->error_report(sk);
 101         }
 102 
 103         if(code<13)
 104         {
 105                 sk->err = icmp_err_convert[code & 0xff].errno;
 106                 sk->error_report(sk);
 107         }
 108         
 109         return;
 110 }
 111 
 112 
 113 /*
 114  *      This should be the easiest of all, all we do is
 115  *      copy it into a buffer. All demultiplexing is done
 116  *      in ip.c
 117  */
 118 
 119 int raw_rcv(struct sock *sk, struct sk_buff *skb, struct device *dev, __u32 saddr, __u32 daddr)
     /* [previous][next][first][last][top][bottom][index][help] */
 120 {
 121         /* Now we need to copy this into memory. */
 122         skb->sk = sk;
 123         skb_trim(skb,ntohs(skb->ip_hdr->tot_len));
 124         
 125         skb->h.raw = (unsigned char *) skb->ip_hdr;
 126         skb->dev = dev;
 127         skb->saddr = daddr;
 128         skb->daddr = saddr;
 129 
 130 #if 0   
 131         /*
 132          *      For no adequately explained reasons BSD likes to mess up the header of
 133          *      the received frame. 
 134          */
 135          
 136         if(sk->bsdism)
 137                 skb->ip_hdr->tot_len=ntohs(skb->ip_hdr->tot_len-4*skb->ip_hdr->ihl);
 138 #endif
 139         
 140         /* Charge it to the socket. */
 141         
 142         if(sock_queue_rcv_skb(sk,skb)<0)
 143         {
 144                 ip_statistics.IpInDiscards++;
 145                 skb->sk=NULL;
 146                 kfree_skb(skb, FREE_READ);
 147                 return(0);
 148         }
 149 
 150         ip_statistics.IpInDelivers++;
 151         return 0;
 152 }
 153 
 154 /*
 155  *      Send a RAW IP packet.
 156  */
 157 
 158 /*
 159  *      Callback support is trivial for SOCK_RAW
 160  */
 161   
 162 static void raw_getfrag(const void *p, __u32 saddr, char *to, unsigned int offset, unsigned int fraglen)
     /* [previous][next][first][last][top][bottom][index][help] */
 163 {
 164         memcpy_fromfs(to, (const unsigned char *)p+offset, fraglen);
 165 }
 166 
 167 /*
 168  *      IPPROTO_RAW needs extra work.
 169  */
 170  
 171 static void raw_getrawfrag(const void *p, __u32 saddr, char *to, unsigned int offset, unsigned int fraglen)
     /* [previous][next][first][last][top][bottom][index][help] */
 172 {
 173         memcpy_fromfs(to, (const unsigned char *)p+offset, fraglen);
 174         if(offset==0)
 175         {
 176                 struct iphdr *iph=(struct iphdr *)to;
 177                 if(!iph->saddr)
 178                         iph->saddr=saddr;
 179                 iph->check=0;
 180                 iph->tot_len=htons(fraglen);    /* This is right as you cant frag
 181                                            RAW packets */
 182                 /*
 183                  *      Deliberate breach of modularity to keep 
 184                  *      ip_build_xmit clean (well less messy).
 185                  */
 186                 iph->id = htons(ip_id_count++);
 187                 iph->check=ip_fast_csum((unsigned char *)iph, iph->ihl);
 188         }
 189 }
 190 
 191 static int raw_sendto(struct sock *sk, const unsigned char *from, 
     /* [previous][next][first][last][top][bottom][index][help] */
 192         int len, int noblock, unsigned flags, struct sockaddr_in *usin, int addr_len)
 193 {
 194         int err;
 195         struct sockaddr_in sin;
 196 
 197         /*
 198          *      Check the flags. Only MSG_DONTROUTE is permitted.
 199          */
 200 
 201         if (flags & MSG_OOB)            /* Mirror BSD error message compatibility */
 202                 return -EOPNOTSUPP;
 203                          
 204         if (flags & ~MSG_DONTROUTE)
 205                 return(-EINVAL);
 206         /*
 207          *      Get and verify the address. 
 208          */
 209 
 210         if (usin) 
 211         {
 212                 if (addr_len < sizeof(sin)) 
 213                         return(-EINVAL);
 214                 memcpy(&sin, usin, sizeof(sin));
 215                 if (sin.sin_family && sin.sin_family != AF_INET) 
 216                         return(-EINVAL);
 217         }
 218         else 
 219         {
 220                 if (sk->state != TCP_ESTABLISHED) 
 221                         return(-EINVAL);
 222                 sin.sin_family = AF_INET;
 223                 sin.sin_port = sk->num;
 224                 sin.sin_addr.s_addr = sk->daddr;
 225         }
 226         if (sin.sin_port == 0) 
 227                 sin.sin_port = sk->num;
 228   
 229         if (sin.sin_addr.s_addr == INADDR_ANY)
 230                 sin.sin_addr.s_addr = ip_my_addr();
 231 
 232         /*
 233          *      BSD raw sockets forget to check SO_BROADCAST ....
 234          */
 235          
 236         if (!sk->bsdism && sk->broadcast == 0 && ip_chk_addr(sin.sin_addr.s_addr)==IS_BROADCAST)
 237                 return -EACCES;
 238 
 239         if(sk->ip_hdrincl)
 240         {
 241                 if(len>65535)
 242                         return -EMSGSIZE;
 243                 err=ip_build_xmit(sk, raw_getrawfrag, from, len, sin.sin_addr.s_addr, 0, sk->opt, flags, sin.sin_port, noblock);
 244         }
 245         else
 246         {
 247                 if(len>65535-sizeof(struct iphdr))
 248                         return -EMSGSIZE;
 249                 err=ip_build_xmit(sk, raw_getfrag, from, len, sin.sin_addr.s_addr, 0, sk->opt, flags, sin.sin_port, noblock);
 250         }
 251         return err<0?err:len;
 252 }
 253 
 254 /*
 255  *      Temporary
 256  */
 257  
 258 static int raw_sendmsg(struct sock *sk, struct msghdr *msg, int len, int noblock, 
     /* [previous][next][first][last][top][bottom][index][help] */
 259         int flags)
 260 {
 261         if(msg->msg_iovlen==1)
 262                 return raw_sendto(sk,msg->msg_iov[0].iov_base,len, noblock, flags, msg->msg_name, msg->msg_namelen);
 263         else
 264         {
 265                 /*
 266                  *      For awkward cases we linearise the buffer first. In theory this is only frames
 267                  *      whose iovec's don't split on 4 byte boundaries, and soon encrypted stuff (to keep
 268                  *      skip happy). We are a bit more general about it.
 269                  */
 270                  
 271                 unsigned char *buf;
 272                 int fs;
 273                 int err;
 274                 if(len>65515)
 275                         return -EMSGSIZE;
 276                 buf=kmalloc(len, GFP_KERNEL);
 277                 if(buf==NULL)
 278                         return -ENOBUFS;
 279                 memcpy_fromiovec(buf, msg->msg_iov, len);
 280                 fs=get_fs();
 281                 set_fs(get_ds());
 282                 err=raw_sendto(sk,buf,len, noblock, flags, msg->msg_name, msg->msg_namelen);
 283                 set_fs(fs);
 284                 kfree_s(buf,len);
 285                 return err;
 286         }
 287 }
 288 
 289 static void raw_close(struct sock *sk, unsigned long timeout)
     /* [previous][next][first][last][top][bottom][index][help] */
 290 {
 291         sk->state = TCP_CLOSE;
 292 #ifdef CONFIG_IP_MROUTE 
 293         if(sk==mroute_socket)
 294         {
 295                 mroute_close(sk);
 296                 mroute_socket=NULL;
 297         }
 298 #endif  
 299         destroy_sock(sk);
 300 }
 301 
 302 
 303 static int raw_init(struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 304 {
 305         return(0);
 306 }
 307 
 308 
 309 /*
 310  *      This should be easy, if there is something there
 311  *      we return it, otherwise we block.
 312  */
 313 
 314 int raw_recvmsg(struct sock *sk, struct msghdr *msg, int len,
     /* [previous][next][first][last][top][bottom][index][help] */
 315      int noblock, int flags,int *addr_len)
 316 {
 317         int copied=0;
 318         struct sk_buff *skb;
 319         int err;
 320         struct sockaddr_in *sin=(struct sockaddr_in *)msg->msg_name;
 321 
 322         if (flags & MSG_OOB)
 323                 return -EOPNOTSUPP;
 324                 
 325         if (sk->shutdown & RCV_SHUTDOWN) 
 326                 return(0);
 327 
 328         if (addr_len) 
 329                 *addr_len=sizeof(*sin);
 330 
 331         skb=skb_recv_datagram(sk,flags,noblock,&err);
 332         if(skb==NULL)
 333                 return err;
 334 
 335         copied = min(len, skb->len);
 336         
 337         skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
 338         sk->stamp=skb->stamp;
 339 
 340         /* Copy the address. */
 341         if (sin) 
 342         {
 343                 sin->sin_family = AF_INET;
 344                 sin->sin_addr.s_addr = skb->daddr;
 345         }
 346         skb_free_datagram(sk, skb);
 347         return (copied);
 348 }
 349 
 350 
 351 struct proto raw_prot = {
 352         raw_close,
 353         ip_build_header,
 354         udp_connect,
 355         NULL,
 356         ip_queue_xmit,
 357         NULL,
 358         NULL,
 359         NULL,
 360         NULL,
 361         datagram_select,
 362 #ifdef CONFIG_IP_MROUTE 
 363         ipmr_ioctl,
 364 #else
 365         NULL,
 366 #endif          
 367         raw_init,
 368         NULL,
 369         ip_setsockopt,
 370         ip_getsockopt,
 371         raw_sendmsg,
 372         raw_recvmsg,
 373         NULL,           /* No special bind */
 374         128,
 375         0,
 376         "RAW",
 377         0, 0,
 378         {NULL,}
 379 };

/* [previous][next][first][last][top][bottom][index][help] */