Import 2.1.13
[davej-history.git] / net / ipv4 / tcp_output.c
blob016cee54ceaa56d806bef141b2252d6743502b3b
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.
6 * Implementation of the Transmission Control Protocol(TCP).
8 * Version: @(#)tcp_input.c 1.0.16 05/25/93
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>
24 * Changes: Pedro Roque : Retransmit queue handled by TCP.
25 * : Fragmentation on mtu decrease
26 * : Segment collapse on retransmit
27 * : AF independence
29 * Linus Torvalds : send_delayed_ack
33 #include <net/tcp.h>
36 * Get rid of any delayed acks, we sent one already..
38 static __inline__ voidclear_delayed_acks(struct sock * sk)
40 sk->delayed_acks =0;
41 sk->ack_backlog =0;
42 sk->bytes_rcv =0;
43 tcp_clear_xmit_timer(sk, TIME_DACK);
46 static __inline__ voidupdate_send_head(struct sock *sk)
48 struct tcp_opt *tp = &sk->tp_pinfo.af_tcp;
50 tp->send_head = tp->send_head->next;
52 if(tp->send_head == (struct sk_buff *) &sk->write_queue)
54 tp->send_head = NULL;
59 static __inline__ inttcp_snd_test(struct sock *sk,struct sk_buff *skb)
61 struct tcp_opt *tp = &(sk->tp_pinfo.af_tcp);
62 int nagle_check =1;
63 int len;
66 * RFC 1122 - section 4.2.3.4
68 * We must queue if
70 * a) The right edge of this frame exceeds the window
71 * b) There are packets in flight and we have a small segment
72 * [SWS avoidance and Nagle algorithm]
73 * (part of SWS is done on packetization)
74 * c) We are retransmiting [Nagle]
75 * d) We have too many packets 'in flight'
78 len = skb->end_seq - skb->seq;
80 if(!sk->nonagle && len < (sk->mss >>1) && sk->packets_out)
82 nagle_check =0;
85 return(nagle_check && sk->packets_out < sk->cong_window &&
86 !after(skb->end_seq, tp->snd_una + tp->snd_wnd) &&
87 sk->retransmits ==0);
91 * This is the main buffer sending routine. We queue the buffer
92 * having checked it is sane seeming.
95 inttcp_send_skb(struct sock *sk,struct sk_buff *skb)
97 struct tcphdr * th = skb->h.th;
98 struct tcp_opt *tp = &(sk->tp_pinfo.af_tcp);
99 int size;
102 * length of packet (not counting length of pre-tcp headers)
105 size = skb->len - ((unsigned char*) th - skb->data);
108 * Sanity check it..
111 if(size <sizeof(struct tcphdr) || size > skb->len)
113 printk("tcp_send_skb: bad skb (skb = %p, data = %p, th = %p, len = %u)\n",
114 skb, skb->data, th, skb->len);
115 kfree_skb(skb, FREE_WRITE);
116 return0;
120 * If we have queued a header size packet.. (these crash a few
121 * tcp stacks if ack is not set)
124 if(size ==sizeof(struct tcphdr))
127 * If it's got a syn or fin discard
129 if(!th->syn && !th->fin)
131 printk("tcp_send_skb: attempt to queue a bogon.\n");
132 kfree_skb(skb,FREE_WRITE);
133 return0;
139 * Actual processing.
142 tcp_statistics.TcpOutSegs++;
143 skb->seq =ntohl(th->seq);
144 skb->end_seq = skb->seq + size -4*th->doff;
147 if(tp->send_head || !tcp_snd_test(sk, skb))
150 * Remember where we must start sending
153 if(tp->send_head == NULL)
154 tp->send_head = skb;
156 skb_queue_tail(&sk->write_queue, skb);
158 if(sk->packets_out ==0&& !tp->pending)
160 tp->pending = TIME_PROBE0;
161 tcp_reset_xmit_timer(sk, TIME_PROBE0, tp->rto);
165 else
167 struct sk_buff * buff;
170 * This is going straight out
173 skb_queue_tail(&sk->write_queue, skb);
175 clear_delayed_acks(sk);
177 th->ack_seq =htonl(tp->rcv_nxt);
178 th->window =htons(tcp_select_window(sk));
180 tp->af_specific->send_check(sk, th, size, skb);
182 tp->snd_nxt = skb->end_seq;
184 atomic_inc(&sk->packets_out);
186 skb->when = jiffies;
188 buff =skb_clone(skb, GFP_ATOMIC);
189 atomic_add(buff->truesize, &sk->wmem_alloc);
191 tp->af_specific->queue_xmit(sk, skb->dev, buff,1);
193 if(!tcp_timer_is_set(sk, TIME_RETRANS))
194 tcp_reset_xmit_timer(sk, TIME_RETRANS, tp->rto);
197 return0;
201 * Function to create two new tcp segments.
202 * Shrinks the given segment to the specified size and appends a new
203 * segment with the rest of the packet to the list.
204 * This won't be called frenquently, I hope...
207 static inttcp_fragment(struct sock *sk,struct sk_buff *skb, u32 len)
209 struct tcp_opt *tp = &(sk->tp_pinfo.af_tcp);
210 struct sk_buff *buff;
211 struct tcphdr *th, *nth;
212 int nsize;
213 int tmp;
215 th = skb->h.th;
217 /* size of new segment */
218 nsize = skb->tail - ((unsigned char*) (th +1)) - len;
220 if(nsize <=0)
222 printk(KERN_DEBUG "tcp_fragment: bug size <= 0\n");
223 return-1;
227 * Get a new skb... force flag on
229 buff =sock_wmalloc(sk, nsize +128+ sk->prot->max_header +15,1,
230 GFP_ATOMIC);
232 if(buff == NULL)
233 return-1;
235 buff->sk = sk;
236 buff->localroute = sk->localroute;
239 * Put headers on the new packet
242 tmp = tp->af_specific->build_net_header(sk, buff);
244 if(tmp <0)
246 sock_wfree(sk, buff);
247 return-1;
251 * Move the TCP header over
254 nth = (struct tcphdr *)skb_put(buff,sizeof(*th));
256 buff->h.th = nth;
258 memcpy(nth, th,sizeof(*th));
261 * Correct the new header
264 buff->seq = skb->seq + len;
265 buff->end_seq = skb->end_seq;
266 nth->seq =htonl(buff->seq);
267 nth->check =0;
268 nth->doff =5;
270 /* urg data is always an headache */
271 if(th->urg)
273 if(th->urg_ptr > len)
275 th->urg =0;
276 nth->urg_ptr -= len;
278 else
280 nth->urg =0;
285 * Copy TCP options and data start to our new buffer
288 buff->csum =csum_partial_copy(((u8 *)(th +1)) + len,
289 skb_put(buff, nsize),
290 nsize,0);
293 skb->end_seq -= nsize;
295 skb_trim(skb, skb->len - nsize);
297 /* remember to checksum this packet afterwards */
298 th->check =0;
299 skb->csum =csum_partial((u8*) (th +1), skb->tail - ((u8 *) (th +1)),
302 skb_append(skb, buff);
304 return0;
307 static voidtcp_wrxmit_prob(struct sock *sk,struct sk_buff *skb)
310 * This is acked data. We can discard it. This
311 * cannot currently occur.
314 sk->retransmits =0;
316 printk(KERN_DEBUG "tcp_write_xmit: bug skb in write queue\n");
318 update_send_head(sk);
320 skb_unlink(skb);
321 skb->sk = NULL;
322 skb->free =1;
323 kfree_skb(skb, FREE_WRITE);
325 if(!sk->dead)
326 sk->write_space(sk);
329 static inttcp_wrxmit_frag(struct sock *sk,struct sk_buff *skb,int size)
331 struct tcp_opt *tp = &sk->tp_pinfo.af_tcp;
333 printk(KERN_DEBUG "tcp_write_xmit: frag needed size=%d mss=%d\n",
334 size, sk->mss);
336 if(tcp_fragment(sk, skb, sk->mss))
338 /* !tcp_frament Failed! */
339 tp->send_head = skb;
340 atomic_dec(&sk->packets_out);
341 return-1;
343 else
346 * If tcp_fragment succeded then
347 * the send head is the resulting
348 * fragment
350 tp->send_head = skb->next;
352 return0;
356 * This routine writes packets to the network.
357 * It advances the send_head.
358 * This happens as incoming acks open up the remote window for us.
361 voidtcp_write_xmit(struct sock *sk)
363 struct sk_buff *skb;
364 struct tcp_opt *tp = &sk->tp_pinfo.af_tcp;
365 u16 rcv_wnd;
366 int sent_pkts =0;
369 * The bytes will have to remain here. In time closedown will
370 * empty the write queue and all will be happy
373 if(sk->zapped)
374 return;
377 * Anything on the transmit queue that fits the window can
378 * be added providing we are:
380 * a) following SWS avoidance [and Nagle algorithm]
381 * b) not exceeding our congestion window.
382 * c) not retransmiting [Nagle]
385 start_bh_atomic();
387 rcv_wnd =htons(tcp_select_window(sk));
389 while((skb = tp->send_head) &&tcp_snd_test(sk, skb))
391 struct tcphdr *th;
392 struct sk_buff *buff;
393 int size;
395 IS_SKB(skb);
398 * See if we really need to send the packet.
401 if(!after(skb->end_seq, tp->snd_una))
403 tcp_wrxmit_prob(sk, skb);
404 continue;
409 * Advance the send_head
410 * This one is going out.
413 update_send_head(sk);
415 atomic_inc(&sk->packets_out);
419 * put in the ack seq and window at this point rather than earlier,
420 * in order to keep them monotonic. We really want to avoid taking
421 * back window allocations. That's legal, but RFC1122 says it's frowned on.
422 * Ack and window will in general have changed since this packet was put
423 * on the write queue.
426 th = skb->h.th;
427 size = skb->len - (((unsigned char*) th) - skb->data);
429 if(size - (th->doff <<2) > sk->mss)
431 if(tcp_wrxmit_frag(sk, skb, size))
432 break;
435 th->ack_seq =htonl(tp->rcv_nxt);
436 th->window = rcv_wnd;
438 tp->af_specific->send_check(sk, th, size, skb);
440 #ifdef TCP_DEBUG
441 if(before(skb->end_seq, tp->snd_nxt))
442 printk(KERN_DEBUG "tcp_write_xmit:"
443 " sending already sent seq\n");
444 #endif
446 tp->snd_nxt = skb->end_seq;
448 skb->when = jiffies;
449 clear_delayed_acks(sk);
451 buff =skb_clone(skb, GFP_ATOMIC);
452 atomic_add(buff->truesize, &sk->wmem_alloc);
454 sent_pkts =1;
455 tp->af_specific->queue_xmit(sk, skb->dev, buff,1);
459 if(sent_pkts && !tcp_timer_is_set(sk, TIME_RETRANS))
461 tcp_reset_xmit_timer(sk, TIME_RETRANS, tp->rto);
464 end_bh_atomic();
470 * This function returns the amount that we can raise the
471 * usable window based on the following constraints
473 * 1. The window can never be shrunk once it is offered (RFC 793)
474 * 2. We limit memory per socket
478 unsigned shorttcp_select_window(struct sock *sk)
480 struct tcp_opt *tp = &sk->tp_pinfo.af_tcp;
481 int mss = sk->mss;
482 long free_space =sock_rspace(sk);
483 long window;
484 long cur_win;
485 long usable;
488 if(sk->window_clamp)
490 free_space =min(sk->window_clamp, free_space);
491 mss =min(sk->window_clamp, mss);
495 * compute the actual window i.e.
496 * old_window - received_bytes_on_that_win
499 cur_win = tp->rcv_wup - (tp->rcv_nxt - tp->rcv_wnd);
500 window = tp->rcv_wnd;
502 if( cur_win <0)
504 cur_win =0;
505 printk(KERN_DEBUG "TSW: win < 0 w=%d 1=%u 2=%u\n",
506 tp->rcv_wnd, tp->rcv_nxt, tp->rcv_wup);
510 * RFC 1122:
511 * "the suggested [SWS] avoidance algoritm for the receiver is to keep
512 * RECV.NEXT + RCV.WIN fixed until:
513 * RCV.BUFF - RCV.USER - RCV.WINDOW >= min(1/2 RCV.BUFF, MSS)"
515 * i.e. don't raise the right edge of the window until you can't raise
516 * it MSS bytes
520 * It would be a good idea if it didn't break header prediction.
521 * and BSD made the header predition standard...
522 * It expects the same value in the header i.e. th->window to be
523 * constant
526 usable = free_space - cur_win;
527 if(usable <0)
529 usable =0;
532 if( window < usable )
535 * Window is not blocking the sender
536 * and we have enought free space for it
539 if(cur_win > (sk->mss <<1))
540 goto out;
544 if(window >= usable)
547 * We are offering too much, cut it down...
548 * but don't shrink the window
551 window =max(usable, cur_win);
553 else
555 if((usable - window) >= mss)
557 window += mss;
561 out:
562 tp->rcv_wnd = window;
563 tp->rcv_wup = tp->rcv_nxt;
564 return window;
567 static inttcp_retrans_try_collapse(struct sock *sk,struct sk_buff *skb)
569 struct tcphdr *th1, *th2;
570 int size1, size2, avail;
571 struct sk_buff *buff = skb->next;
573 th1 = skb->h.th;
575 if(th1->urg)
576 return-1;
578 avail =skb_tailroom(skb);
581 * size of tcp payload
584 size1 = skb->tail - (u8 *) (th1 +1);
586 th2 = buff->h.th;
588 size2 = buff->tail - (u8 *) (th2 +1);
590 if(size2 > avail || size1 + size2 > sk->mss )
591 return-1;
594 * ok. we will be able to collapse the packet
597 skb_unlink(buff);
599 memcpy(skb_put(skb, size2), ((char*) th2) + (th2->doff <<2), size2);
602 * update sizes on original skb. both TCP and IP
605 skb->end_seq += size2;
607 if(th2->urg)
609 th1->urg =1;
610 th1->urg_ptr = th2->urg_ptr + size1;
614 * ... and off you go.
617 buff->free =1;
618 kfree_skb(buff, FREE_WRITE);
619 atomic_dec(&sk->packets_out);
622 * Header checksum will be set by the retransmit procedure
623 * after calling rebuild header
626 th1->check =0;
627 skb->csum =csum_partial((u8*) (th1+1), size1 + size2,0);
629 return0;
634 * A socket has timed out on its send queue and wants to do a
635 * little retransmitting.
636 * retransmit_head can be different from the head of the write_queue
637 * if we are doing fast retransmit.
640 voidtcp_do_retransmit(struct sock *sk,int all)
642 struct sk_buff * skb;
643 int ct=0;
644 struct tcp_opt *tp = &(sk->tp_pinfo.af_tcp);
646 start_bh_atomic();
648 if(tp->retrans_head == NULL)
649 tp->retrans_head =skb_peek(&sk->write_queue);
651 if(tp->retrans_head == tp->send_head)
652 tp->retrans_head = NULL;
654 while((skb = tp->retrans_head) != NULL)
656 struct sk_buff *buff;
657 struct tcphdr *th;
658 int tcp_size;
659 int size;
661 IS_SKB(skb);
664 * In general it's OK just to use the old packet. However we
665 * need to use the current ack and window fields. Urg and
666 * urg_ptr could possibly stand to be updated as well, but we
667 * don't keep the necessary data. That shouldn't be a problem,
668 * if the other end is doing the right thing. Since we're
669 * changing the packet, we have to issue a new IP identifier.
672 th = skb->h.th;
674 tcp_size = skb->tail - ((unsigned char*) (th +1));
676 if(tcp_size > sk->mss)
678 if(tcp_fragment(sk, skb, sk->mss))
680 printk(KERN_DEBUG "tcp_fragment failed\n");
681 return;
683 atomic_inc(&sk->packets_out);
686 if(!th->syn &&
687 tcp_size < (sk->mss >>1) &&
688 skb->next != tp->send_head &&
689 skb->next != (struct sk_buff *)&sk->write_queue)
691 tcp_retrans_try_collapse(sk, skb);
694 if(tp->af_specific->rebuild_header(sk, skb))
696 #ifdef TCP_DEBUG
697 printk(KERN_DEBUG "tcp_do_rebuild_header failed\n");
698 #endif
699 break;
702 if(sk->debug)
703 printk("retransmit sending\n");
706 * update ack and window
709 th->ack_seq =htonl(tp->rcv_nxt);
710 th->window =ntohs(tcp_select_window(sk));
712 size = skb->tail - (unsigned char*) th;
713 tp->af_specific->send_check(sk, th, size, skb);
715 skb->when = jiffies;
716 buff =skb_clone(skb, GFP_ATOMIC);
717 atomic_add(buff->truesize, &sk->wmem_alloc);
719 clear_delayed_acks(sk);
721 tp->af_specific->queue_xmit(sk, skb->dev, buff,1);
724 * Count retransmissions
727 ct++;
728 sk->prot->retransmits ++;
729 tcp_statistics.TcpRetransSegs++;
732 * Record the high sequence number to help avoid doing
733 * to much fast retransmission.
736 if(sk->retransmits)
737 tp->high_seq = tp->snd_nxt;
740 * Only one retransmit requested.
743 if(!all)
744 break;
747 * This should cut it off before we send too many packets.
750 if(ct >= sk->cong_window)
751 break;
754 * Advance the pointer
757 tp->retrans_head = skb->next;
758 if((tp->retrans_head == tp->send_head) ||
759 (tp->retrans_head == (struct sk_buff *) &sk->write_queue))
761 tp->retrans_head = NULL;
765 end_bh_atomic();
769 * Send a fin.
772 voidtcp_send_fin(struct sock *sk)
774 struct tcphdr *th =(struct tcphdr *)&sk->dummy_th;
775 struct tcp_opt *tp = &(sk->tp_pinfo.af_tcp);
776 struct tcphdr *t1;
777 struct sk_buff *buff;
778 int tmp;
781 buff =sock_wmalloc(sk, MAX_RESET_SIZE,1, GFP_KERNEL);
783 if(buff == NULL)
785 /* This is a disaster if it occurs */
786 printk("tcp_send_fin: Impossible malloc failure");
787 return;
791 * Administrivia
794 buff->sk = sk;
795 buff->localroute = sk->localroute;
796 buff->csum =0;
799 * Put in the IP header and routing stuff.
802 tmp = tp->af_specific->build_net_header(sk, buff);
804 if(tmp <0)
806 int t;
808 * Finish anyway, treat this as a send that got lost.
809 * (Not good).
812 buff->free =1;
813 sock_wfree(sk,buff);
814 sk->write_seq++;
815 t=del_timer(&sk->timer);
816 if(t)
817 add_timer(&sk->timer);
818 else
819 tcp_reset_msl_timer(sk, TIME_CLOSE, TCP_TIMEWAIT_LEN);
820 return;
824 * We ought to check if the end of the queue is a buffer and
825 * if so simply add the fin to that buffer, not send it ahead.
828 t1 =(struct tcphdr *)skb_put(buff,sizeof(struct tcphdr));
829 buff->h.th = t1;
831 memcpy(t1, th,sizeof(*t1));
832 buff->seq = sk->write_seq;
833 sk->write_seq++;
834 buff->end_seq = sk->write_seq;
835 t1->seq =htonl(buff->seq);
836 t1->ack_seq =htonl(tp->rcv_nxt);
837 t1->window =htons(tcp_select_window(sk));
838 t1->fin =1;
840 tp->af_specific->send_check(sk, t1,sizeof(*t1), buff);
843 * The fin can only be transmited after the data.
846 skb_queue_tail(&sk->write_queue, buff);
848 if(tp->send_head == NULL)
850 struct sk_buff *skb1;
852 atomic_inc(&sk->packets_out);
853 tp->snd_nxt = sk->write_seq;
854 buff->when = jiffies;
856 skb1 =skb_clone(buff, GFP_KERNEL);
857 atomic_add(skb1->truesize, &sk->wmem_alloc);
859 tp->af_specific->queue_xmit(sk, skb1->dev, skb1,1);
861 if(!tcp_timer_is_set(sk, TIME_RETRANS))
862 tcp_reset_xmit_timer(sk, TIME_RETRANS, tp->rto);
866 inttcp_send_synack(struct sock *sk)
868 struct tcp_opt * tp = &(sk->tp_pinfo.af_tcp);
869 struct sk_buff * skb;
870 struct sk_buff * buff;
871 struct tcphdr *th;
872 unsigned char*ptr;
873 int tmp;
875 skb =sock_wmalloc(sk, MAX_SYN_SIZE,1, GFP_ATOMIC);
877 if(skb == NULL)
879 return-ENOMEM;
882 skb->sk = sk;
883 skb->localroute = sk->localroute;
885 tmp = tp->af_specific->build_net_header(sk, skb);
887 if(tmp <0)
889 skb->free =1;
890 kfree_skb(skb, FREE_WRITE);
891 return tmp;
894 th =(struct tcphdr *)skb_put(skb,sizeof(struct tcphdr));
895 skb->h.th = th;
896 memset(th,0,sizeof(struct tcphdr));
898 th->syn =1;
899 th->ack =1;
901 th->source = sk->dummy_th.source;
902 th->dest = sk->dummy_th.dest;
904 skb->seq = tp->snd_una;
905 skb->end_seq = skb->seq +1/* th->syn */;
906 th->seq =ntohl(skb->seq);
908 th->window =ntohs(tp->rcv_wnd);
910 th->ack_seq =htonl(tp->rcv_nxt);
911 th->doff =sizeof(*th)/4+1;
913 ptr =skb_put(skb, TCPOLEN_MSS);
914 ptr[0] = TCPOPT_MSS;
915 ptr[1] = TCPOLEN_MSS;
916 ptr[2] = ((sk->mss) >>8) &0xff;
917 ptr[3] = (sk->mss) &0xff;
918 skb->csum =csum_partial(ptr, TCPOLEN_MSS,0);
920 tp->af_specific->send_check(sk, th,sizeof(*th)+4, skb);
922 skb_queue_tail(&sk->write_queue, skb);
924 atomic_inc(&sk->packets_out);
926 skb->when = jiffies;
927 buff =skb_clone(skb, GFP_ATOMIC);
929 atomic_add(skb->truesize, &sk->wmem_alloc);
931 tp->af_specific->queue_xmit(sk, skb->dev, buff,1);
933 tcp_reset_xmit_timer(sk, TIME_RETRANS, TCP_TIMEOUT_INIT);
935 tcp_statistics.TcpOutSegs++;
937 return0;
941 * Set up the timers for sending a delayed ack..
943 * rules for delaying an ack:
944 * - delay time <= 0.5 HZ
945 * - must send at least every 2 full sized packets
946 * - we don't have a window update to send
949 voidtcp_send_delayed_ack(struct sock * sk,int max_timeout)
951 struct tcp_opt *tp = &sk->tp_pinfo.af_tcp;
952 unsigned long timeout, now;
954 /* Calculate new timeout */
955 now = jiffies;
956 timeout = tp->ato;
958 if(timeout > max_timeout || sk->bytes_rcv > (sk->mss <<2))
960 timeout = now;
962 else
963 timeout += now;
965 /* Use new timeout only if there wasn't a older one earlier */
966 if(!del_timer(&tp->delack_timer) || timeout < tp->delack_timer.expires)
968 tp->delack_timer.expires = timeout;
971 add_timer(&tp->delack_timer);
977 * This routine sends an ack and also updates the window.
980 voidtcp_send_ack(struct sock *sk)
982 struct sk_buff *buff;
983 struct tcp_opt *tp=&(sk->tp_pinfo.af_tcp);
984 struct tcphdr *th;
985 int tmp;
988 if(sk->zapped)
990 /* We have been reset, we may not send again */
991 return;
995 * We need to grab some memory, and put together an ack,
996 * and then put it into the queue to be sent.
999 buff =sock_wmalloc(sk, MAX_ACK_SIZE,1, GFP_ATOMIC);
1000 if(buff == NULL)
1003 * Force it to send an ack. We don't have to do this
1004 * (ACK is unreliable) but it's much better use of
1005 * bandwidth on slow links to send a spare ack than
1006 * resend packets.
1009 tcp_send_delayed_ack(sk, HZ/2);
1010 return;
1013 clear_delayed_acks(sk);
1016 * Assemble a suitable TCP frame
1019 buff->sk = sk;
1020 buff->localroute = sk->localroute;
1021 buff->csum =0;
1024 * Put in the IP header and routing stuff.
1027 tmp = tp->af_specific->build_net_header(sk, buff);
1029 if(tmp <0)
1031 buff->free =1;
1032 sock_wfree(sk, buff);
1033 return;
1036 th =(struct tcphdr *)skb_put(buff,sizeof(struct tcphdr));
1038 memcpy(th, &sk->dummy_th,sizeof(struct tcphdr));
1041 * Swap the send and the receive.
1044 th->window =ntohs(tcp_select_window(sk));
1045 th->seq =ntohl(tp->snd_nxt);
1046 th->ack_seq =ntohl(tp->rcv_nxt);
1049 * Fill in the packet and send it
1052 tp->af_specific->send_check(sk, th,sizeof(struct tcphdr), buff);
1054 if(sk->debug)
1055 printk("\rtcp_send_ack: seq %x ack %x\n",
1056 tp->snd_nxt, tp->rcv_nxt);
1058 tp->af_specific->queue_xmit(sk, buff->dev, buff,1);
1060 tcp_statistics.TcpOutSegs++;
1064 * This routine sends a packet with an out of date sequence
1065 * number. It assumes the other end will try to ack it.
1068 voidtcp_write_wakeup(struct sock *sk)
1070 struct sk_buff *buff, *skb;
1071 struct tcphdr *t1;
1072 struct tcp_opt *tp = &(sk->tp_pinfo.af_tcp);
1073 int tmp;
1075 if(sk->zapped)
1076 return;/* After a valid reset we can send no more */
1079 * Write data can still be transmitted/retransmitted in the
1080 * following states. If any other state is encountered, return.
1081 * [listen/close will never occur here anyway]
1084 if(sk->state != TCP_ESTABLISHED &&
1085 sk->state != TCP_CLOSE_WAIT &&
1086 sk->state != TCP_FIN_WAIT1 &&
1087 sk->state != TCP_LAST_ACK &&
1088 sk->state != TCP_CLOSING
1091 return;
1094 if(before(tp->snd_nxt, tp->snd_una + tp->snd_wnd) &&
1095 (skb=tp->send_head))
1098 * We are probing the opening of a window
1099 * but the window size is != 0
1100 * must have been a result SWS avoidance ( sender )
1103 struct tcphdr *th;
1104 unsigned long win_size;
1106 win_size = tp->snd_wnd - (tp->snd_nxt - tp->snd_una);
1108 if(win_size < skb->end_seq - skb->seq)
1110 if(tcp_fragment(sk, skb, win_size))
1112 printk(KERN_DEBUG "tcp_write_wakeup: "
1113 "fragment failed\n");
1114 return;
1119 th = skb->h.th;
1121 tp->af_specific->send_check(sk, th, th->doff *4+ win_size,
1122 skb);
1124 buff =skb_clone(skb, GFP_ATOMIC);
1126 atomic_add(buff->truesize, &sk->wmem_alloc);
1127 atomic_inc(&sk->packets_out);
1129 clear_delayed_acks(sk);
1131 if(!tcp_timer_is_set(sk, TIME_RETRANS))
1132 tcp_reset_xmit_timer(sk, TIME_RETRANS, tp->rto);
1134 skb->when = jiffies;
1136 update_send_head(sk);
1138 tp->snd_nxt = skb->end_seq;
1140 else
1142 buff =sock_wmalloc(sk,MAX_ACK_SIZE,1, GFP_ATOMIC);
1143 if(buff == NULL)
1144 return;
1146 buff->free =1;
1147 buff->sk = sk;
1148 buff->localroute = sk->localroute;
1149 buff->csum =0;
1152 * Put in the IP header and routing stuff.
1155 tmp = tp->af_specific->build_net_header(sk, buff);
1157 if(tmp <0)
1159 sock_wfree(sk, buff);
1160 return;
1163 t1 = (struct tcphdr *)skb_put(buff,sizeof(struct tcphdr));
1164 memcpy(t1,(void*) &sk->dummy_th,sizeof(*t1));
1167 * Use a previous sequence.
1168 * This should cause the other end to send an ack.
1171 t1->seq =htonl(tp->snd_nxt-1);
1172 /* t1->fin = 0; -- We are sending a 'previous' sequence, and 0 bytes of data - thus no FIN bit */
1173 t1->ack_seq =htonl(tp->rcv_nxt);
1174 t1->window =htons(tcp_select_window(sk));
1176 tp->af_specific->send_check(sk, t1,sizeof(*t1), buff);
1180 * Send it.
1183 tp->af_specific->queue_xmit(sk, buff->dev, buff,1);
1184 tcp_statistics.TcpOutSegs++;
1188 * A window probe timeout has occurred.
1189 * If window is not closed send a partial packet
1190 * else a zero probe.
1193 voidtcp_send_probe0(struct sock *sk)
1195 struct tcp_opt *tp = &(sk->tp_pinfo.af_tcp);
1197 if(sk->zapped)
1198 return;/* After a valid reset we can send no more */
1201 tcp_write_wakeup(sk);
1203 tp->pending = TIME_PROBE0;
1205 tp->backoff++;
1206 tp->probes_out++;
1208 tcp_reset_xmit_timer(sk, TIME_PROBE0,
1209 min(tp->rto << tp->backoff,120*HZ));
close