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_write
  8. raw_close
  9. raw_init
  10. raw_recvfrom
  11. raw_read

   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  *
  31  *              This program is free software; you can redistribute it and/or
  32  *              modify it under the terms of the GNU General Public License
  33  *              as published by the Free Software Foundation; either version
  34  *              2 of the License, or (at your option) any later version.
  35  */
  36 #include <asm/system.h>
  37 #include <asm/segment.h>
  38 #include <linux/types.h>
  39 #include <linux/sched.h>
  40 #include <linux/errno.h>
  41 #include <linux/timer.h>
  42 #include <linux/mm.h>
  43 #include <linux/kernel.h>
  44 #include <linux/fcntl.h>
  45 #include <linux/socket.h>
  46 #include <linux/in.h>
  47 #include <linux/inet.h>
  48 #include <linux/netdevice.h>
  49 #include <net/ip.h>
  50 #include <net/protocol.h>
  51 #include <linux/skbuff.h>
  52 #include <net/sock.h>
  53 #include <net/icmp.h>
  54 #include <net/udp.h>
  55 #include <net/checksum.h>
  56 
  57 static inline unsigned long min(unsigned long a, unsigned long b)
     /* [previous][next][first][last][top][bottom][index][help] */
  58 {
  59         if (a < b) 
  60                 return(a);
  61         return(b);
  62 }
  63 
  64 
  65 /* raw_err gets called by the icmp module. */
  66 void raw_err (int err, unsigned char *header, unsigned long daddr,
     /* [previous][next][first][last][top][bottom][index][help] */
  67          unsigned long saddr, struct inet_protocol *protocol)
  68 {
  69         struct sock *sk;
  70    
  71         if (protocol == NULL) 
  72                 return;
  73         sk = (struct sock *) protocol->data;
  74         if (sk == NULL) 
  75                 return;
  76 
  77         /* This is meaningless in raw sockets. */
  78         if (err & 0xff00 == (ICMP_SOURCE_QUENCH << 8)) 
  79         {
  80                 if (sk->cong_window > 1) sk->cong_window = sk->cong_window/2;
  81                 return;
  82         }
  83 
  84         sk->err = icmp_err_convert[err & 0xff].errno;
  85         sk->error_report(sk);
  86   
  87         return;
  88 }
  89 
  90 
  91 /*
  92  *      This should be the easiest of all, all we do is
  93  *      copy it into a buffer. All demultiplexing is done
  94  *      in ip.c
  95  */
  96 
  97 int raw_rcv(struct sock *sk, struct sk_buff *skb, struct device *dev, long saddr, long daddr)
     /* [previous][next][first][last][top][bottom][index][help] */
  98 {
  99         /* Now we need to copy this into memory. */
 100         skb->sk = sk;
 101         skb->len = ntohs(skb->ip_hdr->tot_len);
 102         skb->h.raw = (unsigned char *) skb->ip_hdr;
 103         skb->dev = dev;
 104         skb->saddr = daddr;
 105         skb->daddr = saddr;
 106 
 107         /* Charge it to the socket. */
 108         
 109         if(sock_queue_rcv_skb(sk,skb)<0)
 110         {
 111                 ip_statistics.IpInDiscards++;
 112                 skb->sk=NULL;
 113                 kfree_skb(skb, FREE_READ);
 114                 return(0);
 115         }
 116 
 117         ip_statistics.IpInDelivers++;
 118         release_sock(sk);
 119         return(0);
 120 }
 121 
 122 /*
 123  *      Send a RAW IP packet.
 124  */
 125 
 126 /*
 127  *      Callback support is trivial for SOCK_RAW
 128  */
 129   
 130 static void raw_getfrag(void *p, int saddr, char *to, unsigned int offset, unsigned int fraglen)
     /* [previous][next][first][last][top][bottom][index][help] */
 131 {
 132         memcpy_fromfs(to, (unsigned char *)p+offset, fraglen);
 133 }
 134 
 135 /*
 136  *      IPPROTO_RAW needs extra work.
 137  */
 138  
 139 static void raw_getrawfrag(void *p, int saddr, char *to, unsigned int offset, unsigned int fraglen)
     /* [previous][next][first][last][top][bottom][index][help] */
 140 {
 141         memcpy_fromfs(to, (unsigned char *)p+offset, fraglen);
 142         if(offset==0)
 143         {
 144                 struct iphdr *iph=(struct iphdr *)to;
 145                 iph->saddr=saddr;
 146                 iph->check=0;
 147                 iph->check=ip_fast_csum((unsigned char *)iph, iph->ihl);
 148         }
 149 }
 150 
 151 static int raw_sendto(struct sock *sk, unsigned char *from, 
     /* [previous][next][first][last][top][bottom][index][help] */
 152         int len, int noblock, unsigned flags, struct sockaddr_in *usin, int addr_len)
 153 {
 154         int err;
 155         struct sockaddr_in sin;
 156 
 157         /*
 158          *      Check the flags. Only MSG_DONTROUTE is permitted.
 159          */
 160 
 161         if (flags & MSG_OOB)            /* Mirror BSD error message compatibility */
 162                 return -EOPNOTSUPP;
 163                          
 164         if (flags & ~MSG_DONTROUTE)
 165                 return(-EINVAL);
 166         /*
 167          *      Get and verify the address. 
 168          */
 169 
 170         if (usin) 
 171         {
 172                 if (addr_len < sizeof(sin)) 
 173                         return(-EINVAL);
 174                 memcpy(&sin, usin, sizeof(sin));
 175                 if (sin.sin_family && sin.sin_family != AF_INET) 
 176                         return(-EINVAL);
 177         }
 178         else 
 179         {
 180                 if (sk->state != TCP_ESTABLISHED) 
 181                         return(-EINVAL);
 182                 sin.sin_family = AF_INET;
 183                 sin.sin_port = sk->protocol;
 184                 sin.sin_addr.s_addr = sk->daddr;
 185         }
 186         if (sin.sin_port == 0) 
 187                 sin.sin_port = sk->protocol;
 188   
 189         if (sin.sin_addr.s_addr == INADDR_ANY)
 190                 sin.sin_addr.s_addr = ip_my_addr();
 191 
 192         if (sk->broadcast == 0 && ip_chk_addr(sin.sin_addr.s_addr)==IS_BROADCAST)
 193                 return -EACCES;
 194 
 195         if(sk->num==IPPROTO_RAW)
 196                 err=ip_build_xmit(sk, raw_getrawfrag, from, len, sin.sin_addr.s_addr, flags, sin.sin_port);
 197         else
 198                 err=ip_build_xmit(sk, raw_getfrag, from, len, sin.sin_addr.s_addr, flags, sin.sin_port);
 199         return err<0?err:len;
 200 }
 201 
 202 
 203 static int raw_write(struct sock *sk, unsigned char *buff, int len, int noblock,
     /* [previous][next][first][last][top][bottom][index][help] */
 204            unsigned flags)
 205 {
 206         return(raw_sendto(sk, buff, len, noblock, flags, NULL, 0));
 207 }
 208 
 209 
 210 static void raw_close(struct sock *sk, int timeout)
     /* [previous][next][first][last][top][bottom][index][help] */
 211 {
 212         sk->state = TCP_CLOSE;
 213 }
 214 
 215 
 216 static int raw_init(struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 217 {
 218         return(0);
 219 }
 220 
 221 
 222 /*
 223  *      This should be easy, if there is something there
 224  *      we return it, otherwise we block.
 225  */
 226 
 227 int raw_recvfrom(struct sock *sk, unsigned char *to, int len,
     /* [previous][next][first][last][top][bottom][index][help] */
 228      int noblock, unsigned flags, struct sockaddr_in *sin,
 229              int *addr_len)
 230 {
 231         int copied=0;
 232         struct sk_buff *skb;
 233         int err;
 234         int truesize;
 235 
 236         if (flags & MSG_OOB)
 237                 return -EOPNOTSUPP;
 238                 
 239         if (sk->shutdown & RCV_SHUTDOWN) 
 240                 return(0);
 241 
 242         if (addr_len) 
 243                 *addr_len=sizeof(*sin);
 244 
 245         skb=skb_recv_datagram(sk,flags,noblock,&err);
 246         if(skb==NULL)
 247                 return err;
 248 
 249         truesize=skb->len;
 250         copied = min(len, truesize);
 251   
 252         skb_copy_datagram(skb, 0, to, copied);
 253         sk->stamp=skb->stamp;
 254 
 255         /* Copy the address. */
 256         if (sin) 
 257         {
 258                 sin->sin_family = AF_INET;
 259                 sin->sin_addr.s_addr = skb->daddr;
 260         }
 261         skb_free_datagram(skb);
 262         release_sock(sk);
 263         return (truesize);      /* len not copied. BSD returns the true size of the message so you know a bit fell off! */
 264 }
 265 
 266 
 267 int raw_read (struct sock *sk, unsigned char *buff, int len, int noblock,unsigned flags)
     /* [previous][next][first][last][top][bottom][index][help] */
 268 {
 269         return(raw_recvfrom(sk, buff, len, noblock, flags, NULL, NULL));
 270 }
 271 
 272 
 273 struct proto raw_prot = {
 274         sock_wmalloc,
 275         sock_rmalloc,
 276         sock_wfree,
 277         sock_rfree,
 278         sock_rspace,
 279         sock_wspace,
 280         raw_close,
 281         raw_read,
 282         raw_write,
 283         raw_sendto,
 284         raw_recvfrom,
 285         ip_build_header,
 286         udp_connect,
 287         NULL,
 288         ip_queue_xmit,
 289         NULL,
 290         NULL,
 291         NULL,
 292         NULL,
 293         datagram_select,
 294         NULL,
 295         raw_init,
 296         NULL,
 297         ip_setsockopt,
 298         ip_getsockopt,
 299         128,
 300         0,
 301         "RAW",
 302         0, 0,
 303         {NULL,}
 304 };

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