]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/ipv4/af_inet.c
ipv4: add pernet mib operations
[net-next-2.6.git] / net / ipv4 / af_inet.c
CommitLineData
1da177e4
LT
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 *
02c30a84 8 * Authors: Ross Biro
1da177e4
LT
9 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
10 * Florian La Roche, <flla@stud.uni-sb.de>
11 * Alan Cox, <A.Cox@swansea.ac.uk>
12 *
13 * Changes (see also sock.c)
14 *
15 * piggy,
16 * Karl Knutson : Socket protocol table
17 * A.N.Kuznetsov : Socket death error in accept().
18 * John Richardson : Fix non blocking error in connect()
19 * so sockets that fail to connect
20 * don't return -EINPROGRESS.
21 * Alan Cox : Asynchronous I/O support
22 * Alan Cox : Keep correct socket pointer on sock
23 * structures
24 * when accept() ed
25 * Alan Cox : Semantics of SO_LINGER aren't state
26 * moved to close when you look carefully.
27 * With this fixed and the accept bug fixed
28 * some RPC stuff seems happier.
29 * Niibe Yutaka : 4.4BSD style write async I/O
30 * Alan Cox,
31 * Tony Gale : Fixed reuse semantics.
32 * Alan Cox : bind() shouldn't abort existing but dead
33 * sockets. Stops FTP netin:.. I hope.
34 * Alan Cox : bind() works correctly for RAW sockets.
35 * Note that FreeBSD at least was broken
36 * in this respect so be careful with
37 * compatibility tests...
38 * Alan Cox : routing cache support
39 * Alan Cox : memzero the socket structure for
40 * compactness.
41 * Matt Day : nonblock connect error handler
42 * Alan Cox : Allow large numbers of pending sockets
43 * (eg for big web sites), but only if
44 * specifically application requested.
45 * Alan Cox : New buffering throughout IP. Used
46 * dumbly.
47 * Alan Cox : New buffering now used smartly.
48 * Alan Cox : BSD rather than common sense
49 * interpretation of listen.
50 * Germano Caronni : Assorted small races.
51 * Alan Cox : sendmsg/recvmsg basic support.
52 * Alan Cox : Only sendmsg/recvmsg now supported.
53 * Alan Cox : Locked down bind (see security list).
54 * Alan Cox : Loosened bind a little.
55 * Mike McLagan : ADD/DEL DLCI Ioctls
56 * Willy Konynenberg : Transparent proxying support.
57 * David S. Miller : New socket lookup architecture.
58 * Some other random speedups.
59 * Cyrus Durgin : Cleaned up file for kmod hacks.
60 * Andi Kleen : Fix inet_stream_connect TCP race.
61 *
62 * This program is free software; you can redistribute it and/or
63 * modify it under the terms of the GNU General Public License
64 * as published by the Free Software Foundation; either version
65 * 2 of the License, or (at your option) any later version.
66 */
67
f4c50d99 68#include <linux/err.h>
1da177e4
LT
69#include <linux/errno.h>
70#include <linux/types.h>
71#include <linux/socket.h>
72#include <linux/in.h>
73#include <linux/kernel.h>
1da177e4
LT
74#include <linux/module.h>
75#include <linux/sched.h>
76#include <linux/timer.h>
77#include <linux/string.h>
78#include <linux/sockios.h>
79#include <linux/net.h>
4fc268d2 80#include <linux/capability.h>
1da177e4
LT
81#include <linux/fcntl.h>
82#include <linux/mm.h>
83#include <linux/interrupt.h>
84#include <linux/stat.h>
85#include <linux/init.h>
86#include <linux/poll.h>
87#include <linux/netfilter_ipv4.h>
b3da2cf3 88#include <linux/random.h>
1da177e4
LT
89
90#include <asm/uaccess.h>
91#include <asm/system.h>
92
1da177e4
LT
93#include <linux/inet.h>
94#include <linux/igmp.h>
14c85021 95#include <linux/inetdevice.h>
1da177e4
LT
96#include <linux/netdevice.h>
97#include <net/ip.h>
98#include <net/protocol.h>
99#include <net/arp.h>
100#include <net/route.h>
101#include <net/ip_fib.h>
295f7324 102#include <net/inet_connection_sock.h>
1da177e4
LT
103#include <net/tcp.h>
104#include <net/udp.h>
ba4e58ec 105#include <net/udplite.h>
1da177e4
LT
106#include <linux/skbuff.h>
107#include <net/sock.h>
108#include <net/raw.h>
109#include <net/icmp.h>
110#include <net/ipip.h>
111#include <net/inet_common.h>
112#include <net/xfrm.h>
9b4661bd 113#include <net/net_namespace.h>
1da177e4
LT
114#ifdef CONFIG_IP_MROUTE
115#include <linux/mroute.h>
116#endif
117
ba89966c 118DEFINE_SNMP_STAT(struct linux_mib, net_statistics) __read_mostly;
1da177e4 119
1da177e4
LT
120extern void ip_mc_drop_socket(struct sock *sk);
121
122/* The inetsw table contains everything that inet_create needs to
123 * build a new socket.
124 */
125static struct list_head inetsw[SOCK_MAX];
126static DEFINE_SPINLOCK(inetsw_lock);
127
9ba63979
PE
128struct ipv4_config ipv4_config;
129
130EXPORT_SYMBOL(ipv4_config);
131
1da177e4
LT
132/* New destruction routine */
133
134void inet_sock_destruct(struct sock *sk)
135{
136 struct inet_sock *inet = inet_sk(sk);
137
138 __skb_queue_purge(&sk->sk_receive_queue);
139 __skb_queue_purge(&sk->sk_error_queue);
140
95766fff
HA
141 sk_mem_reclaim(sk);
142
1da177e4
LT
143 if (sk->sk_type == SOCK_STREAM && sk->sk_state != TCP_CLOSE) {
144 printk("Attempt to release TCP socket in state %d %p\n",
145 sk->sk_state, sk);
146 return;
147 }
148 if (!sock_flag(sk, SOCK_DEAD)) {
149 printk("Attempt to release alive inet socket %p\n", sk);
150 return;
151 }
152
153 BUG_TRAP(!atomic_read(&sk->sk_rmem_alloc));
154 BUG_TRAP(!atomic_read(&sk->sk_wmem_alloc));
155 BUG_TRAP(!sk->sk_wmem_queued);
156 BUG_TRAP(!sk->sk_forward_alloc);
157
a51482bd 158 kfree(inet->opt);
1da177e4 159 dst_release(sk->sk_dst_cache);
e6848976 160 sk_refcnt_debug_dec(sk);
1da177e4
LT
161}
162
163/*
164 * The routines beyond this point handle the behaviour of an AF_INET
165 * socket object. Mostly it punts to the subprotocols of IP to do
166 * the work.
167 */
168
169/*
170 * Automatically bind an unbound socket.
171 */
172
173static int inet_autobind(struct sock *sk)
174{
175 struct inet_sock *inet;
176 /* We may need to bind the socket. */
177 lock_sock(sk);
178 inet = inet_sk(sk);
179 if (!inet->num) {
180 if (sk->sk_prot->get_port(sk, 0)) {
181 release_sock(sk);
182 return -EAGAIN;
183 }
184 inet->sport = htons(inet->num);
185 }
186 release_sock(sk);
187 return 0;
188}
189
190/*
191 * Move a socket into listening state.
192 */
193int inet_listen(struct socket *sock, int backlog)
194{
195 struct sock *sk = sock->sk;
196 unsigned char old_state;
197 int err;
198
199 lock_sock(sk);
200
201 err = -EINVAL;
202 if (sock->state != SS_UNCONNECTED || sock->type != SOCK_STREAM)
203 goto out;
204
205 old_state = sk->sk_state;
206 if (!((1 << old_state) & (TCPF_CLOSE | TCPF_LISTEN)))
207 goto out;
208
209 /* Really, if the socket is already in listen state
210 * we can only allow the backlog to be adjusted.
211 */
212 if (old_state != TCP_LISTEN) {
72a3effa 213 err = inet_csk_listen_start(sk, backlog);
1da177e4
LT
214 if (err)
215 goto out;
216 }
217 sk->sk_max_ack_backlog = backlog;
218 err = 0;
219
220out:
221 release_sock(sk);
222 return err;
223}
224
be776281 225u32 inet_ehash_secret __read_mostly;
b3da2cf3
DM
226EXPORT_SYMBOL(inet_ehash_secret);
227
be776281
ED
228/*
229 * inet_ehash_secret must be set exactly once
230 * Instead of using a dedicated spinlock, we (ab)use inetsw_lock
231 */
b3da2cf3
DM
232void build_ehash_secret(void)
233{
be776281
ED
234 u32 rnd;
235 do {
236 get_random_bytes(&rnd, sizeof(rnd));
237 } while (rnd == 0);
238 spin_lock_bh(&inetsw_lock);
239 if (!inet_ehash_secret)
240 inet_ehash_secret = rnd;
241 spin_unlock_bh(&inetsw_lock);
b3da2cf3
DM
242}
243EXPORT_SYMBOL(build_ehash_secret);
244
2342fd7e
DL
245static inline int inet_netns_ok(struct net *net, int protocol)
246{
247 int hash;
248 struct net_protocol *ipprot;
249
250 if (net == &init_net)
251 return 1;
252
253 hash = protocol & (MAX_INET_PROTOS - 1);
254 ipprot = rcu_dereference(inet_protos[hash]);
255
256 if (ipprot == NULL)
257 /* raw IP is OK */
258 return 1;
259 return ipprot->netns_ok;
260}
261
1da177e4
LT
262/*
263 * Create an inet socket.
264 */
265
1b8d7ae4 266static int inet_create(struct net *net, struct socket *sock, int protocol)
1da177e4
LT
267{
268 struct sock *sk;
269 struct list_head *p;
270 struct inet_protosw *answer;
271 struct inet_sock *inet;
272 struct proto *answer_prot;
273 unsigned char answer_flags;
274 char answer_no_check;
bb97d31f 275 int try_loading_module = 0;
86c8f9d1 276 int err;
1da177e4 277
b3da2cf3
DM
278 if (sock->type != SOCK_RAW &&
279 sock->type != SOCK_DGRAM &&
280 !inet_ehash_secret)
281 build_ehash_secret();
282
1da177e4
LT
283 sock->state = SS_UNCONNECTED;
284
285 /* Look for the requested type/protocol pair. */
286 answer = NULL;
bb97d31f 287lookup_protocol:
86c8f9d1 288 err = -ESOCKTNOSUPPORT;
1da177e4
LT
289 rcu_read_lock();
290 list_for_each_rcu(p, &inetsw[sock->type]) {
291 answer = list_entry(p, struct inet_protosw, list);
292
293 /* Check the non-wild match. */
294 if (protocol == answer->protocol) {
295 if (protocol != IPPROTO_IP)
296 break;
297 } else {
298 /* Check for the two wild cases. */
299 if (IPPROTO_IP == protocol) {
300 protocol = answer->protocol;
301 break;
302 }
303 if (IPPROTO_IP == answer->protocol)
304 break;
305 }
86c8f9d1 306 err = -EPROTONOSUPPORT;
1da177e4
LT
307 answer = NULL;
308 }
309
bb97d31f
ACM
310 if (unlikely(answer == NULL)) {
311 if (try_loading_module < 2) {
312 rcu_read_unlock();
313 /*
314 * Be more specific, e.g. net-pf-2-proto-132-type-1
315 * (net-pf-PF_INET-proto-IPPROTO_SCTP-type-SOCK_STREAM)
316 */
317 if (++try_loading_module == 1)
318 request_module("net-pf-%d-proto-%d-type-%d",
319 PF_INET, protocol, sock->type);
320 /*
321 * Fall back to generic, e.g. net-pf-2-proto-132
322 * (net-pf-PF_INET-proto-IPPROTO_SCTP)
323 */
324 else
325 request_module("net-pf-%d-proto-%d",
326 PF_INET, protocol);
327 goto lookup_protocol;
328 } else
329 goto out_rcu_unlock;
330 }
331
1da177e4
LT
332 err = -EPERM;
333 if (answer->capability > 0 && !capable(answer->capability))
334 goto out_rcu_unlock;
1da177e4 335
2342fd7e
DL
336 err = -EAFNOSUPPORT;
337 if (!inet_netns_ok(net, protocol))
338 goto out_rcu_unlock;
339
1da177e4
LT
340 sock->ops = answer->ops;
341 answer_prot = answer->prot;
342 answer_no_check = answer->no_check;
343 answer_flags = answer->flags;
344 rcu_read_unlock();
345
346 BUG_TRAP(answer_prot->slab != NULL);
347
348 err = -ENOBUFS;
6257ff21 349 sk = sk_alloc(net, PF_INET, GFP_KERNEL, answer_prot);
1da177e4
LT
350 if (sk == NULL)
351 goto out;
352
353 err = 0;
354 sk->sk_no_check = answer_no_check;
355 if (INET_PROTOSW_REUSE & answer_flags)
356 sk->sk_reuse = 1;
357
358 inet = inet_sk(sk);
469de9b9 359 inet->is_icsk = (INET_PROTOSW_ICSK & answer_flags) != 0;
1da177e4
LT
360
361 if (SOCK_RAW == sock->type) {
362 inet->num = protocol;
363 if (IPPROTO_RAW == protocol)
364 inet->hdrincl = 1;
365 }
366
367 if (ipv4_config.no_pmtu_disc)
368 inet->pmtudisc = IP_PMTUDISC_DONT;
369 else
370 inet->pmtudisc = IP_PMTUDISC_WANT;
371
372 inet->id = 0;
373
374 sock_init_data(sock, sk);
375
376 sk->sk_destruct = inet_sock_destruct;
377 sk->sk_family = PF_INET;
378 sk->sk_protocol = protocol;
379 sk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
380
381 inet->uc_ttl = -1;
382 inet->mc_loop = 1;
383 inet->mc_ttl = 1;
384 inet->mc_index = 0;
385 inet->mc_list = NULL;
386
e6848976 387 sk_refcnt_debug_inc(sk);
1da177e4
LT
388
389 if (inet->num) {
390 /* It assumes that any protocol which allows
391 * the user to assign a number at socket
392 * creation time automatically
393 * shares.
394 */
395 inet->sport = htons(inet->num);
396 /* Add to protocol hash chains. */
397 sk->sk_prot->hash(sk);
398 }
399
400 if (sk->sk_prot->init) {
401 err = sk->sk_prot->init(sk);
402 if (err)
403 sk_common_release(sk);
404 }
405out:
406 return err;
407out_rcu_unlock:
408 rcu_read_unlock();
409 goto out;
410}
411
412
413/*
414 * The peer socket should always be NULL (or else). When we call this
415 * function we are destroying the object and from then on nobody
416 * should refer to it.
417 */
418int inet_release(struct socket *sock)
419{
420 struct sock *sk = sock->sk;
421
422 if (sk) {
423 long timeout;
424
425 /* Applications forget to leave groups before exiting */
426 ip_mc_drop_socket(sk);
427
428 /* If linger is set, we don't return until the close
429 * is complete. Otherwise we return immediately. The
430 * actually closing is done the same either way.
431 *
432 * If the close is due to the process exiting, we never
433 * linger..
434 */
435 timeout = 0;
436 if (sock_flag(sk, SOCK_LINGER) &&
437 !(current->flags & PF_EXITING))
438 timeout = sk->sk_lingertime;
439 sock->sk = NULL;
440 sk->sk_prot->close(sk, timeout);
441 }
442 return 0;
443}
444
445/* It is off by default, see below. */
ab32ea5d 446int sysctl_ip_nonlocal_bind __read_mostly;
1da177e4
LT
447
448int inet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
449{
450 struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
451 struct sock *sk = sock->sk;
452 struct inet_sock *inet = inet_sk(sk);
453 unsigned short snum;
454 int chk_addr_ret;
455 int err;
456
457 /* If the socket has its own bind function then use it. (RAW) */
458 if (sk->sk_prot->bind) {
459 err = sk->sk_prot->bind(sk, uaddr, addr_len);
460 goto out;
461 }
462 err = -EINVAL;
463 if (addr_len < sizeof(struct sockaddr_in))
464 goto out;
465
3b1e0a65 466 chk_addr_ret = inet_addr_type(sock_net(sk), addr->sin_addr.s_addr);
1da177e4
LT
467
468 /* Not specified by any standard per-se, however it breaks too
469 * many applications when removed. It is unfortunate since
470 * allowing applications to make a non-local bind solves
471 * several problems with systems using dynamic addressing.
472 * (ie. your servers still start up even if your ISDN link
473 * is temporarily down)
474 */
475 err = -EADDRNOTAVAIL;
476 if (!sysctl_ip_nonlocal_bind &&
477 !inet->freebind &&
e6f1cebf 478 addr->sin_addr.s_addr != htonl(INADDR_ANY) &&
1da177e4
LT
479 chk_addr_ret != RTN_LOCAL &&
480 chk_addr_ret != RTN_MULTICAST &&
481 chk_addr_ret != RTN_BROADCAST)
482 goto out;
483
484 snum = ntohs(addr->sin_port);
485 err = -EACCES;
486 if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
487 goto out;
488
489 /* We keep a pair of addresses. rcv_saddr is the one
490 * used by hash lookups, and saddr is used for transmit.
491 *
492 * In the BSD API these are the same except where it
493 * would be illegal to use them (multicast/broadcast) in
494 * which case the sending device address is used.
495 */
496 lock_sock(sk);
497
498 /* Check these errors (active socket, double bind). */
499 err = -EINVAL;
500 if (sk->sk_state != TCP_CLOSE || inet->num)
501 goto out_release_sock;
502
503 inet->rcv_saddr = inet->saddr = addr->sin_addr.s_addr;
504 if (chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST)
505 inet->saddr = 0; /* Use device */
506
507 /* Make sure we are allowed to bind here. */
508 if (sk->sk_prot->get_port(sk, snum)) {
509 inet->saddr = inet->rcv_saddr = 0;
510 err = -EADDRINUSE;
511 goto out_release_sock;
512 }
513
514 if (inet->rcv_saddr)
515 sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
516 if (snum)
517 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
518 inet->sport = htons(inet->num);
519 inet->daddr = 0;
520 inet->dport = 0;
521 sk_dst_reset(sk);
522 err = 0;
523out_release_sock:
524 release_sock(sk);
525out:
526 return err;
527}
528
529int inet_dgram_connect(struct socket *sock, struct sockaddr * uaddr,
530 int addr_len, int flags)
531{
532 struct sock *sk = sock->sk;
533
534 if (uaddr->sa_family == AF_UNSPEC)
535 return sk->sk_prot->disconnect(sk, flags);
536
537 if (!inet_sk(sk)->num && inet_autobind(sk))
538 return -EAGAIN;
539 return sk->sk_prot->connect(sk, (struct sockaddr *)uaddr, addr_len);
540}
541
542static long inet_wait_for_connect(struct sock *sk, long timeo)
543{
544 DEFINE_WAIT(wait);
545
546 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
547
548 /* Basic assumption: if someone sets sk->sk_err, he _must_
549 * change state of the socket from TCP_SYN_*.
550 * Connect() does not allow to get error notifications
551 * without closing the socket.
552 */
553 while ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
554 release_sock(sk);
555 timeo = schedule_timeout(timeo);
556 lock_sock(sk);
557 if (signal_pending(current) || !timeo)
558 break;
559 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
560 }
561 finish_wait(sk->sk_sleep, &wait);
562 return timeo;
563}
564
565/*
566 * Connect to a remote host. There is regrettably still a little
567 * TCP 'magic' in here.
568 */
569int inet_stream_connect(struct socket *sock, struct sockaddr *uaddr,
570 int addr_len, int flags)
571{
572 struct sock *sk = sock->sk;
573 int err;
574 long timeo;
575
576 lock_sock(sk);
577
578 if (uaddr->sa_family == AF_UNSPEC) {
579 err = sk->sk_prot->disconnect(sk, flags);
580 sock->state = err ? SS_DISCONNECTING : SS_UNCONNECTED;
581 goto out;
582 }
583
584 switch (sock->state) {
585 default:
586 err = -EINVAL;
587 goto out;
588 case SS_CONNECTED:
589 err = -EISCONN;
590 goto out;
591 case SS_CONNECTING:
592 err = -EALREADY;
593 /* Fall out of switch with err, set for this state */
594 break;
595 case SS_UNCONNECTED:
596 err = -EISCONN;
597 if (sk->sk_state != TCP_CLOSE)
598 goto out;
599
600 err = sk->sk_prot->connect(sk, uaddr, addr_len);
601 if (err < 0)
602 goto out;
603
e905a9ed 604 sock->state = SS_CONNECTING;
1da177e4
LT
605
606 /* Just entered SS_CONNECTING state; the only
607 * difference is that return value in non-blocking
608 * case is EINPROGRESS, rather than EALREADY.
609 */
610 err = -EINPROGRESS;
611 break;
612 }
613
614 timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
615
616 if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
617 /* Error code is set above */
618 if (!timeo || !inet_wait_for_connect(sk, timeo))
619 goto out;
620
621 err = sock_intr_errno(timeo);
622 if (signal_pending(current))
623 goto out;
624 }
625
626 /* Connection was closed by RST, timeout, ICMP error
627 * or another process disconnected us.
628 */
629 if (sk->sk_state == TCP_CLOSE)
630 goto sock_error;
631
632 /* sk->sk_err may be not zero now, if RECVERR was ordered by user
633 * and error was received after socket entered established state.
634 * Hence, it is handled normally after connect() return successfully.
635 */
636
637 sock->state = SS_CONNECTED;
638 err = 0;
639out:
640 release_sock(sk);
641 return err;
642
643sock_error:
644 err = sock_error(sk) ? : -ECONNABORTED;
645 sock->state = SS_UNCONNECTED;
646 if (sk->sk_prot->disconnect(sk, flags))
647 sock->state = SS_DISCONNECTING;
648 goto out;
649}
650
651/*
652 * Accept a pending connection. The TCP layer now gives BSD semantics.
653 */
654
655int inet_accept(struct socket *sock, struct socket *newsock, int flags)
656{
657 struct sock *sk1 = sock->sk;
658 int err = -EINVAL;
659 struct sock *sk2 = sk1->sk_prot->accept(sk1, flags, &err);
660
661 if (!sk2)
662 goto do_err;
663
664 lock_sock(sk2);
665
666 BUG_TRAP((1 << sk2->sk_state) &
667 (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT | TCPF_CLOSE));
668
669 sock_graft(sk2, newsock);
670
671 newsock->state = SS_CONNECTED;
672 err = 0;
673 release_sock(sk2);
674do_err:
675 return err;
676}
677
678
679/*
680 * This does both peername and sockname.
681 */
682int inet_getname(struct socket *sock, struct sockaddr *uaddr,
683 int *uaddr_len, int peer)
684{
685 struct sock *sk = sock->sk;
686 struct inet_sock *inet = inet_sk(sk);
687 struct sockaddr_in *sin = (struct sockaddr_in *)uaddr;
688
689 sin->sin_family = AF_INET;
690 if (peer) {
691 if (!inet->dport ||
692 (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
693 peer == 1))
694 return -ENOTCONN;
695 sin->sin_port = inet->dport;
696 sin->sin_addr.s_addr = inet->daddr;
697 } else {
714e85be 698 __be32 addr = inet->rcv_saddr;
1da177e4
LT
699 if (!addr)
700 addr = inet->saddr;
701 sin->sin_port = inet->sport;
702 sin->sin_addr.s_addr = addr;
703 }
704 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
705 *uaddr_len = sizeof(*sin);
706 return 0;
707}
708
709int inet_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg,
710 size_t size)
711{
712 struct sock *sk = sock->sk;
713
714 /* We may need to bind the socket. */
715 if (!inet_sk(sk)->num && inet_autobind(sk))
716 return -EAGAIN;
717
718 return sk->sk_prot->sendmsg(iocb, sk, msg, size);
719}
720
721
722static ssize_t inet_sendpage(struct socket *sock, struct page *page, int offset, size_t size, int flags)
723{
724 struct sock *sk = sock->sk;
725
726 /* We may need to bind the socket. */
727 if (!inet_sk(sk)->num && inet_autobind(sk))
728 return -EAGAIN;
729
730 if (sk->sk_prot->sendpage)
731 return sk->sk_prot->sendpage(sk, page, offset, size, flags);
732 return sock_no_sendpage(sock, page, offset, size, flags);
733}
734
735
736int inet_shutdown(struct socket *sock, int how)
737{
738 struct sock *sk = sock->sk;
739 int err = 0;
740
741 /* This should really check to make sure
742 * the socket is a TCP socket. (WHY AC...)
743 */
744 how++; /* maps 0->1 has the advantage of making bit 1 rcvs and
745 1->2 bit 2 snds.
746 2->3 */
747 if ((how & ~SHUTDOWN_MASK) || !how) /* MAXINT->0 */
748 return -EINVAL;
749
750 lock_sock(sk);
751 if (sock->state == SS_CONNECTING) {
752 if ((1 << sk->sk_state) &
753 (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_CLOSE))
754 sock->state = SS_DISCONNECTING;
755 else
756 sock->state = SS_CONNECTED;
757 }
758
759 switch (sk->sk_state) {
760 case TCP_CLOSE:
761 err = -ENOTCONN;
762 /* Hack to wake up other listeners, who can poll for
763 POLLHUP, even on eg. unconnected UDP sockets -- RR */
764 default:
765 sk->sk_shutdown |= how;
766 if (sk->sk_prot->shutdown)
767 sk->sk_prot->shutdown(sk, how);
768 break;
769
770 /* Remaining two branches are temporary solution for missing
771 * close() in multithreaded environment. It is _not_ a good idea,
772 * but we have no choice until close() is repaired at VFS level.
773 */
774 case TCP_LISTEN:
775 if (!(how & RCV_SHUTDOWN))
776 break;
777 /* Fall through */
778 case TCP_SYN_SENT:
779 err = sk->sk_prot->disconnect(sk, O_NONBLOCK);
780 sock->state = err ? SS_DISCONNECTING : SS_UNCONNECTED;
781 break;
782 }
783
784 /* Wake up anyone sleeping in poll. */
785 sk->sk_state_change(sk);
786 release_sock(sk);
787 return err;
788}
789
790/*
791 * ioctl() calls you can issue on an INET socket. Most of these are
792 * device configuration and stuff and very rarely used. Some ioctls
793 * pass on to the socket itself.
794 *
795 * NOTE: I like the idea of a module for the config stuff. ie ifconfig
796 * loads the devconfigure module does its configuring and unloads it.
797 * There's a good 20K of config code hanging around the kernel.
798 */
799
800int inet_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
801{
802 struct sock *sk = sock->sk;
803 int err = 0;
3b1e0a65 804 struct net *net = sock_net(sk);
1da177e4
LT
805
806 switch (cmd) {
807 case SIOCGSTAMP:
808 err = sock_get_timestamp(sk, (struct timeval __user *)arg);
809 break;
ae40eb1e
ED
810 case SIOCGSTAMPNS:
811 err = sock_get_timestampns(sk, (struct timespec __user *)arg);
812 break;
1da177e4
LT
813 case SIOCADDRT:
814 case SIOCDELRT:
815 case SIOCRTMSG:
e5b13cb1 816 err = ip_rt_ioctl(net, cmd, (void __user *)arg);
1da177e4
LT
817 break;
818 case SIOCDARP:
819 case SIOCGARP:
820 case SIOCSARP:
e5b13cb1 821 err = arp_ioctl(net, cmd, (void __user *)arg);
1da177e4
LT
822 break;
823 case SIOCGIFADDR:
824 case SIOCSIFADDR:
825 case SIOCGIFBRDADDR:
826 case SIOCSIFBRDADDR:
827 case SIOCGIFNETMASK:
828 case SIOCSIFNETMASK:
829 case SIOCGIFDSTADDR:
830 case SIOCSIFDSTADDR:
831 case SIOCSIFPFLAGS:
832 case SIOCGIFPFLAGS:
833 case SIOCSIFFLAGS:
e5b13cb1 834 err = devinet_ioctl(net, cmd, (void __user *)arg);
1da177e4
LT
835 break;
836 default:
b5e5fa5e
CH
837 if (sk->sk_prot->ioctl)
838 err = sk->sk_prot->ioctl(sk, cmd, arg);
839 else
840 err = -ENOIOCTLCMD;
1da177e4
LT
841 break;
842 }
843 return err;
844}
845
90ddc4f0 846const struct proto_ops inet_stream_ops = {
543d9cfe
ACM
847 .family = PF_INET,
848 .owner = THIS_MODULE,
849 .release = inet_release,
850 .bind = inet_bind,
851 .connect = inet_stream_connect,
852 .socketpair = sock_no_socketpair,
853 .accept = inet_accept,
854 .getname = inet_getname,
855 .poll = tcp_poll,
856 .ioctl = inet_ioctl,
857 .listen = inet_listen,
858 .shutdown = inet_shutdown,
859 .setsockopt = sock_common_setsockopt,
860 .getsockopt = sock_common_getsockopt,
3516ffb0 861 .sendmsg = tcp_sendmsg,
543d9cfe
ACM
862 .recvmsg = sock_common_recvmsg,
863 .mmap = sock_no_mmap,
864 .sendpage = tcp_sendpage,
9c55e01c 865 .splice_read = tcp_splice_read,
3fdadf7d 866#ifdef CONFIG_COMPAT
543d9cfe
ACM
867 .compat_setsockopt = compat_sock_common_setsockopt,
868 .compat_getsockopt = compat_sock_common_getsockopt,
3fdadf7d 869#endif
1da177e4
LT
870};
871
90ddc4f0 872const struct proto_ops inet_dgram_ops = {
543d9cfe
ACM
873 .family = PF_INET,
874 .owner = THIS_MODULE,
875 .release = inet_release,
876 .bind = inet_bind,
877 .connect = inet_dgram_connect,
878 .socketpair = sock_no_socketpair,
879 .accept = sock_no_accept,
880 .getname = inet_getname,
881 .poll = udp_poll,
882 .ioctl = inet_ioctl,
883 .listen = sock_no_listen,
884 .shutdown = inet_shutdown,
885 .setsockopt = sock_common_setsockopt,
886 .getsockopt = sock_common_getsockopt,
887 .sendmsg = inet_sendmsg,
888 .recvmsg = sock_common_recvmsg,
889 .mmap = sock_no_mmap,
890 .sendpage = inet_sendpage,
3fdadf7d 891#ifdef CONFIG_COMPAT
543d9cfe
ACM
892 .compat_setsockopt = compat_sock_common_setsockopt,
893 .compat_getsockopt = compat_sock_common_getsockopt,
3fdadf7d 894#endif
1da177e4
LT
895};
896
897/*
898 * For SOCK_RAW sockets; should be the same as inet_dgram_ops but without
899 * udp_poll
900 */
90ddc4f0 901static const struct proto_ops inet_sockraw_ops = {
543d9cfe
ACM
902 .family = PF_INET,
903 .owner = THIS_MODULE,
904 .release = inet_release,
905 .bind = inet_bind,
906 .connect = inet_dgram_connect,
907 .socketpair = sock_no_socketpair,
908 .accept = sock_no_accept,
909 .getname = inet_getname,
910 .poll = datagram_poll,
911 .ioctl = inet_ioctl,
912 .listen = sock_no_listen,
913 .shutdown = inet_shutdown,
914 .setsockopt = sock_common_setsockopt,
915 .getsockopt = sock_common_getsockopt,
916 .sendmsg = inet_sendmsg,
917 .recvmsg = sock_common_recvmsg,
918 .mmap = sock_no_mmap,
919 .sendpage = inet_sendpage,
3fdadf7d 920#ifdef CONFIG_COMPAT
543d9cfe
ACM
921 .compat_setsockopt = compat_sock_common_setsockopt,
922 .compat_getsockopt = compat_sock_common_getsockopt,
3fdadf7d 923#endif
1da177e4
LT
924};
925
926static struct net_proto_family inet_family_ops = {
927 .family = PF_INET,
928 .create = inet_create,
929 .owner = THIS_MODULE,
930};
931
1da177e4
LT
932/* Upon startup we insert all the elements in inetsw_array[] into
933 * the linked list inetsw.
934 */
935static struct inet_protosw inetsw_array[] =
936{
e905a9ed
YH
937 {
938 .type = SOCK_STREAM,
939 .protocol = IPPROTO_TCP,
940 .prot = &tcp_prot,
941 .ops = &inet_stream_ops,
942 .capability = -1,
943 .no_check = 0,
944 .flags = INET_PROTOSW_PERMANENT |
d83d8461 945 INET_PROTOSW_ICSK,
e905a9ed
YH
946 },
947
948 {
949 .type = SOCK_DGRAM,
950 .protocol = IPPROTO_UDP,
951 .prot = &udp_prot,
952 .ops = &inet_dgram_ops,
953 .capability = -1,
954 .no_check = UDP_CSUM_DEFAULT,
955 .flags = INET_PROTOSW_PERMANENT,
1da177e4 956 },
e905a9ed 957
1da177e4
LT
958
959 {
e905a9ed
YH
960 .type = SOCK_RAW,
961 .protocol = IPPROTO_IP, /* wild card */
962 .prot = &raw_prot,
963 .ops = &inet_sockraw_ops,
964 .capability = CAP_NET_RAW,
965 .no_check = UDP_CSUM_DEFAULT,
966 .flags = INET_PROTOSW_REUSE,
1da177e4
LT
967 }
968};
969
c40f6fff 970#define INETSW_ARRAY_LEN ARRAY_SIZE(inetsw_array)
1da177e4
LT
971
972void inet_register_protosw(struct inet_protosw *p)
973{
974 struct list_head *lh;
975 struct inet_protosw *answer;
976 int protocol = p->protocol;
977 struct list_head *last_perm;
978
979 spin_lock_bh(&inetsw_lock);
980
981 if (p->type >= SOCK_MAX)
982 goto out_illegal;
983
984 /* If we are trying to override a permanent protocol, bail. */
985 answer = NULL;
986 last_perm = &inetsw[p->type];
987 list_for_each(lh, &inetsw[p->type]) {
988 answer = list_entry(lh, struct inet_protosw, list);
989
990 /* Check only the non-wild match. */
991 if (INET_PROTOSW_PERMANENT & answer->flags) {
992 if (protocol == answer->protocol)
993 break;
994 last_perm = lh;
995 }
996
997 answer = NULL;
998 }
999 if (answer)
1000 goto out_permanent;
1001
1002 /* Add the new entry after the last permanent entry if any, so that
1003 * the new entry does not override a permanent entry when matched with
1004 * a wild-card protocol. But it is allowed to override any existing
e905a9ed 1005 * non-permanent entry. This means that when we remove this entry, the
1da177e4
LT
1006 * system automatically returns to the old behavior.
1007 */
1008 list_add_rcu(&p->list, last_perm);
1009out:
1010 spin_unlock_bh(&inetsw_lock);
1011
1012 synchronize_net();
1013
1014 return;
1015
1016out_permanent:
1017 printk(KERN_ERR "Attempt to override permanent protocol %d.\n",
1018 protocol);
1019 goto out;
1020
1021out_illegal:
1022 printk(KERN_ERR
1023 "Ignoring attempt to register invalid socket type %d.\n",
1024 p->type);
1025 goto out;
1026}
1027
1028void inet_unregister_protosw(struct inet_protosw *p)
1029{
1030 if (INET_PROTOSW_PERMANENT & p->flags) {
1031 printk(KERN_ERR
1032 "Attempt to unregister permanent protocol %d.\n",
1033 p->protocol);
1034 } else {
1035 spin_lock_bh(&inetsw_lock);
1036 list_del_rcu(&p->list);
1037 spin_unlock_bh(&inetsw_lock);
1038
1039 synchronize_net();
1040 }
1041}
1042
32519f11
ACM
1043/*
1044 * Shall we try to damage output packets if routing dev changes?
1045 */
1046
ab32ea5d 1047int sysctl_ip_dynaddr __read_mostly;
32519f11
ACM
1048
1049static int inet_sk_reselect_saddr(struct sock *sk)
1050{
1051 struct inet_sock *inet = inet_sk(sk);
1052 int err;
1053 struct rtable *rt;
714e85be
AV
1054 __be32 old_saddr = inet->saddr;
1055 __be32 new_saddr;
bada8adc 1056 __be32 daddr = inet->daddr;
32519f11
ACM
1057
1058 if (inet->opt && inet->opt->srr)
1059 daddr = inet->opt->faddr;
1060
1061 /* Query new route. */
1062 err = ip_route_connect(&rt, daddr, 0,
1063 RT_CONN_FLAGS(sk),
1064 sk->sk_bound_dev_if,
1065 sk->sk_protocol,
8eb9086f 1066 inet->sport, inet->dport, sk, 0);
32519f11
ACM
1067 if (err)
1068 return err;
1069
1070 sk_setup_caps(sk, &rt->u.dst);
1071
1072 new_saddr = rt->rt_src;
1073
1074 if (new_saddr == old_saddr)
1075 return 0;
1076
1077 if (sysctl_ip_dynaddr > 1) {
1078 printk(KERN_INFO "%s(): shifting inet->"
a7d632b6 1079 "saddr from " NIPQUAD_FMT " to " NIPQUAD_FMT "\n",
0dc47877 1080 __func__,
32519f11
ACM
1081 NIPQUAD(old_saddr),
1082 NIPQUAD(new_saddr));
1083 }
1084
1085 inet->saddr = inet->rcv_saddr = new_saddr;
1086
1087 /*
1088 * XXX The only one ugly spot where we need to
1089 * XXX really change the sockets identity after
1090 * XXX it has entered the hashes. -DaveM
1091 *
1092 * Besides that, it does not check for connection
1093 * uniqueness. Wait for troubles.
1094 */
1095 __sk_prot_rehash(sk);
1096 return 0;
1097}
1098
1099int inet_sk_rebuild_header(struct sock *sk)
1100{
1101 struct inet_sock *inet = inet_sk(sk);
1102 struct rtable *rt = (struct rtable *)__sk_dst_check(sk, 0);
3ca3c68e 1103 __be32 daddr;
32519f11
ACM
1104 int err;
1105
1106 /* Route is OK, nothing to do. */
1107 if (rt)
1108 return 0;
1109
1110 /* Reroute. */
1111 daddr = inet->daddr;
1112 if (inet->opt && inet->opt->srr)
1113 daddr = inet->opt->faddr;
1114{
1115 struct flowi fl = {
1116 .oif = sk->sk_bound_dev_if,
1117 .nl_u = {
1118 .ip4_u = {
1119 .daddr = daddr,
1120 .saddr = inet->saddr,
1121 .tos = RT_CONN_FLAGS(sk),
1122 },
1123 },
1124 .proto = sk->sk_protocol,
1125 .uli_u = {
1126 .ports = {
1127 .sport = inet->sport,
1128 .dport = inet->dport,
1129 },
1130 },
1131 };
e905a9ed 1132
beb8d13b 1133 security_sk_classify_flow(sk, &fl);
3b1e0a65 1134 err = ip_route_output_flow(sock_net(sk), &rt, &fl, sk, 0);
32519f11
ACM
1135}
1136 if (!err)
1137 sk_setup_caps(sk, &rt->u.dst);
1138 else {
1139 /* Routing failed... */
1140 sk->sk_route_caps = 0;
1141 /*
1142 * Other protocols have to map its equivalent state to TCP_SYN_SENT.
1143 * DCCP maps its DCCP_REQUESTING state to TCP_SYN_SENT. -acme
1144 */
1145 if (!sysctl_ip_dynaddr ||
1146 sk->sk_state != TCP_SYN_SENT ||
1147 (sk->sk_userlocks & SOCK_BINDADDR_LOCK) ||
1148 (err = inet_sk_reselect_saddr(sk)) != 0)
1149 sk->sk_err_soft = -err;
1150 }
1151
1152 return err;
1153}
1154
1155EXPORT_SYMBOL(inet_sk_rebuild_header);
1156
a430a43d
HX
1157static int inet_gso_send_check(struct sk_buff *skb)
1158{
1159 struct iphdr *iph;
1160 struct net_protocol *ops;
1161 int proto;
1162 int ihl;
1163 int err = -EINVAL;
1164
1165 if (unlikely(!pskb_may_pull(skb, sizeof(*iph))))
1166 goto out;
1167
eddc9ec5 1168 iph = ip_hdr(skb);
a430a43d
HX
1169 ihl = iph->ihl * 4;
1170 if (ihl < sizeof(*iph))
1171 goto out;
1172
1173 if (unlikely(!pskb_may_pull(skb, ihl)))
1174 goto out;
1175
badff6d0
ACM
1176 __skb_pull(skb, ihl);
1177 skb_reset_transport_header(skb);
eddc9ec5 1178 iph = ip_hdr(skb);
a430a43d
HX
1179 proto = iph->protocol & (MAX_INET_PROTOS - 1);
1180 err = -EPROTONOSUPPORT;
1181
1182 rcu_read_lock();
1183 ops = rcu_dereference(inet_protos[proto]);
1184 if (likely(ops && ops->gso_send_check))
1185 err = ops->gso_send_check(skb);
1186 rcu_read_unlock();
1187
1188out:
1189 return err;
1190}
1191
576a30eb 1192static struct sk_buff *inet_gso_segment(struct sk_buff *skb, int features)
f4c50d99
HX
1193{
1194 struct sk_buff *segs = ERR_PTR(-EINVAL);
1195 struct iphdr *iph;
1196 struct net_protocol *ops;
1197 int proto;
1198 int ihl;
1199 int id;
1200
d212f87b
SH
1201 if (!(features & NETIF_F_V4_CSUM))
1202 features &= ~NETIF_F_SG;
1203
bbcf467d
HX
1204 if (unlikely(skb_shinfo(skb)->gso_type &
1205 ~(SKB_GSO_TCPV4 |
1206 SKB_GSO_UDP |
1207 SKB_GSO_DODGY |
1208 SKB_GSO_TCP_ECN |
1209 0)))
1210 goto out;
1211
1212 if (unlikely(!pskb_may_pull(skb, sizeof(*iph))))
f4c50d99
HX
1213 goto out;
1214
eddc9ec5 1215 iph = ip_hdr(skb);
f4c50d99
HX
1216 ihl = iph->ihl * 4;
1217 if (ihl < sizeof(*iph))
1218 goto out;
1219
bbcf467d 1220 if (unlikely(!pskb_may_pull(skb, ihl)))
f4c50d99
HX
1221 goto out;
1222
badff6d0
ACM
1223 __skb_pull(skb, ihl);
1224 skb_reset_transport_header(skb);
eddc9ec5 1225 iph = ip_hdr(skb);
f4c50d99
HX
1226 id = ntohs(iph->id);
1227 proto = iph->protocol & (MAX_INET_PROTOS - 1);
1228 segs = ERR_PTR(-EPROTONOSUPPORT);
1229
1230 rcu_read_lock();
1231 ops = rcu_dereference(inet_protos[proto]);
bbcf467d 1232 if (likely(ops && ops->gso_segment))
576a30eb 1233 segs = ops->gso_segment(skb, features);
f4c50d99
HX
1234 rcu_read_unlock();
1235
801678c5 1236 if (!segs || IS_ERR(segs))
f4c50d99
HX
1237 goto out;
1238
1239 skb = segs;
1240 do {
eddc9ec5 1241 iph = ip_hdr(skb);
f4c50d99
HX
1242 iph->id = htons(id++);
1243 iph->tot_len = htons(skb->len - skb->mac_len);
1244 iph->check = 0;
d56f90a7 1245 iph->check = ip_fast_csum(skb_network_header(skb), iph->ihl);
f4c50d99
HX
1246 } while ((skb = skb->next));
1247
1248out:
1249 return segs;
1250}
1251
eee4fe4d 1252int inet_ctl_sock_create(struct sock **sk, unsigned short family,
5677242f
DL
1253 unsigned short type, unsigned char protocol,
1254 struct net *net)
3d58b5fa 1255{
eee4fe4d
DL
1256 struct socket *sock;
1257 int rc = sock_create_kern(family, type, protocol, &sock);
3d58b5fa
DL
1258
1259 if (rc == 0) {
eee4fe4d
DL
1260 *sk = sock->sk;
1261 (*sk)->sk_allocation = GFP_ATOMIC;
3d58b5fa
DL
1262 /*
1263 * Unhash it so that IP input processing does not even see it,
1264 * we do not wish this socket to see incoming packets.
1265 */
eee4fe4d 1266 (*sk)->sk_prot->unhash(*sk);
5677242f
DL
1267
1268 sk_change_net(*sk, net);
3d58b5fa
DL
1269 }
1270 return rc;
1271}
1272
1273EXPORT_SYMBOL_GPL(inet_ctl_sock_create);
1274
5e0f0435
HX
1275unsigned long snmp_fold_field(void *mib[], int offt)
1276{
1277 unsigned long res = 0;
1278 int i;
1279
1280 for_each_possible_cpu(i) {
1281 res += *(((unsigned long *) per_cpu_ptr(mib[0], i)) + offt);
1282 res += *(((unsigned long *) per_cpu_ptr(mib[1], i)) + offt);
1283 }
1284 return res;
1285}
1286EXPORT_SYMBOL_GPL(snmp_fold_field);
1287
c69bce20 1288int snmp_mib_init(void *ptr[2], size_t mibsize)
5e0f0435
HX
1289{
1290 BUG_ON(ptr == NULL);
1291 ptr[0] = __alloc_percpu(mibsize);
1292 if (!ptr[0])
1293 goto err0;
1294 ptr[1] = __alloc_percpu(mibsize);
1295 if (!ptr[1])
1296 goto err1;
1297 return 0;
1298err1:
1299 free_percpu(ptr[0]);
1300 ptr[0] = NULL;
1301err0:
1302 return -ENOMEM;
1303}
1304EXPORT_SYMBOL_GPL(snmp_mib_init);
1305
1306void snmp_mib_free(void *ptr[2])
1307{
1308 BUG_ON(ptr == NULL);
1309 free_percpu(ptr[0]);
1310 free_percpu(ptr[1]);
1311 ptr[0] = ptr[1] = NULL;
1312}
1313EXPORT_SYMBOL_GPL(snmp_mib_free);
1314
1da177e4
LT
1315#ifdef CONFIG_IP_MULTICAST
1316static struct net_protocol igmp_protocol = {
1317 .handler = igmp_rcv,
1318};
1319#endif
1320
1321static struct net_protocol tcp_protocol = {
1322 .handler = tcp_v4_rcv,
1323 .err_handler = tcp_v4_err,
a430a43d 1324 .gso_send_check = tcp_v4_gso_send_check,
f4c50d99 1325 .gso_segment = tcp_tso_segment,
1da177e4 1326 .no_policy = 1,
92f1fecb 1327 .netns_ok = 1,
1da177e4
LT
1328};
1329
1330static struct net_protocol udp_protocol = {
1331 .handler = udp_rcv,
1332 .err_handler = udp_err,
1333 .no_policy = 1,
92f1fecb 1334 .netns_ok = 1,
1da177e4
LT
1335};
1336
1337static struct net_protocol icmp_protocol = {
1338 .handler = icmp_rcv,
8b7817f3 1339 .no_policy = 1,
92f1fecb 1340 .netns_ok = 1,
1da177e4
LT
1341};
1342
9b4661bd
PE
1343static __net_init int ipv4_mib_init_net(struct net *net)
1344{
1345 return 0;
1346}
1347
1348static __net_exit void ipv4_mib_exit_net(struct net *net)
1349{
1350}
1351
1352static __net_initdata struct pernet_operations ipv4_mib_ops = {
1353 .init = ipv4_mib_init_net,
1354 .exit = ipv4_mib_exit_net,
1355};
1356
1da177e4
LT
1357static int __init init_ipv4_mibs(void)
1358{
33490170 1359 if (snmp_mib_init((void **)net_statistics,
c69bce20 1360 sizeof(struct linux_mib)) < 0)
33490170
YH
1361 goto err_net_mib;
1362 if (snmp_mib_init((void **)ip_statistics,
c69bce20 1363 sizeof(struct ipstats_mib)) < 0)
33490170
YH
1364 goto err_ip_mib;
1365 if (snmp_mib_init((void **)icmp_statistics,
c69bce20 1366 sizeof(struct icmp_mib)) < 0)
33490170 1367 goto err_icmp_mib;
96793b48 1368 if (snmp_mib_init((void **)icmpmsg_statistics,
c69bce20 1369 sizeof(struct icmpmsg_mib)) < 0)
96793b48 1370 goto err_icmpmsg_mib;
33490170 1371 if (snmp_mib_init((void **)tcp_statistics,
c69bce20 1372 sizeof(struct tcp_mib)) < 0)
33490170
YH
1373 goto err_tcp_mib;
1374 if (snmp_mib_init((void **)udp_statistics,
c69bce20 1375 sizeof(struct udp_mib)) < 0)
33490170
YH
1376 goto err_udp_mib;
1377 if (snmp_mib_init((void **)udplite_statistics,
c69bce20 1378 sizeof(struct udp_mib)) < 0)
33490170 1379 goto err_udplite_mib;
db8dac20 1380
a9c19329 1381 tcp_mib_init(&init_net);
1da177e4 1382
9b4661bd
PE
1383 if (register_pernet_subsys(&ipv4_mib_ops))
1384 goto err_net;
1385
1da177e4 1386 return 0;
33490170 1387
9b4661bd
PE
1388err_net:
1389 snmp_mib_free((void **)udplite_statistics);
33490170
YH
1390err_udplite_mib:
1391 snmp_mib_free((void **)udp_statistics);
1392err_udp_mib:
1393 snmp_mib_free((void **)tcp_statistics);
1394err_tcp_mib:
96793b48
DS
1395 snmp_mib_free((void **)icmpmsg_statistics);
1396err_icmpmsg_mib:
33490170
YH
1397 snmp_mib_free((void **)icmp_statistics);
1398err_icmp_mib:
1399 snmp_mib_free((void **)ip_statistics);
1400err_ip_mib:
1401 snmp_mib_free((void **)net_statistics);
1402err_net_mib:
1403 return -ENOMEM;
1da177e4
LT
1404}
1405
1406static int ipv4_proc_init(void);
1da177e4 1407
30e224d7
HX
1408/*
1409 * IP protocol layer initialiser
1410 */
1411
1412static struct packet_type ip_packet_type = {
1413 .type = __constant_htons(ETH_P_IP),
1414 .func = ip_rcv,
a430a43d 1415 .gso_send_check = inet_gso_send_check,
f4c50d99 1416 .gso_segment = inet_gso_segment,
30e224d7
HX
1417};
1418
1da177e4
LT
1419static int __init inet_init(void)
1420{
1421 struct sk_buff *dummy_skb;
1422 struct inet_protosw *q;
1423 struct list_head *r;
1424 int rc = -EINVAL;
1425
ef047f5e 1426 BUILD_BUG_ON(sizeof(struct inet_skb_parm) > sizeof(dummy_skb->cb));
1da177e4
LT
1427
1428 rc = proto_register(&tcp_prot, 1);
1429 if (rc)
1430 goto out;
1431
1432 rc = proto_register(&udp_prot, 1);
1433 if (rc)
1434 goto out_unregister_tcp_proto;
1435
1436 rc = proto_register(&raw_prot, 1);
1437 if (rc)
1438 goto out_unregister_udp_proto;
1439
1440 /*
e905a9ed 1441 * Tell SOCKET that we are alive...
1da177e4
LT
1442 */
1443
e905a9ed 1444 (void)sock_register(&inet_family_ops);
1da177e4
LT
1445
1446 /*
1447 * Add all the base protocols.
1448 */
1449
1450 if (inet_add_protocol(&icmp_protocol, IPPROTO_ICMP) < 0)
1451 printk(KERN_CRIT "inet_init: Cannot add ICMP protocol\n");
1452 if (inet_add_protocol(&udp_protocol, IPPROTO_UDP) < 0)
1453 printk(KERN_CRIT "inet_init: Cannot add UDP protocol\n");
1454 if (inet_add_protocol(&tcp_protocol, IPPROTO_TCP) < 0)
1455 printk(KERN_CRIT "inet_init: Cannot add TCP protocol\n");
1456#ifdef CONFIG_IP_MULTICAST
1457 if (inet_add_protocol(&igmp_protocol, IPPROTO_IGMP) < 0)
1458 printk(KERN_CRIT "inet_init: Cannot add IGMP protocol\n");
1459#endif
1460
1461 /* Register the socket-side information for inet_create. */
1462 for (r = &inetsw[0]; r < &inetsw[SOCK_MAX]; ++r)
1463 INIT_LIST_HEAD(r);
1464
1465 for (q = inetsw_array; q < &inetsw_array[INETSW_ARRAY_LEN]; ++q)
1466 inet_register_protosw(q);
1467
1468 /*
1469 * Set the ARP module up
1470 */
1471
1472 arp_init();
1473
e905a9ed
YH
1474 /*
1475 * Set the IP module up
1476 */
1da177e4
LT
1477
1478 ip_init();
1479
9b0f976f 1480 tcp_v4_init();
1da177e4
LT
1481
1482 /* Setup TCP slab cache for open requests. */
1483 tcp_init();
1484
95766fff
HA
1485 /* Setup UDP memory threshold */
1486 udp_init();
1487
ba4e58ec
GR
1488 /* Add UDP-Lite (RFC 3828) */
1489 udplite4_register();
1da177e4
LT
1490
1491 /*
1492 * Set the ICMP layer up
1493 */
1494
a5710d65
DL
1495 if (icmp_init() < 0)
1496 panic("Failed to create the ICMP control socket.\n");
1da177e4
LT
1497
1498 /*
1499 * Initialise the multicast router
1500 */
1501#if defined(CONFIG_IP_MROUTE)
03d2f897
WC
1502 if (ip_mr_init())
1503 printk(KERN_CRIT "inet_init: Cannot init ipv4 mroute\n");
1da177e4
LT
1504#endif
1505 /*
1506 * Initialise per-cpu ipv4 mibs
e905a9ed 1507 */
1da177e4 1508
132adf54 1509 if (init_ipv4_mibs())
03d2f897 1510 printk(KERN_CRIT "inet_init: Cannot init ipv4 mibs\n");
e905a9ed 1511
1da177e4
LT
1512 ipv4_proc_init();
1513
1514 ipfrag_init();
1515
30e224d7
HX
1516 dev_add_pack(&ip_packet_type);
1517
1da177e4
LT
1518 rc = 0;
1519out:
1520 return rc;
1da177e4
LT
1521out_unregister_udp_proto:
1522 proto_unregister(&udp_prot);
321efff7
OK
1523out_unregister_tcp_proto:
1524 proto_unregister(&tcp_prot);
1da177e4
LT
1525 goto out;
1526}
1527
a536e077 1528fs_initcall(inet_init);
1da177e4
LT
1529
1530/* ------------------------------------------------------------------------ */
1531
1532#ifdef CONFIG_PROC_FS
1da177e4
LT
1533static int __init ipv4_proc_init(void)
1534{
1535 int rc = 0;
1536
1537 if (raw_proc_init())
1538 goto out_raw;
1539 if (tcp4_proc_init())
1540 goto out_tcp;
1541 if (udp4_proc_init())
1542 goto out_udp;
1da177e4
LT
1543 if (ip_misc_proc_init())
1544 goto out_misc;
1545out:
1546 return rc;
1547out_misc:
1da177e4
LT
1548 udp4_proc_exit();
1549out_udp:
1550 tcp4_proc_exit();
1551out_tcp:
1552 raw_proc_exit();
1553out_raw:
1554 rc = -ENOMEM;
1555 goto out;
1556}
1557
1558#else /* CONFIG_PROC_FS */
1559static int __init ipv4_proc_init(void)
1560{
1561 return 0;
1562}
1563#endif /* CONFIG_PROC_FS */
1564
1565MODULE_ALIAS_NETPROTO(PF_INET);
1566
1567EXPORT_SYMBOL(inet_accept);
1568EXPORT_SYMBOL(inet_bind);
1569EXPORT_SYMBOL(inet_dgram_connect);
1570EXPORT_SYMBOL(inet_dgram_ops);
1571EXPORT_SYMBOL(inet_getname);
1572EXPORT_SYMBOL(inet_ioctl);
1573EXPORT_SYMBOL(inet_listen);
1574EXPORT_SYMBOL(inet_register_protosw);
1575EXPORT_SYMBOL(inet_release);
1576EXPORT_SYMBOL(inet_sendmsg);
1577EXPORT_SYMBOL(inet_shutdown);
1578EXPORT_SYMBOL(inet_sock_destruct);
1579EXPORT_SYMBOL(inet_stream_connect);
1580EXPORT_SYMBOL(inet_stream_ops);
1581EXPORT_SYMBOL(inet_unregister_protosw);
1582EXPORT_SYMBOL(net_statistics);
cdac4e07 1583EXPORT_SYMBOL(sysctl_ip_nonlocal_bind);