linux-bk/net/ipv4/af_inet.c
<<
>>
Prefs
   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 *              PF_INET protocol family socket handler.
   7 *
   8 * Version:     $Id: af_inet.c,v 1.137 2002/02/01 22:01:03 davem Exp $
   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 *              piggy,
  18 *              Karl Knutson    :       Socket protocol table
  19 *              A.N.Kuznetsov   :       Socket death error in accept().
  20 *              John Richardson :       Fix non blocking error in connect()
  21 *                                      so sockets that fail to connect
  22 *                                      don't return -EINPROGRESS.
  23 *              Alan Cox        :       Asynchronous I/O support
  24 *              Alan Cox        :       Keep correct socket pointer on sock
  25 *                                      structures
  26 *                                      when accept() ed
  27 *              Alan Cox        :       Semantics of SO_LINGER aren't state
  28 *                                      moved to close when you look carefully.
  29 *                                      With this fixed and the accept bug fixed
  30 *                                      some RPC stuff seems happier.
  31 *              Niibe Yutaka    :       4.4BSD style write async I/O
  32 *              Alan Cox,
  33 *              Tony Gale       :       Fixed reuse semantics.
  34 *              Alan Cox        :       bind() shouldn't abort existing but dead
  35 *                                      sockets. Stops FTP netin:.. I hope.
  36 *              Alan Cox        :       bind() works correctly for RAW sockets.
  37 *                                      Note that FreeBSD at least was broken
  38 *                                      in this respect so be careful with
  39 *                                      compatibility tests...
  40 *              Alan Cox        :       routing cache support
  41 *              Alan Cox        :       memzero the socket structure for
  42 *                                      compactness.
  43 *              Matt Day        :       nonblock connect error handler
  44 *              Alan Cox        :       Allow large numbers of pending sockets
  45 *                                      (eg for big web sites), but only if
  46 *                                      specifically application requested.
  47 *              Alan Cox        :       New buffering throughout IP. Used
  48 *                                      dumbly.
  49 *              Alan Cox        :       New buffering now used smartly.
  50 *              Alan Cox        :       BSD rather than common sense
  51 *                                      interpretation of listen.
  52 *              Germano Caronni :       Assorted small races.
  53 *              Alan Cox        :       sendmsg/recvmsg basic support.
  54 *              Alan Cox        :       Only sendmsg/recvmsg now supported.
  55 *              Alan Cox        :       Locked down bind (see security list).
  56 *              Alan Cox        :       Loosened bind a little.
  57 *              Mike McLagan    :       ADD/DEL DLCI Ioctls
  58 *      Willy Konynenberg       :       Transparent proxying support.
  59 *              David S. Miller :       New socket lookup architecture.
  60 *                                      Some other random speedups.
  61 *              Cyrus Durgin    :       Cleaned up file for kmod hacks.
  62 *              Andi Kleen      :       Fix inet_stream_connect TCP race.
  63 *
  64 *              This program is free software; you can redistribute it and/or
  65 *              modify it under the terms of the GNU General Public License
  66 *              as published by the Free Software Foundation; either version
  67 *              2 of the License, or (at your option) any later version.
  68 */
  69
  70#include <linux/config.h>
  71#include <linux/errno.h>
  72#include <linux/types.h>
  73#include <linux/socket.h>
  74#include <linux/in.h>
  75#include <linux/kernel.h>
  76#include <linux/major.h>
  77#include <linux/module.h>
  78#include <linux/sched.h>
  79#include <linux/timer.h>
  80#include <linux/string.h>
  81#include <linux/sockios.h>
  82#include <linux/net.h>
  83#include <linux/fcntl.h>
  84#include <linux/mm.h>
  85#include <linux/interrupt.h>
  86#include <linux/stat.h>
  87#include <linux/init.h>
  88#include <linux/poll.h>
  89#include <linux/netfilter_ipv4.h>
  90
  91#include <asm/uaccess.h>
  92#include <asm/system.h>
  93
  94#include <linux/smp_lock.h>
  95#include <linux/inet.h>
  96#include <linux/igmp.h>
  97#include <linux/netdevice.h>
  98#include <net/ip.h>
  99#include <net/protocol.h>
 100#include <net/arp.h>
 101#include <net/route.h>
 102#include <net/ip_fib.h>
 103#include <net/tcp.h>
 104#include <net/udp.h>
 105#include <linux/skbuff.h>
 106#include <net/sock.h>
 107#include <net/raw.h>
 108#include <net/icmp.h>
 109#include <net/ipip.h>
 110#include <net/inet_common.h>
 111#include <net/xfrm.h>
 112#ifdef CONFIG_IP_MROUTE
 113#include <linux/mroute.h>
 114#endif
 115
 116DEFINE_SNMP_STAT(struct linux_mib, net_statistics);
 117
 118#ifdef INET_REFCNT_DEBUG
 119atomic_t inet_sock_nr;
 120#endif
 121
 122extern void ip_mc_drop_socket(struct sock *sk);
 123
 124/* Per protocol sock slabcache */
 125kmem_cache_t *tcp_sk_cachep;
 126static kmem_cache_t *udp_sk_cachep;
 127static kmem_cache_t *raw4_sk_cachep;
 128
 129/* The inetsw table contains everything that inet_create needs to
 130 * build a new socket.
 131 */
 132static struct list_head inetsw[SOCK_MAX];
 133static spinlock_t inetsw_lock = SPIN_LOCK_UNLOCKED;
 134
 135/* New destruction routine */
 136
 137void inet_sock_destruct(struct sock *sk)
 138{
 139        struct inet_opt *inet = inet_sk(sk);
 140
 141        __skb_queue_purge(&sk->sk_receive_queue);
 142        __skb_queue_purge(&sk->sk_error_queue);
 143
 144        if (sk->sk_type == SOCK_STREAM && sk->sk_state != TCP_CLOSE) {
 145                printk("Attempt to release TCP socket in state %d %p\n",
 146                       sk->sk_state, sk);
 147                return;
 148        }
 149        if (!sock_flag(sk, SOCK_DEAD)) {
 150                printk("Attempt to release alive inet socket %p\n", sk);
 151                return;
 152        }
 153
 154        BUG_TRAP(!atomic_read(&sk->sk_rmem_alloc));
 155        BUG_TRAP(!atomic_read(&sk->sk_wmem_alloc));
 156        BUG_TRAP(!sk->sk_wmem_queued);
 157        BUG_TRAP(!sk->sk_forward_alloc);
 158
 159        if (inet->opt)
 160                kfree(inet->opt);
 161        dst_release(sk->sk_dst_cache);
 162#ifdef INET_REFCNT_DEBUG
 163        atomic_dec(&inet_sock_nr);
 164        printk(KERN_DEBUG "INET socket %p released, %d are still alive\n",
 165               sk, atomic_read(&inet_sock_nr));
 166#endif
 167}
 168
 169void inet_sock_release(struct sock *sk)
 170{
 171        if (sk->sk_prot->destroy)
 172                sk->sk_prot->destroy(sk);
 173
 174        /* Observation: when inet_sock_release is called, processes have
 175         * no access to socket. But net still has.
 176         * Step one, detach it from networking:
 177         *
 178         * A. Remove from hash tables.
 179         */
 180
 181        sk->sk_prot->unhash(sk);
 182
 183        /* In this point socket cannot receive new packets,
 184         * but it is possible that some packets are in flight
 185         * because some CPU runs receiver and did hash table lookup
 186         * before we unhashed socket. They will achieve receive queue
 187         * and will be purged by socket destructor.
 188         *
 189         * Also we still have packets pending on receive
 190         * queue and probably, our own packets waiting in device queues.
 191         * sock_destroy will drain receive queue, but transmitted
 192         * packets will delay socket destruction until the last reference
 193         * will be released.
 194         */
 195
 196        sock_orphan(sk);
 197
 198        xfrm_sk_free_policy(sk);
 199
 200#ifdef INET_REFCNT_DEBUG
 201        if (atomic_read(&sk->sk_refcnt) != 1)
 202                printk(KERN_DEBUG "Destruction inet %p delayed, c=%d\n",
 203                       sk, atomic_read(&sk->sk_refcnt));
 204#endif
 205        sock_put(sk);
 206}
 207
 208
 209/*
 210 *      The routines beyond this point handle the behaviour of an AF_INET
 211 *      socket object. Mostly it punts to the subprotocols of IP to do
 212 *      the work.
 213 */
 214
 215/*
 216 *      Set socket options on an inet socket.
 217 */
 218int inet_setsockopt(struct socket *sock, int level, int optname,
 219                    char *optval, int optlen)
 220{
 221        struct sock *sk = sock->sk;
 222
 223        return sk->sk_prot->setsockopt(sk, level, optname, optval, optlen);
 224}
 225
 226/*
 227 *      Get a socket option on an AF_INET socket.
 228 *
 229 *      FIX: POSIX 1003.1g is very ambiguous here. It states that
 230 *      asynchronous errors should be reported by getsockopt. We assume
 231 *      this means if you specify SO_ERROR (otherwise whats the point of it).
 232 */
 233
 234int inet_getsockopt(struct socket *sock, int level, int optname,
 235                    char *optval, int *optlen)
 236{
 237        struct sock *sk = sock->sk;
 238
 239        return sk->sk_prot->getsockopt(sk, level, optname, optval, optlen);
 240}
 241
 242/*
 243 *      Automatically bind an unbound socket.
 244 */
 245
 246static int inet_autobind(struct sock *sk)
 247{
 248        struct inet_opt *inet;
 249        /* We may need to bind the socket. */
 250        lock_sock(sk);
 251        inet = inet_sk(sk);
 252        if (!inet->num) {
 253                if (sk->sk_prot->get_port(sk, 0)) {
 254                        release_sock(sk);
 255                        return -EAGAIN;
 256                }
 257                inet->sport = htons(inet->num);
 258        }
 259        release_sock(sk);
 260        return 0;
 261}
 262
 263/*
 264 *      Move a socket into listening state.
 265 */
 266int inet_listen(struct socket *sock, int backlog)
 267{
 268        struct sock *sk = sock->sk;
 269        unsigned char old_state;
 270        int err;
 271
 272        lock_sock(sk);
 273
 274        err = -EINVAL;
 275        if (sock->state != SS_UNCONNECTED || sock->type != SOCK_STREAM)
 276                goto out;
 277
 278        old_state = sk->sk_state;
 279        if (!((1 << old_state) & (TCPF_CLOSE | TCPF_LISTEN)))
 280                goto out;
 281
 282        /* Really, if the socket is already in listen state
 283         * we can only allow the backlog to be adjusted.
 284         */
 285        if (old_state != TCP_LISTEN) {
 286                err = tcp_listen_start(sk);
 287                if (err)
 288                        goto out;
 289        }
 290        sk->sk_max_ack_backlog = backlog;
 291        err = 0;
 292
 293out:
 294        release_sock(sk);
 295        return err;
 296}
 297
 298static __inline__ kmem_cache_t *inet_sk_slab(int protocol)
 299{
 300        kmem_cache_t* rc = tcp_sk_cachep;
 301
 302        if (protocol == IPPROTO_UDP)
 303                rc = udp_sk_cachep;
 304        else if (protocol == IPPROTO_RAW)
 305                rc = raw4_sk_cachep;
 306        return rc;
 307}
 308
 309static __inline__ int inet_sk_size(int protocol)
 310{
 311        int rc = sizeof(struct tcp_sock);
 312
 313        if (protocol == IPPROTO_UDP)
 314                rc = sizeof(struct udp_sock);
 315        else if (protocol == IPPROTO_RAW)
 316                rc = sizeof(struct raw_sock);
 317        return rc;
 318}
 319
 320/*
 321 *      Create an inet socket.
 322 */
 323
 324static int inet_create(struct socket *sock, int protocol)
 325{
 326        struct sock *sk;
 327        struct list_head *p;
 328        struct inet_protosw *answer;
 329        struct inet_opt *inet;
 330        int err = -ENOBUFS;
 331
 332        sock->state = SS_UNCONNECTED;
 333        sk = sk_alloc(PF_INET, GFP_KERNEL, inet_sk_size(protocol),
 334                      inet_sk_slab(protocol));
 335        if (!sk)
 336                goto out;
 337
 338        /* Look for the requested type/protocol pair. */
 339        answer = NULL;
 340        rcu_read_lock();
 341        list_for_each_rcu(p, &inetsw[sock->type]) {
 342                answer = list_entry(p, struct inet_protosw, list);
 343
 344                /* Check the non-wild match. */
 345                if (protocol == answer->protocol) {
 346                        if (protocol != IPPROTO_IP)
 347                                break;
 348                } else {
 349                        /* Check for the two wild cases. */
 350                        if (IPPROTO_IP == protocol) {
 351                                protocol = answer->protocol;
 352                                break;
 353                        }
 354                        if (IPPROTO_IP == answer->protocol)
 355                                break;
 356                }
 357                answer = NULL;
 358        }
 359
 360        err = -ESOCKTNOSUPPORT;
 361        if (!answer)
 362                goto out_sk_free;
 363        err = -EPERM;
 364        if (answer->capability > 0 && !capable(answer->capability))
 365                goto out_sk_free;
 366        err = -EPROTONOSUPPORT;
 367        if (!protocol)
 368                goto out_sk_free;
 369        err = 0;
 370        sock->ops = answer->ops;
 371        sk->sk_prot = answer->prot;
 372        sk->sk_no_check = answer->no_check;
 373        if (INET_PROTOSW_REUSE & answer->flags)
 374                sk->sk_reuse = 1;
 375        rcu_read_unlock();
 376
 377        inet = inet_sk(sk);
 378
 379        if (SOCK_RAW == sock->type) {
 380                inet->num = protocol;
 381                if (IPPROTO_RAW == protocol)
 382                        inet->hdrincl = 1;
 383        }
 384
 385        if (ipv4_config.no_pmtu_disc)
 386                inet->pmtudisc = IP_PMTUDISC_DONT;
 387        else
 388                inet->pmtudisc = IP_PMTUDISC_WANT;
 389
 390        inet->id = 0;
 391
 392        sock_init_data(sock, sk);
 393        sk_set_owner(sk, THIS_MODULE);
 394
 395        sk->sk_destruct    = inet_sock_destruct;
 396        sk->sk_zapped      = 0;
 397        sk->sk_family      = PF_INET;
 398        sk->sk_protocol    = protocol;
 399        sk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
 400
 401        inet->uc_ttl    = -1;
 402        inet->mc_loop   = 1;
 403        inet->mc_ttl    = 1;
 404        inet->mc_index  = 0;
 405        inet->mc_list   = NULL;
 406
 407#ifdef INET_REFCNT_DEBUG
 408        atomic_inc(&inet_sock_nr);
 409#endif
 410
 411        if (inet->num) {
 412                /* It assumes that any protocol which allows
 413                 * the user to assign a number at socket
 414                 * creation time automatically
 415                 * shares.
 416                 */
 417                inet->sport = htons(inet->num);
 418                /* Add to protocol hash chains. */
 419                sk->sk_prot->hash(sk);
 420        }
 421
 422        if (sk->sk_prot->init) {
 423                err = sk->sk_prot->init(sk);
 424                if (err)
 425                        inet_sock_release(sk);
 426        }
 427out:
 428        return err;
 429out_sk_free:
 430        rcu_read_unlock();
 431        sk_free(sk);
 432        goto out;
 433}
 434
 435
 436/*
 437 *      The peer socket should always be NULL (or else). When we call this
 438 *      function we are destroying the object and from then on nobody
 439 *      should refer to it.
 440 */
 441int inet_release(struct socket *sock)
 442{
 443        struct sock *sk = sock->sk;
 444
 445        if (sk) {
 446                long timeout;
 447
 448                /* Applications forget to leave groups before exiting */
 449                ip_mc_drop_socket(sk);
 450
 451                /* If linger is set, we don't return until the close
 452                 * is complete.  Otherwise we return immediately. The
 453                 * actually closing is done the same either way.
 454                 *
 455                 * If the close is due to the process exiting, we never
 456                 * linger..
 457                 */
 458                timeout = 0;
 459                if (sock_flag(sk, SOCK_LINGER) &&
 460                    !(current->flags & PF_EXITING))
 461                        timeout = sk->sk_lingertime;
 462                sock->sk = NULL;
 463                sk->sk_prot->close(sk, timeout);
 464        }
 465        return 0;
 466}
 467
 468/* It is off by default, see below. */
 469int sysctl_ip_nonlocal_bind;
 470
 471int inet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
 472{
 473        struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
 474        struct sock *sk = sock->sk;
 475        struct inet_opt *inet = inet_sk(sk);
 476        unsigned short snum;
 477        int chk_addr_ret;
 478        int err;
 479
 480        /* If the socket has its own bind function then use it. (RAW) */
 481        if (sk->sk_prot->bind) {
 482                err = sk->sk_prot->bind(sk, uaddr, addr_len);
 483                goto out;
 484        }
 485        err = -EINVAL;
 486        if (addr_len < sizeof(struct sockaddr_in))
 487                goto out;
 488
 489        chk_addr_ret = inet_addr_type(addr->sin_addr.s_addr);
 490
 491        /* Not specified by any standard per-se, however it breaks too
 492         * many applications when removed.  It is unfortunate since
 493         * allowing applications to make a non-local bind solves
 494         * several problems with systems using dynamic addressing.
 495         * (ie. your servers still start up even if your ISDN link
 496         *  is temporarily down)
 497         */
 498        err = -EADDRNOTAVAIL;
 499        if (!sysctl_ip_nonlocal_bind &&
 500            !inet->freebind &&
 501            addr->sin_addr.s_addr != INADDR_ANY &&
 502            chk_addr_ret != RTN_LOCAL &&
 503            chk_addr_ret != RTN_MULTICAST &&
 504            chk_addr_ret != RTN_BROADCAST)
 505                goto out;
 506
 507        snum = ntohs(addr->sin_port);
 508        err = -EACCES;
 509        if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
 510                goto out;
 511
 512        /*      We keep a pair of addresses. rcv_saddr is the one
 513         *      used by hash lookups, and saddr is used for transmit.
 514         *
 515         *      In the BSD API these are the same except where it
 516         *      would be illegal to use them (multicast/broadcast) in
 517         *      which case the sending device address is used.
 518         */
 519        lock_sock(sk);
 520
 521        /* Check these errors (active socket, double bind). */
 522        err = -EINVAL;
 523        if (sk->sk_state != TCP_CLOSE || inet->num)
 524                goto out_release_sock;
 525
 526        inet->rcv_saddr = inet->saddr = addr->sin_addr.s_addr;
 527        if (chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST)
 528                inet->saddr = 0;  /* Use device */
 529
 530        /* Make sure we are allowed to bind here. */
 531        if (sk->sk_prot->get_port(sk, snum)) {
 532                inet->saddr = inet->rcv_saddr = 0;
 533                err = -EADDRINUSE;
 534                goto out_release_sock;
 535        }
 536
 537        if (inet->rcv_saddr)
 538                sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
 539        if (snum)
 540                sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
 541        inet->sport = htons(inet->num);
 542        inet->daddr = 0;
 543        inet->dport = 0;
 544        sk_dst_reset(sk);
 545        err = 0;
 546out_release_sock:
 547        release_sock(sk);
 548out:
 549        return err;
 550}
 551
 552int inet_dgram_connect(struct socket *sock, struct sockaddr * uaddr,
 553                       int addr_len, int flags)
 554{
 555        struct sock *sk = sock->sk;
 556
 557        if (uaddr->sa_family == AF_UNSPEC)
 558                return sk->sk_prot->disconnect(sk, flags);
 559
 560        if (!inet_sk(sk)->num && inet_autobind(sk))
 561                return -EAGAIN;
 562        return sk->sk_prot->connect(sk, (struct sockaddr *)uaddr, addr_len);
 563}
 564
 565static long inet_wait_for_connect(struct sock *sk, long timeo)
 566{
 567        DEFINE_WAIT(wait);
 568
 569        prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
 570
 571        /* Basic assumption: if someone sets sk->sk_err, he _must_
 572         * change state of the socket from TCP_SYN_*.
 573         * Connect() does not allow to get error notifications
 574         * without closing the socket.
 575         */
 576        while ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
 577                release_sock(sk);
 578                timeo = schedule_timeout(timeo);
 579                lock_sock(sk);
 580                if (signal_pending(current) || !timeo)
 581                        break;
 582                prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
 583        }
 584        finish_wait(sk->sk_sleep, &wait);
 585        return timeo;
 586}
 587
 588/*
 589 *      Connect to a remote host. There is regrettably still a little
 590 *      TCP 'magic' in here.
 591 */
 592int inet_stream_connect(struct socket *sock, struct sockaddr *uaddr,
 593                        int addr_len, int flags)
 594{
 595        struct sock *sk = sock->sk;
 596        int err;
 597        long timeo;
 598
 599        lock_sock(sk);
 600
 601        if (uaddr->sa_family == AF_UNSPEC) {
 602                err = sk->sk_prot->disconnect(sk, flags);
 603                sock->state = err ? SS_DISCONNECTING : SS_UNCONNECTED;
 604                goto out;
 605        }
 606
 607        switch (sock->state) {
 608        default:
 609                err = -EINVAL;
 610                goto out;
 611        case SS_CONNECTED:
 612                err = -EISCONN;
 613                goto out;
 614        case SS_CONNECTING:
 615                err = -EALREADY;
 616                /* Fall out of switch with err, set for this state */
 617                break;
 618        case SS_UNCONNECTED:
 619                err = -EISCONN;
 620                if (sk->sk_state != TCP_CLOSE)
 621                        goto out;
 622
 623                err = sk->sk_prot->connect(sk, uaddr, addr_len);
 624                if (err < 0)
 625                        goto out;
 626
 627                sock->state = SS_CONNECTING;
 628
 629                /* Just entered SS_CONNECTING state; the only
 630                 * difference is that return value in non-blocking
 631                 * case is EINPROGRESS, rather than EALREADY.
 632                 */
 633                err = -EINPROGRESS;
 634                break;
 635        }
 636
 637        timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
 638
 639        if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
 640                /* Error code is set above */
 641                if (!timeo || !inet_wait_for_connect(sk, timeo))
 642                        goto out;
 643
 644                err = sock_intr_errno(timeo);
 645                if (signal_pending(current))
 646                        goto out;
 647        }
 648
 649        /* Connection was closed by RST, timeout, ICMP error
 650         * or another process disconnected us.
 651         */
 652        if (sk->sk_state == TCP_CLOSE)
 653                goto sock_error;
 654
 655        /* sk->sk_err may be not zero now, if RECVERR was ordered by user
 656         * and error was received after socket entered established state.
 657         * Hence, it is handled normally after connect() return successfully.
 658         */
 659
 660        sock->state = SS_CONNECTED;
 661        err = 0;
 662out:
 663        release_sock(sk);
 664        return err;
 665
 666sock_error:
 667        err = sock_error(sk) ? : -ECONNABORTED;
 668        sock->state = SS_UNCONNECTED;
 669        if (sk->sk_prot->disconnect(sk, flags))
 670                sock->state = SS_DISCONNECTING;
 671        goto out;
 672}
 673
 674/*
 675 *      Accept a pending connection. The TCP layer now gives BSD semantics.
 676 */
 677
 678int inet_accept(struct socket *sock, struct socket *newsock, int flags)
 679{
 680        struct sock *sk1 = sock->sk;
 681        int err = -EINVAL;
 682        struct sock *sk2 = sk1->sk_prot->accept(sk1, flags, &err);
 683
 684        if (!sk2)
 685                goto do_err;
 686
 687        lock_sock(sk2);
 688
 689        BUG_TRAP((1 << sk2->sk_state) &
 690                 (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT | TCPF_CLOSE));
 691
 692        sock_graft(sk2, newsock);
 693
 694        newsock->state = SS_CONNECTED;
 695        err = 0;
 696        release_sock(sk2);
 697do_err:
 698        return err;
 699}
 700
 701
 702/*
 703 *      This does both peername and sockname.
 704 */
 705int inet_getname(struct socket *sock, struct sockaddr *uaddr,
 706                        int *uaddr_len, int peer)
 707{
 708        struct sock *sk         = sock->sk;
 709        struct inet_opt *inet   = inet_sk(sk);
 710        struct sockaddr_in *sin = (struct sockaddr_in *)uaddr;
 711
 712        sin->sin_family = AF_INET;
 713        if (peer) {
 714                if (!inet->dport ||
 715                    (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
 716                     peer == 1))
 717                        return -ENOTCONN;
 718                sin->sin_port = inet->dport;
 719                sin->sin_addr.s_addr = inet->daddr;
 720        } else {
 721                __u32 addr = inet->rcv_saddr;
 722                if (!addr)
 723                        addr = inet->saddr;
 724                sin->sin_port = inet->sport;
 725                sin->sin_addr.s_addr = addr;
 726        }
 727        memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
 728        *uaddr_len = sizeof(*sin);
 729        return 0;
 730}
 731
 732
 733int inet_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg,
 734                 int size, int flags)
 735{
 736        struct sock *sk = sock->sk;
 737        int addr_len = 0;
 738        int err;
 739
 740        err = sk->sk_prot->recvmsg(iocb, sk, msg, size, flags & MSG_DONTWAIT,
 741                                   flags & ~MSG_DONTWAIT, &addr_len);
 742        if (err >= 0)
 743                msg->msg_namelen = addr_len;
 744        return err;
 745}
 746
 747
 748int inet_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg,
 749                 int size)
 750{
 751        struct sock *sk = sock->sk;
 752
 753        /* We may need to bind the socket. */
 754        if (!inet_sk(sk)->num && inet_autobind(sk))
 755                return -EAGAIN;
 756
 757        return sk->sk_prot->sendmsg(iocb, sk, msg, size);
 758}
 759
 760
 761ssize_t inet_sendpage(struct socket *sock, struct page *page, int offset, size_t size, int flags)
 762{
 763        struct sock *sk = sock->sk;
 764
 765        /* We may need to bind the socket. */
 766        if (!inet_sk(sk)->num && inet_autobind(sk))
 767                return -EAGAIN;
 768
 769        if (sk->sk_prot->sendpage)
 770                return sk->sk_prot->sendpage(sk, page, offset, size, flags);
 771        return sock_no_sendpage(sock, page, offset, size, flags);
 772}
 773
 774
 775int inet_shutdown(struct socket *sock, int how)
 776{
 777        struct sock *sk = sock->sk;
 778        int err = 0;
 779
 780        /* This should really check to make sure
 781         * the socket is a TCP socket. (WHY AC...)
 782         */
 783        how++; /* maps 0->1 has the advantage of making bit 1 rcvs and
 784                       1->2 bit 2 snds.
 785                       2->3 */
 786        if ((how & ~SHUTDOWN_MASK) || !how)     /* MAXINT->0 */
 787                return -EINVAL;
 788
 789        lock_sock(sk);
 790        if (sock->state == SS_CONNECTING) {
 791                if ((1 << sk->sk_state) &
 792                    (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_CLOSE))
 793                        sock->state = SS_DISCONNECTING;
 794                else
 795                        sock->state = SS_CONNECTED;
 796        }
 797
 798        switch (sk->sk_state) {
 799        case TCP_CLOSE:
 800                err = -ENOTCONN;
 801                /* Hack to wake up other listeners, who can poll for
 802                   POLLHUP, even on eg. unconnected UDP sockets -- RR */
 803        default:
 804                sk->sk_shutdown |= how;
 805                if (sk->sk_prot->shutdown)
 806                        sk->sk_prot->shutdown(sk, how);
 807                break;
 808
 809        /* Remaining two branches are temporary solution for missing
 810         * close() in multithreaded environment. It is _not_ a good idea,
 811         * but we have no choice until close() is repaired at VFS level.
 812         */
 813        case TCP_LISTEN:
 814                if (!(how & RCV_SHUTDOWN))
 815                        break;
 816                /* Fall through */
 817        case TCP_SYN_SENT:
 818                err = sk->sk_prot->disconnect(sk, O_NONBLOCK);
 819                sock->state = err ? SS_DISCONNECTING : SS_UNCONNECTED;
 820                break;
 821        }
 822
 823        /* Wake up anyone sleeping in poll. */
 824        sk->sk_state_change(sk);
 825        release_sock(sk);
 826        return err;
 827}
 828
 829/*
 830 *      ioctl() calls you can issue on an INET socket. Most of these are
 831 *      device configuration and stuff and very rarely used. Some ioctls
 832 *      pass on to the socket itself.
 833 *
 834 *      NOTE: I like the idea of a module for the config stuff. ie ifconfig
 835 *      loads the devconfigure module does its configuring and unloads it.
 836 *      There's a good 20K of config code hanging around the kernel.
 837 */
 838
 839int inet_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
 840{
 841        struct sock *sk = sock->sk;
 842        int err = 0;
 843
 844        switch (cmd) {
 845                case SIOCGSTAMP:
 846                        if (!sk->sk_stamp.tv_sec)
 847                                err = -ENOENT;
 848                        else if (copy_to_user((void *)arg, &sk->sk_stamp,
 849                                              sizeof(struct timeval)))
 850                                err = -EFAULT;
 851                        break;
 852                case SIOCADDRT:
 853                case SIOCDELRT:
 854                case SIOCRTMSG:
 855                        err = ip_rt_ioctl(cmd, (void *)arg);
 856                        break;
 857                case SIOCDARP:
 858                case SIOCGARP:
 859                case SIOCSARP:
 860                        err = arp_ioctl(cmd, (void *)arg);
 861                        break;
 862                case SIOCGIFADDR:
 863                case SIOCSIFADDR:
 864                case SIOCGIFBRDADDR:
 865                case SIOCSIFBRDADDR:
 866                case SIOCGIFNETMASK:
 867                case SIOCSIFNETMASK:
 868                case SIOCGIFDSTADDR:
 869                case SIOCSIFDSTADDR:
 870                case SIOCSIFPFLAGS:
 871                case SIOCGIFPFLAGS:
 872                case SIOCSIFFLAGS:
 873                        err = devinet_ioctl(cmd, (void *)arg);
 874                        break;
 875                default:
 876                        if (!sk->sk_prot->ioctl ||
 877                            (err = sk->sk_prot->ioctl(sk, cmd, arg)) ==
 878                                                                -ENOIOCTLCMD)
 879                                err = dev_ioctl(cmd, (void *)arg);
 880                        break;
 881        }
 882        return err;
 883}
 884
 885struct proto_ops inet_stream_ops = {
 886        .family =       PF_INET,
 887        .owner =        THIS_MODULE,
 888        .release =      inet_release,
 889        .bind =         inet_bind,
 890        .connect =      inet_stream_connect,
 891        .socketpair =   sock_no_socketpair,
 892        .accept =       inet_accept,
 893        .getname =      inet_getname,
 894        .poll =         tcp_poll,
 895        .ioctl =        inet_ioctl,
 896        .listen =       inet_listen,
 897        .shutdown =     inet_shutdown,
 898        .setsockopt =   inet_setsockopt,
 899        .getsockopt =   inet_getsockopt,
 900        .sendmsg =      inet_sendmsg,
 901        .recvmsg =      inet_recvmsg,
 902        .mmap =         sock_no_mmap,
 903        .sendpage =     tcp_sendpage
 904};
 905
 906struct proto_ops inet_dgram_ops = {
 907        .family =       PF_INET,
 908        .owner =        THIS_MODULE,
 909        .release =      inet_release,
 910        .bind =         inet_bind,
 911        .connect =      inet_dgram_connect,
 912        .socketpair =   sock_no_socketpair,
 913        .accept =       sock_no_accept,
 914        .getname =      inet_getname,
 915        .poll =         datagram_poll,
 916        .ioctl =        inet_ioctl,
 917        .listen =       sock_no_listen,
 918        .shutdown =     inet_shutdown,
 919        .setsockopt =   inet_setsockopt,
 920        .getsockopt =   inet_getsockopt,
 921        .sendmsg =      inet_sendmsg,
 922        .recvmsg =      inet_recvmsg,
 923        .mmap =         sock_no_mmap,
 924        .sendpage =     inet_sendpage,
 925};
 926
 927struct net_proto_family inet_family_ops = {
 928        .family = PF_INET,
 929        .create = inet_create,
 930        .owner  = THIS_MODULE,
 931};
 932
 933
 934extern void tcp_init(void);
 935extern void tcp_v4_init(struct net_proto_family *);
 936
 937/* Upon startup we insert all the elements in inetsw_array[] into
 938 * the linked list inetsw.
 939 */
 940static struct inet_protosw inetsw_array[] =
 941{
 942        {
 943                .type =       SOCK_STREAM,
 944                .protocol =   IPPROTO_TCP,
 945                .prot =       &tcp_prot,
 946                .ops =        &inet_stream_ops,
 947                .capability = -1,
 948                .no_check =   0,
 949                .flags =      INET_PROTOSW_PERMANENT,
 950        },
 951
 952        {
 953                .type =       SOCK_DGRAM,
 954                .protocol =   IPPROTO_UDP,
 955                .prot =       &udp_prot,
 956                .ops =        &inet_dgram_ops,
 957                .capability = -1,
 958                .no_check =   UDP_CSUM_DEFAULT,
 959                .flags =      INET_PROTOSW_PERMANENT,
 960       },
 961        
 962
 963       {
 964               .type =       SOCK_RAW,
 965               .protocol =   IPPROTO_IP,        /* wild card */
 966               .prot =       &raw_prot,
 967               .ops =        &inet_dgram_ops,
 968               .capability = CAP_NET_RAW,
 969               .no_check =   UDP_CSUM_DEFAULT,
 970               .flags =      INET_PROTOSW_REUSE,
 971       }
 972};
 973
 974#define INETSW_ARRAY_LEN (sizeof(inetsw_array) / sizeof(struct inet_protosw))
 975
 976void inet_register_protosw(struct inet_protosw *p)
 977{
 978        struct list_head *lh;
 979        struct inet_protosw *answer;
 980        int protocol = p->protocol;
 981        struct list_head *last_perm;
 982
 983        spin_lock_bh(&inetsw_lock);
 984
 985        if (p->type > SOCK_MAX)
 986                goto out_illegal;
 987
 988        /* If we are trying to override a permanent protocol, bail. */
 989        answer = NULL;
 990        last_perm = &inetsw[p->type];
 991        list_for_each(lh, &inetsw[p->type]) {
 992                answer = list_entry(lh, struct inet_protosw, list);
 993
 994                /* Check only the non-wild match. */
 995                if (INET_PROTOSW_PERMANENT & answer->flags) {
 996                        if (protocol == answer->protocol)
 997                                break;
 998                        last_perm = lh;
 999                }
1000
1001                answer = NULL;
1002        }
1003        if (answer)
1004                goto out_permanent;
1005
1006        /* Add the new entry after the last permanent entry if any, so that
1007         * the new entry does not override a permanent entry when matched with
1008         * a wild-card protocol. But it is allowed to override any existing
1009         * non-permanent entry.  This means that when we remove this entry, the 
1010         * system automatically returns to the old behavior.
1011         */
1012        list_add_rcu(&p->list, last_perm);
1013out:
1014        spin_unlock_bh(&inetsw_lock);
1015
1016        synchronize_net();
1017
1018        return;
1019
1020out_permanent:
1021        printk(KERN_ERR "Attempt to override permanent protocol %d.\n",
1022               protocol);
1023        goto out;
1024
1025out_illegal:
1026        printk(KERN_ERR
1027               "Ignoring attempt to register invalid socket type %d.\n",
1028               p->type);
1029        goto out;
1030}
1031
1032void inet_unregister_protosw(struct inet_protosw *p)
1033{
1034        if (INET_PROTOSW_PERMANENT & p->flags) {
1035                printk(KERN_ERR
1036                       "Attempt to unregister permanent protocol %d.\n",
1037                       p->protocol);
1038        } else {
1039                spin_lock_bh(&inetsw_lock);
1040                list_del_rcu(&p->list);
1041                spin_unlock_bh(&inetsw_lock);
1042
1043                synchronize_net();
1044        }
1045}
1046
1047#ifdef CONFIG_IP_MULTICAST
1048static struct inet_protocol igmp_protocol = {
1049        .handler =      igmp_rcv,
1050};
1051#endif
1052
1053static struct inet_protocol tcp_protocol = {
1054        .handler =      tcp_v4_rcv,
1055        .err_handler =  tcp_v4_err,
1056        .no_policy =    1,
1057};
1058
1059static struct inet_protocol udp_protocol = {
1060        .handler =      udp_rcv,
1061        .err_handler =  udp_err,
1062        .no_policy =    1,
1063};
1064
1065static struct inet_protocol icmp_protocol = {
1066        .handler =      icmp_rcv,
1067};
1068
1069static int __init init_ipv4_mibs(void)
1070{
1071        net_statistics[0] = alloc_percpu(struct linux_mib);
1072        net_statistics[1] = alloc_percpu(struct linux_mib);
1073        ip_statistics[0] = alloc_percpu(struct ip_mib);
1074        ip_statistics[1] = alloc_percpu(struct ip_mib);
1075        icmp_statistics[0] = alloc_percpu(struct icmp_mib);
1076        icmp_statistics[1] = alloc_percpu(struct icmp_mib);
1077        tcp_statistics[0] = alloc_percpu(struct tcp_mib);
1078        tcp_statistics[1] = alloc_percpu(struct tcp_mib);
1079        udp_statistics[0] = alloc_percpu(struct udp_mib);
1080        udp_statistics[1] = alloc_percpu(struct udp_mib);
1081        if (!
1082            (net_statistics[0] && net_statistics[1] && ip_statistics[0]
1083             && ip_statistics[1] && tcp_statistics[0] && tcp_statistics[1]
1084             && udp_statistics[0] && udp_statistics[1]))
1085                return -ENOMEM;
1086
1087        (void) tcp_mib_init();
1088
1089        return 0;
1090}
1091
1092int ipv4_proc_init(void);
1093extern void ipfrag_init(void);
1094
1095static int __init inet_init(void)
1096{
1097        struct sk_buff *dummy_skb;
1098        struct inet_protosw *q;
1099        struct list_head *r;
1100
1101        if (sizeof(struct inet_skb_parm) > sizeof(dummy_skb->cb)) {
1102                printk(KERN_CRIT "%s: panic\n", __FUNCTION__);
1103                return -EINVAL;
1104        }
1105
1106        tcp_sk_cachep = kmem_cache_create("tcp_sock",
1107                                          sizeof(struct tcp_sock), 0,
1108                                          SLAB_HWCACHE_ALIGN, 0, 0);
1109        udp_sk_cachep = kmem_cache_create("udp_sock",
1110                                          sizeof(struct udp_sock), 0,
1111                                          SLAB_HWCACHE_ALIGN, 0, 0);
1112        raw4_sk_cachep = kmem_cache_create("raw4_sock",
1113                                           sizeof(struct raw_sock), 0,
1114                                           SLAB_HWCACHE_ALIGN, 0, 0);
1115        if (!tcp_sk_cachep || !udp_sk_cachep || !raw4_sk_cachep)
1116                printk(KERN_CRIT
1117                       "inet_init: Can't create protocol sock SLAB caches!\n");
1118        /*
1119         *      Tell SOCKET that we are alive... 
1120         */
1121
1122        (void)sock_register(&inet_family_ops);
1123
1124        /*
1125         *      Add all the base protocols.
1126         */
1127
1128        if (inet_add_protocol(&icmp_protocol, IPPROTO_ICMP) < 0)
1129                printk(KERN_CRIT "inet_init: Cannot add ICMP protocol\n");
1130        if (inet_add_protocol(&udp_protocol, IPPROTO_UDP) < 0)
1131                printk(KERN_CRIT "inet_init: Cannot add UDP protocol\n");
1132        if (inet_add_protocol(&tcp_protocol, IPPROTO_TCP) < 0)
1133                printk(KERN_CRIT "inet_init: Cannot add TCP protocol\n");
1134#ifdef CONFIG_IP_MULTICAST
1135        if (inet_add_protocol(&igmp_protocol, IPPROTO_IGMP) < 0)
1136                printk(KERN_CRIT "inet_init: Cannot add IGMP protocol\n");
1137#endif
1138
1139        /* Register the socket-side information for inet_create. */
1140        for (r = &inetsw[0]; r < &inetsw[SOCK_MAX]; ++r)
1141                INIT_LIST_HEAD(r);
1142
1143        for (q = inetsw_array; q < &inetsw_array[INETSW_ARRAY_LEN]; ++q)
1144                inet_register_protosw(q);
1145
1146        /*
1147         *      Set the ARP module up
1148         */
1149
1150        arp_init();
1151
1152        /*
1153         *      Set the IP module up
1154         */
1155
1156        ip_init();
1157
1158        tcp_v4_init(&inet_family_ops);
1159
1160        /* Setup TCP slab cache for open requests. */
1161        tcp_init();
1162
1163
1164        /*
1165         *      Set the ICMP layer up
1166         */
1167
1168        icmp_init(&inet_family_ops);
1169
1170        /* I wish inet_add_protocol had no constructor hook...
1171           I had to move IPIP from net/ipv4/protocol.c :-( --ANK
1172         */
1173#ifdef CONFIG_NET_IPIP
1174        ipip_init();
1175#endif
1176#ifdef CONFIG_NET_IPGRE
1177        ipgre_init();
1178#endif
1179
1180        /*
1181         *      Initialise the multicast router
1182         */
1183#if defined(CONFIG_IP_MROUTE)
1184        ip_mr_init();
1185#endif
1186        /*
1187         *      Initialise per-cpu ipv4 mibs
1188         */ 
1189
1190        if(init_ipv4_mibs())
1191                printk(KERN_CRIT "inet_init: Cannot init ipv4 mibs\n"); ;
1192        
1193        ipv4_proc_init();
1194
1195        ipfrag_init();
1196
1197        return 0;
1198}
1199
1200module_init(inet_init);
1201
1202/* ------------------------------------------------------------------------ */
1203
1204#ifdef CONFIG_PROC_FS
1205extern int  fib_proc_init(void);
1206extern void fib_proc_exit(void);
1207extern int  ip_misc_proc_init(void);
1208extern int  raw_proc_init(void);
1209extern void raw_proc_exit(void);
1210extern int  tcp4_proc_init(void);
1211extern void tcp4_proc_exit(void);
1212extern int  udp4_proc_init(void);
1213extern void udp4_proc_exit(void);
1214
1215int __init ipv4_proc_init(void)
1216{
1217        int rc = 0;
1218
1219        if (raw_proc_init())
1220                goto out_raw;
1221        if (tcp4_proc_init())
1222                goto out_tcp;
1223        if (udp4_proc_init())
1224                goto out_udp;
1225        if (fib_proc_init())
1226                goto out_fib;
1227        if (ip_misc_proc_init())
1228                goto out_misc;
1229out:
1230        return rc;
1231out_misc:
1232        fib_proc_exit();
1233out_fib:
1234        udp4_proc_exit();
1235out_udp:
1236        tcp4_proc_exit();
1237out_tcp:
1238        raw_proc_exit();
1239out_raw:
1240        rc = -ENOMEM;
1241        goto out;
1242}
1243
1244#else /* CONFIG_PROC_FS */
1245int __init ipv4_proc_init(void)
1246{
1247        return 0;
1248}
1249#endif /* CONFIG_PROC_FS */
1250
1251MODULE_ALIAS_NETPROTO(PF_INET);
1252
1253EXPORT_SYMBOL(inet_accept);
1254EXPORT_SYMBOL(inet_bind);
1255EXPORT_SYMBOL(inet_dgram_connect);
1256EXPORT_SYMBOL(inet_dgram_ops);
1257EXPORT_SYMBOL(inet_family_ops);
1258EXPORT_SYMBOL(inet_getname);
1259EXPORT_SYMBOL(inet_getsockopt);
1260EXPORT_SYMBOL(inet_ioctl);
1261EXPORT_SYMBOL(inet_listen);
1262EXPORT_SYMBOL(inet_recvmsg);
1263EXPORT_SYMBOL(inet_register_protosw);
1264EXPORT_SYMBOL(inet_release);
1265EXPORT_SYMBOL(inet_sendmsg);
1266EXPORT_SYMBOL(inet_setsockopt);
1267EXPORT_SYMBOL(inet_shutdown);
1268EXPORT_SYMBOL(inet_sock_destruct);
1269EXPORT_SYMBOL(inet_sock_release);
1270EXPORT_SYMBOL(inet_stream_connect);
1271EXPORT_SYMBOL(inet_stream_ops);
1272EXPORT_SYMBOL(inet_unregister_protosw);
1273EXPORT_SYMBOL(net_statistics);
1274EXPORT_SYMBOL(tcp_protocol);
1275EXPORT_SYMBOL(udp_protocol);
1276
1277#ifdef INET_REFCNT_DEBUG
1278EXPORT_SYMBOL(inet_sock_nr);
1279#endif
1280
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.