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.data = (unsigned long)sk;
 619         sk->timer.function = &net_timer;
 620         skb_queue_head_init(&sk->back_log);
 621         sk->blog = 0;
 622         sock->data =(void *) sk;
 623         sk->dummy_th.doff = sizeof(sk->dummy_th)/4;
 624         sk->dummy_th.res1=0;
 625         sk->dummy_th.res2=0;
 626         sk->dummy_th.urg_ptr = 0;
 627         sk->dummy_th.fin = 0;
 628         sk->dummy_th.syn = 0;
 629         sk->dummy_th.rst = 0;
 630         sk->dummy_th.psh = 0;
 631         sk->dummy_th.ack = 0;
 632         sk->dummy_th.urg = 0;
 633         sk->dummy_th.dest = 0;
 634         sk->ip_tos=0;
 635         sk->ip_ttl=64;
 636         
 637         sk->state_change = def_callback1;
 638         sk->data_ready = def_callback2;
 639         sk->write_space = def_callback1;
 640         sk->error_report = def_callback1;
 641 
 642         if (sk->num) 
 643         {
 644         /*
 645          * It assumes that any protocol which allows
 646          * the user to assign a number at socket
 647          * creation time automatically
 648          * shares.
 649          */
 650                 put_sock(sk->num, sk);
 651                 sk->dummy_th.source = ntohs(sk->num);
 652         }
 653 
 654         if (sk->prot->init) 
 655         {
 656                 err = sk->prot->init(sk);
 657                 if (err != 0) 
 658                 {
 659                         destroy_sock(sk);
 660                         return(err);
 661                 }
 662         }
 663         return(0);
 664 }
 665 
 666 
 667 /*
 668  *      Duplicate a socket.
 669  */
 670  
 671 static int inet_dup(struct socket *newsock, struct socket *oldsock)
     /* [previous][next][first][last][top][bottom][index][help] */
 672 {
 673         return(inet_create(newsock,((struct sock *)(oldsock->data))->protocol));
 674 }
 675 
 676 
 677 /*
 678  *      The peer socket should always be NULL (or else). When we call this
 679  *      function we are destroying the object and from then on nobody
 680  *      should refer to it.
 681  */
 682  
 683 static int inet_release(struct socket *sock, struct socket *peer)
     /* [previous][next][first][last][top][bottom][index][help] */
 684 {
 685         struct sock *sk = (struct sock *) sock->data;
 686         if (sk == NULL) 
 687                 return(0);
 688 
 689         sk->state_change(sk);
 690 
 691         /* Start closing the connection.  This may take a while. */
 692 
 693         /*
 694          * If linger is set, we don't return until the close
 695          * is complete.  Other wise we return immediately. The
 696          * actually closing is done the same either way.
 697          */
 698 
 699         if (sk->linger == 0) 
 700         {
 701                 sk->prot->close(sk,0);
 702                 sk->dead = 1;
 703         } 
 704         else 
 705         {
 706                 sk->prot->close(sk, 0);
 707                 cli();
 708                 if (sk->lingertime)
 709                         current->timeout = jiffies + HZ*sk->lingertime;
 710                 while(sk->state != TCP_CLOSE && current->timeout>0) 
 711                 {
 712                         interruptible_sleep_on(sk->sleep);
 713                         if (current->signal & ~current->blocked) 
 714                         {
 715                                 break;
 716 #if 0
 717                                 /* not working now - closes can't be restarted */
 718                                 sti();
 719                                 current->timeout=0;
 720                                 return(-ERESTARTSYS);
 721 #endif
 722                         }
 723                 }
 724                 current->timeout=0;
 725                 sti();
 726                 sk->dead = 1;
 727         }
 728         sk->inuse = 1;
 729 
 730         /* This will destroy it. */
 731         release_sock(sk);
 732         sock->data = NULL;
 733         return(0);
 734 }
 735 
 736 
 737 /* this needs to be changed to dissallow
 738    the rebinding of sockets.   What error
 739    should it return? */
 740 
 741 static int inet_bind(struct socket *sock, struct sockaddr *uaddr,
     /* [previous][next][first][last][top][bottom][index][help] */
 742                int addr_len)
 743 {
 744         struct sockaddr_in *addr=(struct sockaddr_in *)uaddr;
 745         struct sock *sk=(struct sock *)sock->data, *sk2;
 746         unsigned short snum;
 747         int chk_addr_ret;
 748 
 749         /* check this error. */
 750         if (sk->state != TCP_CLOSE)
 751                 return(-EIO);
 752         if (sk->num != 0) 
 753                 return(-EINVAL);
 754 
 755         if(addr_len<sizeof(struct sockaddr_in))
 756                 return -EINVAL;
 757 
 758         snum = ntohs(addr->sin_port);
 759 
 760         /*
 761          * We can't just leave the socket bound wherever it is, it might
 762          * be bound to a privileged port. However, since there seems to
 763          * be a bug here, we will leave it if the port is not privileged.
 764          */
 765         if (snum == 0) 
 766         {
 767                 snum = get_new_socknum(sk->prot, 0);
 768         }
 769         if (snum < PROT_SOCK && !suser()) 
 770                 return(-EACCES);
 771 
 772         chk_addr_ret = ip_chk_addr(addr->sin_addr.s_addr);
 773         if (addr->sin_addr.s_addr != 0 && chk_addr_ret != IS_MYADDR)
 774                 return(-EADDRNOTAVAIL); /* Source address MUST be ours! */
 775         
 776         if (chk_addr_ret || addr->sin_addr.s_addr == 0)
 777                 sk->saddr = addr->sin_addr.s_addr;
 778 
 779         /* Make sure we are allowed to bind here. */
 780         cli();
 781 outside_loop:
 782         for(sk2 = sk->prot->sock_array[snum & (SOCK_ARRAY_SIZE -1)];
 783                                         sk2 != NULL; sk2 = sk2->next) 
 784         {
 785 /* should be below! */
 786                 if (sk2->num != snum) continue;
 787                 if (sk2->dead) 
 788                 {
 789                         destroy_sock(sk2);
 790                         goto outside_loop;
 791                 }
 792                 if (!sk->reuse) 
 793                 {
 794                         sti();
 795                         return(-EADDRINUSE);
 796                 }
 797                 
 798                 if (sk2->num != snum) 
 799                         continue;               /* more than one */
 800                 if (sk2->saddr != sk->saddr) 
 801                         continue;       /* socket per slot ! -FB */
 802                 if (!sk2->reuse) 
 803                 {
 804                         sti();
 805                         return(-EADDRINUSE);
 806                 }
 807         }
 808         sti();
 809 
 810         remove_sock(sk);
 811         put_sock(snum, sk);
 812         sk->dummy_th.source = ntohs(sk->num);
 813         sk->daddr = 0;
 814         sk->dummy_th.dest = 0;
 815         return(0);
 816 }
 817 
 818 /*
 819  *      Handle sk->err properly. The cli/sti matter.
 820  */
 821  
 822 static int inet_error(struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 823 {
 824         unsigned long flags;
 825         int err;
 826         save_flags(flags);
 827         cli();  
 828         err=sk->err;
 829         sk->err=0;
 830         sti();
 831         return -err;
 832 }
 833 
 834 /*
 835  *      Connect to a remote host. There is regretably still a little
 836  *      TCP 'magic' in here.
 837  */
 838  
 839 static int inet_connect(struct socket *sock, struct sockaddr * uaddr,
     /* [previous][next][first][last][top][bottom][index][help] */
 840                   int addr_len, int flags)
 841 {
 842         struct sock *sk=(struct sock *)sock->data;
 843         int err;
 844         sock->conn = NULL;
 845 
 846         if (sock->state == SS_CONNECTING && tcp_connected(sk->state))
 847         {
 848                 sock->state = SS_CONNECTED;
 849                 /* Connection completing after a connect/EINPROGRESS/select/connect */
 850                 return 0;       /* Rock and roll */
 851         }
 852 
 853         if (sock->state == SS_CONNECTING && sk->protocol == IPPROTO_TCP && (flags & O_NONBLOCK))
 854                 return -EALREADY;       /* Connecting is currently in progress */
 855         
 856         if (sock->state != SS_CONNECTING) 
 857         {
 858                 /* We may need to bind the socket. */
 859                 if(inet_autobind(sk)!=0)
 860                         return(-EAGAIN);
 861                 if (sk->prot->connect == NULL) 
 862                         return(-EOPNOTSUPP);
 863                 err = sk->prot->connect(sk, (struct sockaddr_in *)uaddr, addr_len);
 864                 if (err < 0) 
 865                         return(err);
 866                 sock->state = SS_CONNECTING;
 867         }
 868 
 869         if (sk->state != TCP_ESTABLISHED &&(flags & O_NONBLOCK)) 
 870                 return(-EINPROGRESS);
 871 
 872         cli(); /* avoid the race condition */
 873         while(sk->state == TCP_SYN_SENT || sk->state == TCP_SYN_RECV) 
 874         {
 875                 interruptible_sleep_on(sk->sleep);
 876                 if (current->signal & ~current->blocked) 
 877                 {
 878                         sti();
 879                         return(-ERESTARTSYS);
 880                 }
 881                 /* This fixes a nasty in the tcp/ip code. There is a hideous hassle with
 882                    icmp error packets wanting to close a tcp or udp socket. */
 883                 if(sk->err && sk->protocol == IPPROTO_TCP)
 884                 {
 885                         sti();
 886                         sock->state = SS_UNCONNECTED;
 887                         err = -sk->err;
 888                         sk->err=0;
 889                         return err; /* set by tcp_err() */
 890                 }
 891         }
 892         sti();
 893         sock->state = SS_CONNECTED;
 894 
 895         if (sk->state != TCP_ESTABLISHED && sk->err) 
 896         {
 897                 sock->state = SS_UNCONNECTED;
 898                 err=sk->err;
 899                 sk->err=0;
 900                 return(-err);
 901         }
 902         return(0);
 903 }
 904 
 905 
 906 static int inet_socketpair(struct socket *sock1, struct socket *sock2)
     /* [previous][next][first][last][top][bottom][index][help] */
 907 {
 908          return(-EOPNOTSUPP);
 909 }
 910 
 911 
 912 /*
 913  *      FIXME: Get BSD behaviour
 914  */
 915 
 916 static int inet_accept(struct socket *sock, struct socket *newsock, int flags)
     /* [previous][next][first][last][top][bottom][index][help] */
 917 {
 918         struct sock *sk1, *sk2;
 919         int err;
 920 
 921         sk1 = (struct sock *) sock->data;
 922 
 923         /*
 924          * We've been passed an extra socket.
 925          * We need to free it up because the tcp module creates
 926          * it's own when it accepts one.
 927          */
 928         if (newsock->data)
 929         {
 930                 struct sock *sk=(struct sock *)newsock->data;
 931                 newsock->data=NULL;
 932                 sk->dead = 1;
 933                 destroy_sock(sk);
 934         }
 935   
 936         if (sk1->prot->accept == NULL) 
 937                 return(-EOPNOTSUPP);
 938 
 939         /* Restore the state if we have been interrupted, and then returned. */
 940         if (sk1->pair != NULL ) 
 941         {
 942                 sk2 = sk1->pair;
 943                 sk1->pair = NULL;
 944         } 
 945         else
 946         {
 947                 sk2 = sk1->prot->accept(sk1,flags);
 948                 if (sk2 == NULL) 
 949                 {
 950                         if (sk1->err <= 0)
 951                                 printk("Warning sock.c:sk1->err <= 0.  Returning non-error.\n");
 952                         err=sk1->err;
 953                         sk1->err=0;
 954                         return(-err);
 955                 }
 956         }
 957         newsock->data = (void *)sk2;
 958         sk2->sleep = newsock->wait;
 959         newsock->conn = NULL;
 960         if (flags & O_NONBLOCK) 
 961                 return(0);
 962 
 963         cli(); /* avoid the race. */
 964         while(sk2->state == TCP_SYN_RECV) 
 965         {
 966                 interruptible_sleep_on(sk2->sleep);
 967                 if (current->signal & ~current->blocked) 
 968                 {
 969                         sti();
 970                         sk1->pair = sk2;
 971                         sk2->sleep = NULL;
 972                         newsock->data = NULL;
 973                         return(-ERESTARTSYS);
 974                 }
 975         }
 976         sti();
 977 
 978         if (sk2->state != TCP_ESTABLISHED && sk2->err > 0) 
 979         {
 980                 err = -sk2->err;
 981                 sk2->err=0;
 982                 sk2->dead=1;    /* ANK */
 983                 destroy_sock(sk2);
 984                 newsock->data = NULL;
 985                 return(err);
 986         }
 987         newsock->state = SS_CONNECTED;
 988         return(0);
 989 }
 990 
 991 
 992 /*
 993  *      This does both peername and sockname.
 994  */
 995  
 996 static int inet_getname(struct socket *sock, struct sockaddr *uaddr,
     /* [previous][next][first][last][top][bottom][index][help] */
 997                  int *uaddr_len, int peer)
 998 {
 999         struct sockaddr_in *sin=(struct sockaddr_in *)uaddr;
1000         struct sock *sk;
1001   
1002         sin->sin_family = AF_INET;
1003         sk = (struct sock *) sock->data;
1004         if (peer) 
1005         {
1006                 if (!tcp_connected(sk->state)) 
1007                         return(-ENOTCONN);
1008                 sin->sin_port = sk->dummy_th.dest;
1009                 sin->sin_addr.s_addr = sk->daddr;
1010         } 
1011         else 
1012         {
1013                 sin->sin_port = sk->dummy_th.source;
1014                 if (sk->saddr == 0) 
1015                         sin->sin_addr.s_addr = ip_my_addr();
1016                 else 
1017                         sin->sin_addr.s_addr = sk->saddr;
1018         }
1019         *uaddr_len = sizeof(*sin);
1020         return(0);
1021 }
1022 
1023 
1024 /*
1025  *      The assorted BSD I/O operations
1026  */
1027 
1028 static int inet_recvfrom(struct socket *sock, void *ubuf, int size, int noblock, 
     /* [previous][next][first][last][top][bottom][index][help] */
1029                    unsigned flags, struct sockaddr *sin, int *addr_len )
1030 {
1031         struct sock *sk = (struct sock *) sock->data;
1032         
1033         if (sk->prot->recvfrom == NULL) 
1034                 return(-EOPNOTSUPP);
1035         if(sk->err)
1036                 return inet_error(sk);
1037         /* We may need to bind the socket. */
1038         if(inet_autobind(sk)!=0)
1039                 return(-EAGAIN);
1040         return(sk->prot->recvfrom(sk, (unsigned char *) ubuf, size, noblock, flags,
1041                              (struct sockaddr_in*)sin, addr_len));
1042 }
1043 
1044 
1045 static int inet_recv(struct socket *sock, void *ubuf, int size, int noblock,
     /* [previous][next][first][last][top][bottom][index][help] */
1046           unsigned flags)
1047 {
1048         /* BSD explicitly states these are the same - so we do it this way to be sure */
1049         return inet_recvfrom(sock,ubuf,size,noblock,flags,NULL,NULL);
1050 }
1051 
1052 static int inet_read(struct socket *sock, char *ubuf, int size, int noblock)
     /* [previous][next][first][last][top][bottom][index][help] */
1053 {
1054         struct sock *sk = (struct sock *) sock->data;
1055         
1056         if(sk->err)
1057                 return inet_error(sk);
1058         /* We may need to bind the socket. */
1059         if(inet_autobind(sk))
1060                 return(-EAGAIN);        
1061         return(sk->prot->read(sk, (unsigned char *) ubuf, size, noblock, 0));
1062 }
1063 
1064 static int inet_send(struct socket *sock, void *ubuf, int size, int noblock, 
     /* [previous][next][first][last][top][bottom][index][help] */
1065                unsigned flags)
1066 {
1067         struct sock *sk = (struct sock *) sock->data;
1068         if (sk->shutdown & SEND_SHUTDOWN) 
1069         {
1070                 send_sig(SIGPIPE, current, 1);
1071                 return(-EPIPE);
1072         }
1073         if(sk->err)
1074                 return inet_error(sk);
1075         /* We may need to bind the socket. */
1076         if(inet_autobind(sk)!=0)
1077                 return(-EAGAIN);
1078         return(sk->prot->write(sk, (unsigned char *) ubuf, size, noblock, flags));
1079 }
1080 
1081 static int inet_write(struct socket *sock, char *ubuf, int size, int noblock)
     /* [previous][next][first][last][top][bottom][index][help] */
1082 {
1083         return inet_send(sock,ubuf,size,noblock,0);
1084 }
1085 
1086 static int inet_sendto(struct socket *sock, void *ubuf, int size, int noblock, 
     /* [previous][next][first][last][top][bottom][index][help] */
1087             unsigned flags, struct sockaddr *sin, int addr_len)
1088 {
1089         struct sock *sk = (struct sock *) sock->data;
1090         if (sk->shutdown & SEND_SHUTDOWN) 
1091         {
1092                 send_sig(SIGPIPE, current, 1);
1093                 return(-EPIPE);
1094         }
1095         if (sk->prot->sendto == NULL) 
1096                 return(-EOPNOTSUPP);
1097         if(sk->err)
1098                 return inet_error(sk);
1099         /* We may need to bind the socket. */
1100         if(inet_autobind(sk)!=0)
1101                 return -EAGAIN;
1102         return(sk->prot->sendto(sk, (unsigned char *) ubuf, size, noblock, flags, 
1103                            (struct sockaddr_in *)sin, addr_len));
1104 }
1105 
1106 
1107 static int inet_shutdown(struct socket *sock, int how)
     /* [previous][next][first][last][top][bottom][index][help] */
1108 {
1109         struct sock *sk=(struct sock*)sock->data;
1110 
1111         /*
1112          * This should really check to make sure
1113          * the socket is a TCP socket. (WHY AC...)
1114          */
1115         how++; /* maps 0->1 has the advantage of making bit 1 rcvs and
1116                        1->2 bit 2 snds.
1117                        2->3 */
1118         if ((how & ~SHUTDOWN_MASK) || how==0)   /* MAXINT->0 */
1119                 return(-EINVAL);
1120         if (sock->state == SS_CONNECTING && sk->state == TCP_ESTABLISHED)
1121                 sock->state = SS_CONNECTED;
1122         if (!tcp_connected(sk->state)) 
1123                 return(-ENOTCONN);
1124         sk->shutdown |= how;
1125         if (sk->prot->shutdown)
1126                 sk->prot->shutdown(sk, how);
1127         return(0);
1128 }
1129 
1130 
1131 static int inet_select(struct socket *sock, int sel_type, select_table *wait )
     /* [previous][next][first][last][top][bottom][index][help] */
1132 {
1133         struct sock *sk=(struct sock *) sock->data;
1134         if (sk->prot->select == NULL) 
1135         {
1136                 return(0);
1137         }
1138         return(sk->prot->select(sk, sel_type, wait));
1139 }
1140 
1141 /*
1142  *      ioctl() calls you can issue on an INET socket. Most of these are
1143  *      device configuration and stuff and very rarely used. Some ioctls
1144  *      pass on to the socket itself.
1145  *
1146  *      NOTE: I like the idea of a module for the config stuff. ie ifconfig
1147  *      loads the devconfigure module does its configuring and unloads it.
1148  *      Theres a good 20K of config code hanging around the kernel.
1149  */
1150 
1151 static int inet_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
     /* [previous][next][first][last][top][bottom][index][help] */
1152 {
1153         struct sock *sk=(struct sock *)sock->data;
1154         int err;
1155 
1156         switch(cmd) 
1157         {
1158                 case FIOSETOWN:
1159                 case SIOCSPGRP:
1160                         err=verify_area(VERIFY_READ,(int *)arg,sizeof(long));
1161                         if(err)
1162                                 return err;
1163                         sk->proc = get_fs_long((int *) arg);
1164                         return(0);
1165                 case FIOGETOWN:
1166                 case SIOCGPGRP:
1167                         err=verify_area(VERIFY_WRITE,(void *) arg, sizeof(long));
1168                         if(err)
1169                                 return err;
1170                         put_fs_long(sk->proc,(int *)arg);
1171                         return(0);                      
1172                 case SIOCGSTAMP:
1173                         if(sk->stamp.tv_sec==0)
1174                                 return -ENOENT;
1175                         err=verify_area(VERIFY_WRITE,(void *)arg,sizeof(struct timeval));
1176                         if(err)
1177                                 return err;
1178                         memcpy_tofs((void *)arg,&sk->stamp,sizeof(struct timeval));
1179                         return 0;
1180                 case SIOCADDRT: case SIOCADDRTOLD:
1181                 case SIOCDELRT: case SIOCDELRTOLD:
1182                         return(ip_rt_ioctl(cmd,(void *) arg));
1183                 case SIOCDARP:
1184                 case SIOCGARP:
1185                 case SIOCSARP:
1186                         return(arp_ioctl(cmd,(void *) arg));
1187 #ifdef CONFIG_INET_RARP                 
1188                 case SIOCDRARP:
1189                 case SIOCGRARP:
1190                 case SIOCSRARP:
1191                         return(rarp_ioctl(cmd,(void *) arg));
1192 #endif
1193                 case SIOCGIFCONF:
1194                 case SIOCGIFFLAGS:
1195                 case SIOCSIFFLAGS:
1196                 case SIOCGIFADDR:
1197                 case SIOCSIFADDR:
1198                 case SIOCGIFDSTADDR:
1199                 case SIOCSIFDSTADDR:
1200                 case SIOCGIFBRDADDR:
1201                 case SIOCSIFBRDADDR:
1202                 case SIOCGIFNETMASK:
1203                 case SIOCSIFNETMASK:
1204                 case SIOCGIFMETRIC:
1205                 case SIOCSIFMETRIC:
1206                 case SIOCGIFMEM:
1207                 case SIOCSIFMEM:
1208                 case SIOCGIFMTU:
1209                 case SIOCSIFMTU:
1210                 case SIOCSIFLINK:
1211                 case SIOCGIFHWADDR:
1212                 case SIOCSIFHWADDR:
1213                 case OLD_SIOCGIFHWADDR:
1214                 case SIOCSIFMAP:
1215                 case SIOCGIFMAP:
1216                 case SIOCDEVPRIVATE:
1217                 case SIOCSIFSLAVE:
1218                 case SIOCGIFSLAVE:
1219                         return(dev_ioctl(cmd,(void *) arg));
1220 
1221                 default:
1222                         if (sk->prot->ioctl==NULL) 
1223                                 return(-EINVAL);
1224                         return(sk->prot->ioctl(sk, cmd, arg));
1225         }
1226         /*NOTREACHED*/
1227         return(0);
1228 }
1229 
1230 /*
1231  * This routine must find a socket given a TCP or UDP header.
1232  * Everyhting is assumed to be in net order.
1233  */
1234 
1235 struct sock *get_sock(struct proto *prot, unsigned short num,
     /* [previous][next][first][last][top][bottom][index][help] */
1236                                 unsigned long raddr,
1237                                 unsigned short rnum, unsigned long laddr)
1238 {
1239         struct sock *s;
1240         unsigned short hnum;
1241 
1242         hnum = ntohs(num);
1243 
1244         /*
1245          * SOCK_ARRAY_SIZE must be a power of two.  This will work better
1246          * than a prime unless 3 or more sockets end up using the same
1247          * array entry.  This should not be a problem because most
1248          * well known sockets don't overlap that much, and for
1249          * the other ones, we can just be careful about picking our
1250          * socket number when we choose an arbitrary one.
1251          */
1252 
1253         for(s = prot->sock_array[hnum & (SOCK_ARRAY_SIZE - 1)];
1254                         s != NULL; s = s->next) 
1255         {
1256                 if (s->num != hnum) 
1257                         continue;
1258                 if(s->dead && (s->state == TCP_CLOSE))
1259                         continue;
1260                 if(prot == &udp_prot)
1261                         return s;
1262                 if(ip_addr_match(s->daddr,raddr)==0)
1263                         continue;
1264                 if (s->dummy_th.dest != rnum && s->dummy_th.dest != 0) 
1265                         continue;
1266                 if(ip_addr_match(s->saddr,laddr) == 0)
1267                         continue;
1268                 return(s);
1269         }
1270         return(NULL);
1271 }
1272 
1273 static struct proto_ops inet_proto_ops = {
1274         AF_INET,
1275 
1276         inet_create,
1277         inet_dup,
1278         inet_release,
1279         inet_bind,
1280         inet_connect,
1281         inet_socketpair,
1282         inet_accept,
1283         inet_getname, 
1284         inet_read,
1285         inet_write,
1286         inet_select,
1287         inet_ioctl,
1288         inet_listen,
1289         inet_send,
1290         inet_recv,
1291         inet_sendto,
1292         inet_recvfrom,
1293         inet_shutdown,
1294         inet_setsockopt,
1295         inet_getsockopt,
1296         inet_fcntl,
1297 };
1298 
1299 extern unsigned long seq_offset;
1300 
1301 /*
1302  *      Called by socket.c on kernel startup.  
1303  */
1304  
1305 void inet_proto_init(struct net_proto *pro)
     /* [previous][next][first][last][top][bottom][index][help] */
1306 {
1307         struct inet_protocol *p;
1308         int i;
1309 
1310 
1311         printk("NET3 TCP/IP protocols stack v016\n");
1312 
1313         /*
1314          *      Tell SOCKET that we are alive... 
1315          */
1316    
1317         (void) sock_register(inet_proto_ops.family, &inet_proto_ops);
1318 
1319         seq_offset = CURRENT_TIME*250;
1320 
1321         /*
1322          *      Add all the protocols. 
1323          */
1324          
1325         for(i = 0; i < SOCK_ARRAY_SIZE; i++) 
1326         {
1327                 tcp_prot.sock_array[i] = NULL;
1328                 udp_prot.sock_array[i] = NULL;
1329                 raw_prot.sock_array[i] = NULL;
1330         }
1331 
1332         printk("IP Protocols: ");
1333         for(p = inet_protocol_base; p != NULL;) 
1334         {
1335                 struct inet_protocol *tmp = (struct inet_protocol *) p->next;
1336                 inet_add_protocol(p);
1337                 printk("%s%s",p->name,tmp?", ":"\n");
1338                 p = tmp;
1339         }
1340         /*
1341          *      Set the ARP module up
1342          */
1343         arp_init();
1344         /*
1345          *      Set the IP module up
1346          */
1347         ip_init();
1348 }
1349 

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