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_recv
  23. inet_read
  24. inet_send
  25. inet_write
  26. inet_sendto
  27. inet_recvfrom
  28. inet_shutdown
  29. inet_select
  30. inet_ioctl
  31. get_sock
  32. inet_proto_init

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

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