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