blob: 886596ff0aaeaab7402b7e603f7ba114beb17f8e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 *
Jesper Juhl02c30a82005-05-05 16:16:16 -07008 * Authors: Ross Biro
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * 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
Ilpo Järvinen172589c2007-08-28 15:50:33 -0700248#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249#include <linux/module.h>
250#include <linux/types.h>
251#include <linux/fcntl.h>
252#include <linux/poll.h>
253#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254#include <linux/fs.h>
Jens Axboe9c55e012007-11-06 23:30:13 -0800255#include <linux/skbuff.h>
Andrew Morton81b23b42008-07-03 03:22:02 -0700256#include <linux/scatterlist.h>
Jens Axboe9c55e012007-11-06 23:30:13 -0800257#include <linux/splice.h>
258#include <linux/net.h>
259#include <linux/socket.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260#include <linux/random.h>
261#include <linux/bootmem.h>
Miquel van Smoorenburg57413eb2008-06-27 17:23:57 -0700262#include <linux/highmem.h>
263#include <linux/swap.h>
David S. Millerb8059ea2006-03-25 01:36:56 -0800264#include <linux/cache.h>
Herbert Xuf4c50d92006-06-22 03:02:40 -0700265#include <linux/err.h>
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800266#include <linux/crypto.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268#include <net/icmp.h>
269#include <net/tcp.h>
270#include <net/xfrm.h>
271#include <net/ip.h>
Chris Leech1a2449a2006-05-23 18:05:53 -0700272#include <net/netdma.h>
Jens Axboe9c55e012007-11-06 23:30:13 -0800273#include <net/sock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275#include <asm/uaccess.h>
276#include <asm/ioctls.h>
277
Brian Haleyab32ea52006-09-22 14:15:41 -0700278int sysctl_tcp_fin_timeout __read_mostly = TCP_FIN_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Eric Dumazetdd24c002008-11-25 21:17:14 -0800280struct percpu_counter tcp_orphan_count;
Arnaldo Carvalho de Melo0a5578c2005-08-09 20:11:41 -0700281EXPORT_SYMBOL_GPL(tcp_orphan_count);
282
David S. Millerb8059ea2006-03-25 01:36:56 -0800283int sysctl_tcp_mem[3] __read_mostly;
284int sysctl_tcp_wmem[3] __read_mostly;
285int sysctl_tcp_rmem[3] __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287EXPORT_SYMBOL(sysctl_tcp_mem);
288EXPORT_SYMBOL(sysctl_tcp_rmem);
289EXPORT_SYMBOL(sysctl_tcp_wmem);
290
291atomic_t tcp_memory_allocated; /* Current allocated memory. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292EXPORT_SYMBOL(tcp_memory_allocated);
Eric Dumazet17483762008-11-25 21:16:35 -0800293
294/*
295 * Current number of TCP sockets.
296 */
297struct percpu_counter tcp_sockets_allocated;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298EXPORT_SYMBOL(tcp_sockets_allocated);
299
300/*
Jens Axboe9c55e012007-11-06 23:30:13 -0800301 * TCP splice context
302 */
303struct tcp_splice_state {
304 struct pipe_inode_info *pipe;
305 size_t len;
306 unsigned int flags;
307};
308
309/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 * Pressure flag: try to collapse.
311 * Technical note: it is used by multiple contexts non atomically.
Hideo Aoki3ab224b2007-12-31 00:11:19 -0800312 * All the __sk_mem_schedule() is of this nature: accounting
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 * is strict, actions are advisory and have some latency.
314 */
Eric Dumazet4103f8c2007-03-27 13:58:31 -0700315int tcp_memory_pressure __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317EXPORT_SYMBOL(tcp_memory_pressure);
318
Pavel Emelyanov5c52ba12008-07-16 20:28:10 -0700319void tcp_enter_memory_pressure(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
321 if (!tcp_memory_pressure) {
Pavel Emelyanov4e673442008-07-16 20:30:14 -0700322 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPMEMORYPRESSURES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 tcp_memory_pressure = 1;
324 }
325}
326
327EXPORT_SYMBOL(tcp_enter_memory_pressure);
328
329/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 * Wait for a TCP event.
331 *
332 * Note that we don't need to lock the socket, as the upper poll layers
333 * take care of normal races (between the test and the event) and we don't
334 * go look at any of the socket buffers directly.
335 */
336unsigned int tcp_poll(struct file *file, struct socket *sock, poll_table *wait)
337{
338 unsigned int mask;
339 struct sock *sk = sock->sk;
340 struct tcp_sock *tp = tcp_sk(sk);
341
342 poll_wait(file, sk->sk_sleep, wait);
343 if (sk->sk_state == TCP_LISTEN)
Arnaldo Carvalho de Melodc40c7b2005-08-23 21:52:58 -0700344 return inet_csk_listen_poll(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346 /* Socket is not locked. We are protected from async events
Will Newton70efce22008-07-16 20:13:43 -0700347 * by poll logic and correct handling of state changes
348 * made by other threads is impossible in any case.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 */
350
351 mask = 0;
352 if (sk->sk_err)
353 mask = POLLERR;
354
355 /*
356 * POLLHUP is certainly not done right. But poll() doesn't
357 * have a notion of HUP in just one direction, and for a
358 * socket the read side is more interesting.
359 *
360 * Some poll() documentation says that POLLHUP is incompatible
361 * with the POLLOUT/POLLWR flags, so somebody should check this
362 * all. But careful, it tends to be safer to return too many
363 * bits than too few, and you can easily break real applications
364 * if you don't tell them that something has hung up!
365 *
366 * Check-me.
367 *
368 * Check number 1. POLLHUP is _UNMASKABLE_ event (see UNIX98 and
369 * our fs/select.c). It means that after we received EOF,
370 * poll always returns immediately, making impossible poll() on write()
371 * in state CLOSE_WAIT. One solution is evident --- to set POLLHUP
372 * if and only if shutdown has been made in both directions.
373 * Actually, it is interesting to look how Solaris and DUX
Will Newton70efce22008-07-16 20:13:43 -0700374 * solve this dilemma. I would prefer, if POLLHUP were maskable,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 * then we could set it on SND_SHUTDOWN. BTW examples given
376 * in Stevens' books assume exactly this behaviour, it explains
Will Newton70efce22008-07-16 20:13:43 -0700377 * why POLLHUP is incompatible with POLLOUT. --ANK
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 *
379 * NOTE. Check for TCP_CLOSE is added. The goal is to prevent
380 * blocking on fresh not-connected or disconnected socket. --ANK
381 */
382 if (sk->sk_shutdown == SHUTDOWN_MASK || sk->sk_state == TCP_CLOSE)
383 mask |= POLLHUP;
384 if (sk->sk_shutdown & RCV_SHUTDOWN)
Davide Libenzif348d702006-03-25 03:07:39 -0800385 mask |= POLLIN | POLLRDNORM | POLLRDHUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 /* Connected? */
388 if ((1 << sk->sk_state) & ~(TCPF_SYN_SENT | TCPF_SYN_RECV)) {
David S. Millerc7004482008-10-06 10:43:54 -0700389 int target = sock_rcvlowat(sk, 0, INT_MAX);
390
391 if (tp->urg_seq == tp->copied_seq &&
392 !sock_flag(sk, SOCK_URGINLINE) &&
393 tp->urg_data)
394 target--;
395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 /* Potential race condition. If read of tp below will
397 * escape above sk->sk_state, we can be illegally awaken
398 * in SYN_* states. */
David S. Millerc7004482008-10-06 10:43:54 -0700399 if (tp->rcv_nxt - tp->copied_seq >= target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 mask |= POLLIN | POLLRDNORM;
401
402 if (!(sk->sk_shutdown & SEND_SHUTDOWN)) {
403 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) {
404 mask |= POLLOUT | POLLWRNORM;
405 } else { /* send SIGIO later */
406 set_bit(SOCK_ASYNC_NOSPACE,
407 &sk->sk_socket->flags);
408 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
409
410 /* Race breaker. If space is freed after
411 * wspace test but before the flags are set,
412 * IO signal will be lost.
413 */
414 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk))
415 mask |= POLLOUT | POLLWRNORM;
416 }
417 }
418
419 if (tp->urg_data & TCP_URG_VALID)
420 mask |= POLLPRI;
421 }
422 return mask;
423}
424
425int tcp_ioctl(struct sock *sk, int cmd, unsigned long arg)
426{
427 struct tcp_sock *tp = tcp_sk(sk);
428 int answ;
429
430 switch (cmd) {
431 case SIOCINQ:
432 if (sk->sk_state == TCP_LISTEN)
433 return -EINVAL;
434
435 lock_sock(sk);
436 if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV))
437 answ = 0;
438 else if (sock_flag(sk, SOCK_URGINLINE) ||
439 !tp->urg_data ||
440 before(tp->urg_seq, tp->copied_seq) ||
441 !before(tp->urg_seq, tp->rcv_nxt)) {
442 answ = tp->rcv_nxt - tp->copied_seq;
443
444 /* Subtract 1, if FIN is in queue. */
445 if (answ && !skb_queue_empty(&sk->sk_receive_queue))
446 answ -=
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -0700447 tcp_hdr((struct sk_buff *)sk->sk_receive_queue.prev)->fin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 } else
449 answ = tp->urg_seq - tp->copied_seq;
450 release_sock(sk);
451 break;
452 case SIOCATMARK:
453 answ = tp->urg_data && tp->urg_seq == tp->copied_seq;
454 break;
455 case SIOCOUTQ:
456 if (sk->sk_state == TCP_LISTEN)
457 return -EINVAL;
458
459 if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV))
460 answ = 0;
461 else
462 answ = tp->write_seq - tp->snd_una;
463 break;
464 default:
465 return -ENOIOCTLCMD;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
468 return put_user(answ, (int __user *)arg);
469}
470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471static inline void tcp_mark_push(struct tcp_sock *tp, struct sk_buff *skb)
472{
473 TCP_SKB_CB(skb)->flags |= TCPCB_FLAG_PSH;
474 tp->pushed_seq = tp->write_seq;
475}
476
477static inline int forced_push(struct tcp_sock *tp)
478{
479 return after(tp->write_seq, tp->pushed_seq + (tp->max_window >> 1));
480}
481
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -0700482static inline void skb_entail(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -0700484 struct tcp_sock *tp = tcp_sk(sk);
Arnaldo Carvalho de Melo352d4802006-11-17 19:59:12 -0200485 struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
486
487 skb->csum = 0;
488 tcb->seq = tcb->end_seq = tp->write_seq;
489 tcb->flags = TCPCB_FLAG_ACK;
490 tcb->sacked = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 skb_header_release(skb);
David S. Millerfe067e82007-03-07 12:12:44 -0800492 tcp_add_write_queue_tail(sk, skb);
Hideo Aoki3ab224b2007-12-31 00:11:19 -0800493 sk->sk_wmem_queued += skb->truesize;
494 sk_mem_charge(sk, skb->truesize);
David S. Miller89ebd192005-08-23 10:13:06 -0700495 if (tp->nonagle & TCP_NAGLE_PUSH)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900496 tp->nonagle &= ~TCP_NAGLE_PUSH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497}
498
499static inline void tcp_mark_urg(struct tcp_sock *tp, int flags,
500 struct sk_buff *skb)
501{
Ilpo Järvinen33f5f572008-10-07 14:43:06 -0700502 if (flags & MSG_OOB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 tp->snd_up = tp->write_seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504}
505
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -0700506static inline void tcp_push(struct sock *sk, int flags, int mss_now,
507 int nonagle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -0700509 struct tcp_sock *tp = tcp_sk(sk);
510
David S. Millerfe067e82007-03-07 12:12:44 -0800511 if (tcp_send_head(sk)) {
512 struct sk_buff *skb = tcp_write_queue_tail(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 if (!(flags & MSG_MORE) || forced_push(tp))
514 tcp_mark_push(tp, skb);
515 tcp_mark_urg(tp, flags, skb);
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -0700516 __tcp_push_pending_frames(sk, mss_now,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 (flags & MSG_MORE) ? TCP_NAGLE_CORK : nonagle);
518 }
519}
520
Adrian Bunk6ff77512007-11-06 23:32:26 -0800521static int tcp_splice_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb,
522 unsigned int offset, size_t len)
Jens Axboe9c55e012007-11-06 23:30:13 -0800523{
524 struct tcp_splice_state *tss = rd_desc->arg.data;
Willy Tarreau33966dd02009-01-13 16:04:36 -0800525 int ret;
Jens Axboe9c55e012007-11-06 23:30:13 -0800526
Dimitris Michailidis9fa5fdf2009-01-26 22:15:31 -0800527 ret = skb_splice_bits(skb, offset, tss->pipe, min(rd_desc->count, len),
528 tss->flags);
Willy Tarreau33966dd02009-01-13 16:04:36 -0800529 if (ret > 0)
530 rd_desc->count -= ret;
531 return ret;
Jens Axboe9c55e012007-11-06 23:30:13 -0800532}
533
534static int __tcp_splice_read(struct sock *sk, struct tcp_splice_state *tss)
535{
536 /* Store TCP splice context information in read_descriptor_t. */
537 read_descriptor_t rd_desc = {
538 .arg.data = tss,
Willy Tarreau33966dd02009-01-13 16:04:36 -0800539 .count = tss->len,
Jens Axboe9c55e012007-11-06 23:30:13 -0800540 };
541
542 return tcp_read_sock(sk, &rd_desc, tcp_splice_data_recv);
543}
544
545/**
546 * tcp_splice_read - splice data from TCP socket to a pipe
547 * @sock: socket to splice from
548 * @ppos: position (not valid)
549 * @pipe: pipe to splice to
550 * @len: number of bytes to splice
551 * @flags: splice modifier flags
552 *
553 * Description:
554 * Will read pages from given socket and fill them into a pipe.
555 *
556 **/
557ssize_t tcp_splice_read(struct socket *sock, loff_t *ppos,
558 struct pipe_inode_info *pipe, size_t len,
559 unsigned int flags)
560{
561 struct sock *sk = sock->sk;
562 struct tcp_splice_state tss = {
563 .pipe = pipe,
564 .len = len,
565 .flags = flags,
566 };
567 long timeo;
568 ssize_t spliced;
569 int ret;
570
571 /*
572 * We can't seek on a socket input
573 */
574 if (unlikely(*ppos))
575 return -ESPIPE;
576
577 ret = spliced = 0;
578
579 lock_sock(sk);
580
581 timeo = sock_rcvtimeo(sk, flags & SPLICE_F_NONBLOCK);
582 while (tss.len) {
583 ret = __tcp_splice_read(sk, &tss);
584 if (ret < 0)
585 break;
586 else if (!ret) {
587 if (spliced)
588 break;
Jens Axboe9c55e012007-11-06 23:30:13 -0800589 if (sock_flag(sk, SOCK_DONE))
590 break;
591 if (sk->sk_err) {
592 ret = sock_error(sk);
593 break;
594 }
595 if (sk->sk_shutdown & RCV_SHUTDOWN)
596 break;
597 if (sk->sk_state == TCP_CLOSE) {
598 /*
599 * This occurs when user tries to read
600 * from never connected socket.
601 */
602 if (!sock_flag(sk, SOCK_DONE))
603 ret = -ENOTCONN;
604 break;
605 }
606 if (!timeo) {
607 ret = -EAGAIN;
608 break;
609 }
610 sk_wait_data(sk, &timeo);
611 if (signal_pending(current)) {
612 ret = sock_intr_errno(timeo);
613 break;
614 }
615 continue;
616 }
617 tss.len -= ret;
618 spliced += ret;
619
Willy Tarreau33966dd02009-01-13 16:04:36 -0800620 if (!timeo)
621 break;
Jens Axboe9c55e012007-11-06 23:30:13 -0800622 release_sock(sk);
623 lock_sock(sk);
624
625 if (sk->sk_err || sk->sk_state == TCP_CLOSE ||
Willy Tarreau33966dd02009-01-13 16:04:36 -0800626 (sk->sk_shutdown & RCV_SHUTDOWN) ||
Jens Axboe9c55e012007-11-06 23:30:13 -0800627 signal_pending(current))
628 break;
629 }
630
631 release_sock(sk);
632
633 if (spliced)
634 return spliced;
635
636 return ret;
637}
638
Pavel Emelyanovdf97c702007-11-29 21:22:33 +1100639struct sk_buff *sk_stream_alloc_skb(struct sock *sk, int size, gfp_t gfp)
Pavel Emelyanovf561d0f2007-11-29 20:28:50 +1100640{
641 struct sk_buff *skb;
642
643 /* The TCP header must be at least 32-bit aligned. */
644 size = ALIGN(size, 4);
645
646 skb = alloc_skb_fclone(size + sk->sk_prot->max_header, gfp);
647 if (skb) {
Hideo Aoki3ab224b2007-12-31 00:11:19 -0800648 if (sk_wmem_schedule(sk, skb->truesize)) {
Pavel Emelyanovf561d0f2007-11-29 20:28:50 +1100649 /*
650 * Make sure that we have exactly size bytes
651 * available to the caller, no more, no less.
652 */
653 skb_reserve(skb, skb_tailroom(skb) - size);
654 return skb;
655 }
656 __kfree_skb(skb);
657 } else {
Pavel Emelyanov5c52ba12008-07-16 20:28:10 -0700658 sk->sk_prot->enter_memory_pressure(sk);
Pavel Emelyanovf561d0f2007-11-29 20:28:50 +1100659 sk_stream_moderate_sndbuf(sk);
660 }
661 return NULL;
662}
663
Ilpo Järvinen0c54b852009-03-14 14:23:05 +0000664static unsigned int tcp_xmit_size_goal(struct sock *sk, u32 mss_now,
665 int large_allowed)
666{
667 struct tcp_sock *tp = tcp_sk(sk);
668 u32 xmit_size_goal;
669
670 xmit_size_goal = mss_now;
671
672 if (large_allowed && sk_can_gso(sk)) {
673 xmit_size_goal = ((sk->sk_gso_max_size - 1) -
674 inet_csk(sk)->icsk_af_ops->net_header_len -
675 inet_csk(sk)->icsk_ext_hdr_len -
676 tp->tcp_header_len);
677
678 xmit_size_goal = tcp_bound_to_half_wnd(tp, xmit_size_goal);
679 xmit_size_goal -= (xmit_size_goal % mss_now);
680 }
681
682 return xmit_size_goal;
683}
684
685static int tcp_send_mss(struct sock *sk, int *size_goal, int flags)
686{
687 int mss_now;
688
689 mss_now = tcp_current_mss(sk);
690 *size_goal = tcp_xmit_size_goal(sk, mss_now, !(flags & MSG_OOB));
691
692 return mss_now;
693}
694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695static ssize_t do_tcp_sendpages(struct sock *sk, struct page **pages, int poffset,
696 size_t psize, int flags)
697{
698 struct tcp_sock *tp = tcp_sk(sk);
David S. Millerc1b4a7e2005-07-05 15:24:38 -0700699 int mss_now, size_goal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 int err;
701 ssize_t copied;
702 long timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
703
704 /* Wait for a connection to finish. */
705 if ((1 << sk->sk_state) & ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT))
706 if ((err = sk_stream_wait_connect(sk, &timeo)) != 0)
707 goto out_err;
708
709 clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
710
Ilpo Järvinen0c54b852009-03-14 14:23:05 +0000711 mss_now = tcp_send_mss(sk, &size_goal, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 copied = 0;
713
714 err = -EPIPE;
715 if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN))
Ilpo Järvinen0d6a7752009-02-28 04:44:41 +0000716 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718 while (psize > 0) {
David S. Millerfe067e82007-03-07 12:12:44 -0800719 struct sk_buff *skb = tcp_write_queue_tail(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 struct page *page = pages[poffset / PAGE_SIZE];
721 int copy, i, can_coalesce;
722 int offset = poffset % PAGE_SIZE;
723 int size = min_t(size_t, psize, PAGE_SIZE - offset);
724
David S. Millerfe067e82007-03-07 12:12:44 -0800725 if (!tcp_send_head(sk) || (copy = size_goal - skb->len) <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726new_segment:
727 if (!sk_stream_memory_free(sk))
728 goto wait_for_sndbuf;
729
Pavel Emelyanovdf97c702007-11-29 21:22:33 +1100730 skb = sk_stream_alloc_skb(sk, 0, sk->sk_allocation);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 if (!skb)
732 goto wait_for_memory;
733
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -0700734 skb_entail(sk, skb);
David S. Millerc1b4a7e2005-07-05 15:24:38 -0700735 copy = size_goal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 }
737
738 if (copy > size)
739 copy = size;
740
741 i = skb_shinfo(skb)->nr_frags;
742 can_coalesce = skb_can_coalesce(skb, i, page, offset);
743 if (!can_coalesce && i >= MAX_SKB_FRAGS) {
744 tcp_mark_push(tp, skb);
745 goto new_segment;
746 }
Hideo Aoki3ab224b2007-12-31 00:11:19 -0800747 if (!sk_wmem_schedule(sk, copy))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 goto wait_for_memory;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 if (can_coalesce) {
751 skb_shinfo(skb)->frags[i - 1].size += copy;
752 } else {
753 get_page(page);
754 skb_fill_page_desc(skb, i, page, offset, copy);
755 }
756
757 skb->len += copy;
758 skb->data_len += copy;
759 skb->truesize += copy;
760 sk->sk_wmem_queued += copy;
Hideo Aoki3ab224b2007-12-31 00:11:19 -0800761 sk_mem_charge(sk, copy);
Patrick McHardy84fa7932006-08-29 16:44:56 -0700762 skb->ip_summed = CHECKSUM_PARTIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 tp->write_seq += copy;
764 TCP_SKB_CB(skb)->end_seq += copy;
Herbert Xu79671682006-06-22 02:40:14 -0700765 skb_shinfo(skb)->gso_segs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
767 if (!copied)
768 TCP_SKB_CB(skb)->flags &= ~TCPCB_FLAG_PSH;
769
770 copied += copy;
771 poffset += copy;
772 if (!(psize -= copy))
773 goto out;
774
Herbert Xu69d15062008-03-22 15:47:05 -0700775 if (skb->len < size_goal || (flags & MSG_OOB))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 continue;
777
778 if (forced_push(tp)) {
779 tcp_mark_push(tp, skb);
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -0700780 __tcp_push_pending_frames(sk, mss_now, TCP_NAGLE_PUSH);
David S. Millerfe067e82007-03-07 12:12:44 -0800781 } else if (skb == tcp_send_head(sk))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 tcp_push_one(sk, mss_now);
783 continue;
784
785wait_for_sndbuf:
786 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
787wait_for_memory:
788 if (copied)
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -0700789 tcp_push(sk, flags & ~MSG_MORE, mss_now, TCP_NAGLE_PUSH);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 if ((err = sk_stream_wait_memory(sk, &timeo)) != 0)
792 goto do_error;
793
Ilpo Järvinen0c54b852009-03-14 14:23:05 +0000794 mss_now = tcp_send_mss(sk, &size_goal, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 }
796
797out:
798 if (copied)
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -0700799 tcp_push(sk, flags, mss_now, tp->nonagle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 return copied;
801
802do_error:
803 if (copied)
804 goto out;
805out_err:
806 return sk_stream_error(sk, flags, err);
807}
808
809ssize_t tcp_sendpage(struct socket *sock, struct page *page, int offset,
810 size_t size, int flags)
811{
812 ssize_t res;
813 struct sock *sk = sock->sk;
814
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 if (!(sk->sk_route_caps & NETIF_F_SG) ||
Herbert Xu8648b302006-06-17 22:06:05 -0700816 !(sk->sk_route_caps & NETIF_F_ALL_CSUM))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 return sock_no_sendpage(sock, page, offset, size, flags);
818
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 lock_sock(sk);
820 TCP_CHECK_TIMER(sk);
821 res = do_tcp_sendpages(sk, &page, offset, size, flags);
822 TCP_CHECK_TIMER(sk);
823 release_sock(sk);
824 return res;
825}
826
827#define TCP_PAGE(sk) (sk->sk_sndmsg_page)
828#define TCP_OFF(sk) (sk->sk_sndmsg_off)
829
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -0700830static inline int select_size(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831{
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -0700832 struct tcp_sock *tp = tcp_sk(sk);
David S. Millerc1b4a7e2005-07-05 15:24:38 -0700833 int tmp = tp->mss_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
David S. Millerb4e26f52005-07-05 15:20:27 -0700835 if (sk->sk_route_caps & NETIF_F_SG) {
Herbert Xubcd76112006-06-30 13:36:35 -0700836 if (sk_can_gso(sk))
David S. Millerb4e26f52005-07-05 15:20:27 -0700837 tmp = 0;
838 else {
839 int pgbreak = SKB_MAX_HEAD(MAX_TCP_HEADER);
840
841 if (tmp >= pgbreak &&
842 tmp <= pgbreak + (MAX_SKB_FRAGS - 1) * PAGE_SIZE)
843 tmp = pgbreak;
844 }
845 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 return tmp;
848}
849
David S. Miller3516ffb2007-08-02 19:23:56 -0700850int tcp_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 size_t size)
852{
David S. Miller3516ffb2007-08-02 19:23:56 -0700853 struct sock *sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 struct iovec *iov;
855 struct tcp_sock *tp = tcp_sk(sk);
856 struct sk_buff *skb;
857 int iovlen, flags;
David S. Millerc1b4a7e2005-07-05 15:24:38 -0700858 int mss_now, size_goal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 int err, copied;
860 long timeo;
861
862 lock_sock(sk);
863 TCP_CHECK_TIMER(sk);
864
865 flags = msg->msg_flags;
866 timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
867
868 /* Wait for a connection to finish. */
869 if ((1 << sk->sk_state) & ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT))
870 if ((err = sk_stream_wait_connect(sk, &timeo)) != 0)
871 goto out_err;
872
873 /* This should be in poll */
874 clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
875
Ilpo Järvinen0c54b852009-03-14 14:23:05 +0000876 mss_now = tcp_send_mss(sk, &size_goal, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
878 /* Ok commence sending. */
879 iovlen = msg->msg_iovlen;
880 iov = msg->msg_iov;
881 copied = 0;
882
883 err = -EPIPE;
884 if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN))
Ilpo Järvinen0d6a7752009-02-28 04:44:41 +0000885 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
887 while (--iovlen >= 0) {
888 int seglen = iov->iov_len;
889 unsigned char __user *from = iov->iov_base;
890
891 iov++;
892
893 while (seglen > 0) {
894 int copy;
895
David S. Millerfe067e82007-03-07 12:12:44 -0800896 skb = tcp_write_queue_tail(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
David S. Millerfe067e82007-03-07 12:12:44 -0800898 if (!tcp_send_head(sk) ||
David S. Millerc1b4a7e2005-07-05 15:24:38 -0700899 (copy = size_goal - skb->len) <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
901new_segment:
902 /* Allocate new segment. If the interface is SG,
903 * allocate skb fitting to single page.
904 */
905 if (!sk_stream_memory_free(sk))
906 goto wait_for_sndbuf;
907
Pavel Emelyanovdf97c702007-11-29 21:22:33 +1100908 skb = sk_stream_alloc_skb(sk, select_size(sk),
909 sk->sk_allocation);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 if (!skb)
911 goto wait_for_memory;
912
913 /*
914 * Check whether we can use HW checksum.
915 */
Herbert Xu8648b302006-06-17 22:06:05 -0700916 if (sk->sk_route_caps & NETIF_F_ALL_CSUM)
Patrick McHardy84fa7932006-08-29 16:44:56 -0700917 skb->ip_summed = CHECKSUM_PARTIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -0700919 skb_entail(sk, skb);
David S. Millerc1b4a7e2005-07-05 15:24:38 -0700920 copy = size_goal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 }
922
923 /* Try to append data to the end of skb. */
924 if (copy > seglen)
925 copy = seglen;
926
927 /* Where to copy to? */
928 if (skb_tailroom(skb) > 0) {
929 /* We have some space in skb head. Superb! */
930 if (copy > skb_tailroom(skb))
931 copy = skb_tailroom(skb);
932 if ((err = skb_add_data(skb, from, copy)) != 0)
933 goto do_fault;
934 } else {
935 int merge = 0;
936 int i = skb_shinfo(skb)->nr_frags;
937 struct page *page = TCP_PAGE(sk);
938 int off = TCP_OFF(sk);
939
940 if (skb_can_coalesce(skb, i, page, off) &&
941 off != PAGE_SIZE) {
942 /* We can extend the last page
943 * fragment. */
944 merge = 1;
945 } else if (i == MAX_SKB_FRAGS ||
946 (!i &&
947 !(sk->sk_route_caps & NETIF_F_SG))) {
948 /* Need to add new fragment and cannot
949 * do this because interface is non-SG,
950 * or because all the page slots are
951 * busy. */
952 tcp_mark_push(tp, skb);
953 goto new_segment;
954 } else if (page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 if (off == PAGE_SIZE) {
956 put_page(page);
957 TCP_PAGE(sk) = page = NULL;
Herbert Xufb5f5e62005-09-05 18:55:48 -0700958 off = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 }
Herbert Xuef015782005-09-01 17:48:59 -0700960 } else
Herbert Xufb5f5e62005-09-05 18:55:48 -0700961 off = 0;
Herbert Xuef015782005-09-01 17:48:59 -0700962
963 if (copy > PAGE_SIZE - off)
964 copy = PAGE_SIZE - off;
965
Hideo Aoki3ab224b2007-12-31 00:11:19 -0800966 if (!sk_wmem_schedule(sk, copy))
Herbert Xuef015782005-09-01 17:48:59 -0700967 goto wait_for_memory;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
969 if (!page) {
970 /* Allocate new cache page. */
971 if (!(page = sk_stream_alloc_page(sk)))
972 goto wait_for_memory;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 }
974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 /* Time to copy data. We are close to
976 * the end! */
977 err = skb_copy_to_page(sk, from, skb, page,
978 off, copy);
979 if (err) {
980 /* If this page was new, give it to the
981 * socket so it does not get leaked.
982 */
983 if (!TCP_PAGE(sk)) {
984 TCP_PAGE(sk) = page;
985 TCP_OFF(sk) = 0;
986 }
987 goto do_error;
988 }
989
990 /* Update the skb. */
991 if (merge) {
992 skb_shinfo(skb)->frags[i - 1].size +=
993 copy;
994 } else {
995 skb_fill_page_desc(skb, i, page, off, copy);
996 if (TCP_PAGE(sk)) {
997 get_page(page);
998 } else if (off + copy < PAGE_SIZE) {
999 get_page(page);
1000 TCP_PAGE(sk) = page;
1001 }
1002 }
1003
1004 TCP_OFF(sk) = off + copy;
1005 }
1006
1007 if (!copied)
1008 TCP_SKB_CB(skb)->flags &= ~TCPCB_FLAG_PSH;
1009
1010 tp->write_seq += copy;
1011 TCP_SKB_CB(skb)->end_seq += copy;
Herbert Xu79671682006-06-22 02:40:14 -07001012 skb_shinfo(skb)->gso_segs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
1014 from += copy;
1015 copied += copy;
1016 if ((seglen -= copy) == 0 && iovlen == 0)
1017 goto out;
1018
Herbert Xu69d15062008-03-22 15:47:05 -07001019 if (skb->len < size_goal || (flags & MSG_OOB))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 continue;
1021
1022 if (forced_push(tp)) {
1023 tcp_mark_push(tp, skb);
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07001024 __tcp_push_pending_frames(sk, mss_now, TCP_NAGLE_PUSH);
David S. Millerfe067e82007-03-07 12:12:44 -08001025 } else if (skb == tcp_send_head(sk))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 tcp_push_one(sk, mss_now);
1027 continue;
1028
1029wait_for_sndbuf:
1030 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1031wait_for_memory:
1032 if (copied)
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07001033 tcp_push(sk, flags & ~MSG_MORE, mss_now, TCP_NAGLE_PUSH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
1035 if ((err = sk_stream_wait_memory(sk, &timeo)) != 0)
1036 goto do_error;
1037
Ilpo Järvinen0c54b852009-03-14 14:23:05 +00001038 mss_now = tcp_send_mss(sk, &size_goal, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 }
1040 }
1041
1042out:
1043 if (copied)
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07001044 tcp_push(sk, flags, mss_now, tp->nonagle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 TCP_CHECK_TIMER(sk);
1046 release_sock(sk);
1047 return copied;
1048
1049do_fault:
1050 if (!skb->len) {
David S. Millerfe067e82007-03-07 12:12:44 -08001051 tcp_unlink_write_queue(skb, sk);
1052 /* It is the one place in all of TCP, except connection
1053 * reset, where we can be unlinking the send_head.
1054 */
1055 tcp_check_send_head(sk, skb);
Hideo Aoki3ab224b2007-12-31 00:11:19 -08001056 sk_wmem_free_skb(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 }
1058
1059do_error:
1060 if (copied)
1061 goto out;
1062out_err:
1063 err = sk_stream_error(sk, flags, err);
1064 TCP_CHECK_TIMER(sk);
1065 release_sock(sk);
1066 return err;
1067}
1068
1069/*
1070 * Handle reading urgent data. BSD has very simple semantics for
1071 * this, no blocking and very strange errors 8)
1072 */
1073
1074static int tcp_recv_urg(struct sock *sk, long timeo,
1075 struct msghdr *msg, int len, int flags,
1076 int *addr_len)
1077{
1078 struct tcp_sock *tp = tcp_sk(sk);
1079
1080 /* No URG data to read. */
1081 if (sock_flag(sk, SOCK_URGINLINE) || !tp->urg_data ||
1082 tp->urg_data == TCP_URG_READ)
1083 return -EINVAL; /* Yes this is right ! */
1084
1085 if (sk->sk_state == TCP_CLOSE && !sock_flag(sk, SOCK_DONE))
1086 return -ENOTCONN;
1087
1088 if (tp->urg_data & TCP_URG_VALID) {
1089 int err = 0;
1090 char c = tp->urg_data;
1091
1092 if (!(flags & MSG_PEEK))
1093 tp->urg_data = TCP_URG_READ;
1094
1095 /* Read urgent data. */
1096 msg->msg_flags |= MSG_OOB;
1097
1098 if (len > 0) {
1099 if (!(flags & MSG_TRUNC))
1100 err = memcpy_toiovec(msg->msg_iov, &c, 1);
1101 len = 1;
1102 } else
1103 msg->msg_flags |= MSG_TRUNC;
1104
1105 return err ? -EFAULT : len;
1106 }
1107
1108 if (sk->sk_state == TCP_CLOSE || (sk->sk_shutdown & RCV_SHUTDOWN))
1109 return 0;
1110
1111 /* Fixed the recv(..., MSG_OOB) behaviour. BSD docs and
1112 * the available implementations agree in this case:
1113 * this call should never block, independent of the
1114 * blocking state of the socket.
1115 * Mike <pall@rz.uni-karlsruhe.de>
1116 */
1117 return -EAGAIN;
1118}
1119
1120/* Clean up the receive buffer for full frames taken by the user,
1121 * then send an ACK if necessary. COPIED is the number of bytes
1122 * tcp_recvmsg has given to the user so far, it speeds up the
1123 * calculation of whether or not we must ACK for the sake of
1124 * a window update.
1125 */
Chris Leech0e4b4992006-05-23 18:00:16 -07001126void tcp_cleanup_rbuf(struct sock *sk, int copied)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127{
1128 struct tcp_sock *tp = tcp_sk(sk);
1129 int time_to_ack = 0;
1130
1131#if TCP_DEBUG
1132 struct sk_buff *skb = skb_peek(&sk->sk_receive_queue);
1133
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001134 WARN_ON(skb && !before(tp->copied_seq, TCP_SKB_CB(skb)->end_seq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135#endif
1136
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001137 if (inet_csk_ack_scheduled(sk)) {
1138 const struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 /* Delayed ACKs frequently hit locked sockets during bulk
1140 * receive. */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001141 if (icsk->icsk_ack.blocked ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 /* Once-per-two-segments ACK was not sent by tcp_input.c */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001143 tp->rcv_nxt - tp->rcv_wup > icsk->icsk_ack.rcv_mss ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 /*
1145 * If this read emptied read buffer, we send ACK, if
1146 * connection is not bidirectional, user drained
1147 * receive buffer and there was a small segment
1148 * in queue.
1149 */
Alexey Kuznetsov1ef96962006-09-19 12:52:50 -07001150 (copied > 0 &&
1151 ((icsk->icsk_ack.pending & ICSK_ACK_PUSHED2) ||
1152 ((icsk->icsk_ack.pending & ICSK_ACK_PUSHED) &&
1153 !icsk->icsk_ack.pingpong)) &&
1154 !atomic_read(&sk->sk_rmem_alloc)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 time_to_ack = 1;
1156 }
1157
1158 /* We send an ACK if we can now advertise a non-zero window
1159 * which has been raised "significantly".
1160 *
1161 * Even if window raised up to infinity, do not send window open ACK
1162 * in states, where we will not receive more. It is useless.
1163 */
1164 if (copied > 0 && !time_to_ack && !(sk->sk_shutdown & RCV_SHUTDOWN)) {
1165 __u32 rcv_window_now = tcp_receive_window(tp);
1166
1167 /* Optimize, __tcp_select_window() is not cheap. */
1168 if (2*rcv_window_now <= tp->window_clamp) {
1169 __u32 new_window = __tcp_select_window(sk);
1170
1171 /* Send ACK now, if this read freed lots of space
1172 * in our buffer. Certainly, new_window is new window.
1173 * We can advertise it now, if it is not less than current one.
1174 * "Lots" means "at least twice" here.
1175 */
1176 if (new_window && new_window >= 2 * rcv_window_now)
1177 time_to_ack = 1;
1178 }
1179 }
1180 if (time_to_ack)
1181 tcp_send_ack(sk);
1182}
1183
1184static void tcp_prequeue_process(struct sock *sk)
1185{
1186 struct sk_buff *skb;
1187 struct tcp_sock *tp = tcp_sk(sk);
1188
Pavel Emelyanov6f67c812008-07-16 20:31:39 -07001189 NET_INC_STATS_USER(sock_net(sk), LINUX_MIB_TCPPREQUEUED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
1191 /* RX process wants to run with disabled BHs, though it is not
1192 * necessary */
1193 local_bh_disable();
1194 while ((skb = __skb_dequeue(&tp->ucopy.prequeue)) != NULL)
Peter Zijlstrac57943a2008-10-07 14:18:42 -07001195 sk_backlog_rcv(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 local_bh_enable();
1197
1198 /* Clear memory counter. */
1199 tp->ucopy.memory = 0;
1200}
1201
1202static inline struct sk_buff *tcp_recv_skb(struct sock *sk, u32 seq, u32 *off)
1203{
1204 struct sk_buff *skb;
1205 u32 offset;
1206
1207 skb_queue_walk(&sk->sk_receive_queue, skb) {
1208 offset = seq - TCP_SKB_CB(skb)->seq;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07001209 if (tcp_hdr(skb)->syn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 offset--;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07001211 if (offset < skb->len || tcp_hdr(skb)->fin) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 *off = offset;
1213 return skb;
1214 }
1215 }
1216 return NULL;
1217}
1218
1219/*
1220 * This routine provides an alternative to tcp_recvmsg() for routines
1221 * that would like to handle copying from skbuffs directly in 'sendfile'
1222 * fashion.
1223 * Note:
1224 * - It is assumed that the socket was locked by the caller.
1225 * - The routine does not block.
1226 * - At present, there is no support for reading OOB data
1227 * or for 'peeking' the socket using this routine
1228 * (although both would be easy to implement).
1229 */
1230int tcp_read_sock(struct sock *sk, read_descriptor_t *desc,
1231 sk_read_actor_t recv_actor)
1232{
1233 struct sk_buff *skb;
1234 struct tcp_sock *tp = tcp_sk(sk);
1235 u32 seq = tp->copied_seq;
1236 u32 offset;
1237 int copied = 0;
1238
1239 if (sk->sk_state == TCP_LISTEN)
1240 return -ENOTCONN;
1241 while ((skb = tcp_recv_skb(sk, seq, &offset)) != NULL) {
1242 if (offset < skb->len) {
Octavian Purdila374e7b52008-07-03 03:31:21 -07001243 int used;
1244 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
1246 len = skb->len - offset;
1247 /* Stop reading if we hit a patch of urgent data */
1248 if (tp->urg_data) {
1249 u32 urg_offset = tp->urg_seq - seq;
1250 if (urg_offset < len)
1251 len = urg_offset;
1252 if (!len)
1253 break;
1254 }
1255 used = recv_actor(desc, skb, offset, len);
Jens Axboeddb61a52007-06-23 23:07:50 -07001256 if (used < 0) {
1257 if (!copied)
1258 copied = used;
1259 break;
1260 } else if (used <= len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 seq += used;
1262 copied += used;
1263 offset += used;
1264 }
Octavian Purdila293ad602008-06-04 15:45:58 -07001265 /*
1266 * If recv_actor drops the lock (e.g. TCP splice
1267 * receive) the skb pointer might be invalid when
1268 * getting here: tcp_collapse might have deleted it
1269 * while aggregating skbs from the socket queue.
1270 */
1271 skb = tcp_recv_skb(sk, seq-1, &offset);
1272 if (!skb || (offset+1 != skb->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 break;
1274 }
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07001275 if (tcp_hdr(skb)->fin) {
Chris Leech624d1162006-05-23 18:01:28 -07001276 sk_eat_skb(sk, skb, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 ++seq;
1278 break;
1279 }
Chris Leech624d1162006-05-23 18:01:28 -07001280 sk_eat_skb(sk, skb, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 if (!desc->count)
1282 break;
1283 }
1284 tp->copied_seq = seq;
1285
1286 tcp_rcv_space_adjust(sk);
1287
1288 /* Clean up data we have read: This will do ACK frames. */
Jens Axboeddb61a52007-06-23 23:07:50 -07001289 if (copied > 0)
Chris Leech0e4b4992006-05-23 18:00:16 -07001290 tcp_cleanup_rbuf(sk, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 return copied;
1292}
1293
1294/*
1295 * This routine copies from a sock struct into the user buffer.
1296 *
1297 * Technical note: in 2.3 we work on _locked_ socket, so that
1298 * tricks with *seq access order and skb->users are not required.
1299 * Probably, code can be easily improved even more.
1300 */
1301
1302int tcp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
1303 size_t len, int nonblock, int flags, int *addr_len)
1304{
1305 struct tcp_sock *tp = tcp_sk(sk);
1306 int copied = 0;
1307 u32 peek_seq;
1308 u32 *seq;
1309 unsigned long used;
1310 int err;
1311 int target; /* Read at least this many bytes */
1312 long timeo;
1313 struct task_struct *user_recv = NULL;
Chris Leech1a2449a2006-05-23 18:05:53 -07001314 int copied_early = 0;
Chris Leech2b1244a2007-03-08 09:57:36 -08001315 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
1317 lock_sock(sk);
1318
1319 TCP_CHECK_TIMER(sk);
1320
1321 err = -ENOTCONN;
1322 if (sk->sk_state == TCP_LISTEN)
1323 goto out;
1324
1325 timeo = sock_rcvtimeo(sk, nonblock);
1326
1327 /* Urgent data needs to be handled specially. */
1328 if (flags & MSG_OOB)
1329 goto recv_urg;
1330
1331 seq = &tp->copied_seq;
1332 if (flags & MSG_PEEK) {
1333 peek_seq = tp->copied_seq;
1334 seq = &peek_seq;
1335 }
1336
1337 target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
1338
Chris Leech1a2449a2006-05-23 18:05:53 -07001339#ifdef CONFIG_NET_DMA
1340 tp->ucopy.dma_chan = NULL;
1341 preempt_disable();
Chris Leech2b1244a2007-03-08 09:57:36 -08001342 skb = skb_peek_tail(&sk->sk_receive_queue);
Andrew Mortone00c5d82007-03-08 09:57:36 -08001343 {
1344 int available = 0;
1345
1346 if (skb)
1347 available = TCP_SKB_CB(skb)->seq + skb->len - (*seq);
1348 if ((available < target) &&
1349 (len > sysctl_tcp_dma_copybreak) && !(flags & MSG_PEEK) &&
1350 !sysctl_tcp_low_latency &&
Dan Williamsf67b4592009-01-06 11:38:15 -07001351 dma_find_channel(DMA_MEMCPY)) {
Andrew Mortone00c5d82007-03-08 09:57:36 -08001352 preempt_enable_no_resched();
1353 tp->ucopy.pinned_list =
1354 dma_pin_iovec_pages(msg->msg_iov, len);
1355 } else {
1356 preempt_enable_no_resched();
1357 }
1358 }
Chris Leech1a2449a2006-05-23 18:05:53 -07001359#endif
1360
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 u32 offset;
1363
1364 /* Are we at urgent data? Stop if we have read anything or have SIGURG pending. */
1365 if (tp->urg_data && tp->urg_seq == *seq) {
1366 if (copied)
1367 break;
1368 if (signal_pending(current)) {
1369 copied = timeo ? sock_intr_errno(timeo) : -EAGAIN;
1370 break;
1371 }
1372 }
1373
1374 /* Next get a buffer. */
1375
1376 skb = skb_peek(&sk->sk_receive_queue);
1377 do {
1378 if (!skb)
1379 break;
1380
1381 /* Now that we have two receive queues this
1382 * shouldn't happen.
1383 */
1384 if (before(*seq, TCP_SKB_CB(skb)->seq)) {
1385 printk(KERN_INFO "recvmsg bug: copied %X "
1386 "seq %X\n", *seq, TCP_SKB_CB(skb)->seq);
1387 break;
1388 }
1389 offset = *seq - TCP_SKB_CB(skb)->seq;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07001390 if (tcp_hdr(skb)->syn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 offset--;
1392 if (offset < skb->len)
1393 goto found_ok_skb;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07001394 if (tcp_hdr(skb)->fin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 goto found_fin_ok;
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001396 WARN_ON(!(flags & MSG_PEEK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 skb = skb->next;
1398 } while (skb != (struct sk_buff *)&sk->sk_receive_queue);
1399
1400 /* Well, if we have backlog, try to process it now yet. */
1401
1402 if (copied >= target && !sk->sk_backlog.tail)
1403 break;
1404
1405 if (copied) {
1406 if (sk->sk_err ||
1407 sk->sk_state == TCP_CLOSE ||
1408 (sk->sk_shutdown & RCV_SHUTDOWN) ||
1409 !timeo ||
David S. Miller518a09e2008-11-05 03:36:01 -08001410 signal_pending(current))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 break;
1412 } else {
1413 if (sock_flag(sk, SOCK_DONE))
1414 break;
1415
1416 if (sk->sk_err) {
1417 copied = sock_error(sk);
1418 break;
1419 }
1420
1421 if (sk->sk_shutdown & RCV_SHUTDOWN)
1422 break;
1423
1424 if (sk->sk_state == TCP_CLOSE) {
1425 if (!sock_flag(sk, SOCK_DONE)) {
1426 /* This occurs when user tries to read
1427 * from never connected socket.
1428 */
1429 copied = -ENOTCONN;
1430 break;
1431 }
1432 break;
1433 }
1434
1435 if (!timeo) {
1436 copied = -EAGAIN;
1437 break;
1438 }
1439
1440 if (signal_pending(current)) {
1441 copied = sock_intr_errno(timeo);
1442 break;
1443 }
1444 }
1445
Chris Leech0e4b4992006-05-23 18:00:16 -07001446 tcp_cleanup_rbuf(sk, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
David S. Miller7df55122005-06-18 23:01:10 -07001448 if (!sysctl_tcp_low_latency && tp->ucopy.task == user_recv) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 /* Install new reader */
1450 if (!user_recv && !(flags & (MSG_TRUNC | MSG_PEEK))) {
1451 user_recv = current;
1452 tp->ucopy.task = user_recv;
1453 tp->ucopy.iov = msg->msg_iov;
1454 }
1455
1456 tp->ucopy.len = len;
1457
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001458 WARN_ON(tp->copied_seq != tp->rcv_nxt &&
1459 !(flags & (MSG_PEEK | MSG_TRUNC)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
1461 /* Ugly... If prequeue is not empty, we have to
1462 * process it before releasing socket, otherwise
1463 * order will be broken at second iteration.
1464 * More elegant solution is required!!!
1465 *
1466 * Look: we have the following (pseudo)queues:
1467 *
1468 * 1. packets in flight
1469 * 2. backlog
1470 * 3. prequeue
1471 * 4. receive_queue
1472 *
1473 * Each queue can be processed only if the next ones
1474 * are empty. At this point we have empty receive_queue.
1475 * But prequeue _can_ be not empty after 2nd iteration,
1476 * when we jumped to start of loop because backlog
1477 * processing added something to receive_queue.
1478 * We cannot release_sock(), because backlog contains
1479 * packets arrived _after_ prequeued ones.
1480 *
1481 * Shortly, algorithm is clear --- to process all
1482 * the queues in order. We could make it more directly,
1483 * requeueing packets from backlog to prequeue, if
1484 * is not empty. It is more elegant, but eats cycles,
1485 * unfortunately.
1486 */
David S. Millerb03efcf2005-07-08 14:57:23 -07001487 if (!skb_queue_empty(&tp->ucopy.prequeue))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 goto do_prequeue;
1489
1490 /* __ Set realtime policy in scheduler __ */
1491 }
1492
1493 if (copied >= target) {
1494 /* Do not sleep, just process backlog. */
1495 release_sock(sk);
1496 lock_sock(sk);
1497 } else
1498 sk_wait_data(sk, &timeo);
1499
Chris Leech1a2449a2006-05-23 18:05:53 -07001500#ifdef CONFIG_NET_DMA
1501 tp->ucopy.wakeup = 0;
1502#endif
1503
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 if (user_recv) {
1505 int chunk;
1506
1507 /* __ Restore normal policy in scheduler __ */
1508
1509 if ((chunk = len - tp->ucopy.len) != 0) {
Pavel Emelyanoved880982008-07-16 20:32:45 -07001510 NET_ADD_STATS_USER(sock_net(sk), LINUX_MIB_TCPDIRECTCOPYFROMBACKLOG, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 len -= chunk;
1512 copied += chunk;
1513 }
1514
1515 if (tp->rcv_nxt == tp->copied_seq &&
David S. Millerb03efcf2005-07-08 14:57:23 -07001516 !skb_queue_empty(&tp->ucopy.prequeue)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517do_prequeue:
1518 tcp_prequeue_process(sk);
1519
1520 if ((chunk = len - tp->ucopy.len) != 0) {
Pavel Emelyanoved880982008-07-16 20:32:45 -07001521 NET_ADD_STATS_USER(sock_net(sk), LINUX_MIB_TCPDIRECTCOPYFROMPREQUEUE, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 len -= chunk;
1523 copied += chunk;
1524 }
1525 }
1526 }
1527 if ((flags & MSG_PEEK) && peek_seq != tp->copied_seq) {
1528 if (net_ratelimit())
1529 printk(KERN_DEBUG "TCP(%s:%d): Application bug, race in MSG_PEEK.\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001530 current->comm, task_pid_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 peek_seq = tp->copied_seq;
1532 }
1533 continue;
1534
1535 found_ok_skb:
1536 /* Ok so how much can we use? */
1537 used = skb->len - offset;
1538 if (len < used)
1539 used = len;
1540
1541 /* Do we have urgent data here? */
1542 if (tp->urg_data) {
1543 u32 urg_offset = tp->urg_seq - *seq;
1544 if (urg_offset < used) {
1545 if (!urg_offset) {
1546 if (!sock_flag(sk, SOCK_URGINLINE)) {
1547 ++*seq;
1548 offset++;
1549 used--;
1550 if (!used)
1551 goto skip_copy;
1552 }
1553 } else
1554 used = urg_offset;
1555 }
1556 }
1557
1558 if (!(flags & MSG_TRUNC)) {
Chris Leech1a2449a2006-05-23 18:05:53 -07001559#ifdef CONFIG_NET_DMA
1560 if (!tp->ucopy.dma_chan && tp->ucopy.pinned_list)
Dan Williamsf67b4592009-01-06 11:38:15 -07001561 tp->ucopy.dma_chan = dma_find_channel(DMA_MEMCPY);
Chris Leech1a2449a2006-05-23 18:05:53 -07001562
1563 if (tp->ucopy.dma_chan) {
1564 tp->ucopy.dma_cookie = dma_skb_copy_datagram_iovec(
1565 tp->ucopy.dma_chan, skb, offset,
1566 msg->msg_iov, used,
1567 tp->ucopy.pinned_list);
1568
1569 if (tp->ucopy.dma_cookie < 0) {
1570
1571 printk(KERN_ALERT "dma_cookie < 0\n");
1572
1573 /* Exception. Bailout! */
1574 if (!copied)
1575 copied = -EFAULT;
1576 break;
1577 }
1578 if ((offset + used) == skb->len)
1579 copied_early = 1;
1580
1581 } else
1582#endif
1583 {
1584 err = skb_copy_datagram_iovec(skb, offset,
1585 msg->msg_iov, used);
1586 if (err) {
1587 /* Exception. Bailout! */
1588 if (!copied)
1589 copied = -EFAULT;
1590 break;
1591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 }
1593 }
1594
1595 *seq += used;
1596 copied += used;
1597 len -= used;
1598
1599 tcp_rcv_space_adjust(sk);
1600
1601skip_copy:
1602 if (tp->urg_data && after(tp->copied_seq, tp->urg_seq)) {
1603 tp->urg_data = 0;
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07001604 tcp_fast_path_check(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 }
1606 if (used + offset < skb->len)
1607 continue;
1608
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07001609 if (tcp_hdr(skb)->fin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 goto found_fin_ok;
Chris Leech1a2449a2006-05-23 18:05:53 -07001611 if (!(flags & MSG_PEEK)) {
1612 sk_eat_skb(sk, skb, copied_early);
1613 copied_early = 0;
1614 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 continue;
1616
1617 found_fin_ok:
1618 /* Process the FIN. */
1619 ++*seq;
Chris Leech1a2449a2006-05-23 18:05:53 -07001620 if (!(flags & MSG_PEEK)) {
1621 sk_eat_skb(sk, skb, copied_early);
1622 copied_early = 0;
1623 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 break;
1625 } while (len > 0);
1626
1627 if (user_recv) {
David S. Millerb03efcf2005-07-08 14:57:23 -07001628 if (!skb_queue_empty(&tp->ucopy.prequeue)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 int chunk;
1630
1631 tp->ucopy.len = copied > 0 ? len : 0;
1632
1633 tcp_prequeue_process(sk);
1634
1635 if (copied > 0 && (chunk = len - tp->ucopy.len) != 0) {
Pavel Emelyanoved880982008-07-16 20:32:45 -07001636 NET_ADD_STATS_USER(sock_net(sk), LINUX_MIB_TCPDIRECTCOPYFROMPREQUEUE, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 len -= chunk;
1638 copied += chunk;
1639 }
1640 }
1641
1642 tp->ucopy.task = NULL;
1643 tp->ucopy.len = 0;
1644 }
1645
Chris Leech1a2449a2006-05-23 18:05:53 -07001646#ifdef CONFIG_NET_DMA
1647 if (tp->ucopy.dma_chan) {
Chris Leech1a2449a2006-05-23 18:05:53 -07001648 dma_cookie_t done, used;
1649
1650 dma_async_memcpy_issue_pending(tp->ucopy.dma_chan);
1651
1652 while (dma_async_memcpy_complete(tp->ucopy.dma_chan,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001653 tp->ucopy.dma_cookie, &done,
1654 &used) == DMA_IN_PROGRESS) {
Chris Leech1a2449a2006-05-23 18:05:53 -07001655 /* do partial cleanup of sk_async_wait_queue */
1656 while ((skb = skb_peek(&sk->sk_async_wait_queue)) &&
1657 (dma_async_is_complete(skb->dma_cookie, done,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001658 used) == DMA_SUCCESS)) {
Chris Leech1a2449a2006-05-23 18:05:53 -07001659 __skb_dequeue(&sk->sk_async_wait_queue);
1660 kfree_skb(skb);
1661 }
1662 }
1663
1664 /* Safe to free early-copied skbs now */
1665 __skb_queue_purge(&sk->sk_async_wait_queue);
Chris Leech1a2449a2006-05-23 18:05:53 -07001666 tp->ucopy.dma_chan = NULL;
1667 }
1668 if (tp->ucopy.pinned_list) {
1669 dma_unpin_iovec_pages(tp->ucopy.pinned_list);
1670 tp->ucopy.pinned_list = NULL;
1671 }
1672#endif
1673
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 /* According to UNIX98, msg_name/msg_namelen are ignored
1675 * on connected socket. I was just happy when found this 8) --ANK
1676 */
1677
1678 /* Clean up data we have read: This will do ACK frames. */
Chris Leech0e4b4992006-05-23 18:00:16 -07001679 tcp_cleanup_rbuf(sk, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
1681 TCP_CHECK_TIMER(sk);
1682 release_sock(sk);
1683 return copied;
1684
1685out:
1686 TCP_CHECK_TIMER(sk);
1687 release_sock(sk);
1688 return err;
1689
1690recv_urg:
1691 err = tcp_recv_urg(sk, timeo, msg, len, flags, addr_len);
1692 goto out;
1693}
1694
Ilpo Järvinen490d5042008-01-12 03:17:20 -08001695void tcp_set_state(struct sock *sk, int state)
1696{
1697 int oldstate = sk->sk_state;
1698
1699 switch (state) {
1700 case TCP_ESTABLISHED:
1701 if (oldstate != TCP_ESTABLISHED)
Pavel Emelyanov81cc8a72008-07-16 20:22:04 -07001702 TCP_INC_STATS(sock_net(sk), TCP_MIB_CURRESTAB);
Ilpo Järvinen490d5042008-01-12 03:17:20 -08001703 break;
1704
1705 case TCP_CLOSE:
1706 if (oldstate == TCP_CLOSE_WAIT || oldstate == TCP_ESTABLISHED)
Pavel Emelyanov81cc8a72008-07-16 20:22:04 -07001707 TCP_INC_STATS(sock_net(sk), TCP_MIB_ESTABRESETS);
Ilpo Järvinen490d5042008-01-12 03:17:20 -08001708
1709 sk->sk_prot->unhash(sk);
1710 if (inet_csk(sk)->icsk_bind_hash &&
1711 !(sk->sk_userlocks & SOCK_BINDPORT_LOCK))
Arnaldo Carvalho de Meloab1e0a12008-02-03 04:06:04 -08001712 inet_put_port(sk);
Ilpo Järvinen490d5042008-01-12 03:17:20 -08001713 /* fall through */
1714 default:
Jianjun Kong5a5f3a82008-11-03 00:24:34 -08001715 if (oldstate == TCP_ESTABLISHED)
Pavel Emelyanov74688e42008-07-16 20:22:46 -07001716 TCP_DEC_STATS(sock_net(sk), TCP_MIB_CURRESTAB);
Ilpo Järvinen490d5042008-01-12 03:17:20 -08001717 }
1718
1719 /* Change state AFTER socket is unhashed to avoid closed
1720 * socket sitting in hash tables.
1721 */
1722 sk->sk_state = state;
1723
1724#ifdef STATE_TRACE
Jianjun Kong5a5f3a82008-11-03 00:24:34 -08001725 SOCK_DEBUG(sk, "TCP sk=%p, State %s -> %s\n", sk, statename[oldstate], statename[state]);
Ilpo Järvinen490d5042008-01-12 03:17:20 -08001726#endif
1727}
1728EXPORT_SYMBOL_GPL(tcp_set_state);
1729
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730/*
1731 * State processing on a close. This implements the state shift for
1732 * sending our FIN frame. Note that we only send a FIN for some
1733 * states. A shutdown() may have already sent the FIN, or we may be
1734 * closed.
1735 */
1736
Arjan van de Ven9b5b5cf2005-11-29 16:21:38 -08001737static const unsigned char new_state[16] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 /* current state: new state: action: */
1739 /* (Invalid) */ TCP_CLOSE,
1740 /* TCP_ESTABLISHED */ TCP_FIN_WAIT1 | TCP_ACTION_FIN,
1741 /* TCP_SYN_SENT */ TCP_CLOSE,
1742 /* TCP_SYN_RECV */ TCP_FIN_WAIT1 | TCP_ACTION_FIN,
1743 /* TCP_FIN_WAIT1 */ TCP_FIN_WAIT1,
1744 /* TCP_FIN_WAIT2 */ TCP_FIN_WAIT2,
1745 /* TCP_TIME_WAIT */ TCP_CLOSE,
1746 /* TCP_CLOSE */ TCP_CLOSE,
1747 /* TCP_CLOSE_WAIT */ TCP_LAST_ACK | TCP_ACTION_FIN,
1748 /* TCP_LAST_ACK */ TCP_LAST_ACK,
1749 /* TCP_LISTEN */ TCP_CLOSE,
1750 /* TCP_CLOSING */ TCP_CLOSING,
1751};
1752
1753static int tcp_close_state(struct sock *sk)
1754{
1755 int next = (int)new_state[sk->sk_state];
1756 int ns = next & TCP_STATE_MASK;
1757
1758 tcp_set_state(sk, ns);
1759
1760 return next & TCP_ACTION_FIN;
1761}
1762
1763/*
1764 * Shutdown the sending side of a connection. Much like close except
Satoru SATOH1f29b052008-04-21 02:27:58 -07001765 * that we don't receive shut down or sock_set_flag(sk, SOCK_DEAD).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 */
1767
1768void tcp_shutdown(struct sock *sk, int how)
1769{
1770 /* We need to grab some memory, and put together a FIN,
1771 * and then put it into the queue to be sent.
1772 * Tim MacKenzie(tym@dibbler.cs.monash.edu.au) 4 Dec '92.
1773 */
1774 if (!(how & SEND_SHUTDOWN))
1775 return;
1776
1777 /* If we've already sent a FIN, or it's a closed state, skip this. */
1778 if ((1 << sk->sk_state) &
1779 (TCPF_ESTABLISHED | TCPF_SYN_SENT |
1780 TCPF_SYN_RECV | TCPF_CLOSE_WAIT)) {
1781 /* Clear out any half completed packets. FIN if needed. */
1782 if (tcp_close_state(sk))
1783 tcp_send_fin(sk);
1784 }
1785}
1786
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787void tcp_close(struct sock *sk, long timeout)
1788{
1789 struct sk_buff *skb;
1790 int data_was_unread = 0;
Herbert Xu75c2d9072006-05-03 23:31:35 -07001791 int state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
1793 lock_sock(sk);
1794 sk->sk_shutdown = SHUTDOWN_MASK;
1795
1796 if (sk->sk_state == TCP_LISTEN) {
1797 tcp_set_state(sk, TCP_CLOSE);
1798
1799 /* Special case. */
Arnaldo Carvalho de Melo0a5578c2005-08-09 20:11:41 -07001800 inet_csk_listen_stop(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
1802 goto adjudge_to_death;
1803 }
1804
1805 /* We need to flush the recv. buffs. We do this only on the
1806 * descriptor close, not protocol-sourced closes, because the
1807 * reader process may not have drained the data yet!
1808 */
1809 while ((skb = __skb_dequeue(&sk->sk_receive_queue)) != NULL) {
1810 u32 len = TCP_SKB_CB(skb)->end_seq - TCP_SKB_CB(skb)->seq -
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07001811 tcp_hdr(skb)->fin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 data_was_unread += len;
1813 __kfree_skb(skb);
1814 }
1815
Hideo Aoki3ab224b2007-12-31 00:11:19 -08001816 sk_mem_reclaim(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
Gerrit Renker65bb7232007-04-28 21:21:46 -07001818 /* As outlined in RFC 2525, section 2.17, we send a RST here because
1819 * data was lost. To witness the awful effects of the old behavior of
1820 * always doing a FIN, run an older 2.1.x kernel or 2.0.x, start a bulk
1821 * GET in an FTP client, suspend the process, wait for the client to
1822 * advertise a zero window, then kill -9 the FTP client, wheee...
1823 * Note: timeout is always zero in such a case.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 */
1825 if (data_was_unread) {
1826 /* Unread data was tossed, zap the connection. */
Pavel Emelyanov6f67c812008-07-16 20:31:39 -07001827 NET_INC_STATS_USER(sock_net(sk), LINUX_MIB_TCPABORTONCLOSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 tcp_set_state(sk, TCP_CLOSE);
1829 tcp_send_active_reset(sk, GFP_KERNEL);
1830 } else if (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime) {
1831 /* Check zero linger _after_ checking for unread data. */
1832 sk->sk_prot->disconnect(sk, 0);
Pavel Emelyanov6f67c812008-07-16 20:31:39 -07001833 NET_INC_STATS_USER(sock_net(sk), LINUX_MIB_TCPABORTONDATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 } else if (tcp_close_state(sk)) {
1835 /* We FIN if the application ate all the data before
1836 * zapping the connection.
1837 */
1838
1839 /* RED-PEN. Formally speaking, we have broken TCP state
1840 * machine. State transitions:
1841 *
1842 * TCP_ESTABLISHED -> TCP_FIN_WAIT1
1843 * TCP_SYN_RECV -> TCP_FIN_WAIT1 (forget it, it's impossible)
1844 * TCP_CLOSE_WAIT -> TCP_LAST_ACK
1845 *
1846 * are legal only when FIN has been sent (i.e. in window),
1847 * rather than queued out of window. Purists blame.
1848 *
1849 * F.e. "RFC state" is ESTABLISHED,
1850 * if Linux state is FIN-WAIT-1, but FIN is still not sent.
1851 *
1852 * The visible declinations are that sometimes
1853 * we enter time-wait state, when it is not required really
1854 * (harmless), do not send active resets, when they are
1855 * required by specs (TCP_ESTABLISHED, TCP_CLOSE_WAIT, when
1856 * they look as CLOSING or LAST_ACK for Linux)
1857 * Probably, I missed some more holelets.
1858 * --ANK
1859 */
1860 tcp_send_fin(sk);
1861 }
1862
1863 sk_stream_wait_close(sk, timeout);
1864
1865adjudge_to_death:
Herbert Xu75c2d9072006-05-03 23:31:35 -07001866 state = sk->sk_state;
1867 sock_hold(sk);
1868 sock_orphan(sk);
Herbert Xu75c2d9072006-05-03 23:31:35 -07001869
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 /* It is the last release_sock in its life. It will remove backlog. */
1871 release_sock(sk);
1872
1873
1874 /* Now socket is owned by kernel and we acquire BH lock
1875 to finish close. No need to check for user refs.
1876 */
1877 local_bh_disable();
1878 bh_lock_sock(sk);
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001879 WARN_ON(sock_owned_by_user(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
Herbert Xueb4dea52008-12-29 23:04:08 -08001881 percpu_counter_inc(sk->sk_prot->orphan_count);
1882
Herbert Xu75c2d9072006-05-03 23:31:35 -07001883 /* Have we already been destroyed by a softirq or backlog? */
1884 if (state != TCP_CLOSE && sk->sk_state == TCP_CLOSE)
1885 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
1887 /* This is a (useful) BSD violating of the RFC. There is a
1888 * problem with TCP as specified in that the other end could
1889 * keep a socket open forever with no application left this end.
1890 * We use a 3 minute timeout (about the same as BSD) then kill
1891 * our end. If they send after that then tough - BUT: long enough
1892 * that we won't make the old 4*rto = almost no time - whoops
1893 * reset mistake.
1894 *
1895 * Nope, it was not mistake. It is really desired behaviour
1896 * f.e. on http servers, when such sockets are useless, but
1897 * consume significant resources. Let's do it with special
1898 * linger2 option. --ANK
1899 */
1900
1901 if (sk->sk_state == TCP_FIN_WAIT2) {
1902 struct tcp_sock *tp = tcp_sk(sk);
1903 if (tp->linger2 < 0) {
1904 tcp_set_state(sk, TCP_CLOSE);
1905 tcp_send_active_reset(sk, GFP_ATOMIC);
Pavel Emelyanovde0744a2008-07-16 20:31:16 -07001906 NET_INC_STATS_BH(sock_net(sk),
1907 LINUX_MIB_TCPABORTONLINGER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 } else {
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001909 const int tmo = tcp_fin_time(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
1911 if (tmo > TCP_TIMEWAIT_LEN) {
David S. Miller52499af2006-07-31 22:32:09 -07001912 inet_csk_reset_keepalive_timer(sk,
1913 tmo - TCP_TIMEWAIT_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 tcp_time_wait(sk, TCP_FIN_WAIT2, tmo);
1916 goto out;
1917 }
1918 }
1919 }
1920 if (sk->sk_state != TCP_CLOSE) {
Eric Dumazetdd24c002008-11-25 21:17:14 -08001921 int orphan_count = percpu_counter_read_positive(
1922 sk->sk_prot->orphan_count);
1923
Hideo Aoki3ab224b2007-12-31 00:11:19 -08001924 sk_mem_reclaim(sk);
Eric Dumazetdd24c002008-11-25 21:17:14 -08001925 if (tcp_too_many_orphans(sk, orphan_count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 if (net_ratelimit())
1927 printk(KERN_INFO "TCP: too many of orphaned "
1928 "sockets\n");
1929 tcp_set_state(sk, TCP_CLOSE);
1930 tcp_send_active_reset(sk, GFP_ATOMIC);
Pavel Emelyanovde0744a2008-07-16 20:31:16 -07001931 NET_INC_STATS_BH(sock_net(sk),
1932 LINUX_MIB_TCPABORTONMEMORY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 }
1934 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
1936 if (sk->sk_state == TCP_CLOSE)
Arnaldo Carvalho de Melo0a5578c2005-08-09 20:11:41 -07001937 inet_csk_destroy_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 /* Otherwise, socket is reprieved until protocol close. */
1939
1940out:
1941 bh_unlock_sock(sk);
1942 local_bh_enable();
1943 sock_put(sk);
1944}
1945
1946/* These states need RST on ABORT according to RFC793 */
1947
1948static inline int tcp_need_reset(int state)
1949{
1950 return (1 << state) &
1951 (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT | TCPF_FIN_WAIT1 |
1952 TCPF_FIN_WAIT2 | TCPF_SYN_RECV);
1953}
1954
1955int tcp_disconnect(struct sock *sk, int flags)
1956{
1957 struct inet_sock *inet = inet_sk(sk);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001958 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 struct tcp_sock *tp = tcp_sk(sk);
1960 int err = 0;
1961 int old_state = sk->sk_state;
1962
1963 if (old_state != TCP_CLOSE)
1964 tcp_set_state(sk, TCP_CLOSE);
1965
1966 /* ABORT function of RFC793 */
1967 if (old_state == TCP_LISTEN) {
Arnaldo Carvalho de Melo0a5578c2005-08-09 20:11:41 -07001968 inet_csk_listen_stop(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 } else if (tcp_need_reset(old_state) ||
1970 (tp->snd_nxt != tp->write_seq &&
1971 (1 << old_state) & (TCPF_CLOSING | TCPF_LAST_ACK))) {
Stephen Hemmingercaa20d9a2005-11-10 17:13:47 -08001972 /* The last check adjusts for discrepancy of Linux wrt. RFC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 * states
1974 */
1975 tcp_send_active_reset(sk, gfp_any());
1976 sk->sk_err = ECONNRESET;
1977 } else if (old_state == TCP_SYN_SENT)
1978 sk->sk_err = ECONNRESET;
1979
1980 tcp_clear_xmit_timers(sk);
1981 __skb_queue_purge(&sk->sk_receive_queue);
David S. Millerfe067e82007-03-07 12:12:44 -08001982 tcp_write_queue_purge(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 __skb_queue_purge(&tp->out_of_order_queue);
Chris Leech1a2449a2006-05-23 18:05:53 -07001984#ifdef CONFIG_NET_DMA
1985 __skb_queue_purge(&sk->sk_async_wait_queue);
1986#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
1988 inet->dport = 0;
1989
1990 if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK))
1991 inet_reset_saddr(sk);
1992
1993 sk->sk_shutdown = 0;
1994 sock_reset_flag(sk, SOCK_DONE);
1995 tp->srtt = 0;
1996 if ((tp->write_seq += tp->max_window + 2) == 0)
1997 tp->write_seq = 1;
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001998 icsk->icsk_backoff = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 tp->snd_cwnd = 2;
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002000 icsk->icsk_probes_out = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 tp->packets_out = 0;
2002 tp->snd_ssthresh = 0x7fffffff;
2003 tp->snd_cwnd_cnt = 0;
Stephen Hemminger9772efb2005-11-10 17:09:53 -08002004 tp->bytes_acked = 0;
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002005 tcp_set_ca_state(sk, TCP_CA_Open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 tcp_clear_retrans(tp);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002007 inet_csk_delack_init(sk);
David S. Millerfe067e82007-03-07 12:12:44 -08002008 tcp_init_send_head(sk);
Srinivas Ajib40b4f72007-05-03 17:32:28 -07002009 memset(&tp->rx_opt, 0, sizeof(tp->rx_opt));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 __sk_dst_reset(sk);
2011
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002012 WARN_ON(inet->num && !icsk->icsk_bind_hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
2014 sk->sk_error_report(sk);
2015 return err;
2016}
2017
2018/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 * Socket option code for TCP.
2020 */
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08002021static int do_tcp_setsockopt(struct sock *sk, int level,
2022 int optname, char __user *optval, int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023{
2024 struct tcp_sock *tp = tcp_sk(sk);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002025 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 int val;
2027 int err = 0;
2028
Stephen Hemminger5f8ef482005-06-23 20:37:36 -07002029 /* This is a string value all the others are int's */
2030 if (optname == TCP_CONGESTION) {
2031 char name[TCP_CA_NAME_MAX];
2032
2033 if (optlen < 1)
2034 return -EINVAL;
2035
2036 val = strncpy_from_user(name, optval,
2037 min(TCP_CA_NAME_MAX-1, optlen));
2038 if (val < 0)
2039 return -EFAULT;
2040 name[val] = 0;
2041
2042 lock_sock(sk);
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002043 err = tcp_set_congestion_control(sk, name);
Stephen Hemminger5f8ef482005-06-23 20:37:36 -07002044 release_sock(sk);
2045 return err;
2046 }
2047
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 if (optlen < sizeof(int))
2049 return -EINVAL;
2050
2051 if (get_user(val, (int __user *)optval))
2052 return -EFAULT;
2053
2054 lock_sock(sk);
2055
2056 switch (optname) {
2057 case TCP_MAXSEG:
2058 /* Values greater than interface MTU won't take effect. However
2059 * at the point when this call is done we typically don't yet
2060 * know which interface is going to be used */
2061 if (val < 8 || val > MAX_TCP_WINDOW) {
2062 err = -EINVAL;
2063 break;
2064 }
2065 tp->rx_opt.user_mss = val;
2066 break;
2067
2068 case TCP_NODELAY:
2069 if (val) {
2070 /* TCP_NODELAY is weaker than TCP_CORK, so that
2071 * this option on corked socket is remembered, but
2072 * it is not activated until cork is cleared.
2073 *
2074 * However, when TCP_NODELAY is set we make
2075 * an explicit push, which overrides even TCP_CORK
2076 * for currently queued segments.
2077 */
2078 tp->nonagle |= TCP_NAGLE_OFF|TCP_NAGLE_PUSH;
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07002079 tcp_push_pending_frames(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 } else {
2081 tp->nonagle &= ~TCP_NAGLE_OFF;
2082 }
2083 break;
2084
2085 case TCP_CORK:
2086 /* When set indicates to always queue non-full frames.
2087 * Later the user clears this option and we transmit
2088 * any pending partial frames in the queue. This is
2089 * meant to be used alongside sendfile() to get properly
2090 * filled frames when the user (for example) must write
2091 * out headers with a write() call first and then use
2092 * sendfile to send out the data parts.
2093 *
2094 * TCP_CORK can be set together with TCP_NODELAY and it is
2095 * stronger than TCP_NODELAY.
2096 */
2097 if (val) {
2098 tp->nonagle |= TCP_NAGLE_CORK;
2099 } else {
2100 tp->nonagle &= ~TCP_NAGLE_CORK;
2101 if (tp->nonagle&TCP_NAGLE_OFF)
2102 tp->nonagle |= TCP_NAGLE_PUSH;
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07002103 tcp_push_pending_frames(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 }
2105 break;
2106
2107 case TCP_KEEPIDLE:
2108 if (val < 1 || val > MAX_TCP_KEEPIDLE)
2109 err = -EINVAL;
2110 else {
2111 tp->keepalive_time = val * HZ;
2112 if (sock_flag(sk, SOCK_KEEPOPEN) &&
2113 !((1 << sk->sk_state) &
2114 (TCPF_CLOSE | TCPF_LISTEN))) {
2115 __u32 elapsed = tcp_time_stamp - tp->rcv_tstamp;
2116 if (tp->keepalive_time > elapsed)
2117 elapsed = tp->keepalive_time - elapsed;
2118 else
2119 elapsed = 0;
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002120 inet_csk_reset_keepalive_timer(sk, elapsed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 }
2122 }
2123 break;
2124 case TCP_KEEPINTVL:
2125 if (val < 1 || val > MAX_TCP_KEEPINTVL)
2126 err = -EINVAL;
2127 else
2128 tp->keepalive_intvl = val * HZ;
2129 break;
2130 case TCP_KEEPCNT:
2131 if (val < 1 || val > MAX_TCP_KEEPCNT)
2132 err = -EINVAL;
2133 else
2134 tp->keepalive_probes = val;
2135 break;
2136 case TCP_SYNCNT:
2137 if (val < 1 || val > MAX_TCP_SYNCNT)
2138 err = -EINVAL;
2139 else
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002140 icsk->icsk_syn_retries = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 break;
2142
2143 case TCP_LINGER2:
2144 if (val < 0)
2145 tp->linger2 = -1;
2146 else if (val > sysctl_tcp_fin_timeout / HZ)
2147 tp->linger2 = 0;
2148 else
2149 tp->linger2 = val * HZ;
2150 break;
2151
2152 case TCP_DEFER_ACCEPT:
David S. Millerec0a1962008-06-12 16:31:35 -07002153 icsk->icsk_accept_queue.rskq_defer_accept = 0;
2154 if (val > 0) {
2155 /* Translate value in seconds to number of
2156 * retransmits */
2157 while (icsk->icsk_accept_queue.rskq_defer_accept < 32 &&
2158 val > ((TCP_TIMEOUT_INIT / HZ) <<
2159 icsk->icsk_accept_queue.rskq_defer_accept))
2160 icsk->icsk_accept_queue.rskq_defer_accept++;
2161 icsk->icsk_accept_queue.rskq_defer_accept++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 }
2163 break;
2164
2165 case TCP_WINDOW_CLAMP:
2166 if (!val) {
2167 if (sk->sk_state != TCP_CLOSE) {
2168 err = -EINVAL;
2169 break;
2170 }
2171 tp->window_clamp = 0;
2172 } else
2173 tp->window_clamp = val < SOCK_MIN_RCVBUF / 2 ?
2174 SOCK_MIN_RCVBUF / 2 : val;
2175 break;
2176
2177 case TCP_QUICKACK:
2178 if (!val) {
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002179 icsk->icsk_ack.pingpong = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 } else {
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002181 icsk->icsk_ack.pingpong = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 if ((1 << sk->sk_state) &
2183 (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT) &&
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002184 inet_csk_ack_scheduled(sk)) {
2185 icsk->icsk_ack.pending |= ICSK_ACK_PUSHED;
Chris Leech0e4b4992006-05-23 18:00:16 -07002186 tcp_cleanup_rbuf(sk, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 if (!(val & 1))
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002188 icsk->icsk_ack.pingpong = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 }
2190 }
2191 break;
2192
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08002193#ifdef CONFIG_TCP_MD5SIG
2194 case TCP_MD5SIG:
2195 /* Read the IP->Key mappings from userspace */
2196 err = tp->af_specific->md5_parse(sk, optval, optlen);
2197 break;
2198#endif
2199
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 default:
2201 err = -ENOPROTOOPT;
2202 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002203 }
2204
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 release_sock(sk);
2206 return err;
2207}
2208
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08002209int tcp_setsockopt(struct sock *sk, int level, int optname, char __user *optval,
2210 int optlen)
2211{
2212 struct inet_connection_sock *icsk = inet_csk(sk);
2213
2214 if (level != SOL_TCP)
2215 return icsk->icsk_af_ops->setsockopt(sk, level, optname,
2216 optval, optlen);
2217 return do_tcp_setsockopt(sk, level, optname, optval, optlen);
2218}
2219
2220#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08002221int compat_tcp_setsockopt(struct sock *sk, int level, int optname,
2222 char __user *optval, int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08002223{
Arnaldo Carvalho de Melodec73ff2006-03-20 22:46:16 -08002224 if (level != SOL_TCP)
2225 return inet_csk_compat_setsockopt(sk, level, optname,
2226 optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08002227 return do_tcp_setsockopt(sk, level, optname, optval, optlen);
2228}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08002229
2230EXPORT_SYMBOL(compat_tcp_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08002231#endif
2232
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233/* Return information about state of tcp endpoint in API format. */
2234void tcp_get_info(struct sock *sk, struct tcp_info *info)
2235{
2236 struct tcp_sock *tp = tcp_sk(sk);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002237 const struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 u32 now = tcp_time_stamp;
2239
2240 memset(info, 0, sizeof(*info));
2241
2242 info->tcpi_state = sk->sk_state;
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002243 info->tcpi_ca_state = icsk->icsk_ca_state;
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002244 info->tcpi_retransmits = icsk->icsk_retransmits;
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002245 info->tcpi_probes = icsk->icsk_probes_out;
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002246 info->tcpi_backoff = icsk->icsk_backoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247
2248 if (tp->rx_opt.tstamp_ok)
2249 info->tcpi_options |= TCPI_OPT_TIMESTAMPS;
Ilpo Järvinene60402d2007-08-09 15:14:46 +03002250 if (tcp_is_sack(tp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 info->tcpi_options |= TCPI_OPT_SACK;
2252 if (tp->rx_opt.wscale_ok) {
2253 info->tcpi_options |= TCPI_OPT_WSCALE;
2254 info->tcpi_snd_wscale = tp->rx_opt.snd_wscale;
2255 info->tcpi_rcv_wscale = tp->rx_opt.rcv_wscale;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257
2258 if (tp->ecn_flags&TCP_ECN_OK)
2259 info->tcpi_options |= TCPI_OPT_ECN;
2260
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002261 info->tcpi_rto = jiffies_to_usecs(icsk->icsk_rto);
2262 info->tcpi_ato = jiffies_to_usecs(icsk->icsk_ack.ato);
David S. Millerc1b4a7e2005-07-05 15:24:38 -07002263 info->tcpi_snd_mss = tp->mss_cache;
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002264 info->tcpi_rcv_mss = icsk->icsk_ack.rcv_mss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265
Rick Jones5ee3afb2007-09-18 13:26:31 -07002266 if (sk->sk_state == TCP_LISTEN) {
2267 info->tcpi_unacked = sk->sk_ack_backlog;
2268 info->tcpi_sacked = sk->sk_max_ack_backlog;
2269 } else {
2270 info->tcpi_unacked = tp->packets_out;
2271 info->tcpi_sacked = tp->sacked_out;
2272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 info->tcpi_lost = tp->lost_out;
2274 info->tcpi_retrans = tp->retrans_out;
2275 info->tcpi_fackets = tp->fackets_out;
2276
2277 info->tcpi_last_data_sent = jiffies_to_msecs(now - tp->lsndtime);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002278 info->tcpi_last_data_recv = jiffies_to_msecs(now - icsk->icsk_ack.lrcvtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 info->tcpi_last_ack_recv = jiffies_to_msecs(now - tp->rcv_tstamp);
2280
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -08002281 info->tcpi_pmtu = icsk->icsk_pmtu_cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 info->tcpi_rcv_ssthresh = tp->rcv_ssthresh;
2283 info->tcpi_rtt = jiffies_to_usecs(tp->srtt)>>3;
2284 info->tcpi_rttvar = jiffies_to_usecs(tp->mdev)>>2;
2285 info->tcpi_snd_ssthresh = tp->snd_ssthresh;
2286 info->tcpi_snd_cwnd = tp->snd_cwnd;
2287 info->tcpi_advmss = tp->advmss;
2288 info->tcpi_reordering = tp->reordering;
2289
2290 info->tcpi_rcv_rtt = jiffies_to_usecs(tp->rcv_rtt_est.rtt)>>3;
2291 info->tcpi_rcv_space = tp->rcvq_space.space;
2292
2293 info->tcpi_total_retrans = tp->total_retrans;
2294}
2295
2296EXPORT_SYMBOL_GPL(tcp_get_info);
2297
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08002298static int do_tcp_getsockopt(struct sock *sk, int level,
2299 int optname, char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300{
Arnaldo Carvalho de Melo295f7322005-08-09 20:11:56 -07002301 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 struct tcp_sock *tp = tcp_sk(sk);
2303 int val, len;
2304
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 if (get_user(len, optlen))
2306 return -EFAULT;
2307
2308 len = min_t(unsigned int, len, sizeof(int));
2309
2310 if (len < 0)
2311 return -EINVAL;
2312
2313 switch (optname) {
2314 case TCP_MAXSEG:
David S. Millerc1b4a7e2005-07-05 15:24:38 -07002315 val = tp->mss_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 if (!val && ((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN)))
2317 val = tp->rx_opt.user_mss;
2318 break;
2319 case TCP_NODELAY:
2320 val = !!(tp->nonagle&TCP_NAGLE_OFF);
2321 break;
2322 case TCP_CORK:
2323 val = !!(tp->nonagle&TCP_NAGLE_CORK);
2324 break;
2325 case TCP_KEEPIDLE:
2326 val = (tp->keepalive_time ? : sysctl_tcp_keepalive_time) / HZ;
2327 break;
2328 case TCP_KEEPINTVL:
2329 val = (tp->keepalive_intvl ? : sysctl_tcp_keepalive_intvl) / HZ;
2330 break;
2331 case TCP_KEEPCNT:
2332 val = tp->keepalive_probes ? : sysctl_tcp_keepalive_probes;
2333 break;
2334 case TCP_SYNCNT:
Arnaldo Carvalho de Melo295f7322005-08-09 20:11:56 -07002335 val = icsk->icsk_syn_retries ? : sysctl_tcp_syn_retries;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 break;
2337 case TCP_LINGER2:
2338 val = tp->linger2;
2339 if (val >= 0)
2340 val = (val ? : sysctl_tcp_fin_timeout) / HZ;
2341 break;
2342 case TCP_DEFER_ACCEPT:
David S. Millerec0a1962008-06-12 16:31:35 -07002343 val = !icsk->icsk_accept_queue.rskq_defer_accept ? 0 :
2344 ((TCP_TIMEOUT_INIT / HZ) << (icsk->icsk_accept_queue.rskq_defer_accept - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 break;
2346 case TCP_WINDOW_CLAMP:
2347 val = tp->window_clamp;
2348 break;
2349 case TCP_INFO: {
2350 struct tcp_info info;
2351
2352 if (get_user(len, optlen))
2353 return -EFAULT;
2354
2355 tcp_get_info(sk, &info);
2356
2357 len = min_t(unsigned int, len, sizeof(info));
2358 if (put_user(len, optlen))
2359 return -EFAULT;
2360 if (copy_to_user(optval, &info, len))
2361 return -EFAULT;
2362 return 0;
2363 }
2364 case TCP_QUICKACK:
Arnaldo Carvalho de Melo295f7322005-08-09 20:11:56 -07002365 val = !icsk->icsk_ack.pingpong;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 break;
Stephen Hemminger5f8ef482005-06-23 20:37:36 -07002367
2368 case TCP_CONGESTION:
2369 if (get_user(len, optlen))
2370 return -EFAULT;
2371 len = min_t(unsigned int, len, TCP_CA_NAME_MAX);
2372 if (put_user(len, optlen))
2373 return -EFAULT;
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002374 if (copy_to_user(optval, icsk->icsk_ca_ops->name, len))
Stephen Hemminger5f8ef482005-06-23 20:37:36 -07002375 return -EFAULT;
2376 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 default:
2378 return -ENOPROTOOPT;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380
2381 if (put_user(len, optlen))
2382 return -EFAULT;
2383 if (copy_to_user(optval, &val, len))
2384 return -EFAULT;
2385 return 0;
2386}
2387
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08002388int tcp_getsockopt(struct sock *sk, int level, int optname, char __user *optval,
2389 int __user *optlen)
2390{
2391 struct inet_connection_sock *icsk = inet_csk(sk);
2392
2393 if (level != SOL_TCP)
2394 return icsk->icsk_af_ops->getsockopt(sk, level, optname,
2395 optval, optlen);
2396 return do_tcp_getsockopt(sk, level, optname, optval, optlen);
2397}
2398
2399#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08002400int compat_tcp_getsockopt(struct sock *sk, int level, int optname,
2401 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08002402{
Arnaldo Carvalho de Melodec73ff2006-03-20 22:46:16 -08002403 if (level != SOL_TCP)
2404 return inet_csk_compat_getsockopt(sk, level, optname,
2405 optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08002406 return do_tcp_getsockopt(sk, level, optname, optval, optlen);
2407}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08002408
2409EXPORT_SYMBOL(compat_tcp_getsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08002410#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411
Herbert Xu576a30e2006-06-27 13:22:38 -07002412struct sk_buff *tcp_tso_segment(struct sk_buff *skb, int features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07002413{
2414 struct sk_buff *segs = ERR_PTR(-EINVAL);
2415 struct tcphdr *th;
2416 unsigned thlen;
2417 unsigned int seq;
Al Virod3bc23e2006-11-14 21:24:49 -08002418 __be32 delta;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002419 unsigned int oldlen;
Herbert Xu4e704ee2009-01-14 20:41:12 -08002420 unsigned int mss;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002421
2422 if (!pskb_may_pull(skb, sizeof(*th)))
2423 goto out;
2424
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07002425 th = tcp_hdr(skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002426 thlen = th->doff * 4;
2427 if (thlen < sizeof(*th))
2428 goto out;
2429
2430 if (!pskb_may_pull(skb, thlen))
2431 goto out;
2432
Herbert Xu0718bcc2006-06-25 23:55:46 -07002433 oldlen = (u16)~skb->len;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002434 __skb_pull(skb, thlen);
2435
Herbert Xu4e704ee2009-01-14 20:41:12 -08002436 mss = skb_shinfo(skb)->gso_size;
2437 if (unlikely(skb->len <= mss))
2438 goto out;
2439
Herbert Xu3820c3f2006-06-29 20:11:25 -07002440 if (skb_gso_ok(skb, features | NETIF_F_GSO_ROBUST)) {
2441 /* Packet is from an untrusted source, reset gso_segs. */
Herbert Xubbcf4672006-07-03 19:38:35 -07002442 int type = skb_shinfo(skb)->gso_type;
Herbert Xu3820c3f2006-06-29 20:11:25 -07002443
Herbert Xubbcf4672006-07-03 19:38:35 -07002444 if (unlikely(type &
2445 ~(SKB_GSO_TCPV4 |
2446 SKB_GSO_DODGY |
2447 SKB_GSO_TCP_ECN |
2448 SKB_GSO_TCPV6 |
2449 0) ||
2450 !(type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))))
2451 goto out;
2452
Ilpo Järvinen172589c2007-08-28 15:50:33 -07002453 skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(skb->len, mss);
Herbert Xu3820c3f2006-06-29 20:11:25 -07002454
2455 segs = NULL;
2456 goto out;
2457 }
2458
Herbert Xu576a30e2006-06-27 13:22:38 -07002459 segs = skb_segment(skb, features);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002460 if (IS_ERR(segs))
2461 goto out;
2462
Herbert Xu4e704ee2009-01-14 20:41:12 -08002463 delta = htonl(oldlen + (thlen + mss));
Herbert Xuf4c50d92006-06-22 03:02:40 -07002464
2465 skb = segs;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07002466 th = tcp_hdr(skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002467 seq = ntohl(th->seq);
2468
2469 do {
2470 th->fin = th->psh = 0;
2471
Al Virod3bc23e2006-11-14 21:24:49 -08002472 th->check = ~csum_fold((__force __wsum)((__force u32)th->check +
2473 (__force u32)delta));
Patrick McHardy84fa7932006-08-29 16:44:56 -07002474 if (skb->ip_summed != CHECKSUM_PARTIAL)
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07002475 th->check =
2476 csum_fold(csum_partial(skb_transport_header(skb),
2477 thlen, skb->csum));
Herbert Xuf4c50d92006-06-22 03:02:40 -07002478
Herbert Xu4e704ee2009-01-14 20:41:12 -08002479 seq += mss;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002480 skb = skb->next;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07002481 th = tcp_hdr(skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002482
2483 th->seq = htonl(seq);
2484 th->cwr = 0;
2485 } while (skb->next);
2486
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002487 delta = htonl(oldlen + (skb->tail - skb->transport_header) +
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07002488 skb->data_len);
Al Virod3bc23e2006-11-14 21:24:49 -08002489 th->check = ~csum_fold((__force __wsum)((__force u32)th->check +
2490 (__force u32)delta));
Patrick McHardy84fa7932006-08-29 16:44:56 -07002491 if (skb->ip_summed != CHECKSUM_PARTIAL)
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07002492 th->check = csum_fold(csum_partial(skb_transport_header(skb),
2493 thlen, skb->csum));
Herbert Xuf4c50d92006-06-22 03:02:40 -07002494
2495out:
2496 return segs;
2497}
Herbert Xuadcfc7d2006-06-30 13:36:15 -07002498EXPORT_SYMBOL(tcp_tso_segment);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002499
Herbert Xubf296b12008-12-15 23:43:36 -08002500struct sk_buff **tcp_gro_receive(struct sk_buff **head, struct sk_buff *skb)
2501{
2502 struct sk_buff **pp = NULL;
2503 struct sk_buff *p;
2504 struct tcphdr *th;
2505 struct tcphdr *th2;
2506 unsigned int thlen;
2507 unsigned int flags;
Herbert Xubf296b12008-12-15 23:43:36 -08002508 unsigned int mss = 1;
2509 int flush = 1;
Herbert Xuaa6320d2009-02-08 18:00:40 +00002510 int i;
Herbert Xubf296b12008-12-15 23:43:36 -08002511
Herbert Xu86911732009-01-29 14:19:50 +00002512 th = skb_gro_header(skb, sizeof(*th));
2513 if (unlikely(!th))
Herbert Xubf296b12008-12-15 23:43:36 -08002514 goto out;
2515
Herbert Xubf296b12008-12-15 23:43:36 -08002516 thlen = th->doff * 4;
2517 if (thlen < sizeof(*th))
2518 goto out;
2519
Herbert Xu86911732009-01-29 14:19:50 +00002520 th = skb_gro_header(skb, thlen);
2521 if (unlikely(!th))
Herbert Xubf296b12008-12-15 23:43:36 -08002522 goto out;
2523
Herbert Xu86911732009-01-29 14:19:50 +00002524 skb_gro_pull(skb, thlen);
Herbert Xubf296b12008-12-15 23:43:36 -08002525
2526 flags = tcp_flag_word(th);
2527
2528 for (; (p = *head); head = &p->next) {
2529 if (!NAPI_GRO_CB(p)->same_flow)
2530 continue;
2531
2532 th2 = tcp_hdr(p);
2533
Herbert Xuaa6320d2009-02-08 18:00:40 +00002534 if ((th->source ^ th2->source) | (th->dest ^ th2->dest)) {
Herbert Xubf296b12008-12-15 23:43:36 -08002535 NAPI_GRO_CB(p)->same_flow = 0;
2536 continue;
2537 }
2538
2539 goto found;
2540 }
2541
2542 goto out_check_final;
2543
2544found:
2545 flush = NAPI_GRO_CB(p)->flush;
2546 flush |= flags & TCP_FLAG_CWR;
2547 flush |= (flags ^ tcp_flag_word(th2)) &
2548 ~(TCP_FLAG_CWR | TCP_FLAG_FIN | TCP_FLAG_PSH);
Herbert Xuaa6320d2009-02-08 18:00:40 +00002549 flush |= (th->ack_seq ^ th2->ack_seq) | (th->window ^ th2->window);
2550 for (i = sizeof(*th); !flush && i < thlen; i += 4)
2551 flush |= *(u32 *)((u8 *)th + i) ^
2552 *(u32 *)((u8 *)th2 + i);
Herbert Xubf296b12008-12-15 23:43:36 -08002553
Herbert Xub5302562009-01-04 16:13:19 -08002554 mss = skb_shinfo(p)->gso_size;
Herbert Xubf296b12008-12-15 23:43:36 -08002555
Herbert Xuaa6320d2009-02-08 18:00:40 +00002556 flush |= (skb_gro_len(skb) > mss) | !skb_gro_len(skb);
2557 flush |= (ntohl(th2->seq) + skb_gro_len(p)) ^ ntohl(th->seq);
Herbert Xubf296b12008-12-15 23:43:36 -08002558
2559 if (flush || skb_gro_receive(head, skb)) {
2560 mss = 1;
2561 goto out_check_final;
2562 }
2563
2564 p = *head;
2565 th2 = tcp_hdr(p);
2566 tcp_flag_word(th2) |= flags & (TCP_FLAG_FIN | TCP_FLAG_PSH);
2567
2568out_check_final:
Herbert Xu86911732009-01-29 14:19:50 +00002569 flush = skb_gro_len(skb) < mss;
Herbert Xubf296b12008-12-15 23:43:36 -08002570 flush |= flags & (TCP_FLAG_URG | TCP_FLAG_PSH | TCP_FLAG_RST |
2571 TCP_FLAG_SYN | TCP_FLAG_FIN);
2572
2573 if (p && (!NAPI_GRO_CB(skb)->same_flow || flush))
2574 pp = head;
2575
2576out:
2577 NAPI_GRO_CB(skb)->flush |= flush;
2578
2579 return pp;
2580}
Herbert Xu684f2172009-01-08 10:41:23 -08002581EXPORT_SYMBOL(tcp_gro_receive);
Herbert Xubf296b12008-12-15 23:43:36 -08002582
2583int tcp_gro_complete(struct sk_buff *skb)
2584{
2585 struct tcphdr *th = tcp_hdr(skb);
2586
2587 skb->csum_start = skb_transport_header(skb) - skb->head;
2588 skb->csum_offset = offsetof(struct tcphdr, check);
2589 skb->ip_summed = CHECKSUM_PARTIAL;
2590
Herbert Xubf296b12008-12-15 23:43:36 -08002591 skb_shinfo(skb)->gso_segs = NAPI_GRO_CB(skb)->count;
2592
2593 if (th->cwr)
2594 skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
2595
2596 return 0;
2597}
Herbert Xu684f2172009-01-08 10:41:23 -08002598EXPORT_SYMBOL(tcp_gro_complete);
Herbert Xubf296b12008-12-15 23:43:36 -08002599
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08002600#ifdef CONFIG_TCP_MD5SIG
2601static unsigned long tcp_md5sig_users;
2602static struct tcp_md5sig_pool **tcp_md5sig_pool;
2603static DEFINE_SPINLOCK(tcp_md5sig_pool_lock);
2604
2605static void __tcp_free_md5sig_pool(struct tcp_md5sig_pool **pool)
2606{
2607 int cpu;
2608 for_each_possible_cpu(cpu) {
2609 struct tcp_md5sig_pool *p = *per_cpu_ptr(pool, cpu);
2610 if (p) {
2611 if (p->md5_desc.tfm)
2612 crypto_free_hash(p->md5_desc.tfm);
2613 kfree(p);
2614 p = NULL;
2615 }
2616 }
2617 free_percpu(pool);
2618}
2619
2620void tcp_free_md5sig_pool(void)
2621{
2622 struct tcp_md5sig_pool **pool = NULL;
2623
David S. Miller2c4f6212007-02-20 23:51:47 -08002624 spin_lock_bh(&tcp_md5sig_pool_lock);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08002625 if (--tcp_md5sig_users == 0) {
2626 pool = tcp_md5sig_pool;
2627 tcp_md5sig_pool = NULL;
2628 }
David S. Miller2c4f6212007-02-20 23:51:47 -08002629 spin_unlock_bh(&tcp_md5sig_pool_lock);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08002630 if (pool)
2631 __tcp_free_md5sig_pool(pool);
2632}
2633
2634EXPORT_SYMBOL(tcp_free_md5sig_pool);
2635
Adrian Bunkf5b99bc2006-11-30 17:22:29 -08002636static struct tcp_md5sig_pool **__tcp_alloc_md5sig_pool(void)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08002637{
2638 int cpu;
2639 struct tcp_md5sig_pool **pool;
2640
2641 pool = alloc_percpu(struct tcp_md5sig_pool *);
2642 if (!pool)
2643 return NULL;
2644
2645 for_each_possible_cpu(cpu) {
2646 struct tcp_md5sig_pool *p;
2647 struct crypto_hash *hash;
2648
2649 p = kzalloc(sizeof(*p), GFP_KERNEL);
2650 if (!p)
2651 goto out_free;
2652 *per_cpu_ptr(pool, cpu) = p;
2653
2654 hash = crypto_alloc_hash("md5", 0, CRYPTO_ALG_ASYNC);
2655 if (!hash || IS_ERR(hash))
2656 goto out_free;
2657
2658 p->md5_desc.tfm = hash;
2659 }
2660 return pool;
2661out_free:
2662 __tcp_free_md5sig_pool(pool);
2663 return NULL;
2664}
2665
2666struct tcp_md5sig_pool **tcp_alloc_md5sig_pool(void)
2667{
2668 struct tcp_md5sig_pool **pool;
2669 int alloc = 0;
2670
2671retry:
David S. Miller2c4f6212007-02-20 23:51:47 -08002672 spin_lock_bh(&tcp_md5sig_pool_lock);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08002673 pool = tcp_md5sig_pool;
2674 if (tcp_md5sig_users++ == 0) {
2675 alloc = 1;
David S. Miller2c4f6212007-02-20 23:51:47 -08002676 spin_unlock_bh(&tcp_md5sig_pool_lock);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08002677 } else if (!pool) {
2678 tcp_md5sig_users--;
David S. Miller2c4f6212007-02-20 23:51:47 -08002679 spin_unlock_bh(&tcp_md5sig_pool_lock);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08002680 cpu_relax();
2681 goto retry;
2682 } else
David S. Miller2c4f6212007-02-20 23:51:47 -08002683 spin_unlock_bh(&tcp_md5sig_pool_lock);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08002684
2685 if (alloc) {
2686 /* we cannot hold spinlock here because this may sleep. */
2687 struct tcp_md5sig_pool **p = __tcp_alloc_md5sig_pool();
David S. Miller2c4f6212007-02-20 23:51:47 -08002688 spin_lock_bh(&tcp_md5sig_pool_lock);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08002689 if (!p) {
2690 tcp_md5sig_users--;
David S. Miller2c4f6212007-02-20 23:51:47 -08002691 spin_unlock_bh(&tcp_md5sig_pool_lock);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08002692 return NULL;
2693 }
2694 pool = tcp_md5sig_pool;
2695 if (pool) {
2696 /* oops, it has already been assigned. */
David S. Miller2c4f6212007-02-20 23:51:47 -08002697 spin_unlock_bh(&tcp_md5sig_pool_lock);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08002698 __tcp_free_md5sig_pool(p);
2699 } else {
2700 tcp_md5sig_pool = pool = p;
David S. Miller2c4f6212007-02-20 23:51:47 -08002701 spin_unlock_bh(&tcp_md5sig_pool_lock);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08002702 }
2703 }
2704 return pool;
2705}
2706
2707EXPORT_SYMBOL(tcp_alloc_md5sig_pool);
2708
2709struct tcp_md5sig_pool *__tcp_get_md5sig_pool(int cpu)
2710{
2711 struct tcp_md5sig_pool **p;
David S. Miller2c4f6212007-02-20 23:51:47 -08002712 spin_lock_bh(&tcp_md5sig_pool_lock);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08002713 p = tcp_md5sig_pool;
2714 if (p)
2715 tcp_md5sig_users++;
David S. Miller2c4f6212007-02-20 23:51:47 -08002716 spin_unlock_bh(&tcp_md5sig_pool_lock);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08002717 return (p ? *per_cpu_ptr(p, cpu) : NULL);
2718}
2719
2720EXPORT_SYMBOL(__tcp_get_md5sig_pool);
2721
David S. Miller6931ba72006-12-13 16:25:44 -08002722void __tcp_put_md5sig_pool(void)
2723{
2724 tcp_free_md5sig_pool();
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08002725}
2726
2727EXPORT_SYMBOL(__tcp_put_md5sig_pool);
Adam Langley49a72df2008-07-19 00:01:42 -07002728
2729int tcp_md5_hash_header(struct tcp_md5sig_pool *hp,
2730 struct tcphdr *th)
2731{
2732 struct scatterlist sg;
2733 int err;
2734
2735 __sum16 old_checksum = th->check;
2736 th->check = 0;
2737 /* options aren't included in the hash */
2738 sg_init_one(&sg, th, sizeof(struct tcphdr));
2739 err = crypto_hash_update(&hp->md5_desc, &sg, sizeof(struct tcphdr));
2740 th->check = old_checksum;
2741 return err;
2742}
2743
2744EXPORT_SYMBOL(tcp_md5_hash_header);
2745
2746int tcp_md5_hash_skb_data(struct tcp_md5sig_pool *hp,
2747 struct sk_buff *skb, unsigned header_len)
2748{
2749 struct scatterlist sg;
2750 const struct tcphdr *tp = tcp_hdr(skb);
2751 struct hash_desc *desc = &hp->md5_desc;
2752 unsigned i;
2753 const unsigned head_data_len = skb_headlen(skb) > header_len ?
2754 skb_headlen(skb) - header_len : 0;
2755 const struct skb_shared_info *shi = skb_shinfo(skb);
2756
2757 sg_init_table(&sg, 1);
2758
2759 sg_set_buf(&sg, ((u8 *) tp) + header_len, head_data_len);
2760 if (crypto_hash_update(desc, &sg, head_data_len))
2761 return 1;
2762
2763 for (i = 0; i < shi->nr_frags; ++i) {
2764 const struct skb_frag_struct *f = &shi->frags[i];
2765 sg_set_page(&sg, f->page, f->size, f->page_offset);
2766 if (crypto_hash_update(desc, &sg, f->size))
2767 return 1;
2768 }
2769
2770 return 0;
2771}
2772
2773EXPORT_SYMBOL(tcp_md5_hash_skb_data);
2774
2775int tcp_md5_hash_key(struct tcp_md5sig_pool *hp, struct tcp_md5sig_key *key)
2776{
2777 struct scatterlist sg;
2778
2779 sg_init_one(&sg, key->key, key->keylen);
2780 return crypto_hash_update(&hp->md5_desc, &sg, key->keylen);
2781}
2782
2783EXPORT_SYMBOL(tcp_md5_hash_key);
2784
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08002785#endif
2786
Andi Kleen4ac02ba2007-04-20 17:11:46 -07002787void tcp_done(struct sock *sk)
2788{
Jianjun Kong5a5f3a82008-11-03 00:24:34 -08002789 if (sk->sk_state == TCP_SYN_SENT || sk->sk_state == TCP_SYN_RECV)
Pavel Emelyanov63231bd2008-07-16 20:22:25 -07002790 TCP_INC_STATS_BH(sock_net(sk), TCP_MIB_ATTEMPTFAILS);
Andi Kleen4ac02ba2007-04-20 17:11:46 -07002791
2792 tcp_set_state(sk, TCP_CLOSE);
2793 tcp_clear_xmit_timers(sk);
2794
2795 sk->sk_shutdown = SHUTDOWN_MASK;
2796
2797 if (!sock_flag(sk, SOCK_DEAD))
2798 sk->sk_state_change(sk);
2799 else
2800 inet_csk_destroy_sock(sk);
2801}
2802EXPORT_SYMBOL_GPL(tcp_done);
2803
Stephen Hemminger5f8ef482005-06-23 20:37:36 -07002804extern struct tcp_congestion_ops tcp_reno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805
2806static __initdata unsigned long thash_entries;
2807static int __init set_thash_entries(char *str)
2808{
2809 if (!str)
2810 return 0;
2811 thash_entries = simple_strtoul(str, &str, 0);
2812 return 1;
2813}
2814__setup("thash_entries=", set_thash_entries);
2815
2816void __init tcp_init(void)
2817{
2818 struct sk_buff *skb = NULL;
Miquel van Smoorenburg57413eb2008-06-27 17:23:57 -07002819 unsigned long nr_pages, limit;
John Heffner7b4f4b52006-03-25 01:34:07 -08002820 int order, i, max_share;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821
Pavel Emelyanov1f9e6362007-12-11 02:12:04 -08002822 BUILD_BUG_ON(sizeof(struct tcp_skb_cb) > sizeof(skb->cb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823
Eric Dumazet17483762008-11-25 21:16:35 -08002824 percpu_counter_init(&tcp_sockets_allocated, 0);
Eric Dumazetdd24c002008-11-25 21:17:14 -08002825 percpu_counter_init(&tcp_orphan_count, 0);
Arnaldo Carvalho de Melo6e04e022005-08-09 20:07:35 -07002826 tcp_hashinfo.bind_bucket_cachep =
2827 kmem_cache_create("tcp_bind_bucket",
2828 sizeof(struct inet_bind_bucket), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09002829 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 /* Size and allocate the main established and bind bucket
2832 * hash tables.
2833 *
2834 * The methodology is similar to that of the buffer cache.
2835 */
Arnaldo Carvalho de Melo6e04e022005-08-09 20:07:35 -07002836 tcp_hashinfo.ehash =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 alloc_large_system_hash("TCP established",
Arnaldo Carvalho de Melo0f7ff922005-08-09 19:59:44 -07002838 sizeof(struct inet_ehash_bucket),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 thash_entries,
2840 (num_physpages >= 128 * 1024) ?
Mike Stroyan18955cf2005-11-29 16:12:55 -08002841 13 : 15,
John Heffner9e950ef2006-11-06 23:10:51 -08002842 0,
Arnaldo Carvalho de Melo6e04e022005-08-09 20:07:35 -07002843 &tcp_hashinfo.ehash_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 NULL,
Jean Delvare0ccfe612007-10-30 00:59:25 -07002845 thash_entries ? 0 : 512 * 1024);
Eric Dumazetdbca9b2752007-02-08 14:16:46 -08002846 tcp_hashinfo.ehash_size = 1 << tcp_hashinfo.ehash_size;
2847 for (i = 0; i < tcp_hashinfo.ehash_size; i++) {
Eric Dumazet3ab5aee2008-11-16 19:40:17 -08002848 INIT_HLIST_NULLS_HEAD(&tcp_hashinfo.ehash[i].chain, i);
2849 INIT_HLIST_NULLS_HEAD(&tcp_hashinfo.ehash[i].twchain, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 }
Eric Dumazet230140c2007-11-07 02:40:20 -08002851 if (inet_ehash_locks_alloc(&tcp_hashinfo))
2852 panic("TCP: failed to alloc ehash_locks");
Arnaldo Carvalho de Melo6e04e022005-08-09 20:07:35 -07002853 tcp_hashinfo.bhash =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 alloc_large_system_hash("TCP bind",
Arnaldo Carvalho de Melo0f7ff922005-08-09 19:59:44 -07002855 sizeof(struct inet_bind_hashbucket),
Arnaldo Carvalho de Melo6e04e022005-08-09 20:07:35 -07002856 tcp_hashinfo.ehash_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 (num_physpages >= 128 * 1024) ?
Mike Stroyan18955cf2005-11-29 16:12:55 -08002858 13 : 15,
John Heffner9e950ef2006-11-06 23:10:51 -08002859 0,
Arnaldo Carvalho de Melo6e04e022005-08-09 20:07:35 -07002860 &tcp_hashinfo.bhash_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 NULL,
2862 64 * 1024);
Arnaldo Carvalho de Melo6e04e022005-08-09 20:07:35 -07002863 tcp_hashinfo.bhash_size = 1 << tcp_hashinfo.bhash_size;
2864 for (i = 0; i < tcp_hashinfo.bhash_size; i++) {
2865 spin_lock_init(&tcp_hashinfo.bhash[i].lock);
2866 INIT_HLIST_HEAD(&tcp_hashinfo.bhash[i].chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 }
2868
2869 /* Try to be a bit smarter and adjust defaults depending
2870 * on available memory.
2871 */
2872 for (order = 0; ((1 << order) << PAGE_SHIFT) <
Arnaldo Carvalho de Melo6e04e022005-08-09 20:07:35 -07002873 (tcp_hashinfo.bhash_size * sizeof(struct inet_bind_hashbucket));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 order++)
2875 ;
Andi Kleene7626482005-06-13 14:24:52 -07002876 if (order >= 4) {
Arnaldo Carvalho de Melo295ff7e2005-08-09 20:44:40 -07002877 tcp_death_row.sysctl_max_tw_buckets = 180000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 sysctl_tcp_max_orphans = 4096 << (order - 4);
2879 sysctl_max_syn_backlog = 1024;
2880 } else if (order < 3) {
Arnaldo Carvalho de Melo295ff7e2005-08-09 20:44:40 -07002881 tcp_death_row.sysctl_max_tw_buckets >>= (3 - order);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 sysctl_tcp_max_orphans >>= (3 - order);
2883 sysctl_max_syn_backlog = 128;
2884 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885
John Heffner53cdcc02007-03-16 15:04:03 -07002886 /* Set the pressure threshold to be a fraction of global memory that
2887 * is up to 1/2 at 256 MB, decreasing toward zero with the amount of
2888 * memory, with a floor of 128 pages.
2889 */
Miquel van Smoorenburg57413eb2008-06-27 17:23:57 -07002890 nr_pages = totalram_pages - totalhigh_pages;
2891 limit = min(nr_pages, 1UL<<(28-PAGE_SHIFT)) >> (20-PAGE_SHIFT);
2892 limit = (limit * (nr_pages >> (20-PAGE_SHIFT))) >> (PAGE_SHIFT-11);
John Heffner53cdcc02007-03-16 15:04:03 -07002893 limit = max(limit, 128UL);
2894 sysctl_tcp_mem[0] = limit / 4 * 3;
2895 sysctl_tcp_mem[1] = limit;
John Heffner52bf3762006-11-14 20:25:17 -08002896 sysctl_tcp_mem[2] = sysctl_tcp_mem[0] * 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897
John Heffner53cdcc02007-03-16 15:04:03 -07002898 /* Set per-socket limits to no more than 1/128 the pressure threshold */
John Heffner7b4f4b52006-03-25 01:34:07 -08002899 limit = ((unsigned long)sysctl_tcp_mem[1]) << (PAGE_SHIFT - 7);
2900 max_share = min(4UL*1024*1024, limit);
2901
Hideo Aoki3ab224b2007-12-31 00:11:19 -08002902 sysctl_tcp_wmem[0] = SK_MEM_QUANTUM;
John Heffner7b4f4b52006-03-25 01:34:07 -08002903 sysctl_tcp_wmem[1] = 16*1024;
2904 sysctl_tcp_wmem[2] = max(64*1024, max_share);
2905
Hideo Aoki3ab224b2007-12-31 00:11:19 -08002906 sysctl_tcp_rmem[0] = SK_MEM_QUANTUM;
John Heffner7b4f4b52006-03-25 01:34:07 -08002907 sysctl_tcp_rmem[1] = 87380;
2908 sysctl_tcp_rmem[2] = max(87380, max_share);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909
2910 printk(KERN_INFO "TCP: Hash tables configured "
2911 "(established %d bind %d)\n",
Eric Dumazetdbca9b2752007-02-08 14:16:46 -08002912 tcp_hashinfo.ehash_size, tcp_hashinfo.bhash_size);
Stephen Hemminger317a76f2005-06-23 12:19:55 -07002913
2914 tcp_register_congestion_control(&tcp_reno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915}
2916
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917EXPORT_SYMBOL(tcp_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918EXPORT_SYMBOL(tcp_disconnect);
2919EXPORT_SYMBOL(tcp_getsockopt);
2920EXPORT_SYMBOL(tcp_ioctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921EXPORT_SYMBOL(tcp_poll);
2922EXPORT_SYMBOL(tcp_read_sock);
2923EXPORT_SYMBOL(tcp_recvmsg);
2924EXPORT_SYMBOL(tcp_sendmsg);
Jens Axboe9c55e012007-11-06 23:30:13 -08002925EXPORT_SYMBOL(tcp_splice_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926EXPORT_SYMBOL(tcp_sendpage);
2927EXPORT_SYMBOL(tcp_setsockopt);
2928EXPORT_SYMBOL(tcp_shutdown);