root/net/inet/sock.c

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

DEFINITIONS

This source file includes following definitions.
  1. print_sk
  2. print_skb
  3. sk_inuse
  4. get_new_socknum
  5. put_sock
  6. remove_sock
  7. destroy_sock
  8. inet_fcntl
  9. inet_setsockopt
  10. inet_getsockopt
  11. sock_setsockopt
  12. sock_getsockopt
  13. inet_listen
  14. def_callback1
  15. def_callback2
  16. inet_create
  17. inet_dup
  18. inet_release
  19. inet_bind
  20. inet_connect
  21. inet_socketpair
  22. inet_accept
  23. inet_getname
  24. inet_read
  25. inet_recv
  26. inet_write
  27. inet_send
  28. inet_sendto
  29. inet_recvfrom
  30. inet_shutdown
  31. inet_select
  32. inet_ioctl
  33. sock_wmalloc
  34. sock_rmalloc
  35. sock_rspace
  36. sock_wspace
  37. sock_wfree
  38. sock_rfree
  39. get_sock
  40. release_sock
  41. inet_fioctl
  42. inet_proto_init

   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  *              SOCK - AF_INET protocol family socket handler.
   7  *
   8  * Version:     @(#)sock.c      1.0.17  06/02/93
   9  *
  10  * Authors:     Ross Biro, <bir7@leland.Stanford.Edu>
  11  *              Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  12  *              Florian La Roche, <flla@stud.uni-sb.de>
  13  *
  14  * Fixes:
  15  *              Alan Cox        :       Numerous verify_area() problems
  16  *              Alan Cox        :       Connecting on a connecting socket
  17  *                                      now returns an error for tcp.
  18  *              Alan Cox        :       sock->protocol is set correctly.
  19  *                                      and is not sometimes left as 0.
  20  *              Alan Cox        :       connect handles icmp errors on a
  21  *                                      connect properly. Unfortunately there
  22  *                                      is a restart syscall nasty there. I
  23  *                                      can't match BSD without hacking the C
  24  *                                      library. Ideas urgently sought!
  25  *              Alan Cox        :       Disallow bind() to addresses that are
  26  *                                      not ours - especially broadcast ones!!
  27  *              Alan Cox        :       Socket 1024 _IS_ ok for users. (fencepost)
  28  *              Alan Cox        :       sock_wfree/sock_rfree don't destroy sockets,
  29  *                                      instead they leave that for the DESTROY timer.
  30  *              Alan Cox        :       Clean up error flag in accept
  31  *              Alan Cox        :       TCP ack handling is buggy, the DESTROY timer
  32  *                                      was buggy. Put a remove_sock() in the handler
  33  *                                      for memory when we hit 0. Also altered the timer
  34  *                                      code. The ACK stuff can wait and needs major 
  35  *                                      TCP layer surgery.
  36  *              Alan Cox        :       Fixed TCP ack bug, removed remove sock
  37  *                                      and fixed timer/inet_bh race.
  38  *              Alan Cox        :       Added zapped flag for TCP
  39  *              Alan Cox        :       Move kfree_skb into skbuff.c and tidied up surplus code
  40  *              Alan Cox        :       for new sk_buff allocations wmalloc/rmalloc now call alloc_skb
  41  *              Alan Cox        :       kfree_s calls now are kfree_skbmem so we can track skb resources
  42  *              Alan Cox        :       Supports socket option broadcast now as does udp. Packet and raw need fixing.
  43  *              Alan Cox        :       Added RCVBUF,SNDBUF size setting. It suddenely occured to me how easy it was so...
  44  *              Rick Sladkey    :       Relaxed UDP rules for matching packets.
  45  *              C.E.Hawkins     :       IFF_PROMISC/SIOCGHWADDR support
  46  *      Pauline Middelink       :       Pidentd support
  47  *              Alan Cox        :       Fixed connect() taking signals I think.
  48  *              Alan Cox        :       SO_LINGER supported
  49  *              Alan Cox        :       Error reporting fixes
  50  *              Anonymous       :       inet_create tidied up (sk->reuse setting)
  51  *              Alan Cox        :       inet sockets don't set sk->type!
  52  *              Alan Cox        :       Split socket option code
  53  *              Alan Cox        :       Callbacks
  54  *
  55  * To Fix:
  56  *
  57  *
  58  *              This program is free software; you can redistribute it and/or
  59  *              modify it under the terms of the GNU General Public License
  60  *              as published by the Free Software Foundation; either version
  61  *              2 of the License, or (at your option) any later version.
  62  */
  63 
  64 #include <linux/config.h>
  65 #include <linux/errno.h>
  66 #include <linux/types.h>
  67 #include <linux/socket.h>
  68 #include <linux/in.h>
  69 #include <linux/kernel.h>
  70 #include <linux/major.h>
  71 #include <linux/sched.h>
  72 #include <linux/timer.h>
  73 #include <linux/string.h>
  74 #include <linux/sockios.h>
  75 #include <linux/net.h>
  76 #include <linux/fcntl.h>
  77 #include <linux/mm.h>
  78 #include <linux/interrupt.h>
  79 
  80 #include <asm/segment.h>
  81 #include <asm/system.h>
  82 
  83 #include "inet.h"
  84 #include "dev.h"
  85 #include "ip.h"
  86 #include "protocol.h"
  87 #include "arp.h"
  88 #include "route.h"
  89 #include "tcp.h"
  90 #include "udp.h"
  91 #include "skbuff.h"
  92 #include "sock.h"
  93 #include "raw.h"
  94 #include "icmp.h"
  95 
  96 
  97 int inet_debug = DBG_OFF;               /* INET module debug flag       */
  98 
  99 
 100 #define min(a,b)        ((a)<(b)?(a):(b))
 101 
 102 extern struct proto packet_prot;
 103 
 104 
 105 void
 106 print_sk(struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 107 {
 108   if (!sk) {
 109         printk("  print_sk(NULL)\n");
 110         return;
 111   }
 112   printk("  wmem_alloc = %lu\n", sk->wmem_alloc);
 113   printk("  rmem_alloc = %lu\n", sk->rmem_alloc);
 114   printk("  send_head = %p\n", sk->send_head);
 115   printk("  state = %d\n",sk->state);
 116   printk("  wback = %p, rqueue = %p\n", sk->wback, sk->rqueue);
 117   printk("  wfront = %p\n", sk->wfront);
 118   printk("  daddr = %lX, saddr = %lX\n", sk->daddr,sk->saddr);
 119   printk("  num = %d", sk->num);
 120   printk(" next = %p\n", sk->next);
 121   printk("  send_seq = %ld, acked_seq = %ld, copied_seq = %ld\n",
 122           sk->send_seq, sk->acked_seq, sk->copied_seq);
 123   printk("  rcv_ack_seq = %ld, window_seq = %ld, fin_seq = %ld\n",
 124           sk->rcv_ack_seq, sk->window_seq, sk->fin_seq);
 125   printk("  prot = %p\n", sk->prot);
 126   printk("  pair = %p, back_log = %p\n", sk->pair,sk->back_log);
 127   printk("  inuse = %d , blog = %d\n", sk->inuse, sk->blog);
 128   printk("  dead = %d delay_acks=%d\n", sk->dead, sk->delay_acks);
 129   printk("  retransmits = %ld, timeout = %d\n", sk->retransmits, sk->timeout);
 130   printk("  cong_window = %d, packets_out = %d\n", sk->cong_window,
 131           sk->packets_out);
 132   printk("  urg = %d shutdown=%d\n", sk->urg, sk->shutdown);
 133 }
 134 
 135 
 136 void
 137 print_skb(struct sk_buff *skb)
     /* [previous][next][first][last][top][bottom][index][help] */
 138 {
 139   if (!skb) {
 140         printk("  print_skb(NULL)\n");
 141         return;
 142   }
 143   printk("  prev = %p, next = %p\n", skb->prev, skb->next);
 144   printk("  sk = %p link3 = %p\n", skb->sk, skb->link3);
 145   printk("  mem_addr = %p, mem_len = %lu\n", skb->mem_addr, skb->mem_len);
 146   printk("  used = %d free = %d\n", skb->used,skb->free);
 147 }
 148 
 149 
 150 
 151 static int
 152 sk_inuse(struct proto *prot, int num)
     /* [previous][next][first][last][top][bottom][index][help] */
 153 {
 154   struct sock *sk;
 155 
 156   for(sk = prot->sock_array[num & (SOCK_ARRAY_SIZE -1 )];
 157       sk != NULL;
 158       sk=sk->next) {
 159         if (sk->num == num) return(1);
 160   }
 161   return(0);
 162 }
 163 
 164 
 165 unsigned short
 166 get_new_socknum(struct proto *prot, unsigned short base)
     /* [previous][next][first][last][top][bottom][index][help] */
 167 {
 168   static int start=0;
 169 
 170   /*
 171    * Used to cycle through the port numbers so the
 172    * chances of a confused connection drop.
 173    */
 174   int i, j;
 175   int best = 0;
 176   int size = 32767; /* a big num. */
 177   struct sock *sk;
 178 
 179   if (base == 0) base = PROT_SOCK+1+(start % 1024);
 180   if (base <= PROT_SOCK) {
 181         base += PROT_SOCK+(start % 1024);
 182   }
 183 
 184   /* Now look through the entire array and try to find an empty ptr. */
 185   for(i=0; i < SOCK_ARRAY_SIZE; i++) {
 186         j = 0;
 187         sk = prot->sock_array[(i+base+1) &(SOCK_ARRAY_SIZE -1)];
 188         while(sk != NULL) {
 189                 sk = sk->next;
 190                 j++;
 191         }
 192         if (j == 0) {
 193                 start =(i+1+start )%1024;
 194                 DPRINTF((DBG_INET, "get_new_socknum returning %d, start = %d\n",
 195                                                         i + base + 1, start));
 196                 return(i+base+1);
 197         }
 198         if (j < size) {
 199                 best = i;
 200                 size = j;
 201         }
 202   }
 203 
 204   /* Now make sure the one we want is not in use. */
 205   while(sk_inuse(prot, base +best+1)) {
 206         best += SOCK_ARRAY_SIZE;
 207   }
 208   DPRINTF((DBG_INET, "get_new_socknum returning %d, start = %d\n",
 209                                                 best + base + 1, start));
 210   return(best+base+1);
 211 }
 212 
 213 
 214 void
 215 put_sock(unsigned short num, struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 216 {
 217   struct sock *sk1;
 218   struct sock *sk2;
 219   int mask;
 220 
 221   DPRINTF((DBG_INET, "put_sock(num = %d, sk = %X\n", num, sk));
 222   sk->num = num;
 223   sk->next = NULL;
 224   num = num &(SOCK_ARRAY_SIZE -1);
 225 
 226   /* We can't have an interupt re-enter here. */
 227   cli();
 228   if (sk->prot->sock_array[num] == NULL) {
 229         sk->prot->sock_array[num] = sk;
 230         sti();
 231         return;
 232   }
 233   sti();
 234   for(mask = 0xff000000; mask != 0xffffffff; mask = (mask >> 8) | mask) {
 235         if ((mask & sk->saddr) &&
 236             (mask & sk->saddr) != (mask & 0xffffffff)) {
 237                 mask = mask << 8;
 238                 break;
 239         }
 240   }
 241   DPRINTF((DBG_INET, "mask = %X\n", mask));
 242 
 243   cli();
 244   sk1 = sk->prot->sock_array[num];
 245   for(sk2 = sk1; sk2 != NULL; sk2=sk2->next) {
 246         if (!(sk2->saddr & mask)) {
 247                 if (sk2 == sk1) {
 248                         sk->next = sk->prot->sock_array[num];
 249                         sk->prot->sock_array[num] = sk;
 250                         sti();
 251                         return;
 252                 }
 253                 sk->next = sk2;
 254                 sk1->next= sk;
 255                 sti();
 256                 return;
 257         }
 258         sk1 = sk2;
 259   }
 260 
 261   /* Goes at the end. */
 262   sk->next = NULL;
 263   sk1->next = sk;
 264   sti();
 265 }
 266 
 267 
 268 static void
 269 remove_sock(struct sock *sk1)
     /* [previous][next][first][last][top][bottom][index][help] */
 270 {
 271   struct sock *sk2;
 272 
 273   DPRINTF((DBG_INET, "remove_sock(sk1=%X)\n", sk1));
 274   if (!sk1) {
 275         printk("sock.c: remove_sock: sk1 == NULL\n");
 276         return;
 277   }
 278 
 279   if (!sk1->prot) {
 280         printk("sock.c: remove_sock: sk1->prot == NULL\n");
 281         return;
 282   }
 283 
 284   /* We can't have this changing out from under us. */
 285   cli();
 286   sk2 = sk1->prot->sock_array[sk1->num &(SOCK_ARRAY_SIZE -1)];
 287   if (sk2 == sk1) {
 288         sk1->prot->sock_array[sk1->num &(SOCK_ARRAY_SIZE -1)] = sk1->next;
 289         sti();
 290         return;
 291   }
 292 
 293   while(sk2 && sk2->next != sk1) {
 294         sk2 = sk2->next;
 295   }
 296 
 297   if (sk2) {
 298         sk2->next = sk1->next;
 299         sti();
 300         return;
 301   }
 302   sti();
 303 
 304   if (sk1->num != 0) DPRINTF((DBG_INET, "remove_sock: sock not found.\n"));
 305 }
 306 
 307 
 308 void
 309 destroy_sock(struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 310 {
 311         struct sk_buff *skb;
 312 
 313         DPRINTF((DBG_INET, "destroying socket %X\n", sk));
 314         sk->inuse = 1;                  /* just to be safe. */
 315 
 316         /* Incase it's sleeping somewhere. */
 317         if (!sk->dead) 
 318                 sk->write_space(sk);
 319 
 320         remove_sock(sk);
 321   
 322         /* Now we can no longer get new packets. */
 323         delete_timer(sk);
 324 
 325 
 326         if (sk->send_tmp != NULL) 
 327         {
 328                 IS_SKB(sk->send_tmp);
 329                 kfree_skb(sk->send_tmp, FREE_WRITE);
 330         }
 331 
 332   /* Cleanup up the write buffer. */
 333         for(skb = sk->wfront; skb != NULL; ) 
 334         {
 335                 struct sk_buff *skb2;
 336 
 337                 skb2=(struct sk_buff *)skb->next;
 338                 if (skb->magic != TCP_WRITE_QUEUE_MAGIC) {
 339                         printk("sock.c:destroy_sock write queue with bad magic(%X)\n",
 340                                                                 skb->magic);
 341                         break;
 342                 }
 343                 IS_SKB(skb);
 344                 kfree_skb(skb, FREE_WRITE);
 345                 skb = skb2;
 346         }
 347 
 348         sk->wfront = NULL;
 349         sk->wback = NULL;
 350 
 351         if (sk->rqueue != NULL) 
 352         {
 353                 while((skb=skb_dequeue(&sk->rqueue))!=NULL)
 354                 {
 355                 /*
 356                  * This will take care of closing sockets that were
 357                  * listening and didn't accept everything.
 358                  */
 359                         if (skb->sk != NULL && skb->sk != sk) 
 360                         {
 361                                 IS_SKB(skb);
 362                                 skb->sk->dead = 1;
 363                                 skb->sk->prot->close(skb->sk, 0);
 364                         }
 365                         IS_SKB(skb);
 366                         kfree_skb(skb, FREE_READ);
 367                 }
 368         }
 369         sk->rqueue = NULL;
 370 
 371   /* Now we need to clean up the send head. */
 372         for(skb = sk->send_head; skb != NULL; ) 
 373         {
 374                 struct sk_buff *skb2;
 375 
 376                 /*
 377                  * We need to remove skb from the transmit queue,
 378                  * or maybe the arp queue.
 379                  */
 380                 cli();
 381                 /* see if it's in a transmit queue. */
 382                 /* this can be simplified quite a bit.  Look */
 383                 /* at tcp.c:tcp_ack to see how. */
 384                 if (skb->next != NULL) 
 385                 {
 386                         IS_SKB(skb);
 387                         skb_unlink(skb);
 388                 }
 389                 skb->dev = NULL;
 390                 sti();
 391                 skb2 = (struct sk_buff *)skb->link3;
 392                 kfree_skb(skb, FREE_WRITE);
 393                 skb = skb2;
 394         }       
 395         sk->send_head = NULL;
 396 
 397         /* And now the backlog. */
 398         if (sk->back_log != NULL) 
 399         {
 400                 /* this should never happen. */
 401                 printk("cleaning back_log. \n");
 402                 cli();
 403                 skb = (struct sk_buff *)sk->back_log;
 404                 do 
 405                 {
 406                         struct sk_buff *skb2;
 407         
 408                         skb2 = (struct sk_buff *)skb->next;
 409                         kfree_skb(skb, FREE_READ);
 410                         skb = skb2;
 411                 }
 412                 while(skb != sk->back_log);
 413                 sti();
 414         }
 415         sk->back_log = NULL;
 416 
 417   /* Now if it has a half accepted/ closed socket. */
 418         if (sk->pair) 
 419         {
 420                 sk->pair->dead = 1;
 421                 sk->pair->prot->close(sk->pair, 0);
 422                 sk->pair = NULL;
 423         }
 424 
 425   /*
 426    * Now if everything is gone we can free the socket
 427    * structure, otherwise we need to keep it around until
 428    * everything is gone.
 429    */
 430           if (sk->rmem_alloc == 0 && sk->wmem_alloc == 0) 
 431           {
 432                 kfree_s((void *)sk,sizeof(*sk));
 433           } 
 434           else 
 435           {
 436                 /* this should never happen. */
 437                 /* actually it can if an ack has just been sent. */
 438                 DPRINTF((DBG_INET, "possible memory leak in socket = %X\n", sk));
 439                 sk->destroy = 1;
 440                 sk->ack_backlog = 0;
 441                 sk->inuse = 0;
 442                 reset_timer(sk, TIME_DESTROY, SOCK_DESTROY_TIME);
 443         }
 444         DPRINTF((DBG_INET, "leaving destroy_sock\n"));
 445 }
 446 
 447 
 448 static int
 449 inet_fcntl(struct socket *sock, unsigned int cmd, unsigned long arg)
     /* [previous][next][first][last][top][bottom][index][help] */
 450 {
 451   struct sock *sk;
 452 
 453   sk = (struct sock *) sock->data;
 454   if (sk == NULL) {
 455         printk("Warning: sock->data = NULL: %d\n" ,__LINE__);
 456         return(0);
 457   }
 458 
 459   switch(cmd) {
 460         case F_SETOWN:
 461                 /*
 462                  * This is a little restrictive, but it's the only
 463                  * way to make sure that you can't send a sigurg to
 464                  * another process.
 465                  */
 466                 if (!suser() && current->pgrp != -arg &&
 467                                 current->pid != arg) return(-EPERM);
 468                 sk->proc = arg;
 469                 return(0);
 470         case F_GETOWN:
 471                 return(sk->proc);
 472         default:
 473                 return(-EINVAL);
 474   }
 475 }
 476 
 477 /*
 478  *      Set socket options on an inet socket.
 479  */
 480  
 481 static int inet_setsockopt(struct socket *sock, int level, int optname,
     /* [previous][next][first][last][top][bottom][index][help] */
 482                     char *optval, int optlen)
 483 {
 484         struct sock *sk = (struct sock *) sock->data;  
 485         if (level == SOL_SOCKET)
 486                 return sock_setsockopt(sk,level,optname,optval,optlen);
 487         if (sk->prot->setsockopt==NULL)
 488                 return(-EOPNOTSUPP);
 489         else
 490                 return sk->prot->setsockopt(sk,level,optname,optval,optlen);
 491 }
 492 
 493 
 494 
 495 
 496 static int inet_getsockopt(struct socket *sock, int level, int optname,
     /* [previous][next][first][last][top][bottom][index][help] */
 497                     char *optval, int *optlen)
 498 {
 499         struct sock *sk = sock->data;   
 500         if (level == SOL_SOCKET) 
 501                 return sock_getsockopt(sk,level,optname,optval,optlen);
 502         if(sk->prot->getsockopt==NULL)          
 503                 return(-EOPNOTSUPP);
 504         else
 505                 return sk->prot->getsockopt(sk,level,optname,optval,optlen);
 506 }
 507 
 508 /*
 509  *      This is meant for all protocols to use and covers goings on
 510  *      at the socket level. Everything here is generic.
 511  */
 512 
 513 int sock_setsockopt(struct sock *sk, int level, int optname,
     /* [previous][next][first][last][top][bottom][index][help] */
 514                 char *optval, int optlen)
 515 {
 516         int val;
 517         int err;
 518         struct linger ling;
 519 
 520         if (optval == NULL) 
 521                 return(-EINVAL);
 522 
 523         err=verify_area(VERIFY_READ, optval, sizeof(int));
 524         if(err)
 525                 return err;
 526         
 527         val = get_fs_long((unsigned long *)optval);
 528         switch(optname) 
 529         {
 530                 case SO_TYPE:
 531                 case SO_ERROR:
 532                         return(-ENOPROTOOPT);
 533 
 534                 case SO_DEBUG:  
 535                         sk->debug=val?1:0;
 536                 case SO_DONTROUTE:      /* Still to be implemented */
 537                         return(0);
 538                 case SO_BROADCAST:
 539                         sk->broadcast=val?1:0;
 540                         return 0;
 541                 case SO_SNDBUF:
 542                         if(val>32767)
 543                                 val=32767;
 544                         if(val<256)
 545                                 val=256;
 546                         sk->sndbuf=val;
 547                         return 0;
 548                 case SO_LINGER:
 549                         err=verify_area(VERIFY_READ,optval,sizeof(ling));
 550                         if(err)
 551                                 return err;
 552                         memcpy_fromfs(&ling,optval,sizeof(ling));
 553                         if(ling.l_onoff==0)
 554                                 sk->linger=0;
 555                         else
 556                         {
 557                                 sk->lingertime=ling.l_linger;
 558                                 sk->linger=1;
 559                         }
 560                         return 0;
 561                 case SO_RCVBUF:
 562                         if(val>32767)
 563                                 val=32767;
 564                         if(val<256)
 565                                 val=256;
 566                         sk->rcvbuf=val;
 567                         return(0);
 568 
 569                 case SO_REUSEADDR:
 570                         if (val) 
 571                                 sk->reuse = 1;
 572                         else 
 573                                 sk->reuse = 0;
 574                         return(0);
 575 
 576                 case SO_KEEPALIVE:
 577                         if (val)
 578                                 sk->keepopen = 1;
 579                         else 
 580                                 sk->keepopen = 0;
 581                         return(0);
 582 
 583                 case SO_OOBINLINE:
 584                         if (val) 
 585                                 sk->urginline = 1;
 586                         else 
 587                                 sk->urginline = 0;
 588                         return(0);
 589 
 590                 case SO_NO_CHECK:
 591                         if (val) 
 592                                 sk->no_check = 1;
 593                         else 
 594                                 sk->no_check = 0;
 595                         return(0);
 596 
 597                  case SO_PRIORITY:
 598                         if (val >= 0 && val < DEV_NUMBUFFS) 
 599                         {
 600                                 sk->priority = val;
 601                         } 
 602                         else 
 603                         {
 604                                 return(-EINVAL);
 605                         }
 606                         return(0);
 607 
 608                 default:
 609                         return(-ENOPROTOOPT);
 610         }
 611 }
 612 
 613 
 614 int sock_getsockopt(struct sock *sk, int level, int optname,
     /* [previous][next][first][last][top][bottom][index][help] */
 615                    char *optval, int *optlen)
 616 {               
 617         int val;
 618         int err;
 619         struct linger ling;
 620 
 621         switch(optname) 
 622         {
 623                 case SO_DEBUG:          
 624                         val = sk->debug;
 625                         break;
 626                 
 627                 case SO_DONTROUTE:      /* One last option to implement */
 628                         val = 0;
 629                         break;
 630                 
 631                 case SO_BROADCAST:
 632                         val= sk->broadcast;
 633                         break;
 634                 
 635                 case SO_LINGER: 
 636                         err=verify_area(VERIFY_WRITE,optval,sizeof(ling));
 637                         if(err)
 638                                 return err;
 639                         err=verify_area(VERIFY_WRITE,optlen,sizeof(int));
 640                         if(err)
 641                                 return err;
 642                         put_fs_long(sizeof(ling),(unsigned long *)optlen);
 643                         ling.l_onoff=sk->linger;
 644                         ling.l_linger=sk->lingertime;
 645                         memcpy_tofs(optval,&ling,sizeof(ling));
 646                         return 0;
 647                 
 648                 case SO_SNDBUF:
 649                         val=sk->sndbuf;
 650                         break;
 651                 
 652                 case SO_RCVBUF:
 653                         val =sk->rcvbuf;
 654                         break;
 655 
 656                 case SO_REUSEADDR:
 657                         val = sk->reuse;
 658                         break;
 659 
 660                 case SO_KEEPALIVE:
 661                         val = sk->keepopen;
 662                         break;
 663 
 664                 case SO_TYPE:
 665                         if (sk->prot == &tcp_prot) 
 666                                 val = SOCK_STREAM;
 667                         else 
 668                                 val = SOCK_DGRAM;
 669                         break;
 670 
 671                 case SO_ERROR:
 672                         val = sk->err;
 673                         sk->err = 0;
 674                         break;
 675 
 676                 case SO_OOBINLINE:
 677                         val = sk->urginline;
 678                         break;
 679         
 680                 case SO_NO_CHECK:
 681                         val = sk->no_check;
 682                         break;
 683 
 684                 case SO_PRIORITY:
 685                         val = sk->priority;
 686                         break;
 687 
 688                 default:
 689                         return(-ENOPROTOOPT);
 690         }
 691         err=verify_area(VERIFY_WRITE, optlen, sizeof(int));
 692         if(err)
 693                 return err;
 694         put_fs_long(sizeof(int),(unsigned long *) optlen);
 695 
 696         err=verify_area(VERIFY_WRITE, optval, sizeof(int));
 697         if(err)
 698                 return err;
 699         put_fs_long(val,(unsigned long *)optval);
 700 
 701         return(0);
 702 }
 703 
 704 
 705 
 706 
 707 static int
 708 inet_listen(struct socket *sock, int backlog)
     /* [previous][next][first][last][top][bottom][index][help] */
 709 {
 710   struct sock *sk;
 711 
 712   sk = (struct sock *) sock->data;
 713   if (sk == NULL) {
 714         printk("Warning: sock->data = NULL: %d\n" ,__LINE__);
 715         return(0);
 716   }
 717 
 718   /* We may need to bind the socket. */
 719   if (sk->num == 0) {
 720         sk->num = get_new_socknum(sk->prot, 0);
 721         if (sk->num == 0) return(-EAGAIN);
 722         put_sock(sk->num, sk);
 723         sk->dummy_th.source = ntohs(sk->num);
 724   }
 725 
 726   /* We might as well re use these. */ 
 727   sk->max_ack_backlog = backlog;
 728   if (sk->state != TCP_LISTEN) {
 729         sk->ack_backlog = 0;
 730         sk->state = TCP_LISTEN;
 731   }
 732   return(0);
 733 }
 734 
 735 /*
 736  *      Default callbacks for user INET sockets. These just wake up
 737  *      the user owning the socket.
 738  */
 739 
 740 static void def_callback1(struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 741 {
 742         if(!sk->dead)
 743                 wake_up(sk->sleep);
 744 }
 745 
 746 static void def_callback2(struct sock *sk,int len)
     /* [previous][next][first][last][top][bottom][index][help] */
 747 {
 748         if(!sk->dead)
 749                 wake_up(sk->sleep);
 750 }
 751 
 752 
 753 static int
 754 inet_create(struct socket *sock, int protocol)
     /* [previous][next][first][last][top][bottom][index][help] */
 755 {
 756   struct sock *sk;
 757   struct proto *prot;
 758   int err;
 759 
 760   sk = (struct sock *) kmalloc(sizeof(*sk), GFP_KERNEL);
 761   if (sk == NULL) 
 762         return(-ENOMEM);
 763   sk->num = 0;
 764   sk->reuse = 0;
 765   switch(sock->type) {
 766         case SOCK_STREAM:
 767         case SOCK_SEQPACKET:
 768                 if (protocol && protocol != IPPROTO_TCP) {
 769                         kfree_s((void *)sk, sizeof(*sk));
 770                         return(-EPROTONOSUPPORT);
 771                 }
 772                 protocol = IPPROTO_TCP;
 773                 sk->no_check = TCP_NO_CHECK;
 774                 prot = &tcp_prot;
 775                 break;
 776 
 777         case SOCK_DGRAM:
 778                 if (protocol && protocol != IPPROTO_UDP) {
 779                         kfree_s((void *)sk, sizeof(*sk));
 780                         return(-EPROTONOSUPPORT);
 781                 }
 782                 protocol = IPPROTO_UDP;
 783                 sk->no_check = UDP_NO_CHECK;
 784                 prot=&udp_prot;
 785                 break;
 786       
 787         case SOCK_RAW:
 788                 if (!suser()) {
 789                         kfree_s((void *)sk, sizeof(*sk));
 790                         return(-EPERM);
 791                 }
 792                 if (!protocol) {
 793                         kfree_s((void *)sk, sizeof(*sk));
 794                         return(-EPROTONOSUPPORT);
 795                 }
 796                 prot = &raw_prot;
 797                 sk->reuse = 1;
 798                 sk->no_check = 0;       /*
 799                                          * Doesn't matter no checksum is
 800                                          * preformed anyway.
 801                                          */
 802                 sk->num = protocol;
 803                 break;
 804 
 805         case SOCK_PACKET:
 806                 if (!suser()) {
 807                         kfree_s((void *)sk, sizeof(*sk));
 808                         return(-EPERM);
 809                 }
 810                 if (!protocol) {
 811                         kfree_s((void *)sk, sizeof(*sk));
 812                         return(-EPROTONOSUPPORT);
 813                 }
 814                 prot = &packet_prot;
 815                 sk->reuse = 1;
 816                 sk->no_check = 0;       /* Doesn't matter no checksum is
 817                                          * preformed anyway.
 818                                          */
 819                 sk->num = protocol;
 820                 break;
 821 
 822         default:
 823                 kfree_s((void *)sk, sizeof(*sk));
 824                 return(-ESOCKTNOSUPPORT);
 825   }
 826   sk->socket = sock;
 827   sk->type = sock->type;
 828   sk->protocol = protocol;
 829   sk->wmem_alloc = 0;
 830   sk->rmem_alloc = 0;
 831   sk->sndbuf = SK_WMEM_MAX;
 832   sk->rcvbuf = SK_RMEM_MAX;
 833   sk->pair = NULL;
 834   sk->opt = NULL;
 835   sk->send_seq = 0;
 836   sk->acked_seq = 0;
 837   sk->copied_seq = 0;
 838   sk->fin_seq = 0;
 839   sk->proc = 0;
 840   sk->rtt = TCP_WRITE_TIME;
 841   sk->mdev = 0;
 842   sk->backoff = 0;
 843   sk->packets_out = 0;
 844   sk->cong_window = 1; /* start with only sending one packet at a time. */
 845   sk->exp_growth = 1;  /* if set cong_window grow exponentially every time
 846                           we get an ack. */
 847   sk->urginline = 0;
 848   sk->intr = 0;
 849   sk->linger = 0;
 850   sk->destroy = 0;
 851 
 852   sk->priority = 1;
 853   sk->shutdown = 0;
 854   sk->urg = 0;
 855   sk->keepopen = 0;
 856   sk->zapped = 0;
 857   sk->done = 0;
 858   sk->ack_backlog = 0;
 859   sk->window = 0;
 860   sk->bytes_rcv = 0;
 861   sk->state = TCP_CLOSE;
 862   sk->dead = 0;
 863   sk->ack_timed = 0;
 864   sk->send_tmp = NULL;
 865   sk->mss = 0; /* we will try not to send any packets smaller than this. */
 866   sk->debug = 0;
 867 
 868   /* this is how many unacked bytes we will accept for this socket.  */
 869   sk->max_unacked = 2048; /* needs to be at most 2 full packets. */
 870 
 871   /* how many packets we should send before forcing an ack. 
 872      if this is set to zero it is the same as sk->delay_acks = 0 */
 873   sk->max_ack_backlog = 0;
 874   sk->inuse = 0;
 875   sk->delay_acks = 0;
 876   sk->wback = NULL;
 877   sk->wfront = NULL;
 878   sk->rqueue = NULL;
 879   sk->mtu = 576;
 880   sk->prot = prot;
 881   sk->sleep = sock->wait;
 882   sk->daddr = 0;
 883   sk->saddr = my_addr();
 884   sk->err = 0;
 885   sk->next = NULL;
 886   sk->pair = NULL;
 887   sk->send_tail = NULL;
 888   sk->send_head = NULL;
 889   sk->timeout = 0;
 890   sk->broadcast = 0;
 891   sk->timer.data = (unsigned long)sk;
 892   sk->timer.function = &net_timer;
 893   sk->back_log = NULL;
 894   sk->blog = 0;
 895   sock->data =(void *) sk;
 896   sk->dummy_th.doff = sizeof(sk->dummy_th)/4;
 897   sk->dummy_th.res1=0;
 898   sk->dummy_th.res2=0;
 899   sk->dummy_th.urg_ptr = 0;
 900   sk->dummy_th.fin = 0;
 901   sk->dummy_th.syn = 0;
 902   sk->dummy_th.rst = 0;
 903   sk->dummy_th.psh = 0;
 904   sk->dummy_th.ack = 0;
 905   sk->dummy_th.urg = 0;
 906   sk->dummy_th.dest = 0;
 907 
 908   sk->ip_tos=0;
 909   sk->ip_ttl=64;
 910         
 911   sk->state_change = def_callback1;
 912   sk->data_ready = def_callback2;
 913   sk->write_space = def_callback1;
 914   sk->error_report = def_callback1;
 915 
 916   if (sk->num) {
 917         /*
 918          * It assumes that any protocol which allows
 919          * the user to assign a number at socket
 920          * creation time automatically
 921          * shares.
 922          */
 923         put_sock(sk->num, sk);
 924         sk->dummy_th.source = ntohs(sk->num);
 925   }
 926 
 927   if (sk->prot->init) {
 928         err = sk->prot->init(sk);
 929         if (err != 0) {
 930                 destroy_sock(sk);
 931                 return(err);
 932         }
 933   }
 934   return(0);
 935 }
 936 
 937 
 938 static int
 939 inet_dup(struct socket *newsock, struct socket *oldsock)
     /* [previous][next][first][last][top][bottom][index][help] */
 940 {
 941   return(inet_create(newsock,
 942                    ((struct sock *)(oldsock->data))->protocol));
 943 }
 944 
 945 
 946 /* The peer socket should always be NULL. */
 947 static int
 948 inet_release(struct socket *sock, struct socket *peer)
     /* [previous][next][first][last][top][bottom][index][help] */
 949 {
 950   struct sock *sk;
 951 
 952   sk = (struct sock *) sock->data;
 953   if (sk == NULL) return(0);
 954 
 955   DPRINTF((DBG_INET, "inet_release(sock = %X, peer = %X)\n", sock, peer));
 956   sk->state_change(sk);
 957 
 958   /* Start closing the connection.  This may take a while. */
 959   /*
 960    * If linger is set, we don't return until the close
 961    * is complete.  Other wise we return immediately. The
 962    * actually closing is done the same either way.
 963    */
 964   if (sk->linger == 0) {
 965         sk->prot->close(sk,0);
 966         sk->dead = 1;
 967   } else {
 968         DPRINTF((DBG_INET, "sk->linger set.\n"));
 969         sk->prot->close(sk, 0);
 970         cli();
 971         if (sk->lingertime)
 972                 current->timeout = jiffies + HZ*sk->lingertime;
 973         while(sk->state != TCP_CLOSE && current->timeout>0) {
 974                 interruptible_sleep_on(sk->sleep);
 975                 if (current->signal & ~current->blocked) {
 976                         sti();
 977                         current->timeout=0;
 978                         return(-ERESTARTSYS);
 979                 }
 980         }
 981         current->timeout=0;
 982         sti();
 983         sk->dead = 1;
 984   }
 985   sk->inuse = 1;
 986 
 987   /* This will destroy it. */
 988   release_sock(sk);
 989   sock->data = NULL;
 990   DPRINTF((DBG_INET, "inet_release returning\n"));
 991   return(0);
 992 }
 993 
 994 
 995 /* this needs to be changed to dissallow
 996    the rebinding of sockets.   What error
 997    should it return? */
 998 
 999 static int
1000 inet_bind(struct socket *sock, struct sockaddr *uaddr,
     /* [previous][next][first][last][top][bottom][index][help] */
1001                int addr_len)
1002 {
1003   struct sockaddr_in addr;
1004   struct sock *sk, *sk2;
1005   unsigned short snum;
1006   int err;
1007 
1008   sk = (struct sock *) sock->data;
1009   if (sk == NULL) {
1010         printk("Warning: sock->data = NULL: %d\n" ,__LINE__);
1011         return(0);
1012   }
1013 
1014   /* check this error. */
1015   if (sk->state != TCP_CLOSE) return(-EIO);
1016   if (sk->num != 0) return(-EINVAL);
1017 
1018   err=verify_area(VERIFY_READ, uaddr, addr_len);
1019   if(err)
1020         return err;
1021   memcpy_fromfs(&addr, uaddr, min(sizeof(addr), addr_len));
1022 
1023   snum = ntohs(addr.sin_port);
1024   DPRINTF((DBG_INET, "bind sk =%X to port = %d\n", sk, snum));
1025   sk = (struct sock *) sock->data;
1026 
1027   /*
1028    * We can't just leave the socket bound wherever it is, it might
1029    * be bound to a privileged port. However, since there seems to
1030    * be a bug here, we will leave it if the port is not privileged.
1031    */
1032   if (snum == 0) {
1033         snum = get_new_socknum(sk->prot, 0);
1034   }
1035   if (snum < PROT_SOCK && !suser()) return(-EACCES);
1036 
1037   if (addr.sin_addr.s_addr!=0 && chk_addr(addr.sin_addr.s_addr)!=IS_MYADDR)
1038         return(-EADDRNOTAVAIL); /* Source address MUST be ours! */
1039         
1040   if (chk_addr(addr.sin_addr.s_addr) || addr.sin_addr.s_addr == 0)
1041                                         sk->saddr = addr.sin_addr.s_addr;
1042 
1043   DPRINTF((DBG_INET, "sock_array[%d] = %X:\n", snum &(SOCK_ARRAY_SIZE -1),
1044                         sk->prot->sock_array[snum &(SOCK_ARRAY_SIZE -1)]));
1045 
1046   /* Make sure we are allowed to bind here. */
1047   cli();
1048 outside_loop:
1049   for(sk2 = sk->prot->sock_array[snum & (SOCK_ARRAY_SIZE -1)];
1050                                         sk2 != NULL; sk2 = sk2->next) {
1051 #if     1       /* should be below! */
1052         if (sk2->num != snum) continue;
1053 /*      if (sk2->saddr != sk->saddr) continue; */
1054 #endif
1055         if (sk2->dead) {
1056                 destroy_sock(sk2);
1057                 goto outside_loop;
1058         }
1059         if (!sk->reuse) {
1060                 sti();
1061                 return(-EADDRINUSE);
1062         }
1063         if (sk2->num != snum) continue;         /* more than one */
1064         if (sk2->saddr != sk->saddr) continue;  /* socket per slot ! -FB */
1065         if (!sk2->reuse) {
1066                 sti();
1067                 return(-EADDRINUSE);
1068         }
1069   }
1070   sti();
1071 
1072   remove_sock(sk);
1073   put_sock(snum, sk);
1074   sk->dummy_th.source = ntohs(sk->num);
1075   sk->daddr = 0;
1076   sk->dummy_th.dest = 0;
1077   return(0);
1078 }
1079 
1080 
1081 static int
1082 inet_connect(struct socket *sock, struct sockaddr * uaddr,
     /* [previous][next][first][last][top][bottom][index][help] */
1083                   int addr_len, int flags)
1084 {
1085   struct sock *sk;
1086   int err;
1087 
1088   sock->conn = NULL;
1089   sk = (struct sock *) sock->data;
1090   if (sk == NULL) {
1091         printk("Warning: sock->data = NULL: %d\n" ,__LINE__);
1092         return(0);
1093   }
1094 
1095   if (sock->state == SS_CONNECTING && sk->state == TCP_ESTABLISHED)
1096   {
1097         sock->state = SS_CONNECTED;
1098   /* Connection completing after a connect/EINPROGRESS/select/connect */
1099         return 0;       /* Rock and roll */
1100   }
1101 
1102   if (sock->state == SS_CONNECTING && sk->protocol == IPPROTO_TCP &&
1103         (flags & O_NONBLOCK))
1104         return -EALREADY;       /* Connecting is currently in progress */
1105         
1106   if (sock->state != SS_CONNECTING) {
1107         /* We may need to bind the socket. */
1108         if (sk->num == 0) {
1109                 sk->num = get_new_socknum(sk->prot, 0);
1110                 if (sk->num == 0) 
1111                         return(-EAGAIN);
1112                 put_sock(sk->num, sk);
1113                 sk->dummy_th.source = htons(sk->num);
1114         }
1115 
1116         if (sk->prot->connect == NULL) 
1117                 return(-EOPNOTSUPP);
1118   
1119         err = sk->prot->connect(sk, (struct sockaddr_in *)uaddr, addr_len);
1120         if (err < 0) return(err);
1121   
1122         sock->state = SS_CONNECTING;
1123   }
1124 
1125   if (sk->state != TCP_ESTABLISHED &&(flags & O_NONBLOCK)) 
1126         return(-EINPROGRESS);
1127 
1128   cli(); /* avoid the race condition */
1129   while(sk->state == TCP_SYN_SENT || sk->state == TCP_SYN_RECV) 
1130   {
1131         interruptible_sleep_on(sk->sleep);
1132         if (current->signal & ~current->blocked) {
1133                 sti();
1134                 return(-ERESTARTSYS);
1135         }
1136         /* This fixes a nasty in the tcp/ip code. There is a hideous hassle with
1137            icmp error packets wanting to close a tcp or udp socket. */
1138         if(sk->err && sk->protocol == IPPROTO_TCP)
1139         {
1140                 sti();
1141                 sock->state = SS_UNCONNECTED;
1142                 err = -sk->err;
1143                 sk->err=0;
1144                 return err; /* set by tcp_err() */
1145         }
1146   }
1147   sti();
1148   sock->state = SS_CONNECTED;
1149 
1150   if (sk->state != TCP_ESTABLISHED && sk->err) {
1151         sock->state = SS_UNCONNECTED;
1152         err=sk->err;
1153         sk->err=0;
1154         return(err);
1155   }
1156   return(0);
1157 }
1158 
1159 
1160 static int
1161 inet_socketpair(struct socket *sock1, struct socket *sock2)
     /* [previous][next][first][last][top][bottom][index][help] */
1162 {
1163   return(-EOPNOTSUPP);
1164 }
1165 
1166 
1167 static int
1168 inet_accept(struct socket *sock, struct socket *newsock, int flags)
     /* [previous][next][first][last][top][bottom][index][help] */
1169 {
1170   struct sock *sk1, *sk2;
1171   int err;
1172 
1173   sk1 = (struct sock *) sock->data;
1174   if (sk1 == NULL) {
1175         printk("Warning: sock->data = NULL: %d\n" ,__LINE__);
1176         return(0);
1177   }
1178 
1179   /*
1180    * We've been passed an extra socket.
1181    * We need to free it up because the tcp module creates
1182    * it's own when it accepts one.
1183    */
1184   if (newsock->data) kfree_s(newsock->data, sizeof(struct sock));
1185   newsock->data = NULL;
1186 
1187   if (sk1->prot->accept == NULL) return(-EOPNOTSUPP);
1188 
1189   /* Restore the state if we have been interrupted, and then returned. */
1190   if (sk1->pair != NULL ) {
1191         sk2 = sk1->pair;
1192         sk1->pair = NULL;
1193   } else {
1194         sk2 = sk1->prot->accept(sk1,flags);
1195         if (sk2 == NULL) {
1196                 if (sk1->err <= 0)
1197                         printk("Warning sock.c:sk1->err <= 0.  Returning non-error.\n");
1198                 err=sk1->err;
1199                 sk1->err=0;
1200                 return(-err);
1201         }
1202   }
1203   newsock->data = (void *)sk2;
1204   sk2->sleep = newsock->wait;
1205   newsock->conn = NULL;
1206   if (flags & O_NONBLOCK) return(0);
1207 
1208   cli(); /* avoid the race. */
1209   while(sk2->state == TCP_SYN_RECV) {
1210         interruptible_sleep_on(sk2->sleep);
1211         if (current->signal & ~current->blocked) {
1212                 sti();
1213                 sk1->pair = sk2;
1214                 sk2->sleep = NULL;
1215                 newsock->data = NULL;
1216                 return(-ERESTARTSYS);
1217         }
1218   }
1219   sti();
1220 
1221   if (sk2->state != TCP_ESTABLISHED && sk2->err > 0) {
1222 
1223         err = -sk2->err;
1224         sk2->err=0;
1225         destroy_sock(sk2);
1226         newsock->data = NULL;
1227         return(err);
1228   }
1229   newsock->state = SS_CONNECTED;
1230   return(0);
1231 }
1232 
1233 
1234 static int
1235 inet_getname(struct socket *sock, struct sockaddr *uaddr,
     /* [previous][next][first][last][top][bottom][index][help] */
1236                  int *uaddr_len, int peer)
1237 {
1238   struct sockaddr_in sin;
1239   struct sock *sk;
1240   int len;
1241   int err;
1242   
1243   
1244   err = verify_area(VERIFY_WRITE,uaddr_len,sizeof(long));
1245   if(err)
1246         return err;
1247         
1248   len=get_fs_long(uaddr_len);
1249   
1250   err = verify_area(VERIFY_WRITE, uaddr, len);
1251   if(err)
1252         return err;
1253         
1254   /* Check this error. */
1255   if (len < sizeof(sin)) return(-EINVAL);
1256 
1257   sin.sin_family = AF_INET;
1258   sk = (struct sock *) sock->data;
1259   if (sk == NULL) {
1260         printk("Warning: sock->data = NULL: %d\n" ,__LINE__);
1261         return(0);
1262   }
1263   if (peer) {
1264         if (!tcp_connected(sk->state)) return(-ENOTCONN);
1265         sin.sin_port = sk->dummy_th.dest;
1266         sin.sin_addr.s_addr = sk->daddr;
1267   } else {
1268         sin.sin_port = sk->dummy_th.source;
1269         if (sk->saddr == 0) sin.sin_addr.s_addr = my_addr();
1270           else sin.sin_addr.s_addr = sk->saddr;
1271   }
1272   len = sizeof(sin);
1273 /*  verify_area(VERIFY_WRITE, uaddr, len); NOW DONE ABOVE */
1274   memcpy_tofs(uaddr, &sin, sizeof(sin));
1275 /*  verify_area(VERIFY_WRITE, uaddr_len, sizeof(len)); NOW DONE ABOVE */
1276   put_fs_long(len, uaddr_len);
1277   return(0);
1278 }
1279 
1280 
1281 static int
1282 inet_read(struct socket *sock, char *ubuf, int size, int noblock)
     /* [previous][next][first][last][top][bottom][index][help] */
1283 {
1284   struct sock *sk;
1285 
1286   sk = (struct sock *) sock->data;
1287   if (sk == NULL) {
1288         printk("Warning: sock->data = NULL: %d\n" ,__LINE__);
1289         return(0);
1290   }
1291 
1292   /* We may need to bind the socket. */
1293   if (sk->num == 0) {
1294         sk->num = get_new_socknum(sk->prot, 0);
1295         if (sk->num == 0) return(-EAGAIN);
1296         put_sock(sk->num, sk);
1297         sk->dummy_th.source = ntohs(sk->num);
1298   }
1299   return(sk->prot->read(sk, (unsigned char *) ubuf, size, noblock,0));
1300 }
1301 
1302 
1303 static int
1304 inet_recv(struct socket *sock, void *ubuf, int size, int noblock,
     /* [previous][next][first][last][top][bottom][index][help] */
1305           unsigned flags)
1306 {
1307   struct sock *sk;
1308 
1309   sk = (struct sock *) sock->data;
1310   if (sk == NULL) {
1311         printk("Warning: sock->data = NULL: %d\n" ,__LINE__);
1312         return(0);
1313   }
1314 
1315   /* We may need to bind the socket. */
1316   if (sk->num == 0) {
1317         sk->num = get_new_socknum(sk->prot, 0);
1318         if (sk->num == 0) return(-EAGAIN);
1319         put_sock(sk->num, sk);
1320         sk->dummy_th.source = ntohs(sk->num);
1321   }
1322   return(sk->prot->read(sk, (unsigned char *) ubuf, size, noblock, flags));
1323 }
1324 
1325 
1326 static int
1327 inet_write(struct socket *sock, char *ubuf, int size, int noblock)
     /* [previous][next][first][last][top][bottom][index][help] */
1328 {
1329   struct sock *sk;
1330 
1331   sk = (struct sock *) sock->data;
1332   if (sk == NULL) {
1333         printk("Warning: sock->data = NULL: %d\n" ,__LINE__);
1334         return(0);
1335   }
1336   if (sk->shutdown & SEND_SHUTDOWN) {
1337         send_sig(SIGPIPE, current, 1);
1338         return(-EPIPE);
1339   }
1340 
1341   /* We may need to bind the socket. */
1342   if (sk->num == 0) {
1343         sk->num = get_new_socknum(sk->prot, 0);
1344         if (sk->num == 0) return(-EAGAIN);
1345         put_sock(sk->num, sk);
1346         sk->dummy_th.source = ntohs(sk->num);
1347   }
1348 
1349   return(sk->prot->write(sk, (unsigned char *) ubuf, size, noblock, 0));
1350 }
1351 
1352 
1353 static int
1354 inet_send(struct socket *sock, void *ubuf, int size, int noblock, 
     /* [previous][next][first][last][top][bottom][index][help] */
1355                unsigned flags)
1356 {
1357   struct sock *sk;
1358 
1359   sk = (struct sock *) sock->data;
1360   if (sk == NULL) {
1361         printk("Warning: sock->data = NULL: %d\n" ,__LINE__);
1362         return(0);
1363   }
1364   if (sk->shutdown & SEND_SHUTDOWN) {
1365         send_sig(SIGPIPE, current, 1);
1366         return(-EPIPE);
1367   }
1368 
1369   /* We may need to bind the socket. */
1370   if (sk->num == 0) {
1371         sk->num = get_new_socknum(sk->prot, 0);
1372         if (sk->num == 0) return(-EAGAIN);
1373         put_sock(sk->num, sk);
1374         sk->dummy_th.source = ntohs(sk->num);
1375   }
1376 
1377   return(sk->prot->write(sk, (unsigned char *) ubuf, size, noblock, flags));
1378 }
1379 
1380 
1381 static int
1382 inet_sendto(struct socket *sock, void *ubuf, int size, int noblock, 
     /* [previous][next][first][last][top][bottom][index][help] */
1383             unsigned flags, struct sockaddr *sin, int addr_len)
1384 {
1385   struct sock *sk;
1386 
1387   sk = (struct sock *) sock->data;
1388   if (sk == NULL) {
1389         printk("Warning: sock->data = NULL: %d\n" ,__LINE__);
1390         return(0);
1391   }
1392   if (sk->shutdown & SEND_SHUTDOWN) {
1393         send_sig(SIGPIPE, current, 1);
1394         return(-EPIPE);
1395   }
1396 
1397   if (sk->prot->sendto == NULL) return(-EOPNOTSUPP);
1398 
1399   /* We may need to bind the socket. */
1400   if (sk->num == 0) {
1401         sk->num = get_new_socknum(sk->prot, 0);
1402         if (sk->num == 0) return(-EAGAIN);
1403         put_sock(sk->num, sk);
1404         sk->dummy_th.source = ntohs(sk->num);
1405   }
1406 
1407   return(sk->prot->sendto(sk, (unsigned char *) ubuf, size, noblock, flags, 
1408                            (struct sockaddr_in *)sin, addr_len));
1409 }
1410 
1411 
1412 static int
1413 inet_recvfrom(struct socket *sock, void *ubuf, int size, int noblock, 
     /* [previous][next][first][last][top][bottom][index][help] */
1414                    unsigned flags, struct sockaddr *sin, int *addr_len )
1415 {
1416   struct sock *sk;
1417 
1418   sk = (struct sock *) sock->data;
1419   if (sk == NULL) {
1420         printk("Warning: sock->data = NULL: %d\n" ,__LINE__);
1421         return(0);
1422   }
1423 
1424   if (sk->prot->recvfrom == NULL) return(-EOPNOTSUPP);
1425 
1426   /* We may need to bind the socket. */
1427   if (sk->num == 0) {
1428         sk->num = get_new_socknum(sk->prot, 0);
1429         if (sk->num == 0) return(-EAGAIN);
1430         put_sock(sk->num, sk);
1431         sk->dummy_th.source = ntohs(sk->num);
1432   }
1433 
1434   return(sk->prot->recvfrom(sk, (unsigned char *) ubuf, size, noblock, flags,
1435                              (struct sockaddr_in*)sin, addr_len));
1436 }
1437 
1438 
1439 static int
1440 inet_shutdown(struct socket *sock, int how)
     /* [previous][next][first][last][top][bottom][index][help] */
1441 {
1442   struct sock *sk;
1443 
1444   /*
1445    * This should really check to make sure
1446    * the socket is a TCP socket.
1447    */
1448   how++; /* maps 0->1 has the advantage of making bit 1 rcvs and
1449                        1->2 bit 2 snds.
1450                        2->3 */
1451   if (how & ~SHUTDOWN_MASK) return(-EINVAL);
1452   sk = (struct sock *) sock->data;
1453   if (sk == NULL) {
1454         printk("Warning: sock->data = NULL: %d\n" ,__LINE__);
1455         return(0);
1456   }
1457   if (sock->state == SS_CONNECTING && sk->state == TCP_ESTABLISHED)
1458                                                 sock->state = SS_CONNECTED;
1459 
1460   if (!tcp_connected(sk->state)) return(-ENOTCONN);
1461   sk->shutdown |= how;
1462   if (sk->prot->shutdown) sk->prot->shutdown(sk, how);
1463   return(0);
1464 }
1465 
1466 
1467 static int
1468 inet_select(struct socket *sock, int sel_type, select_table *wait )
     /* [previous][next][first][last][top][bottom][index][help] */
1469 {
1470   struct sock *sk;
1471 
1472   sk = (struct sock *) sock->data;
1473   if (sk == NULL) {
1474         printk("Warning: sock->data = NULL: %d\n" ,__LINE__);
1475         return(0);
1476   }
1477 
1478   if (sk->prot->select == NULL) {
1479         DPRINTF((DBG_INET, "select on non-selectable socket.\n"));
1480         return(0);
1481   }
1482   return(sk->prot->select(sk, sel_type, wait));
1483 }
1484 
1485 
1486 static int
1487 inet_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
     /* [previous][next][first][last][top][bottom][index][help] */
1488 {
1489   struct sock *sk;
1490   int err;
1491 
1492   DPRINTF((DBG_INET, "INET: in inet_ioctl\n"));
1493   sk = NULL;
1494   if (sock && (sk = (struct sock *) sock->data) == NULL) {
1495         printk("AF_INET: Warning: sock->data = NULL: %d\n" , __LINE__);
1496         return(0);
1497   }
1498 
1499   switch(cmd) {
1500         case FIOSETOWN:
1501         case SIOCSPGRP:
1502                 err=verify_area(VERIFY_READ,(int *)arg,sizeof(long));
1503                 if(err)
1504                         return err;
1505                 if (sk)
1506                         sk->proc = get_fs_long((int *) arg);
1507                 return(0);
1508         case FIOGETOWN:
1509         case SIOCGPGRP:
1510                 if (sk) {
1511                         err=verify_area(VERIFY_WRITE,(void *) arg, sizeof(long));
1512                         if(err)
1513                                 return err;
1514                         put_fs_long(sk->proc,(int *)arg);
1515                 }
1516                 return(0);
1517 #if 0   /* FIXME: */
1518         case SIOCATMARK:
1519                 printk("AF_INET: ioctl(SIOCATMARK, 0x%08X)\n",(void *) arg);
1520                 return(-EINVAL);
1521 #endif
1522 
1523         case DDIOCSDBG:
1524                 return(dbg_ioctl((void *) arg, DBG_INET));
1525 
1526         case SIOCADDRT:
1527         case SIOCDELRT:
1528                 return(rt_ioctl(cmd,(void *) arg));
1529 
1530         case SIOCDARP:
1531         case SIOCGARP:
1532         case SIOCSARP:
1533                 return(arp_ioctl(cmd,(void *) arg));
1534 
1535         case IP_SET_DEV:
1536         case SIOCGIFCONF:
1537         case SIOCGIFFLAGS:
1538         case SIOCSIFFLAGS:
1539         case SIOCGIFADDR:
1540         case SIOCSIFADDR:
1541         case SIOCGIFDSTADDR:
1542         case SIOCSIFDSTADDR:
1543         case SIOCGIFBRDADDR:
1544         case SIOCSIFBRDADDR:
1545         case SIOCGIFNETMASK:
1546         case SIOCSIFNETMASK:
1547         case SIOCGIFMETRIC:
1548         case SIOCSIFMETRIC:
1549         case SIOCGIFMEM:
1550         case SIOCSIFMEM:
1551         case SIOCGIFMTU:
1552         case SIOCSIFMTU:
1553         case SIOCSIFLINK:
1554         case SIOCGIFHWADDR:
1555                 return(dev_ioctl(cmd,(void *) arg));
1556 
1557         default:
1558                 if (!sk || !sk->prot->ioctl) return(-EINVAL);
1559                 return(sk->prot->ioctl(sk, cmd, arg));
1560   }
1561   /*NOTREACHED*/
1562   return(0);
1563 }
1564 
1565 
1566 void *
1567 sock_wmalloc(struct sock *sk, unsigned long size, int force,
     /* [previous][next][first][last][top][bottom][index][help] */
1568              int priority)
1569 {
1570   if (sk) {
1571         if (sk->wmem_alloc + size < sk->sndbuf || force) {
1572                 cli();
1573                 sk->wmem_alloc+= size;
1574                 sti();
1575                 return(alloc_skb(size, priority));
1576         }
1577         DPRINTF((DBG_INET, "sock_wmalloc(%X,%d,%d,%d) returning NULL\n",
1578                                                 sk, size, force, priority));
1579         return(NULL);
1580   }
1581   return(alloc_skb(size, priority));
1582 }
1583 
1584 
1585 void *
1586 sock_rmalloc(struct sock *sk, unsigned long size, int force, int priority)
     /* [previous][next][first][last][top][bottom][index][help] */
1587 {
1588   if (sk) {
1589         if (sk->rmem_alloc + size < sk->rcvbuf || force) {
1590                 void *c = alloc_skb(size, priority);
1591                 cli();
1592                 if (c) sk->rmem_alloc += size;
1593                 sti();
1594                 return(c);
1595         }
1596         DPRINTF((DBG_INET, "sock_rmalloc(%X,%d,%d,%d) returning NULL\n",
1597                                                 sk,size,force, priority));
1598         return(NULL);
1599   }
1600   return(alloc_skb(size, priority));
1601 }
1602 
1603 
1604 unsigned long
1605 sock_rspace(struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
1606 {
1607   int amt;
1608 
1609   if (sk != NULL) {
1610         if (sk->rmem_alloc >= sk->rcvbuf-2*MIN_WINDOW) return(0);
1611         amt = min((sk->rcvbuf-sk->rmem_alloc)/2-MIN_WINDOW, MAX_WINDOW);
1612         if (amt < 0) return(0);
1613         return(amt);
1614   }
1615   return(0);
1616 }
1617 
1618 
1619 unsigned long
1620 sock_wspace(struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
1621 {
1622   if (sk != NULL) {
1623         if (sk->shutdown & SEND_SHUTDOWN) return(0);
1624         if (sk->wmem_alloc >= sk->sndbuf) return(0);
1625         return(sk->sndbuf-sk->wmem_alloc );
1626   }
1627   return(0);
1628 }
1629 
1630 
1631 void
1632 sock_wfree(struct sock *sk, void *mem, unsigned long size)
     /* [previous][next][first][last][top][bottom][index][help] */
1633 {
1634   DPRINTF((DBG_INET, "sock_wfree(sk=%X, mem=%X, size=%d)\n", sk, mem, size));
1635 
1636   IS_SKB(mem);
1637   kfree_skbmem(mem, size);
1638   if (sk) {
1639         sk->wmem_alloc -= size;
1640 
1641         /* In case it might be waiting for more memory. */
1642         if (!sk->dead) sk->write_space(sk);
1643         if (sk->destroy && sk->wmem_alloc == 0 && sk->rmem_alloc == 0) {
1644                 DPRINTF((DBG_INET,
1645                         "recovered lost memory, sock = %X\n", sk));
1646         }
1647         return;
1648   }
1649 }
1650 
1651 
1652 void
1653 sock_rfree(struct sock *sk, void *mem, unsigned long size)
     /* [previous][next][first][last][top][bottom][index][help] */
1654 {
1655   DPRINTF((DBG_INET, "sock_rfree(sk=%X, mem=%X, size=%d)\n", sk, mem, size));
1656   IS_SKB(mem);
1657   kfree_skbmem(mem, size);
1658   if (sk) {
1659         sk->rmem_alloc -= size;
1660         if (sk->destroy && sk->wmem_alloc == 0 && sk->rmem_alloc == 0) {
1661                 DPRINTF((DBG_INET,
1662                         "recovered lot memory, sock = %X\n", sk));
1663         }
1664   }
1665 }
1666 
1667 
1668 /*
1669  * This routine must find a socket given a TCP or UDP header.
1670  * Everyhting is assumed to be in net order.
1671  */
1672 struct sock *get_sock(struct proto *prot, unsigned short num,
     /* [previous][next][first][last][top][bottom][index][help] */
1673                                 unsigned long raddr,
1674                                 unsigned short rnum, unsigned long laddr)
1675 {
1676   struct sock *s;
1677   unsigned short hnum;
1678 
1679   hnum = ntohs(num);
1680   DPRINTF((DBG_INET, "get_sock(prot=%X, num=%d, raddr=%X, rnum=%d, laddr=%X)\n",
1681           prot, num, raddr, rnum, laddr));
1682 
1683   /*
1684    * SOCK_ARRAY_SIZE must be a power of two.  This will work better
1685    * than a prime unless 3 or more sockets end up using the same
1686    * array entry.  This should not be a problem because most
1687    * well known sockets don't overlap that much, and for
1688    * the other ones, we can just be careful about picking our
1689    * socket number when we choose an arbitrary one.
1690    */
1691   for(s = prot->sock_array[hnum & (SOCK_ARRAY_SIZE - 1)];
1692       s != NULL; s = s->next) 
1693   {
1694         if (s->num != hnum) 
1695                 continue;
1696         if(s->dead && (s->state == TCP_CLOSE))
1697                 continue;
1698         if(prot == &udp_prot)
1699                 return s;
1700         if(ip_addr_match(s->daddr,raddr)==0)
1701                 continue;
1702         if (s->dummy_th.dest != rnum && s->dummy_th.dest != 0) 
1703                 continue;
1704         if(ip_addr_match(s->saddr,laddr) == 0)
1705                 continue;
1706         return(s);
1707   }
1708   return(NULL);
1709 }
1710 
1711 
1712 void release_sock(struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
1713 {
1714   if (!sk) {
1715         printk("sock.c: release_sock sk == NULL\n");
1716         return;
1717   }
1718   if (!sk->prot) {
1719 /*      printk("sock.c: release_sock sk->prot == NULL\n"); */
1720         return;
1721   }
1722 
1723   if (sk->blog) return;
1724 
1725   /* See if we have any packets built up. */
1726   cli();
1727   sk->inuse = 1;
1728   while(sk->back_log != NULL) {
1729         struct sk_buff *skb;
1730 
1731         sk->blog = 1;
1732         skb =(struct sk_buff *)sk->back_log;
1733         DPRINTF((DBG_INET, "release_sock: skb = %X:\n", skb));
1734         if (skb->next != skb) {
1735                 sk->back_log = skb->next;
1736                 skb->prev->next = skb->next;
1737                 skb->next->prev = skb->prev;
1738         } else {
1739                 sk->back_log = NULL;
1740         }
1741         sti();
1742         DPRINTF((DBG_INET, "sk->back_log = %X\n", sk->back_log));
1743         if (sk->prot->rcv) sk->prot->rcv(skb, skb->dev, sk->opt,
1744                                          skb->saddr, skb->len, skb->daddr, 1,
1745 
1746         /* Only used for/by raw sockets. */
1747         (struct inet_protocol *)sk->pair); 
1748         cli();
1749   }
1750   sk->blog = 0;
1751   sk->inuse = 0;
1752   sti();
1753   if (sk->dead && sk->state == TCP_CLOSE) {
1754         /* Should be about 2 rtt's */
1755         reset_timer(sk, TIME_DONE, min(sk->rtt * 2, TCP_DONE_TIME));
1756   }
1757 }
1758 
1759 
1760 static int
1761 inet_fioctl(struct inode *inode, struct file *file,
     /* [previous][next][first][last][top][bottom][index][help] */
1762          unsigned int cmd, unsigned long arg)
1763 {
1764   int minor, ret;
1765 
1766   /* Extract the minor number on which we work. */
1767   minor = MINOR(inode->i_rdev);
1768   if (minor != 0) return(-ENODEV);
1769 
1770   /* Now dispatch on the minor device. */
1771   switch(minor) {
1772         case 0:         /* INET */
1773                 ret = inet_ioctl(NULL, cmd, arg);
1774                 break;
1775         case 1:         /* IP */
1776                 ret = ip_ioctl(NULL, cmd, arg);
1777                 break;
1778         case 2:         /* ICMP */
1779                 ret = icmp_ioctl(NULL, cmd, arg);
1780                 break;
1781         case 3:         /* TCP */
1782                 ret = tcp_ioctl(NULL, cmd, arg);
1783                 break;
1784         case 4:         /* UDP */
1785                 ret = udp_ioctl(NULL, cmd, arg);
1786                 break;
1787         default:
1788                 ret = -ENODEV;
1789   }
1790 
1791   return(ret);
1792 }
1793 
1794 
1795 static struct file_operations inet_fops = {
1796   NULL,         /* LSEEK        */
1797   NULL,         /* READ         */
1798   NULL,         /* WRITE        */
1799   NULL,         /* READDIR      */
1800   NULL,         /* SELECT       */
1801   inet_fioctl,  /* IOCTL        */
1802   NULL,         /* MMAP         */
1803   NULL,         /* OPEN         */
1804   NULL          /* CLOSE        */
1805 };
1806 
1807 
1808 static struct proto_ops inet_proto_ops = {
1809   AF_INET,
1810 
1811   inet_create,
1812   inet_dup,
1813   inet_release,
1814   inet_bind,
1815   inet_connect,
1816   inet_socketpair,
1817   inet_accept,
1818   inet_getname, 
1819   inet_read,
1820   inet_write,
1821   inet_select,
1822   inet_ioctl,
1823   inet_listen,
1824   inet_send,
1825   inet_recv,
1826   inet_sendto,
1827   inet_recvfrom,
1828   inet_shutdown,
1829   inet_setsockopt,
1830   inet_getsockopt,
1831   inet_fcntl,
1832 };
1833 
1834 extern unsigned long seq_offset;
1835 
1836 /* Called by ddi.c on kernel startup.  */
1837 void inet_proto_init(struct ddi_proto *pro)
     /* [previous][next][first][last][top][bottom][index][help] */
1838 {
1839   struct inet_protocol *p;
1840   int i;
1841 
1842   printk("Swansea University Computer Society Net2Debugged [1.27]\n");
1843   /* Set up our UNIX VFS major device. */
1844   if (register_chrdev(AF_INET_MAJOR, "af_inet", &inet_fops) < 0) {
1845         printk("%s: cannot register major device %d!\n",
1846                                         pro->name, AF_INET_MAJOR);
1847         return;
1848   }
1849 
1850   /* Tell SOCKET that we are alive... */
1851   (void) sock_register(inet_proto_ops.family, &inet_proto_ops);
1852 
1853   seq_offset = CURRENT_TIME*250;
1854 
1855   /* Add all the protocols. */
1856   for(i = 0; i < SOCK_ARRAY_SIZE; i++) {
1857         tcp_prot.sock_array[i] = NULL;
1858         udp_prot.sock_array[i] = NULL;
1859         raw_prot.sock_array[i] = NULL;
1860   }
1861   printk("IP Protocols: ");
1862   for(p = inet_protocol_base; p != NULL;) {
1863         struct inet_protocol *tmp;
1864 
1865         tmp = (struct inet_protocol *) p->next;
1866         inet_add_protocol(p);
1867         printk("%s%s",p->name,tmp?", ":"\n");
1868         p = tmp;
1869   }
1870 
1871   /* Initialize the DEV module. */
1872   dev_init();
1873 
1874   /* Initialize the "Buffer Head" pointers. */
1875   bh_base[INET_BH].routine = inet_bh;
1876 }

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