tcp_output.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:42k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  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: $Id: tcp_output.c,v 1.144 2001/11/06 22:21:08 davem Exp $
  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. /*
  23.  * Changes: Pedro Roque : Retransmit queue handled by TCP.
  24.  * : Fragmentation on mtu decrease
  25.  * : Segment collapse on retransmit
  26.  * : AF independence
  27.  *
  28.  * Linus Torvalds : send_delayed_ack
  29.  * David S. Miller : Charge memory using the right skb
  30.  * during syn/ack processing.
  31.  * David S. Miller : Output engine completely rewritten.
  32.  * Andrea Arcangeli: SYNACK carry ts_recent in tsecr.
  33.  * Cacophonix Gaul : draft-minshall-nagle-01
  34.  * J Hadi Salim : ECN support
  35.  *
  36.  */
  37. #include <net/tcp.h>
  38. #include <linux/compiler.h>
  39. #include <linux/smp_lock.h>
  40. /* People can turn this off for buggy TCP's found in printers etc. */
  41. int sysctl_tcp_retrans_collapse = 1;
  42. static __inline__
  43. void update_send_head(struct sock *sk, struct tcp_opt *tp, struct sk_buff *skb)
  44. {
  45. tp->send_head = skb->next;
  46. if (tp->send_head == (struct sk_buff *) &sk->write_queue)
  47. tp->send_head = NULL;
  48. tp->snd_nxt = TCP_SKB_CB(skb)->end_seq;
  49. if (tp->packets_out++ == 0)
  50. tcp_reset_xmit_timer(sk, TCP_TIME_RETRANS, tp->rto);
  51. }
  52. /* SND.NXT, if window was not shrunk.
  53.  * If window has been shrunk, what should we make? It is not clear at all.
  54.  * Using SND.UNA we will fail to open window, SND.NXT is out of window. :-(
  55.  * Anything in between SND.UNA...SND.UNA+SND.WND also can be already
  56.  * invalid. OK, let's make this for now:
  57.  */
  58. static __inline__ __u32 tcp_acceptable_seq(struct sock *sk, struct tcp_opt *tp)
  59. {
  60. if (!before(tp->snd_una+tp->snd_wnd, tp->snd_nxt))
  61. return tp->snd_nxt;
  62. else
  63. return tp->snd_una+tp->snd_wnd;
  64. }
  65. /* Calculate mss to advertise in SYN segment.
  66.  * RFC1122, RFC1063, draft-ietf-tcpimpl-pmtud-01 state that:
  67.  *
  68.  * 1. It is independent of path mtu.
  69.  * 2. Ideally, it is maximal possible segment size i.e. 65535-40.
  70.  * 3. For IPv4 it is reasonable to calculate it from maximal MTU of
  71.  *    attached devices, because some buggy hosts are confused by
  72.  *    large MSS.
  73.  * 4. We do not make 3, we advertise MSS, calculated from first
  74.  *    hop device mtu, but allow to raise it to ip_rt_min_advmss.
  75.  *    This may be overriden via information stored in routing table.
  76.  * 5. Value 65535 for MSS is valid in IPv6 and means "as large as possible,
  77.  *    probably even Jumbo".
  78.  */
  79. static __u16 tcp_advertise_mss(struct sock *sk)
  80. {
  81. struct tcp_opt *tp = &(sk->tp_pinfo.af_tcp);
  82. struct dst_entry *dst = __sk_dst_get(sk);
  83. int mss = tp->advmss;
  84. if (dst && dst->advmss < mss) {
  85. mss = dst->advmss;
  86. tp->advmss = mss;
  87. }
  88. return (__u16)mss;
  89. }
  90. /* RFC2861. Reset CWND after idle period longer RTO to "restart window".
  91.  * This is the first part of cwnd validation mechanism. */
  92. static void tcp_cwnd_restart(struct tcp_opt *tp)
  93. {
  94. s32 delta = tcp_time_stamp - tp->lsndtime;
  95. u32 restart_cwnd = tcp_init_cwnd(tp);
  96. u32 cwnd = tp->snd_cwnd;
  97. tp->snd_ssthresh = tcp_current_ssthresh(tp);
  98. restart_cwnd = min(restart_cwnd, cwnd);
  99. while ((delta -= tp->rto) > 0 && cwnd > restart_cwnd)
  100. cwnd >>= 1;
  101. tp->snd_cwnd = max(cwnd, restart_cwnd);
  102. tp->snd_cwnd_stamp = tcp_time_stamp;
  103. tp->snd_cwnd_used = 0;
  104. }
  105. static __inline__ void tcp_event_data_sent(struct tcp_opt *tp, struct sk_buff *skb)
  106. {
  107. u32 now = tcp_time_stamp;
  108. if (!tp->packets_out && (s32)(now - tp->lsndtime) > tp->rto)
  109. tcp_cwnd_restart(tp);
  110. tp->lsndtime = now;
  111. /* If it is a reply for ato after last received
  112.  * packet, enter pingpong mode.
  113.  */
  114. if ((u32)(now - tp->ack.lrcvtime) < tp->ack.ato)
  115. tp->ack.pingpong = 1;
  116. }
  117. static __inline__ void tcp_event_ack_sent(struct sock *sk)
  118. {
  119. struct tcp_opt *tp = &(sk->tp_pinfo.af_tcp);
  120. tcp_dec_quickack_mode(tp);
  121. tcp_clear_xmit_timer(sk, TCP_TIME_DACK);
  122. }
  123. /* Chose a new window to advertise, update state in tcp_opt for the
  124.  * socket, and return result with RFC1323 scaling applied.  The return
  125.  * value can be stuffed directly into th->window for an outgoing
  126.  * frame.
  127.  */
  128. static __inline__ u16 tcp_select_window(struct sock *sk)
  129. {
  130. struct tcp_opt *tp = &(sk->tp_pinfo.af_tcp);
  131. u32 cur_win = tcp_receive_window(tp);
  132. u32 new_win = __tcp_select_window(sk);
  133. /* Never shrink the offered window */
  134. if(new_win < cur_win) {
  135. /* Danger Will Robinson!
  136.  * Don't update rcv_wup/rcv_wnd here or else
  137.  * we will not be able to advertise a zero
  138.  * window in time.  --DaveM
  139.  *
  140.  * Relax Will Robinson.
  141.  */
  142. new_win = cur_win;
  143. }
  144. tp->rcv_wnd = new_win;
  145. tp->rcv_wup = tp->rcv_nxt;
  146. /* RFC1323 scaling applied */
  147. new_win >>= tp->rcv_wscale;
  148. /* If we advertise zero window, disable fast path. */
  149. if (new_win == 0)
  150. tp->pred_flags = 0;
  151. return new_win;
  152. }
  153. /* This routine actually transmits TCP packets queued in by
  154.  * tcp_do_sendmsg().  This is used by both the initial
  155.  * transmission and possible later retransmissions.
  156.  * All SKB's seen here are completely headerless.  It is our
  157.  * job to build the TCP header, and pass the packet down to
  158.  * IP so it can do the same plus pass the packet off to the
  159.  * device.
  160.  *
  161.  * We are working here with either a clone of the original
  162.  * SKB, or a fresh unique copy made by the retransmit engine.
  163.  */
  164. int tcp_transmit_skb(struct sock *sk, struct sk_buff *skb)
  165. {
  166. if(skb != NULL) {
  167. struct tcp_opt *tp = &(sk->tp_pinfo.af_tcp);
  168. struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
  169. int tcp_header_size = tp->tcp_header_len;
  170. struct tcphdr *th;
  171. int sysctl_flags;
  172. int err;
  173. #define SYSCTL_FLAG_TSTAMPS 0x1
  174. #define SYSCTL_FLAG_WSCALE 0x2
  175. #define SYSCTL_FLAG_SACK 0x4
  176. sysctl_flags = 0;
  177. if (tcb->flags & TCPCB_FLAG_SYN) {
  178. tcp_header_size = sizeof(struct tcphdr) + TCPOLEN_MSS;
  179. if(sysctl_tcp_timestamps) {
  180. tcp_header_size += TCPOLEN_TSTAMP_ALIGNED;
  181. sysctl_flags |= SYSCTL_FLAG_TSTAMPS;
  182. }
  183. if(sysctl_tcp_window_scaling) {
  184. tcp_header_size += TCPOLEN_WSCALE_ALIGNED;
  185. sysctl_flags |= SYSCTL_FLAG_WSCALE;
  186. }
  187. if(sysctl_tcp_sack) {
  188. sysctl_flags |= SYSCTL_FLAG_SACK;
  189. if(!(sysctl_flags & SYSCTL_FLAG_TSTAMPS))
  190. tcp_header_size += TCPOLEN_SACKPERM_ALIGNED;
  191. }
  192. } else if (tp->eff_sacks) {
  193. /* A SACK is 2 pad bytes, a 2 byte header, plus
  194.  * 2 32-bit sequence numbers for each SACK block.
  195.  */
  196. tcp_header_size += (TCPOLEN_SACK_BASE_ALIGNED +
  197.     (tp->eff_sacks * TCPOLEN_SACK_PERBLOCK));
  198. }
  199. th = (struct tcphdr *) skb_push(skb, tcp_header_size);
  200. skb->h.th = th;
  201. skb_set_owner_w(skb, sk);
  202. /* Build TCP header and checksum it. */
  203. th->source = sk->sport;
  204. th->dest = sk->dport;
  205. th->seq = htonl(tcb->seq);
  206. th->ack_seq = htonl(tp->rcv_nxt);
  207. *(((__u16 *)th) + 6) = htons(((tcp_header_size >> 2) << 12) | tcb->flags);
  208. if (tcb->flags & TCPCB_FLAG_SYN) {
  209. /* RFC1323: The window in SYN & SYN/ACK segments
  210.  * is never scaled.
  211.  */
  212. th->window = htons(tp->rcv_wnd);
  213. } else {
  214. th->window = htons(tcp_select_window(sk));
  215. }
  216. th->check = 0;
  217. th->urg_ptr = 0;
  218. if (tp->urg_mode &&
  219.     between(tp->snd_up, tcb->seq+1, tcb->seq+0xFFFF)) {
  220. th->urg_ptr = htons(tp->snd_up-tcb->seq);
  221. th->urg = 1;
  222. }
  223. if (tcb->flags & TCPCB_FLAG_SYN) {
  224. tcp_syn_build_options((__u32 *)(th + 1),
  225.       tcp_advertise_mss(sk),
  226.       (sysctl_flags & SYSCTL_FLAG_TSTAMPS),
  227.       (sysctl_flags & SYSCTL_FLAG_SACK),
  228.       (sysctl_flags & SYSCTL_FLAG_WSCALE),
  229.       tp->rcv_wscale,
  230.       tcb->when,
  231.              tp->ts_recent);
  232. } else {
  233. tcp_build_and_update_options((__u32 *)(th + 1),
  234.      tp, tcb->when);
  235. TCP_ECN_send(sk, tp, skb, tcp_header_size);
  236. }
  237. tp->af_specific->send_check(sk, th, skb->len, skb);
  238. if (tcb->flags & TCPCB_FLAG_ACK)
  239. tcp_event_ack_sent(sk);
  240. if (skb->len != tcp_header_size)
  241. tcp_event_data_sent(tp, skb);
  242. TCP_INC_STATS(TcpOutSegs);
  243. err = tp->af_specific->queue_xmit(skb);
  244. if (err <= 0)
  245. return err;
  246. tcp_enter_cwr(tp);
  247. /* NET_XMIT_CN is special. It does not guarantee,
  248.  * that this packet is lost. It tells that device
  249.  * is about to start to drop packets or already
  250.  * drops some packets of the same priority and
  251.  * invokes us to send less aggressively.
  252.  */
  253. return err == NET_XMIT_CN ? 0 : err;
  254. }
  255. return -ENOBUFS;
  256. #undef SYSCTL_FLAG_TSTAMPS
  257. #undef SYSCTL_FLAG_WSCALE
  258. #undef SYSCTL_FLAG_SACK
  259. }
  260. /* This is the main buffer sending routine. We queue the buffer
  261.  * and decide whether to queue or transmit now.
  262.  *
  263.  * NOTE: probe0 timer is not checked, do not forget tcp_push_pending_frames,
  264.  * otherwise socket can stall.
  265.  */
  266. void tcp_send_skb(struct sock *sk, struct sk_buff *skb, int force_queue, unsigned cur_mss)
  267. {
  268. struct tcp_opt *tp = &(sk->tp_pinfo.af_tcp);
  269. /* Advance write_seq and place onto the write_queue. */
  270. tp->write_seq = TCP_SKB_CB(skb)->end_seq;
  271. __skb_queue_tail(&sk->write_queue, skb);
  272. tcp_charge_skb(sk, skb);
  273. if (!force_queue && tp->send_head == NULL && tcp_snd_test(tp, skb, cur_mss, tp->nonagle)) {
  274. /* Send it out now. */
  275. TCP_SKB_CB(skb)->when = tcp_time_stamp;
  276. if (tcp_transmit_skb(sk, skb_clone(skb, sk->allocation)) == 0) {
  277. tp->snd_nxt = TCP_SKB_CB(skb)->end_seq;
  278. tcp_minshall_update(tp, cur_mss, skb);
  279. if (tp->packets_out++ == 0)
  280. tcp_reset_xmit_timer(sk, TCP_TIME_RETRANS, tp->rto);
  281. return;
  282. }
  283. }
  284. /* Queue it, remembering where we must start sending. */
  285. if (tp->send_head == NULL)
  286. tp->send_head = skb;
  287. }
  288. /* Send _single_ skb sitting at the send head. This function requires
  289.  * true push pending frames to setup probe timer etc.
  290.  */
  291. void tcp_push_one(struct sock *sk, unsigned cur_mss)
  292. {
  293. struct tcp_opt *tp = &(sk->tp_pinfo.af_tcp);
  294. struct sk_buff *skb = tp->send_head;
  295. if (tcp_snd_test(tp, skb, cur_mss, 1)) {
  296. /* Send it out now. */
  297. TCP_SKB_CB(skb)->when = tcp_time_stamp;
  298. if (tcp_transmit_skb(sk, skb_clone(skb, sk->allocation)) == 0) {
  299. tp->send_head = NULL;
  300. tp->snd_nxt = TCP_SKB_CB(skb)->end_seq;
  301. if (tp->packets_out++ == 0)
  302. tcp_reset_xmit_timer(sk, TCP_TIME_RETRANS, tp->rto);
  303. return;
  304. }
  305. }
  306. }
  307. /* Split fragmented skb to two parts at length len. */
  308. static void skb_split(struct sk_buff *skb, struct sk_buff *skb1, u32 len)
  309. {
  310. int i;
  311. int pos = skb->len - skb->data_len;
  312. if (len < pos) {
  313. /* Split line is inside header. */
  314. memcpy(skb_put(skb1, pos-len), skb->data + len, pos-len);
  315. /* And move data appendix as is. */
  316. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
  317. skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
  318. skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
  319. skb_shinfo(skb)->nr_frags = 0;
  320. skb1->data_len = skb->data_len;
  321. skb1->len += skb1->data_len;
  322. skb->data_len = 0;
  323. skb->len = len;
  324. skb->tail = skb->data+len;
  325. } else {
  326. int k = 0;
  327. int nfrags = skb_shinfo(skb)->nr_frags;
  328. /* Second chunk has no header, nothing to copy. */
  329. skb_shinfo(skb)->nr_frags = 0;
  330. skb1->len = skb1->data_len = skb->len - len;
  331. skb->len = len;
  332. skb->data_len = len - pos;
  333. for (i=0; i<nfrags; i++) {
  334. int size = skb_shinfo(skb)->frags[i].size;
  335. if (pos + size > len) {
  336. skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
  337. if (pos < len) {
  338. /* Split frag.
  339.  * We have to variants in this case:
  340.  * 1. Move all the frag to the second
  341.  *    part, if it is possible. F.e.
  342.  *    this approach is mandatory for TUX,
  343.  *    where splitting is expensive.
  344.  * 2. Split is accurately. We make this.
  345.  */
  346. get_page(skb_shinfo(skb)->frags[i].page);
  347. skb_shinfo(skb1)->frags[0].page_offset += (len-pos);
  348. skb_shinfo(skb1)->frags[0].size -= (len-pos);
  349. skb_shinfo(skb)->frags[i].size = len-pos;
  350. skb_shinfo(skb)->nr_frags++;
  351. }
  352. k++;
  353. } else {
  354. skb_shinfo(skb)->nr_frags++;
  355. }
  356. pos += size;
  357. }
  358. skb_shinfo(skb1)->nr_frags = k;
  359. }
  360. }
  361. /* Function to create two new TCP segments.  Shrinks the given segment
  362.  * to the specified size and appends a new segment with the rest of the
  363.  * packet to the list.  This won't be called frequently, I hope. 
  364.  * Remember, these are still headerless SKBs at this point.
  365.  */
  366. static int tcp_fragment(struct sock *sk, struct sk_buff *skb, u32 len)
  367. {
  368. struct tcp_opt *tp = &sk->tp_pinfo.af_tcp;
  369. struct sk_buff *buff;
  370. int nsize = skb->len - len;
  371. u16 flags;
  372. if (skb_cloned(skb) &&
  373.     skb_is_nonlinear(skb) &&
  374.     pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
  375. return -ENOMEM;
  376. /* Get a new skb... force flag on. */
  377. buff = tcp_alloc_skb(sk, nsize, GFP_ATOMIC);
  378. if (buff == NULL)
  379. return -ENOMEM; /* We'll just try again later. */
  380. tcp_charge_skb(sk, buff);
  381. /* Correct the sequence numbers. */
  382. TCP_SKB_CB(buff)->seq = TCP_SKB_CB(skb)->seq + len;
  383. TCP_SKB_CB(buff)->end_seq = TCP_SKB_CB(skb)->end_seq;
  384. TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(buff)->seq;
  385. /* PSH and FIN should only be set in the second packet. */
  386. flags = TCP_SKB_CB(skb)->flags;
  387. TCP_SKB_CB(skb)->flags = flags & ~(TCPCB_FLAG_FIN|TCPCB_FLAG_PSH);
  388. TCP_SKB_CB(buff)->flags = flags;
  389. TCP_SKB_CB(buff)->sacked = TCP_SKB_CB(skb)->sacked&(TCPCB_LOST|TCPCB_EVER_RETRANS|TCPCB_AT_TAIL);
  390. if (TCP_SKB_CB(buff)->sacked&TCPCB_LOST) {
  391. tp->lost_out++;
  392. tp->left_out++;
  393. }
  394. TCP_SKB_CB(skb)->sacked &= ~TCPCB_AT_TAIL;
  395. if (!skb_shinfo(skb)->nr_frags && skb->ip_summed != CHECKSUM_HW) {
  396. /* Copy and checksum data tail into the new buffer. */
  397. buff->csum = csum_partial_copy_nocheck(skb->data + len, skb_put(buff, nsize),
  398.        nsize, 0);
  399. skb_trim(skb, len);
  400. skb->csum = csum_block_sub(skb->csum, buff->csum, len);
  401. } else {
  402. skb->ip_summed = CHECKSUM_HW;
  403. skb_split(skb, buff, len);
  404. }
  405. buff->ip_summed = skb->ip_summed;
  406. /* Looks stupid, but our code really uses when of
  407.  * skbs, which it never sent before. --ANK
  408.  */
  409. TCP_SKB_CB(buff)->when = TCP_SKB_CB(skb)->when;
  410. /* Link BUFF into the send queue. */
  411. __skb_append(skb, buff);
  412. return 0;
  413. }
  414. /* This function synchronize snd mss to current pmtu/exthdr set.
  415.    tp->user_mss is mss set by user by TCP_MAXSEG. It does NOT counts
  416.    for TCP options, but includes only bare TCP header.
  417.    tp->mss_clamp is mss negotiated at connection setup.
  418.    It is minumum of user_mss and mss received with SYN.
  419.    It also does not include TCP options.
  420.    tp->pmtu_cookie is last pmtu, seen by this function.
  421.    tp->mss_cache is current effective sending mss, including
  422.    all tcp options except for SACKs. It is evaluated,
  423.    taking into account current pmtu, but never exceeds
  424.    tp->mss_clamp.
  425.    NOTE1. rfc1122 clearly states that advertised MSS
  426.    DOES NOT include either tcp or ip options.
  427.    NOTE2. tp->pmtu_cookie and tp->mss_cache are READ ONLY outside
  428.    this function. --ANK (980731)
  429.  */
  430. int tcp_sync_mss(struct sock *sk, u32 pmtu)
  431. {
  432. struct tcp_opt *tp = &sk->tp_pinfo.af_tcp;
  433. int mss_now;
  434. /* Calculate base mss without TCP options:
  435.    It is MMS_S - sizeof(tcphdr) of rfc1122
  436.  */
  437. mss_now = pmtu - tp->af_specific->net_header_len - sizeof(struct tcphdr);
  438. /* Clamp it (mss_clamp does not include tcp options) */
  439. if (mss_now > tp->mss_clamp)
  440. mss_now = tp->mss_clamp;
  441. /* Now subtract optional transport overhead */
  442. mss_now -= tp->ext_header_len;
  443. /* Then reserve room for full set of TCP options and 8 bytes of data */
  444. if (mss_now < 48)
  445. mss_now = 48;
  446. /* Now subtract TCP options size, not including SACKs */
  447. mss_now -= tp->tcp_header_len - sizeof(struct tcphdr);
  448. /* Bound mss with half of window */
  449. if (tp->max_window && mss_now > (tp->max_window>>1))
  450. mss_now = max((tp->max_window>>1), 68U - tp->tcp_header_len);
  451. /* And store cached results */
  452. tp->pmtu_cookie = pmtu;
  453. tp->mss_cache = mss_now;
  454. return mss_now;
  455. }
  456. /* This routine writes packets to the network.  It advances the
  457.  * send_head.  This happens as incoming acks open up the remote
  458.  * window for us.
  459.  *
  460.  * Returns 1, if no segments are in flight and we have queued segments, but
  461.  * cannot send anything now because of SWS or another problem.
  462.  */
  463. int tcp_write_xmit(struct sock *sk, int nonagle)
  464. {
  465. struct tcp_opt *tp = &(sk->tp_pinfo.af_tcp);
  466. unsigned int mss_now;
  467. /* If we are closed, the bytes will have to remain here.
  468.  * In time closedown will finish, we empty the write queue and all
  469.  * will be happy.
  470.  */
  471. if(sk->state != TCP_CLOSE) {
  472. struct sk_buff *skb;
  473. int sent_pkts = 0;
  474. /* Account for SACKS, we may need to fragment due to this.
  475.  * It is just like the real MSS changing on us midstream.
  476.  * We also handle things correctly when the user adds some
  477.  * IP options mid-stream.  Silly to do, but cover it.
  478.  */
  479. mss_now = tcp_current_mss(sk); 
  480. while((skb = tp->send_head) &&
  481.       tcp_snd_test(tp, skb, mss_now, tcp_skb_is_last(sk, skb) ? nonagle : 1)) {
  482. if (skb->len > mss_now) {
  483. if (tcp_fragment(sk, skb, mss_now))
  484. break;
  485. }
  486. TCP_SKB_CB(skb)->when = tcp_time_stamp;
  487. if (tcp_transmit_skb(sk, skb_clone(skb, GFP_ATOMIC)))
  488. break;
  489. /* Advance the send_head.  This one is sent out. */
  490. update_send_head(sk, tp, skb);
  491. tcp_minshall_update(tp, mss_now, skb);
  492. sent_pkts = 1;
  493. }
  494. if (sent_pkts) {
  495. tcp_cwnd_validate(sk, tp);
  496. return 0;
  497. }
  498. return !tp->packets_out && tp->send_head;
  499. }
  500. return 0;
  501. }
  502. /* This function returns the amount that we can raise the
  503.  * usable window based on the following constraints
  504.  *  
  505.  * 1. The window can never be shrunk once it is offered (RFC 793)
  506.  * 2. We limit memory per socket
  507.  *
  508.  * RFC 1122:
  509.  * "the suggested [SWS] avoidance algorithm for the receiver is to keep
  510.  *  RECV.NEXT + RCV.WIN fixed until:
  511.  *  RCV.BUFF - RCV.USER - RCV.WINDOW >= min(1/2 RCV.BUFF, MSS)"
  512.  *
  513.  * i.e. don't raise the right edge of the window until you can raise
  514.  * it at least MSS bytes.
  515.  *
  516.  * Unfortunately, the recommended algorithm breaks header prediction,
  517.  * since header prediction assumes th->window stays fixed.
  518.  *
  519.  * Strictly speaking, keeping th->window fixed violates the receiver
  520.  * side SWS prevention criteria. The problem is that under this rule
  521.  * a stream of single byte packets will cause the right side of the
  522.  * window to always advance by a single byte.
  523.  * 
  524.  * Of course, if the sender implements sender side SWS prevention
  525.  * then this will not be a problem.
  526.  * 
  527.  * BSD seems to make the following compromise:
  528.  * 
  529.  * If the free space is less than the 1/4 of the maximum
  530.  * space available and the free space is less than 1/2 mss,
  531.  * then set the window to 0.
  532.  * [ Actually, bsd uses MSS and 1/4 of maximal _window_ ]
  533.  * Otherwise, just prevent the window from shrinking
  534.  * and from being larger than the largest representable value.
  535.  *
  536.  * This prevents incremental opening of the window in the regime
  537.  * where TCP is limited by the speed of the reader side taking
  538.  * data out of the TCP receive queue. It does nothing about
  539.  * those cases where the window is constrained on the sender side
  540.  * because the pipeline is full.
  541.  *
  542.  * BSD also seems to "accidentally" limit itself to windows that are a
  543.  * multiple of MSS, at least until the free space gets quite small.
  544.  * This would appear to be a side effect of the mbuf implementation.
  545.  * Combining these two algorithms results in the observed behavior
  546.  * of having a fixed window size at almost all times.
  547.  *
  548.  * Below we obtain similar behavior by forcing the offered window to
  549.  * a multiple of the mss when it is feasible to do so.
  550.  *
  551.  * Note, we don't "adjust" for TIMESTAMP or SACK option bytes.
  552.  * Regular options like TIMESTAMP are taken into account.
  553.  */
  554. u32 __tcp_select_window(struct sock *sk)
  555. {
  556. struct tcp_opt *tp = &sk->tp_pinfo.af_tcp;
  557. /* MSS for the peer's data.  Previous verions used mss_clamp
  558.  * here.  I don't know if the value based on our guesses
  559.  * of peer's MSS is better for the performance.  It's more correct
  560.  * but may be worse for the performance because of rcv_mss
  561.  * fluctuations.  --SAW  1998/11/1
  562.  */
  563. int mss = tp->ack.rcv_mss;
  564. int free_space = tcp_space(sk);
  565. int full_space = min_t(int, tp->window_clamp, tcp_full_space(sk));
  566. int window;
  567. if (mss > full_space)
  568. mss = full_space; 
  569. if (free_space < full_space/2) {
  570. tp->ack.quick = 0;
  571. if (tcp_memory_pressure)
  572. tp->rcv_ssthresh = min(tp->rcv_ssthresh, 4U*tp->advmss);
  573. if (free_space < mss)
  574. return 0;
  575. }
  576. if (free_space > tp->rcv_ssthresh)
  577. free_space = tp->rcv_ssthresh;
  578. /* Get the largest window that is a nice multiple of mss.
  579.  * Window clamp already applied above.
  580.  * If our current window offering is within 1 mss of the
  581.  * free space we just keep it. This prevents the divide
  582.  * and multiply from happening most of the time.
  583.  * We also don't do any window rounding when the free space
  584.  * is too small.
  585.  */
  586. window = tp->rcv_wnd;
  587. if (window <= free_space - mss || window > free_space)
  588. window = (free_space/mss)*mss;
  589. return window;
  590. }
  591. /* Attempt to collapse two adjacent SKB's during retransmission. */
  592. static void tcp_retrans_try_collapse(struct sock *sk, struct sk_buff *skb, int mss_now)
  593. {
  594. struct tcp_opt *tp = &sk->tp_pinfo.af_tcp;
  595. struct sk_buff *next_skb = skb->next;
  596. /* The first test we must make is that neither of these two
  597.  * SKB's are still referenced by someone else.
  598.  */
  599. if(!skb_cloned(skb) && !skb_cloned(next_skb)) {
  600. int skb_size = skb->len, next_skb_size = next_skb->len;
  601. u16 flags = TCP_SKB_CB(skb)->flags;
  602. /* Also punt if next skb has been SACK'd. */
  603. if(TCP_SKB_CB(next_skb)->sacked & TCPCB_SACKED_ACKED)
  604. return;
  605. /* Next skb is out of window. */
  606. if (after(TCP_SKB_CB(next_skb)->end_seq, tp->snd_una+tp->snd_wnd))
  607. return;
  608. /* Punt if not enough space exists in the first SKB for
  609.  * the data in the second, or the total combined payload
  610.  * would exceed the MSS.
  611.  */
  612. if ((next_skb_size > skb_tailroom(skb)) ||
  613.     ((skb_size + next_skb_size) > mss_now))
  614. return;
  615. /* Ok.  We will be able to collapse the packet. */
  616. __skb_unlink(next_skb, next_skb->list);
  617. if (next_skb->ip_summed == CHECKSUM_HW)
  618. skb->ip_summed = CHECKSUM_HW;
  619. if (skb->ip_summed != CHECKSUM_HW) {
  620. memcpy(skb_put(skb, next_skb_size), next_skb->data, next_skb_size);
  621. skb->csum = csum_block_add(skb->csum, next_skb->csum, skb_size);
  622. }
  623. /* Update sequence range on original skb. */
  624. TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(next_skb)->end_seq;
  625. /* Merge over control information. */
  626. flags |= TCP_SKB_CB(next_skb)->flags; /* This moves PSH/FIN etc. over */
  627. TCP_SKB_CB(skb)->flags = flags;
  628. /* All done, get rid of second SKB and account for it so
  629.  * packet counting does not break.
  630.  */
  631. TCP_SKB_CB(skb)->sacked |= TCP_SKB_CB(next_skb)->sacked&(TCPCB_EVER_RETRANS|TCPCB_AT_TAIL);
  632. if (TCP_SKB_CB(next_skb)->sacked&TCPCB_SACKED_RETRANS)
  633. tp->retrans_out--;
  634. if (TCP_SKB_CB(next_skb)->sacked&TCPCB_LOST) {
  635. tp->lost_out--;
  636. tp->left_out--;
  637. }
  638. /* Reno case is special. Sigh... */
  639. if (!tp->sack_ok && tp->sacked_out) {
  640. tp->sacked_out--;
  641. tp->left_out--;
  642. }
  643. /* Not quite right: it can be > snd.fack, but
  644.  * it is better to underestimate fackets.
  645.  */
  646. if (tp->fackets_out)
  647. tp->fackets_out--;
  648. tcp_free_skb(sk, next_skb);
  649. tp->packets_out--;
  650. }
  651. }
  652. /* Do a simple retransmit without using the backoff mechanisms in
  653.  * tcp_timer. This is used for path mtu discovery. 
  654.  * The socket is already locked here.
  655.  */ 
  656. void tcp_simple_retransmit(struct sock *sk)
  657. {
  658. struct tcp_opt *tp = &(sk->tp_pinfo.af_tcp);
  659. struct sk_buff *skb;
  660. unsigned int mss = tcp_current_mss(sk);
  661. int lost = 0;
  662. for_retrans_queue(skb, sk, tp) {
  663. if (skb->len > mss && 
  664.     !(TCP_SKB_CB(skb)->sacked&TCPCB_SACKED_ACKED)) {
  665. if (TCP_SKB_CB(skb)->sacked&TCPCB_SACKED_RETRANS) {
  666. TCP_SKB_CB(skb)->sacked &= ~TCPCB_SACKED_RETRANS;
  667. tp->retrans_out--;
  668. }
  669. if (!(TCP_SKB_CB(skb)->sacked&TCPCB_LOST)) {
  670. TCP_SKB_CB(skb)->sacked |= TCPCB_LOST;
  671. tp->lost_out++;
  672. lost = 1;
  673. }
  674. }
  675. }
  676. if (!lost)
  677. return;
  678. tcp_sync_left_out(tp);
  679.   /* Don't muck with the congestion window here.
  680.  * Reason is that we do not increase amount of _data_
  681.  * in network, but units changed and effective
  682.  * cwnd/ssthresh really reduced now.
  683.  */
  684. if (tp->ca_state != TCP_CA_Loss) {
  685. tp->high_seq = tp->snd_nxt;
  686. tp->snd_ssthresh = tcp_current_ssthresh(tp);
  687. tp->prior_ssthresh = 0;
  688. tp->undo_marker = 0;
  689. tp->ca_state = TCP_CA_Loss;
  690. }
  691. tcp_xmit_retransmit_queue(sk);
  692. }
  693. /* This retransmits one SKB.  Policy decisions and retransmit queue
  694.  * state updates are done by the caller.  Returns non-zero if an
  695.  * error occurred which prevented the send.
  696.  */
  697. int tcp_retransmit_skb(struct sock *sk, struct sk_buff *skb)
  698. {
  699. struct tcp_opt *tp = &(sk->tp_pinfo.af_tcp);
  700. unsigned int cur_mss = tcp_current_mss(sk);
  701. int err;
  702. /* Do not sent more than we queued. 1/4 is reserved for possible
  703.  * copying overhead: frgagmentation, tunneling, mangling etc.
  704.  */
  705. if (atomic_read(&sk->wmem_alloc) > min(sk->wmem_queued+(sk->wmem_queued>>2),sk->sndbuf))
  706. return -EAGAIN;
  707. /* If receiver has shrunk his window, and skb is out of
  708.  * new window, do not retransmit it. The exception is the
  709.  * case, when window is shrunk to zero. In this case
  710.  * our retransmit serves as a zero window probe.
  711.  */
  712. if (!before(TCP_SKB_CB(skb)->seq, tp->snd_una+tp->snd_wnd)
  713.     && TCP_SKB_CB(skb)->seq != tp->snd_una)
  714. return -EAGAIN;
  715. if(skb->len > cur_mss) {
  716. if(tcp_fragment(sk, skb, cur_mss))
  717. return -ENOMEM; /* We'll try again later. */
  718. /* New SKB created, account for it. */
  719. tp->packets_out++;
  720. }
  721. /* Collapse two adjacent packets if worthwhile and we can. */
  722. if(!(TCP_SKB_CB(skb)->flags & TCPCB_FLAG_SYN) &&
  723.    (skb->len < (cur_mss >> 1)) &&
  724.    (skb->next != tp->send_head) &&
  725.    (skb->next != (struct sk_buff *)&sk->write_queue) &&
  726.    (skb_shinfo(skb)->nr_frags == 0 && skb_shinfo(skb->next)->nr_frags == 0) &&
  727.    (sysctl_tcp_retrans_collapse != 0))
  728. tcp_retrans_try_collapse(sk, skb, cur_mss);
  729. if(tp->af_specific->rebuild_header(sk))
  730. return -EHOSTUNREACH; /* Routing failure or similar. */
  731. /* Some Solaris stacks overoptimize and ignore the FIN on a
  732.  * retransmit when old data is attached.  So strip it off
  733.  * since it is cheap to do so and saves bytes on the network.
  734.  */
  735. if(skb->len > 0 &&
  736.    (TCP_SKB_CB(skb)->flags & TCPCB_FLAG_FIN) &&
  737.    tp->snd_una == (TCP_SKB_CB(skb)->end_seq - 1)) {
  738. if (!pskb_trim(skb, 0)) {
  739. TCP_SKB_CB(skb)->seq = TCP_SKB_CB(skb)->end_seq - 1;
  740. skb->ip_summed = CHECKSUM_NONE;
  741. skb->csum = 0;
  742. }
  743. }
  744. /* Make a copy, if the first transmission SKB clone we made
  745.  * is still in somebody's hands, else make a clone.
  746.  */
  747. TCP_SKB_CB(skb)->when = tcp_time_stamp;
  748. err = tcp_transmit_skb(sk, (skb_cloned(skb) ?
  749.     pskb_copy(skb, GFP_ATOMIC):
  750.     skb_clone(skb, GFP_ATOMIC)));
  751. if (err == 0) {
  752. /* Update global TCP statistics. */
  753. TCP_INC_STATS(TcpRetransSegs);
  754. #if FASTRETRANS_DEBUG > 0
  755. if (TCP_SKB_CB(skb)->sacked&TCPCB_SACKED_RETRANS) {
  756. if (net_ratelimit())
  757. printk(KERN_DEBUG "retrans_out leaked.n");
  758. }
  759. #endif
  760. TCP_SKB_CB(skb)->sacked |= TCPCB_RETRANS;
  761. tp->retrans_out++;
  762. /* Save stamp of the first retransmit. */
  763. if (!tp->retrans_stamp)
  764. tp->retrans_stamp = TCP_SKB_CB(skb)->when;
  765. tp->undo_retrans++;
  766. /* snd_nxt is stored to detect loss of retransmitted segment,
  767.  * see tcp_input.c tcp_sacktag_write_queue().
  768.  */
  769. TCP_SKB_CB(skb)->ack_seq = tp->snd_nxt;
  770. }
  771. return err;
  772. }
  773. /* This gets called after a retransmit timeout, and the initially
  774.  * retransmitted data is acknowledged.  It tries to continue
  775.  * resending the rest of the retransmit queue, until either
  776.  * we've sent it all or the congestion window limit is reached.
  777.  * If doing SACK, the first ACK which comes back for a timeout
  778.  * based retransmit packet might feed us FACK information again.
  779.  * If so, we use it to avoid unnecessarily retransmissions.
  780.  */
  781. void tcp_xmit_retransmit_queue(struct sock *sk)
  782. {
  783. struct tcp_opt *tp = &(sk->tp_pinfo.af_tcp);
  784. struct sk_buff *skb;
  785. int packet_cnt = tp->lost_out;
  786. /* First pass: retransmit lost packets. */
  787. if (packet_cnt) {
  788. for_retrans_queue(skb, sk, tp) {
  789. __u8 sacked = TCP_SKB_CB(skb)->sacked;
  790. if (tcp_packets_in_flight(tp) >= tp->snd_cwnd)
  791. return;
  792. if (sacked&TCPCB_LOST) {
  793. if (!(sacked&(TCPCB_SACKED_ACKED|TCPCB_SACKED_RETRANS))) {
  794. if (tcp_retransmit_skb(sk, skb))
  795. return;
  796. if (tp->ca_state != TCP_CA_Loss)
  797. NET_INC_STATS_BH(TCPFastRetrans);
  798. else
  799. NET_INC_STATS_BH(TCPSlowStartRetrans);
  800. if (skb == skb_peek(&sk->write_queue))
  801. tcp_reset_xmit_timer(sk, TCP_TIME_RETRANS, tp->rto);
  802. }
  803. if (--packet_cnt <= 0)
  804. break;
  805. }
  806. }
  807. }
  808. /* OK, demanded retransmission is finished. */
  809. /* Forward retransmissions are possible only during Recovery. */
  810. if (tp->ca_state != TCP_CA_Recovery)
  811. return;
  812. /* No forward retransmissions in Reno are possible. */
  813. if (!tp->sack_ok)
  814. return;
  815. /* Yeah, we have to make difficult choice between forward transmission
  816.  * and retransmission... Both ways have their merits...
  817.  *
  818.  * For now we do not retrnamsit anything, while we have some new
  819.  * segments to send.
  820.  */
  821. if (tcp_may_send_now(sk, tp))
  822. return;
  823. packet_cnt = 0;
  824. for_retrans_queue(skb, sk, tp) {
  825. if(++packet_cnt > tp->fackets_out)
  826. break;
  827. if (tcp_packets_in_flight(tp) >= tp->snd_cwnd)
  828. break;
  829. if(TCP_SKB_CB(skb)->sacked & TCPCB_TAGBITS)
  830. continue;
  831. /* Ok, retransmit it. */
  832. if(tcp_retransmit_skb(sk, skb))
  833. break;
  834. if (skb == skb_peek(&sk->write_queue))
  835. tcp_reset_xmit_timer(sk, TCP_TIME_RETRANS, tp->rto);
  836. NET_INC_STATS_BH(TCPForwardRetrans);
  837. }
  838. }
  839. /* Send a fin.  The caller locks the socket for us.  This cannot be
  840.  * allowed to fail queueing a FIN frame under any circumstances.
  841.  */
  842. void tcp_send_fin(struct sock *sk)
  843. {
  844. struct tcp_opt *tp = &(sk->tp_pinfo.af_tcp);
  845. struct sk_buff *skb = skb_peek_tail(&sk->write_queue);
  846. unsigned int mss_now;
  847. /* Optimization, tack on the FIN if we have a queue of
  848.  * unsent frames.  But be careful about outgoing SACKS
  849.  * and IP options.
  850.  */
  851. mss_now = tcp_current_mss(sk); 
  852. if(tp->send_head != NULL) {
  853. TCP_SKB_CB(skb)->flags |= TCPCB_FLAG_FIN;
  854. TCP_SKB_CB(skb)->end_seq++;
  855. tp->write_seq++;
  856. } else {
  857. /* Socket is locked, keep trying until memory is available. */
  858. for (;;) {
  859. skb = alloc_skb(MAX_TCP_HEADER, GFP_KERNEL);
  860. if (skb)
  861. break;
  862. yield();
  863. }
  864. /* Reserve space for headers and prepare control bits. */
  865. skb_reserve(skb, MAX_TCP_HEADER);
  866. skb->csum = 0;
  867. TCP_SKB_CB(skb)->flags = (TCPCB_FLAG_ACK | TCPCB_FLAG_FIN);
  868. TCP_SKB_CB(skb)->sacked = 0;
  869. /* FIN eats a sequence byte, write_seq advanced by tcp_send_skb(). */
  870. TCP_SKB_CB(skb)->seq = tp->write_seq;
  871. TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(skb)->seq + 1;
  872. tcp_send_skb(sk, skb, 1, mss_now);
  873. }
  874. __tcp_push_pending_frames(sk, tp, mss_now, 1);
  875. }
  876. /* We get here when a process closes a file descriptor (either due to
  877.  * an explicit close() or as a byproduct of exit()'ing) and there
  878.  * was unread data in the receive queue.  This behavior is recommended
  879.  * by draft-ietf-tcpimpl-prob-03.txt section 3.10.  -DaveM
  880.  */
  881. void tcp_send_active_reset(struct sock *sk, int priority)
  882. {
  883. struct tcp_opt *tp = &(sk->tp_pinfo.af_tcp);
  884. struct sk_buff *skb;
  885. /* NOTE: No TCP options attached and we never retransmit this. */
  886. skb = alloc_skb(MAX_TCP_HEADER, priority);
  887. if (!skb) {
  888. NET_INC_STATS(TCPAbortFailed);
  889. return;
  890. }
  891. /* Reserve space for headers and prepare control bits. */
  892. skb_reserve(skb, MAX_TCP_HEADER);
  893. skb->csum = 0;
  894. TCP_SKB_CB(skb)->flags = (TCPCB_FLAG_ACK | TCPCB_FLAG_RST);
  895. TCP_SKB_CB(skb)->sacked = 0;
  896. /* Send it off. */
  897. TCP_SKB_CB(skb)->seq = tcp_acceptable_seq(sk, tp);
  898. TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(skb)->seq;
  899. TCP_SKB_CB(skb)->when = tcp_time_stamp;
  900. if (tcp_transmit_skb(sk, skb))
  901. NET_INC_STATS(TCPAbortFailed);
  902. }
  903. /* WARNING: This routine must only be called when we have already sent
  904.  * a SYN packet that crossed the incoming SYN that caused this routine
  905.  * to get called. If this assumption fails then the initial rcv_wnd
  906.  * and rcv_wscale values will not be correct.
  907.  */
  908. int tcp_send_synack(struct sock *sk)
  909. {
  910. struct sk_buff* skb;
  911. skb = skb_peek(&sk->write_queue);
  912. if (skb == NULL || !(TCP_SKB_CB(skb)->flags&TCPCB_FLAG_SYN)) {
  913. printk(KERN_DEBUG "tcp_send_synack: wrong queue staten");
  914. return -EFAULT;
  915. }
  916. if (!(TCP_SKB_CB(skb)->flags&TCPCB_FLAG_ACK)) {
  917. if (skb_cloned(skb)) {
  918. struct sk_buff *nskb = skb_copy(skb, GFP_ATOMIC);
  919. if (nskb == NULL)
  920. return -ENOMEM;
  921. __skb_unlink(skb, &sk->write_queue);
  922. __skb_queue_head(&sk->write_queue, nskb);
  923. tcp_free_skb(sk, skb);
  924. tcp_charge_skb(sk, nskb);
  925. skb = nskb;
  926. }
  927. TCP_SKB_CB(skb)->flags |= TCPCB_FLAG_ACK;
  928. TCP_ECN_send_synack(&sk->tp_pinfo.af_tcp, skb);
  929. }
  930. TCP_SKB_CB(skb)->when = tcp_time_stamp;
  931. return tcp_transmit_skb(sk, skb_clone(skb, GFP_ATOMIC));
  932. }
  933. /*
  934.  * Prepare a SYN-ACK.
  935.  */
  936. struct sk_buff * tcp_make_synack(struct sock *sk, struct dst_entry *dst,
  937.  struct open_request *req)
  938. {
  939. struct tcp_opt *tp = &(sk->tp_pinfo.af_tcp);
  940. struct tcphdr *th;
  941. int tcp_header_size;
  942. struct sk_buff *skb;
  943. skb = sock_wmalloc(sk, MAX_TCP_HEADER + 15, 1, GFP_ATOMIC);
  944. if (skb == NULL)
  945. return NULL;
  946. /* Reserve space for headers. */
  947. skb_reserve(skb, MAX_TCP_HEADER);
  948. skb->dst = dst_clone(dst);
  949. tcp_header_size = (sizeof(struct tcphdr) + TCPOLEN_MSS +
  950.    (req->tstamp_ok ? TCPOLEN_TSTAMP_ALIGNED : 0) +
  951.    (req->wscale_ok ? TCPOLEN_WSCALE_ALIGNED : 0) +
  952.    /* SACK_PERM is in the place of NOP NOP of TS */
  953.    ((req->sack_ok && !req->tstamp_ok) ? TCPOLEN_SACKPERM_ALIGNED : 0));
  954. skb->h.th = th = (struct tcphdr *) skb_push(skb, tcp_header_size);
  955. memset(th, 0, sizeof(struct tcphdr));
  956. th->syn = 1;
  957. th->ack = 1;
  958. TCP_ECN_make_synack(req, th);
  959. th->source = sk->sport;
  960. th->dest = req->rmt_port;
  961. TCP_SKB_CB(skb)->seq = req->snt_isn;
  962. TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(skb)->seq + 1;
  963. th->seq = htonl(TCP_SKB_CB(skb)->seq);
  964. th->ack_seq = htonl(req->rcv_isn + 1);
  965. if (req->rcv_wnd == 0) { /* ignored for retransmitted syns */
  966. __u8 rcv_wscale; 
  967. /* Set this up on the first call only */
  968. req->window_clamp = tp->window_clamp ? : dst->window;
  969. /* tcp_full_space because it is guaranteed to be the first packet */
  970. tcp_select_initial_window(tcp_full_space(sk), 
  971. dst->advmss - (req->tstamp_ok ? TCPOLEN_TSTAMP_ALIGNED : 0),
  972. &req->rcv_wnd,
  973. &req->window_clamp,
  974. req->wscale_ok,
  975. &rcv_wscale);
  976. req->rcv_wscale = rcv_wscale; 
  977. }
  978. /* RFC1323: The window in SYN & SYN/ACK segments is never scaled. */
  979. th->window = htons(req->rcv_wnd);
  980. TCP_SKB_CB(skb)->when = tcp_time_stamp;
  981. tcp_syn_build_options((__u32 *)(th + 1), dst->advmss, req->tstamp_ok,
  982.       req->sack_ok, req->wscale_ok, req->rcv_wscale,
  983.       TCP_SKB_CB(skb)->when,
  984.       req->ts_recent);
  985. skb->csum = 0;
  986. th->doff = (tcp_header_size >> 2);
  987. TCP_INC_STATS(TcpOutSegs);
  988. return skb;
  989. }
  990. /* 
  991.  * Do all connect socket setups that can be done AF independent.
  992.  */ 
  993. static inline void tcp_connect_init(struct sock *sk)
  994. {
  995. struct dst_entry *dst = __sk_dst_get(sk);
  996. struct tcp_opt *tp = &(sk->tp_pinfo.af_tcp);
  997. /* We'll fix this up when we get a response from the other end.
  998.  * See tcp_input.c:tcp_rcv_state_process case TCP_SYN_SENT.
  999.  */
  1000. tp->tcp_header_len = sizeof(struct tcphdr) +
  1001. (sysctl_tcp_timestamps ? TCPOLEN_TSTAMP_ALIGNED : 0);
  1002. /* If user gave his TCP_MAXSEG, record it to clamp */
  1003. if (tp->user_mss)
  1004. tp->mss_clamp = tp->user_mss;
  1005. tp->max_window = 0;
  1006. tcp_sync_mss(sk, dst->pmtu);
  1007. if (!tp->window_clamp)
  1008. tp->window_clamp = dst->window;
  1009. tp->advmss = dst->advmss;
  1010. tcp_initialize_rcv_mss(sk);
  1011. tcp_select_initial_window(tcp_full_space(sk),
  1012.   tp->advmss - (tp->ts_recent_stamp ? tp->tcp_header_len - sizeof(struct tcphdr) : 0),
  1013.   &tp->rcv_wnd,
  1014.   &tp->window_clamp,
  1015.   sysctl_tcp_window_scaling,
  1016.   &tp->rcv_wscale);
  1017. tp->rcv_ssthresh = tp->rcv_wnd;
  1018. sk->err = 0;
  1019. sk->done = 0;
  1020. tp->snd_wnd = 0;
  1021. tcp_init_wl(tp, tp->write_seq, 0);
  1022. tp->snd_una = tp->write_seq;
  1023. tp->snd_sml = tp->write_seq;
  1024. tp->rcv_nxt = 0;
  1025. tp->rcv_wup = 0;
  1026. tp->copied_seq = 0;
  1027. tp->rto = TCP_TIMEOUT_INIT;
  1028. tp->retransmits = 0;
  1029. tcp_clear_retrans(tp);
  1030. }
  1031. /*
  1032.  * Build a SYN and send it off.
  1033.  */ 
  1034. int tcp_connect(struct sock *sk)
  1035. {
  1036. struct tcp_opt *tp = &(sk->tp_pinfo.af_tcp);
  1037. struct sk_buff *buff;
  1038. tcp_connect_init(sk);
  1039. buff = alloc_skb(MAX_TCP_HEADER + 15, sk->allocation);
  1040. if (unlikely(buff == NULL))
  1041. return -ENOBUFS;
  1042. /* Reserve space for headers. */
  1043. skb_reserve(buff, MAX_TCP_HEADER);
  1044. TCP_SKB_CB(buff)->flags = TCPCB_FLAG_SYN;
  1045. TCP_ECN_send_syn(tp, buff);
  1046. TCP_SKB_CB(buff)->sacked = 0;
  1047. buff->csum = 0;
  1048. TCP_SKB_CB(buff)->seq = tp->write_seq++;
  1049. TCP_SKB_CB(buff)->end_seq = tp->write_seq;
  1050. tp->snd_nxt = tp->write_seq;
  1051. tp->pushed_seq = tp->write_seq;
  1052. /* Send it off. */
  1053. TCP_SKB_CB(buff)->when = tcp_time_stamp;
  1054. tp->retrans_stamp = TCP_SKB_CB(buff)->when;
  1055. __skb_queue_tail(&sk->write_queue, buff);
  1056. tcp_charge_skb(sk, buff);
  1057. tp->packets_out++;
  1058. tcp_transmit_skb(sk, skb_clone(buff, GFP_KERNEL));
  1059. TCP_INC_STATS(TcpActiveOpens);
  1060. /* Timer for repeating the SYN until an answer. */
  1061. tcp_reset_xmit_timer(sk, TCP_TIME_RETRANS, tp->rto);
  1062. return 0;
  1063. }
  1064. /* Send out a delayed ack, the caller does the policy checking
  1065.  * to see if we should even be here.  See tcp_input.c:tcp_ack_snd_check()
  1066.  * for details.
  1067.  */
  1068. void tcp_send_delayed_ack(struct sock *sk)
  1069. {
  1070. struct tcp_opt *tp = &sk->tp_pinfo.af_tcp;
  1071. int ato = tp->ack.ato;
  1072. unsigned long timeout;
  1073. if (ato > TCP_DELACK_MIN) {
  1074. int max_ato = HZ/2;
  1075. if (tp->ack.pingpong || (tp->ack.pending&TCP_ACK_PUSHED))
  1076. max_ato = TCP_DELACK_MAX;
  1077. /* Slow path, intersegment interval is "high". */
  1078. /* If some rtt estimate is known, use it to bound delayed ack.
  1079.  * Do not use tp->rto here, use results of rtt measurements
  1080.  * directly.
  1081.  */
  1082. if (tp->srtt) {
  1083. int rtt = max(tp->srtt>>3, TCP_DELACK_MIN);
  1084. if (rtt < max_ato)
  1085. max_ato = rtt;
  1086. }
  1087. ato = min(ato, max_ato);
  1088. }
  1089. /* Stay within the limit we were given */
  1090. timeout = jiffies + ato;
  1091. /* Use new timeout only if there wasn't a older one earlier. */
  1092. if (tp->ack.pending&TCP_ACK_TIMER) {
  1093. /* If delack timer was blocked or is about to expire,
  1094.  * send ACK now.
  1095.  */
  1096. if (tp->ack.blocked || time_before_eq(tp->ack.timeout, jiffies+(ato>>2))) {
  1097. tcp_send_ack(sk);
  1098. return;
  1099. }
  1100. if (!time_before(timeout, tp->ack.timeout))
  1101. timeout = tp->ack.timeout;
  1102. }
  1103. tp->ack.pending |= TCP_ACK_SCHED|TCP_ACK_TIMER;
  1104. tp->ack.timeout = timeout;
  1105. if (!mod_timer(&tp->delack_timer, timeout))
  1106. sock_hold(sk);
  1107. }
  1108. /* This routine sends an ack and also updates the window. */
  1109. void tcp_send_ack(struct sock *sk)
  1110. {
  1111. /* If we have been reset, we may not send again. */
  1112. if(sk->state != TCP_CLOSE) {
  1113. struct tcp_opt *tp = &(sk->tp_pinfo.af_tcp);
  1114. struct sk_buff *buff;
  1115. /* We are not putting this on the write queue, so
  1116.  * tcp_transmit_skb() will set the ownership to this
  1117.  * sock.
  1118.  */
  1119. buff = alloc_skb(MAX_TCP_HEADER, GFP_ATOMIC);
  1120. if (buff == NULL) {
  1121. tcp_schedule_ack(tp);
  1122. tp->ack.ato = TCP_ATO_MIN;
  1123. tcp_reset_xmit_timer(sk, TCP_TIME_DACK, TCP_DELACK_MAX);
  1124. return;
  1125. }
  1126. /* Reserve space for headers and prepare control bits. */
  1127. skb_reserve(buff, MAX_TCP_HEADER);
  1128. buff->csum = 0;
  1129. TCP_SKB_CB(buff)->flags = TCPCB_FLAG_ACK;
  1130. TCP_SKB_CB(buff)->sacked = 0;
  1131. /* Send it off, this clears delayed acks for us. */
  1132. TCP_SKB_CB(buff)->seq = TCP_SKB_CB(buff)->end_seq = tcp_acceptable_seq(sk, tp);
  1133. TCP_SKB_CB(buff)->when = tcp_time_stamp;
  1134. tcp_transmit_skb(sk, buff);
  1135. }
  1136. }
  1137. /* This routine sends a packet with an out of date sequence
  1138.  * number. It assumes the other end will try to ack it.
  1139.  *
  1140.  * Question: what should we make while urgent mode?
  1141.  * 4.4BSD forces sending single byte of data. We cannot send
  1142.  * out of window data, because we have SND.NXT==SND.MAX...
  1143.  *
  1144.  * Current solution: to send TWO zero-length segments in urgent mode:
  1145.  * one is with SEG.SEQ=SND.UNA to deliver urgent pointer, another is
  1146.  * out-of-date with SND.UNA-1 to probe window.
  1147.  */
  1148. static int tcp_xmit_probe_skb(struct sock *sk, int urgent)
  1149. {
  1150. struct tcp_opt *tp = &(sk->tp_pinfo.af_tcp);
  1151. struct sk_buff *skb;
  1152. /* We don't queue it, tcp_transmit_skb() sets ownership. */
  1153. skb = alloc_skb(MAX_TCP_HEADER, GFP_ATOMIC);
  1154. if (skb == NULL) 
  1155. return -1;
  1156. /* Reserve space for headers and set control bits. */
  1157. skb_reserve(skb, MAX_TCP_HEADER);
  1158. skb->csum = 0;
  1159. TCP_SKB_CB(skb)->flags = TCPCB_FLAG_ACK;
  1160. TCP_SKB_CB(skb)->sacked = urgent;
  1161. /* Use a previous sequence.  This should cause the other
  1162.  * end to send an ack.  Don't queue or clone SKB, just
  1163.  * send it.
  1164.  */
  1165. TCP_SKB_CB(skb)->seq = urgent ? tp->snd_una : tp->snd_una - 1;
  1166. TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(skb)->seq;
  1167. TCP_SKB_CB(skb)->when = tcp_time_stamp;
  1168. return tcp_transmit_skb(sk, skb);
  1169. }
  1170. int tcp_write_wakeup(struct sock *sk)
  1171. {
  1172. if (sk->state != TCP_CLOSE) {
  1173. struct tcp_opt *tp = &(sk->tp_pinfo.af_tcp);
  1174. struct sk_buff *skb;
  1175. if ((skb = tp->send_head) != NULL &&
  1176.     before(TCP_SKB_CB(skb)->seq, tp->snd_una+tp->snd_wnd)) {
  1177. int err;
  1178. int mss = tcp_current_mss(sk);
  1179. int seg_size = tp->snd_una+tp->snd_wnd-TCP_SKB_CB(skb)->seq;
  1180. if (before(tp->pushed_seq, TCP_SKB_CB(skb)->end_seq))
  1181. tp->pushed_seq = TCP_SKB_CB(skb)->end_seq;
  1182. /* We are probing the opening of a window
  1183.  * but the window size is != 0
  1184.  * must have been a result SWS avoidance ( sender )
  1185.  */
  1186. if (seg_size < TCP_SKB_CB(skb)->end_seq - TCP_SKB_CB(skb)->seq ||
  1187.     skb->len > mss) {
  1188. seg_size = min(seg_size, mss);
  1189. TCP_SKB_CB(skb)->flags |= TCPCB_FLAG_PSH;
  1190. if (tcp_fragment(sk, skb, seg_size))
  1191. return -1;
  1192. }
  1193. TCP_SKB_CB(skb)->flags |= TCPCB_FLAG_PSH;
  1194. TCP_SKB_CB(skb)->when = tcp_time_stamp;
  1195. err = tcp_transmit_skb(sk, skb_clone(skb, GFP_ATOMIC));
  1196. if (!err) {
  1197. update_send_head(sk, tp, skb);
  1198. }
  1199. return err;
  1200. } else {
  1201. if (tp->urg_mode &&
  1202.     between(tp->snd_up, tp->snd_una+1, tp->snd_una+0xFFFF))
  1203. tcp_xmit_probe_skb(sk, TCPCB_URG);
  1204. return tcp_xmit_probe_skb(sk, 0);
  1205. }
  1206. }
  1207. return -1;
  1208. }
  1209. /* A window probe timeout has occurred.  If window is not closed send
  1210.  * a partial packet else a zero probe.
  1211.  */
  1212. void tcp_send_probe0(struct sock *sk)
  1213. {
  1214. struct tcp_opt *tp = &(sk->tp_pinfo.af_tcp);
  1215. int err;
  1216. err = tcp_write_wakeup(sk);
  1217. if (tp->packets_out || !tp->send_head) {
  1218. /* Cancel probe timer, if it is not required. */
  1219. tp->probes_out = 0;
  1220. tp->backoff = 0;
  1221. return;
  1222. }
  1223. if (err <= 0) {
  1224. tp->backoff++;
  1225. tp->probes_out++;
  1226. tcp_reset_xmit_timer (sk, TCP_TIME_PROBE0, 
  1227.       min(tp->rto << tp->backoff, TCP_RTO_MAX));
  1228. } else {
  1229. /* If packet was not sent due to local congestion,
  1230.  * do not backoff and do not remember probes_out.
  1231.  * Let local senders to fight for local resources.
  1232.  *
  1233.  * Use accumulated backoff yet.
  1234.  */
  1235. if (!tp->probes_out)
  1236. tp->probes_out=1;
  1237. tcp_reset_xmit_timer (sk, TCP_TIME_PROBE0, 
  1238.       min(tp->rto << tp->backoff, TCP_RESOURCE_PROBE_INTERVAL));
  1239. }
  1240. }