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