root/net/inet/af_inet.c

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

DEFINITIONS

This source file includes following definitions.
  1. sk_inuse
  2. get_new_socknum
  3. put_sock
  4. remove_sock
  5. destroy_sock
  6. inet_fcntl
  7. inet_setsockopt
  8. inet_getsockopt
  9. inet_autobind
  10. inet_listen
  11. def_callback1
  12. def_callback2
  13. inet_create
  14. inet_dup
  15. inet_release
  16. inet_bind
  17. inet_error
  18. inet_connect
  19. inet_socketpair
  20. inet_accept
  21. inet_getname
  22. inet_recvfrom
  23. inet_recv
  24. inet_read
  25. inet_send
  26. inet_write
  27. inet_sendto
  28. inet_shutdown
  29. inet_select
  30. inet_ioctl
  31. get_sock
  32. 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  *              AF_INET protocol family socket handler.
   7  *
   8  * Version:     @(#)af_inet.c   (from 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  *              Alan Cox, <A.Cox@swansea.ac.uk>
  14  *
  15  *              This program is free software; you can redistribute it and/or
  16  *              modify it under the terms of the GNU General Public License
  17  *              as published by the Free Software Foundation; either version
  18  *              2 of the License, or (at your option) any later version.
  19  */
  20 
  21 #include <linux/config.h>
  22 #include <linux/errno.h>
  23 #include <linux/types.h>
  24 #include <linux/socket.h>
  25 #include <linux/in.h>
  26 #include <linux/kernel.h>
  27 #include <linux/major.h>
  28 #include <linux/sched.h>
  29 #include <linux/timer.h>
  30 #include <linux/string.h>
  31 #include <linux/sockios.h>
  32 #include <linux/net.h>
  33 #include <linux/fcntl.h>
  34 #include <linux/mm.h>
  35 #include <linux/interrupt.h>
  36 
  37 #include <asm/segment.h>
  38 #include <asm/system.h>
  39 
  40 #include <linux/inet.h>
  41 #include <linux/netdevice.h>
  42 #include "ip.h"
  43 #include "protocol.h"
  44 #include "arp.h"
  45 #include "rarp.h"
  46 #include "route.h"
  47 #include "tcp.h"
  48 #include "udp.h"
  49 #include <linux/skbuff.h>
  50 #include "sock.h"
  51 #include "raw.h"
  52 #include "icmp.h"
  53 
  54 #define min(a,b)        ((a)<(b)?(a):(b))
  55 
  56 extern struct proto packet_prot;
  57 
  58 
  59 /*
  60  *      See if a socket number is in use.
  61  */
  62  
  63 static int sk_inuse(struct proto *prot, int num)
     /* [previous][next][first][last][top][bottom][index][help] */
  64 {
  65         struct sock *sk;
  66 
  67         for(sk = prot->sock_array[num & (SOCK_ARRAY_SIZE -1 )];
  68                 sk != NULL;  sk=sk->next) 
  69         {
  70                 if (sk->num == num) 
  71                         return(1);
  72         }
  73         return(0);
  74 }
  75 
  76 
  77 /*
  78  *      Pick a new socket number
  79  */
  80 
  81 unsigned short get_new_socknum(struct proto *prot, unsigned short base)
     /* [previous][next][first][last][top][bottom][index][help] */
  82 {
  83         static int start=0;
  84 
  85         /*
  86          * Used to cycle through the port numbers so the
  87          * chances of a confused connection drop.
  88          */
  89          
  90         int i, j;
  91         int best = 0;
  92         int size = 32767; /* a big num. */
  93         struct sock *sk;
  94 
  95         if (base == 0) 
  96                 base = PROT_SOCK+1+(start % 1024);
  97         if (base <= PROT_SOCK) 
  98         {
  99                 base += PROT_SOCK+(start % 1024);
 100         }
 101 
 102         /* Now look through the entire array and try to find an empty ptr. */
 103         for(i=0; i < SOCK_ARRAY_SIZE; i++) 
 104         {
 105                 j = 0;
 106                 sk = prot->sock_array[(i+base+1) &(SOCK_ARRAY_SIZE -1)];
 107                 while(sk != NULL) 
 108                 {
 109                         sk = sk->next;
 110                         j++;
 111                 }
 112                 if (j == 0) 
 113                 {
 114                         start =(i+1+start )%1024;
 115                         return(i+base+1);
 116                 }
 117                 if (j < size) 
 118                 {
 119                         best = i;
 120                         size = j;
 121                 }
 122         }
 123 
 124         /* Now make sure the one we want is not in use. */
 125 
 126         while(sk_inuse(prot, base +best+1)) 
 127         {
 128                 best += SOCK_ARRAY_SIZE;
 129         }
 130         return(best+base+1);
 131 }
 132 
 133 /*
 134  *      Add a socket into the socket tables by number.
 135  */
 136 
 137 void put_sock(unsigned short num, struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 138 {
 139         struct sock *sk1;
 140         struct sock *sk2;
 141         int mask;
 142 
 143         sk->num = num;
 144         sk->next = NULL;
 145         num = num &(SOCK_ARRAY_SIZE -1);
 146 
 147         /* We can't have an interupt re-enter here. */
 148         cli();
 149         if (sk->prot->sock_array[num] == NULL) 
 150         {
 151                 sk->prot->sock_array[num] = sk;
 152                 sti();
 153                 return;
 154         }
 155         sti();
 156         for(mask = 0xff000000; mask != 0xffffffff; mask = (mask >> 8) | mask) 
 157         {
 158                 if ((mask & sk->saddr) &&
 159                     (mask & sk->saddr) != (mask & 0xffffffff)) 
 160                 {
 161                         mask = mask << 8;
 162                         break;
 163                 }
 164         }
 165         cli();
 166         sk1 = sk->prot->sock_array[num];
 167         for(sk2 = sk1; sk2 != NULL; sk2=sk2->next) 
 168         {
 169                 if (!(sk2->saddr & mask)) 
 170                 {
 171                         if (sk2 == sk1) 
 172                         {
 173                                 sk->next = sk->prot->sock_array[num];
 174                                 sk->prot->sock_array[num] = sk;
 175                                 sti();
 176                                 return;
 177                         }
 178                         sk->next = sk2;
 179                         sk1->next= sk;
 180                         sti();
 181                         return;
 182                 }
 183                 sk1 = sk2;
 184         }
 185 
 186         /* Goes at the end. */
 187         sk->next = NULL;
 188         sk1->next = sk;
 189         sti();
 190 }
 191 
 192 /*
 193  *      Remove a socket from the socket tables.
 194  */
 195 
 196 static void remove_sock(struct sock *sk1)
     /* [previous][next][first][last][top][bottom][index][help] */
 197 {
 198         struct sock *sk2;
 199 
 200         if (!sk1->prot) 
 201         {
 202                 printk("sock.c: remove_sock: sk1->prot == NULL\n");
 203                 return;
 204         }
 205 
 206         /* We can't have this changing out from under us. */
 207         cli();
 208         sk2 = sk1->prot->sock_array[sk1->num &(SOCK_ARRAY_SIZE -1)];
 209         if (sk2 == sk1) 
 210         {
 211                 sk1->prot->sock_array[sk1->num &(SOCK_ARRAY_SIZE -1)] = sk1->next;
 212                 sti();
 213                 return;
 214         }
 215 
 216         while(sk2 && sk2->next != sk1) 
 217         {
 218                 sk2 = sk2->next;
 219         }
 220 
 221         if (sk2) 
 222         {
 223                 sk2->next = sk1->next;
 224                 sti();
 225                 return;
 226         }
 227         sti();
 228 }
 229 
 230 /*
 231  *      Destroy an AF_INET socket
 232  */
 233  
 234 void destroy_sock(struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 235 {
 236         struct sk_buff *skb;
 237 
 238         sk->inuse = 1;                  /* just to be safe. */
 239 
 240         /* Incase it's sleeping somewhere. */
 241         if (!sk->dead) 
 242                 sk->write_space(sk);
 243 
 244         remove_sock(sk);
 245   
 246         /* Now we can no longer get new packets. */
 247         delete_timer(sk);
 248 
 249         while ((skb = tcp_dequeue_partial(sk)) != NULL) {
 250                 IS_SKB(skb);
 251                 kfree_skb(skb, FREE_WRITE);
 252         }
 253 
 254         /* Cleanup up the write buffer. */
 255         while((skb = skb_dequeue(&sk->write_queue)) != NULL) {
 256                 IS_SKB(skb);
 257                 kfree_skb(skb, FREE_WRITE);
 258         }
 259 
 260         while((skb=skb_dequeue(&sk->receive_queue))!=NULL) {
 261         /*
 262          * This will take care of closing sockets that were
 263          * listening and didn't accept everything.
 264          */
 265                 if (skb->sk != NULL && skb->sk != sk) 
 266                 {
 267                         IS_SKB(skb);
 268                         skb->sk->dead = 1;
 269                         skb->sk->prot->close(skb->sk, 0);
 270                 }
 271                 IS_SKB(skb);
 272                 kfree_skb(skb, FREE_READ);
 273         }
 274 
 275         /* Now we need to clean up the send head. */
 276         cli();
 277         for(skb = sk->send_head; skb != NULL; )
 278         {
 279                 struct sk_buff *skb2;
 280 
 281                 /*
 282                  * We need to remove skb from the transmit queue,
 283                  * or maybe the arp queue.
 284                  */
 285                 if (skb->next  && skb->prev) {
 286 /*                      printk("destroy_sock: unlinked skb\n");*/
 287                         IS_SKB(skb);
 288                         skb_unlink(skb);
 289                 }
 290                 skb->dev = NULL;
 291                 skb2 = skb->link3;
 292                 kfree_skb(skb, FREE_WRITE);
 293                 skb = skb2;
 294         }
 295         sk->send_head = NULL;
 296         sti();
 297 
 298         /* And now the backlog. */
 299         while((skb=skb_dequeue(&sk->back_log))!=NULL) 
 300         {
 301                 /* this should never happen. */
 302 /*              printk("cleaning back_log\n");*/
 303                 kfree_skb(skb, FREE_READ);
 304         }
 305 
 306         /* Now if it has a half accepted/ closed socket. */
 307         if (sk->pair) 
 308         {
 309                 sk->pair->dead = 1;
 310                 sk->pair->prot->close(sk->pair, 0);
 311                 sk->pair = NULL;
 312         }
 313 
 314         /*
 315          * Now if everything is gone we can free the socket
 316          * structure, otherwise we need to keep it around until
 317          * everything is gone.
 318          */
 319 
 320           if (sk->dead && sk->rmem_alloc == 0 && sk->wmem_alloc == 0) 
 321           {
 322                 kfree_s((void *)sk,sizeof(*sk));
 323           } 
 324           else 
 325           {
 326                 /* this should never happen. */
 327                 /* actually it can if an ack has just been sent. */
 328                 sk->destroy = 1;
 329                 sk->ack_backlog = 0;
 330                 sk->inuse = 0;
 331                 reset_timer(sk, TIME_DESTROY, SOCK_DESTROY_TIME);
 332         }
 333 }
 334 
 335 /*
 336  *      The routines beyond this point handle the behaviour of an AF_INET
 337  *      socket object. Mostly it punts to the subprotocols of IP to do
 338  *      the work.
 339  */
 340  
 341 static int inet_fcntl(struct socket *sock, unsigned int cmd, unsigned long arg)
     /* [previous][next][first][last][top][bottom][index][help] */
 342 {
 343         struct sock *sk;
 344 
 345         sk = (struct sock *) sock->data;
 346 
 347         switch(cmd) 
 348         {
 349                 case F_SETOWN:
 350                         /*
 351                          * This is a little restrictive, but it's the only
 352                          * way to make sure that you can't send a sigurg to
 353                          * another process.
 354                          */
 355                         if (!suser() && current->pgrp != -arg &&
 356                                 current->pid != arg) return(-EPERM);
 357                         sk->proc = arg;
 358                         return(0);
 359                 case F_GETOWN:
 360                         return(sk->proc);
 361                 default:
 362                         return(-EINVAL);
 363         }
 364 }
 365 
 366 /*
 367  *      Set socket options on an inet socket.
 368  */
 369  
 370 static int inet_setsockopt(struct socket *sock, int level, int optname,
     /* [previous][next][first][last][top][bottom][index][help] */
 371                     char *optval, int optlen)
 372 {
 373         struct sock *sk = (struct sock *) sock->data;  
 374         if (level == SOL_SOCKET)
 375                 return sock_setsockopt(sk,level,optname,optval,optlen);
 376         if (sk->prot->setsockopt==NULL)
 377                 return(-EOPNOTSUPP);
 378         else
 379                 return sk->prot->setsockopt(sk,level,optname,optval,optlen);
 380 }
 381 
 382 /*
 383  *      Get a socket option on an AF_INET socket.
 384  */
 385 
 386 static int inet_getsockopt(struct socket *sock, int level, int optname,
     /* [previous][next][first][last][top][bottom][index][help] */
 387                     char *optval, int *optlen)
 388 {
 389         struct sock *sk = (struct sock *) sock->data;   
 390         if (level == SOL_SOCKET) 
 391                 return sock_getsockopt(sk,level,optname,optval,optlen);
 392         if(sk->prot->getsockopt==NULL)          
 393                 return(-EOPNOTSUPP);
 394         else
 395                 return sk->prot->getsockopt(sk,level,optname,optval,optlen);
 396 }
 397 
 398 /*
 399  *      Automatically bind an unbound socket.
 400  */
 401 
 402 static int inet_autobind(struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 403 {
 404         /* We may need to bind the socket. */
 405         if (sk->num == 0) 
 406         {
 407                 sk->num = get_new_socknum(sk->prot, 0);
 408                 if (sk->num == 0) 
 409                         return(-EAGAIN);
 410                 put_sock(sk->num, sk);
 411                 sk->dummy_th.source = ntohs(sk->num);
 412         }
 413         return 0;
 414 }
 415 
 416 /*
 417  *      Move a socket into listening state.
 418  */
 419  
 420 static int inet_listen(struct socket *sock, int backlog)
     /* [previous][next][first][last][top][bottom][index][help] */
 421 {
 422         struct sock *sk = (struct sock *) sock->data;
 423 
 424         if(inet_autobind(sk)!=0)
 425                 return -EAGAIN;
 426 
 427         /* We might as well re use these. */ 
 428         sk->max_ack_backlog = backlog;
 429         if (sk->state != TCP_LISTEN) 
 430         {
 431                 sk->ack_backlog = 0;
 432                 sk->state = TCP_LISTEN;
 433         }
 434         return(0);
 435 }
 436 
 437 /*
 438  *      Default callbacks for user INET sockets. These just wake up
 439  *      the user owning the socket.
 440  */
 441 
 442 static void def_callback1(struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 443 {
 444         if(!sk->dead)
 445                 wake_up_interruptible(sk->sleep);
 446 }
 447 
 448 static void def_callback2(struct sock *sk,int len)
     /* [previous][next][first][last][top][bottom][index][help] */
 449 {
 450         if(!sk->dead)
 451                 wake_up_interruptible(sk->sleep);
 452 }
 453 
 454 
 455 /*
 456  *      Create an inet socket.
 457  *
 458  *      FIXME: Gcc would generate much better code if we set the parameters
 459  *      up in in-memory structure order. Gcc68K even more so
 460  */
 461 
 462 static int inet_create(struct socket *sock, int protocol)
     /* [previous][next][first][last][top][bottom][index][help] */
 463 {
 464         struct sock *sk;
 465         struct proto *prot;
 466         int err;
 467 
 468         sk = (struct sock *) kmalloc(sizeof(*sk), GFP_KERNEL);
 469         if (sk == NULL) 
 470                 return(-ENOBUFS);
 471         sk->num = 0;
 472         sk->reuse = 0;
 473         switch(sock->type) 
 474         {
 475                 case SOCK_STREAM:
 476                 case SOCK_SEQPACKET:
 477                         if (protocol && protocol != IPPROTO_TCP) 
 478                         {
 479                                 kfree_s((void *)sk, sizeof(*sk));
 480                                 return(-EPROTONOSUPPORT);
 481                         }
 482                         protocol = IPPROTO_TCP;
 483                         sk->no_check = TCP_NO_CHECK;
 484                         prot = &tcp_prot;
 485                         break;
 486 
 487                 case SOCK_DGRAM:
 488                         if (protocol && protocol != IPPROTO_UDP) 
 489                         {
 490                                 kfree_s((void *)sk, sizeof(*sk));
 491                                 return(-EPROTONOSUPPORT);
 492                         }
 493                         protocol = IPPROTO_UDP;
 494                         sk->no_check = UDP_NO_CHECK;
 495                         prot=&udp_prot;
 496                         break;
 497       
 498                 case SOCK_RAW:
 499                         if (!suser()) 
 500                         {
 501                                 kfree_s((void *)sk, sizeof(*sk));
 502                                 return(-EPERM);
 503                         }
 504                         if (!protocol) 
 505                         {
 506                                 kfree_s((void *)sk, sizeof(*sk));
 507                                 return(-EPROTONOSUPPORT);
 508                         }
 509                         prot = &raw_prot;
 510                         sk->reuse = 1;
 511                         sk->no_check = 0;       /*
 512                                                  * Doesn't matter no checksum is
 513                                                  * preformed anyway.
 514                                                  */
 515                         sk->num = protocol;
 516                         break;
 517 
 518                 case SOCK_PACKET:
 519                         if (!suser()) 
 520                         {
 521                                 kfree_s((void *)sk, sizeof(*sk));
 522                                 return(-EPERM);
 523                         }
 524                         if (!protocol) 
 525                         {
 526                                 kfree_s((void *)sk, sizeof(*sk));
 527                                 return(-EPROTONOSUPPORT);
 528                         }
 529                         prot = &packet_prot;
 530                         sk->reuse = 1;
 531                         sk->no_check = 0;       /* Doesn't matter no checksum is
 532                                                  * preformed anyway.
 533                                                  */
 534                         sk->num = protocol;
 535                         break;
 536 
 537                 default:
 538                         kfree_s((void *)sk, sizeof(*sk));
 539                         return(-ESOCKTNOSUPPORT);
 540         }
 541         sk->socket = sock;
 542 #ifdef CONFIG_TCP_NAGLE_OFF
 543         sk->nonagle = 1;
 544 #else    
 545         sk->nonagle = 0;
 546 #endif  
 547         sk->type = sock->type;
 548         sk->stamp.tv_sec=0;
 549         sk->protocol = protocol;
 550         sk->wmem_alloc = 0;
 551         sk->rmem_alloc = 0;
 552         sk->sndbuf = SK_WMEM_MAX;
 553         sk->rcvbuf = SK_RMEM_MAX;
 554         sk->pair = NULL;
 555         sk->opt = NULL;
 556         sk->write_seq = 0;
 557         sk->acked_seq = 0;
 558         sk->copied_seq = 0;
 559         sk->fin_seq = 0;
 560         sk->urg_seq = 0;
 561         sk->urg_data = 0;
 562         sk->proc = 0;
 563         sk->rtt = 0;                            /*TCP_WRITE_TIME << 3;*/
 564         sk->rto = TCP_TIMEOUT_INIT;             /*TCP_WRITE_TIME*/
 565         sk->mdev = 0;
 566         sk->backoff = 0;
 567         sk->packets_out = 0;
 568         sk->cong_window = 1; /* start with only sending one packet at a time. */
 569         sk->cong_count = 0;
 570         sk->ssthresh = 0;
 571         sk->max_window = 0;
 572         sk->urginline = 0;
 573         sk->intr = 0;
 574         sk->linger = 0;
 575         sk->destroy = 0;
 576         sk->priority = 1;
 577         sk->shutdown = 0;
 578         sk->keepopen = 0;
 579         sk->zapped = 0;
 580         sk->done = 0;
 581         sk->ack_backlog = 0;
 582         sk->window = 0;
 583         sk->bytes_rcv = 0;
 584         sk->state = TCP_CLOSE;
 585         sk->dead = 0;
 586         sk->ack_timed = 0;
 587         sk->partial = NULL;
 588         sk->user_mss = 0;
 589         sk->debug = 0;
 590 
 591         /* this is how many unacked bytes we will accept for this socket.  */
 592         sk->max_unacked = 2048; /* needs to be at most 2 full packets. */
 593 
 594         /* how many packets we should send before forcing an ack. 
 595            if this is set to zero it is the same as sk->delay_acks = 0 */
 596         sk->max_ack_backlog = 0;
 597         sk->inuse = 0;
 598         sk->delay_acks = 0;
 599         skb_queue_head_init(&sk->write_queue);
 600         skb_queue_head_init(&sk->receive_queue);
 601         sk->mtu = 576;
 602         sk->prot = prot;
 603         sk->sleep = sock->wait;
 604         sk->daddr = 0;
 605         sk->saddr = ip_my_addr();
 606         sk->err = 0;
 607         sk->next = NULL;
 608         sk->pair = NULL;
 609         sk->send_tail = NULL;
 610         sk->send_head = NULL;
 611         sk->timeout = 0;
 612         sk->broadcast = 0;
 613         sk->localroute = 0;
 614         sk->timer.data = (unsigned long)sk;
 615         sk->timer.function = &net_timer;
 616         skb_queue_head_init(&sk->back_log);
 617         sk->blog = 0;
 618         sock->data =(void *) sk;
 619         sk->dummy_th.doff = sizeof(sk->dummy_th)/4;
 620         sk->dummy_th.res1=0;
 621         sk->dummy_th.res2=0;
 622         sk->dummy_th.urg_ptr = 0;
 623         sk->dummy_th.fin = 0;
 624         sk->dummy_th.syn = 0;
 625         sk->dummy_th.rst = 0;
 626         sk->dummy_th.psh = 0;
 627         sk->dummy_th.ack = 0;
 628         sk->dummy_th.urg = 0;
 629         sk->dummy_th.dest = 0;
 630         sk->ip_tos=0;
 631         sk->ip_ttl=64;
 632         
 633         sk->state_change = def_callback1;
 634         sk->data_ready = def_callback2;
 635         sk->write_space = def_callback1;
 636         sk->error_report = def_callback1;
 637 
 638         if (sk->num) 
 639         {
 640         /*
 641          * It assumes that any protocol which allows
 642          * the user to assign a number at socket
 643          * creation time automatically
 644          * shares.
 645          */
 646                 put_sock(sk->num, sk);
 647                 sk->dummy_th.source = ntohs(sk->num);
 648         }
 649 
 650         if (sk->prot->init) 
 651         {
 652                 err = sk->prot->init(sk);
 653                 if (err != 0) 
 654                 {
 655                         destroy_sock(sk);
 656                         return(err);
 657                 }
 658         }
 659         return(0);
 660 }
 661 
 662 
 663 /*
 664  *      Duplicate a socket.
 665  */
 666  
 667 static int inet_dup(struct socket *newsock, struct socket *oldsock)
     /* [previous][next][first][last][top][bottom][index][help] */
 668 {
 669         return(inet_create(newsock,((struct sock *)(oldsock->data))->protocol));
 670 }
 671 
 672 
 673 /*
 674  *      The peer socket should always be NULL (or else). When we call this
 675  *      function we are destroying the object and from then on nobody
 676  *      should refer to it.
 677  */
 678  
 679 static int inet_release(struct socket *sock, struct socket *peer)
     /* [previous][next][first][last][top][bottom][index][help] */
 680 {
 681         struct sock *sk = (struct sock *) sock->data;
 682         if (sk == NULL) 
 683                 return(0);
 684 
 685         sk->state_change(sk);
 686 
 687         /* Start closing the connection.  This may take a while. */
 688 
 689         /*
 690          * If linger is set, we don't return until the close
 691          * is complete.  Other wise we return immediately. The
 692          * actually closing is done the same either way.
 693          */
 694 
 695         if (sk->linger == 0) 
 696         {
 697                 sk->prot->close(sk,0);
 698                 sk->dead = 1;
 699         } 
 700         else 
 701         {
 702                 sk->prot->close(sk, 0);
 703                 cli();
 704                 if (sk->lingertime)
 705                         current->timeout = jiffies + HZ*sk->lingertime;
 706                 while(sk->state != TCP_CLOSE && current->timeout>0) 
 707                 {
 708                         interruptible_sleep_on(sk->sleep);
 709                         if (current->signal & ~current->blocked) 
 710                         {
 711                                 break;
 712 #if 0
 713                                 /* not working now - closes can't be restarted */
 714                                 sti();
 715                                 current->timeout=0;
 716                                 return(-ERESTARTSYS);
 717 #endif
 718                         }
 719                 }
 720                 current->timeout=0;
 721                 sti();
 722                 sk->dead = 1;
 723         }
 724         sk->inuse = 1;
 725 
 726         /* This will destroy it. */
 727         release_sock(sk);
 728         sock->data = NULL;
 729         return(0);
 730 }
 731 
 732 
 733 /* this needs to be changed to dissallow
 734    the rebinding of sockets.   What error
 735    should it return? */
 736 
 737 static int inet_bind(struct socket *sock, struct sockaddr *uaddr,
     /* [previous][next][first][last][top][bottom][index][help] */
 738                int addr_len)
 739 {
 740         struct sockaddr_in *addr=(struct sockaddr_in *)uaddr;
 741         struct sock *sk=(struct sock *)sock->data, *sk2;
 742         unsigned short snum;
 743         int chk_addr_ret;
 744 
 745         /* check this error. */
 746         if (sk->state != TCP_CLOSE)
 747                 return(-EIO);
 748         if (sk->num != 0) 
 749                 return(-EINVAL);
 750 
 751         if(addr_len<sizeof(struct sockaddr_in))
 752                 return -EINVAL;
 753 
 754         snum = ntohs(addr->sin_port);
 755 
 756         /*
 757          * We can't just leave the socket bound wherever it is, it might
 758          * be bound to a privileged port. However, since there seems to
 759          * be a bug here, we will leave it if the port is not privileged.
 760          */
 761         if (snum == 0) 
 762         {
 763                 snum = get_new_socknum(sk->prot, 0);
 764         }
 765         if (snum < PROT_SOCK && !suser()) 
 766                 return(-EACCES);
 767 
 768         chk_addr_ret = ip_chk_addr(addr->sin_addr.s_addr);
 769         if (addr->sin_addr.s_addr != 0 && chk_addr_ret != IS_MYADDR)
 770                 return(-EADDRNOTAVAIL); /* Source address MUST be ours! */
 771         
 772         if (chk_addr_ret || addr->sin_addr.s_addr == 0)
 773                 sk->saddr = addr->sin_addr.s_addr;
 774 
 775         /* Make sure we are allowed to bind here. */
 776         cli();
 777 outside_loop:
 778         for(sk2 = sk->prot->sock_array[snum & (SOCK_ARRAY_SIZE -1)];
 779                                         sk2 != NULL; sk2 = sk2->next) 
 780         {
 781 /* should be below! */
 782                 if (sk2->num != snum) continue;
 783                 if (sk2->dead) 
 784                 {
 785                         destroy_sock(sk2);
 786                         goto outside_loop;
 787                 }
 788                 if (!sk->reuse) 
 789                 {
 790                         sti();
 791                         return(-EADDRINUSE);
 792                 }
 793                 
 794                 if (sk2->num != snum) 
 795                         continue;               /* more than one */
 796                 if (sk2->saddr != sk->saddr) 
 797                         continue;       /* socket per slot ! -FB */
 798                 if (!sk2->reuse) 
 799                 {
 800                         sti();
 801                         return(-EADDRINUSE);
 802                 }
 803         }
 804         sti();
 805 
 806         remove_sock(sk);
 807         put_sock(snum, sk);
 808         sk->dummy_th.source = ntohs(sk->num);
 809         sk->daddr = 0;
 810         sk->dummy_th.dest = 0;
 811         return(0);
 812 }
 813 
 814 /*
 815  *      Handle sk->err properly. The cli/sti matter.
 816  */
 817  
 818 static int inet_error(struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 819 {
 820         unsigned long flags;
 821         int err;
 822         save_flags(flags);
 823         cli();  
 824         err=sk->err;
 825         sk->err=0;
 826         sti();
 827         return -err;
 828 }
 829 
 830 /*
 831  *      Connect to a remote host. There is regretably still a little
 832  *      TCP 'magic' in here.
 833  */
 834  
 835 static int inet_connect(struct socket *sock, struct sockaddr * uaddr,
     /* [previous][next][first][last][top][bottom][index][help] */
 836                   int addr_len, int flags)
 837 {
 838         struct sock *sk=(struct sock *)sock->data;
 839         int err;
 840         sock->conn = NULL;
 841 
 842         if (sock->state == SS_CONNECTING && tcp_connected(sk->state))
 843         {
 844                 sock->state = SS_CONNECTED;
 845                 /* Connection completing after a connect/EINPROGRESS/select/connect */
 846                 return 0;       /* Rock and roll */
 847         }
 848 
 849         if (sock->state == SS_CONNECTING && sk->protocol == IPPROTO_TCP && (flags & O_NONBLOCK))
 850                 return -EALREADY;       /* Connecting is currently in progress */
 851         
 852         if (sock->state != SS_CONNECTING) 
 853         {
 854                 /* We may need to bind the socket. */
 855                 if(inet_autobind(sk)!=0)
 856                         return(-EAGAIN);
 857                 if (sk->prot->connect == NULL) 
 858                         return(-EOPNOTSUPP);
 859                 err = sk->prot->connect(sk, (struct sockaddr_in *)uaddr, addr_len);
 860                 if (err < 0) 
 861                         return(err);
 862                 sock->state = SS_CONNECTING;
 863         }
 864 
 865         if (sk->state != TCP_ESTABLISHED &&(flags & O_NONBLOCK)) 
 866                 return(-EINPROGRESS);
 867 
 868         cli(); /* avoid the race condition */
 869         while(sk->state == TCP_SYN_SENT || sk->state == TCP_SYN_RECV) 
 870         {
 871                 interruptible_sleep_on(sk->sleep);
 872                 if (current->signal & ~current->blocked) 
 873                 {
 874                         sti();
 875                         return(-ERESTARTSYS);
 876                 }
 877                 /* This fixes a nasty in the tcp/ip code. There is a hideous hassle with
 878                    icmp error packets wanting to close a tcp or udp socket. */
 879                 if(sk->err && sk->protocol == IPPROTO_TCP)
 880                 {
 881                         sti();
 882                         sock->state = SS_UNCONNECTED;
 883                         err = -sk->err;
 884                         sk->err=0;
 885                         return err; /* set by tcp_err() */
 886                 }
 887         }
 888         sti();
 889         sock->state = SS_CONNECTED;
 890 
 891         if (sk->state != TCP_ESTABLISHED && sk->err) 
 892         {
 893                 sock->state = SS_UNCONNECTED;
 894                 err=sk->err;
 895                 sk->err=0;
 896                 return(-err);
 897         }
 898         return(0);
 899 }
 900 
 901 
 902 static int inet_socketpair(struct socket *sock1, struct socket *sock2)
     /* [previous][next][first][last][top][bottom][index][help] */
 903 {
 904          return(-EOPNOTSUPP);
 905 }
 906 
 907 
 908 /*
 909  *      FIXME: Get BSD behaviour
 910  */
 911 
 912 static int inet_accept(struct socket *sock, struct socket *newsock, int flags)
     /* [previous][next][first][last][top][bottom][index][help] */
 913 {
 914         struct sock *sk1, *sk2;
 915         int err;
 916 
 917         sk1 = (struct sock *) sock->data;
 918 
 919         /*
 920          * We've been passed an extra socket.
 921          * We need to free it up because the tcp module creates
 922          * it's own when it accepts one.
 923          */
 924         if (newsock->data)
 925         {
 926                 struct sock *sk=(struct sock *)newsock->data;
 927                 newsock->data=NULL;
 928                 sk->dead = 1;
 929                 destroy_sock(sk);
 930         }
 931   
 932         if (sk1->prot->accept == NULL) 
 933                 return(-EOPNOTSUPP);
 934 
 935         /* Restore the state if we have been interrupted, and then returned. */
 936         if (sk1->pair != NULL ) 
 937         {
 938                 sk2 = sk1->pair;
 939                 sk1->pair = NULL;
 940         } 
 941         else
 942         {
 943                 sk2 = sk1->prot->accept(sk1,flags);
 944                 if (sk2 == NULL) 
 945                 {
 946                         if (sk1->err <= 0)
 947                                 printk("Warning sock.c:sk1->err <= 0.  Returning non-error.\n");
 948                         err=sk1->err;
 949                         sk1->err=0;
 950                         return(-err);
 951                 }
 952         }
 953         newsock->data = (void *)sk2;
 954         sk2->sleep = newsock->wait;
 955         newsock->conn = NULL;
 956         if (flags & O_NONBLOCK) 
 957                 return(0);
 958 
 959         cli(); /* avoid the race. */
 960         while(sk2->state == TCP_SYN_RECV) 
 961         {
 962                 interruptible_sleep_on(sk2->sleep);
 963                 if (current->signal & ~current->blocked) 
 964                 {
 965                         sti();
 966                         sk1->pair = sk2;
 967                         sk2->sleep = NULL;
 968                         newsock->data = NULL;
 969                         return(-ERESTARTSYS);
 970                 }
 971         }
 972         sti();
 973 
 974         if (sk2->state != TCP_ESTABLISHED && sk2->err > 0) 
 975         {
 976                 err = -sk2->err;
 977                 sk2->err=0;
 978                 destroy_sock(sk2);
 979                 newsock->data = NULL;
 980                 return(err);
 981         }
 982         newsock->state = SS_CONNECTED;
 983         return(0);
 984 }
 985 
 986 
 987 /*
 988  *      This does both peername and sockname.
 989  */
 990  
 991 static int inet_getname(struct socket *sock, struct sockaddr *uaddr,
     /* [previous][next][first][last][top][bottom][index][help] */
 992                  int *uaddr_len, int peer)
 993 {
 994         struct sockaddr_in *sin=(struct sockaddr_in *)uaddr;
 995         struct sock *sk;
 996   
 997         sin->sin_family = AF_INET;
 998         sk = (struct sock *) sock->data;
 999         if (peer) 
1000         {
1001                 if (!tcp_connected(sk->state)) 
1002                         return(-ENOTCONN);
1003                 sin->sin_port = sk->dummy_th.dest;
1004                 sin->sin_addr.s_addr = sk->daddr;
1005         } 
1006         else 
1007         {
1008                 sin->sin_port = sk->dummy_th.source;
1009                 if (sk->saddr == 0) 
1010                         sin->sin_addr.s_addr = ip_my_addr();
1011                 else 
1012                         sin->sin_addr.s_addr = sk->saddr;
1013         }
1014         *uaddr_len = sizeof(*sin);
1015         return(0);
1016 }
1017 
1018 
1019 /*
1020  *      The assorted BSD I/O operations
1021  */
1022 
1023 static int inet_recvfrom(struct socket *sock, void *ubuf, int size, int noblock, 
     /* [previous][next][first][last][top][bottom][index][help] */
1024                    unsigned flags, struct sockaddr *sin, int *addr_len )
1025 {
1026         struct sock *sk = (struct sock *) sock->data;
1027         
1028         if (sk->prot->recvfrom == NULL) 
1029                 return(-EOPNOTSUPP);
1030         if(sk->err)
1031                 return inet_error(sk);
1032         /* We may need to bind the socket. */
1033         if(inet_autobind(sk)!=0)
1034                 return(-EAGAIN);
1035         return(sk->prot->recvfrom(sk, (unsigned char *) ubuf, size, noblock, flags,
1036                              (struct sockaddr_in*)sin, addr_len));
1037 }
1038 
1039 
1040 static int inet_recv(struct socket *sock, void *ubuf, int size, int noblock,
     /* [previous][next][first][last][top][bottom][index][help] */
1041           unsigned flags)
1042 {
1043         /* BSD explicitly states these are the same - so we do it this way to be sure */
1044         return inet_recvfrom(sock,ubuf,size,noblock,flags,NULL,NULL);
1045 }
1046 
1047 static int inet_read(struct socket *sock, char *ubuf, int size, int noblock)
     /* [previous][next][first][last][top][bottom][index][help] */
1048 {
1049         struct sock *sk = (struct sock *) sock->data;
1050         
1051         if(sk->err)
1052                 return inet_error(sk);
1053         /* We may need to bind the socket. */
1054         if(inet_autobind(sk))
1055                 return(-EAGAIN);        
1056         return(sk->prot->read(sk, (unsigned char *) ubuf, size, noblock, 0));
1057 }
1058 
1059 static int inet_send(struct socket *sock, void *ubuf, int size, int noblock, 
     /* [previous][next][first][last][top][bottom][index][help] */
1060                unsigned flags)
1061 {
1062         struct sock *sk = (struct sock *) sock->data;
1063         if (sk->shutdown & SEND_SHUTDOWN) 
1064         {
1065                 send_sig(SIGPIPE, current, 1);
1066                 return(-EPIPE);
1067         }
1068         if(sk->err)
1069                 return inet_error(sk);
1070         /* We may need to bind the socket. */
1071         if(inet_autobind(sk)!=0)
1072                 return(-EAGAIN);
1073         return(sk->prot->write(sk, (unsigned char *) ubuf, size, noblock, flags));
1074 }
1075 
1076 static int inet_write(struct socket *sock, char *ubuf, int size, int noblock)
     /* [previous][next][first][last][top][bottom][index][help] */
1077 {
1078         return inet_send(sock,ubuf,size,noblock,0);
1079 }
1080 
1081 static int inet_sendto(struct socket *sock, void *ubuf, int size, int noblock, 
     /* [previous][next][first][last][top][bottom][index][help] */
1082             unsigned flags, struct sockaddr *sin, int addr_len)
1083 {
1084         struct sock *sk = (struct sock *) sock->data;
1085         if (sk->shutdown & SEND_SHUTDOWN) 
1086         {
1087                 send_sig(SIGPIPE, current, 1);
1088                 return(-EPIPE);
1089         }
1090         if (sk->prot->sendto == NULL) 
1091                 return(-EOPNOTSUPP);
1092         if(sk->err)
1093                 return inet_error(sk);
1094         /* We may need to bind the socket. */
1095         if(inet_autobind(sk)!=0)
1096                 return -EAGAIN;
1097         return(sk->prot->sendto(sk, (unsigned char *) ubuf, size, noblock, flags, 
1098                            (struct sockaddr_in *)sin, addr_len));
1099 }
1100 
1101 
1102 static int inet_shutdown(struct socket *sock, int how)
     /* [previous][next][first][last][top][bottom][index][help] */
1103 {
1104         struct sock *sk=(struct sock*)sock->data;
1105 
1106         /*
1107          * This should really check to make sure
1108          * the socket is a TCP socket. (WHY AC...)
1109          */
1110         how++; /* maps 0->1 has the advantage of making bit 1 rcvs and
1111                        1->2 bit 2 snds.
1112                        2->3 */
1113         if ((how & ~SHUTDOWN_MASK) || how==0)   /* MAXINT->0 */
1114                 return(-EINVAL);
1115         if (sock->state == SS_CONNECTING && sk->state == TCP_ESTABLISHED)
1116                 sock->state = SS_CONNECTED;
1117         if (!tcp_connected(sk->state)) 
1118                 return(-ENOTCONN);
1119         sk->shutdown |= how;
1120         if (sk->prot->shutdown)
1121                 sk->prot->shutdown(sk, how);
1122         return(0);
1123 }
1124 
1125 
1126 static int inet_select(struct socket *sock, int sel_type, select_table *wait )
     /* [previous][next][first][last][top][bottom][index][help] */
1127 {
1128         struct sock *sk=(struct sock *) sock->data;
1129         if (sk->prot->select == NULL) 
1130         {
1131                 return(0);
1132         }
1133         return(sk->prot->select(sk, sel_type, wait));
1134 }
1135 
1136 /*
1137  *      ioctl() calls you can issue on an INET socket. Most of these are
1138  *      device configuration and stuff and very rarely used. Some ioctls
1139  *      pass on to the socket itself.
1140  *
1141  *      NOTE: I like the idea of a module for the config stuff. ie ifconfig
1142  *      loads the devconfigure module does its configuring and unloads it.
1143  *      Theres a good 20K of config code hanging around the kernel.
1144  */
1145 
1146 static int inet_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
     /* [previous][next][first][last][top][bottom][index][help] */
1147 {
1148         struct sock *sk=(struct sock *)sock->data;
1149         int err;
1150 
1151         switch(cmd) 
1152         {
1153                 case FIOSETOWN:
1154                 case SIOCSPGRP:
1155                         err=verify_area(VERIFY_READ,(int *)arg,sizeof(long));
1156                         if(err)
1157                                 return err;
1158                         sk->proc = get_fs_long((int *) arg);
1159                         return(0);
1160                 case FIOGETOWN:
1161                 case SIOCGPGRP:
1162                         err=verify_area(VERIFY_WRITE,(void *) arg, sizeof(long));
1163                         if(err)
1164                                 return err;
1165                         put_fs_long(sk->proc,(int *)arg);
1166                         return(0);                      
1167                 case SIOCGSTAMP:
1168                         if(sk->stamp.tv_sec==0)
1169                                 return -ENOENT;
1170                         err=verify_area(VERIFY_WRITE,(void *)arg,sizeof(struct timeval));
1171                         if(err)
1172                                 return err;
1173                         memcpy_tofs((void *)arg,&sk->stamp,sizeof(struct timeval));
1174                         return 0;
1175                 case SIOCADDRT: case SIOCADDRTOLD:
1176                 case SIOCDELRT: case SIOCDELRTOLD:
1177                         return(ip_rt_ioctl(cmd,(void *) arg));
1178                 case SIOCDARP:
1179                 case SIOCGARP:
1180                 case SIOCSARP:
1181                         return(arp_ioctl(cmd,(void *) arg));
1182 #ifdef CONFIG_INET_RARP                 
1183                 case SIOCDRARP:
1184                 case SIOCGRARP:
1185                 case SIOCSRARP:
1186                         return(rarp_ioctl(cmd,(void *) arg));
1187 #endif
1188                 case SIOCGIFCONF:
1189                 case SIOCGIFFLAGS:
1190                 case SIOCSIFFLAGS:
1191                 case SIOCGIFADDR:
1192                 case SIOCSIFADDR:
1193                 case SIOCGIFDSTADDR:
1194                 case SIOCSIFDSTADDR:
1195                 case SIOCGIFBRDADDR:
1196                 case SIOCSIFBRDADDR:
1197                 case SIOCGIFNETMASK:
1198                 case SIOCSIFNETMASK:
1199                 case SIOCGIFMETRIC:
1200                 case SIOCSIFMETRIC:
1201                 case SIOCGIFMEM:
1202                 case SIOCSIFMEM:
1203                 case SIOCGIFMTU:
1204                 case SIOCSIFMTU:
1205                 case SIOCSIFLINK:
1206                 case SIOCGIFHWADDR:
1207                 case SIOCSIFHWADDR:
1208                 case OLD_SIOCGIFHWADDR:
1209                 case SIOCSIFMAP:
1210                 case SIOCGIFMAP:
1211                 case SIOCDEVPRIVATE:
1212                 case SIOCSIFSLAVE:
1213                 case SIOCGIFSLAVE:
1214                         return(dev_ioctl(cmd,(void *) arg));
1215 
1216                 default:
1217                         if (sk->prot->ioctl==NULL) 
1218                                 return(-EINVAL);
1219                         return(sk->prot->ioctl(sk, cmd, arg));
1220         }
1221         /*NOTREACHED*/
1222         return(0);
1223 }
1224 
1225 /*
1226  * This routine must find a socket given a TCP or UDP header.
1227  * Everyhting is assumed to be in net order.
1228  */
1229 
1230 struct sock *get_sock(struct proto *prot, unsigned short num,
     /* [previous][next][first][last][top][bottom][index][help] */
1231                                 unsigned long raddr,
1232                                 unsigned short rnum, unsigned long laddr)
1233 {
1234         struct sock *s;
1235         unsigned short hnum;
1236 
1237         hnum = ntohs(num);
1238 
1239         /*
1240          * SOCK_ARRAY_SIZE must be a power of two.  This will work better
1241          * than a prime unless 3 or more sockets end up using the same
1242          * array entry.  This should not be a problem because most
1243          * well known sockets don't overlap that much, and for
1244          * the other ones, we can just be careful about picking our
1245          * socket number when we choose an arbitrary one.
1246          */
1247 
1248         for(s = prot->sock_array[hnum & (SOCK_ARRAY_SIZE - 1)];
1249                         s != NULL; s = s->next) 
1250         {
1251                 if (s->num != hnum) 
1252                         continue;
1253                 if(s->dead && (s->state == TCP_CLOSE))
1254                         continue;
1255                 if(prot == &udp_prot)
1256                         return s;
1257                 if(ip_addr_match(s->daddr,raddr)==0)
1258                         continue;
1259                 if (s->dummy_th.dest != rnum && s->dummy_th.dest != 0) 
1260                         continue;
1261                 if(ip_addr_match(s->saddr,laddr) == 0)
1262                         continue;
1263                 return(s);
1264         }
1265         return(NULL);
1266 }
1267 
1268 static struct proto_ops inet_proto_ops = {
1269         AF_INET,
1270 
1271         inet_create,
1272         inet_dup,
1273         inet_release,
1274         inet_bind,
1275         inet_connect,
1276         inet_socketpair,
1277         inet_accept,
1278         inet_getname, 
1279         inet_read,
1280         inet_write,
1281         inet_select,
1282         inet_ioctl,
1283         inet_listen,
1284         inet_send,
1285         inet_recv,
1286         inet_sendto,
1287         inet_recvfrom,
1288         inet_shutdown,
1289         inet_setsockopt,
1290         inet_getsockopt,
1291         inet_fcntl,
1292 };
1293 
1294 extern unsigned long seq_offset;
1295 
1296 /*
1297  *      Called by socket.c on kernel startup.  
1298  */
1299  
1300 void inet_proto_init(struct net_proto *pro)
     /* [previous][next][first][last][top][bottom][index][help] */
1301 {
1302         struct inet_protocol *p;
1303         int i;
1304 
1305 
1306         printk("NET3 TCP/IP protocols stack v016\n");
1307 
1308         /*
1309          *      Tell SOCKET that we are alive... 
1310          */
1311    
1312         (void) sock_register(inet_proto_ops.family, &inet_proto_ops);
1313 
1314         seq_offset = CURRENT_TIME*250;
1315 
1316         /*
1317          *      Add all the protocols. 
1318          */
1319          
1320         for(i = 0; i < SOCK_ARRAY_SIZE; i++) 
1321         {
1322                 tcp_prot.sock_array[i] = NULL;
1323                 udp_prot.sock_array[i] = NULL;
1324                 raw_prot.sock_array[i] = NULL;
1325         }
1326 
1327         printk("IP Protocols: ");
1328         for(p = inet_protocol_base; p != NULL;) 
1329         {
1330                 struct inet_protocol *tmp = (struct inet_protocol *) p->next;
1331                 inet_add_protocol(p);
1332                 printk("%s%s",p->name,tmp?", ":"\n");
1333                 p = tmp;
1334         }
1335         /*
1336          *      Set the ARP module up
1337          */
1338         arp_init();
1339         /*
1340          *      Set the IP module up
1341          */
1342         ip_init();
1343 }
1344 

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