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

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