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

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