blob: 73a38459f63a2445a3253d0f05151448355cc726 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 *
Jesper Juhl02c30a82005-05-05 16:16:16 -07008 * Authors: Ross Biro
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * 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
Joe Perchesafd465032012-03-12 07:03:32 +000068#define pr_fmt(fmt) "IPv4: " fmt
69
Herbert Xuf4c50d92006-06-22 03:02:40 -070070#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#include <linux/errno.h>
72#include <linux/types.h>
73#include <linux/socket.h>
74#include <linux/in.h>
75#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#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>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080082#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#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>
David S. Millerb3da2cf2007-03-23 11:40:27 -070090#include <linux/random.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090091#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Linus Torvalds1da177e2005-04-16 15:20:36 -070095#include <linux/inet.h>
96#include <linux/igmp.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020097#include <linux/inetdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070098#include <linux/netdevice.h>
Herbert Xu73cc19f2008-12-15 23:41:09 -080099#include <net/checksum.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100#include <net/ip.h>
101#include <net/protocol.h>
102#include <net/arp.h>
103#include <net/route.h>
104#include <net/ip_fib.h>
Arnaldo Carvalho de Melo295f7322005-08-09 20:11:56 -0700105#include <net/inet_connection_sock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106#include <net/tcp.h>
107#include <net/udp.h>
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800108#include <net/udplite.h>
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000109#include <net/ping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110#include <linux/skbuff.h>
111#include <net/sock.h>
112#include <net/raw.h>
113#include <net/icmp.h>
114#include <net/ipip.h>
115#include <net/inet_common.h>
116#include <net/xfrm.h>
Pavel Emelyanov9b4661b2008-07-18 04:01:44 -0700117#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118#ifdef CONFIG_IP_MROUTE
119#include <linux/mroute.h>
120#endif
121
Robert Love20386922008-10-15 15:35:44 -0400122#ifdef CONFIG_ANDROID_PARANOID_NETWORK
123#include <linux/android_aid.h>
124#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126/* The inetsw table contains everything that inet_create needs to
127 * build a new socket.
128 */
129static struct list_head inetsw[SOCK_MAX];
130static DEFINE_SPINLOCK(inetsw_lock);
131
Pavel Emelyanov9ba63972007-12-05 01:38:23 -0800132struct ipv4_config ipv4_config;
Pavel Emelyanov9ba63972007-12-05 01:38:23 -0800133EXPORT_SYMBOL(ipv4_config);
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135/* New destruction routine */
136
137void inet_sock_destruct(struct sock *sk)
138{
139 struct inet_sock *inet = inet_sk(sk);
140
141 __skb_queue_purge(&sk->sk_receive_queue);
142 __skb_queue_purge(&sk->sk_error_queue);
143
Hideo Aoki95766ff2007-12-31 00:29:24 -0800144 sk_mem_reclaim(sk);
145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 if (sk->sk_type == SOCK_STREAM && sk->sk_state != TCP_CLOSE) {
Eric Dumazet3d1427f2009-08-28 23:45:21 -0700147 pr_err("Attempt to release TCP socket in state %d %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 sk->sk_state, sk);
149 return;
150 }
151 if (!sock_flag(sk, SOCK_DEAD)) {
Eric Dumazet3d1427f2009-08-28 23:45:21 -0700152 pr_err("Attempt to release alive inet socket %p\n", sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 return;
154 }
155
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700156 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
157 WARN_ON(atomic_read(&sk->sk_wmem_alloc));
158 WARN_ON(sk->sk_wmem_queued);
159 WARN_ON(sk->sk_forward_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000161 kfree(rcu_dereference_protected(inet->inet_opt, 1));
Eric Dumazetb6c67122010-04-08 23:03:29 +0000162 dst_release(rcu_dereference_check(sk->sk_dst_cache, 1));
Arnaldo Carvalho de Meloe6848972005-08-09 19:45:38 -0700163 sk_refcnt_debug_dec(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164}
Eric Dumazet3d1427f2009-08-28 23:45:21 -0700165EXPORT_SYMBOL(inet_sock_destruct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167/*
168 * The routines beyond this point handle the behaviour of an AF_INET
169 * socket object. Mostly it punts to the subprotocols of IP to do
170 * the work.
171 */
172
173/*
174 * Automatically bind an unbound socket.
175 */
176
177static int inet_autobind(struct sock *sk)
178{
179 struct inet_sock *inet;
180 /* We may need to bind the socket. */
181 lock_sock(sk);
182 inet = inet_sk(sk);
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000183 if (!inet->inet_num) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 if (sk->sk_prot->get_port(sk, 0)) {
185 release_sock(sk);
186 return -EAGAIN;
187 }
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000188 inet->inet_sport = htons(inet->inet_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 }
190 release_sock(sk);
191 return 0;
192}
193
194/*
195 * Move a socket into listening state.
196 */
197int inet_listen(struct socket *sock, int backlog)
198{
199 struct sock *sk = sock->sk;
200 unsigned char old_state;
201 int err;
202
203 lock_sock(sk);
204
205 err = -EINVAL;
206 if (sock->state != SS_UNCONNECTED || sock->type != SOCK_STREAM)
207 goto out;
208
209 old_state = sk->sk_state;
210 if (!((1 << old_state) & (TCPF_CLOSE | TCPF_LISTEN)))
211 goto out;
212
213 /* Really, if the socket is already in listen state
214 * we can only allow the backlog to be adjusted.
215 */
216 if (old_state != TCP_LISTEN) {
Eric Dumazet72a3eff2006-11-16 02:30:37 -0800217 err = inet_csk_listen_start(sk, backlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 if (err)
219 goto out;
220 }
221 sk->sk_max_ack_backlog = backlog;
222 err = 0;
223
224out:
225 release_sock(sk);
226 return err;
227}
Eric Dumazet3d1427f2009-08-28 23:45:21 -0700228EXPORT_SYMBOL(inet_listen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Eric Dumazetbe776282007-03-27 13:53:04 -0700230u32 inet_ehash_secret __read_mostly;
David S. Millerb3da2cf2007-03-23 11:40:27 -0700231EXPORT_SYMBOL(inet_ehash_secret);
232
Eric Dumazetbe776282007-03-27 13:53:04 -0700233/*
234 * inet_ehash_secret must be set exactly once
Eric Dumazetbe776282007-03-27 13:53:04 -0700235 */
David S. Millerb3da2cf2007-03-23 11:40:27 -0700236void build_ehash_secret(void)
237{
Eric Dumazetbe776282007-03-27 13:53:04 -0700238 u32 rnd;
Eric Dumazet49e8ab02010-08-19 06:10:45 +0000239
Eric Dumazetbe776282007-03-27 13:53:04 -0700240 do {
241 get_random_bytes(&rnd, sizeof(rnd));
242 } while (rnd == 0);
Eric Dumazet49e8ab02010-08-19 06:10:45 +0000243
244 cmpxchg(&inet_ehash_secret, 0, rnd);
David S. Millerb3da2cf2007-03-23 11:40:27 -0700245}
246EXPORT_SYMBOL(build_ehash_secret);
247
Denis V. Lunev2342fd72008-03-24 15:33:42 -0700248static inline int inet_netns_ok(struct net *net, int protocol)
249{
250 int hash;
Alexey Dobriyan32613092009-09-14 12:21:47 +0000251 const struct net_protocol *ipprot;
Denis V. Lunev2342fd72008-03-24 15:33:42 -0700252
Eric Dumazet04f258c2008-11-23 15:42:23 -0800253 if (net_eq(net, &init_net))
Denis V. Lunev2342fd72008-03-24 15:33:42 -0700254 return 1;
255
256 hash = protocol & (MAX_INET_PROTOS - 1);
257 ipprot = rcu_dereference(inet_protos[hash]);
258
259 if (ipprot == NULL)
260 /* raw IP is OK */
261 return 1;
262 return ipprot->netns_ok;
263}
264
Robert Love20386922008-10-15 15:35:44 -0400265#ifdef CONFIG_ANDROID_PARANOID_NETWORK
266static inline int current_has_network(void)
267{
268 return (!current_euid() || in_egroup_p(AID_INET) ||
269 in_egroup_p(AID_NET_RAW));
270}
271static inline int current_has_cap(int cap)
272{
273 if (cap == CAP_NET_RAW && in_egroup_p(AID_NET_RAW))
274 return 1;
275 return capable(cap);
276}
277# else
278static inline int current_has_network(void)
279{
280 return 1;
281}
282static inline int current_has_cap(int cap)
283{
284 return capable(cap);
285}
286#endif
287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288/*
289 * Create an inet socket.
290 */
291
Eric Paris3f378b62009-11-05 22:18:14 -0800292static int inet_create(struct net *net, struct socket *sock, int protocol,
293 int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
295 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 struct inet_protosw *answer;
297 struct inet_sock *inet;
298 struct proto *answer_prot;
299 unsigned char answer_flags;
300 char answer_no_check;
Arnaldo Carvalho de Melobb97d312005-08-09 20:19:14 -0700301 int try_loading_module = 0;
Herbert Xu86c8f9d2005-12-02 20:43:26 -0800302 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Robert Love20386922008-10-15 15:35:44 -0400304 if (!current_has_network())
305 return -EACCES;
306
Eric Dumazet04f258c2008-11-23 15:42:23 -0800307 if (unlikely(!inet_ehash_secret))
308 if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
309 build_ehash_secret();
David S. Millerb3da2cf2007-03-23 11:40:27 -0700310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 sock->state = SS_UNCONNECTED;
312
313 /* Look for the requested type/protocol pair. */
Arnaldo Carvalho de Melobb97d312005-08-09 20:19:14 -0700314lookup_protocol:
Herbert Xu86c8f9d2005-12-02 20:43:26 -0800315 err = -ESOCKTNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 rcu_read_lock();
Paul E. McKenney696adfe2008-07-25 01:45:34 -0700317 list_for_each_entry_rcu(answer, &inetsw[sock->type], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Paul E. McKenney696adfe2008-07-25 01:45:34 -0700319 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 /* Check the non-wild match. */
321 if (protocol == answer->protocol) {
322 if (protocol != IPPROTO_IP)
323 break;
324 } else {
325 /* Check for the two wild cases. */
326 if (IPPROTO_IP == protocol) {
327 protocol = answer->protocol;
328 break;
329 }
330 if (IPPROTO_IP == answer->protocol)
331 break;
332 }
Herbert Xu86c8f9d2005-12-02 20:43:26 -0800333 err = -EPROTONOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 }
335
Paul E. McKenney696adfe2008-07-25 01:45:34 -0700336 if (unlikely(err)) {
Arnaldo Carvalho de Melobb97d312005-08-09 20:19:14 -0700337 if (try_loading_module < 2) {
338 rcu_read_unlock();
339 /*
340 * Be more specific, e.g. net-pf-2-proto-132-type-1
341 * (net-pf-PF_INET-proto-IPPROTO_SCTP-type-SOCK_STREAM)
342 */
343 if (++try_loading_module == 1)
344 request_module("net-pf-%d-proto-%d-type-%d",
345 PF_INET, protocol, sock->type);
346 /*
347 * Fall back to generic, e.g. net-pf-2-proto-132
348 * (net-pf-PF_INET-proto-IPPROTO_SCTP)
349 */
350 else
351 request_module("net-pf-%d-proto-%d",
352 PF_INET, protocol);
353 goto lookup_protocol;
354 } else
355 goto out_rcu_unlock;
356 }
357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 err = -EPERM;
Robert Love20386922008-10-15 15:35:44 -0400359 if (sock->type == SOCK_RAW && !kern && !current_has_cap(CAP_NET_RAW))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 goto out_rcu_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Denis V. Lunev2342fd72008-03-24 15:33:42 -0700362 err = -EAFNOSUPPORT;
363 if (!inet_netns_ok(net, protocol))
364 goto out_rcu_unlock;
365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 sock->ops = answer->ops;
367 answer_prot = answer->prot;
368 answer_no_check = answer->no_check;
369 answer_flags = answer->flags;
370 rcu_read_unlock();
371
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700372 WARN_ON(answer_prot->slab == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 err = -ENOBUFS;
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700375 sk = sk_alloc(net, PF_INET, GFP_KERNEL, answer_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 if (sk == NULL)
377 goto out;
378
379 err = 0;
380 sk->sk_no_check = answer_no_check;
381 if (INET_PROTOSW_REUSE & answer_flags)
382 sk->sk_reuse = 1;
383
384 inet = inet_sk(sk);
Paul Moore469de9b2007-01-09 14:37:06 -0800385 inet->is_icsk = (INET_PROTOSW_ICSK & answer_flags) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Jiri Olsa7b2ff182010-06-15 01:07:31 +0000387 inet->nodefrag = 0;
388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 if (SOCK_RAW == sock->type) {
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000390 inet->inet_num = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 if (IPPROTO_RAW == protocol)
392 inet->hdrincl = 1;
393 }
394
395 if (ipv4_config.no_pmtu_disc)
396 inet->pmtudisc = IP_PMTUDISC_DONT;
397 else
398 inet->pmtudisc = IP_PMTUDISC_WANT;
399
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000400 inet->inet_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402 sock_init_data(sock, sk);
403
404 sk->sk_destruct = inet_sock_destruct;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 sk->sk_protocol = protocol;
406 sk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
407
408 inet->uc_ttl = -1;
409 inet->mc_loop = 1;
410 inet->mc_ttl = 1;
Nivedita Singhvif771bef2009-05-28 07:00:46 +0000411 inet->mc_all = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 inet->mc_index = 0;
413 inet->mc_list = NULL;
Jiri Benc4c507d22012-02-09 09:35:49 +0000414 inet->rcv_tos = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Arnaldo Carvalho de Meloe6848972005-08-09 19:45:38 -0700416 sk_refcnt_debug_inc(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000418 if (inet->inet_num) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 /* It assumes that any protocol which allows
420 * the user to assign a number at socket
421 * creation time automatically
422 * shares.
423 */
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000424 inet->inet_sport = htons(inet->inet_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 /* Add to protocol hash chains. */
426 sk->sk_prot->hash(sk);
427 }
428
429 if (sk->sk_prot->init) {
430 err = sk->sk_prot->init(sk);
431 if (err)
432 sk_common_release(sk);
433 }
434out:
435 return err;
436out_rcu_unlock:
437 rcu_read_unlock();
438 goto out;
439}
440
441
442/*
443 * The peer socket should always be NULL (or else). When we call this
444 * function we are destroying the object and from then on nobody
445 * should refer to it.
446 */
447int inet_release(struct socket *sock)
448{
449 struct sock *sk = sock->sk;
450
451 if (sk) {
452 long timeout;
453
David S. Millerc58dc012010-04-27 15:05:31 -0700454 sock_rps_reset_flow(sk);
Tom Herbertfec5e652010-04-16 16:01:27 -0700455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 /* Applications forget to leave groups before exiting */
457 ip_mc_drop_socket(sk);
458
459 /* If linger is set, we don't return until the close
460 * is complete. Otherwise we return immediately. The
461 * actually closing is done the same either way.
462 *
463 * If the close is due to the process exiting, we never
464 * linger..
465 */
466 timeout = 0;
467 if (sock_flag(sk, SOCK_LINGER) &&
468 !(current->flags & PF_EXITING))
469 timeout = sk->sk_lingertime;
470 sock->sk = NULL;
471 sk->sk_prot->close(sk, timeout);
472 }
473 return 0;
474}
Eric Dumazet3d1427f2009-08-28 23:45:21 -0700475EXPORT_SYMBOL(inet_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
477/* It is off by default, see below. */
Brian Haleyab32ea52006-09-22 14:15:41 -0700478int sysctl_ip_nonlocal_bind __read_mostly;
Eric Dumazet3d1427f2009-08-28 23:45:21 -0700479EXPORT_SYMBOL(sysctl_ip_nonlocal_bind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481int inet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
482{
483 struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
484 struct sock *sk = sock->sk;
485 struct inet_sock *inet = inet_sk(sk);
486 unsigned short snum;
487 int chk_addr_ret;
488 int err;
489
490 /* If the socket has its own bind function then use it. (RAW) */
491 if (sk->sk_prot->bind) {
492 err = sk->sk_prot->bind(sk, uaddr, addr_len);
493 goto out;
494 }
495 err = -EINVAL;
496 if (addr_len < sizeof(struct sockaddr_in))
497 goto out;
498
Marcus Meissnerc349a522011-07-04 01:30:29 +0000499 if (addr->sin_family != AF_INET) {
Eric Dumazet29c486d2011-08-30 18:57:00 -0400500 /* Compatibility games : accept AF_UNSPEC (mapped to AF_INET)
501 * only if s_addr is INADDR_ANY.
502 */
Marcus Meissnerc349a522011-07-04 01:30:29 +0000503 err = -EAFNOSUPPORT;
Eric Dumazet29c486d2011-08-30 18:57:00 -0400504 if (addr->sin_family != AF_UNSPEC ||
505 addr->sin_addr.s_addr != htonl(INADDR_ANY))
506 goto out;
Marcus Meissnerc349a522011-07-04 01:30:29 +0000507 }
Marcus Meissnerd0733d22011-06-01 21:05:22 -0700508
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900509 chk_addr_ret = inet_addr_type(sock_net(sk), addr->sin_addr.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 /* Not specified by any standard per-se, however it breaks too
512 * many applications when removed. It is unfortunate since
513 * allowing applications to make a non-local bind solves
514 * several problems with systems using dynamic addressing.
515 * (ie. your servers still start up even if your ISDN link
516 * is temporarily down)
517 */
518 err = -EADDRNOTAVAIL;
519 if (!sysctl_ip_nonlocal_bind &&
Tóth László Attilab9fb1502008-10-01 07:31:24 -0700520 !(inet->freebind || inet->transparent) &&
Al Viroe6f1ceb2008-03-17 22:44:53 -0700521 addr->sin_addr.s_addr != htonl(INADDR_ANY) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 chk_addr_ret != RTN_LOCAL &&
523 chk_addr_ret != RTN_MULTICAST &&
524 chk_addr_ret != RTN_BROADCAST)
525 goto out;
526
527 snum = ntohs(addr->sin_port);
528 err = -EACCES;
529 if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
530 goto out;
531
532 /* We keep a pair of addresses. rcv_saddr is the one
533 * used by hash lookups, and saddr is used for transmit.
534 *
535 * In the BSD API these are the same except where it
536 * would be illegal to use them (multicast/broadcast) in
537 * which case the sending device address is used.
538 */
539 lock_sock(sk);
540
541 /* Check these errors (active socket, double bind). */
542 err = -EINVAL;
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000543 if (sk->sk_state != TCP_CLOSE || inet->inet_num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 goto out_release_sock;
545
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000546 inet->inet_rcv_saddr = inet->inet_saddr = addr->sin_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 if (chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST)
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000548 inet->inet_saddr = 0; /* Use device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 /* Make sure we are allowed to bind here. */
551 if (sk->sk_prot->get_port(sk, snum)) {
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000552 inet->inet_saddr = inet->inet_rcv_saddr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 err = -EADDRINUSE;
554 goto out_release_sock;
555 }
556
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000557 if (inet->inet_rcv_saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
559 if (snum)
560 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000561 inet->inet_sport = htons(inet->inet_num);
562 inet->inet_daddr = 0;
563 inet->inet_dport = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 sk_dst_reset(sk);
565 err = 0;
566out_release_sock:
567 release_sock(sk);
568out:
569 return err;
570}
Eric Dumazet3d1427f2009-08-28 23:45:21 -0700571EXPORT_SYMBOL(inet_bind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573int inet_dgram_connect(struct socket *sock, struct sockaddr * uaddr,
574 int addr_len, int flags)
575{
576 struct sock *sk = sock->sk;
577
Changli Gao6503d962010-03-31 22:58:26 +0000578 if (addr_len < sizeof(uaddr->sa_family))
579 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 if (uaddr->sa_family == AF_UNSPEC)
581 return sk->sk_prot->disconnect(sk, flags);
582
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000583 if (!inet_sk(sk)->inet_num && inet_autobind(sk))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 return -EAGAIN;
585 return sk->sk_prot->connect(sk, (struct sockaddr *)uaddr, addr_len);
586}
Eric Dumazet3d1427f2009-08-28 23:45:21 -0700587EXPORT_SYMBOL(inet_dgram_connect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589static long inet_wait_for_connect(struct sock *sk, long timeo)
590{
591 DEFINE_WAIT(wait);
592
Eric Dumazetaa395142010-04-20 13:03:51 +0000593 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595 /* Basic assumption: if someone sets sk->sk_err, he _must_
596 * change state of the socket from TCP_SYN_*.
597 * Connect() does not allow to get error notifications
598 * without closing the socket.
599 */
600 while ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
601 release_sock(sk);
602 timeo = schedule_timeout(timeo);
603 lock_sock(sk);
604 if (signal_pending(current) || !timeo)
605 break;
Eric Dumazetaa395142010-04-20 13:03:51 +0000606 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 }
Eric Dumazetaa395142010-04-20 13:03:51 +0000608 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 return timeo;
610}
611
612/*
613 * Connect to a remote host. There is regrettably still a little
614 * TCP 'magic' in here.
615 */
616int inet_stream_connect(struct socket *sock, struct sockaddr *uaddr,
617 int addr_len, int flags)
618{
619 struct sock *sk = sock->sk;
620 int err;
621 long timeo;
622
Changli Gao6503d962010-03-31 22:58:26 +0000623 if (addr_len < sizeof(uaddr->sa_family))
624 return -EINVAL;
625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 lock_sock(sk);
627
628 if (uaddr->sa_family == AF_UNSPEC) {
629 err = sk->sk_prot->disconnect(sk, flags);
630 sock->state = err ? SS_DISCONNECTING : SS_UNCONNECTED;
631 goto out;
632 }
633
634 switch (sock->state) {
635 default:
636 err = -EINVAL;
637 goto out;
638 case SS_CONNECTED:
639 err = -EISCONN;
640 goto out;
641 case SS_CONNECTING:
642 err = -EALREADY;
643 /* Fall out of switch with err, set for this state */
644 break;
645 case SS_UNCONNECTED:
646 err = -EISCONN;
647 if (sk->sk_state != TCP_CLOSE)
648 goto out;
649
650 err = sk->sk_prot->connect(sk, uaddr, addr_len);
651 if (err < 0)
652 goto out;
653
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900654 sock->state = SS_CONNECTING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
656 /* Just entered SS_CONNECTING state; the only
657 * difference is that return value in non-blocking
658 * case is EINPROGRESS, rather than EALREADY.
659 */
660 err = -EINPROGRESS;
661 break;
662 }
663
664 timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
665
666 if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
667 /* Error code is set above */
668 if (!timeo || !inet_wait_for_connect(sk, timeo))
669 goto out;
670
671 err = sock_intr_errno(timeo);
672 if (signal_pending(current))
673 goto out;
674 }
675
676 /* Connection was closed by RST, timeout, ICMP error
677 * or another process disconnected us.
678 */
679 if (sk->sk_state == TCP_CLOSE)
680 goto sock_error;
681
682 /* sk->sk_err may be not zero now, if RECVERR was ordered by user
683 * and error was received after socket entered established state.
684 * Hence, it is handled normally after connect() return successfully.
685 */
686
687 sock->state = SS_CONNECTED;
688 err = 0;
689out:
690 release_sock(sk);
691 return err;
692
693sock_error:
694 err = sock_error(sk) ? : -ECONNABORTED;
695 sock->state = SS_UNCONNECTED;
696 if (sk->sk_prot->disconnect(sk, flags))
697 sock->state = SS_DISCONNECTING;
698 goto out;
699}
Eric Dumazet3d1427f2009-08-28 23:45:21 -0700700EXPORT_SYMBOL(inet_stream_connect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
702/*
703 * Accept a pending connection. The TCP layer now gives BSD semantics.
704 */
705
706int inet_accept(struct socket *sock, struct socket *newsock, int flags)
707{
708 struct sock *sk1 = sock->sk;
709 int err = -EINVAL;
710 struct sock *sk2 = sk1->sk_prot->accept(sk1, flags, &err);
711
712 if (!sk2)
713 goto do_err;
714
715 lock_sock(sk2);
716
Eric Dumazet1eddcea2011-06-17 03:45:15 +0000717 sock_rps_record_flow(sk2);
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700718 WARN_ON(!((1 << sk2->sk_state) &
719 (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT | TCPF_CLOSE)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
721 sock_graft(sk2, newsock);
722
723 newsock->state = SS_CONNECTED;
724 err = 0;
725 release_sock(sk2);
726do_err:
727 return err;
728}
Eric Dumazet3d1427f2009-08-28 23:45:21 -0700729EXPORT_SYMBOL(inet_accept);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731
732/*
733 * This does both peername and sockname.
734 */
735int inet_getname(struct socket *sock, struct sockaddr *uaddr,
736 int *uaddr_len, int peer)
737{
738 struct sock *sk = sock->sk;
739 struct inet_sock *inet = inet_sk(sk);
Cyrill Gorcunov38bfd8f2009-10-29 02:59:18 -0700740 DECLARE_SOCKADDR(struct sockaddr_in *, sin, uaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
742 sin->sin_family = AF_INET;
743 if (peer) {
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000744 if (!inet->inet_dport ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
746 peer == 1))
747 return -ENOTCONN;
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000748 sin->sin_port = inet->inet_dport;
749 sin->sin_addr.s_addr = inet->inet_daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 } else {
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000751 __be32 addr = inet->inet_rcv_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 if (!addr)
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000753 addr = inet->inet_saddr;
754 sin->sin_port = inet->inet_sport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 sin->sin_addr.s_addr = addr;
756 }
757 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
758 *uaddr_len = sizeof(*sin);
759 return 0;
760}
Eric Dumazet3d1427f2009-08-28 23:45:21 -0700761EXPORT_SYMBOL(inet_getname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
763int inet_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg,
764 size_t size)
765{
766 struct sock *sk = sock->sk;
767
David S. Millerc58dc012010-04-27 15:05:31 -0700768 sock_rps_record_flow(sk);
Tom Herbertfec5e652010-04-16 16:01:27 -0700769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 /* We may need to bind the socket. */
Changli Gao7ba42912010-07-10 20:41:55 +0000771 if (!inet_sk(sk)->inet_num && !sk->sk_prot->no_autobind &&
772 inet_autobind(sk))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 return -EAGAIN;
774
775 return sk->sk_prot->sendmsg(iocb, sk, msg, size);
776}
Eric Dumazet3d1427f2009-08-28 23:45:21 -0700777EXPORT_SYMBOL(inet_sendmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Changli Gao7ba42912010-07-10 20:41:55 +0000779ssize_t inet_sendpage(struct socket *sock, struct page *page, int offset,
780 size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
782 struct sock *sk = sock->sk;
783
David S. Millerc58dc012010-04-27 15:05:31 -0700784 sock_rps_record_flow(sk);
Tom Herbertfec5e652010-04-16 16:01:27 -0700785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 /* We may need to bind the socket. */
Changli Gao7ba42912010-07-10 20:41:55 +0000787 if (!inet_sk(sk)->inet_num && !sk->sk_prot->no_autobind &&
788 inet_autobind(sk))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 return -EAGAIN;
790
791 if (sk->sk_prot->sendpage)
792 return sk->sk_prot->sendpage(sk, page, offset, size, flags);
793 return sock_no_sendpage(sock, page, offset, size, flags);
794}
Changli Gao7ba42912010-07-10 20:41:55 +0000795EXPORT_SYMBOL(inet_sendpage);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Tom Herbertfec5e652010-04-16 16:01:27 -0700797int inet_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg,
798 size_t size, int flags)
799{
800 struct sock *sk = sock->sk;
801 int addr_len = 0;
802 int err;
803
David S. Millerc58dc012010-04-27 15:05:31 -0700804 sock_rps_record_flow(sk);
Tom Herbertfec5e652010-04-16 16:01:27 -0700805
806 err = sk->sk_prot->recvmsg(iocb, sk, msg, size, flags & MSG_DONTWAIT,
807 flags & ~MSG_DONTWAIT, &addr_len);
808 if (err >= 0)
809 msg->msg_namelen = addr_len;
810 return err;
811}
812EXPORT_SYMBOL(inet_recvmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
814int inet_shutdown(struct socket *sock, int how)
815{
816 struct sock *sk = sock->sk;
817 int err = 0;
818
819 /* This should really check to make sure
820 * the socket is a TCP socket. (WHY AC...)
821 */
822 how++; /* maps 0->1 has the advantage of making bit 1 rcvs and
823 1->2 bit 2 snds.
824 2->3 */
825 if ((how & ~SHUTDOWN_MASK) || !how) /* MAXINT->0 */
826 return -EINVAL;
827
828 lock_sock(sk);
829 if (sock->state == SS_CONNECTING) {
830 if ((1 << sk->sk_state) &
831 (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_CLOSE))
832 sock->state = SS_DISCONNECTING;
833 else
834 sock->state = SS_CONNECTED;
835 }
836
837 switch (sk->sk_state) {
838 case TCP_CLOSE:
839 err = -ENOTCONN;
840 /* Hack to wake up other listeners, who can poll for
841 POLLHUP, even on eg. unconnected UDP sockets -- RR */
842 default:
843 sk->sk_shutdown |= how;
844 if (sk->sk_prot->shutdown)
845 sk->sk_prot->shutdown(sk, how);
846 break;
847
848 /* Remaining two branches are temporary solution for missing
849 * close() in multithreaded environment. It is _not_ a good idea,
850 * but we have no choice until close() is repaired at VFS level.
851 */
852 case TCP_LISTEN:
853 if (!(how & RCV_SHUTDOWN))
854 break;
855 /* Fall through */
856 case TCP_SYN_SENT:
857 err = sk->sk_prot->disconnect(sk, O_NONBLOCK);
858 sock->state = err ? SS_DISCONNECTING : SS_UNCONNECTED;
859 break;
860 }
861
862 /* Wake up anyone sleeping in poll. */
863 sk->sk_state_change(sk);
864 release_sock(sk);
865 return err;
866}
Eric Dumazet3d1427f2009-08-28 23:45:21 -0700867EXPORT_SYMBOL(inet_shutdown);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
869/*
870 * ioctl() calls you can issue on an INET socket. Most of these are
871 * device configuration and stuff and very rarely used. Some ioctls
872 * pass on to the socket itself.
873 *
874 * NOTE: I like the idea of a module for the config stuff. ie ifconfig
875 * loads the devconfigure module does its configuring and unloads it.
876 * There's a good 20K of config code hanging around the kernel.
877 */
878
879int inet_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
880{
881 struct sock *sk = sock->sk;
882 int err = 0;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900883 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
885 switch (cmd) {
Eric Dumazet3d1427f2009-08-28 23:45:21 -0700886 case SIOCGSTAMP:
887 err = sock_get_timestamp(sk, (struct timeval __user *)arg);
888 break;
889 case SIOCGSTAMPNS:
890 err = sock_get_timestampns(sk, (struct timespec __user *)arg);
891 break;
892 case SIOCADDRT:
893 case SIOCDELRT:
894 case SIOCRTMSG:
895 err = ip_rt_ioctl(net, cmd, (void __user *)arg);
896 break;
897 case SIOCDARP:
898 case SIOCGARP:
899 case SIOCSARP:
900 err = arp_ioctl(net, cmd, (void __user *)arg);
901 break;
902 case SIOCGIFADDR:
903 case SIOCSIFADDR:
904 case SIOCGIFBRDADDR:
905 case SIOCSIFBRDADDR:
906 case SIOCGIFNETMASK:
907 case SIOCSIFNETMASK:
908 case SIOCGIFDSTADDR:
909 case SIOCSIFDSTADDR:
910 case SIOCSIFPFLAGS:
911 case SIOCGIFPFLAGS:
912 case SIOCSIFFLAGS:
Robert Love2365d052008-05-12 17:08:29 -0400913 case SIOCKILLADDR:
Eric Dumazet3d1427f2009-08-28 23:45:21 -0700914 err = devinet_ioctl(net, cmd, (void __user *)arg);
915 break;
916 default:
917 if (sk->sk_prot->ioctl)
918 err = sk->sk_prot->ioctl(sk, cmd, arg);
919 else
920 err = -ENOIOCTLCMD;
921 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 }
923 return err;
924}
Eric Dumazet3d1427f2009-08-28 23:45:21 -0700925EXPORT_SYMBOL(inet_ioctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Eric W. Biederman709b46e2011-01-29 16:15:56 +0000927#ifdef CONFIG_COMPAT
Gerrit Renker686dc6b2011-10-15 09:26:56 +0000928static int inet_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
Eric W. Biederman709b46e2011-01-29 16:15:56 +0000929{
930 struct sock *sk = sock->sk;
931 int err = -ENOIOCTLCMD;
932
933 if (sk->sk_prot->compat_ioctl)
934 err = sk->sk_prot->compat_ioctl(sk, cmd, arg);
935
936 return err;
937}
938#endif
939
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800940const struct proto_ops inet_stream_ops = {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800941 .family = PF_INET,
942 .owner = THIS_MODULE,
943 .release = inet_release,
944 .bind = inet_bind,
945 .connect = inet_stream_connect,
946 .socketpair = sock_no_socketpair,
947 .accept = inet_accept,
948 .getname = inet_getname,
949 .poll = tcp_poll,
950 .ioctl = inet_ioctl,
951 .listen = inet_listen,
952 .shutdown = inet_shutdown,
953 .setsockopt = sock_common_setsockopt,
954 .getsockopt = sock_common_getsockopt,
Changli Gao7ba42912010-07-10 20:41:55 +0000955 .sendmsg = inet_sendmsg,
Tom Herbertfec5e652010-04-16 16:01:27 -0700956 .recvmsg = inet_recvmsg,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800957 .mmap = sock_no_mmap,
Changli Gao7ba42912010-07-10 20:41:55 +0000958 .sendpage = inet_sendpage,
Jens Axboe9c55e012007-11-06 23:30:13 -0800959 .splice_read = tcp_splice_read,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800960#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800961 .compat_setsockopt = compat_sock_common_setsockopt,
962 .compat_getsockopt = compat_sock_common_getsockopt,
Eric W. Biederman709b46e2011-01-29 16:15:56 +0000963 .compat_ioctl = inet_compat_ioctl,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800964#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965};
Eric Dumazet3d1427f2009-08-28 23:45:21 -0700966EXPORT_SYMBOL(inet_stream_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800968const struct proto_ops inet_dgram_ops = {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800969 .family = PF_INET,
970 .owner = THIS_MODULE,
971 .release = inet_release,
972 .bind = inet_bind,
973 .connect = inet_dgram_connect,
974 .socketpair = sock_no_socketpair,
975 .accept = sock_no_accept,
976 .getname = inet_getname,
977 .poll = udp_poll,
978 .ioctl = inet_ioctl,
979 .listen = sock_no_listen,
980 .shutdown = inet_shutdown,
981 .setsockopt = sock_common_setsockopt,
982 .getsockopt = sock_common_getsockopt,
983 .sendmsg = inet_sendmsg,
Tom Herbertfec5e652010-04-16 16:01:27 -0700984 .recvmsg = inet_recvmsg,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800985 .mmap = sock_no_mmap,
986 .sendpage = inet_sendpage,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800987#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800988 .compat_setsockopt = compat_sock_common_setsockopt,
989 .compat_getsockopt = compat_sock_common_getsockopt,
Eric W. Biederman709b46e2011-01-29 16:15:56 +0000990 .compat_ioctl = inet_compat_ioctl,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800991#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992};
Eric Dumazet3d1427f2009-08-28 23:45:21 -0700993EXPORT_SYMBOL(inet_dgram_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
995/*
996 * For SOCK_RAW sockets; should be the same as inet_dgram_ops but without
997 * udp_poll
998 */
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800999static const struct proto_ops inet_sockraw_ops = {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001000 .family = PF_INET,
1001 .owner = THIS_MODULE,
1002 .release = inet_release,
1003 .bind = inet_bind,
1004 .connect = inet_dgram_connect,
1005 .socketpair = sock_no_socketpair,
1006 .accept = sock_no_accept,
1007 .getname = inet_getname,
1008 .poll = datagram_poll,
1009 .ioctl = inet_ioctl,
1010 .listen = sock_no_listen,
1011 .shutdown = inet_shutdown,
1012 .setsockopt = sock_common_setsockopt,
1013 .getsockopt = sock_common_getsockopt,
1014 .sendmsg = inet_sendmsg,
Tom Herbertfec5e652010-04-16 16:01:27 -07001015 .recvmsg = inet_recvmsg,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001016 .mmap = sock_no_mmap,
1017 .sendpage = inet_sendpage,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001018#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001019 .compat_setsockopt = compat_sock_common_setsockopt,
1020 .compat_getsockopt = compat_sock_common_getsockopt,
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001021 .compat_ioctl = inet_compat_ioctl,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001022#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023};
1024
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00001025static const struct net_proto_family inet_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 .family = PF_INET,
1027 .create = inet_create,
1028 .owner = THIS_MODULE,
1029};
1030
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031/* Upon startup we insert all the elements in inetsw_array[] into
1032 * the linked list inetsw.
1033 */
1034static struct inet_protosw inetsw_array[] =
1035{
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001036 {
1037 .type = SOCK_STREAM,
1038 .protocol = IPPROTO_TCP,
1039 .prot = &tcp_prot,
1040 .ops = &inet_stream_ops,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001041 .no_check = 0,
1042 .flags = INET_PROTOSW_PERMANENT |
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -08001043 INET_PROTOSW_ICSK,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001044 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001046 {
1047 .type = SOCK_DGRAM,
1048 .protocol = IPPROTO_UDP,
1049 .prot = &udp_prot,
1050 .ops = &inet_dgram_ops,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001051 .no_check = UDP_CSUM_DEFAULT,
1052 .flags = INET_PROTOSW_PERMANENT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 },
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001054
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001055 {
1056 .type = SOCK_DGRAM,
1057 .protocol = IPPROTO_ICMP,
1058 .prot = &ping_prot,
1059 .ops = &inet_dgram_ops,
1060 .no_check = UDP_CSUM_DEFAULT,
1061 .flags = INET_PROTOSW_REUSE,
1062 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
1064 {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001065 .type = SOCK_RAW,
1066 .protocol = IPPROTO_IP, /* wild card */
1067 .prot = &raw_prot,
1068 .ops = &inet_sockraw_ops,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001069 .no_check = UDP_CSUM_DEFAULT,
1070 .flags = INET_PROTOSW_REUSE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 }
1072};
1073
Denis Chengc40f6ff2007-09-16 16:39:25 -07001074#define INETSW_ARRAY_LEN ARRAY_SIZE(inetsw_array)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
1076void inet_register_protosw(struct inet_protosw *p)
1077{
1078 struct list_head *lh;
1079 struct inet_protosw *answer;
1080 int protocol = p->protocol;
1081 struct list_head *last_perm;
1082
1083 spin_lock_bh(&inetsw_lock);
1084
1085 if (p->type >= SOCK_MAX)
1086 goto out_illegal;
1087
1088 /* If we are trying to override a permanent protocol, bail. */
1089 answer = NULL;
1090 last_perm = &inetsw[p->type];
1091 list_for_each(lh, &inetsw[p->type]) {
1092 answer = list_entry(lh, struct inet_protosw, list);
1093
1094 /* Check only the non-wild match. */
1095 if (INET_PROTOSW_PERMANENT & answer->flags) {
1096 if (protocol == answer->protocol)
1097 break;
1098 last_perm = lh;
1099 }
1100
1101 answer = NULL;
1102 }
1103 if (answer)
1104 goto out_permanent;
1105
1106 /* Add the new entry after the last permanent entry if any, so that
1107 * the new entry does not override a permanent entry when matched with
1108 * a wild-card protocol. But it is allowed to override any existing
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001109 * non-permanent entry. This means that when we remove this entry, the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 * system automatically returns to the old behavior.
1111 */
1112 list_add_rcu(&p->list, last_perm);
1113out:
1114 spin_unlock_bh(&inetsw_lock);
1115
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 return;
1117
1118out_permanent:
Joe Perches058bd4d2012-03-11 18:36:11 +00001119 pr_err("Attempt to override permanent protocol %d\n", protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 goto out;
1121
1122out_illegal:
Joe Perches058bd4d2012-03-11 18:36:11 +00001123 pr_err("Ignoring attempt to register invalid socket type %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 p->type);
1125 goto out;
1126}
Eric Dumazet3d1427f2009-08-28 23:45:21 -07001127EXPORT_SYMBOL(inet_register_protosw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
1129void inet_unregister_protosw(struct inet_protosw *p)
1130{
1131 if (INET_PROTOSW_PERMANENT & p->flags) {
Joe Perches058bd4d2012-03-11 18:36:11 +00001132 pr_err("Attempt to unregister permanent protocol %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 p->protocol);
1134 } else {
1135 spin_lock_bh(&inetsw_lock);
1136 list_del_rcu(&p->list);
1137 spin_unlock_bh(&inetsw_lock);
1138
1139 synchronize_net();
1140 }
1141}
Eric Dumazet3d1427f2009-08-28 23:45:21 -07001142EXPORT_SYMBOL(inet_unregister_protosw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
Arnaldo Carvalho de Melo32519f12005-08-09 19:50:02 -07001144/*
1145 * Shall we try to damage output packets if routing dev changes?
1146 */
1147
Brian Haleyab32ea52006-09-22 14:15:41 -07001148int sysctl_ip_dynaddr __read_mostly;
Arnaldo Carvalho de Melo32519f12005-08-09 19:50:02 -07001149
1150static int inet_sk_reselect_saddr(struct sock *sk)
1151{
1152 struct inet_sock *inet = inet_sk(sk);
Eric Dumazetc720c7e2009-10-15 06:30:45 +00001153 __be32 old_saddr = inet->inet_saddr;
Eric Dumazetc720c7e2009-10-15 06:30:45 +00001154 __be32 daddr = inet->inet_daddr;
David S. Miller6e869132011-05-06 16:18:04 -07001155 struct flowi4 *fl4;
David S. Millerb23dd4f2011-03-02 14:31:35 -08001156 struct rtable *rt;
1157 __be32 new_saddr;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001158 struct ip_options_rcu *inet_opt;
Arnaldo Carvalho de Melo32519f12005-08-09 19:50:02 -07001159
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001160 inet_opt = rcu_dereference_protected(inet->inet_opt,
1161 sock_owned_by_user(sk));
1162 if (inet_opt && inet_opt->opt.srr)
1163 daddr = inet_opt->opt.faddr;
Arnaldo Carvalho de Melo32519f12005-08-09 19:50:02 -07001164
1165 /* Query new route. */
David S. Miller6e869132011-05-06 16:18:04 -07001166 fl4 = &inet->cork.fl.u.ip4;
1167 rt = ip_route_connect(fl4, daddr, 0, RT_CONN_FLAGS(sk),
David S. Millerb23dd4f2011-03-02 14:31:35 -08001168 sk->sk_bound_dev_if, sk->sk_protocol,
1169 inet->inet_sport, inet->inet_dport, sk, false);
1170 if (IS_ERR(rt))
1171 return PTR_ERR(rt);
Arnaldo Carvalho de Melo32519f12005-08-09 19:50:02 -07001172
Changli Gaod8d1f302010-06-10 23:31:35 -07001173 sk_setup_caps(sk, &rt->dst);
Arnaldo Carvalho de Melo32519f12005-08-09 19:50:02 -07001174
David S. Miller6e869132011-05-06 16:18:04 -07001175 new_saddr = fl4->saddr;
Arnaldo Carvalho de Melo32519f12005-08-09 19:50:02 -07001176
1177 if (new_saddr == old_saddr)
1178 return 0;
1179
1180 if (sysctl_ip_dynaddr > 1) {
Joe Perches058bd4d2012-03-11 18:36:11 +00001181 pr_info("%s(): shifting inet->saddr from %pI4 to %pI4\n",
1182 __func__, &old_saddr, &new_saddr);
Arnaldo Carvalho de Melo32519f12005-08-09 19:50:02 -07001183 }
1184
Eric Dumazetc720c7e2009-10-15 06:30:45 +00001185 inet->inet_saddr = inet->inet_rcv_saddr = new_saddr;
Arnaldo Carvalho de Melo32519f12005-08-09 19:50:02 -07001186
1187 /*
1188 * XXX The only one ugly spot where we need to
1189 * XXX really change the sockets identity after
1190 * XXX it has entered the hashes. -DaveM
1191 *
1192 * Besides that, it does not check for connection
1193 * uniqueness. Wait for troubles.
1194 */
1195 __sk_prot_rehash(sk);
1196 return 0;
1197}
1198
1199int inet_sk_rebuild_header(struct sock *sk)
1200{
1201 struct inet_sock *inet = inet_sk(sk);
1202 struct rtable *rt = (struct rtable *)__sk_dst_check(sk, 0);
Al Viro3ca3c682006-09-27 18:28:07 -07001203 __be32 daddr;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001204 struct ip_options_rcu *inet_opt;
David S. Miller6e869132011-05-06 16:18:04 -07001205 struct flowi4 *fl4;
Arnaldo Carvalho de Melo32519f12005-08-09 19:50:02 -07001206 int err;
1207
1208 /* Route is OK, nothing to do. */
1209 if (rt)
1210 return 0;
1211
1212 /* Reroute. */
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001213 rcu_read_lock();
1214 inet_opt = rcu_dereference(inet->inet_opt);
Eric Dumazetc720c7e2009-10-15 06:30:45 +00001215 daddr = inet->inet_daddr;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001216 if (inet_opt && inet_opt->opt.srr)
1217 daddr = inet_opt->opt.faddr;
1218 rcu_read_unlock();
David S. Miller6e869132011-05-06 16:18:04 -07001219 fl4 = &inet->cork.fl.u.ip4;
1220 rt = ip_route_output_ports(sock_net(sk), fl4, sk, daddr, inet->inet_saddr,
David S. Miller78fbfd82011-03-12 00:00:52 -05001221 inet->inet_dport, inet->inet_sport,
1222 sk->sk_protocol, RT_CONN_FLAGS(sk),
1223 sk->sk_bound_dev_if);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001224 if (!IS_ERR(rt)) {
1225 err = 0;
Changli Gaod8d1f302010-06-10 23:31:35 -07001226 sk_setup_caps(sk, &rt->dst);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001227 } else {
1228 err = PTR_ERR(rt);
1229
Arnaldo Carvalho de Melo32519f12005-08-09 19:50:02 -07001230 /* Routing failed... */
1231 sk->sk_route_caps = 0;
1232 /*
1233 * Other protocols have to map its equivalent state to TCP_SYN_SENT.
1234 * DCCP maps its DCCP_REQUESTING state to TCP_SYN_SENT. -acme
1235 */
1236 if (!sysctl_ip_dynaddr ||
1237 sk->sk_state != TCP_SYN_SENT ||
1238 (sk->sk_userlocks & SOCK_BINDADDR_LOCK) ||
1239 (err = inet_sk_reselect_saddr(sk)) != 0)
1240 sk->sk_err_soft = -err;
1241 }
1242
1243 return err;
1244}
Arnaldo Carvalho de Melo32519f12005-08-09 19:50:02 -07001245EXPORT_SYMBOL(inet_sk_rebuild_header);
1246
Herbert Xua430a432006-07-08 13:34:56 -07001247static int inet_gso_send_check(struct sk_buff *skb)
1248{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001249 const struct iphdr *iph;
Alexey Dobriyan32613092009-09-14 12:21:47 +00001250 const struct net_protocol *ops;
Herbert Xua430a432006-07-08 13:34:56 -07001251 int proto;
1252 int ihl;
1253 int err = -EINVAL;
1254
1255 if (unlikely(!pskb_may_pull(skb, sizeof(*iph))))
1256 goto out;
1257
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001258 iph = ip_hdr(skb);
Herbert Xua430a432006-07-08 13:34:56 -07001259 ihl = iph->ihl * 4;
1260 if (ihl < sizeof(*iph))
1261 goto out;
1262
1263 if (unlikely(!pskb_may_pull(skb, ihl)))
1264 goto out;
1265
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03001266 __skb_pull(skb, ihl);
1267 skb_reset_transport_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001268 iph = ip_hdr(skb);
Herbert Xua430a432006-07-08 13:34:56 -07001269 proto = iph->protocol & (MAX_INET_PROTOS - 1);
1270 err = -EPROTONOSUPPORT;
1271
1272 rcu_read_lock();
1273 ops = rcu_dereference(inet_protos[proto]);
1274 if (likely(ops && ops->gso_send_check))
1275 err = ops->gso_send_check(skb);
1276 rcu_read_unlock();
1277
1278out:
1279 return err;
1280}
1281
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001282static struct sk_buff *inet_gso_segment(struct sk_buff *skb,
1283 netdev_features_t features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07001284{
1285 struct sk_buff *segs = ERR_PTR(-EINVAL);
1286 struct iphdr *iph;
Alexey Dobriyan32613092009-09-14 12:21:47 +00001287 const struct net_protocol *ops;
Herbert Xuf4c50d92006-06-22 03:02:40 -07001288 int proto;
1289 int ihl;
1290 int id;
Sridhar Samudralad7ca4cc2009-07-09 08:09:47 +00001291 unsigned int offset = 0;
Herbert Xuf4c50d92006-06-22 03:02:40 -07001292
Stephen Hemmingerd212f872007-06-27 00:47:37 -07001293 if (!(features & NETIF_F_V4_CSUM))
1294 features &= ~NETIF_F_SG;
1295
Herbert Xubbcf4672006-07-03 19:38:35 -07001296 if (unlikely(skb_shinfo(skb)->gso_type &
1297 ~(SKB_GSO_TCPV4 |
1298 SKB_GSO_UDP |
1299 SKB_GSO_DODGY |
1300 SKB_GSO_TCP_ECN |
1301 0)))
1302 goto out;
1303
1304 if (unlikely(!pskb_may_pull(skb, sizeof(*iph))))
Herbert Xuf4c50d92006-06-22 03:02:40 -07001305 goto out;
1306
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001307 iph = ip_hdr(skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07001308 ihl = iph->ihl * 4;
1309 if (ihl < sizeof(*iph))
1310 goto out;
1311
Herbert Xubbcf4672006-07-03 19:38:35 -07001312 if (unlikely(!pskb_may_pull(skb, ihl)))
Herbert Xuf4c50d92006-06-22 03:02:40 -07001313 goto out;
1314
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03001315 __skb_pull(skb, ihl);
1316 skb_reset_transport_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001317 iph = ip_hdr(skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07001318 id = ntohs(iph->id);
1319 proto = iph->protocol & (MAX_INET_PROTOS - 1);
1320 segs = ERR_PTR(-EPROTONOSUPPORT);
1321
1322 rcu_read_lock();
1323 ops = rcu_dereference(inet_protos[proto]);
Herbert Xubbcf4672006-07-03 19:38:35 -07001324 if (likely(ops && ops->gso_segment))
Herbert Xu576a30e2006-06-27 13:22:38 -07001325 segs = ops->gso_segment(skb, features);
Herbert Xuf4c50d92006-06-22 03:02:40 -07001326 rcu_read_unlock();
1327
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07001328 if (!segs || IS_ERR(segs))
Herbert Xuf4c50d92006-06-22 03:02:40 -07001329 goto out;
1330
1331 skb = segs;
1332 do {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001333 iph = ip_hdr(skb);
Sridhar Samudralad7ca4cc2009-07-09 08:09:47 +00001334 if (proto == IPPROTO_UDP) {
1335 iph->id = htons(id);
1336 iph->frag_off = htons(offset >> 3);
1337 if (skb->next != NULL)
1338 iph->frag_off |= htons(IP_MF);
1339 offset += (skb->len - skb->mac_len - iph->ihl * 4);
1340 } else
1341 iph->id = htons(id++);
Herbert Xuf4c50d92006-06-22 03:02:40 -07001342 iph->tot_len = htons(skb->len - skb->mac_len);
1343 iph->check = 0;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001344 iph->check = ip_fast_csum(skb_network_header(skb), iph->ihl);
Herbert Xuf4c50d92006-06-22 03:02:40 -07001345 } while ((skb = skb->next));
1346
1347out:
1348 return segs;
1349}
1350
Herbert Xu73cc19f2008-12-15 23:41:09 -08001351static struct sk_buff **inet_gro_receive(struct sk_buff **head,
1352 struct sk_buff *skb)
1353{
Alexey Dobriyan32613092009-09-14 12:21:47 +00001354 const struct net_protocol *ops;
Herbert Xu73cc19f2008-12-15 23:41:09 -08001355 struct sk_buff **pp = NULL;
1356 struct sk_buff *p;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001357 const struct iphdr *iph;
Herbert Xua5b1cf22009-05-26 18:50:28 +00001358 unsigned int hlen;
1359 unsigned int off;
Herbert Xu1075f3f2009-05-26 18:50:29 +00001360 unsigned int id;
Herbert Xu73cc19f2008-12-15 23:41:09 -08001361 int flush = 1;
1362 int proto;
Herbert Xu73cc19f2008-12-15 23:41:09 -08001363
Herbert Xua5b1cf22009-05-26 18:50:28 +00001364 off = skb_gro_offset(skb);
1365 hlen = off + sizeof(*iph);
1366 iph = skb_gro_header_fast(skb, off);
1367 if (skb_gro_header_hard(skb, hlen)) {
1368 iph = skb_gro_header_slow(skb, hlen, off);
1369 if (unlikely(!iph))
1370 goto out;
1371 }
Herbert Xu73cc19f2008-12-15 23:41:09 -08001372
Herbert Xu73cc19f2008-12-15 23:41:09 -08001373 proto = iph->protocol & (MAX_INET_PROTOS - 1);
1374
1375 rcu_read_lock();
1376 ops = rcu_dereference(inet_protos[proto]);
1377 if (!ops || !ops->gro_receive)
1378 goto out_unlock;
1379
Herbert Xua5ad24b2009-02-08 18:00:39 +00001380 if (*(u8 *)iph != 0x45)
Herbert Xu73cc19f2008-12-15 23:41:09 -08001381 goto out_unlock;
1382
1383 if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
1384 goto out_unlock;
1385
Eric Dumazet0eae88f2010-04-20 19:06:52 -07001386 id = ntohl(*(__be32 *)&iph->id);
1387 flush = (u16)((ntohl(*(__be32 *)iph) ^ skb_gro_len(skb)) | (id ^ IP_DF));
Herbert Xu1075f3f2009-05-26 18:50:29 +00001388 id >>= 16;
Herbert Xu73cc19f2008-12-15 23:41:09 -08001389
1390 for (p = *head; p; p = p->next) {
1391 struct iphdr *iph2;
1392
1393 if (!NAPI_GRO_CB(p)->same_flow)
1394 continue;
1395
1396 iph2 = ip_hdr(p);
1397
Herbert Xua5ad24b2009-02-08 18:00:39 +00001398 if ((iph->protocol ^ iph2->protocol) |
1399 (iph->tos ^ iph2->tos) |
Eric Dumazet0eae88f2010-04-20 19:06:52 -07001400 ((__force u32)iph->saddr ^ (__force u32)iph2->saddr) |
1401 ((__force u32)iph->daddr ^ (__force u32)iph2->daddr)) {
Herbert Xu73cc19f2008-12-15 23:41:09 -08001402 NAPI_GRO_CB(p)->same_flow = 0;
1403 continue;
1404 }
1405
1406 /* All fields must match except length and checksum. */
1407 NAPI_GRO_CB(p)->flush |=
Herbert Xua5ad24b2009-02-08 18:00:39 +00001408 (iph->ttl ^ iph2->ttl) |
1409 ((u16)(ntohs(iph2->id) + NAPI_GRO_CB(p)->count) ^ id);
Herbert Xu73cc19f2008-12-15 23:41:09 -08001410
1411 NAPI_GRO_CB(p)->flush |= flush;
1412 }
1413
1414 NAPI_GRO_CB(skb)->flush |= flush;
Herbert Xu86911732009-01-29 14:19:50 +00001415 skb_gro_pull(skb, sizeof(*iph));
1416 skb_set_transport_header(skb, skb_gro_offset(skb));
Herbert Xu73cc19f2008-12-15 23:41:09 -08001417
1418 pp = ops->gro_receive(head, skb);
1419
1420out_unlock:
1421 rcu_read_unlock();
1422
1423out:
1424 NAPI_GRO_CB(skb)->flush |= flush;
1425
1426 return pp;
1427}
1428
1429static int inet_gro_complete(struct sk_buff *skb)
1430{
Alexey Dobriyan32613092009-09-14 12:21:47 +00001431 const struct net_protocol *ops;
Herbert Xu73cc19f2008-12-15 23:41:09 -08001432 struct iphdr *iph = ip_hdr(skb);
1433 int proto = iph->protocol & (MAX_INET_PROTOS - 1);
1434 int err = -ENOSYS;
1435 __be16 newlen = htons(skb->len - skb_network_offset(skb));
1436
1437 csum_replace2(&iph->check, iph->tot_len, newlen);
1438 iph->tot_len = newlen;
1439
1440 rcu_read_lock();
1441 ops = rcu_dereference(inet_protos[proto]);
1442 if (WARN_ON(!ops || !ops->gro_complete))
1443 goto out_unlock;
1444
1445 err = ops->gro_complete(skb);
1446
1447out_unlock:
1448 rcu_read_unlock();
1449
1450 return err;
1451}
1452
Denis V. Luneveee4fe42008-04-03 14:27:58 -07001453int inet_ctl_sock_create(struct sock **sk, unsigned short family,
Denis V. Lunev56772422008-04-03 14:28:30 -07001454 unsigned short type, unsigned char protocol,
1455 struct net *net)
Denis V. Lunev3d58b5f2008-04-03 14:22:32 -07001456{
Denis V. Luneveee4fe42008-04-03 14:27:58 -07001457 struct socket *sock;
1458 int rc = sock_create_kern(family, type, protocol, &sock);
Denis V. Lunev3d58b5f2008-04-03 14:22:32 -07001459
1460 if (rc == 0) {
Denis V. Luneveee4fe42008-04-03 14:27:58 -07001461 *sk = sock->sk;
1462 (*sk)->sk_allocation = GFP_ATOMIC;
Denis V. Lunev3d58b5f2008-04-03 14:22:32 -07001463 /*
1464 * Unhash it so that IP input processing does not even see it,
1465 * we do not wish this socket to see incoming packets.
1466 */
Denis V. Luneveee4fe42008-04-03 14:27:58 -07001467 (*sk)->sk_prot->unhash(*sk);
Denis V. Lunev56772422008-04-03 14:28:30 -07001468
1469 sk_change_net(*sk, net);
Denis V. Lunev3d58b5f2008-04-03 14:22:32 -07001470 }
1471 return rc;
1472}
Denis V. Lunev3d58b5f2008-04-03 14:22:32 -07001473EXPORT_SYMBOL_GPL(inet_ctl_sock_create);
1474
Tejun Heo7d720c32010-02-16 15:20:26 +00001475unsigned long snmp_fold_field(void __percpu *mib[], int offt)
Herbert Xu5e0f0432007-04-24 21:53:35 -07001476{
1477 unsigned long res = 0;
Eric Dumazet8f0ea0f2011-06-10 19:45:51 +00001478 int i, j;
Herbert Xu5e0f0432007-04-24 21:53:35 -07001479
1480 for_each_possible_cpu(i) {
Eric Dumazet8f0ea0f2011-06-10 19:45:51 +00001481 for (j = 0; j < SNMP_ARRAY_SZ; j++)
1482 res += *(((unsigned long *) per_cpu_ptr(mib[j], i)) + offt);
Herbert Xu5e0f0432007-04-24 21:53:35 -07001483 }
1484 return res;
1485}
1486EXPORT_SYMBOL_GPL(snmp_fold_field);
1487
Eric Dumazet4ce3c182010-06-30 13:31:19 -07001488#if BITS_PER_LONG==32
1489
1490u64 snmp_fold_field64(void __percpu *mib[], int offt, size_t syncp_offset)
1491{
1492 u64 res = 0;
1493 int cpu;
1494
1495 for_each_possible_cpu(cpu) {
Eric Dumazet8f0ea0f2011-06-10 19:45:51 +00001496 void *bhptr;
Eric Dumazet4ce3c182010-06-30 13:31:19 -07001497 struct u64_stats_sync *syncp;
Eric Dumazet8f0ea0f2011-06-10 19:45:51 +00001498 u64 v;
Eric Dumazet4ce3c182010-06-30 13:31:19 -07001499 unsigned int start;
1500
Eric Dumazet8f0ea0f2011-06-10 19:45:51 +00001501 bhptr = per_cpu_ptr(mib[0], cpu);
Eric Dumazet4ce3c182010-06-30 13:31:19 -07001502 syncp = (struct u64_stats_sync *)(bhptr + syncp_offset);
1503 do {
1504 start = u64_stats_fetch_begin_bh(syncp);
Eric Dumazet8f0ea0f2011-06-10 19:45:51 +00001505 v = *(((u64 *) bhptr) + offt);
Eric Dumazet4ce3c182010-06-30 13:31:19 -07001506 } while (u64_stats_fetch_retry_bh(syncp, start));
1507
Eric Dumazet8f0ea0f2011-06-10 19:45:51 +00001508 res += v;
Eric Dumazet4ce3c182010-06-30 13:31:19 -07001509 }
1510 return res;
1511}
1512EXPORT_SYMBOL_GPL(snmp_fold_field64);
1513#endif
1514
Eric Dumazet1823e4c82010-06-22 20:58:41 +00001515int snmp_mib_init(void __percpu *ptr[2], size_t mibsize, size_t align)
Herbert Xu5e0f0432007-04-24 21:53:35 -07001516{
1517 BUG_ON(ptr == NULL);
Eric Dumazet1823e4c82010-06-22 20:58:41 +00001518 ptr[0] = __alloc_percpu(mibsize, align);
Herbert Xu5e0f0432007-04-24 21:53:35 -07001519 if (!ptr[0])
Eric Dumazet8f0ea0f2011-06-10 19:45:51 +00001520 return -ENOMEM;
1521#if SNMP_ARRAY_SZ == 2
Eric Dumazet1823e4c82010-06-22 20:58:41 +00001522 ptr[1] = __alloc_percpu(mibsize, align);
Eric Dumazet8f0ea0f2011-06-10 19:45:51 +00001523 if (!ptr[1]) {
1524 free_percpu(ptr[0]);
1525 ptr[0] = NULL;
1526 return -ENOMEM;
1527 }
1528#endif
Herbert Xu5e0f0432007-04-24 21:53:35 -07001529 return 0;
Herbert Xu5e0f0432007-04-24 21:53:35 -07001530}
1531EXPORT_SYMBOL_GPL(snmp_mib_init);
1532
Eric Dumazet8f0ea0f2011-06-10 19:45:51 +00001533void snmp_mib_free(void __percpu *ptr[SNMP_ARRAY_SZ])
Herbert Xu5e0f0432007-04-24 21:53:35 -07001534{
Eric Dumazet8f0ea0f2011-06-10 19:45:51 +00001535 int i;
1536
Herbert Xu5e0f0432007-04-24 21:53:35 -07001537 BUG_ON(ptr == NULL);
Eric Dumazet8f0ea0f2011-06-10 19:45:51 +00001538 for (i = 0; i < SNMP_ARRAY_SZ; i++) {
1539 free_percpu(ptr[i]);
1540 ptr[i] = NULL;
1541 }
Herbert Xu5e0f0432007-04-24 21:53:35 -07001542}
1543EXPORT_SYMBOL_GPL(snmp_mib_free);
1544
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545#ifdef CONFIG_IP_MULTICAST
Alexey Dobriyan32613092009-09-14 12:21:47 +00001546static const struct net_protocol igmp_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 .handler = igmp_rcv,
Alexey Dobriyanb4ee07d2008-12-25 16:42:23 -08001548 .netns_ok = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549};
1550#endif
1551
Alexey Dobriyan32613092009-09-14 12:21:47 +00001552static const struct net_protocol tcp_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 .handler = tcp_v4_rcv,
1554 .err_handler = tcp_v4_err,
Herbert Xua430a432006-07-08 13:34:56 -07001555 .gso_send_check = tcp_v4_gso_send_check,
Herbert Xuf4c50d92006-06-22 03:02:40 -07001556 .gso_segment = tcp_tso_segment,
Herbert Xubf296b12008-12-15 23:43:36 -08001557 .gro_receive = tcp4_gro_receive,
1558 .gro_complete = tcp4_gro_complete,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 .no_policy = 1,
Denis V. Lunev92f1fec2008-03-24 15:34:06 -07001560 .netns_ok = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561};
1562
Alexey Dobriyan32613092009-09-14 12:21:47 +00001563static const struct net_protocol udp_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 .handler = udp_rcv,
1565 .err_handler = udp_err,
Sridhar Samudralad7ca4cc2009-07-09 08:09:47 +00001566 .gso_send_check = udp4_ufo_send_check,
1567 .gso_segment = udp4_ufo_fragment,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 .no_policy = 1,
Denis V. Lunev92f1fec2008-03-24 15:34:06 -07001569 .netns_ok = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570};
1571
Alexey Dobriyan32613092009-09-14 12:21:47 +00001572static const struct net_protocol icmp_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 .handler = icmp_rcv,
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001574 .err_handler = ping_err,
Herbert Xu8b7817f2007-12-12 10:44:43 -08001575 .no_policy = 1,
Denis V. Lunev92f1fec2008-03-24 15:34:06 -07001576 .netns_ok = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577};
1578
Pavel Emelyanov9b4661b2008-07-18 04:01:44 -07001579static __net_init int ipv4_mib_init_net(struct net *net)
1580{
Tejun Heo7d720c32010-02-16 15:20:26 +00001581 if (snmp_mib_init((void __percpu **)net->mib.tcp_statistics,
Eric Dumazet1823e4c82010-06-22 20:58:41 +00001582 sizeof(struct tcp_mib),
1583 __alignof__(struct tcp_mib)) < 0)
Pavel Emelyanov57ef42d2008-07-18 04:02:08 -07001584 goto err_tcp_mib;
Tejun Heo7d720c32010-02-16 15:20:26 +00001585 if (snmp_mib_init((void __percpu **)net->mib.ip_statistics,
Eric Dumazet1823e4c82010-06-22 20:58:41 +00001586 sizeof(struct ipstats_mib),
1587 __alignof__(struct ipstats_mib)) < 0)
Pavel Emelyanova20f5792008-07-18 04:02:42 -07001588 goto err_ip_mib;
Tejun Heo7d720c32010-02-16 15:20:26 +00001589 if (snmp_mib_init((void __percpu **)net->mib.net_statistics,
Eric Dumazet1823e4c82010-06-22 20:58:41 +00001590 sizeof(struct linux_mib),
1591 __alignof__(struct linux_mib)) < 0)
Pavel Emelyanov61a7e262008-07-18 04:03:08 -07001592 goto err_net_mib;
Tejun Heo7d720c32010-02-16 15:20:26 +00001593 if (snmp_mib_init((void __percpu **)net->mib.udp_statistics,
Eric Dumazet1823e4c82010-06-22 20:58:41 +00001594 sizeof(struct udp_mib),
1595 __alignof__(struct udp_mib)) < 0)
Pavel Emelyanov2f275f92008-07-18 04:03:27 -07001596 goto err_udp_mib;
Tejun Heo7d720c32010-02-16 15:20:26 +00001597 if (snmp_mib_init((void __percpu **)net->mib.udplite_statistics,
Eric Dumazet1823e4c82010-06-22 20:58:41 +00001598 sizeof(struct udp_mib),
1599 __alignof__(struct udp_mib)) < 0)
Pavel Emelyanov386019d2008-07-18 04:03:45 -07001600 goto err_udplite_mib;
Tejun Heo7d720c32010-02-16 15:20:26 +00001601 if (snmp_mib_init((void __percpu **)net->mib.icmp_statistics,
Eric Dumazet1823e4c82010-06-22 20:58:41 +00001602 sizeof(struct icmp_mib),
1603 __alignof__(struct icmp_mib)) < 0)
Pavel Emelyanovb60538a2008-07-18 04:04:02 -07001604 goto err_icmp_mib;
Eric Dumazetacb32ba2011-11-08 13:04:43 +00001605 net->mib.icmpmsg_statistics = kzalloc(sizeof(struct icmpmsg_mib),
1606 GFP_KERNEL);
1607 if (!net->mib.icmpmsg_statistics)
Pavel Emelyanov923c6582008-07-18 04:04:22 -07001608 goto err_icmpmsg_mib;
Pavel Emelyanov57ef42d2008-07-18 04:02:08 -07001609
1610 tcp_mib_init(net);
Pavel Emelyanov9b4661b2008-07-18 04:01:44 -07001611 return 0;
Pavel Emelyanov57ef42d2008-07-18 04:02:08 -07001612
Pavel Emelyanov923c6582008-07-18 04:04:22 -07001613err_icmpmsg_mib:
Tejun Heo7d720c32010-02-16 15:20:26 +00001614 snmp_mib_free((void __percpu **)net->mib.icmp_statistics);
Pavel Emelyanovb60538a2008-07-18 04:04:02 -07001615err_icmp_mib:
Tejun Heo7d720c32010-02-16 15:20:26 +00001616 snmp_mib_free((void __percpu **)net->mib.udplite_statistics);
Pavel Emelyanov386019d2008-07-18 04:03:45 -07001617err_udplite_mib:
Tejun Heo7d720c32010-02-16 15:20:26 +00001618 snmp_mib_free((void __percpu **)net->mib.udp_statistics);
Pavel Emelyanov2f275f92008-07-18 04:03:27 -07001619err_udp_mib:
Tejun Heo7d720c32010-02-16 15:20:26 +00001620 snmp_mib_free((void __percpu **)net->mib.net_statistics);
Pavel Emelyanov61a7e262008-07-18 04:03:08 -07001621err_net_mib:
Tejun Heo7d720c32010-02-16 15:20:26 +00001622 snmp_mib_free((void __percpu **)net->mib.ip_statistics);
Pavel Emelyanova20f5792008-07-18 04:02:42 -07001623err_ip_mib:
Tejun Heo7d720c32010-02-16 15:20:26 +00001624 snmp_mib_free((void __percpu **)net->mib.tcp_statistics);
Pavel Emelyanov57ef42d2008-07-18 04:02:08 -07001625err_tcp_mib:
1626 return -ENOMEM;
Pavel Emelyanov9b4661b2008-07-18 04:01:44 -07001627}
1628
1629static __net_exit void ipv4_mib_exit_net(struct net *net)
1630{
Eric Dumazetacb32ba2011-11-08 13:04:43 +00001631 kfree(net->mib.icmpmsg_statistics);
Tejun Heo7d720c32010-02-16 15:20:26 +00001632 snmp_mib_free((void __percpu **)net->mib.icmp_statistics);
1633 snmp_mib_free((void __percpu **)net->mib.udplite_statistics);
1634 snmp_mib_free((void __percpu **)net->mib.udp_statistics);
1635 snmp_mib_free((void __percpu **)net->mib.net_statistics);
1636 snmp_mib_free((void __percpu **)net->mib.ip_statistics);
1637 snmp_mib_free((void __percpu **)net->mib.tcp_statistics);
Pavel Emelyanov9b4661b2008-07-18 04:01:44 -07001638}
1639
1640static __net_initdata struct pernet_operations ipv4_mib_ops = {
1641 .init = ipv4_mib_init_net,
1642 .exit = ipv4_mib_exit_net,
1643};
1644
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645static int __init init_ipv4_mibs(void)
1646{
Pavel Emelyanovd89cbbb2008-07-18 04:04:51 -07001647 return register_pernet_subsys(&ipv4_mib_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648}
1649
1650static int ipv4_proc_init(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
Herbert Xu30e224d2005-07-05 14:40:10 -07001652/*
1653 * IP protocol layer initialiser
1654 */
1655
Stephen Hemminger7546dd92009-03-09 08:18:29 +00001656static struct packet_type ip_packet_type __read_mostly = {
Harvey Harrison09640e62009-02-01 00:45:17 -08001657 .type = cpu_to_be16(ETH_P_IP),
Herbert Xu30e224d2005-07-05 14:40:10 -07001658 .func = ip_rcv,
Herbert Xua430a432006-07-08 13:34:56 -07001659 .gso_send_check = inet_gso_send_check,
Herbert Xuf4c50d92006-06-22 03:02:40 -07001660 .gso_segment = inet_gso_segment,
Herbert Xu73cc19f2008-12-15 23:41:09 -08001661 .gro_receive = inet_gro_receive,
1662 .gro_complete = inet_gro_complete,
Herbert Xu30e224d2005-07-05 14:40:10 -07001663};
1664
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665static int __init inet_init(void)
1666{
1667 struct sk_buff *dummy_skb;
1668 struct inet_protosw *q;
1669 struct list_head *r;
1670 int rc = -EINVAL;
1671
YOSHIFUJI Hideakief047f52006-09-01 00:29:06 -07001672 BUILD_BUG_ON(sizeof(struct inet_skb_parm) > sizeof(dummy_skb->cb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
Amerigo Wange3826f12010-05-05 00:27:06 +00001674 sysctl_local_reserved_ports = kzalloc(65536 / 8, GFP_KERNEL);
1675 if (!sysctl_local_reserved_ports)
1676 goto out;
1677
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 rc = proto_register(&tcp_prot, 1);
1679 if (rc)
Amerigo Wange3826f12010-05-05 00:27:06 +00001680 goto out_free_reserved_ports;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
1682 rc = proto_register(&udp_prot, 1);
1683 if (rc)
1684 goto out_unregister_tcp_proto;
1685
1686 rc = proto_register(&raw_prot, 1);
1687 if (rc)
1688 goto out_unregister_udp_proto;
1689
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001690 rc = proto_register(&ping_prot, 1);
1691 if (rc)
1692 goto out_unregister_raw_proto;
1693
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 /*
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001695 * Tell SOCKET that we are alive...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 */
1697
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001698 (void)sock_register(&inet_family_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
Al Virobd7b1532008-07-15 16:00:59 -04001700#ifdef CONFIG_SYSCTL
1701 ip_static_sysctl_init();
1702#endif
1703
Glauber Costa3dc43e32011-12-11 21:47:05 +00001704 tcp_prot.sysctl_mem = init_net.ipv4.sysctl_tcp_mem;
1705
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 /*
1707 * Add all the base protocols.
1708 */
1709
1710 if (inet_add_protocol(&icmp_protocol, IPPROTO_ICMP) < 0)
Joe Perches058bd4d2012-03-11 18:36:11 +00001711 pr_crit("%s: Cannot add ICMP protocol\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 if (inet_add_protocol(&udp_protocol, IPPROTO_UDP) < 0)
Joe Perches058bd4d2012-03-11 18:36:11 +00001713 pr_crit("%s: Cannot add UDP protocol\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 if (inet_add_protocol(&tcp_protocol, IPPROTO_TCP) < 0)
Joe Perches058bd4d2012-03-11 18:36:11 +00001715 pr_crit("%s: Cannot add TCP protocol\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716#ifdef CONFIG_IP_MULTICAST
1717 if (inet_add_protocol(&igmp_protocol, IPPROTO_IGMP) < 0)
Joe Perches058bd4d2012-03-11 18:36:11 +00001718 pr_crit("%s: Cannot add IGMP protocol\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719#endif
1720
1721 /* Register the socket-side information for inet_create. */
1722 for (r = &inetsw[0]; r < &inetsw[SOCK_MAX]; ++r)
1723 INIT_LIST_HEAD(r);
1724
1725 for (q = inetsw_array; q < &inetsw_array[INETSW_ARRAY_LEN]; ++q)
1726 inet_register_protosw(q);
1727
1728 /*
1729 * Set the ARP module up
1730 */
1731
1732 arp_init();
1733
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001734 /*
1735 * Set the IP module up
1736 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
1738 ip_init();
1739
Denis V. Lunev9b0f9762008-02-29 11:13:15 -08001740 tcp_v4_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
1742 /* Setup TCP slab cache for open requests. */
1743 tcp_init();
1744
Hideo Aoki95766ff2007-12-31 00:29:24 -08001745 /* Setup UDP memory threshold */
1746 udp_init();
1747
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001748 /* Add UDP-Lite (RFC 3828) */
1749 udplite4_register();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001751 ping_init();
1752
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 /*
1754 * Set the ICMP layer up
1755 */
1756
Denis V. Luneva5710d62008-02-29 11:14:50 -08001757 if (icmp_init() < 0)
1758 panic("Failed to create the ICMP control socket.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
1760 /*
1761 * Initialise the multicast router
1762 */
1763#if defined(CONFIG_IP_MROUTE)
Wang Chen03d2f892008-07-03 12:13:36 +08001764 if (ip_mr_init())
Joe Perches058bd4d2012-03-11 18:36:11 +00001765 pr_crit("%s: Cannot init ipv4 mroute\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766#endif
1767 /*
1768 * Initialise per-cpu ipv4 mibs
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001769 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
Stephen Hemminger132adf52007-03-08 20:44:43 -08001771 if (init_ipv4_mibs())
Joe Perches058bd4d2012-03-11 18:36:11 +00001772 pr_crit("%s: Cannot init ipv4 mibs\n", __func__);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001773
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 ipv4_proc_init();
1775
1776 ipfrag_init();
1777
Herbert Xu30e224d2005-07-05 14:40:10 -07001778 dev_add_pack(&ip_packet_type);
1779
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 rc = 0;
1781out:
1782 return rc;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001783out_unregister_raw_proto:
1784 proto_unregister(&raw_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785out_unregister_udp_proto:
1786 proto_unregister(&udp_prot);
Olaf Kirch321efff2006-09-27 16:33:45 -07001787out_unregister_tcp_proto:
1788 proto_unregister(&tcp_prot);
Amerigo Wange3826f12010-05-05 00:27:06 +00001789out_free_reserved_ports:
1790 kfree(sysctl_local_reserved_ports);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 goto out;
1792}
1793
Heiko Carstensa536e072006-04-28 15:19:17 -07001794fs_initcall(inet_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
1796/* ------------------------------------------------------------------------ */
1797
1798#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799static int __init ipv4_proc_init(void)
1800{
1801 int rc = 0;
1802
1803 if (raw_proc_init())
1804 goto out_raw;
1805 if (tcp4_proc_init())
1806 goto out_tcp;
1807 if (udp4_proc_init())
1808 goto out_udp;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001809 if (ping_proc_init())
1810 goto out_ping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 if (ip_misc_proc_init())
1812 goto out_misc;
1813out:
1814 return rc;
1815out_misc:
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001816 ping_proc_exit();
1817out_ping:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 udp4_proc_exit();
1819out_udp:
1820 tcp4_proc_exit();
1821out_tcp:
1822 raw_proc_exit();
1823out_raw:
1824 rc = -ENOMEM;
1825 goto out;
1826}
1827
1828#else /* CONFIG_PROC_FS */
1829static int __init ipv4_proc_init(void)
1830{
1831 return 0;
1832}
1833#endif /* CONFIG_PROC_FS */
1834
1835MODULE_ALIAS_NETPROTO(PF_INET);
1836