root/net/inet/sock.c

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

DEFINITIONS

This source file includes following definitions.
  1. sock_setsockopt
  2. sock_getsockopt
  3. sock_wmalloc
  4. sock_rmalloc
  5. sock_rspace
  6. sock_wspace
  7. sock_wfree
  8. sock_rfree
  9. sock_alloc_send_skb
  10. sock_queue_rcv_skb
  11. release_sock

   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  *              Generic socket support routines. Memory allocators, sk->inuse/release
   7  *              handler for protocols to use and generic option handler.
   8  *
   9  *
  10  * Version:     @(#)sock.c      1.0.17  06/02/93
  11  *
  12  * Authors:     Ross Biro, <bir7@leland.Stanford.Edu>
  13  *              Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  14  *              Florian La Roche, <flla@stud.uni-sb.de>
  15  *              Alan Cox, <A.Cox@swansea.ac.uk>
  16  *
  17  * Fixes:
  18  *              Alan Cox        :       Numerous verify_area() problems
  19  *              Alan Cox        :       Connecting on a connecting socket
  20  *                                      now returns an error for tcp.
  21  *              Alan Cox        :       sock->protocol is set correctly.
  22  *                                      and is not sometimes left as 0.
  23  *              Alan Cox        :       connect handles icmp errors on a
  24  *                                      connect properly. Unfortunately there
  25  *                                      is a restart syscall nasty there. I
  26  *                                      can't match BSD without hacking the C
  27  *                                      library. Ideas urgently sought!
  28  *              Alan Cox        :       Disallow bind() to addresses that are
  29  *                                      not ours - especially broadcast ones!!
  30  *              Alan Cox        :       Socket 1024 _IS_ ok for users. (fencepost)
  31  *              Alan Cox        :       sock_wfree/sock_rfree don't destroy sockets,
  32  *                                      instead they leave that for the DESTROY timer.
  33  *              Alan Cox        :       Clean up error flag in accept
  34  *              Alan Cox        :       TCP ack handling is buggy, the DESTROY timer
  35  *                                      was buggy. Put a remove_sock() in the handler
  36  *                                      for memory when we hit 0. Also altered the timer
  37  *                                      code. The ACK stuff can wait and needs major 
  38  *                                      TCP layer surgery.
  39  *              Alan Cox        :       Fixed TCP ack bug, removed remove sock
  40  *                                      and fixed timer/inet_bh race.
  41  *              Alan Cox        :       Added zapped flag for TCP
  42  *              Alan Cox        :       Move kfree_skb into skbuff.c and tidied up surplus code
  43  *              Alan Cox        :       for new sk_buff allocations wmalloc/rmalloc now call alloc_skb
  44  *              Alan Cox        :       kfree_s calls now are kfree_skbmem so we can track skb resources
  45  *              Alan Cox        :       Supports socket option broadcast now as does udp. Packet and raw need fixing.
  46  *              Alan Cox        :       Added RCVBUF,SNDBUF size setting. It suddenly occurred to me how easy it was so...
  47  *              Rick Sladkey    :       Relaxed UDP rules for matching packets.
  48  *              C.E.Hawkins     :       IFF_PROMISC/SIOCGHWADDR support
  49  *      Pauline Middelink       :       identd support
  50  *              Alan Cox        :       Fixed connect() taking signals I think.
  51  *              Alan Cox        :       SO_LINGER supported
  52  *              Alan Cox        :       Error reporting fixes
  53  *              Anonymous       :       inet_create tidied up (sk->reuse setting)
  54  *              Alan Cox        :       inet sockets don't set sk->type!
  55  *              Alan Cox        :       Split socket option code
  56  *              Alan Cox        :       Callbacks
  57  *              Alan Cox        :       Nagle flag for Charles & Johannes stuff
  58  *              Alex            :       Removed restriction on inet fioctl
  59  *              Alan Cox        :       Splitting INET from NET core
  60  *              Alan Cox        :       Fixed bogus SO_TYPE handling in getsockopt()
  61  *              Adam Caldwell   :       Missing return in SO_DONTROUTE/SO_DEBUG code
  62  *              Alan Cox        :       Split IP from generic code
  63  *              Alan Cox        :       New kfree_skbmem()
  64  *
  65  * To Fix:
  66  *
  67  *
  68  *              This program is free software; you can redistribute it and/or
  69  *              modify it under the terms of the GNU General Public License
  70  *              as published by the Free Software Foundation; either version
  71  *              2 of the License, or (at your option) any later version.
  72  */
  73 
  74 #include <linux/config.h>
  75 #include <linux/errno.h>
  76 #include <linux/types.h>
  77 #include <linux/socket.h>
  78 #include <linux/in.h>
  79 #include <linux/kernel.h>
  80 #include <linux/major.h>
  81 #include <linux/sched.h>
  82 #include <linux/timer.h>
  83 #include <linux/string.h>
  84 #include <linux/sockios.h>
  85 #include <linux/net.h>
  86 #include <linux/fcntl.h>
  87 #include <linux/mm.h>
  88 #include <linux/interrupt.h>
  89 
  90 #include <asm/segment.h>
  91 #include <asm/system.h>
  92 
  93 #include <linux/inet.h>
  94 #include <linux/netdevice.h>
  95 #include "ip.h"
  96 #include "protocol.h"
  97 #include "arp.h"
  98 #include "rarp.h"
  99 #include "route.h"
 100 #include "tcp.h"
 101 #include "udp.h"
 102 #include <linux/skbuff.h>
 103 #include "sock.h"
 104 #include "raw.h"
 105 #include "icmp.h"
 106 
 107 #define min(a,b)        ((a)<(b)?(a):(b))
 108 
 109 /*
 110  *      This is meant for all protocols to use and covers goings on
 111  *      at the socket level. Everything here is generic.
 112  */
 113 
 114 int sock_setsockopt(struct sock *sk, int level, int optname,
     /* [previous][next][first][last][top][bottom][index][help] */
 115                 char *optval, int optlen)
 116 {
 117         int val;
 118         int err;
 119         struct linger ling;
 120 
 121         if (optval == NULL) 
 122                 return(-EINVAL);
 123 
 124         err=verify_area(VERIFY_READ, optval, sizeof(int));
 125         if(err)
 126                 return err;
 127         
 128         val = get_fs_long((unsigned long *)optval);
 129         switch(optname) 
 130         {
 131                 case SO_TYPE:
 132                 case SO_ERROR:
 133                         return(-ENOPROTOOPT);
 134 
 135                 case SO_DEBUG:  
 136                         sk->debug=val?1:0;
 137                         return 0;
 138                 case SO_DONTROUTE:
 139                         sk->localroute=val?1:0;
 140                         return 0;
 141                 case SO_BROADCAST:
 142                         sk->broadcast=val?1:0;
 143                         return 0;
 144                 case SO_SNDBUF:
 145                         if(val>32767)
 146                                 val=32767;
 147                         if(val<256)
 148                                 val=256;
 149                         sk->sndbuf=val;
 150                         return 0;
 151                 case SO_LINGER:
 152                         err=verify_area(VERIFY_READ,optval,sizeof(ling));
 153                         if(err)
 154                                 return err;
 155                         memcpy_fromfs(&ling,optval,sizeof(ling));
 156                         if(ling.l_onoff==0)
 157                                 sk->linger=0;
 158                         else
 159                         {
 160                                 sk->lingertime=ling.l_linger;
 161                                 sk->linger=1;
 162                         }
 163                         return 0;
 164                 case SO_RCVBUF:
 165                         if(val>32767)
 166                                 val=32767;
 167                         if(val<256)
 168                                 val=256;
 169                         sk->rcvbuf=val;
 170                         return(0);
 171 
 172                 case SO_REUSEADDR:
 173                         if (val) 
 174                                 sk->reuse = 1;
 175                         else 
 176                                 sk->reuse = 0;
 177                         return(0);
 178 
 179                 case SO_KEEPALIVE:
 180                         if (val)
 181                                 sk->keepopen = 1;
 182                         else 
 183                                 sk->keepopen = 0;
 184                         return(0);
 185 
 186                 case SO_OOBINLINE:
 187                         if (val) 
 188                                 sk->urginline = 1;
 189                         else 
 190                                 sk->urginline = 0;
 191                         return(0);
 192 
 193                 case SO_NO_CHECK:
 194                         if (val) 
 195                                 sk->no_check = 1;
 196                         else 
 197                                 sk->no_check = 0;
 198                         return(0);
 199 
 200                  case SO_PRIORITY:
 201                         if (val >= 0 && val < DEV_NUMBUFFS) 
 202                         {
 203                                 sk->priority = val;
 204                         } 
 205                         else 
 206                         {
 207                                 return(-EINVAL);
 208                         }
 209                         return(0);
 210 
 211                 default:
 212                         return(-ENOPROTOOPT);
 213         }
 214 }
 215 
 216 
 217 int sock_getsockopt(struct sock *sk, int level, int optname,
     /* [previous][next][first][last][top][bottom][index][help] */
 218                    char *optval, int *optlen)
 219 {               
 220         int val;
 221         int err;
 222         struct linger ling;
 223 
 224         switch(optname) 
 225         {
 226                 case SO_DEBUG:          
 227                         val = sk->debug;
 228                         break;
 229                 
 230                 case SO_DONTROUTE:
 231                         val = sk->localroute;
 232                         break;
 233                 
 234                 case SO_BROADCAST:
 235                         val= sk->broadcast;
 236                         break;
 237                 
 238                 case SO_LINGER: 
 239                         err=verify_area(VERIFY_WRITE,optval,sizeof(ling));
 240                         if(err)
 241                                 return err;
 242                         err=verify_area(VERIFY_WRITE,optlen,sizeof(int));
 243                         if(err)
 244                                 return err;
 245                         put_fs_long(sizeof(ling),(unsigned long *)optlen);
 246                         ling.l_onoff=sk->linger;
 247                         ling.l_linger=sk->lingertime;
 248                         memcpy_tofs(optval,&ling,sizeof(ling));
 249                         return 0;
 250                 
 251                 case SO_SNDBUF:
 252                         val=sk->sndbuf;
 253                         break;
 254                 
 255                 case SO_RCVBUF:
 256                         val =sk->rcvbuf;
 257                         break;
 258 
 259                 case SO_REUSEADDR:
 260                         val = sk->reuse;
 261                         break;
 262 
 263                 case SO_KEEPALIVE:
 264                         val = sk->keepopen;
 265                         break;
 266 
 267                 case SO_TYPE:
 268 #if 0           
 269                         if (sk->prot == &tcp_prot) 
 270                                 val = SOCK_STREAM;
 271                         else 
 272                                 val = SOCK_DGRAM;
 273 #endif
 274                         val = sk->type;                         
 275                         break;
 276 
 277                 case SO_ERROR:
 278                         val = sk->err;
 279                         sk->err = 0;
 280                         break;
 281 
 282                 case SO_OOBINLINE:
 283                         val = sk->urginline;
 284                         break;
 285         
 286                 case SO_NO_CHECK:
 287                         val = sk->no_check;
 288                         break;
 289 
 290                 case SO_PRIORITY:
 291                         val = sk->priority;
 292                         break;
 293 
 294                 default:
 295                         return(-ENOPROTOOPT);
 296         }
 297         err=verify_area(VERIFY_WRITE, optlen, sizeof(int));
 298         if(err)
 299                 return err;
 300         put_fs_long(sizeof(int),(unsigned long *) optlen);
 301 
 302         err=verify_area(VERIFY_WRITE, optval, sizeof(int));
 303         if(err)
 304                 return err;
 305         put_fs_long(val,(unsigned long *)optval);
 306 
 307         return(0);
 308 }
 309 
 310 
 311 struct sk_buff *sock_wmalloc(struct sock *sk, unsigned long size, int force, int priority)
     /* [previous][next][first][last][top][bottom][index][help] */
 312 {
 313         if (sk) 
 314         {
 315                 if (sk->wmem_alloc + size < sk->sndbuf || force) 
 316                 {
 317                         struct sk_buff * c = alloc_skb(size, priority);
 318                         if (c) 
 319                         {
 320                                 cli();
 321                                 sk->wmem_alloc+= c->mem_len;
 322                                 sti();
 323                         }
 324                         return c;
 325                 }
 326                 return(NULL);
 327         }
 328         return(alloc_skb(size, priority));
 329 }
 330 
 331 
 332 struct sk_buff *sock_rmalloc(struct sock *sk, unsigned long size, int force, int priority)
     /* [previous][next][first][last][top][bottom][index][help] */
 333 {
 334         if (sk) 
 335         {
 336                 if (sk->rmem_alloc + size < sk->rcvbuf || force) 
 337                 {
 338                         struct sk_buff *c = alloc_skb(size, priority);
 339                         if (c) 
 340                         {
 341                                 cli();
 342                                 sk->rmem_alloc += c->mem_len;
 343                                 sti();
 344                         }
 345                         return(c);
 346                 }
 347                 return(NULL);
 348         }
 349         return(alloc_skb(size, priority));
 350 }
 351 
 352 
 353 unsigned long sock_rspace(struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 354 {
 355         int amt;
 356 
 357         if (sk != NULL) 
 358         {
 359                 if (sk->rmem_alloc >= sk->rcvbuf-2*MIN_WINDOW) 
 360                         return(0);
 361                 amt = min((sk->rcvbuf-sk->rmem_alloc)/2-MIN_WINDOW, MAX_WINDOW);
 362                 if (amt < 0) 
 363                         return(0);
 364                 return(amt);
 365         }
 366         return(0);
 367 }
 368 
 369 
 370 unsigned long sock_wspace(struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 371 {
 372         if (sk != NULL) 
 373         {
 374                 if (sk->shutdown & SEND_SHUTDOWN)
 375                         return(0);
 376                 if (sk->wmem_alloc >= sk->sndbuf)
 377                         return(0);
 378                 return(sk->sndbuf-sk->wmem_alloc );
 379         }
 380         return(0);
 381 }
 382 
 383 
 384 void sock_wfree(struct sock *sk, struct sk_buff *skb, unsigned long size)
     /* [previous][next][first][last][top][bottom][index][help] */
 385 {
 386 #ifdef CONFIG_SKB_CHECK
 387         IS_SKB(skb);
 388 #endif
 389         kfree_skbmem(skb, size);
 390         if (sk) 
 391         {
 392                 sk->wmem_alloc -= size;
 393                 /* In case it might be waiting for more memory. */
 394                 if (!sk->dead)
 395                         sk->write_space(sk);
 396                 return;
 397         }
 398 }
 399 
 400 
 401 void sock_rfree(struct sock *sk, struct sk_buff *skb, unsigned long size)
     /* [previous][next][first][last][top][bottom][index][help] */
 402 {
 403 #ifdef CONFIG_SKB_CHECK
 404         IS_SKB(skb);
 405 #endif  
 406         kfree_skbmem(skb, size);
 407         if (sk) 
 408         {
 409                 sk->rmem_alloc -= size;
 410         }
 411 }
 412 
 413 /*
 414  *      Generic send/receive buffer handlers
 415  */
 416 
 417 struct sk_buff *sock_alloc_send_skb(struct sock *sk, unsigned long size, int noblock, int *errcode)
     /* [previous][next][first][last][top][bottom][index][help] */
 418 {
 419         struct sk_buff *skb;
 420         int err;
 421 
 422         sk->inuse=1;
 423                 
 424         do
 425         {
 426                 if(sk->err!=0)
 427                 {
 428                         cli();
 429                         err= -sk->err;
 430                         sk->err=0;
 431                         sti();
 432                         *errcode=err;
 433                         return NULL;
 434                 }
 435                 
 436                 if(sk->shutdown&SEND_SHUTDOWN)
 437                 {
 438                         *errcode=-EPIPE;
 439                         return NULL;
 440                 }
 441                 
 442                 skb = sock_wmalloc(sk, size, 0, GFP_KERNEL);
 443                 
 444                 if(skb==NULL)
 445                 {
 446                         unsigned long tmp;
 447                         if(noblock)
 448                         {
 449                                 *errcode=-EAGAIN;
 450                                 return NULL;
 451                         }
 452                         if(sk->shutdown&SEND_SHUTDOWN)
 453                         {
 454                                 *errcode=-EPIPE;
 455                                 return NULL;
 456                         }
 457                         tmp = sk->wmem_alloc;
 458                         cli();
 459                         if(sk->shutdown&SEND_SHUTDOWN)
 460                         {
 461                                 sti();
 462                                 *errcode=-EPIPE;
 463                                 return NULL;
 464                         }
 465                         
 466                         if( tmp <= sk->wmem_alloc)
 467                         {
 468                                 interruptible_sleep_on(sk->sleep);
 469                                 if (current->signal & ~current->blocked) 
 470                                 {
 471                                         sti();
 472                                         *errcode = -ERESTARTSYS;
 473                                         return NULL;
 474                                 }
 475                         }
 476                         sti();
 477                 }
 478         }
 479         while(skb==NULL);
 480                 
 481         return skb;
 482 }
 483 
 484 /*
 485  *      Queue a received datagram if it will fit. Stream and sequenced protocols
 486  *      can't normally use this as they need to fit buffers in and play with them.
 487  */
 488 
 489 int sock_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
     /* [previous][next][first][last][top][bottom][index][help] */
 490 {
 491         if(sk->rmem_alloc + skb->mem_len >= sk->rcvbuf)
 492                 return -ENOMEM;
 493         sk->rmem_alloc+=skb->mem_len;
 494         skb->sk=sk;
 495         skb_queue_tail(&sk->receive_queue,skb);
 496         if(!sk->dead)
 497                 sk->data_ready(sk,skb->len);
 498         return 0;
 499 }
 500 
 501 void release_sock(struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 502 {
 503         struct sk_buff *skb;
 504         unsigned long flags;
 505 
 506         if (!sk->prot)
 507                 return;
 508         /*
 509          *      Make the backlog atomic. If we don't do this there is a tiny
 510          *      window where a packet may arrive between the sk->blog being 
 511          *      tested and then set with sk->inuse still 0 causing an extra 
 512          *      unwanted re-entry into release_sock().
 513          */
 514 
 515         save_flags(flags);
 516         cli();
 517         if (sk->blog) 
 518         {
 519                 restore_flags(flags);
 520                 return;
 521         }
 522         sk->blog=1;
 523         sk->inuse = 1;
 524         restore_flags(flags);
 525 #ifdef CONFIG_INET
 526         /* See if we have any packets built up. */
 527         while((skb = skb_dequeue(&sk->back_log)) != NULL) 
 528         {
 529                 sk->blog = 1;
 530                 if (sk->prot->rcv) 
 531                         sk->prot->rcv(skb, skb->dev, sk->opt,
 532                                  skb->saddr, skb->len, skb->daddr, 1,
 533                                 /* Only used for/by raw sockets. */
 534                                 (struct inet_protocol *)sk->pair); 
 535         }
 536 #endif  
 537         sk->blog = 0;
 538         sk->inuse = 0;
 539 #ifdef CONFIG_INET  
 540         if (sk->dead && sk->state == TCP_CLOSE) 
 541         {
 542                 /* Should be about 2 rtt's */
 543                 reset_timer(sk, TIME_DONE, min(sk->rtt * 2, TCP_DONE_TIME));
 544         }
 545 #endif  
 546 }
 547 
 548 

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