12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * INET An implementation of the TCP/IP protocol suite for the LINUX 41da177e4SLinus Torvalds * operating system. INET is implemented using the BSD Socket 51da177e4SLinus Torvalds * interface as the means of communication with the user level. 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * PF_INET protocol family socket handler. 81da177e4SLinus Torvalds * 902c30a84SJesper Juhl * Authors: Ross Biro 101da177e4SLinus Torvalds * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG> 111da177e4SLinus Torvalds * Florian La Roche, <flla@stud.uni-sb.de> 121da177e4SLinus Torvalds * Alan Cox, <A.Cox@swansea.ac.uk> 131da177e4SLinus Torvalds * 141da177e4SLinus Torvalds * Changes (see also sock.c) 151da177e4SLinus Torvalds * 161da177e4SLinus Torvalds * piggy, 171da177e4SLinus Torvalds * Karl Knutson : Socket protocol table 181da177e4SLinus Torvalds * A.N.Kuznetsov : Socket death error in accept(). 191da177e4SLinus Torvalds * John Richardson : Fix non blocking error in connect() 201da177e4SLinus Torvalds * so sockets that fail to connect 211da177e4SLinus Torvalds * don't return -EINPROGRESS. 221da177e4SLinus Torvalds * Alan Cox : Asynchronous I/O support 231da177e4SLinus Torvalds * Alan Cox : Keep correct socket pointer on sock 241da177e4SLinus Torvalds * structures 251da177e4SLinus Torvalds * when accept() ed 261da177e4SLinus Torvalds * Alan Cox : Semantics of SO_LINGER aren't state 271da177e4SLinus Torvalds * moved to close when you look carefully. 281da177e4SLinus Torvalds * With this fixed and the accept bug fixed 291da177e4SLinus Torvalds * some RPC stuff seems happier. 301da177e4SLinus Torvalds * Niibe Yutaka : 4.4BSD style write async I/O 311da177e4SLinus Torvalds * Alan Cox, 321da177e4SLinus Torvalds * Tony Gale : Fixed reuse semantics. 331da177e4SLinus Torvalds * Alan Cox : bind() shouldn't abort existing but dead 341da177e4SLinus Torvalds * sockets. Stops FTP netin:.. I hope. 351da177e4SLinus Torvalds * Alan Cox : bind() works correctly for RAW sockets. 361da177e4SLinus Torvalds * Note that FreeBSD at least was broken 371da177e4SLinus Torvalds * in this respect so be careful with 381da177e4SLinus Torvalds * compatibility tests... 391da177e4SLinus Torvalds * Alan Cox : routing cache support 401da177e4SLinus Torvalds * Alan Cox : memzero the socket structure for 411da177e4SLinus Torvalds * compactness. 421da177e4SLinus Torvalds * Matt Day : nonblock connect error handler 431da177e4SLinus Torvalds * Alan Cox : Allow large numbers of pending sockets 441da177e4SLinus Torvalds * (eg for big web sites), but only if 451da177e4SLinus Torvalds * specifically application requested. 461da177e4SLinus Torvalds * Alan Cox : New buffering throughout IP. Used 471da177e4SLinus Torvalds * dumbly. 481da177e4SLinus Torvalds * Alan Cox : New buffering now used smartly. 491da177e4SLinus Torvalds * Alan Cox : BSD rather than common sense 501da177e4SLinus Torvalds * interpretation of listen. 511da177e4SLinus Torvalds * Germano Caronni : Assorted small races. 521da177e4SLinus Torvalds * Alan Cox : sendmsg/recvmsg basic support. 531da177e4SLinus Torvalds * Alan Cox : Only sendmsg/recvmsg now supported. 541da177e4SLinus Torvalds * Alan Cox : Locked down bind (see security list). 551da177e4SLinus Torvalds * Alan Cox : Loosened bind a little. 561da177e4SLinus Torvalds * Mike McLagan : ADD/DEL DLCI Ioctls 571da177e4SLinus Torvalds * Willy Konynenberg : Transparent proxying support. 581da177e4SLinus Torvalds * David S. Miller : New socket lookup architecture. 591da177e4SLinus Torvalds * Some other random speedups. 601da177e4SLinus Torvalds * Cyrus Durgin : Cleaned up file for kmod hacks. 611da177e4SLinus Torvalds * Andi Kleen : Fix inet_stream_connect TCP race. 621da177e4SLinus Torvalds */ 631da177e4SLinus Torvalds 64afd46503SJoe Perches #define pr_fmt(fmt) "IPv4: " fmt 65afd46503SJoe Perches 66f4c50d99SHerbert Xu #include <linux/err.h> 671da177e4SLinus Torvalds #include <linux/errno.h> 681da177e4SLinus Torvalds #include <linux/types.h> 691da177e4SLinus Torvalds #include <linux/socket.h> 701da177e4SLinus Torvalds #include <linux/in.h> 711da177e4SLinus Torvalds #include <linux/kernel.h> 72d3fc0353SPaul Gortmaker #include <linux/kmod.h> 731da177e4SLinus Torvalds #include <linux/sched.h> 741da177e4SLinus Torvalds #include <linux/timer.h> 751da177e4SLinus Torvalds #include <linux/string.h> 761da177e4SLinus Torvalds #include <linux/sockios.h> 771da177e4SLinus Torvalds #include <linux/net.h> 784fc268d2SRandy Dunlap #include <linux/capability.h> 791da177e4SLinus Torvalds #include <linux/fcntl.h> 801da177e4SLinus Torvalds #include <linux/mm.h> 811da177e4SLinus Torvalds #include <linux/interrupt.h> 821da177e4SLinus Torvalds #include <linux/stat.h> 831da177e4SLinus Torvalds #include <linux/init.h> 841da177e4SLinus Torvalds #include <linux/poll.h> 851da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h> 86b3da2cf3SDavid S. Miller #include <linux/random.h> 875a0e3ad6STejun Heo #include <linux/slab.h> 881da177e4SLinus Torvalds 897c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 901da177e4SLinus Torvalds 911da177e4SLinus Torvalds #include <linux/inet.h> 921da177e4SLinus Torvalds #include <linux/igmp.h> 9314c85021SArnaldo Carvalho de Melo #include <linux/inetdevice.h> 941da177e4SLinus Torvalds #include <linux/netdevice.h> 9573cc19f1SHerbert Xu #include <net/checksum.h> 961da177e4SLinus Torvalds #include <net/ip.h> 971da177e4SLinus Torvalds #include <net/protocol.h> 981da177e4SLinus Torvalds #include <net/arp.h> 991da177e4SLinus Torvalds #include <net/route.h> 1001da177e4SLinus Torvalds #include <net/ip_fib.h> 101295f7324SArnaldo Carvalho de Melo #include <net/inet_connection_sock.h> 1024721031cSEric Dumazet #include <net/gro.h> 1031da177e4SLinus Torvalds #include <net/tcp.h> 1041da177e4SLinus Torvalds #include <net/udp.h> 105ba4e58ecSGerrit Renker #include <net/udplite.h> 106c319b4d7SVasiliy Kulikov #include <net/ping.h> 1071da177e4SLinus Torvalds #include <linux/skbuff.h> 1081da177e4SLinus Torvalds #include <net/sock.h> 1091da177e4SLinus Torvalds #include <net/raw.h> 1101da177e4SLinus Torvalds #include <net/icmp.h> 1111da177e4SLinus Torvalds #include <net/inet_common.h> 112045a0fa0SThomas Graf #include <net/ip_tunnels.h> 1131da177e4SLinus Torvalds #include <net/xfrm.h> 1149b4661bdSPavel Emelyanov #include <net/net_namespace.h> 115aebda156SEric Dumazet #include <net/secure_seq.h> 1161da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE 1171da177e4SLinus Torvalds #include <linux/mroute.h> 1181da177e4SLinus Torvalds #endif 1193236b004SDavid Ahern #include <net/l3mdev.h> 120dc13c876SChristoph Hellwig #include <net/compat.h> 1211da177e4SLinus Torvalds 122563e0bb0SYafang Shao #include <trace/events/sock.h> 1231da177e4SLinus Torvalds 1241da177e4SLinus Torvalds /* The inetsw table contains everything that inet_create needs to 1251da177e4SLinus Torvalds * build a new socket. 1261da177e4SLinus Torvalds */ 1271da177e4SLinus Torvalds static struct list_head inetsw[SOCK_MAX]; 1281da177e4SLinus Torvalds static DEFINE_SPINLOCK(inetsw_lock); 1291da177e4SLinus Torvalds 1301da177e4SLinus Torvalds /* New destruction routine */ 1311da177e4SLinus Torvalds 1321da177e4SLinus Torvalds void inet_sock_destruct(struct sock *sk) 1331da177e4SLinus Torvalds { 1341da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 1351da177e4SLinus Torvalds 1361da177e4SLinus Torvalds __skb_queue_purge(&sk->sk_receive_queue); 1371da177e4SLinus Torvalds __skb_queue_purge(&sk->sk_error_queue); 1381da177e4SLinus Torvalds 1392bb2f5fbSWei Wang sk_mem_reclaim_final(sk); 14095766fffSHideo Aoki 1411da177e4SLinus Torvalds if (sk->sk_type == SOCK_STREAM && sk->sk_state != TCP_CLOSE) { 1423d1427f8SEric Dumazet pr_err("Attempt to release TCP socket in state %d %p\n", 1431da177e4SLinus Torvalds sk->sk_state, sk); 1441da177e4SLinus Torvalds return; 1451da177e4SLinus Torvalds } 1461da177e4SLinus Torvalds if (!sock_flag(sk, SOCK_DEAD)) { 1473d1427f8SEric Dumazet pr_err("Attempt to release alive inet socket %p\n", sk); 1481da177e4SLinus Torvalds return; 1491da177e4SLinus Torvalds } 1501da177e4SLinus Torvalds 1513e7f2b8dSEric Dumazet WARN_ON_ONCE(atomic_read(&sk->sk_rmem_alloc)); 1523e7f2b8dSEric Dumazet WARN_ON_ONCE(refcount_read(&sk->sk_wmem_alloc)); 1533e7f2b8dSEric Dumazet WARN_ON_ONCE(sk->sk_wmem_queued); 1543e7f2b8dSEric Dumazet WARN_ON_ONCE(sk_forward_alloc_get(sk)); 1551da177e4SLinus Torvalds 156f6d8bd05SEric Dumazet kfree(rcu_dereference_protected(inet->inet_opt, 1)); 1575869b8faSXin Long dst_release(rcu_dereference_protected(sk->sk_dst_cache, 1)); 1588f905c0eSEric Dumazet dst_release(rcu_dereference_protected(sk->sk_rx_dst, 1)); 1591da177e4SLinus Torvalds } 1603d1427f8SEric Dumazet EXPORT_SYMBOL(inet_sock_destruct); 1611da177e4SLinus Torvalds 1621da177e4SLinus Torvalds /* 1631da177e4SLinus Torvalds * The routines beyond this point handle the behaviour of an AF_INET 1641da177e4SLinus Torvalds * socket object. Mostly it punts to the subprotocols of IP to do 1651da177e4SLinus Torvalds * the work. 1661da177e4SLinus Torvalds */ 1671da177e4SLinus Torvalds 1681da177e4SLinus Torvalds /* 1691da177e4SLinus Torvalds * Automatically bind an unbound socket. 1701da177e4SLinus Torvalds */ 1711da177e4SLinus Torvalds 1721da177e4SLinus Torvalds static int inet_autobind(struct sock *sk) 1731da177e4SLinus Torvalds { 1741da177e4SLinus Torvalds struct inet_sock *inet; 1751da177e4SLinus Torvalds /* We may need to bind the socket. */ 1761da177e4SLinus Torvalds lock_sock(sk); 1771da177e4SLinus Torvalds inet = inet_sk(sk); 178c720c7e8SEric Dumazet if (!inet->inet_num) { 1791da177e4SLinus Torvalds if (sk->sk_prot->get_port(sk, 0)) { 1801da177e4SLinus Torvalds release_sock(sk); 1811da177e4SLinus Torvalds return -EAGAIN; 1821da177e4SLinus Torvalds } 183c720c7e8SEric Dumazet inet->inet_sport = htons(inet->inet_num); 1841da177e4SLinus Torvalds } 1851da177e4SLinus Torvalds release_sock(sk); 1861da177e4SLinus Torvalds return 0; 1871da177e4SLinus Torvalds } 1881da177e4SLinus Torvalds 1891da177e4SLinus Torvalds /* 1901da177e4SLinus Torvalds * Move a socket into listening state. 1911da177e4SLinus Torvalds */ 1921da177e4SLinus Torvalds int inet_listen(struct socket *sock, int backlog) 1931da177e4SLinus Torvalds { 1941da177e4SLinus Torvalds struct sock *sk = sock->sk; 1951da177e4SLinus Torvalds unsigned char old_state; 196e1cfcbe8SHaishuang Yan int err, tcp_fastopen; 1971da177e4SLinus Torvalds 1981da177e4SLinus Torvalds lock_sock(sk); 1991da177e4SLinus Torvalds 2001da177e4SLinus Torvalds err = -EINVAL; 2011da177e4SLinus Torvalds if (sock->state != SS_UNCONNECTED || sock->type != SOCK_STREAM) 2021da177e4SLinus Torvalds goto out; 2031da177e4SLinus Torvalds 2041da177e4SLinus Torvalds old_state = sk->sk_state; 2051da177e4SLinus Torvalds if (!((1 << old_state) & (TCPF_CLOSE | TCPF_LISTEN))) 2061da177e4SLinus Torvalds goto out; 2071da177e4SLinus Torvalds 208099ecf59SEric Dumazet WRITE_ONCE(sk->sk_max_ack_backlog, backlog); 2091da177e4SLinus Torvalds /* Really, if the socket is already in listen state 2101da177e4SLinus Torvalds * we can only allow the backlog to be adjusted. 2111da177e4SLinus Torvalds */ 2121da177e4SLinus Torvalds if (old_state != TCP_LISTEN) { 213cebc5cbaSYuchung Cheng /* Enable TFO w/o requiring TCP_FASTOPEN socket option. 2148336886fSJerry Chu * Note that only TCP sockets (SOCK_STREAM) will reach here. 215cebc5cbaSYuchung Cheng * Also fastopen backlog may already been set via the option 216cebc5cbaSYuchung Cheng * because the socket was in TCP_LISTEN state previously but 217cebc5cbaSYuchung Cheng * was shutdown() rather than close(). 2188336886fSJerry Chu */ 2195a542133SKuniyuki Iwashima tcp_fastopen = READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_fastopen); 220e1cfcbe8SHaishuang Yan if ((tcp_fastopen & TFO_SERVER_WO_SOCKOPT1) && 221e1cfcbe8SHaishuang Yan (tcp_fastopen & TFO_SERVER_ENABLE) && 2220536fcc0SEric Dumazet !inet_csk(sk)->icsk_accept_queue.fastopenq.max_qlen) { 2230536fcc0SEric Dumazet fastopen_queue_tune(sk, backlog); 22443713848SHaishuang Yan tcp_fastopen_init_key_once(sock_net(sk)); 2258336886fSJerry Chu } 226cebc5cbaSYuchung Cheng 227e7049395SKuniyuki Iwashima err = inet_csk_listen_start(sk); 2281da177e4SLinus Torvalds if (err) 2291da177e4SLinus Torvalds goto out; 230f333ee0cSAndrey Ignatov tcp_call_bpf(sk, BPF_SOCK_OPS_TCP_LISTEN_CB, 0, NULL); 2311da177e4SLinus Torvalds } 2321da177e4SLinus Torvalds err = 0; 2331da177e4SLinus Torvalds 2341da177e4SLinus Torvalds out: 2351da177e4SLinus Torvalds release_sock(sk); 2361da177e4SLinus Torvalds return err; 2371da177e4SLinus Torvalds } 2383d1427f8SEric Dumazet EXPORT_SYMBOL(inet_listen); 2391da177e4SLinus Torvalds 2401da177e4SLinus Torvalds /* 2411da177e4SLinus Torvalds * Create an inet socket. 2421da177e4SLinus Torvalds */ 2431da177e4SLinus Torvalds 2443f378b68SEric Paris static int inet_create(struct net *net, struct socket *sock, int protocol, 2453f378b68SEric Paris int kern) 2461da177e4SLinus Torvalds { 2471da177e4SLinus Torvalds struct sock *sk; 2481da177e4SLinus Torvalds struct inet_protosw *answer; 2491da177e4SLinus Torvalds struct inet_sock *inet; 2501da177e4SLinus Torvalds struct proto *answer_prot; 2511da177e4SLinus Torvalds unsigned char answer_flags; 252bb97d31fSArnaldo Carvalho de Melo int try_loading_module = 0; 25386c8f9d1SHerbert Xu int err; 2541da177e4SLinus Torvalds 25579462ad0SHannes Frederic Sowa if (protocol < 0 || protocol >= IPPROTO_MAX) 25679462ad0SHannes Frederic Sowa return -EINVAL; 25779462ad0SHannes Frederic Sowa 2581da177e4SLinus Torvalds sock->state = SS_UNCONNECTED; 2591da177e4SLinus Torvalds 2601da177e4SLinus Torvalds /* Look for the requested type/protocol pair. */ 261bb97d31fSArnaldo Carvalho de Melo lookup_protocol: 26286c8f9d1SHerbert Xu err = -ESOCKTNOSUPPORT; 2631da177e4SLinus Torvalds rcu_read_lock(); 264696adfe8SPaul E. McKenney list_for_each_entry_rcu(answer, &inetsw[sock->type], list) { 2651da177e4SLinus Torvalds 266696adfe8SPaul E. McKenney err = 0; 2671da177e4SLinus Torvalds /* Check the non-wild match. */ 2681da177e4SLinus Torvalds if (protocol == answer->protocol) { 2691da177e4SLinus Torvalds if (protocol != IPPROTO_IP) 2701da177e4SLinus Torvalds break; 2711da177e4SLinus Torvalds } else { 2721da177e4SLinus Torvalds /* Check for the two wild cases. */ 2731da177e4SLinus Torvalds if (IPPROTO_IP == protocol) { 2741da177e4SLinus Torvalds protocol = answer->protocol; 2751da177e4SLinus Torvalds break; 2761da177e4SLinus Torvalds } 2771da177e4SLinus Torvalds if (IPPROTO_IP == answer->protocol) 2781da177e4SLinus Torvalds break; 2791da177e4SLinus Torvalds } 28086c8f9d1SHerbert Xu err = -EPROTONOSUPPORT; 2811da177e4SLinus Torvalds } 2821da177e4SLinus Torvalds 283696adfe8SPaul E. McKenney if (unlikely(err)) { 284bb97d31fSArnaldo Carvalho de Melo if (try_loading_module < 2) { 285bb97d31fSArnaldo Carvalho de Melo rcu_read_unlock(); 286bb97d31fSArnaldo Carvalho de Melo /* 287bb97d31fSArnaldo Carvalho de Melo * Be more specific, e.g. net-pf-2-proto-132-type-1 288bb97d31fSArnaldo Carvalho de Melo * (net-pf-PF_INET-proto-IPPROTO_SCTP-type-SOCK_STREAM) 289bb97d31fSArnaldo Carvalho de Melo */ 290bb97d31fSArnaldo Carvalho de Melo if (++try_loading_module == 1) 291bb97d31fSArnaldo Carvalho de Melo request_module("net-pf-%d-proto-%d-type-%d", 292bb97d31fSArnaldo Carvalho de Melo PF_INET, protocol, sock->type); 293bb97d31fSArnaldo Carvalho de Melo /* 294bb97d31fSArnaldo Carvalho de Melo * Fall back to generic, e.g. net-pf-2-proto-132 295bb97d31fSArnaldo Carvalho de Melo * (net-pf-PF_INET-proto-IPPROTO_SCTP) 296bb97d31fSArnaldo Carvalho de Melo */ 297bb97d31fSArnaldo Carvalho de Melo else 298bb97d31fSArnaldo Carvalho de Melo request_module("net-pf-%d-proto-%d", 299bb97d31fSArnaldo Carvalho de Melo PF_INET, protocol); 300bb97d31fSArnaldo Carvalho de Melo goto lookup_protocol; 301bb97d31fSArnaldo Carvalho de Melo } else 3021da177e4SLinus Torvalds goto out_rcu_unlock; 303bb97d31fSArnaldo Carvalho de Melo } 304bb97d31fSArnaldo Carvalho de Melo 3051da177e4SLinus Torvalds err = -EPERM; 30652e804c6SEric W. Biederman if (sock->type == SOCK_RAW && !kern && 30752e804c6SEric W. Biederman !ns_capable(net->user_ns, CAP_NET_RAW)) 3081da177e4SLinus Torvalds goto out_rcu_unlock; 3091da177e4SLinus Torvalds 3101da177e4SLinus Torvalds sock->ops = answer->ops; 3111da177e4SLinus Torvalds answer_prot = answer->prot; 3121da177e4SLinus Torvalds answer_flags = answer->flags; 3131da177e4SLinus Torvalds rcu_read_unlock(); 3141da177e4SLinus Torvalds 31551456b29SIan Morris WARN_ON(!answer_prot->slab); 3161da177e4SLinus Torvalds 317ca746c55SZheng Yongjun err = -ENOMEM; 31811aa9c28SEric W. Biederman sk = sk_alloc(net, PF_INET, GFP_KERNEL, answer_prot, kern); 31951456b29SIan Morris if (!sk) 3201da177e4SLinus Torvalds goto out; 3211da177e4SLinus Torvalds 3221da177e4SLinus Torvalds err = 0; 3231da177e4SLinus Torvalds if (INET_PROTOSW_REUSE & answer_flags) 3244a17fd52SPavel Emelyanov sk->sk_reuse = SK_CAN_REUSE; 3251da177e4SLinus Torvalds 3261da177e4SLinus Torvalds inet = inet_sk(sk); 327469de9b9SPaul Moore inet->is_icsk = (INET_PROTOSW_ICSK & answer_flags) != 0; 3281da177e4SLinus Torvalds 3297b2ff18eSJiri Olsa inet->nodefrag = 0; 3307b2ff18eSJiri Olsa 3311da177e4SLinus Torvalds if (SOCK_RAW == sock->type) { 332c720c7e8SEric Dumazet inet->inet_num = protocol; 3331da177e4SLinus Torvalds if (IPPROTO_RAW == protocol) 3341da177e4SLinus Torvalds inet->hdrincl = 1; 3351da177e4SLinus Torvalds } 3361da177e4SLinus Torvalds 3370968d2a4SKuniyuki Iwashima if (READ_ONCE(net->ipv4.sysctl_ip_no_pmtu_disc)) 3381da177e4SLinus Torvalds inet->pmtudisc = IP_PMTUDISC_DONT; 3391da177e4SLinus Torvalds else 3401da177e4SLinus Torvalds inet->pmtudisc = IP_PMTUDISC_WANT; 3411da177e4SLinus Torvalds 342c720c7e8SEric Dumazet inet->inet_id = 0; 3431da177e4SLinus Torvalds 3441da177e4SLinus Torvalds sock_init_data(sock, sk); 3451da177e4SLinus Torvalds 3461da177e4SLinus Torvalds sk->sk_destruct = inet_sock_destruct; 3471da177e4SLinus Torvalds sk->sk_protocol = protocol; 3481da177e4SLinus Torvalds sk->sk_backlog_rcv = sk->sk_prot->backlog_rcv; 349c11204c7SKevin Yang sk->sk_txrehash = READ_ONCE(net->core.sysctl_txrehash); 3501da177e4SLinus Torvalds 3511da177e4SLinus Torvalds inet->uc_ttl = -1; 3521da177e4SLinus Torvalds inet->mc_loop = 1; 3531da177e4SLinus Torvalds inet->mc_ttl = 1; 354f771bef9SNivedita Singhvi inet->mc_all = 1; 3551da177e4SLinus Torvalds inet->mc_index = 0; 3561da177e4SLinus Torvalds inet->mc_list = NULL; 3574c507d28SJiri Benc inet->rcv_tos = 0; 3581da177e4SLinus Torvalds 359c720c7e8SEric Dumazet if (inet->inet_num) { 3601da177e4SLinus Torvalds /* It assumes that any protocol which allows 3611da177e4SLinus Torvalds * the user to assign a number at socket 3621da177e4SLinus Torvalds * creation time automatically 3631da177e4SLinus Torvalds * shares. 3641da177e4SLinus Torvalds */ 365c720c7e8SEric Dumazet inet->inet_sport = htons(inet->inet_num); 3661da177e4SLinus Torvalds /* Add to protocol hash chains. */ 367086c653fSCraig Gallek err = sk->sk_prot->hash(sk); 368086c653fSCraig Gallek if (err) { 369086c653fSCraig Gallek sk_common_release(sk); 370086c653fSCraig Gallek goto out; 371086c653fSCraig Gallek } 3721da177e4SLinus Torvalds } 3731da177e4SLinus Torvalds 3741da177e4SLinus Torvalds if (sk->sk_prot->init) { 3751da177e4SLinus Torvalds err = sk->sk_prot->init(sk); 37661023658SDavid Ahern if (err) { 3771da177e4SLinus Torvalds sk_common_release(sk); 37861023658SDavid Ahern goto out; 37961023658SDavid Ahern } 38061023658SDavid Ahern } 38161023658SDavid Ahern 38261023658SDavid Ahern if (!kern) { 38361023658SDavid Ahern err = BPF_CGROUP_RUN_PROG_INET_SOCK(sk); 38461023658SDavid Ahern if (err) { 38561023658SDavid Ahern sk_common_release(sk); 38661023658SDavid Ahern goto out; 38761023658SDavid Ahern } 3881da177e4SLinus Torvalds } 3891da177e4SLinus Torvalds out: 3901da177e4SLinus Torvalds return err; 3911da177e4SLinus Torvalds out_rcu_unlock: 3921da177e4SLinus Torvalds rcu_read_unlock(); 3931da177e4SLinus Torvalds goto out; 3941da177e4SLinus Torvalds } 3951da177e4SLinus Torvalds 3961da177e4SLinus Torvalds 3971da177e4SLinus Torvalds /* 3981da177e4SLinus Torvalds * The peer socket should always be NULL (or else). When we call this 3991da177e4SLinus Torvalds * function we are destroying the object and from then on nobody 4001da177e4SLinus Torvalds * should refer to it. 4011da177e4SLinus Torvalds */ 4021da177e4SLinus Torvalds int inet_release(struct socket *sock) 4031da177e4SLinus Torvalds { 4041da177e4SLinus Torvalds struct sock *sk = sock->sk; 4051da177e4SLinus Torvalds 4061da177e4SLinus Torvalds if (sk) { 4071da177e4SLinus Torvalds long timeout; 4081da177e4SLinus Torvalds 409f5836749SStanislav Fomichev if (!sk->sk_kern_sock) 410f5836749SStanislav Fomichev BPF_CGROUP_RUN_PROG_INET_SOCK_RELEASE(sk); 411f5836749SStanislav Fomichev 4121da177e4SLinus Torvalds /* Applications forget to leave groups before exiting */ 4131da177e4SLinus Torvalds ip_mc_drop_socket(sk); 4141da177e4SLinus Torvalds 4151da177e4SLinus Torvalds /* If linger is set, we don't return until the close 4161da177e4SLinus Torvalds * is complete. Otherwise we return immediately. The 4171da177e4SLinus Torvalds * actually closing is done the same either way. 4181da177e4SLinus Torvalds * 4191da177e4SLinus Torvalds * If the close is due to the process exiting, we never 4201da177e4SLinus Torvalds * linger.. 4211da177e4SLinus Torvalds */ 4221da177e4SLinus Torvalds timeout = 0; 4231da177e4SLinus Torvalds if (sock_flag(sk, SOCK_LINGER) && 4241da177e4SLinus Torvalds !(current->flags & PF_EXITING)) 4251da177e4SLinus Torvalds timeout = sk->sk_lingertime; 4261da177e4SLinus Torvalds sk->sk_prot->close(sk, timeout); 4272b81f816SJakub Kicinski sock->sk = NULL; 4281da177e4SLinus Torvalds } 4291da177e4SLinus Torvalds return 0; 4301da177e4SLinus Torvalds } 4313d1427f8SEric Dumazet EXPORT_SYMBOL(inet_release); 4321da177e4SLinus Torvalds 4331da177e4SLinus Torvalds int inet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len) 4341da177e4SLinus Torvalds { 4351da177e4SLinus Torvalds struct sock *sk = sock->sk; 43677241217SStanislav Fomichev u32 flags = BIND_WITH_LOCK; 4371da177e4SLinus Torvalds int err; 4381da177e4SLinus Torvalds 4391da177e4SLinus Torvalds /* If the socket has its own bind function then use it. (RAW) */ 4401da177e4SLinus Torvalds if (sk->sk_prot->bind) { 4413679d585SAndrey Ignatov return sk->sk_prot->bind(sk, uaddr, addr_len); 4421da177e4SLinus Torvalds } 4431da177e4SLinus Torvalds if (addr_len < sizeof(struct sockaddr_in)) 4443679d585SAndrey Ignatov return -EINVAL; 4451da177e4SLinus Torvalds 4464fbac77dSAndrey Ignatov /* BPF prog is run before any checks are done so that if the prog 4474fbac77dSAndrey Ignatov * changes context in a wrong way it will be caught. 4484fbac77dSAndrey Ignatov */ 44977241217SStanislav Fomichev err = BPF_CGROUP_RUN_PROG_INET_BIND_LOCK(sk, uaddr, 4506fc88c35SDave Marchevsky CGROUP_INET4_BIND, &flags); 4514fbac77dSAndrey Ignatov if (err) 4523679d585SAndrey Ignatov return err; 4533679d585SAndrey Ignatov 45477241217SStanislav Fomichev return __inet_bind(sk, uaddr, addr_len, flags); 4553679d585SAndrey Ignatov } 4563679d585SAndrey Ignatov EXPORT_SYMBOL(inet_bind); 4573679d585SAndrey Ignatov 4583679d585SAndrey Ignatov int __inet_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len, 459cb0721c7SStanislav Fomichev u32 flags) 4603679d585SAndrey Ignatov { 4613679d585SAndrey Ignatov struct sockaddr_in *addr = (struct sockaddr_in *)uaddr; 4621da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 4631da177e4SLinus Torvalds struct net *net = sock_net(sk); 4641da177e4SLinus Torvalds unsigned short snum; 4651da177e4SLinus Torvalds int chk_addr_ret; 4661da177e4SLinus Torvalds u32 tb_id = RT_TABLE_LOCAL; 4671da177e4SLinus Torvalds int err; 4681da177e4SLinus Torvalds 469c349a528SMarcus Meissner if (addr->sin_family != AF_INET) { 47029c486dfSEric Dumazet /* Compatibility games : accept AF_UNSPEC (mapped to AF_INET) 47129c486dfSEric Dumazet * only if s_addr is INADDR_ANY. 47229c486dfSEric Dumazet */ 473c349a528SMarcus Meissner err = -EAFNOSUPPORT; 47429c486dfSEric Dumazet if (addr->sin_family != AF_UNSPEC || 47529c486dfSEric Dumazet addr->sin_addr.s_addr != htonl(INADDR_ANY)) 476d0733d2eSMarcus Meissner goto out; 477c349a528SMarcus Meissner } 478d0733d2eSMarcus Meissner 4793236b004SDavid Ahern tb_id = l3mdev_fib_table_by_index(net, sk->sk_bound_dev_if) ? : tb_id; 48030bbaa19SDavid Ahern chk_addr_ret = inet_addr_type_table(net, addr->sin_addr.s_addr, tb_id); 4811da177e4SLinus Torvalds 4821da177e4SLinus Torvalds /* Not specified by any standard per-se, however it breaks too 4831da177e4SLinus Torvalds * many applications when removed. It is unfortunate since 4841da177e4SLinus Torvalds * allowing applications to make a non-local bind solves 4851da177e4SLinus Torvalds * several problems with systems using dynamic addressing. 4861da177e4SLinus Torvalds * (ie. your servers still start up even if your ISDN link 4871da177e4SLinus Torvalds * is temporarily down) 4881da177e4SLinus Torvalds */ 4891da177e4SLinus Torvalds err = -EADDRNOTAVAIL; 4908ff978b8SRiccardo Paolo Bestetti if (!inet_addr_valid_or_nonlocal(net, inet, addr->sin_addr.s_addr, 4918ff978b8SRiccardo Paolo Bestetti chk_addr_ret)) 4921da177e4SLinus Torvalds goto out; 4931da177e4SLinus Torvalds 4941da177e4SLinus Torvalds snum = ntohs(addr->sin_port); 4951da177e4SLinus Torvalds err = -EACCES; 49677241217SStanislav Fomichev if (!(flags & BIND_NO_CAP_NET_BIND_SERVICE) && 49777241217SStanislav Fomichev snum && inet_port_requires_bind_service(net, snum) && 4983594698aSEric W. Biederman !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE)) 4991da177e4SLinus Torvalds goto out; 5001da177e4SLinus Torvalds 5011da177e4SLinus Torvalds /* We keep a pair of addresses. rcv_saddr is the one 5021da177e4SLinus Torvalds * used by hash lookups, and saddr is used for transmit. 5031da177e4SLinus Torvalds * 5041da177e4SLinus Torvalds * In the BSD API these are the same except where it 5051da177e4SLinus Torvalds * would be illegal to use them (multicast/broadcast) in 5061da177e4SLinus Torvalds * which case the sending device address is used. 5071da177e4SLinus Torvalds */ 508cb0721c7SStanislav Fomichev if (flags & BIND_WITH_LOCK) 5091da177e4SLinus Torvalds lock_sock(sk); 5101da177e4SLinus Torvalds 5111da177e4SLinus Torvalds /* Check these errors (active socket, double bind). */ 5121da177e4SLinus Torvalds err = -EINVAL; 513c720c7e8SEric Dumazet if (sk->sk_state != TCP_CLOSE || inet->inet_num) 5141da177e4SLinus Torvalds goto out_release_sock; 5151da177e4SLinus Torvalds 516c720c7e8SEric Dumazet inet->inet_rcv_saddr = inet->inet_saddr = addr->sin_addr.s_addr; 5171da177e4SLinus Torvalds if (chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST) 518c720c7e8SEric Dumazet inet->inet_saddr = 0; /* Use device */ 5191da177e4SLinus Torvalds 5201da177e4SLinus Torvalds /* Make sure we are allowed to bind here. */ 521aac3fc32SAndrey Ignatov if (snum || !(inet->bind_address_no_port || 522cb0721c7SStanislav Fomichev (flags & BIND_FORCE_ADDRESS_NO_PORT))) { 5237a7160edSKuniyuki Iwashima err = sk->sk_prot->get_port(sk, snum); 5247a7160edSKuniyuki Iwashima if (err) { 525c720c7e8SEric Dumazet inet->inet_saddr = inet->inet_rcv_saddr = 0; 5261da177e4SLinus Torvalds goto out_release_sock; 5271da177e4SLinus Torvalds } 5288086fbafSStanislav Fomichev if (!(flags & BIND_FROM_BPF)) { 529aac3fc32SAndrey Ignatov err = BPF_CGROUP_RUN_PROG_INET4_POST_BIND(sk); 530aac3fc32SAndrey Ignatov if (err) { 531aac3fc32SAndrey Ignatov inet->inet_saddr = inet->inet_rcv_saddr = 0; 53291a760b2SMenglong Dong if (sk->sk_prot->put_port) 53391a760b2SMenglong Dong sk->sk_prot->put_port(sk); 534aac3fc32SAndrey Ignatov goto out_release_sock; 535aac3fc32SAndrey Ignatov } 536aac3fc32SAndrey Ignatov } 5378086fbafSStanislav Fomichev } 5381da177e4SLinus Torvalds 539c720c7e8SEric Dumazet if (inet->inet_rcv_saddr) 5401da177e4SLinus Torvalds sk->sk_userlocks |= SOCK_BINDADDR_LOCK; 5411da177e4SLinus Torvalds if (snum) 5421da177e4SLinus Torvalds sk->sk_userlocks |= SOCK_BINDPORT_LOCK; 543c720c7e8SEric Dumazet inet->inet_sport = htons(inet->inet_num); 544c720c7e8SEric Dumazet inet->inet_daddr = 0; 545c720c7e8SEric Dumazet inet->inet_dport = 0; 5461da177e4SLinus Torvalds sk_dst_reset(sk); 5471da177e4SLinus Torvalds err = 0; 5481da177e4SLinus Torvalds out_release_sock: 549cb0721c7SStanislav Fomichev if (flags & BIND_WITH_LOCK) 5501da177e4SLinus Torvalds release_sock(sk); 5511da177e4SLinus Torvalds out: 5521da177e4SLinus Torvalds return err; 5531da177e4SLinus Torvalds } 5541da177e4SLinus Torvalds 5551da177e4SLinus Torvalds int inet_dgram_connect(struct socket *sock, struct sockaddr *uaddr, 5561da177e4SLinus Torvalds int addr_len, int flags) 5571da177e4SLinus Torvalds { 5581da177e4SLinus Torvalds struct sock *sk = sock->sk; 559364f997bSKuniyuki Iwashima const struct proto *prot; 560d74bad4eSAndrey Ignatov int err; 5611da177e4SLinus Torvalds 5626503d961SChangli Gao if (addr_len < sizeof(uaddr->sa_family)) 5636503d961SChangli Gao return -EINVAL; 564364f997bSKuniyuki Iwashima 565364f997bSKuniyuki Iwashima /* IPV6_ADDRFORM can change sk->sk_prot under us. */ 566364f997bSKuniyuki Iwashima prot = READ_ONCE(sk->sk_prot); 567364f997bSKuniyuki Iwashima 5681da177e4SLinus Torvalds if (uaddr->sa_family == AF_UNSPEC) 569364f997bSKuniyuki Iwashima return prot->disconnect(sk, flags); 5701da177e4SLinus Torvalds 571d74bad4eSAndrey Ignatov if (BPF_CGROUP_PRE_CONNECT_ENABLED(sk)) { 572364f997bSKuniyuki Iwashima err = prot->pre_connect(sk, uaddr, addr_len); 573d74bad4eSAndrey Ignatov if (err) 574d74bad4eSAndrey Ignatov return err; 575d74bad4eSAndrey Ignatov } 576d74bad4eSAndrey Ignatov 577dcd01eeaSEric Dumazet if (data_race(!inet_sk(sk)->inet_num) && inet_autobind(sk)) 5781da177e4SLinus Torvalds return -EAGAIN; 579364f997bSKuniyuki Iwashima return prot->connect(sk, uaddr, addr_len); 5801da177e4SLinus Torvalds } 5813d1427f8SEric Dumazet EXPORT_SYMBOL(inet_dgram_connect); 5821da177e4SLinus Torvalds 583783237e8SYuchung Cheng static long inet_wait_for_connect(struct sock *sk, long timeo, int writebias) 5841da177e4SLinus Torvalds { 58514135f30SWANG Cong DEFINE_WAIT_FUNC(wait, woken_wake_function); 5861da177e4SLinus Torvalds 58714135f30SWANG Cong add_wait_queue(sk_sleep(sk), &wait); 588783237e8SYuchung Cheng sk->sk_write_pending += writebias; 589*4faeee0cSEric Dumazet sk->sk_wait_pending++; 5901da177e4SLinus Torvalds 5911da177e4SLinus Torvalds /* Basic assumption: if someone sets sk->sk_err, he _must_ 5921da177e4SLinus Torvalds * change state of the socket from TCP_SYN_*. 5931da177e4SLinus Torvalds * Connect() does not allow to get error notifications 5941da177e4SLinus Torvalds * without closing the socket. 5951da177e4SLinus Torvalds */ 5961da177e4SLinus Torvalds while ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) { 5971da177e4SLinus Torvalds release_sock(sk); 59814135f30SWANG Cong timeo = wait_woken(&wait, TASK_INTERRUPTIBLE, timeo); 5991da177e4SLinus Torvalds lock_sock(sk); 6001da177e4SLinus Torvalds if (signal_pending(current) || !timeo) 6011da177e4SLinus Torvalds break; 6021da177e4SLinus Torvalds } 60314135f30SWANG Cong remove_wait_queue(sk_sleep(sk), &wait); 604783237e8SYuchung Cheng sk->sk_write_pending -= writebias; 605*4faeee0cSEric Dumazet sk->sk_wait_pending--; 6061da177e4SLinus Torvalds return timeo; 6071da177e4SLinus Torvalds } 6081da177e4SLinus Torvalds 6091da177e4SLinus Torvalds /* 6101da177e4SLinus Torvalds * Connect to a remote host. There is regrettably still a little 6111da177e4SLinus Torvalds * TCP 'magic' in here. 6121da177e4SLinus Torvalds */ 613cf60af03SYuchung Cheng int __inet_stream_connect(struct socket *sock, struct sockaddr *uaddr, 6143979ad7eSWilly Tarreau int addr_len, int flags, int is_sendmsg) 6151da177e4SLinus Torvalds { 6161da177e4SLinus Torvalds struct sock *sk = sock->sk; 6171da177e4SLinus Torvalds int err; 6181da177e4SLinus Torvalds long timeo; 6191da177e4SLinus Torvalds 62019f6d3f3SWei Wang /* 62119f6d3f3SWei Wang * uaddr can be NULL and addr_len can be 0 if: 62219f6d3f3SWei Wang * sk is a TCP fastopen active socket and 62319f6d3f3SWei Wang * TCP_FASTOPEN_CONNECT sockopt is set and 62419f6d3f3SWei Wang * we already have a valid cookie for this socket. 62519f6d3f3SWei Wang * In this case, user can call write() after connect(). 62619f6d3f3SWei Wang * write() will invoke tcp_sendmsg_fastopen() which calls 62719f6d3f3SWei Wang * __inet_stream_connect(). 62819f6d3f3SWei Wang */ 62919f6d3f3SWei Wang if (uaddr) { 6306503d961SChangli Gao if (addr_len < sizeof(uaddr->sa_family)) 6316503d961SChangli Gao return -EINVAL; 6326503d961SChangli Gao 6331da177e4SLinus Torvalds if (uaddr->sa_family == AF_UNSPEC) { 6341da177e4SLinus Torvalds err = sk->sk_prot->disconnect(sk, flags); 6351da177e4SLinus Torvalds sock->state = err ? SS_DISCONNECTING : SS_UNCONNECTED; 6361da177e4SLinus Torvalds goto out; 6371da177e4SLinus Torvalds } 63819f6d3f3SWei Wang } 6391da177e4SLinus Torvalds 6401da177e4SLinus Torvalds switch (sock->state) { 6411da177e4SLinus Torvalds default: 6421da177e4SLinus Torvalds err = -EINVAL; 6431da177e4SLinus Torvalds goto out; 6441da177e4SLinus Torvalds case SS_CONNECTED: 6451da177e4SLinus Torvalds err = -EISCONN; 6461da177e4SLinus Torvalds goto out; 6471da177e4SLinus Torvalds case SS_CONNECTING: 64819f6d3f3SWei Wang if (inet_sk(sk)->defer_connect) 6493979ad7eSWilly Tarreau err = is_sendmsg ? -EINPROGRESS : -EISCONN; 65019f6d3f3SWei Wang else 6511da177e4SLinus Torvalds err = -EALREADY; 6521da177e4SLinus Torvalds /* Fall out of switch with err, set for this state */ 6531da177e4SLinus Torvalds break; 6541da177e4SLinus Torvalds case SS_UNCONNECTED: 6551da177e4SLinus Torvalds err = -EISCONN; 6561da177e4SLinus Torvalds if (sk->sk_state != TCP_CLOSE) 6571da177e4SLinus Torvalds goto out; 6581da177e4SLinus Torvalds 659d74bad4eSAndrey Ignatov if (BPF_CGROUP_PRE_CONNECT_ENABLED(sk)) { 660d74bad4eSAndrey Ignatov err = sk->sk_prot->pre_connect(sk, uaddr, addr_len); 661d74bad4eSAndrey Ignatov if (err) 662d74bad4eSAndrey Ignatov goto out; 663d74bad4eSAndrey Ignatov } 664d74bad4eSAndrey Ignatov 6651da177e4SLinus Torvalds err = sk->sk_prot->connect(sk, uaddr, addr_len); 6661da177e4SLinus Torvalds if (err < 0) 6671da177e4SLinus Torvalds goto out; 6681da177e4SLinus Torvalds 6691da177e4SLinus Torvalds sock->state = SS_CONNECTING; 6701da177e4SLinus Torvalds 67119f6d3f3SWei Wang if (!err && inet_sk(sk)->defer_connect) 67219f6d3f3SWei Wang goto out; 67319f6d3f3SWei Wang 6741da177e4SLinus Torvalds /* Just entered SS_CONNECTING state; the only 6751da177e4SLinus Torvalds * difference is that return value in non-blocking 6761da177e4SLinus Torvalds * case is EINPROGRESS, rather than EALREADY. 6771da177e4SLinus Torvalds */ 6781da177e4SLinus Torvalds err = -EINPROGRESS; 6791da177e4SLinus Torvalds break; 6801da177e4SLinus Torvalds } 6811da177e4SLinus Torvalds 6821da177e4SLinus Torvalds timeo = sock_sndtimeo(sk, flags & O_NONBLOCK); 6831da177e4SLinus Torvalds 6841da177e4SLinus Torvalds if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) { 685783237e8SYuchung Cheng int writebias = (sk->sk_protocol == IPPROTO_TCP) && 686783237e8SYuchung Cheng tcp_sk(sk)->fastopen_req && 687783237e8SYuchung Cheng tcp_sk(sk)->fastopen_req->data ? 1 : 0; 688783237e8SYuchung Cheng 6891da177e4SLinus Torvalds /* Error code is set above */ 690783237e8SYuchung Cheng if (!timeo || !inet_wait_for_connect(sk, timeo, writebias)) 6911da177e4SLinus Torvalds goto out; 6921da177e4SLinus Torvalds 6931da177e4SLinus Torvalds err = sock_intr_errno(timeo); 6941da177e4SLinus Torvalds if (signal_pending(current)) 6951da177e4SLinus Torvalds goto out; 6961da177e4SLinus Torvalds } 6971da177e4SLinus Torvalds 6981da177e4SLinus Torvalds /* Connection was closed by RST, timeout, ICMP error 6991da177e4SLinus Torvalds * or another process disconnected us. 7001da177e4SLinus Torvalds */ 7011da177e4SLinus Torvalds if (sk->sk_state == TCP_CLOSE) 7021da177e4SLinus Torvalds goto sock_error; 7031da177e4SLinus Torvalds 7041da177e4SLinus Torvalds /* sk->sk_err may be not zero now, if RECVERR was ordered by user 7051da177e4SLinus Torvalds * and error was received after socket entered established state. 7061da177e4SLinus Torvalds * Hence, it is handled normally after connect() return successfully. 7071da177e4SLinus Torvalds */ 7081da177e4SLinus Torvalds 7091da177e4SLinus Torvalds sock->state = SS_CONNECTED; 7101da177e4SLinus Torvalds err = 0; 7111da177e4SLinus Torvalds out: 7121da177e4SLinus Torvalds return err; 7131da177e4SLinus Torvalds 7141da177e4SLinus Torvalds sock_error: 7151da177e4SLinus Torvalds err = sock_error(sk) ? : -ECONNABORTED; 7161da177e4SLinus Torvalds sock->state = SS_UNCONNECTED; 7171da177e4SLinus Torvalds if (sk->sk_prot->disconnect(sk, flags)) 7181da177e4SLinus Torvalds sock->state = SS_DISCONNECTING; 7191da177e4SLinus Torvalds goto out; 7201da177e4SLinus Torvalds } 721cf60af03SYuchung Cheng EXPORT_SYMBOL(__inet_stream_connect); 722cf60af03SYuchung Cheng 723cf60af03SYuchung Cheng int inet_stream_connect(struct socket *sock, struct sockaddr *uaddr, 724cf60af03SYuchung Cheng int addr_len, int flags) 725cf60af03SYuchung Cheng { 726cf60af03SYuchung Cheng int err; 727cf60af03SYuchung Cheng 728cf60af03SYuchung Cheng lock_sock(sock->sk); 7293979ad7eSWilly Tarreau err = __inet_stream_connect(sock, uaddr, addr_len, flags, 0); 730cf60af03SYuchung Cheng release_sock(sock->sk); 731cf60af03SYuchung Cheng return err; 732cf60af03SYuchung Cheng } 7333d1427f8SEric Dumazet EXPORT_SYMBOL(inet_stream_connect); 7341da177e4SLinus Torvalds 7351da177e4SLinus Torvalds /* 7361da177e4SLinus Torvalds * Accept a pending connection. The TCP layer now gives BSD semantics. 7371da177e4SLinus Torvalds */ 7381da177e4SLinus Torvalds 739cdfbabfbSDavid Howells int inet_accept(struct socket *sock, struct socket *newsock, int flags, 740cdfbabfbSDavid Howells bool kern) 7411da177e4SLinus Torvalds { 742364f997bSKuniyuki Iwashima struct sock *sk1 = sock->sk, *sk2; 7431da177e4SLinus Torvalds int err = -EINVAL; 7441da177e4SLinus Torvalds 745364f997bSKuniyuki Iwashima /* IPV6_ADDRFORM can change sk->sk_prot under us. */ 746364f997bSKuniyuki Iwashima sk2 = READ_ONCE(sk1->sk_prot)->accept(sk1, flags, &err, kern); 7471da177e4SLinus Torvalds if (!sk2) 7481da177e4SLinus Torvalds goto do_err; 7491da177e4SLinus Torvalds 7501da177e4SLinus Torvalds lock_sock(sk2); 7511da177e4SLinus Torvalds 7521eddceadSEric Dumazet sock_rps_record_flow(sk2); 753547b792cSIlpo Järvinen WARN_ON(!((1 << sk2->sk_state) & 7548336886fSJerry Chu (TCPF_ESTABLISHED | TCPF_SYN_RECV | 7558336886fSJerry Chu TCPF_CLOSE_WAIT | TCPF_CLOSE))); 7561da177e4SLinus Torvalds 75771b7786eSStefan Metzmacher if (test_bit(SOCK_SUPPORT_ZC, &sock->flags)) 75871b7786eSStefan Metzmacher set_bit(SOCK_SUPPORT_ZC, &newsock->flags); 7591da177e4SLinus Torvalds sock_graft(sk2, newsock); 7601da177e4SLinus Torvalds 7611da177e4SLinus Torvalds newsock->state = SS_CONNECTED; 7621da177e4SLinus Torvalds err = 0; 7631da177e4SLinus Torvalds release_sock(sk2); 7641da177e4SLinus Torvalds do_err: 7651da177e4SLinus Torvalds return err; 7661da177e4SLinus Torvalds } 7673d1427f8SEric Dumazet EXPORT_SYMBOL(inet_accept); 7681da177e4SLinus Torvalds 7691da177e4SLinus Torvalds /* 7701da177e4SLinus Torvalds * This does both peername and sockname. 7711da177e4SLinus Torvalds */ 7721da177e4SLinus Torvalds int inet_getname(struct socket *sock, struct sockaddr *uaddr, 7739b2c45d4SDenys Vlasenko int peer) 7741da177e4SLinus Torvalds { 7751da177e4SLinus Torvalds struct sock *sk = sock->sk; 7761da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 77738bfd8f5SCyrill Gorcunov DECLARE_SOCKADDR(struct sockaddr_in *, sin, uaddr); 7781da177e4SLinus Torvalds 7791da177e4SLinus Torvalds sin->sin_family = AF_INET; 7809dfc685eSEric Dumazet lock_sock(sk); 7811da177e4SLinus Torvalds if (peer) { 782c720c7e8SEric Dumazet if (!inet->inet_dport || 7831da177e4SLinus Torvalds (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) && 7849dfc685eSEric Dumazet peer == 1)) { 7859dfc685eSEric Dumazet release_sock(sk); 7861da177e4SLinus Torvalds return -ENOTCONN; 7879dfc685eSEric Dumazet } 788c720c7e8SEric Dumazet sin->sin_port = inet->inet_dport; 789c720c7e8SEric Dumazet sin->sin_addr.s_addr = inet->inet_daddr; 7909dfc685eSEric Dumazet BPF_CGROUP_RUN_SA_PROG(sk, (struct sockaddr *)sin, 7919dfc685eSEric Dumazet CGROUP_INET4_GETPEERNAME); 7921da177e4SLinus Torvalds } else { 793c720c7e8SEric Dumazet __be32 addr = inet->inet_rcv_saddr; 7941da177e4SLinus Torvalds if (!addr) 795c720c7e8SEric Dumazet addr = inet->inet_saddr; 796c720c7e8SEric Dumazet sin->sin_port = inet->inet_sport; 7971da177e4SLinus Torvalds sin->sin_addr.s_addr = addr; 7989dfc685eSEric Dumazet BPF_CGROUP_RUN_SA_PROG(sk, (struct sockaddr *)sin, 7999dfc685eSEric Dumazet CGROUP_INET4_GETSOCKNAME); 800a9ed15daSStanislav Fomichev } 8019dfc685eSEric Dumazet release_sock(sk); 8021da177e4SLinus Torvalds memset(sin->sin_zero, 0, sizeof(sin->sin_zero)); 8039b2c45d4SDenys Vlasenko return sizeof(*sin); 8041da177e4SLinus Torvalds } 8053d1427f8SEric Dumazet EXPORT_SYMBOL(inet_getname); 8061da177e4SLinus Torvalds 807e4730936SPaolo Abeni int inet_send_prepare(struct sock *sk) 8081da177e4SLinus Torvalds { 809c58dc01bSDavid S. Miller sock_rps_record_flow(sk); 810fec5e652STom Herbert 8111da177e4SLinus Torvalds /* We may need to bind the socket. */ 812dcd01eeaSEric Dumazet if (data_race(!inet_sk(sk)->inet_num) && !sk->sk_prot->no_autobind && 8137ba42910SChangli Gao inet_autobind(sk)) 8141da177e4SLinus Torvalds return -EAGAIN; 8151da177e4SLinus Torvalds 816e4730936SPaolo Abeni return 0; 817e4730936SPaolo Abeni } 818e4730936SPaolo Abeni EXPORT_SYMBOL_GPL(inet_send_prepare); 819e4730936SPaolo Abeni 820e4730936SPaolo Abeni int inet_sendmsg(struct socket *sock, struct msghdr *msg, size_t size) 821e4730936SPaolo Abeni { 822e4730936SPaolo Abeni struct sock *sk = sock->sk; 823e4730936SPaolo Abeni 824e4730936SPaolo Abeni if (unlikely(inet_send_prepare(sk))) 825e4730936SPaolo Abeni return -EAGAIN; 826e4730936SPaolo Abeni 8276f24080eSPaolo Abeni return INDIRECT_CALL_2(sk->sk_prot->sendmsg, tcp_sendmsg, udp_sendmsg, 8286f24080eSPaolo Abeni sk, msg, size); 8291da177e4SLinus Torvalds } 8303d1427f8SEric Dumazet EXPORT_SYMBOL(inet_sendmsg); 8311da177e4SLinus Torvalds 8327ba42910SChangli Gao ssize_t inet_sendpage(struct socket *sock, struct page *page, int offset, 8333d1427f8SEric Dumazet size_t size, int flags) 8341da177e4SLinus Torvalds { 8351da177e4SLinus Torvalds struct sock *sk = sock->sk; 836364f997bSKuniyuki Iwashima const struct proto *prot; 8371da177e4SLinus Torvalds 838e4730936SPaolo Abeni if (unlikely(inet_send_prepare(sk))) 8391da177e4SLinus Torvalds return -EAGAIN; 8401da177e4SLinus Torvalds 841364f997bSKuniyuki Iwashima /* IPV6_ADDRFORM can change sk->sk_prot under us. */ 842364f997bSKuniyuki Iwashima prot = READ_ONCE(sk->sk_prot); 843364f997bSKuniyuki Iwashima if (prot->sendpage) 844364f997bSKuniyuki Iwashima return prot->sendpage(sk, page, offset, size, flags); 8451da177e4SLinus Torvalds return sock_no_sendpage(sock, page, offset, size, flags); 8461da177e4SLinus Torvalds } 8477ba42910SChangli Gao EXPORT_SYMBOL(inet_sendpage); 8481da177e4SLinus Torvalds 8496f24080eSPaolo Abeni INDIRECT_CALLABLE_DECLARE(int udp_recvmsg(struct sock *, struct msghdr *, 850ec095263SOliver Hartkopp size_t, int, int *)); 8511b784140SYing Xue int inet_recvmsg(struct socket *sock, struct msghdr *msg, size_t size, 8521b784140SYing Xue int flags) 853fec5e652STom Herbert { 854fec5e652STom Herbert struct sock *sk = sock->sk; 855fec5e652STom Herbert int addr_len = 0; 856fec5e652STom Herbert int err; 857fec5e652STom Herbert 858e3f2c4a3SSoheil Hassas Yeganeh if (likely(!(flags & MSG_ERRQUEUE))) 859c58dc01bSDavid S. Miller sock_rps_record_flow(sk); 860fec5e652STom Herbert 8616f24080eSPaolo Abeni err = INDIRECT_CALL_2(sk->sk_prot->recvmsg, tcp_recvmsg, udp_recvmsg, 862ec095263SOliver Hartkopp sk, msg, size, flags, &addr_len); 863fec5e652STom Herbert if (err >= 0) 864fec5e652STom Herbert msg->msg_namelen = addr_len; 865fec5e652STom Herbert return err; 866fec5e652STom Herbert } 867fec5e652STom Herbert EXPORT_SYMBOL(inet_recvmsg); 8681da177e4SLinus Torvalds 8691da177e4SLinus Torvalds int inet_shutdown(struct socket *sock, int how) 8701da177e4SLinus Torvalds { 8711da177e4SLinus Torvalds struct sock *sk = sock->sk; 8721da177e4SLinus Torvalds int err = 0; 8731da177e4SLinus Torvalds 8741da177e4SLinus Torvalds /* This should really check to make sure 8751da177e4SLinus Torvalds * the socket is a TCP socket. (WHY AC...) 8761da177e4SLinus Torvalds */ 8771da177e4SLinus Torvalds how++; /* maps 0->1 has the advantage of making bit 1 rcvs and 8781da177e4SLinus Torvalds 1->2 bit 2 snds. 8791da177e4SLinus Torvalds 2->3 */ 8801da177e4SLinus Torvalds if ((how & ~SHUTDOWN_MASK) || !how) /* MAXINT->0 */ 8811da177e4SLinus Torvalds return -EINVAL; 8821da177e4SLinus Torvalds 8831da177e4SLinus Torvalds lock_sock(sk); 8841da177e4SLinus Torvalds if (sock->state == SS_CONNECTING) { 8851da177e4SLinus Torvalds if ((1 << sk->sk_state) & 8861da177e4SLinus Torvalds (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_CLOSE)) 8871da177e4SLinus Torvalds sock->state = SS_DISCONNECTING; 8881da177e4SLinus Torvalds else 8891da177e4SLinus Torvalds sock->state = SS_CONNECTED; 8901da177e4SLinus Torvalds } 8911da177e4SLinus Torvalds 8921da177e4SLinus Torvalds switch (sk->sk_state) { 8931da177e4SLinus Torvalds case TCP_CLOSE: 8941da177e4SLinus Torvalds err = -ENOTCONN; 8951da177e4SLinus Torvalds /* Hack to wake up other listeners, who can poll for 896a9a08845SLinus Torvalds EPOLLHUP, even on eg. unconnected UDP sockets -- RR */ 897a8eceea8SJoe Perches fallthrough; 8981da177e4SLinus Torvalds default: 899e14cadfdSEric Dumazet WRITE_ONCE(sk->sk_shutdown, sk->sk_shutdown | how); 9001da177e4SLinus Torvalds if (sk->sk_prot->shutdown) 9011da177e4SLinus Torvalds sk->sk_prot->shutdown(sk, how); 9021da177e4SLinus Torvalds break; 9031da177e4SLinus Torvalds 9041da177e4SLinus Torvalds /* Remaining two branches are temporary solution for missing 9051da177e4SLinus Torvalds * close() in multithreaded environment. It is _not_ a good idea, 9061da177e4SLinus Torvalds * but we have no choice until close() is repaired at VFS level. 9071da177e4SLinus Torvalds */ 9081da177e4SLinus Torvalds case TCP_LISTEN: 9091da177e4SLinus Torvalds if (!(how & RCV_SHUTDOWN)) 9101da177e4SLinus Torvalds break; 911a8eceea8SJoe Perches fallthrough; 9121da177e4SLinus Torvalds case TCP_SYN_SENT: 9131da177e4SLinus Torvalds err = sk->sk_prot->disconnect(sk, O_NONBLOCK); 9141da177e4SLinus Torvalds sock->state = err ? SS_DISCONNECTING : SS_UNCONNECTED; 9151da177e4SLinus Torvalds break; 9161da177e4SLinus Torvalds } 9171da177e4SLinus Torvalds 9181da177e4SLinus Torvalds /* Wake up anyone sleeping in poll. */ 9191da177e4SLinus Torvalds sk->sk_state_change(sk); 9201da177e4SLinus Torvalds release_sock(sk); 9211da177e4SLinus Torvalds return err; 9221da177e4SLinus Torvalds } 9233d1427f8SEric Dumazet EXPORT_SYMBOL(inet_shutdown); 9241da177e4SLinus Torvalds 9251da177e4SLinus Torvalds /* 9261da177e4SLinus Torvalds * ioctl() calls you can issue on an INET socket. Most of these are 9271da177e4SLinus Torvalds * device configuration and stuff and very rarely used. Some ioctls 9281da177e4SLinus Torvalds * pass on to the socket itself. 9291da177e4SLinus Torvalds * 9301da177e4SLinus Torvalds * NOTE: I like the idea of a module for the config stuff. ie ifconfig 9311da177e4SLinus Torvalds * loads the devconfigure module does its configuring and unloads it. 9321da177e4SLinus Torvalds * There's a good 20K of config code hanging around the kernel. 9331da177e4SLinus Torvalds */ 9341da177e4SLinus Torvalds 9351da177e4SLinus Torvalds int inet_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) 9361da177e4SLinus Torvalds { 9371da177e4SLinus Torvalds struct sock *sk = sock->sk; 9381da177e4SLinus Torvalds int err = 0; 9393b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(sk); 94003aef17bSAl Viro void __user *p = (void __user *)arg; 94103aef17bSAl Viro struct ifreq ifr; 942ca25c300SAl Viro struct rtentry rt; 9431da177e4SLinus Torvalds 9441da177e4SLinus Torvalds switch (cmd) { 9451da177e4SLinus Torvalds case SIOCADDRT: 9461da177e4SLinus Torvalds case SIOCDELRT: 947ca25c300SAl Viro if (copy_from_user(&rt, p, sizeof(struct rtentry))) 948ca25c300SAl Viro return -EFAULT; 949ca25c300SAl Viro err = ip_rt_ioctl(net, cmd, &rt); 950ca25c300SAl Viro break; 9511da177e4SLinus Torvalds case SIOCRTMSG: 952ca25c300SAl Viro err = -EINVAL; 9531da177e4SLinus Torvalds break; 9541da177e4SLinus Torvalds case SIOCDARP: 9551da177e4SLinus Torvalds case SIOCGARP: 9561da177e4SLinus Torvalds case SIOCSARP: 957e5b13cb1SDenis V. Lunev err = arp_ioctl(net, cmd, (void __user *)arg); 9581da177e4SLinus Torvalds break; 9591da177e4SLinus Torvalds case SIOCGIFADDR: 9601da177e4SLinus Torvalds case SIOCGIFBRDADDR: 9611da177e4SLinus Torvalds case SIOCGIFNETMASK: 9621da177e4SLinus Torvalds case SIOCGIFDSTADDR: 96303aef17bSAl Viro case SIOCGIFPFLAGS: 96429c49648SArnd Bergmann if (get_user_ifreq(&ifr, NULL, p)) 96503aef17bSAl Viro return -EFAULT; 96603aef17bSAl Viro err = devinet_ioctl(net, cmd, &ifr); 96729c49648SArnd Bergmann if (!err && put_user_ifreq(&ifr, p)) 96803aef17bSAl Viro err = -EFAULT; 96903aef17bSAl Viro break; 97003aef17bSAl Viro 97103aef17bSAl Viro case SIOCSIFADDR: 97203aef17bSAl Viro case SIOCSIFBRDADDR: 97303aef17bSAl Viro case SIOCSIFNETMASK: 9741da177e4SLinus Torvalds case SIOCSIFDSTADDR: 9751da177e4SLinus Torvalds case SIOCSIFPFLAGS: 9761da177e4SLinus Torvalds case SIOCSIFFLAGS: 97729c49648SArnd Bergmann if (get_user_ifreq(&ifr, NULL, p)) 97803aef17bSAl Viro return -EFAULT; 97903aef17bSAl Viro err = devinet_ioctl(net, cmd, &ifr); 9801da177e4SLinus Torvalds break; 9811da177e4SLinus Torvalds default: 982b5e5fa5eSChristoph Hellwig if (sk->sk_prot->ioctl) 983b5e5fa5eSChristoph Hellwig err = sk->sk_prot->ioctl(sk, cmd, arg); 984b5e5fa5eSChristoph Hellwig else 985b5e5fa5eSChristoph Hellwig err = -ENOIOCTLCMD; 9861da177e4SLinus Torvalds break; 9871da177e4SLinus Torvalds } 9881da177e4SLinus Torvalds return err; 9891da177e4SLinus Torvalds } 9903d1427f8SEric Dumazet EXPORT_SYMBOL(inet_ioctl); 9911da177e4SLinus Torvalds 992709b46e8SEric W. Biederman #ifdef CONFIG_COMPAT 993dc13c876SChristoph Hellwig static int inet_compat_routing_ioctl(struct sock *sk, unsigned int cmd, 994dc13c876SChristoph Hellwig struct compat_rtentry __user *ur) 995dc13c876SChristoph Hellwig { 996dc13c876SChristoph Hellwig compat_uptr_t rtdev; 997dc13c876SChristoph Hellwig struct rtentry rt; 998dc13c876SChristoph Hellwig 999dc13c876SChristoph Hellwig if (copy_from_user(&rt.rt_dst, &ur->rt_dst, 1000dc13c876SChristoph Hellwig 3 * sizeof(struct sockaddr)) || 1001dc13c876SChristoph Hellwig get_user(rt.rt_flags, &ur->rt_flags) || 1002dc13c876SChristoph Hellwig get_user(rt.rt_metric, &ur->rt_metric) || 1003dc13c876SChristoph Hellwig get_user(rt.rt_mtu, &ur->rt_mtu) || 1004dc13c876SChristoph Hellwig get_user(rt.rt_window, &ur->rt_window) || 1005dc13c876SChristoph Hellwig get_user(rt.rt_irtt, &ur->rt_irtt) || 1006dc13c876SChristoph Hellwig get_user(rtdev, &ur->rt_dev)) 1007dc13c876SChristoph Hellwig return -EFAULT; 1008dc13c876SChristoph Hellwig 1009dc13c876SChristoph Hellwig rt.rt_dev = compat_ptr(rtdev); 1010dc13c876SChristoph Hellwig return ip_rt_ioctl(sock_net(sk), cmd, &rt); 1011dc13c876SChristoph Hellwig } 1012dc13c876SChristoph Hellwig 1013686dc6b6SGerrit Renker static int inet_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) 1014709b46e8SEric W. Biederman { 1015dc13c876SChristoph Hellwig void __user *argp = compat_ptr(arg); 1016709b46e8SEric W. Biederman struct sock *sk = sock->sk; 1017709b46e8SEric W. Biederman 1018dc13c876SChristoph Hellwig switch (cmd) { 1019dc13c876SChristoph Hellwig case SIOCADDRT: 1020dc13c876SChristoph Hellwig case SIOCDELRT: 1021dc13c876SChristoph Hellwig return inet_compat_routing_ioctl(sk, cmd, argp); 1022dc13c876SChristoph Hellwig default: 1023dc13c876SChristoph Hellwig if (!sk->sk_prot->compat_ioctl) 1024dc13c876SChristoph Hellwig return -ENOIOCTLCMD; 1025dc13c876SChristoph Hellwig return sk->sk_prot->compat_ioctl(sk, cmd, arg); 1026709b46e8SEric W. Biederman } 1027dc13c876SChristoph Hellwig } 1028dc13c876SChristoph Hellwig #endif /* CONFIG_COMPAT */ 1029709b46e8SEric W. Biederman 103090ddc4f0SEric Dumazet const struct proto_ops inet_stream_ops = { 10311da177e4SLinus Torvalds .family = PF_INET, 10321da177e4SLinus Torvalds .owner = THIS_MODULE, 10331da177e4SLinus Torvalds .release = inet_release, 10341da177e4SLinus Torvalds .bind = inet_bind, 10351da177e4SLinus Torvalds .connect = inet_stream_connect, 10361da177e4SLinus Torvalds .socketpair = sock_no_socketpair, 10371da177e4SLinus Torvalds .accept = inet_accept, 10381da177e4SLinus Torvalds .getname = inet_getname, 1039a11e1d43SLinus Torvalds .poll = tcp_poll, 10401da177e4SLinus Torvalds .ioctl = inet_ioctl, 1041c7cbdbf2SArnd Bergmann .gettstamp = sock_gettstamp, 10421da177e4SLinus Torvalds .listen = inet_listen, 10431da177e4SLinus Torvalds .shutdown = inet_shutdown, 10441da177e4SLinus Torvalds .setsockopt = sock_common_setsockopt, 10451da177e4SLinus Torvalds .getsockopt = sock_common_getsockopt, 10467ba42910SChangli Gao .sendmsg = inet_sendmsg, 1047fec5e652STom Herbert .recvmsg = inet_recvmsg, 104805255b82SEric Dumazet #ifdef CONFIG_MMU 104993ab6cc6SEric Dumazet .mmap = tcp_mmap, 105005255b82SEric Dumazet #endif 10517ba42910SChangli Gao .sendpage = inet_sendpage, 10529c55e01cSJens Axboe .splice_read = tcp_splice_read, 105332035585STom Herbert .read_sock = tcp_read_sock, 1054965b57b4SCong Wang .read_skb = tcp_read_skb, 1055306b13ebSTom Herbert .sendmsg_locked = tcp_sendmsg_locked, 1056306b13ebSTom Herbert .sendpage_locked = tcp_sendpage_locked, 105732035585STom Herbert .peek_len = tcp_peek_len, 10583fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT 1059709b46e8SEric W. Biederman .compat_ioctl = inet_compat_ioctl, 10603fdadf7dSDmitry Mishin #endif 1061d1361840SEric Dumazet .set_rcvlowat = tcp_set_rcvlowat, 10621da177e4SLinus Torvalds }; 10633d1427f8SEric Dumazet EXPORT_SYMBOL(inet_stream_ops); 10641da177e4SLinus Torvalds 106590ddc4f0SEric Dumazet const struct proto_ops inet_dgram_ops = { 10661da177e4SLinus Torvalds .family = PF_INET, 10671da177e4SLinus Torvalds .owner = THIS_MODULE, 10681da177e4SLinus Torvalds .release = inet_release, 10691da177e4SLinus Torvalds .bind = inet_bind, 10701da177e4SLinus Torvalds .connect = inet_dgram_connect, 10711da177e4SLinus Torvalds .socketpair = sock_no_socketpair, 10721da177e4SLinus Torvalds .accept = sock_no_accept, 10731da177e4SLinus Torvalds .getname = inet_getname, 1074a11e1d43SLinus Torvalds .poll = udp_poll, 10751da177e4SLinus Torvalds .ioctl = inet_ioctl, 1076c7cbdbf2SArnd Bergmann .gettstamp = sock_gettstamp, 10771da177e4SLinus Torvalds .listen = sock_no_listen, 10781da177e4SLinus Torvalds .shutdown = inet_shutdown, 10791da177e4SLinus Torvalds .setsockopt = sock_common_setsockopt, 10801da177e4SLinus Torvalds .getsockopt = sock_common_getsockopt, 10811da177e4SLinus Torvalds .sendmsg = inet_sendmsg, 1082965b57b4SCong Wang .read_skb = udp_read_skb, 1083fec5e652STom Herbert .recvmsg = inet_recvmsg, 10841da177e4SLinus Torvalds .mmap = sock_no_mmap, 10851da177e4SLinus Torvalds .sendpage = inet_sendpage, 1086627d2d6bSsamanthakumar .set_peek_off = sk_set_peek_off, 1087543d9cfeSArnaldo Carvalho de Melo #ifdef CONFIG_COMPAT 1088709b46e8SEric W. Biederman .compat_ioctl = inet_compat_ioctl, 1089543d9cfeSArnaldo Carvalho de Melo #endif 10901da177e4SLinus Torvalds }; 10913d1427f8SEric Dumazet EXPORT_SYMBOL(inet_dgram_ops); 10921da177e4SLinus Torvalds 10931da177e4SLinus Torvalds /* 10941da177e4SLinus Torvalds * For SOCK_RAW sockets; should be the same as inet_dgram_ops but without 1095a11e1d43SLinus Torvalds * udp_poll 10961da177e4SLinus Torvalds */ 109790ddc4f0SEric Dumazet static const struct proto_ops inet_sockraw_ops = { 10981da177e4SLinus Torvalds .family = PF_INET, 10991da177e4SLinus Torvalds .owner = THIS_MODULE, 11001da177e4SLinus Torvalds .release = inet_release, 11011da177e4SLinus Torvalds .bind = inet_bind, 11021da177e4SLinus Torvalds .connect = inet_dgram_connect, 11031da177e4SLinus Torvalds .socketpair = sock_no_socketpair, 11041da177e4SLinus Torvalds .accept = sock_no_accept, 11051da177e4SLinus Torvalds .getname = inet_getname, 1106a11e1d43SLinus Torvalds .poll = datagram_poll, 11071da177e4SLinus Torvalds .ioctl = inet_ioctl, 1108c7cbdbf2SArnd Bergmann .gettstamp = sock_gettstamp, 11091da177e4SLinus Torvalds .listen = sock_no_listen, 11101da177e4SLinus Torvalds .shutdown = inet_shutdown, 11111da177e4SLinus Torvalds .setsockopt = sock_common_setsockopt, 11121da177e4SLinus Torvalds .getsockopt = sock_common_getsockopt, 11131da177e4SLinus Torvalds .sendmsg = inet_sendmsg, 1114fec5e652STom Herbert .recvmsg = inet_recvmsg, 11151da177e4SLinus Torvalds .mmap = sock_no_mmap, 11161da177e4SLinus Torvalds .sendpage = inet_sendpage, 1117543d9cfeSArnaldo Carvalho de Melo #ifdef CONFIG_COMPAT 1118709b46e8SEric W. Biederman .compat_ioctl = inet_compat_ioctl, 1119543d9cfeSArnaldo Carvalho de Melo #endif 11201da177e4SLinus Torvalds }; 11211da177e4SLinus Torvalds 1122ec1b4cf7SStephen Hemminger static const struct net_proto_family inet_family_ops = { 11231da177e4SLinus Torvalds .family = PF_INET, 11241da177e4SLinus Torvalds .create = inet_create, 11251da177e4SLinus Torvalds .owner = THIS_MODULE, 11261da177e4SLinus Torvalds }; 11271da177e4SLinus Torvalds 11281da177e4SLinus Torvalds /* Upon startup we insert all the elements in inetsw_array[] into 11291da177e4SLinus Torvalds * the linked list inetsw. 11301da177e4SLinus Torvalds */ 11311da177e4SLinus Torvalds static struct inet_protosw inetsw_array[] = 11321da177e4SLinus Torvalds { 11331da177e4SLinus Torvalds { 11341da177e4SLinus Torvalds .type = SOCK_STREAM, 11351da177e4SLinus Torvalds .protocol = IPPROTO_TCP, 11361da177e4SLinus Torvalds .prot = &tcp_prot, 11371da177e4SLinus Torvalds .ops = &inet_stream_ops, 1138d83d8461SArnaldo Carvalho de Melo .flags = INET_PROTOSW_PERMANENT | 1139d83d8461SArnaldo Carvalho de Melo INET_PROTOSW_ICSK, 11401da177e4SLinus Torvalds }, 11411da177e4SLinus Torvalds 11421da177e4SLinus Torvalds { 11431da177e4SLinus Torvalds .type = SOCK_DGRAM, 11441da177e4SLinus Torvalds .protocol = IPPROTO_UDP, 11451da177e4SLinus Torvalds .prot = &udp_prot, 11461da177e4SLinus Torvalds .ops = &inet_dgram_ops, 11471da177e4SLinus Torvalds .flags = INET_PROTOSW_PERMANENT, 11481da177e4SLinus Torvalds }, 11491da177e4SLinus Torvalds 1150c319b4d7SVasiliy Kulikov { 1151c319b4d7SVasiliy Kulikov .type = SOCK_DGRAM, 1152c319b4d7SVasiliy Kulikov .protocol = IPPROTO_ICMP, 1153c319b4d7SVasiliy Kulikov .prot = &ping_prot, 115477d4b1d3SEric Dumazet .ops = &inet_sockraw_ops, 1155c319b4d7SVasiliy Kulikov .flags = INET_PROTOSW_REUSE, 1156c319b4d7SVasiliy Kulikov }, 11571da177e4SLinus Torvalds 11581da177e4SLinus Torvalds { 11591da177e4SLinus Torvalds .type = SOCK_RAW, 11601da177e4SLinus Torvalds .protocol = IPPROTO_IP, /* wild card */ 11611da177e4SLinus Torvalds .prot = &raw_prot, 11621da177e4SLinus Torvalds .ops = &inet_sockraw_ops, 11631da177e4SLinus Torvalds .flags = INET_PROTOSW_REUSE, 11641da177e4SLinus Torvalds } 11651da177e4SLinus Torvalds }; 11661da177e4SLinus Torvalds 1167c40f6fffSDenis Cheng #define INETSW_ARRAY_LEN ARRAY_SIZE(inetsw_array) 11681da177e4SLinus Torvalds 11691da177e4SLinus Torvalds void inet_register_protosw(struct inet_protosw *p) 11701da177e4SLinus Torvalds { 11711da177e4SLinus Torvalds struct list_head *lh; 11721da177e4SLinus Torvalds struct inet_protosw *answer; 11731da177e4SLinus Torvalds int protocol = p->protocol; 11741da177e4SLinus Torvalds struct list_head *last_perm; 11751da177e4SLinus Torvalds 11761da177e4SLinus Torvalds spin_lock_bh(&inetsw_lock); 11771da177e4SLinus Torvalds 11781da177e4SLinus Torvalds if (p->type >= SOCK_MAX) 11791da177e4SLinus Torvalds goto out_illegal; 11801da177e4SLinus Torvalds 11811da177e4SLinus Torvalds /* If we are trying to override a permanent protocol, bail. */ 11821da177e4SLinus Torvalds last_perm = &inetsw[p->type]; 11831da177e4SLinus Torvalds list_for_each(lh, &inetsw[p->type]) { 11841da177e4SLinus Torvalds answer = list_entry(lh, struct inet_protosw, list); 11851da177e4SLinus Torvalds /* Check only the non-wild match. */ 1186f6c53334SJunwei Zhang if ((INET_PROTOSW_PERMANENT & answer->flags) == 0) 11871da177e4SLinus Torvalds break; 1188f6c53334SJunwei Zhang if (protocol == answer->protocol) 1189f6c53334SJunwei Zhang goto out_permanent; 11901da177e4SLinus Torvalds last_perm = lh; 11911da177e4SLinus Torvalds } 11921da177e4SLinus Torvalds 11931da177e4SLinus Torvalds /* Add the new entry after the last permanent entry if any, so that 11941da177e4SLinus Torvalds * the new entry does not override a permanent entry when matched with 11951da177e4SLinus Torvalds * a wild-card protocol. But it is allowed to override any existing 11961da177e4SLinus Torvalds * non-permanent entry. This means that when we remove this entry, the 11971da177e4SLinus Torvalds * system automatically returns to the old behavior. 11981da177e4SLinus Torvalds */ 11991da177e4SLinus Torvalds list_add_rcu(&p->list, last_perm); 12001da177e4SLinus Torvalds out: 12011da177e4SLinus Torvalds spin_unlock_bh(&inetsw_lock); 12021da177e4SLinus Torvalds 12031da177e4SLinus Torvalds return; 12041da177e4SLinus Torvalds 12051da177e4SLinus Torvalds out_permanent: 1206058bd4d2SJoe Perches pr_err("Attempt to override permanent protocol %d\n", protocol); 12071da177e4SLinus Torvalds goto out; 12081da177e4SLinus Torvalds 12091da177e4SLinus Torvalds out_illegal: 1210058bd4d2SJoe Perches pr_err("Ignoring attempt to register invalid socket type %d\n", 12111da177e4SLinus Torvalds p->type); 12121da177e4SLinus Torvalds goto out; 12131da177e4SLinus Torvalds } 12143d1427f8SEric Dumazet EXPORT_SYMBOL(inet_register_protosw); 12151da177e4SLinus Torvalds 12161da177e4SLinus Torvalds void inet_unregister_protosw(struct inet_protosw *p) 12171da177e4SLinus Torvalds { 12181da177e4SLinus Torvalds if (INET_PROTOSW_PERMANENT & p->flags) { 1219058bd4d2SJoe Perches pr_err("Attempt to unregister permanent protocol %d\n", 12201da177e4SLinus Torvalds p->protocol); 12211da177e4SLinus Torvalds } else { 12221da177e4SLinus Torvalds spin_lock_bh(&inetsw_lock); 12231da177e4SLinus Torvalds list_del_rcu(&p->list); 12241da177e4SLinus Torvalds spin_unlock_bh(&inetsw_lock); 12251da177e4SLinus Torvalds 12261da177e4SLinus Torvalds synchronize_net(); 12271da177e4SLinus Torvalds } 12281da177e4SLinus Torvalds } 12293d1427f8SEric Dumazet EXPORT_SYMBOL(inet_unregister_protosw); 12301da177e4SLinus Torvalds 123132519f11SArnaldo Carvalho de Melo static int inet_sk_reselect_saddr(struct sock *sk) 123232519f11SArnaldo Carvalho de Melo { 123332519f11SArnaldo Carvalho de Melo struct inet_sock *inet = inet_sk(sk); 1234c720c7e8SEric Dumazet __be32 old_saddr = inet->inet_saddr; 1235c720c7e8SEric Dumazet __be32 daddr = inet->inet_daddr; 12366e869138SDavid S. Miller struct flowi4 *fl4; 1237b23dd4feSDavid S. Miller struct rtable *rt; 1238b23dd4feSDavid S. Miller __be32 new_saddr; 1239f6d8bd05SEric Dumazet struct ip_options_rcu *inet_opt; 124028044fc1SJoanne Koong int err; 124132519f11SArnaldo Carvalho de Melo 1242f6d8bd05SEric Dumazet inet_opt = rcu_dereference_protected(inet->inet_opt, 12431e1d04e6SHannes Frederic Sowa lockdep_sock_is_held(sk)); 1244f6d8bd05SEric Dumazet if (inet_opt && inet_opt->opt.srr) 1245f6d8bd05SEric Dumazet daddr = inet_opt->opt.faddr; 124632519f11SArnaldo Carvalho de Melo 124732519f11SArnaldo Carvalho de Melo /* Query new route. */ 12486e869138SDavid S. Miller fl4 = &inet->cork.fl.u.ip4; 124967e1e2f4SGuillaume Nault rt = ip_route_connect(fl4, daddr, 0, sk->sk_bound_dev_if, 125067e1e2f4SGuillaume Nault sk->sk_protocol, inet->inet_sport, 125167e1e2f4SGuillaume Nault inet->inet_dport, sk); 1252b23dd4feSDavid S. Miller if (IS_ERR(rt)) 1253b23dd4feSDavid S. Miller return PTR_ERR(rt); 125432519f11SArnaldo Carvalho de Melo 12556e869138SDavid S. Miller new_saddr = fl4->saddr; 125632519f11SArnaldo Carvalho de Melo 125728044fc1SJoanne Koong if (new_saddr == old_saddr) { 125828044fc1SJoanne Koong sk_setup_caps(sk, &rt->dst); 125932519f11SArnaldo Carvalho de Melo return 0; 126028044fc1SJoanne Koong } 126128044fc1SJoanne Koong 12628c5dae4cSKuniyuki Iwashima err = inet_bhash2_update_saddr(sk, &new_saddr, AF_INET); 126328044fc1SJoanne Koong if (err) { 126428044fc1SJoanne Koong ip_rt_put(rt); 126528044fc1SJoanne Koong return err; 126628044fc1SJoanne Koong } 126728044fc1SJoanne Koong 126828044fc1SJoanne Koong sk_setup_caps(sk, &rt->dst); 126932519f11SArnaldo Carvalho de Melo 1270e49e4affSKuniyuki Iwashima if (READ_ONCE(sock_net(sk)->ipv4.sysctl_ip_dynaddr) > 1) { 1271058bd4d2SJoe Perches pr_info("%s(): shifting inet->saddr from %pI4 to %pI4\n", 1272673d57e7SHarvey Harrison __func__, &old_saddr, &new_saddr); 127332519f11SArnaldo Carvalho de Melo } 127432519f11SArnaldo Carvalho de Melo 127532519f11SArnaldo Carvalho de Melo /* 127632519f11SArnaldo Carvalho de Melo * XXX The only one ugly spot where we need to 127732519f11SArnaldo Carvalho de Melo * XXX really change the sockets identity after 127832519f11SArnaldo Carvalho de Melo * XXX it has entered the hashes. -DaveM 127932519f11SArnaldo Carvalho de Melo * 128032519f11SArnaldo Carvalho de Melo * Besides that, it does not check for connection 128132519f11SArnaldo Carvalho de Melo * uniqueness. Wait for troubles. 128232519f11SArnaldo Carvalho de Melo */ 1283086c653fSCraig Gallek return __sk_prot_rehash(sk); 128432519f11SArnaldo Carvalho de Melo } 128532519f11SArnaldo Carvalho de Melo 128632519f11SArnaldo Carvalho de Melo int inet_sk_rebuild_header(struct sock *sk) 128732519f11SArnaldo Carvalho de Melo { 128832519f11SArnaldo Carvalho de Melo struct inet_sock *inet = inet_sk(sk); 128932519f11SArnaldo Carvalho de Melo struct rtable *rt = (struct rtable *)__sk_dst_check(sk, 0); 12903ca3c68eSAl Viro __be32 daddr; 1291f6d8bd05SEric Dumazet struct ip_options_rcu *inet_opt; 12926e869138SDavid S. Miller struct flowi4 *fl4; 129332519f11SArnaldo Carvalho de Melo int err; 129432519f11SArnaldo Carvalho de Melo 129532519f11SArnaldo Carvalho de Melo /* Route is OK, nothing to do. */ 129632519f11SArnaldo Carvalho de Melo if (rt) 129732519f11SArnaldo Carvalho de Melo return 0; 129832519f11SArnaldo Carvalho de Melo 129932519f11SArnaldo Carvalho de Melo /* Reroute. */ 1300f6d8bd05SEric Dumazet rcu_read_lock(); 1301f6d8bd05SEric Dumazet inet_opt = rcu_dereference(inet->inet_opt); 1302c720c7e8SEric Dumazet daddr = inet->inet_daddr; 1303f6d8bd05SEric Dumazet if (inet_opt && inet_opt->opt.srr) 1304f6d8bd05SEric Dumazet daddr = inet_opt->opt.faddr; 1305f6d8bd05SEric Dumazet rcu_read_unlock(); 13066e869138SDavid S. Miller fl4 = &inet->cork.fl.u.ip4; 13076e869138SDavid S. Miller rt = ip_route_output_ports(sock_net(sk), fl4, sk, daddr, inet->inet_saddr, 130878fbfd8aSDavid S. Miller inet->inet_dport, inet->inet_sport, 130978fbfd8aSDavid S. Miller sk->sk_protocol, RT_CONN_FLAGS(sk), 131078fbfd8aSDavid S. Miller sk->sk_bound_dev_if); 1311b23dd4feSDavid S. Miller if (!IS_ERR(rt)) { 1312b23dd4feSDavid S. Miller err = 0; 1313d8d1f30bSChangli Gao sk_setup_caps(sk, &rt->dst); 1314b23dd4feSDavid S. Miller } else { 1315b23dd4feSDavid S. Miller err = PTR_ERR(rt); 1316b23dd4feSDavid S. Miller 131732519f11SArnaldo Carvalho de Melo /* Routing failed... */ 131832519f11SArnaldo Carvalho de Melo sk->sk_route_caps = 0; 131932519f11SArnaldo Carvalho de Melo /* 132032519f11SArnaldo Carvalho de Melo * Other protocols have to map its equivalent state to TCP_SYN_SENT. 132132519f11SArnaldo Carvalho de Melo * DCCP maps its DCCP_REQUESTING state to TCP_SYN_SENT. -acme 132232519f11SArnaldo Carvalho de Melo */ 1323e49e4affSKuniyuki Iwashima if (!READ_ONCE(sock_net(sk)->ipv4.sysctl_ip_dynaddr) || 132432519f11SArnaldo Carvalho de Melo sk->sk_state != TCP_SYN_SENT || 132532519f11SArnaldo Carvalho de Melo (sk->sk_userlocks & SOCK_BINDADDR_LOCK) || 132632519f11SArnaldo Carvalho de Melo (err = inet_sk_reselect_saddr(sk)) != 0) 13272f2d9972SEric Dumazet WRITE_ONCE(sk->sk_err_soft, -err); 132832519f11SArnaldo Carvalho de Melo } 132932519f11SArnaldo Carvalho de Melo 133032519f11SArnaldo Carvalho de Melo return err; 133132519f11SArnaldo Carvalho de Melo } 133232519f11SArnaldo Carvalho de Melo EXPORT_SYMBOL(inet_sk_rebuild_header); 133332519f11SArnaldo Carvalho de Melo 1334563e0bb0SYafang Shao void inet_sk_set_state(struct sock *sk, int state) 1335563e0bb0SYafang Shao { 1336563e0bb0SYafang Shao trace_inet_sock_set_state(sk, sk->sk_state, state); 1337563e0bb0SYafang Shao sk->sk_state = state; 1338563e0bb0SYafang Shao } 1339563e0bb0SYafang Shao EXPORT_SYMBOL(inet_sk_set_state); 1340563e0bb0SYafang Shao 1341563e0bb0SYafang Shao void inet_sk_state_store(struct sock *sk, int newstate) 1342563e0bb0SYafang Shao { 1343563e0bb0SYafang Shao trace_inet_sock_set_state(sk, sk->sk_state, newstate); 1344563e0bb0SYafang Shao smp_store_release(&sk->sk_state, newstate); 1345563e0bb0SYafang Shao } 1346563e0bb0SYafang Shao 1347b8921ca8STom Herbert struct sk_buff *inet_gso_segment(struct sk_buff *skb, 1348c8f44affSMichał Mirosław netdev_features_t features) 1349f4c50d99SHerbert Xu { 13500c19f846SWillem de Bruijn bool udpfrag = false, fixedid = false, gso_partial, encap; 1351f4c50d99SHerbert Xu struct sk_buff *segs = ERR_PTR(-EINVAL); 1352bca49f84SVlad Yasevich const struct net_offload *ops; 13530c19f846SWillem de Bruijn unsigned int offset = 0; 1354f9242b6bSDavid S. Miller struct iphdr *iph; 1355802ab55aSAlexander Duyck int proto, tot_len; 13563347c960SEric Dumazet int nhoff; 1357f4c50d99SHerbert Xu int ihl; 1358f4c50d99SHerbert Xu int id; 1359f4c50d99SHerbert Xu 13603347c960SEric Dumazet skb_reset_network_header(skb); 13613347c960SEric Dumazet nhoff = skb_network_header(skb) - skb_mac_header(skb); 1362bbcf467dSHerbert Xu if (unlikely(!pskb_may_pull(skb, sizeof(*iph)))) 1363f4c50d99SHerbert Xu goto out; 1364f4c50d99SHerbert Xu 1365eddc9ec5SArnaldo Carvalho de Melo iph = ip_hdr(skb); 1366f4c50d99SHerbert Xu ihl = iph->ihl * 4; 1367f4c50d99SHerbert Xu if (ihl < sizeof(*iph)) 1368f4c50d99SHerbert Xu goto out; 1369f4c50d99SHerbert Xu 137047d27aadSEric Dumazet id = ntohs(iph->id); 137147d27aadSEric Dumazet proto = iph->protocol; 137247d27aadSEric Dumazet 137347d27aadSEric Dumazet /* Warning: after this point, iph might be no longer valid */ 1374bbcf467dSHerbert Xu if (unlikely(!pskb_may_pull(skb, ihl))) 1375f4c50d99SHerbert Xu goto out; 137647d27aadSEric Dumazet __skb_pull(skb, ihl); 1377f4c50d99SHerbert Xu 13788c3a897bSEric Dumazet encap = SKB_GSO_CB(skb)->encap_level > 0; 13798c3a897bSEric Dumazet if (encap) 13801e16aa3dSFlorian Westphal features &= skb->dev->hw_enc_features; 13813347c960SEric Dumazet SKB_GSO_CB(skb)->encap_level += ihl; 138273136267SPravin B Shelar 1383badff6d0SArnaldo Carvalho de Melo skb_reset_transport_header(skb); 138447d27aadSEric Dumazet 1385f4c50d99SHerbert Xu segs = ERR_PTR(-EPROTONOSUPPORT); 1386f4c50d99SHerbert Xu 1387cbc53e08SAlexander Duyck if (!skb->encapsulation || encap) { 13880c19f846SWillem de Bruijn udpfrag = !!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP); 1389cbc53e08SAlexander Duyck fixedid = !!(skb_shinfo(skb)->gso_type & SKB_GSO_TCP_FIXEDID); 1390cbc53e08SAlexander Duyck 1391cbc53e08SAlexander Duyck /* fixed ID is invalid if DF bit is not set */ 1392a5108878SArnaldo Carvalho de Melo if (fixedid && !(ip_hdr(skb)->frag_off & htons(IP_DF))) 1393cbc53e08SAlexander Duyck goto out; 1394cbc53e08SAlexander Duyck } 1395dcd60771SEric Dumazet 1396bca49f84SVlad Yasevich ops = rcu_dereference(inet_offloads[proto]); 1397cc20ccedSTao Liu if (likely(ops && ops->callbacks.gso_segment)) { 1398f191a1d1SVlad Yasevich segs = ops->callbacks.gso_segment(skb, features); 1399cc20ccedSTao Liu if (!segs) 1400cc20ccedSTao Liu skb->network_header = skb_mac_header(skb) + nhoff - skb->head; 1401cc20ccedSTao Liu } 1402f4c50d99SHerbert Xu 140350c3a487SYOSHIFUJI Hideaki / 吉藤英明 if (IS_ERR_OR_NULL(segs)) 1404f4c50d99SHerbert Xu goto out; 1405f4c50d99SHerbert Xu 140607b26c94SSteffen Klassert gso_partial = !!(skb_shinfo(segs)->gso_type & SKB_GSO_PARTIAL); 140707b26c94SSteffen Klassert 1408f4c50d99SHerbert Xu skb = segs; 1409f4c50d99SHerbert Xu do { 14103347c960SEric Dumazet iph = (struct iphdr *)(skb_mac_header(skb) + nhoff); 14110c19f846SWillem de Bruijn if (udpfrag) { 14120c19f846SWillem de Bruijn iph->frag_off = htons(offset >> 3); 14130c19f846SWillem de Bruijn if (skb->next) 14140c19f846SWillem de Bruijn iph->frag_off |= htons(IP_MF); 14150c19f846SWillem de Bruijn offset += skb->len - nhoff - ihl; 14160c19f846SWillem de Bruijn tot_len = skb->len - nhoff; 14170c19f846SWillem de Bruijn } else if (skb_is_gso(skb)) { 1418802ab55aSAlexander Duyck if (!fixedid) { 1419802ab55aSAlexander Duyck iph->id = htons(id); 1420802ab55aSAlexander Duyck id += skb_shinfo(skb)->gso_segs; 1421490ab081SPravin B Shelar } 142207b26c94SSteffen Klassert 142307b26c94SSteffen Klassert if (gso_partial) 1424802ab55aSAlexander Duyck tot_len = skb_shinfo(skb)->gso_size + 1425802ab55aSAlexander Duyck SKB_GSO_CB(skb)->data_offset + 1426802ab55aSAlexander Duyck skb->head - (unsigned char *)iph; 142707b26c94SSteffen Klassert else 142807b26c94SSteffen Klassert tot_len = skb->len - nhoff; 1429802ab55aSAlexander Duyck } else { 1430802ab55aSAlexander Duyck if (!fixedid) 1431802ab55aSAlexander Duyck iph->id = htons(id++); 1432802ab55aSAlexander Duyck tot_len = skb->len - nhoff; 1433802ab55aSAlexander Duyck } 1434802ab55aSAlexander Duyck iph->tot_len = htons(tot_len); 143547d27aadSEric Dumazet ip_send_check(iph); 14368c3a897bSEric Dumazet if (encap) 14373347c960SEric Dumazet skb_reset_inner_headers(skb); 14383347c960SEric Dumazet skb->network_header = (u8 *)iph - skb->head; 1439c56cae23SToke Høiland-Jørgensen skb_reset_mac_len(skb); 1440f4c50d99SHerbert Xu } while ((skb = skb->next)); 1441f4c50d99SHerbert Xu 1442f4c50d99SHerbert Xu out: 1443f4c50d99SHerbert Xu return segs; 1444f4c50d99SHerbert Xu } 1445f4c50d99SHerbert Xu 1446418e897eSWillem de Bruijn static struct sk_buff *ipip_gso_segment(struct sk_buff *skb, 1447418e897eSWillem de Bruijn netdev_features_t features) 1448418e897eSWillem de Bruijn { 1449418e897eSWillem de Bruijn if (!(skb_shinfo(skb)->gso_type & SKB_GSO_IPXIP4)) 1450418e897eSWillem de Bruijn return ERR_PTR(-EINVAL); 1451418e897eSWillem de Bruijn 1452418e897eSWillem de Bruijn return inet_gso_segment(skb, features); 1453418e897eSWillem de Bruijn } 1454418e897eSWillem de Bruijn 1455d4546c25SDavid Miller struct sk_buff *inet_gro_receive(struct list_head *head, struct sk_buff *skb) 145673cc19f1SHerbert Xu { 1457bca49f84SVlad Yasevich const struct net_offload *ops; 1458d4546c25SDavid Miller struct sk_buff *pp = NULL; 1459b71d1d42SEric Dumazet const struct iphdr *iph; 1460d4546c25SDavid Miller struct sk_buff *p; 1461a5b1cf28SHerbert Xu unsigned int hlen; 1462a5b1cf28SHerbert Xu unsigned int off; 14631075f3f6SHerbert Xu unsigned int id; 146473cc19f1SHerbert Xu int flush = 1; 146573cc19f1SHerbert Xu int proto; 146673cc19f1SHerbert Xu 1467a5b1cf28SHerbert Xu off = skb_gro_offset(skb); 1468a5b1cf28SHerbert Xu hlen = off + sizeof(*iph); 146935ffb665SRichard Gobert iph = skb_gro_header(skb, hlen, off); 147086911732SHerbert Xu if (unlikely(!iph)) 147173cc19f1SHerbert Xu goto out; 147273cc19f1SHerbert Xu 1473f9242b6bSDavid S. Miller proto = iph->protocol; 147473cc19f1SHerbert Xu 1475bca49f84SVlad Yasevich ops = rcu_dereference(inet_offloads[proto]); 1476f191a1d1SVlad Yasevich if (!ops || !ops->callbacks.gro_receive) 1477fc1ca334SEric Dumazet goto out; 147873cc19f1SHerbert Xu 1479a5ad24beSHerbert Xu if (*(u8 *)iph != 0x45) 1480fc1ca334SEric Dumazet goto out; 148173cc19f1SHerbert Xu 14829b83e031SSteffen Klassert if (ip_is_fragment(iph)) 1483fc1ca334SEric Dumazet goto out; 14849b83e031SSteffen Klassert 1485a9e050f4SEric Dumazet if (unlikely(ip_fast_csum((u8 *)iph, 5))) 1486fc1ca334SEric Dumazet goto out; 148773cc19f1SHerbert Xu 1488b1a78b9bSXin Long NAPI_GRO_CB(skb)->proto = proto; 14890eae88f3SEric Dumazet id = ntohl(*(__be32 *)&iph->id); 1490db8caf3dSEric Dumazet flush = (u16)((ntohl(*(__be32 *)iph) ^ skb_gro_len(skb)) | (id & ~IP_DF)); 14911075f3f6SHerbert Xu id >>= 16; 149273cc19f1SHerbert Xu 1493d4546c25SDavid Miller list_for_each_entry(p, head, list) { 149473cc19f1SHerbert Xu struct iphdr *iph2; 14951530545eSAlexander Duyck u16 flush_id; 149673cc19f1SHerbert Xu 149773cc19f1SHerbert Xu if (!NAPI_GRO_CB(p)->same_flow) 149873cc19f1SHerbert Xu continue; 149973cc19f1SHerbert Xu 1500299603e8SJerry Chu iph2 = (struct iphdr *)(p->data + off); 1501299603e8SJerry Chu /* The above works because, with the exception of the top 1502299603e8SJerry Chu * (inner most) layer, we only aggregate pkts with the same 1503299603e8SJerry Chu * hdr length so all the hdrs we'll need to verify will start 1504299603e8SJerry Chu * at the same offset. 1505299603e8SJerry Chu */ 1506a5ad24beSHerbert Xu if ((iph->protocol ^ iph2->protocol) | 15070eae88f3SEric Dumazet ((__force u32)iph->saddr ^ (__force u32)iph2->saddr) | 15080eae88f3SEric Dumazet ((__force u32)iph->daddr ^ (__force u32)iph2->daddr)) { 150973cc19f1SHerbert Xu NAPI_GRO_CB(p)->same_flow = 0; 151073cc19f1SHerbert Xu continue; 151173cc19f1SHerbert Xu } 151273cc19f1SHerbert Xu 151373cc19f1SHerbert Xu /* All fields must match except length and checksum. */ 151473cc19f1SHerbert Xu NAPI_GRO_CB(p)->flush |= 1515a5ad24beSHerbert Xu (iph->ttl ^ iph2->ttl) | 1516a9e050f4SEric Dumazet (iph->tos ^ iph2->tos) | 1517bf5a755fSJerry Chu ((iph->frag_off ^ iph2->frag_off) & htons(IP_DF)); 151873cc19f1SHerbert Xu 151973cc19f1SHerbert Xu NAPI_GRO_CB(p)->flush |= flush; 15201530545eSAlexander Duyck 15211530545eSAlexander Duyck /* We need to store of the IP ID check to be included later 15221530545eSAlexander Duyck * when we can verify that this packet does in fact belong 15231530545eSAlexander Duyck * to a given flow. 15241530545eSAlexander Duyck */ 15251530545eSAlexander Duyck flush_id = (u16)(id - ntohs(iph2->id)); 15261530545eSAlexander Duyck 15271530545eSAlexander Duyck /* This bit of code makes it much easier for us to identify 15281530545eSAlexander Duyck * the cases where we are doing atomic vs non-atomic IP ID 15291530545eSAlexander Duyck * checks. Specifically an atomic check can return IP ID 15301530545eSAlexander Duyck * values 0 - 0xFFFF, while a non-atomic check can only 15311530545eSAlexander Duyck * return 0 or 0xFFFF. 15321530545eSAlexander Duyck */ 15331530545eSAlexander Duyck if (!NAPI_GRO_CB(p)->is_atomic || 15341530545eSAlexander Duyck !(iph->frag_off & htons(IP_DF))) { 15351530545eSAlexander Duyck flush_id ^= NAPI_GRO_CB(p)->count; 15361530545eSAlexander Duyck flush_id = flush_id ? 0xFFFF : 0; 153773cc19f1SHerbert Xu } 153873cc19f1SHerbert Xu 15391530545eSAlexander Duyck /* If the previous IP ID value was based on an atomic 15401530545eSAlexander Duyck * datagram we can overwrite the value and ignore it. 15411530545eSAlexander Duyck */ 15421530545eSAlexander Duyck if (NAPI_GRO_CB(skb)->is_atomic) 15431530545eSAlexander Duyck NAPI_GRO_CB(p)->flush_id = flush_id; 15441530545eSAlexander Duyck else 15451530545eSAlexander Duyck NAPI_GRO_CB(p)->flush_id |= flush_id; 15461530545eSAlexander Duyck } 15471530545eSAlexander Duyck 15481530545eSAlexander Duyck NAPI_GRO_CB(skb)->is_atomic = !!(iph->frag_off & htons(IP_DF)); 154973cc19f1SHerbert Xu NAPI_GRO_CB(skb)->flush |= flush; 1550299603e8SJerry Chu skb_set_network_header(skb, off); 1551299603e8SJerry Chu /* The above will be needed by the transport layer if there is one 1552299603e8SJerry Chu * immediately following this IP hdr. 1553299603e8SJerry Chu */ 1554299603e8SJerry Chu 15552c804d0fSEric Dumazet /* Note : No need to call skb_gro_postpull_rcsum() here, 15562c804d0fSEric Dumazet * as we already checked checksum over ipv4 header was 0 15572c804d0fSEric Dumazet */ 155886911732SHerbert Xu skb_gro_pull(skb, sizeof(*iph)); 155986911732SHerbert Xu skb_set_transport_header(skb, skb_gro_offset(skb)); 156073cc19f1SHerbert Xu 1561028e0a47SPaolo Abeni pp = indirect_call_gro_receive(tcp4_gro_receive, udp4_gro_receive, 1562028e0a47SPaolo Abeni ops->callbacks.gro_receive, head, skb); 156373cc19f1SHerbert Xu 156473cc19f1SHerbert Xu out: 15655f114163SSteffen Klassert skb_gro_flush_final(skb, pp, flush); 156673cc19f1SHerbert Xu 156773cc19f1SHerbert Xu return pp; 156873cc19f1SHerbert Xu } 156973cc19f1SHerbert Xu 1570d4546c25SDavid Miller static struct sk_buff *ipip_gro_receive(struct list_head *head, 1571fac8e0f5SJesse Gross struct sk_buff *skb) 1572fac8e0f5SJesse Gross { 1573fac8e0f5SJesse Gross if (NAPI_GRO_CB(skb)->encap_mark) { 1574fac8e0f5SJesse Gross NAPI_GRO_CB(skb)->flush = 1; 1575fac8e0f5SJesse Gross return NULL; 1576fac8e0f5SJesse Gross } 1577fac8e0f5SJesse Gross 1578fac8e0f5SJesse Gross NAPI_GRO_CB(skb)->encap_mark = 1; 1579fac8e0f5SJesse Gross 1580fac8e0f5SJesse Gross return inet_gro_receive(head, skb); 1581fac8e0f5SJesse Gross } 1582fac8e0f5SJesse Gross 1583822c8685SDeepa Dinamani #define SECONDS_PER_DAY 86400 1584822c8685SDeepa Dinamani 1585822c8685SDeepa Dinamani /* inet_current_timestamp - Return IP network timestamp 1586822c8685SDeepa Dinamani * 1587822c8685SDeepa Dinamani * Return milliseconds since midnight in network byte order. 1588822c8685SDeepa Dinamani */ 1589822c8685SDeepa Dinamani __be32 inet_current_timestamp(void) 1590822c8685SDeepa Dinamani { 1591822c8685SDeepa Dinamani u32 secs; 1592822c8685SDeepa Dinamani u32 msecs; 1593822c8685SDeepa Dinamani struct timespec64 ts; 1594822c8685SDeepa Dinamani 1595822c8685SDeepa Dinamani ktime_get_real_ts64(&ts); 1596822c8685SDeepa Dinamani 1597822c8685SDeepa Dinamani /* Get secs since midnight. */ 1598822c8685SDeepa Dinamani (void)div_u64_rem(ts.tv_sec, SECONDS_PER_DAY, &secs); 1599822c8685SDeepa Dinamani /* Convert to msecs. */ 1600822c8685SDeepa Dinamani msecs = secs * MSEC_PER_SEC; 1601822c8685SDeepa Dinamani /* Convert nsec to msec. */ 1602822c8685SDeepa Dinamani msecs += (u32)ts.tv_nsec / NSEC_PER_MSEC; 1603822c8685SDeepa Dinamani 1604822c8685SDeepa Dinamani /* Convert to network byte order. */ 16053ba9d300SDeepa Dinamani return htonl(msecs); 1606822c8685SDeepa Dinamani } 1607822c8685SDeepa Dinamani EXPORT_SYMBOL(inet_current_timestamp); 1608822c8685SDeepa Dinamani 1609f4713a3dSWillem de Bruijn int inet_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len) 1610f4713a3dSWillem de Bruijn { 1611f4713a3dSWillem de Bruijn if (sk->sk_family == AF_INET) 1612f4713a3dSWillem de Bruijn return ip_recv_error(sk, msg, len, addr_len); 1613f4713a3dSWillem de Bruijn #if IS_ENABLED(CONFIG_IPV6) 1614f4713a3dSWillem de Bruijn if (sk->sk_family == AF_INET6) 1615f4713a3dSWillem de Bruijn return pingv6_ops.ipv6_recv_error(sk, msg, len, addr_len); 1616f4713a3dSWillem de Bruijn #endif 1617f4713a3dSWillem de Bruijn return -EINVAL; 1618f4713a3dSWillem de Bruijn } 1619f4713a3dSWillem de Bruijn 1620b8921ca8STom Herbert int inet_gro_complete(struct sk_buff *skb, int nhoff) 162173cc19f1SHerbert Xu { 1622299603e8SJerry Chu struct iphdr *iph = (struct iphdr *)(skb->data + nhoff); 1623bca49f84SVlad Yasevich const struct net_offload *ops; 1624b1a78b9bSXin Long __be16 totlen = iph->tot_len; 1625f9242b6bSDavid S. Miller int proto = iph->protocol; 1626f9242b6bSDavid S. Miller int err = -ENOSYS; 162773cc19f1SHerbert Xu 1628294acf1cSPaolo Abeni if (skb->encapsulation) { 1629294acf1cSPaolo Abeni skb_set_inner_protocol(skb, cpu_to_be16(ETH_P_IP)); 1630c3caf119SJerry Chu skb_set_inner_network_header(skb, nhoff); 1631294acf1cSPaolo Abeni } 1632c3caf119SJerry Chu 1633b1a78b9bSXin Long iph_set_totlen(iph, skb->len - nhoff); 1634b1a78b9bSXin Long csum_replace2(&iph->check, totlen, iph->tot_len); 163573cc19f1SHerbert Xu 1636bca49f84SVlad Yasevich ops = rcu_dereference(inet_offloads[proto]); 1637f191a1d1SVlad Yasevich if (WARN_ON(!ops || !ops->callbacks.gro_complete)) 1638627b94f7SEric Dumazet goto out; 163973cc19f1SHerbert Xu 1640299603e8SJerry Chu /* Only need to add sizeof(*iph) to get to the next hdr below 1641299603e8SJerry Chu * because any hdr with option will have been flushed in 1642299603e8SJerry Chu * inet_gro_receive(). 1643299603e8SJerry Chu */ 1644028e0a47SPaolo Abeni err = INDIRECT_CALL_2(ops->callbacks.gro_complete, 1645028e0a47SPaolo Abeni tcp4_gro_complete, udp4_gro_complete, 1646028e0a47SPaolo Abeni skb, nhoff + sizeof(*iph)); 164773cc19f1SHerbert Xu 1648627b94f7SEric Dumazet out: 164973cc19f1SHerbert Xu return err; 165073cc19f1SHerbert Xu } 165173cc19f1SHerbert Xu 1652b8cba75bSJesse Gross static int ipip_gro_complete(struct sk_buff *skb, int nhoff) 1653b8cba75bSJesse Gross { 1654b8cba75bSJesse Gross skb->encapsulation = 1; 16557e13318dSTom Herbert skb_shinfo(skb)->gso_type |= SKB_GSO_IPXIP4; 1656b8cba75bSJesse Gross return inet_gro_complete(skb, nhoff); 1657b8cba75bSJesse Gross } 1658b8cba75bSJesse Gross 1659eee4fe4dSDenis V. Lunev int inet_ctl_sock_create(struct sock **sk, unsigned short family, 16605677242fSDenis V. Lunev unsigned short type, unsigned char protocol, 16615677242fSDenis V. Lunev struct net *net) 16623d58b5faSDenis V. Lunev { 1663eee4fe4dSDenis V. Lunev struct socket *sock; 166426abe143SEric W. Biederman int rc = sock_create_kern(net, family, type, protocol, &sock); 16653d58b5faSDenis V. Lunev 16663d58b5faSDenis V. Lunev if (rc == 0) { 1667eee4fe4dSDenis V. Lunev *sk = sock->sk; 1668eee4fe4dSDenis V. Lunev (*sk)->sk_allocation = GFP_ATOMIC; 16691ac88557SEric Dumazet (*sk)->sk_use_task_frag = false; 16703d58b5faSDenis V. Lunev /* 16713d58b5faSDenis V. Lunev * Unhash it so that IP input processing does not even see it, 16723d58b5faSDenis V. Lunev * we do not wish this socket to see incoming packets. 16733d58b5faSDenis V. Lunev */ 1674eee4fe4dSDenis V. Lunev (*sk)->sk_prot->unhash(*sk); 16753d58b5faSDenis V. Lunev } 16763d58b5faSDenis V. Lunev return rc; 16773d58b5faSDenis V. Lunev } 16783d58b5faSDenis V. Lunev EXPORT_SYMBOL_GPL(inet_ctl_sock_create); 16793d58b5faSDenis V. Lunev 1680698365faSWANG Cong unsigned long snmp_fold_field(void __percpu *mib, int offt) 16815e0f0435SHerbert Xu { 16825e0f0435SHerbert Xu unsigned long res = 0; 1683698365faSWANG Cong int i; 16845e0f0435SHerbert Xu 1685698365faSWANG Cong for_each_possible_cpu(i) 1686c4c6bc31SRaghavendra K T res += snmp_get_cpu_field(mib, i, offt); 16875e0f0435SHerbert Xu return res; 16885e0f0435SHerbert Xu } 16895e0f0435SHerbert Xu EXPORT_SYMBOL_GPL(snmp_fold_field); 16905e0f0435SHerbert Xu 16914ce3c183SEric Dumazet #if BITS_PER_LONG==32 16924ce3c183SEric Dumazet 169380ec1927SDavid S. Miller u64 snmp_get_cpu_field64(void __percpu *mib, int cpu, int offt, 1694c4c6bc31SRaghavendra K T size_t syncp_offset) 16954ce3c183SEric Dumazet { 16968f0ea0feSEric Dumazet void *bhptr; 16974ce3c183SEric Dumazet struct u64_stats_sync *syncp; 16988f0ea0feSEric Dumazet u64 v; 16994ce3c183SEric Dumazet unsigned int start; 17004ce3c183SEric Dumazet 1701698365faSWANG Cong bhptr = per_cpu_ptr(mib, cpu); 17024ce3c183SEric Dumazet syncp = (struct u64_stats_sync *)(bhptr + syncp_offset); 17034ce3c183SEric Dumazet do { 1704d120d1a6SThomas Gleixner start = u64_stats_fetch_begin(syncp); 17058f0ea0feSEric Dumazet v = *(((u64 *)bhptr) + offt); 1706d120d1a6SThomas Gleixner } while (u64_stats_fetch_retry(syncp, start)); 17074ce3c183SEric Dumazet 1708c4c6bc31SRaghavendra K T return v; 1709c4c6bc31SRaghavendra K T } 1710c4c6bc31SRaghavendra K T EXPORT_SYMBOL_GPL(snmp_get_cpu_field64); 1711c4c6bc31SRaghavendra K T 1712c4c6bc31SRaghavendra K T u64 snmp_fold_field64(void __percpu *mib, int offt, size_t syncp_offset) 1713c4c6bc31SRaghavendra K T { 1714c4c6bc31SRaghavendra K T u64 res = 0; 1715c4c6bc31SRaghavendra K T int cpu; 1716c4c6bc31SRaghavendra K T 1717c4c6bc31SRaghavendra K T for_each_possible_cpu(cpu) { 1718d1bfc625SMadalin Bucur res += snmp_get_cpu_field64(mib, cpu, offt, syncp_offset); 17194ce3c183SEric Dumazet } 17204ce3c183SEric Dumazet return res; 17214ce3c183SEric Dumazet } 17224ce3c183SEric Dumazet EXPORT_SYMBOL_GPL(snmp_fold_field64); 17234ce3c183SEric Dumazet #endif 17244ce3c183SEric Dumazet 17251da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 172632613090SAlexey Dobriyan static const struct net_protocol igmp_protocol = { 17271da177e4SLinus Torvalds .handler = igmp_rcv, 17281da177e4SLinus Torvalds }; 17291da177e4SLinus Torvalds #endif 17301da177e4SLinus Torvalds 173111052589SKuniyuki Iwashima static const struct net_protocol tcp_protocol = { 17321da177e4SLinus Torvalds .handler = tcp_v4_rcv, 17331da177e4SLinus Torvalds .err_handler = tcp_v4_err, 17341da177e4SLinus Torvalds .no_policy = 1, 17358ed1dc44SHannes Frederic Sowa .icmp_strict_tag_validation = 1, 17361da177e4SLinus Torvalds }; 17371da177e4SLinus Torvalds 173811052589SKuniyuki Iwashima static const struct net_protocol udp_protocol = { 17391da177e4SLinus Torvalds .handler = udp_rcv, 17401da177e4SLinus Torvalds .err_handler = udp_err, 17411da177e4SLinus Torvalds .no_policy = 1, 17421da177e4SLinus Torvalds }; 17431da177e4SLinus Torvalds 174432613090SAlexey Dobriyan static const struct net_protocol icmp_protocol = { 17451da177e4SLinus Torvalds .handler = icmp_rcv, 17465b052042SLi Wei .err_handler = icmp_err, 17478b7817f3SHerbert Xu .no_policy = 1, 17481da177e4SLinus Torvalds }; 17491da177e4SLinus Torvalds 17509b4661bdSPavel Emelyanov static __net_init int ipv4_mib_init_net(struct net *net) 17519b4661bdSPavel Emelyanov { 1752827da44cSJohn Stultz int i; 1753827da44cSJohn Stultz 1754698365faSWANG Cong net->mib.tcp_statistics = alloc_percpu(struct tcp_mib); 1755698365faSWANG Cong if (!net->mib.tcp_statistics) 175657ef42d5SPavel Emelyanov goto err_tcp_mib; 1757698365faSWANG Cong net->mib.ip_statistics = alloc_percpu(struct ipstats_mib); 1758698365faSWANG Cong if (!net->mib.ip_statistics) 1759a20f5799SPavel Emelyanov goto err_ip_mib; 1760827da44cSJohn Stultz 1761827da44cSJohn Stultz for_each_possible_cpu(i) { 1762827da44cSJohn Stultz struct ipstats_mib *af_inet_stats; 1763698365faSWANG Cong af_inet_stats = per_cpu_ptr(net->mib.ip_statistics, i); 1764827da44cSJohn Stultz u64_stats_init(&af_inet_stats->syncp); 1765827da44cSJohn Stultz } 1766827da44cSJohn Stultz 1767698365faSWANG Cong net->mib.net_statistics = alloc_percpu(struct linux_mib); 1768698365faSWANG Cong if (!net->mib.net_statistics) 176961a7e260SPavel Emelyanov goto err_net_mib; 1770698365faSWANG Cong net->mib.udp_statistics = alloc_percpu(struct udp_mib); 1771698365faSWANG Cong if (!net->mib.udp_statistics) 17722f275f91SPavel Emelyanov goto err_udp_mib; 1773698365faSWANG Cong net->mib.udplite_statistics = alloc_percpu(struct udp_mib); 1774698365faSWANG Cong if (!net->mib.udplite_statistics) 1775386019d3SPavel Emelyanov goto err_udplite_mib; 1776698365faSWANG Cong net->mib.icmp_statistics = alloc_percpu(struct icmp_mib); 1777698365faSWANG Cong if (!net->mib.icmp_statistics) 1778b60538a0SPavel Emelyanov goto err_icmp_mib; 1779acb32ba3SEric Dumazet net->mib.icmpmsg_statistics = kzalloc(sizeof(struct icmpmsg_mib), 1780acb32ba3SEric Dumazet GFP_KERNEL); 1781acb32ba3SEric Dumazet if (!net->mib.icmpmsg_statistics) 1782923c6586SPavel Emelyanov goto err_icmpmsg_mib; 178357ef42d5SPavel Emelyanov 178457ef42d5SPavel Emelyanov tcp_mib_init(net); 17859b4661bdSPavel Emelyanov return 0; 178657ef42d5SPavel Emelyanov 1787923c6586SPavel Emelyanov err_icmpmsg_mib: 1788698365faSWANG Cong free_percpu(net->mib.icmp_statistics); 1789b60538a0SPavel Emelyanov err_icmp_mib: 1790698365faSWANG Cong free_percpu(net->mib.udplite_statistics); 1791386019d3SPavel Emelyanov err_udplite_mib: 1792698365faSWANG Cong free_percpu(net->mib.udp_statistics); 17932f275f91SPavel Emelyanov err_udp_mib: 1794698365faSWANG Cong free_percpu(net->mib.net_statistics); 179561a7e260SPavel Emelyanov err_net_mib: 1796698365faSWANG Cong free_percpu(net->mib.ip_statistics); 1797a20f5799SPavel Emelyanov err_ip_mib: 1798698365faSWANG Cong free_percpu(net->mib.tcp_statistics); 179957ef42d5SPavel Emelyanov err_tcp_mib: 180057ef42d5SPavel Emelyanov return -ENOMEM; 18019b4661bdSPavel Emelyanov } 18029b4661bdSPavel Emelyanov 18039b4661bdSPavel Emelyanov static __net_exit void ipv4_mib_exit_net(struct net *net) 18049b4661bdSPavel Emelyanov { 1805acb32ba3SEric Dumazet kfree(net->mib.icmpmsg_statistics); 1806698365faSWANG Cong free_percpu(net->mib.icmp_statistics); 1807698365faSWANG Cong free_percpu(net->mib.udplite_statistics); 1808698365faSWANG Cong free_percpu(net->mib.udp_statistics); 1809698365faSWANG Cong free_percpu(net->mib.net_statistics); 1810698365faSWANG Cong free_percpu(net->mib.ip_statistics); 1811698365faSWANG Cong free_percpu(net->mib.tcp_statistics); 1812fc518953SFlorian Westphal #ifdef CONFIG_MPTCP 1813fc518953SFlorian Westphal /* allocated on demand, see mptcp_init_sock() */ 1814fc518953SFlorian Westphal free_percpu(net->mib.mptcp_statistics); 1815fc518953SFlorian Westphal #endif 18169b4661bdSPavel Emelyanov } 18179b4661bdSPavel Emelyanov 18189b4661bdSPavel Emelyanov static __net_initdata struct pernet_operations ipv4_mib_ops = { 18199b4661bdSPavel Emelyanov .init = ipv4_mib_init_net, 18209b4661bdSPavel Emelyanov .exit = ipv4_mib_exit_net, 18219b4661bdSPavel Emelyanov }; 18229b4661bdSPavel Emelyanov 18231da177e4SLinus Torvalds static int __init init_ipv4_mibs(void) 18241da177e4SLinus Torvalds { 1825d89cbbb1SPavel Emelyanov return register_pernet_subsys(&ipv4_mib_ops); 18261da177e4SLinus Torvalds } 18271da177e4SLinus Torvalds 1828c9d8f1a6SCong Wang static __net_init int inet_init_net(struct net *net) 1829c9d8f1a6SCong Wang { 1830c9d8f1a6SCong Wang /* 1831c9d8f1a6SCong Wang * Set defaults for local port range 1832c9d8f1a6SCong Wang */ 1833c9d8f1a6SCong Wang seqlock_init(&net->ipv4.ip_local_ports.lock); 1834c9d8f1a6SCong Wang net->ipv4.ip_local_ports.range[0] = 32768; 183507f4c900SEric Dumazet net->ipv4.ip_local_ports.range[1] = 60999; 1836ba6b918aSCong Wang 1837ba6b918aSCong Wang seqlock_init(&net->ipv4.ping_group_range.lock); 1838ba6b918aSCong Wang /* 1839ba6b918aSCong Wang * Sane defaults - nobody may create ping sockets. 1840ba6b918aSCong Wang * Boot scripts should set this to distro-specific group. 1841ba6b918aSCong Wang */ 1842ba6b918aSCong Wang net->ipv4.ping_group_range.range[0] = make_kgid(&init_user_ns, 1); 1843ba6b918aSCong Wang net->ipv4.ping_group_range.range[1] = make_kgid(&init_user_ns, 0); 1844049bbf58SEzequiel Garcia 1845049bbf58SEzequiel Garcia /* Default values for sysctl-controlled parameters. 1846049bbf58SEzequiel Garcia * We set them here, in case sysctl is not compiled. 1847049bbf58SEzequiel Garcia */ 1848049bbf58SEzequiel Garcia net->ipv4.sysctl_ip_default_ttl = IPDEFTTL; 1849432e05d3SPetr Machata net->ipv4.sysctl_ip_fwd_update_priority = 1; 1850049bbf58SEzequiel Garcia net->ipv4.sysctl_ip_dynaddr = 0; 1851049bbf58SEzequiel Garcia net->ipv4.sysctl_ip_early_demux = 1; 1852dddb64bcSsubashab@codeaurora.org net->ipv4.sysctl_udp_early_demux = 1; 1853dddb64bcSsubashab@codeaurora.org net->ipv4.sysctl_tcp_early_demux = 1; 18544f80116dSRoopa Prabhu net->ipv4.sysctl_nexthop_compat_mode = 1; 18554548b683SKrister Johansen #ifdef CONFIG_SYSCTL 18564548b683SKrister Johansen net->ipv4.sysctl_ip_prot_sock = PROT_SOCK; 18574548b683SKrister Johansen #endif 1858049bbf58SEzequiel Garcia 18591714020eSNikolay Borisov /* Some igmp sysctl, whose values are always used */ 18601714020eSNikolay Borisov net->ipv4.sysctl_igmp_max_memberships = 20; 18611714020eSNikolay Borisov net->ipv4.sysctl_igmp_max_msf = 10; 18621714020eSNikolay Borisov /* IGMP reports for link-local multicast groups are enabled by default */ 18631714020eSNikolay Borisov net->ipv4.sysctl_igmp_llm_reports = 1; 18641714020eSNikolay Borisov net->ipv4.sysctl_igmp_qrv = 2; 18651714020eSNikolay Borisov 1866680aea08SAmit Cohen net->ipv4.sysctl_fib_notify_on_flag_change = 0; 1867680aea08SAmit Cohen 1868c9d8f1a6SCong Wang return 0; 1869c9d8f1a6SCong Wang } 1870c9d8f1a6SCong Wang 1871c9d8f1a6SCong Wang static __net_initdata struct pernet_operations af_inet_ops = { 1872c9d8f1a6SCong Wang .init = inet_init_net, 1873c9d8f1a6SCong Wang }; 1874c9d8f1a6SCong Wang 1875c9d8f1a6SCong Wang static int __init init_inet_pernet_ops(void) 1876c9d8f1a6SCong Wang { 1877c9d8f1a6SCong Wang return register_pernet_subsys(&af_inet_ops); 1878c9d8f1a6SCong Wang } 1879c9d8f1a6SCong Wang 18801da177e4SLinus Torvalds static int ipv4_proc_init(void); 18811da177e4SLinus Torvalds 188230e224d7SHerbert Xu /* 188330e224d7SHerbert Xu * IP protocol layer initialiser 188430e224d7SHerbert Xu */ 188530e224d7SHerbert Xu 188622061d80SVlad Yasevich static struct packet_offload ip_packet_offload __read_mostly = { 188722061d80SVlad Yasevich .type = cpu_to_be16(ETH_P_IP), 1888f191a1d1SVlad Yasevich .callbacks = { 1889f4c50d99SHerbert Xu .gso_segment = inet_gso_segment, 189073cc19f1SHerbert Xu .gro_receive = inet_gro_receive, 189173cc19f1SHerbert Xu .gro_complete = inet_gro_complete, 1892f191a1d1SVlad Yasevich }, 189330e224d7SHerbert Xu }; 189430e224d7SHerbert Xu 1895cb32f511SEric Dumazet static const struct net_offload ipip_offload = { 1896cb32f511SEric Dumazet .callbacks = { 1897418e897eSWillem de Bruijn .gso_segment = ipip_gso_segment, 1898fac8e0f5SJesse Gross .gro_receive = ipip_gro_receive, 1899b8cba75bSJesse Gross .gro_complete = ipip_gro_complete, 1900cb32f511SEric Dumazet }, 1901cb32f511SEric Dumazet }; 1902cb32f511SEric Dumazet 190393b1b31fSTonghao Zhang static int __init ipip_offload_init(void) 190493b1b31fSTonghao Zhang { 190593b1b31fSTonghao Zhang return inet_add_offload(&ipip_offload, IPPROTO_IPIP); 190693b1b31fSTonghao Zhang } 190793b1b31fSTonghao Zhang 1908808a8f88SVlad Yasevich static int __init ipv4_offload_init(void) 1909808a8f88SVlad Yasevich { 1910808a8f88SVlad Yasevich /* 1911808a8f88SVlad Yasevich * Add offloads 1912808a8f88SVlad Yasevich */ 1913da5bab07SDaniel Borkmann if (udpv4_offload_init() < 0) 1914808a8f88SVlad Yasevich pr_crit("%s: Cannot add UDP protocol offload\n", __func__); 191528850dc7SDaniel Borkmann if (tcpv4_offload_init() < 0) 191628850dc7SDaniel Borkmann pr_crit("%s: Cannot add TCP protocol offload\n", __func__); 191793b1b31fSTonghao Zhang if (ipip_offload_init() < 0) 191893b1b31fSTonghao Zhang pr_crit("%s: Cannot add IPIP protocol offload\n", __func__); 1919808a8f88SVlad Yasevich 1920808a8f88SVlad Yasevich dev_add_offload(&ip_packet_offload); 1921808a8f88SVlad Yasevich return 0; 1922808a8f88SVlad Yasevich } 1923808a8f88SVlad Yasevich 1924808a8f88SVlad Yasevich fs_initcall(ipv4_offload_init); 1925808a8f88SVlad Yasevich 1926808a8f88SVlad Yasevich static struct packet_type ip_packet_type __read_mostly = { 1927808a8f88SVlad Yasevich .type = cpu_to_be16(ETH_P_IP), 1928808a8f88SVlad Yasevich .func = ip_rcv, 192917266ee9SEdward Cree .list_func = ip_list_rcv, 1930808a8f88SVlad Yasevich }; 1931808a8f88SVlad Yasevich 19321da177e4SLinus Torvalds static int __init inet_init(void) 19331da177e4SLinus Torvalds { 19341da177e4SLinus Torvalds struct inet_protosw *q; 19351da177e4SLinus Torvalds struct list_head *r; 1936b6246f4dSColin Ian King int rc; 19371da177e4SLinus Torvalds 1938b4772ef8SEyal Birger sock_skb_cb_check_size(sizeof(struct inet_skb_parm)); 19391da177e4SLinus Torvalds 19400daf07e5SEric Dumazet raw_hashinfo_init(&raw_v4_hashinfo); 19410daf07e5SEric Dumazet 19421da177e4SLinus Torvalds rc = proto_register(&tcp_prot, 1); 19431da177e4SLinus Torvalds if (rc) 1944122ff243SWANG Cong goto out; 19451da177e4SLinus Torvalds 19461da177e4SLinus Torvalds rc = proto_register(&udp_prot, 1); 19471da177e4SLinus Torvalds if (rc) 19481da177e4SLinus Torvalds goto out_unregister_tcp_proto; 19491da177e4SLinus Torvalds 19501da177e4SLinus Torvalds rc = proto_register(&raw_prot, 1); 19511da177e4SLinus Torvalds if (rc) 19521da177e4SLinus Torvalds goto out_unregister_udp_proto; 19531da177e4SLinus Torvalds 1954c319b4d7SVasiliy Kulikov rc = proto_register(&ping_prot, 1); 1955c319b4d7SVasiliy Kulikov if (rc) 1956c319b4d7SVasiliy Kulikov goto out_unregister_raw_proto; 1957c319b4d7SVasiliy Kulikov 19581da177e4SLinus Torvalds /* 19591da177e4SLinus Torvalds * Tell SOCKET that we are alive... 19601da177e4SLinus Torvalds */ 19611da177e4SLinus Torvalds 19621da177e4SLinus Torvalds (void)sock_register(&inet_family_ops); 19631da177e4SLinus Torvalds 1964bd7b1533SAl Viro #ifdef CONFIG_SYSCTL 1965bd7b1533SAl Viro ip_static_sysctl_init(); 1966bd7b1533SAl Viro #endif 1967bd7b1533SAl Viro 19681da177e4SLinus Torvalds /* 19691da177e4SLinus Torvalds * Add all the base protocols. 19701da177e4SLinus Torvalds */ 19711da177e4SLinus Torvalds 19721da177e4SLinus Torvalds if (inet_add_protocol(&icmp_protocol, IPPROTO_ICMP) < 0) 1973058bd4d2SJoe Perches pr_crit("%s: Cannot add ICMP protocol\n", __func__); 19741da177e4SLinus Torvalds if (inet_add_protocol(&udp_protocol, IPPROTO_UDP) < 0) 1975058bd4d2SJoe Perches pr_crit("%s: Cannot add UDP protocol\n", __func__); 19761da177e4SLinus Torvalds if (inet_add_protocol(&tcp_protocol, IPPROTO_TCP) < 0) 1977058bd4d2SJoe Perches pr_crit("%s: Cannot add TCP protocol\n", __func__); 19781da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 19791da177e4SLinus Torvalds if (inet_add_protocol(&igmp_protocol, IPPROTO_IGMP) < 0) 1980058bd4d2SJoe Perches pr_crit("%s: Cannot add IGMP protocol\n", __func__); 19811da177e4SLinus Torvalds #endif 19821da177e4SLinus Torvalds 19831da177e4SLinus Torvalds /* Register the socket-side information for inet_create. */ 19841da177e4SLinus Torvalds for (r = &inetsw[0]; r < &inetsw[SOCK_MAX]; ++r) 19851da177e4SLinus Torvalds INIT_LIST_HEAD(r); 19861da177e4SLinus Torvalds 19871da177e4SLinus Torvalds for (q = inetsw_array; q < &inetsw_array[INETSW_ARRAY_LEN]; ++q) 19881da177e4SLinus Torvalds inet_register_protosw(q); 19891da177e4SLinus Torvalds 19901da177e4SLinus Torvalds /* 19911da177e4SLinus Torvalds * Set the ARP module up 19921da177e4SLinus Torvalds */ 19931da177e4SLinus Torvalds 19941da177e4SLinus Torvalds arp_init(); 19951da177e4SLinus Torvalds 19961da177e4SLinus Torvalds /* 19971da177e4SLinus Torvalds * Set the IP module up 19981da177e4SLinus Torvalds */ 19991da177e4SLinus Torvalds 20001da177e4SLinus Torvalds ip_init(); 20011da177e4SLinus Torvalds 2002e22e45fcSMuchun Song /* Initialise per-cpu ipv4 mibs */ 2003e22e45fcSMuchun Song if (init_ipv4_mibs()) 2004e22e45fcSMuchun Song panic("%s: Cannot init ipv4 mibs\n", __func__); 2005e22e45fcSMuchun Song 20061da177e4SLinus Torvalds /* Setup TCP slab cache for open requests. */ 20071da177e4SLinus Torvalds tcp_init(); 20081da177e4SLinus Torvalds 200995766fffSHideo Aoki /* Setup UDP memory threshold */ 201095766fffSHideo Aoki udp_init(); 201195766fffSHideo Aoki 2012ba4e58ecSGerrit Renker /* Add UDP-Lite (RFC 3828) */ 2013ba4e58ecSGerrit Renker udplite4_register(); 20141da177e4SLinus Torvalds 20156897445fSMike Manning raw_init(); 20166897445fSMike Manning 2017c319b4d7SVasiliy Kulikov ping_init(); 2018c319b4d7SVasiliy Kulikov 20191da177e4SLinus Torvalds /* 20201da177e4SLinus Torvalds * Set the ICMP layer up 20211da177e4SLinus Torvalds */ 20221da177e4SLinus Torvalds 2023a5710d65SDenis V. Lunev if (icmp_init() < 0) 2024a5710d65SDenis V. Lunev panic("Failed to create the ICMP control socket.\n"); 20251da177e4SLinus Torvalds 20261da177e4SLinus Torvalds /* 20271da177e4SLinus Torvalds * Initialise the multicast router 20281da177e4SLinus Torvalds */ 20291da177e4SLinus Torvalds #if defined(CONFIG_IP_MROUTE) 203003d2f897SWang Chen if (ip_mr_init()) 2031058bd4d2SJoe Perches pr_crit("%s: Cannot init ipv4 mroute\n", __func__); 20321da177e4SLinus Torvalds #endif 2033c9d8f1a6SCong Wang 2034c9d8f1a6SCong Wang if (init_inet_pernet_ops()) 2035c9d8f1a6SCong Wang pr_crit("%s: Cannot init ipv4 inet pernet ops\n", __func__); 20361da177e4SLinus Torvalds 20371da177e4SLinus Torvalds ipv4_proc_init(); 20381da177e4SLinus Torvalds 20391da177e4SLinus Torvalds ipfrag_init(); 20401da177e4SLinus Torvalds 204130e224d7SHerbert Xu dev_add_pack(&ip_packet_type); 204230e224d7SHerbert Xu 2043045a0fa0SThomas Graf ip_tunnel_core_init(); 2044045a0fa0SThomas Graf 20451da177e4SLinus Torvalds rc = 0; 20461da177e4SLinus Torvalds out: 20471da177e4SLinus Torvalds return rc; 2048c319b4d7SVasiliy Kulikov out_unregister_raw_proto: 2049c319b4d7SVasiliy Kulikov proto_unregister(&raw_prot); 20501da177e4SLinus Torvalds out_unregister_udp_proto: 20511da177e4SLinus Torvalds proto_unregister(&udp_prot); 2052321efff7SOlaf Kirch out_unregister_tcp_proto: 2053321efff7SOlaf Kirch proto_unregister(&tcp_prot); 20541da177e4SLinus Torvalds goto out; 20551da177e4SLinus Torvalds } 20561da177e4SLinus Torvalds 2057a536e077SHeiko Carstens fs_initcall(inet_init); 20581da177e4SLinus Torvalds 20591da177e4SLinus Torvalds /* ------------------------------------------------------------------------ */ 20601da177e4SLinus Torvalds 20611da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 20621da177e4SLinus Torvalds static int __init ipv4_proc_init(void) 20631da177e4SLinus Torvalds { 20641da177e4SLinus Torvalds int rc = 0; 20651da177e4SLinus Torvalds 20661da177e4SLinus Torvalds if (raw_proc_init()) 20671da177e4SLinus Torvalds goto out_raw; 20681da177e4SLinus Torvalds if (tcp4_proc_init()) 20691da177e4SLinus Torvalds goto out_tcp; 20701da177e4SLinus Torvalds if (udp4_proc_init()) 20711da177e4SLinus Torvalds goto out_udp; 2072c319b4d7SVasiliy Kulikov if (ping_proc_init()) 2073c319b4d7SVasiliy Kulikov goto out_ping; 20741da177e4SLinus Torvalds if (ip_misc_proc_init()) 20751da177e4SLinus Torvalds goto out_misc; 20761da177e4SLinus Torvalds out: 20771da177e4SLinus Torvalds return rc; 20781da177e4SLinus Torvalds out_misc: 2079c319b4d7SVasiliy Kulikov ping_proc_exit(); 2080c319b4d7SVasiliy Kulikov out_ping: 20811da177e4SLinus Torvalds udp4_proc_exit(); 20821da177e4SLinus Torvalds out_udp: 20831da177e4SLinus Torvalds tcp4_proc_exit(); 20841da177e4SLinus Torvalds out_tcp: 20851da177e4SLinus Torvalds raw_proc_exit(); 20861da177e4SLinus Torvalds out_raw: 20871da177e4SLinus Torvalds rc = -ENOMEM; 20881da177e4SLinus Torvalds goto out; 20891da177e4SLinus Torvalds } 20901da177e4SLinus Torvalds 20911da177e4SLinus Torvalds #else /* CONFIG_PROC_FS */ 20921da177e4SLinus Torvalds static int __init ipv4_proc_init(void) 20931da177e4SLinus Torvalds { 20941da177e4SLinus Torvalds return 0; 20951da177e4SLinus Torvalds } 20961da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 2097