blob: de5ec03036f16edb0faba9335c7397798af427ac [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>
Chia-chi Yehc543f112009-06-30 11:23:04 +0800124
125static inline int current_has_network(void)
126{
127 return in_egroup_p(AID_INET) || capable(CAP_NET_RAW);
128}
129#else
130static inline int current_has_network(void)
131{
132 return 1;
133}
Robert Love20386922008-10-15 15:35:44 -0400134#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136/* The inetsw table contains everything that inet_create needs to
137 * build a new socket.
138 */
139static struct list_head inetsw[SOCK_MAX];
140static DEFINE_SPINLOCK(inetsw_lock);
141
Pavel Emelyanov9ba63972007-12-05 01:38:23 -0800142struct ipv4_config ipv4_config;
Pavel Emelyanov9ba63972007-12-05 01:38:23 -0800143EXPORT_SYMBOL(ipv4_config);
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145/* New destruction routine */
146
147void inet_sock_destruct(struct sock *sk)
148{
149 struct inet_sock *inet = inet_sk(sk);
150
151 __skb_queue_purge(&sk->sk_receive_queue);
152 __skb_queue_purge(&sk->sk_error_queue);
153
Hideo Aoki95766ff2007-12-31 00:29:24 -0800154 sk_mem_reclaim(sk);
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 if (sk->sk_type == SOCK_STREAM && sk->sk_state != TCP_CLOSE) {
Eric Dumazet3d1427f2009-08-28 23:45:21 -0700157 pr_err("Attempt to release TCP socket in state %d %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 sk->sk_state, sk);
159 return;
160 }
161 if (!sock_flag(sk, SOCK_DEAD)) {
Eric Dumazet3d1427f2009-08-28 23:45:21 -0700162 pr_err("Attempt to release alive inet socket %p\n", sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 return;
164 }
165
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700166 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
167 WARN_ON(atomic_read(&sk->sk_wmem_alloc));
168 WARN_ON(sk->sk_wmem_queued);
169 WARN_ON(sk->sk_forward_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000171 kfree(rcu_dereference_protected(inet->inet_opt, 1));
Eric Dumazetb6c67122010-04-08 23:03:29 +0000172 dst_release(rcu_dereference_check(sk->sk_dst_cache, 1));
Arnaldo Carvalho de Meloe6848972005-08-09 19:45:38 -0700173 sk_refcnt_debug_dec(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174}
Eric Dumazet3d1427f2009-08-28 23:45:21 -0700175EXPORT_SYMBOL(inet_sock_destruct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177/*
178 * The routines beyond this point handle the behaviour of an AF_INET
179 * socket object. Mostly it punts to the subprotocols of IP to do
180 * the work.
181 */
182
183/*
184 * Automatically bind an unbound socket.
185 */
186
187static int inet_autobind(struct sock *sk)
188{
189 struct inet_sock *inet;
190 /* We may need to bind the socket. */
191 lock_sock(sk);
192 inet = inet_sk(sk);
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000193 if (!inet->inet_num) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 if (sk->sk_prot->get_port(sk, 0)) {
195 release_sock(sk);
196 return -EAGAIN;
197 }
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000198 inet->inet_sport = htons(inet->inet_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 }
200 release_sock(sk);
201 return 0;
202}
203
204/*
205 * Move a socket into listening state.
206 */
207int inet_listen(struct socket *sock, int backlog)
208{
209 struct sock *sk = sock->sk;
210 unsigned char old_state;
211 int err;
212
213 lock_sock(sk);
214
215 err = -EINVAL;
216 if (sock->state != SS_UNCONNECTED || sock->type != SOCK_STREAM)
217 goto out;
218
219 old_state = sk->sk_state;
220 if (!((1 << old_state) & (TCPF_CLOSE | TCPF_LISTEN)))
221 goto out;
222
223 /* Really, if the socket is already in listen state
224 * we can only allow the backlog to be adjusted.
225 */
226 if (old_state != TCP_LISTEN) {
Eric Dumazet72a3eff2006-11-16 02:30:37 -0800227 err = inet_csk_listen_start(sk, backlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 if (err)
229 goto out;
230 }
231 sk->sk_max_ack_backlog = backlog;
232 err = 0;
233
234out:
235 release_sock(sk);
236 return err;
237}
Eric Dumazet3d1427f2009-08-28 23:45:21 -0700238EXPORT_SYMBOL(inet_listen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Eric Dumazetbe776282007-03-27 13:53:04 -0700240u32 inet_ehash_secret __read_mostly;
David S. Millerb3da2cf2007-03-23 11:40:27 -0700241EXPORT_SYMBOL(inet_ehash_secret);
242
Eric Dumazete5a096a2013-02-21 12:18:52 +0000243u32 ipv6_hash_secret __read_mostly;
244EXPORT_SYMBOL(ipv6_hash_secret);
245
Eric Dumazetbe776282007-03-27 13:53:04 -0700246/*
Eric Dumazete5a096a2013-02-21 12:18:52 +0000247 * inet_ehash_secret must be set exactly once, and to a non nul value
248 * ipv6_hash_secret must be set exactly once.
Eric Dumazetbe776282007-03-27 13:53:04 -0700249 */
David S. Millerb3da2cf2007-03-23 11:40:27 -0700250void build_ehash_secret(void)
251{
Eric Dumazetbe776282007-03-27 13:53:04 -0700252 u32 rnd;
Eric Dumazet49e8ab02010-08-19 06:10:45 +0000253
Eric Dumazetbe776282007-03-27 13:53:04 -0700254 do {
255 get_random_bytes(&rnd, sizeof(rnd));
256 } while (rnd == 0);
Eric Dumazet49e8ab02010-08-19 06:10:45 +0000257
Eric Dumazete5a096a2013-02-21 12:18:52 +0000258 if (cmpxchg(&inet_ehash_secret, 0, rnd) == 0)
259 get_random_bytes(&ipv6_hash_secret, sizeof(ipv6_hash_secret));
David S. Millerb3da2cf2007-03-23 11:40:27 -0700260}
261EXPORT_SYMBOL(build_ehash_secret);
262
Denis V. Lunev2342fd72008-03-24 15:33:42 -0700263static inline int inet_netns_ok(struct net *net, int protocol)
264{
265 int hash;
Alexey Dobriyan32613092009-09-14 12:21:47 +0000266 const struct net_protocol *ipprot;
Denis V. Lunev2342fd72008-03-24 15:33:42 -0700267
Eric Dumazet04f258c2008-11-23 15:42:23 -0800268 if (net_eq(net, &init_net))
Denis V. Lunev2342fd72008-03-24 15:33:42 -0700269 return 1;
270
271 hash = protocol & (MAX_INET_PROTOS - 1);
272 ipprot = rcu_dereference(inet_protos[hash]);
273
274 if (ipprot == NULL)
275 /* raw IP is OK */
276 return 1;
277 return ipprot->netns_ok;
278}
279
Robert Love20386922008-10-15 15:35:44 -0400280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281/*
282 * Create an inet socket.
283 */
284
Eric Paris3f378b62009-11-05 22:18:14 -0800285static int inet_create(struct net *net, struct socket *sock, int protocol,
286 int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
288 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 struct inet_protosw *answer;
290 struct inet_sock *inet;
291 struct proto *answer_prot;
292 unsigned char answer_flags;
293 char answer_no_check;
Arnaldo Carvalho de Melobb97d312005-08-09 20:19:14 -0700294 int try_loading_module = 0;
Herbert Xu86c8f9d2005-12-02 20:43:26 -0800295 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Robert Love20386922008-10-15 15:35:44 -0400297 if (!current_has_network())
298 return -EACCES;
299
Eric Dumazet04f258c2008-11-23 15:42:23 -0800300 if (unlikely(!inet_ehash_secret))
301 if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
302 build_ehash_secret();
David S. Millerb3da2cf2007-03-23 11:40:27 -0700303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 sock->state = SS_UNCONNECTED;
305
306 /* Look for the requested type/protocol pair. */
Arnaldo Carvalho de Melobb97d312005-08-09 20:19:14 -0700307lookup_protocol:
Herbert Xu86c8f9d2005-12-02 20:43:26 -0800308 err = -ESOCKTNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 rcu_read_lock();
Paul E. McKenney696adfe2008-07-25 01:45:34 -0700310 list_for_each_entry_rcu(answer, &inetsw[sock->type], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Paul E. McKenney696adfe2008-07-25 01:45:34 -0700312 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 /* Check the non-wild match. */
314 if (protocol == answer->protocol) {
315 if (protocol != IPPROTO_IP)
316 break;
317 } else {
318 /* Check for the two wild cases. */
319 if (IPPROTO_IP == protocol) {
320 protocol = answer->protocol;
321 break;
322 }
323 if (IPPROTO_IP == answer->protocol)
324 break;
325 }
Herbert Xu86c8f9d2005-12-02 20:43:26 -0800326 err = -EPROTONOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 }
328
Paul E. McKenney696adfe2008-07-25 01:45:34 -0700329 if (unlikely(err)) {
Arnaldo Carvalho de Melobb97d312005-08-09 20:19:14 -0700330 if (try_loading_module < 2) {
331 rcu_read_unlock();
332 /*
333 * Be more specific, e.g. net-pf-2-proto-132-type-1
334 * (net-pf-PF_INET-proto-IPPROTO_SCTP-type-SOCK_STREAM)
335 */
336 if (++try_loading_module == 1)
337 request_module("net-pf-%d-proto-%d-type-%d",
338 PF_INET, protocol, sock->type);
339 /*
340 * Fall back to generic, e.g. net-pf-2-proto-132
341 * (net-pf-PF_INET-proto-IPPROTO_SCTP)
342 */
343 else
344 request_module("net-pf-%d-proto-%d",
345 PF_INET, protocol);
346 goto lookup_protocol;
347 } else
348 goto out_rcu_unlock;
349 }
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 err = -EPERM;
Chia-chi Yehc543f112009-06-30 11:23:04 +0800352 if (sock->type == SOCK_RAW && !kern && !capable(CAP_NET_RAW))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 goto out_rcu_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Denis V. Lunev2342fd72008-03-24 15:33:42 -0700355 err = -EAFNOSUPPORT;
356 if (!inet_netns_ok(net, protocol))
357 goto out_rcu_unlock;
358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 sock->ops = answer->ops;
360 answer_prot = answer->prot;
361 answer_no_check = answer->no_check;
362 answer_flags = answer->flags;
363 rcu_read_unlock();
364
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700365 WARN_ON(answer_prot->slab == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 err = -ENOBUFS;
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700368 sk = sk_alloc(net, PF_INET, GFP_KERNEL, answer_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 if (sk == NULL)
370 goto out;
371
372 err = 0;
373 sk->sk_no_check = answer_no_check;
374 if (INET_PROTOSW_REUSE & answer_flags)
375 sk->sk_reuse = 1;
376
377 inet = inet_sk(sk);
Paul Moore469de9b2007-01-09 14:37:06 -0800378 inet->is_icsk = (INET_PROTOSW_ICSK & answer_flags) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Jiri Olsa7b2ff182010-06-15 01:07:31 +0000380 inet->nodefrag = 0;
381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 if (SOCK_RAW == sock->type) {
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000383 inet->inet_num = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 if (IPPROTO_RAW == protocol)
385 inet->hdrincl = 1;
386 }
387
388 if (ipv4_config.no_pmtu_disc)
389 inet->pmtudisc = IP_PMTUDISC_DONT;
390 else
391 inet->pmtudisc = IP_PMTUDISC_WANT;
392
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000393 inet->inet_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 sock_init_data(sock, sk);
396
397 sk->sk_destruct = inet_sock_destruct;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 sk->sk_protocol = protocol;
399 sk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
400
401 inet->uc_ttl = -1;
402 inet->mc_loop = 1;
403 inet->mc_ttl = 1;
Nivedita Singhvif771bef2009-05-28 07:00:46 +0000404 inet->mc_all = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 inet->mc_index = 0;
406 inet->mc_list = NULL;
Jiri Benc4c507d22012-02-09 09:35:49 +0000407 inet->rcv_tos = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Arnaldo Carvalho de Meloe6848972005-08-09 19:45:38 -0700409 sk_refcnt_debug_inc(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000411 if (inet->inet_num) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 /* It assumes that any protocol which allows
413 * the user to assign a number at socket
414 * creation time automatically
415 * shares.
416 */
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000417 inet->inet_sport = htons(inet->inet_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 /* Add to protocol hash chains. */
419 sk->sk_prot->hash(sk);
420 }
421
422 if (sk->sk_prot->init) {
423 err = sk->sk_prot->init(sk);
424 if (err)
425 sk_common_release(sk);
426 }
427out:
428 return err;
429out_rcu_unlock:
430 rcu_read_unlock();
431 goto out;
432}
433
434
435/*
436 * The peer socket should always be NULL (or else). When we call this
437 * function we are destroying the object and from then on nobody
438 * should refer to it.
439 */
440int inet_release(struct socket *sock)
441{
442 struct sock *sk = sock->sk;
443
444 if (sk) {
445 long timeout;
446
David S. Millerc58dc012010-04-27 15:05:31 -0700447 sock_rps_reset_flow(sk);
Tom Herbertfec5e652010-04-16 16:01:27 -0700448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 /* Applications forget to leave groups before exiting */
450 ip_mc_drop_socket(sk);
451
452 /* If linger is set, we don't return until the close
453 * is complete. Otherwise we return immediately. The
454 * actually closing is done the same either way.
455 *
456 * If the close is due to the process exiting, we never
457 * linger..
458 */
459 timeout = 0;
460 if (sock_flag(sk, SOCK_LINGER) &&
461 !(current->flags & PF_EXITING))
462 timeout = sk->sk_lingertime;
463 sock->sk = NULL;
464 sk->sk_prot->close(sk, timeout);
465 }
466 return 0;
467}
Eric Dumazet3d1427f2009-08-28 23:45:21 -0700468EXPORT_SYMBOL(inet_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
470/* It is off by default, see below. */
Brian Haleyab32ea52006-09-22 14:15:41 -0700471int sysctl_ip_nonlocal_bind __read_mostly;
Eric Dumazet3d1427f2009-08-28 23:45:21 -0700472EXPORT_SYMBOL(sysctl_ip_nonlocal_bind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
474int inet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
475{
476 struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
477 struct sock *sk = sock->sk;
478 struct inet_sock *inet = inet_sk(sk);
479 unsigned short snum;
480 int chk_addr_ret;
481 int err;
482
483 /* If the socket has its own bind function then use it. (RAW) */
484 if (sk->sk_prot->bind) {
485 err = sk->sk_prot->bind(sk, uaddr, addr_len);
486 goto out;
487 }
488 err = -EINVAL;
489 if (addr_len < sizeof(struct sockaddr_in))
490 goto out;
491
Marcus Meissnerc349a522011-07-04 01:30:29 +0000492 if (addr->sin_family != AF_INET) {
Eric Dumazet29c486d2011-08-30 18:57:00 -0400493 /* Compatibility games : accept AF_UNSPEC (mapped to AF_INET)
494 * only if s_addr is INADDR_ANY.
495 */
Marcus Meissnerc349a522011-07-04 01:30:29 +0000496 err = -EAFNOSUPPORT;
Eric Dumazet29c486d2011-08-30 18:57:00 -0400497 if (addr->sin_family != AF_UNSPEC ||
498 addr->sin_addr.s_addr != htonl(INADDR_ANY))
499 goto out;
Marcus Meissnerc349a522011-07-04 01:30:29 +0000500 }
Marcus Meissnerd0733d22011-06-01 21:05:22 -0700501
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900502 chk_addr_ret = inet_addr_type(sock_net(sk), addr->sin_addr.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
504 /* Not specified by any standard per-se, however it breaks too
505 * many applications when removed. It is unfortunate since
506 * allowing applications to make a non-local bind solves
507 * several problems with systems using dynamic addressing.
508 * (ie. your servers still start up even if your ISDN link
509 * is temporarily down)
510 */
511 err = -EADDRNOTAVAIL;
512 if (!sysctl_ip_nonlocal_bind &&
Tóth László Attilab9fb1502008-10-01 07:31:24 -0700513 !(inet->freebind || inet->transparent) &&
Al Viroe6f1ceb2008-03-17 22:44:53 -0700514 addr->sin_addr.s_addr != htonl(INADDR_ANY) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 chk_addr_ret != RTN_LOCAL &&
516 chk_addr_ret != RTN_MULTICAST &&
517 chk_addr_ret != RTN_BROADCAST)
518 goto out;
519
520 snum = ntohs(addr->sin_port);
521 err = -EACCES;
522 if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
523 goto out;
524
525 /* We keep a pair of addresses. rcv_saddr is the one
526 * used by hash lookups, and saddr is used for transmit.
527 *
528 * In the BSD API these are the same except where it
529 * would be illegal to use them (multicast/broadcast) in
530 * which case the sending device address is used.
531 */
532 lock_sock(sk);
533
534 /* Check these errors (active socket, double bind). */
535 err = -EINVAL;
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000536 if (sk->sk_state != TCP_CLOSE || inet->inet_num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 goto out_release_sock;
538
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000539 inet->inet_rcv_saddr = inet->inet_saddr = addr->sin_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 if (chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST)
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000541 inet->inet_saddr = 0; /* Use device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543 /* Make sure we are allowed to bind here. */
544 if (sk->sk_prot->get_port(sk, snum)) {
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000545 inet->inet_saddr = inet->inet_rcv_saddr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 err = -EADDRINUSE;
547 goto out_release_sock;
548 }
549
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000550 if (inet->inet_rcv_saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
552 if (snum)
553 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000554 inet->inet_sport = htons(inet->inet_num);
555 inet->inet_daddr = 0;
556 inet->inet_dport = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 sk_dst_reset(sk);
558 err = 0;
559out_release_sock:
560 release_sock(sk);
561out:
562 return err;
563}
Eric Dumazet3d1427f2009-08-28 23:45:21 -0700564EXPORT_SYMBOL(inet_bind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566int inet_dgram_connect(struct socket *sock, struct sockaddr * uaddr,
567 int addr_len, int flags)
568{
569 struct sock *sk = sock->sk;
570
Changli Gao6503d962010-03-31 22:58:26 +0000571 if (addr_len < sizeof(uaddr->sa_family))
572 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 if (uaddr->sa_family == AF_UNSPEC)
574 return sk->sk_prot->disconnect(sk, flags);
575
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000576 if (!inet_sk(sk)->inet_num && inet_autobind(sk))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 return -EAGAIN;
578 return sk->sk_prot->connect(sk, (struct sockaddr *)uaddr, addr_len);
579}
Eric Dumazet3d1427f2009-08-28 23:45:21 -0700580EXPORT_SYMBOL(inet_dgram_connect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582static long inet_wait_for_connect(struct sock *sk, long timeo)
583{
584 DEFINE_WAIT(wait);
585
Eric Dumazetaa395142010-04-20 13:03:51 +0000586 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 /* Basic assumption: if someone sets sk->sk_err, he _must_
589 * change state of the socket from TCP_SYN_*.
590 * Connect() does not allow to get error notifications
591 * without closing the socket.
592 */
593 while ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
594 release_sock(sk);
595 timeo = schedule_timeout(timeo);
596 lock_sock(sk);
597 if (signal_pending(current) || !timeo)
598 break;
Eric Dumazetaa395142010-04-20 13:03:51 +0000599 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 }
Eric Dumazetaa395142010-04-20 13:03:51 +0000601 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 return timeo;
603}
604
605/*
606 * Connect to a remote host. There is regrettably still a little
607 * TCP 'magic' in here.
608 */
609int inet_stream_connect(struct socket *sock, struct sockaddr *uaddr,
610 int addr_len, int flags)
611{
612 struct sock *sk = sock->sk;
613 int err;
614 long timeo;
615
Changli Gao6503d962010-03-31 22:58:26 +0000616 if (addr_len < sizeof(uaddr->sa_family))
617 return -EINVAL;
618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 lock_sock(sk);
620
621 if (uaddr->sa_family == AF_UNSPEC) {
622 err = sk->sk_prot->disconnect(sk, flags);
623 sock->state = err ? SS_DISCONNECTING : SS_UNCONNECTED;
624 goto out;
625 }
626
627 switch (sock->state) {
628 default:
629 err = -EINVAL;
630 goto out;
631 case SS_CONNECTED:
632 err = -EISCONN;
633 goto out;
634 case SS_CONNECTING:
635 err = -EALREADY;
636 /* Fall out of switch with err, set for this state */
637 break;
638 case SS_UNCONNECTED:
639 err = -EISCONN;
640 if (sk->sk_state != TCP_CLOSE)
641 goto out;
642
643 err = sk->sk_prot->connect(sk, uaddr, addr_len);
644 if (err < 0)
645 goto out;
646
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900647 sock->state = SS_CONNECTING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
649 /* Just entered SS_CONNECTING state; the only
650 * difference is that return value in non-blocking
651 * case is EINPROGRESS, rather than EALREADY.
652 */
653 err = -EINPROGRESS;
654 break;
655 }
656
657 timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
658
659 if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
660 /* Error code is set above */
661 if (!timeo || !inet_wait_for_connect(sk, timeo))
662 goto out;
663
664 err = sock_intr_errno(timeo);
665 if (signal_pending(current))
666 goto out;
667 }
668
669 /* Connection was closed by RST, timeout, ICMP error
670 * or another process disconnected us.
671 */
672 if (sk->sk_state == TCP_CLOSE)
673 goto sock_error;
674
675 /* sk->sk_err may be not zero now, if RECVERR was ordered by user
676 * and error was received after socket entered established state.
677 * Hence, it is handled normally after connect() return successfully.
678 */
679
680 sock->state = SS_CONNECTED;
681 err = 0;
682out:
683 release_sock(sk);
684 return err;
685
686sock_error:
687 err = sock_error(sk) ? : -ECONNABORTED;
688 sock->state = SS_UNCONNECTED;
689 if (sk->sk_prot->disconnect(sk, flags))
690 sock->state = SS_DISCONNECTING;
691 goto out;
692}
Eric Dumazet3d1427f2009-08-28 23:45:21 -0700693EXPORT_SYMBOL(inet_stream_connect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695/*
696 * Accept a pending connection. The TCP layer now gives BSD semantics.
697 */
698
699int inet_accept(struct socket *sock, struct socket *newsock, int flags)
700{
701 struct sock *sk1 = sock->sk;
702 int err = -EINVAL;
703 struct sock *sk2 = sk1->sk_prot->accept(sk1, flags, &err);
704
705 if (!sk2)
706 goto do_err;
707
708 lock_sock(sk2);
709
Eric Dumazet1eddcea2011-06-17 03:45:15 +0000710 sock_rps_record_flow(sk2);
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700711 WARN_ON(!((1 << sk2->sk_state) &
712 (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT | TCPF_CLOSE)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714 sock_graft(sk2, newsock);
715
716 newsock->state = SS_CONNECTED;
717 err = 0;
718 release_sock(sk2);
719do_err:
720 return err;
721}
Eric Dumazet3d1427f2009-08-28 23:45:21 -0700722EXPORT_SYMBOL(inet_accept);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
724
725/*
726 * This does both peername and sockname.
727 */
728int inet_getname(struct socket *sock, struct sockaddr *uaddr,
729 int *uaddr_len, int peer)
730{
731 struct sock *sk = sock->sk;
732 struct inet_sock *inet = inet_sk(sk);
Cyrill Gorcunov38bfd8f2009-10-29 02:59:18 -0700733 DECLARE_SOCKADDR(struct sockaddr_in *, sin, uaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
735 sin->sin_family = AF_INET;
736 if (peer) {
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000737 if (!inet->inet_dport ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
739 peer == 1))
740 return -ENOTCONN;
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000741 sin->sin_port = inet->inet_dport;
742 sin->sin_addr.s_addr = inet->inet_daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 } else {
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000744 __be32 addr = inet->inet_rcv_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 if (!addr)
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000746 addr = inet->inet_saddr;
747 sin->sin_port = inet->inet_sport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 sin->sin_addr.s_addr = addr;
749 }
750 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
751 *uaddr_len = sizeof(*sin);
752 return 0;
753}
Eric Dumazet3d1427f2009-08-28 23:45:21 -0700754EXPORT_SYMBOL(inet_getname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
756int inet_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg,
757 size_t size)
758{
759 struct sock *sk = sock->sk;
760
David S. Millerc58dc012010-04-27 15:05:31 -0700761 sock_rps_record_flow(sk);
Tom Herbertfec5e652010-04-16 16:01:27 -0700762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 /* We may need to bind the socket. */
Changli Gao7ba42912010-07-10 20:41:55 +0000764 if (!inet_sk(sk)->inet_num && !sk->sk_prot->no_autobind &&
765 inet_autobind(sk))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 return -EAGAIN;
767
768 return sk->sk_prot->sendmsg(iocb, sk, msg, size);
769}
Eric Dumazet3d1427f2009-08-28 23:45:21 -0700770EXPORT_SYMBOL(inet_sendmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Changli Gao7ba42912010-07-10 20:41:55 +0000772ssize_t inet_sendpage(struct socket *sock, struct page *page, int offset,
773 size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
775 struct sock *sk = sock->sk;
776
David S. Millerc58dc012010-04-27 15:05:31 -0700777 sock_rps_record_flow(sk);
Tom Herbertfec5e652010-04-16 16:01:27 -0700778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 /* We may need to bind the socket. */
Changli Gao7ba42912010-07-10 20:41:55 +0000780 if (!inet_sk(sk)->inet_num && !sk->sk_prot->no_autobind &&
781 inet_autobind(sk))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 return -EAGAIN;
783
784 if (sk->sk_prot->sendpage)
785 return sk->sk_prot->sendpage(sk, page, offset, size, flags);
786 return sock_no_sendpage(sock, page, offset, size, flags);
787}
Changli Gao7ba42912010-07-10 20:41:55 +0000788EXPORT_SYMBOL(inet_sendpage);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Tom Herbertfec5e652010-04-16 16:01:27 -0700790int inet_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg,
791 size_t size, int flags)
792{
793 struct sock *sk = sock->sk;
794 int addr_len = 0;
795 int err;
796
David S. Millerc58dc012010-04-27 15:05:31 -0700797 sock_rps_record_flow(sk);
Tom Herbertfec5e652010-04-16 16:01:27 -0700798
799 err = sk->sk_prot->recvmsg(iocb, sk, msg, size, flags & MSG_DONTWAIT,
800 flags & ~MSG_DONTWAIT, &addr_len);
801 if (err >= 0)
802 msg->msg_namelen = addr_len;
803 return err;
804}
805EXPORT_SYMBOL(inet_recvmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
807int inet_shutdown(struct socket *sock, int how)
808{
809 struct sock *sk = sock->sk;
810 int err = 0;
811
812 /* This should really check to make sure
813 * the socket is a TCP socket. (WHY AC...)
814 */
815 how++; /* maps 0->1 has the advantage of making bit 1 rcvs and
816 1->2 bit 2 snds.
817 2->3 */
818 if ((how & ~SHUTDOWN_MASK) || !how) /* MAXINT->0 */
819 return -EINVAL;
820
821 lock_sock(sk);
822 if (sock->state == SS_CONNECTING) {
823 if ((1 << sk->sk_state) &
824 (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_CLOSE))
825 sock->state = SS_DISCONNECTING;
826 else
827 sock->state = SS_CONNECTED;
828 }
829
830 switch (sk->sk_state) {
831 case TCP_CLOSE:
832 err = -ENOTCONN;
833 /* Hack to wake up other listeners, who can poll for
834 POLLHUP, even on eg. unconnected UDP sockets -- RR */
835 default:
836 sk->sk_shutdown |= how;
837 if (sk->sk_prot->shutdown)
838 sk->sk_prot->shutdown(sk, how);
839 break;
840
841 /* Remaining two branches are temporary solution for missing
842 * close() in multithreaded environment. It is _not_ a good idea,
843 * but we have no choice until close() is repaired at VFS level.
844 */
845 case TCP_LISTEN:
846 if (!(how & RCV_SHUTDOWN))
847 break;
848 /* Fall through */
849 case TCP_SYN_SENT:
850 err = sk->sk_prot->disconnect(sk, O_NONBLOCK);
851 sock->state = err ? SS_DISCONNECTING : SS_UNCONNECTED;
852 break;
853 }
854
855 /* Wake up anyone sleeping in poll. */
856 sk->sk_state_change(sk);
857 release_sock(sk);
858 return err;
859}
Eric Dumazet3d1427f2009-08-28 23:45:21 -0700860EXPORT_SYMBOL(inet_shutdown);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
862/*
863 * ioctl() calls you can issue on an INET socket. Most of these are
864 * device configuration and stuff and very rarely used. Some ioctls
865 * pass on to the socket itself.
866 *
867 * NOTE: I like the idea of a module for the config stuff. ie ifconfig
868 * loads the devconfigure module does its configuring and unloads it.
869 * There's a good 20K of config code hanging around the kernel.
870 */
871
872int inet_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
873{
874 struct sock *sk = sock->sk;
875 int err = 0;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900876 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
878 switch (cmd) {
Eric Dumazet3d1427f2009-08-28 23:45:21 -0700879 case SIOCGSTAMP:
880 err = sock_get_timestamp(sk, (struct timeval __user *)arg);
881 break;
882 case SIOCGSTAMPNS:
883 err = sock_get_timestampns(sk, (struct timespec __user *)arg);
884 break;
885 case SIOCADDRT:
886 case SIOCDELRT:
887 case SIOCRTMSG:
888 err = ip_rt_ioctl(net, cmd, (void __user *)arg);
889 break;
890 case SIOCDARP:
891 case SIOCGARP:
892 case SIOCSARP:
893 err = arp_ioctl(net, cmd, (void __user *)arg);
894 break;
895 case SIOCGIFADDR:
896 case SIOCSIFADDR:
897 case SIOCGIFBRDADDR:
898 case SIOCSIFBRDADDR:
899 case SIOCGIFNETMASK:
900 case SIOCSIFNETMASK:
901 case SIOCGIFDSTADDR:
902 case SIOCSIFDSTADDR:
903 case SIOCSIFPFLAGS:
904 case SIOCGIFPFLAGS:
905 case SIOCSIFFLAGS:
Robert Love2365d052008-05-12 17:08:29 -0400906 case SIOCKILLADDR:
Eric Dumazet3d1427f2009-08-28 23:45:21 -0700907 err = devinet_ioctl(net, cmd, (void __user *)arg);
908 break;
909 default:
910 if (sk->sk_prot->ioctl)
911 err = sk->sk_prot->ioctl(sk, cmd, arg);
912 else
913 err = -ENOIOCTLCMD;
914 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 }
916 return err;
917}
Eric Dumazet3d1427f2009-08-28 23:45:21 -0700918EXPORT_SYMBOL(inet_ioctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Eric W. Biederman709b46e2011-01-29 16:15:56 +0000920#ifdef CONFIG_COMPAT
Gerrit Renker686dc6b2011-10-15 09:26:56 +0000921static int inet_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
Eric W. Biederman709b46e2011-01-29 16:15:56 +0000922{
923 struct sock *sk = sock->sk;
924 int err = -ENOIOCTLCMD;
925
926 if (sk->sk_prot->compat_ioctl)
927 err = sk->sk_prot->compat_ioctl(sk, cmd, arg);
928
929 return err;
930}
931#endif
932
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800933const struct proto_ops inet_stream_ops = {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800934 .family = PF_INET,
935 .owner = THIS_MODULE,
936 .release = inet_release,
937 .bind = inet_bind,
938 .connect = inet_stream_connect,
939 .socketpair = sock_no_socketpair,
940 .accept = inet_accept,
941 .getname = inet_getname,
942 .poll = tcp_poll,
943 .ioctl = inet_ioctl,
944 .listen = inet_listen,
945 .shutdown = inet_shutdown,
946 .setsockopt = sock_common_setsockopt,
947 .getsockopt = sock_common_getsockopt,
Changli Gao7ba42912010-07-10 20:41:55 +0000948 .sendmsg = inet_sendmsg,
Tom Herbertfec5e652010-04-16 16:01:27 -0700949 .recvmsg = inet_recvmsg,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800950 .mmap = sock_no_mmap,
Changli Gao7ba42912010-07-10 20:41:55 +0000951 .sendpage = inet_sendpage,
Jens Axboe9c55e012007-11-06 23:30:13 -0800952 .splice_read = tcp_splice_read,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800953#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800954 .compat_setsockopt = compat_sock_common_setsockopt,
955 .compat_getsockopt = compat_sock_common_getsockopt,
Eric W. Biederman709b46e2011-01-29 16:15:56 +0000956 .compat_ioctl = inet_compat_ioctl,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800957#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958};
Eric Dumazet3d1427f2009-08-28 23:45:21 -0700959EXPORT_SYMBOL(inet_stream_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800961const struct proto_ops inet_dgram_ops = {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800962 .family = PF_INET,
963 .owner = THIS_MODULE,
964 .release = inet_release,
965 .bind = inet_bind,
966 .connect = inet_dgram_connect,
967 .socketpair = sock_no_socketpair,
968 .accept = sock_no_accept,
969 .getname = inet_getname,
970 .poll = udp_poll,
971 .ioctl = inet_ioctl,
972 .listen = sock_no_listen,
973 .shutdown = inet_shutdown,
974 .setsockopt = sock_common_setsockopt,
975 .getsockopt = sock_common_getsockopt,
976 .sendmsg = inet_sendmsg,
Tom Herbertfec5e652010-04-16 16:01:27 -0700977 .recvmsg = inet_recvmsg,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800978 .mmap = sock_no_mmap,
979 .sendpage = inet_sendpage,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800980#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800981 .compat_setsockopt = compat_sock_common_setsockopt,
982 .compat_getsockopt = compat_sock_common_getsockopt,
Eric W. Biederman709b46e2011-01-29 16:15:56 +0000983 .compat_ioctl = inet_compat_ioctl,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800984#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985};
Eric Dumazet3d1427f2009-08-28 23:45:21 -0700986EXPORT_SYMBOL(inet_dgram_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
988/*
989 * For SOCK_RAW sockets; should be the same as inet_dgram_ops but without
990 * udp_poll
991 */
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800992static const struct proto_ops inet_sockraw_ops = {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800993 .family = PF_INET,
994 .owner = THIS_MODULE,
995 .release = inet_release,
996 .bind = inet_bind,
997 .connect = inet_dgram_connect,
998 .socketpair = sock_no_socketpair,
999 .accept = sock_no_accept,
1000 .getname = inet_getname,
1001 .poll = datagram_poll,
1002 .ioctl = inet_ioctl,
1003 .listen = sock_no_listen,
1004 .shutdown = inet_shutdown,
1005 .setsockopt = sock_common_setsockopt,
1006 .getsockopt = sock_common_getsockopt,
1007 .sendmsg = inet_sendmsg,
Tom Herbertfec5e652010-04-16 16:01:27 -07001008 .recvmsg = inet_recvmsg,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001009 .mmap = sock_no_mmap,
1010 .sendpage = inet_sendpage,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001011#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001012 .compat_setsockopt = compat_sock_common_setsockopt,
1013 .compat_getsockopt = compat_sock_common_getsockopt,
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001014 .compat_ioctl = inet_compat_ioctl,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001015#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016};
1017
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00001018static const struct net_proto_family inet_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 .family = PF_INET,
1020 .create = inet_create,
1021 .owner = THIS_MODULE,
1022};
1023
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024/* Upon startup we insert all the elements in inetsw_array[] into
1025 * the linked list inetsw.
1026 */
1027static struct inet_protosw inetsw_array[] =
1028{
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001029 {
1030 .type = SOCK_STREAM,
1031 .protocol = IPPROTO_TCP,
1032 .prot = &tcp_prot,
1033 .ops = &inet_stream_ops,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001034 .no_check = 0,
1035 .flags = INET_PROTOSW_PERMANENT |
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -08001036 INET_PROTOSW_ICSK,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001037 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001039 {
1040 .type = SOCK_DGRAM,
1041 .protocol = IPPROTO_UDP,
1042 .prot = &udp_prot,
1043 .ops = &inet_dgram_ops,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001044 .no_check = UDP_CSUM_DEFAULT,
1045 .flags = INET_PROTOSW_PERMANENT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 },
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001047
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001048 {
1049 .type = SOCK_DGRAM,
1050 .protocol = IPPROTO_ICMP,
1051 .prot = &ping_prot,
1052 .ops = &inet_dgram_ops,
1053 .no_check = UDP_CSUM_DEFAULT,
1054 .flags = INET_PROTOSW_REUSE,
1055 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
1057 {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001058 .type = SOCK_RAW,
1059 .protocol = IPPROTO_IP, /* wild card */
1060 .prot = &raw_prot,
1061 .ops = &inet_sockraw_ops,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001062 .no_check = UDP_CSUM_DEFAULT,
1063 .flags = INET_PROTOSW_REUSE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 }
1065};
1066
Denis Chengc40f6ff2007-09-16 16:39:25 -07001067#define INETSW_ARRAY_LEN ARRAY_SIZE(inetsw_array)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
1069void inet_register_protosw(struct inet_protosw *p)
1070{
1071 struct list_head *lh;
1072 struct inet_protosw *answer;
1073 int protocol = p->protocol;
1074 struct list_head *last_perm;
1075
1076 spin_lock_bh(&inetsw_lock);
1077
1078 if (p->type >= SOCK_MAX)
1079 goto out_illegal;
1080
1081 /* If we are trying to override a permanent protocol, bail. */
1082 answer = NULL;
1083 last_perm = &inetsw[p->type];
1084 list_for_each(lh, &inetsw[p->type]) {
1085 answer = list_entry(lh, struct inet_protosw, list);
1086
1087 /* Check only the non-wild match. */
1088 if (INET_PROTOSW_PERMANENT & answer->flags) {
1089 if (protocol == answer->protocol)
1090 break;
1091 last_perm = lh;
1092 }
1093
1094 answer = NULL;
1095 }
1096 if (answer)
1097 goto out_permanent;
1098
1099 /* Add the new entry after the last permanent entry if any, so that
1100 * the new entry does not override a permanent entry when matched with
1101 * a wild-card protocol. But it is allowed to override any existing
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001102 * non-permanent entry. This means that when we remove this entry, the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 * system automatically returns to the old behavior.
1104 */
1105 list_add_rcu(&p->list, last_perm);
1106out:
1107 spin_unlock_bh(&inetsw_lock);
1108
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 return;
1110
1111out_permanent:
Joe Perches058bd4d2012-03-11 18:36:11 +00001112 pr_err("Attempt to override permanent protocol %d\n", protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 goto out;
1114
1115out_illegal:
Joe Perches058bd4d2012-03-11 18:36:11 +00001116 pr_err("Ignoring attempt to register invalid socket type %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 p->type);
1118 goto out;
1119}
Eric Dumazet3d1427f2009-08-28 23:45:21 -07001120EXPORT_SYMBOL(inet_register_protosw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
1122void inet_unregister_protosw(struct inet_protosw *p)
1123{
1124 if (INET_PROTOSW_PERMANENT & p->flags) {
Joe Perches058bd4d2012-03-11 18:36:11 +00001125 pr_err("Attempt to unregister permanent protocol %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 p->protocol);
1127 } else {
1128 spin_lock_bh(&inetsw_lock);
1129 list_del_rcu(&p->list);
1130 spin_unlock_bh(&inetsw_lock);
1131
1132 synchronize_net();
1133 }
1134}
Eric Dumazet3d1427f2009-08-28 23:45:21 -07001135EXPORT_SYMBOL(inet_unregister_protosw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Arnaldo Carvalho de Melo32519f12005-08-09 19:50:02 -07001137/*
1138 * Shall we try to damage output packets if routing dev changes?
1139 */
1140
Brian Haleyab32ea52006-09-22 14:15:41 -07001141int sysctl_ip_dynaddr __read_mostly;
Arnaldo Carvalho de Melo32519f12005-08-09 19:50:02 -07001142
1143static int inet_sk_reselect_saddr(struct sock *sk)
1144{
1145 struct inet_sock *inet = inet_sk(sk);
Eric Dumazetc720c7e2009-10-15 06:30:45 +00001146 __be32 old_saddr = inet->inet_saddr;
Eric Dumazetc720c7e2009-10-15 06:30:45 +00001147 __be32 daddr = inet->inet_daddr;
David S. Miller6e869132011-05-06 16:18:04 -07001148 struct flowi4 *fl4;
David S. Millerb23dd4f2011-03-02 14:31:35 -08001149 struct rtable *rt;
1150 __be32 new_saddr;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001151 struct ip_options_rcu *inet_opt;
Arnaldo Carvalho de Melo32519f12005-08-09 19:50:02 -07001152
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001153 inet_opt = rcu_dereference_protected(inet->inet_opt,
1154 sock_owned_by_user(sk));
1155 if (inet_opt && inet_opt->opt.srr)
1156 daddr = inet_opt->opt.faddr;
Arnaldo Carvalho de Melo32519f12005-08-09 19:50:02 -07001157
1158 /* Query new route. */
David S. Miller6e869132011-05-06 16:18:04 -07001159 fl4 = &inet->cork.fl.u.ip4;
1160 rt = ip_route_connect(fl4, daddr, 0, RT_CONN_FLAGS(sk),
David S. Millerb23dd4f2011-03-02 14:31:35 -08001161 sk->sk_bound_dev_if, sk->sk_protocol,
1162 inet->inet_sport, inet->inet_dport, sk, false);
1163 if (IS_ERR(rt))
1164 return PTR_ERR(rt);
Arnaldo Carvalho de Melo32519f12005-08-09 19:50:02 -07001165
Changli Gaod8d1f302010-06-10 23:31:35 -07001166 sk_setup_caps(sk, &rt->dst);
Arnaldo Carvalho de Melo32519f12005-08-09 19:50:02 -07001167
David S. Miller6e869132011-05-06 16:18:04 -07001168 new_saddr = fl4->saddr;
Arnaldo Carvalho de Melo32519f12005-08-09 19:50:02 -07001169
1170 if (new_saddr == old_saddr)
1171 return 0;
1172
1173 if (sysctl_ip_dynaddr > 1) {
Joe Perches058bd4d2012-03-11 18:36:11 +00001174 pr_info("%s(): shifting inet->saddr from %pI4 to %pI4\n",
1175 __func__, &old_saddr, &new_saddr);
Arnaldo Carvalho de Melo32519f12005-08-09 19:50:02 -07001176 }
1177
Eric Dumazetc720c7e2009-10-15 06:30:45 +00001178 inet->inet_saddr = inet->inet_rcv_saddr = new_saddr;
Arnaldo Carvalho de Melo32519f12005-08-09 19:50:02 -07001179
1180 /*
1181 * XXX The only one ugly spot where we need to
1182 * XXX really change the sockets identity after
1183 * XXX it has entered the hashes. -DaveM
1184 *
1185 * Besides that, it does not check for connection
1186 * uniqueness. Wait for troubles.
1187 */
1188 __sk_prot_rehash(sk);
1189 return 0;
1190}
1191
1192int inet_sk_rebuild_header(struct sock *sk)
1193{
1194 struct inet_sock *inet = inet_sk(sk);
1195 struct rtable *rt = (struct rtable *)__sk_dst_check(sk, 0);
Al Viro3ca3c682006-09-27 18:28:07 -07001196 __be32 daddr;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001197 struct ip_options_rcu *inet_opt;
David S. Miller6e869132011-05-06 16:18:04 -07001198 struct flowi4 *fl4;
Arnaldo Carvalho de Melo32519f12005-08-09 19:50:02 -07001199 int err;
1200
1201 /* Route is OK, nothing to do. */
1202 if (rt)
1203 return 0;
1204
1205 /* Reroute. */
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001206 rcu_read_lock();
1207 inet_opt = rcu_dereference(inet->inet_opt);
Eric Dumazetc720c7e2009-10-15 06:30:45 +00001208 daddr = inet->inet_daddr;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001209 if (inet_opt && inet_opt->opt.srr)
1210 daddr = inet_opt->opt.faddr;
1211 rcu_read_unlock();
David S. Miller6e869132011-05-06 16:18:04 -07001212 fl4 = &inet->cork.fl.u.ip4;
1213 rt = ip_route_output_ports(sock_net(sk), fl4, sk, daddr, inet->inet_saddr,
David S. Miller78fbfd82011-03-12 00:00:52 -05001214 inet->inet_dport, inet->inet_sport,
1215 sk->sk_protocol, RT_CONN_FLAGS(sk),
1216 sk->sk_bound_dev_if);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001217 if (!IS_ERR(rt)) {
1218 err = 0;
Changli Gaod8d1f302010-06-10 23:31:35 -07001219 sk_setup_caps(sk, &rt->dst);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001220 } else {
1221 err = PTR_ERR(rt);
1222
Arnaldo Carvalho de Melo32519f12005-08-09 19:50:02 -07001223 /* Routing failed... */
1224 sk->sk_route_caps = 0;
1225 /*
1226 * Other protocols have to map its equivalent state to TCP_SYN_SENT.
1227 * DCCP maps its DCCP_REQUESTING state to TCP_SYN_SENT. -acme
1228 */
1229 if (!sysctl_ip_dynaddr ||
1230 sk->sk_state != TCP_SYN_SENT ||
1231 (sk->sk_userlocks & SOCK_BINDADDR_LOCK) ||
1232 (err = inet_sk_reselect_saddr(sk)) != 0)
1233 sk->sk_err_soft = -err;
1234 }
1235
1236 return err;
1237}
Arnaldo Carvalho de Melo32519f12005-08-09 19:50:02 -07001238EXPORT_SYMBOL(inet_sk_rebuild_header);
1239
Herbert Xua430a432006-07-08 13:34:56 -07001240static int inet_gso_send_check(struct sk_buff *skb)
1241{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001242 const struct iphdr *iph;
Alexey Dobriyan32613092009-09-14 12:21:47 +00001243 const struct net_protocol *ops;
Herbert Xua430a432006-07-08 13:34:56 -07001244 int proto;
1245 int ihl;
1246 int err = -EINVAL;
1247
1248 if (unlikely(!pskb_may_pull(skb, sizeof(*iph))))
1249 goto out;
1250
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001251 iph = ip_hdr(skb);
Herbert Xua430a432006-07-08 13:34:56 -07001252 ihl = iph->ihl * 4;
1253 if (ihl < sizeof(*iph))
1254 goto out;
1255
1256 if (unlikely(!pskb_may_pull(skb, ihl)))
1257 goto out;
1258
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03001259 __skb_pull(skb, ihl);
1260 skb_reset_transport_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001261 iph = ip_hdr(skb);
Herbert Xua430a432006-07-08 13:34:56 -07001262 proto = iph->protocol & (MAX_INET_PROTOS - 1);
1263 err = -EPROTONOSUPPORT;
1264
1265 rcu_read_lock();
1266 ops = rcu_dereference(inet_protos[proto]);
1267 if (likely(ops && ops->gso_send_check))
1268 err = ops->gso_send_check(skb);
1269 rcu_read_unlock();
1270
1271out:
1272 return err;
1273}
1274
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001275static struct sk_buff *inet_gso_segment(struct sk_buff *skb,
1276 netdev_features_t features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07001277{
1278 struct sk_buff *segs = ERR_PTR(-EINVAL);
1279 struct iphdr *iph;
Alexey Dobriyan32613092009-09-14 12:21:47 +00001280 const struct net_protocol *ops;
Herbert Xuf4c50d92006-06-22 03:02:40 -07001281 int proto;
1282 int ihl;
1283 int id;
Sridhar Samudralad7ca4cc2009-07-09 08:09:47 +00001284 unsigned int offset = 0;
Herbert Xuf4c50d92006-06-22 03:02:40 -07001285
Stephen Hemmingerd212f872007-06-27 00:47:37 -07001286 if (!(features & NETIF_F_V4_CSUM))
1287 features &= ~NETIF_F_SG;
1288
Herbert Xubbcf4672006-07-03 19:38:35 -07001289 if (unlikely(skb_shinfo(skb)->gso_type &
1290 ~(SKB_GSO_TCPV4 |
1291 SKB_GSO_UDP |
1292 SKB_GSO_DODGY |
1293 SKB_GSO_TCP_ECN |
1294 0)))
1295 goto out;
1296
1297 if (unlikely(!pskb_may_pull(skb, sizeof(*iph))))
Herbert Xuf4c50d92006-06-22 03:02:40 -07001298 goto out;
1299
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001300 iph = ip_hdr(skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07001301 ihl = iph->ihl * 4;
1302 if (ihl < sizeof(*iph))
1303 goto out;
1304
Herbert Xubbcf4672006-07-03 19:38:35 -07001305 if (unlikely(!pskb_may_pull(skb, ihl)))
Herbert Xuf4c50d92006-06-22 03:02:40 -07001306 goto out;
1307
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03001308 __skb_pull(skb, ihl);
1309 skb_reset_transport_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001310 iph = ip_hdr(skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07001311 id = ntohs(iph->id);
1312 proto = iph->protocol & (MAX_INET_PROTOS - 1);
1313 segs = ERR_PTR(-EPROTONOSUPPORT);
1314
1315 rcu_read_lock();
1316 ops = rcu_dereference(inet_protos[proto]);
Herbert Xubbcf4672006-07-03 19:38:35 -07001317 if (likely(ops && ops->gso_segment))
Herbert Xu576a30e2006-06-27 13:22:38 -07001318 segs = ops->gso_segment(skb, features);
Herbert Xuf4c50d92006-06-22 03:02:40 -07001319 rcu_read_unlock();
1320
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07001321 if (!segs || IS_ERR(segs))
Herbert Xuf4c50d92006-06-22 03:02:40 -07001322 goto out;
1323
1324 skb = segs;
1325 do {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001326 iph = ip_hdr(skb);
Sridhar Samudralad7ca4cc2009-07-09 08:09:47 +00001327 if (proto == IPPROTO_UDP) {
1328 iph->id = htons(id);
1329 iph->frag_off = htons(offset >> 3);
1330 if (skb->next != NULL)
1331 iph->frag_off |= htons(IP_MF);
1332 offset += (skb->len - skb->mac_len - iph->ihl * 4);
1333 } else
1334 iph->id = htons(id++);
Herbert Xuf4c50d92006-06-22 03:02:40 -07001335 iph->tot_len = htons(skb->len - skb->mac_len);
1336 iph->check = 0;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001337 iph->check = ip_fast_csum(skb_network_header(skb), iph->ihl);
Herbert Xuf4c50d92006-06-22 03:02:40 -07001338 } while ((skb = skb->next));
1339
1340out:
1341 return segs;
1342}
1343
Herbert Xu73cc19f2008-12-15 23:41:09 -08001344static struct sk_buff **inet_gro_receive(struct sk_buff **head,
1345 struct sk_buff *skb)
1346{
Alexey Dobriyan32613092009-09-14 12:21:47 +00001347 const struct net_protocol *ops;
Herbert Xu73cc19f2008-12-15 23:41:09 -08001348 struct sk_buff **pp = NULL;
1349 struct sk_buff *p;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001350 const struct iphdr *iph;
Herbert Xua5b1cf22009-05-26 18:50:28 +00001351 unsigned int hlen;
1352 unsigned int off;
Herbert Xu1075f3f2009-05-26 18:50:29 +00001353 unsigned int id;
Herbert Xu73cc19f2008-12-15 23:41:09 -08001354 int flush = 1;
1355 int proto;
Herbert Xu73cc19f2008-12-15 23:41:09 -08001356
Herbert Xua5b1cf22009-05-26 18:50:28 +00001357 off = skb_gro_offset(skb);
1358 hlen = off + sizeof(*iph);
1359 iph = skb_gro_header_fast(skb, off);
1360 if (skb_gro_header_hard(skb, hlen)) {
1361 iph = skb_gro_header_slow(skb, hlen, off);
1362 if (unlikely(!iph))
1363 goto out;
1364 }
Herbert Xu73cc19f2008-12-15 23:41:09 -08001365
Herbert Xu73cc19f2008-12-15 23:41:09 -08001366 proto = iph->protocol & (MAX_INET_PROTOS - 1);
1367
1368 rcu_read_lock();
1369 ops = rcu_dereference(inet_protos[proto]);
1370 if (!ops || !ops->gro_receive)
1371 goto out_unlock;
1372
Herbert Xua5ad24b2009-02-08 18:00:39 +00001373 if (*(u8 *)iph != 0x45)
Herbert Xu73cc19f2008-12-15 23:41:09 -08001374 goto out_unlock;
1375
1376 if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
1377 goto out_unlock;
1378
Eric Dumazet0eae88f2010-04-20 19:06:52 -07001379 id = ntohl(*(__be32 *)&iph->id);
1380 flush = (u16)((ntohl(*(__be32 *)iph) ^ skb_gro_len(skb)) | (id ^ IP_DF));
Herbert Xu1075f3f2009-05-26 18:50:29 +00001381 id >>= 16;
Herbert Xu73cc19f2008-12-15 23:41:09 -08001382
1383 for (p = *head; p; p = p->next) {
1384 struct iphdr *iph2;
1385
1386 if (!NAPI_GRO_CB(p)->same_flow)
1387 continue;
1388
1389 iph2 = ip_hdr(p);
1390
Herbert Xua5ad24b2009-02-08 18:00:39 +00001391 if ((iph->protocol ^ iph2->protocol) |
1392 (iph->tos ^ iph2->tos) |
Eric Dumazet0eae88f2010-04-20 19:06:52 -07001393 ((__force u32)iph->saddr ^ (__force u32)iph2->saddr) |
1394 ((__force u32)iph->daddr ^ (__force u32)iph2->daddr)) {
Herbert Xu73cc19f2008-12-15 23:41:09 -08001395 NAPI_GRO_CB(p)->same_flow = 0;
1396 continue;
1397 }
1398
1399 /* All fields must match except length and checksum. */
1400 NAPI_GRO_CB(p)->flush |=
Herbert Xua5ad24b2009-02-08 18:00:39 +00001401 (iph->ttl ^ iph2->ttl) |
1402 ((u16)(ntohs(iph2->id) + NAPI_GRO_CB(p)->count) ^ id);
Herbert Xu73cc19f2008-12-15 23:41:09 -08001403
1404 NAPI_GRO_CB(p)->flush |= flush;
1405 }
1406
1407 NAPI_GRO_CB(skb)->flush |= flush;
Herbert Xu86911732009-01-29 14:19:50 +00001408 skb_gro_pull(skb, sizeof(*iph));
1409 skb_set_transport_header(skb, skb_gro_offset(skb));
Herbert Xu73cc19f2008-12-15 23:41:09 -08001410
1411 pp = ops->gro_receive(head, skb);
1412
1413out_unlock:
1414 rcu_read_unlock();
1415
1416out:
1417 NAPI_GRO_CB(skb)->flush |= flush;
1418
1419 return pp;
1420}
1421
1422static int inet_gro_complete(struct sk_buff *skb)
1423{
Alexey Dobriyan32613092009-09-14 12:21:47 +00001424 const struct net_protocol *ops;
Herbert Xu73cc19f2008-12-15 23:41:09 -08001425 struct iphdr *iph = ip_hdr(skb);
1426 int proto = iph->protocol & (MAX_INET_PROTOS - 1);
1427 int err = -ENOSYS;
1428 __be16 newlen = htons(skb->len - skb_network_offset(skb));
1429
1430 csum_replace2(&iph->check, iph->tot_len, newlen);
1431 iph->tot_len = newlen;
1432
1433 rcu_read_lock();
1434 ops = rcu_dereference(inet_protos[proto]);
1435 if (WARN_ON(!ops || !ops->gro_complete))
1436 goto out_unlock;
1437
1438 err = ops->gro_complete(skb);
1439
1440out_unlock:
1441 rcu_read_unlock();
1442
1443 return err;
1444}
1445
Denis V. Luneveee4fe42008-04-03 14:27:58 -07001446int inet_ctl_sock_create(struct sock **sk, unsigned short family,
Denis V. Lunev56772422008-04-03 14:28:30 -07001447 unsigned short type, unsigned char protocol,
1448 struct net *net)
Denis V. Lunev3d58b5f2008-04-03 14:22:32 -07001449{
Denis V. Luneveee4fe42008-04-03 14:27:58 -07001450 struct socket *sock;
1451 int rc = sock_create_kern(family, type, protocol, &sock);
Denis V. Lunev3d58b5f2008-04-03 14:22:32 -07001452
1453 if (rc == 0) {
Denis V. Luneveee4fe42008-04-03 14:27:58 -07001454 *sk = sock->sk;
1455 (*sk)->sk_allocation = GFP_ATOMIC;
Denis V. Lunev3d58b5f2008-04-03 14:22:32 -07001456 /*
1457 * Unhash it so that IP input processing does not even see it,
1458 * we do not wish this socket to see incoming packets.
1459 */
Denis V. Luneveee4fe42008-04-03 14:27:58 -07001460 (*sk)->sk_prot->unhash(*sk);
Denis V. Lunev56772422008-04-03 14:28:30 -07001461
1462 sk_change_net(*sk, net);
Denis V. Lunev3d58b5f2008-04-03 14:22:32 -07001463 }
1464 return rc;
1465}
Denis V. Lunev3d58b5f2008-04-03 14:22:32 -07001466EXPORT_SYMBOL_GPL(inet_ctl_sock_create);
1467
Tejun Heo7d720c32010-02-16 15:20:26 +00001468unsigned long snmp_fold_field(void __percpu *mib[], int offt)
Herbert Xu5e0f0432007-04-24 21:53:35 -07001469{
1470 unsigned long res = 0;
Eric Dumazet8f0ea0f2011-06-10 19:45:51 +00001471 int i, j;
Herbert Xu5e0f0432007-04-24 21:53:35 -07001472
1473 for_each_possible_cpu(i) {
Eric Dumazet8f0ea0f2011-06-10 19:45:51 +00001474 for (j = 0; j < SNMP_ARRAY_SZ; j++)
1475 res += *(((unsigned long *) per_cpu_ptr(mib[j], i)) + offt);
Herbert Xu5e0f0432007-04-24 21:53:35 -07001476 }
1477 return res;
1478}
1479EXPORT_SYMBOL_GPL(snmp_fold_field);
1480
Eric Dumazet4ce3c182010-06-30 13:31:19 -07001481#if BITS_PER_LONG==32
1482
1483u64 snmp_fold_field64(void __percpu *mib[], int offt, size_t syncp_offset)
1484{
1485 u64 res = 0;
1486 int cpu;
1487
1488 for_each_possible_cpu(cpu) {
Eric Dumazet8f0ea0f2011-06-10 19:45:51 +00001489 void *bhptr;
Eric Dumazet4ce3c182010-06-30 13:31:19 -07001490 struct u64_stats_sync *syncp;
Eric Dumazet8f0ea0f2011-06-10 19:45:51 +00001491 u64 v;
Eric Dumazet4ce3c182010-06-30 13:31:19 -07001492 unsigned int start;
1493
Eric Dumazet8f0ea0f2011-06-10 19:45:51 +00001494 bhptr = per_cpu_ptr(mib[0], cpu);
Eric Dumazet4ce3c182010-06-30 13:31:19 -07001495 syncp = (struct u64_stats_sync *)(bhptr + syncp_offset);
1496 do {
1497 start = u64_stats_fetch_begin_bh(syncp);
Eric Dumazet8f0ea0f2011-06-10 19:45:51 +00001498 v = *(((u64 *) bhptr) + offt);
Eric Dumazet4ce3c182010-06-30 13:31:19 -07001499 } while (u64_stats_fetch_retry_bh(syncp, start));
1500
Eric Dumazet8f0ea0f2011-06-10 19:45:51 +00001501 res += v;
Eric Dumazet4ce3c182010-06-30 13:31:19 -07001502 }
1503 return res;
1504}
1505EXPORT_SYMBOL_GPL(snmp_fold_field64);
1506#endif
1507
Eric Dumazet1823e4c82010-06-22 20:58:41 +00001508int snmp_mib_init(void __percpu *ptr[2], size_t mibsize, size_t align)
Herbert Xu5e0f0432007-04-24 21:53:35 -07001509{
1510 BUG_ON(ptr == NULL);
Eric Dumazet1823e4c82010-06-22 20:58:41 +00001511 ptr[0] = __alloc_percpu(mibsize, align);
Herbert Xu5e0f0432007-04-24 21:53:35 -07001512 if (!ptr[0])
Eric Dumazet8f0ea0f2011-06-10 19:45:51 +00001513 return -ENOMEM;
1514#if SNMP_ARRAY_SZ == 2
Eric Dumazet1823e4c82010-06-22 20:58:41 +00001515 ptr[1] = __alloc_percpu(mibsize, align);
Eric Dumazet8f0ea0f2011-06-10 19:45:51 +00001516 if (!ptr[1]) {
1517 free_percpu(ptr[0]);
1518 ptr[0] = NULL;
1519 return -ENOMEM;
1520 }
1521#endif
Herbert Xu5e0f0432007-04-24 21:53:35 -07001522 return 0;
Herbert Xu5e0f0432007-04-24 21:53:35 -07001523}
1524EXPORT_SYMBOL_GPL(snmp_mib_init);
1525
Eric Dumazet8f0ea0f2011-06-10 19:45:51 +00001526void snmp_mib_free(void __percpu *ptr[SNMP_ARRAY_SZ])
Herbert Xu5e0f0432007-04-24 21:53:35 -07001527{
Eric Dumazet8f0ea0f2011-06-10 19:45:51 +00001528 int i;
1529
Herbert Xu5e0f0432007-04-24 21:53:35 -07001530 BUG_ON(ptr == NULL);
Eric Dumazet8f0ea0f2011-06-10 19:45:51 +00001531 for (i = 0; i < SNMP_ARRAY_SZ; i++) {
1532 free_percpu(ptr[i]);
1533 ptr[i] = NULL;
1534 }
Herbert Xu5e0f0432007-04-24 21:53:35 -07001535}
1536EXPORT_SYMBOL_GPL(snmp_mib_free);
1537
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538#ifdef CONFIG_IP_MULTICAST
Alexey Dobriyan32613092009-09-14 12:21:47 +00001539static const struct net_protocol igmp_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 .handler = igmp_rcv,
Alexey Dobriyanb4ee07d2008-12-25 16:42:23 -08001541 .netns_ok = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542};
1543#endif
1544
Alexey Dobriyan32613092009-09-14 12:21:47 +00001545static const struct net_protocol tcp_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 .handler = tcp_v4_rcv,
1547 .err_handler = tcp_v4_err,
Herbert Xua430a432006-07-08 13:34:56 -07001548 .gso_send_check = tcp_v4_gso_send_check,
Herbert Xuf4c50d92006-06-22 03:02:40 -07001549 .gso_segment = tcp_tso_segment,
Herbert Xubf296b12008-12-15 23:43:36 -08001550 .gro_receive = tcp4_gro_receive,
1551 .gro_complete = tcp4_gro_complete,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 .no_policy = 1,
Denis V. Lunev92f1fec2008-03-24 15:34:06 -07001553 .netns_ok = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554};
1555
Alexey Dobriyan32613092009-09-14 12:21:47 +00001556static const struct net_protocol udp_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 .handler = udp_rcv,
1558 .err_handler = udp_err,
Sridhar Samudralad7ca4cc2009-07-09 08:09:47 +00001559 .gso_send_check = udp4_ufo_send_check,
1560 .gso_segment = udp4_ufo_fragment,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 .no_policy = 1,
Denis V. Lunev92f1fec2008-03-24 15:34:06 -07001562 .netns_ok = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563};
1564
Alexey Dobriyan32613092009-09-14 12:21:47 +00001565static const struct net_protocol icmp_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 .handler = icmp_rcv,
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001567 .err_handler = ping_err,
Herbert Xu8b7817f2007-12-12 10:44:43 -08001568 .no_policy = 1,
Denis V. Lunev92f1fec2008-03-24 15:34:06 -07001569 .netns_ok = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570};
1571
Pavel Emelyanov9b4661b2008-07-18 04:01:44 -07001572static __net_init int ipv4_mib_init_net(struct net *net)
1573{
Tejun Heo7d720c32010-02-16 15:20:26 +00001574 if (snmp_mib_init((void __percpu **)net->mib.tcp_statistics,
Eric Dumazet1823e4c82010-06-22 20:58:41 +00001575 sizeof(struct tcp_mib),
1576 __alignof__(struct tcp_mib)) < 0)
Pavel Emelyanov57ef42d2008-07-18 04:02:08 -07001577 goto err_tcp_mib;
Tejun Heo7d720c32010-02-16 15:20:26 +00001578 if (snmp_mib_init((void __percpu **)net->mib.ip_statistics,
Eric Dumazet1823e4c82010-06-22 20:58:41 +00001579 sizeof(struct ipstats_mib),
1580 __alignof__(struct ipstats_mib)) < 0)
Pavel Emelyanova20f5792008-07-18 04:02:42 -07001581 goto err_ip_mib;
Tejun Heo7d720c32010-02-16 15:20:26 +00001582 if (snmp_mib_init((void __percpu **)net->mib.net_statistics,
Eric Dumazet1823e4c82010-06-22 20:58:41 +00001583 sizeof(struct linux_mib),
1584 __alignof__(struct linux_mib)) < 0)
Pavel Emelyanov61a7e262008-07-18 04:03:08 -07001585 goto err_net_mib;
Tejun Heo7d720c32010-02-16 15:20:26 +00001586 if (snmp_mib_init((void __percpu **)net->mib.udp_statistics,
Eric Dumazet1823e4c82010-06-22 20:58:41 +00001587 sizeof(struct udp_mib),
1588 __alignof__(struct udp_mib)) < 0)
Pavel Emelyanov2f275f92008-07-18 04:03:27 -07001589 goto err_udp_mib;
Tejun Heo7d720c32010-02-16 15:20:26 +00001590 if (snmp_mib_init((void __percpu **)net->mib.udplite_statistics,
Eric Dumazet1823e4c82010-06-22 20:58:41 +00001591 sizeof(struct udp_mib),
1592 __alignof__(struct udp_mib)) < 0)
Pavel Emelyanov386019d2008-07-18 04:03:45 -07001593 goto err_udplite_mib;
Tejun Heo7d720c32010-02-16 15:20:26 +00001594 if (snmp_mib_init((void __percpu **)net->mib.icmp_statistics,
Eric Dumazet1823e4c82010-06-22 20:58:41 +00001595 sizeof(struct icmp_mib),
1596 __alignof__(struct icmp_mib)) < 0)
Pavel Emelyanovb60538a2008-07-18 04:04:02 -07001597 goto err_icmp_mib;
Eric Dumazetacb32ba2011-11-08 13:04:43 +00001598 net->mib.icmpmsg_statistics = kzalloc(sizeof(struct icmpmsg_mib),
1599 GFP_KERNEL);
1600 if (!net->mib.icmpmsg_statistics)
Pavel Emelyanov923c6582008-07-18 04:04:22 -07001601 goto err_icmpmsg_mib;
Pavel Emelyanov57ef42d2008-07-18 04:02:08 -07001602
1603 tcp_mib_init(net);
Pavel Emelyanov9b4661b2008-07-18 04:01:44 -07001604 return 0;
Pavel Emelyanov57ef42d2008-07-18 04:02:08 -07001605
Pavel Emelyanov923c6582008-07-18 04:04:22 -07001606err_icmpmsg_mib:
Tejun Heo7d720c32010-02-16 15:20:26 +00001607 snmp_mib_free((void __percpu **)net->mib.icmp_statistics);
Pavel Emelyanovb60538a2008-07-18 04:04:02 -07001608err_icmp_mib:
Tejun Heo7d720c32010-02-16 15:20:26 +00001609 snmp_mib_free((void __percpu **)net->mib.udplite_statistics);
Pavel Emelyanov386019d2008-07-18 04:03:45 -07001610err_udplite_mib:
Tejun Heo7d720c32010-02-16 15:20:26 +00001611 snmp_mib_free((void __percpu **)net->mib.udp_statistics);
Pavel Emelyanov2f275f92008-07-18 04:03:27 -07001612err_udp_mib:
Tejun Heo7d720c32010-02-16 15:20:26 +00001613 snmp_mib_free((void __percpu **)net->mib.net_statistics);
Pavel Emelyanov61a7e262008-07-18 04:03:08 -07001614err_net_mib:
Tejun Heo7d720c32010-02-16 15:20:26 +00001615 snmp_mib_free((void __percpu **)net->mib.ip_statistics);
Pavel Emelyanova20f5792008-07-18 04:02:42 -07001616err_ip_mib:
Tejun Heo7d720c32010-02-16 15:20:26 +00001617 snmp_mib_free((void __percpu **)net->mib.tcp_statistics);
Pavel Emelyanov57ef42d2008-07-18 04:02:08 -07001618err_tcp_mib:
1619 return -ENOMEM;
Pavel Emelyanov9b4661b2008-07-18 04:01:44 -07001620}
1621
1622static __net_exit void ipv4_mib_exit_net(struct net *net)
1623{
Eric Dumazetacb32ba2011-11-08 13:04:43 +00001624 kfree(net->mib.icmpmsg_statistics);
Tejun Heo7d720c32010-02-16 15:20:26 +00001625 snmp_mib_free((void __percpu **)net->mib.icmp_statistics);
1626 snmp_mib_free((void __percpu **)net->mib.udplite_statistics);
1627 snmp_mib_free((void __percpu **)net->mib.udp_statistics);
1628 snmp_mib_free((void __percpu **)net->mib.net_statistics);
1629 snmp_mib_free((void __percpu **)net->mib.ip_statistics);
1630 snmp_mib_free((void __percpu **)net->mib.tcp_statistics);
Pavel Emelyanov9b4661b2008-07-18 04:01:44 -07001631}
1632
1633static __net_initdata struct pernet_operations ipv4_mib_ops = {
1634 .init = ipv4_mib_init_net,
1635 .exit = ipv4_mib_exit_net,
1636};
1637
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638static int __init init_ipv4_mibs(void)
1639{
Pavel Emelyanovd89cbbb2008-07-18 04:04:51 -07001640 return register_pernet_subsys(&ipv4_mib_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641}
1642
1643static int ipv4_proc_init(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
Herbert Xu30e224d2005-07-05 14:40:10 -07001645/*
1646 * IP protocol layer initialiser
1647 */
1648
Stephen Hemminger7546dd92009-03-09 08:18:29 +00001649static struct packet_type ip_packet_type __read_mostly = {
Harvey Harrison09640e62009-02-01 00:45:17 -08001650 .type = cpu_to_be16(ETH_P_IP),
Herbert Xu30e224d2005-07-05 14:40:10 -07001651 .func = ip_rcv,
Herbert Xua430a432006-07-08 13:34:56 -07001652 .gso_send_check = inet_gso_send_check,
Herbert Xuf4c50d92006-06-22 03:02:40 -07001653 .gso_segment = inet_gso_segment,
Herbert Xu73cc19f2008-12-15 23:41:09 -08001654 .gro_receive = inet_gro_receive,
1655 .gro_complete = inet_gro_complete,
Herbert Xu30e224d2005-07-05 14:40:10 -07001656};
1657
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658static int __init inet_init(void)
1659{
1660 struct sk_buff *dummy_skb;
1661 struct inet_protosw *q;
1662 struct list_head *r;
1663 int rc = -EINVAL;
1664
YOSHIFUJI Hideakief047f52006-09-01 00:29:06 -07001665 BUILD_BUG_ON(sizeof(struct inet_skb_parm) > sizeof(dummy_skb->cb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
Amerigo Wange3826f12010-05-05 00:27:06 +00001667 sysctl_local_reserved_ports = kzalloc(65536 / 8, GFP_KERNEL);
1668 if (!sysctl_local_reserved_ports)
1669 goto out;
1670
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 rc = proto_register(&tcp_prot, 1);
1672 if (rc)
Amerigo Wange3826f12010-05-05 00:27:06 +00001673 goto out_free_reserved_ports;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
1675 rc = proto_register(&udp_prot, 1);
1676 if (rc)
1677 goto out_unregister_tcp_proto;
1678
1679 rc = proto_register(&raw_prot, 1);
1680 if (rc)
1681 goto out_unregister_udp_proto;
1682
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001683 rc = proto_register(&ping_prot, 1);
1684 if (rc)
1685 goto out_unregister_raw_proto;
1686
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 /*
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001688 * Tell SOCKET that we are alive...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 */
1690
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001691 (void)sock_register(&inet_family_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
Al Virobd7b1532008-07-15 16:00:59 -04001693#ifdef CONFIG_SYSCTL
1694 ip_static_sysctl_init();
1695#endif
1696
Glauber Costa3dc43e32011-12-11 21:47:05 +00001697 tcp_prot.sysctl_mem = init_net.ipv4.sysctl_tcp_mem;
1698
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 /*
1700 * Add all the base protocols.
1701 */
1702
1703 if (inet_add_protocol(&icmp_protocol, IPPROTO_ICMP) < 0)
Joe Perches058bd4d2012-03-11 18:36:11 +00001704 pr_crit("%s: Cannot add ICMP protocol\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 if (inet_add_protocol(&udp_protocol, IPPROTO_UDP) < 0)
Joe Perches058bd4d2012-03-11 18:36:11 +00001706 pr_crit("%s: Cannot add UDP protocol\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 if (inet_add_protocol(&tcp_protocol, IPPROTO_TCP) < 0)
Joe Perches058bd4d2012-03-11 18:36:11 +00001708 pr_crit("%s: Cannot add TCP protocol\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709#ifdef CONFIG_IP_MULTICAST
1710 if (inet_add_protocol(&igmp_protocol, IPPROTO_IGMP) < 0)
Joe Perches058bd4d2012-03-11 18:36:11 +00001711 pr_crit("%s: Cannot add IGMP protocol\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712#endif
1713
1714 /* Register the socket-side information for inet_create. */
1715 for (r = &inetsw[0]; r < &inetsw[SOCK_MAX]; ++r)
1716 INIT_LIST_HEAD(r);
1717
1718 for (q = inetsw_array; q < &inetsw_array[INETSW_ARRAY_LEN]; ++q)
1719 inet_register_protosw(q);
1720
1721 /*
1722 * Set the ARP module up
1723 */
1724
1725 arp_init();
1726
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001727 /*
1728 * Set the IP module up
1729 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
1731 ip_init();
1732
Denis V. Lunev9b0f9762008-02-29 11:13:15 -08001733 tcp_v4_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
1735 /* Setup TCP slab cache for open requests. */
1736 tcp_init();
1737
Hideo Aoki95766ff2007-12-31 00:29:24 -08001738 /* Setup UDP memory threshold */
1739 udp_init();
1740
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001741 /* Add UDP-Lite (RFC 3828) */
1742 udplite4_register();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001744 ping_init();
1745
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 /*
1747 * Set the ICMP layer up
1748 */
1749
Denis V. Luneva5710d62008-02-29 11:14:50 -08001750 if (icmp_init() < 0)
1751 panic("Failed to create the ICMP control socket.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
1753 /*
1754 * Initialise the multicast router
1755 */
1756#if defined(CONFIG_IP_MROUTE)
Wang Chen03d2f892008-07-03 12:13:36 +08001757 if (ip_mr_init())
Joe Perches058bd4d2012-03-11 18:36:11 +00001758 pr_crit("%s: Cannot init ipv4 mroute\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759#endif
1760 /*
1761 * Initialise per-cpu ipv4 mibs
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001762 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
Stephen Hemminger132adf52007-03-08 20:44:43 -08001764 if (init_ipv4_mibs())
Joe Perches058bd4d2012-03-11 18:36:11 +00001765 pr_crit("%s: Cannot init ipv4 mibs\n", __func__);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001766
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 ipv4_proc_init();
1768
1769 ipfrag_init();
1770
Herbert Xu30e224d2005-07-05 14:40:10 -07001771 dev_add_pack(&ip_packet_type);
1772
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 rc = 0;
1774out:
1775 return rc;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001776out_unregister_raw_proto:
1777 proto_unregister(&raw_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778out_unregister_udp_proto:
1779 proto_unregister(&udp_prot);
Olaf Kirch321efff2006-09-27 16:33:45 -07001780out_unregister_tcp_proto:
1781 proto_unregister(&tcp_prot);
Amerigo Wange3826f12010-05-05 00:27:06 +00001782out_free_reserved_ports:
1783 kfree(sysctl_local_reserved_ports);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 goto out;
1785}
1786
Heiko Carstensa536e072006-04-28 15:19:17 -07001787fs_initcall(inet_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
1789/* ------------------------------------------------------------------------ */
1790
1791#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792static int __init ipv4_proc_init(void)
1793{
1794 int rc = 0;
1795
1796 if (raw_proc_init())
1797 goto out_raw;
1798 if (tcp4_proc_init())
1799 goto out_tcp;
1800 if (udp4_proc_init())
1801 goto out_udp;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001802 if (ping_proc_init())
1803 goto out_ping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 if (ip_misc_proc_init())
1805 goto out_misc;
1806out:
1807 return rc;
1808out_misc:
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001809 ping_proc_exit();
1810out_ping:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 udp4_proc_exit();
1812out_udp:
1813 tcp4_proc_exit();
1814out_tcp:
1815 raw_proc_exit();
1816out_raw:
1817 rc = -ENOMEM;
1818 goto out;
1819}
1820
1821#else /* CONFIG_PROC_FS */
1822static int __init ipv4_proc_init(void)
1823{
1824 return 0;
1825}
1826#endif /* CONFIG_PROC_FS */
1827
1828MODULE_ALIAS_NETPROTO(PF_INET);
1829