blob: 49f024f62963e7de8dc3e652713232e77535ed6f [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
Herbert Xuf4c50d92006-06-22 03:02:40 -070068#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#include <linux/errno.h>
70#include <linux/types.h>
71#include <linux/socket.h>
72#include <linux/in.h>
73#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#include <linux/module.h>
75#include <linux/sched.h>
76#include <linux/timer.h>
77#include <linux/string.h>
78#include <linux/sockios.h>
79#include <linux/net.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080080#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#include <linux/fcntl.h>
82#include <linux/mm.h>
83#include <linux/interrupt.h>
84#include <linux/stat.h>
85#include <linux/init.h>
86#include <linux/poll.h>
87#include <linux/netfilter_ipv4.h>
David S. Millerb3da2cf2007-03-23 11:40:27 -070088#include <linux/random.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090089#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91#include <asm/uaccess.h>
92#include <asm/system.h>
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094#include <linux/inet.h>
95#include <linux/igmp.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020096#include <linux/inetdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070097#include <linux/netdevice.h>
Herbert Xu73cc19f2008-12-15 23:41:09 -080098#include <net/checksum.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070099#include <net/ip.h>
100#include <net/protocol.h>
101#include <net/arp.h>
102#include <net/route.h>
103#include <net/ip_fib.h>
Arnaldo Carvalho de Melo295f7322005-08-09 20:11:56 -0700104#include <net/inet_connection_sock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105#include <net/tcp.h>
106#include <net/udp.h>
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800107#include <net/udplite.h>
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000108#include <net/ping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109#include <linux/skbuff.h>
110#include <net/sock.h>
111#include <net/raw.h>
112#include <net/icmp.h>
113#include <net/ipip.h>
114#include <net/inet_common.h>
115#include <net/xfrm.h>
Pavel Emelyanov9b4661b2008-07-18 04:01:44 -0700116#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117#ifdef CONFIG_IP_MROUTE
118#include <linux/mroute.h>
119#endif
120
Robert Love8affb4e2008-10-15 15:35:44 -0400121#ifdef CONFIG_ANDROID_PARANOID_NETWORK
122#include <linux/android_aid.h>
123#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125/* The inetsw table contains everything that inet_create needs to
126 * build a new socket.
127 */
128static struct list_head inetsw[SOCK_MAX];
129static DEFINE_SPINLOCK(inetsw_lock);
130
Pavel Emelyanov9ba63972007-12-05 01:38:23 -0800131struct ipv4_config ipv4_config;
Pavel Emelyanov9ba63972007-12-05 01:38:23 -0800132EXPORT_SYMBOL(ipv4_config);
133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134/* New destruction routine */
135
136void inet_sock_destruct(struct sock *sk)
137{
138 struct inet_sock *inet = inet_sk(sk);
139
140 __skb_queue_purge(&sk->sk_receive_queue);
141 __skb_queue_purge(&sk->sk_error_queue);
142
Hideo Aoki95766ff2007-12-31 00:29:24 -0800143 sk_mem_reclaim(sk);
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 if (sk->sk_type == SOCK_STREAM && sk->sk_state != TCP_CLOSE) {
Eric Dumazet3d1427f2009-08-28 23:45:21 -0700146 pr_err("Attempt to release TCP socket in state %d %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 sk->sk_state, sk);
148 return;
149 }
150 if (!sock_flag(sk, SOCK_DEAD)) {
Eric Dumazet3d1427f2009-08-28 23:45:21 -0700151 pr_err("Attempt to release alive inet socket %p\n", sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 return;
153 }
154
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700155 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
156 WARN_ON(atomic_read(&sk->sk_wmem_alloc));
157 WARN_ON(sk->sk_wmem_queued);
158 WARN_ON(sk->sk_forward_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000160 kfree(rcu_dereference_protected(inet->inet_opt, 1));
Eric Dumazetb6c67122010-04-08 23:03:29 +0000161 dst_release(rcu_dereference_check(sk->sk_dst_cache, 1));
Arnaldo Carvalho de Meloe6848972005-08-09 19:45:38 -0700162 sk_refcnt_debug_dec(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
Eric Dumazet3d1427f2009-08-28 23:45:21 -0700164EXPORT_SYMBOL(inet_sock_destruct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166/*
167 * The routines beyond this point handle the behaviour of an AF_INET
168 * socket object. Mostly it punts to the subprotocols of IP to do
169 * the work.
170 */
171
172/*
173 * Automatically bind an unbound socket.
174 */
175
176static int inet_autobind(struct sock *sk)
177{
178 struct inet_sock *inet;
179 /* We may need to bind the socket. */
180 lock_sock(sk);
181 inet = inet_sk(sk);
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000182 if (!inet->inet_num) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 if (sk->sk_prot->get_port(sk, 0)) {
184 release_sock(sk);
185 return -EAGAIN;
186 }
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000187 inet->inet_sport = htons(inet->inet_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 }
189 release_sock(sk);
190 return 0;
191}
192
193/*
194 * Move a socket into listening state.
195 */
196int inet_listen(struct socket *sock, int backlog)
197{
198 struct sock *sk = sock->sk;
199 unsigned char old_state;
200 int err;
201
202 lock_sock(sk);
203
204 err = -EINVAL;
205 if (sock->state != SS_UNCONNECTED || sock->type != SOCK_STREAM)
206 goto out;
207
208 old_state = sk->sk_state;
209 if (!((1 << old_state) & (TCPF_CLOSE | TCPF_LISTEN)))
210 goto out;
211
212 /* Really, if the socket is already in listen state
213 * we can only allow the backlog to be adjusted.
214 */
215 if (old_state != TCP_LISTEN) {
Eric Dumazet72a3eff2006-11-16 02:30:37 -0800216 err = inet_csk_listen_start(sk, backlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 if (err)
218 goto out;
219 }
220 sk->sk_max_ack_backlog = backlog;
221 err = 0;
222
223out:
224 release_sock(sk);
225 return err;
226}
Eric Dumazet3d1427f2009-08-28 23:45:21 -0700227EXPORT_SYMBOL(inet_listen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Eric Dumazetbe776282007-03-27 13:53:04 -0700229u32 inet_ehash_secret __read_mostly;
David S. Millerb3da2cf2007-03-23 11:40:27 -0700230EXPORT_SYMBOL(inet_ehash_secret);
231
Eric Dumazetbe776282007-03-27 13:53:04 -0700232/*
233 * inet_ehash_secret must be set exactly once
Eric Dumazetbe776282007-03-27 13:53:04 -0700234 */
David S. Millerb3da2cf2007-03-23 11:40:27 -0700235void build_ehash_secret(void)
236{
Eric Dumazetbe776282007-03-27 13:53:04 -0700237 u32 rnd;
Eric Dumazet49e8ab02010-08-19 06:10:45 +0000238
Eric Dumazetbe776282007-03-27 13:53:04 -0700239 do {
240 get_random_bytes(&rnd, sizeof(rnd));
241 } while (rnd == 0);
Eric Dumazet49e8ab02010-08-19 06:10:45 +0000242
243 cmpxchg(&inet_ehash_secret, 0, rnd);
David S. Millerb3da2cf2007-03-23 11:40:27 -0700244}
245EXPORT_SYMBOL(build_ehash_secret);
246
Denis V. Lunev2342fd72008-03-24 15:33:42 -0700247static inline int inet_netns_ok(struct net *net, int protocol)
248{
249 int hash;
Alexey Dobriyan32613092009-09-14 12:21:47 +0000250 const struct net_protocol *ipprot;
Denis V. Lunev2342fd72008-03-24 15:33:42 -0700251
Eric Dumazet04f258c2008-11-23 15:42:23 -0800252 if (net_eq(net, &init_net))
Denis V. Lunev2342fd72008-03-24 15:33:42 -0700253 return 1;
254
255 hash = protocol & (MAX_INET_PROTOS - 1);
256 ipprot = rcu_dereference(inet_protos[hash]);
257
258 if (ipprot == NULL)
259 /* raw IP is OK */
260 return 1;
261 return ipprot->netns_ok;
262}
263
Robert Love8affb4e2008-10-15 15:35:44 -0400264#ifdef CONFIG_ANDROID_PARANOID_NETWORK
265static inline int current_has_network(void)
266{
267 return (!current_euid() || in_egroup_p(AID_INET) ||
268 in_egroup_p(AID_NET_RAW));
269}
270static inline int current_has_cap(int cap)
271{
272 if (cap == CAP_NET_RAW && in_egroup_p(AID_NET_RAW))
273 return 1;
274 return capable(cap);
275}
276# else
277static inline int current_has_network(void)
278{
279 return 1;
280}
281static inline int current_has_cap(int cap)
282{
283 return capable(cap);
284}
285#endif
286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287/*
288 * Create an inet socket.
289 */
290
Eric Paris3f378b62009-11-05 22:18:14 -0800291static int inet_create(struct net *net, struct socket *sock, int protocol,
292 int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
294 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 struct inet_protosw *answer;
296 struct inet_sock *inet;
297 struct proto *answer_prot;
298 unsigned char answer_flags;
299 char answer_no_check;
Arnaldo Carvalho de Melobb97d312005-08-09 20:19:14 -0700300 int try_loading_module = 0;
Herbert Xu86c8f9d2005-12-02 20:43:26 -0800301 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Robert Love8affb4e2008-10-15 15:35:44 -0400303 if (!current_has_network())
304 return -EACCES;
305
Eric Dumazet04f258c2008-11-23 15:42:23 -0800306 if (unlikely(!inet_ehash_secret))
307 if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
308 build_ehash_secret();
David S. Millerb3da2cf2007-03-23 11:40:27 -0700309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 sock->state = SS_UNCONNECTED;
311
312 /* Look for the requested type/protocol pair. */
Arnaldo Carvalho de Melobb97d312005-08-09 20:19:14 -0700313lookup_protocol:
Herbert Xu86c8f9d2005-12-02 20:43:26 -0800314 err = -ESOCKTNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 rcu_read_lock();
Paul E. McKenney696adfe2008-07-25 01:45:34 -0700316 list_for_each_entry_rcu(answer, &inetsw[sock->type], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Paul E. McKenney696adfe2008-07-25 01:45:34 -0700318 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 /* Check the non-wild match. */
320 if (protocol == answer->protocol) {
321 if (protocol != IPPROTO_IP)
322 break;
323 } else {
324 /* Check for the two wild cases. */
325 if (IPPROTO_IP == protocol) {
326 protocol = answer->protocol;
327 break;
328 }
329 if (IPPROTO_IP == answer->protocol)
330 break;
331 }
Herbert Xu86c8f9d2005-12-02 20:43:26 -0800332 err = -EPROTONOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 }
334
Paul E. McKenney696adfe2008-07-25 01:45:34 -0700335 if (unlikely(err)) {
Arnaldo Carvalho de Melobb97d312005-08-09 20:19:14 -0700336 if (try_loading_module < 2) {
337 rcu_read_unlock();
338 /*
339 * Be more specific, e.g. net-pf-2-proto-132-type-1
340 * (net-pf-PF_INET-proto-IPPROTO_SCTP-type-SOCK_STREAM)
341 */
342 if (++try_loading_module == 1)
343 request_module("net-pf-%d-proto-%d-type-%d",
344 PF_INET, protocol, sock->type);
345 /*
346 * Fall back to generic, e.g. net-pf-2-proto-132
347 * (net-pf-PF_INET-proto-IPPROTO_SCTP)
348 */
349 else
350 request_module("net-pf-%d-proto-%d",
351 PF_INET, protocol);
352 goto lookup_protocol;
353 } else
354 goto out_rcu_unlock;
355 }
356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 err = -EPERM;
Robert Love8affb4e2008-10-15 15:35:44 -0400358 if (sock->type == SOCK_RAW && !kern && !current_has_cap(CAP_NET_RAW))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 goto out_rcu_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Denis V. Lunev2342fd72008-03-24 15:33:42 -0700361 err = -EAFNOSUPPORT;
362 if (!inet_netns_ok(net, protocol))
363 goto out_rcu_unlock;
364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 sock->ops = answer->ops;
366 answer_prot = answer->prot;
367 answer_no_check = answer->no_check;
368 answer_flags = answer->flags;
369 rcu_read_unlock();
370
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700371 WARN_ON(answer_prot->slab == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373 err = -ENOBUFS;
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700374 sk = sk_alloc(net, PF_INET, GFP_KERNEL, answer_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 if (sk == NULL)
376 goto out;
377
378 err = 0;
379 sk->sk_no_check = answer_no_check;
380 if (INET_PROTOSW_REUSE & answer_flags)
381 sk->sk_reuse = 1;
382
383 inet = inet_sk(sk);
Paul Moore469de9b2007-01-09 14:37:06 -0800384 inet->is_icsk = (INET_PROTOSW_ICSK & answer_flags) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Jiri Olsa7b2ff182010-06-15 01:07:31 +0000386 inet->nodefrag = 0;
387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 if (SOCK_RAW == sock->type) {
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000389 inet->inet_num = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 if (IPPROTO_RAW == protocol)
391 inet->hdrincl = 1;
392 }
393
394 if (ipv4_config.no_pmtu_disc)
395 inet->pmtudisc = IP_PMTUDISC_DONT;
396 else
397 inet->pmtudisc = IP_PMTUDISC_WANT;
398
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000399 inet->inet_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 sock_init_data(sock, sk);
402
403 sk->sk_destruct = inet_sock_destruct;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 sk->sk_protocol = protocol;
405 sk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
406
407 inet->uc_ttl = -1;
408 inet->mc_loop = 1;
409 inet->mc_ttl = 1;
Nivedita Singhvif771bef2009-05-28 07:00:46 +0000410 inet->mc_all = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 inet->mc_index = 0;
412 inet->mc_list = NULL;
413
Arnaldo Carvalho de Meloe6848972005-08-09 19:45:38 -0700414 sk_refcnt_debug_inc(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000416 if (inet->inet_num) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 /* It assumes that any protocol which allows
418 * the user to assign a number at socket
419 * creation time automatically
420 * shares.
421 */
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000422 inet->inet_sport = htons(inet->inet_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 /* Add to protocol hash chains. */
424 sk->sk_prot->hash(sk);
425 }
426
427 if (sk->sk_prot->init) {
428 err = sk->sk_prot->init(sk);
429 if (err)
430 sk_common_release(sk);
431 }
432out:
433 return err;
434out_rcu_unlock:
435 rcu_read_unlock();
436 goto out;
437}
438
439
440/*
441 * The peer socket should always be NULL (or else). When we call this
442 * function we are destroying the object and from then on nobody
443 * should refer to it.
444 */
445int inet_release(struct socket *sock)
446{
447 struct sock *sk = sock->sk;
448
449 if (sk) {
450 long timeout;
451
David S. Millerc58dc012010-04-27 15:05:31 -0700452 sock_rps_reset_flow(sk);
Tom Herbertfec5e652010-04-16 16:01:27 -0700453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 /* Applications forget to leave groups before exiting */
455 ip_mc_drop_socket(sk);
456
457 /* If linger is set, we don't return until the close
458 * is complete. Otherwise we return immediately. The
459 * actually closing is done the same either way.
460 *
461 * If the close is due to the process exiting, we never
462 * linger..
463 */
464 timeout = 0;
465 if (sock_flag(sk, SOCK_LINGER) &&
466 !(current->flags & PF_EXITING))
467 timeout = sk->sk_lingertime;
468 sock->sk = NULL;
469 sk->sk_prot->close(sk, timeout);
470 }
471 return 0;
472}
Eric Dumazet3d1427f2009-08-28 23:45:21 -0700473EXPORT_SYMBOL(inet_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475/* It is off by default, see below. */
Brian Haleyab32ea52006-09-22 14:15:41 -0700476int sysctl_ip_nonlocal_bind __read_mostly;
Eric Dumazet3d1427f2009-08-28 23:45:21 -0700477EXPORT_SYMBOL(sysctl_ip_nonlocal_bind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
479int inet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
480{
481 struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
482 struct sock *sk = sock->sk;
483 struct inet_sock *inet = inet_sk(sk);
484 unsigned short snum;
485 int chk_addr_ret;
486 int err;
487
488 /* If the socket has its own bind function then use it. (RAW) */
489 if (sk->sk_prot->bind) {
490 err = sk->sk_prot->bind(sk, uaddr, addr_len);
491 goto out;
492 }
493 err = -EINVAL;
494 if (addr_len < sizeof(struct sockaddr_in))
495 goto out;
496
Marcus Meissnerd0733d22011-06-01 21:05:22 -0700497 if (addr->sin_family != AF_INET)
498 goto out;
499
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900500 chk_addr_ret = inet_addr_type(sock_net(sk), addr->sin_addr.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 /* Not specified by any standard per-se, however it breaks too
503 * many applications when removed. It is unfortunate since
504 * allowing applications to make a non-local bind solves
505 * several problems with systems using dynamic addressing.
506 * (ie. your servers still start up even if your ISDN link
507 * is temporarily down)
508 */
509 err = -EADDRNOTAVAIL;
510 if (!sysctl_ip_nonlocal_bind &&
Tóth László Attilab9fb1502008-10-01 07:31:24 -0700511 !(inet->freebind || inet->transparent) &&
Al Viroe6f1ceb2008-03-17 22:44:53 -0700512 addr->sin_addr.s_addr != htonl(INADDR_ANY) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 chk_addr_ret != RTN_LOCAL &&
514 chk_addr_ret != RTN_MULTICAST &&
515 chk_addr_ret != RTN_BROADCAST)
516 goto out;
517
518 snum = ntohs(addr->sin_port);
519 err = -EACCES;
520 if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
521 goto out;
522
523 /* We keep a pair of addresses. rcv_saddr is the one
524 * used by hash lookups, and saddr is used for transmit.
525 *
526 * In the BSD API these are the same except where it
527 * would be illegal to use them (multicast/broadcast) in
528 * which case the sending device address is used.
529 */
530 lock_sock(sk);
531
532 /* Check these errors (active socket, double bind). */
533 err = -EINVAL;
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000534 if (sk->sk_state != TCP_CLOSE || inet->inet_num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 goto out_release_sock;
536
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000537 inet->inet_rcv_saddr = inet->inet_saddr = addr->sin_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 if (chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST)
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000539 inet->inet_saddr = 0; /* Use device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541 /* Make sure we are allowed to bind here. */
542 if (sk->sk_prot->get_port(sk, snum)) {
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000543 inet->inet_saddr = inet->inet_rcv_saddr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 err = -EADDRINUSE;
545 goto out_release_sock;
546 }
547
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000548 if (inet->inet_rcv_saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
550 if (snum)
551 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000552 inet->inet_sport = htons(inet->inet_num);
553 inet->inet_daddr = 0;
554 inet->inet_dport = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 sk_dst_reset(sk);
556 err = 0;
557out_release_sock:
558 release_sock(sk);
559out:
560 return err;
561}
Eric Dumazet3d1427f2009-08-28 23:45:21 -0700562EXPORT_SYMBOL(inet_bind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564int inet_dgram_connect(struct socket *sock, struct sockaddr * uaddr,
565 int addr_len, int flags)
566{
567 struct sock *sk = sock->sk;
568
Changli Gao6503d962010-03-31 22:58:26 +0000569 if (addr_len < sizeof(uaddr->sa_family))
570 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 if (uaddr->sa_family == AF_UNSPEC)
572 return sk->sk_prot->disconnect(sk, flags);
573
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000574 if (!inet_sk(sk)->inet_num && inet_autobind(sk))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 return -EAGAIN;
576 return sk->sk_prot->connect(sk, (struct sockaddr *)uaddr, addr_len);
577}
Eric Dumazet3d1427f2009-08-28 23:45:21 -0700578EXPORT_SYMBOL(inet_dgram_connect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580static long inet_wait_for_connect(struct sock *sk, long timeo)
581{
582 DEFINE_WAIT(wait);
583
Eric Dumazetaa395142010-04-20 13:03:51 +0000584 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586 /* Basic assumption: if someone sets sk->sk_err, he _must_
587 * change state of the socket from TCP_SYN_*.
588 * Connect() does not allow to get error notifications
589 * without closing the socket.
590 */
591 while ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
592 release_sock(sk);
593 timeo = schedule_timeout(timeo);
594 lock_sock(sk);
595 if (signal_pending(current) || !timeo)
596 break;
Eric Dumazetaa395142010-04-20 13:03:51 +0000597 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 }
Eric Dumazetaa395142010-04-20 13:03:51 +0000599 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 return timeo;
601}
602
603/*
604 * Connect to a remote host. There is regrettably still a little
605 * TCP 'magic' in here.
606 */
607int inet_stream_connect(struct socket *sock, struct sockaddr *uaddr,
608 int addr_len, int flags)
609{
610 struct sock *sk = sock->sk;
611 int err;
612 long timeo;
613
Changli Gao6503d962010-03-31 22:58:26 +0000614 if (addr_len < sizeof(uaddr->sa_family))
615 return -EINVAL;
616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 lock_sock(sk);
618
619 if (uaddr->sa_family == AF_UNSPEC) {
620 err = sk->sk_prot->disconnect(sk, flags);
621 sock->state = err ? SS_DISCONNECTING : SS_UNCONNECTED;
622 goto out;
623 }
624
625 switch (sock->state) {
626 default:
627 err = -EINVAL;
628 goto out;
629 case SS_CONNECTED:
630 err = -EISCONN;
631 goto out;
632 case SS_CONNECTING:
633 err = -EALREADY;
634 /* Fall out of switch with err, set for this state */
635 break;
636 case SS_UNCONNECTED:
637 err = -EISCONN;
638 if (sk->sk_state != TCP_CLOSE)
639 goto out;
640
641 err = sk->sk_prot->connect(sk, uaddr, addr_len);
642 if (err < 0)
643 goto out;
644
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900645 sock->state = SS_CONNECTING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
647 /* Just entered SS_CONNECTING state; the only
648 * difference is that return value in non-blocking
649 * case is EINPROGRESS, rather than EALREADY.
650 */
651 err = -EINPROGRESS;
652 break;
653 }
654
655 timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
656
657 if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
658 /* Error code is set above */
659 if (!timeo || !inet_wait_for_connect(sk, timeo))
660 goto out;
661
662 err = sock_intr_errno(timeo);
663 if (signal_pending(current))
664 goto out;
665 }
666
667 /* Connection was closed by RST, timeout, ICMP error
668 * or another process disconnected us.
669 */
670 if (sk->sk_state == TCP_CLOSE)
671 goto sock_error;
672
673 /* sk->sk_err may be not zero now, if RECVERR was ordered by user
674 * and error was received after socket entered established state.
675 * Hence, it is handled normally after connect() return successfully.
676 */
677
678 sock->state = SS_CONNECTED;
679 err = 0;
680out:
681 release_sock(sk);
682 return err;
683
684sock_error:
685 err = sock_error(sk) ? : -ECONNABORTED;
686 sock->state = SS_UNCONNECTED;
687 if (sk->sk_prot->disconnect(sk, flags))
688 sock->state = SS_DISCONNECTING;
689 goto out;
690}
Eric Dumazet3d1427f2009-08-28 23:45:21 -0700691EXPORT_SYMBOL(inet_stream_connect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693/*
694 * Accept a pending connection. The TCP layer now gives BSD semantics.
695 */
696
697int inet_accept(struct socket *sock, struct socket *newsock, int flags)
698{
699 struct sock *sk1 = sock->sk;
700 int err = -EINVAL;
701 struct sock *sk2 = sk1->sk_prot->accept(sk1, flags, &err);
702
703 if (!sk2)
704 goto do_err;
705
706 lock_sock(sk2);
707
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700708 WARN_ON(!((1 << sk2->sk_state) &
709 (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT | TCPF_CLOSE)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
711 sock_graft(sk2, newsock);
712
713 newsock->state = SS_CONNECTED;
714 err = 0;
715 release_sock(sk2);
716do_err:
717 return err;
718}
Eric Dumazet3d1427f2009-08-28 23:45:21 -0700719EXPORT_SYMBOL(inet_accept);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
721
722/*
723 * This does both peername and sockname.
724 */
725int inet_getname(struct socket *sock, struct sockaddr *uaddr,
726 int *uaddr_len, int peer)
727{
728 struct sock *sk = sock->sk;
729 struct inet_sock *inet = inet_sk(sk);
Cyrill Gorcunov38bfd8f2009-10-29 02:59:18 -0700730 DECLARE_SOCKADDR(struct sockaddr_in *, sin, uaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
732 sin->sin_family = AF_INET;
733 if (peer) {
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000734 if (!inet->inet_dport ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
736 peer == 1))
737 return -ENOTCONN;
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000738 sin->sin_port = inet->inet_dport;
739 sin->sin_addr.s_addr = inet->inet_daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 } else {
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000741 __be32 addr = inet->inet_rcv_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 if (!addr)
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000743 addr = inet->inet_saddr;
744 sin->sin_port = inet->inet_sport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 sin->sin_addr.s_addr = addr;
746 }
747 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
748 *uaddr_len = sizeof(*sin);
749 return 0;
750}
Eric Dumazet3d1427f2009-08-28 23:45:21 -0700751EXPORT_SYMBOL(inet_getname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753int inet_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg,
754 size_t size)
755{
756 struct sock *sk = sock->sk;
757
David S. Millerc58dc012010-04-27 15:05:31 -0700758 sock_rps_record_flow(sk);
Tom Herbertfec5e652010-04-16 16:01:27 -0700759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 /* We may need to bind the socket. */
Changli Gao7ba42912010-07-10 20:41:55 +0000761 if (!inet_sk(sk)->inet_num && !sk->sk_prot->no_autobind &&
762 inet_autobind(sk))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 return -EAGAIN;
764
765 return sk->sk_prot->sendmsg(iocb, sk, msg, size);
766}
Eric Dumazet3d1427f2009-08-28 23:45:21 -0700767EXPORT_SYMBOL(inet_sendmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Changli Gao7ba42912010-07-10 20:41:55 +0000769ssize_t inet_sendpage(struct socket *sock, struct page *page, int offset,
770 size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
772 struct sock *sk = sock->sk;
773
David S. Millerc58dc012010-04-27 15:05:31 -0700774 sock_rps_record_flow(sk);
Tom Herbertfec5e652010-04-16 16:01:27 -0700775
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 /* We may need to bind the socket. */
Changli Gao7ba42912010-07-10 20:41:55 +0000777 if (!inet_sk(sk)->inet_num && !sk->sk_prot->no_autobind &&
778 inet_autobind(sk))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 return -EAGAIN;
780
781 if (sk->sk_prot->sendpage)
782 return sk->sk_prot->sendpage(sk, page, offset, size, flags);
783 return sock_no_sendpage(sock, page, offset, size, flags);
784}
Changli Gao7ba42912010-07-10 20:41:55 +0000785EXPORT_SYMBOL(inet_sendpage);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
Tom Herbertfec5e652010-04-16 16:01:27 -0700787int inet_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg,
788 size_t size, int flags)
789{
790 struct sock *sk = sock->sk;
791 int addr_len = 0;
792 int err;
793
David S. Millerc58dc012010-04-27 15:05:31 -0700794 sock_rps_record_flow(sk);
Tom Herbertfec5e652010-04-16 16:01:27 -0700795
796 err = sk->sk_prot->recvmsg(iocb, sk, msg, size, flags & MSG_DONTWAIT,
797 flags & ~MSG_DONTWAIT, &addr_len);
798 if (err >= 0)
799 msg->msg_namelen = addr_len;
800 return err;
801}
802EXPORT_SYMBOL(inet_recvmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
804int inet_shutdown(struct socket *sock, int how)
805{
806 struct sock *sk = sock->sk;
807 int err = 0;
808
809 /* This should really check to make sure
810 * the socket is a TCP socket. (WHY AC...)
811 */
812 how++; /* maps 0->1 has the advantage of making bit 1 rcvs and
813 1->2 bit 2 snds.
814 2->3 */
815 if ((how & ~SHUTDOWN_MASK) || !how) /* MAXINT->0 */
816 return -EINVAL;
817
818 lock_sock(sk);
819 if (sock->state == SS_CONNECTING) {
820 if ((1 << sk->sk_state) &
821 (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_CLOSE))
822 sock->state = SS_DISCONNECTING;
823 else
824 sock->state = SS_CONNECTED;
825 }
826
827 switch (sk->sk_state) {
828 case TCP_CLOSE:
829 err = -ENOTCONN;
830 /* Hack to wake up other listeners, who can poll for
831 POLLHUP, even on eg. unconnected UDP sockets -- RR */
832 default:
833 sk->sk_shutdown |= how;
834 if (sk->sk_prot->shutdown)
835 sk->sk_prot->shutdown(sk, how);
836 break;
837
838 /* Remaining two branches are temporary solution for missing
839 * close() in multithreaded environment. It is _not_ a good idea,
840 * but we have no choice until close() is repaired at VFS level.
841 */
842 case TCP_LISTEN:
843 if (!(how & RCV_SHUTDOWN))
844 break;
845 /* Fall through */
846 case TCP_SYN_SENT:
847 err = sk->sk_prot->disconnect(sk, O_NONBLOCK);
848 sock->state = err ? SS_DISCONNECTING : SS_UNCONNECTED;
849 break;
850 }
851
852 /* Wake up anyone sleeping in poll. */
853 sk->sk_state_change(sk);
854 release_sock(sk);
855 return err;
856}
Eric Dumazet3d1427f2009-08-28 23:45:21 -0700857EXPORT_SYMBOL(inet_shutdown);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
859/*
860 * ioctl() calls you can issue on an INET socket. Most of these are
861 * device configuration and stuff and very rarely used. Some ioctls
862 * pass on to the socket itself.
863 *
864 * NOTE: I like the idea of a module for the config stuff. ie ifconfig
865 * loads the devconfigure module does its configuring and unloads it.
866 * There's a good 20K of config code hanging around the kernel.
867 */
868
869int inet_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
870{
871 struct sock *sk = sock->sk;
872 int err = 0;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900873 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
875 switch (cmd) {
Eric Dumazet3d1427f2009-08-28 23:45:21 -0700876 case SIOCGSTAMP:
877 err = sock_get_timestamp(sk, (struct timeval __user *)arg);
878 break;
879 case SIOCGSTAMPNS:
880 err = sock_get_timestampns(sk, (struct timespec __user *)arg);
881 break;
882 case SIOCADDRT:
883 case SIOCDELRT:
884 case SIOCRTMSG:
885 err = ip_rt_ioctl(net, cmd, (void __user *)arg);
886 break;
887 case SIOCDARP:
888 case SIOCGARP:
889 case SIOCSARP:
890 err = arp_ioctl(net, cmd, (void __user *)arg);
891 break;
892 case SIOCGIFADDR:
893 case SIOCSIFADDR:
894 case SIOCGIFBRDADDR:
895 case SIOCSIFBRDADDR:
896 case SIOCGIFNETMASK:
897 case SIOCSIFNETMASK:
898 case SIOCGIFDSTADDR:
899 case SIOCSIFDSTADDR:
900 case SIOCSIFPFLAGS:
901 case SIOCGIFPFLAGS:
902 case SIOCSIFFLAGS:
Robert Lovea7429e52008-05-12 17:08:29 -0400903 case SIOCKILLADDR:
Eric Dumazet3d1427f2009-08-28 23:45:21 -0700904 err = devinet_ioctl(net, cmd, (void __user *)arg);
905 break;
906 default:
907 if (sk->sk_prot->ioctl)
908 err = sk->sk_prot->ioctl(sk, cmd, arg);
909 else
910 err = -ENOIOCTLCMD;
911 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 }
913 return err;
914}
Eric Dumazet3d1427f2009-08-28 23:45:21 -0700915EXPORT_SYMBOL(inet_ioctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
Eric W. Biederman709b46e2011-01-29 16:15:56 +0000917#ifdef CONFIG_COMPAT
918int inet_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
919{
920 struct sock *sk = sock->sk;
921 int err = -ENOIOCTLCMD;
922
923 if (sk->sk_prot->compat_ioctl)
924 err = sk->sk_prot->compat_ioctl(sk, cmd, arg);
925
926 return err;
927}
928#endif
929
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800930const struct proto_ops inet_stream_ops = {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800931 .family = PF_INET,
932 .owner = THIS_MODULE,
933 .release = inet_release,
934 .bind = inet_bind,
935 .connect = inet_stream_connect,
936 .socketpair = sock_no_socketpair,
937 .accept = inet_accept,
938 .getname = inet_getname,
939 .poll = tcp_poll,
940 .ioctl = inet_ioctl,
941 .listen = inet_listen,
942 .shutdown = inet_shutdown,
943 .setsockopt = sock_common_setsockopt,
944 .getsockopt = sock_common_getsockopt,
Changli Gao7ba42912010-07-10 20:41:55 +0000945 .sendmsg = inet_sendmsg,
Tom Herbertfec5e652010-04-16 16:01:27 -0700946 .recvmsg = inet_recvmsg,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800947 .mmap = sock_no_mmap,
Changli Gao7ba42912010-07-10 20:41:55 +0000948 .sendpage = inet_sendpage,
Jens Axboe9c55e012007-11-06 23:30:13 -0800949 .splice_read = tcp_splice_read,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800950#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800951 .compat_setsockopt = compat_sock_common_setsockopt,
952 .compat_getsockopt = compat_sock_common_getsockopt,
Eric W. Biederman709b46e2011-01-29 16:15:56 +0000953 .compat_ioctl = inet_compat_ioctl,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800954#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955};
Eric Dumazet3d1427f2009-08-28 23:45:21 -0700956EXPORT_SYMBOL(inet_stream_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800958const struct proto_ops inet_dgram_ops = {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800959 .family = PF_INET,
960 .owner = THIS_MODULE,
961 .release = inet_release,
962 .bind = inet_bind,
963 .connect = inet_dgram_connect,
964 .socketpair = sock_no_socketpair,
965 .accept = sock_no_accept,
966 .getname = inet_getname,
967 .poll = udp_poll,
968 .ioctl = inet_ioctl,
969 .listen = sock_no_listen,
970 .shutdown = inet_shutdown,
971 .setsockopt = sock_common_setsockopt,
972 .getsockopt = sock_common_getsockopt,
973 .sendmsg = inet_sendmsg,
Tom Herbertfec5e652010-04-16 16:01:27 -0700974 .recvmsg = inet_recvmsg,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800975 .mmap = sock_no_mmap,
976 .sendpage = inet_sendpage,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800977#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800978 .compat_setsockopt = compat_sock_common_setsockopt,
979 .compat_getsockopt = compat_sock_common_getsockopt,
Eric W. Biederman709b46e2011-01-29 16:15:56 +0000980 .compat_ioctl = inet_compat_ioctl,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800981#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982};
Eric Dumazet3d1427f2009-08-28 23:45:21 -0700983EXPORT_SYMBOL(inet_dgram_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
985/*
986 * For SOCK_RAW sockets; should be the same as inet_dgram_ops but without
987 * udp_poll
988 */
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800989static const struct proto_ops inet_sockraw_ops = {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800990 .family = PF_INET,
991 .owner = THIS_MODULE,
992 .release = inet_release,
993 .bind = inet_bind,
994 .connect = inet_dgram_connect,
995 .socketpair = sock_no_socketpair,
996 .accept = sock_no_accept,
997 .getname = inet_getname,
998 .poll = datagram_poll,
999 .ioctl = inet_ioctl,
1000 .listen = sock_no_listen,
1001 .shutdown = inet_shutdown,
1002 .setsockopt = sock_common_setsockopt,
1003 .getsockopt = sock_common_getsockopt,
1004 .sendmsg = inet_sendmsg,
Tom Herbertfec5e652010-04-16 16:01:27 -07001005 .recvmsg = inet_recvmsg,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001006 .mmap = sock_no_mmap,
1007 .sendpage = inet_sendpage,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001008#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001009 .compat_setsockopt = compat_sock_common_setsockopt,
1010 .compat_getsockopt = compat_sock_common_getsockopt,
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001011 .compat_ioctl = inet_compat_ioctl,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001012#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013};
1014
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00001015static const struct net_proto_family inet_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 .family = PF_INET,
1017 .create = inet_create,
1018 .owner = THIS_MODULE,
1019};
1020
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021/* Upon startup we insert all the elements in inetsw_array[] into
1022 * the linked list inetsw.
1023 */
1024static struct inet_protosw inetsw_array[] =
1025{
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001026 {
1027 .type = SOCK_STREAM,
1028 .protocol = IPPROTO_TCP,
1029 .prot = &tcp_prot,
1030 .ops = &inet_stream_ops,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001031 .no_check = 0,
1032 .flags = INET_PROTOSW_PERMANENT |
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -08001033 INET_PROTOSW_ICSK,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001034 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001036 {
1037 .type = SOCK_DGRAM,
1038 .protocol = IPPROTO_UDP,
1039 .prot = &udp_prot,
1040 .ops = &inet_dgram_ops,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001041 .no_check = UDP_CSUM_DEFAULT,
1042 .flags = INET_PROTOSW_PERMANENT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 },
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001044
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001045 {
1046 .type = SOCK_DGRAM,
1047 .protocol = IPPROTO_ICMP,
1048 .prot = &ping_prot,
1049 .ops = &inet_dgram_ops,
1050 .no_check = UDP_CSUM_DEFAULT,
1051 .flags = INET_PROTOSW_REUSE,
1052 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
1054 {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001055 .type = SOCK_RAW,
1056 .protocol = IPPROTO_IP, /* wild card */
1057 .prot = &raw_prot,
1058 .ops = &inet_sockraw_ops,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001059 .no_check = UDP_CSUM_DEFAULT,
1060 .flags = INET_PROTOSW_REUSE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 }
1062};
1063
Denis Chengc40f6ff2007-09-16 16:39:25 -07001064#define INETSW_ARRAY_LEN ARRAY_SIZE(inetsw_array)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
1066void inet_register_protosw(struct inet_protosw *p)
1067{
1068 struct list_head *lh;
1069 struct inet_protosw *answer;
1070 int protocol = p->protocol;
1071 struct list_head *last_perm;
1072
1073 spin_lock_bh(&inetsw_lock);
1074
1075 if (p->type >= SOCK_MAX)
1076 goto out_illegal;
1077
1078 /* If we are trying to override a permanent protocol, bail. */
1079 answer = NULL;
1080 last_perm = &inetsw[p->type];
1081 list_for_each(lh, &inetsw[p->type]) {
1082 answer = list_entry(lh, struct inet_protosw, list);
1083
1084 /* Check only the non-wild match. */
1085 if (INET_PROTOSW_PERMANENT & answer->flags) {
1086 if (protocol == answer->protocol)
1087 break;
1088 last_perm = lh;
1089 }
1090
1091 answer = NULL;
1092 }
1093 if (answer)
1094 goto out_permanent;
1095
1096 /* Add the new entry after the last permanent entry if any, so that
1097 * the new entry does not override a permanent entry when matched with
1098 * a wild-card protocol. But it is allowed to override any existing
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001099 * non-permanent entry. This means that when we remove this entry, the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 * system automatically returns to the old behavior.
1101 */
1102 list_add_rcu(&p->list, last_perm);
1103out:
1104 spin_unlock_bh(&inetsw_lock);
1105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 return;
1107
1108out_permanent:
1109 printk(KERN_ERR "Attempt to override permanent protocol %d.\n",
1110 protocol);
1111 goto out;
1112
1113out_illegal:
1114 printk(KERN_ERR
1115 "Ignoring attempt to register invalid socket type %d.\n",
1116 p->type);
1117 goto out;
1118}
Eric Dumazet3d1427f2009-08-28 23:45:21 -07001119EXPORT_SYMBOL(inet_register_protosw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
1121void inet_unregister_protosw(struct inet_protosw *p)
1122{
1123 if (INET_PROTOSW_PERMANENT & p->flags) {
1124 printk(KERN_ERR
1125 "Attempt to unregister permanent protocol %d.\n",
1126 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) {
Harvey Harrison673d57e2008-10-31 00:53:57 -07001174 printk(KERN_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ław04ed3e72011-01-24 15:32:47 -08001275static struct sk_buff *inet_gso_segment(struct sk_buff *skb, u32 features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07001276{
1277 struct sk_buff *segs = ERR_PTR(-EINVAL);
1278 struct iphdr *iph;
Alexey Dobriyan32613092009-09-14 12:21:47 +00001279 const struct net_protocol *ops;
Herbert Xuf4c50d92006-06-22 03:02:40 -07001280 int proto;
1281 int ihl;
1282 int id;
Sridhar Samudralad7ca4cc2009-07-09 08:09:47 +00001283 unsigned int offset = 0;
Herbert Xuf4c50d92006-06-22 03:02:40 -07001284
Stephen Hemmingerd212f872007-06-27 00:47:37 -07001285 if (!(features & NETIF_F_V4_CSUM))
1286 features &= ~NETIF_F_SG;
1287
Herbert Xubbcf4672006-07-03 19:38:35 -07001288 if (unlikely(skb_shinfo(skb)->gso_type &
1289 ~(SKB_GSO_TCPV4 |
1290 SKB_GSO_UDP |
1291 SKB_GSO_DODGY |
1292 SKB_GSO_TCP_ECN |
1293 0)))
1294 goto out;
1295
1296 if (unlikely(!pskb_may_pull(skb, sizeof(*iph))))
Herbert Xuf4c50d92006-06-22 03:02:40 -07001297 goto out;
1298
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001299 iph = ip_hdr(skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07001300 ihl = iph->ihl * 4;
1301 if (ihl < sizeof(*iph))
1302 goto out;
1303
Herbert Xubbcf4672006-07-03 19:38:35 -07001304 if (unlikely(!pskb_may_pull(skb, ihl)))
Herbert Xuf4c50d92006-06-22 03:02:40 -07001305 goto out;
1306
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03001307 __skb_pull(skb, ihl);
1308 skb_reset_transport_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001309 iph = ip_hdr(skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07001310 id = ntohs(iph->id);
1311 proto = iph->protocol & (MAX_INET_PROTOS - 1);
1312 segs = ERR_PTR(-EPROTONOSUPPORT);
1313
1314 rcu_read_lock();
1315 ops = rcu_dereference(inet_protos[proto]);
Herbert Xubbcf4672006-07-03 19:38:35 -07001316 if (likely(ops && ops->gso_segment))
Herbert Xu576a30e2006-06-27 13:22:38 -07001317 segs = ops->gso_segment(skb, features);
Herbert Xuf4c50d92006-06-22 03:02:40 -07001318 rcu_read_unlock();
1319
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07001320 if (!segs || IS_ERR(segs))
Herbert Xuf4c50d92006-06-22 03:02:40 -07001321 goto out;
1322
1323 skb = segs;
1324 do {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001325 iph = ip_hdr(skb);
Sridhar Samudralad7ca4cc2009-07-09 08:09:47 +00001326 if (proto == IPPROTO_UDP) {
1327 iph->id = htons(id);
1328 iph->frag_off = htons(offset >> 3);
1329 if (skb->next != NULL)
1330 iph->frag_off |= htons(IP_MF);
1331 offset += (skb->len - skb->mac_len - iph->ihl * 4);
1332 } else
1333 iph->id = htons(id++);
Herbert Xuf4c50d92006-06-22 03:02:40 -07001334 iph->tot_len = htons(skb->len - skb->mac_len);
1335 iph->check = 0;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001336 iph->check = ip_fast_csum(skb_network_header(skb), iph->ihl);
Herbert Xuf4c50d92006-06-22 03:02:40 -07001337 } while ((skb = skb->next));
1338
1339out:
1340 return segs;
1341}
1342
Herbert Xu73cc19f2008-12-15 23:41:09 -08001343static struct sk_buff **inet_gro_receive(struct sk_buff **head,
1344 struct sk_buff *skb)
1345{
Alexey Dobriyan32613092009-09-14 12:21:47 +00001346 const struct net_protocol *ops;
Herbert Xu73cc19f2008-12-15 23:41:09 -08001347 struct sk_buff **pp = NULL;
1348 struct sk_buff *p;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001349 const struct iphdr *iph;
Herbert Xua5b1cf22009-05-26 18:50:28 +00001350 unsigned int hlen;
1351 unsigned int off;
Herbert Xu1075f3f2009-05-26 18:50:29 +00001352 unsigned int id;
Herbert Xu73cc19f2008-12-15 23:41:09 -08001353 int flush = 1;
1354 int proto;
Herbert Xu73cc19f2008-12-15 23:41:09 -08001355
Herbert Xua5b1cf22009-05-26 18:50:28 +00001356 off = skb_gro_offset(skb);
1357 hlen = off + sizeof(*iph);
1358 iph = skb_gro_header_fast(skb, off);
1359 if (skb_gro_header_hard(skb, hlen)) {
1360 iph = skb_gro_header_slow(skb, hlen, off);
1361 if (unlikely(!iph))
1362 goto out;
1363 }
Herbert Xu73cc19f2008-12-15 23:41:09 -08001364
Herbert Xu73cc19f2008-12-15 23:41:09 -08001365 proto = iph->protocol & (MAX_INET_PROTOS - 1);
1366
1367 rcu_read_lock();
1368 ops = rcu_dereference(inet_protos[proto]);
1369 if (!ops || !ops->gro_receive)
1370 goto out_unlock;
1371
Herbert Xua5ad24b2009-02-08 18:00:39 +00001372 if (*(u8 *)iph != 0x45)
Herbert Xu73cc19f2008-12-15 23:41:09 -08001373 goto out_unlock;
1374
1375 if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
1376 goto out_unlock;
1377
Eric Dumazet0eae88f2010-04-20 19:06:52 -07001378 id = ntohl(*(__be32 *)&iph->id);
1379 flush = (u16)((ntohl(*(__be32 *)iph) ^ skb_gro_len(skb)) | (id ^ IP_DF));
Herbert Xu1075f3f2009-05-26 18:50:29 +00001380 id >>= 16;
Herbert Xu73cc19f2008-12-15 23:41:09 -08001381
1382 for (p = *head; p; p = p->next) {
1383 struct iphdr *iph2;
1384
1385 if (!NAPI_GRO_CB(p)->same_flow)
1386 continue;
1387
1388 iph2 = ip_hdr(p);
1389
Herbert Xua5ad24b2009-02-08 18:00:39 +00001390 if ((iph->protocol ^ iph2->protocol) |
1391 (iph->tos ^ iph2->tos) |
Eric Dumazet0eae88f2010-04-20 19:06:52 -07001392 ((__force u32)iph->saddr ^ (__force u32)iph2->saddr) |
1393 ((__force u32)iph->daddr ^ (__force u32)iph2->daddr)) {
Herbert Xu73cc19f2008-12-15 23:41:09 -08001394 NAPI_GRO_CB(p)->same_flow = 0;
1395 continue;
1396 }
1397
1398 /* All fields must match except length and checksum. */
1399 NAPI_GRO_CB(p)->flush |=
Herbert Xua5ad24b2009-02-08 18:00:39 +00001400 (iph->ttl ^ iph2->ttl) |
1401 ((u16)(ntohs(iph2->id) + NAPI_GRO_CB(p)->count) ^ id);
Herbert Xu73cc19f2008-12-15 23:41:09 -08001402
1403 NAPI_GRO_CB(p)->flush |= flush;
1404 }
1405
1406 NAPI_GRO_CB(skb)->flush |= flush;
Herbert Xu86911732009-01-29 14:19:50 +00001407 skb_gro_pull(skb, sizeof(*iph));
1408 skb_set_transport_header(skb, skb_gro_offset(skb));
Herbert Xu73cc19f2008-12-15 23:41:09 -08001409
1410 pp = ops->gro_receive(head, skb);
1411
1412out_unlock:
1413 rcu_read_unlock();
1414
1415out:
1416 NAPI_GRO_CB(skb)->flush |= flush;
1417
1418 return pp;
1419}
1420
1421static int inet_gro_complete(struct sk_buff *skb)
1422{
Alexey Dobriyan32613092009-09-14 12:21:47 +00001423 const struct net_protocol *ops;
Herbert Xu73cc19f2008-12-15 23:41:09 -08001424 struct iphdr *iph = ip_hdr(skb);
1425 int proto = iph->protocol & (MAX_INET_PROTOS - 1);
1426 int err = -ENOSYS;
1427 __be16 newlen = htons(skb->len - skb_network_offset(skb));
1428
1429 csum_replace2(&iph->check, iph->tot_len, newlen);
1430 iph->tot_len = newlen;
1431
1432 rcu_read_lock();
1433 ops = rcu_dereference(inet_protos[proto]);
1434 if (WARN_ON(!ops || !ops->gro_complete))
1435 goto out_unlock;
1436
1437 err = ops->gro_complete(skb);
1438
1439out_unlock:
1440 rcu_read_unlock();
1441
1442 return err;
1443}
1444
Denis V. Luneveee4fe42008-04-03 14:27:58 -07001445int inet_ctl_sock_create(struct sock **sk, unsigned short family,
Denis V. Lunev56772422008-04-03 14:28:30 -07001446 unsigned short type, unsigned char protocol,
1447 struct net *net)
Denis V. Lunev3d58b5f2008-04-03 14:22:32 -07001448{
Denis V. Luneveee4fe42008-04-03 14:27:58 -07001449 struct socket *sock;
1450 int rc = sock_create_kern(family, type, protocol, &sock);
Denis V. Lunev3d58b5f2008-04-03 14:22:32 -07001451
1452 if (rc == 0) {
Denis V. Luneveee4fe42008-04-03 14:27:58 -07001453 *sk = sock->sk;
1454 (*sk)->sk_allocation = GFP_ATOMIC;
Denis V. Lunev3d58b5f2008-04-03 14:22:32 -07001455 /*
1456 * Unhash it so that IP input processing does not even see it,
1457 * we do not wish this socket to see incoming packets.
1458 */
Denis V. Luneveee4fe42008-04-03 14:27:58 -07001459 (*sk)->sk_prot->unhash(*sk);
Denis V. Lunev56772422008-04-03 14:28:30 -07001460
1461 sk_change_net(*sk, net);
Denis V. Lunev3d58b5f2008-04-03 14:22:32 -07001462 }
1463 return rc;
1464}
Denis V. Lunev3d58b5f2008-04-03 14:22:32 -07001465EXPORT_SYMBOL_GPL(inet_ctl_sock_create);
1466
Tejun Heo7d720c32010-02-16 15:20:26 +00001467unsigned long snmp_fold_field(void __percpu *mib[], int offt)
Herbert Xu5e0f0432007-04-24 21:53:35 -07001468{
1469 unsigned long res = 0;
1470 int i;
1471
1472 for_each_possible_cpu(i) {
1473 res += *(((unsigned long *) per_cpu_ptr(mib[0], i)) + offt);
1474 res += *(((unsigned long *) per_cpu_ptr(mib[1], i)) + offt);
1475 }
1476 return res;
1477}
1478EXPORT_SYMBOL_GPL(snmp_fold_field);
1479
Eric Dumazet4ce3c182010-06-30 13:31:19 -07001480#if BITS_PER_LONG==32
1481
1482u64 snmp_fold_field64(void __percpu *mib[], int offt, size_t syncp_offset)
1483{
1484 u64 res = 0;
1485 int cpu;
1486
1487 for_each_possible_cpu(cpu) {
1488 void *bhptr, *userptr;
1489 struct u64_stats_sync *syncp;
1490 u64 v_bh, v_user;
1491 unsigned int start;
1492
1493 /* first mib used by softirq context, we must use _bh() accessors */
1494 bhptr = per_cpu_ptr(SNMP_STAT_BHPTR(mib), cpu);
1495 syncp = (struct u64_stats_sync *)(bhptr + syncp_offset);
1496 do {
1497 start = u64_stats_fetch_begin_bh(syncp);
1498 v_bh = *(((u64 *) bhptr) + offt);
1499 } while (u64_stats_fetch_retry_bh(syncp, start));
1500
1501 /* second mib used in USER context */
1502 userptr = per_cpu_ptr(SNMP_STAT_USRPTR(mib), cpu);
1503 syncp = (struct u64_stats_sync *)(userptr + syncp_offset);
1504 do {
1505 start = u64_stats_fetch_begin(syncp);
1506 v_user = *(((u64 *) userptr) + offt);
1507 } while (u64_stats_fetch_retry(syncp, start));
1508
1509 res += v_bh + v_user;
1510 }
1511 return res;
1512}
1513EXPORT_SYMBOL_GPL(snmp_fold_field64);
1514#endif
1515
Eric Dumazet1823e4c82010-06-22 20:58:41 +00001516int snmp_mib_init(void __percpu *ptr[2], size_t mibsize, size_t align)
Herbert Xu5e0f0432007-04-24 21:53:35 -07001517{
1518 BUG_ON(ptr == NULL);
Eric Dumazet1823e4c82010-06-22 20:58:41 +00001519 ptr[0] = __alloc_percpu(mibsize, align);
Herbert Xu5e0f0432007-04-24 21:53:35 -07001520 if (!ptr[0])
1521 goto err0;
Eric Dumazet1823e4c82010-06-22 20:58:41 +00001522 ptr[1] = __alloc_percpu(mibsize, align);
Herbert Xu5e0f0432007-04-24 21:53:35 -07001523 if (!ptr[1])
1524 goto err1;
1525 return 0;
1526err1:
1527 free_percpu(ptr[0]);
1528 ptr[0] = NULL;
1529err0:
1530 return -ENOMEM;
1531}
1532EXPORT_SYMBOL_GPL(snmp_mib_init);
1533
Tejun Heo7d720c32010-02-16 15:20:26 +00001534void snmp_mib_free(void __percpu *ptr[2])
Herbert Xu5e0f0432007-04-24 21:53:35 -07001535{
1536 BUG_ON(ptr == NULL);
1537 free_percpu(ptr[0]);
1538 free_percpu(ptr[1]);
1539 ptr[0] = ptr[1] = NULL;
1540}
1541EXPORT_SYMBOL_GPL(snmp_mib_free);
1542
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543#ifdef CONFIG_IP_MULTICAST
Alexey Dobriyan32613092009-09-14 12:21:47 +00001544static const struct net_protocol igmp_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 .handler = igmp_rcv,
Alexey Dobriyanb4ee07d2008-12-25 16:42:23 -08001546 .netns_ok = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547};
1548#endif
1549
Alexey Dobriyan32613092009-09-14 12:21:47 +00001550static const struct net_protocol tcp_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 .handler = tcp_v4_rcv,
1552 .err_handler = tcp_v4_err,
Herbert Xua430a432006-07-08 13:34:56 -07001553 .gso_send_check = tcp_v4_gso_send_check,
Herbert Xuf4c50d92006-06-22 03:02:40 -07001554 .gso_segment = tcp_tso_segment,
Herbert Xubf296b12008-12-15 23:43:36 -08001555 .gro_receive = tcp4_gro_receive,
1556 .gro_complete = tcp4_gro_complete,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 .no_policy = 1,
Denis V. Lunev92f1fec2008-03-24 15:34:06 -07001558 .netns_ok = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559};
1560
Alexey Dobriyan32613092009-09-14 12:21:47 +00001561static const struct net_protocol udp_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 .handler = udp_rcv,
1563 .err_handler = udp_err,
Sridhar Samudralad7ca4cc2009-07-09 08:09:47 +00001564 .gso_send_check = udp4_ufo_send_check,
1565 .gso_segment = udp4_ufo_fragment,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 .no_policy = 1,
Denis V. Lunev92f1fec2008-03-24 15:34:06 -07001567 .netns_ok = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568};
1569
Alexey Dobriyan32613092009-09-14 12:21:47 +00001570static const struct net_protocol icmp_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 .handler = icmp_rcv,
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001572 .err_handler = ping_err,
Herbert Xu8b7817f2007-12-12 10:44:43 -08001573 .no_policy = 1,
Denis V. Lunev92f1fec2008-03-24 15:34:06 -07001574 .netns_ok = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575};
1576
Pavel Emelyanov9b4661b2008-07-18 04:01:44 -07001577static __net_init int ipv4_mib_init_net(struct net *net)
1578{
Tejun Heo7d720c32010-02-16 15:20:26 +00001579 if (snmp_mib_init((void __percpu **)net->mib.tcp_statistics,
Eric Dumazet1823e4c82010-06-22 20:58:41 +00001580 sizeof(struct tcp_mib),
1581 __alignof__(struct tcp_mib)) < 0)
Pavel Emelyanov57ef42d2008-07-18 04:02:08 -07001582 goto err_tcp_mib;
Tejun Heo7d720c32010-02-16 15:20:26 +00001583 if (snmp_mib_init((void __percpu **)net->mib.ip_statistics,
Eric Dumazet1823e4c82010-06-22 20:58:41 +00001584 sizeof(struct ipstats_mib),
1585 __alignof__(struct ipstats_mib)) < 0)
Pavel Emelyanova20f5792008-07-18 04:02:42 -07001586 goto err_ip_mib;
Tejun Heo7d720c32010-02-16 15:20:26 +00001587 if (snmp_mib_init((void __percpu **)net->mib.net_statistics,
Eric Dumazet1823e4c82010-06-22 20:58:41 +00001588 sizeof(struct linux_mib),
1589 __alignof__(struct linux_mib)) < 0)
Pavel Emelyanov61a7e262008-07-18 04:03:08 -07001590 goto err_net_mib;
Tejun Heo7d720c32010-02-16 15:20:26 +00001591 if (snmp_mib_init((void __percpu **)net->mib.udp_statistics,
Eric Dumazet1823e4c82010-06-22 20:58:41 +00001592 sizeof(struct udp_mib),
1593 __alignof__(struct udp_mib)) < 0)
Pavel Emelyanov2f275f92008-07-18 04:03:27 -07001594 goto err_udp_mib;
Tejun Heo7d720c32010-02-16 15:20:26 +00001595 if (snmp_mib_init((void __percpu **)net->mib.udplite_statistics,
Eric Dumazet1823e4c82010-06-22 20:58:41 +00001596 sizeof(struct udp_mib),
1597 __alignof__(struct udp_mib)) < 0)
Pavel Emelyanov386019d2008-07-18 04:03:45 -07001598 goto err_udplite_mib;
Tejun Heo7d720c32010-02-16 15:20:26 +00001599 if (snmp_mib_init((void __percpu **)net->mib.icmp_statistics,
Eric Dumazet1823e4c82010-06-22 20:58:41 +00001600 sizeof(struct icmp_mib),
1601 __alignof__(struct icmp_mib)) < 0)
Pavel Emelyanovb60538a2008-07-18 04:04:02 -07001602 goto err_icmp_mib;
Tejun Heo7d720c32010-02-16 15:20:26 +00001603 if (snmp_mib_init((void __percpu **)net->mib.icmpmsg_statistics,
Eric Dumazet1823e4c82010-06-22 20:58:41 +00001604 sizeof(struct icmpmsg_mib),
1605 __alignof__(struct icmpmsg_mib)) < 0)
Pavel Emelyanov923c6582008-07-18 04:04:22 -07001606 goto err_icmpmsg_mib;
Pavel Emelyanov57ef42d2008-07-18 04:02:08 -07001607
1608 tcp_mib_init(net);
Pavel Emelyanov9b4661b2008-07-18 04:01:44 -07001609 return 0;
Pavel Emelyanov57ef42d2008-07-18 04:02:08 -07001610
Pavel Emelyanov923c6582008-07-18 04:04:22 -07001611err_icmpmsg_mib:
Tejun Heo7d720c32010-02-16 15:20:26 +00001612 snmp_mib_free((void __percpu **)net->mib.icmp_statistics);
Pavel Emelyanovb60538a2008-07-18 04:04:02 -07001613err_icmp_mib:
Tejun Heo7d720c32010-02-16 15:20:26 +00001614 snmp_mib_free((void __percpu **)net->mib.udplite_statistics);
Pavel Emelyanov386019d2008-07-18 04:03:45 -07001615err_udplite_mib:
Tejun Heo7d720c32010-02-16 15:20:26 +00001616 snmp_mib_free((void __percpu **)net->mib.udp_statistics);
Pavel Emelyanov2f275f92008-07-18 04:03:27 -07001617err_udp_mib:
Tejun Heo7d720c32010-02-16 15:20:26 +00001618 snmp_mib_free((void __percpu **)net->mib.net_statistics);
Pavel Emelyanov61a7e262008-07-18 04:03:08 -07001619err_net_mib:
Tejun Heo7d720c32010-02-16 15:20:26 +00001620 snmp_mib_free((void __percpu **)net->mib.ip_statistics);
Pavel Emelyanova20f5792008-07-18 04:02:42 -07001621err_ip_mib:
Tejun Heo7d720c32010-02-16 15:20:26 +00001622 snmp_mib_free((void __percpu **)net->mib.tcp_statistics);
Pavel Emelyanov57ef42d2008-07-18 04:02:08 -07001623err_tcp_mib:
1624 return -ENOMEM;
Pavel Emelyanov9b4661b2008-07-18 04:01:44 -07001625}
1626
1627static __net_exit void ipv4_mib_exit_net(struct net *net)
1628{
Tejun Heo7d720c32010-02-16 15:20:26 +00001629 snmp_mib_free((void __percpu **)net->mib.icmpmsg_statistics);
1630 snmp_mib_free((void __percpu **)net->mib.icmp_statistics);
1631 snmp_mib_free((void __percpu **)net->mib.udplite_statistics);
1632 snmp_mib_free((void __percpu **)net->mib.udp_statistics);
1633 snmp_mib_free((void __percpu **)net->mib.net_statistics);
1634 snmp_mib_free((void __percpu **)net->mib.ip_statistics);
1635 snmp_mib_free((void __percpu **)net->mib.tcp_statistics);
Pavel Emelyanov9b4661b2008-07-18 04:01:44 -07001636}
1637
1638static __net_initdata struct pernet_operations ipv4_mib_ops = {
1639 .init = ipv4_mib_init_net,
1640 .exit = ipv4_mib_exit_net,
1641};
1642
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643static int __init init_ipv4_mibs(void)
1644{
Pavel Emelyanovd89cbbb2008-07-18 04:04:51 -07001645 return register_pernet_subsys(&ipv4_mib_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646}
1647
1648static int ipv4_proc_init(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
Herbert Xu30e224d2005-07-05 14:40:10 -07001650/*
1651 * IP protocol layer initialiser
1652 */
1653
Stephen Hemminger7546dd92009-03-09 08:18:29 +00001654static struct packet_type ip_packet_type __read_mostly = {
Harvey Harrison09640e62009-02-01 00:45:17 -08001655 .type = cpu_to_be16(ETH_P_IP),
Herbert Xu30e224d2005-07-05 14:40:10 -07001656 .func = ip_rcv,
Herbert Xua430a432006-07-08 13:34:56 -07001657 .gso_send_check = inet_gso_send_check,
Herbert Xuf4c50d92006-06-22 03:02:40 -07001658 .gso_segment = inet_gso_segment,
Herbert Xu73cc19f2008-12-15 23:41:09 -08001659 .gro_receive = inet_gro_receive,
1660 .gro_complete = inet_gro_complete,
Herbert Xu30e224d2005-07-05 14:40:10 -07001661};
1662
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663static int __init inet_init(void)
1664{
1665 struct sk_buff *dummy_skb;
1666 struct inet_protosw *q;
1667 struct list_head *r;
1668 int rc = -EINVAL;
1669
YOSHIFUJI Hideakief047f52006-09-01 00:29:06 -07001670 BUILD_BUG_ON(sizeof(struct inet_skb_parm) > sizeof(dummy_skb->cb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
Amerigo Wange3826f12010-05-05 00:27:06 +00001672 sysctl_local_reserved_ports = kzalloc(65536 / 8, GFP_KERNEL);
1673 if (!sysctl_local_reserved_ports)
1674 goto out;
1675
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 rc = proto_register(&tcp_prot, 1);
1677 if (rc)
Amerigo Wange3826f12010-05-05 00:27:06 +00001678 goto out_free_reserved_ports;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
1680 rc = proto_register(&udp_prot, 1);
1681 if (rc)
1682 goto out_unregister_tcp_proto;
1683
1684 rc = proto_register(&raw_prot, 1);
1685 if (rc)
1686 goto out_unregister_udp_proto;
1687
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001688 rc = proto_register(&ping_prot, 1);
1689 if (rc)
1690 goto out_unregister_raw_proto;
1691
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 /*
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001693 * Tell SOCKET that we are alive...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 */
1695
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001696 (void)sock_register(&inet_family_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
Al Virobd7b1532008-07-15 16:00:59 -04001698#ifdef CONFIG_SYSCTL
1699 ip_static_sysctl_init();
1700#endif
1701
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 /*
1703 * Add all the base protocols.
1704 */
1705
1706 if (inet_add_protocol(&icmp_protocol, IPPROTO_ICMP) < 0)
1707 printk(KERN_CRIT "inet_init: Cannot add ICMP protocol\n");
1708 if (inet_add_protocol(&udp_protocol, IPPROTO_UDP) < 0)
1709 printk(KERN_CRIT "inet_init: Cannot add UDP protocol\n");
1710 if (inet_add_protocol(&tcp_protocol, IPPROTO_TCP) < 0)
1711 printk(KERN_CRIT "inet_init: Cannot add TCP protocol\n");
1712#ifdef CONFIG_IP_MULTICAST
1713 if (inet_add_protocol(&igmp_protocol, IPPROTO_IGMP) < 0)
1714 printk(KERN_CRIT "inet_init: Cannot add IGMP protocol\n");
1715#endif
1716
1717 /* Register the socket-side information for inet_create. */
1718 for (r = &inetsw[0]; r < &inetsw[SOCK_MAX]; ++r)
1719 INIT_LIST_HEAD(r);
1720
1721 for (q = inetsw_array; q < &inetsw_array[INETSW_ARRAY_LEN]; ++q)
1722 inet_register_protosw(q);
1723
1724 /*
1725 * Set the ARP module up
1726 */
1727
1728 arp_init();
1729
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001730 /*
1731 * Set the IP module up
1732 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
1734 ip_init();
1735
Denis V. Lunev9b0f9762008-02-29 11:13:15 -08001736 tcp_v4_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
1738 /* Setup TCP slab cache for open requests. */
1739 tcp_init();
1740
Hideo Aoki95766ff2007-12-31 00:29:24 -08001741 /* Setup UDP memory threshold */
1742 udp_init();
1743
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001744 /* Add UDP-Lite (RFC 3828) */
1745 udplite4_register();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001747 ping_init();
1748
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 /*
1750 * Set the ICMP layer up
1751 */
1752
Denis V. Luneva5710d62008-02-29 11:14:50 -08001753 if (icmp_init() < 0)
1754 panic("Failed to create the ICMP control socket.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
1756 /*
1757 * Initialise the multicast router
1758 */
1759#if defined(CONFIG_IP_MROUTE)
Wang Chen03d2f892008-07-03 12:13:36 +08001760 if (ip_mr_init())
1761 printk(KERN_CRIT "inet_init: Cannot init ipv4 mroute\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762#endif
1763 /*
1764 * Initialise per-cpu ipv4 mibs
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001765 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
Stephen Hemminger132adf52007-03-08 20:44:43 -08001767 if (init_ipv4_mibs())
Wang Chen03d2f892008-07-03 12:13:36 +08001768 printk(KERN_CRIT "inet_init: Cannot init ipv4 mibs\n");
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001769
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 ipv4_proc_init();
1771
1772 ipfrag_init();
1773
Herbert Xu30e224d2005-07-05 14:40:10 -07001774 dev_add_pack(&ip_packet_type);
1775
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 rc = 0;
1777out:
1778 return rc;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001779out_unregister_raw_proto:
1780 proto_unregister(&raw_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781out_unregister_udp_proto:
1782 proto_unregister(&udp_prot);
Olaf Kirch321efff2006-09-27 16:33:45 -07001783out_unregister_tcp_proto:
1784 proto_unregister(&tcp_prot);
Amerigo Wange3826f12010-05-05 00:27:06 +00001785out_free_reserved_ports:
1786 kfree(sysctl_local_reserved_ports);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 goto out;
1788}
1789
Heiko Carstensa536e072006-04-28 15:19:17 -07001790fs_initcall(inet_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
1792/* ------------------------------------------------------------------------ */
1793
1794#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795static int __init ipv4_proc_init(void)
1796{
1797 int rc = 0;
1798
1799 if (raw_proc_init())
1800 goto out_raw;
1801 if (tcp4_proc_init())
1802 goto out_tcp;
1803 if (udp4_proc_init())
1804 goto out_udp;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001805 if (ping_proc_init())
1806 goto out_ping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 if (ip_misc_proc_init())
1808 goto out_misc;
1809out:
1810 return rc;
1811out_misc:
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001812 ping_proc_exit();
1813out_ping:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 udp4_proc_exit();
1815out_udp:
1816 tcp4_proc_exit();
1817out_tcp:
1818 raw_proc_exit();
1819out_raw:
1820 rc = -ENOMEM;
1821 goto out;
1822}
1823
1824#else /* CONFIG_PROC_FS */
1825static int __init ipv4_proc_init(void)
1826{
1827 return 0;
1828}
1829#endif /* CONFIG_PROC_FS */
1830
1831MODULE_ALIAS_NETPROTO(PF_INET);
1832