blob: a72639889642b3084755e2bb2b334db5965e0d7c [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>
Paul Gortmakerd3fc0352016-07-11 16:37:51 -040076#include <linux/kmod.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114#include <net/inet_common.h>
Thomas Graf045a0fa2015-07-23 10:08:44 +0200115#include <net/ip_tunnels.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116#include <net/xfrm.h>
Pavel Emelyanov9b4661b2008-07-18 04:01:44 -0700117#include <net/net_namespace.h>
Eric Dumazetaebda152013-04-29 05:58:52 +0000118#include <net/secure_seq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119#ifdef CONFIG_IP_MROUTE
120#include <linux/mroute.h>
121#endif
David Ahern3236b002015-09-29 20:07:14 -0700122#include <net/l3mdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Robert Loveec2622b2008-10-15 15:35:44 -0400124#ifdef CONFIG_ANDROID_PARANOID_NETWORK
125#include <linux/android_aid.h>
Chia-chi Yehc003a302009-06-30 11:23:04 +0800126
127static inline int current_has_network(void)
128{
129 return in_egroup_p(AID_INET) || capable(CAP_NET_RAW);
130}
131#else
132static inline int current_has_network(void)
133{
134 return 1;
135}
Robert Loveec2622b2008-10-15 15:35:44 -0400136#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Subash Abhinov Kasiviswanathan51ce9472017-06-01 13:27:12 -0600138int sysctl_reserved_port_bind __read_mostly = 1;
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140/* The inetsw table contains everything that inet_create needs to
141 * build a new socket.
142 */
143static struct list_head inetsw[SOCK_MAX];
144static DEFINE_SPINLOCK(inetsw_lock);
145
146/* New destruction routine */
147
148void inet_sock_destruct(struct sock *sk)
149{
150 struct inet_sock *inet = inet_sk(sk);
151
152 __skb_queue_purge(&sk->sk_receive_queue);
153 __skb_queue_purge(&sk->sk_error_queue);
154
Hideo Aoki95766ff2007-12-31 00:29:24 -0800155 sk_mem_reclaim(sk);
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 if (sk->sk_type == SOCK_STREAM && sk->sk_state != TCP_CLOSE) {
Eric Dumazet3d1427f2009-08-28 23:45:21 -0700158 pr_err("Attempt to release TCP socket in state %d %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 sk->sk_state, sk);
160 return;
161 }
162 if (!sock_flag(sk, SOCK_DEAD)) {
Eric Dumazet3d1427f2009-08-28 23:45:21 -0700163 pr_err("Attempt to release alive inet socket %p\n", sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 return;
165 }
166
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700167 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
168 WARN_ON(atomic_read(&sk->sk_wmem_alloc));
169 WARN_ON(sk->sk_wmem_queued);
170 WARN_ON(sk->sk_forward_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000172 kfree(rcu_dereference_protected(inet->inet_opt, 1));
Eric Dumazetb6c67122010-04-08 23:03:29 +0000173 dst_release(rcu_dereference_check(sk->sk_dst_cache, 1));
David S. Miller41063e92012-06-19 21:22:05 -0700174 dst_release(sk->sk_rx_dst);
Arnaldo Carvalho de Meloe6848972005-08-09 19:45:38 -0700175 sk_refcnt_debug_dec(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
Eric Dumazet3d1427f2009-08-28 23:45:21 -0700177EXPORT_SYMBOL(inet_sock_destruct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179/*
180 * The routines beyond this point handle the behaviour of an AF_INET
181 * socket object. Mostly it punts to the subprotocols of IP to do
182 * the work.
183 */
184
185/*
186 * Automatically bind an unbound socket.
187 */
188
189static int inet_autobind(struct sock *sk)
190{
191 struct inet_sock *inet;
192 /* We may need to bind the socket. */
193 lock_sock(sk);
194 inet = inet_sk(sk);
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000195 if (!inet->inet_num) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 if (sk->sk_prot->get_port(sk, 0)) {
197 release_sock(sk);
198 return -EAGAIN;
199 }
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000200 inet->inet_sport = htons(inet->inet_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 }
202 release_sock(sk);
203 return 0;
204}
205
206/*
207 * Move a socket into listening state.
208 */
209int inet_listen(struct socket *sock, int backlog)
210{
211 struct sock *sk = sock->sk;
212 unsigned char old_state;
213 int err;
214
215 lock_sock(sk);
216
217 err = -EINVAL;
218 if (sock->state != SS_UNCONNECTED || sock->type != SOCK_STREAM)
219 goto out;
220
221 old_state = sk->sk_state;
222 if (!((1 << old_state) & (TCPF_CLOSE | TCPF_LISTEN)))
223 goto out;
224
225 /* Really, if the socket is already in listen state
226 * we can only allow the backlog to be adjusted.
227 */
228 if (old_state != TCP_LISTEN) {
Yuchung Chengcebc5cb2016-08-22 17:17:54 -0700229 /* Enable TFO w/o requiring TCP_FASTOPEN socket option.
Jerry Chu83368862012-08-31 12:29:12 +0000230 * Note that only TCP sockets (SOCK_STREAM) will reach here.
Yuchung Chengcebc5cb2016-08-22 17:17:54 -0700231 * Also fastopen backlog may already been set via the option
232 * because the socket was in TCP_LISTEN state previously but
233 * was shutdown() rather than close().
Jerry Chu83368862012-08-31 12:29:12 +0000234 */
Yuchung Chengcebc5cb2016-08-22 17:17:54 -0700235 if ((sysctl_tcp_fastopen & TFO_SERVER_WO_SOCKOPT1) &&
236 (sysctl_tcp_fastopen & TFO_SERVER_ENABLE) &&
Eric Dumazet0536fcc2015-09-29 07:42:52 -0700237 !inet_csk(sk)->icsk_accept_queue.fastopenq.max_qlen) {
Yuchung Chengcebc5cb2016-08-22 17:17:54 -0700238 fastopen_queue_tune(sk, backlog);
Christoph Paaschdfea2aa2015-06-18 09:15:34 -0700239 tcp_fastopen_init_key_once(true);
Jerry Chu83368862012-08-31 12:29:12 +0000240 }
Yuchung Chengcebc5cb2016-08-22 17:17:54 -0700241
Eric Dumazet72a3eff2006-11-16 02:30:37 -0800242 err = inet_csk_listen_start(sk, backlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 if (err)
244 goto out;
245 }
246 sk->sk_max_ack_backlog = backlog;
247 err = 0;
248
249out:
250 release_sock(sk);
251 return err;
252}
Eric Dumazet3d1427f2009-08-28 23:45:21 -0700253EXPORT_SYMBOL(inet_listen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255/*
256 * Create an inet socket.
257 */
258
Eric Paris3f378b62009-11-05 22:18:14 -0800259static int inet_create(struct net *net, struct socket *sock, int protocol,
260 int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
262 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 struct inet_protosw *answer;
264 struct inet_sock *inet;
265 struct proto *answer_prot;
266 unsigned char answer_flags;
Arnaldo Carvalho de Melobb97d312005-08-09 20:19:14 -0700267 int try_loading_module = 0;
Herbert Xu86c8f9d2005-12-02 20:43:26 -0800268 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Hannes Frederic Sowa79462ad02015-12-14 22:03:39 +0100270 if (protocol < 0 || protocol >= IPPROTO_MAX)
271 return -EINVAL;
272
Robert Loveec2622b2008-10-15 15:35:44 -0400273 if (!current_has_network())
274 return -EACCES;
275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 sock->state = SS_UNCONNECTED;
277
278 /* Look for the requested type/protocol pair. */
Arnaldo Carvalho de Melobb97d312005-08-09 20:19:14 -0700279lookup_protocol:
Herbert Xu86c8f9d2005-12-02 20:43:26 -0800280 err = -ESOCKTNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 rcu_read_lock();
Paul E. McKenney696adfe2008-07-25 01:45:34 -0700282 list_for_each_entry_rcu(answer, &inetsw[sock->type], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Paul E. McKenney696adfe2008-07-25 01:45:34 -0700284 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 /* Check the non-wild match. */
286 if (protocol == answer->protocol) {
287 if (protocol != IPPROTO_IP)
288 break;
289 } else {
290 /* Check for the two wild cases. */
291 if (IPPROTO_IP == protocol) {
292 protocol = answer->protocol;
293 break;
294 }
295 if (IPPROTO_IP == answer->protocol)
296 break;
297 }
Herbert Xu86c8f9d2005-12-02 20:43:26 -0800298 err = -EPROTONOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
300
Paul E. McKenney696adfe2008-07-25 01:45:34 -0700301 if (unlikely(err)) {
Arnaldo Carvalho de Melobb97d312005-08-09 20:19:14 -0700302 if (try_loading_module < 2) {
303 rcu_read_unlock();
304 /*
305 * Be more specific, e.g. net-pf-2-proto-132-type-1
306 * (net-pf-PF_INET-proto-IPPROTO_SCTP-type-SOCK_STREAM)
307 */
308 if (++try_loading_module == 1)
309 request_module("net-pf-%d-proto-%d-type-%d",
310 PF_INET, protocol, sock->type);
311 /*
312 * Fall back to generic, e.g. net-pf-2-proto-132
313 * (net-pf-PF_INET-proto-IPPROTO_SCTP)
314 */
315 else
316 request_module("net-pf-%d-proto-%d",
317 PF_INET, protocol);
318 goto lookup_protocol;
319 } else
320 goto out_rcu_unlock;
321 }
322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 err = -EPERM;
Chia-chi Yehc003a302009-06-30 11:23:04 +0800324 if (sock->type == SOCK_RAW && !kern && !capable(CAP_NET_RAW))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 goto out_rcu_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327 sock->ops = answer->ops;
328 answer_prot = answer->prot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 answer_flags = answer->flags;
330 rcu_read_unlock();
331
Ian Morris51456b22015-04-03 09:17:26 +0100332 WARN_ON(!answer_prot->slab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 err = -ENOBUFS;
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500335 sk = sk_alloc(net, PF_INET, GFP_KERNEL, answer_prot, kern);
Ian Morris51456b22015-04-03 09:17:26 +0100336 if (!sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 goto out;
338
339 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 if (INET_PROTOSW_REUSE & answer_flags)
Pavel Emelyanov4a17fd52012-04-19 03:39:36 +0000341 sk->sk_reuse = SK_CAN_REUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343 inet = inet_sk(sk);
Paul Moore469de9b2007-01-09 14:37:06 -0800344 inet->is_icsk = (INET_PROTOSW_ICSK & answer_flags) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Jiri Olsa7b2ff182010-06-15 01:07:31 +0000346 inet->nodefrag = 0;
347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 if (SOCK_RAW == sock->type) {
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000349 inet->inet_num = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 if (IPPROTO_RAW == protocol)
351 inet->hdrincl = 1;
352 }
353
Hannes Frederic Sowa974eda12013-12-14 05:13:38 +0100354 if (net->ipv4.sysctl_ip_no_pmtu_disc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 inet->pmtudisc = IP_PMTUDISC_DONT;
356 else
357 inet->pmtudisc = IP_PMTUDISC_WANT;
358
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000359 inet->inet_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361 sock_init_data(sock, sk);
362
363 sk->sk_destruct = inet_sock_destruct;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 sk->sk_protocol = protocol;
365 sk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
366
367 inet->uc_ttl = -1;
368 inet->mc_loop = 1;
369 inet->mc_ttl = 1;
Nivedita Singhvif771bef2009-05-28 07:00:46 +0000370 inet->mc_all = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 inet->mc_index = 0;
372 inet->mc_list = NULL;
Jiri Benc4c507d22012-02-09 09:35:49 +0000373 inet->rcv_tos = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Arnaldo Carvalho de Meloe6848972005-08-09 19:45:38 -0700375 sk_refcnt_debug_inc(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000377 if (inet->inet_num) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 /* It assumes that any protocol which allows
379 * the user to assign a number at socket
380 * creation time automatically
381 * shares.
382 */
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000383 inet->inet_sport = htons(inet->inet_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 /* Add to protocol hash chains. */
Craig Gallek086c6532016-02-10 11:50:35 -0500385 err = sk->sk_prot->hash(sk);
386 if (err) {
387 sk_common_release(sk);
388 goto out;
389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 }
391
392 if (sk->sk_prot->init) {
393 err = sk->sk_prot->init(sk);
394 if (err)
395 sk_common_release(sk);
396 }
397out:
398 return err;
399out_rcu_unlock:
400 rcu_read_unlock();
401 goto out;
402}
403
404
405/*
406 * The peer socket should always be NULL (or else). When we call this
407 * function we are destroying the object and from then on nobody
408 * should refer to it.
409 */
410int inet_release(struct socket *sock)
411{
412 struct sock *sk = sock->sk;
413
414 if (sk) {
415 long timeout;
416
417 /* Applications forget to leave groups before exiting */
418 ip_mc_drop_socket(sk);
419
420 /* If linger is set, we don't return until the close
421 * is complete. Otherwise we return immediately. The
422 * actually closing is done the same either way.
423 *
424 * If the close is due to the process exiting, we never
425 * linger..
426 */
427 timeout = 0;
428 if (sock_flag(sk, SOCK_LINGER) &&
429 !(current->flags & PF_EXITING))
430 timeout = sk->sk_lingertime;
431 sock->sk = NULL;
432 sk->sk_prot->close(sk, timeout);
433 }
434 return 0;
435}
Eric Dumazet3d1427f2009-08-28 23:45:21 -0700436EXPORT_SYMBOL(inet_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438int inet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
439{
440 struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
441 struct sock *sk = sock->sk;
442 struct inet_sock *inet = inet_sk(sk);
Eric W. Biederman35946982012-11-16 03:03:12 +0000443 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 unsigned short snum;
445 int chk_addr_ret;
David Ahern9b8ff512015-09-01 14:26:35 -0600446 u32 tb_id = RT_TABLE_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 int err;
448
449 /* If the socket has its own bind function then use it. (RAW) */
450 if (sk->sk_prot->bind) {
451 err = sk->sk_prot->bind(sk, uaddr, addr_len);
452 goto out;
453 }
454 err = -EINVAL;
455 if (addr_len < sizeof(struct sockaddr_in))
456 goto out;
457
Marcus Meissnerc349a522011-07-04 01:30:29 +0000458 if (addr->sin_family != AF_INET) {
Eric Dumazet29c486d2011-08-30 18:57:00 -0400459 /* Compatibility games : accept AF_UNSPEC (mapped to AF_INET)
460 * only if s_addr is INADDR_ANY.
461 */
Marcus Meissnerc349a522011-07-04 01:30:29 +0000462 err = -EAFNOSUPPORT;
Eric Dumazet29c486d2011-08-30 18:57:00 -0400463 if (addr->sin_family != AF_UNSPEC ||
464 addr->sin_addr.s_addr != htonl(INADDR_ANY))
465 goto out;
Marcus Meissnerc349a522011-07-04 01:30:29 +0000466 }
Marcus Meissnerd0733d22011-06-01 21:05:22 -0700467
David Ahern3236b002015-09-29 20:07:14 -0700468 tb_id = l3mdev_fib_table_by_index(net, sk->sk_bound_dev_if) ? : tb_id;
David Ahern30bbaa12015-08-13 14:59:05 -0600469 chk_addr_ret = inet_addr_type_table(net, addr->sin_addr.s_addr, tb_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471 /* Not specified by any standard per-se, however it breaks too
472 * many applications when removed. It is unfortunate since
473 * allowing applications to make a non-local bind solves
474 * several problems with systems using dynamic addressing.
475 * (ie. your servers still start up even if your ISDN link
476 * is temporarily down)
477 */
478 err = -EADDRNOTAVAIL;
Vincent Bernat49a60152014-09-05 15:09:03 +0200479 if (!net->ipv4.sysctl_ip_nonlocal_bind &&
Tóth László Attilab9fb1502008-10-01 07:31:24 -0700480 !(inet->freebind || inet->transparent) &&
Al Viroe6f1ceb2008-03-17 22:44:53 -0700481 addr->sin_addr.s_addr != htonl(INADDR_ANY) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 chk_addr_ret != RTN_LOCAL &&
483 chk_addr_ret != RTN_MULTICAST &&
484 chk_addr_ret != RTN_BROADCAST)
485 goto out;
486
487 snum = ntohs(addr->sin_port);
488 err = -EACCES;
Eric W. Biederman35946982012-11-16 03:03:12 +0000489 if (snum && snum < PROT_SOCK &&
490 !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 goto out;
492
493 /* We keep a pair of addresses. rcv_saddr is the one
494 * used by hash lookups, and saddr is used for transmit.
495 *
496 * In the BSD API these are the same except where it
497 * would be illegal to use them (multicast/broadcast) in
498 * which case the sending device address is used.
499 */
500 lock_sock(sk);
501
502 /* Check these errors (active socket, double bind). */
503 err = -EINVAL;
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000504 if (sk->sk_state != TCP_CLOSE || inet->inet_num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 goto out_release_sock;
506
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000507 inet->inet_rcv_saddr = inet->inet_saddr = addr->sin_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 if (chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST)
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000509 inet->inet_saddr = 0; /* Use device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 /* Make sure we are allowed to bind here. */
Eric Dumazet90c337d2015-06-06 21:17:57 -0700512 if ((snum || !inet->bind_address_no_port) &&
513 sk->sk_prot->get_port(sk, snum)) {
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000514 inet->inet_saddr = inet->inet_rcv_saddr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 err = -EADDRINUSE;
516 goto out_release_sock;
517 }
518
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000519 if (inet->inet_rcv_saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
521 if (snum)
522 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000523 inet->inet_sport = htons(inet->inet_num);
524 inet->inet_daddr = 0;
525 inet->inet_dport = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 sk_dst_reset(sk);
527 err = 0;
528out_release_sock:
529 release_sock(sk);
530out:
531 return err;
532}
Eric Dumazet3d1427f2009-08-28 23:45:21 -0700533EXPORT_SYMBOL(inet_bind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Daniel Baluta5e73ea12012-04-15 01:34:41 +0000535int inet_dgram_connect(struct socket *sock, struct sockaddr *uaddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 int addr_len, int flags)
537{
538 struct sock *sk = sock->sk;
539
Changli Gao6503d962010-03-31 22:58:26 +0000540 if (addr_len < sizeof(uaddr->sa_family))
541 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 if (uaddr->sa_family == AF_UNSPEC)
543 return sk->sk_prot->disconnect(sk, flags);
544
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000545 if (!inet_sk(sk)->inet_num && inet_autobind(sk))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 return -EAGAIN;
Joe Perchese3192692012-06-03 17:41:40 +0000547 return sk->sk_prot->connect(sk, uaddr, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548}
Eric Dumazet3d1427f2009-08-28 23:45:21 -0700549EXPORT_SYMBOL(inet_dgram_connect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Yuchung Cheng783237e2012-07-19 06:43:07 +0000551static long inet_wait_for_connect(struct sock *sk, long timeo, int writebias)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
WANG Cong14135f32016-11-01 16:04:36 -0700553 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
WANG Cong14135f32016-11-01 16:04:36 -0700555 add_wait_queue(sk_sleep(sk), &wait);
Yuchung Cheng783237e2012-07-19 06:43:07 +0000556 sk->sk_write_pending += writebias;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558 /* Basic assumption: if someone sets sk->sk_err, he _must_
559 * change state of the socket from TCP_SYN_*.
560 * Connect() does not allow to get error notifications
561 * without closing the socket.
562 */
563 while ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
564 release_sock(sk);
WANG Cong14135f32016-11-01 16:04:36 -0700565 timeo = wait_woken(&wait, TASK_INTERRUPTIBLE, timeo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 lock_sock(sk);
567 if (signal_pending(current) || !timeo)
568 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 }
WANG Cong14135f32016-11-01 16:04:36 -0700570 remove_wait_queue(sk_sleep(sk), &wait);
Yuchung Cheng783237e2012-07-19 06:43:07 +0000571 sk->sk_write_pending -= writebias;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 return timeo;
573}
574
575/*
576 * Connect to a remote host. There is regrettably still a little
577 * TCP 'magic' in here.
578 */
Yuchung Chengcf60af02012-07-19 06:43:09 +0000579int __inet_stream_connect(struct socket *sock, struct sockaddr *uaddr,
580 int addr_len, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
582 struct sock *sk = sock->sk;
583 int err;
584 long timeo;
585
Wei Wangd55e6302017-11-12 15:11:18 +0900586 /*
587 * uaddr can be NULL and addr_len can be 0 if:
588 * sk is a TCP fastopen active socket and
589 * TCP_FASTOPEN_CONNECT sockopt is set and
590 * we already have a valid cookie for this socket.
591 * In this case, user can call write() after connect().
592 * write() will invoke tcp_sendmsg_fastopen() which calls
593 * __inet_stream_connect().
594 */
595 if (uaddr) {
596 if (addr_len < sizeof(uaddr->sa_family))
597 return -EINVAL;
Changli Gao6503d962010-03-31 22:58:26 +0000598
Wei Wangd55e6302017-11-12 15:11:18 +0900599 if (uaddr->sa_family == AF_UNSPEC) {
600 err = sk->sk_prot->disconnect(sk, flags);
601 sock->state = err ? SS_DISCONNECTING : SS_UNCONNECTED;
602 goto out;
603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 }
605
606 switch (sock->state) {
607 default:
608 err = -EINVAL;
609 goto out;
610 case SS_CONNECTED:
611 err = -EISCONN;
612 goto out;
613 case SS_CONNECTING:
Wei Wangd55e6302017-11-12 15:11:18 +0900614 if (inet_sk(sk)->defer_connect)
615 err = -EINPROGRESS;
616 else
617 err = -EALREADY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 /* Fall out of switch with err, set for this state */
619 break;
620 case SS_UNCONNECTED:
621 err = -EISCONN;
622 if (sk->sk_state != TCP_CLOSE)
623 goto out;
624
625 err = sk->sk_prot->connect(sk, uaddr, addr_len);
626 if (err < 0)
627 goto out;
628
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900629 sock->state = SS_CONNECTING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Wei Wangd55e6302017-11-12 15:11:18 +0900631 if (!err && inet_sk(sk)->defer_connect)
632 goto out;
633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 /* Just entered SS_CONNECTING state; the only
635 * difference is that return value in non-blocking
636 * case is EINPROGRESS, rather than EALREADY.
637 */
638 err = -EINPROGRESS;
639 break;
640 }
641
642 timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
643
644 if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
Yuchung Cheng783237e2012-07-19 06:43:07 +0000645 int writebias = (sk->sk_protocol == IPPROTO_TCP) &&
646 tcp_sk(sk)->fastopen_req &&
647 tcp_sk(sk)->fastopen_req->data ? 1 : 0;
648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 /* Error code is set above */
Yuchung Cheng783237e2012-07-19 06:43:07 +0000650 if (!timeo || !inet_wait_for_connect(sk, timeo, writebias))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 goto out;
652
653 err = sock_intr_errno(timeo);
654 if (signal_pending(current))
655 goto out;
656 }
657
658 /* Connection was closed by RST, timeout, ICMP error
659 * or another process disconnected us.
660 */
661 if (sk->sk_state == TCP_CLOSE)
662 goto sock_error;
663
664 /* sk->sk_err may be not zero now, if RECVERR was ordered by user
665 * and error was received after socket entered established state.
666 * Hence, it is handled normally after connect() return successfully.
667 */
668
669 sock->state = SS_CONNECTED;
670 err = 0;
671out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 return err;
673
674sock_error:
675 err = sock_error(sk) ? : -ECONNABORTED;
676 sock->state = SS_UNCONNECTED;
677 if (sk->sk_prot->disconnect(sk, flags))
678 sock->state = SS_DISCONNECTING;
679 goto out;
680}
Yuchung Chengcf60af02012-07-19 06:43:09 +0000681EXPORT_SYMBOL(__inet_stream_connect);
682
683int inet_stream_connect(struct socket *sock, struct sockaddr *uaddr,
684 int addr_len, int flags)
685{
686 int err;
687
688 lock_sock(sock->sk);
689 err = __inet_stream_connect(sock, uaddr, addr_len, flags);
690 release_sock(sock->sk);
691 return err;
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) &
Jerry Chu83368862012-08-31 12:29:12 +0000712 (TCPF_ESTABLISHED | TCPF_SYN_RECV |
713 TCPF_CLOSE_WAIT | TCPF_CLOSE)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
715 sock_graft(sk2, newsock);
716
717 newsock->state = SS_CONNECTED;
718 err = 0;
719 release_sock(sk2);
720do_err:
721 return err;
722}
Eric Dumazet3d1427f2009-08-28 23:45:21 -0700723EXPORT_SYMBOL(inet_accept);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725
726/*
727 * This does both peername and sockname.
728 */
729int inet_getname(struct socket *sock, struct sockaddr *uaddr,
730 int *uaddr_len, int peer)
731{
732 struct sock *sk = sock->sk;
733 struct inet_sock *inet = inet_sk(sk);
Cyrill Gorcunov38bfd8f2009-10-29 02:59:18 -0700734 DECLARE_SOCKADDR(struct sockaddr_in *, sin, uaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
736 sin->sin_family = AF_INET;
737 if (peer) {
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000738 if (!inet->inet_dport ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
740 peer == 1))
741 return -ENOTCONN;
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000742 sin->sin_port = inet->inet_dport;
743 sin->sin_addr.s_addr = inet->inet_daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 } else {
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000745 __be32 addr = inet->inet_rcv_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 if (!addr)
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000747 addr = inet->inet_saddr;
748 sin->sin_port = inet->inet_sport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 sin->sin_addr.s_addr = addr;
750 }
751 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
752 *uaddr_len = sizeof(*sin);
753 return 0;
754}
Eric Dumazet3d1427f2009-08-28 23:45:21 -0700755EXPORT_SYMBOL(inet_getname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
Ying Xue1b784142015-03-02 15:37:48 +0800757int inet_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
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
Ying Xue1b784142015-03-02 15:37:48 +0800768 return sk->sk_prot->sendmsg(sk, msg, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769}
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
Ying Xue1b784142015-03-02 15:37:48 +0800790int inet_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
791 int flags)
Tom Herbertfec5e652010-04-16 16:01:27 -0700792{
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
Ying Xue1b784142015-03-02 15:37:48 +0800799 err = sk->sk_prot->recvmsg(sk, msg, size, flags & MSG_DONTWAIT,
Tom Herbertfec5e652010-04-16 16:01:27 -0700800 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:
906 err = devinet_ioctl(net, cmd, (void __user *)arg);
907 break;
908 default:
909 if (sk->sk_prot->ioctl)
910 err = sk->sk_prot->ioctl(sk, cmd, arg);
911 else
912 err = -ENOIOCTLCMD;
913 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 }
915 return err;
916}
Eric Dumazet3d1427f2009-08-28 23:45:21 -0700917EXPORT_SYMBOL(inet_ioctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Eric W. Biederman709b46e2011-01-29 16:15:56 +0000919#ifdef CONFIG_COMPAT
Gerrit Renker686dc6b2011-10-15 09:26:56 +0000920static int inet_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
Eric W. Biederman709b46e2011-01-29 16:15:56 +0000921{
922 struct sock *sk = sock->sk;
923 int err = -ENOIOCTLCMD;
924
925 if (sk->sk_prot->compat_ioctl)
926 err = sk->sk_prot->compat_ioctl(sk, cmd, arg);
927
928 return err;
929}
930#endif
931
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800932const struct proto_ops inet_stream_ops = {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800933 .family = PF_INET,
934 .owner = THIS_MODULE,
935 .release = inet_release,
936 .bind = inet_bind,
937 .connect = inet_stream_connect,
938 .socketpair = sock_no_socketpair,
939 .accept = inet_accept,
940 .getname = inet_getname,
941 .poll = tcp_poll,
942 .ioctl = inet_ioctl,
943 .listen = inet_listen,
944 .shutdown = inet_shutdown,
945 .setsockopt = sock_common_setsockopt,
946 .getsockopt = sock_common_getsockopt,
Changli Gao7ba42912010-07-10 20:41:55 +0000947 .sendmsg = inet_sendmsg,
Tom Herbertfec5e652010-04-16 16:01:27 -0700948 .recvmsg = inet_recvmsg,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800949 .mmap = sock_no_mmap,
Changli Gao7ba42912010-07-10 20:41:55 +0000950 .sendpage = inet_sendpage,
Jens Axboe9c55e012007-11-06 23:30:13 -0800951 .splice_read = tcp_splice_read,
Tom Herbert32035582016-08-28 14:43:18 -0700952 .read_sock = tcp_read_sock,
953 .peek_len = tcp_peek_len,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800954#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800955 .compat_setsockopt = compat_sock_common_setsockopt,
956 .compat_getsockopt = compat_sock_common_getsockopt,
Eric W. Biederman709b46e2011-01-29 16:15:56 +0000957 .compat_ioctl = inet_compat_ioctl,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800958#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959};
Eric Dumazet3d1427f2009-08-28 23:45:21 -0700960EXPORT_SYMBOL(inet_stream_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800962const struct proto_ops inet_dgram_ops = {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800963 .family = PF_INET,
964 .owner = THIS_MODULE,
965 .release = inet_release,
966 .bind = inet_bind,
967 .connect = inet_dgram_connect,
968 .socketpair = sock_no_socketpair,
969 .accept = sock_no_accept,
970 .getname = inet_getname,
971 .poll = udp_poll,
972 .ioctl = inet_ioctl,
973 .listen = sock_no_listen,
974 .shutdown = inet_shutdown,
975 .setsockopt = sock_common_setsockopt,
976 .getsockopt = sock_common_getsockopt,
977 .sendmsg = inet_sendmsg,
Tom Herbertfec5e652010-04-16 16:01:27 -0700978 .recvmsg = inet_recvmsg,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800979 .mmap = sock_no_mmap,
980 .sendpage = inet_sendpage,
samanthakumar627d2d62016-04-05 12:41:16 -0400981 .set_peek_off = sk_set_peek_off,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800982#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800983 .compat_setsockopt = compat_sock_common_setsockopt,
984 .compat_getsockopt = compat_sock_common_getsockopt,
Eric W. Biederman709b46e2011-01-29 16:15:56 +0000985 .compat_ioctl = inet_compat_ioctl,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800986#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987};
Eric Dumazet3d1427f2009-08-28 23:45:21 -0700988EXPORT_SYMBOL(inet_dgram_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
990/*
991 * For SOCK_RAW sockets; should be the same as inet_dgram_ops but without
992 * udp_poll
993 */
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800994static const struct proto_ops inet_sockraw_ops = {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800995 .family = PF_INET,
996 .owner = THIS_MODULE,
997 .release = inet_release,
998 .bind = inet_bind,
999 .connect = inet_dgram_connect,
1000 .socketpair = sock_no_socketpair,
1001 .accept = sock_no_accept,
1002 .getname = inet_getname,
1003 .poll = datagram_poll,
1004 .ioctl = inet_ioctl,
1005 .listen = sock_no_listen,
1006 .shutdown = inet_shutdown,
1007 .setsockopt = sock_common_setsockopt,
1008 .getsockopt = sock_common_getsockopt,
1009 .sendmsg = inet_sendmsg,
Tom Herbertfec5e652010-04-16 16:01:27 -07001010 .recvmsg = inet_recvmsg,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001011 .mmap = sock_no_mmap,
1012 .sendpage = inet_sendpage,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001013#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001014 .compat_setsockopt = compat_sock_common_setsockopt,
1015 .compat_getsockopt = compat_sock_common_getsockopt,
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001016 .compat_ioctl = inet_compat_ioctl,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001017#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018};
1019
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00001020static const struct net_proto_family inet_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 .family = PF_INET,
1022 .create = inet_create,
1023 .owner = THIS_MODULE,
1024};
1025
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026/* Upon startup we insert all the elements in inetsw_array[] into
1027 * the linked list inetsw.
1028 */
1029static struct inet_protosw inetsw_array[] =
1030{
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001031 {
1032 .type = SOCK_STREAM,
1033 .protocol = IPPROTO_TCP,
1034 .prot = &tcp_prot,
1035 .ops = &inet_stream_ops,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001036 .flags = INET_PROTOSW_PERMANENT |
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -08001037 INET_PROTOSW_ICSK,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001038 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001040 {
1041 .type = SOCK_DGRAM,
1042 .protocol = IPPROTO_UDP,
1043 .prot = &udp_prot,
1044 .ops = &inet_dgram_ops,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001045 .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,
Eric Dumazet0aa89f12017-06-03 09:29:25 -07001052 .ops = &inet_sockraw_ops,
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001053 .flags = INET_PROTOSW_REUSE,
1054 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
1056 {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001057 .type = SOCK_RAW,
1058 .protocol = IPPROTO_IP, /* wild card */
1059 .prot = &raw_prot,
1060 .ops = &inet_sockraw_ops,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001061 .flags = INET_PROTOSW_REUSE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 }
1063};
1064
Denis Chengc40f6ff2007-09-16 16:39:25 -07001065#define INETSW_ARRAY_LEN ARRAY_SIZE(inetsw_array)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
1067void inet_register_protosw(struct inet_protosw *p)
1068{
1069 struct list_head *lh;
1070 struct inet_protosw *answer;
1071 int protocol = p->protocol;
1072 struct list_head *last_perm;
1073
1074 spin_lock_bh(&inetsw_lock);
1075
1076 if (p->type >= SOCK_MAX)
1077 goto out_illegal;
1078
1079 /* If we are trying to override a permanent protocol, bail. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 last_perm = &inetsw[p->type];
1081 list_for_each(lh, &inetsw[p->type]) {
1082 answer = list_entry(lh, struct inet_protosw, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 /* Check only the non-wild match. */
Junwei Zhangf6c53332015-09-18 00:00:05 -04001084 if ((INET_PROTOSW_PERMANENT & answer->flags) == 0)
1085 break;
1086 if (protocol == answer->protocol)
1087 goto out_permanent;
1088 last_perm = lh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
1091 /* Add the new entry after the last permanent entry if any, so that
1092 * the new entry does not override a permanent entry when matched with
1093 * a wild-card protocol. But it is allowed to override any existing
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001094 * non-permanent entry. This means that when we remove this entry, the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 * system automatically returns to the old behavior.
1096 */
1097 list_add_rcu(&p->list, last_perm);
1098out:
1099 spin_unlock_bh(&inetsw_lock);
1100
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 return;
1102
1103out_permanent:
Joe Perches058bd4d2012-03-11 18:36:11 +00001104 pr_err("Attempt to override permanent protocol %d\n", protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 goto out;
1106
1107out_illegal:
Joe Perches058bd4d2012-03-11 18:36:11 +00001108 pr_err("Ignoring attempt to register invalid socket type %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 p->type);
1110 goto out;
1111}
Eric Dumazet3d1427f2009-08-28 23:45:21 -07001112EXPORT_SYMBOL(inet_register_protosw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
1114void inet_unregister_protosw(struct inet_protosw *p)
1115{
1116 if (INET_PROTOSW_PERMANENT & p->flags) {
Joe Perches058bd4d2012-03-11 18:36:11 +00001117 pr_err("Attempt to unregister permanent protocol %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 p->protocol);
1119 } else {
1120 spin_lock_bh(&inetsw_lock);
1121 list_del_rcu(&p->list);
1122 spin_unlock_bh(&inetsw_lock);
1123
1124 synchronize_net();
1125 }
1126}
Eric Dumazet3d1427f2009-08-28 23:45:21 -07001127EXPORT_SYMBOL(inet_unregister_protosw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Arnaldo Carvalho de Melo32519f12005-08-09 19:50:02 -07001129static int inet_sk_reselect_saddr(struct sock *sk)
1130{
1131 struct inet_sock *inet = inet_sk(sk);
Eric Dumazetc720c7e2009-10-15 06:30:45 +00001132 __be32 old_saddr = inet->inet_saddr;
Eric Dumazetc720c7e2009-10-15 06:30:45 +00001133 __be32 daddr = inet->inet_daddr;
David S. Miller6e869132011-05-06 16:18:04 -07001134 struct flowi4 *fl4;
David S. Millerb23dd4f2011-03-02 14:31:35 -08001135 struct rtable *rt;
1136 __be32 new_saddr;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001137 struct ip_options_rcu *inet_opt;
Arnaldo Carvalho de Melo32519f12005-08-09 19:50:02 -07001138
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001139 inet_opt = rcu_dereference_protected(inet->inet_opt,
Hannes Frederic Sowa1e1d04e2016-04-05 17:10:15 +02001140 lockdep_sock_is_held(sk));
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001141 if (inet_opt && inet_opt->opt.srr)
1142 daddr = inet_opt->opt.faddr;
Arnaldo Carvalho de Melo32519f12005-08-09 19:50:02 -07001143
1144 /* Query new route. */
David S. Miller6e869132011-05-06 16:18:04 -07001145 fl4 = &inet->cork.fl.u.ip4;
1146 rt = ip_route_connect(fl4, daddr, 0, RT_CONN_FLAGS(sk),
David S. Millerb23dd4f2011-03-02 14:31:35 -08001147 sk->sk_bound_dev_if, sk->sk_protocol,
Steffen Klassert0e0d44a2013-08-28 08:04:14 +02001148 inet->inet_sport, inet->inet_dport, sk);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001149 if (IS_ERR(rt))
1150 return PTR_ERR(rt);
Arnaldo Carvalho de Melo32519f12005-08-09 19:50:02 -07001151
Changli Gaod8d1f302010-06-10 23:31:35 -07001152 sk_setup_caps(sk, &rt->dst);
Arnaldo Carvalho de Melo32519f12005-08-09 19:50:02 -07001153
David S. Miller6e869132011-05-06 16:18:04 -07001154 new_saddr = fl4->saddr;
Arnaldo Carvalho de Melo32519f12005-08-09 19:50:02 -07001155
1156 if (new_saddr == old_saddr)
1157 return 0;
1158
Nikolay Borisov287b7f32016-02-15 12:11:29 +02001159 if (sock_net(sk)->ipv4.sysctl_ip_dynaddr > 1) {
Joe Perches058bd4d2012-03-11 18:36:11 +00001160 pr_info("%s(): shifting inet->saddr from %pI4 to %pI4\n",
1161 __func__, &old_saddr, &new_saddr);
Arnaldo Carvalho de Melo32519f12005-08-09 19:50:02 -07001162 }
1163
Eric Dumazetc720c7e2009-10-15 06:30:45 +00001164 inet->inet_saddr = inet->inet_rcv_saddr = new_saddr;
Arnaldo Carvalho de Melo32519f12005-08-09 19:50:02 -07001165
1166 /*
1167 * XXX The only one ugly spot where we need to
1168 * XXX really change the sockets identity after
1169 * XXX it has entered the hashes. -DaveM
1170 *
1171 * Besides that, it does not check for connection
1172 * uniqueness. Wait for troubles.
1173 */
Craig Gallek086c6532016-02-10 11:50:35 -05001174 return __sk_prot_rehash(sk);
Arnaldo Carvalho de Melo32519f12005-08-09 19:50:02 -07001175}
1176
1177int inet_sk_rebuild_header(struct sock *sk)
1178{
1179 struct inet_sock *inet = inet_sk(sk);
1180 struct rtable *rt = (struct rtable *)__sk_dst_check(sk, 0);
Al Viro3ca3c682006-09-27 18:28:07 -07001181 __be32 daddr;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001182 struct ip_options_rcu *inet_opt;
David S. Miller6e869132011-05-06 16:18:04 -07001183 struct flowi4 *fl4;
Arnaldo Carvalho de Melo32519f12005-08-09 19:50:02 -07001184 int err;
1185
1186 /* Route is OK, nothing to do. */
1187 if (rt)
1188 return 0;
1189
1190 /* Reroute. */
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001191 rcu_read_lock();
1192 inet_opt = rcu_dereference(inet->inet_opt);
Eric Dumazetc720c7e2009-10-15 06:30:45 +00001193 daddr = inet->inet_daddr;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001194 if (inet_opt && inet_opt->opt.srr)
1195 daddr = inet_opt->opt.faddr;
1196 rcu_read_unlock();
David S. Miller6e869132011-05-06 16:18:04 -07001197 fl4 = &inet->cork.fl.u.ip4;
1198 rt = ip_route_output_ports(sock_net(sk), fl4, sk, daddr, inet->inet_saddr,
David S. Miller78fbfd82011-03-12 00:00:52 -05001199 inet->inet_dport, inet->inet_sport,
1200 sk->sk_protocol, RT_CONN_FLAGS(sk),
1201 sk->sk_bound_dev_if);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001202 if (!IS_ERR(rt)) {
1203 err = 0;
Changli Gaod8d1f302010-06-10 23:31:35 -07001204 sk_setup_caps(sk, &rt->dst);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001205 } else {
1206 err = PTR_ERR(rt);
1207
Arnaldo Carvalho de Melo32519f12005-08-09 19:50:02 -07001208 /* Routing failed... */
1209 sk->sk_route_caps = 0;
1210 /*
1211 * Other protocols have to map its equivalent state to TCP_SYN_SENT.
1212 * DCCP maps its DCCP_REQUESTING state to TCP_SYN_SENT. -acme
1213 */
Nikolay Borisov287b7f32016-02-15 12:11:29 +02001214 if (!sock_net(sk)->ipv4.sysctl_ip_dynaddr ||
Arnaldo Carvalho de Melo32519f12005-08-09 19:50:02 -07001215 sk->sk_state != TCP_SYN_SENT ||
1216 (sk->sk_userlocks & SOCK_BINDADDR_LOCK) ||
1217 (err = inet_sk_reselect_saddr(sk)) != 0)
1218 sk->sk_err_soft = -err;
1219 }
1220
1221 return err;
1222}
Arnaldo Carvalho de Melo32519f12005-08-09 19:50:02 -07001223EXPORT_SYMBOL(inet_sk_rebuild_header);
1224
Tom Herbertb8921ca2016-05-18 09:06:23 -07001225struct sk_buff *inet_gso_segment(struct sk_buff *skb,
1226 netdev_features_t features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07001227{
Steffen Klassert07b26c92016-09-19 12:58:47 +02001228 bool udpfrag = false, fixedid = false, gso_partial, encap;
Herbert Xuf4c50d92006-06-22 03:02:40 -07001229 struct sk_buff *segs = ERR_PTR(-EINVAL);
Vlad Yasevichbca49f82012-11-15 08:49:14 +00001230 const struct net_offload *ops;
Eric Dumazet3347c962013-10-19 11:42:56 -07001231 unsigned int offset = 0;
David S. Millerf9242b62012-06-19 18:56:21 -07001232 struct iphdr *iph;
Alexander Duyck802ab552016-04-10 21:45:03 -04001233 int proto, tot_len;
Eric Dumazet3347c962013-10-19 11:42:56 -07001234 int nhoff;
Herbert Xuf4c50d92006-06-22 03:02:40 -07001235 int ihl;
1236 int id;
1237
Eric Dumazet3347c962013-10-19 11:42:56 -07001238 skb_reset_network_header(skb);
1239 nhoff = skb_network_header(skb) - skb_mac_header(skb);
Herbert Xubbcf4672006-07-03 19:38:35 -07001240 if (unlikely(!pskb_may_pull(skb, sizeof(*iph))))
Herbert Xuf4c50d92006-06-22 03:02:40 -07001241 goto out;
1242
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001243 iph = ip_hdr(skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07001244 ihl = iph->ihl * 4;
1245 if (ihl < sizeof(*iph))
1246 goto out;
1247
Eric Dumazet47d27aa2013-10-18 13:13:27 -07001248 id = ntohs(iph->id);
1249 proto = iph->protocol;
1250
1251 /* Warning: after this point, iph might be no longer valid */
Herbert Xubbcf4672006-07-03 19:38:35 -07001252 if (unlikely(!pskb_may_pull(skb, ihl)))
Herbert Xuf4c50d92006-06-22 03:02:40 -07001253 goto out;
Eric Dumazet47d27aa2013-10-18 13:13:27 -07001254 __skb_pull(skb, ihl);
Herbert Xuf4c50d92006-06-22 03:02:40 -07001255
Eric Dumazet8c3a8972013-10-27 18:18:16 -07001256 encap = SKB_GSO_CB(skb)->encap_level > 0;
1257 if (encap)
Florian Westphal1e16aa32014-10-20 13:49:16 +02001258 features &= skb->dev->hw_enc_features;
Eric Dumazet3347c962013-10-19 11:42:56 -07001259 SKB_GSO_CB(skb)->encap_level += ihl;
Pravin B Shelar73136262013-03-07 13:21:51 +00001260
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03001261 skb_reset_transport_header(skb);
Eric Dumazet47d27aa2013-10-18 13:13:27 -07001262
Herbert Xuf4c50d92006-06-22 03:02:40 -07001263 segs = ERR_PTR(-EPROTONOSUPPORT);
1264
Alexander Duyckcbc53e02016-04-10 21:44:51 -04001265 if (!skb->encapsulation || encap) {
1266 udpfrag = !!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP);
1267 fixedid = !!(skb_shinfo(skb)->gso_type & SKB_GSO_TCP_FIXEDID);
1268
1269 /* fixed ID is invalid if DF bit is not set */
Arnaldo Carvalho de Meloa5108872016-11-28 12:36:58 -03001270 if (fixedid && !(ip_hdr(skb)->frag_off & htons(IP_DF)))
Alexander Duyckcbc53e02016-04-10 21:44:51 -04001271 goto out;
1272 }
Eric Dumazetdcd60772013-11-07 18:32:06 -08001273
Vlad Yasevichbca49f82012-11-15 08:49:14 +00001274 ops = rcu_dereference(inet_offloads[proto]);
Vlad Yasevichf191a1d2012-11-15 08:49:23 +00001275 if (likely(ops && ops->callbacks.gso_segment))
1276 segs = ops->callbacks.gso_segment(skb, features);
Herbert Xuf4c50d92006-06-22 03:02:40 -07001277
YOSHIFUJI Hideaki / 吉藤英明50c3a482013-01-22 06:32:49 +00001278 if (IS_ERR_OR_NULL(segs))
Herbert Xuf4c50d92006-06-22 03:02:40 -07001279 goto out;
1280
Steffen Klassert07b26c92016-09-19 12:58:47 +02001281 gso_partial = !!(skb_shinfo(segs)->gso_type & SKB_GSO_PARTIAL);
1282
Herbert Xuf4c50d92006-06-22 03:02:40 -07001283 skb = segs;
1284 do {
Eric Dumazet3347c962013-10-19 11:42:56 -07001285 iph = (struct iphdr *)(skb_mac_header(skb) + nhoff);
Eric Dumazet8c3a8972013-10-27 18:18:16 -07001286 if (udpfrag) {
Sridhar Samudralad7ca4cc2009-07-09 08:09:47 +00001287 iph->frag_off = htons(offset >> 3);
Ian Morris00db4122015-04-03 09:17:27 +01001288 if (skb->next)
Sridhar Samudralad7ca4cc2009-07-09 08:09:47 +00001289 iph->frag_off |= htons(IP_MF);
Eric Dumazet3347c962013-10-19 11:42:56 -07001290 offset += skb->len - nhoff - ihl;
Alexander Duyck802ab552016-04-10 21:45:03 -04001291 tot_len = skb->len - nhoff;
1292 } else if (skb_is_gso(skb)) {
1293 if (!fixedid) {
1294 iph->id = htons(id);
1295 id += skb_shinfo(skb)->gso_segs;
1296 }
Steffen Klassert07b26c92016-09-19 12:58:47 +02001297
1298 if (gso_partial)
1299 tot_len = skb_shinfo(skb)->gso_size +
1300 SKB_GSO_CB(skb)->data_offset +
1301 skb->head - (unsigned char *)iph;
1302 else
1303 tot_len = skb->len - nhoff;
Alexander Duyck802ab552016-04-10 21:45:03 -04001304 } else {
1305 if (!fixedid)
1306 iph->id = htons(id++);
1307 tot_len = skb->len - nhoff;
Pravin B Shelar490ab082013-02-22 07:30:30 +00001308 }
Alexander Duyck802ab552016-04-10 21:45:03 -04001309 iph->tot_len = htons(tot_len);
Eric Dumazet47d27aa2013-10-18 13:13:27 -07001310 ip_send_check(iph);
Eric Dumazet8c3a8972013-10-27 18:18:16 -07001311 if (encap)
Eric Dumazet3347c962013-10-19 11:42:56 -07001312 skb_reset_inner_headers(skb);
Eric Dumazet3347c962013-10-19 11:42:56 -07001313 skb->network_header = (u8 *)iph - skb->head;
Toke Høiland-Jørgensen2b797b62018-09-13 16:43:07 +02001314 skb_reset_mac_len(skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07001315 } while ((skb = skb->next));
1316
1317out:
1318 return segs;
1319}
Tom Herbertb8921ca2016-05-18 09:06:23 -07001320EXPORT_SYMBOL(inet_gso_segment);
Herbert Xuf4c50d92006-06-22 03:02:40 -07001321
Tom Herbertb8921ca2016-05-18 09:06:23 -07001322struct sk_buff **inet_gro_receive(struct sk_buff **head, struct sk_buff *skb)
Herbert Xu73cc19f2008-12-15 23:41:09 -08001323{
Vlad Yasevichbca49f82012-11-15 08:49:14 +00001324 const struct net_offload *ops;
Herbert Xu73cc19f2008-12-15 23:41:09 -08001325 struct sk_buff **pp = NULL;
1326 struct sk_buff *p;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001327 const struct iphdr *iph;
Herbert Xua5b1cf22009-05-26 18:50:28 +00001328 unsigned int hlen;
1329 unsigned int off;
Herbert Xu1075f3f2009-05-26 18:50:29 +00001330 unsigned int id;
Herbert Xu73cc19f2008-12-15 23:41:09 -08001331 int flush = 1;
1332 int proto;
Herbert Xu73cc19f2008-12-15 23:41:09 -08001333
Herbert Xua5b1cf22009-05-26 18:50:28 +00001334 off = skb_gro_offset(skb);
1335 hlen = off + sizeof(*iph);
1336 iph = skb_gro_header_fast(skb, off);
1337 if (skb_gro_header_hard(skb, hlen)) {
1338 iph = skb_gro_header_slow(skb, hlen, off);
1339 if (unlikely(!iph))
1340 goto out;
1341 }
Herbert Xu73cc19f2008-12-15 23:41:09 -08001342
David S. Millerf9242b62012-06-19 18:56:21 -07001343 proto = iph->protocol;
Herbert Xu73cc19f2008-12-15 23:41:09 -08001344
1345 rcu_read_lock();
Vlad Yasevichbca49f82012-11-15 08:49:14 +00001346 ops = rcu_dereference(inet_offloads[proto]);
Vlad Yasevichf191a1d2012-11-15 08:49:23 +00001347 if (!ops || !ops->callbacks.gro_receive)
Herbert Xu73cc19f2008-12-15 23:41:09 -08001348 goto out_unlock;
1349
Herbert Xua5ad24b2009-02-08 18:00:39 +00001350 if (*(u8 *)iph != 0x45)
Herbert Xu73cc19f2008-12-15 23:41:09 -08001351 goto out_unlock;
1352
Eric Dumazeta9e050f2012-08-05 22:34:50 +00001353 if (unlikely(ip_fast_csum((u8 *)iph, 5)))
Herbert Xu73cc19f2008-12-15 23:41:09 -08001354 goto out_unlock;
1355
Eric Dumazet0eae88f2010-04-20 19:06:52 -07001356 id = ntohl(*(__be32 *)&iph->id);
Eric Dumazetdb8caf32013-05-31 11:18:10 +00001357 flush = (u16)((ntohl(*(__be32 *)iph) ^ skb_gro_len(skb)) | (id & ~IP_DF));
Herbert Xu1075f3f2009-05-26 18:50:29 +00001358 id >>= 16;
Herbert Xu73cc19f2008-12-15 23:41:09 -08001359
1360 for (p = *head; p; p = p->next) {
1361 struct iphdr *iph2;
1362
1363 if (!NAPI_GRO_CB(p)->same_flow)
1364 continue;
1365
Jerry Chu299603e82013-12-11 20:53:45 -08001366 iph2 = (struct iphdr *)(p->data + off);
1367 /* The above works because, with the exception of the top
1368 * (inner most) layer, we only aggregate pkts with the same
1369 * hdr length so all the hdrs we'll need to verify will start
1370 * at the same offset.
1371 */
Herbert Xua5ad24b2009-02-08 18:00:39 +00001372 if ((iph->protocol ^ iph2->protocol) |
Eric Dumazet0eae88f2010-04-20 19:06:52 -07001373 ((__force u32)iph->saddr ^ (__force u32)iph2->saddr) |
1374 ((__force u32)iph->daddr ^ (__force u32)iph2->daddr)) {
Herbert Xu73cc19f2008-12-15 23:41:09 -08001375 NAPI_GRO_CB(p)->same_flow = 0;
1376 continue;
1377 }
1378
1379 /* All fields must match except length and checksum. */
1380 NAPI_GRO_CB(p)->flush |=
Herbert Xua5ad24b2009-02-08 18:00:39 +00001381 (iph->ttl ^ iph2->ttl) |
Eric Dumazeta9e050f2012-08-05 22:34:50 +00001382 (iph->tos ^ iph2->tos) |
Jerry Chubf5a7552014-01-07 10:23:19 -08001383 ((iph->frag_off ^ iph2->frag_off) & htons(IP_DF));
Herbert Xu73cc19f2008-12-15 23:41:09 -08001384
1385 NAPI_GRO_CB(p)->flush |= flush;
Alexander Duyck3f590472018-04-10 18:18:01 -07001386
1387 /* For non-atomic datagrams we need to save the IP ID offset
1388 * to be included later. If the frame has the DF bit set
1389 * we must ignore the IP ID value as per RFC 6864.
1390 */
1391 if (iph2->frag_off & htons(IP_DF))
1392 continue;
1393
1394 /* We must save the offset as it is possible to have multiple
1395 * flows using the same protocol and address pairs so we
1396 * need to wait until we can validate this is part of the
1397 * same flow with a 5-tuple or better to avoid unnecessary
1398 * collisions between flows.
1399 */
1400 NAPI_GRO_CB(p)->flush_id |= ntohs(iph2->id) ^
1401 (u16)(id - NAPI_GRO_CB(p)->count);
Herbert Xu73cc19f2008-12-15 23:41:09 -08001402 }
1403
1404 NAPI_GRO_CB(skb)->flush |= flush;
Jerry Chu299603e82013-12-11 20:53:45 -08001405 skb_set_network_header(skb, off);
1406 /* The above will be needed by the transport layer if there is one
1407 * immediately following this IP hdr.
1408 */
1409
Eric Dumazet2c804d02014-09-30 22:12:05 -07001410 /* Note : No need to call skb_gro_postpull_rcsum() here,
1411 * as we already checked checksum over ipv4 header was 0
1412 */
Herbert Xu86911732009-01-29 14:19:50 +00001413 skb_gro_pull(skb, sizeof(*iph));
1414 skb_set_transport_header(skb, skb_gro_offset(skb));
Herbert Xu73cc19f2008-12-15 23:41:09 -08001415
Sabrina Dubrocafcd91dd2016-10-20 15:58:02 +02001416 pp = call_gro_receive(ops->callbacks.gro_receive, head, skb);
Herbert Xu73cc19f2008-12-15 23:41:09 -08001417
1418out_unlock:
1419 rcu_read_unlock();
1420
1421out:
1422 NAPI_GRO_CB(skb)->flush |= flush;
1423
1424 return pp;
1425}
Tom Herbertb8921ca2016-05-18 09:06:23 -07001426EXPORT_SYMBOL(inet_gro_receive);
Herbert Xu73cc19f2008-12-15 23:41:09 -08001427
Jesse Grossfac8e0f2016-03-19 09:32:01 -07001428static struct sk_buff **ipip_gro_receive(struct sk_buff **head,
1429 struct sk_buff *skb)
1430{
1431 if (NAPI_GRO_CB(skb)->encap_mark) {
1432 NAPI_GRO_CB(skb)->flush = 1;
1433 return NULL;
1434 }
1435
1436 NAPI_GRO_CB(skb)->encap_mark = 1;
1437
1438 return inet_gro_receive(head, skb);
1439}
1440
Deepa Dinamani822c8682016-02-27 00:32:15 -08001441#define SECONDS_PER_DAY 86400
1442
1443/* inet_current_timestamp - Return IP network timestamp
1444 *
1445 * Return milliseconds since midnight in network byte order.
1446 */
1447__be32 inet_current_timestamp(void)
1448{
1449 u32 secs;
1450 u32 msecs;
1451 struct timespec64 ts;
1452
1453 ktime_get_real_ts64(&ts);
1454
1455 /* Get secs since midnight. */
1456 (void)div_u64_rem(ts.tv_sec, SECONDS_PER_DAY, &secs);
1457 /* Convert to msecs. */
1458 msecs = secs * MSEC_PER_SEC;
1459 /* Convert nsec to msec. */
1460 msecs += (u32)ts.tv_nsec / NSEC_PER_MSEC;
1461
1462 /* Convert to network byte order. */
Deepa Dinamani3ba9d302016-03-21 18:21:26 -07001463 return htonl(msecs);
Deepa Dinamani822c8682016-02-27 00:32:15 -08001464}
1465EXPORT_SYMBOL(inet_current_timestamp);
1466
Willem de Bruijnf4713a32014-11-26 14:53:02 -05001467int inet_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len)
1468{
1469 if (sk->sk_family == AF_INET)
1470 return ip_recv_error(sk, msg, len, addr_len);
1471#if IS_ENABLED(CONFIG_IPV6)
1472 if (sk->sk_family == AF_INET6)
1473 return pingv6_ops.ipv6_recv_error(sk, msg, len, addr_len);
1474#endif
1475 return -EINVAL;
1476}
1477
Tom Herbertb8921ca2016-05-18 09:06:23 -07001478int inet_gro_complete(struct sk_buff *skb, int nhoff)
Herbert Xu73cc19f2008-12-15 23:41:09 -08001479{
Jerry Chu299603e82013-12-11 20:53:45 -08001480 __be16 newlen = htons(skb->len - nhoff);
1481 struct iphdr *iph = (struct iphdr *)(skb->data + nhoff);
Vlad Yasevichbca49f82012-11-15 08:49:14 +00001482 const struct net_offload *ops;
David S. Millerf9242b62012-06-19 18:56:21 -07001483 int proto = iph->protocol;
1484 int err = -ENOSYS;
Herbert Xu73cc19f2008-12-15 23:41:09 -08001485
Paolo Abenib07eed82017-03-07 18:33:31 +01001486 if (skb->encapsulation) {
1487 skb_set_inner_protocol(skb, cpu_to_be16(ETH_P_IP));
Jerry Chuc3caf112014-07-14 15:54:46 -07001488 skb_set_inner_network_header(skb, nhoff);
Paolo Abenib07eed82017-03-07 18:33:31 +01001489 }
Jerry Chuc3caf112014-07-14 15:54:46 -07001490
Herbert Xu73cc19f2008-12-15 23:41:09 -08001491 csum_replace2(&iph->check, iph->tot_len, newlen);
1492 iph->tot_len = newlen;
1493
1494 rcu_read_lock();
Vlad Yasevichbca49f82012-11-15 08:49:14 +00001495 ops = rcu_dereference(inet_offloads[proto]);
Vlad Yasevichf191a1d2012-11-15 08:49:23 +00001496 if (WARN_ON(!ops || !ops->callbacks.gro_complete))
Herbert Xu73cc19f2008-12-15 23:41:09 -08001497 goto out_unlock;
1498
Jerry Chu299603e82013-12-11 20:53:45 -08001499 /* Only need to add sizeof(*iph) to get to the next hdr below
1500 * because any hdr with option will have been flushed in
1501 * inet_gro_receive().
1502 */
1503 err = ops->callbacks.gro_complete(skb, nhoff + sizeof(*iph));
Herbert Xu73cc19f2008-12-15 23:41:09 -08001504
1505out_unlock:
1506 rcu_read_unlock();
1507
1508 return err;
1509}
Tom Herbertb8921ca2016-05-18 09:06:23 -07001510EXPORT_SYMBOL(inet_gro_complete);
Herbert Xu73cc19f2008-12-15 23:41:09 -08001511
Jesse Grossb8cba752016-03-19 09:32:00 -07001512static int ipip_gro_complete(struct sk_buff *skb, int nhoff)
1513{
1514 skb->encapsulation = 1;
Tom Herbert7e133182016-05-18 09:06:10 -07001515 skb_shinfo(skb)->gso_type |= SKB_GSO_IPXIP4;
Jesse Grossb8cba752016-03-19 09:32:00 -07001516 return inet_gro_complete(skb, nhoff);
1517}
1518
Denis V. Luneveee4fe42008-04-03 14:27:58 -07001519int inet_ctl_sock_create(struct sock **sk, unsigned short family,
Denis V. Lunev56772422008-04-03 14:28:30 -07001520 unsigned short type, unsigned char protocol,
1521 struct net *net)
Denis V. Lunev3d58b5f2008-04-03 14:22:32 -07001522{
Denis V. Luneveee4fe42008-04-03 14:27:58 -07001523 struct socket *sock;
Eric W. Biederman26abe142015-05-08 21:10:31 -05001524 int rc = sock_create_kern(net, family, type, protocol, &sock);
Denis V. Lunev3d58b5f2008-04-03 14:22:32 -07001525
1526 if (rc == 0) {
Denis V. Luneveee4fe42008-04-03 14:27:58 -07001527 *sk = sock->sk;
1528 (*sk)->sk_allocation = GFP_ATOMIC;
Denis V. Lunev3d58b5f2008-04-03 14:22:32 -07001529 /*
1530 * Unhash it so that IP input processing does not even see it,
1531 * we do not wish this socket to see incoming packets.
1532 */
Denis V. Luneveee4fe42008-04-03 14:27:58 -07001533 (*sk)->sk_prot->unhash(*sk);
Denis V. Lunev3d58b5f2008-04-03 14:22:32 -07001534 }
1535 return rc;
1536}
Denis V. Lunev3d58b5f2008-04-03 14:22:32 -07001537EXPORT_SYMBOL_GPL(inet_ctl_sock_create);
1538
Raghavendra K Tc4c6bc32015-08-30 11:29:41 +05301539u64 snmp_get_cpu_field(void __percpu *mib, int cpu, int offt)
1540{
1541 return *(((unsigned long *)per_cpu_ptr(mib, cpu)) + offt);
1542}
1543EXPORT_SYMBOL_GPL(snmp_get_cpu_field);
1544
WANG Cong698365f2014-05-05 15:55:55 -07001545unsigned long snmp_fold_field(void __percpu *mib, int offt)
Herbert Xu5e0f0432007-04-24 21:53:35 -07001546{
1547 unsigned long res = 0;
WANG Cong698365f2014-05-05 15:55:55 -07001548 int i;
Herbert Xu5e0f0432007-04-24 21:53:35 -07001549
WANG Cong698365f2014-05-05 15:55:55 -07001550 for_each_possible_cpu(i)
Raghavendra K Tc4c6bc32015-08-30 11:29:41 +05301551 res += snmp_get_cpu_field(mib, i, offt);
Herbert Xu5e0f0432007-04-24 21:53:35 -07001552 return res;
1553}
1554EXPORT_SYMBOL_GPL(snmp_fold_field);
1555
Eric Dumazet4ce3c182010-06-30 13:31:19 -07001556#if BITS_PER_LONG==32
1557
David S. Miller80ec1922015-08-30 22:40:44 -07001558u64 snmp_get_cpu_field64(void __percpu *mib, int cpu, int offt,
Raghavendra K Tc4c6bc32015-08-30 11:29:41 +05301559 size_t syncp_offset)
1560{
1561 void *bhptr;
1562 struct u64_stats_sync *syncp;
1563 u64 v;
1564 unsigned int start;
1565
1566 bhptr = per_cpu_ptr(mib, cpu);
1567 syncp = (struct u64_stats_sync *)(bhptr + syncp_offset);
1568 do {
1569 start = u64_stats_fetch_begin_irq(syncp);
1570 v = *(((u64 *)bhptr) + offt);
1571 } while (u64_stats_fetch_retry_irq(syncp, start));
1572
1573 return v;
1574}
1575EXPORT_SYMBOL_GPL(snmp_get_cpu_field64);
1576
WANG Cong698365f2014-05-05 15:55:55 -07001577u64 snmp_fold_field64(void __percpu *mib, int offt, size_t syncp_offset)
Eric Dumazet4ce3c182010-06-30 13:31:19 -07001578{
1579 u64 res = 0;
1580 int cpu;
1581
1582 for_each_possible_cpu(cpu) {
Madalin Bucurd1bfc622015-08-31 15:46:07 +03001583 res += snmp_get_cpu_field64(mib, cpu, offt, syncp_offset);
Eric Dumazet4ce3c182010-06-30 13:31:19 -07001584 }
1585 return res;
1586}
1587EXPORT_SYMBOL_GPL(snmp_fold_field64);
1588#endif
1589
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590#ifdef CONFIG_IP_MULTICAST
Alexey Dobriyan32613092009-09-14 12:21:47 +00001591static const struct net_protocol igmp_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 .handler = igmp_rcv,
Alexey Dobriyanb4ee07d2008-12-25 16:42:23 -08001593 .netns_ok = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594};
1595#endif
1596
Alexey Dobriyan32613092009-09-14 12:21:47 +00001597static const struct net_protocol tcp_protocol = {
David S. Miller41063e92012-06-19 21:22:05 -07001598 .early_demux = tcp_v4_early_demux,
1599 .handler = tcp_v4_rcv,
1600 .err_handler = tcp_v4_err,
David S. Miller41063e92012-06-19 21:22:05 -07001601 .no_policy = 1,
1602 .netns_ok = 1,
Hannes Frederic Sowa8ed1dc42014-01-09 10:01:17 +01001603 .icmp_strict_tag_validation = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604};
1605
Alexey Dobriyan32613092009-09-14 12:21:47 +00001606static const struct net_protocol udp_protocol = {
Shawn Bohrer421b3882013-10-07 11:01:39 -05001607 .early_demux = udp_v4_early_demux,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 .handler = udp_rcv,
1609 .err_handler = udp_err,
1610 .no_policy = 1,
Denis V. Lunev92f1fec2008-03-24 15:34:06 -07001611 .netns_ok = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612};
1613
Alexey Dobriyan32613092009-09-14 12:21:47 +00001614static const struct net_protocol icmp_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 .handler = icmp_rcv,
Li Wei5b052042013-02-21 22:18:44 +00001616 .err_handler = icmp_err,
Herbert Xu8b7817f2007-12-12 10:44:43 -08001617 .no_policy = 1,
Denis V. Lunev92f1fec2008-03-24 15:34:06 -07001618 .netns_ok = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619};
1620
Pavel Emelyanov9b4661b2008-07-18 04:01:44 -07001621static __net_init int ipv4_mib_init_net(struct net *net)
1622{
John Stultz827da442013-10-07 15:51:58 -07001623 int i;
1624
WANG Cong698365f2014-05-05 15:55:55 -07001625 net->mib.tcp_statistics = alloc_percpu(struct tcp_mib);
1626 if (!net->mib.tcp_statistics)
Pavel Emelyanov57ef42d2008-07-18 04:02:08 -07001627 goto err_tcp_mib;
WANG Cong698365f2014-05-05 15:55:55 -07001628 net->mib.ip_statistics = alloc_percpu(struct ipstats_mib);
1629 if (!net->mib.ip_statistics)
Pavel Emelyanova20f5792008-07-18 04:02:42 -07001630 goto err_ip_mib;
John Stultz827da442013-10-07 15:51:58 -07001631
1632 for_each_possible_cpu(i) {
1633 struct ipstats_mib *af_inet_stats;
WANG Cong698365f2014-05-05 15:55:55 -07001634 af_inet_stats = per_cpu_ptr(net->mib.ip_statistics, i);
John Stultz827da442013-10-07 15:51:58 -07001635 u64_stats_init(&af_inet_stats->syncp);
John Stultz827da442013-10-07 15:51:58 -07001636 }
1637
WANG Cong698365f2014-05-05 15:55:55 -07001638 net->mib.net_statistics = alloc_percpu(struct linux_mib);
1639 if (!net->mib.net_statistics)
Pavel Emelyanov61a7e262008-07-18 04:03:08 -07001640 goto err_net_mib;
WANG Cong698365f2014-05-05 15:55:55 -07001641 net->mib.udp_statistics = alloc_percpu(struct udp_mib);
1642 if (!net->mib.udp_statistics)
Pavel Emelyanov2f275f92008-07-18 04:03:27 -07001643 goto err_udp_mib;
WANG Cong698365f2014-05-05 15:55:55 -07001644 net->mib.udplite_statistics = alloc_percpu(struct udp_mib);
1645 if (!net->mib.udplite_statistics)
Pavel Emelyanov386019d2008-07-18 04:03:45 -07001646 goto err_udplite_mib;
WANG Cong698365f2014-05-05 15:55:55 -07001647 net->mib.icmp_statistics = alloc_percpu(struct icmp_mib);
1648 if (!net->mib.icmp_statistics)
Pavel Emelyanovb60538a2008-07-18 04:04:02 -07001649 goto err_icmp_mib;
Eric Dumazetacb32ba2011-11-08 13:04:43 +00001650 net->mib.icmpmsg_statistics = kzalloc(sizeof(struct icmpmsg_mib),
1651 GFP_KERNEL);
1652 if (!net->mib.icmpmsg_statistics)
Pavel Emelyanov923c6582008-07-18 04:04:22 -07001653 goto err_icmpmsg_mib;
Pavel Emelyanov57ef42d2008-07-18 04:02:08 -07001654
1655 tcp_mib_init(net);
Pavel Emelyanov9b4661b2008-07-18 04:01:44 -07001656 return 0;
Pavel Emelyanov57ef42d2008-07-18 04:02:08 -07001657
Pavel Emelyanov923c6582008-07-18 04:04:22 -07001658err_icmpmsg_mib:
WANG Cong698365f2014-05-05 15:55:55 -07001659 free_percpu(net->mib.icmp_statistics);
Pavel Emelyanovb60538a2008-07-18 04:04:02 -07001660err_icmp_mib:
WANG Cong698365f2014-05-05 15:55:55 -07001661 free_percpu(net->mib.udplite_statistics);
Pavel Emelyanov386019d2008-07-18 04:03:45 -07001662err_udplite_mib:
WANG Cong698365f2014-05-05 15:55:55 -07001663 free_percpu(net->mib.udp_statistics);
Pavel Emelyanov2f275f92008-07-18 04:03:27 -07001664err_udp_mib:
WANG Cong698365f2014-05-05 15:55:55 -07001665 free_percpu(net->mib.net_statistics);
Pavel Emelyanov61a7e262008-07-18 04:03:08 -07001666err_net_mib:
WANG Cong698365f2014-05-05 15:55:55 -07001667 free_percpu(net->mib.ip_statistics);
Pavel Emelyanova20f5792008-07-18 04:02:42 -07001668err_ip_mib:
WANG Cong698365f2014-05-05 15:55:55 -07001669 free_percpu(net->mib.tcp_statistics);
Pavel Emelyanov57ef42d2008-07-18 04:02:08 -07001670err_tcp_mib:
1671 return -ENOMEM;
Pavel Emelyanov9b4661b2008-07-18 04:01:44 -07001672}
1673
1674static __net_exit void ipv4_mib_exit_net(struct net *net)
1675{
Eric Dumazetacb32ba2011-11-08 13:04:43 +00001676 kfree(net->mib.icmpmsg_statistics);
WANG Cong698365f2014-05-05 15:55:55 -07001677 free_percpu(net->mib.icmp_statistics);
1678 free_percpu(net->mib.udplite_statistics);
1679 free_percpu(net->mib.udp_statistics);
1680 free_percpu(net->mib.net_statistics);
1681 free_percpu(net->mib.ip_statistics);
1682 free_percpu(net->mib.tcp_statistics);
Pavel Emelyanov9b4661b2008-07-18 04:01:44 -07001683}
1684
1685static __net_initdata struct pernet_operations ipv4_mib_ops = {
1686 .init = ipv4_mib_init_net,
1687 .exit = ipv4_mib_exit_net,
1688};
1689
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690static int __init init_ipv4_mibs(void)
1691{
Pavel Emelyanovd89cbbb2008-07-18 04:04:51 -07001692 return register_pernet_subsys(&ipv4_mib_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693}
1694
Cong Wangc9d8f1a2014-05-06 11:02:49 -07001695static __net_init int inet_init_net(struct net *net)
1696{
1697 /*
1698 * Set defaults for local port range
1699 */
1700 seqlock_init(&net->ipv4.ip_local_ports.lock);
1701 net->ipv4.ip_local_ports.range[0] = 32768;
Eric Dumazet07f4c902015-05-24 14:49:35 -07001702 net->ipv4.ip_local_ports.range[1] = 60999;
Cong Wangba6b9182014-05-06 11:02:50 -07001703
1704 seqlock_init(&net->ipv4.ping_group_range.lock);
1705 /*
1706 * Sane defaults - nobody may create ping sockets.
1707 * Boot scripts should set this to distro-specific group.
1708 */
1709 net->ipv4.ping_group_range.range[0] = make_kgid(&init_user_ns, 1);
1710 net->ipv4.ping_group_range.range[1] = make_kgid(&init_user_ns, 0);
Ezequiel Garcia049bbf52016-05-20 13:21:10 -03001711
1712 /* Default values for sysctl-controlled parameters.
1713 * We set them here, in case sysctl is not compiled.
1714 */
1715 net->ipv4.sysctl_ip_default_ttl = IPDEFTTL;
1716 net->ipv4.sysctl_ip_dynaddr = 0;
1717 net->ipv4.sysctl_ip_early_demux = 1;
1718
Nikolay Borisovf628c9d2017-08-09 14:38:04 +03001719 /* Some igmp sysctl, whose values are always used */
1720 net->ipv4.sysctl_igmp_max_memberships = 20;
1721 net->ipv4.sysctl_igmp_max_msf = 10;
1722 /* IGMP reports for link-local multicast groups are enabled by default */
1723 net->ipv4.sysctl_igmp_llm_reports = 1;
1724 net->ipv4.sysctl_igmp_qrv = 2;
1725
Cong Wangc9d8f1a2014-05-06 11:02:49 -07001726 return 0;
1727}
1728
1729static __net_exit void inet_exit_net(struct net *net)
1730{
1731}
1732
1733static __net_initdata struct pernet_operations af_inet_ops = {
1734 .init = inet_init_net,
1735 .exit = inet_exit_net,
1736};
1737
1738static int __init init_inet_pernet_ops(void)
1739{
1740 return register_pernet_subsys(&af_inet_ops);
1741}
1742
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743static int ipv4_proc_init(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
Herbert Xu30e224d2005-07-05 14:40:10 -07001745/*
1746 * IP protocol layer initialiser
1747 */
1748
Vlad Yasevich22061d82012-11-15 08:49:11 +00001749static struct packet_offload ip_packet_offload __read_mostly = {
1750 .type = cpu_to_be16(ETH_P_IP),
Vlad Yasevichf191a1d2012-11-15 08:49:23 +00001751 .callbacks = {
Vlad Yasevichf191a1d2012-11-15 08:49:23 +00001752 .gso_segment = inet_gso_segment,
1753 .gro_receive = inet_gro_receive,
1754 .gro_complete = inet_gro_complete,
1755 },
Herbert Xu30e224d2005-07-05 14:40:10 -07001756};
1757
Eric Dumazetcb32f512013-10-19 11:42:57 -07001758static const struct net_offload ipip_offload = {
1759 .callbacks = {
Eric Dumazetcb32f512013-10-19 11:42:57 -07001760 .gso_segment = inet_gso_segment,
Jesse Grossfac8e0f2016-03-19 09:32:01 -07001761 .gro_receive = ipip_gro_receive,
Jesse Grossb8cba752016-03-19 09:32:00 -07001762 .gro_complete = ipip_gro_complete,
Eric Dumazetcb32f512013-10-19 11:42:57 -07001763 },
1764};
1765
Vlad Yasevich808a8f82012-11-15 08:49:21 +00001766static int __init ipv4_offload_init(void)
1767{
1768 /*
1769 * Add offloads
1770 */
Daniel Borkmannda5bab02013-06-08 12:56:03 +02001771 if (udpv4_offload_init() < 0)
Vlad Yasevich808a8f82012-11-15 08:49:21 +00001772 pr_crit("%s: Cannot add UDP protocol offload\n", __func__);
Daniel Borkmann28850dc2013-06-07 05:11:46 +00001773 if (tcpv4_offload_init() < 0)
1774 pr_crit("%s: Cannot add TCP protocol offload\n", __func__);
Vlad Yasevich808a8f82012-11-15 08:49:21 +00001775
1776 dev_add_offload(&ip_packet_offload);
Eric Dumazetcb32f512013-10-19 11:42:57 -07001777 inet_add_offload(&ipip_offload, IPPROTO_IPIP);
Vlad Yasevich808a8f82012-11-15 08:49:21 +00001778 return 0;
1779}
1780
1781fs_initcall(ipv4_offload_init);
1782
1783static struct packet_type ip_packet_type __read_mostly = {
1784 .type = cpu_to_be16(ETH_P_IP),
1785 .func = ip_rcv,
1786};
1787
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788static int __init inet_init(void)
1789{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 struct inet_protosw *q;
1791 struct list_head *r;
1792 int rc = -EINVAL;
1793
Eyal Birgerb4772ef2015-03-01 14:58:29 +02001794 sock_skb_cb_check_size(sizeof(struct inet_skb_parm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
1796 rc = proto_register(&tcp_prot, 1);
1797 if (rc)
WANG Cong122ff242014-05-12 16:04:53 -07001798 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
1800 rc = proto_register(&udp_prot, 1);
1801 if (rc)
1802 goto out_unregister_tcp_proto;
1803
1804 rc = proto_register(&raw_prot, 1);
1805 if (rc)
1806 goto out_unregister_udp_proto;
1807
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001808 rc = proto_register(&ping_prot, 1);
1809 if (rc)
1810 goto out_unregister_raw_proto;
1811
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 /*
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001813 * Tell SOCKET that we are alive...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 */
1815
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001816 (void)sock_register(&inet_family_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
Al Virobd7b1532008-07-15 16:00:59 -04001818#ifdef CONFIG_SYSCTL
1819 ip_static_sysctl_init();
1820#endif
1821
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 /*
1823 * Add all the base protocols.
1824 */
1825
1826 if (inet_add_protocol(&icmp_protocol, IPPROTO_ICMP) < 0)
Joe Perches058bd4d2012-03-11 18:36:11 +00001827 pr_crit("%s: Cannot add ICMP protocol\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 if (inet_add_protocol(&udp_protocol, IPPROTO_UDP) < 0)
Joe Perches058bd4d2012-03-11 18:36:11 +00001829 pr_crit("%s: Cannot add UDP protocol\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 if (inet_add_protocol(&tcp_protocol, IPPROTO_TCP) < 0)
Joe Perches058bd4d2012-03-11 18:36:11 +00001831 pr_crit("%s: Cannot add TCP protocol\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832#ifdef CONFIG_IP_MULTICAST
1833 if (inet_add_protocol(&igmp_protocol, IPPROTO_IGMP) < 0)
Joe Perches058bd4d2012-03-11 18:36:11 +00001834 pr_crit("%s: Cannot add IGMP protocol\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835#endif
1836
1837 /* Register the socket-side information for inet_create. */
1838 for (r = &inetsw[0]; r < &inetsw[SOCK_MAX]; ++r)
1839 INIT_LIST_HEAD(r);
1840
1841 for (q = inetsw_array; q < &inetsw_array[INETSW_ARRAY_LEN]; ++q)
1842 inet_register_protosw(q);
1843
1844 /*
1845 * Set the ARP module up
1846 */
1847
1848 arp_init();
1849
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001850 /*
1851 * Set the IP module up
1852 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853
1854 ip_init();
1855
Denis V. Lunev9b0f9762008-02-29 11:13:15 -08001856 tcp_v4_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
1858 /* Setup TCP slab cache for open requests. */
1859 tcp_init();
1860
Hideo Aoki95766ff2007-12-31 00:29:24 -08001861 /* Setup UDP memory threshold */
1862 udp_init();
1863
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001864 /* Add UDP-Lite (RFC 3828) */
1865 udplite4_register();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001867 ping_init();
1868
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 /*
1870 * Set the ICMP layer up
1871 */
1872
Denis V. Luneva5710d62008-02-29 11:14:50 -08001873 if (icmp_init() < 0)
1874 panic("Failed to create the ICMP control socket.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
1876 /*
1877 * Initialise the multicast router
1878 */
1879#if defined(CONFIG_IP_MROUTE)
Wang Chen03d2f892008-07-03 12:13:36 +08001880 if (ip_mr_init())
Joe Perches058bd4d2012-03-11 18:36:11 +00001881 pr_crit("%s: Cannot init ipv4 mroute\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882#endif
Cong Wangc9d8f1a2014-05-06 11:02:49 -07001883
1884 if (init_inet_pernet_ops())
1885 pr_crit("%s: Cannot init ipv4 inet pernet ops\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 /*
1887 * Initialise per-cpu ipv4 mibs
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001888 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
Stephen Hemminger132adf52007-03-08 20:44:43 -08001890 if (init_ipv4_mibs())
Joe Perches058bd4d2012-03-11 18:36:11 +00001891 pr_crit("%s: Cannot init ipv4 mibs\n", __func__);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001892
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 ipv4_proc_init();
1894
1895 ipfrag_init();
1896
Herbert Xu30e224d2005-07-05 14:40:10 -07001897 dev_add_pack(&ip_packet_type);
1898
Thomas Graf045a0fa2015-07-23 10:08:44 +02001899 ip_tunnel_core_init();
1900
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 rc = 0;
1902out:
1903 return rc;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001904out_unregister_raw_proto:
1905 proto_unregister(&raw_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906out_unregister_udp_proto:
1907 proto_unregister(&udp_prot);
Olaf Kirch321efff2006-09-27 16:33:45 -07001908out_unregister_tcp_proto:
1909 proto_unregister(&tcp_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 goto out;
1911}
1912
Heiko Carstensa536e072006-04-28 15:19:17 -07001913fs_initcall(inet_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
1915/* ------------------------------------------------------------------------ */
1916
1917#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918static int __init ipv4_proc_init(void)
1919{
1920 int rc = 0;
1921
1922 if (raw_proc_init())
1923 goto out_raw;
1924 if (tcp4_proc_init())
1925 goto out_tcp;
1926 if (udp4_proc_init())
1927 goto out_udp;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001928 if (ping_proc_init())
1929 goto out_ping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 if (ip_misc_proc_init())
1931 goto out_misc;
1932out:
1933 return rc;
1934out_misc:
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001935 ping_proc_exit();
1936out_ping:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 udp4_proc_exit();
1938out_udp:
1939 tcp4_proc_exit();
1940out_tcp:
1941 raw_proc_exit();
1942out_raw:
1943 rc = -ENOMEM;
1944 goto out;
1945}
1946
1947#else /* CONFIG_PROC_FS */
1948static int __init ipv4_proc_init(void)
1949{
1950 return 0;
1951}
1952#endif /* CONFIG_PROC_FS */