new VNET
[linux-2.6.git] / net / ipv4 / af_inet.c
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
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/err.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/module.h>
77 #include <linux/sched.h>
78 #include <linux/timer.h>
79 #include <linux/string.h>
80 #include <linux/sockios.h>
81 #include <linux/net.h>
82 #include <linux/capability.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/inetdevice.h>
98 #include <linux/netdevice.h>
99 #include <net/ip.h>
100 #include <net/protocol.h>
101 #include <net/arp.h>
102 #include <net/route.h>
103 #include <net/ip_fib.h>
104 #include <net/inet_connection_sock.h>
105 #include <net/tcp.h>
106 #include <net/udp.h>
107 #include <net/udplite.h>
108 #include <linux/skbuff.h>
109 #include <net/sock.h>
110 #include <net/raw.h>
111 #include <net/icmp.h>
112 #include <net/ipip.h>
113 #include <net/inet_common.h>
114 #include <net/xfrm.h>
115 #ifdef CONFIG_IP_MROUTE
116 #include <linux/mroute.h>
117 #endif
118 #include <linux/vs_limit.h>
119
120 DEFINE_SNMP_STAT(struct linux_mib, net_statistics) __read_mostly;
121
122 extern void ip_mc_drop_socket(struct sock *sk);
123
124 /* The inetsw table contains everything that inet_create needs to
125  * build a new socket.
126  */
127 static struct list_head inetsw[SOCK_MAX];
128 static DEFINE_SPINLOCK(inetsw_lock);
129
130 /* New destruction routine */
131
132 void inet_sock_destruct(struct sock *sk)
133 {
134         struct inet_sock *inet = inet_sk(sk);
135
136         __skb_queue_purge(&sk->sk_receive_queue);
137         __skb_queue_purge(&sk->sk_error_queue);
138
139         if (sk->sk_type == SOCK_STREAM && sk->sk_state != TCP_CLOSE) {
140                 printk("Attempt to release TCP socket in state %d %p\n",
141                        sk->sk_state, sk);
142                 return;
143         }
144         if (!sock_flag(sk, SOCK_DEAD)) {
145                 printk("Attempt to release alive inet socket %p\n", sk);
146                 return;
147         }
148
149         BUG_TRAP(!atomic_read(&sk->sk_rmem_alloc));
150         BUG_TRAP(!atomic_read(&sk->sk_wmem_alloc));
151         BUG_TRAP(!sk->sk_wmem_queued);
152         BUG_TRAP(!sk->sk_forward_alloc);
153
154         kfree(inet->opt);
155         dst_release(sk->sk_dst_cache);
156         sk_refcnt_debug_dec(sk);
157 }
158
159 /*
160  *      The routines beyond this point handle the behaviour of an AF_INET
161  *      socket object. Mostly it punts to the subprotocols of IP to do
162  *      the work.
163  */
164
165 /*
166  *      Automatically bind an unbound socket.
167  */
168
169 static int inet_autobind(struct sock *sk)
170 {
171         struct inet_sock *inet;
172         /* We may need to bind the socket. */
173         lock_sock(sk);
174         inet = inet_sk(sk);
175         if (!inet->num) {
176                 if (sk->sk_prot->get_port(sk, 0)) {
177                         release_sock(sk);
178                         return -EAGAIN;
179                 }
180                 inet->sport = htons(inet->num);
181                 sk->sk_xid = vx_current_xid();
182                 sk->sk_nid = nx_current_nid();
183         }
184         release_sock(sk);
185         return 0;
186 }
187
188 /*
189  *      Move a socket into listening state.
190  */
191 int inet_listen(struct socket *sock, int backlog)
192 {
193         struct sock *sk = sock->sk;
194         unsigned char old_state;
195         int err;
196
197         lock_sock(sk);
198
199         err = -EINVAL;
200         if (sock->state != SS_UNCONNECTED || sock->type != SOCK_STREAM)
201                 goto out;
202
203         old_state = sk->sk_state;
204         if (!((1 << old_state) & (TCPF_CLOSE | TCPF_LISTEN)))
205                 goto out;
206
207         /* Really, if the socket is already in listen state
208          * we can only allow the backlog to be adjusted.
209          */
210         if (old_state != TCP_LISTEN) {
211                 err = inet_csk_listen_start(sk, backlog);
212                 if (err)
213                         goto out;
214         }
215         sk->sk_max_ack_backlog = backlog;
216         err = 0;
217
218 out:
219         release_sock(sk);
220         return err;
221 }
222
223 /*
224  *      Create an inet socket.
225  */
226
227 static int inet_create(struct socket *sock, int protocol)
228 {
229         struct sock *sk;
230         struct list_head *p;
231         struct inet_protosw *answer;
232         struct inet_sock *inet;
233         struct proto *answer_prot;
234         unsigned char answer_flags;
235         char answer_no_check;
236         int try_loading_module = 0;
237         int err;
238
239         sock->state = SS_UNCONNECTED;
240
241         /* Look for the requested type/protocol pair. */
242         answer = NULL;
243 lookup_protocol:
244         err = -ESOCKTNOSUPPORT;
245         rcu_read_lock();
246         list_for_each_rcu(p, &inetsw[sock->type]) {
247                 answer = list_entry(p, struct inet_protosw, list);
248
249                 /* Check the non-wild match. */
250                 if (protocol == answer->protocol) {
251                         if (protocol != IPPROTO_IP)
252                                 break;
253                 } else {
254                         /* Check for the two wild cases. */
255                         if (IPPROTO_IP == protocol) {
256                                 protocol = answer->protocol;
257                                 break;
258                         }
259                         if (IPPROTO_IP == answer->protocol)
260                                 break;
261                 }
262                 err = -EPROTONOSUPPORT;
263                 answer = NULL;
264         }
265
266         if (unlikely(answer == NULL)) {
267                 if (try_loading_module < 2) {
268                         rcu_read_unlock();
269                         /*
270                          * Be more specific, e.g. net-pf-2-proto-132-type-1
271                          * (net-pf-PF_INET-proto-IPPROTO_SCTP-type-SOCK_STREAM)
272                          */
273                         if (++try_loading_module == 1)
274                                 request_module("net-pf-%d-proto-%d-type-%d",
275                                                PF_INET, protocol, sock->type);
276                         /*
277                          * Fall back to generic, e.g. net-pf-2-proto-132
278                          * (net-pf-PF_INET-proto-IPPROTO_SCTP)
279                          */
280                         else
281                                 request_module("net-pf-%d-proto-%d",
282                                                PF_INET, protocol);
283                         goto lookup_protocol;
284                 } else
285                         goto out_rcu_unlock;
286         }
287
288         err = -EPERM;
289         if ((protocol == IPPROTO_ICMP) && vx_ccaps(VXC_RAW_ICMP))
290                 goto override;
291         if (answer->capability > 0 && !capable(answer->capability))
292                 goto out_rcu_unlock;
293 override:
294         sock->ops = answer->ops;
295         answer_prot = answer->prot;
296         answer_no_check = answer->no_check;
297         answer_flags = answer->flags;
298         rcu_read_unlock();
299
300         BUG_TRAP(answer_prot->slab != NULL);
301
302         err = -ENOBUFS;
303         sk = sk_alloc(PF_INET, GFP_KERNEL, answer_prot, 1);
304         if (sk == NULL)
305                 goto out;
306
307         err = 0;
308         sk->sk_no_check = answer_no_check;
309         if (INET_PROTOSW_REUSE & answer_flags)
310                 sk->sk_reuse = 1;
311
312         inet = inet_sk(sk);
313         inet->is_icsk = (INET_PROTOSW_ICSK & answer_flags) != 0;
314
315         if (SOCK_RAW == sock->type) {
316                 inet->num = protocol;
317                 if (IPPROTO_RAW == protocol)
318                         inet->hdrincl = 1;
319         }
320
321         if (ipv4_config.no_pmtu_disc)
322                 inet->pmtudisc = IP_PMTUDISC_DONT;
323         else
324                 inet->pmtudisc = IP_PMTUDISC_WANT;
325
326         inet->id = 0;
327
328         sock_init_data(sock, sk);
329
330         sk->sk_destruct    = inet_sock_destruct;
331         sk->sk_family      = PF_INET;
332         sk->sk_protocol    = protocol;
333         sk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
334
335         inet->uc_ttl    = -1;
336         inet->mc_loop   = 1;
337         inet->mc_ttl    = 1;
338         inet->mc_index  = 0;
339         inet->mc_list   = NULL;
340
341         sk_refcnt_debug_inc(sk);
342
343         if (inet->num) {
344                 /* It assumes that any protocol which allows
345                  * the user to assign a number at socket
346                  * creation time automatically
347                  * shares.
348                  */
349                 inet->sport = htons(inet->num);
350                 /* Add to protocol hash chains. */
351                 sk->sk_prot->hash(sk);
352         }
353
354         if (sk->sk_prot->init) {
355                 err = sk->sk_prot->init(sk);
356                 if (err)
357                         sk_common_release(sk);
358         }
359 out:
360         return err;
361 out_rcu_unlock:
362         rcu_read_unlock();
363         goto out;
364 }
365
366
367 /*
368  *      The peer socket should always be NULL (or else). When we call this
369  *      function we are destroying the object and from then on nobody
370  *      should refer to it.
371  */
372 int inet_release(struct socket *sock)
373 {
374         struct sock *sk = sock->sk;
375
376         if (sk) {
377                 long timeout;
378
379                 /* Applications forget to leave groups before exiting */
380                 ip_mc_drop_socket(sk);
381
382                 /* If linger is set, we don't return until the close
383                  * is complete.  Otherwise we return immediately. The
384                  * actually closing is done the same either way.
385                  *
386                  * If the close is due to the process exiting, we never
387                  * linger..
388                  */
389                 timeout = 0;
390                 if (sock_flag(sk, SOCK_LINGER) &&
391                     !(current->flags & PF_EXITING))
392                         timeout = sk->sk_lingertime;
393                 sock->sk = NULL;
394                 sk->sk_prot->close(sk, timeout);
395         }
396         return 0;
397 }
398
399 /* It is off by default, see below. */
400 int sysctl_ip_nonlocal_bind __read_mostly;
401
402 int inet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
403 {
404         struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
405         struct sock *sk = sock->sk;
406         struct inet_sock *inet = inet_sk(sk);
407         unsigned short snum;
408         int chk_addr_ret;
409         int err;
410         __u32 s_addr;   /* Address used for validation */
411         __u32 s_addr1;  /* Address used for socket */
412         __u32 s_addr2;  /* Broadcast address for the socket */
413         struct nx_info *nxi = sk->sk_nx_info;
414
415         /* If the socket has its own bind function then use it. (RAW) */
416         if (sk->sk_prot->bind) {
417                 err = sk->sk_prot->bind(sk, uaddr, addr_len);
418                 goto out;
419         }
420         err = -EINVAL;
421         if (addr_len < sizeof(struct sockaddr_in))
422                 goto out;
423
424         s_addr = addr->sin_addr.s_addr;
425         s_addr1 = s_addr;
426         s_addr2 = 0xffffffffl;
427
428         vxdprintk(VXD_CBIT(net, 3),
429                 "inet_bind(%p)* %p,%p;%lx %d.%d.%d.%d",
430                 sk, sk->sk_nx_info, sk->sk_socket,
431                 (sk->sk_socket?sk->sk_socket->flags:0),
432                 VXD_QUAD(s_addr));
433         if (nxi) {
434                 __u32 v4_bcast = nxi->v4_bcast;
435                 __u32 ipv4root = nxi->ipv4[0];
436                 int nbipv4 = nxi->nbipv4;
437
438                 if (s_addr == 0) {
439                         /* bind to any for 1-n */
440                         s_addr = ipv4root;
441                         s_addr1 = (nbipv4 > 1) ? 0 : s_addr;
442                         s_addr2 = v4_bcast;
443                 } else if (s_addr == IPI_LOOPBACK) {
444                         /* rewrite localhost to ipv4root */
445                         s_addr = ipv4root;
446                         s_addr1 = ipv4root;
447                 } else if (s_addr != v4_bcast) {
448                         /* normal address bind */
449                         if (!addr_in_nx_info(nxi, s_addr))
450                                 return -EADDRNOTAVAIL;
451                 }
452         }
453         chk_addr_ret = inet_addr_type(s_addr);
454
455         vxdprintk(VXD_CBIT(net, 3),
456                 "inet_bind(%p) %d.%d.%d.%d, %d.%d.%d.%d, %d.%d.%d.%d",
457                 sk, VXD_QUAD(s_addr), VXD_QUAD(s_addr1), VXD_QUAD(s_addr2));
458
459         /* Not specified by any standard per-se, however it breaks too
460          * many applications when removed.  It is unfortunate since
461          * allowing applications to make a non-local bind solves
462          * several problems with systems using dynamic addressing.
463          * (ie. your servers still start up even if your ISDN link
464          *  is temporarily down)
465          */
466         err = -EADDRNOTAVAIL;
467         if (!sysctl_ip_nonlocal_bind &&
468             !inet->freebind &&
469             s_addr != INADDR_ANY &&
470             chk_addr_ret != RTN_LOCAL &&
471             chk_addr_ret != RTN_MULTICAST &&
472             chk_addr_ret != RTN_BROADCAST)
473                 goto out;
474
475         snum = ntohs(addr->sin_port);
476         err = -EACCES;
477         if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
478                 goto out;
479
480         /*      We keep a pair of addresses. rcv_saddr is the one
481          *      used by hash lookups, and saddr is used for transmit.
482          *
483          *      In the BSD API these are the same except where it
484          *      would be illegal to use them (multicast/broadcast) in
485          *      which case the sending device address is used.
486          */
487         lock_sock(sk);
488
489         /* Check these errors (active socket, double bind). */
490         err = -EINVAL;
491         if (sk->sk_state != TCP_CLOSE || inet->num)
492                 goto out_release_sock;
493
494         inet->rcv_saddr = inet->saddr = s_addr1;
495         inet->rcv_saddr2 = s_addr2;
496         if (chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST)
497                 inet->saddr = 0;  /* Use device */
498
499         /* Make sure we are allowed to bind here. */
500         if (sk->sk_prot->get_port(sk, snum)) {
501                 inet->saddr = inet->rcv_saddr = 0;
502                 err = -EADDRINUSE;
503                 goto out_release_sock;
504         }
505
506         if (inet->rcv_saddr)
507                 sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
508         if (snum)
509                 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
510         inet->sport = htons(inet->num);
511         inet->daddr = 0;
512         inet->dport = 0;
513         sk_dst_reset(sk);
514         err = 0;
515 out_release_sock:
516         release_sock(sk);
517 out:
518         return err;
519 }
520
521 int inet_dgram_connect(struct socket *sock, struct sockaddr * uaddr,
522                        int addr_len, int flags)
523 {
524         struct sock *sk = sock->sk;
525
526         if (uaddr->sa_family == AF_UNSPEC)
527                 return sk->sk_prot->disconnect(sk, flags);
528
529         if (!inet_sk(sk)->num && inet_autobind(sk))
530                 return -EAGAIN;
531         return sk->sk_prot->connect(sk, (struct sockaddr *)uaddr, addr_len);
532 }
533
534 static long inet_wait_for_connect(struct sock *sk, long timeo)
535 {
536         DEFINE_WAIT(wait);
537
538         prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
539
540         /* Basic assumption: if someone sets sk->sk_err, he _must_
541          * change state of the socket from TCP_SYN_*.
542          * Connect() does not allow to get error notifications
543          * without closing the socket.
544          */
545         while ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
546                 release_sock(sk);
547                 timeo = schedule_timeout(timeo);
548                 lock_sock(sk);
549                 if (signal_pending(current) || !timeo)
550                         break;
551                 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
552         }
553         finish_wait(sk->sk_sleep, &wait);
554         return timeo;
555 }
556
557 /*
558  *      Connect to a remote host. There is regrettably still a little
559  *      TCP 'magic' in here.
560  */
561 int inet_stream_connect(struct socket *sock, struct sockaddr *uaddr,
562                         int addr_len, int flags)
563 {
564         struct sock *sk = sock->sk;
565         int err;
566         long timeo;
567
568         lock_sock(sk);
569
570         if (uaddr->sa_family == AF_UNSPEC) {
571                 err = sk->sk_prot->disconnect(sk, flags);
572                 sock->state = err ? SS_DISCONNECTING : SS_UNCONNECTED;
573                 goto out;
574         }
575
576         switch (sock->state) {
577         default:
578                 err = -EINVAL;
579                 goto out;
580         case SS_CONNECTED:
581                 err = -EISCONN;
582                 goto out;
583         case SS_CONNECTING:
584                 err = -EALREADY;
585                 /* Fall out of switch with err, set for this state */
586                 break;
587         case SS_UNCONNECTED:
588                 err = -EISCONN;
589                 if (sk->sk_state != TCP_CLOSE)
590                         goto out;
591
592                 err = sk->sk_prot->connect(sk, uaddr, addr_len);
593                 if (err < 0)
594                         goto out;
595
596                 sock->state = SS_CONNECTING;
597
598                 /* Just entered SS_CONNECTING state; the only
599                  * difference is that return value in non-blocking
600                  * case is EINPROGRESS, rather than EALREADY.
601                  */
602                 err = -EINPROGRESS;
603                 break;
604         }
605
606         timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
607
608         if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
609                 /* Error code is set above */
610                 if (!timeo || !inet_wait_for_connect(sk, timeo))
611                         goto out;
612
613                 err = sock_intr_errno(timeo);
614                 if (signal_pending(current))
615                         goto out;
616         }
617
618         /* Connection was closed by RST, timeout, ICMP error
619          * or another process disconnected us.
620          */
621         if (sk->sk_state == TCP_CLOSE)
622                 goto sock_error;
623
624         /* sk->sk_err may be not zero now, if RECVERR was ordered by user
625          * and error was received after socket entered established state.
626          * Hence, it is handled normally after connect() return successfully.
627          */
628
629         sock->state = SS_CONNECTED;
630         err = 0;
631 out:
632         release_sock(sk);
633         return err;
634
635 sock_error:
636         err = sock_error(sk) ? : -ECONNABORTED;
637         sock->state = SS_UNCONNECTED;
638         if (sk->sk_prot->disconnect(sk, flags))
639                 sock->state = SS_DISCONNECTING;
640         goto out;
641 }
642
643 /*
644  *      Accept a pending connection. The TCP layer now gives BSD semantics.
645  */
646
647 int inet_accept(struct socket *sock, struct socket *newsock, int flags)
648 {
649         struct sock *sk1 = sock->sk;
650         int err = -EINVAL;
651         struct sock *sk2 = sk1->sk_prot->accept(sk1, flags, &err);
652
653         if (!sk2)
654                 goto do_err;
655
656         lock_sock(sk2);
657
658         BUG_TRAP((1 << sk2->sk_state) &
659                  (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT | TCPF_CLOSE));
660
661         sock_graft(sk2, newsock);
662
663         newsock->state = SS_CONNECTED;
664         err = 0;
665         release_sock(sk2);
666 do_err:
667         return err;
668 }
669
670
671 /*
672  *      This does both peername and sockname.
673  */
674 int inet_getname(struct socket *sock, struct sockaddr *uaddr,
675                         int *uaddr_len, int peer)
676 {
677         struct sock *sk         = sock->sk;
678         struct inet_sock *inet  = inet_sk(sk);
679         struct sockaddr_in *sin = (struct sockaddr_in *)uaddr;
680
681         sin->sin_family = AF_INET;
682         if (peer) {
683                 if (!inet->dport ||
684                     (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
685                      peer == 1))
686                         return -ENOTCONN;
687                 sin->sin_port = inet->dport;
688                 sin->sin_addr.s_addr = inet->daddr;
689         } else {
690                 __be32 addr = inet->rcv_saddr;
691                 if (!addr)
692                         addr = inet->saddr;
693                 sin->sin_port = inet->sport;
694                 sin->sin_addr.s_addr = addr;
695         }
696         memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
697         *uaddr_len = sizeof(*sin);
698         return 0;
699 }
700
701 int inet_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg,
702                  size_t size)
703 {
704         struct sock *sk = sock->sk;
705
706         /* We may need to bind the socket. */
707         if (!inet_sk(sk)->num && inet_autobind(sk))
708                 return -EAGAIN;
709
710         return sk->sk_prot->sendmsg(iocb, sk, msg, size);
711 }
712
713
714 static ssize_t inet_sendpage(struct socket *sock, struct page *page, int offset, size_t size, int flags)
715 {
716         struct sock *sk = sock->sk;
717
718         /* We may need to bind the socket. */
719         if (!inet_sk(sk)->num && inet_autobind(sk))
720                 return -EAGAIN;
721
722         if (sk->sk_prot->sendpage)
723                 return sk->sk_prot->sendpage(sk, page, offset, size, flags);
724         return sock_no_sendpage(sock, page, offset, size, flags);
725 }
726
727
728 int inet_shutdown(struct socket *sock, int how)
729 {
730         struct sock *sk = sock->sk;
731         int err = 0;
732
733         /* This should really check to make sure
734          * the socket is a TCP socket. (WHY AC...)
735          */
736         how++; /* maps 0->1 has the advantage of making bit 1 rcvs and
737                        1->2 bit 2 snds.
738                        2->3 */
739         if ((how & ~SHUTDOWN_MASK) || !how)     /* MAXINT->0 */
740                 return -EINVAL;
741
742         lock_sock(sk);
743         if (sock->state == SS_CONNECTING) {
744                 if ((1 << sk->sk_state) &
745                     (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_CLOSE))
746                         sock->state = SS_DISCONNECTING;
747                 else
748                         sock->state = SS_CONNECTED;
749         }
750
751         switch (sk->sk_state) {
752         case TCP_CLOSE:
753                 err = -ENOTCONN;
754                 /* Hack to wake up other listeners, who can poll for
755                    POLLHUP, even on eg. unconnected UDP sockets -- RR */
756         default:
757                 sk->sk_shutdown |= how;
758                 if (sk->sk_prot->shutdown)
759                         sk->sk_prot->shutdown(sk, how);
760                 break;
761
762         /* Remaining two branches are temporary solution for missing
763          * close() in multithreaded environment. It is _not_ a good idea,
764          * but we have no choice until close() is repaired at VFS level.
765          */
766         case TCP_LISTEN:
767                 if (!(how & RCV_SHUTDOWN))
768                         break;
769                 /* Fall through */
770         case TCP_SYN_SENT:
771                 err = sk->sk_prot->disconnect(sk, O_NONBLOCK);
772                 sock->state = err ? SS_DISCONNECTING : SS_UNCONNECTED;
773                 break;
774         }
775
776         /* Wake up anyone sleeping in poll. */
777         sk->sk_state_change(sk);
778         release_sock(sk);
779         return err;
780 }
781
782 /*
783  *      ioctl() calls you can issue on an INET socket. Most of these are
784  *      device configuration and stuff and very rarely used. Some ioctls
785  *      pass on to the socket itself.
786  *
787  *      NOTE: I like the idea of a module for the config stuff. ie ifconfig
788  *      loads the devconfigure module does its configuring and unloads it.
789  *      There's a good 20K of config code hanging around the kernel.
790  */
791
792 int inet_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
793 {
794         struct sock *sk = sock->sk;
795         int err = 0;
796
797         switch (cmd) {
798                 case SIOCGSTAMP:
799                         err = sock_get_timestamp(sk, (struct timeval __user *)arg);
800                         break;
801                 case SIOCADDRT:
802                 case SIOCDELRT:
803                 case SIOCRTMSG:
804                         err = ip_rt_ioctl(cmd, (void __user *)arg);
805                         break;
806                 case SIOCDARP:
807                 case SIOCGARP:
808                 case SIOCSARP:
809                         err = arp_ioctl(cmd, (void __user *)arg);
810                         break;
811                 case SIOCGIFADDR:
812                 case SIOCSIFADDR:
813                 case SIOCGIFBRDADDR:
814                 case SIOCSIFBRDADDR:
815                 case SIOCGIFNETMASK:
816                 case SIOCSIFNETMASK:
817                 case SIOCGIFDSTADDR:
818                 case SIOCSIFDSTADDR:
819                 case SIOCSIFPFLAGS:
820                 case SIOCGIFPFLAGS:
821                 case SIOCSIFFLAGS:
822                         err = devinet_ioctl(cmd, (void __user *)arg);
823                         break;
824                 default:
825                         if (sk->sk_prot->ioctl)
826                                 err = sk->sk_prot->ioctl(sk, cmd, arg);
827                         else
828                                 err = -ENOIOCTLCMD;
829                         break;
830         }
831         return err;
832 }
833
834 const struct proto_ops inet_stream_ops = {
835         .family            = PF_INET,
836         .owner             = THIS_MODULE,
837         .release           = inet_release,
838         .bind              = inet_bind,
839         .connect           = inet_stream_connect,
840         .socketpair        = sock_no_socketpair,
841         .accept            = inet_accept,
842         .getname           = inet_getname,
843         .poll              = tcp_poll,
844         .ioctl             = inet_ioctl,
845         .listen            = inet_listen,
846         .shutdown          = inet_shutdown,
847         .setsockopt        = sock_common_setsockopt,
848         .getsockopt        = sock_common_getsockopt,
849         .sendmsg           = inet_sendmsg,
850         .recvmsg           = sock_common_recvmsg,
851         .mmap              = sock_no_mmap,
852         .sendpage          = tcp_sendpage,
853 #ifdef CONFIG_COMPAT
854         .compat_setsockopt = compat_sock_common_setsockopt,
855         .compat_getsockopt = compat_sock_common_getsockopt,
856 #endif
857 };
858
859 const struct proto_ops inet_dgram_ops = {
860         .family            = PF_INET,
861         .owner             = THIS_MODULE,
862         .release           = inet_release,
863         .bind              = inet_bind,
864         .connect           = inet_dgram_connect,
865         .socketpair        = sock_no_socketpair,
866         .accept            = sock_no_accept,
867         .getname           = inet_getname,
868         .poll              = udp_poll,
869         .ioctl             = inet_ioctl,
870         .listen            = sock_no_listen,
871         .shutdown          = inet_shutdown,
872         .setsockopt        = sock_common_setsockopt,
873         .getsockopt        = sock_common_getsockopt,
874         .sendmsg           = inet_sendmsg,
875         .recvmsg           = sock_common_recvmsg,
876         .mmap              = sock_no_mmap,
877         .sendpage          = inet_sendpage,
878 #ifdef CONFIG_COMPAT
879         .compat_setsockopt = compat_sock_common_setsockopt,
880         .compat_getsockopt = compat_sock_common_getsockopt,
881 #endif
882 };
883
884 /*
885  * For SOCK_RAW sockets; should be the same as inet_dgram_ops but without
886  * udp_poll
887  */
888 static const struct proto_ops inet_sockraw_ops = {
889         .family            = PF_INET,
890         .owner             = THIS_MODULE,
891         .release           = inet_release,
892         .bind              = inet_bind,
893         .connect           = inet_dgram_connect,
894         .socketpair        = sock_no_socketpair,
895         .accept            = sock_no_accept,
896         .getname           = inet_getname,
897         .poll              = datagram_poll,
898         .ioctl             = inet_ioctl,
899         .listen            = sock_no_listen,
900         .shutdown          = inet_shutdown,
901         .setsockopt        = sock_common_setsockopt,
902         .getsockopt        = sock_common_getsockopt,
903         .sendmsg           = inet_sendmsg,
904         .recvmsg           = sock_common_recvmsg,
905         .mmap              = sock_no_mmap,
906         .sendpage          = inet_sendpage,
907 #ifdef CONFIG_COMPAT
908         .compat_setsockopt = compat_sock_common_setsockopt,
909         .compat_getsockopt = compat_sock_common_getsockopt,
910 #endif
911 };
912
913 static struct net_proto_family inet_family_ops = {
914         .family = PF_INET,
915         .create = inet_create,
916         .owner  = THIS_MODULE,
917 };
918
919 /* Upon startup we insert all the elements in inetsw_array[] into
920  * the linked list inetsw.
921  */
922 static struct inet_protosw inetsw_array[] =
923 {
924         {
925                 .type =       SOCK_STREAM,
926                 .protocol =   IPPROTO_TCP,
927                 .prot =       &tcp_prot,
928                 .ops =        &inet_stream_ops,
929                 .capability = -1,
930                 .no_check =   0,
931                 .flags =      INET_PROTOSW_PERMANENT |
932                               INET_PROTOSW_ICSK,
933         },
934
935         {
936                 .type =       SOCK_DGRAM,
937                 .protocol =   IPPROTO_UDP,
938                 .prot =       &udp_prot,
939                 .ops =        &inet_dgram_ops,
940                 .capability = -1,
941                 .no_check =   UDP_CSUM_DEFAULT,
942                 .flags =      INET_PROTOSW_PERMANENT,
943        },
944         
945
946        {
947                .type =       SOCK_RAW,
948                .protocol =   IPPROTO_IP,        /* wild card */
949                .prot =       &raw_prot,
950                .ops =        &inet_sockraw_ops,
951                .capability = CAP_NET_RAW,
952                .no_check =   UDP_CSUM_DEFAULT,
953                .flags =      INET_PROTOSW_REUSE,
954        }
955 };
956
957 #define INETSW_ARRAY_LEN (sizeof(inetsw_array) / sizeof(struct inet_protosw))
958
959 void inet_register_protosw(struct inet_protosw *p)
960 {
961         struct list_head *lh;
962         struct inet_protosw *answer;
963         int protocol = p->protocol;
964         struct list_head *last_perm;
965
966         spin_lock_bh(&inetsw_lock);
967
968         if (p->type >= SOCK_MAX)
969                 goto out_illegal;
970
971         /* If we are trying to override a permanent protocol, bail. */
972         answer = NULL;
973         last_perm = &inetsw[p->type];
974         list_for_each(lh, &inetsw[p->type]) {
975                 answer = list_entry(lh, struct inet_protosw, list);
976
977                 /* Check only the non-wild match. */
978                 if (INET_PROTOSW_PERMANENT & answer->flags) {
979                         if (protocol == answer->protocol)
980                                 break;
981                         last_perm = lh;
982                 }
983
984                 answer = NULL;
985         }
986         if (answer)
987                 goto out_permanent;
988
989         /* Add the new entry after the last permanent entry if any, so that
990          * the new entry does not override a permanent entry when matched with
991          * a wild-card protocol. But it is allowed to override any existing
992          * non-permanent entry.  This means that when we remove this entry, the 
993          * system automatically returns to the old behavior.
994          */
995         list_add_rcu(&p->list, last_perm);
996 out:
997         spin_unlock_bh(&inetsw_lock);
998
999         synchronize_net();
1000
1001         return;
1002
1003 out_permanent:
1004         printk(KERN_ERR "Attempt to override permanent protocol %d.\n",
1005                protocol);
1006         goto out;
1007
1008 out_illegal:
1009         printk(KERN_ERR
1010                "Ignoring attempt to register invalid socket type %d.\n",
1011                p->type);
1012         goto out;
1013 }
1014
1015 void inet_unregister_protosw(struct inet_protosw *p)
1016 {
1017         if (INET_PROTOSW_PERMANENT & p->flags) {
1018                 printk(KERN_ERR
1019                        "Attempt to unregister permanent protocol %d.\n",
1020                        p->protocol);
1021         } else {
1022                 spin_lock_bh(&inetsw_lock);
1023                 list_del_rcu(&p->list);
1024                 spin_unlock_bh(&inetsw_lock);
1025
1026                 synchronize_net();
1027         }
1028 }
1029
1030 /*
1031  *      Shall we try to damage output packets if routing dev changes?
1032  */
1033
1034 int sysctl_ip_dynaddr __read_mostly;
1035
1036 static int inet_sk_reselect_saddr(struct sock *sk)
1037 {
1038         struct inet_sock *inet = inet_sk(sk);
1039         int err;
1040         struct rtable *rt;
1041         __be32 old_saddr = inet->saddr;
1042         __be32 new_saddr;
1043         __be32 daddr = inet->daddr;
1044
1045         if (inet->opt && inet->opt->srr)
1046                 daddr = inet->opt->faddr;
1047
1048         /* Query new route. */
1049         err = ip_route_connect(&rt, daddr, 0,
1050                                RT_CONN_FLAGS(sk),
1051                                sk->sk_bound_dev_if,
1052                                sk->sk_protocol,
1053                                inet->sport, inet->dport, sk);
1054         if (err)
1055                 return err;
1056
1057         sk_setup_caps(sk, &rt->u.dst);
1058
1059         new_saddr = rt->rt_src;
1060
1061         if (new_saddr == old_saddr)
1062                 return 0;
1063
1064         if (sysctl_ip_dynaddr > 1) {
1065                 printk(KERN_INFO "%s(): shifting inet->"
1066                                  "saddr from %d.%d.%d.%d to %d.%d.%d.%d\n",
1067                        __FUNCTION__,
1068                        NIPQUAD(old_saddr),
1069                        NIPQUAD(new_saddr));
1070         }
1071
1072         inet->saddr = inet->rcv_saddr = new_saddr;
1073
1074         /*
1075          * XXX The only one ugly spot where we need to
1076          * XXX really change the sockets identity after
1077          * XXX it has entered the hashes. -DaveM
1078          *
1079          * Besides that, it does not check for connection
1080          * uniqueness. Wait for troubles.
1081          */
1082         __sk_prot_rehash(sk);
1083         return 0;
1084 }
1085
1086 int inet_sk_rebuild_header(struct sock *sk)
1087 {
1088         struct inet_sock *inet = inet_sk(sk);
1089         struct rtable *rt = (struct rtable *)__sk_dst_check(sk, 0);
1090         __be32 daddr;
1091         int err;
1092
1093         /* Route is OK, nothing to do. */
1094         if (rt)
1095                 return 0;
1096
1097         /* Reroute. */
1098         daddr = inet->daddr;
1099         if (inet->opt && inet->opt->srr)
1100                 daddr = inet->opt->faddr;
1101 {
1102         struct flowi fl = {
1103                 .oif = sk->sk_bound_dev_if,
1104                 .nl_u = {
1105                         .ip4_u = {
1106                                 .daddr  = daddr,
1107                                 .saddr  = inet->saddr,
1108                                 .tos    = RT_CONN_FLAGS(sk),
1109                         },
1110                 },
1111                 .proto = sk->sk_protocol,
1112                 .uli_u = {
1113                         .ports = {
1114                                 .sport = inet->sport,
1115                                 .dport = inet->dport,
1116                         },
1117                 },
1118         };
1119                                                 
1120         security_sk_classify_flow(sk, &fl);
1121         err = ip_route_output_flow(&rt, &fl, sk, 0);
1122 }
1123         if (!err)
1124                 sk_setup_caps(sk, &rt->u.dst);
1125         else {
1126                 /* Routing failed... */
1127                 sk->sk_route_caps = 0;
1128                 /*
1129                  * Other protocols have to map its equivalent state to TCP_SYN_SENT.
1130                  * DCCP maps its DCCP_REQUESTING state to TCP_SYN_SENT. -acme
1131                  */
1132                 if (!sysctl_ip_dynaddr ||
1133                     sk->sk_state != TCP_SYN_SENT ||
1134                     (sk->sk_userlocks & SOCK_BINDADDR_LOCK) ||
1135                     (err = inet_sk_reselect_saddr(sk)) != 0)
1136                         sk->sk_err_soft = -err;
1137         }
1138
1139         return err;
1140 }
1141
1142 EXPORT_SYMBOL(inet_sk_rebuild_header);
1143
1144 static int inet_gso_send_check(struct sk_buff *skb)
1145 {
1146         struct iphdr *iph;
1147         struct net_protocol *ops;
1148         int proto;
1149         int ihl;
1150         int err = -EINVAL;
1151
1152         if (unlikely(!pskb_may_pull(skb, sizeof(*iph))))
1153                 goto out;
1154
1155         iph = skb->nh.iph;
1156         ihl = iph->ihl * 4;
1157         if (ihl < sizeof(*iph))
1158                 goto out;
1159
1160         if (unlikely(!pskb_may_pull(skb, ihl)))
1161                 goto out;
1162
1163         skb->h.raw = __skb_pull(skb, ihl);
1164         iph = skb->nh.iph;
1165         proto = iph->protocol & (MAX_INET_PROTOS - 1);
1166         err = -EPROTONOSUPPORT;
1167
1168         rcu_read_lock();
1169         ops = rcu_dereference(inet_protos[proto]);
1170         if (likely(ops && ops->gso_send_check))
1171                 err = ops->gso_send_check(skb);
1172         rcu_read_unlock();
1173
1174 out:
1175         return err;
1176 }
1177
1178 static struct sk_buff *inet_gso_segment(struct sk_buff *skb, int features)
1179 {
1180         struct sk_buff *segs = ERR_PTR(-EINVAL);
1181         struct iphdr *iph;
1182         struct net_protocol *ops;
1183         int proto;
1184         int ihl;
1185         int id;
1186
1187         if (unlikely(skb_shinfo(skb)->gso_type &
1188                      ~(SKB_GSO_TCPV4 |
1189                        SKB_GSO_UDP |
1190                        SKB_GSO_DODGY |
1191                        SKB_GSO_TCP_ECN |
1192                        0)))
1193                 goto out;
1194
1195         if (unlikely(!pskb_may_pull(skb, sizeof(*iph))))
1196                 goto out;
1197
1198         iph = skb->nh.iph;
1199         ihl = iph->ihl * 4;
1200         if (ihl < sizeof(*iph))
1201                 goto out;
1202
1203         if (unlikely(!pskb_may_pull(skb, ihl)))
1204                 goto out;
1205
1206         skb->h.raw = __skb_pull(skb, ihl);
1207         iph = skb->nh.iph;
1208         id = ntohs(iph->id);
1209         proto = iph->protocol & (MAX_INET_PROTOS - 1);
1210         segs = ERR_PTR(-EPROTONOSUPPORT);
1211
1212         rcu_read_lock();
1213         ops = rcu_dereference(inet_protos[proto]);
1214         if (likely(ops && ops->gso_segment))
1215                 segs = ops->gso_segment(skb, features);
1216         rcu_read_unlock();
1217
1218         if (!segs || unlikely(IS_ERR(segs)))
1219                 goto out;
1220
1221         skb = segs;
1222         do {
1223                 iph = skb->nh.iph;
1224                 iph->id = htons(id++);
1225                 iph->tot_len = htons(skb->len - skb->mac_len);
1226                 iph->check = 0;
1227                 iph->check = ip_fast_csum(skb->nh.raw, iph->ihl);
1228         } while ((skb = skb->next));
1229
1230 out:
1231         return segs;
1232 }
1233
1234 #ifdef CONFIG_IP_MULTICAST
1235 static struct net_protocol igmp_protocol = {
1236         .handler =      igmp_rcv,
1237 };
1238 #endif
1239
1240 static struct net_protocol tcp_protocol = {
1241         .handler =      tcp_v4_rcv,
1242         .err_handler =  tcp_v4_err,
1243         .gso_send_check = tcp_v4_gso_send_check,
1244         .gso_segment =  tcp_tso_segment,
1245         .no_policy =    1,
1246 };
1247
1248 static struct net_protocol udp_protocol = {
1249         .handler =      udp_rcv,
1250         .err_handler =  udp_err,
1251         .no_policy =    1,
1252 };
1253
1254 static struct net_protocol icmp_protocol = {
1255         .handler =      icmp_rcv,
1256 };
1257
1258 static int __init init_ipv4_mibs(void)
1259 {
1260         net_statistics[0] = alloc_percpu(struct linux_mib);
1261         net_statistics[1] = alloc_percpu(struct linux_mib);
1262         ip_statistics[0] = alloc_percpu(struct ipstats_mib);
1263         ip_statistics[1] = alloc_percpu(struct ipstats_mib);
1264         icmp_statistics[0] = alloc_percpu(struct icmp_mib);
1265         icmp_statistics[1] = alloc_percpu(struct icmp_mib);
1266         tcp_statistics[0] = alloc_percpu(struct tcp_mib);
1267         tcp_statistics[1] = alloc_percpu(struct tcp_mib);
1268         udp_statistics[0] = alloc_percpu(struct udp_mib);
1269         udp_statistics[1] = alloc_percpu(struct udp_mib);
1270         udplite_statistics[0] = alloc_percpu(struct udp_mib);
1271         udplite_statistics[1] = alloc_percpu(struct udp_mib);
1272         if (!
1273             (net_statistics[0] && net_statistics[1] && ip_statistics[0]
1274              && ip_statistics[1] && tcp_statistics[0] && tcp_statistics[1]
1275              && udp_statistics[0] && udp_statistics[1]
1276              && udplite_statistics[0] && udplite_statistics[1]             ) )
1277                 return -ENOMEM;
1278
1279         (void) tcp_mib_init();
1280
1281         return 0;
1282 }
1283
1284 static int ipv4_proc_init(void);
1285
1286 /*
1287  *      IP protocol layer initialiser
1288  */
1289
1290 static struct packet_type ip_packet_type = {
1291         .type = __constant_htons(ETH_P_IP),
1292         .func = ip_rcv,
1293         .gso_send_check = inet_gso_send_check,
1294         .gso_segment = inet_gso_segment,
1295 };
1296
1297 static int __init inet_init(void)
1298 {
1299         struct sk_buff *dummy_skb;
1300         struct inet_protosw *q;
1301         struct list_head *r;
1302         int rc = -EINVAL;
1303
1304         BUILD_BUG_ON(sizeof(struct inet_skb_parm) > sizeof(dummy_skb->cb));
1305
1306         rc = proto_register(&tcp_prot, 1);
1307         if (rc)
1308                 goto out;
1309
1310         rc = proto_register(&udp_prot, 1);
1311         if (rc)
1312                 goto out_unregister_tcp_proto;
1313
1314         rc = proto_register(&raw_prot, 1);
1315         if (rc)
1316                 goto out_unregister_udp_proto;
1317
1318         /*
1319          *      Tell SOCKET that we are alive... 
1320          */
1321
1322         (void)sock_register(&inet_family_ops);
1323
1324         /*
1325          *      Add all the base protocols.
1326          */
1327
1328         if (inet_add_protocol(&icmp_protocol, IPPROTO_ICMP) < 0)
1329                 printk(KERN_CRIT "inet_init: Cannot add ICMP protocol\n");
1330         if (inet_add_protocol(&udp_protocol, IPPROTO_UDP) < 0)
1331                 printk(KERN_CRIT "inet_init: Cannot add UDP protocol\n");
1332         if (inet_add_protocol(&tcp_protocol, IPPROTO_TCP) < 0)
1333                 printk(KERN_CRIT "inet_init: Cannot add TCP protocol\n");
1334 #ifdef CONFIG_IP_MULTICAST
1335         if (inet_add_protocol(&igmp_protocol, IPPROTO_IGMP) < 0)
1336                 printk(KERN_CRIT "inet_init: Cannot add IGMP protocol\n");
1337 #endif
1338
1339         /* Register the socket-side information for inet_create. */
1340         for (r = &inetsw[0]; r < &inetsw[SOCK_MAX]; ++r)
1341                 INIT_LIST_HEAD(r);
1342
1343         for (q = inetsw_array; q < &inetsw_array[INETSW_ARRAY_LEN]; ++q)
1344                 inet_register_protosw(q);
1345
1346         /*
1347          *      Set the ARP module up
1348          */
1349
1350         arp_init();
1351
1352         /*
1353          *      Set the IP module up
1354          */
1355
1356         ip_init();
1357
1358         tcp_v4_init(&inet_family_ops);
1359
1360         /* Setup TCP slab cache for open requests. */
1361         tcp_init();
1362
1363         /* Add UDP-Lite (RFC 3828) */
1364         udplite4_register();
1365
1366         /*
1367          *      Set the ICMP layer up
1368          */
1369
1370         icmp_init(&inet_family_ops);
1371
1372         /*
1373          *      Initialise the multicast router
1374          */
1375 #if defined(CONFIG_IP_MROUTE)
1376         ip_mr_init();
1377 #endif
1378         /*
1379          *      Initialise per-cpu ipv4 mibs
1380          */ 
1381
1382         if(init_ipv4_mibs())
1383                 printk(KERN_CRIT "inet_init: Cannot init ipv4 mibs\n"); ;
1384         
1385         ipv4_proc_init();
1386
1387         ipfrag_init();
1388
1389         dev_add_pack(&ip_packet_type);
1390
1391         rc = 0;
1392 out:
1393         return rc;
1394 out_unregister_udp_proto:
1395         proto_unregister(&udp_prot);
1396 out_unregister_tcp_proto:
1397         proto_unregister(&tcp_prot);
1398         goto out;
1399 }
1400
1401 fs_initcall(inet_init);
1402
1403 /* ------------------------------------------------------------------------ */
1404
1405 #ifdef CONFIG_PROC_FS
1406 static int __init ipv4_proc_init(void)
1407 {
1408         int rc = 0;
1409
1410         if (raw_proc_init())
1411                 goto out_raw;
1412         if (tcp4_proc_init())
1413                 goto out_tcp;
1414         if (udp4_proc_init())
1415                 goto out_udp;
1416         if (fib_proc_init())
1417                 goto out_fib;
1418         if (ip_misc_proc_init())
1419                 goto out_misc;
1420 out:
1421         return rc;
1422 out_misc:
1423         fib_proc_exit();
1424 out_fib:
1425         udp4_proc_exit();
1426 out_udp:
1427         tcp4_proc_exit();
1428 out_tcp:
1429         raw_proc_exit();
1430 out_raw:
1431         rc = -ENOMEM;
1432         goto out;
1433 }
1434
1435 #else /* CONFIG_PROC_FS */
1436 static int __init ipv4_proc_init(void)
1437 {
1438         return 0;
1439 }
1440 #endif /* CONFIG_PROC_FS */
1441
1442 MODULE_ALIAS_NETPROTO(PF_INET);
1443
1444 EXPORT_SYMBOL(inet_accept);
1445 EXPORT_SYMBOL(inet_bind);
1446 EXPORT_SYMBOL(inet_dgram_connect);
1447 EXPORT_SYMBOL(inet_dgram_ops);
1448 EXPORT_SYMBOL(inet_getname);
1449 EXPORT_SYMBOL(inet_ioctl);
1450 EXPORT_SYMBOL(inet_listen);
1451 EXPORT_SYMBOL(inet_register_protosw);
1452 EXPORT_SYMBOL(inet_release);
1453 EXPORT_SYMBOL(inet_sendmsg);
1454 EXPORT_SYMBOL(inet_shutdown);
1455 EXPORT_SYMBOL(inet_sock_destruct);
1456 EXPORT_SYMBOL(inet_stream_connect);
1457 EXPORT_SYMBOL(inet_stream_ops);
1458 EXPORT_SYMBOL(inet_unregister_protosw);
1459 EXPORT_SYMBOL(net_statistics);
1460 EXPORT_SYMBOL(sysctl_ip_nonlocal_bind);