Branch data Line data Source code
1 : : /*
2 : : * INET An implementation of the TCP/IP protocol suite for the LINUX
3 : : * operating system. INET is implemented using the BSD Socket
4 : : * interface as the means of communication with the user level.
5 : : *
6 : : * Implementation of the Transmission Control Protocol(TCP).
7 : : *
8 : : * Authors: Ross Biro
9 : : * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
10 : : * Mark Evans, <evansmp@uhura.aston.ac.uk>
11 : : * Corey Minyard <wf-rch!minyard@relay.EU.net>
12 : : * Florian La Roche, <flla@stud.uni-sb.de>
13 : : * Charles Hedrick, <hedrick@klinzhai.rutgers.edu>
14 : : * Linus Torvalds, <torvalds@cs.helsinki.fi>
15 : : * Alan Cox, <gw4pts@gw4pts.ampr.org>
16 : : * Matthew Dillon, <dillon@apollo.west.oic.com>
17 : : * Arnt Gulbrandsen, <agulbra@nvg.unit.no>
18 : : * Jorge Cwik, <jorge@laser.satlink.net>
19 : : *
20 : : * Fixes:
21 : : * Alan Cox : Numerous verify_area() calls
22 : : * Alan Cox : Set the ACK bit on a reset
23 : : * Alan Cox : Stopped it crashing if it closed while
24 : : * sk->inuse=1 and was trying to connect
25 : : * (tcp_err()).
26 : : * Alan Cox : All icmp error handling was broken
27 : : * pointers passed where wrong and the
28 : : * socket was looked up backwards. Nobody
29 : : * tested any icmp error code obviously.
30 : : * Alan Cox : tcp_err() now handled properly. It
31 : : * wakes people on errors. poll
32 : : * behaves and the icmp error race
33 : : * has gone by moving it into sock.c
34 : : * Alan Cox : tcp_send_reset() fixed to work for
35 : : * everything not just packets for
36 : : * unknown sockets.
37 : : * Alan Cox : tcp option processing.
38 : : * Alan Cox : Reset tweaked (still not 100%) [Had
39 : : * syn rule wrong]
40 : : * Herp Rosmanith : More reset fixes
41 : : * Alan Cox : No longer acks invalid rst frames.
42 : : * Acking any kind of RST is right out.
43 : : * Alan Cox : Sets an ignore me flag on an rst
44 : : * receive otherwise odd bits of prattle
45 : : * escape still
46 : : * Alan Cox : Fixed another acking RST frame bug.
47 : : * Should stop LAN workplace lockups.
48 : : * Alan Cox : Some tidyups using the new skb list
49 : : * facilities
50 : : * Alan Cox : sk->keepopen now seems to work
51 : : * Alan Cox : Pulls options out correctly on accepts
52 : : * Alan Cox : Fixed assorted sk->rqueue->next errors
53 : : * Alan Cox : PSH doesn't end a TCP read. Switched a
54 : : * bit to skb ops.
55 : : * Alan Cox : Tidied tcp_data to avoid a potential
56 : : * nasty.
57 : : * Alan Cox : Added some better commenting, as the
58 : : * tcp is hard to follow
59 : : * Alan Cox : Removed incorrect check for 20 * psh
60 : : * Michael O'Reilly : ack < copied bug fix.
61 : : * Johannes Stille : Misc tcp fixes (not all in yet).
62 : : * Alan Cox : FIN with no memory -> CRASH
63 : : * Alan Cox : Added socket option proto entries.
64 : : * Also added awareness of them to accept.
65 : : * Alan Cox : Added TCP options (SOL_TCP)
66 : : * Alan Cox : Switched wakeup calls to callbacks,
67 : : * so the kernel can layer network
68 : : * sockets.
69 : : * Alan Cox : Use ip_tos/ip_ttl settings.
70 : : * Alan Cox : Handle FIN (more) properly (we hope).
71 : : * Alan Cox : RST frames sent on unsynchronised
72 : : * state ack error.
73 : : * Alan Cox : Put in missing check for SYN bit.
74 : : * Alan Cox : Added tcp_select_window() aka NET2E
75 : : * window non shrink trick.
76 : : * Alan Cox : Added a couple of small NET2E timer
77 : : * fixes
78 : : * Charles Hedrick : TCP fixes
79 : : * Toomas Tamm : TCP window fixes
80 : : * Alan Cox : Small URG fix to rlogin ^C ack fight
81 : : * Charles Hedrick : Rewrote most of it to actually work
82 : : * Linus : Rewrote tcp_read() and URG handling
83 : : * completely
84 : : * Gerhard Koerting: Fixed some missing timer handling
85 : : * Matthew Dillon : Reworked TCP machine states as per RFC
86 : : * Gerhard Koerting: PC/TCP workarounds
87 : : * Adam Caldwell : Assorted timer/timing errors
88 : : * Matthew Dillon : Fixed another RST bug
89 : : * Alan Cox : Move to kernel side addressing changes.
90 : : * Alan Cox : Beginning work on TCP fastpathing
91 : : * (not yet usable)
92 : : * Arnt Gulbrandsen: Turbocharged tcp_check() routine.
93 : : * Alan Cox : TCP fast path debugging
94 : : * Alan Cox : Window clamping
95 : : * Michael Riepe : Bug in tcp_check()
96 : : * Matt Dillon : More TCP improvements and RST bug fixes
97 : : * Matt Dillon : Yet more small nasties remove from the
98 : : * TCP code (Be very nice to this man if
99 : : * tcp finally works 100%) 8)
100 : : * Alan Cox : BSD accept semantics.
101 : : * Alan Cox : Reset on closedown bug.
102 : : * Peter De Schrijver : ENOTCONN check missing in tcp_sendto().
103 : : * Michael Pall : Handle poll() after URG properly in
104 : : * all cases.
105 : : * Michael Pall : Undo the last fix in tcp_read_urg()
106 : : * (multi URG PUSH broke rlogin).
107 : : * Michael Pall : Fix the multi URG PUSH problem in
108 : : * tcp_readable(), poll() after URG
109 : : * works now.
110 : : * Michael Pall : recv(...,MSG_OOB) never blocks in the
111 : : * BSD api.
112 : : * Alan Cox : Changed the semantics of sk->socket to
113 : : * fix a race and a signal problem with
114 : : * accept() and async I/O.
115 : : * Alan Cox : Relaxed the rules on tcp_sendto().
116 : : * Yury Shevchuk : Really fixed accept() blocking problem.
117 : : * Craig I. Hagan : Allow for BSD compatible TIME_WAIT for
118 : : * clients/servers which listen in on
119 : : * fixed ports.
120 : : * Alan Cox : Cleaned the above up and shrank it to
121 : : * a sensible code size.
122 : : * Alan Cox : Self connect lockup fix.
123 : : * Alan Cox : No connect to multicast.
124 : : * Ross Biro : Close unaccepted children on master
125 : : * socket close.
126 : : * Alan Cox : Reset tracing code.
127 : : * Alan Cox : Spurious resets on shutdown.
128 : : * Alan Cox : Giant 15 minute/60 second timer error
129 : : * Alan Cox : Small whoops in polling before an
130 : : * accept.
131 : : * Alan Cox : Kept the state trace facility since
132 : : * it's handy for debugging.
133 : : * Alan Cox : More reset handler fixes.
134 : : * Alan Cox : Started rewriting the code based on
135 : : * the RFC's for other useful protocol
136 : : * references see: Comer, KA9Q NOS, and
137 : : * for a reference on the difference
138 : : * between specifications and how BSD
139 : : * works see the 4.4lite source.
140 : : * A.N.Kuznetsov : Don't time wait on completion of tidy
141 : : * close.
142 : : * Linus Torvalds : Fin/Shutdown & copied_seq changes.
143 : : * Linus Torvalds : Fixed BSD port reuse to work first syn
144 : : * Alan Cox : Reimplemented timers as per the RFC
145 : : * and using multiple timers for sanity.
146 : : * Alan Cox : Small bug fixes, and a lot of new
147 : : * comments.
148 : : * Alan Cox : Fixed dual reader crash by locking
149 : : * the buffers (much like datagram.c)
150 : : * Alan Cox : Fixed stuck sockets in probe. A probe
151 : : * now gets fed up of retrying without
152 : : * (even a no space) answer.
153 : : * Alan Cox : Extracted closing code better
154 : : * Alan Cox : Fixed the closing state machine to
155 : : * resemble the RFC.
156 : : * Alan Cox : More 'per spec' fixes.
157 : : * Jorge Cwik : Even faster checksumming.
158 : : * Alan Cox : tcp_data() doesn't ack illegal PSH
159 : : * only frames. At least one pc tcp stack
160 : : * generates them.
161 : : * Alan Cox : Cache last socket.
162 : : * Alan Cox : Per route irtt.
163 : : * Matt Day : poll()->select() match BSD precisely on error
164 : : * Alan Cox : New buffers
165 : : * Marc Tamsky : Various sk->prot->retransmits and
166 : : * sk->retransmits misupdating fixed.
167 : : * Fixed tcp_write_timeout: stuck close,
168 : : * and TCP syn retries gets used now.
169 : : * Mark Yarvis : In tcp_read_wakeup(), don't send an
170 : : * ack if state is TCP_CLOSED.
171 : : * Alan Cox : Look up device on a retransmit - routes may
172 : : * change. Doesn't yet cope with MSS shrink right
173 : : * but it's a start!
174 : : * Marc Tamsky : Closing in closing fixes.
175 : : * Mike Shaver : RFC1122 verifications.
176 : : * Alan Cox : rcv_saddr errors.
177 : : * Alan Cox : Block double connect().
178 : : * Alan Cox : Small hooks for enSKIP.
179 : : * Alexey Kuznetsov: Path MTU discovery.
180 : : * Alan Cox : Support soft errors.
181 : : * Alan Cox : Fix MTU discovery pathological case
182 : : * when the remote claims no mtu!
183 : : * Marc Tamsky : TCP_CLOSE fix.
184 : : * Colin (G3TNE) : Send a reset on syn ack replies in
185 : : * window but wrong (fixes NT lpd problems)
186 : : * Pedro Roque : Better TCP window handling, delayed ack.
187 : : * Joerg Reuter : No modification of locked buffers in
188 : : * tcp_do_retransmit()
189 : : * Eric Schenk : Changed receiver side silly window
190 : : * avoidance algorithm to BSD style
191 : : * algorithm. This doubles throughput
192 : : * against machines running Solaris,
193 : : * and seems to result in general
194 : : * improvement.
195 : : * Stefan Magdalinski : adjusted tcp_readable() to fix FIONREAD
196 : : * Willy Konynenberg : Transparent proxying support.
197 : : * Mike McLagan : Routing by source
198 : : * Keith Owens : Do proper merging with partial SKB's in
199 : : * tcp_do_sendmsg to avoid burstiness.
200 : : * Eric Schenk : Fix fast close down bug with
201 : : * shutdown() followed by close().
202 : : * Andi Kleen : Make poll agree with SIGIO
203 : : * Salvatore Sanfilippo : Support SO_LINGER with linger == 1 and
204 : : * lingertime == 0 (RFC 793 ABORT Call)
205 : : * Hirokazu Takahashi : Use copy_from_user() instead of
206 : : * csum_and_copy_from_user() if possible.
207 : : *
208 : : * This program is free software; you can redistribute it and/or
209 : : * modify it under the terms of the GNU General Public License
210 : : * as published by the Free Software Foundation; either version
211 : : * 2 of the License, or(at your option) any later version.
212 : : *
213 : : * Description of States:
214 : : *
215 : : * TCP_SYN_SENT sent a connection request, waiting for ack
216 : : *
217 : : * TCP_SYN_RECV received a connection request, sent ack,
218 : : * waiting for final ack in three-way handshake.
219 : : *
220 : : * TCP_ESTABLISHED connection established
221 : : *
222 : : * TCP_FIN_WAIT1 our side has shutdown, waiting to complete
223 : : * transmission of remaining buffered data
224 : : *
225 : : * TCP_FIN_WAIT2 all buffered data sent, waiting for remote
226 : : * to shutdown
227 : : *
228 : : * TCP_CLOSING both sides have shutdown but we still have
229 : : * data we have to finish sending
230 : : *
231 : : * TCP_TIME_WAIT timeout to catch resent junk before entering
232 : : * closed, can only be entered from FIN_WAIT2
233 : : * or CLOSING. Required because the other end
234 : : * may not have gotten our last ACK causing it
235 : : * to retransmit the data packet (which we ignore)
236 : : *
237 : : * TCP_CLOSE_WAIT remote side has shutdown and is waiting for
238 : : * us to finish writing our data and to shutdown
239 : : * (we have to close() to move on to LAST_ACK)
240 : : *
241 : : * TCP_LAST_ACK out side has shutdown after remote has
242 : : * shutdown. There may still be data in our
243 : : * buffer that we have to finish sending
244 : : *
245 : : * TCP_CLOSE socket is finished
246 : : */
247 : :
248 : : #define pr_fmt(fmt) "TCP: " fmt
249 : :
250 : : #include <linux/kernel.h>
251 : : #include <linux/module.h>
252 : : #include <linux/types.h>
253 : : #include <linux/fcntl.h>
254 : : #include <linux/poll.h>
255 : : #include <linux/init.h>
256 : : #include <linux/fs.h>
257 : : #include <linux/skbuff.h>
258 : : #include <linux/scatterlist.h>
259 : : #include <linux/splice.h>
260 : : #include <linux/net.h>
261 : : #include <linux/socket.h>
262 : : #include <linux/random.h>
263 : : #include <linux/bootmem.h>
264 : : #include <linux/highmem.h>
265 : : #include <linux/swap.h>
266 : : #include <linux/cache.h>
267 : : #include <linux/err.h>
268 : : #include <linux/crypto.h>
269 : : #include <linux/time.h>
270 : : #include <linux/slab.h>
271 : : #include <linux/uid_stat.h>
272 : :
273 : : #include <net/icmp.h>
274 : : #include <net/inet_common.h>
275 : : #include <net/tcp.h>
276 : : #include <net/xfrm.h>
277 : : #include <net/ip.h>
278 : : #include <net/ip6_route.h>
279 : : #include <net/ipv6.h>
280 : : #include <net/transp_v6.h>
281 : : #include <net/netdma.h>
282 : : #include <net/sock.h>
283 : :
284 : : #include <asm/uaccess.h>
285 : : #include <asm/ioctls.h>
286 : : #include <net/busy_poll.h>
287 : :
288 : : int sysctl_tcp_fin_timeout __read_mostly = TCP_FIN_TIMEOUT;
289 : :
290 : : int sysctl_tcp_min_tso_segs __read_mostly = 2;
291 : :
292 : : struct percpu_counter tcp_orphan_count;
293 : : EXPORT_SYMBOL_GPL(tcp_orphan_count);
294 : :
295 : : long sysctl_tcp_mem[3] __read_mostly;
296 : : int sysctl_tcp_wmem[3] __read_mostly;
297 : : int sysctl_tcp_rmem[3] __read_mostly;
298 : :
299 : : EXPORT_SYMBOL(sysctl_tcp_mem);
300 : : EXPORT_SYMBOL(sysctl_tcp_rmem);
301 : : EXPORT_SYMBOL(sysctl_tcp_wmem);
302 : :
303 : : atomic_long_t tcp_memory_allocated; /* Current allocated memory. */
304 : : EXPORT_SYMBOL(tcp_memory_allocated);
305 : :
306 : : /*
307 : : * Current number of TCP sockets.
308 : : */
309 : : struct percpu_counter tcp_sockets_allocated;
310 : : EXPORT_SYMBOL(tcp_sockets_allocated);
311 : :
312 : : /*
313 : : * TCP splice context
314 : : */
315 : : struct tcp_splice_state {
316 : : struct pipe_inode_info *pipe;
317 : : size_t len;
318 : : unsigned int flags;
319 : : };
320 : :
321 : : /*
322 : : * Pressure flag: try to collapse.
323 : : * Technical note: it is used by multiple contexts non atomically.
324 : : * All the __sk_mem_schedule() is of this nature: accounting
325 : : * is strict, actions are advisory and have some latency.
326 : : */
327 : : int tcp_memory_pressure __read_mostly;
328 : : EXPORT_SYMBOL(tcp_memory_pressure);
329 : :
330 : 0 : void tcp_enter_memory_pressure(struct sock *sk)
331 : : {
332 [ # # ]: 0 : if (!tcp_memory_pressure) {
333 : 0 : NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPMEMORYPRESSURES);
334 : 0 : tcp_memory_pressure = 1;
335 : : }
336 : 0 : }
337 : : EXPORT_SYMBOL(tcp_enter_memory_pressure);
338 : :
339 : : /* Convert seconds to retransmits based on initial and max timeout */
340 : : static u8 secs_to_retrans(int seconds, int timeout, int rto_max)
341 : : {
342 : : u8 res = 0;
343 : :
344 [ # # ]: 3 : if (seconds > 0) {
345 : : int period = timeout;
346 : :
347 : : res = 1;
348 [ # # ]: 0 : while (seconds > period && res < 255) {
349 : 0 : res++;
350 : 0 : timeout <<= 1;
351 [ # # ]: 0 : if (timeout > rto_max)
352 : : timeout = rto_max;
353 : 0 : period += timeout;
354 : : }
355 : : }
356 : : return res;
357 : : }
358 : :
359 : : /* Convert retransmits to seconds based on initial and max timeout */
360 : : static int retrans_to_secs(u8 retrans, int timeout, int rto_max)
361 : : {
362 : : int period = 0;
363 : :
364 [ # # ]: 0 : if (retrans > 0) {
365 : : period = timeout;
366 [ # # ]: 0 : while (--retrans) {
367 : 0 : timeout <<= 1;
368 [ # # ]: 0 : if (timeout > rto_max)
369 : : timeout = rto_max;
370 : 0 : period += timeout;
371 : : }
372 : : }
373 : : return period;
374 : : }
375 : :
376 : : /* Address-family independent initialization for a tcp_sock.
377 : : *
378 : : * NOTE: A lot of things set to zero explicitly by call to
379 : : * sk_alloc() so need not be done here.
380 : : */
381 : 0 : void tcp_init_sock(struct sock *sk)
382 : : {
383 : : struct inet_connection_sock *icsk = inet_csk(sk);
384 : : struct tcp_sock *tp = tcp_sk(sk);
385 : :
386 : 83 : skb_queue_head_init(&tp->out_of_order_queue);
387 : 83 : tcp_init_xmit_timers(sk);
388 : : tcp_prequeue_init(tp);
389 : 83 : INIT_LIST_HEAD(&tp->tsq_node);
390 : :
391 : 83 : icsk->icsk_rto = TCP_TIMEOUT_INIT;
392 : 83 : tp->mdev = TCP_TIMEOUT_INIT;
393 : :
394 : : /* So many TCP implementations out there (incorrectly) count the
395 : : * initial SYN frame in their delayed-ACK and congestion control
396 : : * algorithms that we must have the following bandaid to talk
397 : : * efficiently to them. -DaveM
398 : : */
399 : 83 : tp->snd_cwnd = TCP_INIT_CWND;
400 : :
401 : : /* See draft-stevens-tcpca-spec-01 for discussion of the
402 : : * initialization of these values.
403 : : */
404 : 83 : tp->snd_ssthresh = TCP_INFINITE_SSTHRESH;
405 : 83 : tp->snd_cwnd_clamp = ~0;
406 : 83 : tp->mss_cache = TCP_MSS_DEFAULT;
407 : :
408 : 83 : tp->reordering = sysctl_tcp_reordering;
409 : : tcp_enable_early_retrans(tp);
410 : 0 : icsk->icsk_ca_ops = &tcp_init_congestion_ops;
411 : :
412 : 0 : tp->tsoffset = 0;
413 : :
414 : 0 : sk->sk_state = TCP_CLOSE;
415 : :
416 : 0 : sk->sk_write_space = sk_stream_write_space;
417 : : sock_set_flag(sk, SOCK_USE_WRITE_QUEUE);
418 : :
419 : 0 : icsk->icsk_sync_mss = tcp_sync_mss;
420 : :
421 : 0 : sk->sk_sndbuf = sysctl_tcp_wmem[1];
422 : 0 : sk->sk_rcvbuf = sysctl_tcp_rmem[1];
423 : :
424 : 83 : local_bh_disable();
425 : : sock_update_memcg(sk);
426 : : sk_sockets_allocated_inc(sk);
427 : 83 : local_bh_enable();
428 : 83 : }
429 : : EXPORT_SYMBOL(tcp_init_sock);
430 : :
431 : : /*
432 : : * Wait for a TCP event.
433 : : *
434 : : * Note that we don't need to lock the socket, as the upper poll layers
435 : : * take care of normal races (between the test and the event) and we don't
436 : : * go look at any of the socket buffers directly.
437 : : */
438 : 0 : unsigned int tcp_poll(struct file *file, struct socket *sock, poll_table *wait)
439 : : {
440 : : unsigned int mask;
441 : 95184 : struct sock *sk = sock->sk;
442 : : const struct tcp_sock *tp = tcp_sk(sk);
443 : :
444 : : sock_rps_record_flow(sk);
445 : :
446 : 95184 : sock_poll_wait(file, sk_sleep(sk), wait);
447 [ + + ]: 95184 : if (sk->sk_state == TCP_LISTEN)
448 : 90 : return inet_csk_listen_poll(sk);
449 : :
450 : : /* Socket is not locked. We are protected from async events
451 : : * by poll logic and correct handling of state changes
452 : : * made by other threads is impossible in any case.
453 : : */
454 : :
455 : : mask = 0;
456 : :
457 : : /*
458 : : * POLLHUP is certainly not done right. But poll() doesn't
459 : : * have a notion of HUP in just one direction, and for a
460 : : * socket the read side is more interesting.
461 : : *
462 : : * Some poll() documentation says that POLLHUP is incompatible
463 : : * with the POLLOUT/POLLWR flags, so somebody should check this
464 : : * all. But careful, it tends to be safer to return too many
465 : : * bits than too few, and you can easily break real applications
466 : : * if you don't tell them that something has hung up!
467 : : *
468 : : * Check-me.
469 : : *
470 : : * Check number 1. POLLHUP is _UNMASKABLE_ event (see UNIX98 and
471 : : * our fs/select.c). It means that after we received EOF,
472 : : * poll always returns immediately, making impossible poll() on write()
473 : : * in state CLOSE_WAIT. One solution is evident --- to set POLLHUP
474 : : * if and only if shutdown has been made in both directions.
475 : : * Actually, it is interesting to look how Solaris and DUX
476 : : * solve this dilemma. I would prefer, if POLLHUP were maskable,
477 : : * then we could set it on SND_SHUTDOWN. BTW examples given
478 : : * in Stevens' books assume exactly this behaviour, it explains
479 : : * why POLLHUP is incompatible with POLLOUT. --ANK
480 : : *
481 : : * NOTE. Check for TCP_CLOSE is added. The goal is to prevent
482 : : * blocking on fresh not-connected or disconnected socket. --ANK
483 : : */
484 [ + + ][ - + ]: 95094 : if (sk->sk_shutdown == SHUTDOWN_MASK || sk->sk_state == TCP_CLOSE)
485 : : mask |= POLLHUP;
486 [ + + ]: 95094 : if (sk->sk_shutdown & RCV_SHUTDOWN)
487 : 19 : mask |= POLLIN | POLLRDNORM | POLLRDHUP;
488 : :
489 : : /* Connected or passive Fast Open socket? */
490 [ + + ][ - + ]: 95094 : if (sk->sk_state != TCP_SYN_SENT &&
491 [ # # ]: 0 : (sk->sk_state != TCP_SYN_RECV || tp->fastopen_rsk != NULL)) {
492 : : int target = sock_rcvlowat(sk, 0, INT_MAX);
493 : :
494 [ - + ][ # # ]: 190276 : if (tp->urg_seq == tp->copied_seq &&
495 [ # # ]: 0 : !sock_flag(sk, SOCK_URGINLINE) &&
496 : 0 : tp->urg_data)
497 : 0 : target++;
498 : :
499 : : /* Potential race condition. If read of tp below will
500 : : * escape above sk->sk_state, we can be illegally awaken
501 : : * in SYN_* states. */
502 [ + + ]: 95092 : if (tp->rcv_nxt - tp->copied_seq >= target)
503 : 6105 : mask |= POLLIN | POLLRDNORM;
504 : :
505 [ + + ]: 95092 : if (!(sk->sk_shutdown & SEND_SHUTDOWN)) {
506 [ + + ]: 95083 : if (sk_stream_is_writeable(sk)) {
507 : 95023 : mask |= POLLOUT | POLLWRNORM;
508 : : } else { /* send SIGIO later */
509 : 60 : set_bit(SOCK_ASYNC_NOSPACE,
510 : : &sk->sk_socket->flags);
511 : 60 : set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
512 : :
513 : : /* Race breaker. If space is freed after
514 : : * wspace test but before the flags are set,
515 : : * IO signal will be lost.
516 : : */
517 [ - + ]: 60 : if (sk_stream_is_writeable(sk))
518 : 0 : mask |= POLLOUT | POLLWRNORM;
519 : : }
520 : : } else
521 : 10 : mask |= POLLOUT | POLLWRNORM;
522 : :
523 [ - + ]: 95093 : if (tp->urg_data & TCP_URG_VALID)
524 : 0 : mask |= POLLPRI;
525 : : }
526 : : /* This barrier is coupled with smp_wmb() in tcp_reset() */
527 : 0 : smp_rmb();
528 [ + + ]: 95094 : if (sk->sk_err)
529 : 10 : mask |= POLLERR;
530 : :
531 : 95094 : return mask;
532 : : }
533 : : EXPORT_SYMBOL(tcp_poll);
534 : :
535 : 0 : int tcp_ioctl(struct sock *sk, int cmd, unsigned long arg)
536 : : {
537 : : struct tcp_sock *tp = tcp_sk(sk);
538 : : int answ;
539 : : bool slow;
540 : :
541 [ - + - - : 1 : switch (cmd) {
- ]
542 : : case SIOCINQ:
543 [ # # ]: 0 : if (sk->sk_state == TCP_LISTEN)
544 : : return -EINVAL;
545 : :
546 : 0 : slow = lock_sock_fast(sk);
547 [ # # ]: 0 : if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV))
548 : : answ = 0;
549 [ # # ][ # # ]: 0 : else if (sock_flag(sk, SOCK_URGINLINE) ||
550 [ # # ]: 0 : !tp->urg_data ||
551 [ # # ]: 0 : before(tp->urg_seq, tp->copied_seq) ||
552 : 0 : !before(tp->urg_seq, tp->rcv_nxt)) {
553 : :
554 : 0 : answ = tp->rcv_nxt - tp->copied_seq;
555 : :
556 : : /* Subtract 1, if FIN was received */
557 [ # # ][ # # ]: 0 : if (answ && sock_flag(sk, SOCK_DONE))
558 : 0 : answ--;
559 : : } else
560 : : answ = tp->urg_seq - tp->copied_seq;
561 : : unlock_sock_fast(sk, slow);
562 : : break;
563 : : case SIOCATMARK:
564 [ - + ][ # # ]: 1 : answ = tp->urg_data && tp->urg_seq == tp->copied_seq;
565 : 1 : break;
566 : : case SIOCOUTQ:
567 [ # # ]: 0 : if (sk->sk_state == TCP_LISTEN)
568 : : return -EINVAL;
569 : :
570 [ # # ]: 0 : if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV))
571 : : answ = 0;
572 : : else
573 : 0 : answ = tp->write_seq - tp->snd_una;
574 : : break;
575 : : case SIOCOUTQNSD:
576 [ # # ]: 0 : if (sk->sk_state == TCP_LISTEN)
577 : : return -EINVAL;
578 : :
579 [ # # ]: 0 : if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV))
580 : : answ = 0;
581 : : else
582 : 0 : answ = tp->write_seq - tp->snd_nxt;
583 : : break;
584 : : default:
585 : : return -ENOIOCTLCMD;
586 : : }
587 : :
588 : 1 : return put_user(answ, (int __user *)arg);
589 : : }
590 : : EXPORT_SYMBOL(tcp_ioctl);
591 : :
592 : : static inline void tcp_mark_push(struct tcp_sock *tp, struct sk_buff *skb)
593 : : {
594 : 41195 : TCP_SKB_CB(skb)->tcp_flags |= TCPHDR_PSH;
595 : 41195 : tp->pushed_seq = tp->write_seq;
596 : : }
597 : :
598 : : static inline bool forced_push(const struct tcp_sock *tp)
599 : : {
600 : 556 : return after(tp->write_seq, tp->pushed_seq + (tp->max_window >> 1));
601 : : }
602 : :
603 : : static inline void skb_entail(struct sock *sk, struct sk_buff *skb)
604 : : {
605 : : struct tcp_sock *tp = tcp_sk(sk);
606 : : struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
607 : :
608 : 41593 : skb->csum = 0;
609 : 41593 : tcb->seq = tcb->end_seq = tp->write_seq;
610 : 41593 : tcb->tcp_flags = TCPHDR_ACK;
611 : 41593 : tcb->sacked = 0;
612 : : skb_header_release(skb);
613 : : tcp_add_write_queue_tail(sk, skb);
614 : 41594 : sk->sk_wmem_queued += skb->truesize;
615 : 41594 : sk_mem_charge(sk, skb->truesize);
616 [ + + ][ # # ]: 41594 : if (tp->nonagle & TCP_NAGLE_PUSH)
617 : 2 : tp->nonagle &= ~TCP_NAGLE_PUSH;
618 : : }
619 : :
620 : : static inline void tcp_mark_urg(struct tcp_sock *tp, int flags)
621 : : {
622 [ # # ][ - + ]: 41195 : if (flags & MSG_OOB)
[ # # ][ # # ]
623 : 0 : tp->snd_up = tp->write_seq;
624 : : }
625 : :
626 : : static inline void tcp_push(struct sock *sk, int flags, int mss_now,
627 : : int nonagle)
628 : : {
629 [ # # ][ + - ]: 41195 : if (tcp_send_head(sk)) {
[ # # ][ # # ]
630 : : struct tcp_sock *tp = tcp_sk(sk);
631 : :
632 [ - + ][ # # ]: 41195 : if (!(flags & MSG_MORE) || forced_push(tp))
[ # # ][ # # ]
633 : : tcp_mark_push(tp, tcp_write_queue_tail(sk));
634 : :
635 : : tcp_mark_urg(tp, flags);
636 [ + - ][ # # ]: 41195 : __tcp_push_pending_frames(sk, mss_now,
637 : : (flags & MSG_MORE) ? TCP_NAGLE_CORK : nonagle);
638 : : }
639 : : }
640 : :
641 : 0 : static int tcp_splice_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb,
642 : : unsigned int offset, size_t len)
643 : : {
644 : 0 : struct tcp_splice_state *tss = rd_desc->arg.data;
645 : : int ret;
646 : :
647 : 0 : ret = skb_splice_bits(skb, offset, tss->pipe, min(rd_desc->count, len),
648 : : tss->flags);
649 [ # # ]: 0 : if (ret > 0)
650 : 0 : rd_desc->count -= ret;
651 : 0 : return ret;
652 : : }
653 : :
654 : 0 : static int __tcp_splice_read(struct sock *sk, struct tcp_splice_state *tss)
655 : : {
656 : : /* Store TCP splice context information in read_descriptor_t. */
657 : 0 : read_descriptor_t rd_desc = {
658 : : .arg.data = tss,
659 : 0 : .count = tss->len,
660 : : };
661 : :
662 : 0 : return tcp_read_sock(sk, &rd_desc, tcp_splice_data_recv);
663 : : }
664 : :
665 : : /**
666 : : * tcp_splice_read - splice data from TCP socket to a pipe
667 : : * @sock: socket to splice from
668 : : * @ppos: position (not valid)
669 : : * @pipe: pipe to splice to
670 : : * @len: number of bytes to splice
671 : : * @flags: splice modifier flags
672 : : *
673 : : * Description:
674 : : * Will read pages from given socket and fill them into a pipe.
675 : : *
676 : : **/
677 : 0 : ssize_t tcp_splice_read(struct socket *sock, loff_t *ppos,
678 : : struct pipe_inode_info *pipe, size_t len,
679 : : unsigned int flags)
680 : : {
681 : 0 : struct sock *sk = sock->sk;
682 : 0 : struct tcp_splice_state tss = {
683 : : .pipe = pipe,
684 : : .len = len,
685 : : .flags = flags,
686 : : };
687 : : long timeo;
688 : : ssize_t spliced;
689 : : int ret;
690 : :
691 : : sock_rps_record_flow(sk);
692 : : /*
693 : : * We can't seek on a socket input
694 : : */
695 [ # # ]: 0 : if (unlikely(*ppos))
696 : : return -ESPIPE;
697 : :
698 : : ret = spliced = 0;
699 : :
700 : : lock_sock(sk);
701 : :
702 : 0 : timeo = sock_rcvtimeo(sk, sock->file->f_flags & O_NONBLOCK);
703 [ # # ]: 0 : while (tss.len) {
704 : 0 : ret = __tcp_splice_read(sk, &tss);
705 [ # # ]: 0 : if (ret < 0)
706 : : break;
707 [ # # ]: 0 : else if (!ret) {
708 [ # # ]: 0 : if (spliced)
709 : : break;
710 [ # # ]: 0 : if (sock_flag(sk, SOCK_DONE))
711 : : break;
712 [ # # ]: 0 : if (sk->sk_err) {
713 : : ret = sock_error(sk);
714 : 0 : break;
715 : : }
716 [ # # ]: 0 : if (sk->sk_shutdown & RCV_SHUTDOWN)
717 : : break;
718 [ # # ]: 0 : if (sk->sk_state == TCP_CLOSE) {
719 : : /*
720 : : * This occurs when user tries to read
721 : : * from never connected socket.
722 : : */
723 [ # # ]: 0 : if (!sock_flag(sk, SOCK_DONE))
724 : : ret = -ENOTCONN;
725 : : break;
726 : : }
727 [ # # ]: 0 : if (!timeo) {
728 : : ret = -EAGAIN;
729 : : break;
730 : : }
731 : 0 : sk_wait_data(sk, &timeo);
732 [ # # ]: 0 : if (signal_pending(current)) {
733 : 0 : ret = sock_intr_errno(timeo);
734 : 0 : break;
735 : : }
736 : 0 : continue;
737 : : }
738 : 0 : tss.len -= ret;
739 : 0 : spliced += ret;
740 : :
741 [ # # ]: 0 : if (!timeo)
742 : : break;
743 : 0 : release_sock(sk);
744 : : lock_sock(sk);
745 : :
746 [ # # ][ # # ]: 0 : if (sk->sk_err || sk->sk_state == TCP_CLOSE ||
[ # # ]
747 [ # # ]: 0 : (sk->sk_shutdown & RCV_SHUTDOWN) ||
748 : 0 : signal_pending(current))
749 : : break;
750 : : }
751 : :
752 : 0 : release_sock(sk);
753 : :
754 [ # # ]: 0 : if (spliced)
755 : : return spliced;
756 : :
757 : 0 : return ret;
758 : : }
759 : : EXPORT_SYMBOL(tcp_splice_read);
760 : :
761 : 0 : struct sk_buff *sk_stream_alloc_skb(struct sock *sk, int size, gfp_t gfp)
762 : : {
763 : : struct sk_buff *skb;
764 : :
765 : : /* The TCP header must be at least 32-bit aligned. */
766 : 41594 : size = ALIGN(size, 4);
767 : :
768 : 41594 : skb = alloc_skb_fclone(size + sk->sk_prot->max_header, gfp);
769 [ + - ]: 83188 : if (skb) {
770 [ + - ]: 41594 : if (sk_wmem_schedule(sk, skb->truesize)) {
771 : 41594 : skb_reserve(skb, sk->sk_prot->max_header);
772 : : /*
773 : : * Make sure that we have exactly size bytes
774 : : * available to the caller, no more, no less.
775 : : */
776 : 41594 : skb->reserved_tailroom = skb->end - skb->tail - size;
777 : 41594 : return skb;
778 : : }
779 : 0 : __kfree_skb(skb);
780 : : } else {
781 : 0 : sk->sk_prot->enter_memory_pressure(sk);
782 : : sk_stream_moderate_sndbuf(sk);
783 : : }
784 : : return NULL;
785 : : }
786 : :
787 : 0 : static unsigned int tcp_xmit_size_goal(struct sock *sk, u32 mss_now,
788 : : int large_allowed)
789 : : {
790 : : struct tcp_sock *tp = tcp_sk(sk);
791 : : u32 xmit_size_goal, old_size_goal;
792 : :
793 : : xmit_size_goal = mss_now;
794 : :
795 [ + + ][ + + ]: 41196 : if (large_allowed && sk_can_gso(sk)) {
796 : : u32 gso_size, hlen;
797 : :
798 : : /* Maybe we should/could use sk->sk_prot->max_header here ? */
799 : 1773 : hlen = inet_csk(sk)->icsk_af_ops->net_header_len +
800 : 1182 : inet_csk(sk)->icsk_ext_hdr_len +
801 : 591 : tp->tcp_header_len;
802 : :
803 : : /* Goal is to send at least one packet per ms,
804 : : * not one big TSO packet every 100 ms.
805 : : * This preserves ACK clocking and is consistent
806 : : * with tcp_tso_should_defer() heuristic.
807 : : */
808 : 591 : gso_size = sk->sk_pacing_rate / (2 * MSEC_PER_SEC);
809 : 591 : gso_size = max_t(u32, gso_size,
810 : : sysctl_tcp_min_tso_segs * mss_now);
811 : :
812 : 591 : xmit_size_goal = min_t(u32, gso_size,
813 : : sk->sk_gso_max_size - 1 - hlen);
814 : :
815 : 1182 : xmit_size_goal = tcp_bound_to_half_wnd(tp, xmit_size_goal);
816 : :
817 : : /* We try hard to avoid divides here */
818 : 591 : old_size_goal = tp->xmit_size_goal_segs * mss_now;
819 : :
820 [ + ][ + ]: 591 : if (likely(old_size_goal <= xmit_size_goal &&
821 : : old_size_goal + mss_now > xmit_size_goal)) {
822 : : xmit_size_goal = old_size_goal;
823 : : } else {
824 : 17 : tp->xmit_size_goal_segs =
825 : 17 : min_t(u16, xmit_size_goal / mss_now,
826 : : sk->sk_gso_max_segs);
827 : 17 : xmit_size_goal = tp->xmit_size_goal_segs * mss_now;
828 : : }
829 : : }
830 : :
831 : 0 : return max(xmit_size_goal, mss_now);
832 : : }
833 : :
834 : 0 : static int tcp_send_mss(struct sock *sk, int *size_goal, int flags)
835 : : {
836 : : int mss_now;
837 : :
838 : 41196 : mss_now = tcp_current_mss(sk);
839 : 41196 : *size_goal = tcp_xmit_size_goal(sk, mss_now, !(flags & MSG_OOB));
840 : :
841 : 41196 : return mss_now;
842 : : }
843 : :
844 : 0 : static ssize_t do_tcp_sendpages(struct sock *sk, struct page *page, int offset,
845 : : size_t size, int flags)
846 : : {
847 : : struct tcp_sock *tp = tcp_sk(sk);
848 : : int mss_now, size_goal;
849 : : int err;
850 : : ssize_t copied;
851 : 0 : long timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
852 : :
853 : : /* Wait for a connection to finish. One exception is TCP Fast Open
854 : : * (passive side) where data is allowed to be sent before a connection
855 : : * is fully established.
856 : : */
857 [ # # ][ # # ]: 0 : if (((1 << sk->sk_state) & ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) &&
858 : : !tcp_passive_fastopen(sk)) {
859 [ # # ]: 0 : if ((err = sk_stream_wait_connect(sk, &timeo)) != 0)
860 : : goto out_err;
861 : : }
862 : :
863 : 0 : clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
864 : :
865 : 0 : mss_now = tcp_send_mss(sk, &size_goal, flags);
866 : : copied = 0;
867 : :
868 : : err = -EPIPE;
869 [ # # ][ # # ]: 0 : if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN))
870 : : goto out_err;
871 : :
872 [ # # ]: 0 : while (size > 0) {
873 : 0 : struct sk_buff *skb = tcp_write_queue_tail(sk);
874 : : int copy, i;
875 : : bool can_coalesce;
876 : :
877 [ # # ][ # # ]: 0 : if (!tcp_send_head(sk) || (copy = size_goal - skb->len) <= 0) {
878 : : new_segment:
879 [ # # ]: 0 : if (!sk_stream_memory_free(sk))
880 : : goto wait_for_sndbuf;
881 : :
882 : 0 : skb = sk_stream_alloc_skb(sk, 0, sk->sk_allocation);
883 [ # # ]: 0 : if (!skb)
884 : : goto wait_for_memory;
885 : :
886 : : skb_entail(sk, skb);
887 : 0 : copy = size_goal;
888 : : }
889 : :
890 [ # # ]: 0 : if (copy > size)
891 : 0 : copy = size;
892 : :
893 : 0 : i = skb_shinfo(skb)->nr_frags;
894 : : can_coalesce = skb_can_coalesce(skb, i, page, offset);
895 [ # # ]: 0 : if (!can_coalesce && i >= MAX_SKB_FRAGS) {
896 : : tcp_mark_push(tp, skb);
897 : : goto new_segment;
898 : : }
899 [ # # ]: 0 : if (!sk_wmem_schedule(sk, copy))
900 : : goto wait_for_memory;
901 : :
902 [ # # ]: 0 : if (can_coalesce) {
903 : 0 : skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy);
904 : : } else {
905 : : get_page(page);
906 : : skb_fill_page_desc(skb, i, page, offset, copy);
907 : : }
908 : 0 : skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG;
909 : :
910 : 0 : skb->len += copy;
911 : 0 : skb->data_len += copy;
912 : 0 : skb->truesize += copy;
913 : 0 : sk->sk_wmem_queued += copy;
914 : : sk_mem_charge(sk, copy);
915 : 0 : skb->ip_summed = CHECKSUM_PARTIAL;
916 : 0 : tp->write_seq += copy;
917 : 0 : TCP_SKB_CB(skb)->end_seq += copy;
918 : 0 : skb_shinfo(skb)->gso_segs = 0;
919 : :
920 [ # # ]: 0 : if (!copied)
921 : 0 : TCP_SKB_CB(skb)->tcp_flags &= ~TCPHDR_PSH;
922 : :
923 : 0 : copied += copy;
924 : 0 : offset += copy;
925 [ # # ]: 0 : if (!(size -= copy))
926 : : goto out;
927 : :
928 [ # # ][ # # ]: 0 : if (skb->len < size_goal || (flags & MSG_OOB))
929 : 0 : continue;
930 : :
931 [ # # ]: 0 : if (forced_push(tp)) {
932 : : tcp_mark_push(tp, skb);
933 : 0 : __tcp_push_pending_frames(sk, mss_now, TCP_NAGLE_PUSH);
934 [ # # ]: 0 : } else if (skb == tcp_send_head(sk))
935 : 0 : tcp_push_one(sk, mss_now);
936 : 0 : continue;
937 : :
938 : : wait_for_sndbuf:
939 : 0 : set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
940 : : wait_for_memory:
941 : : tcp_push(sk, flags & ~MSG_MORE, mss_now, TCP_NAGLE_PUSH);
942 : :
943 [ # # ]: 0 : if ((err = sk_stream_wait_memory(sk, &timeo)) != 0)
944 : : goto do_error;
945 : :
946 : 0 : mss_now = tcp_send_mss(sk, &size_goal, flags);
947 : : }
948 : :
949 : : out:
950 [ # # ][ # # ]: 0 : if (copied && !(flags & MSG_SENDPAGE_NOTLAST))
951 : 0 : tcp_push(sk, flags, mss_now, tp->nonagle);
952 : 0 : return copied;
953 : :
954 : : do_error:
955 [ # # ]: 0 : if (copied)
956 : : goto out;
957 : : out_err:
958 : 0 : return sk_stream_error(sk, flags, err);
959 : : }
960 : :
961 : 0 : int tcp_sendpage(struct sock *sk, struct page *page, int offset,
962 : : size_t size, int flags)
963 : : {
964 : : ssize_t res;
965 : :
966 [ # # ][ # # ]: 0 : if (!(sk->sk_route_caps & NETIF_F_SG) ||
967 : 0 : !(sk->sk_route_caps & NETIF_F_ALL_CSUM))
968 : 0 : return sock_no_sendpage(sk->sk_socket, page, offset, size,
969 : : flags);
970 : :
971 : : lock_sock(sk);
972 : 0 : res = do_tcp_sendpages(sk, page, offset, size, flags);
973 : 0 : release_sock(sk);
974 : 0 : return res;
975 : : }
976 : : EXPORT_SYMBOL(tcp_sendpage);
977 : :
978 : : static inline int select_size(const struct sock *sk, bool sg)
979 : : {
980 : : const struct tcp_sock *tp = tcp_sk(sk);
981 : 41593 : int tmp = tp->mss_cache;
982 : :
983 [ + + ]: 41593 : if (sg) {
984 [ - + ]: 591 : if (sk_can_gso(sk)) {
985 : : /* Small frames wont use a full page:
986 : : * Payload will immediately follow tcp header.
987 : : */
988 : : tmp = SKB_WITH_OVERHEAD(2048 - MAX_TCP_HEADER);
989 : : } else {
990 : : int pgbreak = SKB_MAX_HEAD(MAX_TCP_HEADER);
991 : :
992 [ # # ][ # # ]: 0 : if (tmp >= pgbreak &&
993 : : tmp <= pgbreak + (MAX_SKB_FRAGS - 1) * PAGE_SIZE)
994 : : tmp = pgbreak;
995 : : }
996 : : }
997 : :
998 : : return tmp;
999 : : }
1000 : :
1001 : 0 : void tcp_free_fastopen_req(struct tcp_sock *tp)
1002 : : {
1003 [ # # ][ - + ]: 109 : if (tp->fastopen_req != NULL) {
1004 : 0 : kfree(tp->fastopen_req);
1005 : 0 : tp->fastopen_req = NULL;
1006 : : }
1007 : 0 : }
1008 : :
1009 : 0 : static int tcp_sendmsg_fastopen(struct sock *sk, struct msghdr *msg, int *size)
1010 : : {
1011 : : struct tcp_sock *tp = tcp_sk(sk);
1012 : : int err, flags;
1013 : :
1014 [ # # ]: 0 : if (!(sysctl_tcp_fastopen & TFO_CLIENT_ENABLE))
1015 : : return -EOPNOTSUPP;
1016 [ # # ]: 0 : if (tp->fastopen_req != NULL)
1017 : : return -EALREADY; /* Another Fast Open is in progress */
1018 : :
1019 : 0 : tp->fastopen_req = kzalloc(sizeof(struct tcp_fastopen_request),
1020 : : sk->sk_allocation);
1021 [ # # ]: 0 : if (unlikely(tp->fastopen_req == NULL))
1022 : : return -ENOBUFS;
1023 : 0 : tp->fastopen_req->data = msg;
1024 : :
1025 [ # # ]: 0 : flags = (msg->msg_flags & MSG_DONTWAIT) ? O_NONBLOCK : 0;
1026 : 0 : err = __inet_stream_connect(sk->sk_socket, msg->msg_name,
1027 : : msg->msg_namelen, flags);
1028 : 0 : *size = tp->fastopen_req->copied;
1029 : : tcp_free_fastopen_req(tp);
1030 : 0 : return err;
1031 : : }
1032 : :
1033 : 0 : int tcp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
1034 : : size_t size)
1035 : : {
1036 : : struct iovec *iov;
1037 : : struct tcp_sock *tp = tcp_sk(sk);
1038 : 41750 : struct sk_buff *skb;
1039 : : int iovlen, flags, err, copied = 0;
1040 : 41202 : int mss_now = 0, size_goal, copied_syn = 0, offset = 0;
1041 : : bool sg;
1042 : : long timeo;
1043 : :
1044 : : lock_sock(sk);
1045 : :
1046 : 41202 : flags = msg->msg_flags;
1047 [ - + ]: 41202 : if (flags & MSG_FASTOPEN) {
1048 : 0 : err = tcp_sendmsg_fastopen(sk, msg, &copied_syn);
1049 [ # # ][ # # ]: 0 : if (err == -EINPROGRESS && copied_syn > 0)
1050 : : goto out;
1051 [ - ]: 0 : else if (err)
1052 : : goto out_err;
1053 : 0 : offset = copied_syn;
1054 : : }
1055 : :
1056 : 82404 : timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
1057 : :
1058 : : /* Wait for a connection to finish. One exception is TCP Fast Open
1059 : : * (passive side) where data is allowed to be sent before a connection
1060 : : * is fully established.
1061 : : */
1062 [ + + ][ + - ]: 41208 : if (((1 << sk->sk_state) & ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) &&
1063 : : !tcp_passive_fastopen(sk)) {
1064 [ - + ]: 6 : if ((err = sk_stream_wait_connect(sk, &timeo)) != 0)
1065 : : goto do_error;
1066 : : }
1067 : :
1068 [ - + ]: 41196 : if (unlikely(tp->repair)) {
1069 [ # # ]: 0 : if (tp->repair_queue == TCP_RECV_QUEUE) {
1070 : 0 : copied = tcp_send_rcvq(sk, msg, size);
1071 : 0 : goto out;
1072 : : }
1073 : :
1074 : : err = -EINVAL;
1075 [ # # ]: 0 : if (tp->repair_queue == TCP_NO_QUEUE)
1076 : : goto out_err;
1077 : :
1078 : : /* 'common' sending to sendq */
1079 : : }
1080 : :
1081 : : /* This should be in poll */
1082 : 41196 : clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
1083 : :
1084 : 41196 : mss_now = tcp_send_mss(sk, &size_goal, flags);
1085 : :
1086 : : /* Ok commence sending. */
1087 : 41195 : iovlen = msg->msg_iovlen;
1088 : 41195 : iov = msg->msg_iov;
1089 : : copied = 0;
1090 : :
1091 : : err = -EPIPE;
1092 [ + - ][ + ]: 41195 : if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN))
1093 : : goto out_err;
1094 : :
1095 : 41196 : sg = !!(sk->sk_route_caps & NETIF_F_SG);
1096 : :
1097 [ + - ]: 41196 : while (--iovlen >= 0) {
1098 : 41196 : size_t seglen = iov->iov_len;
1099 : 41196 : unsigned char __user *from = iov->iov_base;
1100 : :
1101 : 41196 : iov++;
1102 [ - + ]: 41196 : if (unlikely(offset > 0)) { /* Skip bytes copied in SYN */
1103 [ # # ]: 0 : if (offset >= seglen) {
1104 : 0 : offset -= seglen;
1105 : 0 : continue;
1106 : : }
1107 : 0 : seglen -= offset;
1108 : 41196 : from += offset;
1109 : : offset = 0;
1110 : : }
1111 : :
1112 [ + - ]: 41752 : while (seglen > 0) {
1113 : : int copy = 0;
1114 : 41752 : int max = size_goal;
1115 : :
1116 : : skb = tcp_write_queue_tail(sk);
1117 [ + + ]: 41752 : if (tcp_send_head(sk)) {
1118 [ + - ]: 170 : if (skb->ip_summed == CHECKSUM_NONE)
1119 : : max = mss_now;
1120 : 170 : copy = max - skb->len;
1121 : : }
1122 : :
1123 [ + + ]: 41752 : if (copy <= 0) {
1124 : : new_segment:
1125 : : /* Allocate new segment. If the interface is SG,
1126 : : * allocate skb fitting to single page.
1127 : : */
1128 [ + - ]: 41593 : if (!sk_stream_memory_free(sk))
1129 : : goto wait_for_sndbuf;
1130 : :
1131 : 41593 : skb = sk_stream_alloc_skb(sk,
1132 : : select_size(sk, sg),
1133 : : sk->sk_allocation);
1134 [ + + ]: 41594 : if (!skb)
1135 : : goto wait_for_memory;
1136 : :
1137 : : /*
1138 : : * All packets are restored as if they have
1139 : : * already been sent.
1140 : : */
1141 [ - + ]: 41593 : if (tp->repair)
1142 : 0 : TCP_SKB_CB(skb)->when = tcp_time_stamp;
1143 : :
1144 : : /*
1145 : : * Check whether we can use HW checksum.
1146 : : */
1147 [ + + ]: 41593 : if (sk->sk_route_caps & NETIF_F_ALL_CSUM)
1148 : 591 : skb->ip_summed = CHECKSUM_PARTIAL;
1149 : :
1150 : : skb_entail(sk, skb);
1151 : 41594 : copy = size_goal;
1152 : : max = size_goal;
1153 : : }
1154 : :
1155 : : /* Try to append data to the end of skb. */
1156 [ + + ]: 41752 : if (copy > seglen)
1157 : 41195 : copy = seglen;
1158 : :
1159 : : /* Where to copy to? */
1160 [ + - ]: 41752 : if (skb_availroom(skb) > 0) {
1161 : : /* We have some space in skb head. Superb! */
1162 : 41752 : copy = min_t(int, copy, skb_availroom(skb));
1163 : : err = skb_add_data_nocache(sk, skb, from, copy);
1164 [ + + ]: 41751 : if (err)
1165 : : goto do_fault;
1166 : : } else {
1167 : : bool merge = true;
1168 : 0 : int i = skb_shinfo(skb)->nr_frags;
1169 : : struct page_frag *pfrag = sk_page_frag(sk);
1170 : :
1171 [ # # ]: 0 : if (!sk_page_frag_refill(sk, pfrag))
1172 : : goto wait_for_memory;
1173 : :
1174 [ # # ]: 0 : if (!skb_can_coalesce(skb, i, pfrag->page,
1175 : 0 : pfrag->offset)) {
1176 [ # # ]: 0 : if (i == MAX_SKB_FRAGS || !sg) {
1177 : : tcp_mark_push(tp, skb);
1178 : : goto new_segment;
1179 : : }
1180 : : merge = false;
1181 : : }
1182 : :
1183 : 0 : copy = min_t(int, copy, pfrag->size - pfrag->offset);
1184 : :
1185 [ # # ]: 0 : if (!sk_wmem_schedule(sk, copy))
1186 : : goto wait_for_memory;
1187 : :
1188 : 0 : err = skb_copy_to_page_nocache(sk, from, skb,
1189 : : pfrag->page,
1190 : 0 : pfrag->offset,
1191 : : copy);
1192 [ # # ]: 0 : if (err)
1193 : : goto do_error;
1194 : :
1195 : : /* Update the skb. */
1196 [ # # ]: 0 : if (merge) {
1197 : 0 : skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy);
1198 : : } else {
1199 : 0 : skb_fill_page_desc(skb, i, pfrag->page,
1200 : 0 : pfrag->offset, copy);
1201 : 0 : get_page(pfrag->page);
1202 : : }
1203 : 0 : pfrag->offset += copy;
1204 : : }
1205 : :
1206 [ + + ]: 41750 : if (!copied)
1207 : 41195 : TCP_SKB_CB(skb)->tcp_flags &= ~TCPHDR_PSH;
1208 : :
1209 : 41750 : tp->write_seq += copy;
1210 : 41750 : TCP_SKB_CB(skb)->end_seq += copy;
1211 : 41750 : skb_shinfo(skb)->gso_segs = 0;
1212 : :
1213 : 41750 : from += copy;
1214 : 41750 : copied += copy;
1215 [ + + ][ - + ]: 41750 : if ((seglen -= copy) == 0 && iovlen == 0)
1216 : : goto out;
1217 : :
1218 [ + - ][ + - ]: 556 : if (skb->len < max || (flags & MSG_OOB) || unlikely(tp->repair))
[ - + ]
1219 : 0 : continue;
1220 : :
1221 [ - + ]: 556 : if (forced_push(tp)) {
1222 : : tcp_mark_push(tp, skb);
1223 : 0 : __tcp_push_pending_frames(sk, mss_now, TCP_NAGLE_PUSH);
1224 [ + + ]: 556 : } else if (skb == tcp_send_head(sk))
1225 : 551 : tcp_push_one(sk, mss_now);
1226 : 556 : continue;
1227 : :
1228 : : wait_for_sndbuf:
1229 : 0 : set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1230 : : wait_for_memory:
1231 [ # # ]: 0 : if (copied)
1232 : : tcp_push(sk, flags & ~MSG_MORE, mss_now, TCP_NAGLE_PUSH);
1233 : :
1234 [ # # ]: 0 : if ((err = sk_stream_wait_memory(sk, &timeo)) != 0)
1235 : : goto do_error;
1236 : :
1237 : 556 : mss_now = tcp_send_mss(sk, &size_goal, flags);
1238 : : }
1239 : : }
1240 : :
1241 : : out:
1242 [ + - ]: 41195 : if (copied)
1243 : 41195 : tcp_push(sk, flags, mss_now, tp->nonagle);
1244 : 41194 : release_sock(sk);
1245 : :
1246 : : if (copied + copied_syn)
1247 : : uid_stat_tcp_snd(current_uid(), copied + copied_syn);
1248 : 41195 : return copied + copied_syn;
1249 : :
1250 : : do_fault:
1251 [ + - ]: 1 : if (!skb->len) {
1252 : : tcp_unlink_write_queue(skb, sk);
1253 : : /* It is the one place in all of TCP, except connection
1254 : : * reset, where we can be unlinking the send_head.
1255 : : */
1256 : : tcp_check_send_head(sk, skb);
1257 : : sk_wmem_free_skb(sk, skb);
1258 : : }
1259 : :
1260 : : do_error:
1261 [ - + ]: 7 : if (copied + copied_syn)
1262 : : goto out;
1263 : : out_err:
1264 : 5 : err = sk_stream_error(sk, flags, err);
1265 : 7 : release_sock(sk);
1266 : 7 : return err;
1267 : : }
1268 : : EXPORT_SYMBOL(tcp_sendmsg);
1269 : :
1270 : : /*
1271 : : * Handle reading urgent data. BSD has very simple semantics for
1272 : : * this, no blocking and very strange errors 8)
1273 : : */
1274 : :
1275 : 5 : static int tcp_recv_urg(struct sock *sk, struct msghdr *msg, int len, int flags)
1276 : : {
1277 : : struct tcp_sock *tp = tcp_sk(sk);
1278 : :
1279 : : /* No URG data to read. */
1280 [ + - ][ - + ]: 5 : if (sock_flag(sk, SOCK_URGINLINE) || !tp->urg_data ||
[ # # ]
1281 : : tp->urg_data == TCP_URG_READ)
1282 : : return -EINVAL; /* Yes this is right ! */
1283 : :
1284 [ # # ][ # # ]: 0 : if (sk->sk_state == TCP_CLOSE && !sock_flag(sk, SOCK_DONE))
1285 : : return -ENOTCONN;
1286 : :
1287 [ # # ]: 5 : if (tp->urg_data & TCP_URG_VALID) {
1288 : : int err = 0;
1289 : 0 : char c = tp->urg_data;
1290 : :
1291 [ # # ]: 0 : if (!(flags & MSG_PEEK))
1292 : 0 : tp->urg_data = TCP_URG_READ;
1293 : :
1294 : : /* Read urgent data. */
1295 : 0 : msg->msg_flags |= MSG_OOB;
1296 : :
1297 [ # # ]: 0 : if (len > 0) {
1298 [ # # ]: 0 : if (!(flags & MSG_TRUNC))
1299 : 0 : err = memcpy_toiovec(msg->msg_iov, &c, 1);
1300 : : len = 1;
1301 : : } else
1302 : 0 : msg->msg_flags |= MSG_TRUNC;
1303 : :
1304 [ # # ]: 0 : return err ? -EFAULT : len;
1305 : : }
1306 : :
1307 [ # # ][ # # ]: 0 : if (sk->sk_state == TCP_CLOSE || (sk->sk_shutdown & RCV_SHUTDOWN))
1308 : : return 0;
1309 : :
1310 : : /* Fixed the recv(..., MSG_OOB) behaviour. BSD docs and
1311 : : * the available implementations agree in this case:
1312 : : * this call should never block, independent of the
1313 : : * blocking state of the socket.
1314 : : * Mike <pall@rz.uni-karlsruhe.de>
1315 : : */
1316 : : return -EAGAIN;
1317 : : }
1318 : :
1319 : 0 : static int tcp_peek_sndq(struct sock *sk, struct msghdr *msg, int len)
1320 : : {
1321 : : struct sk_buff *skb;
1322 : : int copied = 0, err = 0;
1323 : :
1324 : : /* XXX -- need to support SO_PEEK_OFF */
1325 : :
1326 [ # # ]: 0 : skb_queue_walk(&sk->sk_write_queue, skb) {
1327 : 0 : err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, skb->len);
1328 [ # # ]: 0 : if (err)
1329 : : break;
1330 : :
1331 : 0 : copied += skb->len;
1332 : : }
1333 : :
1334 [ # # ]: 0 : return err ?: copied;
1335 : : }
1336 : :
1337 : : /* Clean up the receive buffer for full frames taken by the user,
1338 : : * then send an ACK if necessary. COPIED is the number of bytes
1339 : : * tcp_recvmsg has given to the user so far, it speeds up the
1340 : : * calculation of whether or not we must ACK for the sake of
1341 : : * a window update.
1342 : : */
1343 : 0 : void tcp_cleanup_rbuf(struct sock *sk, int copied)
1344 : : {
1345 : : struct tcp_sock *tp = tcp_sk(sk);
1346 : : bool time_to_ack = false;
1347 : :
1348 : 26648 : struct sk_buff *skb = skb_peek(&sk->sk_receive_queue);
1349 : :
1350 [ + + ][ + - ]: 26648 : WARN(skb && !before(tp->copied_seq, TCP_SKB_CB(skb)->end_seq),
[ - + ]
1351 : : "cleanup rbuf bug: copied %X seq %X rcvnxt %X\n",
1352 : : tp->copied_seq, TCP_SKB_CB(skb)->end_seq, tp->rcv_nxt);
1353 : :
1354 [ + + ]: 53296 : if (inet_csk_ack_scheduled(sk)) {
1355 : : const struct inet_connection_sock *icsk = inet_csk(sk);
1356 : : /* Delayed ACKs frequently hit locked sockets during bulk
1357 : : * receive. */
1358 [ + - ][ + + ]: 21465 : if (icsk->icsk_ack.blocked ||
1359 : : /* Once-per-two-segments ACK was not sent by tcp_input.c */
1360 [ + + ]: 19758 : tp->rcv_nxt - tp->rcv_wup > icsk->icsk_ack.rcv_mss ||
1361 : : /*
1362 : : * If this read emptied read buffer, we send ACK, if
1363 : : * connection is not bidirectional, user drained
1364 : : * receive buffer and there was a small segment
1365 : : * in queue.
1366 : : */
1367 [ + + ]: 19700 : (copied > 0 &&
1368 [ + + ]: 19699 : ((icsk->icsk_ack.pending & ICSK_ACK_PUSHED2) ||
1369 [ + + ]: 19388 : ((icsk->icsk_ack.pending & ICSK_ACK_PUSHED) &&
1370 [ + - ]: 1710 : !icsk->icsk_ack.pingpong)) &&
1371 : 3 : !atomic_read(&sk->sk_rmem_alloc)))
1372 : : time_to_ack = true;
1373 : : }
1374 : :
1375 : : /* We send an ACK if we can now advertise a non-zero window
1376 : : * which has been raised "significantly".
1377 : : *
1378 : : * Even if window raised up to infinity, do not send window open ACK
1379 : : * in states, where we will not receive more. It is useless.
1380 : : */
1381 [ + + ][ + + ]: 26648 : if (copied > 0 && !time_to_ack && !(sk->sk_shutdown & RCV_SHUTDOWN)) {
1382 : : __u32 rcv_window_now = tcp_receive_window(tp);
1383 : :
1384 : : /* Optimize, __tcp_select_window() is not cheap. */
1385 [ + + ]: 23483 : if (2*rcv_window_now <= tp->window_clamp) {
1386 : 19649 : __u32 new_window = __tcp_select_window(sk);
1387 : :
1388 : : /* Send ACK now, if this read freed lots of space
1389 : : * in our buffer. Certainly, new_window is new window.
1390 : : * We can advertise it now, if it is not less than current one.
1391 : : * "Lots" means "at least twice" here.
1392 : : */
1393 [ + - ][ - + ]: 19649 : if (new_window && new_window >= 2 * rcv_window_now)
1394 : : time_to_ack = true;
1395 : : }
1396 : : }
1397 [ + + ]: 26648 : if (time_to_ack)
1398 : 1710 : tcp_send_ack(sk);
1399 : 26648 : }
1400 : :
1401 : 0 : static void tcp_prequeue_process(struct sock *sk)
1402 : : {
1403 : : struct sk_buff *skb;
1404 : : struct tcp_sock *tp = tcp_sk(sk);
1405 : :
1406 : 1818 : NET_INC_STATS_USER(sock_net(sk), LINUX_MIB_TCPPREQUEUED);
1407 : :
1408 : : /* RX process wants to run with disabled BHs, though it is not
1409 : : * necessary */
1410 : 909 : local_bh_disable();
1411 [ + + ]: 4899 : while ((skb = __skb_dequeue(&tp->ucopy.prequeue)) != NULL)
1412 : : sk_backlog_rcv(sk, skb);
1413 : 909 : local_bh_enable();
1414 : :
1415 : : /* Clear memory counter. */
1416 : 909 : tp->ucopy.memory = 0;
1417 : 909 : }
1418 : :
1419 : : #ifdef CONFIG_NET_DMA
1420 : : static void tcp_service_net_dma(struct sock *sk, bool wait)
1421 : : {
1422 : : dma_cookie_t done, used;
1423 : : dma_cookie_t last_issued;
1424 : : struct tcp_sock *tp = tcp_sk(sk);
1425 : :
1426 : : if (!tp->ucopy.dma_chan)
1427 : : return;
1428 : :
1429 : : last_issued = tp->ucopy.dma_cookie;
1430 : : dma_async_issue_pending(tp->ucopy.dma_chan);
1431 : :
1432 : : do {
1433 : : if (dma_async_is_tx_complete(tp->ucopy.dma_chan,
1434 : : last_issued, &done,
1435 : : &used) == DMA_COMPLETE) {
1436 : : /* Safe to free early-copied skbs now */
1437 : : __skb_queue_purge(&sk->sk_async_wait_queue);
1438 : : break;
1439 : : } else {
1440 : : struct sk_buff *skb;
1441 : : while ((skb = skb_peek(&sk->sk_async_wait_queue)) &&
1442 : : (dma_async_is_complete(skb->dma_cookie, done,
1443 : : used) == DMA_COMPLETE)) {
1444 : : __skb_dequeue(&sk->sk_async_wait_queue);
1445 : : kfree_skb(skb);
1446 : : }
1447 : : }
1448 : : } while (wait);
1449 : : }
1450 : : #endif
1451 : :
1452 : 0 : static struct sk_buff *tcp_recv_skb(struct sock *sk, u32 seq, u32 *off)
1453 : : {
1454 : : struct sk_buff *skb;
1455 : : u32 offset;
1456 : :
1457 [ # # ]: 0 : while ((skb = skb_peek(&sk->sk_receive_queue)) != NULL) {
1458 : 0 : offset = seq - TCP_SKB_CB(skb)->seq;
1459 [ # # ]: 0 : if (tcp_hdr(skb)->syn)
1460 : 0 : offset--;
1461 [ # # ][ # # ]: 0 : if (offset < skb->len || tcp_hdr(skb)->fin) {
1462 : 0 : *off = offset;
1463 : 0 : return skb;
1464 : : }
1465 : : /* This looks weird, but this can happen if TCP collapsing
1466 : : * splitted a fat GRO packet, while we released socket lock
1467 : : * in skb_splice_bits()
1468 : : */
1469 : : sk_eat_skb(sk, skb, false);
1470 : : }
1471 : : return NULL;
1472 : : }
1473 : :
1474 : : /*
1475 : : * This routine provides an alternative to tcp_recvmsg() for routines
1476 : : * that would like to handle copying from skbuffs directly in 'sendfile'
1477 : : * fashion.
1478 : : * Note:
1479 : : * - It is assumed that the socket was locked by the caller.
1480 : : * - The routine does not block.
1481 : : * - At present, there is no support for reading OOB data
1482 : : * or for 'peeking' the socket using this routine
1483 : : * (although both would be easy to implement).
1484 : : */
1485 : 0 : int tcp_read_sock(struct sock *sk, read_descriptor_t *desc,
1486 : : sk_read_actor_t recv_actor)
1487 : : {
1488 : : struct sk_buff *skb;
1489 : : struct tcp_sock *tp = tcp_sk(sk);
1490 : 0 : u32 seq = tp->copied_seq;
1491 : : u32 offset;
1492 : : int copied = 0;
1493 : :
1494 [ # # ]: 0 : if (sk->sk_state == TCP_LISTEN)
1495 : : return -ENOTCONN;
1496 [ # # ]: 0 : while ((skb = tcp_recv_skb(sk, seq, &offset)) != NULL) {
1497 [ # # ]: 0 : if (offset < skb->len) {
1498 : : int used;
1499 : : size_t len;
1500 : :
1501 : 0 : len = skb->len - offset;
1502 : : /* Stop reading if we hit a patch of urgent data */
1503 [ # # ]: 0 : if (tp->urg_data) {
1504 : 0 : u32 urg_offset = tp->urg_seq - seq;
1505 [ # # ]: 0 : if (urg_offset < len)
1506 : : len = urg_offset;
1507 [ # # ]: 0 : if (!len)
1508 : : break;
1509 : : }
1510 : 0 : used = recv_actor(desc, skb, offset, len);
1511 [ # # ]: 0 : if (used <= 0) {
1512 [ # # ]: 0 : if (!copied)
1513 : : copied = used;
1514 : : break;
1515 [ # # ]: 0 : } else if (used <= len) {
1516 : 0 : seq += used;
1517 : 0 : copied += used;
1518 : 0 : offset += used;
1519 : : }
1520 : : /* If recv_actor drops the lock (e.g. TCP splice
1521 : : * receive) the skb pointer might be invalid when
1522 : : * getting here: tcp_collapse might have deleted it
1523 : : * while aggregating skbs from the socket queue.
1524 : : */
1525 : 0 : skb = tcp_recv_skb(sk, seq - 1, &offset);
1526 [ # # ]: 0 : if (!skb)
1527 : : break;
1528 : : /* TCP coalescing might have appended data to the skb.
1529 : : * Try to splice more frags
1530 : : */
1531 [ # # ]: 0 : if (offset + 1 != skb->len)
1532 : 0 : continue;
1533 : : }
1534 [ # # ]: 0 : if (tcp_hdr(skb)->fin) {
1535 : : sk_eat_skb(sk, skb, false);
1536 : 0 : ++seq;
1537 : 0 : break;
1538 : : }
1539 : : sk_eat_skb(sk, skb, false);
1540 [ # # ]: 0 : if (!desc->count)
1541 : : break;
1542 : 0 : tp->copied_seq = seq;
1543 : : }
1544 : 0 : tp->copied_seq = seq;
1545 : :
1546 : 0 : tcp_rcv_space_adjust(sk);
1547 : :
1548 : : /* Clean up data we have read: This will do ACK frames. */
1549 [ # # ]: 0 : if (copied > 0) {
1550 : 0 : tcp_recv_skb(sk, seq, &offset);
1551 : 0 : tcp_cleanup_rbuf(sk, copied);
1552 : : uid_stat_tcp_rcv(current_uid(), copied);
1553 : : }
1554 : 0 : return copied;
1555 : : }
1556 : : EXPORT_SYMBOL(tcp_read_sock);
1557 : :
1558 : : /*
1559 : : * This routine copies from a sock struct into the user buffer.
1560 : : *
1561 : : * Technical note: in 2.3 we work on _locked_ socket, so that
1562 : : * tricks with *seq access order and skb->users are not required.
1563 : : * Probably, code can be easily improved even more.
1564 : : */
1565 : :
1566 : 0 : int tcp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
1567 : : size_t len, int nonblock, int flags, int *addr_len)
1568 : : {
1569 : : struct tcp_sock *tp = tcp_sk(sk);
1570 : : int copied = 0;
1571 : : u32 peek_seq;
1572 : : u32 *seq;
1573 : : unsigned long used;
1574 : : int err;
1575 : : int target; /* Read at least this many bytes */
1576 : : long timeo;
1577 : : struct task_struct *user_recv = NULL;
1578 : : bool copied_early = false;
1579 : : struct sk_buff *skb;
1580 : : u32 urg_hole = 0;
1581 : :
1582 [ - + ][ # # ]: 25691 : if (sk_can_busy_loop(sk) && skb_queue_empty(&sk->sk_receive_queue) &&
[ # # ]
1583 : 0 : (sk->sk_state == TCP_ESTABLISHED))
1584 : : sk_busy_loop(sk, nonblock);
1585 : :
1586 : : lock_sock(sk);
1587 : :
1588 : : err = -ENOTCONN;
1589 [ + - ]: 25691 : if (sk->sk_state == TCP_LISTEN)
1590 : : goto out;
1591 : :
1592 : 25691 : timeo = sock_rcvtimeo(sk, nonblock);
1593 : :
1594 : : /* Urgent data needs to be handled specially. */
1595 [ + + ]: 25691 : if (flags & MSG_OOB)
1596 : : goto recv_urg;
1597 : :
1598 [ - + ]: 25686 : if (unlikely(tp->repair)) {
1599 : : err = -EPERM;
1600 [ # # ]: 0 : if (!(flags & MSG_PEEK))
1601 : : goto out;
1602 : :
1603 [ # # ]: 0 : if (tp->repair_queue == TCP_SEND_QUEUE)
1604 : : goto recv_sndq;
1605 : :
1606 : : err = -EINVAL;
1607 [ # # ]: 0 : if (tp->repair_queue == TCP_NO_QUEUE)
1608 : : goto out;
1609 : :
1610 : : /* 'common' recv queue MSG_PEEK-ing */
1611 : : }
1612 : :
1613 : 25686 : seq = &tp->copied_seq;
1614 [ + + ]: 25686 : if (flags & MSG_PEEK) {
1615 : 1 : peek_seq = tp->copied_seq;
1616 : : seq = &peek_seq;
1617 : : }
1618 : :
1619 : 25686 : target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
1620 : :
1621 : : #ifdef CONFIG_NET_DMA
1622 : : tp->ucopy.dma_chan = NULL;
1623 : : preempt_disable();
1624 : : skb = skb_peek_tail(&sk->sk_receive_queue);
1625 : : {
1626 : : int available = 0;
1627 : :
1628 : : if (skb)
1629 : : available = TCP_SKB_CB(skb)->seq + skb->len - (*seq);
1630 : : if ((available < target) &&
1631 : : (len > sysctl_tcp_dma_copybreak) && !(flags & MSG_PEEK) &&
1632 : : !sysctl_tcp_low_latency &&
1633 : : net_dma_find_channel()) {
1634 : : preempt_enable_no_resched();
1635 : : tp->ucopy.pinned_list =
1636 : : dma_pin_iovec_pages(msg->msg_iov, len);
1637 : : } else {
1638 : : preempt_enable_no_resched();
1639 : : }
1640 : : }
1641 : : #endif
1642 : :
1643 : : do {
1644 : : u32 offset;
1645 : :
1646 : : /* Are we at urgent data? Stop if we have read anything or have SIGURG pending. */
1647 [ - + ][ # # ]: 30954 : if (tp->urg_data && tp->urg_seq == *seq) {
1648 [ # # ]: 0 : if (copied)
1649 : : break;
1650 [ # # ]: 0 : if (signal_pending(current)) {
1651 [ # # ]: 0 : copied = timeo ? sock_intr_errno(timeo) : -EAGAIN;
1652 : 0 : break;
1653 : : }
1654 : : }
1655 : :
1656 : : /* Next get a buffer. */
1657 : :
1658 [ + + ]: 30954 : skb_queue_walk(&sk->sk_receive_queue, skb) {
1659 : : /* Now that we have two receive queues this
1660 : : * shouldn't happen.
1661 : : */
1662 [ - + ][ + - ]: 24867 : if (WARN(before(*seq, TCP_SKB_CB(skb)->seq),
1663 : : "recvmsg bug: copied %X seq %X rcvnxt %X fl %X\n",
1664 : : *seq, TCP_SKB_CB(skb)->seq, tp->rcv_nxt,
1665 : : flags))
1666 : : break;
1667 : :
1668 : 24867 : offset = *seq - TCP_SKB_CB(skb)->seq;
1669 [ - + ]: 24867 : if (tcp_hdr(skb)->syn)
1670 : 0 : offset--;
1671 [ + + ]: 24867 : if (offset < skb->len)
1672 : : goto found_ok_skb;
1673 [ - + ]: 9 : if (tcp_hdr(skb)->fin)
1674 : : goto found_fin_ok;
1675 [ # # ]: 0 : WARN(!(flags & MSG_PEEK),
1676 : : "recvmsg bug 2: copied %X seq %X rcvnxt %X fl %X\n",
1677 : : *seq, TCP_SKB_CB(skb)->seq, tp->rcv_nxt, flags);
1678 : : }
1679 : :
1680 : : /* Well, if we have backlog, try to process it now yet. */
1681 : :
1682 [ + + ][ + + ]: 6087 : if (copied >= target && !sk->sk_backlog.tail)
1683 : : break;
1684 : :
1685 [ + + ]: 1496 : if (copied) {
1686 [ + - ][ + - ]: 54 : if (sk->sk_err ||
1687 [ + - ]: 54 : sk->sk_state == TCP_CLOSE ||
1688 [ + + ]: 54 : (sk->sk_shutdown & RCV_SHUTDOWN) ||
1689 [ + - ]: 47 : !timeo ||
1690 : 47 : signal_pending(current))
1691 : : break;
1692 : : } else {
1693 [ + - ]: 1442 : if (sock_flag(sk, SOCK_DONE))
1694 : : break;
1695 : :
1696 [ + + ]: 1442 : if (sk->sk_err) {
1697 : : copied = sock_error(sk);
1698 : 9 : break;
1699 : : }
1700 : :
1701 [ + - ]: 1433 : if (sk->sk_shutdown & RCV_SHUTDOWN)
1702 : : break;
1703 : :
1704 [ - + ]: 1433 : if (sk->sk_state == TCP_CLOSE) {
1705 [ # # ]: 0 : if (!sock_flag(sk, SOCK_DONE)) {
1706 : : /* This occurs when user tries to read
1707 : : * from never connected socket.
1708 : : */
1709 : : copied = -ENOTCONN;
1710 : 0 : break;
1711 : : }
1712 : : break;
1713 : : }
1714 : :
1715 [ + + ]: 1433 : if (!timeo) {
1716 : : copied = -EAGAIN;
1717 : : break;
1718 : : }
1719 : :
1720 [ - + ]: 915 : if (signal_pending(current)) {
1721 : : copied = sock_intr_errno(timeo);
1722 : 0 : break;
1723 : : }
1724 : : }
1725 : :
1726 : 962 : tcp_cleanup_rbuf(sk, copied);
1727 : :
1728 [ + - ][ + - ]: 962 : if (!sysctl_tcp_low_latency && tp->ucopy.task == user_recv) {
1729 : : /* Install new reader */
1730 [ + + ][ + - ]: 962 : if (!user_recv && !(flags & (MSG_TRUNC | MSG_PEEK))) {
1731 : 910 : user_recv = current;
1732 : 910 : tp->ucopy.task = user_recv;
1733 : 910 : tp->ucopy.iov = msg->msg_iov;
1734 : : }
1735 : :
1736 : 962 : tp->ucopy.len = len;
1737 : :
1738 [ - + ][ # # ]: 962 : WARN_ON(tp->copied_seq != tp->rcv_nxt &&
[ - + ]
1739 : : !(flags & (MSG_PEEK | MSG_TRUNC)));
1740 : :
1741 : : /* Ugly... If prequeue is not empty, we have to
1742 : : * process it before releasing socket, otherwise
1743 : : * order will be broken at second iteration.
1744 : : * More elegant solution is required!!!
1745 : : *
1746 : : * Look: we have the following (pseudo)queues:
1747 : : *
1748 : : * 1. packets in flight
1749 : : * 2. backlog
1750 : : * 3. prequeue
1751 : : * 4. receive_queue
1752 : : *
1753 : : * Each queue can be processed only if the next ones
1754 : : * are empty. At this point we have empty receive_queue.
1755 : : * But prequeue _can_ be not empty after 2nd iteration,
1756 : : * when we jumped to start of loop because backlog
1757 : : * processing added something to receive_queue.
1758 : : * We cannot release_sock(), because backlog contains
1759 : : * packets arrived _after_ prequeued ones.
1760 : : *
1761 : : * Shortly, algorithm is clear --- to process all
1762 : : * the queues in order. We could make it more directly,
1763 : : * requeueing packets from backlog to prequeue, if
1764 : : * is not empty. It is more elegant, but eats cycles,
1765 : : * unfortunately.
1766 : : */
1767 [ + - ]: 962 : if (!skb_queue_empty(&tp->ucopy.prequeue))
1768 : : goto do_prequeue;
1769 : :
1770 : : /* __ Set realtime policy in scheduler __ */
1771 : : }
1772 : :
1773 : : #ifdef CONFIG_NET_DMA
1774 : : if (tp->ucopy.dma_chan) {
1775 : : if (tp->rcv_wnd == 0 &&
1776 : : !skb_queue_empty(&sk->sk_async_wait_queue)) {
1777 : : tcp_service_net_dma(sk, true);
1778 : : tcp_cleanup_rbuf(sk, copied);
1779 : : } else
1780 : : dma_async_issue_pending(tp->ucopy.dma_chan);
1781 : : }
1782 : : #endif
1783 [ + + ]: 962 : if (copied >= target) {
1784 : : /* Do not sleep, just process backlog. */
1785 : 47 : release_sock(sk);
1786 : : lock_sock(sk);
1787 : : } else
1788 : 915 : sk_wait_data(sk, &timeo);
1789 : :
1790 : : #ifdef CONFIG_NET_DMA
1791 : : tcp_service_net_dma(sk, false); /* Don't block */
1792 : : tp->ucopy.wakeup = 0;
1793 : : #endif
1794 : :
1795 [ + - ]: 962 : if (user_recv) {
1796 : : int chunk;
1797 : :
1798 : : /* __ Restore normal policy in scheduler __ */
1799 : :
1800 [ + + ]: 962 : if ((chunk = len - tp->ucopy.len) != 0) {
1801 : 94 : NET_ADD_STATS_USER(sock_net(sk), LINUX_MIB_TCPDIRECTCOPYFROMBACKLOG, chunk);
1802 : 47 : len -= chunk;
1803 : 47 : copied += chunk;
1804 : : }
1805 : :
1806 [ + + ][ + + ]: 962 : if (tp->rcv_nxt == tp->copied_seq &&
1807 : 931 : !skb_queue_empty(&tp->ucopy.prequeue)) {
1808 : : do_prequeue:
1809 : 909 : tcp_prequeue_process(sk);
1810 : :
1811 [ + + ]: 909 : if ((chunk = len - tp->ucopy.len) != 0) {
1812 : 1714 : NET_ADD_STATS_USER(sock_net(sk), LINUX_MIB_TCPDIRECTCOPYFROMPREQUEUE, chunk);
1813 : 857 : len -= chunk;
1814 : 857 : copied += chunk;
1815 : : }
1816 : : }
1817 : : }
1818 [ - + ][ # # ]: 962 : if ((flags & MSG_PEEK) &&
1819 : 0 : (peek_seq - copied - urg_hole != tp->copied_seq)) {
1820 : 0 : net_dbg_ratelimited("TCP(%s:%d): Application bug, race in MSG_PEEK\n",
1821 : : current->comm,
1822 : : task_pid_nr(current));
1823 : 0 : peek_seq = tp->copied_seq;
1824 : : }
1825 : 962 : continue;
1826 : :
1827 : : found_ok_skb:
1828 : : /* Ok so how much can we use? */
1829 : 24858 : used = skb->len - offset;
1830 [ + + ]: 24858 : if (len < used)
1831 : : used = len;
1832 : :
1833 : : /* Do we have urgent data here? */
1834 [ - + ]: 24858 : if (tp->urg_data) {
1835 : 0 : u32 urg_offset = tp->urg_seq - *seq;
1836 [ # # ]: 0 : if (urg_offset < used) {
1837 [ # # ]: 0 : if (!urg_offset) {
1838 [ # # ]: 0 : if (!sock_flag(sk, SOCK_URGINLINE)) {
1839 : 0 : ++*seq;
1840 : 0 : urg_hole++;
1841 : 0 : offset++;
1842 : 0 : used--;
1843 [ # # ]: 0 : if (!used)
1844 : : goto skip_copy;
1845 : : }
1846 : : } else
1847 : : used = urg_offset;
1848 : : }
1849 : : }
1850 : :
1851 [ + - ]: 24858 : if (!(flags & MSG_TRUNC)) {
1852 : : #ifdef CONFIG_NET_DMA
1853 : : if (!tp->ucopy.dma_chan && tp->ucopy.pinned_list)
1854 : : tp->ucopy.dma_chan = net_dma_find_channel();
1855 : :
1856 : : if (tp->ucopy.dma_chan) {
1857 : : tp->ucopy.dma_cookie = dma_skb_copy_datagram_iovec(
1858 : : tp->ucopy.dma_chan, skb, offset,
1859 : : msg->msg_iov, used,
1860 : : tp->ucopy.pinned_list);
1861 : :
1862 : : if (tp->ucopy.dma_cookie < 0) {
1863 : :
1864 : : pr_alert("%s: dma_cookie < 0\n",
1865 : : __func__);
1866 : :
1867 : : /* Exception. Bailout! */
1868 : : if (!copied)
1869 : : copied = -EFAULT;
1870 : : break;
1871 : : }
1872 : :
1873 : : dma_async_issue_pending(tp->ucopy.dma_chan);
1874 : :
1875 : : if ((offset + used) == skb->len)
1876 : : copied_early = true;
1877 : :
1878 : : } else
1879 : : #endif
1880 : : {
1881 : 24858 : err = skb_copy_datagram_iovec(skb, offset,
1882 : : msg->msg_iov, used);
1883 [ + + ]: 24858 : if (err) {
1884 : : /* Exception. Bailout! */
1885 [ + - ]: 3 : if (!copied)
1886 : : copied = -EFAULT;
1887 : : break;
1888 : : }
1889 : : }
1890 : : }
1891 : :
1892 : 24855 : *seq += used;
1893 : 24855 : copied += used;
1894 : 24855 : len -= used;
1895 : :
1896 : 24855 : tcp_rcv_space_adjust(sk);
1897 : :
1898 : : skip_copy:
1899 [ - + ][ # # ]: 24855 : if (tp->urg_data && after(tp->copied_seq, tp->urg_seq)) {
1900 : 0 : tp->urg_data = 0;
1901 : : tcp_fast_path_check(sk);
1902 : : }
1903 [ + + ]: 24855 : if (used + offset < skb->len)
1904 : 19297 : continue;
1905 : :
1906 [ + - ]: 5558 : if (tcp_hdr(skb)->fin)
1907 : : goto found_fin_ok;
1908 [ + - ]: 5558 : if (!(flags & MSG_PEEK)) {
1909 : : sk_eat_skb(sk, skb, copied_early);
1910 : : copied_early = false;
1911 : : }
1912 : 5558 : continue;
1913 : :
1914 : : found_fin_ok:
1915 : : /* Process the FIN. */
1916 : 9 : ++*seq;
1917 [ + - ]: 9 : if (!(flags & MSG_PEEK)) {
1918 : : sk_eat_skb(sk, skb, copied_early);
1919 : : copied_early = false;
1920 : : }
1921 : : break;
1922 [ + + ]: 25817 : } while (len > 0);
1923 : :
1924 [ + + ]: 25686 : if (user_recv) {
1925 [ - + ]: 910 : if (!skb_queue_empty(&tp->ucopy.prequeue)) {
1926 : : int chunk;
1927 : :
1928 [ # # ]: 0 : tp->ucopy.len = copied > 0 ? len : 0;
1929 : :
1930 : 0 : tcp_prequeue_process(sk);
1931 : :
1932 [ # # ][ # # ]: 0 : if (copied > 0 && (chunk = len - tp->ucopy.len) != 0) {
1933 : 0 : NET_ADD_STATS_USER(sock_net(sk), LINUX_MIB_TCPDIRECTCOPYFROMPREQUEUE, chunk);
1934 : : len -= chunk;
1935 : 0 : copied += chunk;
1936 : : }
1937 : : }
1938 : :
1939 : 910 : tp->ucopy.task = NULL;
1940 : 910 : tp->ucopy.len = 0;
1941 : : }
1942 : :
1943 : : #ifdef CONFIG_NET_DMA
1944 : : tcp_service_net_dma(sk, true); /* Wait for queue to drain */
1945 : : tp->ucopy.dma_chan = NULL;
1946 : :
1947 : : if (tp->ucopy.pinned_list) {
1948 : : dma_unpin_iovec_pages(tp->ucopy.pinned_list);
1949 : : tp->ucopy.pinned_list = NULL;
1950 : : }
1951 : : #endif
1952 : :
1953 : : /* According to UNIX98, msg_name/msg_namelen are ignored
1954 : : * on connected socket. I was just happy when found this 8) --ANK
1955 : : */
1956 : :
1957 : : /* Clean up data we have read: This will do ACK frames. */
1958 : 25686 : tcp_cleanup_rbuf(sk, copied);
1959 : :
1960 : 25686 : release_sock(sk);
1961 : :
1962 : : if (copied > 0)
1963 : : uid_stat_tcp_rcv(current_uid(), copied);
1964 : 25686 : return copied;
1965 : :
1966 : : out:
1967 : 5 : release_sock(sk);
1968 : 5 : return err;
1969 : :
1970 : : recv_urg:
1971 : 5 : err = tcp_recv_urg(sk, msg, len, flags);
1972 : : if (err > 0)
1973 : : uid_stat_tcp_rcv(current_uid(), err);
1974 : 5 : goto out;
1975 : :
1976 : : recv_sndq:
1977 : 0 : err = tcp_peek_sndq(sk, msg, len);
1978 : 0 : goto out;
1979 : : }
1980 : : EXPORT_SYMBOL(tcp_recvmsg);
1981 : :
1982 : 0 : void tcp_set_state(struct sock *sk, int state)
1983 : : {
1984 : 221 : int oldstate = sk->sk_state;
1985 : :
1986 [ + + + ]: 221 : switch (state) {
1987 : : case TCP_ESTABLISHED:
1988 [ + - ]: 57 : if (oldstate != TCP_ESTABLISHED)
1989 : 114 : TCP_INC_STATS(sock_net(sk), TCP_MIB_CURRESTAB);
1990 : : break;
1991 : :
1992 : : case TCP_CLOSE:
1993 [ + + ]: 65 : if (oldstate == TCP_CLOSE_WAIT || oldstate == TCP_ESTABLISHED)
1994 : 44 : TCP_INC_STATS(sock_net(sk), TCP_MIB_ESTABRESETS);
1995 : :
1996 : 65 : sk->sk_prot->unhash(sk);
1997 [ + - ][ + + ]: 65 : if (inet_csk(sk)->icsk_bind_hash &&
1998 : 65 : !(sk->sk_userlocks & SOCK_BINDPORT_LOCK))
1999 : 56 : inet_put_port(sk);
2000 : : /* fall through */
2001 : : default:
2002 [ + + ]: 164 : if (oldstate == TCP_ESTABLISHED)
2003 : 110 : TCP_DEC_STATS(sock_net(sk), TCP_MIB_CURRESTAB);
2004 : : }
2005 : :
2006 : : /* Change state AFTER socket is unhashed to avoid closed
2007 : : * socket sitting in hash tables.
2008 : : */
2009 : 221 : sk->sk_state = state;
2010 : :
2011 : : #ifdef STATE_TRACE
2012 : : SOCK_DEBUG(sk, "TCP sk=%p, State %s -> %s\n", sk, statename[oldstate], statename[state]);
2013 : : #endif
2014 : 221 : }
2015 : : EXPORT_SYMBOL_GPL(tcp_set_state);
2016 : :
2017 : : /*
2018 : : * State processing on a close. This implements the state shift for
2019 : : * sending our FIN frame. Note that we only send a FIN for some
2020 : : * states. A shutdown() may have already sent the FIN, or we may be
2021 : : * closed.
2022 : : */
2023 : :
2024 : : static const unsigned char new_state[16] = {
2025 : : /* current state: new state: action: */
2026 : : /* (Invalid) */ TCP_CLOSE,
2027 : : /* TCP_ESTABLISHED */ TCP_FIN_WAIT1 | TCP_ACTION_FIN,
2028 : : /* TCP_SYN_SENT */ TCP_CLOSE,
2029 : : /* TCP_SYN_RECV */ TCP_FIN_WAIT1 | TCP_ACTION_FIN,
2030 : : /* TCP_FIN_WAIT1 */ TCP_FIN_WAIT1,
2031 : : /* TCP_FIN_WAIT2 */ TCP_FIN_WAIT2,
2032 : : /* TCP_TIME_WAIT */ TCP_CLOSE,
2033 : : /* TCP_CLOSE */ TCP_CLOSE,
2034 : : /* TCP_CLOSE_WAIT */ TCP_LAST_ACK | TCP_ACTION_FIN,
2035 : : /* TCP_LAST_ACK */ TCP_LAST_ACK,
2036 : : /* TCP_LISTEN */ TCP_CLOSE,
2037 : : /* TCP_CLOSING */ TCP_CLOSING,
2038 : : };
2039 : :
2040 : : static int tcp_close_state(struct sock *sk)
2041 : : {
2042 : 36 : int next = (int)new_state[sk->sk_state];
2043 : 36 : int ns = next & TCP_STATE_MASK;
2044 : :
2045 : 36 : tcp_set_state(sk, ns);
2046 : :
2047 : 36 : return next & TCP_ACTION_FIN;
2048 : : }
2049 : :
2050 : : /*
2051 : : * Shutdown the sending side of a connection. Much like close except
2052 : : * that we don't receive shut down or sock_set_flag(sk, SOCK_DEAD).
2053 : : */
2054 : :
2055 : 0 : void tcp_shutdown(struct sock *sk, int how)
2056 : : {
2057 : : /* We need to grab some memory, and put together a FIN,
2058 : : * and then put it into the queue to be sent.
2059 : : * Tim MacKenzie(tym@dibbler.cs.monash.edu.au) 4 Dec '92.
2060 : : */
2061 [ + - ]: 3 : if (!(how & SEND_SHUTDOWN))
2062 : 0 : return;
2063 : :
2064 : : /* If we've already sent a FIN, or it's a closed state, skip this. */
2065 [ + - ]: 3 : if ((1 << sk->sk_state) &
2066 : : (TCPF_ESTABLISHED | TCPF_SYN_SENT |
2067 : : TCPF_SYN_RECV | TCPF_CLOSE_WAIT)) {
2068 : : /* Clear out any half completed packets. FIN if needed. */
2069 [ + - ]: 3 : if (tcp_close_state(sk))
2070 : 3 : tcp_send_fin(sk);
2071 : : }
2072 : : }
2073 : : EXPORT_SYMBOL(tcp_shutdown);
2074 : :
2075 : 0 : bool tcp_check_oom(struct sock *sk, int shift)
2076 : : {
2077 : : bool too_many_orphans, out_of_socket_memory;
2078 : :
2079 : : too_many_orphans = tcp_too_many_orphans(sk, shift);
2080 : : out_of_socket_memory = tcp_out_of_memory(sk);
2081 : :
2082 [ # # ]: 19 : if (too_many_orphans)
2083 [ # # ]: 0 : net_info_ratelimited("too many orphaned sockets\n");
2084 [ - + ]: 19 : if (out_of_socket_memory)
2085 [ # # ]: 0 : net_info_ratelimited("out of memory -- consider tuning tcp_mem\n");
2086 : 19 : return too_many_orphans || out_of_socket_memory;
2087 : : }
2088 : :
2089 : 0 : void tcp_close(struct sock *sk, long timeout)
2090 : : {
2091 : : struct sk_buff *skb;
2092 : : int data_was_unread = 0;
2093 : : int state;
2094 : :
2095 : : lock_sock(sk);
2096 : 107 : sk->sk_shutdown = SHUTDOWN_MASK;
2097 : :
2098 [ + + ]: 107 : if (sk->sk_state == TCP_LISTEN) {
2099 : 9 : tcp_set_state(sk, TCP_CLOSE);
2100 : :
2101 : : /* Special case. */
2102 : 9 : inet_csk_listen_stop(sk);
2103 : :
2104 : 9 : goto adjudge_to_death;
2105 : : }
2106 : :
2107 : : /* We need to flush the recv. buffs. We do this only on the
2108 : : * descriptor close, not protocol-sourced closes, because the
2109 : : * reader process may not have drained the data yet!
2110 : : */
2111 [ + + ]: 113 : while ((skb = __skb_dequeue(&sk->sk_receive_queue)) != NULL) {
2112 : 30 : u32 len = TCP_SKB_CB(skb)->end_seq - TCP_SKB_CB(skb)->seq -
2113 : 15 : tcp_hdr(skb)->fin;
2114 : 15 : data_was_unread += len;
2115 : 15 : __kfree_skb(skb);
2116 : : }
2117 : :
2118 : : sk_mem_reclaim(sk);
2119 : :
2120 : : /* If socket has been already reset (e.g. in tcp_reset()) - kill it. */
2121 [ + + ]: 98 : if (sk->sk_state == TCP_CLOSE)
2122 : : goto adjudge_to_death;
2123 : :
2124 : : /* As outlined in RFC 2525, section 2.17, we send a RST here because
2125 : : * data was lost. To witness the awful effects of the old behavior of
2126 : : * always doing a FIN, run an older 2.1.x kernel or 2.0.x, start a bulk
2127 : : * GET in an FTP client, suspend the process, wait for the client to
2128 : : * advertise a zero window, then kill -9 the FTP client, wheee...
2129 : : * Note: timeout is always zero in such a case.
2130 : : */
2131 [ - + ]: 43 : if (unlikely(tcp_sk(sk)->repair)) {
2132 : 0 : sk->sk_prot->disconnect(sk, 0);
2133 [ + + ]: 43 : } else if (data_was_unread) {
2134 : : /* Unread data was tossed, zap the connection. */
2135 : 20 : NET_INC_STATS_USER(sock_net(sk), LINUX_MIB_TCPABORTONCLOSE);
2136 : 10 : tcp_set_state(sk, TCP_CLOSE);
2137 : 10 : tcp_send_active_reset(sk, sk->sk_allocation);
2138 [ - + ][ # # ]: 33 : } else if (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime) {
2139 : : /* Check zero linger _after_ checking for unread data. */
2140 : 0 : sk->sk_prot->disconnect(sk, 0);
2141 : 0 : NET_INC_STATS_USER(sock_net(sk), LINUX_MIB_TCPABORTONDATA);
2142 [ + + ]: 33 : } else if (tcp_close_state(sk)) {
2143 : : /* We FIN if the application ate all the data before
2144 : : * zapping the connection.
2145 : : */
2146 : :
2147 : : /* RED-PEN. Formally speaking, we have broken TCP state
2148 : : * machine. State transitions:
2149 : : *
2150 : : * TCP_ESTABLISHED -> TCP_FIN_WAIT1
2151 : : * TCP_SYN_RECV -> TCP_FIN_WAIT1 (forget it, it's impossible)
2152 : : * TCP_CLOSE_WAIT -> TCP_LAST_ACK
2153 : : *
2154 : : * are legal only when FIN has been sent (i.e. in window),
2155 : : * rather than queued out of window. Purists blame.
2156 : : *
2157 : : * F.e. "RFC state" is ESTABLISHED,
2158 : : * if Linux state is FIN-WAIT-1, but FIN is still not sent.
2159 : : *
2160 : : * The visible declinations are that sometimes
2161 : : * we enter time-wait state, when it is not required really
2162 : : * (harmless), do not send active resets, when they are
2163 : : * required by specs (TCP_ESTABLISHED, TCP_CLOSE_WAIT, when
2164 : : * they look as CLOSING or LAST_ACK for Linux)
2165 : : * Probably, I missed some more holelets.
2166 : : * --ANK
2167 : : * XXX (TFO) - To start off we don't support SYN+ACK+FIN
2168 : : * in a single packet! (May consider it later but will
2169 : : * probably need API support or TCP_CORK SYN-ACK until
2170 : : * data is written and socket is closed.)
2171 : : */
2172 : 30 : tcp_send_fin(sk);
2173 : : }
2174 : :
2175 : 43 : sk_stream_wait_close(sk, timeout);
2176 : :
2177 : : adjudge_to_death:
2178 : 107 : state = sk->sk_state;
2179 : : sock_hold(sk);
2180 : : sock_orphan(sk);
2181 : :
2182 : : /* It is the last release_sock in its life. It will remove backlog. */
2183 : 107 : release_sock(sk);
2184 : :
2185 : :
2186 : : /* Now socket is owned by kernel and we acquire BH lock
2187 : : to finish close. No need to check for user refs.
2188 : : */
2189 : 107 : local_bh_disable();
2190 : : bh_lock_sock(sk);
2191 [ - + ]: 107 : WARN_ON(sock_owned_by_user(sk));
2192 : :
2193 : 107 : percpu_counter_inc(sk->sk_prot->orphan_count);
2194 : :
2195 : : /* Have we already been destroyed by a softirq or backlog? */
2196 [ + + ][ + ]: 107 : if (state != TCP_CLOSE && sk->sk_state == TCP_CLOSE)
2197 : : goto out;
2198 : :
2199 : : /* This is a (useful) BSD violating of the RFC. There is a
2200 : : * problem with TCP as specified in that the other end could
2201 : : * keep a socket open forever with no application left this end.
2202 : : * We use a 3 minute timeout (about the same as BSD) then kill
2203 : : * our end. If they send after that then tough - BUT: long enough
2204 : : * that we won't make the old 4*rto = almost no time - whoops
2205 : : * reset mistake.
2206 : : *
2207 : : * Nope, it was not mistake. It is really desired behaviour
2208 : : * f.e. on http servers, when such sockets are useless, but
2209 : : * consume significant resources. Let's do it with special
2210 : : * linger2 option. --ANK
2211 : : */
2212 : :
2213 [ - + ]: 200 : if (sk->sk_state == TCP_FIN_WAIT2) {
2214 : : struct tcp_sock *tp = tcp_sk(sk);
2215 [ # # ]: 0 : if (tp->linger2 < 0) {
2216 : 0 : tcp_set_state(sk, TCP_CLOSE);
2217 : 0 : tcp_send_active_reset(sk, GFP_ATOMIC);
2218 : 0 : NET_INC_STATS_BH(sock_net(sk),
2219 : : LINUX_MIB_TCPABORTONLINGER);
2220 : : } else {
2221 : : const int tmo = tcp_fin_time(sk);
2222 : :
2223 [ # # ]: 0 : if (tmo > TCP_TIMEWAIT_LEN) {
2224 : 0 : inet_csk_reset_keepalive_timer(sk,
2225 : 0 : tmo - TCP_TIMEWAIT_LEN);
2226 : : } else {
2227 : 0 : tcp_time_wait(sk, TCP_FIN_WAIT2, tmo);
2228 : 0 : goto out;
2229 : : }
2230 : : }
2231 : : }
2232 [ + + ]: 93 : if (sk->sk_state != TCP_CLOSE) {
2233 : : sk_mem_reclaim(sk);
2234 [ - + ]: 19 : if (tcp_check_oom(sk, 0)) {
2235 : 0 : tcp_set_state(sk, TCP_CLOSE);
2236 : 0 : tcp_send_active_reset(sk, GFP_ATOMIC);
2237 : 0 : NET_INC_STATS_BH(sock_net(sk),
2238 : : LINUX_MIB_TCPABORTONMEMORY);
2239 : : }
2240 : : }
2241 : :
2242 [ + + ]: 93 : if (sk->sk_state == TCP_CLOSE) {
2243 : 74 : struct request_sock *req = tcp_sk(sk)->fastopen_rsk;
2244 : : /* We could get here with a non-NULL req if the socket is
2245 : : * aborted (e.g., closed with unread data) before 3WHS
2246 : : * finishes.
2247 : : */
2248 [ - + ]: 74 : if (req != NULL)
2249 : 0 : reqsk_fastopen_remove(sk, req, false);
2250 : 74 : inet_csk_destroy_sock(sk);
2251 : : }
2252 : : /* Otherwise, socket is reprieved until protocol close. */
2253 : :
2254 : : out:
2255 : : bh_unlock_sock(sk);
2256 : 107 : local_bh_enable();
2257 : : sock_put(sk);
2258 : 107 : }
2259 : : EXPORT_SYMBOL(tcp_close);
2260 : :
2261 : : /* These states need RST on ABORT according to RFC793 */
2262 : :
2263 : : static inline bool tcp_need_reset(int state)
2264 : : {
2265 : 3 : return (1 << state) &
2266 : : (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT | TCPF_FIN_WAIT1 |
2267 : : TCPF_FIN_WAIT2 | TCPF_SYN_RECV);
2268 : : }
2269 : :
2270 : 0 : int tcp_disconnect(struct sock *sk, int flags)
2271 : : {
2272 : : struct inet_sock *inet = inet_sk(sk);
2273 : : struct inet_connection_sock *icsk = inet_csk(sk);
2274 : : struct tcp_sock *tp = tcp_sk(sk);
2275 : : int err = 0;
2276 : 3 : int old_state = sk->sk_state;
2277 : :
2278 [ + + ]: 3 : if (old_state != TCP_CLOSE)
2279 : 2 : tcp_set_state(sk, TCP_CLOSE);
2280 : :
2281 : : /* ABORT function of RFC793 */
2282 [ - + ]: 3 : if (old_state == TCP_LISTEN) {
2283 : 0 : inet_csk_listen_stop(sk);
2284 [ - + ]: 3 : } else if (unlikely(tp->repair)) {
2285 : 0 : sk->sk_err = ECONNABORTED;
2286 [ + + ][ - + ]: 3 : } else if (tcp_need_reset(old_state) ||
2287 [ # # ]: 0 : (tp->snd_nxt != tp->write_seq &&
2288 : : (1 << old_state) & (TCPF_CLOSING | TCPF_LAST_ACK))) {
2289 : : /* The last check adjusts for discrepancy of Linux wrt. RFC
2290 : : * states
2291 : : */
2292 : 2 : tcp_send_active_reset(sk, gfp_any());
2293 : 2 : sk->sk_err = ECONNRESET;
2294 [ - + ]: 1 : } else if (old_state == TCP_SYN_SENT)
2295 : 0 : sk->sk_err = ECONNRESET;
2296 : :
2297 : : tcp_clear_xmit_timers(sk);
2298 : 3 : __skb_queue_purge(&sk->sk_receive_queue);
2299 : : tcp_write_queue_purge(sk);
2300 : 3 : __skb_queue_purge(&tp->out_of_order_queue);
2301 : : #ifdef CONFIG_NET_DMA
2302 : : __skb_queue_purge(&sk->sk_async_wait_queue);
2303 : : #endif
2304 : :
2305 : 3 : inet->inet_dport = 0;
2306 : :
2307 [ + - ]: 3 : if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK))
2308 : : inet_reset_saddr(sk);
2309 : :
2310 : 3 : sk->sk_shutdown = 0;
2311 : : sock_reset_flag(sk, SOCK_DONE);
2312 : 3 : tp->srtt = 0;
2313 [ - + ]: 3 : if ((tp->write_seq += tp->max_window + 2) == 0)
2314 : 0 : tp->write_seq = 1;
2315 : 3 : icsk->icsk_backoff = 0;
2316 : 3 : tp->snd_cwnd = 2;
2317 : 3 : icsk->icsk_probes_out = 0;
2318 : 3 : tp->packets_out = 0;
2319 : 3 : tp->snd_ssthresh = TCP_INFINITE_SSTHRESH;
2320 : 3 : tp->snd_cwnd_cnt = 0;
2321 : 3 : tp->window_clamp = 0;
2322 : : tcp_set_ca_state(sk, TCP_CA_Open);
2323 : 3 : tcp_clear_retrans(tp);
2324 : : inet_csk_delack_init(sk);
2325 : : tcp_init_send_head(sk);
2326 : 3 : memset(&tp->rx_opt, 0, sizeof(tp->rx_opt));
2327 : : __sk_dst_reset(sk);
2328 : :
2329 [ - + ][ # # ]: 3 : WARN_ON(inet->inet_num && !icsk->icsk_bind_hash);
[ - + ]
2330 : :
2331 : 3 : sk->sk_error_report(sk);
2332 : 3 : return err;
2333 : : }
2334 : : EXPORT_SYMBOL(tcp_disconnect);
2335 : :
2336 : 0 : void tcp_sock_destruct(struct sock *sk)
2337 : : {
2338 : 0 : inet_sock_destruct(sk);
2339 : :
2340 : 0 : kfree(inet_csk(sk)->icsk_accept_queue.fastopenq);
2341 : 0 : }
2342 : :
2343 : : static inline bool tcp_can_repair_sock(const struct sock *sk)
2344 : : {
2345 [ # # ][ # # ]: 0 : return ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN) &&
2346 : 0 : ((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_ESTABLISHED));
2347 : : }
2348 : :
2349 : 0 : static int tcp_repair_options_est(struct tcp_sock *tp,
2350 : : struct tcp_repair_opt __user *optbuf, unsigned int len)
2351 : : {
2352 : : struct tcp_repair_opt opt;
2353 : :
2354 [ # # ]: 0 : while (len >= sizeof(opt)) {
2355 [ # # ]: 0 : if (copy_from_user(&opt, optbuf, sizeof(opt)))
2356 : : return -EFAULT;
2357 : :
2358 : 0 : optbuf++;
2359 : 0 : len -= sizeof(opt);
2360 : :
2361 [ # # # # : 0 : switch (opt.opt_code) {
# ]
2362 : : case TCPOPT_MSS:
2363 : 0 : tp->rx_opt.mss_clamp = opt.opt_val;
2364 : 0 : break;
2365 : : case TCPOPT_WINDOW:
2366 : : {
2367 : 0 : u16 snd_wscale = opt.opt_val & 0xFFFF;
2368 : 0 : u16 rcv_wscale = opt.opt_val >> 16;
2369 : :
2370 [ # # ]: 0 : if (snd_wscale > 14 || rcv_wscale > 14)
2371 : : return -EFBIG;
2372 : :
2373 : 0 : tp->rx_opt.snd_wscale = snd_wscale;
2374 : 0 : tp->rx_opt.rcv_wscale = rcv_wscale;
2375 : 0 : tp->rx_opt.wscale_ok = 1;
2376 : : }
2377 : 0 : break;
2378 : : case TCPOPT_SACK_PERM:
2379 [ # # ]: 0 : if (opt.opt_val != 0)
2380 : : return -EINVAL;
2381 : :
2382 : 0 : tp->rx_opt.sack_ok |= TCP_SACK_SEEN;
2383 [ # # ]: 0 : if (sysctl_tcp_fack)
2384 : : tcp_enable_fack(tp);
2385 : : break;
2386 : : case TCPOPT_TIMESTAMP:
2387 [ # # ]: 0 : if (opt.opt_val != 0)
2388 : : return -EINVAL;
2389 : :
2390 : 0 : tp->rx_opt.tstamp_ok = 1;
2391 : 0 : break;
2392 : : }
2393 : : }
2394 : :
2395 : : return 0;
2396 : : }
2397 : :
2398 : : /*
2399 : : * Socket option code for TCP.
2400 : : */
2401 : 3 : static int do_tcp_setsockopt(struct sock *sk, int level,
2402 : : int optname, char __user *optval, unsigned int optlen)
2403 : : {
2404 : : struct tcp_sock *tp = tcp_sk(sk);
2405 : : struct inet_connection_sock *icsk = inet_csk(sk);
2406 : : int val;
2407 : : int err = 0;
2408 : :
2409 : : /* These are data/string values, all the others are ints */
2410 [ - + ]: 3 : switch (optname) {
2411 : : case TCP_CONGESTION: {
2412 : : char name[TCP_CA_NAME_MAX];
2413 : :
2414 [ # # ]: 0 : if (optlen < 1)
2415 : : return -EINVAL;
2416 : :
2417 : 0 : val = strncpy_from_user(name, optval,
2418 : 0 : min_t(long, TCP_CA_NAME_MAX-1, optlen));
2419 [ # # ]: 0 : if (val < 0)
2420 : : return -EFAULT;
2421 : 0 : name[val] = 0;
2422 : :
2423 : : lock_sock(sk);
2424 : 0 : err = tcp_set_congestion_control(sk, name);
2425 : 0 : release_sock(sk);
2426 : : return err;
2427 : : }
2428 : : default:
2429 : : /* fallthru */
2430 : : break;
2431 : : }
2432 : :
2433 [ + - ]: 3 : if (optlen < sizeof(int))
2434 : : return -EINVAL;
2435 : :
2436 [ + - ]: 3 : if (get_user(val, (int __user *)optval))
2437 : : return -EFAULT;
2438 : :
2439 : : lock_sock(sk);
2440 : :
2441 [ - + - - : 3 : switch (optname) {
- - - - -
- - - - -
+ - - - -
- - ]
2442 : : case TCP_MAXSEG:
2443 : : /* Values greater than interface MTU won't take effect. However
2444 : : * at the point when this call is done we typically don't yet
2445 : : * know which interface is going to be used */
2446 [ # # ]: 0 : if (val < TCP_MIN_MSS || val > MAX_TCP_WINDOW) {
2447 : : err = -EINVAL;
2448 : : break;
2449 : : }
2450 : 0 : tp->rx_opt.user_mss = val;
2451 : : break;
2452 : :
2453 : : case TCP_NODELAY:
2454 [ + - ]: 2 : if (val) {
2455 : : /* TCP_NODELAY is weaker than TCP_CORK, so that
2456 : : * this option on corked socket is remembered, but
2457 : : * it is not activated until cork is cleared.
2458 : : *
2459 : : * However, when TCP_NODELAY is set we make
2460 : : * an explicit push, which overrides even TCP_CORK
2461 : : * for currently queued segments.
2462 : : */
2463 : 2 : tp->nonagle |= TCP_NAGLE_OFF|TCP_NAGLE_PUSH;
2464 : : tcp_push_pending_frames(sk);
2465 : : } else {
2466 : 0 : tp->nonagle &= ~TCP_NAGLE_OFF;
2467 : : }
2468 : : break;
2469 : :
2470 : : case TCP_THIN_LINEAR_TIMEOUTS:
2471 [ # # ]: 0 : if (val < 0 || val > 1)
2472 : : err = -EINVAL;
2473 : : else
2474 : 0 : tp->thin_lto = val;
2475 : : break;
2476 : :
2477 : : case TCP_THIN_DUPACK:
2478 [ # # ]: 0 : if (val < 0 || val > 1)
2479 : : err = -EINVAL;
2480 : : else {
2481 : 0 : tp->thin_dupack = val;
2482 [ # # ]: 0 : if (tp->thin_dupack)
2483 : : tcp_disable_early_retrans(tp);
2484 : : }
2485 : : break;
2486 : :
2487 : : case TCP_REPAIR:
2488 [ # # ]: 0 : if (!tcp_can_repair_sock(sk))
2489 : : err = -EPERM;
2490 [ # # ]: 0 : else if (val == 1) {
2491 : 0 : tp->repair = 1;
2492 : 0 : sk->sk_reuse = SK_FORCE_REUSE;
2493 : 0 : tp->repair_queue = TCP_NO_QUEUE;
2494 [ # # ]: 0 : } else if (val == 0) {
2495 : 0 : tp->repair = 0;
2496 : 0 : sk->sk_reuse = SK_NO_REUSE;
2497 : 0 : tcp_send_window_probe(sk);
2498 : : } else
2499 : : err = -EINVAL;
2500 : :
2501 : : break;
2502 : :
2503 : : case TCP_REPAIR_QUEUE:
2504 [ # # ]: 0 : if (!tp->repair)
2505 : : err = -EPERM;
2506 [ # # ]: 0 : else if (val < TCP_QUEUES_NR)
2507 : 0 : tp->repair_queue = val;
2508 : : else
2509 : : err = -EINVAL;
2510 : : break;
2511 : :
2512 : : case TCP_QUEUE_SEQ:
2513 [ # # ]: 0 : if (sk->sk_state != TCP_CLOSE)
2514 : : err = -EPERM;
2515 [ # # ]: 0 : else if (tp->repair_queue == TCP_SEND_QUEUE)
2516 : 0 : tp->write_seq = val;
2517 [ # # ]: 0 : else if (tp->repair_queue == TCP_RECV_QUEUE)
2518 : 0 : tp->rcv_nxt = val;
2519 : : else
2520 : : err = -EINVAL;
2521 : : break;
2522 : :
2523 : : case TCP_REPAIR_OPTIONS:
2524 [ # # ]: 0 : if (!tp->repair)
2525 : : err = -EINVAL;
2526 [ # # ]: 0 : else if (sk->sk_state == TCP_ESTABLISHED)
2527 : 0 : err = tcp_repair_options_est(tp,
2528 : : (struct tcp_repair_opt __user *)optval,
2529 : : optlen);
2530 : : else
2531 : : err = -EPERM;
2532 : : break;
2533 : :
2534 : : case TCP_CORK:
2535 : : /* When set indicates to always queue non-full frames.
2536 : : * Later the user clears this option and we transmit
2537 : : * any pending partial frames in the queue. This is
2538 : : * meant to be used alongside sendfile() to get properly
2539 : : * filled frames when the user (for example) must write
2540 : : * out headers with a write() call first and then use
2541 : : * sendfile to send out the data parts.
2542 : : *
2543 : : * TCP_CORK can be set together with TCP_NODELAY and it is
2544 : : * stronger than TCP_NODELAY.
2545 : : */
2546 [ # # ]: 0 : if (val) {
2547 : 0 : tp->nonagle |= TCP_NAGLE_CORK;
2548 : : } else {
2549 : 0 : tp->nonagle &= ~TCP_NAGLE_CORK;
2550 [ # # ]: 0 : if (tp->nonagle&TCP_NAGLE_OFF)
2551 : 0 : tp->nonagle |= TCP_NAGLE_PUSH;
2552 : : tcp_push_pending_frames(sk);
2553 : : }
2554 : : break;
2555 : :
2556 : : case TCP_KEEPIDLE:
2557 [ # # ]: 0 : if (val < 1 || val > MAX_TCP_KEEPIDLE)
2558 : : err = -EINVAL;
2559 : : else {
2560 : 0 : tp->keepalive_time = val * HZ;
2561 [ # # ][ # # ]: 0 : if (sock_flag(sk, SOCK_KEEPOPEN) &&
2562 : 0 : !((1 << sk->sk_state) &
2563 : : (TCPF_CLOSE | TCPF_LISTEN))) {
2564 : : u32 elapsed = keepalive_time_elapsed(tp);
2565 [ # # ]: 0 : if (tp->keepalive_time > elapsed)
2566 : 0 : elapsed = tp->keepalive_time - elapsed;
2567 : : else
2568 : : elapsed = 0;
2569 : 0 : inet_csk_reset_keepalive_timer(sk, elapsed);
2570 : : }
2571 : : }
2572 : : break;
2573 : : case TCP_KEEPINTVL:
2574 [ # # ]: 0 : if (val < 1 || val > MAX_TCP_KEEPINTVL)
2575 : : err = -EINVAL;
2576 : : else
2577 : 0 : tp->keepalive_intvl = val * HZ;
2578 : : break;
2579 : : case TCP_KEEPCNT:
2580 [ # # ]: 0 : if (val < 1 || val > MAX_TCP_KEEPCNT)
2581 : : err = -EINVAL;
2582 : : else
2583 : 0 : tp->keepalive_probes = val;
2584 : : break;
2585 : : case TCP_SYNCNT:
2586 [ # # ]: 0 : if (val < 1 || val > MAX_TCP_SYNCNT)
2587 : : err = -EINVAL;
2588 : : else
2589 : 0 : icsk->icsk_syn_retries = val;
2590 : : break;
2591 : :
2592 : : case TCP_LINGER2:
2593 [ # # ]: 0 : if (val < 0)
2594 : 0 : tp->linger2 = -1;
2595 [ # # ]: 0 : else if (val > sysctl_tcp_fin_timeout / HZ)
2596 : 0 : tp->linger2 = 0;
2597 : : else
2598 : 0 : tp->linger2 = val * HZ;
2599 : : break;
2600 : :
2601 : : case TCP_DEFER_ACCEPT:
2602 : : /* Translate value in seconds to number of retransmits */
2603 : 0 : icsk->icsk_accept_queue.rskq_defer_accept =
2604 : : secs_to_retrans(val, TCP_TIMEOUT_INIT / HZ,
2605 : : TCP_RTO_MAX / HZ);
2606 : : break;
2607 : :
2608 : : case TCP_WINDOW_CLAMP:
2609 [ # # ]: 0 : if (!val) {
2610 [ # # ]: 0 : if (sk->sk_state != TCP_CLOSE) {
2611 : : err = -EINVAL;
2612 : : break;
2613 : : }
2614 : 0 : tp->window_clamp = 0;
2615 : : } else
2616 : 0 : tp->window_clamp = val < SOCK_MIN_RCVBUF / 2 ?
2617 : 0 : SOCK_MIN_RCVBUF / 2 : val;
2618 : : break;
2619 : :
2620 : : case TCP_QUICKACK:
2621 [ # # ]: 0 : if (!val) {
2622 : 0 : icsk->icsk_ack.pingpong = 1;
2623 : : } else {
2624 : 0 : icsk->icsk_ack.pingpong = 0;
2625 [ # # ]: 0 : if ((1 << sk->sk_state) &
2626 [ # # ]: 0 : (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT) &&
2627 : : inet_csk_ack_scheduled(sk)) {
2628 : 0 : icsk->icsk_ack.pending |= ICSK_ACK_PUSHED;
2629 : 0 : tcp_cleanup_rbuf(sk, 1);
2630 [ # # ]: 0 : if (!(val & 1))
2631 : 0 : icsk->icsk_ack.pingpong = 1;
2632 : : }
2633 : : }
2634 : : break;
2635 : :
2636 : : #ifdef CONFIG_TCP_MD5SIG
2637 : : case TCP_MD5SIG:
2638 : : /* Read the IP->Key mappings from userspace */
2639 : : err = tp->af_specific->md5_parse(sk, optval, optlen);
2640 : : break;
2641 : : #endif
2642 : : case TCP_USER_TIMEOUT:
2643 : : /* Cap the max timeout in ms TCP will retry/retrans
2644 : : * before giving up and aborting (ETIMEDOUT) a connection.
2645 : : */
2646 [ # # ]: 0 : if (val < 0)
2647 : : err = -EINVAL;
2648 : : else
2649 : 0 : icsk->icsk_user_timeout = msecs_to_jiffies(val);
2650 : : break;
2651 : :
2652 : : case TCP_FASTOPEN:
2653 [ # # ][ # # ]: 0 : if (val >= 0 && ((1 << sk->sk_state) & (TCPF_CLOSE |
2654 : : TCPF_LISTEN)))
2655 : : err = fastopen_init_queue(sk, val);
2656 : : else
2657 : : err = -EINVAL;
2658 : : break;
2659 : : case TCP_TIMESTAMP:
2660 [ # # ]: 0 : if (!tp->repair)
2661 : : err = -EPERM;
2662 : : else
2663 : 0 : tp->tsoffset = val - tcp_time_stamp;
2664 : : break;
2665 : : case TCP_NOTSENT_LOWAT:
2666 : 0 : tp->notsent_lowat = val;
2667 : 0 : sk->sk_write_space(sk);
2668 : : break;
2669 : : default:
2670 : : err = -ENOPROTOOPT;
2671 : : break;
2672 : : }
2673 : :
2674 : 0 : release_sock(sk);
2675 : : return err;
2676 : : }
2677 : :
2678 : 0 : int tcp_setsockopt(struct sock *sk, int level, int optname, char __user *optval,
2679 : : unsigned int optlen)
2680 : : {
2681 : : const struct inet_connection_sock *icsk = inet_csk(sk);
2682 : :
2683 [ + + ]: 9 : if (level != SOL_TCP)
2684 : 6 : return icsk->icsk_af_ops->setsockopt(sk, level, optname,
2685 : : optval, optlen);
2686 : 3 : return do_tcp_setsockopt(sk, level, optname, optval, optlen);
2687 : : }
2688 : : EXPORT_SYMBOL(tcp_setsockopt);
2689 : :
2690 : : #ifdef CONFIG_COMPAT
2691 : : int compat_tcp_setsockopt(struct sock *sk, int level, int optname,
2692 : : char __user *optval, unsigned int optlen)
2693 : : {
2694 : : if (level != SOL_TCP)
2695 : : return inet_csk_compat_setsockopt(sk, level, optname,
2696 : : optval, optlen);
2697 : : return do_tcp_setsockopt(sk, level, optname, optval, optlen);
2698 : : }
2699 : : EXPORT_SYMBOL(compat_tcp_setsockopt);
2700 : : #endif
2701 : :
2702 : : /* Return information about state of tcp endpoint in API format. */
2703 : 0 : void tcp_get_info(const struct sock *sk, struct tcp_info *info)
2704 : : {
2705 : : const struct tcp_sock *tp = tcp_sk(sk);
2706 : : const struct inet_connection_sock *icsk = inet_csk(sk);
2707 : 0 : u32 now = tcp_time_stamp;
2708 : :
2709 : 0 : memset(info, 0, sizeof(*info));
2710 : :
2711 : 0 : info->tcpi_state = sk->sk_state;
2712 : 0 : info->tcpi_ca_state = icsk->icsk_ca_state;
2713 : 0 : info->tcpi_retransmits = icsk->icsk_retransmits;
2714 : 0 : info->tcpi_probes = icsk->icsk_probes_out;
2715 : 0 : info->tcpi_backoff = icsk->icsk_backoff;
2716 : :
2717 [ # # ]: 0 : if (tp->rx_opt.tstamp_ok)
2718 : 0 : info->tcpi_options |= TCPI_OPT_TIMESTAMPS;
2719 [ # # ]: 0 : if (tcp_is_sack(tp))
2720 : 0 : info->tcpi_options |= TCPI_OPT_SACK;
2721 [ # # ]: 0 : if (tp->rx_opt.wscale_ok) {
2722 : 0 : info->tcpi_options |= TCPI_OPT_WSCALE;
2723 : 0 : info->tcpi_snd_wscale = tp->rx_opt.snd_wscale;
2724 : 0 : info->tcpi_rcv_wscale = tp->rx_opt.rcv_wscale;
2725 : : }
2726 : :
2727 [ # # ]: 0 : if (tp->ecn_flags & TCP_ECN_OK)
2728 : 0 : info->tcpi_options |= TCPI_OPT_ECN;
2729 [ # # ]: 0 : if (tp->ecn_flags & TCP_ECN_SEEN)
2730 : 0 : info->tcpi_options |= TCPI_OPT_ECN_SEEN;
2731 [ # # ]: 0 : if (tp->syn_data_acked)
2732 : 0 : info->tcpi_options |= TCPI_OPT_SYN_DATA;
2733 : :
2734 : 0 : info->tcpi_rto = jiffies_to_usecs(icsk->icsk_rto);
2735 : 0 : info->tcpi_ato = jiffies_to_usecs(icsk->icsk_ack.ato);
2736 : 0 : info->tcpi_snd_mss = tp->mss_cache;
2737 : 0 : info->tcpi_rcv_mss = icsk->icsk_ack.rcv_mss;
2738 : :
2739 [ # # ]: 0 : if (sk->sk_state == TCP_LISTEN) {
2740 : 0 : info->tcpi_unacked = sk->sk_ack_backlog;
2741 : 0 : info->tcpi_sacked = sk->sk_max_ack_backlog;
2742 : : } else {
2743 : 0 : info->tcpi_unacked = tp->packets_out;
2744 : 0 : info->tcpi_sacked = tp->sacked_out;
2745 : : }
2746 : 0 : info->tcpi_lost = tp->lost_out;
2747 : 0 : info->tcpi_retrans = tp->retrans_out;
2748 : 0 : info->tcpi_fackets = tp->fackets_out;
2749 : :
2750 : 0 : info->tcpi_last_data_sent = jiffies_to_msecs(now - tp->lsndtime);
2751 : 0 : info->tcpi_last_data_recv = jiffies_to_msecs(now - icsk->icsk_ack.lrcvtime);
2752 : 0 : info->tcpi_last_ack_recv = jiffies_to_msecs(now - tp->rcv_tstamp);
2753 : :
2754 : 0 : info->tcpi_pmtu = icsk->icsk_pmtu_cookie;
2755 : 0 : info->tcpi_rcv_ssthresh = tp->rcv_ssthresh;
2756 : 0 : info->tcpi_rtt = jiffies_to_usecs(tp->srtt)>>3;
2757 : 0 : info->tcpi_rttvar = jiffies_to_usecs(tp->mdev)>>2;
2758 : 0 : info->tcpi_snd_ssthresh = tp->snd_ssthresh;
2759 : 0 : info->tcpi_snd_cwnd = tp->snd_cwnd;
2760 : 0 : info->tcpi_advmss = tp->advmss;
2761 : 0 : info->tcpi_reordering = tp->reordering;
2762 : :
2763 : 0 : info->tcpi_rcv_rtt = jiffies_to_usecs(tp->rcv_rtt_est.rtt)>>3;
2764 : 0 : info->tcpi_rcv_space = tp->rcvq_space.space;
2765 : :
2766 : 0 : info->tcpi_total_retrans = tp->total_retrans;
2767 : 0 : }
2768 : : EXPORT_SYMBOL_GPL(tcp_get_info);
2769 : :
2770 : 3 : static int do_tcp_getsockopt(struct sock *sk, int level,
2771 : : int optname, char __user *optval, int __user *optlen)
2772 : : {
2773 : : struct inet_connection_sock *icsk = inet_csk(sk);
2774 : : struct tcp_sock *tp = tcp_sk(sk);
2775 : : int val, len;
2776 : :
2777 [ + - ]: 3 : if (get_user(len, optlen))
2778 : : return -EFAULT;
2779 : :
2780 : 3 : len = min_t(unsigned int, len, sizeof(int));
2781 : :
2782 [ + - ]: 3 : if (len < 0)
2783 : : return -EINVAL;
2784 : :
2785 [ - + - - : 3 : switch (optname) {
- - - - -
- - - - -
- - - - -
- - + ]
2786 : : case TCP_MAXSEG:
2787 : 0 : val = tp->mss_cache;
2788 [ # # ][ # # ]: 0 : if (!val && ((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN)))
2789 : 0 : val = tp->rx_opt.user_mss;
2790 [ # # ]: 0 : if (tp->repair)
2791 : 0 : val = tp->rx_opt.mss_clamp;
2792 : : break;
2793 : : case TCP_NODELAY:
2794 : 2 : val = !!(tp->nonagle&TCP_NAGLE_OFF);
2795 : : break;
2796 : : case TCP_CORK:
2797 : 0 : val = !!(tp->nonagle&TCP_NAGLE_CORK);
2798 : : break;
2799 : : case TCP_KEEPIDLE:
2800 : 0 : val = keepalive_time_when(tp) / HZ;
2801 : : break;
2802 : : case TCP_KEEPINTVL:
2803 : 0 : val = keepalive_intvl_when(tp) / HZ;
2804 : : break;
2805 : : case TCP_KEEPCNT:
2806 : 0 : val = keepalive_probes(tp);
2807 : : break;
2808 : : case TCP_SYNCNT:
2809 [ # # ]: 0 : val = icsk->icsk_syn_retries ? : sysctl_tcp_syn_retries;
2810 : : break;
2811 : : case TCP_LINGER2:
2812 : 0 : val = tp->linger2;
2813 [ # # ]: 0 : if (val >= 0)
2814 [ # # ]: 0 : val = (val ? : sysctl_tcp_fin_timeout) / HZ;
2815 : : break;
2816 : : case TCP_DEFER_ACCEPT:
2817 : 0 : val = retrans_to_secs(icsk->icsk_accept_queue.rskq_defer_accept,
2818 : : TCP_TIMEOUT_INIT / HZ, TCP_RTO_MAX / HZ);
2819 : : break;
2820 : : case TCP_WINDOW_CLAMP:
2821 : 0 : val = tp->window_clamp;
2822 : : break;
2823 : : case TCP_INFO: {
2824 : : struct tcp_info info;
2825 : :
2826 [ # # ]: 0 : if (get_user(len, optlen))
2827 : : return -EFAULT;
2828 : :
2829 : 0 : tcp_get_info(sk, &info);
2830 : :
2831 : 0 : len = min_t(unsigned int, len, sizeof(info));
2832 [ # # ]: 0 : if (put_user(len, optlen))
2833 : : return -EFAULT;
2834 [ # # ]: 0 : if (copy_to_user(optval, &info, len))
2835 : : return -EFAULT;
2836 : : return 0;
2837 : : }
2838 : : case TCP_QUICKACK:
2839 : 0 : val = !icsk->icsk_ack.pingpong;
2840 : : break;
2841 : :
2842 : : case TCP_CONGESTION:
2843 [ # # ]: 0 : if (get_user(len, optlen))
2844 : : return -EFAULT;
2845 : 0 : len = min_t(unsigned int, len, TCP_CA_NAME_MAX);
2846 [ # # ]: 0 : if (put_user(len, optlen))
2847 : : return -EFAULT;
2848 [ # # ]: 0 : if (copy_to_user(optval, icsk->icsk_ca_ops->name, len))
2849 : : return -EFAULT;
2850 : : return 0;
2851 : :
2852 : : case TCP_THIN_LINEAR_TIMEOUTS:
2853 : 0 : val = tp->thin_lto;
2854 : : break;
2855 : : case TCP_THIN_DUPACK:
2856 : 0 : val = tp->thin_dupack;
2857 : : break;
2858 : :
2859 : : case TCP_REPAIR:
2860 : 0 : val = tp->repair;
2861 : : break;
2862 : :
2863 : : case TCP_REPAIR_QUEUE:
2864 [ # # ]: 0 : if (tp->repair)
2865 : 0 : val = tp->repair_queue;
2866 : : else
2867 : : return -EINVAL;
2868 : : break;
2869 : :
2870 : : case TCP_QUEUE_SEQ:
2871 [ # # ]: 0 : if (tp->repair_queue == TCP_SEND_QUEUE)
2872 : 0 : val = tp->write_seq;
2873 [ # # ]: 0 : else if (tp->repair_queue == TCP_RECV_QUEUE)
2874 : 0 : val = tp->rcv_nxt;
2875 : : else
2876 : : return -EINVAL;
2877 : : break;
2878 : :
2879 : : case TCP_USER_TIMEOUT:
2880 : 0 : val = jiffies_to_msecs(icsk->icsk_user_timeout);
2881 : : break;
2882 : : case TCP_TIMESTAMP:
2883 : 0 : val = tcp_time_stamp + tp->tsoffset;
2884 : : break;
2885 : : case TCP_NOTSENT_LOWAT:
2886 : 0 : val = tp->notsent_lowat;
2887 : : break;
2888 : : default:
2889 : : return -ENOPROTOOPT;
2890 : : }
2891 : :
2892 [ + - ]: 2 : if (put_user(len, optlen))
2893 : : return -EFAULT;
2894 [ + - ]: 2 : if (copy_to_user(optval, &val, len))
2895 : : return -EFAULT;
2896 : : return 0;
2897 : : }
2898 : :
2899 : 0 : int tcp_getsockopt(struct sock *sk, int level, int optname, char __user *optval,
2900 : : int __user *optlen)
2901 : : {
2902 : : struct inet_connection_sock *icsk = inet_csk(sk);
2903 : :
2904 [ + + ]: 11 : if (level != SOL_TCP)
2905 : 8 : return icsk->icsk_af_ops->getsockopt(sk, level, optname,
2906 : : optval, optlen);
2907 : 3 : return do_tcp_getsockopt(sk, level, optname, optval, optlen);
2908 : : }
2909 : : EXPORT_SYMBOL(tcp_getsockopt);
2910 : :
2911 : : #ifdef CONFIG_COMPAT
2912 : : int compat_tcp_getsockopt(struct sock *sk, int level, int optname,
2913 : : char __user *optval, int __user *optlen)
2914 : : {
2915 : : if (level != SOL_TCP)
2916 : : return inet_csk_compat_getsockopt(sk, level, optname,
2917 : : optval, optlen);
2918 : : return do_tcp_getsockopt(sk, level, optname, optval, optlen);
2919 : : }
2920 : : EXPORT_SYMBOL(compat_tcp_getsockopt);
2921 : : #endif
2922 : :
2923 : : #ifdef CONFIG_TCP_MD5SIG
2924 : : static struct tcp_md5sig_pool __percpu *tcp_md5sig_pool __read_mostly;
2925 : : static DEFINE_MUTEX(tcp_md5sig_mutex);
2926 : :
2927 : : static void __tcp_free_md5sig_pool(struct tcp_md5sig_pool __percpu *pool)
2928 : : {
2929 : : int cpu;
2930 : :
2931 : : for_each_possible_cpu(cpu) {
2932 : : struct tcp_md5sig_pool *p = per_cpu_ptr(pool, cpu);
2933 : :
2934 : : if (p->md5_desc.tfm)
2935 : : crypto_free_hash(p->md5_desc.tfm);
2936 : : }
2937 : : free_percpu(pool);
2938 : : }
2939 : :
2940 : : static void __tcp_alloc_md5sig_pool(void)
2941 : : {
2942 : : int cpu;
2943 : : struct tcp_md5sig_pool __percpu *pool;
2944 : :
2945 : : pool = alloc_percpu(struct tcp_md5sig_pool);
2946 : : if (!pool)
2947 : : return;
2948 : :
2949 : : for_each_possible_cpu(cpu) {
2950 : : struct crypto_hash *hash;
2951 : :
2952 : : hash = crypto_alloc_hash("md5", 0, CRYPTO_ALG_ASYNC);
2953 : : if (IS_ERR_OR_NULL(hash))
2954 : : goto out_free;
2955 : :
2956 : : per_cpu_ptr(pool, cpu)->md5_desc.tfm = hash;
2957 : : }
2958 : : /* before setting tcp_md5sig_pool, we must commit all writes
2959 : : * to memory. See ACCESS_ONCE() in tcp_get_md5sig_pool()
2960 : : */
2961 : : smp_wmb();
2962 : : tcp_md5sig_pool = pool;
2963 : : return;
2964 : : out_free:
2965 : : __tcp_free_md5sig_pool(pool);
2966 : : }
2967 : :
2968 : : bool tcp_alloc_md5sig_pool(void)
2969 : : {
2970 : : if (unlikely(!tcp_md5sig_pool)) {
2971 : : mutex_lock(&tcp_md5sig_mutex);
2972 : :
2973 : : if (!tcp_md5sig_pool)
2974 : : __tcp_alloc_md5sig_pool();
2975 : :
2976 : : mutex_unlock(&tcp_md5sig_mutex);
2977 : : }
2978 : : return tcp_md5sig_pool != NULL;
2979 : : }
2980 : : EXPORT_SYMBOL(tcp_alloc_md5sig_pool);
2981 : :
2982 : :
2983 : : /**
2984 : : * tcp_get_md5sig_pool - get md5sig_pool for this user
2985 : : *
2986 : : * We use percpu structure, so if we succeed, we exit with preemption
2987 : : * and BH disabled, to make sure another thread or softirq handling
2988 : : * wont try to get same context.
2989 : : */
2990 : : struct tcp_md5sig_pool *tcp_get_md5sig_pool(void)
2991 : : {
2992 : : struct tcp_md5sig_pool __percpu *p;
2993 : :
2994 : : local_bh_disable();
2995 : : p = ACCESS_ONCE(tcp_md5sig_pool);
2996 : : if (p)
2997 : : return __this_cpu_ptr(p);
2998 : :
2999 : : local_bh_enable();
3000 : : return NULL;
3001 : : }
3002 : : EXPORT_SYMBOL(tcp_get_md5sig_pool);
3003 : :
3004 : : int tcp_md5_hash_header(struct tcp_md5sig_pool *hp,
3005 : : const struct tcphdr *th)
3006 : : {
3007 : : struct scatterlist sg;
3008 : : struct tcphdr hdr;
3009 : : int err;
3010 : :
3011 : : /* We are not allowed to change tcphdr, make a local copy */
3012 : : memcpy(&hdr, th, sizeof(hdr));
3013 : : hdr.check = 0;
3014 : :
3015 : : /* options aren't included in the hash */
3016 : : sg_init_one(&sg, &hdr, sizeof(hdr));
3017 : : err = crypto_hash_update(&hp->md5_desc, &sg, sizeof(hdr));
3018 : : return err;
3019 : : }
3020 : : EXPORT_SYMBOL(tcp_md5_hash_header);
3021 : :
3022 : : int tcp_md5_hash_skb_data(struct tcp_md5sig_pool *hp,
3023 : : const struct sk_buff *skb, unsigned int header_len)
3024 : : {
3025 : : struct scatterlist sg;
3026 : : const struct tcphdr *tp = tcp_hdr(skb);
3027 : : struct hash_desc *desc = &hp->md5_desc;
3028 : : unsigned int i;
3029 : : const unsigned int head_data_len = skb_headlen(skb) > header_len ?
3030 : : skb_headlen(skb) - header_len : 0;
3031 : : const struct skb_shared_info *shi = skb_shinfo(skb);
3032 : : struct sk_buff *frag_iter;
3033 : :
3034 : : sg_init_table(&sg, 1);
3035 : :
3036 : : sg_set_buf(&sg, ((u8 *) tp) + header_len, head_data_len);
3037 : : if (crypto_hash_update(desc, &sg, head_data_len))
3038 : : return 1;
3039 : :
3040 : : for (i = 0; i < shi->nr_frags; ++i) {
3041 : : const struct skb_frag_struct *f = &shi->frags[i];
3042 : : unsigned int offset = f->page_offset;
3043 : : struct page *page = skb_frag_page(f) + (offset >> PAGE_SHIFT);
3044 : :
3045 : : sg_set_page(&sg, page, skb_frag_size(f),
3046 : : offset_in_page(offset));
3047 : : if (crypto_hash_update(desc, &sg, skb_frag_size(f)))
3048 : : return 1;
3049 : : }
3050 : :
3051 : : skb_walk_frags(skb, frag_iter)
3052 : : if (tcp_md5_hash_skb_data(hp, frag_iter, 0))
3053 : : return 1;
3054 : :
3055 : : return 0;
3056 : : }
3057 : : EXPORT_SYMBOL(tcp_md5_hash_skb_data);
3058 : :
3059 : : int tcp_md5_hash_key(struct tcp_md5sig_pool *hp, const struct tcp_md5sig_key *key)
3060 : : {
3061 : : struct scatterlist sg;
3062 : :
3063 : : sg_init_one(&sg, key->key, key->keylen);
3064 : : return crypto_hash_update(&hp->md5_desc, &sg, key->keylen);
3065 : : }
3066 : : EXPORT_SYMBOL(tcp_md5_hash_key);
3067 : :
3068 : : #endif
3069 : :
3070 : 0 : void tcp_done(struct sock *sk)
3071 : : {
3072 : 44 : struct request_sock *req = tcp_sk(sk)->fastopen_rsk;
3073 : :
3074 [ + + ][ - + ]: 44 : if (sk->sk_state == TCP_SYN_SENT || sk->sk_state == TCP_SYN_RECV)
3075 : 1 : TCP_INC_STATS_BH(sock_net(sk), TCP_MIB_ATTEMPTFAILS);
3076 : :
3077 : 44 : tcp_set_state(sk, TCP_CLOSE);
3078 : : tcp_clear_xmit_timers(sk);
3079 [ - + ]: 44 : if (req != NULL)
3080 : 0 : reqsk_fastopen_remove(sk, req, false);
3081 : :
3082 : 44 : sk->sk_shutdown = SHUTDOWN_MASK;
3083 : :
3084 [ + + ]: 44 : if (!sock_flag(sk, SOCK_DEAD))
3085 : 11 : sk->sk_state_change(sk);
3086 : : else
3087 : 33 : inet_csk_destroy_sock(sk);
3088 : 44 : }
3089 : : EXPORT_SYMBOL_GPL(tcp_done);
3090 : :
3091 : : extern struct tcp_congestion_ops tcp_reno;
3092 : :
3093 : : static __initdata unsigned long thash_entries;
3094 : 0 : static int __init set_thash_entries(char *str)
3095 : : {
3096 : : ssize_t ret;
3097 : :
3098 [ # # ]: 0 : if (!str)
3099 : : return 0;
3100 : :
3101 : : ret = kstrtoul(str, 0, &thash_entries);
3102 [ # # ]: 0 : if (ret)
3103 : : return 0;
3104 : :
3105 : 0 : return 1;
3106 : : }
3107 : : __setup("thash_entries=", set_thash_entries);
3108 : :
3109 : 0 : static void tcp_init_mem(void)
3110 : : {
3111 : 0 : unsigned long limit = nr_free_buffer_pages() / 8;
3112 : 0 : limit = max(limit, 128UL);
3113 : 0 : sysctl_tcp_mem[0] = limit / 4 * 3;
3114 : 0 : sysctl_tcp_mem[1] = limit;
3115 : 0 : sysctl_tcp_mem[2] = sysctl_tcp_mem[0] * 2;
3116 : 0 : }
3117 : :
3118 : 0 : void __init tcp_init(void)
3119 : : {
3120 : : struct sk_buff *skb = NULL;
3121 : : unsigned long limit;
3122 : : int max_rshare, max_wshare, cnt;
3123 : : unsigned int i;
3124 : :
3125 : : BUILD_BUG_ON(sizeof(struct tcp_skb_cb) > sizeof(skb->cb));
3126 : :
3127 : 0 : percpu_counter_init(&tcp_sockets_allocated, 0);
3128 : 0 : percpu_counter_init(&tcp_orphan_count, 0);
3129 : 0 : tcp_hashinfo.bind_bucket_cachep =
3130 : 0 : kmem_cache_create("tcp_bind_bucket",
3131 : : sizeof(struct inet_bind_bucket), 0,
3132 : : SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
3133 : :
3134 : : /* Size and allocate the main established and bind bucket
3135 : : * hash tables.
3136 : : *
3137 : : * The methodology is similar to that of the buffer cache.
3138 : : */
3139 : 0 : tcp_hashinfo.ehash =
3140 [ # # ]: 0 : alloc_large_system_hash("TCP established",
3141 : : sizeof(struct inet_ehash_bucket),
3142 : : thash_entries,
3143 : : 17, /* one slot per 128 KB of memory */
3144 : : 0,
3145 : : NULL,
3146 : : &tcp_hashinfo.ehash_mask,
3147 : : 0,
3148 : 0 : thash_entries ? 0 : 512 * 1024);
3149 [ # # ]: 0 : for (i = 0; i <= tcp_hashinfo.ehash_mask; i++)
3150 : 0 : INIT_HLIST_NULLS_HEAD(&tcp_hashinfo.ehash[i].chain, i);
3151 : :
3152 [ # # ]: 0 : if (inet_ehash_locks_alloc(&tcp_hashinfo))
3153 : 0 : panic("TCP: failed to alloc ehash_locks");
3154 : 0 : tcp_hashinfo.bhash =
3155 : 0 : alloc_large_system_hash("TCP bind",
3156 : : sizeof(struct inet_bind_hashbucket),
3157 : 0 : tcp_hashinfo.ehash_mask + 1,
3158 : : 17, /* one slot per 128 KB of memory */
3159 : : 0,
3160 : : &tcp_hashinfo.bhash_size,
3161 : : NULL,
3162 : : 0,
3163 : : 64 * 1024);
3164 : 0 : tcp_hashinfo.bhash_size = 1U << tcp_hashinfo.bhash_size;
3165 [ # # ]: 0 : for (i = 0; i < tcp_hashinfo.bhash_size; i++) {
3166 : 0 : spin_lock_init(&tcp_hashinfo.bhash[i].lock);
3167 : 0 : INIT_HLIST_HEAD(&tcp_hashinfo.bhash[i].chain);
3168 : : }
3169 : :
3170 : :
3171 : 0 : cnt = tcp_hashinfo.ehash_mask + 1;
3172 : :
3173 : 0 : tcp_death_row.sysctl_max_tw_buckets = cnt / 2;
3174 : 0 : sysctl_tcp_max_orphans = cnt / 2;
3175 : 0 : sysctl_max_syn_backlog = max(128, cnt / 256);
3176 : :
3177 : 0 : tcp_init_mem();
3178 : : /* Set per-socket limits to no more than 1/128 the pressure threshold */
3179 : 0 : limit = nr_free_buffer_pages() << (PAGE_SHIFT - 7);
3180 : 0 : max_wshare = min(4UL*1024*1024, limit);
3181 : 0 : max_rshare = min(6UL*1024*1024, limit);
3182 : :
3183 : 0 : sysctl_tcp_wmem[0] = SK_MEM_QUANTUM;
3184 : 0 : sysctl_tcp_wmem[1] = 16*1024;
3185 : 0 : sysctl_tcp_wmem[2] = max(64*1024, max_wshare);
3186 : :
3187 : 0 : sysctl_tcp_rmem[0] = SK_MEM_QUANTUM;
3188 : 0 : sysctl_tcp_rmem[1] = 87380;
3189 : 0 : sysctl_tcp_rmem[2] = max(87380, max_rshare);
3190 : :
3191 : 0 : pr_info("Hash tables configured (established %u bind %u)\n",
3192 : : tcp_hashinfo.ehash_mask + 1, tcp_hashinfo.bhash_size);
3193 : :
3194 : 0 : tcp_metrics_init();
3195 : :
3196 : 0 : tcp_register_congestion_control(&tcp_reno);
3197 : :
3198 : 0 : tcp_tasklet_init();
3199 : 0 : }
3200 : :
3201 : 0 : static int tcp_is_local(struct net *net, __be32 addr) {
3202 : : struct rtable *rt;
3203 : 0 : struct flowi4 fl4 = { .daddr = addr };
3204 : : rt = ip_route_output_key(net, &fl4);
3205 [ # # ]: 0 : if (IS_ERR_OR_NULL(rt))
3206 : : return 0;
3207 [ # # ][ # # ]: 0 : return rt->dst.dev && (rt->dst.dev->flags & IFF_LOOPBACK);
3208 : : }
3209 : :
3210 : : #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3211 : 0 : static int tcp_is_local6(struct net *net, struct in6_addr *addr) {
3212 : 0 : struct rt6_info *rt6 = rt6_lookup(net, addr, addr, 0, 0);
3213 [ # # ][ # # ]: 0 : return rt6 && rt6->dst.dev && (rt6->dst.dev->flags & IFF_LOOPBACK);
[ # # ]
3214 : : }
3215 : : #endif
3216 : :
3217 : : /*
3218 : : * tcp_nuke_addr - destroy all sockets on the given local address
3219 : : * if local address is the unspecified address (0.0.0.0 or ::), destroy all
3220 : : * sockets with local addresses that are not configured.
3221 : : */
3222 : 0 : int tcp_nuke_addr(struct net *net, struct sockaddr *addr)
3223 : : {
3224 : 0 : int family = addr->sa_family;
3225 : : unsigned int bucket;
3226 : :
3227 : : struct in_addr *in;
3228 : : #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3229 : : struct in6_addr *in6;
3230 : : #endif
3231 [ # # ]: 0 : if (family == AF_INET) {
3232 : 0 : in = &((struct sockaddr_in *)addr)->sin_addr;
3233 : : #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3234 [ # # ]: 0 : } else if (family == AF_INET6) {
3235 : 0 : in6 = &((struct sockaddr_in6 *)addr)->sin6_addr;
3236 : : #endif
3237 : : } else {
3238 : : return -EAFNOSUPPORT;
3239 : : }
3240 : :
3241 [ # # ]: 0 : for (bucket = 0; bucket < tcp_hashinfo.ehash_mask; bucket++) {
3242 : : struct hlist_nulls_node *node;
3243 : : struct sock *sk;
3244 : 0 : spinlock_t *lock = inet_ehash_lockp(&tcp_hashinfo, bucket);
3245 : :
3246 : : restart:
3247 : : spin_lock_bh(lock);
3248 [ # # ]: 0 : sk_nulls_for_each(sk, node, &tcp_hashinfo.ehash[bucket].chain) {
3249 : : struct inet_sock *inet = inet_sk(sk);
3250 : :
3251 [ # # ][ # # ]: 0 : if (sysctl_ip_dynaddr && sk->sk_state == TCP_SYN_SENT)
3252 : 0 : continue;
3253 [ # # ]: 0 : if (sock_flag(sk, SOCK_DEAD))
3254 : 0 : continue;
3255 : :
3256 [ # # ]: 0 : if (family == AF_INET) {
3257 : 0 : __be32 s4 = inet->inet_rcv_saddr;
3258 [ # # ]: 0 : if (s4 == LOOPBACK4_IPV6)
3259 : 0 : continue;
3260 : :
3261 [ # # ][ # # ]: 0 : if (in->s_addr != s4 &&
3262 [ # # ]: 0 : !(in->s_addr == INADDR_ANY &&
3263 : 0 : !tcp_is_local(net, s4)))
3264 : 0 : continue;
3265 : : }
3266 : :
3267 : : #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3268 [ # # ]: 0 : if (family == AF_INET6) {
3269 : : struct in6_addr *s6;
3270 [ # # ]: 0 : if (!inet->pinet6)
3271 : 0 : continue;
3272 : :
3273 : 0 : s6 = &sk->sk_v6_rcv_saddr;
3274 [ # # ]: 0 : if (ipv6_addr_type(s6) == IPV6_ADDR_MAPPED)
3275 : 0 : continue;
3276 : :
3277 [ # # ][ # # ]: 0 : if (!ipv6_addr_equal(in6, s6) &&
3278 [ # # ]: 0 : !(ipv6_addr_equal(in6, &in6addr_any) &&
3279 : 0 : !tcp_is_local6(net, s6)))
3280 : 0 : continue;
3281 : : }
3282 : : #endif
3283 : :
3284 : : sock_hold(sk);
3285 : : spin_unlock_bh(lock);
3286 : :
3287 : 0 : local_bh_disable();
3288 : : bh_lock_sock(sk);
3289 : 0 : sk->sk_err = ETIMEDOUT;
3290 : 0 : sk->sk_error_report(sk);
3291 : :
3292 : 0 : tcp_done(sk);
3293 : : bh_unlock_sock(sk);
3294 : 0 : local_bh_enable();
3295 : : sock_put(sk);
3296 : :
3297 : : goto restart;
3298 : : }
3299 : : spin_unlock_bh(lock);
3300 : : }
3301 : :
3302 : : return 0;
3303 : : }
|