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

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