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

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