12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * INET An implementation of the TCP/IP protocol suite for the LINUX 41da177e4SLinus Torvalds * operating system. INET is implemented using the BSD Socket 51da177e4SLinus Torvalds * interface as the means of communication with the user level. 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * Implementation of the Transmission Control Protocol(TCP). 81da177e4SLinus Torvalds * 902c30a84SJesper Juhl * Authors: Ross Biro 101da177e4SLinus Torvalds * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG> 111da177e4SLinus Torvalds * Mark Evans, <evansmp@uhura.aston.ac.uk> 121da177e4SLinus Torvalds * Corey Minyard <wf-rch!minyard@relay.EU.net> 131da177e4SLinus Torvalds * Florian La Roche, <flla@stud.uni-sb.de> 141da177e4SLinus Torvalds * Charles Hedrick, <hedrick@klinzhai.rutgers.edu> 151da177e4SLinus Torvalds * Linus Torvalds, <torvalds@cs.helsinki.fi> 161da177e4SLinus Torvalds * Alan Cox, <gw4pts@gw4pts.ampr.org> 171da177e4SLinus Torvalds * Matthew Dillon, <dillon@apollo.west.oic.com> 181da177e4SLinus Torvalds * Arnt Gulbrandsen, <agulbra@nvg.unit.no> 191da177e4SLinus Torvalds * Jorge Cwik, <jorge@laser.satlink.net> 201da177e4SLinus Torvalds * 211da177e4SLinus Torvalds * Fixes: 221da177e4SLinus Torvalds * Alan Cox : Numerous verify_area() calls 231da177e4SLinus Torvalds * Alan Cox : Set the ACK bit on a reset 241da177e4SLinus Torvalds * Alan Cox : Stopped it crashing if it closed while 251da177e4SLinus Torvalds * sk->inuse=1 and was trying to connect 261da177e4SLinus Torvalds * (tcp_err()). 271da177e4SLinus Torvalds * Alan Cox : All icmp error handling was broken 281da177e4SLinus Torvalds * pointers passed where wrong and the 291da177e4SLinus Torvalds * socket was looked up backwards. Nobody 301da177e4SLinus Torvalds * tested any icmp error code obviously. 311da177e4SLinus Torvalds * Alan Cox : tcp_err() now handled properly. It 321da177e4SLinus Torvalds * wakes people on errors. poll 331da177e4SLinus Torvalds * behaves and the icmp error race 341da177e4SLinus Torvalds * has gone by moving it into sock.c 351da177e4SLinus Torvalds * Alan Cox : tcp_send_reset() fixed to work for 361da177e4SLinus Torvalds * everything not just packets for 371da177e4SLinus Torvalds * unknown sockets. 381da177e4SLinus Torvalds * Alan Cox : tcp option processing. 391da177e4SLinus Torvalds * Alan Cox : Reset tweaked (still not 100%) [Had 401da177e4SLinus Torvalds * syn rule wrong] 411da177e4SLinus Torvalds * Herp Rosmanith : More reset fixes 421da177e4SLinus Torvalds * Alan Cox : No longer acks invalid rst frames. 431da177e4SLinus Torvalds * Acking any kind of RST is right out. 441da177e4SLinus Torvalds * Alan Cox : Sets an ignore me flag on an rst 451da177e4SLinus Torvalds * receive otherwise odd bits of prattle 461da177e4SLinus Torvalds * escape still 471da177e4SLinus Torvalds * Alan Cox : Fixed another acking RST frame bug. 481da177e4SLinus Torvalds * Should stop LAN workplace lockups. 491da177e4SLinus Torvalds * Alan Cox : Some tidyups using the new skb list 501da177e4SLinus Torvalds * facilities 511da177e4SLinus Torvalds * Alan Cox : sk->keepopen now seems to work 521da177e4SLinus Torvalds * Alan Cox : Pulls options out correctly on accepts 531da177e4SLinus Torvalds * Alan Cox : Fixed assorted sk->rqueue->next errors 541da177e4SLinus Torvalds * Alan Cox : PSH doesn't end a TCP read. Switched a 551da177e4SLinus Torvalds * bit to skb ops. 561da177e4SLinus Torvalds * Alan Cox : Tidied tcp_data to avoid a potential 571da177e4SLinus Torvalds * nasty. 581da177e4SLinus Torvalds * Alan Cox : Added some better commenting, as the 591da177e4SLinus Torvalds * tcp is hard to follow 601da177e4SLinus Torvalds * Alan Cox : Removed incorrect check for 20 * psh 611da177e4SLinus Torvalds * Michael O'Reilly : ack < copied bug fix. 621da177e4SLinus Torvalds * Johannes Stille : Misc tcp fixes (not all in yet). 631da177e4SLinus Torvalds * Alan Cox : FIN with no memory -> CRASH 641da177e4SLinus Torvalds * Alan Cox : Added socket option proto entries. 651da177e4SLinus Torvalds * Also added awareness of them to accept. 661da177e4SLinus Torvalds * Alan Cox : Added TCP options (SOL_TCP) 671da177e4SLinus Torvalds * Alan Cox : Switched wakeup calls to callbacks, 681da177e4SLinus Torvalds * so the kernel can layer network 691da177e4SLinus Torvalds * sockets. 701da177e4SLinus Torvalds * Alan Cox : Use ip_tos/ip_ttl settings. 711da177e4SLinus Torvalds * Alan Cox : Handle FIN (more) properly (we hope). 721da177e4SLinus Torvalds * Alan Cox : RST frames sent on unsynchronised 731da177e4SLinus Torvalds * state ack error. 741da177e4SLinus Torvalds * Alan Cox : Put in missing check for SYN bit. 751da177e4SLinus Torvalds * Alan Cox : Added tcp_select_window() aka NET2E 761da177e4SLinus Torvalds * window non shrink trick. 771da177e4SLinus Torvalds * Alan Cox : Added a couple of small NET2E timer 781da177e4SLinus Torvalds * fixes 791da177e4SLinus Torvalds * Charles Hedrick : TCP fixes 801da177e4SLinus Torvalds * Toomas Tamm : TCP window fixes 811da177e4SLinus Torvalds * Alan Cox : Small URG fix to rlogin ^C ack fight 821da177e4SLinus Torvalds * Charles Hedrick : Rewrote most of it to actually work 831da177e4SLinus Torvalds * Linus : Rewrote tcp_read() and URG handling 841da177e4SLinus Torvalds * completely 851da177e4SLinus Torvalds * Gerhard Koerting: Fixed some missing timer handling 861da177e4SLinus Torvalds * Matthew Dillon : Reworked TCP machine states as per RFC 871da177e4SLinus Torvalds * Gerhard Koerting: PC/TCP workarounds 881da177e4SLinus Torvalds * Adam Caldwell : Assorted timer/timing errors 891da177e4SLinus Torvalds * Matthew Dillon : Fixed another RST bug 901da177e4SLinus Torvalds * Alan Cox : Move to kernel side addressing changes. 911da177e4SLinus Torvalds * Alan Cox : Beginning work on TCP fastpathing 921da177e4SLinus Torvalds * (not yet usable) 931da177e4SLinus Torvalds * Arnt Gulbrandsen: Turbocharged tcp_check() routine. 941da177e4SLinus Torvalds * Alan Cox : TCP fast path debugging 951da177e4SLinus Torvalds * Alan Cox : Window clamping 961da177e4SLinus Torvalds * Michael Riepe : Bug in tcp_check() 971da177e4SLinus Torvalds * Matt Dillon : More TCP improvements and RST bug fixes 981da177e4SLinus Torvalds * Matt Dillon : Yet more small nasties remove from the 991da177e4SLinus Torvalds * TCP code (Be very nice to this man if 1001da177e4SLinus Torvalds * tcp finally works 100%) 8) 1011da177e4SLinus Torvalds * Alan Cox : BSD accept semantics. 1021da177e4SLinus Torvalds * Alan Cox : Reset on closedown bug. 1031da177e4SLinus Torvalds * Peter De Schrijver : ENOTCONN check missing in tcp_sendto(). 1041da177e4SLinus Torvalds * Michael Pall : Handle poll() after URG properly in 1051da177e4SLinus Torvalds * all cases. 1061da177e4SLinus Torvalds * Michael Pall : Undo the last fix in tcp_read_urg() 1071da177e4SLinus Torvalds * (multi URG PUSH broke rlogin). 1081da177e4SLinus Torvalds * Michael Pall : Fix the multi URG PUSH problem in 1091da177e4SLinus Torvalds * tcp_readable(), poll() after URG 1101da177e4SLinus Torvalds * works now. 1111da177e4SLinus Torvalds * Michael Pall : recv(...,MSG_OOB) never blocks in the 1121da177e4SLinus Torvalds * BSD api. 1131da177e4SLinus Torvalds * Alan Cox : Changed the semantics of sk->socket to 1141da177e4SLinus Torvalds * fix a race and a signal problem with 1151da177e4SLinus Torvalds * accept() and async I/O. 1161da177e4SLinus Torvalds * Alan Cox : Relaxed the rules on tcp_sendto(). 1171da177e4SLinus Torvalds * Yury Shevchuk : Really fixed accept() blocking problem. 1181da177e4SLinus Torvalds * Craig I. Hagan : Allow for BSD compatible TIME_WAIT for 1191da177e4SLinus Torvalds * clients/servers which listen in on 1201da177e4SLinus Torvalds * fixed ports. 1211da177e4SLinus Torvalds * Alan Cox : Cleaned the above up and shrank it to 1221da177e4SLinus Torvalds * a sensible code size. 1231da177e4SLinus Torvalds * Alan Cox : Self connect lockup fix. 1241da177e4SLinus Torvalds * Alan Cox : No connect to multicast. 1251da177e4SLinus Torvalds * Ross Biro : Close unaccepted children on master 1261da177e4SLinus Torvalds * socket close. 1271da177e4SLinus Torvalds * Alan Cox : Reset tracing code. 1281da177e4SLinus Torvalds * Alan Cox : Spurious resets on shutdown. 1291da177e4SLinus Torvalds * Alan Cox : Giant 15 minute/60 second timer error 1301da177e4SLinus Torvalds * Alan Cox : Small whoops in polling before an 1311da177e4SLinus Torvalds * accept. 1321da177e4SLinus Torvalds * Alan Cox : Kept the state trace facility since 1331da177e4SLinus Torvalds * it's handy for debugging. 1341da177e4SLinus Torvalds * Alan Cox : More reset handler fixes. 1351da177e4SLinus Torvalds * Alan Cox : Started rewriting the code based on 1361da177e4SLinus Torvalds * the RFC's for other useful protocol 1371da177e4SLinus Torvalds * references see: Comer, KA9Q NOS, and 1381da177e4SLinus Torvalds * for a reference on the difference 1391da177e4SLinus Torvalds * between specifications and how BSD 1401da177e4SLinus Torvalds * works see the 4.4lite source. 1411da177e4SLinus Torvalds * A.N.Kuznetsov : Don't time wait on completion of tidy 1421da177e4SLinus Torvalds * close. 1431da177e4SLinus Torvalds * Linus Torvalds : Fin/Shutdown & copied_seq changes. 1441da177e4SLinus Torvalds * Linus Torvalds : Fixed BSD port reuse to work first syn 1451da177e4SLinus Torvalds * Alan Cox : Reimplemented timers as per the RFC 1461da177e4SLinus Torvalds * and using multiple timers for sanity. 1471da177e4SLinus Torvalds * Alan Cox : Small bug fixes, and a lot of new 1481da177e4SLinus Torvalds * comments. 1491da177e4SLinus Torvalds * Alan Cox : Fixed dual reader crash by locking 1501da177e4SLinus Torvalds * the buffers (much like datagram.c) 1511da177e4SLinus Torvalds * Alan Cox : Fixed stuck sockets in probe. A probe 1521da177e4SLinus Torvalds * now gets fed up of retrying without 1531da177e4SLinus Torvalds * (even a no space) answer. 1541da177e4SLinus Torvalds * Alan Cox : Extracted closing code better 1551da177e4SLinus Torvalds * Alan Cox : Fixed the closing state machine to 1561da177e4SLinus Torvalds * resemble the RFC. 1571da177e4SLinus Torvalds * Alan Cox : More 'per spec' fixes. 1581da177e4SLinus Torvalds * Jorge Cwik : Even faster checksumming. 1591da177e4SLinus Torvalds * Alan Cox : tcp_data() doesn't ack illegal PSH 1601da177e4SLinus Torvalds * only frames. At least one pc tcp stack 1611da177e4SLinus Torvalds * generates them. 1621da177e4SLinus Torvalds * Alan Cox : Cache last socket. 1631da177e4SLinus Torvalds * Alan Cox : Per route irtt. 1641da177e4SLinus Torvalds * Matt Day : poll()->select() match BSD precisely on error 1651da177e4SLinus Torvalds * Alan Cox : New buffers 1661da177e4SLinus Torvalds * Marc Tamsky : Various sk->prot->retransmits and 1671da177e4SLinus Torvalds * sk->retransmits misupdating fixed. 1681da177e4SLinus Torvalds * Fixed tcp_write_timeout: stuck close, 1691da177e4SLinus Torvalds * and TCP syn retries gets used now. 1701da177e4SLinus Torvalds * Mark Yarvis : In tcp_read_wakeup(), don't send an 1711da177e4SLinus Torvalds * ack if state is TCP_CLOSED. 1721da177e4SLinus Torvalds * Alan Cox : Look up device on a retransmit - routes may 1731da177e4SLinus Torvalds * change. Doesn't yet cope with MSS shrink right 1741da177e4SLinus Torvalds * but it's a start! 1751da177e4SLinus Torvalds * Marc Tamsky : Closing in closing fixes. 1761da177e4SLinus Torvalds * Mike Shaver : RFC1122 verifications. 1771da177e4SLinus Torvalds * Alan Cox : rcv_saddr errors. 1781da177e4SLinus Torvalds * Alan Cox : Block double connect(). 1791da177e4SLinus Torvalds * Alan Cox : Small hooks for enSKIP. 1801da177e4SLinus Torvalds * Alexey Kuznetsov: Path MTU discovery. 1811da177e4SLinus Torvalds * Alan Cox : Support soft errors. 1821da177e4SLinus Torvalds * Alan Cox : Fix MTU discovery pathological case 1831da177e4SLinus Torvalds * when the remote claims no mtu! 1841da177e4SLinus Torvalds * Marc Tamsky : TCP_CLOSE fix. 1851da177e4SLinus Torvalds * Colin (G3TNE) : Send a reset on syn ack replies in 1861da177e4SLinus Torvalds * window but wrong (fixes NT lpd problems) 1871da177e4SLinus Torvalds * Pedro Roque : Better TCP window handling, delayed ack. 1881da177e4SLinus Torvalds * Joerg Reuter : No modification of locked buffers in 1891da177e4SLinus Torvalds * tcp_do_retransmit() 1901da177e4SLinus Torvalds * Eric Schenk : Changed receiver side silly window 1911da177e4SLinus Torvalds * avoidance algorithm to BSD style 1921da177e4SLinus Torvalds * algorithm. This doubles throughput 1931da177e4SLinus Torvalds * against machines running Solaris, 1941da177e4SLinus Torvalds * and seems to result in general 1951da177e4SLinus Torvalds * improvement. 1961da177e4SLinus Torvalds * Stefan Magdalinski : adjusted tcp_readable() to fix FIONREAD 1971da177e4SLinus Torvalds * Willy Konynenberg : Transparent proxying support. 1981da177e4SLinus Torvalds * Mike McLagan : Routing by source 1991da177e4SLinus Torvalds * Keith Owens : Do proper merging with partial SKB's in 2001da177e4SLinus Torvalds * tcp_do_sendmsg to avoid burstiness. 2011da177e4SLinus Torvalds * Eric Schenk : Fix fast close down bug with 2021da177e4SLinus Torvalds * shutdown() followed by close(). 2031da177e4SLinus Torvalds * Andi Kleen : Make poll agree with SIGIO 2041da177e4SLinus Torvalds * Salvatore Sanfilippo : Support SO_LINGER with linger == 1 and 2051da177e4SLinus Torvalds * lingertime == 0 (RFC 793 ABORT Call) 2061da177e4SLinus Torvalds * Hirokazu Takahashi : Use copy_from_user() instead of 2071da177e4SLinus Torvalds * csum_and_copy_from_user() if possible. 2081da177e4SLinus Torvalds * 2091da177e4SLinus Torvalds * Description of States: 2101da177e4SLinus Torvalds * 2111da177e4SLinus Torvalds * TCP_SYN_SENT sent a connection request, waiting for ack 2121da177e4SLinus Torvalds * 2131da177e4SLinus Torvalds * TCP_SYN_RECV received a connection request, sent ack, 2141da177e4SLinus Torvalds * waiting for final ack in three-way handshake. 2151da177e4SLinus Torvalds * 2161da177e4SLinus Torvalds * TCP_ESTABLISHED connection established 2171da177e4SLinus Torvalds * 2181da177e4SLinus Torvalds * TCP_FIN_WAIT1 our side has shutdown, waiting to complete 2191da177e4SLinus Torvalds * transmission of remaining buffered data 2201da177e4SLinus Torvalds * 2211da177e4SLinus Torvalds * TCP_FIN_WAIT2 all buffered data sent, waiting for remote 2221da177e4SLinus Torvalds * to shutdown 2231da177e4SLinus Torvalds * 2241da177e4SLinus Torvalds * TCP_CLOSING both sides have shutdown but we still have 2251da177e4SLinus Torvalds * data we have to finish sending 2261da177e4SLinus Torvalds * 2271da177e4SLinus Torvalds * TCP_TIME_WAIT timeout to catch resent junk before entering 2281da177e4SLinus Torvalds * closed, can only be entered from FIN_WAIT2 2291da177e4SLinus Torvalds * or CLOSING. Required because the other end 2301da177e4SLinus Torvalds * may not have gotten our last ACK causing it 2311da177e4SLinus Torvalds * to retransmit the data packet (which we ignore) 2321da177e4SLinus Torvalds * 2331da177e4SLinus Torvalds * TCP_CLOSE_WAIT remote side has shutdown and is waiting for 2341da177e4SLinus Torvalds * us to finish writing our data and to shutdown 2351da177e4SLinus Torvalds * (we have to close() to move on to LAST_ACK) 2361da177e4SLinus Torvalds * 2371da177e4SLinus Torvalds * TCP_LAST_ACK out side has shutdown after remote has 2381da177e4SLinus Torvalds * shutdown. There may still be data in our 2391da177e4SLinus Torvalds * buffer that we have to finish sending 2401da177e4SLinus Torvalds * 2411da177e4SLinus Torvalds * TCP_CLOSE socket is finished 2421da177e4SLinus Torvalds */ 2431da177e4SLinus Torvalds 244afd46503SJoe Perches #define pr_fmt(fmt) "TCP: " fmt 245afd46503SJoe Perches 246cf80e0e4SHerbert Xu #include <crypto/hash.h> 247172589ccSIlpo Järvinen #include <linux/kernel.h> 2481da177e4SLinus Torvalds #include <linux/module.h> 2491da177e4SLinus Torvalds #include <linux/types.h> 2501da177e4SLinus Torvalds #include <linux/fcntl.h> 2511da177e4SLinus Torvalds #include <linux/poll.h> 2526e9250f5SEric Dumazet #include <linux/inet_diag.h> 2531da177e4SLinus Torvalds #include <linux/init.h> 2541da177e4SLinus Torvalds #include <linux/fs.h> 2559c55e01cSJens Axboe #include <linux/skbuff.h> 25681b23b4aSAndrew Morton #include <linux/scatterlist.h> 2579c55e01cSJens Axboe #include <linux/splice.h> 2589c55e01cSJens Axboe #include <linux/net.h> 2599c55e01cSJens Axboe #include <linux/socket.h> 2601da177e4SLinus Torvalds #include <linux/random.h> 26157c8a661SMike Rapoport #include <linux/memblock.h> 26257413ebcSMiquel van Smoorenburg #include <linux/highmem.h> 26357413ebcSMiquel van Smoorenburg #include <linux/swap.h> 264b8059eadSDavid S. Miller #include <linux/cache.h> 265f4c50d99SHerbert Xu #include <linux/err.h> 266da5c78c8SWilliam Allen Simpson #include <linux/time.h> 2675a0e3ad6STejun Heo #include <linux/slab.h> 26898aaa913SMike Maloney #include <linux/errqueue.h> 26960e2a778SUrsula Braun #include <linux/static_key.h> 2701da177e4SLinus Torvalds 2711da177e4SLinus Torvalds #include <net/icmp.h> 272cf60af03SYuchung Cheng #include <net/inet_common.h> 2731da177e4SLinus Torvalds #include <net/tcp.h> 274f870fa0bSMat Martineau #include <net/mptcp.h> 2751da177e4SLinus Torvalds #include <net/xfrm.h> 2761da177e4SLinus Torvalds #include <net/ip.h> 2779c55e01cSJens Axboe #include <net/sock.h> 2781da177e4SLinus Torvalds 2797c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 2801da177e4SLinus Torvalds #include <asm/ioctls.h> 281076bb0c8SEliezer Tamir #include <net/busy_poll.h> 2821da177e4SLinus Torvalds 283dd24c001SEric Dumazet struct percpu_counter tcp_orphan_count; 2840a5578cfSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(tcp_orphan_count); 2850a5578cfSArnaldo Carvalho de Melo 286a4fe34bfSEric W. Biederman long sysctl_tcp_mem[3] __read_mostly; 287a4fe34bfSEric W. Biederman EXPORT_SYMBOL(sysctl_tcp_mem); 2881da177e4SLinus Torvalds 2898d987e5cSEric Dumazet atomic_long_t tcp_memory_allocated; /* Current allocated memory. */ 2901da177e4SLinus Torvalds EXPORT_SYMBOL(tcp_memory_allocated); 2911748376bSEric Dumazet 29260e2a778SUrsula Braun #if IS_ENABLED(CONFIG_SMC) 29360e2a778SUrsula Braun DEFINE_STATIC_KEY_FALSE(tcp_have_smc); 29460e2a778SUrsula Braun EXPORT_SYMBOL(tcp_have_smc); 29560e2a778SUrsula Braun #endif 29660e2a778SUrsula Braun 2971748376bSEric Dumazet /* 2981748376bSEric Dumazet * Current number of TCP sockets. 2991748376bSEric Dumazet */ 3001748376bSEric Dumazet struct percpu_counter tcp_sockets_allocated; 3011da177e4SLinus Torvalds EXPORT_SYMBOL(tcp_sockets_allocated); 3021da177e4SLinus Torvalds 3031da177e4SLinus Torvalds /* 3049c55e01cSJens Axboe * TCP splice context 3059c55e01cSJens Axboe */ 3069c55e01cSJens Axboe struct tcp_splice_state { 3079c55e01cSJens Axboe struct pipe_inode_info *pipe; 3089c55e01cSJens Axboe size_t len; 3099c55e01cSJens Axboe unsigned int flags; 3109c55e01cSJens Axboe }; 3119c55e01cSJens Axboe 3129c55e01cSJens Axboe /* 3131da177e4SLinus Torvalds * Pressure flag: try to collapse. 3141da177e4SLinus Torvalds * Technical note: it is used by multiple contexts non atomically. 3153ab224beSHideo Aoki * All the __sk_mem_schedule() is of this nature: accounting 3161da177e4SLinus Torvalds * is strict, actions are advisory and have some latency. 3171da177e4SLinus Torvalds */ 31806044751SEric Dumazet unsigned long tcp_memory_pressure __read_mostly; 31906044751SEric Dumazet EXPORT_SYMBOL_GPL(tcp_memory_pressure); 3201da177e4SLinus Torvalds 3212e05fcaeSEric Dumazet DEFINE_STATIC_KEY_FALSE(tcp_rx_skb_cache_key); 3222e05fcaeSEric Dumazet EXPORT_SYMBOL(tcp_rx_skb_cache_key); 3232e05fcaeSEric Dumazet 3242e05fcaeSEric Dumazet DEFINE_STATIC_KEY_FALSE(tcp_tx_skb_cache_key); 3252e05fcaeSEric Dumazet 3265c52ba17SPavel Emelyanov void tcp_enter_memory_pressure(struct sock *sk) 3271da177e4SLinus Torvalds { 32806044751SEric Dumazet unsigned long val; 32906044751SEric Dumazet 3301f142c17SEric Dumazet if (READ_ONCE(tcp_memory_pressure)) 33106044751SEric Dumazet return; 33206044751SEric Dumazet val = jiffies; 33306044751SEric Dumazet 33406044751SEric Dumazet if (!val) 33506044751SEric Dumazet val--; 33606044751SEric Dumazet if (!cmpxchg(&tcp_memory_pressure, 0, val)) 3374e673444SPavel Emelyanov NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPMEMORYPRESSURES); 3381da177e4SLinus Torvalds } 33906044751SEric Dumazet EXPORT_SYMBOL_GPL(tcp_enter_memory_pressure); 34006044751SEric Dumazet 34106044751SEric Dumazet void tcp_leave_memory_pressure(struct sock *sk) 34206044751SEric Dumazet { 34306044751SEric Dumazet unsigned long val; 34406044751SEric Dumazet 3451f142c17SEric Dumazet if (!READ_ONCE(tcp_memory_pressure)) 34606044751SEric Dumazet return; 34706044751SEric Dumazet val = xchg(&tcp_memory_pressure, 0); 34806044751SEric Dumazet if (val) 34906044751SEric Dumazet NET_ADD_STATS(sock_net(sk), LINUX_MIB_TCPMEMORYPRESSURESCHRONO, 35006044751SEric Dumazet jiffies_to_msecs(jiffies - val)); 3511da177e4SLinus Torvalds } 35206044751SEric Dumazet EXPORT_SYMBOL_GPL(tcp_leave_memory_pressure); 3531da177e4SLinus Torvalds 354b103cf34SJulian Anastasov /* Convert seconds to retransmits based on initial and max timeout */ 355b103cf34SJulian Anastasov static u8 secs_to_retrans(int seconds, int timeout, int rto_max) 356b103cf34SJulian Anastasov { 357b103cf34SJulian Anastasov u8 res = 0; 358b103cf34SJulian Anastasov 359b103cf34SJulian Anastasov if (seconds > 0) { 360b103cf34SJulian Anastasov int period = timeout; 361b103cf34SJulian Anastasov 362b103cf34SJulian Anastasov res = 1; 363b103cf34SJulian Anastasov while (seconds > period && res < 255) { 364b103cf34SJulian Anastasov res++; 365b103cf34SJulian Anastasov timeout <<= 1; 366b103cf34SJulian Anastasov if (timeout > rto_max) 367b103cf34SJulian Anastasov timeout = rto_max; 368b103cf34SJulian Anastasov period += timeout; 369b103cf34SJulian Anastasov } 370b103cf34SJulian Anastasov } 371b103cf34SJulian Anastasov return res; 372b103cf34SJulian Anastasov } 373b103cf34SJulian Anastasov 374b103cf34SJulian Anastasov /* Convert retransmits to seconds based on initial and max timeout */ 375b103cf34SJulian Anastasov static int retrans_to_secs(u8 retrans, int timeout, int rto_max) 376b103cf34SJulian Anastasov { 377b103cf34SJulian Anastasov int period = 0; 378b103cf34SJulian Anastasov 379b103cf34SJulian Anastasov if (retrans > 0) { 380b103cf34SJulian Anastasov period = timeout; 381b103cf34SJulian Anastasov while (--retrans) { 382b103cf34SJulian Anastasov timeout <<= 1; 383b103cf34SJulian Anastasov if (timeout > rto_max) 384b103cf34SJulian Anastasov timeout = rto_max; 385b103cf34SJulian Anastasov period += timeout; 386b103cf34SJulian Anastasov } 387b103cf34SJulian Anastasov } 388b103cf34SJulian Anastasov return period; 389b103cf34SJulian Anastasov } 390b103cf34SJulian Anastasov 3910263598cSWei Wang static u64 tcp_compute_delivery_rate(const struct tcp_sock *tp) 3920263598cSWei Wang { 3930263598cSWei Wang u32 rate = READ_ONCE(tp->rate_delivered); 3940263598cSWei Wang u32 intv = READ_ONCE(tp->rate_interval_us); 3950263598cSWei Wang u64 rate64 = 0; 3960263598cSWei Wang 3970263598cSWei Wang if (rate && intv) { 3980263598cSWei Wang rate64 = (u64)rate * tp->mss_cache * USEC_PER_SEC; 3990263598cSWei Wang do_div(rate64, intv); 4000263598cSWei Wang } 4010263598cSWei Wang return rate64; 4020263598cSWei Wang } 4030263598cSWei Wang 404900f65d3SNeal Cardwell /* Address-family independent initialization for a tcp_sock. 405900f65d3SNeal Cardwell * 406900f65d3SNeal Cardwell * NOTE: A lot of things set to zero explicitly by call to 407900f65d3SNeal Cardwell * sk_alloc() so need not be done here. 408900f65d3SNeal Cardwell */ 409900f65d3SNeal Cardwell void tcp_init_sock(struct sock *sk) 410900f65d3SNeal Cardwell { 411900f65d3SNeal Cardwell struct inet_connection_sock *icsk = inet_csk(sk); 412900f65d3SNeal Cardwell struct tcp_sock *tp = tcp_sk(sk); 413900f65d3SNeal Cardwell 4149f5afeaeSYaogong Wang tp->out_of_order_queue = RB_ROOT; 41575c119afSEric Dumazet sk->tcp_rtx_queue = RB_ROOT; 416900f65d3SNeal Cardwell tcp_init_xmit_timers(sk); 41746d3ceabSEric Dumazet INIT_LIST_HEAD(&tp->tsq_node); 418e2080072SEric Dumazet INIT_LIST_HEAD(&tp->tsorted_sent_queue); 419900f65d3SNeal Cardwell 420900f65d3SNeal Cardwell icsk->icsk_rto = TCP_TIMEOUT_INIT; 421740b0f18SEric Dumazet tp->mdev_us = jiffies_to_usecs(TCP_TIMEOUT_INIT); 422ac9517fcSEric Dumazet minmax_reset(&tp->rtt_min, tcp_jiffies32, ~0U); 423900f65d3SNeal Cardwell 424900f65d3SNeal Cardwell /* So many TCP implementations out there (incorrectly) count the 425900f65d3SNeal Cardwell * initial SYN frame in their delayed-ACK and congestion control 426900f65d3SNeal Cardwell * algorithms that we must have the following bandaid to talk 427900f65d3SNeal Cardwell * efficiently to them. -DaveM 428900f65d3SNeal Cardwell */ 429900f65d3SNeal Cardwell tp->snd_cwnd = TCP_INIT_CWND; 430900f65d3SNeal Cardwell 431d7722e85SSoheil Hassas Yeganeh /* There's a bubble in the pipe until at least the first ACK. */ 432d7722e85SSoheil Hassas Yeganeh tp->app_limited = ~0U; 433d7722e85SSoheil Hassas Yeganeh 434900f65d3SNeal Cardwell /* See draft-stevens-tcpca-spec-01 for discussion of the 435900f65d3SNeal Cardwell * initialization of these values. 436900f65d3SNeal Cardwell */ 437900f65d3SNeal Cardwell tp->snd_ssthresh = TCP_INFINITE_SSTHRESH; 438900f65d3SNeal Cardwell tp->snd_cwnd_clamp = ~0; 439900f65d3SNeal Cardwell tp->mss_cache = TCP_MSS_DEFAULT; 440900f65d3SNeal Cardwell 4411043e25fSNikolay Borisov tp->reordering = sock_net(sk)->ipv4.sysctl_tcp_reordering; 44255d8694fSFlorian Westphal tcp_assign_congestion_control(sk); 443900f65d3SNeal Cardwell 444ceaa1fefSAndrey Vagin tp->tsoffset = 0; 4451f255691SPriyaranjan Jha tp->rack.reo_wnd_steps = 1; 446ceaa1fefSAndrey Vagin 447900f65d3SNeal Cardwell sk->sk_write_space = sk_stream_write_space; 448900f65d3SNeal Cardwell sock_set_flag(sk, SOCK_USE_WRITE_QUEUE); 449900f65d3SNeal Cardwell 450900f65d3SNeal Cardwell icsk->icsk_sync_mss = tcp_sync_mss; 451900f65d3SNeal Cardwell 452e292f05eSEric Dumazet WRITE_ONCE(sk->sk_sndbuf, sock_net(sk)->ipv4.sysctl_tcp_wmem[1]); 453ebb3b78dSEric Dumazet WRITE_ONCE(sk->sk_rcvbuf, sock_net(sk)->ipv4.sysctl_tcp_rmem[1]); 454900f65d3SNeal Cardwell 455900f65d3SNeal Cardwell sk_sockets_allocated_inc(sk); 4560a6b2a1dSEric Dumazet sk->sk_route_forced_caps = NETIF_F_GSO; 457900f65d3SNeal Cardwell } 458900f65d3SNeal Cardwell EXPORT_SYMBOL(tcp_init_sock); 459900f65d3SNeal Cardwell 4604e8cc228SEric Dumazet static void tcp_tx_timestamp(struct sock *sk, u16 tsflags) 4614ed2d765SWillem de Bruijn { 4624e8cc228SEric Dumazet struct sk_buff *skb = tcp_write_queue_tail(sk); 4634e8cc228SEric Dumazet 464ad02c4f5SSoheil Hassas Yeganeh if (tsflags && skb) { 4654ed2d765SWillem de Bruijn struct skb_shared_info *shinfo = skb_shinfo(skb); 4666b084928SSoheil Hassas Yeganeh struct tcp_skb_cb *tcb = TCP_SKB_CB(skb); 4674ed2d765SWillem de Bruijn 468c14ac945SSoheil Hassas Yeganeh sock_tx_timestamp(sk, tsflags, &shinfo->tx_flags); 4690a2cf20cSSoheil Hassas Yeganeh if (tsflags & SOF_TIMESTAMPING_TX_ACK) 4700a2cf20cSSoheil Hassas Yeganeh tcb->txstamp_ack = 1; 4710a2cf20cSSoheil Hassas Yeganeh if (tsflags & SOF_TIMESTAMPING_TX_RECORD_MASK) 4724ed2d765SWillem de Bruijn shinfo->tskey = TCP_SKB_CB(skb)->seq + skb->len - 1; 4734ed2d765SWillem de Bruijn } 474f066e2b0SWillem de Bruijn } 4754ed2d765SWillem de Bruijn 4768934ce2fSJohn Fastabend static inline bool tcp_stream_is_readable(const struct tcp_sock *tp, 4778934ce2fSJohn Fastabend int target, struct sock *sk) 4788934ce2fSJohn Fastabend { 47924adbc16SEric Dumazet int avail = READ_ONCE(tp->rcv_nxt) - READ_ONCE(tp->copied_seq); 48024adbc16SEric Dumazet 48124adbc16SEric Dumazet if (avail > 0) { 48224adbc16SEric Dumazet if (avail >= target) 48324adbc16SEric Dumazet return true; 48424adbc16SEric Dumazet if (tcp_rmem_pressure(sk)) 48524adbc16SEric Dumazet return true; 48624adbc16SEric Dumazet } 48724adbc16SEric Dumazet if (sk->sk_prot->stream_memory_read) 48824adbc16SEric Dumazet return sk->sk_prot->stream_memory_read(sk); 48924adbc16SEric Dumazet return false; 4908934ce2fSJohn Fastabend } 4918934ce2fSJohn Fastabend 4921da177e4SLinus Torvalds /* 493a11e1d43SLinus Torvalds * Wait for a TCP event. 494a11e1d43SLinus Torvalds * 495a11e1d43SLinus Torvalds * Note that we don't need to lock the socket, as the upper poll layers 496a11e1d43SLinus Torvalds * take care of normal races (between the test and the event) and we don't 497a11e1d43SLinus Torvalds * go look at any of the socket buffers directly. 4981da177e4SLinus Torvalds */ 499a11e1d43SLinus Torvalds __poll_t tcp_poll(struct file *file, struct socket *sock, poll_table *wait) 5001da177e4SLinus Torvalds { 501a11e1d43SLinus Torvalds __poll_t mask; 5021da177e4SLinus Torvalds struct sock *sk = sock->sk; 503cf533ea5SEric Dumazet const struct tcp_sock *tp = tcp_sk(sk); 50400fd38d9SEric Dumazet int state; 5051da177e4SLinus Torvalds 50689ab066dSKarsten Graul sock_poll_wait(file, sock, wait); 507a11e1d43SLinus Torvalds 508986ffdfdSYafang Shao state = inet_sk_state_load(sk); 50900fd38d9SEric Dumazet if (state == TCP_LISTEN) 510dc40c7bcSArnaldo Carvalho de Melo return inet_csk_listen_poll(sk); 5111da177e4SLinus Torvalds 512a11e1d43SLinus Torvalds /* Socket is not locked. We are protected from async events 513a11e1d43SLinus Torvalds * by poll logic and correct handling of state changes 514a11e1d43SLinus Torvalds * made by other threads is impossible in any case. 515a11e1d43SLinus Torvalds */ 516a11e1d43SLinus Torvalds 517a11e1d43SLinus Torvalds mask = 0; 518a11e1d43SLinus Torvalds 5191da177e4SLinus Torvalds /* 520a9a08845SLinus Torvalds * EPOLLHUP is certainly not done right. But poll() doesn't 5211da177e4SLinus Torvalds * have a notion of HUP in just one direction, and for a 5221da177e4SLinus Torvalds * socket the read side is more interesting. 5231da177e4SLinus Torvalds * 524a9a08845SLinus Torvalds * Some poll() documentation says that EPOLLHUP is incompatible 525a9a08845SLinus Torvalds * with the EPOLLOUT/POLLWR flags, so somebody should check this 5261da177e4SLinus Torvalds * all. But careful, it tends to be safer to return too many 5271da177e4SLinus Torvalds * bits than too few, and you can easily break real applications 5281da177e4SLinus Torvalds * if you don't tell them that something has hung up! 5291da177e4SLinus Torvalds * 5301da177e4SLinus Torvalds * Check-me. 5311da177e4SLinus Torvalds * 532a9a08845SLinus Torvalds * Check number 1. EPOLLHUP is _UNMASKABLE_ event (see UNIX98 and 5331da177e4SLinus Torvalds * our fs/select.c). It means that after we received EOF, 5341da177e4SLinus Torvalds * poll always returns immediately, making impossible poll() on write() 535a9a08845SLinus Torvalds * in state CLOSE_WAIT. One solution is evident --- to set EPOLLHUP 5361da177e4SLinus Torvalds * if and only if shutdown has been made in both directions. 5371da177e4SLinus Torvalds * Actually, it is interesting to look how Solaris and DUX 538a9a08845SLinus Torvalds * solve this dilemma. I would prefer, if EPOLLHUP were maskable, 5391da177e4SLinus Torvalds * then we could set it on SND_SHUTDOWN. BTW examples given 5401da177e4SLinus Torvalds * in Stevens' books assume exactly this behaviour, it explains 541a9a08845SLinus Torvalds * why EPOLLHUP is incompatible with EPOLLOUT. --ANK 5421da177e4SLinus Torvalds * 5431da177e4SLinus Torvalds * NOTE. Check for TCP_CLOSE is added. The goal is to prevent 5441da177e4SLinus Torvalds * blocking on fresh not-connected or disconnected socket. --ANK 5451da177e4SLinus Torvalds */ 54600fd38d9SEric Dumazet if (sk->sk_shutdown == SHUTDOWN_MASK || state == TCP_CLOSE) 547a9a08845SLinus Torvalds mask |= EPOLLHUP; 5481da177e4SLinus Torvalds if (sk->sk_shutdown & RCV_SHUTDOWN) 549a9a08845SLinus Torvalds mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP; 5501da177e4SLinus Torvalds 5518336886fSJerry Chu /* Connected or passive Fast Open socket? */ 55200fd38d9SEric Dumazet if (state != TCP_SYN_SENT && 553d983ea6fSEric Dumazet (state != TCP_SYN_RECV || rcu_access_pointer(tp->fastopen_rsk))) { 554c7004482SDavid S. Miller int target = sock_rcvlowat(sk, 0, INT_MAX); 555c7004482SDavid S. Miller 556d9b55bf7SEric Dumazet if (READ_ONCE(tp->urg_seq) == READ_ONCE(tp->copied_seq) && 557c7004482SDavid S. Miller !sock_flag(sk, SOCK_URGINLINE) && 558c7004482SDavid S. Miller tp->urg_data) 559b634f875SAlexandra Kossovsky target++; 560c7004482SDavid S. Miller 5618934ce2fSJohn Fastabend if (tcp_stream_is_readable(tp, target, sk)) 562a9a08845SLinus Torvalds mask |= EPOLLIN | EPOLLRDNORM; 5631da177e4SLinus Torvalds 5641da177e4SLinus Torvalds if (!(sk->sk_shutdown & SEND_SHUTDOWN)) { 56564dc6130SEric Dumazet if (sk_stream_is_writeable(sk)) { 566a9a08845SLinus Torvalds mask |= EPOLLOUT | EPOLLWRNORM; 5671da177e4SLinus Torvalds } else { /* send SIGIO later */ 5689cd3e072SEric Dumazet sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk); 5691da177e4SLinus Torvalds set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 5701da177e4SLinus Torvalds 5711da177e4SLinus Torvalds /* Race breaker. If space is freed after 5721da177e4SLinus Torvalds * wspace test but before the flags are set, 5733c715127Sjbaron@akamai.com * IO signal will be lost. Memory barrier 5743c715127Sjbaron@akamai.com * pairs with the input side. 5751da177e4SLinus Torvalds */ 5763c715127Sjbaron@akamai.com smp_mb__after_atomic(); 57764dc6130SEric Dumazet if (sk_stream_is_writeable(sk)) 578a9a08845SLinus Torvalds mask |= EPOLLOUT | EPOLLWRNORM; 5791da177e4SLinus Torvalds } 580d84ba638SKOSAKI Motohiro } else 581a9a08845SLinus Torvalds mask |= EPOLLOUT | EPOLLWRNORM; 5821da177e4SLinus Torvalds 5831da177e4SLinus Torvalds if (tp->urg_data & TCP_URG_VALID) 584a9a08845SLinus Torvalds mask |= EPOLLPRI; 585d68be71eSDavide Caratti } else if (state == TCP_SYN_SENT && inet_sk(sk)->defer_connect) { 58619f6d3f3SWei Wang /* Active TCP fastopen socket with defer_connect 587a9a08845SLinus Torvalds * Return EPOLLOUT so application can call write() 58819f6d3f3SWei Wang * in order for kernel to generate SYN+data 58919f6d3f3SWei Wang */ 590a9a08845SLinus Torvalds mask |= EPOLLOUT | EPOLLWRNORM; 5911da177e4SLinus Torvalds } 592a4d25803STom Marshall /* This barrier is coupled with smp_wmb() in tcp_reset() */ 593a4d25803STom Marshall smp_rmb(); 5943ef7cf57SEric Dumazet if (sk->sk_err || !skb_queue_empty_lockless(&sk->sk_error_queue)) 595a9a08845SLinus Torvalds mask |= EPOLLERR; 596a4d25803STom Marshall 5971da177e4SLinus Torvalds return mask; 5981da177e4SLinus Torvalds } 599a11e1d43SLinus Torvalds EXPORT_SYMBOL(tcp_poll); 6001da177e4SLinus Torvalds 6011da177e4SLinus Torvalds int tcp_ioctl(struct sock *sk, int cmd, unsigned long arg) 6021da177e4SLinus Torvalds { 6031da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 6041da177e4SLinus Torvalds int answ; 6050e71c55cSEric Dumazet bool slow; 6061da177e4SLinus Torvalds 6071da177e4SLinus Torvalds switch (cmd) { 6081da177e4SLinus Torvalds case SIOCINQ: 6091da177e4SLinus Torvalds if (sk->sk_state == TCP_LISTEN) 6101da177e4SLinus Torvalds return -EINVAL; 6111da177e4SLinus Torvalds 6120e71c55cSEric Dumazet slow = lock_sock_fast(sk); 613473bd239STom Herbert answ = tcp_inq(sk); 6140e71c55cSEric Dumazet unlock_sock_fast(sk, slow); 6151da177e4SLinus Torvalds break; 6161da177e4SLinus Torvalds case SIOCATMARK: 617d9b55bf7SEric Dumazet answ = tp->urg_data && 618d9b55bf7SEric Dumazet READ_ONCE(tp->urg_seq) == READ_ONCE(tp->copied_seq); 6191da177e4SLinus Torvalds break; 6201da177e4SLinus Torvalds case SIOCOUTQ: 6211da177e4SLinus Torvalds if (sk->sk_state == TCP_LISTEN) 6221da177e4SLinus Torvalds return -EINVAL; 6231da177e4SLinus Torvalds 6241da177e4SLinus Torvalds if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) 6251da177e4SLinus Torvalds answ = 0; 6261da177e4SLinus Torvalds else 6270f317464SEric Dumazet answ = READ_ONCE(tp->write_seq) - tp->snd_una; 6281da177e4SLinus Torvalds break; 6292f4e1b39SMario Schuknecht case SIOCOUTQNSD: 6302f4e1b39SMario Schuknecht if (sk->sk_state == TCP_LISTEN) 6312f4e1b39SMario Schuknecht return -EINVAL; 6322f4e1b39SMario Schuknecht 6332f4e1b39SMario Schuknecht if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) 6342f4e1b39SMario Schuknecht answ = 0; 6352f4e1b39SMario Schuknecht else 636e0d694d6SEric Dumazet answ = READ_ONCE(tp->write_seq) - 637e0d694d6SEric Dumazet READ_ONCE(tp->snd_nxt); 6382f4e1b39SMario Schuknecht break; 6391da177e4SLinus Torvalds default: 6401da177e4SLinus Torvalds return -ENOIOCTLCMD; 6413ff50b79SStephen Hemminger } 6421da177e4SLinus Torvalds 6431da177e4SLinus Torvalds return put_user(answ, (int __user *)arg); 6441da177e4SLinus Torvalds } 6454bc2f18bSEric Dumazet EXPORT_SYMBOL(tcp_ioctl); 6461da177e4SLinus Torvalds 6471da177e4SLinus Torvalds static inline void tcp_mark_push(struct tcp_sock *tp, struct sk_buff *skb) 6481da177e4SLinus Torvalds { 6494de075e0SEric Dumazet TCP_SKB_CB(skb)->tcp_flags |= TCPHDR_PSH; 6501da177e4SLinus Torvalds tp->pushed_seq = tp->write_seq; 6511da177e4SLinus Torvalds } 6521da177e4SLinus Torvalds 653a2a385d6SEric Dumazet static inline bool forced_push(const struct tcp_sock *tp) 6541da177e4SLinus Torvalds { 6551da177e4SLinus Torvalds return after(tp->write_seq, tp->pushed_seq + (tp->max_window >> 1)); 6561da177e4SLinus Torvalds } 6571da177e4SLinus Torvalds 658f4a775d1SEric Dumazet static void skb_entail(struct sock *sk, struct sk_buff *skb) 6591da177e4SLinus Torvalds { 6609e412ba7SIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk); 661352d4800SArnaldo Carvalho de Melo struct tcp_skb_cb *tcb = TCP_SKB_CB(skb); 662352d4800SArnaldo Carvalho de Melo 6631da177e4SLinus Torvalds skb->csum = 0; 664352d4800SArnaldo Carvalho de Melo tcb->seq = tcb->end_seq = tp->write_seq; 6654de075e0SEric Dumazet tcb->tcp_flags = TCPHDR_ACK; 666352d4800SArnaldo Carvalho de Melo tcb->sacked = 0; 667f4a775d1SEric Dumazet __skb_header_release(skb); 668fe067e8aSDavid S. Miller tcp_add_write_queue_tail(sk, skb); 669ab4e846aSEric Dumazet sk_wmem_queued_add(sk, skb->truesize); 6703ab224beSHideo Aoki sk_mem_charge(sk, skb->truesize); 67189ebd197SDavid S. Miller if (tp->nonagle & TCP_NAGLE_PUSH) 6721da177e4SLinus Torvalds tp->nonagle &= ~TCP_NAGLE_PUSH; 6736f021c62SEric Dumazet 6746f021c62SEric Dumazet tcp_slow_start_after_idle_check(sk); 6751da177e4SLinus Torvalds } 6761da177e4SLinus Torvalds 677afeca340SKrishna Kumar static inline void tcp_mark_urg(struct tcp_sock *tp, int flags) 6781da177e4SLinus Torvalds { 67933f5f57eSIlpo Järvinen if (flags & MSG_OOB) 6801da177e4SLinus Torvalds tp->snd_up = tp->write_seq; 6811da177e4SLinus Torvalds } 6821da177e4SLinus Torvalds 683f54b3111SEric Dumazet /* If a not yet filled skb is pushed, do not send it if 684a181ceb5SEric Dumazet * we have data packets in Qdisc or NIC queues : 685f54b3111SEric Dumazet * Because TX completion will happen shortly, it gives a chance 686f54b3111SEric Dumazet * to coalesce future sendmsg() payload into this skb, without 687f54b3111SEric Dumazet * need for a timer, and with no latency trade off. 688f54b3111SEric Dumazet * As packets containing data payload have a bigger truesize 689a181ceb5SEric Dumazet * than pure acks (dataless) packets, the last checks prevent 690a181ceb5SEric Dumazet * autocorking if we only have an ACK in Qdisc/NIC queues, 691a181ceb5SEric Dumazet * or if TX completion was delayed after we processed ACK packet. 692f54b3111SEric Dumazet */ 693f54b3111SEric Dumazet static bool tcp_should_autocork(struct sock *sk, struct sk_buff *skb, 694f54b3111SEric Dumazet int size_goal) 6951da177e4SLinus Torvalds { 696f54b3111SEric Dumazet return skb->len < size_goal && 697790f00e1SEric Dumazet sock_net(sk)->ipv4.sysctl_tcp_autocorking && 698114f39feSEric Dumazet !tcp_rtx_queue_empty(sk) && 69914afee4bSReshetova, Elena refcount_read(&sk->sk_wmem_alloc) > skb->truesize; 700f54b3111SEric Dumazet } 7019e412ba7SIlpo Järvinen 70235b2c321SMat Martineau void tcp_push(struct sock *sk, int flags, int mss_now, 703f54b3111SEric Dumazet int nonagle, int size_goal) 704f54b3111SEric Dumazet { 705f54b3111SEric Dumazet struct tcp_sock *tp = tcp_sk(sk); 706f54b3111SEric Dumazet struct sk_buff *skb; 707f54b3111SEric Dumazet 708f54b3111SEric Dumazet skb = tcp_write_queue_tail(sk); 70975c119afSEric Dumazet if (!skb) 71075c119afSEric Dumazet return; 7111da177e4SLinus Torvalds if (!(flags & MSG_MORE) || forced_push(tp)) 712f54b3111SEric Dumazet tcp_mark_push(tp, skb); 713afeca340SKrishna Kumar 714afeca340SKrishna Kumar tcp_mark_urg(tp, flags); 715f54b3111SEric Dumazet 716f54b3111SEric Dumazet if (tcp_should_autocork(sk, skb, size_goal)) { 717f54b3111SEric Dumazet 718f54b3111SEric Dumazet /* avoid atomic op if TSQ_THROTTLED bit is already set */ 7197aa5470cSEric Dumazet if (!test_bit(TSQ_THROTTLED, &sk->sk_tsq_flags)) { 720f54b3111SEric Dumazet NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPAUTOCORKING); 7217aa5470cSEric Dumazet set_bit(TSQ_THROTTLED, &sk->sk_tsq_flags); 7221da177e4SLinus Torvalds } 723a181ceb5SEric Dumazet /* It is possible TX completion already happened 724a181ceb5SEric Dumazet * before we set TSQ_THROTTLED. 725a181ceb5SEric Dumazet */ 72614afee4bSReshetova, Elena if (refcount_read(&sk->sk_wmem_alloc) > skb->truesize) 727f54b3111SEric Dumazet return; 728f54b3111SEric Dumazet } 729f54b3111SEric Dumazet 730f54b3111SEric Dumazet if (flags & MSG_MORE) 731f54b3111SEric Dumazet nonagle = TCP_NAGLE_CORK; 732f54b3111SEric Dumazet 733f54b3111SEric Dumazet __tcp_push_pending_frames(sk, mss_now, nonagle); 7341da177e4SLinus Torvalds } 7351da177e4SLinus Torvalds 7366ff7751dSAdrian Bunk static int tcp_splice_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb, 7379c55e01cSJens Axboe unsigned int offset, size_t len) 7389c55e01cSJens Axboe { 7399c55e01cSJens Axboe struct tcp_splice_state *tss = rd_desc->arg.data; 74033966dd0SWilly Tarreau int ret; 7419c55e01cSJens Axboe 742a60e3cc7SHannes Frederic Sowa ret = skb_splice_bits(skb, skb->sk, offset, tss->pipe, 74325869262SAl Viro min(rd_desc->count, len), tss->flags); 74433966dd0SWilly Tarreau if (ret > 0) 74533966dd0SWilly Tarreau rd_desc->count -= ret; 74633966dd0SWilly Tarreau return ret; 7479c55e01cSJens Axboe } 7489c55e01cSJens Axboe 7499c55e01cSJens Axboe static int __tcp_splice_read(struct sock *sk, struct tcp_splice_state *tss) 7509c55e01cSJens Axboe { 7519c55e01cSJens Axboe /* Store TCP splice context information in read_descriptor_t. */ 7529c55e01cSJens Axboe read_descriptor_t rd_desc = { 7539c55e01cSJens Axboe .arg.data = tss, 75433966dd0SWilly Tarreau .count = tss->len, 7559c55e01cSJens Axboe }; 7569c55e01cSJens Axboe 7579c55e01cSJens Axboe return tcp_read_sock(sk, &rd_desc, tcp_splice_data_recv); 7589c55e01cSJens Axboe } 7599c55e01cSJens Axboe 7609c55e01cSJens Axboe /** 7619c55e01cSJens Axboe * tcp_splice_read - splice data from TCP socket to a pipe 7629c55e01cSJens Axboe * @sock: socket to splice from 7639c55e01cSJens Axboe * @ppos: position (not valid) 7649c55e01cSJens Axboe * @pipe: pipe to splice to 7659c55e01cSJens Axboe * @len: number of bytes to splice 7669c55e01cSJens Axboe * @flags: splice modifier flags 7679c55e01cSJens Axboe * 7689c55e01cSJens Axboe * Description: 7699c55e01cSJens Axboe * Will read pages from given socket and fill them into a pipe. 7709c55e01cSJens Axboe * 7719c55e01cSJens Axboe **/ 7729c55e01cSJens Axboe ssize_t tcp_splice_read(struct socket *sock, loff_t *ppos, 7739c55e01cSJens Axboe struct pipe_inode_info *pipe, size_t len, 7749c55e01cSJens Axboe unsigned int flags) 7759c55e01cSJens Axboe { 7769c55e01cSJens Axboe struct sock *sk = sock->sk; 7779c55e01cSJens Axboe struct tcp_splice_state tss = { 7789c55e01cSJens Axboe .pipe = pipe, 7799c55e01cSJens Axboe .len = len, 7809c55e01cSJens Axboe .flags = flags, 7819c55e01cSJens Axboe }; 7829c55e01cSJens Axboe long timeo; 7839c55e01cSJens Axboe ssize_t spliced; 7849c55e01cSJens Axboe int ret; 7859c55e01cSJens Axboe 7863a047bf8SChangli Gao sock_rps_record_flow(sk); 7879c55e01cSJens Axboe /* 7889c55e01cSJens Axboe * We can't seek on a socket input 7899c55e01cSJens Axboe */ 7909c55e01cSJens Axboe if (unlikely(*ppos)) 7919c55e01cSJens Axboe return -ESPIPE; 7929c55e01cSJens Axboe 7939c55e01cSJens Axboe ret = spliced = 0; 7949c55e01cSJens Axboe 7959c55e01cSJens Axboe lock_sock(sk); 7969c55e01cSJens Axboe 79742324c62SEric Dumazet timeo = sock_rcvtimeo(sk, sock->file->f_flags & O_NONBLOCK); 7989c55e01cSJens Axboe while (tss.len) { 7999c55e01cSJens Axboe ret = __tcp_splice_read(sk, &tss); 8009c55e01cSJens Axboe if (ret < 0) 8019c55e01cSJens Axboe break; 8029c55e01cSJens Axboe else if (!ret) { 8039c55e01cSJens Axboe if (spliced) 8049c55e01cSJens Axboe break; 8059c55e01cSJens Axboe if (sock_flag(sk, SOCK_DONE)) 8069c55e01cSJens Axboe break; 8079c55e01cSJens Axboe if (sk->sk_err) { 8089c55e01cSJens Axboe ret = sock_error(sk); 8099c55e01cSJens Axboe break; 8109c55e01cSJens Axboe } 8119c55e01cSJens Axboe if (sk->sk_shutdown & RCV_SHUTDOWN) 8129c55e01cSJens Axboe break; 8139c55e01cSJens Axboe if (sk->sk_state == TCP_CLOSE) { 8149c55e01cSJens Axboe /* 8159c55e01cSJens Axboe * This occurs when user tries to read 8169c55e01cSJens Axboe * from never connected socket. 8179c55e01cSJens Axboe */ 8189c55e01cSJens Axboe ret = -ENOTCONN; 8199c55e01cSJens Axboe break; 8209c55e01cSJens Axboe } 8219c55e01cSJens Axboe if (!timeo) { 8229c55e01cSJens Axboe ret = -EAGAIN; 8239c55e01cSJens Axboe break; 8249c55e01cSJens Axboe } 825ccf7abb9SEric Dumazet /* if __tcp_splice_read() got nothing while we have 826ccf7abb9SEric Dumazet * an skb in receive queue, we do not want to loop. 827ccf7abb9SEric Dumazet * This might happen with URG data. 828ccf7abb9SEric Dumazet */ 829ccf7abb9SEric Dumazet if (!skb_queue_empty(&sk->sk_receive_queue)) 830ccf7abb9SEric Dumazet break; 831dfbafc99SSabrina Dubroca sk_wait_data(sk, &timeo, NULL); 8329c55e01cSJens Axboe if (signal_pending(current)) { 8339c55e01cSJens Axboe ret = sock_intr_errno(timeo); 8349c55e01cSJens Axboe break; 8359c55e01cSJens Axboe } 8369c55e01cSJens Axboe continue; 8379c55e01cSJens Axboe } 8389c55e01cSJens Axboe tss.len -= ret; 8399c55e01cSJens Axboe spliced += ret; 8409c55e01cSJens Axboe 84133966dd0SWilly Tarreau if (!timeo) 84233966dd0SWilly Tarreau break; 8439c55e01cSJens Axboe release_sock(sk); 8449c55e01cSJens Axboe lock_sock(sk); 8459c55e01cSJens Axboe 8469c55e01cSJens Axboe if (sk->sk_err || sk->sk_state == TCP_CLOSE || 84733966dd0SWilly Tarreau (sk->sk_shutdown & RCV_SHUTDOWN) || 8489c55e01cSJens Axboe signal_pending(current)) 8499c55e01cSJens Axboe break; 8509c55e01cSJens Axboe } 8519c55e01cSJens Axboe 8529c55e01cSJens Axboe release_sock(sk); 8539c55e01cSJens Axboe 8549c55e01cSJens Axboe if (spliced) 8559c55e01cSJens Axboe return spliced; 8569c55e01cSJens Axboe 8579c55e01cSJens Axboe return ret; 8589c55e01cSJens Axboe } 8594bc2f18bSEric Dumazet EXPORT_SYMBOL(tcp_splice_read); 8609c55e01cSJens Axboe 861eb934478SEric Dumazet struct sk_buff *sk_stream_alloc_skb(struct sock *sk, int size, gfp_t gfp, 862eb934478SEric Dumazet bool force_schedule) 863f561d0f2SPavel Emelyanov { 864f561d0f2SPavel Emelyanov struct sk_buff *skb; 865f561d0f2SPavel Emelyanov 8664f661542SEric Dumazet if (likely(!size)) { 867472c2e07SEric Dumazet skb = sk->sk_tx_skb_cache; 868858f5017SEric Dumazet if (skb) { 869d7cc399eSEric Dumazet skb->truesize = SKB_TRUESIZE(skb_end_offset(skb)); 870472c2e07SEric Dumazet sk->sk_tx_skb_cache = NULL; 871472c2e07SEric Dumazet pskb_trim(skb, 0); 872472c2e07SEric Dumazet INIT_LIST_HEAD(&skb->tcp_tsorted_anchor); 873eb70a1aeSEric Dumazet skb_shinfo(skb)->tx_flags = 0; 874eb70a1aeSEric Dumazet memset(TCP_SKB_CB(skb), 0, sizeof(struct tcp_skb_cb)); 875472c2e07SEric Dumazet return skb; 876472c2e07SEric Dumazet } 877472c2e07SEric Dumazet } 878f561d0f2SPavel Emelyanov /* The TCP header must be at least 32-bit aligned. */ 879f561d0f2SPavel Emelyanov size = ALIGN(size, 4); 880f561d0f2SPavel Emelyanov 8818e4d980aSEric Dumazet if (unlikely(tcp_under_memory_pressure(sk))) 8828e4d980aSEric Dumazet sk_mem_reclaim_partial(sk); 8838e4d980aSEric Dumazet 884f561d0f2SPavel Emelyanov skb = alloc_skb_fclone(size + sk->sk_prot->max_header, gfp); 8858e4d980aSEric Dumazet if (likely(skb)) { 886eb934478SEric Dumazet bool mem_scheduled; 8878e4d980aSEric Dumazet 888eb934478SEric Dumazet if (force_schedule) { 889eb934478SEric Dumazet mem_scheduled = true; 8908e4d980aSEric Dumazet sk_forced_mem_schedule(sk, skb->truesize); 8918e4d980aSEric Dumazet } else { 892eb934478SEric Dumazet mem_scheduled = sk_wmem_schedule(sk, skb->truesize); 8938e4d980aSEric Dumazet } 894eb934478SEric Dumazet if (likely(mem_scheduled)) { 895a21d4572SEric Dumazet skb_reserve(skb, sk->sk_prot->max_header); 896f561d0f2SPavel Emelyanov /* 897f561d0f2SPavel Emelyanov * Make sure that we have exactly size bytes 898f561d0f2SPavel Emelyanov * available to the caller, no more, no less. 899f561d0f2SPavel Emelyanov */ 90016fad69cSEric Dumazet skb->reserved_tailroom = skb->end - skb->tail - size; 901e2080072SEric Dumazet INIT_LIST_HEAD(&skb->tcp_tsorted_anchor); 902f561d0f2SPavel Emelyanov return skb; 903f561d0f2SPavel Emelyanov } 904f561d0f2SPavel Emelyanov __kfree_skb(skb); 905f561d0f2SPavel Emelyanov } else { 9065c52ba17SPavel Emelyanov sk->sk_prot->enter_memory_pressure(sk); 907f561d0f2SPavel Emelyanov sk_stream_moderate_sndbuf(sk); 908f561d0f2SPavel Emelyanov } 909f561d0f2SPavel Emelyanov return NULL; 910f561d0f2SPavel Emelyanov } 911f561d0f2SPavel Emelyanov 9120c54b85fSIlpo Järvinen static unsigned int tcp_xmit_size_goal(struct sock *sk, u32 mss_now, 9130c54b85fSIlpo Järvinen int large_allowed) 9140c54b85fSIlpo Järvinen { 9150c54b85fSIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk); 9166c09fa09SEric Dumazet u32 new_size_goal, size_goal; 9170c54b85fSIlpo Järvinen 91874d4a8f8SEric Dumazet if (!large_allowed) 919605ad7f1SEric Dumazet return mss_now; 9200c54b85fSIlpo Järvinen 9216c09fa09SEric Dumazet /* Note : tcp_tso_autosize() will eventually split this later */ 9226c09fa09SEric Dumazet new_size_goal = sk->sk_gso_max_size - 1 - MAX_TCP_HEADER; 923605ad7f1SEric Dumazet new_size_goal = tcp_bound_to_half_wnd(tp, new_size_goal); 9242a3a041cSIlpo Järvinen 9252a3a041cSIlpo Järvinen /* We try hard to avoid divides here */ 926605ad7f1SEric Dumazet size_goal = tp->gso_segs * mss_now; 927605ad7f1SEric Dumazet if (unlikely(new_size_goal < size_goal || 928605ad7f1SEric Dumazet new_size_goal >= size_goal + mss_now)) { 929605ad7f1SEric Dumazet tp->gso_segs = min_t(u16, new_size_goal / mss_now, 9301485348dSBen Hutchings sk->sk_gso_max_segs); 931605ad7f1SEric Dumazet size_goal = tp->gso_segs * mss_now; 9320c54b85fSIlpo Järvinen } 9330c54b85fSIlpo Järvinen 934605ad7f1SEric Dumazet return max(size_goal, mss_now); 9350c54b85fSIlpo Järvinen } 9360c54b85fSIlpo Järvinen 93735b2c321SMat Martineau int tcp_send_mss(struct sock *sk, int *size_goal, int flags) 9380c54b85fSIlpo Järvinen { 9390c54b85fSIlpo Järvinen int mss_now; 9400c54b85fSIlpo Järvinen 9410c54b85fSIlpo Järvinen mss_now = tcp_current_mss(sk); 9420c54b85fSIlpo Järvinen *size_goal = tcp_xmit_size_goal(sk, mss_now, !(flags & MSG_OOB)); 9430c54b85fSIlpo Järvinen 9440c54b85fSIlpo Järvinen return mss_now; 9450c54b85fSIlpo Järvinen } 9460c54b85fSIlpo Järvinen 947fdfc5c85SEric Dumazet /* In some cases, both sendpage() and sendmsg() could have added 948fdfc5c85SEric Dumazet * an skb to the write queue, but failed adding payload on it. 949fdfc5c85SEric Dumazet * We need to remove it to consume less memory, but more 950fdfc5c85SEric Dumazet * importantly be able to generate EPOLLOUT for Edge Trigger epoll() 951fdfc5c85SEric Dumazet * users. 952fdfc5c85SEric Dumazet */ 953fdfc5c85SEric Dumazet static void tcp_remove_empty_skb(struct sock *sk, struct sk_buff *skb) 954fdfc5c85SEric Dumazet { 955fdfc5c85SEric Dumazet if (skb && !skb->len) { 956fdfc5c85SEric Dumazet tcp_unlink_write_queue(skb, sk); 957fdfc5c85SEric Dumazet if (tcp_write_queue_empty(sk)) 958fdfc5c85SEric Dumazet tcp_chrono_stop(sk, TCP_CHRONO_BUSY); 959fdfc5c85SEric Dumazet sk_wmem_free_skb(sk, skb); 960fdfc5c85SEric Dumazet } 961fdfc5c85SEric Dumazet } 962fdfc5c85SEric Dumazet 963e3b5616aSDave Watson ssize_t do_tcp_sendpages(struct sock *sk, struct page *page, int offset, 96464022d0bSEric Dumazet size_t size, int flags) 9651da177e4SLinus Torvalds { 9661da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 967c1b4a7e6SDavid S. Miller int mss_now, size_goal; 9681da177e4SLinus Torvalds int err; 9691da177e4SLinus Torvalds ssize_t copied; 9701da177e4SLinus Torvalds long timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT); 9711da177e4SLinus Torvalds 972a10674bfSVasily Averin if (IS_ENABLED(CONFIG_DEBUG_VM) && 973a10674bfSVasily Averin WARN_ONCE(PageSlab(page), "page must not be a Slab one")) 974a10674bfSVasily Averin return -EINVAL; 975a10674bfSVasily Averin 9768336886fSJerry Chu /* Wait for a connection to finish. One exception is TCP Fast Open 9778336886fSJerry Chu * (passive side) where data is allowed to be sent before a connection 9788336886fSJerry Chu * is fully established. 9798336886fSJerry Chu */ 9808336886fSJerry Chu if (((1 << sk->sk_state) & ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) && 9818336886fSJerry Chu !tcp_passive_fastopen(sk)) { 982686a5624SYuvaraja Mariappan err = sk_stream_wait_connect(sk, &timeo); 983686a5624SYuvaraja Mariappan if (err != 0) 9841da177e4SLinus Torvalds goto out_err; 9858336886fSJerry Chu } 9861da177e4SLinus Torvalds 9879cd3e072SEric Dumazet sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk); 9881da177e4SLinus Torvalds 9890c54b85fSIlpo Järvinen mss_now = tcp_send_mss(sk, &size_goal, flags); 9901da177e4SLinus Torvalds copied = 0; 9911da177e4SLinus Torvalds 9921da177e4SLinus Torvalds err = -EPIPE; 9931da177e4SLinus Torvalds if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN)) 9940d6a775eSIlpo Järvinen goto out_err; 9951da177e4SLinus Torvalds 99664022d0bSEric Dumazet while (size > 0) { 997fe067e8aSDavid S. Miller struct sk_buff *skb = tcp_write_queue_tail(sk); 99838ba0a65SEric Dumazet int copy, i; 99938ba0a65SEric Dumazet bool can_coalesce; 10001da177e4SLinus Torvalds 100175c119afSEric Dumazet if (!skb || (copy = size_goal - skb->len) <= 0 || 1002c134ecb8SMartin KaFai Lau !tcp_skb_can_collapse_to(skb)) { 10031da177e4SLinus Torvalds new_segment: 10041da177e4SLinus Torvalds if (!sk_stream_memory_free(sk)) 10051da177e4SLinus Torvalds goto wait_for_sndbuf; 10061da177e4SLinus Torvalds 1007eb934478SEric Dumazet skb = sk_stream_alloc_skb(sk, 0, sk->sk_allocation, 100875c119afSEric Dumazet tcp_rtx_and_write_queues_empty(sk)); 10091da177e4SLinus Torvalds if (!skb) 10101da177e4SLinus Torvalds goto wait_for_memory; 10111da177e4SLinus Torvalds 101241477662SJakub Kicinski #ifdef CONFIG_TLS_DEVICE 101341477662SJakub Kicinski skb->decrypted = !!(flags & MSG_SENDPAGE_DECRYPTED); 101441477662SJakub Kicinski #endif 10159e412ba7SIlpo Järvinen skb_entail(sk, skb); 1016c1b4a7e6SDavid S. Miller copy = size_goal; 10171da177e4SLinus Torvalds } 10181da177e4SLinus Torvalds 10191da177e4SLinus Torvalds if (copy > size) 10201da177e4SLinus Torvalds copy = size; 10211da177e4SLinus Torvalds 10221da177e4SLinus Torvalds i = skb_shinfo(skb)->nr_frags; 10231da177e4SLinus Torvalds can_coalesce = skb_can_coalesce(skb, i, page, offset); 10245f74f82eSHans Westgaard Ry if (!can_coalesce && i >= sysctl_max_skb_frags) { 10251da177e4SLinus Torvalds tcp_mark_push(tp, skb); 10261da177e4SLinus Torvalds goto new_segment; 10271da177e4SLinus Torvalds } 10283ab224beSHideo Aoki if (!sk_wmem_schedule(sk, copy)) 10291da177e4SLinus Torvalds goto wait_for_memory; 10301da177e4SLinus Torvalds 10311da177e4SLinus Torvalds if (can_coalesce) { 10329e903e08SEric Dumazet skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy); 10331da177e4SLinus Torvalds } else { 10341da177e4SLinus Torvalds get_page(page); 10351da177e4SLinus Torvalds skb_fill_page_desc(skb, i, page, offset, copy); 10361da177e4SLinus Torvalds } 1037312fc2b4SJohn Fastabend 1038312fc2b4SJohn Fastabend if (!(flags & MSG_NO_SHARED_FRAGS)) 1039c9af6db4SPravin B Shelar skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG; 1040cef401deSEric Dumazet 10411da177e4SLinus Torvalds skb->len += copy; 10421da177e4SLinus Torvalds skb->data_len += copy; 10431da177e4SLinus Torvalds skb->truesize += copy; 1044ab4e846aSEric Dumazet sk_wmem_queued_add(sk, copy); 10453ab224beSHideo Aoki sk_mem_charge(sk, copy); 104684fa7933SPatrick McHardy skb->ip_summed = CHECKSUM_PARTIAL; 10470f317464SEric Dumazet WRITE_ONCE(tp->write_seq, tp->write_seq + copy); 10481da177e4SLinus Torvalds TCP_SKB_CB(skb)->end_seq += copy; 1049cd7d8498SEric Dumazet tcp_skb_pcount_set(skb, 0); 10501da177e4SLinus Torvalds 10511da177e4SLinus Torvalds if (!copied) 10524de075e0SEric Dumazet TCP_SKB_CB(skb)->tcp_flags &= ~TCPHDR_PSH; 10531da177e4SLinus Torvalds 10541da177e4SLinus Torvalds copied += copy; 105564022d0bSEric Dumazet offset += copy; 1056686a5624SYuvaraja Mariappan size -= copy; 1057ad02c4f5SSoheil Hassas Yeganeh if (!size) 10581da177e4SLinus Torvalds goto out; 10591da177e4SLinus Torvalds 106069d15067SHerbert Xu if (skb->len < size_goal || (flags & MSG_OOB)) 10611da177e4SLinus Torvalds continue; 10621da177e4SLinus Torvalds 10631da177e4SLinus Torvalds if (forced_push(tp)) { 10641da177e4SLinus Torvalds tcp_mark_push(tp, skb); 10659e412ba7SIlpo Järvinen __tcp_push_pending_frames(sk, mss_now, TCP_NAGLE_PUSH); 1066fe067e8aSDavid S. Miller } else if (skb == tcp_send_head(sk)) 10671da177e4SLinus Torvalds tcp_push_one(sk, mss_now); 10681da177e4SLinus Torvalds continue; 10691da177e4SLinus Torvalds 10701da177e4SLinus Torvalds wait_for_sndbuf: 10711da177e4SLinus Torvalds set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 10721da177e4SLinus Torvalds wait_for_memory: 1073f54b3111SEric Dumazet tcp_push(sk, flags & ~MSG_MORE, mss_now, 1074f54b3111SEric Dumazet TCP_NAGLE_PUSH, size_goal); 10751da177e4SLinus Torvalds 1076686a5624SYuvaraja Mariappan err = sk_stream_wait_memory(sk, &timeo); 1077686a5624SYuvaraja Mariappan if (err != 0) 10781da177e4SLinus Torvalds goto do_error; 10791da177e4SLinus Torvalds 10800c54b85fSIlpo Järvinen mss_now = tcp_send_mss(sk, &size_goal, flags); 10811da177e4SLinus Torvalds } 10821da177e4SLinus Torvalds 10831da177e4SLinus Torvalds out: 1084ad02c4f5SSoheil Hassas Yeganeh if (copied) { 10854e8cc228SEric Dumazet tcp_tx_timestamp(sk, sk->sk_tsflags); 1086ad02c4f5SSoheil Hassas Yeganeh if (!(flags & MSG_SENDPAGE_NOTLAST)) 1087f54b3111SEric Dumazet tcp_push(sk, flags, mss_now, tp->nonagle, size_goal); 1088ad02c4f5SSoheil Hassas Yeganeh } 10891da177e4SLinus Torvalds return copied; 10901da177e4SLinus Torvalds 10911da177e4SLinus Torvalds do_error: 1092fdfc5c85SEric Dumazet tcp_remove_empty_skb(sk, tcp_write_queue_tail(sk)); 10931da177e4SLinus Torvalds if (copied) 10941da177e4SLinus Torvalds goto out; 10951da177e4SLinus Torvalds out_err: 1096ce5ec440SJason Baron /* make sure we wake any epoll edge trigger waiter */ 1097216808c6SEric Dumazet if (unlikely(tcp_rtx_and_write_queues_empty(sk) && err == -EAGAIN)) { 1098ce5ec440SJason Baron sk->sk_write_space(sk); 1099b0f71bd3SFrancis Yan tcp_chrono_stop(sk, TCP_CHRONO_SNDBUF_LIMITED); 1100b0f71bd3SFrancis Yan } 11011da177e4SLinus Torvalds return sk_stream_error(sk, flags, err); 11021da177e4SLinus Torvalds } 1103e3b5616aSDave Watson EXPORT_SYMBOL_GPL(do_tcp_sendpages); 11041da177e4SLinus Torvalds 1105306b13ebSTom Herbert int tcp_sendpage_locked(struct sock *sk, struct page *page, int offset, 11061da177e4SLinus Torvalds size_t size, int flags) 11071da177e4SLinus Torvalds { 1108dead7cdbSEric Dumazet if (!(sk->sk_route_caps & NETIF_F_SG)) 1109bd9dfc54SEric Dumazet return sock_no_sendpage_locked(sk, page, offset, size, flags); 11101da177e4SLinus Torvalds 1111d7722e85SSoheil Hassas Yeganeh tcp_rate_check_app_limited(sk); /* is sending application-limited? */ 1112d7722e85SSoheil Hassas Yeganeh 1113306b13ebSTom Herbert return do_tcp_sendpages(sk, page, offset, size, flags); 1114306b13ebSTom Herbert } 1115774c4673SDavid S. Miller EXPORT_SYMBOL_GPL(tcp_sendpage_locked); 1116306b13ebSTom Herbert 1117306b13ebSTom Herbert int tcp_sendpage(struct sock *sk, struct page *page, int offset, 1118306b13ebSTom Herbert size_t size, int flags) 1119306b13ebSTom Herbert { 1120306b13ebSTom Herbert int ret; 1121306b13ebSTom Herbert 1122306b13ebSTom Herbert lock_sock(sk); 1123306b13ebSTom Herbert ret = tcp_sendpage_locked(sk, page, offset, size, flags); 11241da177e4SLinus Torvalds release_sock(sk); 1125306b13ebSTom Herbert 1126306b13ebSTom Herbert return ret; 11271da177e4SLinus Torvalds } 11284bc2f18bSEric Dumazet EXPORT_SYMBOL(tcp_sendpage); 11291da177e4SLinus Torvalds 1130cf60af03SYuchung Cheng void tcp_free_fastopen_req(struct tcp_sock *tp) 1131cf60af03SYuchung Cheng { 113200db4124SIan Morris if (tp->fastopen_req) { 1133cf60af03SYuchung Cheng kfree(tp->fastopen_req); 1134cf60af03SYuchung Cheng tp->fastopen_req = NULL; 1135cf60af03SYuchung Cheng } 1136cf60af03SYuchung Cheng } 1137cf60af03SYuchung Cheng 1138f5ddcbbbSEric Dumazet static int tcp_sendmsg_fastopen(struct sock *sk, struct msghdr *msg, 1139f859a448SWillem de Bruijn int *copied, size_t size, 1140f859a448SWillem de Bruijn struct ubuf_info *uarg) 1141cf60af03SYuchung Cheng { 1142cf60af03SYuchung Cheng struct tcp_sock *tp = tcp_sk(sk); 114319f6d3f3SWei Wang struct inet_sock *inet = inet_sk(sk); 1144ba615f67SWei Wang struct sockaddr *uaddr = msg->msg_name; 1145cf60af03SYuchung Cheng int err, flags; 1146cf60af03SYuchung Cheng 1147e1cfcbe8SHaishuang Yan if (!(sock_net(sk)->ipv4.sysctl_tcp_fastopen & TFO_CLIENT_ENABLE) || 1148ba615f67SWei Wang (uaddr && msg->msg_namelen >= sizeof(uaddr->sa_family) && 1149ba615f67SWei Wang uaddr->sa_family == AF_UNSPEC)) 1150cf60af03SYuchung Cheng return -EOPNOTSUPP; 115100db4124SIan Morris if (tp->fastopen_req) 1152cf60af03SYuchung Cheng return -EALREADY; /* Another Fast Open is in progress */ 1153cf60af03SYuchung Cheng 1154cf60af03SYuchung Cheng tp->fastopen_req = kzalloc(sizeof(struct tcp_fastopen_request), 1155cf60af03SYuchung Cheng sk->sk_allocation); 115651456b29SIan Morris if (unlikely(!tp->fastopen_req)) 1157cf60af03SYuchung Cheng return -ENOBUFS; 1158cf60af03SYuchung Cheng tp->fastopen_req->data = msg; 1159f5ddcbbbSEric Dumazet tp->fastopen_req->size = size; 1160f859a448SWillem de Bruijn tp->fastopen_req->uarg = uarg; 1161cf60af03SYuchung Cheng 116219f6d3f3SWei Wang if (inet->defer_connect) { 116319f6d3f3SWei Wang err = tcp_connect(sk); 116419f6d3f3SWei Wang /* Same failure procedure as in tcp_v4/6_connect */ 116519f6d3f3SWei Wang if (err) { 116619f6d3f3SWei Wang tcp_set_state(sk, TCP_CLOSE); 116719f6d3f3SWei Wang inet->inet_dport = 0; 116819f6d3f3SWei Wang sk->sk_route_caps = 0; 116919f6d3f3SWei Wang } 117019f6d3f3SWei Wang } 1171cf60af03SYuchung Cheng flags = (msg->msg_flags & MSG_DONTWAIT) ? O_NONBLOCK : 0; 1172ba615f67SWei Wang err = __inet_stream_connect(sk->sk_socket, uaddr, 11733979ad7eSWilly Tarreau msg->msg_namelen, flags, 1); 11747db92362SWei Wang /* fastopen_req could already be freed in __inet_stream_connect 11757db92362SWei Wang * if the connection times out or gets rst 11767db92362SWei Wang */ 11777db92362SWei Wang if (tp->fastopen_req) { 1178f5ddcbbbSEric Dumazet *copied = tp->fastopen_req->copied; 1179cf60af03SYuchung Cheng tcp_free_fastopen_req(tp); 11807db92362SWei Wang inet->defer_connect = 0; 11817db92362SWei Wang } 1182cf60af03SYuchung Cheng return err; 1183cf60af03SYuchung Cheng } 1184cf60af03SYuchung Cheng 1185306b13ebSTom Herbert int tcp_sendmsg_locked(struct sock *sk, struct msghdr *msg, size_t size) 11861da177e4SLinus Torvalds { 11871da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 1188f214f915SWillem de Bruijn struct ubuf_info *uarg = NULL; 11891da177e4SLinus Torvalds struct sk_buff *skb; 1190c14ac945SSoheil Hassas Yeganeh struct sockcm_cookie sockc; 119157be5bdaSAl Viro int flags, err, copied = 0; 119257be5bdaSAl Viro int mss_now = 0, size_goal, copied_syn = 0; 11931a991488SEric Dumazet int process_backlog = 0; 119474d4a8f8SEric Dumazet bool zc = false; 11951da177e4SLinus Torvalds long timeo; 11961da177e4SLinus Torvalds 11971da177e4SLinus Torvalds flags = msg->msg_flags; 1198f214f915SWillem de Bruijn 11995cf4a853SVincent Whitchurch if (flags & MSG_ZEROCOPY && size && sock_flag(sk, SOCK_ZEROCOPY)) { 120075c119afSEric Dumazet skb = tcp_write_queue_tail(sk); 1201f214f915SWillem de Bruijn uarg = sock_zerocopy_realloc(sk, size, skb_zcopy(skb)); 1202f214f915SWillem de Bruijn if (!uarg) { 1203f214f915SWillem de Bruijn err = -ENOBUFS; 1204f214f915SWillem de Bruijn goto out_err; 1205f214f915SWillem de Bruijn } 1206f214f915SWillem de Bruijn 1207dead7cdbSEric Dumazet zc = sk->sk_route_caps & NETIF_F_SG; 120802583adeSWillem de Bruijn if (!zc) 1209f214f915SWillem de Bruijn uarg->zerocopy = 0; 1210f214f915SWillem de Bruijn } 1211f214f915SWillem de Bruijn 121216ae6aa1SYuchung Cheng if (unlikely(flags & MSG_FASTOPEN || inet_sk(sk)->defer_connect) && 121316ae6aa1SYuchung Cheng !tp->repair) { 1214f859a448SWillem de Bruijn err = tcp_sendmsg_fastopen(sk, msg, &copied_syn, size, uarg); 1215cf60af03SYuchung Cheng if (err == -EINPROGRESS && copied_syn > 0) 1216cf60af03SYuchung Cheng goto out; 1217cf60af03SYuchung Cheng else if (err) 1218cf60af03SYuchung Cheng goto out_err; 1219cf60af03SYuchung Cheng } 1220cf60af03SYuchung Cheng 12211da177e4SLinus Torvalds timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT); 12221da177e4SLinus Torvalds 1223d7722e85SSoheil Hassas Yeganeh tcp_rate_check_app_limited(sk); /* is sending application-limited? */ 1224d7722e85SSoheil Hassas Yeganeh 12258336886fSJerry Chu /* Wait for a connection to finish. One exception is TCP Fast Open 12268336886fSJerry Chu * (passive side) where data is allowed to be sent before a connection 12278336886fSJerry Chu * is fully established. 12288336886fSJerry Chu */ 12298336886fSJerry Chu if (((1 << sk->sk_state) & ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) && 12308336886fSJerry Chu !tcp_passive_fastopen(sk)) { 1231686a5624SYuvaraja Mariappan err = sk_stream_wait_connect(sk, &timeo); 1232686a5624SYuvaraja Mariappan if (err != 0) 1233cf60af03SYuchung Cheng goto do_error; 12348336886fSJerry Chu } 12351da177e4SLinus Torvalds 1236c0e88ff0SPavel Emelyanov if (unlikely(tp->repair)) { 1237c0e88ff0SPavel Emelyanov if (tp->repair_queue == TCP_RECV_QUEUE) { 1238c0e88ff0SPavel Emelyanov copied = tcp_send_rcvq(sk, msg, size); 12395924f17aSChristoph Paasch goto out_nopush; 1240c0e88ff0SPavel Emelyanov } 1241c0e88ff0SPavel Emelyanov 1242c0e88ff0SPavel Emelyanov err = -EINVAL; 1243c0e88ff0SPavel Emelyanov if (tp->repair_queue == TCP_NO_QUEUE) 1244c0e88ff0SPavel Emelyanov goto out_err; 1245c0e88ff0SPavel Emelyanov 1246c0e88ff0SPavel Emelyanov /* 'common' sending to sendq */ 1247c0e88ff0SPavel Emelyanov } 1248c0e88ff0SPavel Emelyanov 1249657a0667SWillem de Bruijn sockcm_init(&sockc, sk); 1250c14ac945SSoheil Hassas Yeganeh if (msg->msg_controllen) { 1251c14ac945SSoheil Hassas Yeganeh err = sock_cmsg_send(sk, msg, &sockc); 1252c14ac945SSoheil Hassas Yeganeh if (unlikely(err)) { 1253c14ac945SSoheil Hassas Yeganeh err = -EINVAL; 1254c14ac945SSoheil Hassas Yeganeh goto out_err; 1255c14ac945SSoheil Hassas Yeganeh } 1256c14ac945SSoheil Hassas Yeganeh } 1257c14ac945SSoheil Hassas Yeganeh 12581da177e4SLinus Torvalds /* This should be in poll */ 12599cd3e072SEric Dumazet sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk); 12601da177e4SLinus Torvalds 12611da177e4SLinus Torvalds /* Ok commence sending. */ 12621da177e4SLinus Torvalds copied = 0; 12631da177e4SLinus Torvalds 1264d41a69f1SEric Dumazet restart: 1265d41a69f1SEric Dumazet mss_now = tcp_send_mss(sk, &size_goal, flags); 1266d41a69f1SEric Dumazet 12671da177e4SLinus Torvalds err = -EPIPE; 12681da177e4SLinus Torvalds if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN)) 126979d8665bSEric Dumazet goto do_error; 12701da177e4SLinus Torvalds 127101e97e65SAl Viro while (msg_data_left(msg)) { 12726828b92bSHerbert Xu int copy = 0; 12731da177e4SLinus Torvalds 1274fe067e8aSDavid S. Miller skb = tcp_write_queue_tail(sk); 127565ec6097SEric Dumazet if (skb) 127665ec6097SEric Dumazet copy = size_goal - skb->len; 12771da177e4SLinus Torvalds 1278c134ecb8SMartin KaFai Lau if (copy <= 0 || !tcp_skb_can_collapse_to(skb)) { 12793613b3dbSEric Dumazet bool first_skb; 12803613b3dbSEric Dumazet 12811da177e4SLinus Torvalds new_segment: 12821da177e4SLinus Torvalds if (!sk_stream_memory_free(sk)) 12831da177e4SLinus Torvalds goto wait_for_sndbuf; 12841da177e4SLinus Torvalds 12851a991488SEric Dumazet if (unlikely(process_backlog >= 16)) { 12861a991488SEric Dumazet process_backlog = 0; 12871a991488SEric Dumazet if (sk_flush_backlog(sk)) 1288d41a69f1SEric Dumazet goto restart; 1289d4011239SEric Dumazet } 129075c119afSEric Dumazet first_skb = tcp_rtx_and_write_queues_empty(sk); 1291472c2e07SEric Dumazet skb = sk_stream_alloc_skb(sk, 0, sk->sk_allocation, 12923613b3dbSEric Dumazet first_skb); 12931da177e4SLinus Torvalds if (!skb) 12941da177e4SLinus Torvalds goto wait_for_memory; 12951da177e4SLinus Torvalds 12961a991488SEric Dumazet process_backlog++; 129784fa7933SPatrick McHardy skb->ip_summed = CHECKSUM_PARTIAL; 12981da177e4SLinus Torvalds 12999e412ba7SIlpo Järvinen skb_entail(sk, skb); 1300c1b4a7e6SDavid S. Miller copy = size_goal; 13019d186cacSAndrey Vagin 13029d186cacSAndrey Vagin /* All packets are restored as if they have 1303d3edd06eSEric Dumazet * already been sent. skb_mstamp_ns isn't set to 13049d186cacSAndrey Vagin * avoid wrong rtt estimation. 13059d186cacSAndrey Vagin */ 13069d186cacSAndrey Vagin if (tp->repair) 13079d186cacSAndrey Vagin TCP_SKB_CB(skb)->sacked |= TCPCB_REPAIRED; 13081da177e4SLinus Torvalds } 13091da177e4SLinus Torvalds 13101da177e4SLinus Torvalds /* Try to append data to the end of skb. */ 131101e97e65SAl Viro if (copy > msg_data_left(msg)) 131201e97e65SAl Viro copy = msg_data_left(msg); 13131da177e4SLinus Torvalds 13141da177e4SLinus Torvalds /* Where to copy to? */ 131502583adeSWillem de Bruijn if (skb_availroom(skb) > 0 && !zc) { 13161da177e4SLinus Torvalds /* We have some space in skb head. Superb! */ 1317a21d4572SEric Dumazet copy = min_t(int, copy, skb_availroom(skb)); 131857be5bdaSAl Viro err = skb_add_data_nocache(sk, skb, &msg->msg_iter, copy); 1319c6e1a0d1STom Herbert if (err) 13201da177e4SLinus Torvalds goto do_fault; 132102583adeSWillem de Bruijn } else if (!zc) { 13225640f768SEric Dumazet bool merge = true; 13231da177e4SLinus Torvalds int i = skb_shinfo(skb)->nr_frags; 13245640f768SEric Dumazet struct page_frag *pfrag = sk_page_frag(sk); 1325761965eaSEric Dumazet 13265640f768SEric Dumazet if (!sk_page_frag_refill(sk, pfrag)) 13275640f768SEric Dumazet goto wait_for_memory; 1328761965eaSEric Dumazet 13295640f768SEric Dumazet if (!skb_can_coalesce(skb, i, pfrag->page, 13305640f768SEric Dumazet pfrag->offset)) { 133174d4a8f8SEric Dumazet if (i >= sysctl_max_skb_frags) { 13321da177e4SLinus Torvalds tcp_mark_push(tp, skb); 13331da177e4SLinus Torvalds goto new_segment; 13341da177e4SLinus Torvalds } 13355640f768SEric Dumazet merge = false; 13365640f768SEric Dumazet } 1337ef015786SHerbert Xu 13385640f768SEric Dumazet copy = min_t(int, copy, pfrag->size - pfrag->offset); 1339ef015786SHerbert Xu 13403ab224beSHideo Aoki if (!sk_wmem_schedule(sk, copy)) 1341ef015786SHerbert Xu goto wait_for_memory; 13421da177e4SLinus Torvalds 134357be5bdaSAl Viro err = skb_copy_to_page_nocache(sk, &msg->msg_iter, skb, 13445640f768SEric Dumazet pfrag->page, 13455640f768SEric Dumazet pfrag->offset, 13465640f768SEric Dumazet copy); 13475640f768SEric Dumazet if (err) 13481da177e4SLinus Torvalds goto do_error; 13491da177e4SLinus Torvalds 13501da177e4SLinus Torvalds /* Update the skb. */ 13511da177e4SLinus Torvalds if (merge) { 13529e903e08SEric Dumazet skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy); 13531da177e4SLinus Torvalds } else { 13545640f768SEric Dumazet skb_fill_page_desc(skb, i, pfrag->page, 13555640f768SEric Dumazet pfrag->offset, copy); 13564e33e346SEric Dumazet page_ref_inc(pfrag->page); 13571da177e4SLinus Torvalds } 13585640f768SEric Dumazet pfrag->offset += copy; 1359f214f915SWillem de Bruijn } else { 1360f214f915SWillem de Bruijn err = skb_zerocopy_iter_stream(sk, skb, msg, copy, uarg); 1361111856c7SWillem de Bruijn if (err == -EMSGSIZE || err == -EEXIST) { 1362111856c7SWillem de Bruijn tcp_mark_push(tp, skb); 1363f214f915SWillem de Bruijn goto new_segment; 1364111856c7SWillem de Bruijn } 1365f214f915SWillem de Bruijn if (err < 0) 1366f214f915SWillem de Bruijn goto do_error; 1367f214f915SWillem de Bruijn copy = err; 13681da177e4SLinus Torvalds } 13691da177e4SLinus Torvalds 13701da177e4SLinus Torvalds if (!copied) 13714de075e0SEric Dumazet TCP_SKB_CB(skb)->tcp_flags &= ~TCPHDR_PSH; 13721da177e4SLinus Torvalds 13730f317464SEric Dumazet WRITE_ONCE(tp->write_seq, tp->write_seq + copy); 13741da177e4SLinus Torvalds TCP_SKB_CB(skb)->end_seq += copy; 1375cd7d8498SEric Dumazet tcp_skb_pcount_set(skb, 0); 13761da177e4SLinus Torvalds 13771da177e4SLinus Torvalds copied += copy; 137801e97e65SAl Viro if (!msg_data_left(msg)) { 1379c134ecb8SMartin KaFai Lau if (unlikely(flags & MSG_EOR)) 1380c134ecb8SMartin KaFai Lau TCP_SKB_CB(skb)->eor = 1; 13811da177e4SLinus Torvalds goto out; 13824ed2d765SWillem de Bruijn } 13831da177e4SLinus Torvalds 138465ec6097SEric Dumazet if (skb->len < size_goal || (flags & MSG_OOB) || unlikely(tp->repair)) 13851da177e4SLinus Torvalds continue; 13861da177e4SLinus Torvalds 13871da177e4SLinus Torvalds if (forced_push(tp)) { 13881da177e4SLinus Torvalds tcp_mark_push(tp, skb); 13899e412ba7SIlpo Järvinen __tcp_push_pending_frames(sk, mss_now, TCP_NAGLE_PUSH); 1390fe067e8aSDavid S. Miller } else if (skb == tcp_send_head(sk)) 13911da177e4SLinus Torvalds tcp_push_one(sk, mss_now); 13921da177e4SLinus Torvalds continue; 13931da177e4SLinus Torvalds 13941da177e4SLinus Torvalds wait_for_sndbuf: 13951da177e4SLinus Torvalds set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 13961da177e4SLinus Torvalds wait_for_memory: 1397ec342325SAndrew Vagin if (copied) 1398f54b3111SEric Dumazet tcp_push(sk, flags & ~MSG_MORE, mss_now, 1399f54b3111SEric Dumazet TCP_NAGLE_PUSH, size_goal); 14001da177e4SLinus Torvalds 1401686a5624SYuvaraja Mariappan err = sk_stream_wait_memory(sk, &timeo); 1402686a5624SYuvaraja Mariappan if (err != 0) 14031da177e4SLinus Torvalds goto do_error; 14041da177e4SLinus Torvalds 14050c54b85fSIlpo Järvinen mss_now = tcp_send_mss(sk, &size_goal, flags); 14061da177e4SLinus Torvalds } 14071da177e4SLinus Torvalds 14081da177e4SLinus Torvalds out: 1409ad02c4f5SSoheil Hassas Yeganeh if (copied) { 14104e8cc228SEric Dumazet tcp_tx_timestamp(sk, sockc.tsflags); 1411f54b3111SEric Dumazet tcp_push(sk, flags, mss_now, tp->nonagle, size_goal); 1412ad02c4f5SSoheil Hassas Yeganeh } 14135924f17aSChristoph Paasch out_nopush: 1414f214f915SWillem de Bruijn sock_zerocopy_put(uarg); 1415cf60af03SYuchung Cheng return copied + copied_syn; 14161da177e4SLinus Torvalds 14171da177e4SLinus Torvalds do_error: 1418fdfc5c85SEric Dumazet skb = tcp_write_queue_tail(sk); 1419fdfc5c85SEric Dumazet do_fault: 1420fdfc5c85SEric Dumazet tcp_remove_empty_skb(sk, skb); 1421fdfc5c85SEric Dumazet 1422cf60af03SYuchung Cheng if (copied + copied_syn) 14231da177e4SLinus Torvalds goto out; 14241da177e4SLinus Torvalds out_err: 142552900d22SWillem de Bruijn sock_zerocopy_put_abort(uarg, true); 14261da177e4SLinus Torvalds err = sk_stream_error(sk, flags, err); 1427ce5ec440SJason Baron /* make sure we wake any epoll edge trigger waiter */ 1428216808c6SEric Dumazet if (unlikely(tcp_rtx_and_write_queues_empty(sk) && err == -EAGAIN)) { 1429ce5ec440SJason Baron sk->sk_write_space(sk); 1430b0f71bd3SFrancis Yan tcp_chrono_stop(sk, TCP_CHRONO_SNDBUF_LIMITED); 1431b0f71bd3SFrancis Yan } 14321da177e4SLinus Torvalds return err; 14331da177e4SLinus Torvalds } 1434774c4673SDavid S. Miller EXPORT_SYMBOL_GPL(tcp_sendmsg_locked); 1435306b13ebSTom Herbert 1436306b13ebSTom Herbert int tcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t size) 1437306b13ebSTom Herbert { 1438306b13ebSTom Herbert int ret; 1439306b13ebSTom Herbert 1440306b13ebSTom Herbert lock_sock(sk); 1441306b13ebSTom Herbert ret = tcp_sendmsg_locked(sk, msg, size); 1442306b13ebSTom Herbert release_sock(sk); 1443306b13ebSTom Herbert 1444306b13ebSTom Herbert return ret; 1445306b13ebSTom Herbert } 14464bc2f18bSEric Dumazet EXPORT_SYMBOL(tcp_sendmsg); 14471da177e4SLinus Torvalds 14481da177e4SLinus Torvalds /* 14491da177e4SLinus Torvalds * Handle reading urgent data. BSD has very simple semantics for 14501da177e4SLinus Torvalds * this, no blocking and very strange errors 8) 14511da177e4SLinus Torvalds */ 14521da177e4SLinus Torvalds 1453377f0a08SRami Rosen static int tcp_recv_urg(struct sock *sk, struct msghdr *msg, int len, int flags) 14541da177e4SLinus Torvalds { 14551da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 14561da177e4SLinus Torvalds 14571da177e4SLinus Torvalds /* No URG data to read. */ 14581da177e4SLinus Torvalds if (sock_flag(sk, SOCK_URGINLINE) || !tp->urg_data || 14591da177e4SLinus Torvalds tp->urg_data == TCP_URG_READ) 14601da177e4SLinus Torvalds return -EINVAL; /* Yes this is right ! */ 14611da177e4SLinus Torvalds 14621da177e4SLinus Torvalds if (sk->sk_state == TCP_CLOSE && !sock_flag(sk, SOCK_DONE)) 14631da177e4SLinus Torvalds return -ENOTCONN; 14641da177e4SLinus Torvalds 14651da177e4SLinus Torvalds if (tp->urg_data & TCP_URG_VALID) { 14661da177e4SLinus Torvalds int err = 0; 14671da177e4SLinus Torvalds char c = tp->urg_data; 14681da177e4SLinus Torvalds 14691da177e4SLinus Torvalds if (!(flags & MSG_PEEK)) 14701da177e4SLinus Torvalds tp->urg_data = TCP_URG_READ; 14711da177e4SLinus Torvalds 14721da177e4SLinus Torvalds /* Read urgent data. */ 14731da177e4SLinus Torvalds msg->msg_flags |= MSG_OOB; 14741da177e4SLinus Torvalds 14751da177e4SLinus Torvalds if (len > 0) { 14761da177e4SLinus Torvalds if (!(flags & MSG_TRUNC)) 14777eab8d9eSAl Viro err = memcpy_to_msg(msg, &c, 1); 14781da177e4SLinus Torvalds len = 1; 14791da177e4SLinus Torvalds } else 14801da177e4SLinus Torvalds msg->msg_flags |= MSG_TRUNC; 14811da177e4SLinus Torvalds 14821da177e4SLinus Torvalds return err ? -EFAULT : len; 14831da177e4SLinus Torvalds } 14841da177e4SLinus Torvalds 14851da177e4SLinus Torvalds if (sk->sk_state == TCP_CLOSE || (sk->sk_shutdown & RCV_SHUTDOWN)) 14861da177e4SLinus Torvalds return 0; 14871da177e4SLinus Torvalds 14881da177e4SLinus Torvalds /* Fixed the recv(..., MSG_OOB) behaviour. BSD docs and 14891da177e4SLinus Torvalds * the available implementations agree in this case: 14901da177e4SLinus Torvalds * this call should never block, independent of the 14911da177e4SLinus Torvalds * blocking state of the socket. 14921da177e4SLinus Torvalds * Mike <pall@rz.uni-karlsruhe.de> 14931da177e4SLinus Torvalds */ 14941da177e4SLinus Torvalds return -EAGAIN; 14951da177e4SLinus Torvalds } 14961da177e4SLinus Torvalds 1497c0e88ff0SPavel Emelyanov static int tcp_peek_sndq(struct sock *sk, struct msghdr *msg, int len) 1498c0e88ff0SPavel Emelyanov { 1499c0e88ff0SPavel Emelyanov struct sk_buff *skb; 1500c0e88ff0SPavel Emelyanov int copied = 0, err = 0; 1501c0e88ff0SPavel Emelyanov 1502c0e88ff0SPavel Emelyanov /* XXX -- need to support SO_PEEK_OFF */ 1503c0e88ff0SPavel Emelyanov 150475c119afSEric Dumazet skb_rbtree_walk(skb, &sk->tcp_rtx_queue) { 150575c119afSEric Dumazet err = skb_copy_datagram_msg(skb, 0, msg, skb->len); 150675c119afSEric Dumazet if (err) 150775c119afSEric Dumazet return err; 150875c119afSEric Dumazet copied += skb->len; 150975c119afSEric Dumazet } 151075c119afSEric Dumazet 1511c0e88ff0SPavel Emelyanov skb_queue_walk(&sk->sk_write_queue, skb) { 151251f3d02bSDavid S. Miller err = skb_copy_datagram_msg(skb, 0, msg, skb->len); 1513c0e88ff0SPavel Emelyanov if (err) 1514c0e88ff0SPavel Emelyanov break; 1515c0e88ff0SPavel Emelyanov 1516c0e88ff0SPavel Emelyanov copied += skb->len; 1517c0e88ff0SPavel Emelyanov } 1518c0e88ff0SPavel Emelyanov 1519c0e88ff0SPavel Emelyanov return err ?: copied; 1520c0e88ff0SPavel Emelyanov } 1521c0e88ff0SPavel Emelyanov 15221da177e4SLinus Torvalds /* Clean up the receive buffer for full frames taken by the user, 15231da177e4SLinus Torvalds * then send an ACK if necessary. COPIED is the number of bytes 15241da177e4SLinus Torvalds * tcp_recvmsg has given to the user so far, it speeds up the 15251da177e4SLinus Torvalds * calculation of whether or not we must ACK for the sake of 15261da177e4SLinus Torvalds * a window update. 15271da177e4SLinus Torvalds */ 15283f334078SDan Williams static void tcp_cleanup_rbuf(struct sock *sk, int copied) 15291da177e4SLinus Torvalds { 15301da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 1531a2a385d6SEric Dumazet bool time_to_ack = false; 15321da177e4SLinus Torvalds 15331da177e4SLinus Torvalds struct sk_buff *skb = skb_peek(&sk->sk_receive_queue); 15341da177e4SLinus Torvalds 1535d792c100SIlpo Järvinen WARN(skb && !before(tp->copied_seq, TCP_SKB_CB(skb)->end_seq), 15362af6fd8bSJoe Perches "cleanup rbuf bug: copied %X seq %X rcvnxt %X\n", 1537d792c100SIlpo Järvinen tp->copied_seq, TCP_SKB_CB(skb)->end_seq, tp->rcv_nxt); 15381da177e4SLinus Torvalds 1539463c84b9SArnaldo Carvalho de Melo if (inet_csk_ack_scheduled(sk)) { 1540463c84b9SArnaldo Carvalho de Melo const struct inet_connection_sock *icsk = inet_csk(sk); 15411da177e4SLinus Torvalds /* Delayed ACKs frequently hit locked sockets during bulk 15421da177e4SLinus Torvalds * receive. */ 1543463c84b9SArnaldo Carvalho de Melo if (icsk->icsk_ack.blocked || 15441da177e4SLinus Torvalds /* Once-per-two-segments ACK was not sent by tcp_input.c */ 1545463c84b9SArnaldo Carvalho de Melo tp->rcv_nxt - tp->rcv_wup > icsk->icsk_ack.rcv_mss || 15461da177e4SLinus Torvalds /* 15471da177e4SLinus Torvalds * If this read emptied read buffer, we send ACK, if 15481da177e4SLinus Torvalds * connection is not bidirectional, user drained 15491da177e4SLinus Torvalds * receive buffer and there was a small segment 15501da177e4SLinus Torvalds * in queue. 15511da177e4SLinus Torvalds */ 15521ef9696cSAlexey Kuznetsov (copied > 0 && 15531ef9696cSAlexey Kuznetsov ((icsk->icsk_ack.pending & ICSK_ACK_PUSHED2) || 15541ef9696cSAlexey Kuznetsov ((icsk->icsk_ack.pending & ICSK_ACK_PUSHED) && 155531954cd8SWei Wang !inet_csk_in_pingpong_mode(sk))) && 15561ef9696cSAlexey Kuznetsov !atomic_read(&sk->sk_rmem_alloc))) 1557a2a385d6SEric Dumazet time_to_ack = true; 15581da177e4SLinus Torvalds } 15591da177e4SLinus Torvalds 15601da177e4SLinus Torvalds /* We send an ACK if we can now advertise a non-zero window 15611da177e4SLinus Torvalds * which has been raised "significantly". 15621da177e4SLinus Torvalds * 15631da177e4SLinus Torvalds * Even if window raised up to infinity, do not send window open ACK 15641da177e4SLinus Torvalds * in states, where we will not receive more. It is useless. 15651da177e4SLinus Torvalds */ 15661da177e4SLinus Torvalds if (copied > 0 && !time_to_ack && !(sk->sk_shutdown & RCV_SHUTDOWN)) { 15671da177e4SLinus Torvalds __u32 rcv_window_now = tcp_receive_window(tp); 15681da177e4SLinus Torvalds 15691da177e4SLinus Torvalds /* Optimize, __tcp_select_window() is not cheap. */ 15701da177e4SLinus Torvalds if (2*rcv_window_now <= tp->window_clamp) { 15711da177e4SLinus Torvalds __u32 new_window = __tcp_select_window(sk); 15721da177e4SLinus Torvalds 15731da177e4SLinus Torvalds /* Send ACK now, if this read freed lots of space 15741da177e4SLinus Torvalds * in our buffer. Certainly, new_window is new window. 15751da177e4SLinus Torvalds * We can advertise it now, if it is not less than current one. 15761da177e4SLinus Torvalds * "Lots" means "at least twice" here. 15771da177e4SLinus Torvalds */ 15781da177e4SLinus Torvalds if (new_window && new_window >= 2 * rcv_window_now) 1579a2a385d6SEric Dumazet time_to_ack = true; 15801da177e4SLinus Torvalds } 15811da177e4SLinus Torvalds } 15821da177e4SLinus Torvalds if (time_to_ack) 15831da177e4SLinus Torvalds tcp_send_ack(sk); 15841da177e4SLinus Torvalds } 15851da177e4SLinus Torvalds 1586f26845b4SEric Dumazet static struct sk_buff *tcp_recv_skb(struct sock *sk, u32 seq, u32 *off) 15871da177e4SLinus Torvalds { 15881da177e4SLinus Torvalds struct sk_buff *skb; 15891da177e4SLinus Torvalds u32 offset; 15901da177e4SLinus Torvalds 1591f26845b4SEric Dumazet while ((skb = skb_peek(&sk->sk_receive_queue)) != NULL) { 15921da177e4SLinus Torvalds offset = seq - TCP_SKB_CB(skb)->seq; 15939d691539SEric Dumazet if (unlikely(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_SYN)) { 15949d691539SEric Dumazet pr_err_once("%s: found a SYN, please report !\n", __func__); 15951da177e4SLinus Torvalds offset--; 15969d691539SEric Dumazet } 1597e11ecddfSEric Dumazet if (offset < skb->len || (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN)) { 15981da177e4SLinus Torvalds *off = offset; 15991da177e4SLinus Torvalds return skb; 16001da177e4SLinus Torvalds } 1601f26845b4SEric Dumazet /* This looks weird, but this can happen if TCP collapsing 1602f26845b4SEric Dumazet * splitted a fat GRO packet, while we released socket lock 1603f26845b4SEric Dumazet * in skb_splice_bits() 1604f26845b4SEric Dumazet */ 16057bced397SDan Williams sk_eat_skb(sk, skb); 16061da177e4SLinus Torvalds } 16071da177e4SLinus Torvalds return NULL; 16081da177e4SLinus Torvalds } 16091da177e4SLinus Torvalds 16101da177e4SLinus Torvalds /* 16111da177e4SLinus Torvalds * This routine provides an alternative to tcp_recvmsg() for routines 16121da177e4SLinus Torvalds * that would like to handle copying from skbuffs directly in 'sendfile' 16131da177e4SLinus Torvalds * fashion. 16141da177e4SLinus Torvalds * Note: 16151da177e4SLinus Torvalds * - It is assumed that the socket was locked by the caller. 16161da177e4SLinus Torvalds * - The routine does not block. 16171da177e4SLinus Torvalds * - At present, there is no support for reading OOB data 16181da177e4SLinus Torvalds * or for 'peeking' the socket using this routine 16191da177e4SLinus Torvalds * (although both would be easy to implement). 16201da177e4SLinus Torvalds */ 16211da177e4SLinus Torvalds int tcp_read_sock(struct sock *sk, read_descriptor_t *desc, 16221da177e4SLinus Torvalds sk_read_actor_t recv_actor) 16231da177e4SLinus Torvalds { 16241da177e4SLinus Torvalds struct sk_buff *skb; 16251da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 16261da177e4SLinus Torvalds u32 seq = tp->copied_seq; 16271da177e4SLinus Torvalds u32 offset; 16281da177e4SLinus Torvalds int copied = 0; 16291da177e4SLinus Torvalds 16301da177e4SLinus Torvalds if (sk->sk_state == TCP_LISTEN) 16311da177e4SLinus Torvalds return -ENOTCONN; 16321da177e4SLinus Torvalds while ((skb = tcp_recv_skb(sk, seq, &offset)) != NULL) { 16331da177e4SLinus Torvalds if (offset < skb->len) { 1634374e7b59SOctavian Purdila int used; 1635374e7b59SOctavian Purdila size_t len; 16361da177e4SLinus Torvalds 16371da177e4SLinus Torvalds len = skb->len - offset; 16381da177e4SLinus Torvalds /* Stop reading if we hit a patch of urgent data */ 16391da177e4SLinus Torvalds if (tp->urg_data) { 16401da177e4SLinus Torvalds u32 urg_offset = tp->urg_seq - seq; 16411da177e4SLinus Torvalds if (urg_offset < len) 16421da177e4SLinus Torvalds len = urg_offset; 16431da177e4SLinus Torvalds if (!len) 16441da177e4SLinus Torvalds break; 16451da177e4SLinus Torvalds } 16461da177e4SLinus Torvalds used = recv_actor(desc, skb, offset, len); 1647ff905b1eSEric Dumazet if (used <= 0) { 1648ddb61a57SJens Axboe if (!copied) 1649ddb61a57SJens Axboe copied = used; 1650ddb61a57SJens Axboe break; 1651ddb61a57SJens Axboe } else if (used <= len) { 16521da177e4SLinus Torvalds seq += used; 16531da177e4SLinus Torvalds copied += used; 16541da177e4SLinus Torvalds offset += used; 16551da177e4SLinus Torvalds } 165602275a2eSWilly Tarreau /* If recv_actor drops the lock (e.g. TCP splice 1657293ad604SOctavian Purdila * receive) the skb pointer might be invalid when 1658293ad604SOctavian Purdila * getting here: tcp_collapse might have deleted it 1659293ad604SOctavian Purdila * while aggregating skbs from the socket queue. 1660293ad604SOctavian Purdila */ 1661293ad604SOctavian Purdila skb = tcp_recv_skb(sk, seq - 1, &offset); 166202275a2eSWilly Tarreau if (!skb) 16631da177e4SLinus Torvalds break; 166402275a2eSWilly Tarreau /* TCP coalescing might have appended data to the skb. 166502275a2eSWilly Tarreau * Try to splice more frags 166602275a2eSWilly Tarreau */ 166702275a2eSWilly Tarreau if (offset + 1 != skb->len) 166802275a2eSWilly Tarreau continue; 16691da177e4SLinus Torvalds } 1670e11ecddfSEric Dumazet if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN) { 16717bced397SDan Williams sk_eat_skb(sk, skb); 16721da177e4SLinus Torvalds ++seq; 16731da177e4SLinus Torvalds break; 16741da177e4SLinus Torvalds } 16757bced397SDan Williams sk_eat_skb(sk, skb); 16761da177e4SLinus Torvalds if (!desc->count) 16771da177e4SLinus Torvalds break; 16787db48e98SEric Dumazet WRITE_ONCE(tp->copied_seq, seq); 16791da177e4SLinus Torvalds } 16807db48e98SEric Dumazet WRITE_ONCE(tp->copied_seq, seq); 16811da177e4SLinus Torvalds 16821da177e4SLinus Torvalds tcp_rcv_space_adjust(sk); 16831da177e4SLinus Torvalds 16841da177e4SLinus Torvalds /* Clean up data we have read: This will do ACK frames. */ 1685f26845b4SEric Dumazet if (copied > 0) { 1686f26845b4SEric Dumazet tcp_recv_skb(sk, seq, &offset); 16870e4b4992SChris Leech tcp_cleanup_rbuf(sk, copied); 1688f26845b4SEric Dumazet } 16891da177e4SLinus Torvalds return copied; 16901da177e4SLinus Torvalds } 16914bc2f18bSEric Dumazet EXPORT_SYMBOL(tcp_read_sock); 16921da177e4SLinus Torvalds 169332035585STom Herbert int tcp_peek_len(struct socket *sock) 169432035585STom Herbert { 169532035585STom Herbert return tcp_inq(sock->sk); 169632035585STom Herbert } 169732035585STom Herbert EXPORT_SYMBOL(tcp_peek_len); 169832035585STom Herbert 1699d1361840SEric Dumazet /* Make sure sk_rcvbuf is big enough to satisfy SO_RCVLOWAT hint */ 1700d1361840SEric Dumazet int tcp_set_rcvlowat(struct sock *sk, int val) 1701d1361840SEric Dumazet { 1702867f816bSSoheil Hassas Yeganeh int cap; 1703867f816bSSoheil Hassas Yeganeh 1704867f816bSSoheil Hassas Yeganeh if (sk->sk_userlocks & SOCK_RCVBUF_LOCK) 1705867f816bSSoheil Hassas Yeganeh cap = sk->sk_rcvbuf >> 1; 1706867f816bSSoheil Hassas Yeganeh else 1707867f816bSSoheil Hassas Yeganeh cap = sock_net(sk)->ipv4.sysctl_tcp_rmem[2] >> 1; 1708867f816bSSoheil Hassas Yeganeh val = min(val, cap); 1709eac66402SEric Dumazet WRITE_ONCE(sk->sk_rcvlowat, val ? : 1); 171003f45c88SEric Dumazet 171103f45c88SEric Dumazet /* Check if we need to signal EPOLLIN right now */ 171203f45c88SEric Dumazet tcp_data_ready(sk); 171303f45c88SEric Dumazet 1714d1361840SEric Dumazet if (sk->sk_userlocks & SOCK_RCVBUF_LOCK) 1715d1361840SEric Dumazet return 0; 1716d1361840SEric Dumazet 1717d1361840SEric Dumazet val <<= 1; 1718d1361840SEric Dumazet if (val > sk->sk_rcvbuf) { 1719ebb3b78dSEric Dumazet WRITE_ONCE(sk->sk_rcvbuf, val); 1720d1361840SEric Dumazet tcp_sk(sk)->window_clamp = tcp_win_from_space(sk, val); 1721d1361840SEric Dumazet } 1722d1361840SEric Dumazet return 0; 1723d1361840SEric Dumazet } 1724d1361840SEric Dumazet EXPORT_SYMBOL(tcp_set_rcvlowat); 1725d1361840SEric Dumazet 172605255b82SEric Dumazet #ifdef CONFIG_MMU 172705255b82SEric Dumazet static const struct vm_operations_struct tcp_vm_ops = { 172805255b82SEric Dumazet }; 172905255b82SEric Dumazet 173093ab6cc6SEric Dumazet int tcp_mmap(struct file *file, struct socket *sock, 173193ab6cc6SEric Dumazet struct vm_area_struct *vma) 173293ab6cc6SEric Dumazet { 173305255b82SEric Dumazet if (vma->vm_flags & (VM_WRITE | VM_EXEC)) 173405255b82SEric Dumazet return -EPERM; 173505255b82SEric Dumazet vma->vm_flags &= ~(VM_MAYWRITE | VM_MAYEXEC); 173605255b82SEric Dumazet 173705255b82SEric Dumazet /* Instruct vm_insert_page() to not down_read(mmap_sem) */ 173805255b82SEric Dumazet vma->vm_flags |= VM_MIXEDMAP; 173905255b82SEric Dumazet 174005255b82SEric Dumazet vma->vm_ops = &tcp_vm_ops; 174105255b82SEric Dumazet return 0; 174205255b82SEric Dumazet } 174305255b82SEric Dumazet EXPORT_SYMBOL(tcp_mmap); 174405255b82SEric Dumazet 174505255b82SEric Dumazet static int tcp_zerocopy_receive(struct sock *sk, 174605255b82SEric Dumazet struct tcp_zerocopy_receive *zc) 174705255b82SEric Dumazet { 174805255b82SEric Dumazet unsigned long address = (unsigned long)zc->address; 1749c208bdb9SSoheil Hassas Yeganeh u32 length = 0, seq, offset, zap_len; 175005255b82SEric Dumazet const skb_frag_t *frags = NULL; 175105255b82SEric Dumazet struct vm_area_struct *vma; 175205255b82SEric Dumazet struct sk_buff *skb = NULL; 175393ab6cc6SEric Dumazet struct tcp_sock *tp; 17548f2b0293SSoheil Hassas Yeganeh int inq; 175593ab6cc6SEric Dumazet int ret; 175693ab6cc6SEric Dumazet 175705255b82SEric Dumazet if (address & (PAGE_SIZE - 1) || address != zc->address) 175893ab6cc6SEric Dumazet return -EINVAL; 175993ab6cc6SEric Dumazet 176093ab6cc6SEric Dumazet if (sk->sk_state == TCP_LISTEN) 176105255b82SEric Dumazet return -ENOTCONN; 176293ab6cc6SEric Dumazet 176393ab6cc6SEric Dumazet sock_rps_record_flow(sk); 176493ab6cc6SEric Dumazet 176505255b82SEric Dumazet down_read(¤t->mm->mmap_sem); 176693ab6cc6SEric Dumazet 176705255b82SEric Dumazet vma = find_vma(current->mm, address); 1768e776af60SEric Dumazet if (!vma || vma->vm_start > address || vma->vm_ops != &tcp_vm_ops) { 1769e776af60SEric Dumazet up_read(¤t->mm->mmap_sem); 1770e776af60SEric Dumazet return -EINVAL; 1771e776af60SEric Dumazet } 177205255b82SEric Dumazet zc->length = min_t(unsigned long, zc->length, vma->vm_end - address); 177305255b82SEric Dumazet 177405255b82SEric Dumazet tp = tcp_sk(sk); 177505255b82SEric Dumazet seq = tp->copied_seq; 17768f2b0293SSoheil Hassas Yeganeh inq = tcp_inq(sk); 17778f2b0293SSoheil Hassas Yeganeh zc->length = min_t(u32, zc->length, inq); 1778c208bdb9SSoheil Hassas Yeganeh zap_len = zc->length & ~(PAGE_SIZE - 1); 1779c208bdb9SSoheil Hassas Yeganeh if (zap_len) { 1780c208bdb9SSoheil Hassas Yeganeh zap_page_range(vma, address, zap_len); 178105255b82SEric Dumazet zc->recv_skip_hint = 0; 17828f2b0293SSoheil Hassas Yeganeh } else { 1783c208bdb9SSoheil Hassas Yeganeh zc->recv_skip_hint = zc->length; 17848f2b0293SSoheil Hassas Yeganeh } 178505255b82SEric Dumazet ret = 0; 178605255b82SEric Dumazet while (length + PAGE_SIZE <= zc->length) { 178705255b82SEric Dumazet if (zc->recv_skip_hint < PAGE_SIZE) { 178805255b82SEric Dumazet if (skb) { 17890e627190SArjun Roy if (zc->recv_skip_hint > 0) 17900e627190SArjun Roy break; 179105255b82SEric Dumazet skb = skb->next; 179205255b82SEric Dumazet offset = seq - TCP_SKB_CB(skb)->seq; 179305255b82SEric Dumazet } else { 179493ab6cc6SEric Dumazet skb = tcp_recv_skb(sk, seq, &offset); 179505255b82SEric Dumazet } 179605255b82SEric Dumazet 179705255b82SEric Dumazet zc->recv_skip_hint = skb->len - offset; 179893ab6cc6SEric Dumazet offset -= skb_headlen(skb); 179905255b82SEric Dumazet if ((int)offset < 0 || skb_has_frag_list(skb)) 180005255b82SEric Dumazet break; 180193ab6cc6SEric Dumazet frags = skb_shinfo(skb)->frags; 180293ab6cc6SEric Dumazet while (offset) { 1803d8e18a51SMatthew Wilcox (Oracle) if (skb_frag_size(frags) > offset) 180493ab6cc6SEric Dumazet goto out; 1805d8e18a51SMatthew Wilcox (Oracle) offset -= skb_frag_size(frags); 180693ab6cc6SEric Dumazet frags++; 180793ab6cc6SEric Dumazet } 180805255b82SEric Dumazet } 1809b54c9d5bSJonathan Lemon if (skb_frag_size(frags) != PAGE_SIZE || skb_frag_off(frags)) { 1810789762ceSSoheil Hassas Yeganeh int remaining = zc->recv_skip_hint; 1811789762ceSSoheil Hassas Yeganeh 18123afb0961SEric Dumazet while (remaining && (skb_frag_size(frags) != PAGE_SIZE || 1813b54c9d5bSJonathan Lemon skb_frag_off(frags))) { 18143afb0961SEric Dumazet remaining -= skb_frag_size(frags); 1815789762ceSSoheil Hassas Yeganeh frags++; 1816789762ceSSoheil Hassas Yeganeh } 1817789762ceSSoheil Hassas Yeganeh zc->recv_skip_hint -= remaining; 181805255b82SEric Dumazet break; 1819789762ceSSoheil Hassas Yeganeh } 182005255b82SEric Dumazet ret = vm_insert_page(vma, address + length, 182105255b82SEric Dumazet skb_frag_page(frags)); 182293ab6cc6SEric Dumazet if (ret) 182305255b82SEric Dumazet break; 182405255b82SEric Dumazet length += PAGE_SIZE; 182505255b82SEric Dumazet seq += PAGE_SIZE; 182605255b82SEric Dumazet zc->recv_skip_hint -= PAGE_SIZE; 182705255b82SEric Dumazet frags++; 182893ab6cc6SEric Dumazet } 182905255b82SEric Dumazet out: 183005255b82SEric Dumazet up_read(¤t->mm->mmap_sem); 183105255b82SEric Dumazet if (length) { 18327db48e98SEric Dumazet WRITE_ONCE(tp->copied_seq, seq); 183393ab6cc6SEric Dumazet tcp_rcv_space_adjust(sk); 183493ab6cc6SEric Dumazet 183593ab6cc6SEric Dumazet /* Clean up data we have read: This will do ACK frames. */ 183693ab6cc6SEric Dumazet tcp_recv_skb(sk, seq, &offset); 183705255b82SEric Dumazet tcp_cleanup_rbuf(sk, length); 183893ab6cc6SEric Dumazet ret = 0; 183905255b82SEric Dumazet if (length == zc->length) 184005255b82SEric Dumazet zc->recv_skip_hint = 0; 184105255b82SEric Dumazet } else { 184205255b82SEric Dumazet if (!zc->recv_skip_hint && sock_flag(sk, SOCK_DONE)) 184305255b82SEric Dumazet ret = -EIO; 184405255b82SEric Dumazet } 184505255b82SEric Dumazet zc->length = length; 184693ab6cc6SEric Dumazet return ret; 184793ab6cc6SEric Dumazet } 184805255b82SEric Dumazet #endif 184993ab6cc6SEric Dumazet 185098aaa913SMike Maloney static void tcp_update_recv_tstamps(struct sk_buff *skb, 18519718475eSDeepa Dinamani struct scm_timestamping_internal *tss) 185298aaa913SMike Maloney { 185398aaa913SMike Maloney if (skb->tstamp) 18549718475eSDeepa Dinamani tss->ts[0] = ktime_to_timespec64(skb->tstamp); 185598aaa913SMike Maloney else 18569718475eSDeepa Dinamani tss->ts[0] = (struct timespec64) {0}; 185798aaa913SMike Maloney 185898aaa913SMike Maloney if (skb_hwtstamps(skb)->hwtstamp) 18599718475eSDeepa Dinamani tss->ts[2] = ktime_to_timespec64(skb_hwtstamps(skb)->hwtstamp); 186098aaa913SMike Maloney else 18619718475eSDeepa Dinamani tss->ts[2] = (struct timespec64) {0}; 186298aaa913SMike Maloney } 186398aaa913SMike Maloney 186498aaa913SMike Maloney /* Similar to __sock_recv_timestamp, but does not require an skb */ 1865809a79e9SWei Yongjun static void tcp_recv_timestamp(struct msghdr *msg, const struct sock *sk, 18669718475eSDeepa Dinamani struct scm_timestamping_internal *tss) 186798aaa913SMike Maloney { 1868887feae3SDeepa Dinamani int new_tstamp = sock_flag(sk, SOCK_TSTAMP_NEW); 186998aaa913SMike Maloney bool has_timestamping = false; 187098aaa913SMike Maloney 187198aaa913SMike Maloney if (tss->ts[0].tv_sec || tss->ts[0].tv_nsec) { 187298aaa913SMike Maloney if (sock_flag(sk, SOCK_RCVTSTAMP)) { 187398aaa913SMike Maloney if (sock_flag(sk, SOCK_RCVTSTAMPNS)) { 1874887feae3SDeepa Dinamani if (new_tstamp) { 1875df1b4ba9SArnd Bergmann struct __kernel_timespec kts = { 1876df1b4ba9SArnd Bergmann .tv_sec = tss->ts[0].tv_sec, 1877df1b4ba9SArnd Bergmann .tv_nsec = tss->ts[0].tv_nsec, 1878df1b4ba9SArnd Bergmann }; 1879887feae3SDeepa Dinamani put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMPNS_NEW, 1880887feae3SDeepa Dinamani sizeof(kts), &kts); 1881887feae3SDeepa Dinamani } else { 1882df1b4ba9SArnd Bergmann struct __kernel_old_timespec ts_old = { 1883df1b4ba9SArnd Bergmann .tv_sec = tss->ts[0].tv_sec, 1884df1b4ba9SArnd Bergmann .tv_nsec = tss->ts[0].tv_nsec, 1885df1b4ba9SArnd Bergmann }; 18867f1bc6e9SDeepa Dinamani put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMPNS_OLD, 18879718475eSDeepa Dinamani sizeof(ts_old), &ts_old); 1888887feae3SDeepa Dinamani } 188998aaa913SMike Maloney } else { 1890887feae3SDeepa Dinamani if (new_tstamp) { 1891df1b4ba9SArnd Bergmann struct __kernel_sock_timeval stv = { 1892df1b4ba9SArnd Bergmann .tv_sec = tss->ts[0].tv_sec, 1893df1b4ba9SArnd Bergmann .tv_usec = tss->ts[0].tv_nsec / 1000, 1894df1b4ba9SArnd Bergmann }; 1895887feae3SDeepa Dinamani put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMP_NEW, 1896887feae3SDeepa Dinamani sizeof(stv), &stv); 1897887feae3SDeepa Dinamani } else { 1898df1b4ba9SArnd Bergmann struct __kernel_old_timeval tv = { 1899df1b4ba9SArnd Bergmann .tv_sec = tss->ts[0].tv_sec, 1900df1b4ba9SArnd Bergmann .tv_usec = tss->ts[0].tv_nsec / 1000, 1901df1b4ba9SArnd Bergmann }; 19027f1bc6e9SDeepa Dinamani put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMP_OLD, 190398aaa913SMike Maloney sizeof(tv), &tv); 190498aaa913SMike Maloney } 190598aaa913SMike Maloney } 1906887feae3SDeepa Dinamani } 190798aaa913SMike Maloney 190898aaa913SMike Maloney if (sk->sk_tsflags & SOF_TIMESTAMPING_SOFTWARE) 190998aaa913SMike Maloney has_timestamping = true; 191098aaa913SMike Maloney else 19119718475eSDeepa Dinamani tss->ts[0] = (struct timespec64) {0}; 191298aaa913SMike Maloney } 191398aaa913SMike Maloney 191498aaa913SMike Maloney if (tss->ts[2].tv_sec || tss->ts[2].tv_nsec) { 191598aaa913SMike Maloney if (sk->sk_tsflags & SOF_TIMESTAMPING_RAW_HARDWARE) 191698aaa913SMike Maloney has_timestamping = true; 191798aaa913SMike Maloney else 19189718475eSDeepa Dinamani tss->ts[2] = (struct timespec64) {0}; 191998aaa913SMike Maloney } 192098aaa913SMike Maloney 192198aaa913SMike Maloney if (has_timestamping) { 19229718475eSDeepa Dinamani tss->ts[1] = (struct timespec64) {0}; 19239718475eSDeepa Dinamani if (sock_flag(sk, SOCK_TSTAMP_NEW)) 19249718475eSDeepa Dinamani put_cmsg_scm_timestamping64(msg, tss); 19259718475eSDeepa Dinamani else 19269718475eSDeepa Dinamani put_cmsg_scm_timestamping(msg, tss); 192798aaa913SMike Maloney } 192898aaa913SMike Maloney } 192998aaa913SMike Maloney 1930b75eba76SSoheil Hassas Yeganeh static int tcp_inq_hint(struct sock *sk) 1931b75eba76SSoheil Hassas Yeganeh { 1932b75eba76SSoheil Hassas Yeganeh const struct tcp_sock *tp = tcp_sk(sk); 1933b75eba76SSoheil Hassas Yeganeh u32 copied_seq = READ_ONCE(tp->copied_seq); 1934b75eba76SSoheil Hassas Yeganeh u32 rcv_nxt = READ_ONCE(tp->rcv_nxt); 1935b75eba76SSoheil Hassas Yeganeh int inq; 1936b75eba76SSoheil Hassas Yeganeh 1937b75eba76SSoheil Hassas Yeganeh inq = rcv_nxt - copied_seq; 1938b75eba76SSoheil Hassas Yeganeh if (unlikely(inq < 0 || copied_seq != READ_ONCE(tp->copied_seq))) { 1939b75eba76SSoheil Hassas Yeganeh lock_sock(sk); 1940b75eba76SSoheil Hassas Yeganeh inq = tp->rcv_nxt - tp->copied_seq; 1941b75eba76SSoheil Hassas Yeganeh release_sock(sk); 1942b75eba76SSoheil Hassas Yeganeh } 19436466e715SSoheil Hassas Yeganeh /* After receiving a FIN, tell the user-space to continue reading 19446466e715SSoheil Hassas Yeganeh * by returning a non-zero inq. 19456466e715SSoheil Hassas Yeganeh */ 19466466e715SSoheil Hassas Yeganeh if (inq == 0 && sock_flag(sk, SOCK_DONE)) 19476466e715SSoheil Hassas Yeganeh inq = 1; 1948b75eba76SSoheil Hassas Yeganeh return inq; 1949b75eba76SSoheil Hassas Yeganeh } 1950b75eba76SSoheil Hassas Yeganeh 19511da177e4SLinus Torvalds /* 19521da177e4SLinus Torvalds * This routine copies from a sock struct into the user buffer. 19531da177e4SLinus Torvalds * 19541da177e4SLinus Torvalds * Technical note: in 2.3 we work on _locked_ socket, so that 19551da177e4SLinus Torvalds * tricks with *seq access order and skb->users are not required. 19561da177e4SLinus Torvalds * Probably, code can be easily improved even more. 19571da177e4SLinus Torvalds */ 19581da177e4SLinus Torvalds 19591b784140SYing Xue int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int nonblock, 19601b784140SYing Xue int flags, int *addr_len) 19611da177e4SLinus Torvalds { 19621da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 19631da177e4SLinus Torvalds int copied = 0; 19641da177e4SLinus Torvalds u32 peek_seq; 19651da177e4SLinus Torvalds u32 *seq; 19661da177e4SLinus Torvalds unsigned long used; 1967b75eba76SSoheil Hassas Yeganeh int err, inq; 19681da177e4SLinus Torvalds int target; /* Read at least this many bytes */ 19691da177e4SLinus Torvalds long timeo; 1970dfbafc99SSabrina Dubroca struct sk_buff *skb, *last; 197177527313SIlpo Järvinen u32 urg_hole = 0; 19729718475eSDeepa Dinamani struct scm_timestamping_internal tss; 1973a5a7daa5SEric Dumazet int cmsg_flags; 19741da177e4SLinus Torvalds 19754ed2d765SWillem de Bruijn if (unlikely(flags & MSG_ERRQUEUE)) 1976f4713a3dSWillem de Bruijn return inet_recv_error(sk, msg, len, addr_len); 19774ed2d765SWillem de Bruijn 19783f926af3SEric Dumazet if (sk_can_busy_loop(sk) && skb_queue_empty_lockless(&sk->sk_receive_queue) && 1979cbf55001SEliezer Tamir (sk->sk_state == TCP_ESTABLISHED)) 1980cbf55001SEliezer Tamir sk_busy_loop(sk, nonblock); 1981d30e383bSEliezer Tamir 19821da177e4SLinus Torvalds lock_sock(sk); 19831da177e4SLinus Torvalds 19841da177e4SLinus Torvalds err = -ENOTCONN; 19851da177e4SLinus Torvalds if (sk->sk_state == TCP_LISTEN) 19861da177e4SLinus Torvalds goto out; 19871da177e4SLinus Torvalds 1988a5a7daa5SEric Dumazet cmsg_flags = tp->recvmsg_inq ? 1 : 0; 19891da177e4SLinus Torvalds timeo = sock_rcvtimeo(sk, nonblock); 19901da177e4SLinus Torvalds 19911da177e4SLinus Torvalds /* Urgent data needs to be handled specially. */ 19921da177e4SLinus Torvalds if (flags & MSG_OOB) 19931da177e4SLinus Torvalds goto recv_urg; 19941da177e4SLinus Torvalds 1995c0e88ff0SPavel Emelyanov if (unlikely(tp->repair)) { 1996c0e88ff0SPavel Emelyanov err = -EPERM; 1997c0e88ff0SPavel Emelyanov if (!(flags & MSG_PEEK)) 1998c0e88ff0SPavel Emelyanov goto out; 1999c0e88ff0SPavel Emelyanov 2000c0e88ff0SPavel Emelyanov if (tp->repair_queue == TCP_SEND_QUEUE) 2001c0e88ff0SPavel Emelyanov goto recv_sndq; 2002c0e88ff0SPavel Emelyanov 2003c0e88ff0SPavel Emelyanov err = -EINVAL; 2004c0e88ff0SPavel Emelyanov if (tp->repair_queue == TCP_NO_QUEUE) 2005c0e88ff0SPavel Emelyanov goto out; 2006c0e88ff0SPavel Emelyanov 2007c0e88ff0SPavel Emelyanov /* 'common' recv queue MSG_PEEK-ing */ 2008c0e88ff0SPavel Emelyanov } 2009c0e88ff0SPavel Emelyanov 20101da177e4SLinus Torvalds seq = &tp->copied_seq; 20111da177e4SLinus Torvalds if (flags & MSG_PEEK) { 20121da177e4SLinus Torvalds peek_seq = tp->copied_seq; 20131da177e4SLinus Torvalds seq = &peek_seq; 20141da177e4SLinus Torvalds } 20151da177e4SLinus Torvalds 20161da177e4SLinus Torvalds target = sock_rcvlowat(sk, flags & MSG_WAITALL, len); 20171da177e4SLinus Torvalds 20181da177e4SLinus Torvalds do { 20191da177e4SLinus Torvalds u32 offset; 20201da177e4SLinus Torvalds 20211da177e4SLinus Torvalds /* Are we at urgent data? Stop if we have read anything or have SIGURG pending. */ 20221da177e4SLinus Torvalds if (tp->urg_data && tp->urg_seq == *seq) { 20231da177e4SLinus Torvalds if (copied) 20241da177e4SLinus Torvalds break; 20251da177e4SLinus Torvalds if (signal_pending(current)) { 20261da177e4SLinus Torvalds copied = timeo ? sock_intr_errno(timeo) : -EAGAIN; 20271da177e4SLinus Torvalds break; 20281da177e4SLinus Torvalds } 20291da177e4SLinus Torvalds } 20301da177e4SLinus Torvalds 20311da177e4SLinus Torvalds /* Next get a buffer. */ 20321da177e4SLinus Torvalds 2033dfbafc99SSabrina Dubroca last = skb_peek_tail(&sk->sk_receive_queue); 203491521944SDavid S. Miller skb_queue_walk(&sk->sk_receive_queue, skb) { 2035dfbafc99SSabrina Dubroca last = skb; 20361da177e4SLinus Torvalds /* Now that we have two receive queues this 20371da177e4SLinus Torvalds * shouldn't happen. 20381da177e4SLinus Torvalds */ 2039d792c100SIlpo Järvinen if (WARN(before(*seq, TCP_SKB_CB(skb)->seq), 2040e56b8ce3SRandy Dunlap "TCP recvmsg seq # bug: copied %X, seq %X, rcvnxt %X, fl %X\n", 20412af6fd8bSJoe Perches *seq, TCP_SKB_CB(skb)->seq, tp->rcv_nxt, 2042d792c100SIlpo Järvinen flags)) 20431da177e4SLinus Torvalds break; 2044d792c100SIlpo Järvinen 20451da177e4SLinus Torvalds offset = *seq - TCP_SKB_CB(skb)->seq; 20469d691539SEric Dumazet if (unlikely(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_SYN)) { 20479d691539SEric Dumazet pr_err_once("%s: found a SYN, please report !\n", __func__); 20481da177e4SLinus Torvalds offset--; 20499d691539SEric Dumazet } 20501da177e4SLinus Torvalds if (offset < skb->len) 20511da177e4SLinus Torvalds goto found_ok_skb; 2052e11ecddfSEric Dumazet if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN) 20531da177e4SLinus Torvalds goto found_fin_ok; 20542af6fd8bSJoe Perches WARN(!(flags & MSG_PEEK), 2055e56b8ce3SRandy Dunlap "TCP recvmsg seq # bug 2: copied %X, seq %X, rcvnxt %X, fl %X\n", 20562af6fd8bSJoe Perches *seq, TCP_SKB_CB(skb)->seq, tp->rcv_nxt, flags); 205791521944SDavid S. Miller } 20581da177e4SLinus Torvalds 20591da177e4SLinus Torvalds /* Well, if we have backlog, try to process it now yet. */ 20601da177e4SLinus Torvalds 20619ed498c6SEric Dumazet if (copied >= target && !READ_ONCE(sk->sk_backlog.tail)) 20621da177e4SLinus Torvalds break; 20631da177e4SLinus Torvalds 20641da177e4SLinus Torvalds if (copied) { 20651da177e4SLinus Torvalds if (sk->sk_err || 20661da177e4SLinus Torvalds sk->sk_state == TCP_CLOSE || 20671da177e4SLinus Torvalds (sk->sk_shutdown & RCV_SHUTDOWN) || 20681da177e4SLinus Torvalds !timeo || 2069518a09efSDavid S. Miller signal_pending(current)) 20701da177e4SLinus Torvalds break; 20711da177e4SLinus Torvalds } else { 20721da177e4SLinus Torvalds if (sock_flag(sk, SOCK_DONE)) 20731da177e4SLinus Torvalds break; 20741da177e4SLinus Torvalds 20751da177e4SLinus Torvalds if (sk->sk_err) { 20761da177e4SLinus Torvalds copied = sock_error(sk); 20771da177e4SLinus Torvalds break; 20781da177e4SLinus Torvalds } 20791da177e4SLinus Torvalds 20801da177e4SLinus Torvalds if (sk->sk_shutdown & RCV_SHUTDOWN) 20811da177e4SLinus Torvalds break; 20821da177e4SLinus Torvalds 20831da177e4SLinus Torvalds if (sk->sk_state == TCP_CLOSE) { 20841da177e4SLinus Torvalds /* This occurs when user tries to read 20851da177e4SLinus Torvalds * from never connected socket. 20861da177e4SLinus Torvalds */ 20871da177e4SLinus Torvalds copied = -ENOTCONN; 20881da177e4SLinus Torvalds break; 20891da177e4SLinus Torvalds } 20901da177e4SLinus Torvalds 20911da177e4SLinus Torvalds if (!timeo) { 20921da177e4SLinus Torvalds copied = -EAGAIN; 20931da177e4SLinus Torvalds break; 20941da177e4SLinus Torvalds } 20951da177e4SLinus Torvalds 20961da177e4SLinus Torvalds if (signal_pending(current)) { 20971da177e4SLinus Torvalds copied = sock_intr_errno(timeo); 20981da177e4SLinus Torvalds break; 20991da177e4SLinus Torvalds } 21001da177e4SLinus Torvalds } 21011da177e4SLinus Torvalds 21020e4b4992SChris Leech tcp_cleanup_rbuf(sk, copied); 21031da177e4SLinus Torvalds 21041da177e4SLinus Torvalds if (copied >= target) { 21051da177e4SLinus Torvalds /* Do not sleep, just process backlog. */ 21061da177e4SLinus Torvalds release_sock(sk); 21071da177e4SLinus Torvalds lock_sock(sk); 2108dfbafc99SSabrina Dubroca } else { 2109dfbafc99SSabrina Dubroca sk_wait_data(sk, &timeo, last); 2110dfbafc99SSabrina Dubroca } 21111da177e4SLinus Torvalds 211277527313SIlpo Järvinen if ((flags & MSG_PEEK) && 211377527313SIlpo Järvinen (peek_seq - copied - urg_hole != tp->copied_seq)) { 2114e87cc472SJoe Perches net_dbg_ratelimited("TCP(%s:%d): Application bug, race in MSG_PEEK\n", 2115e87cc472SJoe Perches current->comm, 2116e87cc472SJoe Perches task_pid_nr(current)); 21171da177e4SLinus Torvalds peek_seq = tp->copied_seq; 21181da177e4SLinus Torvalds } 21191da177e4SLinus Torvalds continue; 21201da177e4SLinus Torvalds 21211da177e4SLinus Torvalds found_ok_skb: 21221da177e4SLinus Torvalds /* Ok so how much can we use? */ 21231da177e4SLinus Torvalds used = skb->len - offset; 21241da177e4SLinus Torvalds if (len < used) 21251da177e4SLinus Torvalds used = len; 21261da177e4SLinus Torvalds 21271da177e4SLinus Torvalds /* Do we have urgent data here? */ 21281da177e4SLinus Torvalds if (tp->urg_data) { 21291da177e4SLinus Torvalds u32 urg_offset = tp->urg_seq - *seq; 21301da177e4SLinus Torvalds if (urg_offset < used) { 21311da177e4SLinus Torvalds if (!urg_offset) { 21321da177e4SLinus Torvalds if (!sock_flag(sk, SOCK_URGINLINE)) { 21337db48e98SEric Dumazet WRITE_ONCE(*seq, *seq + 1); 213477527313SIlpo Järvinen urg_hole++; 21351da177e4SLinus Torvalds offset++; 21361da177e4SLinus Torvalds used--; 21371da177e4SLinus Torvalds if (!used) 21381da177e4SLinus Torvalds goto skip_copy; 21391da177e4SLinus Torvalds } 21401da177e4SLinus Torvalds } else 21411da177e4SLinus Torvalds used = urg_offset; 21421da177e4SLinus Torvalds } 21431da177e4SLinus Torvalds } 21441da177e4SLinus Torvalds 21451da177e4SLinus Torvalds if (!(flags & MSG_TRUNC)) { 214651f3d02bSDavid S. Miller err = skb_copy_datagram_msg(skb, offset, msg, used); 21471da177e4SLinus Torvalds if (err) { 21481da177e4SLinus Torvalds /* Exception. Bailout! */ 21491da177e4SLinus Torvalds if (!copied) 21501da177e4SLinus Torvalds copied = -EFAULT; 21511da177e4SLinus Torvalds break; 21521da177e4SLinus Torvalds } 21531da177e4SLinus Torvalds } 21541da177e4SLinus Torvalds 21557db48e98SEric Dumazet WRITE_ONCE(*seq, *seq + used); 21561da177e4SLinus Torvalds copied += used; 21571da177e4SLinus Torvalds len -= used; 21581da177e4SLinus Torvalds 21591da177e4SLinus Torvalds tcp_rcv_space_adjust(sk); 21601da177e4SLinus Torvalds 21611da177e4SLinus Torvalds skip_copy: 216231770e34SFlorian Westphal if (tp->urg_data && after(tp->copied_seq, tp->urg_seq)) { 21631da177e4SLinus Torvalds tp->urg_data = 0; 216431770e34SFlorian Westphal tcp_fast_path_check(sk); 216531770e34SFlorian Westphal } 21661da177e4SLinus Torvalds 216798aaa913SMike Maloney if (TCP_SKB_CB(skb)->has_rxtstamp) { 216898aaa913SMike Maloney tcp_update_recv_tstamps(skb, &tss); 2169a5a7daa5SEric Dumazet cmsg_flags |= 2; 217098aaa913SMike Maloney } 2171cc4de047SKelly Littlepage 2172cc4de047SKelly Littlepage if (used + offset < skb->len) 2173cc4de047SKelly Littlepage continue; 2174cc4de047SKelly Littlepage 2175e11ecddfSEric Dumazet if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN) 21761da177e4SLinus Torvalds goto found_fin_ok; 21777bced397SDan Williams if (!(flags & MSG_PEEK)) 21787bced397SDan Williams sk_eat_skb(sk, skb); 21791da177e4SLinus Torvalds continue; 21801da177e4SLinus Torvalds 21811da177e4SLinus Torvalds found_fin_ok: 21821da177e4SLinus Torvalds /* Process the FIN. */ 21837db48e98SEric Dumazet WRITE_ONCE(*seq, *seq + 1); 21847bced397SDan Williams if (!(flags & MSG_PEEK)) 21857bced397SDan Williams sk_eat_skb(sk, skb); 21861da177e4SLinus Torvalds break; 21871da177e4SLinus Torvalds } while (len > 0); 21881da177e4SLinus Torvalds 21891da177e4SLinus Torvalds /* According to UNIX98, msg_name/msg_namelen are ignored 21901da177e4SLinus Torvalds * on connected socket. I was just happy when found this 8) --ANK 21911da177e4SLinus Torvalds */ 21921da177e4SLinus Torvalds 21931da177e4SLinus Torvalds /* Clean up data we have read: This will do ACK frames. */ 21940e4b4992SChris Leech tcp_cleanup_rbuf(sk, copied); 21951da177e4SLinus Torvalds 21961da177e4SLinus Torvalds release_sock(sk); 2197b75eba76SSoheil Hassas Yeganeh 2198a5a7daa5SEric Dumazet if (cmsg_flags) { 2199a5a7daa5SEric Dumazet if (cmsg_flags & 2) 2200b75eba76SSoheil Hassas Yeganeh tcp_recv_timestamp(msg, sk, &tss); 2201a5a7daa5SEric Dumazet if (cmsg_flags & 1) { 2202b75eba76SSoheil Hassas Yeganeh inq = tcp_inq_hint(sk); 2203b75eba76SSoheil Hassas Yeganeh put_cmsg(msg, SOL_TCP, TCP_CM_INQ, sizeof(inq), &inq); 2204b75eba76SSoheil Hassas Yeganeh } 2205b75eba76SSoheil Hassas Yeganeh } 2206b75eba76SSoheil Hassas Yeganeh 22071da177e4SLinus Torvalds return copied; 22081da177e4SLinus Torvalds 22091da177e4SLinus Torvalds out: 22101da177e4SLinus Torvalds release_sock(sk); 22111da177e4SLinus Torvalds return err; 22121da177e4SLinus Torvalds 22131da177e4SLinus Torvalds recv_urg: 2214377f0a08SRami Rosen err = tcp_recv_urg(sk, msg, len, flags); 22151da177e4SLinus Torvalds goto out; 2216c0e88ff0SPavel Emelyanov 2217c0e88ff0SPavel Emelyanov recv_sndq: 2218c0e88ff0SPavel Emelyanov err = tcp_peek_sndq(sk, msg, len); 2219c0e88ff0SPavel Emelyanov goto out; 22201da177e4SLinus Torvalds } 22214bc2f18bSEric Dumazet EXPORT_SYMBOL(tcp_recvmsg); 22221da177e4SLinus Torvalds 2223490d5046SIlpo Järvinen void tcp_set_state(struct sock *sk, int state) 2224490d5046SIlpo Järvinen { 2225490d5046SIlpo Järvinen int oldstate = sk->sk_state; 2226490d5046SIlpo Järvinen 2227d4487491SLawrence Brakmo /* We defined a new enum for TCP states that are exported in BPF 2228d4487491SLawrence Brakmo * so as not force the internal TCP states to be frozen. The 2229d4487491SLawrence Brakmo * following checks will detect if an internal state value ever 2230d4487491SLawrence Brakmo * differs from the BPF value. If this ever happens, then we will 2231d4487491SLawrence Brakmo * need to remap the internal value to the BPF value before calling 2232d4487491SLawrence Brakmo * tcp_call_bpf_2arg. 2233d4487491SLawrence Brakmo */ 2234d4487491SLawrence Brakmo BUILD_BUG_ON((int)BPF_TCP_ESTABLISHED != (int)TCP_ESTABLISHED); 2235d4487491SLawrence Brakmo BUILD_BUG_ON((int)BPF_TCP_SYN_SENT != (int)TCP_SYN_SENT); 2236d4487491SLawrence Brakmo BUILD_BUG_ON((int)BPF_TCP_SYN_RECV != (int)TCP_SYN_RECV); 2237d4487491SLawrence Brakmo BUILD_BUG_ON((int)BPF_TCP_FIN_WAIT1 != (int)TCP_FIN_WAIT1); 2238d4487491SLawrence Brakmo BUILD_BUG_ON((int)BPF_TCP_FIN_WAIT2 != (int)TCP_FIN_WAIT2); 2239d4487491SLawrence Brakmo BUILD_BUG_ON((int)BPF_TCP_TIME_WAIT != (int)TCP_TIME_WAIT); 2240d4487491SLawrence Brakmo BUILD_BUG_ON((int)BPF_TCP_CLOSE != (int)TCP_CLOSE); 2241d4487491SLawrence Brakmo BUILD_BUG_ON((int)BPF_TCP_CLOSE_WAIT != (int)TCP_CLOSE_WAIT); 2242d4487491SLawrence Brakmo BUILD_BUG_ON((int)BPF_TCP_LAST_ACK != (int)TCP_LAST_ACK); 2243d4487491SLawrence Brakmo BUILD_BUG_ON((int)BPF_TCP_LISTEN != (int)TCP_LISTEN); 2244d4487491SLawrence Brakmo BUILD_BUG_ON((int)BPF_TCP_CLOSING != (int)TCP_CLOSING); 2245d4487491SLawrence Brakmo BUILD_BUG_ON((int)BPF_TCP_NEW_SYN_RECV != (int)TCP_NEW_SYN_RECV); 2246d4487491SLawrence Brakmo BUILD_BUG_ON((int)BPF_TCP_MAX_STATES != (int)TCP_MAX_STATES); 2247d4487491SLawrence Brakmo 2248d4487491SLawrence Brakmo if (BPF_SOCK_OPS_TEST_FLAG(tcp_sk(sk), BPF_SOCK_OPS_STATE_CB_FLAG)) 2249d4487491SLawrence Brakmo tcp_call_bpf_2arg(sk, BPF_SOCK_OPS_STATE_CB, oldstate, state); 2250e8fce239SSong Liu 2251490d5046SIlpo Järvinen switch (state) { 2252490d5046SIlpo Järvinen case TCP_ESTABLISHED: 2253490d5046SIlpo Järvinen if (oldstate != TCP_ESTABLISHED) 225481cc8a75SPavel Emelyanov TCP_INC_STATS(sock_net(sk), TCP_MIB_CURRESTAB); 2255490d5046SIlpo Järvinen break; 2256490d5046SIlpo Järvinen 2257490d5046SIlpo Järvinen case TCP_CLOSE: 2258490d5046SIlpo Järvinen if (oldstate == TCP_CLOSE_WAIT || oldstate == TCP_ESTABLISHED) 225981cc8a75SPavel Emelyanov TCP_INC_STATS(sock_net(sk), TCP_MIB_ESTABRESETS); 2260490d5046SIlpo Järvinen 2261490d5046SIlpo Järvinen sk->sk_prot->unhash(sk); 2262490d5046SIlpo Järvinen if (inet_csk(sk)->icsk_bind_hash && 2263490d5046SIlpo Järvinen !(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) 2264ab1e0a13SArnaldo Carvalho de Melo inet_put_port(sk); 2265a8eceea8SJoe Perches fallthrough; 2266490d5046SIlpo Järvinen default: 2267490d5046SIlpo Järvinen if (oldstate == TCP_ESTABLISHED) 226874688e48SPavel Emelyanov TCP_DEC_STATS(sock_net(sk), TCP_MIB_CURRESTAB); 2269490d5046SIlpo Järvinen } 2270490d5046SIlpo Järvinen 2271490d5046SIlpo Järvinen /* Change state AFTER socket is unhashed to avoid closed 2272490d5046SIlpo Järvinen * socket sitting in hash tables. 2273490d5046SIlpo Järvinen */ 2274563e0bb0SYafang Shao inet_sk_state_store(sk, state); 2275490d5046SIlpo Järvinen } 2276490d5046SIlpo Järvinen EXPORT_SYMBOL_GPL(tcp_set_state); 2277490d5046SIlpo Järvinen 22781da177e4SLinus Torvalds /* 22791da177e4SLinus Torvalds * State processing on a close. This implements the state shift for 22801da177e4SLinus Torvalds * sending our FIN frame. Note that we only send a FIN for some 22811da177e4SLinus Torvalds * states. A shutdown() may have already sent the FIN, or we may be 22821da177e4SLinus Torvalds * closed. 22831da177e4SLinus Torvalds */ 22841da177e4SLinus Torvalds 22859b5b5cffSArjan van de Ven static const unsigned char new_state[16] = { 22861da177e4SLinus Torvalds /* current state: new state: action: */ 22870980c1e3SEric Dumazet [0 /* (Invalid) */] = TCP_CLOSE, 22880980c1e3SEric Dumazet [TCP_ESTABLISHED] = TCP_FIN_WAIT1 | TCP_ACTION_FIN, 22890980c1e3SEric Dumazet [TCP_SYN_SENT] = TCP_CLOSE, 22900980c1e3SEric Dumazet [TCP_SYN_RECV] = TCP_FIN_WAIT1 | TCP_ACTION_FIN, 22910980c1e3SEric Dumazet [TCP_FIN_WAIT1] = TCP_FIN_WAIT1, 22920980c1e3SEric Dumazet [TCP_FIN_WAIT2] = TCP_FIN_WAIT2, 22930980c1e3SEric Dumazet [TCP_TIME_WAIT] = TCP_CLOSE, 22940980c1e3SEric Dumazet [TCP_CLOSE] = TCP_CLOSE, 22950980c1e3SEric Dumazet [TCP_CLOSE_WAIT] = TCP_LAST_ACK | TCP_ACTION_FIN, 22960980c1e3SEric Dumazet [TCP_LAST_ACK] = TCP_LAST_ACK, 22970980c1e3SEric Dumazet [TCP_LISTEN] = TCP_CLOSE, 22980980c1e3SEric Dumazet [TCP_CLOSING] = TCP_CLOSING, 22990980c1e3SEric Dumazet [TCP_NEW_SYN_RECV] = TCP_CLOSE, /* should not happen ! */ 23001da177e4SLinus Torvalds }; 23011da177e4SLinus Torvalds 23021da177e4SLinus Torvalds static int tcp_close_state(struct sock *sk) 23031da177e4SLinus Torvalds { 23041da177e4SLinus Torvalds int next = (int)new_state[sk->sk_state]; 23051da177e4SLinus Torvalds int ns = next & TCP_STATE_MASK; 23061da177e4SLinus Torvalds 23071da177e4SLinus Torvalds tcp_set_state(sk, ns); 23081da177e4SLinus Torvalds 23091da177e4SLinus Torvalds return next & TCP_ACTION_FIN; 23101da177e4SLinus Torvalds } 23111da177e4SLinus Torvalds 23121da177e4SLinus Torvalds /* 23131da177e4SLinus Torvalds * Shutdown the sending side of a connection. Much like close except 23141f29b058SSatoru SATOH * that we don't receive shut down or sock_set_flag(sk, SOCK_DEAD). 23151da177e4SLinus Torvalds */ 23161da177e4SLinus Torvalds 23171da177e4SLinus Torvalds void tcp_shutdown(struct sock *sk, int how) 23181da177e4SLinus Torvalds { 23191da177e4SLinus Torvalds /* We need to grab some memory, and put together a FIN, 23201da177e4SLinus Torvalds * and then put it into the queue to be sent. 23211da177e4SLinus Torvalds * Tim MacKenzie(tym@dibbler.cs.monash.edu.au) 4 Dec '92. 23221da177e4SLinus Torvalds */ 23231da177e4SLinus Torvalds if (!(how & SEND_SHUTDOWN)) 23241da177e4SLinus Torvalds return; 23251da177e4SLinus Torvalds 23261da177e4SLinus Torvalds /* If we've already sent a FIN, or it's a closed state, skip this. */ 23271da177e4SLinus Torvalds if ((1 << sk->sk_state) & 23281da177e4SLinus Torvalds (TCPF_ESTABLISHED | TCPF_SYN_SENT | 23291da177e4SLinus Torvalds TCPF_SYN_RECV | TCPF_CLOSE_WAIT)) { 23301da177e4SLinus Torvalds /* Clear out any half completed packets. FIN if needed. */ 23311da177e4SLinus Torvalds if (tcp_close_state(sk)) 23321da177e4SLinus Torvalds tcp_send_fin(sk); 23331da177e4SLinus Torvalds } 23341da177e4SLinus Torvalds } 23354bc2f18bSEric Dumazet EXPORT_SYMBOL(tcp_shutdown); 23361da177e4SLinus Torvalds 2337efcdbf24SArun Sharma bool tcp_check_oom(struct sock *sk, int shift) 2338efcdbf24SArun Sharma { 2339efcdbf24SArun Sharma bool too_many_orphans, out_of_socket_memory; 2340efcdbf24SArun Sharma 2341efcdbf24SArun Sharma too_many_orphans = tcp_too_many_orphans(sk, shift); 2342efcdbf24SArun Sharma out_of_socket_memory = tcp_out_of_memory(sk); 2343efcdbf24SArun Sharma 2344e87cc472SJoe Perches if (too_many_orphans) 2345e87cc472SJoe Perches net_info_ratelimited("too many orphaned sockets\n"); 2346e87cc472SJoe Perches if (out_of_socket_memory) 2347e87cc472SJoe Perches net_info_ratelimited("out of memory -- consider tuning tcp_mem\n"); 2348efcdbf24SArun Sharma return too_many_orphans || out_of_socket_memory; 2349efcdbf24SArun Sharma } 2350efcdbf24SArun Sharma 23511da177e4SLinus Torvalds void tcp_close(struct sock *sk, long timeout) 23521da177e4SLinus Torvalds { 23531da177e4SLinus Torvalds struct sk_buff *skb; 23541da177e4SLinus Torvalds int data_was_unread = 0; 235575c2d907SHerbert Xu int state; 23561da177e4SLinus Torvalds 23571da177e4SLinus Torvalds lock_sock(sk); 23581da177e4SLinus Torvalds sk->sk_shutdown = SHUTDOWN_MASK; 23591da177e4SLinus Torvalds 23601da177e4SLinus Torvalds if (sk->sk_state == TCP_LISTEN) { 23611da177e4SLinus Torvalds tcp_set_state(sk, TCP_CLOSE); 23621da177e4SLinus Torvalds 23631da177e4SLinus Torvalds /* Special case. */ 23640a5578cfSArnaldo Carvalho de Melo inet_csk_listen_stop(sk); 23651da177e4SLinus Torvalds 23661da177e4SLinus Torvalds goto adjudge_to_death; 23671da177e4SLinus Torvalds } 23681da177e4SLinus Torvalds 23691da177e4SLinus Torvalds /* We need to flush the recv. buffs. We do this only on the 23701da177e4SLinus Torvalds * descriptor close, not protocol-sourced closes, because the 23711da177e4SLinus Torvalds * reader process may not have drained the data yet! 23721da177e4SLinus Torvalds */ 23731da177e4SLinus Torvalds while ((skb = __skb_dequeue(&sk->sk_receive_queue)) != NULL) { 2374e11ecddfSEric Dumazet u32 len = TCP_SKB_CB(skb)->end_seq - TCP_SKB_CB(skb)->seq; 2375e11ecddfSEric Dumazet 2376e11ecddfSEric Dumazet if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN) 2377e11ecddfSEric Dumazet len--; 23781da177e4SLinus Torvalds data_was_unread += len; 23791da177e4SLinus Torvalds __kfree_skb(skb); 23801da177e4SLinus Torvalds } 23811da177e4SLinus Torvalds 23823ab224beSHideo Aoki sk_mem_reclaim(sk); 23831da177e4SLinus Torvalds 2384565b7b2dSKonstantin Khorenko /* If socket has been already reset (e.g. in tcp_reset()) - kill it. */ 2385565b7b2dSKonstantin Khorenko if (sk->sk_state == TCP_CLOSE) 2386565b7b2dSKonstantin Khorenko goto adjudge_to_death; 2387565b7b2dSKonstantin Khorenko 238865bb723cSGerrit Renker /* As outlined in RFC 2525, section 2.17, we send a RST here because 238965bb723cSGerrit Renker * data was lost. To witness the awful effects of the old behavior of 239065bb723cSGerrit Renker * always doing a FIN, run an older 2.1.x kernel or 2.0.x, start a bulk 239165bb723cSGerrit Renker * GET in an FTP client, suspend the process, wait for the client to 239265bb723cSGerrit Renker * advertise a zero window, then kill -9 the FTP client, wheee... 239365bb723cSGerrit Renker * Note: timeout is always zero in such a case. 23941da177e4SLinus Torvalds */ 2395ee995283SPavel Emelyanov if (unlikely(tcp_sk(sk)->repair)) { 2396ee995283SPavel Emelyanov sk->sk_prot->disconnect(sk, 0); 2397ee995283SPavel Emelyanov } else if (data_was_unread) { 23981da177e4SLinus Torvalds /* Unread data was tossed, zap the connection. */ 23996aef70a8SEric Dumazet NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPABORTONCLOSE); 24001da177e4SLinus Torvalds tcp_set_state(sk, TCP_CLOSE); 2401aa133076SWu Fengguang tcp_send_active_reset(sk, sk->sk_allocation); 24021da177e4SLinus Torvalds } else if (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime) { 24031da177e4SLinus Torvalds /* Check zero linger _after_ checking for unread data. */ 24041da177e4SLinus Torvalds sk->sk_prot->disconnect(sk, 0); 24056aef70a8SEric Dumazet NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPABORTONDATA); 24061da177e4SLinus Torvalds } else if (tcp_close_state(sk)) { 24071da177e4SLinus Torvalds /* We FIN if the application ate all the data before 24081da177e4SLinus Torvalds * zapping the connection. 24091da177e4SLinus Torvalds */ 24101da177e4SLinus Torvalds 24111da177e4SLinus Torvalds /* RED-PEN. Formally speaking, we have broken TCP state 24121da177e4SLinus Torvalds * machine. State transitions: 24131da177e4SLinus Torvalds * 24141da177e4SLinus Torvalds * TCP_ESTABLISHED -> TCP_FIN_WAIT1 24151da177e4SLinus Torvalds * TCP_SYN_RECV -> TCP_FIN_WAIT1 (forget it, it's impossible) 24161da177e4SLinus Torvalds * TCP_CLOSE_WAIT -> TCP_LAST_ACK 24171da177e4SLinus Torvalds * 24181da177e4SLinus Torvalds * are legal only when FIN has been sent (i.e. in window), 24191da177e4SLinus Torvalds * rather than queued out of window. Purists blame. 24201da177e4SLinus Torvalds * 24211da177e4SLinus Torvalds * F.e. "RFC state" is ESTABLISHED, 24221da177e4SLinus Torvalds * if Linux state is FIN-WAIT-1, but FIN is still not sent. 24231da177e4SLinus Torvalds * 24241da177e4SLinus Torvalds * The visible declinations are that sometimes 24251da177e4SLinus Torvalds * we enter time-wait state, when it is not required really 24261da177e4SLinus Torvalds * (harmless), do not send active resets, when they are 24271da177e4SLinus Torvalds * required by specs (TCP_ESTABLISHED, TCP_CLOSE_WAIT, when 24281da177e4SLinus Torvalds * they look as CLOSING or LAST_ACK for Linux) 24291da177e4SLinus Torvalds * Probably, I missed some more holelets. 24301da177e4SLinus Torvalds * --ANK 24318336886fSJerry Chu * XXX (TFO) - To start off we don't support SYN+ACK+FIN 24328336886fSJerry Chu * in a single packet! (May consider it later but will 24338336886fSJerry Chu * probably need API support or TCP_CORK SYN-ACK until 24348336886fSJerry Chu * data is written and socket is closed.) 24351da177e4SLinus Torvalds */ 24361da177e4SLinus Torvalds tcp_send_fin(sk); 24371da177e4SLinus Torvalds } 24381da177e4SLinus Torvalds 24391da177e4SLinus Torvalds sk_stream_wait_close(sk, timeout); 24401da177e4SLinus Torvalds 24411da177e4SLinus Torvalds adjudge_to_death: 244275c2d907SHerbert Xu state = sk->sk_state; 244375c2d907SHerbert Xu sock_hold(sk); 244475c2d907SHerbert Xu sock_orphan(sk); 244575c2d907SHerbert Xu 24461da177e4SLinus Torvalds local_bh_disable(); 24471da177e4SLinus Torvalds bh_lock_sock(sk); 24488873c064SEric Dumazet /* remove backlog if any, without releasing ownership. */ 24498873c064SEric Dumazet __release_sock(sk); 24501da177e4SLinus Torvalds 2451eb4dea58SHerbert Xu percpu_counter_inc(sk->sk_prot->orphan_count); 2452eb4dea58SHerbert Xu 245375c2d907SHerbert Xu /* Have we already been destroyed by a softirq or backlog? */ 245475c2d907SHerbert Xu if (state != TCP_CLOSE && sk->sk_state == TCP_CLOSE) 245575c2d907SHerbert Xu goto out; 24561da177e4SLinus Torvalds 24571da177e4SLinus Torvalds /* This is a (useful) BSD violating of the RFC. There is a 24581da177e4SLinus Torvalds * problem with TCP as specified in that the other end could 24591da177e4SLinus Torvalds * keep a socket open forever with no application left this end. 2460b10bd54cSJesper Juhl * We use a 1 minute timeout (about the same as BSD) then kill 24611da177e4SLinus Torvalds * our end. If they send after that then tough - BUT: long enough 24621da177e4SLinus Torvalds * that we won't make the old 4*rto = almost no time - whoops 24631da177e4SLinus Torvalds * reset mistake. 24641da177e4SLinus Torvalds * 24651da177e4SLinus Torvalds * Nope, it was not mistake. It is really desired behaviour 24661da177e4SLinus Torvalds * f.e. on http servers, when such sockets are useless, but 24671da177e4SLinus Torvalds * consume significant resources. Let's do it with special 24681da177e4SLinus Torvalds * linger2 option. --ANK 24691da177e4SLinus Torvalds */ 24701da177e4SLinus Torvalds 24711da177e4SLinus Torvalds if (sk->sk_state == TCP_FIN_WAIT2) { 24721da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 24731da177e4SLinus Torvalds if (tp->linger2 < 0) { 24741da177e4SLinus Torvalds tcp_set_state(sk, TCP_CLOSE); 24751da177e4SLinus Torvalds tcp_send_active_reset(sk, GFP_ATOMIC); 247602a1d6e7SEric Dumazet __NET_INC_STATS(sock_net(sk), 2477de0744afSPavel Emelyanov LINUX_MIB_TCPABORTONLINGER); 24781da177e4SLinus Torvalds } else { 2479463c84b9SArnaldo Carvalho de Melo const int tmo = tcp_fin_time(sk); 24801da177e4SLinus Torvalds 24811da177e4SLinus Torvalds if (tmo > TCP_TIMEWAIT_LEN) { 248252499afeSDavid S. Miller inet_csk_reset_keepalive_timer(sk, 248352499afeSDavid S. Miller tmo - TCP_TIMEWAIT_LEN); 24841da177e4SLinus Torvalds } else { 24851da177e4SLinus Torvalds tcp_time_wait(sk, TCP_FIN_WAIT2, tmo); 24861da177e4SLinus Torvalds goto out; 24871da177e4SLinus Torvalds } 24881da177e4SLinus Torvalds } 24891da177e4SLinus Torvalds } 24901da177e4SLinus Torvalds if (sk->sk_state != TCP_CLOSE) { 24913ab224beSHideo Aoki sk_mem_reclaim(sk); 2492efcdbf24SArun Sharma if (tcp_check_oom(sk, 0)) { 24931da177e4SLinus Torvalds tcp_set_state(sk, TCP_CLOSE); 24941da177e4SLinus Torvalds tcp_send_active_reset(sk, GFP_ATOMIC); 249502a1d6e7SEric Dumazet __NET_INC_STATS(sock_net(sk), 2496de0744afSPavel Emelyanov LINUX_MIB_TCPABORTONMEMORY); 24974ee806d5SDan Streetman } else if (!check_net(sock_net(sk))) { 24984ee806d5SDan Streetman /* Not possible to send reset; just close */ 24994ee806d5SDan Streetman tcp_set_state(sk, TCP_CLOSE); 25001da177e4SLinus Torvalds } 25011da177e4SLinus Torvalds } 25021da177e4SLinus Torvalds 25038336886fSJerry Chu if (sk->sk_state == TCP_CLOSE) { 2504d983ea6fSEric Dumazet struct request_sock *req; 2505d983ea6fSEric Dumazet 2506d983ea6fSEric Dumazet req = rcu_dereference_protected(tcp_sk(sk)->fastopen_rsk, 2507d983ea6fSEric Dumazet lockdep_sock_is_held(sk)); 25088336886fSJerry Chu /* We could get here with a non-NULL req if the socket is 25098336886fSJerry Chu * aborted (e.g., closed with unread data) before 3WHS 25108336886fSJerry Chu * finishes. 25118336886fSJerry Chu */ 251200db4124SIan Morris if (req) 25138336886fSJerry Chu reqsk_fastopen_remove(sk, req, false); 25140a5578cfSArnaldo Carvalho de Melo inet_csk_destroy_sock(sk); 25158336886fSJerry Chu } 25161da177e4SLinus Torvalds /* Otherwise, socket is reprieved until protocol close. */ 25171da177e4SLinus Torvalds 25181da177e4SLinus Torvalds out: 25191da177e4SLinus Torvalds bh_unlock_sock(sk); 25201da177e4SLinus Torvalds local_bh_enable(); 25218873c064SEric Dumazet release_sock(sk); 25221da177e4SLinus Torvalds sock_put(sk); 25231da177e4SLinus Torvalds } 25244bc2f18bSEric Dumazet EXPORT_SYMBOL(tcp_close); 25251da177e4SLinus Torvalds 25261da177e4SLinus Torvalds /* These states need RST on ABORT according to RFC793 */ 25271da177e4SLinus Torvalds 2528a2a385d6SEric Dumazet static inline bool tcp_need_reset(int state) 25291da177e4SLinus Torvalds { 25301da177e4SLinus Torvalds return (1 << state) & 25311da177e4SLinus Torvalds (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT | TCPF_FIN_WAIT1 | 25321da177e4SLinus Torvalds TCPF_FIN_WAIT2 | TCPF_SYN_RECV); 25331da177e4SLinus Torvalds } 25341da177e4SLinus Torvalds 253575c119afSEric Dumazet static void tcp_rtx_queue_purge(struct sock *sk) 253675c119afSEric Dumazet { 253775c119afSEric Dumazet struct rb_node *p = rb_first(&sk->tcp_rtx_queue); 253875c119afSEric Dumazet 25392bec445fSEric Dumazet tcp_sk(sk)->highest_sack = NULL; 254075c119afSEric Dumazet while (p) { 254175c119afSEric Dumazet struct sk_buff *skb = rb_to_skb(p); 254275c119afSEric Dumazet 254375c119afSEric Dumazet p = rb_next(p); 254475c119afSEric Dumazet /* Since we are deleting whole queue, no need to 254575c119afSEric Dumazet * list_del(&skb->tcp_tsorted_anchor) 254675c119afSEric Dumazet */ 254775c119afSEric Dumazet tcp_rtx_queue_unlink(skb, sk); 254875c119afSEric Dumazet sk_wmem_free_skb(sk, skb); 254975c119afSEric Dumazet } 255075c119afSEric Dumazet } 255175c119afSEric Dumazet 2552ac3f09baSEric Dumazet void tcp_write_queue_purge(struct sock *sk) 2553ac3f09baSEric Dumazet { 2554ac3f09baSEric Dumazet struct sk_buff *skb; 2555ac3f09baSEric Dumazet 2556ac3f09baSEric Dumazet tcp_chrono_stop(sk, TCP_CHRONO_BUSY); 2557ac3f09baSEric Dumazet while ((skb = __skb_dequeue(&sk->sk_write_queue)) != NULL) { 2558ac3f09baSEric Dumazet tcp_skb_tsorted_anchor_cleanup(skb); 2559ac3f09baSEric Dumazet sk_wmem_free_skb(sk, skb); 2560ac3f09baSEric Dumazet } 256175c119afSEric Dumazet tcp_rtx_queue_purge(sk); 2562472c2e07SEric Dumazet skb = sk->sk_tx_skb_cache; 2563472c2e07SEric Dumazet if (skb) { 2564472c2e07SEric Dumazet __kfree_skb(skb); 2565472c2e07SEric Dumazet sk->sk_tx_skb_cache = NULL; 2566472c2e07SEric Dumazet } 2567ac3f09baSEric Dumazet INIT_LIST_HEAD(&tcp_sk(sk)->tsorted_sent_queue); 2568ac3f09baSEric Dumazet sk_mem_reclaim(sk); 2569ac3f09baSEric Dumazet tcp_clear_all_retrans_hints(tcp_sk(sk)); 2570bffd168cSSoheil Hassas Yeganeh tcp_sk(sk)->packets_out = 0; 257104c03114SEric Dumazet inet_csk(sk)->icsk_backoff = 0; 2572ac3f09baSEric Dumazet } 2573ac3f09baSEric Dumazet 25741da177e4SLinus Torvalds int tcp_disconnect(struct sock *sk, int flags) 25751da177e4SLinus Torvalds { 25761da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 2577463c84b9SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 25781da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 25791da177e4SLinus Torvalds int old_state = sk->sk_state; 25800f317464SEric Dumazet u32 seq; 25811da177e4SLinus Torvalds 25821da177e4SLinus Torvalds if (old_state != TCP_CLOSE) 25831da177e4SLinus Torvalds tcp_set_state(sk, TCP_CLOSE); 25841da177e4SLinus Torvalds 25851da177e4SLinus Torvalds /* ABORT function of RFC793 */ 25861da177e4SLinus Torvalds if (old_state == TCP_LISTEN) { 25870a5578cfSArnaldo Carvalho de Melo inet_csk_listen_stop(sk); 2588ee995283SPavel Emelyanov } else if (unlikely(tp->repair)) { 2589ee995283SPavel Emelyanov sk->sk_err = ECONNABORTED; 25901da177e4SLinus Torvalds } else if (tcp_need_reset(old_state) || 25911da177e4SLinus Torvalds (tp->snd_nxt != tp->write_seq && 25921da177e4SLinus Torvalds (1 << old_state) & (TCPF_CLOSING | TCPF_LAST_ACK))) { 2593caa20d9aSStephen Hemminger /* The last check adjusts for discrepancy of Linux wrt. RFC 25941da177e4SLinus Torvalds * states 25951da177e4SLinus Torvalds */ 25961da177e4SLinus Torvalds tcp_send_active_reset(sk, gfp_any()); 25971da177e4SLinus Torvalds sk->sk_err = ECONNRESET; 25981da177e4SLinus Torvalds } else if (old_state == TCP_SYN_SENT) 25991da177e4SLinus Torvalds sk->sk_err = ECONNRESET; 26001da177e4SLinus Torvalds 26011da177e4SLinus Torvalds tcp_clear_xmit_timers(sk); 26021da177e4SLinus Torvalds __skb_queue_purge(&sk->sk_receive_queue); 26038b27dae5SEric Dumazet if (sk->sk_rx_skb_cache) { 26048b27dae5SEric Dumazet __kfree_skb(sk->sk_rx_skb_cache); 26058b27dae5SEric Dumazet sk->sk_rx_skb_cache = NULL; 26068b27dae5SEric Dumazet } 26077db48e98SEric Dumazet WRITE_ONCE(tp->copied_seq, tp->rcv_nxt); 26086508b678SEric Dumazet tp->urg_data = 0; 2609fe067e8aSDavid S. Miller tcp_write_queue_purge(sk); 2610cf1ef3f0SWei Wang tcp_fastopen_active_disable_ofo_check(sk); 26119f5afeaeSYaogong Wang skb_rbtree_purge(&tp->out_of_order_queue); 26121da177e4SLinus Torvalds 2613c720c7e8SEric Dumazet inet->inet_dport = 0; 26141da177e4SLinus Torvalds 26151da177e4SLinus Torvalds if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK)) 26161da177e4SLinus Torvalds inet_reset_saddr(sk); 26171da177e4SLinus Torvalds 26181da177e4SLinus Torvalds sk->sk_shutdown = 0; 26191da177e4SLinus Torvalds sock_reset_flag(sk, SOCK_DONE); 2620740b0f18SEric Dumazet tp->srtt_us = 0; 2621b9e2e689SEric Dumazet tp->mdev_us = jiffies_to_usecs(TCP_TIMEOUT_INIT); 26223f6c65d6SWei Wang tp->rcv_rtt_last_tsecr = 0; 26230f317464SEric Dumazet 26240f317464SEric Dumazet seq = tp->write_seq + tp->max_window + 2; 26250f317464SEric Dumazet if (!seq) 26260f317464SEric Dumazet seq = 1; 26270f317464SEric Dumazet WRITE_ONCE(tp->write_seq, seq); 26280f317464SEric Dumazet 2629463c84b9SArnaldo Carvalho de Melo icsk->icsk_backoff = 0; 26306687e988SArnaldo Carvalho de Melo icsk->icsk_probes_out = 0; 26316a408147SEric Dumazet icsk->icsk_rto = TCP_TIMEOUT_INIT; 26320b6a05c1SIlpo Järvinen tp->snd_ssthresh = TCP_INFINITE_SSTHRESH; 26333a9a57f6SEric Dumazet tp->snd_cwnd = TCP_INIT_CWND; 26341da177e4SLinus Torvalds tp->snd_cwnd_cnt = 0; 26351fdf475aSEric Dumazet tp->window_clamp = 0; 26362fbdd562SEric Dumazet tp->delivered = 0; 2637e21db6f6SYuchung Cheng tp->delivered_ce = 0; 26386687e988SArnaldo Carvalho de Melo tcp_set_ca_state(sk, TCP_CA_Open); 2639d4761754SYousuk Seung tp->is_sack_reneg = 0; 26401da177e4SLinus Torvalds tcp_clear_retrans(tp); 2641c13c48c0SEric Dumazet tp->total_retrans = 0; 2642463c84b9SArnaldo Carvalho de Melo inet_csk_delack_init(sk); 2643499350a5SWei Wang /* Initialize rcv_mss to TCP_MIN_MSS to avoid division by 0 2644499350a5SWei Wang * issue in __tcp_select_window() 2645499350a5SWei Wang */ 2646499350a5SWei Wang icsk->icsk_ack.rcv_mss = TCP_MIN_MSS; 2647b40b4f79SSrinivas Aji memset(&tp->rx_opt, 0, sizeof(tp->rx_opt)); 26481da177e4SLinus Torvalds __sk_dst_reset(sk); 2649d747a7a5SWANG Cong dst_release(sk->sk_rx_dst); 2650d747a7a5SWANG Cong sk->sk_rx_dst = NULL; 265117c3060bSEric Dumazet tcp_saved_syn_free(tp); 26525d9f4262SEric Dumazet tp->compressed_ack = 0; 2653784f8344SEric Dumazet tp->segs_in = 0; 2654784f8344SEric Dumazet tp->segs_out = 0; 2655ba113c3aSWei Wang tp->bytes_sent = 0; 2656e858faf5SChristoph Paasch tp->bytes_acked = 0; 2657e858faf5SChristoph Paasch tp->bytes_received = 0; 2658fb31c9b9SWei Wang tp->bytes_retrans = 0; 2659db7ffee6SEric Dumazet tp->data_segs_in = 0; 2660db7ffee6SEric Dumazet tp->data_segs_out = 0; 26617788174eSYuchung Cheng tp->duplicate_sack[0].start_seq = 0; 26627788174eSYuchung Cheng tp->duplicate_sack[0].end_seq = 0; 26637e10b655SWei Wang tp->dsack_dups = 0; 26647ec65372SWei Wang tp->reord_seen = 0; 26655c701549SEric Dumazet tp->retrans_out = 0; 26665c701549SEric Dumazet tp->sacked_out = 0; 26675c701549SEric Dumazet tp->tlp_high_seq = 0; 26685c701549SEric Dumazet tp->last_oow_ack_time = 0; 26696cda8b74SEric Dumazet /* There's a bubble in the pipe until at least the first ACK. */ 26706cda8b74SEric Dumazet tp->app_limited = ~0U; 2671792c4354SEric Dumazet tp->rack.mstamp = 0; 2672792c4354SEric Dumazet tp->rack.advanced = 0; 2673792c4354SEric Dumazet tp->rack.reo_wnd_steps = 1; 2674792c4354SEric Dumazet tp->rack.last_delivered = 0; 2675792c4354SEric Dumazet tp->rack.reo_wnd_persist = 0; 2676792c4354SEric Dumazet tp->rack.dsack_seen = 0; 26776bcdc40dSEric Dumazet tp->syn_data_acked = 0; 26786bcdc40dSEric Dumazet tp->rx_opt.saw_tstamp = 0; 26796bcdc40dSEric Dumazet tp->rx_opt.dsack = 0; 26806bcdc40dSEric Dumazet tp->rx_opt.num_sacks = 0; 2681f9af2dbbSThomas Higdon tp->rcv_ooopack = 0; 26826cda8b74SEric Dumazet 26831da177e4SLinus Torvalds 26847db92362SWei Wang /* Clean up fastopen related fields */ 26857db92362SWei Wang tcp_free_fastopen_req(tp); 26867db92362SWei Wang inet->defer_connect = 0; 268748027478SJason Baron tp->fastopen_client_fail = 0; 26887db92362SWei Wang 2689c720c7e8SEric Dumazet WARN_ON(inet->inet_num && !icsk->icsk_bind_hash); 26901da177e4SLinus Torvalds 26919b42d55aSLi RongQing if (sk->sk_frag.page) { 26929b42d55aSLi RongQing put_page(sk->sk_frag.page); 26939b42d55aSLi RongQing sk->sk_frag.page = NULL; 26949b42d55aSLi RongQing sk->sk_frag.offset = 0; 26959b42d55aSLi RongQing } 26969b42d55aSLi RongQing 26971da177e4SLinus Torvalds sk->sk_error_report(sk); 2698a01512b1SYueHaibing return 0; 26991da177e4SLinus Torvalds } 27004bc2f18bSEric Dumazet EXPORT_SYMBOL(tcp_disconnect); 27011da177e4SLinus Torvalds 2702a2a385d6SEric Dumazet static inline bool tcp_can_repair_sock(const struct sock *sk) 2703ee995283SPavel Emelyanov { 270452e804c6SEric W. Biederman return ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN) && 2705319b0534SAndrey Vagin (sk->sk_state != TCP_LISTEN); 2706ee995283SPavel Emelyanov } 2707ee995283SPavel Emelyanov 2708b1ed4c4fSAndrey Vagin static int tcp_repair_set_window(struct tcp_sock *tp, char __user *optbuf, int len) 2709b1ed4c4fSAndrey Vagin { 2710b1ed4c4fSAndrey Vagin struct tcp_repair_window opt; 2711b1ed4c4fSAndrey Vagin 2712b1ed4c4fSAndrey Vagin if (!tp->repair) 2713b1ed4c4fSAndrey Vagin return -EPERM; 2714b1ed4c4fSAndrey Vagin 2715b1ed4c4fSAndrey Vagin if (len != sizeof(opt)) 2716b1ed4c4fSAndrey Vagin return -EINVAL; 2717b1ed4c4fSAndrey Vagin 2718b1ed4c4fSAndrey Vagin if (copy_from_user(&opt, optbuf, sizeof(opt))) 2719b1ed4c4fSAndrey Vagin return -EFAULT; 2720b1ed4c4fSAndrey Vagin 2721b1ed4c4fSAndrey Vagin if (opt.max_window < opt.snd_wnd) 2722b1ed4c4fSAndrey Vagin return -EINVAL; 2723b1ed4c4fSAndrey Vagin 2724b1ed4c4fSAndrey Vagin if (after(opt.snd_wl1, tp->rcv_nxt + opt.rcv_wnd)) 2725b1ed4c4fSAndrey Vagin return -EINVAL; 2726b1ed4c4fSAndrey Vagin 2727b1ed4c4fSAndrey Vagin if (after(opt.rcv_wup, tp->rcv_nxt)) 2728b1ed4c4fSAndrey Vagin return -EINVAL; 2729b1ed4c4fSAndrey Vagin 2730b1ed4c4fSAndrey Vagin tp->snd_wl1 = opt.snd_wl1; 2731b1ed4c4fSAndrey Vagin tp->snd_wnd = opt.snd_wnd; 2732b1ed4c4fSAndrey Vagin tp->max_window = opt.max_window; 2733b1ed4c4fSAndrey Vagin 2734b1ed4c4fSAndrey Vagin tp->rcv_wnd = opt.rcv_wnd; 2735b1ed4c4fSAndrey Vagin tp->rcv_wup = opt.rcv_wup; 2736b1ed4c4fSAndrey Vagin 2737b1ed4c4fSAndrey Vagin return 0; 2738b1ed4c4fSAndrey Vagin } 2739b1ed4c4fSAndrey Vagin 274015e56515SDouglas Caetano dos Santos static int tcp_repair_options_est(struct sock *sk, 2741de248a75SPavel Emelyanov struct tcp_repair_opt __user *optbuf, unsigned int len) 2742b139ba4eSPavel Emelyanov { 274315e56515SDouglas Caetano dos Santos struct tcp_sock *tp = tcp_sk(sk); 2744de248a75SPavel Emelyanov struct tcp_repair_opt opt; 2745b139ba4eSPavel Emelyanov 2746de248a75SPavel Emelyanov while (len >= sizeof(opt)) { 2747de248a75SPavel Emelyanov if (copy_from_user(&opt, optbuf, sizeof(opt))) 2748b139ba4eSPavel Emelyanov return -EFAULT; 2749b139ba4eSPavel Emelyanov 2750b139ba4eSPavel Emelyanov optbuf++; 2751de248a75SPavel Emelyanov len -= sizeof(opt); 2752b139ba4eSPavel Emelyanov 2753de248a75SPavel Emelyanov switch (opt.opt_code) { 2754de248a75SPavel Emelyanov case TCPOPT_MSS: 2755de248a75SPavel Emelyanov tp->rx_opt.mss_clamp = opt.opt_val; 275615e56515SDouglas Caetano dos Santos tcp_mtup_init(sk); 2757b139ba4eSPavel Emelyanov break; 2758de248a75SPavel Emelyanov case TCPOPT_WINDOW: 2759bc26ccd8SAndrey Vagin { 2760bc26ccd8SAndrey Vagin u16 snd_wscale = opt.opt_val & 0xFFFF; 2761bc26ccd8SAndrey Vagin u16 rcv_wscale = opt.opt_val >> 16; 2762bc26ccd8SAndrey Vagin 2763589c49cbSGao Feng if (snd_wscale > TCP_MAX_WSCALE || rcv_wscale > TCP_MAX_WSCALE) 2764b139ba4eSPavel Emelyanov return -EFBIG; 2765b139ba4eSPavel Emelyanov 2766bc26ccd8SAndrey Vagin tp->rx_opt.snd_wscale = snd_wscale; 2767bc26ccd8SAndrey Vagin tp->rx_opt.rcv_wscale = rcv_wscale; 2768bc26ccd8SAndrey Vagin tp->rx_opt.wscale_ok = 1; 2769bc26ccd8SAndrey Vagin } 2770b139ba4eSPavel Emelyanov break; 2771b139ba4eSPavel Emelyanov case TCPOPT_SACK_PERM: 2772de248a75SPavel Emelyanov if (opt.opt_val != 0) 2773de248a75SPavel Emelyanov return -EINVAL; 2774de248a75SPavel Emelyanov 2775b139ba4eSPavel Emelyanov tp->rx_opt.sack_ok |= TCP_SACK_SEEN; 2776b139ba4eSPavel Emelyanov break; 2777b139ba4eSPavel Emelyanov case TCPOPT_TIMESTAMP: 2778de248a75SPavel Emelyanov if (opt.opt_val != 0) 2779de248a75SPavel Emelyanov return -EINVAL; 2780de248a75SPavel Emelyanov 2781b139ba4eSPavel Emelyanov tp->rx_opt.tstamp_ok = 1; 2782b139ba4eSPavel Emelyanov break; 2783b139ba4eSPavel Emelyanov } 2784b139ba4eSPavel Emelyanov } 2785b139ba4eSPavel Emelyanov 2786b139ba4eSPavel Emelyanov return 0; 2787b139ba4eSPavel Emelyanov } 2788b139ba4eSPavel Emelyanov 2789a842fe14SEric Dumazet DEFINE_STATIC_KEY_FALSE(tcp_tx_delay_enabled); 2790a842fe14SEric Dumazet EXPORT_SYMBOL(tcp_tx_delay_enabled); 2791a842fe14SEric Dumazet 2792a842fe14SEric Dumazet static void tcp_enable_tx_delay(void) 2793a842fe14SEric Dumazet { 2794a842fe14SEric Dumazet if (!static_branch_unlikely(&tcp_tx_delay_enabled)) { 2795a842fe14SEric Dumazet static int __tcp_tx_delay_enabled = 0; 2796a842fe14SEric Dumazet 2797a842fe14SEric Dumazet if (cmpxchg(&__tcp_tx_delay_enabled, 0, 1) == 0) { 2798a842fe14SEric Dumazet static_branch_enable(&tcp_tx_delay_enabled); 2799a842fe14SEric Dumazet pr_info("TCP_TX_DELAY enabled\n"); 2800a842fe14SEric Dumazet } 2801a842fe14SEric Dumazet } 2802a842fe14SEric Dumazet } 2803a842fe14SEric Dumazet 2804db10538aSChristoph Hellwig /* When set indicates to always queue non-full frames. Later the user clears 2805db10538aSChristoph Hellwig * this option and we transmit any pending partial frames in the queue. This is 2806db10538aSChristoph Hellwig * meant to be used alongside sendfile() to get properly filled frames when the 2807db10538aSChristoph Hellwig * user (for example) must write out headers with a write() call first and then 2808db10538aSChristoph Hellwig * use sendfile to send out the data parts. 2809db10538aSChristoph Hellwig * 2810db10538aSChristoph Hellwig * TCP_CORK can be set together with TCP_NODELAY and it is stronger than 2811db10538aSChristoph Hellwig * TCP_NODELAY. 2812db10538aSChristoph Hellwig */ 2813db10538aSChristoph Hellwig static void __tcp_sock_set_cork(struct sock *sk, bool on) 2814db10538aSChristoph Hellwig { 2815db10538aSChristoph Hellwig struct tcp_sock *tp = tcp_sk(sk); 2816db10538aSChristoph Hellwig 2817db10538aSChristoph Hellwig if (on) { 2818db10538aSChristoph Hellwig tp->nonagle |= TCP_NAGLE_CORK; 2819db10538aSChristoph Hellwig } else { 2820db10538aSChristoph Hellwig tp->nonagle &= ~TCP_NAGLE_CORK; 2821db10538aSChristoph Hellwig if (tp->nonagle & TCP_NAGLE_OFF) 2822db10538aSChristoph Hellwig tp->nonagle |= TCP_NAGLE_PUSH; 2823db10538aSChristoph Hellwig tcp_push_pending_frames(sk); 2824db10538aSChristoph Hellwig } 2825db10538aSChristoph Hellwig } 2826db10538aSChristoph Hellwig 2827db10538aSChristoph Hellwig void tcp_sock_set_cork(struct sock *sk, bool on) 2828db10538aSChristoph Hellwig { 2829db10538aSChristoph Hellwig lock_sock(sk); 2830db10538aSChristoph Hellwig __tcp_sock_set_cork(sk, on); 2831db10538aSChristoph Hellwig release_sock(sk); 2832db10538aSChristoph Hellwig } 2833db10538aSChristoph Hellwig EXPORT_SYMBOL(tcp_sock_set_cork); 2834db10538aSChristoph Hellwig 283512abc5eeSChristoph Hellwig /* TCP_NODELAY is weaker than TCP_CORK, so that this option on corked socket is 283612abc5eeSChristoph Hellwig * remembered, but it is not activated until cork is cleared. 283712abc5eeSChristoph Hellwig * 283812abc5eeSChristoph Hellwig * However, when TCP_NODELAY is set we make an explicit push, which overrides 283912abc5eeSChristoph Hellwig * even TCP_CORK for currently queued segments. 284012abc5eeSChristoph Hellwig */ 284112abc5eeSChristoph Hellwig static void __tcp_sock_set_nodelay(struct sock *sk, bool on) 284212abc5eeSChristoph Hellwig { 284312abc5eeSChristoph Hellwig if (on) { 284412abc5eeSChristoph Hellwig tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF|TCP_NAGLE_PUSH; 284512abc5eeSChristoph Hellwig tcp_push_pending_frames(sk); 284612abc5eeSChristoph Hellwig } else { 284712abc5eeSChristoph Hellwig tcp_sk(sk)->nonagle &= ~TCP_NAGLE_OFF; 284812abc5eeSChristoph Hellwig } 284912abc5eeSChristoph Hellwig } 285012abc5eeSChristoph Hellwig 285112abc5eeSChristoph Hellwig void tcp_sock_set_nodelay(struct sock *sk) 285212abc5eeSChristoph Hellwig { 285312abc5eeSChristoph Hellwig lock_sock(sk); 285412abc5eeSChristoph Hellwig __tcp_sock_set_nodelay(sk, true); 285512abc5eeSChristoph Hellwig release_sock(sk); 285612abc5eeSChristoph Hellwig } 285712abc5eeSChristoph Hellwig EXPORT_SYMBOL(tcp_sock_set_nodelay); 285812abc5eeSChristoph Hellwig 2859ddd061b8SChristoph Hellwig static void __tcp_sock_set_quickack(struct sock *sk, int val) 2860ddd061b8SChristoph Hellwig { 2861ddd061b8SChristoph Hellwig if (!val) { 2862ddd061b8SChristoph Hellwig inet_csk_enter_pingpong_mode(sk); 2863ddd061b8SChristoph Hellwig return; 2864ddd061b8SChristoph Hellwig } 2865ddd061b8SChristoph Hellwig 2866ddd061b8SChristoph Hellwig inet_csk_exit_pingpong_mode(sk); 2867ddd061b8SChristoph Hellwig if ((1 << sk->sk_state) & (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT) && 2868ddd061b8SChristoph Hellwig inet_csk_ack_scheduled(sk)) { 2869ddd061b8SChristoph Hellwig inet_csk(sk)->icsk_ack.pending |= ICSK_ACK_PUSHED; 2870ddd061b8SChristoph Hellwig tcp_cleanup_rbuf(sk, 1); 2871ddd061b8SChristoph Hellwig if (!(val & 1)) 2872ddd061b8SChristoph Hellwig inet_csk_enter_pingpong_mode(sk); 2873ddd061b8SChristoph Hellwig } 2874ddd061b8SChristoph Hellwig } 2875ddd061b8SChristoph Hellwig 2876ddd061b8SChristoph Hellwig void tcp_sock_set_quickack(struct sock *sk, int val) 2877ddd061b8SChristoph Hellwig { 2878ddd061b8SChristoph Hellwig lock_sock(sk); 2879ddd061b8SChristoph Hellwig __tcp_sock_set_quickack(sk, val); 2880ddd061b8SChristoph Hellwig release_sock(sk); 2881ddd061b8SChristoph Hellwig } 2882ddd061b8SChristoph Hellwig EXPORT_SYMBOL(tcp_sock_set_quickack); 2883ddd061b8SChristoph Hellwig 2884557eadfcSChristoph Hellwig int tcp_sock_set_syncnt(struct sock *sk, int val) 2885557eadfcSChristoph Hellwig { 2886557eadfcSChristoph Hellwig if (val < 1 || val > MAX_TCP_SYNCNT) 2887557eadfcSChristoph Hellwig return -EINVAL; 2888557eadfcSChristoph Hellwig 2889557eadfcSChristoph Hellwig lock_sock(sk); 2890557eadfcSChristoph Hellwig inet_csk(sk)->icsk_syn_retries = val; 2891557eadfcSChristoph Hellwig release_sock(sk); 2892557eadfcSChristoph Hellwig return 0; 2893557eadfcSChristoph Hellwig } 2894557eadfcSChristoph Hellwig EXPORT_SYMBOL(tcp_sock_set_syncnt); 2895557eadfcSChristoph Hellwig 2896c488aeadSChristoph Hellwig void tcp_sock_set_user_timeout(struct sock *sk, u32 val) 2897c488aeadSChristoph Hellwig { 2898c488aeadSChristoph Hellwig lock_sock(sk); 2899c488aeadSChristoph Hellwig inet_csk(sk)->icsk_user_timeout = val; 2900c488aeadSChristoph Hellwig release_sock(sk); 2901c488aeadSChristoph Hellwig } 2902c488aeadSChristoph Hellwig EXPORT_SYMBOL(tcp_sock_set_user_timeout); 2903c488aeadSChristoph Hellwig 290471c48eb8SChristoph Hellwig static int __tcp_sock_set_keepidle(struct sock *sk, int val) 290571c48eb8SChristoph Hellwig { 290671c48eb8SChristoph Hellwig struct tcp_sock *tp = tcp_sk(sk); 290771c48eb8SChristoph Hellwig 290871c48eb8SChristoph Hellwig if (val < 1 || val > MAX_TCP_KEEPIDLE) 290971c48eb8SChristoph Hellwig return -EINVAL; 291071c48eb8SChristoph Hellwig 291171c48eb8SChristoph Hellwig tp->keepalive_time = val * HZ; 291271c48eb8SChristoph Hellwig if (sock_flag(sk, SOCK_KEEPOPEN) && 291371c48eb8SChristoph Hellwig !((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN))) { 291471c48eb8SChristoph Hellwig u32 elapsed = keepalive_time_elapsed(tp); 291571c48eb8SChristoph Hellwig 291671c48eb8SChristoph Hellwig if (tp->keepalive_time > elapsed) 291771c48eb8SChristoph Hellwig elapsed = tp->keepalive_time - elapsed; 291871c48eb8SChristoph Hellwig else 291971c48eb8SChristoph Hellwig elapsed = 0; 292071c48eb8SChristoph Hellwig inet_csk_reset_keepalive_timer(sk, elapsed); 292171c48eb8SChristoph Hellwig } 292271c48eb8SChristoph Hellwig 292371c48eb8SChristoph Hellwig return 0; 292471c48eb8SChristoph Hellwig } 292571c48eb8SChristoph Hellwig 292671c48eb8SChristoph Hellwig int tcp_sock_set_keepidle(struct sock *sk, int val) 292771c48eb8SChristoph Hellwig { 292871c48eb8SChristoph Hellwig int err; 292971c48eb8SChristoph Hellwig 293071c48eb8SChristoph Hellwig lock_sock(sk); 293171c48eb8SChristoph Hellwig err = __tcp_sock_set_keepidle(sk, val); 293271c48eb8SChristoph Hellwig release_sock(sk); 293371c48eb8SChristoph Hellwig return err; 293471c48eb8SChristoph Hellwig } 293571c48eb8SChristoph Hellwig EXPORT_SYMBOL(tcp_sock_set_keepidle); 293671c48eb8SChristoph Hellwig 2937*d41ecaacSChristoph Hellwig int tcp_sock_set_keepintvl(struct sock *sk, int val) 2938*d41ecaacSChristoph Hellwig { 2939*d41ecaacSChristoph Hellwig if (val < 1 || val > MAX_TCP_KEEPINTVL) 2940*d41ecaacSChristoph Hellwig return -EINVAL; 2941*d41ecaacSChristoph Hellwig 2942*d41ecaacSChristoph Hellwig lock_sock(sk); 2943*d41ecaacSChristoph Hellwig tcp_sk(sk)->keepalive_intvl = val * HZ; 2944*d41ecaacSChristoph Hellwig release_sock(sk); 2945*d41ecaacSChristoph Hellwig return 0; 2946*d41ecaacSChristoph Hellwig } 2947*d41ecaacSChristoph Hellwig EXPORT_SYMBOL(tcp_sock_set_keepintvl); 2948*d41ecaacSChristoph Hellwig 29491da177e4SLinus Torvalds /* 29501da177e4SLinus Torvalds * Socket option code for TCP. 29511da177e4SLinus Torvalds */ 29523fdadf7dSDmitry Mishin static int do_tcp_setsockopt(struct sock *sk, int level, 2953b7058842SDavid S. Miller int optname, char __user *optval, unsigned int optlen) 29541da177e4SLinus Torvalds { 29551da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 2956463c84b9SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 29571e579caaSNikolay Borisov struct net *net = sock_net(sk); 29581da177e4SLinus Torvalds int val; 29591da177e4SLinus Torvalds int err = 0; 29601da177e4SLinus Torvalds 2961e56fb50fSWilliam Allen Simpson /* These are data/string values, all the others are ints */ 2962e56fb50fSWilliam Allen Simpson switch (optname) { 2963e56fb50fSWilliam Allen Simpson case TCP_CONGESTION: { 29645f8ef48dSStephen Hemminger char name[TCP_CA_NAME_MAX]; 29655f8ef48dSStephen Hemminger 29665f8ef48dSStephen Hemminger if (optlen < 1) 29675f8ef48dSStephen Hemminger return -EINVAL; 29685f8ef48dSStephen Hemminger 29695f8ef48dSStephen Hemminger val = strncpy_from_user(name, optval, 29704fdb78d3SAndrew Morton min_t(long, TCP_CA_NAME_MAX-1, optlen)); 29715f8ef48dSStephen Hemminger if (val < 0) 29725f8ef48dSStephen Hemminger return -EFAULT; 29735f8ef48dSStephen Hemminger name[val] = 0; 29745f8ef48dSStephen Hemminger 29755f8ef48dSStephen Hemminger lock_sock(sk); 29768d650cdeSEric Dumazet err = tcp_set_congestion_control(sk, name, true, true, 29778d650cdeSEric Dumazet ns_capable(sock_net(sk)->user_ns, 29788d650cdeSEric Dumazet CAP_NET_ADMIN)); 29795f8ef48dSStephen Hemminger release_sock(sk); 29805f8ef48dSStephen Hemminger return err; 29815f8ef48dSStephen Hemminger } 2982734942ccSDave Watson case TCP_ULP: { 2983734942ccSDave Watson char name[TCP_ULP_NAME_MAX]; 2984734942ccSDave Watson 2985734942ccSDave Watson if (optlen < 1) 2986734942ccSDave Watson return -EINVAL; 2987734942ccSDave Watson 2988734942ccSDave Watson val = strncpy_from_user(name, optval, 2989734942ccSDave Watson min_t(long, TCP_ULP_NAME_MAX - 1, 2990734942ccSDave Watson optlen)); 2991734942ccSDave Watson if (val < 0) 2992734942ccSDave Watson return -EFAULT; 2993734942ccSDave Watson name[val] = 0; 2994734942ccSDave Watson 2995734942ccSDave Watson lock_sock(sk); 2996734942ccSDave Watson err = tcp_set_ulp(sk, name); 2997734942ccSDave Watson release_sock(sk); 2998734942ccSDave Watson return err; 2999734942ccSDave Watson } 30001fba70e5SYuchung Cheng case TCP_FASTOPEN_KEY: { 30010f1ce023SJason Baron __u8 key[TCP_FASTOPEN_KEY_BUF_LENGTH]; 30020f1ce023SJason Baron __u8 *backup_key = NULL; 30031fba70e5SYuchung Cheng 30040f1ce023SJason Baron /* Allow a backup key as well to facilitate key rotation 30050f1ce023SJason Baron * First key is the active one. 30060f1ce023SJason Baron */ 30070f1ce023SJason Baron if (optlen != TCP_FASTOPEN_KEY_LENGTH && 30080f1ce023SJason Baron optlen != TCP_FASTOPEN_KEY_BUF_LENGTH) 30091fba70e5SYuchung Cheng return -EINVAL; 30101fba70e5SYuchung Cheng 30111fba70e5SYuchung Cheng if (copy_from_user(key, optval, optlen)) 30121fba70e5SYuchung Cheng return -EFAULT; 30131fba70e5SYuchung Cheng 30140f1ce023SJason Baron if (optlen == TCP_FASTOPEN_KEY_BUF_LENGTH) 30150f1ce023SJason Baron backup_key = key + TCP_FASTOPEN_KEY_LENGTH; 30160f1ce023SJason Baron 3017438ac880SArd Biesheuvel return tcp_fastopen_reset_cipher(net, sk, key, backup_key); 30181fba70e5SYuchung Cheng } 3019e56fb50fSWilliam Allen Simpson default: 3020e56fb50fSWilliam Allen Simpson /* fallthru */ 3021e56fb50fSWilliam Allen Simpson break; 3022ccbd6a5aSJoe Perches } 30235f8ef48dSStephen Hemminger 30241da177e4SLinus Torvalds if (optlen < sizeof(int)) 30251da177e4SLinus Torvalds return -EINVAL; 30261da177e4SLinus Torvalds 30271da177e4SLinus Torvalds if (get_user(val, (int __user *)optval)) 30281da177e4SLinus Torvalds return -EFAULT; 30291da177e4SLinus Torvalds 30301da177e4SLinus Torvalds lock_sock(sk); 30311da177e4SLinus Torvalds 30321da177e4SLinus Torvalds switch (optname) { 30331da177e4SLinus Torvalds case TCP_MAXSEG: 30341da177e4SLinus Torvalds /* Values greater than interface MTU won't take effect. However 30351da177e4SLinus Torvalds * at the point when this call is done we typically don't yet 3036a777f715SRohit Chavan * know which interface is going to be used 3037a777f715SRohit Chavan */ 3038cfc62d87SGao Feng if (val && (val < TCP_MIN_MSS || val > MAX_TCP_WINDOW)) { 30391da177e4SLinus Torvalds err = -EINVAL; 30401da177e4SLinus Torvalds break; 30411da177e4SLinus Torvalds } 30421da177e4SLinus Torvalds tp->rx_opt.user_mss = val; 30431da177e4SLinus Torvalds break; 30441da177e4SLinus Torvalds 30451da177e4SLinus Torvalds case TCP_NODELAY: 304612abc5eeSChristoph Hellwig __tcp_sock_set_nodelay(sk, val); 30471da177e4SLinus Torvalds break; 30481da177e4SLinus Torvalds 304936e31b0aSAndreas Petlund case TCP_THIN_LINEAR_TIMEOUTS: 305036e31b0aSAndreas Petlund if (val < 0 || val > 1) 305136e31b0aSAndreas Petlund err = -EINVAL; 305236e31b0aSAndreas Petlund else 305336e31b0aSAndreas Petlund tp->thin_lto = val; 305436e31b0aSAndreas Petlund break; 305536e31b0aSAndreas Petlund 30567e380175SAndreas Petlund case TCP_THIN_DUPACK: 30577e380175SAndreas Petlund if (val < 0 || val > 1) 30587e380175SAndreas Petlund err = -EINVAL; 30597e380175SAndreas Petlund break; 30607e380175SAndreas Petlund 3061ee995283SPavel Emelyanov case TCP_REPAIR: 3062ee995283SPavel Emelyanov if (!tcp_can_repair_sock(sk)) 3063ee995283SPavel Emelyanov err = -EPERM; 306431048d7aSStefan Baranoff else if (val == TCP_REPAIR_ON) { 3065ee995283SPavel Emelyanov tp->repair = 1; 3066ee995283SPavel Emelyanov sk->sk_reuse = SK_FORCE_REUSE; 3067ee995283SPavel Emelyanov tp->repair_queue = TCP_NO_QUEUE; 306831048d7aSStefan Baranoff } else if (val == TCP_REPAIR_OFF) { 3069ee995283SPavel Emelyanov tp->repair = 0; 3070ee995283SPavel Emelyanov sk->sk_reuse = SK_NO_REUSE; 3071ee995283SPavel Emelyanov tcp_send_window_probe(sk); 307231048d7aSStefan Baranoff } else if (val == TCP_REPAIR_OFF_NO_WP) { 307331048d7aSStefan Baranoff tp->repair = 0; 307431048d7aSStefan Baranoff sk->sk_reuse = SK_NO_REUSE; 3075ee995283SPavel Emelyanov } else 3076ee995283SPavel Emelyanov err = -EINVAL; 3077ee995283SPavel Emelyanov 3078ee995283SPavel Emelyanov break; 3079ee995283SPavel Emelyanov 3080ee995283SPavel Emelyanov case TCP_REPAIR_QUEUE: 3081ee995283SPavel Emelyanov if (!tp->repair) 3082ee995283SPavel Emelyanov err = -EPERM; 3083bf2acc94SEric Dumazet else if ((unsigned int)val < TCP_QUEUES_NR) 3084ee995283SPavel Emelyanov tp->repair_queue = val; 3085ee995283SPavel Emelyanov else 3086ee995283SPavel Emelyanov err = -EINVAL; 3087ee995283SPavel Emelyanov break; 3088ee995283SPavel Emelyanov 3089ee995283SPavel Emelyanov case TCP_QUEUE_SEQ: 3090ee995283SPavel Emelyanov if (sk->sk_state != TCP_CLOSE) 3091ee995283SPavel Emelyanov err = -EPERM; 3092ee995283SPavel Emelyanov else if (tp->repair_queue == TCP_SEND_QUEUE) 30930f317464SEric Dumazet WRITE_ONCE(tp->write_seq, val); 30946cd6cbf5SEric Dumazet else if (tp->repair_queue == TCP_RECV_QUEUE) { 3095dba7d9b8SEric Dumazet WRITE_ONCE(tp->rcv_nxt, val); 30966cd6cbf5SEric Dumazet WRITE_ONCE(tp->copied_seq, val); 30976cd6cbf5SEric Dumazet } 3098ee995283SPavel Emelyanov else 3099ee995283SPavel Emelyanov err = -EINVAL; 3100ee995283SPavel Emelyanov break; 3101ee995283SPavel Emelyanov 3102b139ba4eSPavel Emelyanov case TCP_REPAIR_OPTIONS: 3103b139ba4eSPavel Emelyanov if (!tp->repair) 3104b139ba4eSPavel Emelyanov err = -EINVAL; 3105b139ba4eSPavel Emelyanov else if (sk->sk_state == TCP_ESTABLISHED) 310615e56515SDouglas Caetano dos Santos err = tcp_repair_options_est(sk, 3107de248a75SPavel Emelyanov (struct tcp_repair_opt __user *)optval, 3108de248a75SPavel Emelyanov optlen); 3109b139ba4eSPavel Emelyanov else 3110b139ba4eSPavel Emelyanov err = -EPERM; 3111b139ba4eSPavel Emelyanov break; 3112b139ba4eSPavel Emelyanov 31131da177e4SLinus Torvalds case TCP_CORK: 3114db10538aSChristoph Hellwig __tcp_sock_set_cork(sk, val); 31151da177e4SLinus Torvalds break; 31161da177e4SLinus Torvalds 31171da177e4SLinus Torvalds case TCP_KEEPIDLE: 311871c48eb8SChristoph Hellwig err = __tcp_sock_set_keepidle(sk, val); 31191da177e4SLinus Torvalds break; 31201da177e4SLinus Torvalds case TCP_KEEPINTVL: 31211da177e4SLinus Torvalds if (val < 1 || val > MAX_TCP_KEEPINTVL) 31221da177e4SLinus Torvalds err = -EINVAL; 31231da177e4SLinus Torvalds else 31241da177e4SLinus Torvalds tp->keepalive_intvl = val * HZ; 31251da177e4SLinus Torvalds break; 31261da177e4SLinus Torvalds case TCP_KEEPCNT: 31271da177e4SLinus Torvalds if (val < 1 || val > MAX_TCP_KEEPCNT) 31281da177e4SLinus Torvalds err = -EINVAL; 31291da177e4SLinus Torvalds else 31301da177e4SLinus Torvalds tp->keepalive_probes = val; 31311da177e4SLinus Torvalds break; 31321da177e4SLinus Torvalds case TCP_SYNCNT: 31331da177e4SLinus Torvalds if (val < 1 || val > MAX_TCP_SYNCNT) 31341da177e4SLinus Torvalds err = -EINVAL; 31351da177e4SLinus Torvalds else 3136463c84b9SArnaldo Carvalho de Melo icsk->icsk_syn_retries = val; 31371da177e4SLinus Torvalds break; 31381da177e4SLinus Torvalds 3139cd8ae852SEric Dumazet case TCP_SAVE_SYN: 3140cd8ae852SEric Dumazet if (val < 0 || val > 1) 3141cd8ae852SEric Dumazet err = -EINVAL; 3142cd8ae852SEric Dumazet else 3143cd8ae852SEric Dumazet tp->save_syn = val; 3144cd8ae852SEric Dumazet break; 3145cd8ae852SEric Dumazet 31461da177e4SLinus Torvalds case TCP_LINGER2: 31471da177e4SLinus Torvalds if (val < 0) 31481da177e4SLinus Torvalds tp->linger2 = -1; 3149f0628c52SCambda Zhu else if (val > TCP_FIN_TIMEOUT_MAX / HZ) 3150f0628c52SCambda Zhu tp->linger2 = TCP_FIN_TIMEOUT_MAX; 31511da177e4SLinus Torvalds else 31521da177e4SLinus Torvalds tp->linger2 = val * HZ; 31531da177e4SLinus Torvalds break; 31541da177e4SLinus Torvalds 31551da177e4SLinus Torvalds case TCP_DEFER_ACCEPT: 3156b103cf34SJulian Anastasov /* Translate value in seconds to number of retransmits */ 3157b103cf34SJulian Anastasov icsk->icsk_accept_queue.rskq_defer_accept = 3158b103cf34SJulian Anastasov secs_to_retrans(val, TCP_TIMEOUT_INIT / HZ, 3159b103cf34SJulian Anastasov TCP_RTO_MAX / HZ); 31601da177e4SLinus Torvalds break; 31611da177e4SLinus Torvalds 31621da177e4SLinus Torvalds case TCP_WINDOW_CLAMP: 31631da177e4SLinus Torvalds if (!val) { 31641da177e4SLinus Torvalds if (sk->sk_state != TCP_CLOSE) { 31651da177e4SLinus Torvalds err = -EINVAL; 31661da177e4SLinus Torvalds break; 31671da177e4SLinus Torvalds } 31681da177e4SLinus Torvalds tp->window_clamp = 0; 31691da177e4SLinus Torvalds } else 31701da177e4SLinus Torvalds tp->window_clamp = val < SOCK_MIN_RCVBUF / 2 ? 31711da177e4SLinus Torvalds SOCK_MIN_RCVBUF / 2 : val; 31721da177e4SLinus Torvalds break; 31731da177e4SLinus Torvalds 31741da177e4SLinus Torvalds case TCP_QUICKACK: 3175ddd061b8SChristoph Hellwig __tcp_sock_set_quickack(sk, val); 31761da177e4SLinus Torvalds break; 31771da177e4SLinus Torvalds 3178cfb6eeb4SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG 3179cfb6eeb4SYOSHIFUJI Hideaki case TCP_MD5SIG: 31808917a777SIvan Delalande case TCP_MD5SIG_EXT: 318172123032SEric Dumazet if ((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN)) 31828917a777SIvan Delalande err = tp->af_specific->md5_parse(sk, optname, optval, optlen); 318372123032SEric Dumazet else 318472123032SEric Dumazet err = -EINVAL; 3185cfb6eeb4SYOSHIFUJI Hideaki break; 3186cfb6eeb4SYOSHIFUJI Hideaki #endif 3187dca43c75SJerry Chu case TCP_USER_TIMEOUT: 3188b248230cSYuchung Cheng /* Cap the max time in ms TCP will retry or probe the window 3189dca43c75SJerry Chu * before giving up and aborting (ETIMEDOUT) a connection. 3190dca43c75SJerry Chu */ 319142493570SHangbin Liu if (val < 0) 319242493570SHangbin Liu err = -EINVAL; 319342493570SHangbin Liu else 31949bcc66e1SJon Maxwell icsk->icsk_user_timeout = val; 3195dca43c75SJerry Chu break; 31968336886fSJerry Chu 31978336886fSJerry Chu case TCP_FASTOPEN: 31988336886fSJerry Chu if (val >= 0 && ((1 << sk->sk_state) & (TCPF_CLOSE | 3199dfea2aa6SChristoph Paasch TCPF_LISTEN))) { 320043713848SHaishuang Yan tcp_fastopen_init_key_once(net); 3201dfea2aa6SChristoph Paasch 32020536fcc0SEric Dumazet fastopen_queue_tune(sk, val); 3203dfea2aa6SChristoph Paasch } else { 32048336886fSJerry Chu err = -EINVAL; 3205dfea2aa6SChristoph Paasch } 32068336886fSJerry Chu break; 320719f6d3f3SWei Wang case TCP_FASTOPEN_CONNECT: 320819f6d3f3SWei Wang if (val > 1 || val < 0) { 320919f6d3f3SWei Wang err = -EINVAL; 3210e1cfcbe8SHaishuang Yan } else if (net->ipv4.sysctl_tcp_fastopen & TFO_CLIENT_ENABLE) { 321119f6d3f3SWei Wang if (sk->sk_state == TCP_CLOSE) 321219f6d3f3SWei Wang tp->fastopen_connect = val; 321319f6d3f3SWei Wang else 321419f6d3f3SWei Wang err = -EINVAL; 321519f6d3f3SWei Wang } else { 321619f6d3f3SWei Wang err = -EOPNOTSUPP; 321719f6d3f3SWei Wang } 321819f6d3f3SWei Wang break; 321971c02379SChristoph Paasch case TCP_FASTOPEN_NO_COOKIE: 322071c02379SChristoph Paasch if (val > 1 || val < 0) 322171c02379SChristoph Paasch err = -EINVAL; 322271c02379SChristoph Paasch else if (!((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN))) 322371c02379SChristoph Paasch err = -EINVAL; 322471c02379SChristoph Paasch else 322571c02379SChristoph Paasch tp->fastopen_no_cookie = val; 322671c02379SChristoph Paasch break; 322793be6ce0SAndrey Vagin case TCP_TIMESTAMP: 322893be6ce0SAndrey Vagin if (!tp->repair) 322993be6ce0SAndrey Vagin err = -EPERM; 323093be6ce0SAndrey Vagin else 32319a568de4SEric Dumazet tp->tsoffset = val - tcp_time_stamp_raw(); 323293be6ce0SAndrey Vagin break; 3233b1ed4c4fSAndrey Vagin case TCP_REPAIR_WINDOW: 3234b1ed4c4fSAndrey Vagin err = tcp_repair_set_window(tp, optval, optlen); 3235b1ed4c4fSAndrey Vagin break; 3236c9bee3b7SEric Dumazet case TCP_NOTSENT_LOWAT: 3237c9bee3b7SEric Dumazet tp->notsent_lowat = val; 3238c9bee3b7SEric Dumazet sk->sk_write_space(sk); 3239c9bee3b7SEric Dumazet break; 3240b75eba76SSoheil Hassas Yeganeh case TCP_INQ: 3241b75eba76SSoheil Hassas Yeganeh if (val > 1 || val < 0) 3242b75eba76SSoheil Hassas Yeganeh err = -EINVAL; 3243b75eba76SSoheil Hassas Yeganeh else 3244b75eba76SSoheil Hassas Yeganeh tp->recvmsg_inq = val; 3245b75eba76SSoheil Hassas Yeganeh break; 3246a842fe14SEric Dumazet case TCP_TX_DELAY: 3247a842fe14SEric Dumazet if (val) 3248a842fe14SEric Dumazet tcp_enable_tx_delay(); 3249a842fe14SEric Dumazet tp->tcp_tx_delay = val; 3250a842fe14SEric Dumazet break; 32511da177e4SLinus Torvalds default: 32521da177e4SLinus Torvalds err = -ENOPROTOOPT; 32531da177e4SLinus Torvalds break; 32543ff50b79SStephen Hemminger } 32553ff50b79SStephen Hemminger 32561da177e4SLinus Torvalds release_sock(sk); 32571da177e4SLinus Torvalds return err; 32581da177e4SLinus Torvalds } 32591da177e4SLinus Torvalds 32603fdadf7dSDmitry Mishin int tcp_setsockopt(struct sock *sk, int level, int optname, char __user *optval, 3261b7058842SDavid S. Miller unsigned int optlen) 32623fdadf7dSDmitry Mishin { 3263cf533ea5SEric Dumazet const struct inet_connection_sock *icsk = inet_csk(sk); 32643fdadf7dSDmitry Mishin 32653fdadf7dSDmitry Mishin if (level != SOL_TCP) 32663fdadf7dSDmitry Mishin return icsk->icsk_af_ops->setsockopt(sk, level, optname, 32673fdadf7dSDmitry Mishin optval, optlen); 32683fdadf7dSDmitry Mishin return do_tcp_setsockopt(sk, level, optname, optval, optlen); 32693fdadf7dSDmitry Mishin } 32704bc2f18bSEric Dumazet EXPORT_SYMBOL(tcp_setsockopt); 32713fdadf7dSDmitry Mishin 32723fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT 3273543d9cfeSArnaldo Carvalho de Melo int compat_tcp_setsockopt(struct sock *sk, int level, int optname, 3274b7058842SDavid S. Miller char __user *optval, unsigned int optlen) 32753fdadf7dSDmitry Mishin { 3276dec73ff0SArnaldo Carvalho de Melo if (level != SOL_TCP) 3277dec73ff0SArnaldo Carvalho de Melo return inet_csk_compat_setsockopt(sk, level, optname, 3278dec73ff0SArnaldo Carvalho de Melo optval, optlen); 32793fdadf7dSDmitry Mishin return do_tcp_setsockopt(sk, level, optname, optval, optlen); 32803fdadf7dSDmitry Mishin } 3281543d9cfeSArnaldo Carvalho de Melo EXPORT_SYMBOL(compat_tcp_setsockopt); 32823fdadf7dSDmitry Mishin #endif 32833fdadf7dSDmitry Mishin 3284efd90174SFrancis Yan static void tcp_get_info_chrono_stats(const struct tcp_sock *tp, 3285efd90174SFrancis Yan struct tcp_info *info) 3286efd90174SFrancis Yan { 3287efd90174SFrancis Yan u64 stats[__TCP_CHRONO_MAX], total = 0; 3288efd90174SFrancis Yan enum tcp_chrono i; 3289efd90174SFrancis Yan 3290efd90174SFrancis Yan for (i = TCP_CHRONO_BUSY; i < __TCP_CHRONO_MAX; ++i) { 3291efd90174SFrancis Yan stats[i] = tp->chrono_stat[i - 1]; 3292efd90174SFrancis Yan if (i == tp->chrono_type) 3293628174ccSEric Dumazet stats[i] += tcp_jiffies32 - tp->chrono_start; 3294efd90174SFrancis Yan stats[i] *= USEC_PER_SEC / HZ; 3295efd90174SFrancis Yan total += stats[i]; 3296efd90174SFrancis Yan } 3297efd90174SFrancis Yan 3298efd90174SFrancis Yan info->tcpi_busy_time = total; 3299efd90174SFrancis Yan info->tcpi_rwnd_limited = stats[TCP_CHRONO_RWND_LIMITED]; 3300efd90174SFrancis Yan info->tcpi_sndbuf_limited = stats[TCP_CHRONO_SNDBUF_LIMITED]; 3301efd90174SFrancis Yan } 3302efd90174SFrancis Yan 33031da177e4SLinus Torvalds /* Return information about state of tcp endpoint in API format. */ 33040df48c26SEric Dumazet void tcp_get_info(struct sock *sk, struct tcp_info *info) 33051da177e4SLinus Torvalds { 330635ac838aSCraig Gallek const struct tcp_sock *tp = tcp_sk(sk); /* iff sk_type == SOCK_STREAM */ 3307463c84b9SArnaldo Carvalho de Melo const struct inet_connection_sock *icsk = inet_csk(sk); 330876a9ebe8SEric Dumazet unsigned long rate; 33090263598cSWei Wang u32 now; 3310ff5d7497SEric Dumazet u64 rate64; 331167db3e4bSEric Dumazet bool slow; 33121da177e4SLinus Torvalds 33131da177e4SLinus Torvalds memset(info, 0, sizeof(*info)); 331435ac838aSCraig Gallek if (sk->sk_type != SOCK_STREAM) 331535ac838aSCraig Gallek return; 33161da177e4SLinus Torvalds 3317986ffdfdSYafang Shao info->tcpi_state = inet_sk_state_load(sk); 331800fd38d9SEric Dumazet 3319ccbf3bfaSEric Dumazet /* Report meaningful fields for all TCP states, including listeners */ 3320ccbf3bfaSEric Dumazet rate = READ_ONCE(sk->sk_pacing_rate); 332176a9ebe8SEric Dumazet rate64 = (rate != ~0UL) ? rate : ~0ULL; 3322f522a5fcSEric Dumazet info->tcpi_pacing_rate = rate64; 3323ccbf3bfaSEric Dumazet 3324ccbf3bfaSEric Dumazet rate = READ_ONCE(sk->sk_max_pacing_rate); 332576a9ebe8SEric Dumazet rate64 = (rate != ~0UL) ? rate : ~0ULL; 3326f522a5fcSEric Dumazet info->tcpi_max_pacing_rate = rate64; 3327ccbf3bfaSEric Dumazet 3328ccbf3bfaSEric Dumazet info->tcpi_reordering = tp->reordering; 3329ccbf3bfaSEric Dumazet info->tcpi_snd_cwnd = tp->snd_cwnd; 3330ccbf3bfaSEric Dumazet 3331ccbf3bfaSEric Dumazet if (info->tcpi_state == TCP_LISTEN) { 3332ccbf3bfaSEric Dumazet /* listeners aliased fields : 3333ccbf3bfaSEric Dumazet * tcpi_unacked -> Number of children ready for accept() 3334ccbf3bfaSEric Dumazet * tcpi_sacked -> max backlog 3335ccbf3bfaSEric Dumazet */ 3336288efe86SEric Dumazet info->tcpi_unacked = READ_ONCE(sk->sk_ack_backlog); 3337099ecf59SEric Dumazet info->tcpi_sacked = READ_ONCE(sk->sk_max_ack_backlog); 3338ccbf3bfaSEric Dumazet return; 3339ccbf3bfaSEric Dumazet } 3340b369e7fdSEric Dumazet 3341b369e7fdSEric Dumazet slow = lock_sock_fast(sk); 3342b369e7fdSEric Dumazet 33436687e988SArnaldo Carvalho de Melo info->tcpi_ca_state = icsk->icsk_ca_state; 3344463c84b9SArnaldo Carvalho de Melo info->tcpi_retransmits = icsk->icsk_retransmits; 33456687e988SArnaldo Carvalho de Melo info->tcpi_probes = icsk->icsk_probes_out; 3346463c84b9SArnaldo Carvalho de Melo info->tcpi_backoff = icsk->icsk_backoff; 33471da177e4SLinus Torvalds 33481da177e4SLinus Torvalds if (tp->rx_opt.tstamp_ok) 33491da177e4SLinus Torvalds info->tcpi_options |= TCPI_OPT_TIMESTAMPS; 3350e60402d0SIlpo Järvinen if (tcp_is_sack(tp)) 33511da177e4SLinus Torvalds info->tcpi_options |= TCPI_OPT_SACK; 33521da177e4SLinus Torvalds if (tp->rx_opt.wscale_ok) { 33531da177e4SLinus Torvalds info->tcpi_options |= TCPI_OPT_WSCALE; 33541da177e4SLinus Torvalds info->tcpi_snd_wscale = tp->rx_opt.snd_wscale; 33551da177e4SLinus Torvalds info->tcpi_rcv_wscale = tp->rx_opt.rcv_wscale; 33561da177e4SLinus Torvalds } 33571da177e4SLinus Torvalds 33581da177e4SLinus Torvalds if (tp->ecn_flags & TCP_ECN_OK) 33591da177e4SLinus Torvalds info->tcpi_options |= TCPI_OPT_ECN; 3360b5c5693bSEric Dumazet if (tp->ecn_flags & TCP_ECN_SEEN) 3361b5c5693bSEric Dumazet info->tcpi_options |= TCPI_OPT_ECN_SEEN; 33626f73601eSYuchung Cheng if (tp->syn_data_acked) 33636f73601eSYuchung Cheng info->tcpi_options |= TCPI_OPT_SYN_DATA; 33641da177e4SLinus Torvalds 3365463c84b9SArnaldo Carvalho de Melo info->tcpi_rto = jiffies_to_usecs(icsk->icsk_rto); 3366463c84b9SArnaldo Carvalho de Melo info->tcpi_ato = jiffies_to_usecs(icsk->icsk_ack.ato); 3367c1b4a7e6SDavid S. Miller info->tcpi_snd_mss = tp->mss_cache; 3368463c84b9SArnaldo Carvalho de Melo info->tcpi_rcv_mss = icsk->icsk_ack.rcv_mss; 33691da177e4SLinus Torvalds 33701da177e4SLinus Torvalds info->tcpi_unacked = tp->packets_out; 33711da177e4SLinus Torvalds info->tcpi_sacked = tp->sacked_out; 3372ccbf3bfaSEric Dumazet 33731da177e4SLinus Torvalds info->tcpi_lost = tp->lost_out; 33741da177e4SLinus Torvalds info->tcpi_retrans = tp->retrans_out; 33751da177e4SLinus Torvalds 3376d635fbe2SEric Dumazet now = tcp_jiffies32; 33771da177e4SLinus Torvalds info->tcpi_last_data_sent = jiffies_to_msecs(now - tp->lsndtime); 3378463c84b9SArnaldo Carvalho de Melo info->tcpi_last_data_recv = jiffies_to_msecs(now - icsk->icsk_ack.lrcvtime); 33791da177e4SLinus Torvalds info->tcpi_last_ack_recv = jiffies_to_msecs(now - tp->rcv_tstamp); 33801da177e4SLinus Torvalds 3381d83d8461SArnaldo Carvalho de Melo info->tcpi_pmtu = icsk->icsk_pmtu_cookie; 33821da177e4SLinus Torvalds info->tcpi_rcv_ssthresh = tp->rcv_ssthresh; 3383740b0f18SEric Dumazet info->tcpi_rtt = tp->srtt_us >> 3; 3384740b0f18SEric Dumazet info->tcpi_rttvar = tp->mdev_us >> 2; 33851da177e4SLinus Torvalds info->tcpi_snd_ssthresh = tp->snd_ssthresh; 33861da177e4SLinus Torvalds info->tcpi_advmss = tp->advmss; 33871da177e4SLinus Torvalds 3388645f4c6fSEric Dumazet info->tcpi_rcv_rtt = tp->rcv_rtt_est.rtt_us >> 3; 33891da177e4SLinus Torvalds info->tcpi_rcv_space = tp->rcvq_space.space; 33901da177e4SLinus Torvalds 33911da177e4SLinus Torvalds info->tcpi_total_retrans = tp->total_retrans; 3392977cb0ecSEric Dumazet 3393f522a5fcSEric Dumazet info->tcpi_bytes_acked = tp->bytes_acked; 3394f522a5fcSEric Dumazet info->tcpi_bytes_received = tp->bytes_received; 339567db3e4bSEric Dumazet info->tcpi_notsent_bytes = max_t(int, 0, tp->write_seq - tp->snd_nxt); 3396efd90174SFrancis Yan tcp_get_info_chrono_stats(tp, info); 339767db3e4bSEric Dumazet 33982efd055cSMarcelo Ricardo Leitner info->tcpi_segs_out = tp->segs_out; 33992efd055cSMarcelo Ricardo Leitner info->tcpi_segs_in = tp->segs_in; 3400cd9b2660SEric Dumazet 3401cd9b2660SEric Dumazet info->tcpi_min_rtt = tcp_min_rtt(tp); 3402a44d6eacSMartin KaFai Lau info->tcpi_data_segs_in = tp->data_segs_in; 3403a44d6eacSMartin KaFai Lau info->tcpi_data_segs_out = tp->data_segs_out; 3404eb8329e0SYuchung Cheng 3405eb8329e0SYuchung Cheng info->tcpi_delivery_rate_app_limited = tp->rate_app_limited ? 1 : 0; 34060263598cSWei Wang rate64 = tcp_compute_delivery_rate(tp); 34070263598cSWei Wang if (rate64) 3408f522a5fcSEric Dumazet info->tcpi_delivery_rate = rate64; 3409feb5f2ecSYuchung Cheng info->tcpi_delivered = tp->delivered; 3410feb5f2ecSYuchung Cheng info->tcpi_delivered_ce = tp->delivered_ce; 3411ba113c3aSWei Wang info->tcpi_bytes_sent = tp->bytes_sent; 3412fb31c9b9SWei Wang info->tcpi_bytes_retrans = tp->bytes_retrans; 34137e10b655SWei Wang info->tcpi_dsack_dups = tp->dsack_dups; 34147ec65372SWei Wang info->tcpi_reord_seen = tp->reord_seen; 3415f9af2dbbSThomas Higdon info->tcpi_rcv_ooopack = tp->rcv_ooopack; 34168f7baad7SThomas Higdon info->tcpi_snd_wnd = tp->snd_wnd; 341748027478SJason Baron info->tcpi_fastopen_client_fail = tp->fastopen_client_fail; 3418b369e7fdSEric Dumazet unlock_sock_fast(sk, slow); 34191da177e4SLinus Torvalds } 34201da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(tcp_get_info); 34211da177e4SLinus Torvalds 3422984988aaSWei Wang static size_t tcp_opt_stats_get_size(void) 3423984988aaSWei Wang { 3424984988aaSWei Wang return 3425984988aaSWei Wang nla_total_size_64bit(sizeof(u64)) + /* TCP_NLA_BUSY */ 3426984988aaSWei Wang nla_total_size_64bit(sizeof(u64)) + /* TCP_NLA_RWND_LIMITED */ 3427984988aaSWei Wang nla_total_size_64bit(sizeof(u64)) + /* TCP_NLA_SNDBUF_LIMITED */ 3428984988aaSWei Wang nla_total_size_64bit(sizeof(u64)) + /* TCP_NLA_DATA_SEGS_OUT */ 3429984988aaSWei Wang nla_total_size_64bit(sizeof(u64)) + /* TCP_NLA_TOTAL_RETRANS */ 3430984988aaSWei Wang nla_total_size_64bit(sizeof(u64)) + /* TCP_NLA_PACING_RATE */ 3431984988aaSWei Wang nla_total_size_64bit(sizeof(u64)) + /* TCP_NLA_DELIVERY_RATE */ 3432984988aaSWei Wang nla_total_size(sizeof(u32)) + /* TCP_NLA_SND_CWND */ 3433984988aaSWei Wang nla_total_size(sizeof(u32)) + /* TCP_NLA_REORDERING */ 3434984988aaSWei Wang nla_total_size(sizeof(u32)) + /* TCP_NLA_MIN_RTT */ 3435984988aaSWei Wang nla_total_size(sizeof(u8)) + /* TCP_NLA_RECUR_RETRANS */ 3436984988aaSWei Wang nla_total_size(sizeof(u8)) + /* TCP_NLA_DELIVERY_RATE_APP_LMT */ 3437984988aaSWei Wang nla_total_size(sizeof(u32)) + /* TCP_NLA_SNDQ_SIZE */ 3438984988aaSWei Wang nla_total_size(sizeof(u8)) + /* TCP_NLA_CA_STATE */ 3439984988aaSWei Wang nla_total_size(sizeof(u32)) + /* TCP_NLA_SND_SSTHRESH */ 3440984988aaSWei Wang nla_total_size(sizeof(u32)) + /* TCP_NLA_DELIVERED */ 3441984988aaSWei Wang nla_total_size(sizeof(u32)) + /* TCP_NLA_DELIVERED_CE */ 3442ba113c3aSWei Wang nla_total_size_64bit(sizeof(u64)) + /* TCP_NLA_BYTES_SENT */ 3443fb31c9b9SWei Wang nla_total_size_64bit(sizeof(u64)) + /* TCP_NLA_BYTES_RETRANS */ 34447e10b655SWei Wang nla_total_size(sizeof(u32)) + /* TCP_NLA_DSACK_DUPS */ 34457ec65372SWei Wang nla_total_size(sizeof(u32)) + /* TCP_NLA_REORD_SEEN */ 3446e8bd8fcaSYousuk Seung nla_total_size(sizeof(u32)) + /* TCP_NLA_SRTT */ 344732efcc06SAbdul Kabbani nla_total_size(sizeof(u16)) + /* TCP_NLA_TIMEOUT_REHASH */ 3448e08ab0b3SYousuk Seung nla_total_size(sizeof(u32)) + /* TCP_NLA_BYTES_NOTSENT */ 3449984988aaSWei Wang 0; 3450984988aaSWei Wang } 3451984988aaSWei Wang 34521c885808SFrancis Yan struct sk_buff *tcp_get_timestamping_opt_stats(const struct sock *sk) 34531c885808SFrancis Yan { 34541c885808SFrancis Yan const struct tcp_sock *tp = tcp_sk(sk); 34551c885808SFrancis Yan struct sk_buff *stats; 34561c885808SFrancis Yan struct tcp_info info; 345776a9ebe8SEric Dumazet unsigned long rate; 3458bb7c19f9SWei Wang u64 rate64; 34591c885808SFrancis Yan 3460984988aaSWei Wang stats = alloc_skb(tcp_opt_stats_get_size(), GFP_ATOMIC); 34611c885808SFrancis Yan if (!stats) 34621c885808SFrancis Yan return NULL; 34631c885808SFrancis Yan 34641c885808SFrancis Yan tcp_get_info_chrono_stats(tp, &info); 34651c885808SFrancis Yan nla_put_u64_64bit(stats, TCP_NLA_BUSY, 34661c885808SFrancis Yan info.tcpi_busy_time, TCP_NLA_PAD); 34671c885808SFrancis Yan nla_put_u64_64bit(stats, TCP_NLA_RWND_LIMITED, 34681c885808SFrancis Yan info.tcpi_rwnd_limited, TCP_NLA_PAD); 34691c885808SFrancis Yan nla_put_u64_64bit(stats, TCP_NLA_SNDBUF_LIMITED, 34701c885808SFrancis Yan info.tcpi_sndbuf_limited, TCP_NLA_PAD); 34717e98102fSYuchung Cheng nla_put_u64_64bit(stats, TCP_NLA_DATA_SEGS_OUT, 34727e98102fSYuchung Cheng tp->data_segs_out, TCP_NLA_PAD); 34737e98102fSYuchung Cheng nla_put_u64_64bit(stats, TCP_NLA_TOTAL_RETRANS, 34747e98102fSYuchung Cheng tp->total_retrans, TCP_NLA_PAD); 3475bb7c19f9SWei Wang 3476bb7c19f9SWei Wang rate = READ_ONCE(sk->sk_pacing_rate); 347776a9ebe8SEric Dumazet rate64 = (rate != ~0UL) ? rate : ~0ULL; 3478bb7c19f9SWei Wang nla_put_u64_64bit(stats, TCP_NLA_PACING_RATE, rate64, TCP_NLA_PAD); 3479bb7c19f9SWei Wang 3480bb7c19f9SWei Wang rate64 = tcp_compute_delivery_rate(tp); 3481bb7c19f9SWei Wang nla_put_u64_64bit(stats, TCP_NLA_DELIVERY_RATE, rate64, TCP_NLA_PAD); 3482bb7c19f9SWei Wang 3483bb7c19f9SWei Wang nla_put_u32(stats, TCP_NLA_SND_CWND, tp->snd_cwnd); 3484bb7c19f9SWei Wang nla_put_u32(stats, TCP_NLA_REORDERING, tp->reordering); 3485bb7c19f9SWei Wang nla_put_u32(stats, TCP_NLA_MIN_RTT, tcp_min_rtt(tp)); 3486bb7c19f9SWei Wang 3487bb7c19f9SWei Wang nla_put_u8(stats, TCP_NLA_RECUR_RETRANS, inet_csk(sk)->icsk_retransmits); 3488bb7c19f9SWei Wang nla_put_u8(stats, TCP_NLA_DELIVERY_RATE_APP_LMT, !!tp->rate_app_limited); 34897156d194SYousuk Seung nla_put_u32(stats, TCP_NLA_SND_SSTHRESH, tp->snd_ssthresh); 3490feb5f2ecSYuchung Cheng nla_put_u32(stats, TCP_NLA_DELIVERED, tp->delivered); 3491feb5f2ecSYuchung Cheng nla_put_u32(stats, TCP_NLA_DELIVERED_CE, tp->delivered_ce); 349287ecc95dSPriyaranjan Jha 349387ecc95dSPriyaranjan Jha nla_put_u32(stats, TCP_NLA_SNDQ_SIZE, tp->write_seq - tp->snd_una); 3494be631892SPriyaranjan Jha nla_put_u8(stats, TCP_NLA_CA_STATE, inet_csk(sk)->icsk_ca_state); 3495feb5f2ecSYuchung Cheng 3496ba113c3aSWei Wang nla_put_u64_64bit(stats, TCP_NLA_BYTES_SENT, tp->bytes_sent, 3497ba113c3aSWei Wang TCP_NLA_PAD); 3498fb31c9b9SWei Wang nla_put_u64_64bit(stats, TCP_NLA_BYTES_RETRANS, tp->bytes_retrans, 3499fb31c9b9SWei Wang TCP_NLA_PAD); 35007e10b655SWei Wang nla_put_u32(stats, TCP_NLA_DSACK_DUPS, tp->dsack_dups); 35017ec65372SWei Wang nla_put_u32(stats, TCP_NLA_REORD_SEEN, tp->reord_seen); 3502e8bd8fcaSYousuk Seung nla_put_u32(stats, TCP_NLA_SRTT, tp->srtt_us >> 3); 350332efcc06SAbdul Kabbani nla_put_u16(stats, TCP_NLA_TIMEOUT_REHASH, tp->timeout_rehash); 3504e08ab0b3SYousuk Seung nla_put_u32(stats, TCP_NLA_BYTES_NOTSENT, 3505e08ab0b3SYousuk Seung max_t(int, 0, tp->write_seq - tp->snd_nxt)); 3506ba113c3aSWei Wang 35071c885808SFrancis Yan return stats; 35081c885808SFrancis Yan } 35091c885808SFrancis Yan 35103fdadf7dSDmitry Mishin static int do_tcp_getsockopt(struct sock *sk, int level, 35113fdadf7dSDmitry Mishin int optname, char __user *optval, int __user *optlen) 35121da177e4SLinus Torvalds { 3513295f7324SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 35141da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 35156fa25166SNikolay Borisov struct net *net = sock_net(sk); 35161da177e4SLinus Torvalds int val, len; 35171da177e4SLinus Torvalds 35181da177e4SLinus Torvalds if (get_user(len, optlen)) 35191da177e4SLinus Torvalds return -EFAULT; 35201da177e4SLinus Torvalds 35211da177e4SLinus Torvalds len = min_t(unsigned int, len, sizeof(int)); 35221da177e4SLinus Torvalds 35231da177e4SLinus Torvalds if (len < 0) 35241da177e4SLinus Torvalds return -EINVAL; 35251da177e4SLinus Torvalds 35261da177e4SLinus Torvalds switch (optname) { 35271da177e4SLinus Torvalds case TCP_MAXSEG: 3528c1b4a7e6SDavid S. Miller val = tp->mss_cache; 35291da177e4SLinus Torvalds if (!val && ((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN))) 35301da177e4SLinus Torvalds val = tp->rx_opt.user_mss; 35315e6a3ce6SPavel Emelyanov if (tp->repair) 35325e6a3ce6SPavel Emelyanov val = tp->rx_opt.mss_clamp; 35331da177e4SLinus Torvalds break; 35341da177e4SLinus Torvalds case TCP_NODELAY: 35351da177e4SLinus Torvalds val = !!(tp->nonagle&TCP_NAGLE_OFF); 35361da177e4SLinus Torvalds break; 35371da177e4SLinus Torvalds case TCP_CORK: 35381da177e4SLinus Torvalds val = !!(tp->nonagle&TCP_NAGLE_CORK); 35391da177e4SLinus Torvalds break; 35401da177e4SLinus Torvalds case TCP_KEEPIDLE: 3541df19a626SEric Dumazet val = keepalive_time_when(tp) / HZ; 35421da177e4SLinus Torvalds break; 35431da177e4SLinus Torvalds case TCP_KEEPINTVL: 3544df19a626SEric Dumazet val = keepalive_intvl_when(tp) / HZ; 35451da177e4SLinus Torvalds break; 35461da177e4SLinus Torvalds case TCP_KEEPCNT: 3547df19a626SEric Dumazet val = keepalive_probes(tp); 35481da177e4SLinus Torvalds break; 35491da177e4SLinus Torvalds case TCP_SYNCNT: 35506fa25166SNikolay Borisov val = icsk->icsk_syn_retries ? : net->ipv4.sysctl_tcp_syn_retries; 35511da177e4SLinus Torvalds break; 35521da177e4SLinus Torvalds case TCP_LINGER2: 35531da177e4SLinus Torvalds val = tp->linger2; 35541da177e4SLinus Torvalds if (val >= 0) 35551e579caaSNikolay Borisov val = (val ? : net->ipv4.sysctl_tcp_fin_timeout) / HZ; 35561da177e4SLinus Torvalds break; 35571da177e4SLinus Torvalds case TCP_DEFER_ACCEPT: 3558b103cf34SJulian Anastasov val = retrans_to_secs(icsk->icsk_accept_queue.rskq_defer_accept, 3559b103cf34SJulian Anastasov TCP_TIMEOUT_INIT / HZ, TCP_RTO_MAX / HZ); 35601da177e4SLinus Torvalds break; 35611da177e4SLinus Torvalds case TCP_WINDOW_CLAMP: 35621da177e4SLinus Torvalds val = tp->window_clamp; 35631da177e4SLinus Torvalds break; 35641da177e4SLinus Torvalds case TCP_INFO: { 35651da177e4SLinus Torvalds struct tcp_info info; 35661da177e4SLinus Torvalds 35671da177e4SLinus Torvalds if (get_user(len, optlen)) 35681da177e4SLinus Torvalds return -EFAULT; 35691da177e4SLinus Torvalds 35701da177e4SLinus Torvalds tcp_get_info(sk, &info); 35711da177e4SLinus Torvalds 35721da177e4SLinus Torvalds len = min_t(unsigned int, len, sizeof(info)); 35731da177e4SLinus Torvalds if (put_user(len, optlen)) 35741da177e4SLinus Torvalds return -EFAULT; 35751da177e4SLinus Torvalds if (copy_to_user(optval, &info, len)) 35761da177e4SLinus Torvalds return -EFAULT; 35771da177e4SLinus Torvalds return 0; 35781da177e4SLinus Torvalds } 35796e9250f5SEric Dumazet case TCP_CC_INFO: { 35806e9250f5SEric Dumazet const struct tcp_congestion_ops *ca_ops; 35816e9250f5SEric Dumazet union tcp_cc_info info; 35826e9250f5SEric Dumazet size_t sz = 0; 35836e9250f5SEric Dumazet int attr; 35846e9250f5SEric Dumazet 35856e9250f5SEric Dumazet if (get_user(len, optlen)) 35866e9250f5SEric Dumazet return -EFAULT; 35876e9250f5SEric Dumazet 35886e9250f5SEric Dumazet ca_ops = icsk->icsk_ca_ops; 35896e9250f5SEric Dumazet if (ca_ops && ca_ops->get_info) 35906e9250f5SEric Dumazet sz = ca_ops->get_info(sk, ~0U, &attr, &info); 35916e9250f5SEric Dumazet 35926e9250f5SEric Dumazet len = min_t(unsigned int, len, sz); 35936e9250f5SEric Dumazet if (put_user(len, optlen)) 35946e9250f5SEric Dumazet return -EFAULT; 35956e9250f5SEric Dumazet if (copy_to_user(optval, &info, len)) 35966e9250f5SEric Dumazet return -EFAULT; 35976e9250f5SEric Dumazet return 0; 35986e9250f5SEric Dumazet } 35991da177e4SLinus Torvalds case TCP_QUICKACK: 360031954cd8SWei Wang val = !inet_csk_in_pingpong_mode(sk); 36011da177e4SLinus Torvalds break; 36025f8ef48dSStephen Hemminger 36035f8ef48dSStephen Hemminger case TCP_CONGESTION: 36045f8ef48dSStephen Hemminger if (get_user(len, optlen)) 36055f8ef48dSStephen Hemminger return -EFAULT; 36065f8ef48dSStephen Hemminger len = min_t(unsigned int, len, TCP_CA_NAME_MAX); 36075f8ef48dSStephen Hemminger if (put_user(len, optlen)) 36085f8ef48dSStephen Hemminger return -EFAULT; 36096687e988SArnaldo Carvalho de Melo if (copy_to_user(optval, icsk->icsk_ca_ops->name, len)) 36105f8ef48dSStephen Hemminger return -EFAULT; 36115f8ef48dSStephen Hemminger return 0; 3612e56fb50fSWilliam Allen Simpson 3613734942ccSDave Watson case TCP_ULP: 3614734942ccSDave Watson if (get_user(len, optlen)) 3615734942ccSDave Watson return -EFAULT; 3616734942ccSDave Watson len = min_t(unsigned int, len, TCP_ULP_NAME_MAX); 3617d97af30fSDave Watson if (!icsk->icsk_ulp_ops) { 3618d97af30fSDave Watson if (put_user(0, optlen)) 3619d97af30fSDave Watson return -EFAULT; 3620d97af30fSDave Watson return 0; 3621d97af30fSDave Watson } 3622734942ccSDave Watson if (put_user(len, optlen)) 3623734942ccSDave Watson return -EFAULT; 3624734942ccSDave Watson if (copy_to_user(optval, icsk->icsk_ulp_ops->name, len)) 3625734942ccSDave Watson return -EFAULT; 3626734942ccSDave Watson return 0; 3627734942ccSDave Watson 36281fba70e5SYuchung Cheng case TCP_FASTOPEN_KEY: { 36290f1ce023SJason Baron __u8 key[TCP_FASTOPEN_KEY_BUF_LENGTH]; 36301fba70e5SYuchung Cheng struct tcp_fastopen_context *ctx; 36310f1ce023SJason Baron unsigned int key_len = 0; 36321fba70e5SYuchung Cheng 36331fba70e5SYuchung Cheng if (get_user(len, optlen)) 36341fba70e5SYuchung Cheng return -EFAULT; 36351fba70e5SYuchung Cheng 36361fba70e5SYuchung Cheng rcu_read_lock(); 36371fba70e5SYuchung Cheng ctx = rcu_dereference(icsk->icsk_accept_queue.fastopenq.ctx); 36380f1ce023SJason Baron if (ctx) { 36390f1ce023SJason Baron key_len = tcp_fastopen_context_len(ctx) * 36400f1ce023SJason Baron TCP_FASTOPEN_KEY_LENGTH; 36410f1ce023SJason Baron memcpy(&key[0], &ctx->key[0], key_len); 36420f1ce023SJason Baron } 36431fba70e5SYuchung Cheng rcu_read_unlock(); 36441fba70e5SYuchung Cheng 36450f1ce023SJason Baron len = min_t(unsigned int, len, key_len); 36461fba70e5SYuchung Cheng if (put_user(len, optlen)) 36471fba70e5SYuchung Cheng return -EFAULT; 36481fba70e5SYuchung Cheng if (copy_to_user(optval, key, len)) 36491fba70e5SYuchung Cheng return -EFAULT; 36501fba70e5SYuchung Cheng return 0; 36511fba70e5SYuchung Cheng } 36523c0fef0bSJosh Hunt case TCP_THIN_LINEAR_TIMEOUTS: 36533c0fef0bSJosh Hunt val = tp->thin_lto; 36543c0fef0bSJosh Hunt break; 36554a7f6009SYuchung Cheng 36563c0fef0bSJosh Hunt case TCP_THIN_DUPACK: 36574a7f6009SYuchung Cheng val = 0; 36583c0fef0bSJosh Hunt break; 3659dca43c75SJerry Chu 3660ee995283SPavel Emelyanov case TCP_REPAIR: 3661ee995283SPavel Emelyanov val = tp->repair; 3662ee995283SPavel Emelyanov break; 3663ee995283SPavel Emelyanov 3664ee995283SPavel Emelyanov case TCP_REPAIR_QUEUE: 3665ee995283SPavel Emelyanov if (tp->repair) 3666ee995283SPavel Emelyanov val = tp->repair_queue; 3667ee995283SPavel Emelyanov else 3668ee995283SPavel Emelyanov return -EINVAL; 3669ee995283SPavel Emelyanov break; 3670ee995283SPavel Emelyanov 3671b1ed4c4fSAndrey Vagin case TCP_REPAIR_WINDOW: { 3672b1ed4c4fSAndrey Vagin struct tcp_repair_window opt; 3673b1ed4c4fSAndrey Vagin 3674b1ed4c4fSAndrey Vagin if (get_user(len, optlen)) 3675b1ed4c4fSAndrey Vagin return -EFAULT; 3676b1ed4c4fSAndrey Vagin 3677b1ed4c4fSAndrey Vagin if (len != sizeof(opt)) 3678b1ed4c4fSAndrey Vagin return -EINVAL; 3679b1ed4c4fSAndrey Vagin 3680b1ed4c4fSAndrey Vagin if (!tp->repair) 3681b1ed4c4fSAndrey Vagin return -EPERM; 3682b1ed4c4fSAndrey Vagin 3683b1ed4c4fSAndrey Vagin opt.snd_wl1 = tp->snd_wl1; 3684b1ed4c4fSAndrey Vagin opt.snd_wnd = tp->snd_wnd; 3685b1ed4c4fSAndrey Vagin opt.max_window = tp->max_window; 3686b1ed4c4fSAndrey Vagin opt.rcv_wnd = tp->rcv_wnd; 3687b1ed4c4fSAndrey Vagin opt.rcv_wup = tp->rcv_wup; 3688b1ed4c4fSAndrey Vagin 3689b1ed4c4fSAndrey Vagin if (copy_to_user(optval, &opt, len)) 3690b1ed4c4fSAndrey Vagin return -EFAULT; 3691b1ed4c4fSAndrey Vagin return 0; 3692b1ed4c4fSAndrey Vagin } 3693ee995283SPavel Emelyanov case TCP_QUEUE_SEQ: 3694ee995283SPavel Emelyanov if (tp->repair_queue == TCP_SEND_QUEUE) 3695ee995283SPavel Emelyanov val = tp->write_seq; 3696ee995283SPavel Emelyanov else if (tp->repair_queue == TCP_RECV_QUEUE) 3697ee995283SPavel Emelyanov val = tp->rcv_nxt; 3698ee995283SPavel Emelyanov else 3699ee995283SPavel Emelyanov return -EINVAL; 3700ee995283SPavel Emelyanov break; 3701ee995283SPavel Emelyanov 3702dca43c75SJerry Chu case TCP_USER_TIMEOUT: 37039bcc66e1SJon Maxwell val = icsk->icsk_user_timeout; 3704dca43c75SJerry Chu break; 37051536e285SKenjiro Nakayama 37061536e285SKenjiro Nakayama case TCP_FASTOPEN: 37070536fcc0SEric Dumazet val = icsk->icsk_accept_queue.fastopenq.max_qlen; 37081536e285SKenjiro Nakayama break; 37091536e285SKenjiro Nakayama 371019f6d3f3SWei Wang case TCP_FASTOPEN_CONNECT: 371119f6d3f3SWei Wang val = tp->fastopen_connect; 371219f6d3f3SWei Wang break; 371319f6d3f3SWei Wang 371471c02379SChristoph Paasch case TCP_FASTOPEN_NO_COOKIE: 371571c02379SChristoph Paasch val = tp->fastopen_no_cookie; 371671c02379SChristoph Paasch break; 371771c02379SChristoph Paasch 3718a842fe14SEric Dumazet case TCP_TX_DELAY: 3719a842fe14SEric Dumazet val = tp->tcp_tx_delay; 3720a842fe14SEric Dumazet break; 3721a842fe14SEric Dumazet 372293be6ce0SAndrey Vagin case TCP_TIMESTAMP: 37239a568de4SEric Dumazet val = tcp_time_stamp_raw() + tp->tsoffset; 372493be6ce0SAndrey Vagin break; 3725c9bee3b7SEric Dumazet case TCP_NOTSENT_LOWAT: 3726c9bee3b7SEric Dumazet val = tp->notsent_lowat; 3727c9bee3b7SEric Dumazet break; 3728b75eba76SSoheil Hassas Yeganeh case TCP_INQ: 3729b75eba76SSoheil Hassas Yeganeh val = tp->recvmsg_inq; 3730b75eba76SSoheil Hassas Yeganeh break; 3731cd8ae852SEric Dumazet case TCP_SAVE_SYN: 3732cd8ae852SEric Dumazet val = tp->save_syn; 3733cd8ae852SEric Dumazet break; 3734cd8ae852SEric Dumazet case TCP_SAVED_SYN: { 3735cd8ae852SEric Dumazet if (get_user(len, optlen)) 3736cd8ae852SEric Dumazet return -EFAULT; 3737cd8ae852SEric Dumazet 3738cd8ae852SEric Dumazet lock_sock(sk); 3739cd8ae852SEric Dumazet if (tp->saved_syn) { 3740aea0929eSEric B Munson if (len < tp->saved_syn[0]) { 3741aea0929eSEric B Munson if (put_user(tp->saved_syn[0], optlen)) { 3742aea0929eSEric B Munson release_sock(sk); 3743aea0929eSEric B Munson return -EFAULT; 3744aea0929eSEric B Munson } 3745aea0929eSEric B Munson release_sock(sk); 3746aea0929eSEric B Munson return -EINVAL; 3747aea0929eSEric B Munson } 3748aea0929eSEric B Munson len = tp->saved_syn[0]; 3749cd8ae852SEric Dumazet if (put_user(len, optlen)) { 3750cd8ae852SEric Dumazet release_sock(sk); 3751cd8ae852SEric Dumazet return -EFAULT; 3752cd8ae852SEric Dumazet } 3753cd8ae852SEric Dumazet if (copy_to_user(optval, tp->saved_syn + 1, len)) { 3754cd8ae852SEric Dumazet release_sock(sk); 3755cd8ae852SEric Dumazet return -EFAULT; 3756cd8ae852SEric Dumazet } 3757cd8ae852SEric Dumazet tcp_saved_syn_free(tp); 3758cd8ae852SEric Dumazet release_sock(sk); 3759cd8ae852SEric Dumazet } else { 3760cd8ae852SEric Dumazet release_sock(sk); 3761cd8ae852SEric Dumazet len = 0; 3762cd8ae852SEric Dumazet if (put_user(len, optlen)) 3763cd8ae852SEric Dumazet return -EFAULT; 3764cd8ae852SEric Dumazet } 3765cd8ae852SEric Dumazet return 0; 3766cd8ae852SEric Dumazet } 376705255b82SEric Dumazet #ifdef CONFIG_MMU 376805255b82SEric Dumazet case TCP_ZEROCOPY_RECEIVE: { 376905255b82SEric Dumazet struct tcp_zerocopy_receive zc; 377005255b82SEric Dumazet int err; 377105255b82SEric Dumazet 377205255b82SEric Dumazet if (get_user(len, optlen)) 377305255b82SEric Dumazet return -EFAULT; 3774c8856c05SArjun Roy if (len < offsetofend(struct tcp_zerocopy_receive, length)) 377505255b82SEric Dumazet return -EINVAL; 37760b7f41f6SArjun Roy if (len > sizeof(zc)) { 3777c8856c05SArjun Roy len = sizeof(zc); 37780b7f41f6SArjun Roy if (put_user(len, optlen)) 37790b7f41f6SArjun Roy return -EFAULT; 37800b7f41f6SArjun Roy } 378105255b82SEric Dumazet if (copy_from_user(&zc, optval, len)) 378205255b82SEric Dumazet return -EFAULT; 378305255b82SEric Dumazet lock_sock(sk); 378405255b82SEric Dumazet err = tcp_zerocopy_receive(sk, &zc); 378505255b82SEric Dumazet release_sock(sk); 378633946518SArjun Roy if (len == sizeof(zc)) 378733946518SArjun Roy goto zerocopy_rcv_sk_err; 3788c8856c05SArjun Roy switch (len) { 378933946518SArjun Roy case offsetofend(struct tcp_zerocopy_receive, err): 379033946518SArjun Roy goto zerocopy_rcv_sk_err; 3791c8856c05SArjun Roy case offsetofend(struct tcp_zerocopy_receive, inq): 3792c8856c05SArjun Roy goto zerocopy_rcv_inq; 3793c8856c05SArjun Roy case offsetofend(struct tcp_zerocopy_receive, length): 3794c8856c05SArjun Roy default: 3795c8856c05SArjun Roy goto zerocopy_rcv_out; 3796c8856c05SArjun Roy } 379733946518SArjun Roy zerocopy_rcv_sk_err: 379833946518SArjun Roy if (!err) 379933946518SArjun Roy zc.err = sock_error(sk); 3800c8856c05SArjun Roy zerocopy_rcv_inq: 3801c8856c05SArjun Roy zc.inq = tcp_inq_hint(sk); 3802c8856c05SArjun Roy zerocopy_rcv_out: 380305255b82SEric Dumazet if (!err && copy_to_user(optval, &zc, len)) 380405255b82SEric Dumazet err = -EFAULT; 380505255b82SEric Dumazet return err; 380605255b82SEric Dumazet } 380705255b82SEric Dumazet #endif 38081da177e4SLinus Torvalds default: 38091da177e4SLinus Torvalds return -ENOPROTOOPT; 38103ff50b79SStephen Hemminger } 38111da177e4SLinus Torvalds 38121da177e4SLinus Torvalds if (put_user(len, optlen)) 38131da177e4SLinus Torvalds return -EFAULT; 38141da177e4SLinus Torvalds if (copy_to_user(optval, &val, len)) 38151da177e4SLinus Torvalds return -EFAULT; 38161da177e4SLinus Torvalds return 0; 38171da177e4SLinus Torvalds } 38181da177e4SLinus Torvalds 38193fdadf7dSDmitry Mishin int tcp_getsockopt(struct sock *sk, int level, int optname, char __user *optval, 38203fdadf7dSDmitry Mishin int __user *optlen) 38213fdadf7dSDmitry Mishin { 38223fdadf7dSDmitry Mishin struct inet_connection_sock *icsk = inet_csk(sk); 38233fdadf7dSDmitry Mishin 38243fdadf7dSDmitry Mishin if (level != SOL_TCP) 38253fdadf7dSDmitry Mishin return icsk->icsk_af_ops->getsockopt(sk, level, optname, 38263fdadf7dSDmitry Mishin optval, optlen); 38273fdadf7dSDmitry Mishin return do_tcp_getsockopt(sk, level, optname, optval, optlen); 38283fdadf7dSDmitry Mishin } 38294bc2f18bSEric Dumazet EXPORT_SYMBOL(tcp_getsockopt); 38303fdadf7dSDmitry Mishin 38313fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT 3832543d9cfeSArnaldo Carvalho de Melo int compat_tcp_getsockopt(struct sock *sk, int level, int optname, 3833543d9cfeSArnaldo Carvalho de Melo char __user *optval, int __user *optlen) 38343fdadf7dSDmitry Mishin { 3835dec73ff0SArnaldo Carvalho de Melo if (level != SOL_TCP) 3836dec73ff0SArnaldo Carvalho de Melo return inet_csk_compat_getsockopt(sk, level, optname, 3837dec73ff0SArnaldo Carvalho de Melo optval, optlen); 38383fdadf7dSDmitry Mishin return do_tcp_getsockopt(sk, level, optname, optval, optlen); 38393fdadf7dSDmitry Mishin } 3840543d9cfeSArnaldo Carvalho de Melo EXPORT_SYMBOL(compat_tcp_getsockopt); 38413fdadf7dSDmitry Mishin #endif 38421da177e4SLinus Torvalds 3843cfb6eeb4SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG 3844349ce993SEric Dumazet static DEFINE_PER_CPU(struct tcp_md5sig_pool, tcp_md5sig_pool); 384571cea17eSEric Dumazet static DEFINE_MUTEX(tcp_md5sig_mutex); 3846349ce993SEric Dumazet static bool tcp_md5sig_pool_populated = false; 3847cfb6eeb4SYOSHIFUJI Hideaki 384871cea17eSEric Dumazet static void __tcp_alloc_md5sig_pool(void) 3849cfb6eeb4SYOSHIFUJI Hideaki { 3850cf80e0e4SHerbert Xu struct crypto_ahash *hash; 3851cfb6eeb4SYOSHIFUJI Hideaki int cpu; 3852cfb6eeb4SYOSHIFUJI Hideaki 3853cf80e0e4SHerbert Xu hash = crypto_alloc_ahash("md5", 0, CRYPTO_ALG_ASYNC); 38541eea84b7SInsu Yun if (IS_ERR(hash)) 3855349ce993SEric Dumazet return; 3856cf80e0e4SHerbert Xu 3857cf80e0e4SHerbert Xu for_each_possible_cpu(cpu) { 385819689e38SEric Dumazet void *scratch = per_cpu(tcp_md5sig_pool, cpu).scratch; 3859cf80e0e4SHerbert Xu struct ahash_request *req; 3860cf80e0e4SHerbert Xu 386119689e38SEric Dumazet if (!scratch) { 386219689e38SEric Dumazet scratch = kmalloc_node(sizeof(union tcp_md5sum_block) + 386319689e38SEric Dumazet sizeof(struct tcphdr), 386419689e38SEric Dumazet GFP_KERNEL, 386519689e38SEric Dumazet cpu_to_node(cpu)); 386619689e38SEric Dumazet if (!scratch) 386719689e38SEric Dumazet return; 386819689e38SEric Dumazet per_cpu(tcp_md5sig_pool, cpu).scratch = scratch; 386919689e38SEric Dumazet } 3870cf80e0e4SHerbert Xu if (per_cpu(tcp_md5sig_pool, cpu).md5_req) 3871cf80e0e4SHerbert Xu continue; 3872cf80e0e4SHerbert Xu 3873cf80e0e4SHerbert Xu req = ahash_request_alloc(hash, GFP_KERNEL); 3874cf80e0e4SHerbert Xu if (!req) 3875cf80e0e4SHerbert Xu return; 3876cf80e0e4SHerbert Xu 3877cf80e0e4SHerbert Xu ahash_request_set_callback(req, 0, NULL, NULL); 3878cf80e0e4SHerbert Xu 3879cf80e0e4SHerbert Xu per_cpu(tcp_md5sig_pool, cpu).md5_req = req; 3880349ce993SEric Dumazet } 3881349ce993SEric Dumazet /* before setting tcp_md5sig_pool_populated, we must commit all writes 3882349ce993SEric Dumazet * to memory. See smp_rmb() in tcp_get_md5sig_pool() 388371cea17eSEric Dumazet */ 388471cea17eSEric Dumazet smp_wmb(); 3885349ce993SEric Dumazet tcp_md5sig_pool_populated = true; 3886cfb6eeb4SYOSHIFUJI Hideaki } 3887cfb6eeb4SYOSHIFUJI Hideaki 388871cea17eSEric Dumazet bool tcp_alloc_md5sig_pool(void) 3889cfb6eeb4SYOSHIFUJI Hideaki { 3890349ce993SEric Dumazet if (unlikely(!tcp_md5sig_pool_populated)) { 389171cea17eSEric Dumazet mutex_lock(&tcp_md5sig_mutex); 3892cfb6eeb4SYOSHIFUJI Hideaki 38936015c71eSEric Dumazet if (!tcp_md5sig_pool_populated) { 389471cea17eSEric Dumazet __tcp_alloc_md5sig_pool(); 38956015c71eSEric Dumazet if (tcp_md5sig_pool_populated) 3896921f9a0fSEric Dumazet static_branch_inc(&tcp_md5_needed); 38976015c71eSEric Dumazet } 3898cfb6eeb4SYOSHIFUJI Hideaki 389971cea17eSEric Dumazet mutex_unlock(&tcp_md5sig_mutex); 3900cfb6eeb4SYOSHIFUJI Hideaki } 3901349ce993SEric Dumazet return tcp_md5sig_pool_populated; 3902cfb6eeb4SYOSHIFUJI Hideaki } 3903cfb6eeb4SYOSHIFUJI Hideaki EXPORT_SYMBOL(tcp_alloc_md5sig_pool); 3904cfb6eeb4SYOSHIFUJI Hideaki 390535790c04SEric Dumazet 390635790c04SEric Dumazet /** 390735790c04SEric Dumazet * tcp_get_md5sig_pool - get md5sig_pool for this user 390835790c04SEric Dumazet * 390935790c04SEric Dumazet * We use percpu structure, so if we succeed, we exit with preemption 391035790c04SEric Dumazet * and BH disabled, to make sure another thread or softirq handling 391135790c04SEric Dumazet * wont try to get same context. 391235790c04SEric Dumazet */ 391335790c04SEric Dumazet struct tcp_md5sig_pool *tcp_get_md5sig_pool(void) 3914cfb6eeb4SYOSHIFUJI Hideaki { 391535790c04SEric Dumazet local_bh_disable(); 391635790c04SEric Dumazet 3917349ce993SEric Dumazet if (tcp_md5sig_pool_populated) { 3918349ce993SEric Dumazet /* coupled with smp_wmb() in __tcp_alloc_md5sig_pool() */ 3919349ce993SEric Dumazet smp_rmb(); 3920349ce993SEric Dumazet return this_cpu_ptr(&tcp_md5sig_pool); 3921349ce993SEric Dumazet } 392235790c04SEric Dumazet local_bh_enable(); 392335790c04SEric Dumazet return NULL; 3924cfb6eeb4SYOSHIFUJI Hideaki } 392535790c04SEric Dumazet EXPORT_SYMBOL(tcp_get_md5sig_pool); 3926cfb6eeb4SYOSHIFUJI Hideaki 392749a72dfbSAdam Langley int tcp_md5_hash_skb_data(struct tcp_md5sig_pool *hp, 3928cf533ea5SEric Dumazet const struct sk_buff *skb, unsigned int header_len) 392949a72dfbSAdam Langley { 393049a72dfbSAdam Langley struct scatterlist sg; 393149a72dfbSAdam Langley const struct tcphdr *tp = tcp_hdr(skb); 3932cf80e0e4SHerbert Xu struct ahash_request *req = hp->md5_req; 393395c96174SEric Dumazet unsigned int i; 393495c96174SEric Dumazet const unsigned int head_data_len = skb_headlen(skb) > header_len ? 393549a72dfbSAdam Langley skb_headlen(skb) - header_len : 0; 393649a72dfbSAdam Langley const struct skb_shared_info *shi = skb_shinfo(skb); 3937d7fd1b57SEric Dumazet struct sk_buff *frag_iter; 393849a72dfbSAdam Langley 393949a72dfbSAdam Langley sg_init_table(&sg, 1); 394049a72dfbSAdam Langley 394149a72dfbSAdam Langley sg_set_buf(&sg, ((u8 *) tp) + header_len, head_data_len); 3942cf80e0e4SHerbert Xu ahash_request_set_crypt(req, &sg, NULL, head_data_len); 3943cf80e0e4SHerbert Xu if (crypto_ahash_update(req)) 394449a72dfbSAdam Langley return 1; 394549a72dfbSAdam Langley 394649a72dfbSAdam Langley for (i = 0; i < shi->nr_frags; ++i) { 3947d8e18a51SMatthew Wilcox (Oracle) const skb_frag_t *f = &shi->frags[i]; 3948b54c9d5bSJonathan Lemon unsigned int offset = skb_frag_off(f); 394954d27fcbSEric Dumazet struct page *page = skb_frag_page(f) + (offset >> PAGE_SHIFT); 395054d27fcbSEric Dumazet 395154d27fcbSEric Dumazet sg_set_page(&sg, page, skb_frag_size(f), 395254d27fcbSEric Dumazet offset_in_page(offset)); 3953cf80e0e4SHerbert Xu ahash_request_set_crypt(req, &sg, NULL, skb_frag_size(f)); 3954cf80e0e4SHerbert Xu if (crypto_ahash_update(req)) 395549a72dfbSAdam Langley return 1; 395649a72dfbSAdam Langley } 395749a72dfbSAdam Langley 3958d7fd1b57SEric Dumazet skb_walk_frags(skb, frag_iter) 3959d7fd1b57SEric Dumazet if (tcp_md5_hash_skb_data(hp, frag_iter, 0)) 3960d7fd1b57SEric Dumazet return 1; 3961d7fd1b57SEric Dumazet 396249a72dfbSAdam Langley return 0; 396349a72dfbSAdam Langley } 396449a72dfbSAdam Langley EXPORT_SYMBOL(tcp_md5_hash_skb_data); 396549a72dfbSAdam Langley 3966cf533ea5SEric Dumazet int tcp_md5_hash_key(struct tcp_md5sig_pool *hp, const struct tcp_md5sig_key *key) 396749a72dfbSAdam Langley { 396849a72dfbSAdam Langley struct scatterlist sg; 396949a72dfbSAdam Langley 397049a72dfbSAdam Langley sg_init_one(&sg, key->key, key->keylen); 3971cf80e0e4SHerbert Xu ahash_request_set_crypt(hp->md5_req, &sg, NULL, key->keylen); 3972cf80e0e4SHerbert Xu return crypto_ahash_update(hp->md5_req); 397349a72dfbSAdam Langley } 397449a72dfbSAdam Langley EXPORT_SYMBOL(tcp_md5_hash_key); 397549a72dfbSAdam Langley 3976cfb6eeb4SYOSHIFUJI Hideaki #endif 3977cfb6eeb4SYOSHIFUJI Hideaki 39784ac02babSAndi Kleen void tcp_done(struct sock *sk) 39794ac02babSAndi Kleen { 3980d983ea6fSEric Dumazet struct request_sock *req; 39818336886fSJerry Chu 3982cab209e5SEric Dumazet /* We might be called with a new socket, after 3983cab209e5SEric Dumazet * inet_csk_prepare_forced_close() has been called 3984cab209e5SEric Dumazet * so we can not use lockdep_sock_is_held(sk) 3985cab209e5SEric Dumazet */ 3986cab209e5SEric Dumazet req = rcu_dereference_protected(tcp_sk(sk)->fastopen_rsk, 1); 39874ac02babSAndi Kleen 39884ac02babSAndi Kleen if (sk->sk_state == TCP_SYN_SENT || sk->sk_state == TCP_SYN_RECV) 3989c10d9310SEric Dumazet TCP_INC_STATS(sock_net(sk), TCP_MIB_ATTEMPTFAILS); 39904ac02babSAndi Kleen 39914ac02babSAndi Kleen tcp_set_state(sk, TCP_CLOSE); 39924ac02babSAndi Kleen tcp_clear_xmit_timers(sk); 399300db4124SIan Morris if (req) 39948336886fSJerry Chu reqsk_fastopen_remove(sk, req, false); 39954ac02babSAndi Kleen 39964ac02babSAndi Kleen sk->sk_shutdown = SHUTDOWN_MASK; 39974ac02babSAndi Kleen 39984ac02babSAndi Kleen if (!sock_flag(sk, SOCK_DEAD)) 39994ac02babSAndi Kleen sk->sk_state_change(sk); 40004ac02babSAndi Kleen else 40014ac02babSAndi Kleen inet_csk_destroy_sock(sk); 40024ac02babSAndi Kleen } 40034ac02babSAndi Kleen EXPORT_SYMBOL_GPL(tcp_done); 40044ac02babSAndi Kleen 4005c1e64e29SLorenzo Colitti int tcp_abort(struct sock *sk, int err) 4006c1e64e29SLorenzo Colitti { 4007c1e64e29SLorenzo Colitti if (!sk_fullsock(sk)) { 400807f6f4a3SEric Dumazet if (sk->sk_state == TCP_NEW_SYN_RECV) { 400907f6f4a3SEric Dumazet struct request_sock *req = inet_reqsk(sk); 401007f6f4a3SEric Dumazet 401107f6f4a3SEric Dumazet local_bh_disable(); 4012acc2cf4eSLorenzo Colitti inet_csk_reqsk_queue_drop(req->rsk_listener, req); 401307f6f4a3SEric Dumazet local_bh_enable(); 401407f6f4a3SEric Dumazet return 0; 401507f6f4a3SEric Dumazet } 4016c1e64e29SLorenzo Colitti return -EOPNOTSUPP; 4017c1e64e29SLorenzo Colitti } 4018c1e64e29SLorenzo Colitti 4019c1e64e29SLorenzo Colitti /* Don't race with userspace socket closes such as tcp_close. */ 4020c1e64e29SLorenzo Colitti lock_sock(sk); 4021c1e64e29SLorenzo Colitti 40222010b93eSLorenzo Colitti if (sk->sk_state == TCP_LISTEN) { 40232010b93eSLorenzo Colitti tcp_set_state(sk, TCP_CLOSE); 40242010b93eSLorenzo Colitti inet_csk_listen_stop(sk); 40252010b93eSLorenzo Colitti } 40262010b93eSLorenzo Colitti 4027c1e64e29SLorenzo Colitti /* Don't race with BH socket closes such as inet_csk_listen_stop. */ 4028c1e64e29SLorenzo Colitti local_bh_disable(); 4029c1e64e29SLorenzo Colitti bh_lock_sock(sk); 4030c1e64e29SLorenzo Colitti 4031c1e64e29SLorenzo Colitti if (!sock_flag(sk, SOCK_DEAD)) { 4032c1e64e29SLorenzo Colitti sk->sk_err = err; 4033c1e64e29SLorenzo Colitti /* This barrier is coupled with smp_rmb() in tcp_poll() */ 4034c1e64e29SLorenzo Colitti smp_wmb(); 4035c1e64e29SLorenzo Colitti sk->sk_error_report(sk); 4036c1e64e29SLorenzo Colitti if (tcp_need_reset(sk->sk_state)) 4037c1e64e29SLorenzo Colitti tcp_send_active_reset(sk, GFP_ATOMIC); 4038c1e64e29SLorenzo Colitti tcp_done(sk); 4039c1e64e29SLorenzo Colitti } 4040c1e64e29SLorenzo Colitti 4041c1e64e29SLorenzo Colitti bh_unlock_sock(sk); 4042c1e64e29SLorenzo Colitti local_bh_enable(); 4043e05836acSSoheil Hassas Yeganeh tcp_write_queue_purge(sk); 4044c1e64e29SLorenzo Colitti release_sock(sk); 4045c1e64e29SLorenzo Colitti return 0; 4046c1e64e29SLorenzo Colitti } 4047c1e64e29SLorenzo Colitti EXPORT_SYMBOL_GPL(tcp_abort); 4048c1e64e29SLorenzo Colitti 40495f8ef48dSStephen Hemminger extern struct tcp_congestion_ops tcp_reno; 40501da177e4SLinus Torvalds 40511da177e4SLinus Torvalds static __initdata unsigned long thash_entries; 40521da177e4SLinus Torvalds static int __init set_thash_entries(char *str) 40531da177e4SLinus Torvalds { 4054413c27d8SEldad Zack ssize_t ret; 4055413c27d8SEldad Zack 40561da177e4SLinus Torvalds if (!str) 40571da177e4SLinus Torvalds return 0; 4058413c27d8SEldad Zack 4059413c27d8SEldad Zack ret = kstrtoul(str, 0, &thash_entries); 4060413c27d8SEldad Zack if (ret) 4061413c27d8SEldad Zack return 0; 4062413c27d8SEldad Zack 40631da177e4SLinus Torvalds return 1; 40641da177e4SLinus Torvalds } 40651da177e4SLinus Torvalds __setup("thash_entries=", set_thash_entries); 40661da177e4SLinus Torvalds 406747d7a88cSFabian Frederick static void __init tcp_init_mem(void) 40684acb4190SGlauber Costa { 4069b66e91ccSEric Dumazet unsigned long limit = nr_free_buffer_pages() / 16; 4070b66e91ccSEric Dumazet 40714acb4190SGlauber Costa limit = max(limit, 128UL); 4072b66e91ccSEric Dumazet sysctl_tcp_mem[0] = limit / 4 * 3; /* 4.68 % */ 4073b66e91ccSEric Dumazet sysctl_tcp_mem[1] = limit; /* 6.25 % */ 4074b66e91ccSEric Dumazet sysctl_tcp_mem[2] = sysctl_tcp_mem[0] * 2; /* 9.37 % */ 40754acb4190SGlauber Costa } 40764acb4190SGlauber Costa 40771da177e4SLinus Torvalds void __init tcp_init(void) 40781da177e4SLinus Torvalds { 4079b49960a0SEric Dumazet int max_rshare, max_wshare, cnt; 4080b2d3ea4aSEric Dumazet unsigned long limit; 4081074b8517SDimitri Sivanich unsigned int i; 40821da177e4SLinus Torvalds 40833b4929f6SEric Dumazet BUILD_BUG_ON(TCP_MIN_SND_MSS <= MAX_TCP_OPTION_SPACE); 4084b2d3ea4aSEric Dumazet BUILD_BUG_ON(sizeof(struct tcp_skb_cb) > 4085c593642cSPankaj Bharadiya sizeof_field(struct sk_buff, cb)); 40861da177e4SLinus Torvalds 4087908c7f19STejun Heo percpu_counter_init(&tcp_sockets_allocated, 0, GFP_KERNEL); 4088908c7f19STejun Heo percpu_counter_init(&tcp_orphan_count, 0, GFP_KERNEL); 40891946e672SHaishuang Yan inet_hashinfo_init(&tcp_hashinfo); 409027da6d37SMartin KaFai Lau inet_hashinfo2_init(&tcp_hashinfo, "tcp_listen_portaddr_hash", 409127da6d37SMartin KaFai Lau thash_entries, 21, /* one slot per 2 MB*/ 409227da6d37SMartin KaFai Lau 0, 64 * 1024); 40936e04e021SArnaldo Carvalho de Melo tcp_hashinfo.bind_bucket_cachep = 40946e04e021SArnaldo Carvalho de Melo kmem_cache_create("tcp_bind_bucket", 40956e04e021SArnaldo Carvalho de Melo sizeof(struct inet_bind_bucket), 0, 409620c2df83SPaul Mundt SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL); 40971da177e4SLinus Torvalds 40981da177e4SLinus Torvalds /* Size and allocate the main established and bind bucket 40991da177e4SLinus Torvalds * hash tables. 41001da177e4SLinus Torvalds * 41011da177e4SLinus Torvalds * The methodology is similar to that of the buffer cache. 41021da177e4SLinus Torvalds */ 41036e04e021SArnaldo Carvalho de Melo tcp_hashinfo.ehash = 41041da177e4SLinus Torvalds alloc_large_system_hash("TCP established", 41050f7ff927SArnaldo Carvalho de Melo sizeof(struct inet_ehash_bucket), 41061da177e4SLinus Torvalds thash_entries, 4107fd90b29dSEric Dumazet 17, /* one slot per 128 KB of memory */ 41089e950efaSJohn Heffner 0, 41091da177e4SLinus Torvalds NULL, 4110f373b53bSEric Dumazet &tcp_hashinfo.ehash_mask, 411131fe62b9STim Bird 0, 41120ccfe618SJean Delvare thash_entries ? 0 : 512 * 1024); 411305dbc7b5SEric Dumazet for (i = 0; i <= tcp_hashinfo.ehash_mask; i++) 41143ab5aee7SEric Dumazet INIT_HLIST_NULLS_HEAD(&tcp_hashinfo.ehash[i].chain, i); 411505dbc7b5SEric Dumazet 4116230140cfSEric Dumazet if (inet_ehash_locks_alloc(&tcp_hashinfo)) 4117230140cfSEric Dumazet panic("TCP: failed to alloc ehash_locks"); 41186e04e021SArnaldo Carvalho de Melo tcp_hashinfo.bhash = 41191da177e4SLinus Torvalds alloc_large_system_hash("TCP bind", 41200f7ff927SArnaldo Carvalho de Melo sizeof(struct inet_bind_hashbucket), 4121f373b53bSEric Dumazet tcp_hashinfo.ehash_mask + 1, 4122fd90b29dSEric Dumazet 17, /* one slot per 128 KB of memory */ 41239e950efaSJohn Heffner 0, 41246e04e021SArnaldo Carvalho de Melo &tcp_hashinfo.bhash_size, 41251da177e4SLinus Torvalds NULL, 412631fe62b9STim Bird 0, 41271da177e4SLinus Torvalds 64 * 1024); 4128074b8517SDimitri Sivanich tcp_hashinfo.bhash_size = 1U << tcp_hashinfo.bhash_size; 41296e04e021SArnaldo Carvalho de Melo for (i = 0; i < tcp_hashinfo.bhash_size; i++) { 41306e04e021SArnaldo Carvalho de Melo spin_lock_init(&tcp_hashinfo.bhash[i].lock); 41316e04e021SArnaldo Carvalho de Melo INIT_HLIST_HEAD(&tcp_hashinfo.bhash[i].chain); 41321da177e4SLinus Torvalds } 41331da177e4SLinus Torvalds 4134c5ed63d6SEric Dumazet 4135c5ed63d6SEric Dumazet cnt = tcp_hashinfo.ehash_mask + 1; 4136c5ed63d6SEric Dumazet sysctl_tcp_max_orphans = cnt / 2; 41371da177e4SLinus Torvalds 4138a4fe34bfSEric W. Biederman tcp_init_mem(); 4139c43b874dSJason Wang /* Set per-socket limits to no more than 1/128 the pressure threshold */ 41405fb84b14SEric Dumazet limit = nr_free_buffer_pages() << (PAGE_SHIFT - 7); 4141b49960a0SEric Dumazet max_wshare = min(4UL*1024*1024, limit); 4142b49960a0SEric Dumazet max_rshare = min(6UL*1024*1024, limit); 41437b4f4b5eSJohn Heffner 4144356d1833SEric Dumazet init_net.ipv4.sysctl_tcp_wmem[0] = SK_MEM_QUANTUM; 4145356d1833SEric Dumazet init_net.ipv4.sysctl_tcp_wmem[1] = 16*1024; 4146356d1833SEric Dumazet init_net.ipv4.sysctl_tcp_wmem[2] = max(64*1024, max_wshare); 41477b4f4b5eSJohn Heffner 4148356d1833SEric Dumazet init_net.ipv4.sysctl_tcp_rmem[0] = SK_MEM_QUANTUM; 4149a337531bSYuchung Cheng init_net.ipv4.sysctl_tcp_rmem[1] = 131072; 4150a337531bSYuchung Cheng init_net.ipv4.sysctl_tcp_rmem[2] = max(131072, max_rshare); 41511da177e4SLinus Torvalds 4152afd46503SJoe Perches pr_info("Hash tables configured (established %u bind %u)\n", 4153f373b53bSEric Dumazet tcp_hashinfo.ehash_mask + 1, tcp_hashinfo.bhash_size); 4154317a76f9SStephen Hemminger 41551946e672SHaishuang Yan tcp_v4_init(); 415651c5d0c4SDavid S. Miller tcp_metrics_init(); 415755d8694fSFlorian Westphal BUG_ON(tcp_register_congestion_control(&tcp_reno) != 0); 415846d3ceabSEric Dumazet tcp_tasklet_init(); 4159f870fa0bSMat Martineau mptcp_init(); 41601da177e4SLinus Torvalds } 4161