root/net/inet/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_sendto
  5. raw_write
  6. raw_close
  7. raw_init
  8. raw_recvfrom
  9. 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  *
  27  *              This program is free software; you can redistribute it and/or
  28  *              modify it under the terms of the GNU General Public License
  29  *              as published by the Free Software Foundation; either version
  30  *              2 of the License, or (at your option) any later version.
  31  */
  32 #include <asm/system.h>
  33 #include <asm/segment.h>
  34 #include <linux/types.h>
  35 #include <linux/sched.h>
  36 #include <linux/errno.h>
  37 #include <linux/timer.h>
  38 #include <linux/mm.h>
  39 #include <linux/kernel.h>
  40 #include <linux/fcntl.h>
  41 #include <linux/socket.h>
  42 #include <linux/in.h>
  43 #include "inet.h"
  44 #include "dev.h"
  45 #include "ip.h"
  46 #include "protocol.h"
  47 #include "tcp.h"
  48 #include "skbuff.h"
  49 #include "sock.h"
  50 #include "icmp.h"
  51 #include "udp.h"
  52 
  53 
  54 static unsigned long
  55 min(unsigned long a, unsigned long b)
     /* [previous][next][first][last][top][bottom][index][help] */
  56 {
  57   if (a < b) return(a);
  58   return(b);
  59 }
  60 
  61 
  62 /* raw_err gets called by the icmp module. */
  63 void
  64 raw_err (int err, unsigned char *header, unsigned long daddr,
     /* [previous][next][first][last][top][bottom][index][help] */
  65          unsigned long saddr, struct inet_protocol *protocol)
  66 {
  67   struct sock *sk;
  68    
  69   DPRINTF((DBG_RAW, "raw_err(err=%d, hdr=%X, daddr=%X, saddr=%X, protocl=%X)\n",
  70                 err, header, daddr, saddr, protocol));
  71 
  72   if (protocol == NULL) return;
  73   sk = (struct sock *) protocol->data;
  74   if (sk == NULL) return;
  75 
  76   /* This is meaningless in raw sockets. */
  77   if (err & 0xff00 == (ICMP_SOURCE_QUENCH << 8)) {
  78         if (sk->cong_window > 1) sk->cong_window = sk->cong_window/2;
  79         return;
  80   }
  81 
  82   sk->err = icmp_err_convert[err & 0xff].errno;
  83   sk->error_report(sk);
  84   
  85   return;
  86 }
  87 
  88 
  89 /*
  90  * This should be the easiest of all, all we do is\
  91  * copy it into a buffer.
  92  */
  93 int
  94 raw_rcv(struct sk_buff *skb, struct device *dev, struct options *opt,
     /* [previous][next][first][last][top][bottom][index][help] */
  95         unsigned long daddr, unsigned short len, unsigned long saddr,
  96         int redo, struct inet_protocol *protocol)
  97 {
  98   struct sock *sk;
  99 
 100   DPRINTF((DBG_RAW, "raw_rcv(skb=%X, dev=%X, opt=%X, daddr=%X,\n"
 101            "         len=%d, saddr=%X, redo=%d, protocol=%X)\n",
 102            skb, dev, opt, daddr, len, saddr, redo, protocol));
 103 
 104   if (skb == NULL) return(0);
 105   if (protocol == NULL) {
 106         kfree_skb(skb, FREE_READ);
 107         return(0);
 108   }
 109   sk = (struct sock *) protocol->data;
 110   if (sk == NULL) {
 111         kfree_skb(skb, FREE_READ);
 112         return(0);
 113   }
 114 
 115   /* Now we need to copy this into memory. */
 116   skb->sk = sk;
 117   skb->len = len;
 118   skb->dev = dev;
 119   skb->saddr = daddr;
 120   skb->daddr = saddr;
 121 
 122   /* Charge it too the socket. */
 123   if (sk->rmem_alloc + skb->mem_len >= sk->rcvbuf) {
 124         skb->sk = NULL;
 125         kfree_skb(skb, FREE_READ);
 126         return(0);
 127   }
 128   sk->rmem_alloc += skb->mem_len;
 129   skb_queue_tail(&sk->rqueue,skb);
 130   sk->data_ready(sk,skb->len);
 131   release_sock(sk);
 132   return(0);
 133 }
 134 
 135 
 136 /* This will do terrible things if len + ipheader + devheader > dev->mtu */
 137 static int
 138 raw_sendto(struct sock *sk, unsigned char *from, int len,
     /* [previous][next][first][last][top][bottom][index][help] */
 139            int noblock,
 140            unsigned flags, struct sockaddr_in *usin, int addr_len)
 141 {
 142   struct sk_buff *skb;
 143   struct device *dev=NULL;
 144   struct sockaddr_in sin;
 145   int tmp;
 146   int err;
 147 
 148   DPRINTF((DBG_RAW, "raw_sendto(sk=%X, from=%X, len=%d, noblock=%d, flags=%X,\n"
 149            "            usin=%X, addr_len = %d)\n", sk, from, len, noblock,
 150            flags, usin, addr_len));
 151 
 152   /* Check the flags. */
 153   if (flags) return(-EINVAL);
 154   if (len < 0) return(-EINVAL);
 155 
 156   err=verify_area(VERIFY_READ,from,len);
 157   if(err)
 158         return err;
 159   /* Get and verify the address. */
 160   if (usin) {
 161         if (addr_len < sizeof(sin)) return(-EINVAL);
 162         err=verify_area (VERIFY_READ, usin, sizeof (sin));
 163         if(err)
 164                 return err;
 165         memcpy_fromfs(&sin, usin, sizeof(sin));
 166         if (sin.sin_family && sin.sin_family != AF_INET) return(-EINVAL);
 167   } else {
 168         if (sk->state != TCP_ESTABLISHED) return(-EINVAL);
 169         sin.sin_family = AF_INET;
 170         sin.sin_port = sk->protocol;
 171         sin.sin_addr.s_addr = sk->daddr;
 172   }
 173   if (sin.sin_port == 0) sin.sin_port = sk->protocol;
 174   
 175   if (sk->broadcast == 0 && chk_addr(sin.sin_addr.s_addr)==IS_BROADCAST)
 176         return -EACCES;
 177 
 178   sk->inuse = 1;
 179   skb = NULL;
 180   while (skb == NULL) {
 181         if(sk->err!=0)
 182         {
 183                 err= -sk->err;
 184                 sk->err=0;
 185                 release_sock(sk);
 186                 return(err);
 187         }
 188         
 189         skb = (struct sk_buff *) sk->prot->wmalloc(sk,
 190                         len+sizeof(*skb) + sk->prot->max_header,
 191                         0, GFP_KERNEL);
 192         if (skb == NULL) {
 193                 int tmp;
 194 
 195                 DPRINTF((DBG_RAW, "raw_sendto: write buffer full?\n"));
 196                 if (noblock) 
 197                         return(-EAGAIN);
 198                 tmp = sk->wmem_alloc;
 199                 release_sock(sk);
 200                 cli();
 201                 if (tmp <= sk->wmem_alloc) {
 202                         interruptible_sleep_on(sk->sleep);
 203                         if (current->signal & ~current->blocked) {
 204                                 sti();
 205                                 return(-ERESTARTSYS);
 206                         }
 207                 }
 208                 sk->inuse = 1;
 209                 sti();
 210         }
 211   }
 212   skb->mem_addr = skb;
 213   skb->mem_len = len + sizeof(*skb) +sk->prot->max_header;
 214   skb->sk = sk;
 215 
 216   skb->free = 1; /* these two should be unecessary. */
 217   skb->arp = 0;
 218 
 219   tmp = sk->prot->build_header(skb, sk->saddr, 
 220                                sin.sin_addr.s_addr, &dev,
 221                                sk->protocol, sk->opt, skb->mem_len, sk->ip_tos,sk->ip_ttl);
 222   if (tmp < 0) {
 223         DPRINTF((DBG_RAW, "raw_sendto: error building ip header.\n"));
 224         kfree_skb(skb,FREE_WRITE);
 225         release_sock(sk);
 226         return(tmp);
 227   }
 228 
 229   /* verify_area(VERIFY_WRITE, from, len);*/
 230   memcpy_fromfs ((unsigned char *)(skb+1)+tmp, from, len);
 231 
 232   /* If we are using IPPROTO_RAW, we need to fill in the source address in
 233      the IP header */
 234 
 235   if(sk->protocol==IPPROTO_RAW) {
 236     unsigned char *buff;
 237     struct iphdr *iph;
 238 
 239     buff = (unsigned char *)(skb + 1);
 240     buff += tmp;
 241     iph = (struct iphdr *)buff;
 242     iph->saddr = sk->saddr;
 243   }
 244 
 245   skb->len = tmp + len;
 246   
 247   if(dev!=NULL && skb->len > 4095)
 248   {
 249         kfree_skb(skb, FREE_WRITE);
 250         release_sock(sk);
 251         return(-EMSGSIZE);
 252   }
 253   
 254   sk->prot->queue_xmit(sk, dev, skb, 1);
 255   release_sock(sk);
 256   return(len);
 257 }
 258 
 259 
 260 static int
 261 raw_write(struct sock *sk, unsigned char *buff, int len, int noblock,
     /* [previous][next][first][last][top][bottom][index][help] */
 262            unsigned flags)
 263 {
 264   return(raw_sendto(sk, buff, len, noblock, flags, NULL, 0));
 265 }
 266 
 267 
 268 static void
 269 raw_close(struct sock *sk, int timeout)
     /* [previous][next][first][last][top][bottom][index][help] */
 270 {
 271   sk->inuse = 1;
 272   sk->state = TCP_CLOSE;
 273 
 274   DPRINTF((DBG_RAW, "raw_close: deleting protocol %d\n",
 275            ((struct inet_protocol *)sk->pair)->protocol));
 276 
 277   if (inet_del_protocol((struct inet_protocol *)sk->pair) < 0)
 278                 DPRINTF((DBG_RAW, "raw_close: del_protocol failed.\n"));
 279   kfree_s((void *)sk->pair, sizeof (struct inet_protocol));
 280   sk->pair = NULL;
 281   release_sock(sk);
 282 }
 283 
 284 
 285 static int
 286 raw_init(struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 287 {
 288   struct inet_protocol *p;
 289 
 290   p = (struct inet_protocol *) kmalloc(sizeof (*p), GFP_KERNEL);
 291   if (p == NULL) return(-ENOMEM);
 292 
 293   p->handler = raw_rcv;
 294   p->protocol = sk->protocol;
 295   p->data = (void *)sk;
 296   p->err_handler = raw_err;
 297   p->name="USER";
 298   p->frag_handler = NULL;       /* For now */
 299   inet_add_protocol(p);
 300    
 301   /* We need to remember this somewhere. */
 302   sk->pair = (struct sock *)p;
 303 
 304   DPRINTF((DBG_RAW, "raw init added protocol %d\n", sk->protocol));
 305 
 306   return(0);
 307 }
 308 
 309 
 310 /*
 311  * This should be easy, if there is something there
 312  * we return it, otherwise we block.
 313  */
 314 int
 315 raw_recvfrom(struct sock *sk, unsigned char *to, int len,
     /* [previous][next][first][last][top][bottom][index][help] */
 316              int noblock, unsigned flags, struct sockaddr_in *sin,
 317              int *addr_len)
 318 {
 319   int copied=0;
 320   struct sk_buff *skb;
 321   int err;
 322 
 323   DPRINTF((DBG_RAW, "raw_recvfrom (sk=%X, to=%X, len=%d, noblock=%d, flags=%X,\n"
 324            "              sin=%X, addr_len=%X)\n",
 325                 sk, to, len, noblock, flags, sin, addr_len));
 326 
 327   if (len == 0) return(0);
 328   if (len < 0) return(-EINVAL);
 329 
 330   if (sk->shutdown & RCV_SHUTDOWN) return(0);
 331   if (addr_len) {
 332         err=verify_area(VERIFY_WRITE, addr_len, sizeof(*addr_len));
 333         if(err)
 334                 return err;
 335         put_fs_long(sizeof(*sin), addr_len);
 336   }
 337   if(sin)
 338   {
 339         err=verify_area(VERIFY_WRITE, sin, sizeof(*sin));
 340         if(err)
 341                 return err;
 342   }
 343   
 344   err=verify_area(VERIFY_WRITE,to,len);
 345   if(err)
 346         return err;
 347 
 348   skb=skb_recv_datagram(sk,flags,noblock,&err);
 349   if(skb==NULL)
 350         return err;
 351 
 352   copied = min(len, skb->len);
 353   
 354   skb_copy_datagram(skb, 0, to, copied);
 355 
 356   /* Copy the address. */
 357   if (sin) {
 358         struct sockaddr_in addr;
 359 
 360         addr.sin_family = AF_INET;
 361         addr.sin_addr.s_addr = skb->daddr;
 362         memcpy_tofs(sin, &addr, sizeof(*sin));
 363   }
 364 
 365   skb_free_datagram(skb);
 366   release_sock(sk);
 367   return (copied);
 368 }
 369 
 370 
 371 int
 372 raw_read (struct sock *sk, unsigned char *buff, int len, int noblock,
     /* [previous][next][first][last][top][bottom][index][help] */
 373           unsigned flags)
 374 {
 375   return(raw_recvfrom(sk, buff, len, noblock, flags, NULL, NULL));
 376 }
 377 
 378 
 379 struct proto raw_prot = {
 380   sock_wmalloc,
 381   sock_rmalloc,
 382   sock_wfree,
 383   sock_rfree,
 384   sock_rspace,
 385   sock_wspace,
 386   raw_close,
 387   raw_read,
 388   raw_write,
 389   raw_sendto,
 390   raw_recvfrom,
 391   ip_build_header,
 392   udp_connect,
 393   NULL,
 394   ip_queue_xmit,
 395   ip_retransmit,
 396   NULL,
 397   NULL,
 398   raw_rcv,
 399   datagram_select,
 400   NULL,
 401   raw_init,
 402   NULL,
 403   ip_setsockopt,
 404   ip_getsockopt,
 405   128,
 406   0,
 407   {NULL,},
 408   "RAW"
 409 };

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