11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * INET An implementation of the TCP/IP protocol suite for the LINUX 31da177e4SLinus Torvalds * operating system. INET is implemented using the BSD Socket 41da177e4SLinus Torvalds * interface as the means of communication with the user level. 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * PF_INET protocol family socket handler. 71da177e4SLinus Torvalds * 802c30a84SJesper Juhl * Authors: Ross Biro 91da177e4SLinus Torvalds * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG> 101da177e4SLinus Torvalds * Florian La Roche, <flla@stud.uni-sb.de> 111da177e4SLinus Torvalds * Alan Cox, <A.Cox@swansea.ac.uk> 121da177e4SLinus Torvalds * 131da177e4SLinus Torvalds * Changes (see also sock.c) 141da177e4SLinus Torvalds * 151da177e4SLinus Torvalds * piggy, 161da177e4SLinus Torvalds * Karl Knutson : Socket protocol table 171da177e4SLinus Torvalds * A.N.Kuznetsov : Socket death error in accept(). 181da177e4SLinus Torvalds * John Richardson : Fix non blocking error in connect() 191da177e4SLinus Torvalds * so sockets that fail to connect 201da177e4SLinus Torvalds * don't return -EINPROGRESS. 211da177e4SLinus Torvalds * Alan Cox : Asynchronous I/O support 221da177e4SLinus Torvalds * Alan Cox : Keep correct socket pointer on sock 231da177e4SLinus Torvalds * structures 241da177e4SLinus Torvalds * when accept() ed 251da177e4SLinus Torvalds * Alan Cox : Semantics of SO_LINGER aren't state 261da177e4SLinus Torvalds * moved to close when you look carefully. 271da177e4SLinus Torvalds * With this fixed and the accept bug fixed 281da177e4SLinus Torvalds * some RPC stuff seems happier. 291da177e4SLinus Torvalds * Niibe Yutaka : 4.4BSD style write async I/O 301da177e4SLinus Torvalds * Alan Cox, 311da177e4SLinus Torvalds * Tony Gale : Fixed reuse semantics. 321da177e4SLinus Torvalds * Alan Cox : bind() shouldn't abort existing but dead 331da177e4SLinus Torvalds * sockets. Stops FTP netin:.. I hope. 341da177e4SLinus Torvalds * Alan Cox : bind() works correctly for RAW sockets. 351da177e4SLinus Torvalds * Note that FreeBSD at least was broken 361da177e4SLinus Torvalds * in this respect so be careful with 371da177e4SLinus Torvalds * compatibility tests... 381da177e4SLinus Torvalds * Alan Cox : routing cache support 391da177e4SLinus Torvalds * Alan Cox : memzero the socket structure for 401da177e4SLinus Torvalds * compactness. 411da177e4SLinus Torvalds * Matt Day : nonblock connect error handler 421da177e4SLinus Torvalds * Alan Cox : Allow large numbers of pending sockets 431da177e4SLinus Torvalds * (eg for big web sites), but only if 441da177e4SLinus Torvalds * specifically application requested. 451da177e4SLinus Torvalds * Alan Cox : New buffering throughout IP. Used 461da177e4SLinus Torvalds * dumbly. 471da177e4SLinus Torvalds * Alan Cox : New buffering now used smartly. 481da177e4SLinus Torvalds * Alan Cox : BSD rather than common sense 491da177e4SLinus Torvalds * interpretation of listen. 501da177e4SLinus Torvalds * Germano Caronni : Assorted small races. 511da177e4SLinus Torvalds * Alan Cox : sendmsg/recvmsg basic support. 521da177e4SLinus Torvalds * Alan Cox : Only sendmsg/recvmsg now supported. 531da177e4SLinus Torvalds * Alan Cox : Locked down bind (see security list). 541da177e4SLinus Torvalds * Alan Cox : Loosened bind a little. 551da177e4SLinus Torvalds * Mike McLagan : ADD/DEL DLCI Ioctls 561da177e4SLinus Torvalds * Willy Konynenberg : Transparent proxying support. 571da177e4SLinus Torvalds * David S. Miller : New socket lookup architecture. 581da177e4SLinus Torvalds * Some other random speedups. 591da177e4SLinus Torvalds * Cyrus Durgin : Cleaned up file for kmod hacks. 601da177e4SLinus Torvalds * Andi Kleen : Fix inet_stream_connect TCP race. 611da177e4SLinus Torvalds * 621da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 631da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License 641da177e4SLinus Torvalds * as published by the Free Software Foundation; either version 651da177e4SLinus Torvalds * 2 of the License, or (at your option) any later version. 661da177e4SLinus Torvalds */ 671da177e4SLinus Torvalds 68afd46503SJoe Perches #define pr_fmt(fmt) "IPv4: " fmt 69afd46503SJoe Perches 70f4c50d99SHerbert Xu #include <linux/err.h> 711da177e4SLinus Torvalds #include <linux/errno.h> 721da177e4SLinus Torvalds #include <linux/types.h> 731da177e4SLinus Torvalds #include <linux/socket.h> 741da177e4SLinus Torvalds #include <linux/in.h> 751da177e4SLinus Torvalds #include <linux/kernel.h> 761da177e4SLinus Torvalds #include <linux/module.h> 771da177e4SLinus Torvalds #include <linux/sched.h> 781da177e4SLinus Torvalds #include <linux/timer.h> 791da177e4SLinus Torvalds #include <linux/string.h> 801da177e4SLinus Torvalds #include <linux/sockios.h> 811da177e4SLinus Torvalds #include <linux/net.h> 824fc268d2SRandy Dunlap #include <linux/capability.h> 831da177e4SLinus Torvalds #include <linux/fcntl.h> 841da177e4SLinus Torvalds #include <linux/mm.h> 851da177e4SLinus Torvalds #include <linux/interrupt.h> 861da177e4SLinus Torvalds #include <linux/stat.h> 871da177e4SLinus Torvalds #include <linux/init.h> 881da177e4SLinus Torvalds #include <linux/poll.h> 891da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h> 90b3da2cf3SDavid S. Miller #include <linux/random.h> 915a0e3ad6STejun Heo #include <linux/slab.h> 921da177e4SLinus Torvalds 931da177e4SLinus Torvalds #include <asm/uaccess.h> 941da177e4SLinus Torvalds 951da177e4SLinus Torvalds #include <linux/inet.h> 961da177e4SLinus Torvalds #include <linux/igmp.h> 9714c85021SArnaldo Carvalho de Melo #include <linux/inetdevice.h> 981da177e4SLinus Torvalds #include <linux/netdevice.h> 9973cc19f1SHerbert Xu #include <net/checksum.h> 1001da177e4SLinus Torvalds #include <net/ip.h> 1011da177e4SLinus Torvalds #include <net/protocol.h> 1021da177e4SLinus Torvalds #include <net/arp.h> 1031da177e4SLinus Torvalds #include <net/route.h> 1041da177e4SLinus Torvalds #include <net/ip_fib.h> 105295f7324SArnaldo Carvalho de Melo #include <net/inet_connection_sock.h> 1061da177e4SLinus Torvalds #include <net/tcp.h> 1071da177e4SLinus Torvalds #include <net/udp.h> 108ba4e58ecSGerrit Renker #include <net/udplite.h> 109c319b4d7SVasiliy Kulikov #include <net/ping.h> 1101da177e4SLinus Torvalds #include <linux/skbuff.h> 1111da177e4SLinus Torvalds #include <net/sock.h> 1121da177e4SLinus Torvalds #include <net/raw.h> 1131da177e4SLinus Torvalds #include <net/icmp.h> 1141da177e4SLinus Torvalds #include <net/inet_common.h> 115045a0fa0SThomas Graf #include <net/ip_tunnels.h> 1161da177e4SLinus Torvalds #include <net/xfrm.h> 1179b4661bdSPavel Emelyanov #include <net/net_namespace.h> 118aebda156SEric Dumazet #include <net/secure_seq.h> 1191da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE 1201da177e4SLinus Torvalds #include <linux/mroute.h> 1211da177e4SLinus Torvalds #endif 1223236b004SDavid Ahern #include <net/l3mdev.h> 1231da177e4SLinus Torvalds 1241da177e4SLinus Torvalds 1251da177e4SLinus Torvalds /* The inetsw table contains everything that inet_create needs to 1261da177e4SLinus Torvalds * build a new socket. 1271da177e4SLinus Torvalds */ 1281da177e4SLinus Torvalds static struct list_head inetsw[SOCK_MAX]; 1291da177e4SLinus Torvalds static DEFINE_SPINLOCK(inetsw_lock); 1301da177e4SLinus Torvalds 1311da177e4SLinus Torvalds /* New destruction routine */ 1321da177e4SLinus Torvalds 1331da177e4SLinus Torvalds void inet_sock_destruct(struct sock *sk) 1341da177e4SLinus Torvalds { 1351da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 1361da177e4SLinus Torvalds 1371da177e4SLinus Torvalds __skb_queue_purge(&sk->sk_receive_queue); 1381da177e4SLinus Torvalds __skb_queue_purge(&sk->sk_error_queue); 1391da177e4SLinus Torvalds 14095766fffSHideo Aoki sk_mem_reclaim(sk); 14195766fffSHideo Aoki 1421da177e4SLinus Torvalds if (sk->sk_type == SOCK_STREAM && sk->sk_state != TCP_CLOSE) { 1433d1427f8SEric Dumazet pr_err("Attempt to release TCP socket in state %d %p\n", 1441da177e4SLinus Torvalds sk->sk_state, sk); 1451da177e4SLinus Torvalds return; 1461da177e4SLinus Torvalds } 1471da177e4SLinus Torvalds if (!sock_flag(sk, SOCK_DEAD)) { 1483d1427f8SEric Dumazet pr_err("Attempt to release alive inet socket %p\n", sk); 1491da177e4SLinus Torvalds return; 1501da177e4SLinus Torvalds } 1511da177e4SLinus Torvalds 152547b792cSIlpo Järvinen WARN_ON(atomic_read(&sk->sk_rmem_alloc)); 153547b792cSIlpo Järvinen WARN_ON(atomic_read(&sk->sk_wmem_alloc)); 154547b792cSIlpo Järvinen WARN_ON(sk->sk_wmem_queued); 155547b792cSIlpo Järvinen WARN_ON(sk->sk_forward_alloc); 1561da177e4SLinus Torvalds 157f6d8bd05SEric Dumazet kfree(rcu_dereference_protected(inet->inet_opt, 1)); 158b6c6712aSEric Dumazet dst_release(rcu_dereference_check(sk->sk_dst_cache, 1)); 15941063e9dSDavid S. Miller dst_release(sk->sk_rx_dst); 160e6848976SArnaldo Carvalho de Melo sk_refcnt_debug_dec(sk); 1611da177e4SLinus Torvalds } 1623d1427f8SEric Dumazet EXPORT_SYMBOL(inet_sock_destruct); 1631da177e4SLinus Torvalds 1641da177e4SLinus Torvalds /* 1651da177e4SLinus Torvalds * The routines beyond this point handle the behaviour of an AF_INET 1661da177e4SLinus Torvalds * socket object. Mostly it punts to the subprotocols of IP to do 1671da177e4SLinus Torvalds * the work. 1681da177e4SLinus Torvalds */ 1691da177e4SLinus Torvalds 1701da177e4SLinus Torvalds /* 1711da177e4SLinus Torvalds * Automatically bind an unbound socket. 1721da177e4SLinus Torvalds */ 1731da177e4SLinus Torvalds 1741da177e4SLinus Torvalds static int inet_autobind(struct sock *sk) 1751da177e4SLinus Torvalds { 1761da177e4SLinus Torvalds struct inet_sock *inet; 1771da177e4SLinus Torvalds /* We may need to bind the socket. */ 1781da177e4SLinus Torvalds lock_sock(sk); 1791da177e4SLinus Torvalds inet = inet_sk(sk); 180c720c7e8SEric Dumazet if (!inet->inet_num) { 1811da177e4SLinus Torvalds if (sk->sk_prot->get_port(sk, 0)) { 1821da177e4SLinus Torvalds release_sock(sk); 1831da177e4SLinus Torvalds return -EAGAIN; 1841da177e4SLinus Torvalds } 185c720c7e8SEric Dumazet inet->inet_sport = htons(inet->inet_num); 1861da177e4SLinus Torvalds } 1871da177e4SLinus Torvalds release_sock(sk); 1881da177e4SLinus Torvalds return 0; 1891da177e4SLinus Torvalds } 1901da177e4SLinus Torvalds 1911da177e4SLinus Torvalds /* 1921da177e4SLinus Torvalds * Move a socket into listening state. 1931da177e4SLinus Torvalds */ 1941da177e4SLinus Torvalds int inet_listen(struct socket *sock, int backlog) 1951da177e4SLinus Torvalds { 1961da177e4SLinus Torvalds struct sock *sk = sock->sk; 1971da177e4SLinus Torvalds unsigned char old_state; 1981da177e4SLinus Torvalds int err; 1991da177e4SLinus Torvalds 2001da177e4SLinus Torvalds lock_sock(sk); 2011da177e4SLinus Torvalds 2021da177e4SLinus Torvalds err = -EINVAL; 2031da177e4SLinus Torvalds if (sock->state != SS_UNCONNECTED || sock->type != SOCK_STREAM) 2041da177e4SLinus Torvalds goto out; 2051da177e4SLinus Torvalds 2061da177e4SLinus Torvalds old_state = sk->sk_state; 2071da177e4SLinus Torvalds if (!((1 << old_state) & (TCPF_CLOSE | TCPF_LISTEN))) 2081da177e4SLinus Torvalds goto out; 2091da177e4SLinus Torvalds 2101da177e4SLinus Torvalds /* Really, if the socket is already in listen state 2111da177e4SLinus Torvalds * we can only allow the backlog to be adjusted. 2121da177e4SLinus Torvalds */ 2131da177e4SLinus Torvalds if (old_state != TCP_LISTEN) { 2148336886fSJerry Chu /* Check special setups for testing purpose to enable TFO w/o 2158336886fSJerry Chu * requiring TCP_FASTOPEN sockopt. 2168336886fSJerry Chu * Note that only TCP sockets (SOCK_STREAM) will reach here. 2178336886fSJerry Chu * Also fastopenq may already been allocated because this 2188336886fSJerry Chu * socket was in TCP_LISTEN state previously but was 2198336886fSJerry Chu * shutdown() (rather than close()). 2208336886fSJerry Chu */ 2218336886fSJerry Chu if ((sysctl_tcp_fastopen & TFO_SERVER_ENABLE) != 0 && 2220536fcc0SEric Dumazet !inet_csk(sk)->icsk_accept_queue.fastopenq.max_qlen) { 2238336886fSJerry Chu if ((sysctl_tcp_fastopen & TFO_SERVER_WO_SOCKOPT1) != 0) 2240536fcc0SEric Dumazet fastopen_queue_tune(sk, backlog); 2258336886fSJerry Chu else if ((sysctl_tcp_fastopen & 2268336886fSJerry Chu TFO_SERVER_WO_SOCKOPT2) != 0) 2270536fcc0SEric Dumazet fastopen_queue_tune(sk, 2288336886fSJerry Chu ((uint)sysctl_tcp_fastopen) >> 16); 229dfea2aa6SChristoph Paasch 230dfea2aa6SChristoph Paasch tcp_fastopen_init_key_once(true); 2318336886fSJerry Chu } 23272a3effaSEric Dumazet err = inet_csk_listen_start(sk, backlog); 2331da177e4SLinus Torvalds if (err) 2341da177e4SLinus Torvalds goto out; 2351da177e4SLinus Torvalds } 2361da177e4SLinus Torvalds sk->sk_max_ack_backlog = backlog; 2371da177e4SLinus Torvalds err = 0; 2381da177e4SLinus Torvalds 2391da177e4SLinus Torvalds out: 2401da177e4SLinus Torvalds release_sock(sk); 2411da177e4SLinus Torvalds return err; 2421da177e4SLinus Torvalds } 2433d1427f8SEric Dumazet EXPORT_SYMBOL(inet_listen); 2441da177e4SLinus Torvalds 2451da177e4SLinus Torvalds /* 2461da177e4SLinus Torvalds * Create an inet socket. 2471da177e4SLinus Torvalds */ 2481da177e4SLinus Torvalds 2493f378b68SEric Paris static int inet_create(struct net *net, struct socket *sock, int protocol, 2503f378b68SEric Paris int kern) 2511da177e4SLinus Torvalds { 2521da177e4SLinus Torvalds struct sock *sk; 2531da177e4SLinus Torvalds struct inet_protosw *answer; 2541da177e4SLinus Torvalds struct inet_sock *inet; 2551da177e4SLinus Torvalds struct proto *answer_prot; 2561da177e4SLinus Torvalds unsigned char answer_flags; 257bb97d31fSArnaldo Carvalho de Melo int try_loading_module = 0; 25886c8f9d1SHerbert Xu int err; 2591da177e4SLinus Torvalds 26079462ad0SHannes Frederic Sowa if (protocol < 0 || protocol >= IPPROTO_MAX) 26179462ad0SHannes Frederic Sowa return -EINVAL; 26279462ad0SHannes Frederic Sowa 2631da177e4SLinus Torvalds sock->state = SS_UNCONNECTED; 2641da177e4SLinus Torvalds 2651da177e4SLinus Torvalds /* Look for the requested type/protocol pair. */ 266bb97d31fSArnaldo Carvalho de Melo lookup_protocol: 26786c8f9d1SHerbert Xu err = -ESOCKTNOSUPPORT; 2681da177e4SLinus Torvalds rcu_read_lock(); 269696adfe8SPaul E. McKenney list_for_each_entry_rcu(answer, &inetsw[sock->type], list) { 2701da177e4SLinus Torvalds 271696adfe8SPaul E. McKenney err = 0; 2721da177e4SLinus Torvalds /* Check the non-wild match. */ 2731da177e4SLinus Torvalds if (protocol == answer->protocol) { 2741da177e4SLinus Torvalds if (protocol != IPPROTO_IP) 2751da177e4SLinus Torvalds break; 2761da177e4SLinus Torvalds } else { 2771da177e4SLinus Torvalds /* Check for the two wild cases. */ 2781da177e4SLinus Torvalds if (IPPROTO_IP == protocol) { 2791da177e4SLinus Torvalds protocol = answer->protocol; 2801da177e4SLinus Torvalds break; 2811da177e4SLinus Torvalds } 2821da177e4SLinus Torvalds if (IPPROTO_IP == answer->protocol) 2831da177e4SLinus Torvalds break; 2841da177e4SLinus Torvalds } 28586c8f9d1SHerbert Xu err = -EPROTONOSUPPORT; 2861da177e4SLinus Torvalds } 2871da177e4SLinus Torvalds 288696adfe8SPaul E. McKenney if (unlikely(err)) { 289bb97d31fSArnaldo Carvalho de Melo if (try_loading_module < 2) { 290bb97d31fSArnaldo Carvalho de Melo rcu_read_unlock(); 291bb97d31fSArnaldo Carvalho de Melo /* 292bb97d31fSArnaldo Carvalho de Melo * Be more specific, e.g. net-pf-2-proto-132-type-1 293bb97d31fSArnaldo Carvalho de Melo * (net-pf-PF_INET-proto-IPPROTO_SCTP-type-SOCK_STREAM) 294bb97d31fSArnaldo Carvalho de Melo */ 295bb97d31fSArnaldo Carvalho de Melo if (++try_loading_module == 1) 296bb97d31fSArnaldo Carvalho de Melo request_module("net-pf-%d-proto-%d-type-%d", 297bb97d31fSArnaldo Carvalho de Melo PF_INET, protocol, sock->type); 298bb97d31fSArnaldo Carvalho de Melo /* 299bb97d31fSArnaldo Carvalho de Melo * Fall back to generic, e.g. net-pf-2-proto-132 300bb97d31fSArnaldo Carvalho de Melo * (net-pf-PF_INET-proto-IPPROTO_SCTP) 301bb97d31fSArnaldo Carvalho de Melo */ 302bb97d31fSArnaldo Carvalho de Melo else 303bb97d31fSArnaldo Carvalho de Melo request_module("net-pf-%d-proto-%d", 304bb97d31fSArnaldo Carvalho de Melo PF_INET, protocol); 305bb97d31fSArnaldo Carvalho de Melo goto lookup_protocol; 306bb97d31fSArnaldo Carvalho de Melo } else 3071da177e4SLinus Torvalds goto out_rcu_unlock; 308bb97d31fSArnaldo Carvalho de Melo } 309bb97d31fSArnaldo Carvalho de Melo 3101da177e4SLinus Torvalds err = -EPERM; 31152e804c6SEric W. Biederman if (sock->type == SOCK_RAW && !kern && 31252e804c6SEric W. Biederman !ns_capable(net->user_ns, CAP_NET_RAW)) 3131da177e4SLinus Torvalds goto out_rcu_unlock; 3141da177e4SLinus Torvalds 3151da177e4SLinus Torvalds sock->ops = answer->ops; 3161da177e4SLinus Torvalds answer_prot = answer->prot; 3171da177e4SLinus Torvalds answer_flags = answer->flags; 3181da177e4SLinus Torvalds rcu_read_unlock(); 3191da177e4SLinus Torvalds 32051456b29SIan Morris WARN_ON(!answer_prot->slab); 3211da177e4SLinus Torvalds 3221da177e4SLinus Torvalds err = -ENOBUFS; 32311aa9c28SEric W. Biederman sk = sk_alloc(net, PF_INET, GFP_KERNEL, answer_prot, kern); 32451456b29SIan Morris if (!sk) 3251da177e4SLinus Torvalds goto out; 3261da177e4SLinus Torvalds 3271da177e4SLinus Torvalds err = 0; 3281da177e4SLinus Torvalds if (INET_PROTOSW_REUSE & answer_flags) 3294a17fd52SPavel Emelyanov sk->sk_reuse = SK_CAN_REUSE; 3301da177e4SLinus Torvalds 3311da177e4SLinus Torvalds inet = inet_sk(sk); 332469de9b9SPaul Moore inet->is_icsk = (INET_PROTOSW_ICSK & answer_flags) != 0; 3331da177e4SLinus Torvalds 3347b2ff18eSJiri Olsa inet->nodefrag = 0; 3357b2ff18eSJiri Olsa 3361da177e4SLinus Torvalds if (SOCK_RAW == sock->type) { 337c720c7e8SEric Dumazet inet->inet_num = protocol; 3381da177e4SLinus Torvalds if (IPPROTO_RAW == protocol) 3391da177e4SLinus Torvalds inet->hdrincl = 1; 3401da177e4SLinus Torvalds } 3411da177e4SLinus Torvalds 342974eda11SHannes Frederic Sowa if (net->ipv4.sysctl_ip_no_pmtu_disc) 3431da177e4SLinus Torvalds inet->pmtudisc = IP_PMTUDISC_DONT; 3441da177e4SLinus Torvalds else 3451da177e4SLinus Torvalds inet->pmtudisc = IP_PMTUDISC_WANT; 3461da177e4SLinus Torvalds 347c720c7e8SEric Dumazet inet->inet_id = 0; 3481da177e4SLinus Torvalds 3491da177e4SLinus Torvalds sock_init_data(sock, sk); 3501da177e4SLinus Torvalds 3511da177e4SLinus Torvalds sk->sk_destruct = inet_sock_destruct; 3521da177e4SLinus Torvalds sk->sk_protocol = protocol; 3531da177e4SLinus Torvalds sk->sk_backlog_rcv = sk->sk_prot->backlog_rcv; 3541da177e4SLinus Torvalds 3551da177e4SLinus Torvalds inet->uc_ttl = -1; 3561da177e4SLinus Torvalds inet->mc_loop = 1; 3571da177e4SLinus Torvalds inet->mc_ttl = 1; 358f771bef9SNivedita Singhvi inet->mc_all = 1; 3591da177e4SLinus Torvalds inet->mc_index = 0; 3601da177e4SLinus Torvalds inet->mc_list = NULL; 3614c507d28SJiri Benc inet->rcv_tos = 0; 3621da177e4SLinus Torvalds 363e6848976SArnaldo Carvalho de Melo sk_refcnt_debug_inc(sk); 3641da177e4SLinus Torvalds 365c720c7e8SEric Dumazet if (inet->inet_num) { 3661da177e4SLinus Torvalds /* It assumes that any protocol which allows 3671da177e4SLinus Torvalds * the user to assign a number at socket 3681da177e4SLinus Torvalds * creation time automatically 3691da177e4SLinus Torvalds * shares. 3701da177e4SLinus Torvalds */ 371c720c7e8SEric Dumazet inet->inet_sport = htons(inet->inet_num); 3721da177e4SLinus Torvalds /* Add to protocol hash chains. */ 373086c653fSCraig Gallek err = sk->sk_prot->hash(sk); 374086c653fSCraig Gallek if (err) { 375086c653fSCraig Gallek sk_common_release(sk); 376086c653fSCraig Gallek goto out; 377086c653fSCraig Gallek } 3781da177e4SLinus Torvalds } 3791da177e4SLinus Torvalds 3801da177e4SLinus Torvalds if (sk->sk_prot->init) { 3811da177e4SLinus Torvalds err = sk->sk_prot->init(sk); 3821da177e4SLinus Torvalds if (err) 3831da177e4SLinus Torvalds sk_common_release(sk); 3841da177e4SLinus Torvalds } 3851da177e4SLinus Torvalds out: 3861da177e4SLinus Torvalds return err; 3871da177e4SLinus Torvalds out_rcu_unlock: 3881da177e4SLinus Torvalds rcu_read_unlock(); 3891da177e4SLinus Torvalds goto out; 3901da177e4SLinus Torvalds } 3911da177e4SLinus Torvalds 3921da177e4SLinus Torvalds 3931da177e4SLinus Torvalds /* 3941da177e4SLinus Torvalds * The peer socket should always be NULL (or else). When we call this 3951da177e4SLinus Torvalds * function we are destroying the object and from then on nobody 3961da177e4SLinus Torvalds * should refer to it. 3971da177e4SLinus Torvalds */ 3981da177e4SLinus Torvalds int inet_release(struct socket *sock) 3991da177e4SLinus Torvalds { 4001da177e4SLinus Torvalds struct sock *sk = sock->sk; 4011da177e4SLinus Torvalds 4021da177e4SLinus Torvalds if (sk) { 4031da177e4SLinus Torvalds long timeout; 4041da177e4SLinus Torvalds 4051da177e4SLinus Torvalds /* Applications forget to leave groups before exiting */ 4061da177e4SLinus Torvalds ip_mc_drop_socket(sk); 4071da177e4SLinus Torvalds 4081da177e4SLinus Torvalds /* If linger is set, we don't return until the close 4091da177e4SLinus Torvalds * is complete. Otherwise we return immediately. The 4101da177e4SLinus Torvalds * actually closing is done the same either way. 4111da177e4SLinus Torvalds * 4121da177e4SLinus Torvalds * If the close is due to the process exiting, we never 4131da177e4SLinus Torvalds * linger.. 4141da177e4SLinus Torvalds */ 4151da177e4SLinus Torvalds timeout = 0; 4161da177e4SLinus Torvalds if (sock_flag(sk, SOCK_LINGER) && 4171da177e4SLinus Torvalds !(current->flags & PF_EXITING)) 4181da177e4SLinus Torvalds timeout = sk->sk_lingertime; 4191da177e4SLinus Torvalds sock->sk = NULL; 4201da177e4SLinus Torvalds sk->sk_prot->close(sk, timeout); 4211da177e4SLinus Torvalds } 4221da177e4SLinus Torvalds return 0; 4231da177e4SLinus Torvalds } 4243d1427f8SEric Dumazet EXPORT_SYMBOL(inet_release); 4251da177e4SLinus Torvalds 4261da177e4SLinus Torvalds int inet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len) 4271da177e4SLinus Torvalds { 4281da177e4SLinus Torvalds struct sockaddr_in *addr = (struct sockaddr_in *)uaddr; 4291da177e4SLinus Torvalds struct sock *sk = sock->sk; 4301da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 4313594698aSEric W. Biederman struct net *net = sock_net(sk); 4321da177e4SLinus Torvalds unsigned short snum; 4331da177e4SLinus Torvalds int chk_addr_ret; 4349b8ff518SDavid Ahern u32 tb_id = RT_TABLE_LOCAL; 4351da177e4SLinus Torvalds int err; 4361da177e4SLinus Torvalds 4371da177e4SLinus Torvalds /* If the socket has its own bind function then use it. (RAW) */ 4381da177e4SLinus Torvalds if (sk->sk_prot->bind) { 4391da177e4SLinus Torvalds err = sk->sk_prot->bind(sk, uaddr, addr_len); 4401da177e4SLinus Torvalds goto out; 4411da177e4SLinus Torvalds } 4421da177e4SLinus Torvalds err = -EINVAL; 4431da177e4SLinus Torvalds if (addr_len < sizeof(struct sockaddr_in)) 4441da177e4SLinus Torvalds goto out; 4451da177e4SLinus Torvalds 446c349a528SMarcus Meissner if (addr->sin_family != AF_INET) { 44729c486dfSEric Dumazet /* Compatibility games : accept AF_UNSPEC (mapped to AF_INET) 44829c486dfSEric Dumazet * only if s_addr is INADDR_ANY. 44929c486dfSEric Dumazet */ 450c349a528SMarcus Meissner err = -EAFNOSUPPORT; 45129c486dfSEric Dumazet if (addr->sin_family != AF_UNSPEC || 45229c486dfSEric Dumazet addr->sin_addr.s_addr != htonl(INADDR_ANY)) 453d0733d2eSMarcus Meissner goto out; 454c349a528SMarcus Meissner } 455d0733d2eSMarcus Meissner 4563236b004SDavid Ahern tb_id = l3mdev_fib_table_by_index(net, sk->sk_bound_dev_if) ? : tb_id; 45730bbaa19SDavid Ahern chk_addr_ret = inet_addr_type_table(net, addr->sin_addr.s_addr, tb_id); 4581da177e4SLinus Torvalds 4591da177e4SLinus Torvalds /* Not specified by any standard per-se, however it breaks too 4601da177e4SLinus Torvalds * many applications when removed. It is unfortunate since 4611da177e4SLinus Torvalds * allowing applications to make a non-local bind solves 4621da177e4SLinus Torvalds * several problems with systems using dynamic addressing. 4631da177e4SLinus Torvalds * (ie. your servers still start up even if your ISDN link 4641da177e4SLinus Torvalds * is temporarily down) 4651da177e4SLinus Torvalds */ 4661da177e4SLinus Torvalds err = -EADDRNOTAVAIL; 46749a60158SVincent Bernat if (!net->ipv4.sysctl_ip_nonlocal_bind && 468b9fb1506STóth László Attila !(inet->freebind || inet->transparent) && 469e6f1cebfSAl Viro addr->sin_addr.s_addr != htonl(INADDR_ANY) && 4701da177e4SLinus Torvalds chk_addr_ret != RTN_LOCAL && 4711da177e4SLinus Torvalds chk_addr_ret != RTN_MULTICAST && 4721da177e4SLinus Torvalds chk_addr_ret != RTN_BROADCAST) 4731da177e4SLinus Torvalds goto out; 4741da177e4SLinus Torvalds 4751da177e4SLinus Torvalds snum = ntohs(addr->sin_port); 4761da177e4SLinus Torvalds err = -EACCES; 4773594698aSEric W. Biederman if (snum && snum < PROT_SOCK && 4783594698aSEric W. Biederman !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE)) 4791da177e4SLinus Torvalds goto out; 4801da177e4SLinus Torvalds 4811da177e4SLinus Torvalds /* We keep a pair of addresses. rcv_saddr is the one 4821da177e4SLinus Torvalds * used by hash lookups, and saddr is used for transmit. 4831da177e4SLinus Torvalds * 4841da177e4SLinus Torvalds * In the BSD API these are the same except where it 4851da177e4SLinus Torvalds * would be illegal to use them (multicast/broadcast) in 4861da177e4SLinus Torvalds * which case the sending device address is used. 4871da177e4SLinus Torvalds */ 4881da177e4SLinus Torvalds lock_sock(sk); 4891da177e4SLinus Torvalds 4901da177e4SLinus Torvalds /* Check these errors (active socket, double bind). */ 4911da177e4SLinus Torvalds err = -EINVAL; 492c720c7e8SEric Dumazet if (sk->sk_state != TCP_CLOSE || inet->inet_num) 4931da177e4SLinus Torvalds goto out_release_sock; 4941da177e4SLinus Torvalds 495c720c7e8SEric Dumazet inet->inet_rcv_saddr = inet->inet_saddr = addr->sin_addr.s_addr; 4961da177e4SLinus Torvalds if (chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST) 497c720c7e8SEric Dumazet inet->inet_saddr = 0; /* Use device */ 4981da177e4SLinus Torvalds 4991da177e4SLinus Torvalds /* Make sure we are allowed to bind here. */ 50090c337daSEric Dumazet if ((snum || !inet->bind_address_no_port) && 50190c337daSEric Dumazet sk->sk_prot->get_port(sk, snum)) { 502c720c7e8SEric Dumazet inet->inet_saddr = inet->inet_rcv_saddr = 0; 5031da177e4SLinus Torvalds err = -EADDRINUSE; 5041da177e4SLinus Torvalds goto out_release_sock; 5051da177e4SLinus Torvalds } 5061da177e4SLinus Torvalds 507c720c7e8SEric Dumazet if (inet->inet_rcv_saddr) 5081da177e4SLinus Torvalds sk->sk_userlocks |= SOCK_BINDADDR_LOCK; 5091da177e4SLinus Torvalds if (snum) 5101da177e4SLinus Torvalds sk->sk_userlocks |= SOCK_BINDPORT_LOCK; 511c720c7e8SEric Dumazet inet->inet_sport = htons(inet->inet_num); 512c720c7e8SEric Dumazet inet->inet_daddr = 0; 513c720c7e8SEric Dumazet inet->inet_dport = 0; 5141da177e4SLinus Torvalds sk_dst_reset(sk); 5151da177e4SLinus Torvalds err = 0; 5161da177e4SLinus Torvalds out_release_sock: 5171da177e4SLinus Torvalds release_sock(sk); 5181da177e4SLinus Torvalds out: 5191da177e4SLinus Torvalds return err; 5201da177e4SLinus Torvalds } 5213d1427f8SEric Dumazet EXPORT_SYMBOL(inet_bind); 5221da177e4SLinus Torvalds 5231da177e4SLinus Torvalds int inet_dgram_connect(struct socket *sock, struct sockaddr *uaddr, 5241da177e4SLinus Torvalds int addr_len, int flags) 5251da177e4SLinus Torvalds { 5261da177e4SLinus Torvalds struct sock *sk = sock->sk; 5271da177e4SLinus Torvalds 5286503d961SChangli Gao if (addr_len < sizeof(uaddr->sa_family)) 5296503d961SChangli Gao return -EINVAL; 5301da177e4SLinus Torvalds if (uaddr->sa_family == AF_UNSPEC) 5311da177e4SLinus Torvalds return sk->sk_prot->disconnect(sk, flags); 5321da177e4SLinus Torvalds 533c720c7e8SEric Dumazet if (!inet_sk(sk)->inet_num && inet_autobind(sk)) 5341da177e4SLinus Torvalds return -EAGAIN; 535e3192690SJoe Perches return sk->sk_prot->connect(sk, uaddr, addr_len); 5361da177e4SLinus Torvalds } 5373d1427f8SEric Dumazet EXPORT_SYMBOL(inet_dgram_connect); 5381da177e4SLinus Torvalds 539783237e8SYuchung Cheng static long inet_wait_for_connect(struct sock *sk, long timeo, int writebias) 5401da177e4SLinus Torvalds { 5411da177e4SLinus Torvalds DEFINE_WAIT(wait); 5421da177e4SLinus Torvalds 543aa395145SEric Dumazet prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); 544783237e8SYuchung Cheng sk->sk_write_pending += writebias; 5451da177e4SLinus Torvalds 5461da177e4SLinus Torvalds /* Basic assumption: if someone sets sk->sk_err, he _must_ 5471da177e4SLinus Torvalds * change state of the socket from TCP_SYN_*. 5481da177e4SLinus Torvalds * Connect() does not allow to get error notifications 5491da177e4SLinus Torvalds * without closing the socket. 5501da177e4SLinus Torvalds */ 5511da177e4SLinus Torvalds while ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) { 5521da177e4SLinus Torvalds release_sock(sk); 5531da177e4SLinus Torvalds timeo = schedule_timeout(timeo); 5541da177e4SLinus Torvalds lock_sock(sk); 5551da177e4SLinus Torvalds if (signal_pending(current) || !timeo) 5561da177e4SLinus Torvalds break; 557aa395145SEric Dumazet prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); 5581da177e4SLinus Torvalds } 559aa395145SEric Dumazet finish_wait(sk_sleep(sk), &wait); 560783237e8SYuchung Cheng sk->sk_write_pending -= writebias; 5611da177e4SLinus Torvalds return timeo; 5621da177e4SLinus Torvalds } 5631da177e4SLinus Torvalds 5641da177e4SLinus Torvalds /* 5651da177e4SLinus Torvalds * Connect to a remote host. There is regrettably still a little 5661da177e4SLinus Torvalds * TCP 'magic' in here. 5671da177e4SLinus Torvalds */ 568cf60af03SYuchung Cheng int __inet_stream_connect(struct socket *sock, struct sockaddr *uaddr, 5691da177e4SLinus Torvalds int addr_len, int flags) 5701da177e4SLinus Torvalds { 5711da177e4SLinus Torvalds struct sock *sk = sock->sk; 5721da177e4SLinus Torvalds int err; 5731da177e4SLinus Torvalds long timeo; 5741da177e4SLinus Torvalds 5756503d961SChangli Gao if (addr_len < sizeof(uaddr->sa_family)) 5766503d961SChangli Gao return -EINVAL; 5776503d961SChangli Gao 5781da177e4SLinus Torvalds if (uaddr->sa_family == AF_UNSPEC) { 5791da177e4SLinus Torvalds err = sk->sk_prot->disconnect(sk, flags); 5801da177e4SLinus Torvalds sock->state = err ? SS_DISCONNECTING : SS_UNCONNECTED; 5811da177e4SLinus Torvalds goto out; 5821da177e4SLinus Torvalds } 5831da177e4SLinus Torvalds 5841da177e4SLinus Torvalds switch (sock->state) { 5851da177e4SLinus Torvalds default: 5861da177e4SLinus Torvalds err = -EINVAL; 5871da177e4SLinus Torvalds goto out; 5881da177e4SLinus Torvalds case SS_CONNECTED: 5891da177e4SLinus Torvalds err = -EISCONN; 5901da177e4SLinus Torvalds goto out; 5911da177e4SLinus Torvalds case SS_CONNECTING: 5921da177e4SLinus Torvalds err = -EALREADY; 5931da177e4SLinus Torvalds /* Fall out of switch with err, set for this state */ 5941da177e4SLinus Torvalds break; 5951da177e4SLinus Torvalds case SS_UNCONNECTED: 5961da177e4SLinus Torvalds err = -EISCONN; 5971da177e4SLinus Torvalds if (sk->sk_state != TCP_CLOSE) 5981da177e4SLinus Torvalds goto out; 5991da177e4SLinus Torvalds 6001da177e4SLinus Torvalds err = sk->sk_prot->connect(sk, uaddr, addr_len); 6011da177e4SLinus Torvalds if (err < 0) 6021da177e4SLinus Torvalds goto out; 6031da177e4SLinus Torvalds 6041da177e4SLinus Torvalds sock->state = SS_CONNECTING; 6051da177e4SLinus Torvalds 6061da177e4SLinus Torvalds /* Just entered SS_CONNECTING state; the only 6071da177e4SLinus Torvalds * difference is that return value in non-blocking 6081da177e4SLinus Torvalds * case is EINPROGRESS, rather than EALREADY. 6091da177e4SLinus Torvalds */ 6101da177e4SLinus Torvalds err = -EINPROGRESS; 6111da177e4SLinus Torvalds break; 6121da177e4SLinus Torvalds } 6131da177e4SLinus Torvalds 6141da177e4SLinus Torvalds timeo = sock_sndtimeo(sk, flags & O_NONBLOCK); 6151da177e4SLinus Torvalds 6161da177e4SLinus Torvalds if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) { 617783237e8SYuchung Cheng int writebias = (sk->sk_protocol == IPPROTO_TCP) && 618783237e8SYuchung Cheng tcp_sk(sk)->fastopen_req && 619783237e8SYuchung Cheng tcp_sk(sk)->fastopen_req->data ? 1 : 0; 620783237e8SYuchung Cheng 6211da177e4SLinus Torvalds /* Error code is set above */ 622783237e8SYuchung Cheng if (!timeo || !inet_wait_for_connect(sk, timeo, writebias)) 6231da177e4SLinus Torvalds goto out; 6241da177e4SLinus Torvalds 6251da177e4SLinus Torvalds err = sock_intr_errno(timeo); 6261da177e4SLinus Torvalds if (signal_pending(current)) 6271da177e4SLinus Torvalds goto out; 6281da177e4SLinus Torvalds } 6291da177e4SLinus Torvalds 6301da177e4SLinus Torvalds /* Connection was closed by RST, timeout, ICMP error 6311da177e4SLinus Torvalds * or another process disconnected us. 6321da177e4SLinus Torvalds */ 6331da177e4SLinus Torvalds if (sk->sk_state == TCP_CLOSE) 6341da177e4SLinus Torvalds goto sock_error; 6351da177e4SLinus Torvalds 6361da177e4SLinus Torvalds /* sk->sk_err may be not zero now, if RECVERR was ordered by user 6371da177e4SLinus Torvalds * and error was received after socket entered established state. 6381da177e4SLinus Torvalds * Hence, it is handled normally after connect() return successfully. 6391da177e4SLinus Torvalds */ 6401da177e4SLinus Torvalds 6411da177e4SLinus Torvalds sock->state = SS_CONNECTED; 6421da177e4SLinus Torvalds err = 0; 6431da177e4SLinus Torvalds out: 6441da177e4SLinus Torvalds return err; 6451da177e4SLinus Torvalds 6461da177e4SLinus Torvalds sock_error: 6471da177e4SLinus Torvalds err = sock_error(sk) ? : -ECONNABORTED; 6481da177e4SLinus Torvalds sock->state = SS_UNCONNECTED; 6491da177e4SLinus Torvalds if (sk->sk_prot->disconnect(sk, flags)) 6501da177e4SLinus Torvalds sock->state = SS_DISCONNECTING; 6511da177e4SLinus Torvalds goto out; 6521da177e4SLinus Torvalds } 653cf60af03SYuchung Cheng EXPORT_SYMBOL(__inet_stream_connect); 654cf60af03SYuchung Cheng 655cf60af03SYuchung Cheng int inet_stream_connect(struct socket *sock, struct sockaddr *uaddr, 656cf60af03SYuchung Cheng int addr_len, int flags) 657cf60af03SYuchung Cheng { 658cf60af03SYuchung Cheng int err; 659cf60af03SYuchung Cheng 660cf60af03SYuchung Cheng lock_sock(sock->sk); 661cf60af03SYuchung Cheng err = __inet_stream_connect(sock, uaddr, addr_len, flags); 662cf60af03SYuchung Cheng release_sock(sock->sk); 663cf60af03SYuchung Cheng return err; 664cf60af03SYuchung Cheng } 6653d1427f8SEric Dumazet EXPORT_SYMBOL(inet_stream_connect); 6661da177e4SLinus Torvalds 6671da177e4SLinus Torvalds /* 6681da177e4SLinus Torvalds * Accept a pending connection. The TCP layer now gives BSD semantics. 6691da177e4SLinus Torvalds */ 6701da177e4SLinus Torvalds 6711da177e4SLinus Torvalds int inet_accept(struct socket *sock, struct socket *newsock, int flags) 6721da177e4SLinus Torvalds { 6731da177e4SLinus Torvalds struct sock *sk1 = sock->sk; 6741da177e4SLinus Torvalds int err = -EINVAL; 6751da177e4SLinus Torvalds struct sock *sk2 = sk1->sk_prot->accept(sk1, flags, &err); 6761da177e4SLinus Torvalds 6771da177e4SLinus Torvalds if (!sk2) 6781da177e4SLinus Torvalds goto do_err; 6791da177e4SLinus Torvalds 6801da177e4SLinus Torvalds lock_sock(sk2); 6811da177e4SLinus Torvalds 6821eddceadSEric Dumazet sock_rps_record_flow(sk2); 683547b792cSIlpo Järvinen WARN_ON(!((1 << sk2->sk_state) & 6848336886fSJerry Chu (TCPF_ESTABLISHED | TCPF_SYN_RECV | 6858336886fSJerry Chu TCPF_CLOSE_WAIT | TCPF_CLOSE))); 6861da177e4SLinus Torvalds 6871da177e4SLinus Torvalds sock_graft(sk2, newsock); 6881da177e4SLinus Torvalds 6891da177e4SLinus Torvalds newsock->state = SS_CONNECTED; 6901da177e4SLinus Torvalds err = 0; 6911da177e4SLinus Torvalds release_sock(sk2); 6921da177e4SLinus Torvalds do_err: 6931da177e4SLinus Torvalds return err; 6941da177e4SLinus Torvalds } 6953d1427f8SEric Dumazet EXPORT_SYMBOL(inet_accept); 6961da177e4SLinus Torvalds 6971da177e4SLinus Torvalds 6981da177e4SLinus Torvalds /* 6991da177e4SLinus Torvalds * This does both peername and sockname. 7001da177e4SLinus Torvalds */ 7011da177e4SLinus Torvalds int inet_getname(struct socket *sock, struct sockaddr *uaddr, 7021da177e4SLinus Torvalds int *uaddr_len, int peer) 7031da177e4SLinus Torvalds { 7041da177e4SLinus Torvalds struct sock *sk = sock->sk; 7051da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 70638bfd8f5SCyrill Gorcunov DECLARE_SOCKADDR(struct sockaddr_in *, sin, uaddr); 7071da177e4SLinus Torvalds 7081da177e4SLinus Torvalds sin->sin_family = AF_INET; 7091da177e4SLinus Torvalds if (peer) { 710c720c7e8SEric Dumazet if (!inet->inet_dport || 7111da177e4SLinus Torvalds (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) && 7121da177e4SLinus Torvalds peer == 1)) 7131da177e4SLinus Torvalds return -ENOTCONN; 714c720c7e8SEric Dumazet sin->sin_port = inet->inet_dport; 715c720c7e8SEric Dumazet sin->sin_addr.s_addr = inet->inet_daddr; 7161da177e4SLinus Torvalds } else { 717c720c7e8SEric Dumazet __be32 addr = inet->inet_rcv_saddr; 7181da177e4SLinus Torvalds if (!addr) 719c720c7e8SEric Dumazet addr = inet->inet_saddr; 720c720c7e8SEric Dumazet sin->sin_port = inet->inet_sport; 7211da177e4SLinus Torvalds sin->sin_addr.s_addr = addr; 7221da177e4SLinus Torvalds } 7231da177e4SLinus Torvalds memset(sin->sin_zero, 0, sizeof(sin->sin_zero)); 7241da177e4SLinus Torvalds *uaddr_len = sizeof(*sin); 7251da177e4SLinus Torvalds return 0; 7261da177e4SLinus Torvalds } 7273d1427f8SEric Dumazet EXPORT_SYMBOL(inet_getname); 7281da177e4SLinus Torvalds 7291b784140SYing Xue int inet_sendmsg(struct socket *sock, struct msghdr *msg, size_t size) 7301da177e4SLinus Torvalds { 7311da177e4SLinus Torvalds struct sock *sk = sock->sk; 7321da177e4SLinus Torvalds 733c58dc01bSDavid S. Miller sock_rps_record_flow(sk); 734fec5e652STom Herbert 7351da177e4SLinus Torvalds /* We may need to bind the socket. */ 7367ba42910SChangli Gao if (!inet_sk(sk)->inet_num && !sk->sk_prot->no_autobind && 7377ba42910SChangli Gao inet_autobind(sk)) 7381da177e4SLinus Torvalds return -EAGAIN; 7391da177e4SLinus Torvalds 7401b784140SYing Xue return sk->sk_prot->sendmsg(sk, msg, size); 7411da177e4SLinus Torvalds } 7423d1427f8SEric Dumazet EXPORT_SYMBOL(inet_sendmsg); 7431da177e4SLinus Torvalds 7447ba42910SChangli Gao ssize_t inet_sendpage(struct socket *sock, struct page *page, int offset, 7453d1427f8SEric Dumazet size_t size, int flags) 7461da177e4SLinus Torvalds { 7471da177e4SLinus Torvalds struct sock *sk = sock->sk; 7481da177e4SLinus Torvalds 749c58dc01bSDavid S. Miller sock_rps_record_flow(sk); 750fec5e652STom Herbert 7511da177e4SLinus Torvalds /* We may need to bind the socket. */ 7527ba42910SChangli Gao if (!inet_sk(sk)->inet_num && !sk->sk_prot->no_autobind && 7537ba42910SChangli Gao inet_autobind(sk)) 7541da177e4SLinus Torvalds return -EAGAIN; 7551da177e4SLinus Torvalds 7561da177e4SLinus Torvalds if (sk->sk_prot->sendpage) 7571da177e4SLinus Torvalds return sk->sk_prot->sendpage(sk, page, offset, size, flags); 7581da177e4SLinus Torvalds return sock_no_sendpage(sock, page, offset, size, flags); 7591da177e4SLinus Torvalds } 7607ba42910SChangli Gao EXPORT_SYMBOL(inet_sendpage); 7611da177e4SLinus Torvalds 7621b784140SYing Xue int inet_recvmsg(struct socket *sock, struct msghdr *msg, size_t size, 7631b784140SYing Xue int flags) 764fec5e652STom Herbert { 765fec5e652STom Herbert struct sock *sk = sock->sk; 766fec5e652STom Herbert int addr_len = 0; 767fec5e652STom Herbert int err; 768fec5e652STom Herbert 769c58dc01bSDavid S. Miller sock_rps_record_flow(sk); 770fec5e652STom Herbert 7711b784140SYing Xue err = sk->sk_prot->recvmsg(sk, msg, size, flags & MSG_DONTWAIT, 772fec5e652STom Herbert flags & ~MSG_DONTWAIT, &addr_len); 773fec5e652STom Herbert if (err >= 0) 774fec5e652STom Herbert msg->msg_namelen = addr_len; 775fec5e652STom Herbert return err; 776fec5e652STom Herbert } 777fec5e652STom Herbert EXPORT_SYMBOL(inet_recvmsg); 7781da177e4SLinus Torvalds 7791da177e4SLinus Torvalds int inet_shutdown(struct socket *sock, int how) 7801da177e4SLinus Torvalds { 7811da177e4SLinus Torvalds struct sock *sk = sock->sk; 7821da177e4SLinus Torvalds int err = 0; 7831da177e4SLinus Torvalds 7841da177e4SLinus Torvalds /* This should really check to make sure 7851da177e4SLinus Torvalds * the socket is a TCP socket. (WHY AC...) 7861da177e4SLinus Torvalds */ 7871da177e4SLinus Torvalds how++; /* maps 0->1 has the advantage of making bit 1 rcvs and 7881da177e4SLinus Torvalds 1->2 bit 2 snds. 7891da177e4SLinus Torvalds 2->3 */ 7901da177e4SLinus Torvalds if ((how & ~SHUTDOWN_MASK) || !how) /* MAXINT->0 */ 7911da177e4SLinus Torvalds return -EINVAL; 7921da177e4SLinus Torvalds 7931da177e4SLinus Torvalds lock_sock(sk); 7941da177e4SLinus Torvalds if (sock->state == SS_CONNECTING) { 7951da177e4SLinus Torvalds if ((1 << sk->sk_state) & 7961da177e4SLinus Torvalds (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_CLOSE)) 7971da177e4SLinus Torvalds sock->state = SS_DISCONNECTING; 7981da177e4SLinus Torvalds else 7991da177e4SLinus Torvalds sock->state = SS_CONNECTED; 8001da177e4SLinus Torvalds } 8011da177e4SLinus Torvalds 8021da177e4SLinus Torvalds switch (sk->sk_state) { 8031da177e4SLinus Torvalds case TCP_CLOSE: 8041da177e4SLinus Torvalds err = -ENOTCONN; 8051da177e4SLinus Torvalds /* Hack to wake up other listeners, who can poll for 8061da177e4SLinus Torvalds POLLHUP, even on eg. unconnected UDP sockets -- RR */ 8071da177e4SLinus Torvalds default: 8081da177e4SLinus Torvalds sk->sk_shutdown |= how; 8091da177e4SLinus Torvalds if (sk->sk_prot->shutdown) 8101da177e4SLinus Torvalds sk->sk_prot->shutdown(sk, how); 8111da177e4SLinus Torvalds break; 8121da177e4SLinus Torvalds 8131da177e4SLinus Torvalds /* Remaining two branches are temporary solution for missing 8141da177e4SLinus Torvalds * close() in multithreaded environment. It is _not_ a good idea, 8151da177e4SLinus Torvalds * but we have no choice until close() is repaired at VFS level. 8161da177e4SLinus Torvalds */ 8171da177e4SLinus Torvalds case TCP_LISTEN: 8181da177e4SLinus Torvalds if (!(how & RCV_SHUTDOWN)) 8191da177e4SLinus Torvalds break; 8201da177e4SLinus Torvalds /* Fall through */ 8211da177e4SLinus Torvalds case TCP_SYN_SENT: 8221da177e4SLinus Torvalds err = sk->sk_prot->disconnect(sk, O_NONBLOCK); 8231da177e4SLinus Torvalds sock->state = err ? SS_DISCONNECTING : SS_UNCONNECTED; 8241da177e4SLinus Torvalds break; 8251da177e4SLinus Torvalds } 8261da177e4SLinus Torvalds 8271da177e4SLinus Torvalds /* Wake up anyone sleeping in poll. */ 8281da177e4SLinus Torvalds sk->sk_state_change(sk); 8291da177e4SLinus Torvalds release_sock(sk); 8301da177e4SLinus Torvalds return err; 8311da177e4SLinus Torvalds } 8323d1427f8SEric Dumazet EXPORT_SYMBOL(inet_shutdown); 8331da177e4SLinus Torvalds 8341da177e4SLinus Torvalds /* 8351da177e4SLinus Torvalds * ioctl() calls you can issue on an INET socket. Most of these are 8361da177e4SLinus Torvalds * device configuration and stuff and very rarely used. Some ioctls 8371da177e4SLinus Torvalds * pass on to the socket itself. 8381da177e4SLinus Torvalds * 8391da177e4SLinus Torvalds * NOTE: I like the idea of a module for the config stuff. ie ifconfig 8401da177e4SLinus Torvalds * loads the devconfigure module does its configuring and unloads it. 8411da177e4SLinus Torvalds * There's a good 20K of config code hanging around the kernel. 8421da177e4SLinus Torvalds */ 8431da177e4SLinus Torvalds 8441da177e4SLinus Torvalds int inet_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) 8451da177e4SLinus Torvalds { 8461da177e4SLinus Torvalds struct sock *sk = sock->sk; 8471da177e4SLinus Torvalds int err = 0; 8483b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(sk); 8491da177e4SLinus Torvalds 8501da177e4SLinus Torvalds switch (cmd) { 8511da177e4SLinus Torvalds case SIOCGSTAMP: 8521da177e4SLinus Torvalds err = sock_get_timestamp(sk, (struct timeval __user *)arg); 8531da177e4SLinus Torvalds break; 854ae40eb1eSEric Dumazet case SIOCGSTAMPNS: 855ae40eb1eSEric Dumazet err = sock_get_timestampns(sk, (struct timespec __user *)arg); 856ae40eb1eSEric Dumazet break; 8571da177e4SLinus Torvalds case SIOCADDRT: 8581da177e4SLinus Torvalds case SIOCDELRT: 8591da177e4SLinus Torvalds case SIOCRTMSG: 860e5b13cb1SDenis V. Lunev err = ip_rt_ioctl(net, cmd, (void __user *)arg); 8611da177e4SLinus Torvalds break; 8621da177e4SLinus Torvalds case SIOCDARP: 8631da177e4SLinus Torvalds case SIOCGARP: 8641da177e4SLinus Torvalds case SIOCSARP: 865e5b13cb1SDenis V. Lunev err = arp_ioctl(net, cmd, (void __user *)arg); 8661da177e4SLinus Torvalds break; 8671da177e4SLinus Torvalds case SIOCGIFADDR: 8681da177e4SLinus Torvalds case SIOCSIFADDR: 8691da177e4SLinus Torvalds case SIOCGIFBRDADDR: 8701da177e4SLinus Torvalds case SIOCSIFBRDADDR: 8711da177e4SLinus Torvalds case SIOCGIFNETMASK: 8721da177e4SLinus Torvalds case SIOCSIFNETMASK: 8731da177e4SLinus Torvalds case SIOCGIFDSTADDR: 8741da177e4SLinus Torvalds case SIOCSIFDSTADDR: 8751da177e4SLinus Torvalds case SIOCSIFPFLAGS: 8761da177e4SLinus Torvalds case SIOCGIFPFLAGS: 8771da177e4SLinus Torvalds case SIOCSIFFLAGS: 878e5b13cb1SDenis V. Lunev err = devinet_ioctl(net, cmd, (void __user *)arg); 8791da177e4SLinus Torvalds break; 8801da177e4SLinus Torvalds default: 881b5e5fa5eSChristoph Hellwig if (sk->sk_prot->ioctl) 882b5e5fa5eSChristoph Hellwig err = sk->sk_prot->ioctl(sk, cmd, arg); 883b5e5fa5eSChristoph Hellwig else 884b5e5fa5eSChristoph Hellwig err = -ENOIOCTLCMD; 8851da177e4SLinus Torvalds break; 8861da177e4SLinus Torvalds } 8871da177e4SLinus Torvalds return err; 8881da177e4SLinus Torvalds } 8893d1427f8SEric Dumazet EXPORT_SYMBOL(inet_ioctl); 8901da177e4SLinus Torvalds 891709b46e8SEric W. Biederman #ifdef CONFIG_COMPAT 892686dc6b6SGerrit Renker static int inet_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) 893709b46e8SEric W. Biederman { 894709b46e8SEric W. Biederman struct sock *sk = sock->sk; 895709b46e8SEric W. Biederman int err = -ENOIOCTLCMD; 896709b46e8SEric W. Biederman 897709b46e8SEric W. Biederman if (sk->sk_prot->compat_ioctl) 898709b46e8SEric W. Biederman err = sk->sk_prot->compat_ioctl(sk, cmd, arg); 899709b46e8SEric W. Biederman 900709b46e8SEric W. Biederman return err; 901709b46e8SEric W. Biederman } 902709b46e8SEric W. Biederman #endif 903709b46e8SEric W. Biederman 90490ddc4f0SEric Dumazet const struct proto_ops inet_stream_ops = { 9051da177e4SLinus Torvalds .family = PF_INET, 9061da177e4SLinus Torvalds .owner = THIS_MODULE, 9071da177e4SLinus Torvalds .release = inet_release, 9081da177e4SLinus Torvalds .bind = inet_bind, 9091da177e4SLinus Torvalds .connect = inet_stream_connect, 9101da177e4SLinus Torvalds .socketpair = sock_no_socketpair, 9111da177e4SLinus Torvalds .accept = inet_accept, 9121da177e4SLinus Torvalds .getname = inet_getname, 9131da177e4SLinus Torvalds .poll = tcp_poll, 9141da177e4SLinus Torvalds .ioctl = inet_ioctl, 9151da177e4SLinus Torvalds .listen = inet_listen, 9161da177e4SLinus Torvalds .shutdown = inet_shutdown, 9171da177e4SLinus Torvalds .setsockopt = sock_common_setsockopt, 9181da177e4SLinus Torvalds .getsockopt = sock_common_getsockopt, 9197ba42910SChangli Gao .sendmsg = inet_sendmsg, 920fec5e652STom Herbert .recvmsg = inet_recvmsg, 921543d9cfeSArnaldo Carvalho de Melo .mmap = sock_no_mmap, 9227ba42910SChangli Gao .sendpage = inet_sendpage, 9239c55e01cSJens Axboe .splice_read = tcp_splice_read, 9243fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT 9253fdadf7dSDmitry Mishin .compat_setsockopt = compat_sock_common_setsockopt, 9263fdadf7dSDmitry Mishin .compat_getsockopt = compat_sock_common_getsockopt, 927709b46e8SEric W. Biederman .compat_ioctl = inet_compat_ioctl, 9283fdadf7dSDmitry Mishin #endif 9291da177e4SLinus Torvalds }; 9303d1427f8SEric Dumazet EXPORT_SYMBOL(inet_stream_ops); 9311da177e4SLinus Torvalds 93290ddc4f0SEric Dumazet const struct proto_ops inet_dgram_ops = { 9331da177e4SLinus Torvalds .family = PF_INET, 9341da177e4SLinus Torvalds .owner = THIS_MODULE, 9351da177e4SLinus Torvalds .release = inet_release, 9361da177e4SLinus Torvalds .bind = inet_bind, 9371da177e4SLinus Torvalds .connect = inet_dgram_connect, 9381da177e4SLinus Torvalds .socketpair = sock_no_socketpair, 9391da177e4SLinus Torvalds .accept = sock_no_accept, 9401da177e4SLinus Torvalds .getname = inet_getname, 9411da177e4SLinus Torvalds .poll = udp_poll, 9421da177e4SLinus Torvalds .ioctl = inet_ioctl, 9431da177e4SLinus Torvalds .listen = sock_no_listen, 9441da177e4SLinus Torvalds .shutdown = inet_shutdown, 9451da177e4SLinus Torvalds .setsockopt = sock_common_setsockopt, 9461da177e4SLinus Torvalds .getsockopt = sock_common_getsockopt, 9471da177e4SLinus Torvalds .sendmsg = inet_sendmsg, 948fec5e652STom Herbert .recvmsg = inet_recvmsg, 9491da177e4SLinus Torvalds .mmap = sock_no_mmap, 9501da177e4SLinus Torvalds .sendpage = inet_sendpage, 951627d2d6bSsamanthakumar .set_peek_off = sk_set_peek_off, 952543d9cfeSArnaldo Carvalho de Melo #ifdef CONFIG_COMPAT 953543d9cfeSArnaldo Carvalho de Melo .compat_setsockopt = compat_sock_common_setsockopt, 954543d9cfeSArnaldo Carvalho de Melo .compat_getsockopt = compat_sock_common_getsockopt, 955709b46e8SEric W. Biederman .compat_ioctl = inet_compat_ioctl, 956543d9cfeSArnaldo Carvalho de Melo #endif 9571da177e4SLinus Torvalds }; 9583d1427f8SEric Dumazet EXPORT_SYMBOL(inet_dgram_ops); 9591da177e4SLinus Torvalds 9601da177e4SLinus Torvalds /* 9611da177e4SLinus Torvalds * For SOCK_RAW sockets; should be the same as inet_dgram_ops but without 9621da177e4SLinus Torvalds * udp_poll 9631da177e4SLinus Torvalds */ 96490ddc4f0SEric Dumazet static const struct proto_ops inet_sockraw_ops = { 9651da177e4SLinus Torvalds .family = PF_INET, 9661da177e4SLinus Torvalds .owner = THIS_MODULE, 9671da177e4SLinus Torvalds .release = inet_release, 9681da177e4SLinus Torvalds .bind = inet_bind, 9691da177e4SLinus Torvalds .connect = inet_dgram_connect, 9701da177e4SLinus Torvalds .socketpair = sock_no_socketpair, 9711da177e4SLinus Torvalds .accept = sock_no_accept, 9721da177e4SLinus Torvalds .getname = inet_getname, 9731da177e4SLinus Torvalds .poll = datagram_poll, 9741da177e4SLinus Torvalds .ioctl = inet_ioctl, 9751da177e4SLinus Torvalds .listen = sock_no_listen, 9761da177e4SLinus Torvalds .shutdown = inet_shutdown, 9771da177e4SLinus Torvalds .setsockopt = sock_common_setsockopt, 9781da177e4SLinus Torvalds .getsockopt = sock_common_getsockopt, 9791da177e4SLinus Torvalds .sendmsg = inet_sendmsg, 980fec5e652STom Herbert .recvmsg = inet_recvmsg, 9811da177e4SLinus Torvalds .mmap = sock_no_mmap, 9821da177e4SLinus Torvalds .sendpage = inet_sendpage, 983543d9cfeSArnaldo Carvalho de Melo #ifdef CONFIG_COMPAT 984543d9cfeSArnaldo Carvalho de Melo .compat_setsockopt = compat_sock_common_setsockopt, 985543d9cfeSArnaldo Carvalho de Melo .compat_getsockopt = compat_sock_common_getsockopt, 986709b46e8SEric W. Biederman .compat_ioctl = inet_compat_ioctl, 987543d9cfeSArnaldo Carvalho de Melo #endif 9881da177e4SLinus Torvalds }; 9891da177e4SLinus Torvalds 990ec1b4cf7SStephen Hemminger static const struct net_proto_family inet_family_ops = { 9911da177e4SLinus Torvalds .family = PF_INET, 9921da177e4SLinus Torvalds .create = inet_create, 9931da177e4SLinus Torvalds .owner = THIS_MODULE, 9941da177e4SLinus Torvalds }; 9951da177e4SLinus Torvalds 9961da177e4SLinus Torvalds /* Upon startup we insert all the elements in inetsw_array[] into 9971da177e4SLinus Torvalds * the linked list inetsw. 9981da177e4SLinus Torvalds */ 9991da177e4SLinus Torvalds static struct inet_protosw inetsw_array[] = 10001da177e4SLinus Torvalds { 10011da177e4SLinus Torvalds { 10021da177e4SLinus Torvalds .type = SOCK_STREAM, 10031da177e4SLinus Torvalds .protocol = IPPROTO_TCP, 10041da177e4SLinus Torvalds .prot = &tcp_prot, 10051da177e4SLinus Torvalds .ops = &inet_stream_ops, 1006d83d8461SArnaldo Carvalho de Melo .flags = INET_PROTOSW_PERMANENT | 1007d83d8461SArnaldo Carvalho de Melo INET_PROTOSW_ICSK, 10081da177e4SLinus Torvalds }, 10091da177e4SLinus Torvalds 10101da177e4SLinus Torvalds { 10111da177e4SLinus Torvalds .type = SOCK_DGRAM, 10121da177e4SLinus Torvalds .protocol = IPPROTO_UDP, 10131da177e4SLinus Torvalds .prot = &udp_prot, 10141da177e4SLinus Torvalds .ops = &inet_dgram_ops, 10151da177e4SLinus Torvalds .flags = INET_PROTOSW_PERMANENT, 10161da177e4SLinus Torvalds }, 10171da177e4SLinus Torvalds 1018c319b4d7SVasiliy Kulikov { 1019c319b4d7SVasiliy Kulikov .type = SOCK_DGRAM, 1020c319b4d7SVasiliy Kulikov .protocol = IPPROTO_ICMP, 1021c319b4d7SVasiliy Kulikov .prot = &ping_prot, 1022c319b4d7SVasiliy Kulikov .ops = &inet_dgram_ops, 1023c319b4d7SVasiliy Kulikov .flags = INET_PROTOSW_REUSE, 1024c319b4d7SVasiliy Kulikov }, 10251da177e4SLinus Torvalds 10261da177e4SLinus Torvalds { 10271da177e4SLinus Torvalds .type = SOCK_RAW, 10281da177e4SLinus Torvalds .protocol = IPPROTO_IP, /* wild card */ 10291da177e4SLinus Torvalds .prot = &raw_prot, 10301da177e4SLinus Torvalds .ops = &inet_sockraw_ops, 10311da177e4SLinus Torvalds .flags = INET_PROTOSW_REUSE, 10321da177e4SLinus Torvalds } 10331da177e4SLinus Torvalds }; 10341da177e4SLinus Torvalds 1035c40f6fffSDenis Cheng #define INETSW_ARRAY_LEN ARRAY_SIZE(inetsw_array) 10361da177e4SLinus Torvalds 10371da177e4SLinus Torvalds void inet_register_protosw(struct inet_protosw *p) 10381da177e4SLinus Torvalds { 10391da177e4SLinus Torvalds struct list_head *lh; 10401da177e4SLinus Torvalds struct inet_protosw *answer; 10411da177e4SLinus Torvalds int protocol = p->protocol; 10421da177e4SLinus Torvalds struct list_head *last_perm; 10431da177e4SLinus Torvalds 10441da177e4SLinus Torvalds spin_lock_bh(&inetsw_lock); 10451da177e4SLinus Torvalds 10461da177e4SLinus Torvalds if (p->type >= SOCK_MAX) 10471da177e4SLinus Torvalds goto out_illegal; 10481da177e4SLinus Torvalds 10491da177e4SLinus Torvalds /* If we are trying to override a permanent protocol, bail. */ 10501da177e4SLinus Torvalds last_perm = &inetsw[p->type]; 10511da177e4SLinus Torvalds list_for_each(lh, &inetsw[p->type]) { 10521da177e4SLinus Torvalds answer = list_entry(lh, struct inet_protosw, list); 10531da177e4SLinus Torvalds /* Check only the non-wild match. */ 1054f6c53334SJunwei Zhang if ((INET_PROTOSW_PERMANENT & answer->flags) == 0) 10551da177e4SLinus Torvalds break; 1056f6c53334SJunwei Zhang if (protocol == answer->protocol) 1057f6c53334SJunwei Zhang goto out_permanent; 10581da177e4SLinus Torvalds last_perm = lh; 10591da177e4SLinus Torvalds } 10601da177e4SLinus Torvalds 10611da177e4SLinus Torvalds /* Add the new entry after the last permanent entry if any, so that 10621da177e4SLinus Torvalds * the new entry does not override a permanent entry when matched with 10631da177e4SLinus Torvalds * a wild-card protocol. But it is allowed to override any existing 10641da177e4SLinus Torvalds * non-permanent entry. This means that when we remove this entry, the 10651da177e4SLinus Torvalds * system automatically returns to the old behavior. 10661da177e4SLinus Torvalds */ 10671da177e4SLinus Torvalds list_add_rcu(&p->list, last_perm); 10681da177e4SLinus Torvalds out: 10691da177e4SLinus Torvalds spin_unlock_bh(&inetsw_lock); 10701da177e4SLinus Torvalds 10711da177e4SLinus Torvalds return; 10721da177e4SLinus Torvalds 10731da177e4SLinus Torvalds out_permanent: 1074058bd4d2SJoe Perches pr_err("Attempt to override permanent protocol %d\n", protocol); 10751da177e4SLinus Torvalds goto out; 10761da177e4SLinus Torvalds 10771da177e4SLinus Torvalds out_illegal: 1078058bd4d2SJoe Perches pr_err("Ignoring attempt to register invalid socket type %d\n", 10791da177e4SLinus Torvalds p->type); 10801da177e4SLinus Torvalds goto out; 10811da177e4SLinus Torvalds } 10823d1427f8SEric Dumazet EXPORT_SYMBOL(inet_register_protosw); 10831da177e4SLinus Torvalds 10841da177e4SLinus Torvalds void inet_unregister_protosw(struct inet_protosw *p) 10851da177e4SLinus Torvalds { 10861da177e4SLinus Torvalds if (INET_PROTOSW_PERMANENT & p->flags) { 1087058bd4d2SJoe Perches pr_err("Attempt to unregister permanent protocol %d\n", 10881da177e4SLinus Torvalds p->protocol); 10891da177e4SLinus Torvalds } else { 10901da177e4SLinus Torvalds spin_lock_bh(&inetsw_lock); 10911da177e4SLinus Torvalds list_del_rcu(&p->list); 10921da177e4SLinus Torvalds spin_unlock_bh(&inetsw_lock); 10931da177e4SLinus Torvalds 10941da177e4SLinus Torvalds synchronize_net(); 10951da177e4SLinus Torvalds } 10961da177e4SLinus Torvalds } 10973d1427f8SEric Dumazet EXPORT_SYMBOL(inet_unregister_protosw); 10981da177e4SLinus Torvalds 109932519f11SArnaldo Carvalho de Melo static int inet_sk_reselect_saddr(struct sock *sk) 110032519f11SArnaldo Carvalho de Melo { 110132519f11SArnaldo Carvalho de Melo struct inet_sock *inet = inet_sk(sk); 1102c720c7e8SEric Dumazet __be32 old_saddr = inet->inet_saddr; 1103c720c7e8SEric Dumazet __be32 daddr = inet->inet_daddr; 11046e869138SDavid S. Miller struct flowi4 *fl4; 1105b23dd4feSDavid S. Miller struct rtable *rt; 1106b23dd4feSDavid S. Miller __be32 new_saddr; 1107f6d8bd05SEric Dumazet struct ip_options_rcu *inet_opt; 110832519f11SArnaldo Carvalho de Melo 1109f6d8bd05SEric Dumazet inet_opt = rcu_dereference_protected(inet->inet_opt, 11101e1d04e6SHannes Frederic Sowa lockdep_sock_is_held(sk)); 1111f6d8bd05SEric Dumazet if (inet_opt && inet_opt->opt.srr) 1112f6d8bd05SEric Dumazet daddr = inet_opt->opt.faddr; 111332519f11SArnaldo Carvalho de Melo 111432519f11SArnaldo Carvalho de Melo /* Query new route. */ 11156e869138SDavid S. Miller fl4 = &inet->cork.fl.u.ip4; 11166e869138SDavid S. Miller rt = ip_route_connect(fl4, daddr, 0, RT_CONN_FLAGS(sk), 1117b23dd4feSDavid S. Miller sk->sk_bound_dev_if, sk->sk_protocol, 11180e0d44abSSteffen Klassert inet->inet_sport, inet->inet_dport, sk); 1119b23dd4feSDavid S. Miller if (IS_ERR(rt)) 1120b23dd4feSDavid S. Miller return PTR_ERR(rt); 112132519f11SArnaldo Carvalho de Melo 1122d8d1f30bSChangli Gao sk_setup_caps(sk, &rt->dst); 112332519f11SArnaldo Carvalho de Melo 11246e869138SDavid S. Miller new_saddr = fl4->saddr; 112532519f11SArnaldo Carvalho de Melo 112632519f11SArnaldo Carvalho de Melo if (new_saddr == old_saddr) 112732519f11SArnaldo Carvalho de Melo return 0; 112832519f11SArnaldo Carvalho de Melo 1129287b7f38SNikolay Borisov if (sock_net(sk)->ipv4.sysctl_ip_dynaddr > 1) { 1130058bd4d2SJoe Perches pr_info("%s(): shifting inet->saddr from %pI4 to %pI4\n", 1131673d57e7SHarvey Harrison __func__, &old_saddr, &new_saddr); 113232519f11SArnaldo Carvalho de Melo } 113332519f11SArnaldo Carvalho de Melo 1134c720c7e8SEric Dumazet inet->inet_saddr = inet->inet_rcv_saddr = new_saddr; 113532519f11SArnaldo Carvalho de Melo 113632519f11SArnaldo Carvalho de Melo /* 113732519f11SArnaldo Carvalho de Melo * XXX The only one ugly spot where we need to 113832519f11SArnaldo Carvalho de Melo * XXX really change the sockets identity after 113932519f11SArnaldo Carvalho de Melo * XXX it has entered the hashes. -DaveM 114032519f11SArnaldo Carvalho de Melo * 114132519f11SArnaldo Carvalho de Melo * Besides that, it does not check for connection 114232519f11SArnaldo Carvalho de Melo * uniqueness. Wait for troubles. 114332519f11SArnaldo Carvalho de Melo */ 1144086c653fSCraig Gallek return __sk_prot_rehash(sk); 114532519f11SArnaldo Carvalho de Melo } 114632519f11SArnaldo Carvalho de Melo 114732519f11SArnaldo Carvalho de Melo int inet_sk_rebuild_header(struct sock *sk) 114832519f11SArnaldo Carvalho de Melo { 114932519f11SArnaldo Carvalho de Melo struct inet_sock *inet = inet_sk(sk); 115032519f11SArnaldo Carvalho de Melo struct rtable *rt = (struct rtable *)__sk_dst_check(sk, 0); 11513ca3c68eSAl Viro __be32 daddr; 1152f6d8bd05SEric Dumazet struct ip_options_rcu *inet_opt; 11536e869138SDavid S. Miller struct flowi4 *fl4; 115432519f11SArnaldo Carvalho de Melo int err; 115532519f11SArnaldo Carvalho de Melo 115632519f11SArnaldo Carvalho de Melo /* Route is OK, nothing to do. */ 115732519f11SArnaldo Carvalho de Melo if (rt) 115832519f11SArnaldo Carvalho de Melo return 0; 115932519f11SArnaldo Carvalho de Melo 116032519f11SArnaldo Carvalho de Melo /* Reroute. */ 1161f6d8bd05SEric Dumazet rcu_read_lock(); 1162f6d8bd05SEric Dumazet inet_opt = rcu_dereference(inet->inet_opt); 1163c720c7e8SEric Dumazet daddr = inet->inet_daddr; 1164f6d8bd05SEric Dumazet if (inet_opt && inet_opt->opt.srr) 1165f6d8bd05SEric Dumazet daddr = inet_opt->opt.faddr; 1166f6d8bd05SEric Dumazet rcu_read_unlock(); 11676e869138SDavid S. Miller fl4 = &inet->cork.fl.u.ip4; 11686e869138SDavid S. Miller rt = ip_route_output_ports(sock_net(sk), fl4, sk, daddr, inet->inet_saddr, 116978fbfd8aSDavid S. Miller inet->inet_dport, inet->inet_sport, 117078fbfd8aSDavid S. Miller sk->sk_protocol, RT_CONN_FLAGS(sk), 117178fbfd8aSDavid S. Miller sk->sk_bound_dev_if); 1172b23dd4feSDavid S. Miller if (!IS_ERR(rt)) { 1173b23dd4feSDavid S. Miller err = 0; 1174d8d1f30bSChangli Gao sk_setup_caps(sk, &rt->dst); 1175b23dd4feSDavid S. Miller } else { 1176b23dd4feSDavid S. Miller err = PTR_ERR(rt); 1177b23dd4feSDavid S. Miller 117832519f11SArnaldo Carvalho de Melo /* Routing failed... */ 117932519f11SArnaldo Carvalho de Melo sk->sk_route_caps = 0; 118032519f11SArnaldo Carvalho de Melo /* 118132519f11SArnaldo Carvalho de Melo * Other protocols have to map its equivalent state to TCP_SYN_SENT. 118232519f11SArnaldo Carvalho de Melo * DCCP maps its DCCP_REQUESTING state to TCP_SYN_SENT. -acme 118332519f11SArnaldo Carvalho de Melo */ 1184287b7f38SNikolay Borisov if (!sock_net(sk)->ipv4.sysctl_ip_dynaddr || 118532519f11SArnaldo Carvalho de Melo sk->sk_state != TCP_SYN_SENT || 118632519f11SArnaldo Carvalho de Melo (sk->sk_userlocks & SOCK_BINDADDR_LOCK) || 118732519f11SArnaldo Carvalho de Melo (err = inet_sk_reselect_saddr(sk)) != 0) 118832519f11SArnaldo Carvalho de Melo sk->sk_err_soft = -err; 118932519f11SArnaldo Carvalho de Melo } 119032519f11SArnaldo Carvalho de Melo 119132519f11SArnaldo Carvalho de Melo return err; 119232519f11SArnaldo Carvalho de Melo } 119332519f11SArnaldo Carvalho de Melo EXPORT_SYMBOL(inet_sk_rebuild_header); 119432519f11SArnaldo Carvalho de Melo 1195b8921ca8STom Herbert struct sk_buff *inet_gso_segment(struct sk_buff *skb, 1196c8f44affSMichał Mirosław netdev_features_t features) 1197f4c50d99SHerbert Xu { 1198cbc53e08SAlexander Duyck bool udpfrag = false, fixedid = false, encap; 1199f4c50d99SHerbert Xu struct sk_buff *segs = ERR_PTR(-EINVAL); 1200bca49f84SVlad Yasevich const struct net_offload *ops; 12013347c960SEric Dumazet unsigned int offset = 0; 1202f9242b6bSDavid S. Miller struct iphdr *iph; 1203802ab55aSAlexander Duyck int proto, tot_len; 12043347c960SEric Dumazet int nhoff; 1205f4c50d99SHerbert Xu int ihl; 1206f4c50d99SHerbert Xu int id; 1207f4c50d99SHerbert Xu 12083347c960SEric Dumazet skb_reset_network_header(skb); 12093347c960SEric Dumazet nhoff = skb_network_header(skb) - skb_mac_header(skb); 1210bbcf467dSHerbert Xu if (unlikely(!pskb_may_pull(skb, sizeof(*iph)))) 1211f4c50d99SHerbert Xu goto out; 1212f4c50d99SHerbert Xu 1213eddc9ec5SArnaldo Carvalho de Melo iph = ip_hdr(skb); 1214f4c50d99SHerbert Xu ihl = iph->ihl * 4; 1215f4c50d99SHerbert Xu if (ihl < sizeof(*iph)) 1216f4c50d99SHerbert Xu goto out; 1217f4c50d99SHerbert Xu 121847d27aadSEric Dumazet id = ntohs(iph->id); 121947d27aadSEric Dumazet proto = iph->protocol; 122047d27aadSEric Dumazet 122147d27aadSEric Dumazet /* Warning: after this point, iph might be no longer valid */ 1222bbcf467dSHerbert Xu if (unlikely(!pskb_may_pull(skb, ihl))) 1223f4c50d99SHerbert Xu goto out; 122447d27aadSEric Dumazet __skb_pull(skb, ihl); 1225f4c50d99SHerbert Xu 12268c3a897bSEric Dumazet encap = SKB_GSO_CB(skb)->encap_level > 0; 12278c3a897bSEric Dumazet if (encap) 12281e16aa3dSFlorian Westphal features &= skb->dev->hw_enc_features; 12293347c960SEric Dumazet SKB_GSO_CB(skb)->encap_level += ihl; 123073136267SPravin B Shelar 1231badff6d0SArnaldo Carvalho de Melo skb_reset_transport_header(skb); 123247d27aadSEric Dumazet 1233f4c50d99SHerbert Xu segs = ERR_PTR(-EPROTONOSUPPORT); 1234f4c50d99SHerbert Xu 1235cbc53e08SAlexander Duyck if (!skb->encapsulation || encap) { 1236cbc53e08SAlexander Duyck udpfrag = !!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP); 1237cbc53e08SAlexander Duyck fixedid = !!(skb_shinfo(skb)->gso_type & SKB_GSO_TCP_FIXEDID); 1238cbc53e08SAlexander Duyck 1239cbc53e08SAlexander Duyck /* fixed ID is invalid if DF bit is not set */ 1240cbc53e08SAlexander Duyck if (fixedid && !(iph->frag_off & htons(IP_DF))) 1241cbc53e08SAlexander Duyck goto out; 1242cbc53e08SAlexander Duyck } 1243dcd60771SEric Dumazet 1244bca49f84SVlad Yasevich ops = rcu_dereference(inet_offloads[proto]); 1245f191a1d1SVlad Yasevich if (likely(ops && ops->callbacks.gso_segment)) 1246f191a1d1SVlad Yasevich segs = ops->callbacks.gso_segment(skb, features); 1247f4c50d99SHerbert Xu 124850c3a487SYOSHIFUJI Hideaki / 吉藤英明 if (IS_ERR_OR_NULL(segs)) 1249f4c50d99SHerbert Xu goto out; 1250f4c50d99SHerbert Xu 1251f4c50d99SHerbert Xu skb = segs; 1252f4c50d99SHerbert Xu do { 12533347c960SEric Dumazet iph = (struct iphdr *)(skb_mac_header(skb) + nhoff); 12548c3a897bSEric Dumazet if (udpfrag) { 1255d7ca4cc0SSridhar Samudrala iph->frag_off = htons(offset >> 3); 125600db4124SIan Morris if (skb->next) 1257d7ca4cc0SSridhar Samudrala iph->frag_off |= htons(IP_MF); 12583347c960SEric Dumazet offset += skb->len - nhoff - ihl; 1259802ab55aSAlexander Duyck tot_len = skb->len - nhoff; 1260802ab55aSAlexander Duyck } else if (skb_is_gso(skb)) { 1261802ab55aSAlexander Duyck if (!fixedid) { 1262802ab55aSAlexander Duyck iph->id = htons(id); 1263802ab55aSAlexander Duyck id += skb_shinfo(skb)->gso_segs; 1264490ab081SPravin B Shelar } 1265802ab55aSAlexander Duyck tot_len = skb_shinfo(skb)->gso_size + 1266802ab55aSAlexander Duyck SKB_GSO_CB(skb)->data_offset + 1267802ab55aSAlexander Duyck skb->head - (unsigned char *)iph; 1268802ab55aSAlexander Duyck } else { 1269802ab55aSAlexander Duyck if (!fixedid) 1270802ab55aSAlexander Duyck iph->id = htons(id++); 1271802ab55aSAlexander Duyck tot_len = skb->len - nhoff; 1272802ab55aSAlexander Duyck } 1273802ab55aSAlexander Duyck iph->tot_len = htons(tot_len); 127447d27aadSEric Dumazet ip_send_check(iph); 12758c3a897bSEric Dumazet if (encap) 12763347c960SEric Dumazet skb_reset_inner_headers(skb); 12773347c960SEric Dumazet skb->network_header = (u8 *)iph - skb->head; 1278f4c50d99SHerbert Xu } while ((skb = skb->next)); 1279f4c50d99SHerbert Xu 1280f4c50d99SHerbert Xu out: 1281f4c50d99SHerbert Xu return segs; 1282f4c50d99SHerbert Xu } 1283b8921ca8STom Herbert EXPORT_SYMBOL(inet_gso_segment); 1284f4c50d99SHerbert Xu 1285b8921ca8STom Herbert struct sk_buff **inet_gro_receive(struct sk_buff **head, struct sk_buff *skb) 128673cc19f1SHerbert Xu { 1287bca49f84SVlad Yasevich const struct net_offload *ops; 128873cc19f1SHerbert Xu struct sk_buff **pp = NULL; 128973cc19f1SHerbert Xu struct sk_buff *p; 1290b71d1d42SEric Dumazet const struct iphdr *iph; 1291a5b1cf28SHerbert Xu unsigned int hlen; 1292a5b1cf28SHerbert Xu unsigned int off; 12931075f3f6SHerbert Xu unsigned int id; 129473cc19f1SHerbert Xu int flush = 1; 129573cc19f1SHerbert Xu int proto; 129673cc19f1SHerbert Xu 1297a5b1cf28SHerbert Xu off = skb_gro_offset(skb); 1298a5b1cf28SHerbert Xu hlen = off + sizeof(*iph); 1299a5b1cf28SHerbert Xu iph = skb_gro_header_fast(skb, off); 1300a5b1cf28SHerbert Xu if (skb_gro_header_hard(skb, hlen)) { 1301a5b1cf28SHerbert Xu iph = skb_gro_header_slow(skb, hlen, off); 130286911732SHerbert Xu if (unlikely(!iph)) 130373cc19f1SHerbert Xu goto out; 1304a5b1cf28SHerbert Xu } 130573cc19f1SHerbert Xu 1306f9242b6bSDavid S. Miller proto = iph->protocol; 130773cc19f1SHerbert Xu 130873cc19f1SHerbert Xu rcu_read_lock(); 1309bca49f84SVlad Yasevich ops = rcu_dereference(inet_offloads[proto]); 1310f191a1d1SVlad Yasevich if (!ops || !ops->callbacks.gro_receive) 131173cc19f1SHerbert Xu goto out_unlock; 131273cc19f1SHerbert Xu 1313a5ad24beSHerbert Xu if (*(u8 *)iph != 0x45) 131473cc19f1SHerbert Xu goto out_unlock; 131573cc19f1SHerbert Xu 1316a9e050f4SEric Dumazet if (unlikely(ip_fast_csum((u8 *)iph, 5))) 131773cc19f1SHerbert Xu goto out_unlock; 131873cc19f1SHerbert Xu 13190eae88f3SEric Dumazet id = ntohl(*(__be32 *)&iph->id); 1320db8caf3dSEric Dumazet flush = (u16)((ntohl(*(__be32 *)iph) ^ skb_gro_len(skb)) | (id & ~IP_DF)); 13211075f3f6SHerbert Xu id >>= 16; 132273cc19f1SHerbert Xu 132373cc19f1SHerbert Xu for (p = *head; p; p = p->next) { 132473cc19f1SHerbert Xu struct iphdr *iph2; 13251530545eSAlexander Duyck u16 flush_id; 132673cc19f1SHerbert Xu 132773cc19f1SHerbert Xu if (!NAPI_GRO_CB(p)->same_flow) 132873cc19f1SHerbert Xu continue; 132973cc19f1SHerbert Xu 1330299603e8SJerry Chu iph2 = (struct iphdr *)(p->data + off); 1331299603e8SJerry Chu /* The above works because, with the exception of the top 1332299603e8SJerry Chu * (inner most) layer, we only aggregate pkts with the same 1333299603e8SJerry Chu * hdr length so all the hdrs we'll need to verify will start 1334299603e8SJerry Chu * at the same offset. 1335299603e8SJerry Chu */ 1336a5ad24beSHerbert Xu if ((iph->protocol ^ iph2->protocol) | 13370eae88f3SEric Dumazet ((__force u32)iph->saddr ^ (__force u32)iph2->saddr) | 13380eae88f3SEric Dumazet ((__force u32)iph->daddr ^ (__force u32)iph2->daddr)) { 133973cc19f1SHerbert Xu NAPI_GRO_CB(p)->same_flow = 0; 134073cc19f1SHerbert Xu continue; 134173cc19f1SHerbert Xu } 134273cc19f1SHerbert Xu 134373cc19f1SHerbert Xu /* All fields must match except length and checksum. */ 134473cc19f1SHerbert Xu NAPI_GRO_CB(p)->flush |= 1345a5ad24beSHerbert Xu (iph->ttl ^ iph2->ttl) | 1346a9e050f4SEric Dumazet (iph->tos ^ iph2->tos) | 1347bf5a755fSJerry Chu ((iph->frag_off ^ iph2->frag_off) & htons(IP_DF)); 134873cc19f1SHerbert Xu 134973cc19f1SHerbert Xu NAPI_GRO_CB(p)->flush |= flush; 13501530545eSAlexander Duyck 13511530545eSAlexander Duyck /* We need to store of the IP ID check to be included later 13521530545eSAlexander Duyck * when we can verify that this packet does in fact belong 13531530545eSAlexander Duyck * to a given flow. 13541530545eSAlexander Duyck */ 13551530545eSAlexander Duyck flush_id = (u16)(id - ntohs(iph2->id)); 13561530545eSAlexander Duyck 13571530545eSAlexander Duyck /* This bit of code makes it much easier for us to identify 13581530545eSAlexander Duyck * the cases where we are doing atomic vs non-atomic IP ID 13591530545eSAlexander Duyck * checks. Specifically an atomic check can return IP ID 13601530545eSAlexander Duyck * values 0 - 0xFFFF, while a non-atomic check can only 13611530545eSAlexander Duyck * return 0 or 0xFFFF. 13621530545eSAlexander Duyck */ 13631530545eSAlexander Duyck if (!NAPI_GRO_CB(p)->is_atomic || 13641530545eSAlexander Duyck !(iph->frag_off & htons(IP_DF))) { 13651530545eSAlexander Duyck flush_id ^= NAPI_GRO_CB(p)->count; 13661530545eSAlexander Duyck flush_id = flush_id ? 0xFFFF : 0; 136773cc19f1SHerbert Xu } 136873cc19f1SHerbert Xu 13691530545eSAlexander Duyck /* If the previous IP ID value was based on an atomic 13701530545eSAlexander Duyck * datagram we can overwrite the value and ignore it. 13711530545eSAlexander Duyck */ 13721530545eSAlexander Duyck if (NAPI_GRO_CB(skb)->is_atomic) 13731530545eSAlexander Duyck NAPI_GRO_CB(p)->flush_id = flush_id; 13741530545eSAlexander Duyck else 13751530545eSAlexander Duyck NAPI_GRO_CB(p)->flush_id |= flush_id; 13761530545eSAlexander Duyck } 13771530545eSAlexander Duyck 13781530545eSAlexander Duyck NAPI_GRO_CB(skb)->is_atomic = !!(iph->frag_off & htons(IP_DF)); 137973cc19f1SHerbert Xu NAPI_GRO_CB(skb)->flush |= flush; 1380299603e8SJerry Chu skb_set_network_header(skb, off); 1381299603e8SJerry Chu /* The above will be needed by the transport layer if there is one 1382299603e8SJerry Chu * immediately following this IP hdr. 1383299603e8SJerry Chu */ 1384299603e8SJerry Chu 13852c804d0fSEric Dumazet /* Note : No need to call skb_gro_postpull_rcsum() here, 13862c804d0fSEric Dumazet * as we already checked checksum over ipv4 header was 0 13872c804d0fSEric Dumazet */ 138886911732SHerbert Xu skb_gro_pull(skb, sizeof(*iph)); 138986911732SHerbert Xu skb_set_transport_header(skb, skb_gro_offset(skb)); 139073cc19f1SHerbert Xu 1391f191a1d1SVlad Yasevich pp = ops->callbacks.gro_receive(head, skb); 139273cc19f1SHerbert Xu 139373cc19f1SHerbert Xu out_unlock: 139473cc19f1SHerbert Xu rcu_read_unlock(); 139573cc19f1SHerbert Xu 139673cc19f1SHerbert Xu out: 139773cc19f1SHerbert Xu NAPI_GRO_CB(skb)->flush |= flush; 139873cc19f1SHerbert Xu 139973cc19f1SHerbert Xu return pp; 140073cc19f1SHerbert Xu } 1401b8921ca8STom Herbert EXPORT_SYMBOL(inet_gro_receive); 140273cc19f1SHerbert Xu 1403fac8e0f5SJesse Gross static struct sk_buff **ipip_gro_receive(struct sk_buff **head, 1404fac8e0f5SJesse Gross struct sk_buff *skb) 1405fac8e0f5SJesse Gross { 1406fac8e0f5SJesse Gross if (NAPI_GRO_CB(skb)->encap_mark) { 1407fac8e0f5SJesse Gross NAPI_GRO_CB(skb)->flush = 1; 1408fac8e0f5SJesse Gross return NULL; 1409fac8e0f5SJesse Gross } 1410fac8e0f5SJesse Gross 1411fac8e0f5SJesse Gross NAPI_GRO_CB(skb)->encap_mark = 1; 1412fac8e0f5SJesse Gross 1413fac8e0f5SJesse Gross return inet_gro_receive(head, skb); 1414fac8e0f5SJesse Gross } 1415fac8e0f5SJesse Gross 1416822c8685SDeepa Dinamani #define SECONDS_PER_DAY 86400 1417822c8685SDeepa Dinamani 1418822c8685SDeepa Dinamani /* inet_current_timestamp - Return IP network timestamp 1419822c8685SDeepa Dinamani * 1420822c8685SDeepa Dinamani * Return milliseconds since midnight in network byte order. 1421822c8685SDeepa Dinamani */ 1422822c8685SDeepa Dinamani __be32 inet_current_timestamp(void) 1423822c8685SDeepa Dinamani { 1424822c8685SDeepa Dinamani u32 secs; 1425822c8685SDeepa Dinamani u32 msecs; 1426822c8685SDeepa Dinamani struct timespec64 ts; 1427822c8685SDeepa Dinamani 1428822c8685SDeepa Dinamani ktime_get_real_ts64(&ts); 1429822c8685SDeepa Dinamani 1430822c8685SDeepa Dinamani /* Get secs since midnight. */ 1431822c8685SDeepa Dinamani (void)div_u64_rem(ts.tv_sec, SECONDS_PER_DAY, &secs); 1432822c8685SDeepa Dinamani /* Convert to msecs. */ 1433822c8685SDeepa Dinamani msecs = secs * MSEC_PER_SEC; 1434822c8685SDeepa Dinamani /* Convert nsec to msec. */ 1435822c8685SDeepa Dinamani msecs += (u32)ts.tv_nsec / NSEC_PER_MSEC; 1436822c8685SDeepa Dinamani 1437822c8685SDeepa Dinamani /* Convert to network byte order. */ 14383ba9d300SDeepa Dinamani return htonl(msecs); 1439822c8685SDeepa Dinamani } 1440822c8685SDeepa Dinamani EXPORT_SYMBOL(inet_current_timestamp); 1441822c8685SDeepa Dinamani 1442f4713a3dSWillem de Bruijn int inet_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len) 1443f4713a3dSWillem de Bruijn { 1444f4713a3dSWillem de Bruijn if (sk->sk_family == AF_INET) 1445f4713a3dSWillem de Bruijn return ip_recv_error(sk, msg, len, addr_len); 1446f4713a3dSWillem de Bruijn #if IS_ENABLED(CONFIG_IPV6) 1447f4713a3dSWillem de Bruijn if (sk->sk_family == AF_INET6) 1448f4713a3dSWillem de Bruijn return pingv6_ops.ipv6_recv_error(sk, msg, len, addr_len); 1449f4713a3dSWillem de Bruijn #endif 1450f4713a3dSWillem de Bruijn return -EINVAL; 1451f4713a3dSWillem de Bruijn } 1452f4713a3dSWillem de Bruijn 1453b8921ca8STom Herbert int inet_gro_complete(struct sk_buff *skb, int nhoff) 145473cc19f1SHerbert Xu { 1455299603e8SJerry Chu __be16 newlen = htons(skb->len - nhoff); 1456299603e8SJerry Chu struct iphdr *iph = (struct iphdr *)(skb->data + nhoff); 1457bca49f84SVlad Yasevich const struct net_offload *ops; 1458f9242b6bSDavid S. Miller int proto = iph->protocol; 1459f9242b6bSDavid S. Miller int err = -ENOSYS; 146073cc19f1SHerbert Xu 1461c3caf119SJerry Chu if (skb->encapsulation) 1462c3caf119SJerry Chu skb_set_inner_network_header(skb, nhoff); 1463c3caf119SJerry Chu 146473cc19f1SHerbert Xu csum_replace2(&iph->check, iph->tot_len, newlen); 146573cc19f1SHerbert Xu iph->tot_len = newlen; 146673cc19f1SHerbert Xu 146773cc19f1SHerbert Xu rcu_read_lock(); 1468bca49f84SVlad Yasevich ops = rcu_dereference(inet_offloads[proto]); 1469f191a1d1SVlad Yasevich if (WARN_ON(!ops || !ops->callbacks.gro_complete)) 147073cc19f1SHerbert Xu goto out_unlock; 147173cc19f1SHerbert Xu 1472299603e8SJerry Chu /* Only need to add sizeof(*iph) to get to the next hdr below 1473299603e8SJerry Chu * because any hdr with option will have been flushed in 1474299603e8SJerry Chu * inet_gro_receive(). 1475299603e8SJerry Chu */ 1476299603e8SJerry Chu err = ops->callbacks.gro_complete(skb, nhoff + sizeof(*iph)); 147773cc19f1SHerbert Xu 147873cc19f1SHerbert Xu out_unlock: 147973cc19f1SHerbert Xu rcu_read_unlock(); 148073cc19f1SHerbert Xu 148173cc19f1SHerbert Xu return err; 148273cc19f1SHerbert Xu } 1483b8921ca8STom Herbert EXPORT_SYMBOL(inet_gro_complete); 148473cc19f1SHerbert Xu 1485b8cba75bSJesse Gross static int ipip_gro_complete(struct sk_buff *skb, int nhoff) 1486b8cba75bSJesse Gross { 1487b8cba75bSJesse Gross skb->encapsulation = 1; 14887e13318dSTom Herbert skb_shinfo(skb)->gso_type |= SKB_GSO_IPXIP4; 1489b8cba75bSJesse Gross return inet_gro_complete(skb, nhoff); 1490b8cba75bSJesse Gross } 1491b8cba75bSJesse Gross 1492eee4fe4dSDenis V. Lunev int inet_ctl_sock_create(struct sock **sk, unsigned short family, 14935677242fSDenis V. Lunev unsigned short type, unsigned char protocol, 14945677242fSDenis V. Lunev struct net *net) 14953d58b5faSDenis V. Lunev { 1496eee4fe4dSDenis V. Lunev struct socket *sock; 149726abe143SEric W. Biederman int rc = sock_create_kern(net, family, type, protocol, &sock); 14983d58b5faSDenis V. Lunev 14993d58b5faSDenis V. Lunev if (rc == 0) { 1500eee4fe4dSDenis V. Lunev *sk = sock->sk; 1501eee4fe4dSDenis V. Lunev (*sk)->sk_allocation = GFP_ATOMIC; 15023d58b5faSDenis V. Lunev /* 15033d58b5faSDenis V. Lunev * Unhash it so that IP input processing does not even see it, 15043d58b5faSDenis V. Lunev * we do not wish this socket to see incoming packets. 15053d58b5faSDenis V. Lunev */ 1506eee4fe4dSDenis V. Lunev (*sk)->sk_prot->unhash(*sk); 15073d58b5faSDenis V. Lunev } 15083d58b5faSDenis V. Lunev return rc; 15093d58b5faSDenis V. Lunev } 15103d58b5faSDenis V. Lunev EXPORT_SYMBOL_GPL(inet_ctl_sock_create); 15113d58b5faSDenis V. Lunev 1512c4c6bc31SRaghavendra K T u64 snmp_get_cpu_field(void __percpu *mib, int cpu, int offt) 1513c4c6bc31SRaghavendra K T { 1514c4c6bc31SRaghavendra K T return *(((unsigned long *)per_cpu_ptr(mib, cpu)) + offt); 1515c4c6bc31SRaghavendra K T } 1516c4c6bc31SRaghavendra K T EXPORT_SYMBOL_GPL(snmp_get_cpu_field); 1517c4c6bc31SRaghavendra K T 1518698365faSWANG Cong unsigned long snmp_fold_field(void __percpu *mib, int offt) 15195e0f0435SHerbert Xu { 15205e0f0435SHerbert Xu unsigned long res = 0; 1521698365faSWANG Cong int i; 15225e0f0435SHerbert Xu 1523698365faSWANG Cong for_each_possible_cpu(i) 1524c4c6bc31SRaghavendra K T res += snmp_get_cpu_field(mib, i, offt); 15255e0f0435SHerbert Xu return res; 15265e0f0435SHerbert Xu } 15275e0f0435SHerbert Xu EXPORT_SYMBOL_GPL(snmp_fold_field); 15285e0f0435SHerbert Xu 15294ce3c183SEric Dumazet #if BITS_PER_LONG==32 15304ce3c183SEric Dumazet 153180ec1927SDavid S. Miller u64 snmp_get_cpu_field64(void __percpu *mib, int cpu, int offt, 1532c4c6bc31SRaghavendra K T size_t syncp_offset) 15334ce3c183SEric Dumazet { 15348f0ea0feSEric Dumazet void *bhptr; 15354ce3c183SEric Dumazet struct u64_stats_sync *syncp; 15368f0ea0feSEric Dumazet u64 v; 15374ce3c183SEric Dumazet unsigned int start; 15384ce3c183SEric Dumazet 1539698365faSWANG Cong bhptr = per_cpu_ptr(mib, cpu); 15404ce3c183SEric Dumazet syncp = (struct u64_stats_sync *)(bhptr + syncp_offset); 15414ce3c183SEric Dumazet do { 154257a7744eSEric W. Biederman start = u64_stats_fetch_begin_irq(syncp); 15438f0ea0feSEric Dumazet v = *(((u64 *)bhptr) + offt); 154457a7744eSEric W. Biederman } while (u64_stats_fetch_retry_irq(syncp, start)); 15454ce3c183SEric Dumazet 1546c4c6bc31SRaghavendra K T return v; 1547c4c6bc31SRaghavendra K T } 1548c4c6bc31SRaghavendra K T EXPORT_SYMBOL_GPL(snmp_get_cpu_field64); 1549c4c6bc31SRaghavendra K T 1550c4c6bc31SRaghavendra K T u64 snmp_fold_field64(void __percpu *mib, int offt, size_t syncp_offset) 1551c4c6bc31SRaghavendra K T { 1552c4c6bc31SRaghavendra K T u64 res = 0; 1553c4c6bc31SRaghavendra K T int cpu; 1554c4c6bc31SRaghavendra K T 1555c4c6bc31SRaghavendra K T for_each_possible_cpu(cpu) { 1556d1bfc625SMadalin Bucur res += snmp_get_cpu_field64(mib, cpu, offt, syncp_offset); 15574ce3c183SEric Dumazet } 15584ce3c183SEric Dumazet return res; 15594ce3c183SEric Dumazet } 15604ce3c183SEric Dumazet EXPORT_SYMBOL_GPL(snmp_fold_field64); 15614ce3c183SEric Dumazet #endif 15624ce3c183SEric Dumazet 15631da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 156432613090SAlexey Dobriyan static const struct net_protocol igmp_protocol = { 15651da177e4SLinus Torvalds .handler = igmp_rcv, 1566b4ee07dfSAlexey Dobriyan .netns_ok = 1, 15671da177e4SLinus Torvalds }; 15681da177e4SLinus Torvalds #endif 15691da177e4SLinus Torvalds 157032613090SAlexey Dobriyan static const struct net_protocol tcp_protocol = { 157141063e9dSDavid S. Miller .early_demux = tcp_v4_early_demux, 15721da177e4SLinus Torvalds .handler = tcp_v4_rcv, 15731da177e4SLinus Torvalds .err_handler = tcp_v4_err, 15741da177e4SLinus Torvalds .no_policy = 1, 157592f1fecbSDenis V. Lunev .netns_ok = 1, 15768ed1dc44SHannes Frederic Sowa .icmp_strict_tag_validation = 1, 15771da177e4SLinus Torvalds }; 15781da177e4SLinus Torvalds 157932613090SAlexey Dobriyan static const struct net_protocol udp_protocol = { 1580421b3885SShawn Bohrer .early_demux = udp_v4_early_demux, 15811da177e4SLinus Torvalds .handler = udp_rcv, 15821da177e4SLinus Torvalds .err_handler = udp_err, 15831da177e4SLinus Torvalds .no_policy = 1, 158492f1fecbSDenis V. Lunev .netns_ok = 1, 15851da177e4SLinus Torvalds }; 15861da177e4SLinus Torvalds 158732613090SAlexey Dobriyan static const struct net_protocol icmp_protocol = { 15881da177e4SLinus Torvalds .handler = icmp_rcv, 15895b052042SLi Wei .err_handler = icmp_err, 15908b7817f3SHerbert Xu .no_policy = 1, 159192f1fecbSDenis V. Lunev .netns_ok = 1, 15921da177e4SLinus Torvalds }; 15931da177e4SLinus Torvalds 15949b4661bdSPavel Emelyanov static __net_init int ipv4_mib_init_net(struct net *net) 15959b4661bdSPavel Emelyanov { 1596827da44cSJohn Stultz int i; 1597827da44cSJohn Stultz 1598698365faSWANG Cong net->mib.tcp_statistics = alloc_percpu(struct tcp_mib); 1599698365faSWANG Cong if (!net->mib.tcp_statistics) 160057ef42d5SPavel Emelyanov goto err_tcp_mib; 1601698365faSWANG Cong net->mib.ip_statistics = alloc_percpu(struct ipstats_mib); 1602698365faSWANG Cong if (!net->mib.ip_statistics) 1603a20f5799SPavel Emelyanov goto err_ip_mib; 1604827da44cSJohn Stultz 1605827da44cSJohn Stultz for_each_possible_cpu(i) { 1606827da44cSJohn Stultz struct ipstats_mib *af_inet_stats; 1607698365faSWANG Cong af_inet_stats = per_cpu_ptr(net->mib.ip_statistics, i); 1608827da44cSJohn Stultz u64_stats_init(&af_inet_stats->syncp); 1609827da44cSJohn Stultz } 1610827da44cSJohn Stultz 1611698365faSWANG Cong net->mib.net_statistics = alloc_percpu(struct linux_mib); 1612698365faSWANG Cong if (!net->mib.net_statistics) 161361a7e260SPavel Emelyanov goto err_net_mib; 1614698365faSWANG Cong net->mib.udp_statistics = alloc_percpu(struct udp_mib); 1615698365faSWANG Cong if (!net->mib.udp_statistics) 16162f275f91SPavel Emelyanov goto err_udp_mib; 1617698365faSWANG Cong net->mib.udplite_statistics = alloc_percpu(struct udp_mib); 1618698365faSWANG Cong if (!net->mib.udplite_statistics) 1619386019d3SPavel Emelyanov goto err_udplite_mib; 1620698365faSWANG Cong net->mib.icmp_statistics = alloc_percpu(struct icmp_mib); 1621698365faSWANG Cong if (!net->mib.icmp_statistics) 1622b60538a0SPavel Emelyanov goto err_icmp_mib; 1623acb32ba3SEric Dumazet net->mib.icmpmsg_statistics = kzalloc(sizeof(struct icmpmsg_mib), 1624acb32ba3SEric Dumazet GFP_KERNEL); 1625acb32ba3SEric Dumazet if (!net->mib.icmpmsg_statistics) 1626923c6586SPavel Emelyanov goto err_icmpmsg_mib; 162757ef42d5SPavel Emelyanov 162857ef42d5SPavel Emelyanov tcp_mib_init(net); 16299b4661bdSPavel Emelyanov return 0; 163057ef42d5SPavel Emelyanov 1631923c6586SPavel Emelyanov err_icmpmsg_mib: 1632698365faSWANG Cong free_percpu(net->mib.icmp_statistics); 1633b60538a0SPavel Emelyanov err_icmp_mib: 1634698365faSWANG Cong free_percpu(net->mib.udplite_statistics); 1635386019d3SPavel Emelyanov err_udplite_mib: 1636698365faSWANG Cong free_percpu(net->mib.udp_statistics); 16372f275f91SPavel Emelyanov err_udp_mib: 1638698365faSWANG Cong free_percpu(net->mib.net_statistics); 163961a7e260SPavel Emelyanov err_net_mib: 1640698365faSWANG Cong free_percpu(net->mib.ip_statistics); 1641a20f5799SPavel Emelyanov err_ip_mib: 1642698365faSWANG Cong free_percpu(net->mib.tcp_statistics); 164357ef42d5SPavel Emelyanov err_tcp_mib: 164457ef42d5SPavel Emelyanov return -ENOMEM; 16459b4661bdSPavel Emelyanov } 16469b4661bdSPavel Emelyanov 16479b4661bdSPavel Emelyanov static __net_exit void ipv4_mib_exit_net(struct net *net) 16489b4661bdSPavel Emelyanov { 1649acb32ba3SEric Dumazet kfree(net->mib.icmpmsg_statistics); 1650698365faSWANG Cong free_percpu(net->mib.icmp_statistics); 1651698365faSWANG Cong free_percpu(net->mib.udplite_statistics); 1652698365faSWANG Cong free_percpu(net->mib.udp_statistics); 1653698365faSWANG Cong free_percpu(net->mib.net_statistics); 1654698365faSWANG Cong free_percpu(net->mib.ip_statistics); 1655698365faSWANG Cong free_percpu(net->mib.tcp_statistics); 16569b4661bdSPavel Emelyanov } 16579b4661bdSPavel Emelyanov 16589b4661bdSPavel Emelyanov static __net_initdata struct pernet_operations ipv4_mib_ops = { 16599b4661bdSPavel Emelyanov .init = ipv4_mib_init_net, 16609b4661bdSPavel Emelyanov .exit = ipv4_mib_exit_net, 16619b4661bdSPavel Emelyanov }; 16629b4661bdSPavel Emelyanov 16631da177e4SLinus Torvalds static int __init init_ipv4_mibs(void) 16641da177e4SLinus Torvalds { 1665d89cbbb1SPavel Emelyanov return register_pernet_subsys(&ipv4_mib_ops); 16661da177e4SLinus Torvalds } 16671da177e4SLinus Torvalds 1668c9d8f1a6SCong Wang static __net_init int inet_init_net(struct net *net) 1669c9d8f1a6SCong Wang { 1670c9d8f1a6SCong Wang /* 1671c9d8f1a6SCong Wang * Set defaults for local port range 1672c9d8f1a6SCong Wang */ 1673c9d8f1a6SCong Wang seqlock_init(&net->ipv4.ip_local_ports.lock); 1674c9d8f1a6SCong Wang net->ipv4.ip_local_ports.range[0] = 32768; 167507f4c900SEric Dumazet net->ipv4.ip_local_ports.range[1] = 60999; 1676ba6b918aSCong Wang 1677ba6b918aSCong Wang seqlock_init(&net->ipv4.ping_group_range.lock); 1678ba6b918aSCong Wang /* 1679ba6b918aSCong Wang * Sane defaults - nobody may create ping sockets. 1680ba6b918aSCong Wang * Boot scripts should set this to distro-specific group. 1681ba6b918aSCong Wang */ 1682ba6b918aSCong Wang net->ipv4.ping_group_range.range[0] = make_kgid(&init_user_ns, 1); 1683ba6b918aSCong Wang net->ipv4.ping_group_range.range[1] = make_kgid(&init_user_ns, 0); 1684049bbf58SEzequiel Garcia 1685049bbf58SEzequiel Garcia /* Default values for sysctl-controlled parameters. 1686049bbf58SEzequiel Garcia * We set them here, in case sysctl is not compiled. 1687049bbf58SEzequiel Garcia */ 1688049bbf58SEzequiel Garcia net->ipv4.sysctl_ip_default_ttl = IPDEFTTL; 1689049bbf58SEzequiel Garcia net->ipv4.sysctl_ip_dynaddr = 0; 1690049bbf58SEzequiel Garcia net->ipv4.sysctl_ip_early_demux = 1; 1691049bbf58SEzequiel Garcia 1692c9d8f1a6SCong Wang return 0; 1693c9d8f1a6SCong Wang } 1694c9d8f1a6SCong Wang 1695c9d8f1a6SCong Wang static __net_exit void inet_exit_net(struct net *net) 1696c9d8f1a6SCong Wang { 1697c9d8f1a6SCong Wang } 1698c9d8f1a6SCong Wang 1699c9d8f1a6SCong Wang static __net_initdata struct pernet_operations af_inet_ops = { 1700c9d8f1a6SCong Wang .init = inet_init_net, 1701c9d8f1a6SCong Wang .exit = inet_exit_net, 1702c9d8f1a6SCong Wang }; 1703c9d8f1a6SCong Wang 1704c9d8f1a6SCong Wang static int __init init_inet_pernet_ops(void) 1705c9d8f1a6SCong Wang { 1706c9d8f1a6SCong Wang return register_pernet_subsys(&af_inet_ops); 1707c9d8f1a6SCong Wang } 1708c9d8f1a6SCong Wang 17091da177e4SLinus Torvalds static int ipv4_proc_init(void); 17101da177e4SLinus Torvalds 171130e224d7SHerbert Xu /* 171230e224d7SHerbert Xu * IP protocol layer initialiser 171330e224d7SHerbert Xu */ 171430e224d7SHerbert Xu 171522061d80SVlad Yasevich static struct packet_offload ip_packet_offload __read_mostly = { 171622061d80SVlad Yasevich .type = cpu_to_be16(ETH_P_IP), 1717f191a1d1SVlad Yasevich .callbacks = { 1718f4c50d99SHerbert Xu .gso_segment = inet_gso_segment, 171973cc19f1SHerbert Xu .gro_receive = inet_gro_receive, 172073cc19f1SHerbert Xu .gro_complete = inet_gro_complete, 1721f191a1d1SVlad Yasevich }, 172230e224d7SHerbert Xu }; 172330e224d7SHerbert Xu 1724cb32f511SEric Dumazet static const struct net_offload ipip_offload = { 1725cb32f511SEric Dumazet .callbacks = { 1726cb32f511SEric Dumazet .gso_segment = inet_gso_segment, 1727fac8e0f5SJesse Gross .gro_receive = ipip_gro_receive, 1728b8cba75bSJesse Gross .gro_complete = ipip_gro_complete, 1729cb32f511SEric Dumazet }, 1730cb32f511SEric Dumazet }; 1731cb32f511SEric Dumazet 1732808a8f88SVlad Yasevich static int __init ipv4_offload_init(void) 1733808a8f88SVlad Yasevich { 1734808a8f88SVlad Yasevich /* 1735808a8f88SVlad Yasevich * Add offloads 1736808a8f88SVlad Yasevich */ 1737da5bab07SDaniel Borkmann if (udpv4_offload_init() < 0) 1738808a8f88SVlad Yasevich pr_crit("%s: Cannot add UDP protocol offload\n", __func__); 173928850dc7SDaniel Borkmann if (tcpv4_offload_init() < 0) 174028850dc7SDaniel Borkmann pr_crit("%s: Cannot add TCP protocol offload\n", __func__); 1741808a8f88SVlad Yasevich 1742808a8f88SVlad Yasevich dev_add_offload(&ip_packet_offload); 1743cb32f511SEric Dumazet inet_add_offload(&ipip_offload, IPPROTO_IPIP); 1744808a8f88SVlad Yasevich return 0; 1745808a8f88SVlad Yasevich } 1746808a8f88SVlad Yasevich 1747808a8f88SVlad Yasevich fs_initcall(ipv4_offload_init); 1748808a8f88SVlad Yasevich 1749808a8f88SVlad Yasevich static struct packet_type ip_packet_type __read_mostly = { 1750808a8f88SVlad Yasevich .type = cpu_to_be16(ETH_P_IP), 1751808a8f88SVlad Yasevich .func = ip_rcv, 1752808a8f88SVlad Yasevich }; 1753808a8f88SVlad Yasevich 17541da177e4SLinus Torvalds static int __init inet_init(void) 17551da177e4SLinus Torvalds { 17561da177e4SLinus Torvalds struct inet_protosw *q; 17571da177e4SLinus Torvalds struct list_head *r; 17581da177e4SLinus Torvalds int rc = -EINVAL; 17591da177e4SLinus Torvalds 1760b4772ef8SEyal Birger sock_skb_cb_check_size(sizeof(struct inet_skb_parm)); 17611da177e4SLinus Torvalds 17621da177e4SLinus Torvalds rc = proto_register(&tcp_prot, 1); 17631da177e4SLinus Torvalds if (rc) 1764122ff243SWANG Cong goto out; 17651da177e4SLinus Torvalds 17661da177e4SLinus Torvalds rc = proto_register(&udp_prot, 1); 17671da177e4SLinus Torvalds if (rc) 17681da177e4SLinus Torvalds goto out_unregister_tcp_proto; 17691da177e4SLinus Torvalds 17701da177e4SLinus Torvalds rc = proto_register(&raw_prot, 1); 17711da177e4SLinus Torvalds if (rc) 17721da177e4SLinus Torvalds goto out_unregister_udp_proto; 17731da177e4SLinus Torvalds 1774c319b4d7SVasiliy Kulikov rc = proto_register(&ping_prot, 1); 1775c319b4d7SVasiliy Kulikov if (rc) 1776c319b4d7SVasiliy Kulikov goto out_unregister_raw_proto; 1777c319b4d7SVasiliy Kulikov 17781da177e4SLinus Torvalds /* 17791da177e4SLinus Torvalds * Tell SOCKET that we are alive... 17801da177e4SLinus Torvalds */ 17811da177e4SLinus Torvalds 17821da177e4SLinus Torvalds (void)sock_register(&inet_family_ops); 17831da177e4SLinus Torvalds 1784bd7b1533SAl Viro #ifdef CONFIG_SYSCTL 1785bd7b1533SAl Viro ip_static_sysctl_init(); 1786bd7b1533SAl Viro #endif 1787bd7b1533SAl Viro 17881da177e4SLinus Torvalds /* 17891da177e4SLinus Torvalds * Add all the base protocols. 17901da177e4SLinus Torvalds */ 17911da177e4SLinus Torvalds 17921da177e4SLinus Torvalds if (inet_add_protocol(&icmp_protocol, IPPROTO_ICMP) < 0) 1793058bd4d2SJoe Perches pr_crit("%s: Cannot add ICMP protocol\n", __func__); 17941da177e4SLinus Torvalds if (inet_add_protocol(&udp_protocol, IPPROTO_UDP) < 0) 1795058bd4d2SJoe Perches pr_crit("%s: Cannot add UDP protocol\n", __func__); 17961da177e4SLinus Torvalds if (inet_add_protocol(&tcp_protocol, IPPROTO_TCP) < 0) 1797058bd4d2SJoe Perches pr_crit("%s: Cannot add TCP protocol\n", __func__); 17981da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 17991da177e4SLinus Torvalds if (inet_add_protocol(&igmp_protocol, IPPROTO_IGMP) < 0) 1800058bd4d2SJoe Perches pr_crit("%s: Cannot add IGMP protocol\n", __func__); 18011da177e4SLinus Torvalds #endif 18021da177e4SLinus Torvalds 18031da177e4SLinus Torvalds /* Register the socket-side information for inet_create. */ 18041da177e4SLinus Torvalds for (r = &inetsw[0]; r < &inetsw[SOCK_MAX]; ++r) 18051da177e4SLinus Torvalds INIT_LIST_HEAD(r); 18061da177e4SLinus Torvalds 18071da177e4SLinus Torvalds for (q = inetsw_array; q < &inetsw_array[INETSW_ARRAY_LEN]; ++q) 18081da177e4SLinus Torvalds inet_register_protosw(q); 18091da177e4SLinus Torvalds 18101da177e4SLinus Torvalds /* 18111da177e4SLinus Torvalds * Set the ARP module up 18121da177e4SLinus Torvalds */ 18131da177e4SLinus Torvalds 18141da177e4SLinus Torvalds arp_init(); 18151da177e4SLinus Torvalds 18161da177e4SLinus Torvalds /* 18171da177e4SLinus Torvalds * Set the IP module up 18181da177e4SLinus Torvalds */ 18191da177e4SLinus Torvalds 18201da177e4SLinus Torvalds ip_init(); 18211da177e4SLinus Torvalds 18229b0f976fSDenis V. Lunev tcp_v4_init(); 18231da177e4SLinus Torvalds 18241da177e4SLinus Torvalds /* Setup TCP slab cache for open requests. */ 18251da177e4SLinus Torvalds tcp_init(); 18261da177e4SLinus Torvalds 182795766fffSHideo Aoki /* Setup UDP memory threshold */ 182895766fffSHideo Aoki udp_init(); 182995766fffSHideo Aoki 1830ba4e58ecSGerrit Renker /* Add UDP-Lite (RFC 3828) */ 1831ba4e58ecSGerrit Renker udplite4_register(); 18321da177e4SLinus Torvalds 1833c319b4d7SVasiliy Kulikov ping_init(); 1834c319b4d7SVasiliy Kulikov 18351da177e4SLinus Torvalds /* 18361da177e4SLinus Torvalds * Set the ICMP layer up 18371da177e4SLinus Torvalds */ 18381da177e4SLinus Torvalds 1839a5710d65SDenis V. Lunev if (icmp_init() < 0) 1840a5710d65SDenis V. Lunev panic("Failed to create the ICMP control socket.\n"); 18411da177e4SLinus Torvalds 18421da177e4SLinus Torvalds /* 18431da177e4SLinus Torvalds * Initialise the multicast router 18441da177e4SLinus Torvalds */ 18451da177e4SLinus Torvalds #if defined(CONFIG_IP_MROUTE) 184603d2f897SWang Chen if (ip_mr_init()) 1847058bd4d2SJoe Perches pr_crit("%s: Cannot init ipv4 mroute\n", __func__); 18481da177e4SLinus Torvalds #endif 1849c9d8f1a6SCong Wang 1850c9d8f1a6SCong Wang if (init_inet_pernet_ops()) 1851c9d8f1a6SCong Wang pr_crit("%s: Cannot init ipv4 inet pernet ops\n", __func__); 18521da177e4SLinus Torvalds /* 18531da177e4SLinus Torvalds * Initialise per-cpu ipv4 mibs 18541da177e4SLinus Torvalds */ 18551da177e4SLinus Torvalds 18561da177e4SLinus Torvalds if (init_ipv4_mibs()) 1857058bd4d2SJoe Perches pr_crit("%s: Cannot init ipv4 mibs\n", __func__); 18581da177e4SLinus Torvalds 18591da177e4SLinus Torvalds ipv4_proc_init(); 18601da177e4SLinus Torvalds 18611da177e4SLinus Torvalds ipfrag_init(); 18621da177e4SLinus Torvalds 186330e224d7SHerbert Xu dev_add_pack(&ip_packet_type); 186430e224d7SHerbert Xu 1865045a0fa0SThomas Graf ip_tunnel_core_init(); 1866045a0fa0SThomas Graf 18671da177e4SLinus Torvalds rc = 0; 18681da177e4SLinus Torvalds out: 18691da177e4SLinus Torvalds return rc; 1870c319b4d7SVasiliy Kulikov out_unregister_raw_proto: 1871c319b4d7SVasiliy Kulikov proto_unregister(&raw_prot); 18721da177e4SLinus Torvalds out_unregister_udp_proto: 18731da177e4SLinus Torvalds proto_unregister(&udp_prot); 1874321efff7SOlaf Kirch out_unregister_tcp_proto: 1875321efff7SOlaf Kirch proto_unregister(&tcp_prot); 18761da177e4SLinus Torvalds goto out; 18771da177e4SLinus Torvalds } 18781da177e4SLinus Torvalds 1879a536e077SHeiko Carstens fs_initcall(inet_init); 18801da177e4SLinus Torvalds 18811da177e4SLinus Torvalds /* ------------------------------------------------------------------------ */ 18821da177e4SLinus Torvalds 18831da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 18841da177e4SLinus Torvalds static int __init ipv4_proc_init(void) 18851da177e4SLinus Torvalds { 18861da177e4SLinus Torvalds int rc = 0; 18871da177e4SLinus Torvalds 18881da177e4SLinus Torvalds if (raw_proc_init()) 18891da177e4SLinus Torvalds goto out_raw; 18901da177e4SLinus Torvalds if (tcp4_proc_init()) 18911da177e4SLinus Torvalds goto out_tcp; 18921da177e4SLinus Torvalds if (udp4_proc_init()) 18931da177e4SLinus Torvalds goto out_udp; 1894c319b4d7SVasiliy Kulikov if (ping_proc_init()) 1895c319b4d7SVasiliy Kulikov goto out_ping; 18961da177e4SLinus Torvalds if (ip_misc_proc_init()) 18971da177e4SLinus Torvalds goto out_misc; 18981da177e4SLinus Torvalds out: 18991da177e4SLinus Torvalds return rc; 19001da177e4SLinus Torvalds out_misc: 1901c319b4d7SVasiliy Kulikov ping_proc_exit(); 1902c319b4d7SVasiliy Kulikov out_ping: 19031da177e4SLinus Torvalds udp4_proc_exit(); 19041da177e4SLinus Torvalds out_udp: 19051da177e4SLinus Torvalds tcp4_proc_exit(); 19061da177e4SLinus Torvalds out_tcp: 19071da177e4SLinus Torvalds raw_proc_exit(); 19081da177e4SLinus Torvalds out_raw: 19091da177e4SLinus Torvalds rc = -ENOMEM; 19101da177e4SLinus Torvalds goto out; 19111da177e4SLinus Torvalds } 19121da177e4SLinus Torvalds 19131da177e4SLinus Torvalds #else /* CONFIG_PROC_FS */ 19141da177e4SLinus Torvalds static int __init ipv4_proc_init(void) 19151da177e4SLinus Torvalds { 19161da177e4SLinus Torvalds return 0; 19171da177e4SLinus Torvalds } 19181da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 19191da177e4SLinus Torvalds 19201da177e4SLinus Torvalds MODULE_ALIAS_NETPROTO(PF_INET); 19211da177e4SLinus Torvalds 1922