root/net/core/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. 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  *              Alan Cox        :       Make SO_DEBUG superuser only.
  65  *              Alan Cox        :       Allow anyone to clear SO_DEBUG
  66  *                                      (compatibility fix)
  67  *              Alan Cox        :       Added optimistic memory grabbing for AF_UNIX throughput.
  68  *
  69  * To Fix:
  70  *
  71  *
  72  *              This program is free software; you can redistribute it and/or
  73  *              modify it under the terms of the GNU General Public License
  74  *              as published by the Free Software Foundation; either version
  75  *              2 of the License, or (at your option) any later version.
  76  */
  77 
  78 #include <linux/config.h>
  79 #include <linux/errno.h>
  80 #include <linux/types.h>
  81 #include <linux/socket.h>
  82 #include <linux/in.h>
  83 #include <linux/kernel.h>
  84 #include <linux/major.h>
  85 #include <linux/sched.h>
  86 #include <linux/timer.h>
  87 #include <linux/string.h>
  88 #include <linux/sockios.h>
  89 #include <linux/net.h>
  90 #include <linux/fcntl.h>
  91 #include <linux/mm.h>
  92 #include <linux/interrupt.h>
  93 
  94 #include <asm/segment.h>
  95 #include <asm/system.h>
  96 
  97 #include <linux/inet.h>
  98 #include <linux/netdevice.h>
  99 #include <net/ip.h>
 100 #include <net/protocol.h>
 101 #include <net/arp.h>
 102 #include <net/rarp.h>
 103 #include <net/route.h>
 104 #include <net/tcp.h>
 105 #include <net/udp.h>
 106 #include <linux/skbuff.h>
 107 #include <net/sock.h>
 108 #include <net/raw.h>
 109 #include <net/icmp.h>
 110 
 111 #define min(a,b)        ((a)<(b)?(a):(b))
 112 
 113 /*
 114  *      This is meant for all protocols to use and covers goings on
 115  *      at the socket level. Everything here is generic.
 116  */
 117 
 118 int sock_setsockopt(struct sock *sk, int level, int optname,
     /* [previous][next][first][last][top][bottom][index][help] */
 119                 char *optval, int optlen)
 120 {
 121         int val;
 122         int valbool;
 123         int err;
 124         struct linger ling;
 125 
 126         if (optval == NULL) 
 127                 return(-EINVAL);
 128 
 129         err=verify_area(VERIFY_READ, optval, sizeof(int));
 130         if(err)
 131                 return err;
 132         
 133         val = get_user((int *)optval);
 134         valbool = val?1:0;
 135         
 136         switch(optname) 
 137         {
 138                 case SO_DEBUG:  
 139                         if(val && !suser())
 140                                 return(-EPERM);
 141                         sk->debug=valbool;
 142                         return 0;
 143                 case SO_REUSEADDR:
 144                         sk->reuse = valbool;
 145                         return(0);
 146                 case SO_TYPE:
 147                 case SO_ERROR:
 148                         return(-ENOPROTOOPT);
 149                 case SO_DONTROUTE:
 150                         sk->localroute=valbool;
 151                         return 0;
 152                 case SO_BROADCAST:
 153                         sk->broadcast=valbool;
 154                         return 0;
 155                 case SO_SNDBUF:
 156                         if(val>32767)
 157                                 val=32767;
 158                         if(val<256)
 159                                 val=256;
 160                         sk->sndbuf=val;
 161                         return 0;
 162 
 163                 case SO_RCVBUF:
 164                         if(val>32767)
 165                                 val=32767;
 166                         if(val<256)
 167                                 val=256;
 168                         sk->rcvbuf=val;
 169                         return(0);
 170 
 171                 case SO_KEEPALIVE:
 172                         sk->keepopen = valbool;
 173                         return(0);
 174 
 175                 case SO_OOBINLINE:
 176                         sk->urginline = valbool;
 177                         return(0);
 178 
 179                 case SO_NO_CHECK:
 180                         sk->no_check = valbool;
 181                         return(0);
 182 
 183                 case SO_PRIORITY:
 184                         if (val >= 0 && val < DEV_NUMBUFFS) 
 185                         {
 186                                 sk->priority = val;
 187                         } 
 188                         else 
 189                         {
 190                                 return(-EINVAL);
 191                         }
 192                         return(0);
 193 
 194 
 195                 case SO_LINGER:
 196                         err=verify_area(VERIFY_READ,optval,sizeof(ling));
 197                         if(err)
 198                                 return err;
 199                         memcpy_fromfs(&ling,optval,sizeof(ling));
 200                         if(ling.l_onoff==0)
 201                                 sk->linger=0;
 202                         else
 203                         {
 204                                 sk->lingertime=ling.l_linger;
 205                                 sk->linger=1;
 206                         }
 207                         return 0;
 208 
 209 
 210                 default:
 211                         return(-ENOPROTOOPT);
 212         }
 213 }
 214 
 215 
 216 int sock_getsockopt(struct sock *sk, int level, int optname,
     /* [previous][next][first][last][top][bottom][index][help] */
 217                    char *optval, int *optlen)
 218 {               
 219         int val;
 220         int err;
 221         struct linger ling;
 222 
 223         switch(optname) 
 224         {
 225                 case SO_DEBUG:          
 226                         val = sk->debug;
 227                         break;
 228                 
 229                 case SO_DONTROUTE:
 230                         val = sk->localroute;
 231                         break;
 232                 
 233                 case SO_BROADCAST:
 234                         val= sk->broadcast;
 235                         break;
 236 
 237                 case SO_SNDBUF:
 238                         val=sk->sndbuf;
 239                         break;
 240                 
 241                 case SO_RCVBUF:
 242                         val =sk->rcvbuf;
 243                         break;
 244 
 245                 case SO_REUSEADDR:
 246                         val = sk->reuse;
 247                         break;
 248 
 249                 case SO_KEEPALIVE:
 250                         val = sk->keepopen;
 251                         break;
 252 
 253                 case SO_TYPE:
 254                         val = sk->type;                         
 255                         break;
 256 
 257                 case SO_ERROR:
 258                         val = sk->err;
 259                         sk->err = 0;
 260                         break;
 261 
 262                 case SO_OOBINLINE:
 263                         val = sk->urginline;
 264                         break;
 265         
 266                 case SO_NO_CHECK:
 267                         val = sk->no_check;
 268                         break;
 269 
 270                 case SO_PRIORITY:
 271                         val = sk->priority;
 272                         break;
 273                 
 274                 case SO_LINGER: 
 275                         err=verify_area(VERIFY_WRITE,optval,sizeof(ling));
 276                         if(err)
 277                                 return err;
 278                         err=verify_area(VERIFY_WRITE,optlen,sizeof(int));
 279                         if(err)
 280                                 return err;
 281                         put_fs_long(sizeof(ling),(unsigned long *)optlen);
 282                         ling.l_onoff=sk->linger;
 283                         ling.l_linger=sk->lingertime;
 284                         memcpy_tofs(optval,&ling,sizeof(ling));
 285                         return 0;
 286                 
 287 
 288 
 289                 default:
 290                         return(-ENOPROTOOPT);
 291         }
 292         err=verify_area(VERIFY_WRITE, optlen, sizeof(int));
 293         if(err)
 294                 return err;
 295         put_fs_long(sizeof(int),(unsigned long *) optlen);
 296 
 297         err=verify_area(VERIFY_WRITE, optval, sizeof(int));
 298         if(err)
 299                 return err;
 300         put_fs_long(val,(unsigned long *)optval);
 301 
 302         return(0);
 303 }
 304 
 305 
 306 struct sk_buff *sock_wmalloc(struct sock *sk, unsigned long size, int force, int priority)
     /* [previous][next][first][last][top][bottom][index][help] */
 307 {
 308         if (sk) 
 309         {
 310                 if (sk->wmem_alloc + size < sk->sndbuf || force) 
 311                 {
 312                         struct sk_buff * c = alloc_skb(size, priority);
 313                         if (c) 
 314                         {
 315                                 unsigned long flags;
 316                                 save_flags(flags);
 317                                 cli();
 318                                 sk->wmem_alloc+= c->truesize;
 319                                 restore_flags(flags); /* was sti(); */
 320                         }
 321                         return c;
 322                 }
 323                 return(NULL);
 324         }
 325         return(alloc_skb(size, priority));
 326 }
 327 
 328 
 329 struct sk_buff *sock_rmalloc(struct sock *sk, unsigned long size, int force, int priority)
     /* [previous][next][first][last][top][bottom][index][help] */
 330 {
 331         if (sk) 
 332         {
 333                 if (sk->rmem_alloc + size < sk->rcvbuf || force) 
 334                 {
 335                         struct sk_buff *c = alloc_skb(size, priority);
 336                         if (c) 
 337                         {
 338                                 unsigned long flags;
 339                                 save_flags(flags);
 340                                 cli();
 341                                 sk->rmem_alloc += c->truesize;
 342                                 restore_flags(flags); /* was sti(); */
 343                         }
 344                         return(c);
 345                 }
 346                 return(NULL);
 347         }
 348         return(alloc_skb(size, priority));
 349 }
 350 
 351 
 352 unsigned long sock_rspace(struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 353 {
 354         int amt;
 355 
 356         if (sk != NULL) 
 357         {
 358                 if (sk->rmem_alloc >= sk->rcvbuf-2*MIN_WINDOW) 
 359                         return(0);
 360                 amt = min((sk->rcvbuf-sk->rmem_alloc)/2-MIN_WINDOW, MAX_WINDOW);
 361                 if (amt < 0) 
 362                         return(0);
 363                 return(amt);
 364         }
 365         return(0);
 366 }
 367 
 368 
 369 unsigned long sock_wspace(struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 370 {
 371         if (sk != NULL) 
 372         {
 373                 if (sk->shutdown & SEND_SHUTDOWN)
 374                         return(0);
 375                 if (sk->wmem_alloc >= sk->sndbuf)
 376                         return(0);
 377                 return(sk->sndbuf-sk->wmem_alloc );
 378         }
 379         return(0);
 380 }
 381 
 382 
 383 void sock_wfree(struct sock *sk, struct sk_buff *skb)
     /* [previous][next][first][last][top][bottom][index][help] */
 384 {
 385         int s=skb->truesize;
 386 #if CONFIG_SKB_CHECK
 387         IS_SKB(skb);
 388 #endif
 389         kfree_skbmem(skb);
 390         if (sk) 
 391         {
 392                 unsigned long flags;
 393                 save_flags(flags);
 394                 cli();
 395                 sk->wmem_alloc -= s;
 396                 restore_flags(flags);
 397                 /* In case it might be waiting for more memory. */
 398                 sk->write_space(sk);
 399                 return;
 400         }
 401 }
 402 
 403 
 404 void sock_rfree(struct sock *sk, struct sk_buff *skb)
     /* [previous][next][first][last][top][bottom][index][help] */
 405 {
 406         int s=skb->truesize;
 407 #if CONFIG_SKB_CHECK
 408         IS_SKB(skb);
 409 #endif  
 410         kfree_skbmem(skb);
 411         if (sk) 
 412         {
 413                 unsigned long flags;
 414                 save_flags(flags);
 415                 cli();
 416                 sk->rmem_alloc -= s;
 417                 restore_flags(flags);
 418         }
 419 }
 420 
 421 /*
 422  *      Generic send/receive buffer handlers
 423  */
 424 
 425 struct sk_buff *sock_alloc_send_skb(struct sock *sk, unsigned long size, unsigned long fallback, int noblock, int *errcode)
     /* [previous][next][first][last][top][bottom][index][help] */
 426 {
 427         struct sk_buff *skb;
 428         int err;
 429 
 430         sk->inuse=1;
 431                 
 432         do
 433         {
 434                 if(sk->err!=0)
 435                 {
 436                         cli();
 437                         err= -sk->err;
 438                         sk->err=0;
 439                         sti();
 440                         *errcode=err;
 441                         return NULL;
 442                 }
 443                 
 444                 if(sk->shutdown&SEND_SHUTDOWN)
 445                 {
 446                         *errcode=-EPIPE;
 447                         return NULL;
 448                 }
 449                 
 450                 if(!fallback)
 451                         skb = sock_wmalloc(sk, size, 0, GFP_KERNEL);
 452                 else
 453                 {
 454                         /* The buffer get won't block, or use the atomic queue. It does
 455                            produce annoying no free page messages still.... */
 456                         skb = sock_wmalloc(sk, size, 0 , GFP_BUFFER);
 457                         if(!skb)
 458                                 skb=sock_wmalloc(sk, fallback, 0, GFP_KERNEL);
 459                 }
 460                 
 461                 /*
 462                  *      This means we have too many buffers for this socket already.
 463                  */
 464                  
 465                 if(skb==NULL)
 466                 {
 467                         unsigned long tmp;
 468 
 469                         sk->socket->flags |= SO_NOSPACE;
 470                         if(noblock)
 471                         {
 472                                 *errcode=-EAGAIN;
 473                                 return NULL;
 474                         }
 475                         if(sk->shutdown&SEND_SHUTDOWN)
 476                         {
 477                                 *errcode=-EPIPE;
 478                                 return NULL;
 479                         }
 480                         tmp = sk->wmem_alloc;
 481                         cli();
 482                         if(sk->shutdown&SEND_SHUTDOWN)
 483                         {
 484                                 sti();
 485                                 *errcode=-EPIPE;
 486                                 return NULL;
 487                         }
 488                         
 489                         if( tmp <= sk->wmem_alloc)
 490                         {
 491                                 sk->socket->flags &= ~SO_NOSPACE;
 492                                 interruptible_sleep_on(sk->sleep);
 493                                 if (current->signal & ~current->blocked) 
 494                                 {
 495                                         sti();
 496                                         *errcode = -ERESTARTSYS;
 497                                         return NULL;
 498                                 }
 499                         }
 500                         sti();
 501                 }
 502         }
 503         while(skb==NULL);
 504                 
 505         return skb;
 506 }
 507 
 508 
 509 void release_sock(struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 510 {
 511         unsigned long flags;
 512 #ifdef CONFIG_INET
 513         struct sk_buff *skb;
 514 #endif
 515 
 516         if (!sk->prot)
 517                 return;
 518         /*
 519          *      Make the backlog atomic. If we don't do this there is a tiny
 520          *      window where a packet may arrive between the sk->blog being 
 521          *      tested and then set with sk->inuse still 0 causing an extra 
 522          *      unwanted re-entry into release_sock().
 523          */
 524 
 525         save_flags(flags);
 526         cli();
 527         if (sk->blog) 
 528         {
 529                 restore_flags(flags);
 530                 return;
 531         }
 532         sk->blog=1;
 533         sk->inuse = 1;
 534         restore_flags(flags);
 535 #ifdef CONFIG_INET
 536         /* See if we have any packets built up. */
 537         while((skb = skb_dequeue(&sk->back_log)) != NULL) 
 538         {
 539                 sk->blog = 1;
 540                 if (sk->prot->rcv) 
 541                         sk->prot->rcv(skb, skb->dev, sk->opt,
 542                                  skb->saddr, skb->len, skb->daddr, 1,
 543                                 /* Only used for/by raw sockets. */
 544                                 (struct inet_protocol *)sk->pair); 
 545         }
 546 #endif  
 547         sk->blog = 0;
 548         sk->inuse = 0;
 549 #ifdef CONFIG_INET  
 550         if (sk->dead && sk->state == TCP_CLOSE) 
 551         {
 552                 /* Should be about 2 rtt's */
 553                 reset_timer(sk, TIME_DONE, min(sk->rtt * 2, TCP_DONE_TIME));
 554         }
 555 #endif  
 556 }
 557 
 558 

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