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

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