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