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

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