1 /*
2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
5 *
6 * Implementation of the Transmission Control Protocol(TCP).
7 *
8 * Version: @(#)tcp.c 1.0.16 05/25/93
9 *
10 * Authors: Ross Biro, <bir7@leland.Stanford.Edu>
11 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
12 * Mark Evans, <evansmp@uhura.aston.ac.uk>
13 * Corey Minyard <wf-rch!minyard@relay.EU.net>
14 * Florian La Roche, <flla@stud.uni-sb.de>
15 * Charles Hedrick, <hedrick@klinzhai.rutgers.edu>
16 * Linus Torvalds, <torvalds@cs.helsinki.fi>
17 * Alan Cox, <gw4pts@gw4pts.ampr.org>
18 * Matthew Dillon, <dillon@apollo.west.oic.com>
19 * Arnt Gulbrandsen, <agulbra@nvg.unit.no>
20 * Jorge Cwik, <jorge@laser.satlink.net>
21 *
22 * Fixes:
23 * Alan Cox : Numerous verify_area() calls
24 * Alan Cox : Set the ACK bit on a reset
25 * Alan Cox : Stopped it crashing if it closed while
26 * sk->inuse=1 and was trying to connect
27 * (tcp_err()).
28 * Alan Cox : All icmp error handling was broken
29 * pointers passed where wrong and the
30 * socket was looked up backwards. Nobody
31 * tested any icmp error code obviously.
32 * Alan Cox : tcp_err() now handled properly. It
33 * wakes people on errors. select
34 * behaves and the icmp error race
35 * has gone by moving it into sock.c
36 * Alan Cox : tcp_send_reset() fixed to work for
37 * everything not just packets for
38 * unknown sockets.
39 * Alan Cox : tcp option processing.
40 * Alan Cox : Reset tweaked (still not 100%) [Had
41 * syn rule wrong]
42 * Herp Rosmanith : More reset fixes
43 * Alan Cox : No longer acks invalid rst frames.
44 * Acking any kind of RST is right out.
45 * Alan Cox : Sets an ignore me flag on an rst
46 * receive otherwise odd bits of prattle
47 * escape still
48 * Alan Cox : Fixed another acking RST frame bug.
49 * Should stop LAN workplace lockups.
50 * Alan Cox : Some tidyups using the new skb list
51 * facilities
52 * Alan Cox : sk->keepopen now seems to work
53 * Alan Cox : Pulls options out correctly on accepts
54 * Alan Cox : Fixed assorted sk->rqueue->next errors
55 * Alan Cox : PSH doesn't end a TCP read. Switched a
56 * bit to skb ops.
57 * Alan Cox : Tidied tcp_data to avoid a potential
58 * nasty.
59 * Alan Cox : Added some better commenting, as the
60 * tcp is hard to follow
61 * Alan Cox : Removed incorrect check for 20 * psh
62 * Michael O'Reilly : ack < copied bug fix.
63 * Johannes Stille : Misc tcp fixes (not all in yet).
64 * Alan Cox : FIN with no memory -> CRASH
65 * Alan Cox : Added socket option proto entries.
66 * Also added awareness of them to accept.
67 * Alan Cox : Added TCP options (SOL_TCP)
68 * Alan Cox : Switched wakeup calls to callbacks,
69 * so the kernel can layer network
70 * sockets.
71 * Alan Cox : Use ip_tos/ip_ttl settings.
72 * Alan Cox : Handle FIN (more) properly (we hope).
73 * Alan Cox : RST frames sent on unsynchronised
74 * state ack error.
75 * Alan Cox : Put in missing check for SYN bit.
76 * Alan Cox : Added tcp_select_window() aka NET2E
77 * window non shrink trick.
78 * Alan Cox : Added a couple of small NET2E timer
79 * fixes
80 * Charles Hedrick : TCP fixes
81 * Toomas Tamm : TCP window fixes
82 * Alan Cox : Small URG fix to rlogin ^C ack fight
83 * Charles Hedrick : Rewrote most of it to actually work
84 * Linus : Rewrote tcp_read() and URG handling
85 * completely
86 * Gerhard Koerting: Fixed some missing timer handling
87 * Matthew Dillon : Reworked TCP machine states as per RFC
88 * Gerhard Koerting: PC/TCP workarounds
89 * Adam Caldwell : Assorted timer/timing errors
90 * Matthew Dillon : Fixed another RST bug
91 * Alan Cox : Move to kernel side addressing changes.
92 * Alan Cox : Beginning work on TCP fastpathing
93 * (not yet usable)
94 * Arnt Gulbrandsen: Turbocharged tcp_check() routine.
95 * Alan Cox : TCP fast path debugging
96 * Alan Cox : Window clamping
97 * Michael Riepe : Bug in tcp_check()
98 * Matt Dillon : More TCP improvements and RST bug fixes
99 * Matt Dillon : Yet more small nasties remove from the
100 * TCP code (Be very nice to this man if
101 * tcp finally works 100%) 8)
102 * Alan Cox : BSD accept semantics.
103 * Alan Cox : Reset on closedown bug.
104 * Peter De Schrijver : ENOTCONN check missing in tcp_sendto().
105 * Michael Pall : Handle select() after URG properly in
106 * all cases.
107 * Michael Pall : Undo the last fix in tcp_read_urg()
108 * (multi URG PUSH broke rlogin).
109 * Michael Pall : Fix the multi URG PUSH problem in
110 * tcp_readable(), select() after URG
111 * works now.
112 * Michael Pall : recv(...,MSG_OOB) never blocks in the
113 * BSD api.
114 * Alan Cox : Changed the semantics of sk->socket to
115 * fix a race and a signal problem with
116 * accept() and async I/O.
117 * Alan Cox : Relaxed the rules on tcp_sendto().
118 * Yury Shevchuk : Really fixed accept() blocking problem.
119 * Craig I. Hagan : Allow for BSD compatible TIME_WAIT for
120 * clients/servers which listen in on
121 * fixed ports.
122 * Alan Cox : Cleaned the above up and shrank it to
123 * a sensible code size.
124 * Alan Cox : Self connect lockup fix.
125 * Alan Cox : No connect to multicast.
126 * Ross Biro : Close unaccepted children on master
127 * socket close.
128 * Alan Cox : Reset tracing code.
129 * Alan Cox : Spurious resets on shutdown.
130 * Alan Cox : Giant 15 minute/60 second timer error
131 * Alan Cox : Small whoops in selecting before an
132 * accept.
133 * Alan Cox : Kept the state trace facility since
134 * it's handy for debugging.
135 * Alan Cox : More reset handler fixes.
136 * Alan Cox : Started rewriting the code based on
137 * the RFC's for other useful protocol
138 * references see: Comer, KA9Q NOS, and
139 * for a reference on the difference
140 * between specifications and how BSD
141 * works see the 4.4lite source.
142 * A.N.Kuznetsov : Don't time wait on completion of tidy
143 * close.
144 * Linus Torvalds : Fin/Shutdown & copied_seq changes.
145 * Linus Torvalds : Fixed BSD port reuse to work first syn
146 * Alan Cox : Reimplemented timers as per the RFC
147 * and using multiple timers for sanity.
148 * Alan Cox : Small bug fixes, and a lot of new
149 * comments.
150 * Alan Cox : Fixed dual reader crash by locking
151 * the buffers (much like datagram.c)
152 * Alan Cox : Fixed stuck sockets in probe. A probe
153 * now gets fed up of retrying without
154 * (even a no space) answer.
155 * Alan Cox : Extracted closing code better
156 * Alan Cox : Fixed the closing state machine to
157 * resemble the RFC.
158 * Alan Cox : More 'per spec' fixes.
159 * Jorge Cwik : Even faster checksumming.
160 * Alan Cox : tcp_data() doesn't ack illegal PSH
161 * only frames. At least one pc tcp stack
162 * generates them.
163 * Alan Cox : Cache last socket.
164 * Alan Cox : Per route irtt.
165 * Matt Day : Select() match BSD precisely on error
166 * Alan Cox : New buffers
167 * Marc Tamsky : Various sk->prot->retransmits and
168 * sk->retransmits misupdating fixed.
169 * Fixed tcp_write_timeout: stuck close,
170 * and TCP syn retries gets used now.
171 * Mark Yarvis : In tcp_read_wakeup(), don't send an
172 * ack if stat is TCP_CLOSED.
173 * Alan Cox : Look up device on a retransmit - routes may
174 * change. Doesn't yet cope with MSS shrink right
175 * but its a start!
176 * Marc Tamsky : Closing in closing fixes.
177 * Mike Shaver : RFC1122 verifications.
178 * Alan Cox : rcv_saddr errors.
179 * Alan Cox : Block double connect().
180 * Alan Cox : Small hooks for enSKIP.
181 * Alexey Kuznetsov: Path MTU discovery.
182 * Alan Cox : Support soft errors.
183 * Alan Cox : Fix MTU discovery pathalogical case
184 * when the remote claims no mtu!
185 * Marc Tamsky : TCP_CLOSE fix.
186 * Colin (G3TNE) : Send a reset on syn ack replies in
187 * window but wrong (fixes NT lpd problems)
188 * Pedro Roque : Better TCP window handling, delayed ack.
189 * Joerg Reuter : No modification of locked buffers in
190 * tcp_do_retransmit()
191 *
192 * To Fix:
193 * Fast path the code. Two things here - fix the window calculation
194 * so it doesn't iterate over the queue, also spot packets with no funny
195 * options arriving in order and process directly.
196 *
197 * Rewrite output state machine to use a single queue.
198 * Speed up input assembly algorithm.
199 * RFC1323 - PAWS and window scaling. PAWS is required for IPv6 so we
200 * could do with it working on IPv4
201 * User settable/learned rtt/max window/mtu
202 *
203 * Change the fundamental structure to a single send queue maintained
204 * by TCP (removing the bogus ip stuff [thus fixing mtu drops on
205 * active routes too]). Cut the queue off in tcp_retransmit/
206 * tcp_transmit.
207 * Change the receive queue to assemble as it goes. This lets us
208 * dispose of most of tcp_sequence, half of tcp_ack and chunks of
209 * tcp_data/tcp_read as well as the window shrink crud.
210 * Separate out duplicated code - tcp_alloc_skb, tcp_build_ack
211 * tcp_queue_skb seem obvious routines to extract.
212 *
213 * This program is free software; you can redistribute it and/or
214 * modify it under the terms of the GNU General Public License
215 * as published by the Free Software Foundation; either version
216 * 2 of the License, or(at your option) any later version.
217 *
218 * Description of States:
219 *
220 * TCP_SYN_SENT sent a connection request, waiting for ack
221 *
222 * TCP_SYN_RECV received a connection request, sent ack,
223 * waiting for final ack in three-way handshake.
224 *
225 * TCP_ESTABLISHED connection established
226 *
227 * TCP_FIN_WAIT1 our side has shutdown, waiting to complete
228 * transmission of remaining buffered data
229 *
230 * TCP_FIN_WAIT2 all buffered data sent, waiting for remote
231 * to shutdown
232 *
233 * TCP_CLOSING both sides have shutdown but we still have
234 * data we have to finish sending
235 *
236 * TCP_TIME_WAIT timeout to catch resent junk before entering
237 * closed, can only be entered from FIN_WAIT2
238 * or CLOSING. Required because the other end
239 * may not have gotten our last ACK causing it
240 * to retransmit the data packet (which we ignore)
241 *
242 * TCP_CLOSE_WAIT remote side has shutdown and is waiting for
243 * us to finish writing our data and to shutdown
244 * (we have to close() to move on to LAST_ACK)
245 *
246 * TCP_LAST_ACK out side has shutdown after remote has
247 * shutdown. There may still be data in our
248 * buffer that we have to finish sending
249 *
250 * TCP_CLOSE socket is finished
251 */
252
253 /*
254 * RFC1122 status:
255 * NOTE: I'm not going to be doing comments in the code for this one except
256 * for violations and the like. tcp.c is just too big... If I say something
257 * "does?" or "doesn't?", it means I'm not sure, and will have to hash it out
258 * with Alan. -- MS 950903
259 *
260 * Use of PSH (4.2.2.2)
261 * MAY aggregate data sent without the PSH flag. (does)
262 * MAY queue data received without the PSH flag. (does)
263 * SHOULD collapse successive PSH flags when it packetizes data. (doesn't)
264 * MAY implement PSH on send calls. (doesn't, thus:)
265 * MUST NOT buffer data indefinitely (doesn't [1 second])
266 * MUST set PSH on last segment (does)
267 * MAY pass received PSH to application layer (doesn't)
268 * SHOULD send maximum-sized segment whenever possible. (almost always does)
269 *
270 * Window Size (4.2.2.3, 4.2.2.16)
271 * MUST treat window size as an unsigned number (does)
272 * SHOULD treat window size as a 32-bit number (does not)
273 * MUST NOT shrink window once it is offered (does not normally)
274 *
275 * Urgent Pointer (4.2.2.4)
276 * **MUST point urgent pointer to last byte of urgent data (not right
277 * after). (doesn't, to be like BSD)
278 * MUST inform application layer asynchronously of incoming urgent
279 * data. (does)
280 * MUST provide application with means of determining the amount of
281 * urgent data pending. (does)
282 * **MUST support urgent data sequence of arbitrary length. (doesn't, but
283 * it's sort of tricky to fix, as urg_ptr is a 16-bit quantity)
284 * [Follows BSD 1 byte of urgent data]
285 *
286 * TCP Options (4.2.2.5)
287 * MUST be able to receive TCP options in any segment. (does)
288 * MUST ignore unsupported options (does)
289 *
290 * Maximum Segment Size Option (4.2.2.6)
291 * MUST implement both sending and receiving MSS. (does)
292 * SHOULD send an MSS with every SYN where receive MSS != 536 (MAY send
293 * it always). (does, even when MSS == 536, which is legal)
294 * MUST assume MSS == 536 if no MSS received at connection setup (does)
295 * MUST calculate "effective send MSS" correctly:
296 * min(physical_MTU, remote_MSS+20) - sizeof(tcphdr) - sizeof(ipopts)
297 * (does - but allows operator override)
298 *
299 * TCP Checksum (4.2.2.7)
300 * MUST generate and check TCP checksum. (does)
301 *
302 * Initial Sequence Number Selection (4.2.2.8)
303 * MUST use the RFC 793 clock selection mechanism. (doesn't, but it's
304 * OK: RFC 793 specifies a 250KHz clock, while we use 1MHz, which is
305 * necessary for 10Mbps networks - and harder than BSD to spoof!)
306 *
307 * Simultaneous Open Attempts (4.2.2.10)
308 * MUST support simultaneous open attempts (does)
309 *
310 * Recovery from Old Duplicate SYN (4.2.2.11)
311 * MUST keep track of active vs. passive open (does)
312 *
313 * RST segment (4.2.2.12)
314 * SHOULD allow an RST segment to contain data (does, but doesn't do
315 * anything with it, which is standard)
316 *
317 * Closing a Connection (4.2.2.13)
318 * MUST inform application of whether connectin was closed by RST or
319 * normal close. (does)
320 * MAY allow "half-duplex" close (treat connection as closed for the
321 * local app, even before handshake is done). (does)
322 * MUST linger in TIME_WAIT for 2 * MSL (does)
323 *
324 * Retransmission Timeout (4.2.2.15)
325 * MUST implement Jacobson's slow start and congestion avoidance
326 * stuff. (does)
327 *
328 * Probing Zero Windows (4.2.2.17)
329 * MUST support probing of zero windows. (does)
330 * MAY keep offered window closed indefinitely. (does)
331 * MUST allow remote window to stay closed indefinitely. (does)
332 *
333 * Passive Open Calls (4.2.2.18)
334 * MUST NOT let new passive open affect other connections. (doesn't)
335 * MUST support passive opens (LISTENs) concurrently. (does)
336 *
337 * Time to Live (4.2.2.19)
338 * MUST make TCP TTL configurable. (does - IP_TTL option)
339 *
340 * Event Processing (4.2.2.20)
341 * SHOULD queue out-of-order segments. (does)
342 * MUST aggregate ACK segments whenever possible. (does but badly)
343 *
344 * Retransmission Timeout Calculation (4.2.3.1)
345 * MUST implement Karn's algorithm and Jacobson's algorithm for RTO
346 * calculation. (does, or at least explains them in the comments 8*b)
347 * SHOULD initialize RTO to 0 and RTT to 3. (does)
348 *
349 * When to Send an ACK Segment (4.2.3.2)
350 * SHOULD implement delayed ACK. (does)
351 * MUST keep ACK delay < 0.5 sec. (does)
352 *
353 * When to Send a Window Update (4.2.3.3)
354 * MUST implement receiver-side SWS. (does)
355 *
356 * When to Send Data (4.2.3.4)
357 * MUST implement sender-side SWS. (does)
358 * SHOULD implement Nagle algorithm. (does)
359 *
360 * TCP Connection Failures (4.2.3.5)
361 * MUST handle excessive retransmissions "properly" (see the RFC). (does)
362 * SHOULD inform application layer of soft errors. (does)
363 *
364 * TCP Keep-Alives (4.2.3.6)
365 * MAY provide keep-alives. (does)
366 * MUST make keep-alives configurable on a per-connection basis. (does)
367 * MUST default to no keep-alives. (does)
368 * **MUST make keep-alive interval configurable. (doesn't)
369 * **MUST make default keep-alive interval > 2 hours. (doesn't)
370 * MUST NOT interpret failure to ACK keep-alive packet as dead
371 * connection. (doesn't)
372 * SHOULD send keep-alive with no data. (does)
373 *
374 * TCP Multihoming (4.2.3.7)
375 * MUST get source address from IP layer before sending first
376 * SYN. (does)
377 * MUST use same local address for all segments of a connection. (does)
378 *
379 * IP Options (4.2.3.8)
380 * MUST ignore unsupported IP options. (does)
381 * MAY support Time Stamp and Record Route. (does)
382 * MUST allow application to specify a source route. (does)
383 * MUST allow receieved Source Route option to set route for all future
384 * segments on this connection. (does not (security issues))
385 *
386 * ICMP messages (4.2.3.9)
387 * MUST act on ICMP errors. (does)
388 * MUST slow transmission upon receipt of a Source Quench. (does)
389 * MUST NOT abort connection upon receipt of soft Destination
390 * Unreachables (0, 1, 5), Time Exceededs and Parameter
391 * Problems. (doesn't)
392 * SHOULD report soft Destination Unreachables etc. to the
393 * application. (does)
394 * SHOULD abort connection upon receipt of hard Destination Unreachable
395 * messages (2, 3, 4). (does)
396 *
397 * Remote Address Validation (4.2.3.10)
398 * MUST reject as an error OPEN for invalid remote IP address. (does)
399 * MUST ignore SYN with invalid source address. (does)
400 * MUST silently discard incoming SYN for broadcast/multicast
401 * address. (does)
402 *
403 * Asynchronous Reports (4.2.4.1)
404 * MUST provide mechanism for reporting soft errors to application
405 * layer. (does)
406 *
407 * Type of Service (4.2.4.2)
408 * MUST allow application layer to set Type of Service. (does IP_TOS)
409 *
410 * (Whew. -- MS 950903)
411 **/
412
413 #include <linux/config.h>
414 #include <linux/types.h>
415 #include <linux/fcntl.h>
416
417 #include <net/icmp.h>
418 #include <net/tcp.h>
419
420 #include <asm/segment.h>
421
422 unsigned long seq_offset;
423 struct tcp_mib tcp_statistics;
424
425 static void tcp_close(struct sock *sk, unsigned long timeout);
426
427 /*
428 * The less said about this the better, but it works and will do for 1.2 (and 1.4 ;))
429 */
430
431 struct wait_queue *master_select_wakeup;
432
433 /*
434 * Find someone to 'accept'. Must be called with
435 * the socket locked or with interrupts disabled
436 */
437
438 static struct sk_buff *tcp_find_established(struct sock *s)
/* ![[previous]](../icons/n_left.png)
![[next]](../icons/right.png)
![[first]](../icons/n_first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
439 {
440 struct sk_buff *p=skb_peek(&s->receive_queue);
441 if(p==NULL)
442 return NULL;
443 do
444 {
445 if(p->sk->state == TCP_ESTABLISHED || p->sk->state >= TCP_FIN_WAIT1)
446 return p;
447 p=p->next;
448 }
449 while(p!=(struct sk_buff *)&s->receive_queue);
450 return NULL;
451 }
452
453 /*
454 * Remove a completed connection and return it. This is used by
455 * tcp_accept() to get connections from the queue.
456 */
457
458 static struct sk_buff *tcp_dequeue_established(struct sock *s)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
459 {
460 struct sk_buff *skb;
461 unsigned long flags;
462 save_flags(flags);
463 cli();
464 skb=tcp_find_established(s);
465 if(skb!=NULL)
466 skb_unlink(skb); /* Take it off the queue */
467 restore_flags(flags);
468 return skb;
469 }
470
471 /*
472 * This routine closes sockets which have been at least partially
473 * opened, but not yet accepted. Currently it is only called by
474 * tcp_close, and timeout mirrors the value there.
475 */
476
477 static void tcp_close_pending (struct sock *sk)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
478 {
479 struct sk_buff *skb;
480
481 while ((skb = skb_dequeue(&sk->receive_queue)) != NULL)
482 {
483 tcp_close(skb->sk, 0);
484 kfree_skb(skb, FREE_READ);
485 }
486 return;
487 }
488
489 /*
490 * Enter the time wait state.
491 */
492
493 void tcp_time_wait(struct sock *sk)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
494 {
495 tcp_set_state(sk,TCP_TIME_WAIT);
496 sk->shutdown = SHUTDOWN_MASK;
497 if (!sk->dead)
498 sk->state_change(sk);
499 tcp_reset_msl_timer(sk, TIME_CLOSE, TCP_TIMEWAIT_LEN);
500 }
501
502
503 /*
504 * This routine is called by the ICMP module when it gets some
505 * sort of error condition. If err < 0 then the socket should
506 * be closed and the error returned to the user. If err > 0
507 * it's just the icmp type << 8 | icmp code. After adjustment
508 * header points to the first 8 bytes of the tcp header. We need
509 * to find the appropriate port.
510 */
511
512 void tcp_err(int type, int code, unsigned char *header, __u32 daddr,
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
513 __u32 saddr, struct inet_protocol *protocol)
514 {
515 struct tcphdr *th = (struct tcphdr *)header;
516 struct sock *sk;
517
518 /*
519 * This one is _WRONG_. FIXME urgently.
520 */
521 #ifndef CONFIG_NO_PATH_MTU_DISCOVERY
522 struct iphdr *iph=(struct iphdr *)(header-sizeof(struct iphdr));
523 #endif
524 th =(struct tcphdr *)header;
525 sk = get_sock(&tcp_prot, th->source, daddr, th->dest, saddr);
526
527 if (sk == NULL)
528 return;
529
530 if (type == ICMP_SOURCE_QUENCH)
531 {
532 /*
533 * FIXME:
534 * For now we will just trigger a linear backoff.
535 * The slow start code should cause a real backoff here.
536 */
537 if (sk->cong_window > 4)
538 sk->cong_window--;
539 return;
540 }
541
542 if (type == ICMP_PARAMETERPROB)
543 {
544 sk->err=EPROTO;
545 sk->error_report(sk);
546 }
547
548 #ifndef CONFIG_NO_PATH_MTU_DISCOVERY
549 if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
550 {
551 struct rtable * rt;
552 /*
553 * Ugly trick to pass MTU to protocol layer.
554 * Really we should add argument "info" to error handler.
555 */
556 unsigned short new_mtu = ntohs(iph->id);
557
558 if ((rt = sk->ip_route_cache) != NULL)
559 if (rt->rt_mtu > new_mtu)
560 rt->rt_mtu = new_mtu;
561
562 if (sk->mtu > new_mtu - sizeof(struct iphdr) - sizeof(struct tcphdr)
563 && new_mtu > sizeof(struct iphdr)+sizeof(struct tcphdr))
564 sk->mtu = new_mtu - sizeof(struct iphdr) - sizeof(struct tcphdr);
565
566 return;
567 }
568 #endif
569
570 /*
571 * If we've already connected we will keep trying
572 * until we time out, or the user gives up.
573 */
574
575 if (code < 13)
576 {
577 if(icmp_err_convert[code].fatal || sk->state == TCP_SYN_SENT || sk->state == TCP_SYN_RECV)
578 {
579 sk->err = icmp_err_convert[code].errno;
580 if (sk->state == TCP_SYN_SENT || sk->state == TCP_SYN_RECV)
581 {
582 tcp_statistics.TcpAttemptFails++;
583 tcp_set_state(sk,TCP_CLOSE);
584 sk->error_report(sk); /* Wake people up to see the error (see connect in sock.c) */
585 }
586 }
587 else /* Only an error on timeout */
588 sk->err_soft = icmp_err_convert[code].errno;
589 }
590 }
591
592
593 /*
594 * Walk down the receive queue counting readable data until we hit the end or we find a gap
595 * in the received data queue (ie a frame missing that needs sending to us). Not
596 * sorting using two queues as data arrives makes life so much harder.
597 */
598
599 static int tcp_readable(struct sock *sk)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
600 {
601 unsigned long counted;
602 unsigned long amount;
603 struct sk_buff *skb;
604 int sum;
605 unsigned long flags;
606
607 if(sk && sk->debug)
608 printk("tcp_readable: %p - ",sk);
609
610 save_flags(flags);
611 cli();
612 if (sk == NULL || (skb = skb_peek(&sk->receive_queue)) == NULL)
613 {
614 restore_flags(flags);
615 if(sk && sk->debug)
616 printk("empty\n");
617 return(0);
618 }
619
620 counted = sk->copied_seq; /* Where we are at the moment */
621 amount = 0;
622
623 /*
624 * Do until a push or until we are out of data.
625 */
626
627 do
628 {
629 if (before(counted, skb->seq)) /* Found a hole so stops here */
630 break;
631 sum = skb->len - (counted - skb->seq); /* Length - header but start from where we are up to (avoid overlaps) */
632 if (skb->h.th->syn)
633 sum++;
634 if (sum > 0)
635 { /* Add it up, move on */
636 amount += sum;
637 if (skb->h.th->syn)
638 amount--;
639 counted += sum;
640 }
641 /*
642 * Don't count urg data ... but do it in the right place!
643 * Consider: "old_data (ptr is here) URG PUSH data"
644 * The old code would stop at the first push because
645 * it counted the urg (amount==1) and then does amount--
646 * *after* the loop. This means tcp_readable() always
647 * returned zero if any URG PUSH was in the queue, even
648 * though there was normal data available. If we subtract
649 * the urg data right here, we even get it to work for more
650 * than one URG PUSH skb without normal data.
651 * This means that select() finally works now with urg data
652 * in the queue. Note that rlogin was never affected
653 * because it doesn't use select(); it uses two processes
654 * and a blocking read(). And the queue scan in tcp_read()
655 * was correct. Mike <pall@rz.uni-karlsruhe.de>
656 */
657 if (skb->h.th->urg)
658 amount--; /* don't count urg data */
659 if (amount && skb->h.th->psh) break;
660 skb = skb->next;
661 }
662 while(skb != (struct sk_buff *)&sk->receive_queue);
663
664 restore_flags(flags);
665 if(sk->debug)
666 printk("got %lu bytes.\n",amount);
667 return(amount);
668 }
669
670 /*
671 * LISTEN is a special case for select..
672 */
673 static int tcp_listen_select(struct sock *sk, int sel_type, select_table *wait)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
674 {
675 if (sel_type == SEL_IN) {
676 int retval;
677
678 lock_sock(sk);
679 retval = (tcp_find_established(sk) != NULL);
680 release_sock(sk);
681 if (!retval)
682 select_wait(&master_select_wakeup,wait);
683 return retval;
684 }
685 return 0;
686 }
687
688
689 /*
690 * Wait for a TCP event.
691 *
692 * Note that we don't need to lock the socket, as the upper select layers
693 * take care of normal races (between the test and the event) and we don't
694 * go look at any of the socket buffers directly.
695 */
696 static int tcp_select(struct sock *sk, int sel_type, select_table *wait)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
697 {
698 if (sk->state == TCP_LISTEN)
699 return tcp_listen_select(sk, sel_type, wait);
700
701 switch(sel_type) {
702 case SEL_IN:
703 if (sk->err)
704 return 1;
705 if (sk->state == TCP_SYN_SENT || sk->state == TCP_SYN_RECV)
706 break;
707
708 if (sk->shutdown & RCV_SHUTDOWN)
709 return 1;
710
711 if (sk->acked_seq == sk->copied_seq)
712 break;
713
714 if (sk->urg_seq != sk->copied_seq ||
715 sk->acked_seq != sk->copied_seq+1 ||
716 sk->urginline || !sk->urg_data)
717 return 1;
718 break;
719
720 case SEL_OUT:
721 if (sk->err)
722 return 1;
723 if (sk->shutdown & SEND_SHUTDOWN)
724 return 0;
725 if (sk->state == TCP_SYN_SENT || sk->state == TCP_SYN_RECV)
726 break;
727 /*
728 * This is now right thanks to a small fix
729 * by Matt Dillon.
730 */
731
732 if (sock_wspace(sk) < sk->mtu+128+sk->prot->max_header)
733 break;
734 return 1;
735
736 case SEL_EX:
737 if (sk->urg_data)
738 return 1;
739 break;
740 }
741 select_wait(sk->sleep, wait);
742 return 0;
743 }
744
745 int tcp_ioctl(struct sock *sk, int cmd, unsigned long arg)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
746 {
747 int err;
748 switch(cmd)
749 {
750
751 case TIOCINQ:
752 #ifdef FIXME /* FIXME: */
753 case FIONREAD:
754 #endif
755 {
756 unsigned long amount;
757
758 if (sk->state == TCP_LISTEN)
759 return(-EINVAL);
760
761 lock_sock(sk);
762 amount = tcp_readable(sk);
763 release_sock(sk);
764 err=verify_area(VERIFY_WRITE,(void *)arg, sizeof(int));
765 if(err)
766 return err;
767 put_user(amount, (int *)arg);
768 return(0);
769 }
770 case SIOCATMARK:
771 {
772 int answ = sk->urg_data && sk->urg_seq == sk->copied_seq;
773
774 err = verify_area(VERIFY_WRITE,(void *) arg, sizeof(int));
775 if (err)
776 return err;
777 put_user(answ,(int *) arg);
778 return(0);
779 }
780 case TIOCOUTQ:
781 {
782 unsigned long amount;
783
784 if (sk->state == TCP_LISTEN) return(-EINVAL);
785 amount = sock_wspace(sk);
786 err=verify_area(VERIFY_WRITE,(void *)arg, sizeof(int));
787 if(err)
788 return err;
789 put_user(amount, (int *)arg);
790 return(0);
791 }
792 default:
793 return(-EINVAL);
794 }
795 }
796
797
798 /*
799 * This routine computes a TCP checksum.
800 *
801 * Modified January 1995 from a go-faster DOS routine by
802 * Jorge Cwik <jorge@laser.satlink.net>
803 */
804 #undef DEBUG_TCP_CHECK
805 void tcp_send_check(struct tcphdr *th, unsigned long saddr,
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
806 unsigned long daddr, int len, struct sk_buff *skb)
807 {
808 #ifdef DEBUG_TCP_CHECK
809 u16 check;
810 #endif
811 th->check = 0;
812 th->check = tcp_check(th, len, saddr, daddr,
813 csum_partial((char *)th,sizeof(*th),skb->csum));
814
815 #ifdef DEBUG_TCP_CHECK
816 check = th->check;
817 th->check = 0;
818 th->check = tcp_check(th, len, saddr, daddr,
819 csum_partial((char *)th,len,0));
820 if (check != th->check) {
821 static int count = 0;
822 if (++count < 10) {
823 printk("Checksum %x (%x) from %p\n", th->check, check,
824 (&th)[-1]);
825 printk("TCP=<off:%d a:%d s:%d f:%d>\n", th->doff*4, th->ack, th->syn, th->fin);
826 }
827 }
828 #endif
829 }
830
831
832 /*
833 * This routine builds a generic TCP header.
834 */
835
836 extern __inline int tcp_build_header(struct tcphdr *th, struct sock *sk, int push)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
837 {
838
839 memcpy(th,(void *) &(sk->dummy_th), sizeof(*th));
840 th->seq = htonl(sk->write_seq);
841 th->psh =(push == 0) ? 1 : 0;
842 sk->ack_backlog = 0;
843 sk->bytes_rcv = 0;
844 sk->ack_timed = 0;
845 th->ack_seq = htonl(sk->acked_seq);
846 sk->window = tcp_select_window(sk);
847 th->window = htons(sk->window);
848
849 return(sizeof(*th));
850 }
851
852 /*
853 * Wait for a socket to get into the connected state
854 */
855 static void wait_for_tcp_connect(struct sock * sk)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
856 {
857 release_sock(sk);
858 cli();
859 if (sk->state != TCP_ESTABLISHED && sk->state != TCP_CLOSE_WAIT && sk->err == 0)
860 {
861 interruptible_sleep_on(sk->sleep);
862 }
863 sti();
864 lock_sock(sk);
865 }
866
867 /*
868 * Wait for more memory for a socket
869 */
870 static void wait_for_tcp_memory(struct sock * sk)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
871 {
872 release_sock(sk);
873 cli();
874 if (sk->wmem_alloc*2 > sk->sndbuf &&
875 (sk->state == TCP_ESTABLISHED||sk->state == TCP_CLOSE_WAIT)
876 && sk->err == 0)
877 {
878 sk->socket->flags &= ~SO_NOSPACE;
879 interruptible_sleep_on(sk->sleep);
880 }
881 sti();
882 lock_sock(sk);
883 }
884
885
886 /*
887 * This routine copies from a user buffer into a socket,
888 * and starts the transmit system.
889 */
890
891 static int do_tcp_sendmsg(struct sock *sk, struct msghdr *msg,
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
892 int len, int nonblock, int flags)
893 {
894 int copied = 0;
895 int copy;
896 int tmp;
897 int seglen;
898 int iovct=0;
899 struct sk_buff *skb;
900 struct sk_buff *send_tmp;
901 struct proto *prot;
902 struct device *dev = NULL;
903 unsigned char *from;
904
905 /*
906 * Ok commence sending
907 */
908
909 while(iovct<msg->msg_iovlen)
910 {
911 seglen=msg->msg_iov[iovct].iov_len;
912 from=msg->msg_iov[iovct++].iov_base;
913 prot = sk->prot;
914 while(seglen > 0)
915 {
916 /*
917 * Stop on errors
918 */
919 if (sk->err)
920 {
921 if (copied)
922 return copied;
923 return sock_error(sk);
924 }
925
926 /*
927 * Make sure that we are established.
928 */
929 if (sk->shutdown & SEND_SHUTDOWN)
930 {
931 if (copied)
932 return copied;
933 return -EPIPE;
934 }
935
936 /*
937 * Wait for a connection to finish.
938 */
939 while (sk->state != TCP_ESTABLISHED && sk->state != TCP_CLOSE_WAIT)
940 {
941 if (copied)
942 return copied;
943
944 if (sk->err)
945 return sock_error(sk);
946
947 if (sk->state != TCP_SYN_SENT && sk->state != TCP_SYN_RECV)
948 {
949 if (sk->keepopen)
950 send_sig(SIGPIPE, current, 0);
951 return -EPIPE;
952 }
953
954 if (nonblock)
955 return -EAGAIN;
956
957 if (current->signal & ~current->blocked)
958 return -ERESTARTSYS;
959
960 wait_for_tcp_connect(sk);
961 }
962
963 /*
964 * The following code can result in copy <= if sk->mss is ever
965 * decreased. It shouldn't be. sk->mss is min(sk->mtu, sk->max_window).
966 * sk->mtu is constant once SYN processing is finished. I.e. we
967 * had better not get here until we've seen his SYN and at least one
968 * valid ack. (The SYN sets sk->mtu and the ack sets sk->max_window.)
969 * But ESTABLISHED should guarantee that. sk->max_window is by definition
970 * non-decreasing. Note that any ioctl to set user_mss must be done
971 * before the exchange of SYN's. If the initial ack from the other
972 * end has a window of 0, max_window and thus mss will both be 0.
973 */
974
975 /*
976 * Now we need to check if we have a half built packet.
977 */
978 #ifndef CONFIG_NO_PATH_MTU_DISCOVERY
979 /*
980 * FIXME: I'm almost sure that this fragment is BUG,
981 * but it works... I do not know why 8) --ANK
982 *
983 * Really, we should rebuild all the queues...
984 * It's difficult. Temprorary hack is to send all
985 * queued segments with allowed fragmentation.
986 */
987 {
988 int new_mss = min(sk->mtu, sk->max_window);
989 if (new_mss < sk->mss)
990 {
991 tcp_send_partial(sk);
992 sk->mss = new_mss;
993 }
994 }
995 #endif
996
997 if ((skb = tcp_dequeue_partial(sk)) != NULL)
998 {
999 int tcp_size;
1000
1001 tcp_size = skb->tail - (unsigned char *)(skb->h.th + 1);
1002
1003 /* Add more stuff to the end of skb->len */
1004 if (!(flags & MSG_OOB))
1005 {
1006 copy = min(sk->mss - tcp_size, seglen);
1007 if (copy <= 0)
1008 {
1009 printk("TCP: **bug**: \"copy\" <= 0\n");
1010 return -EFAULT;
1011 }
1012 tcp_size += copy;
1013 memcpy_fromfs(skb_put(skb,copy), from, copy);
1014 skb->csum = csum_partial(skb->tail - tcp_size, tcp_size, 0);
1015 from += copy;
1016 copied += copy;
1017 len -= copy;
1018 sk->write_seq += copy;
1019 seglen -= copy;
1020 }
1021 if (tcp_size >= sk->mss || (flags & MSG_OOB) || !sk->packets_out)
1022 tcp_send_skb(sk, skb);
1023 else
1024 tcp_enqueue_partial(skb, sk);
1025 continue;
1026 }
1027
1028 /*
1029 * We also need to worry about the window.
1030 * If window < 1/2 the maximum window we've seen from this
1031 * host, don't use it. This is sender side
1032 * silly window prevention, as specified in RFC1122.
1033 * (Note that this is different than earlier versions of
1034 * SWS prevention, e.g. RFC813.). What we actually do is
1035 * use the whole MSS. Since the results in the right
1036 * edge of the packet being outside the window, it will
1037 * be queued for later rather than sent.
1038 */
1039
1040 copy = sk->window_seq - sk->write_seq;
1041 if (copy <= 0 || copy < (sk->max_window >> 1) || copy > sk->mss)
1042 copy = sk->mss;
1043 if (copy > seglen)
1044 copy = seglen;
1045 if (copy <= 0)
1046 {
1047 printk("TCP: **bug**: copy=%d, sk->mss=%d\n", copy, sk->mss);
1048 return -EFAULT;
1049 }
1050
1051 /*
1052 * We should really check the window here also.
1053 */
1054
1055 send_tmp = NULL;
1056 if (copy < sk->mss && !(flags & MSG_OOB) && sk->packets_out)
1057 {
1058 skb = sock_wmalloc(sk, sk->mtu + 128 + prot->max_header + 15, 0, GFP_KERNEL);
1059 send_tmp = skb;
1060 }
1061 else
1062 {
1063 skb = sock_wmalloc(sk, copy + prot->max_header + 15 , 0, GFP_KERNEL);
1064 }
1065
1066 /*
1067 * If we didn't get any memory, we need to sleep.
1068 */
1069
1070 if (skb == NULL)
1071 {
1072 sk->socket->flags |= SO_NOSPACE;
1073 if (nonblock)
1074 {
1075 if (copied)
1076 return copied;
1077 return -EAGAIN;
1078 }
1079
1080 if (current->signal & ~current->blocked)
1081 {
1082 if (copied)
1083 return copied;
1084 return -ERESTARTSYS;
1085 }
1086
1087 wait_for_tcp_memory(sk);
1088 continue;
1089 }
1090
1091 skb->sk = sk;
1092 skb->free = 0;
1093 skb->localroute = sk->localroute|(flags&MSG_DONTROUTE);
1094
1095 /*
1096 * FIXME: we need to optimize this.
1097 * Perhaps some hints here would be good.
1098 */
1099
1100 tmp = prot->build_header(skb, sk->saddr, sk->daddr, &dev,
1101 IPPROTO_TCP, sk->opt, skb->truesize,sk->ip_tos,sk->ip_ttl,&sk->ip_route_cache);
1102 if (tmp < 0 )
1103 {
1104 sock_wfree(sk, skb);
1105 if (copied)
1106 return(copied);
1107 return(tmp);
1108 }
1109 #ifndef CONFIG_NO_PATH_MTU_DISCOVERY
1110 skb->ip_hdr->frag_off |= htons(IP_DF);
1111 #endif
1112 skb->dev = dev;
1113 skb->h.th =(struct tcphdr *)skb_put(skb,sizeof(struct tcphdr));
1114 tmp = tcp_build_header(skb->h.th, sk, seglen-copy);
1115 if (tmp < 0)
1116 {
1117 sock_wfree(sk, skb);
1118 if (copied)
1119 return(copied);
1120 return(tmp);
1121 }
1122
1123 if (flags & MSG_OOB)
1124 {
1125 skb->h.th->urg = 1;
1126 skb->h.th->urg_ptr = ntohs(copy);
1127 }
1128
1129 skb->csum = csum_partial_copy_fromuser(from,
1130 skb_put(skb,copy), copy, 0);
1131
1132 from += copy;
1133 copied += copy;
1134 len -= copy;
1135 seglen -= copy;
1136 skb->free = 0;
1137 sk->write_seq += copy;
1138
1139 if (send_tmp != NULL)
1140 {
1141 tcp_enqueue_partial(send_tmp, sk);
1142 continue;
1143 }
1144 tcp_send_skb(sk, skb);
1145 }
1146 }
1147 sk->err = 0;
1148
1149 return copied;
1150 }
1151
1152
1153 static int tcp_sendmsg(struct sock *sk, struct msghdr *msg,
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
1154 int len, int nonblock, int flags)
1155 {
1156 int retval = -EINVAL;
1157
1158 /*
1159 * Do sanity checking for sendmsg/sendto/send
1160 */
1161
1162 if (flags & ~(MSG_OOB|MSG_DONTROUTE))
1163 goto out;
1164 if (msg->msg_name) {
1165 struct sockaddr_in *addr=(struct sockaddr_in *)msg->msg_name;
1166
1167 if (msg->msg_namelen < sizeof(*addr))
1168 goto out;
1169 if (addr->sin_family && addr->sin_family != AF_INET)
1170 goto out;
1171 retval = -ENOTCONN;
1172 if(sk->state == TCP_CLOSE)
1173 goto out;
1174 retval = -EISCONN;
1175 if (addr->sin_port != sk->dummy_th.dest)
1176 goto out;
1177 if (addr->sin_addr.s_addr != sk->daddr)
1178 goto out;
1179 }
1180
1181 lock_sock(sk);
1182 retval = do_tcp_sendmsg(sk, msg, len, nonblock, flags);
1183
1184 /*
1185 * Nagle's rule. Turn Nagle off with TCP_NODELAY for highly
1186 * interactive fast network servers. It's meant to be on and
1187 * it really improves the throughput though not the echo time
1188 * on my slow slip link - Alan
1189 *
1190 * If not nagling we can send on the before case too..
1191 */
1192
1193 if (sk->partial) {
1194 if (!sk->packets_out ||
1195 (sk->nonagle && before(sk->write_seq , sk->window_seq))) {
1196 tcp_send_partial(sk);
1197 }
1198 }
1199
1200 release_sock(sk);
1201
1202 out:
1203 return retval;
1204 }
1205
1206
1207 /*
1208 * Send an ack if one is backlogged at this point. Ought to merge
1209 * this with tcp_send_ack().
1210 * This is called for delayed acks also.
1211 */
1212
1213 void tcp_read_wakeup(struct sock *sk)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
1214 {
1215 int tmp;
1216 struct device *dev = NULL;
1217 struct tcphdr *t1;
1218 struct sk_buff *buff;
1219
1220 if (!sk->ack_backlog)
1221 return;
1222
1223 /*
1224 * If we're closed, don't send an ack, or we'll get a RST
1225 * from the closed destination.
1226 */
1227 if ((sk->state == TCP_CLOSE) || (sk->state == TCP_TIME_WAIT))
1228 return;
1229
1230 /*
1231 * FIXME: we need to put code here to prevent this routine from
1232 * being called. Being called once in a while is ok, so only check
1233 * if this is the second time in a row.
1234 */
1235
1236 /*
1237 * We need to grab some memory, and put together an ack,
1238 * and then put it into the queue to be sent.
1239 */
1240
1241 buff = sock_wmalloc(sk,MAX_ACK_SIZE,1, GFP_ATOMIC);
1242 if (buff == NULL)
1243 {
1244 /* Try again real soon. */
1245 tcp_reset_xmit_timer(sk, TIME_WRITE, HZ);
1246 return;
1247 }
1248
1249 buff->sk = sk;
1250 buff->localroute = sk->localroute;
1251 buff->csum = 0;
1252
1253 /*
1254 * Put in the IP header and routing stuff.
1255 */
1256
1257 tmp = sk->prot->build_header(buff, sk->saddr, sk->daddr, &dev,
1258 IPPROTO_TCP, sk->opt, MAX_ACK_SIZE,sk->ip_tos,sk->ip_ttl,&sk->ip_route_cache);
1259 if (tmp < 0)
1260 {
1261 buff->free = 1;
1262 sock_wfree(sk, buff);
1263 return;
1264 }
1265
1266 t1 =(struct tcphdr *)skb_put(buff,sizeof(struct tcphdr));
1267
1268 memcpy(t1,(void *) &sk->dummy_th, sizeof(*t1));
1269 t1->seq = htonl(sk->sent_seq);
1270
1271 sk->ack_backlog = 0;
1272 sk->bytes_rcv = 0;
1273
1274 sk->window = tcp_select_window(sk);
1275 t1->window = htons(sk->window);
1276 t1->ack_seq = htonl(sk->acked_seq);
1277 t1->doff = sizeof(*t1)/4;
1278 tcp_send_check(t1, sk->saddr, sk->daddr, sizeof(*t1), buff);
1279 sk->prot->queue_xmit(sk, dev, buff, 1);
1280 tcp_statistics.TcpOutSegs++;
1281 }
1282
1283
1284 /*
1285 * Handle reading urgent data. BSD has very simple semantics for
1286 * this, no blocking and very strange errors 8)
1287 */
1288
1289 static int tcp_recv_urg(struct sock * sk, int nonblock,
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
1290 struct msghdr *msg, int len, int flags, int *addr_len)
1291 {
1292 /*
1293 * No URG data to read
1294 */
1295 if (sk->urginline || !sk->urg_data || sk->urg_data == URG_READ)
1296 return -EINVAL; /* Yes this is right ! */
1297
1298 if (sk->err)
1299 return sock_error(sk);
1300
1301 if (sk->state == TCP_CLOSE || sk->done)
1302 {
1303 if (!sk->done)
1304 {
1305 sk->done = 1;
1306 return 0;
1307 }
1308 return -ENOTCONN;
1309 }
1310
1311 if (sk->shutdown & RCV_SHUTDOWN)
1312 {
1313 sk->done = 1;
1314 return 0;
1315 }
1316 lock_sock(sk);
1317 if (sk->urg_data & URG_VALID)
1318 {
1319 char c = sk->urg_data;
1320 if (!(flags & MSG_PEEK))
1321 sk->urg_data = URG_READ;
1322 memcpy_toiovec(msg->msg_iov, &c, 1);
1323 if(msg->msg_name)
1324 {
1325 struct sockaddr_in *sin=(struct sockaddr_in *)msg->msg_name;
1326 sin->sin_family=AF_INET;
1327 sin->sin_addr.s_addr=sk->daddr;
1328 sin->sin_port=sk->dummy_th.dest;
1329 }
1330 if(addr_len)
1331 *addr_len=sizeof(struct sockaddr_in);
1332 release_sock(sk);
1333 return 1;
1334 }
1335 release_sock(sk);
1336
1337 /*
1338 * Fixed the recv(..., MSG_OOB) behaviour. BSD docs and
1339 * the available implementations agree in this case:
1340 * this call should never block, independent of the
1341 * blocking state of the socket.
1342 * Mike <pall@rz.uni-karlsruhe.de>
1343 */
1344 return -EAGAIN;
1345 }
1346
1347 /*
1348 * Release a skb if it is no longer needed. This routine
1349 * must be called with interrupts disabled or with the
1350 * socket locked so that the sk_buff queue operation is ok.
1351 */
1352
1353 static inline void tcp_eat_skb(struct sock *sk, struct sk_buff * skb)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
1354 {
1355 skb->sk = sk;
1356 __skb_unlink(skb, &sk->receive_queue);
1357 kfree_skb(skb, FREE_READ);
1358 }
1359
1360 /*
1361 * FIXME:
1362 * This routine frees used buffers.
1363 * It should consider sending an ACK to let the
1364 * other end know we now have a bigger window.
1365 */
1366
1367 static void cleanup_rbuf(struct sock *sk)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
1368 {
1369 struct sk_buff *skb;
1370
1371 /*
1372 * NOTE! The socket must be locked, so that we don't get
1373 * a messed-up receive queue.
1374 */
1375 while ((skb=skb_peek(&sk->receive_queue)) != NULL) {
1376 if (!skb->used || skb->users)
1377 break;
1378 tcp_eat_skb(sk, skb);
1379 }
1380
1381 /* If we raised the window due to cleaning up, tell the world.. */
1382 if (tcp_raise_window(sk)) {
1383 sk->ack_backlog++;
1384 tcp_read_wakeup(sk);
1385 }
1386 }
1387
1388
1389 /*
1390 * This routine copies from a sock struct into the user buffer.
1391 */
1392
1393 static int tcp_recvmsg(struct sock *sk, struct msghdr *msg,
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
1394 int len, int nonblock, int flags, int *addr_len)
1395 {
1396 struct wait_queue wait = { current, NULL };
1397 int copied = 0;
1398 u32 peek_seq;
1399 volatile u32 *seq; /* So gcc doesn't overoptimise */
1400 unsigned long used;
1401
1402 /*
1403 * This error should be checked.
1404 */
1405
1406 if (sk->state == TCP_LISTEN)
1407 return -ENOTCONN;
1408
1409 /*
1410 * Urgent data needs to be handled specially.
1411 */
1412
1413 if (flags & MSG_OOB)
1414 return tcp_recv_urg(sk, nonblock, msg, len, flags, addr_len);
1415
1416 /*
1417 * Copying sequence to update. This is volatile to handle
1418 * the multi-reader case neatly (memcpy_to/fromfs might be
1419 * inline and thus not flush cached variables otherwise).
1420 */
1421
1422 peek_seq = sk->copied_seq;
1423 seq = &sk->copied_seq;
1424 if (flags & MSG_PEEK)
1425 seq = &peek_seq;
1426
1427 add_wait_queue(sk->sleep, &wait);
1428 lock_sock(sk);
1429 while (len > 0)
1430 {
1431 struct sk_buff * skb;
1432 u32 offset;
1433
1434 /*
1435 * Are we at urgent data? Stop if we have read anything.
1436 */
1437
1438 if (copied && sk->urg_data && sk->urg_seq == *seq)
1439 break;
1440
1441 /*
1442 * Next get a buffer.
1443 */
1444
1445 current->state = TASK_INTERRUPTIBLE;
1446
1447 skb = skb_peek(&sk->receive_queue);
1448 do
1449 {
1450 if (!skb)
1451 break;
1452 if (before(*seq, skb->seq))
1453 break;
1454 offset = *seq - skb->seq;
1455 if (skb->h.th->syn)
1456 offset--;
1457 if (offset < skb->len)
1458 goto found_ok_skb;
1459 if (skb->h.th->fin)
1460 goto found_fin_ok;
1461 if (!(flags & MSG_PEEK))
1462 skb->used = 1;
1463 skb = skb->next;
1464 }
1465 while (skb != (struct sk_buff *)&sk->receive_queue);
1466
1467 if (copied)
1468 break;
1469
1470 if (sk->err)
1471 {
1472 copied = sock_error(sk);
1473 break;
1474 }
1475
1476 if (sk->state == TCP_CLOSE)
1477 {
1478 if (!sk->done)
1479 {
1480 sk->done = 1;
1481 break;
1482 }
1483 copied = -ENOTCONN;
1484 break;
1485 }
1486
1487 if (sk->shutdown & RCV_SHUTDOWN)
1488 {
1489 sk->done = 1;
1490 break;
1491 }
1492
1493 if (nonblock)
1494 {
1495 copied = -EAGAIN;
1496 break;
1497 }
1498
1499 cleanup_rbuf(sk);
1500 release_sock(sk);
1501 sk->socket->flags |= SO_WAITDATA;
1502 schedule();
1503 sk->socket->flags &= ~SO_WAITDATA;
1504 lock_sock(sk);
1505
1506 if (current->signal & ~current->blocked)
1507 {
1508 copied = -ERESTARTSYS;
1509 break;
1510 }
1511 continue;
1512
1513 found_ok_skb:
1514 /*
1515 * Lock the buffer. We can be fairly relaxed as
1516 * an interrupt will never steal a buffer we are
1517 * using unless I've missed something serious in
1518 * tcp_data.
1519 */
1520
1521 skb->users++;
1522
1523 /*
1524 * Ok so how much can we use ?
1525 */
1526
1527 used = skb->len - offset;
1528 if (len < used)
1529 used = len;
1530 /*
1531 * Do we have urgent data here?
1532 */
1533
1534 if (sk->urg_data)
1535 {
1536 u32 urg_offset = sk->urg_seq - *seq;
1537 if (urg_offset < used)
1538 {
1539 if (!urg_offset)
1540 {
1541 if (!sk->urginline)
1542 {
1543 ++*seq;
1544 offset++;
1545 used--;
1546 }
1547 }
1548 else
1549 used = urg_offset;
1550 }
1551 }
1552
1553 /*
1554 * Copy it - We _MUST_ update *seq first so that we
1555 * don't ever double read when we have dual readers
1556 */
1557
1558 *seq += used;
1559
1560 /*
1561 * This memcpy_tofs can sleep. If it sleeps and we
1562 * do a second read it relies on the skb->users to avoid
1563 * a crash when cleanup_rbuf() gets called.
1564 */
1565
1566 memcpy_toiovec(msg->msg_iov,((unsigned char *)skb->h.th) +
1567 skb->h.th->doff*4 + offset, used);
1568 copied += used;
1569 len -= used;
1570
1571 /*
1572 * We now will not sleep again until we are finished
1573 * with skb. Sorry if you are doing the SMP port
1574 * but you'll just have to fix it neatly ;)
1575 */
1576
1577 skb->users --;
1578
1579 if (after(sk->copied_seq,sk->urg_seq))
1580 sk->urg_data = 0;
1581 if (used + offset < skb->len)
1582 continue;
1583
1584 /*
1585 * Process the FIN.
1586 */
1587
1588 if (skb->h.th->fin)
1589 goto found_fin_ok;
1590 if (flags & MSG_PEEK)
1591 continue;
1592 skb->used = 1;
1593 if (!skb->users)
1594 tcp_eat_skb(sk, skb);
1595 continue;
1596
1597 found_fin_ok:
1598 ++*seq;
1599 if (flags & MSG_PEEK)
1600 break;
1601
1602 /*
1603 * All is done
1604 */
1605
1606 skb->used = 1;
1607 sk->shutdown |= RCV_SHUTDOWN;
1608 break;
1609
1610 }
1611
1612 if(copied>0 && msg->msg_name)
1613 {
1614 struct sockaddr_in *sin=(struct sockaddr_in *)msg->msg_name;
1615 sin->sin_family=AF_INET;
1616 sin->sin_addr.s_addr=sk->daddr;
1617 sin->sin_port=sk->dummy_th.dest;
1618 }
1619 if(addr_len)
1620 *addr_len=sizeof(struct sockaddr_in);
1621
1622 remove_wait_queue(sk->sleep, &wait);
1623 current->state = TASK_RUNNING;
1624
1625 /* Clean up data we have read: This will do ACK frames */
1626 cleanup_rbuf(sk);
1627 release_sock(sk);
1628 return copied;
1629 }
1630
1631
1632
1633 /*
1634 * State processing on a close. This implements the state shift for
1635 * sending our FIN frame. Note that we only send a FIN for some
1636 * states. A shutdown() may have already sent the FIN, or we may be
1637 * closed.
1638 */
1639
1640 static int tcp_close_state(struct sock *sk, int dead)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
1641 {
1642 int ns=TCP_CLOSE;
1643 int send_fin=0;
1644 switch(sk->state)
1645 {
1646 case TCP_SYN_SENT: /* No SYN back, no FIN needed */
1647 break;
1648 case TCP_SYN_RECV:
1649 case TCP_ESTABLISHED: /* Closedown begin */
1650 ns=TCP_FIN_WAIT1;
1651 send_fin=1;
1652 break;
1653 case TCP_FIN_WAIT1: /* Already closing, or FIN sent: no change */
1654 case TCP_FIN_WAIT2:
1655 case TCP_CLOSING:
1656 ns=sk->state;
1657 break;
1658 case TCP_CLOSE:
1659 case TCP_LISTEN:
1660 break;
1661 case TCP_CLOSE_WAIT: /* They have FIN'd us. We send our FIN and
1662 wait only for the ACK */
1663 ns=TCP_LAST_ACK;
1664 send_fin=1;
1665 }
1666
1667 tcp_set_state(sk,ns);
1668
1669 /*
1670 * This is a (useful) BSD violating of the RFC. There is a
1671 * problem with TCP as specified in that the other end could
1672 * keep a socket open forever with no application left this end.
1673 * We use a 3 minute timeout (about the same as BSD) then kill
1674 * our end. If they send after that then tough - BUT: long enough
1675 * that we won't make the old 4*rto = almost no time - whoops
1676 * reset mistake.
1677 */
1678 if(dead && ns==TCP_FIN_WAIT2)
1679 {
1680 int timer_active=del_timer(&sk->timer);
1681 if(timer_active)
1682 add_timer(&sk->timer);
1683 else
1684 tcp_reset_msl_timer(sk, TIME_CLOSE, TCP_FIN_TIMEOUT);
1685 }
1686
1687 return send_fin;
1688 }
1689
1690 /*
1691 * Shutdown the sending side of a connection. Much like close except
1692 * that we don't receive shut down or set sk->dead.
1693 */
1694
1695 void tcp_shutdown(struct sock *sk, int how)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
1696 {
1697 /*
1698 * We need to grab some memory, and put together a FIN,
1699 * and then put it into the queue to be sent.
1700 * Tim MacKenzie(tym@dibbler.cs.monash.edu.au) 4 Dec '92.
1701 */
1702
1703 if (!(how & SEND_SHUTDOWN))
1704 return;
1705
1706 /*
1707 * If we've already sent a FIN, or it's a closed state
1708 */
1709
1710 if (sk->state == TCP_FIN_WAIT1 ||
1711 sk->state == TCP_FIN_WAIT2 ||
1712 sk->state == TCP_CLOSING ||
1713 sk->state == TCP_LAST_ACK ||
1714 sk->state == TCP_TIME_WAIT ||
1715 sk->state == TCP_CLOSE ||
1716 sk->state == TCP_LISTEN
1717 )
1718 {
1719 return;
1720 }
1721 lock_sock(sk);
1722
1723 /*
1724 * flag that the sender has shutdown
1725 */
1726
1727 sk->shutdown |= SEND_SHUTDOWN;
1728
1729 /*
1730 * Clear out any half completed packets.
1731 */
1732
1733 if (sk->partial)
1734 tcp_send_partial(sk);
1735
1736 /*
1737 * FIN if needed
1738 */
1739
1740 if (tcp_close_state(sk,0))
1741 tcp_send_fin(sk);
1742
1743 release_sock(sk);
1744 }
1745
1746
1747 /*
1748 * Return 1 if we still have things to send in our buffers.
1749 */
1750
1751 static inline int closing(struct sock * sk)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
1752 {
1753 switch (sk->state) {
1754 case TCP_FIN_WAIT1:
1755 case TCP_CLOSING:
1756 case TCP_LAST_ACK:
1757 return 1;
1758 }
1759 return 0;
1760 }
1761
1762
1763 static void tcp_close(struct sock *sk, unsigned long timeout)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
1764 {
1765 struct sk_buff *skb;
1766
1767 /*
1768 * We need to grab some memory, and put together a FIN,
1769 * and then put it into the queue to be sent.
1770 */
1771
1772 lock_sock(sk);
1773
1774 tcp_cache_zap();
1775 if(sk->state == TCP_LISTEN)
1776 {
1777 /* Special case */
1778 tcp_set_state(sk, TCP_CLOSE);
1779 tcp_close_pending(sk);
1780 release_sock(sk);
1781 sk->dead = 1;
1782 return;
1783 }
1784
1785 sk->keepopen = 1;
1786 sk->shutdown = SHUTDOWN_MASK;
1787
1788 if (!sk->dead)
1789 sk->state_change(sk);
1790
1791 /*
1792 * We need to flush the recv. buffs. We do this only on the
1793 * descriptor close, not protocol-sourced closes, because the
1794 * reader process may not have drained the data yet!
1795 */
1796
1797 while((skb=skb_dequeue(&sk->receive_queue))!=NULL)
1798 kfree_skb(skb, FREE_READ);
1799
1800 /*
1801 * Get rid off any half-completed packets.
1802 */
1803
1804 if (sk->partial)
1805 tcp_send_partial(sk);
1806
1807 /*
1808 * Timeout is not the same thing - however the code likes
1809 * to send both the same way (sigh).
1810 */
1811
1812 if (tcp_close_state(sk,1)==1)
1813 {
1814 tcp_send_fin(sk);
1815 }
1816
1817 if (timeout) {
1818 cli();
1819 release_sock(sk);
1820 current->timeout = timeout;
1821 while(closing(sk) && current->timeout)
1822 {
1823 interruptible_sleep_on(sk->sleep);
1824 if (current->signal & ~current->blocked)
1825 {
1826 break;
1827 }
1828 }
1829 current->timeout=0;
1830 lock_sock(sk);
1831 sti();
1832 }
1833
1834 /*
1835 * This will destroy it. The timers will take care of actually
1836 * free'ing up the memory.
1837 */
1838 tcp_cache_zap(); /* Kill the cache again. */
1839 release_sock(sk);
1840 sk->dead = 1;
1841 }
1842
1843
1844 /*
1845 * This will accept the next outstanding connection.
1846 */
1847
1848 static struct sock *tcp_accept(struct sock *sk, int flags)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
1849 {
1850 struct sock *newsk;
1851 struct sk_buff *skb;
1852
1853 /*
1854 * We need to make sure that this socket is listening,
1855 * and that it has something pending.
1856 */
1857
1858 if (sk->state != TCP_LISTEN)
1859 {
1860 sk->err = EINVAL;
1861 return(NULL);
1862 }
1863
1864 /* Avoid the race. */
1865 cli();
1866 lock_sock(sk);
1867
1868 while((skb = tcp_dequeue_established(sk)) == NULL)
1869 {
1870 if (flags & O_NONBLOCK)
1871 {
1872 sti();
1873 release_sock(sk);
1874 sk->err = EAGAIN;
1875 return(NULL);
1876 }
1877
1878 release_sock(sk);
1879 interruptible_sleep_on(sk->sleep);
1880 if (current->signal & ~current->blocked)
1881 {
1882 sti();
1883 sk->err = ERESTARTSYS;
1884 return(NULL);
1885 }
1886 lock_sock(sk);
1887 }
1888 sti();
1889
1890 /*
1891 * Now all we need to do is return skb->sk.
1892 */
1893
1894 newsk = skb->sk;
1895
1896 kfree_skb(skb, FREE_READ);
1897 sk->ack_backlog--;
1898 release_sock(sk);
1899 return(newsk);
1900 }
1901
1902 /*
1903 * This will initiate an outgoing connection.
1904 */
1905
1906 static int tcp_connect(struct sock *sk, struct sockaddr_in *usin, int addr_len)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
1907 {
1908 struct sk_buff *buff;
1909 struct device *dev=NULL;
1910 unsigned char *ptr;
1911 int tmp;
1912 int atype;
1913 struct tcphdr *t1;
1914 struct rtable *rt;
1915
1916 if (sk->state != TCP_CLOSE)
1917 return(-EISCONN);
1918
1919 /*
1920 * Don't allow a double connect.
1921 */
1922
1923 if(sk->daddr)
1924 return -EINVAL;
1925
1926 if (addr_len < 8)
1927 return(-EINVAL);
1928
1929 if (usin->sin_family && usin->sin_family != AF_INET)
1930 return(-EAFNOSUPPORT);
1931
1932 /*
1933 * connect() to INADDR_ANY means loopback (BSD'ism).
1934 */
1935
1936 if(usin->sin_addr.s_addr==INADDR_ANY)
1937 usin->sin_addr.s_addr=ip_my_addr();
1938
1939 /*
1940 * Don't want a TCP connection going to a broadcast address
1941 */
1942
1943 if ((atype=ip_chk_addr(usin->sin_addr.s_addr)) == IS_BROADCAST || atype==IS_MULTICAST)
1944 return -ENETUNREACH;
1945
1946 lock_sock(sk);
1947 sk->daddr = usin->sin_addr.s_addr;
1948 sk->write_seq = tcp_init_seq();
1949 sk->window_seq = sk->write_seq;
1950 sk->rcv_ack_seq = sk->write_seq -1;
1951 sk->err = 0;
1952 sk->dummy_th.dest = usin->sin_port;
1953 release_sock(sk);
1954
1955 buff = sock_wmalloc(sk,MAX_SYN_SIZE,0, GFP_KERNEL);
1956 if (buff == NULL)
1957 {
1958 return(-ENOMEM);
1959 }
1960 lock_sock(sk);
1961 buff->sk = sk;
1962 buff->free = 0;
1963 buff->localroute = sk->localroute;
1964
1965
1966 /*
1967 * Put in the IP header and routing stuff.
1968 */
1969
1970 tmp = sk->prot->build_header(buff, sk->saddr, sk->daddr, &dev,
1971 IPPROTO_TCP, NULL, MAX_SYN_SIZE,sk->ip_tos,sk->ip_ttl,&sk->ip_route_cache);
1972 if (tmp < 0)
1973 {
1974 sock_wfree(sk, buff);
1975 release_sock(sk);
1976 return(-ENETUNREACH);
1977 }
1978 if ((rt = sk->ip_route_cache) != NULL && !sk->saddr)
1979 sk->saddr = rt->rt_src;
1980 sk->rcv_saddr = sk->saddr;
1981
1982 t1 = (struct tcphdr *) skb_put(buff,sizeof(struct tcphdr));
1983
1984 memcpy(t1,(void *)&(sk->dummy_th), sizeof(*t1));
1985 buff->seq = sk->write_seq++;
1986 t1->seq = htonl(buff->seq);
1987 sk->sent_seq = sk->write_seq;
1988 buff->end_seq = sk->write_seq;
1989 t1->ack = 0;
1990 t1->window = 2;
1991 t1->syn = 1;
1992 t1->doff = 6;
1993 /* use 512 or whatever user asked for */
1994
1995 if(rt!=NULL && (rt->rt_flags&RTF_WINDOW))
1996 sk->window_clamp=rt->rt_window;
1997 else
1998 sk->window_clamp=0;
1999
2000 if (sk->user_mss)
2001 sk->mtu = sk->user_mss;
2002 else if (rt)
2003 sk->mtu = rt->rt_mtu - sizeof(struct iphdr) - sizeof(struct tcphdr);
2004 else
2005 sk->mtu = 576 - sizeof(struct iphdr) - sizeof(struct tcphdr);
2006
2007 /*
2008 * but not bigger than device MTU
2009 */
2010
2011 if(sk->mtu <32)
2012 sk->mtu = 32; /* Sanity limit */
2013
2014 sk->mtu = min(sk->mtu, dev->mtu - sizeof(struct iphdr) - sizeof(struct tcphdr));
2015
2016 #ifdef CONFIG_SKIP
2017
2018 /*
2019 * SKIP devices set their MTU to 65535. This is so they can take packets
2020 * unfragmented to security process then fragment. They could lie to the
2021 * TCP layer about a suitable MTU, but its easier to let skip sort it out
2022 * simply because the final package we want unfragmented is going to be
2023 *
2024 * [IPHDR][IPSP][Security data][Modified TCP data][Security data]
2025 */
2026
2027 if(skip_pick_mtu!=NULL) /* If SKIP is loaded.. */
2028 sk->mtu=skip_pick_mtu(sk->mtu,dev);
2029 #endif
2030
2031 /*
2032 * Put in the TCP options to say MTU.
2033 */
2034
2035 ptr = skb_put(buff,4);
2036 ptr[0] = 2;
2037 ptr[1] = 4;
2038 ptr[2] = (sk->mtu) >> 8;
2039 ptr[3] = (sk->mtu) & 0xff;
2040 buff->csum = csum_partial(ptr, 4, 0);
2041 tcp_send_check(t1, sk->saddr, sk->daddr,
2042 sizeof(struct tcphdr) + 4, buff);
2043
2044 /*
2045 * This must go first otherwise a really quick response will get reset.
2046 */
2047
2048 tcp_cache_zap();
2049 tcp_set_state(sk,TCP_SYN_SENT);
2050 if(rt&&rt->rt_flags&RTF_IRTT)
2051 sk->rto = rt->rt_irtt;
2052 else
2053 sk->rto = TCP_TIMEOUT_INIT;
2054 sk->retransmit_timer.function=&tcp_retransmit_timer;
2055 sk->retransmit_timer.data = (unsigned long)sk;
2056 tcp_reset_xmit_timer(sk, TIME_WRITE, sk->rto); /* Timer for repeating the SYN until an answer */
2057 sk->retransmits = 0; /* Now works the right way instead of a hacked
2058 initial setting */
2059
2060 sk->prot->queue_xmit(sk, dev, buff, 0);
2061 tcp_reset_xmit_timer(sk, TIME_WRITE, sk->rto);
2062 tcp_statistics.TcpActiveOpens++;
2063 tcp_statistics.TcpOutSegs++;
2064
2065 release_sock(sk);
2066 return(0);
2067 }
2068
2069 /*
2070 * Socket option code for TCP.
2071 */
2072
2073 int tcp_setsockopt(struct sock *sk, int level, int optname, char *optval, int optlen)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
2074 {
2075 int val,err;
2076
2077 if(level!=SOL_TCP)
2078 return ip_setsockopt(sk,level,optname,optval,optlen);
2079
2080 if (optval == NULL)
2081 return(-EINVAL);
2082
2083 err=verify_area(VERIFY_READ, optval, sizeof(int));
2084 if(err)
2085 return err;
2086
2087 val = get_user((int *)optval);
2088
2089 switch(optname)
2090 {
2091 case TCP_MAXSEG:
2092 /*
2093 * values greater than interface MTU won't take effect. however at
2094 * the point when this call is done we typically don't yet know
2095 * which interface is going to be used
2096 */
2097 if(val<1||val>MAX_WINDOW)
2098 return -EINVAL;
2099 sk->user_mss=val;
2100 return 0;
2101 case TCP_NODELAY:
2102 sk->nonagle=(val==0)?0:1;
2103 return 0;
2104 default:
2105 return(-ENOPROTOOPT);
2106 }
2107 }
2108
2109 int tcp_getsockopt(struct sock *sk, int level, int optname, char *optval, int *optlen)
/* ![[previous]](../icons/left.png)
![[next]](../icons/n_right.png)
![[first]](../icons/first.png)
![[last]](../icons/n_last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
2110 {
2111 int val,err;
2112
2113 if(level!=SOL_TCP)
2114 return ip_getsockopt(sk,level,optname,optval,optlen);
2115
2116 switch(optname)
2117 {
2118 case TCP_MAXSEG:
2119 val=sk->user_mss;
2120 break;
2121 case TCP_NODELAY:
2122 val=sk->nonagle;
2123 break;
2124 default:
2125 return(-ENOPROTOOPT);
2126 }
2127 err=verify_area(VERIFY_WRITE, optlen, sizeof(int));
2128 if(err)
2129 return err;
2130 put_user(sizeof(int),(int *) optlen);
2131
2132 err=verify_area(VERIFY_WRITE, optval, sizeof(int));
2133 if(err)
2134 return err;
2135 put_user(val,(int *)optval);
2136
2137 return(0);
2138 }
2139
2140
2141 struct proto tcp_prot = {
2142 tcp_close,
2143 ip_build_header,
2144 tcp_connect,
2145 tcp_accept,
2146 ip_queue_xmit,
2147 tcp_retransmit,
2148 tcp_write_wakeup,
2149 tcp_read_wakeup,
2150 tcp_rcv,
2151 tcp_select,
2152 tcp_ioctl,
2153 NULL,
2154 tcp_shutdown,
2155 tcp_setsockopt,
2156 tcp_getsockopt,
2157 tcp_sendmsg,
2158 tcp_recvmsg,
2159 NULL, /* No special bind() */
2160 128,
2161 0,
2162 "TCP",
2163 0, 0,
2164 {NULL,}
2165 };