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

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