tcp_output.c revision 1.59 1 /* $NetBSD: tcp_output.c,v 1.59 2000/10/17 02:57:02 thorpej Exp $ */
2
3 /*
4 %%% portions-copyright-nrl-95
5 Portions of this software are Copyright 1995-1998 by Randall Atkinson,
6 Ronald Lee, Daniel McDonald, Bao Phan, and Chris Winters. All Rights
7 Reserved. All rights under this copyright have been assigned to the US
8 Naval Research Laboratory (NRL). The NRL Copyright Notice and License
9 Agreement Version 1.1 (January 17, 1995) applies to these portions of the
10 software.
11 You should have received a copy of the license with this software. If you
12 didn't get a copy, you may request one from <license (at) ipv6.nrl.navy.mil>.
13
14 */
15
16 /*
17 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
18 * All rights reserved.
19 *
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
22 * are met:
23 * 1. Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer.
25 * 2. Redistributions in binary form must reproduce the above copyright
26 * notice, this list of conditions and the following disclaimer in the
27 * documentation and/or other materials provided with the distribution.
28 * 3. Neither the name of the project nor the names of its contributors
29 * may be used to endorse or promote products derived from this software
30 * without specific prior written permission.
31 *
32 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
33 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
36 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 * SUCH DAMAGE.
43 */
44
45 /*-
46 * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
47 * All rights reserved.
48 *
49 * This code is derived from software contributed to The NetBSD Foundation
50 * by Jason R. Thorpe and Kevin M. Lahey of the Numerical Aerospace Simulation
51 * Facility, NASA Ames Research Center.
52 *
53 * Redistribution and use in source and binary forms, with or without
54 * modification, are permitted provided that the following conditions
55 * are met:
56 * 1. Redistributions of source code must retain the above copyright
57 * notice, this list of conditions and the following disclaimer.
58 * 2. Redistributions in binary form must reproduce the above copyright
59 * notice, this list of conditions and the following disclaimer in the
60 * documentation and/or other materials provided with the distribution.
61 * 3. All advertising materials mentioning features or use of this software
62 * must display the following acknowledgement:
63 * This product includes software developed by the NetBSD
64 * Foundation, Inc. and its contributors.
65 * 4. Neither the name of The NetBSD Foundation nor the names of its
66 * contributors may be used to endorse or promote products derived
67 * from this software without specific prior written permission.
68 *
69 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
70 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
71 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
72 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
73 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
74 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
75 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
76 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
77 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
78 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
79 * POSSIBILITY OF SUCH DAMAGE.
80 */
81
82 /*
83 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
84 * The Regents of the University of California. All rights reserved.
85 *
86 * Redistribution and use in source and binary forms, with or without
87 * modification, are permitted provided that the following conditions
88 * are met:
89 * 1. Redistributions of source code must retain the above copyright
90 * notice, this list of conditions and the following disclaimer.
91 * 2. Redistributions in binary form must reproduce the above copyright
92 * notice, this list of conditions and the following disclaimer in the
93 * documentation and/or other materials provided with the distribution.
94 * 3. All advertising materials mentioning features or use of this software
95 * must display the following acknowledgement:
96 * This product includes software developed by the University of
97 * California, Berkeley and its contributors.
98 * 4. Neither the name of the University nor the names of its contributors
99 * may be used to endorse or promote products derived from this software
100 * without specific prior written permission.
101 *
102 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
103 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
104 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
105 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
106 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
107 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
108 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
109 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
110 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
111 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
112 * SUCH DAMAGE.
113 *
114 * @(#)tcp_output.c 8.4 (Berkeley) 5/24/95
115 */
116
117 #include "opt_inet.h"
118 #include "opt_ipsec.h"
119
120 #include <sys/param.h>
121 #include <sys/systm.h>
122 #include <sys/malloc.h>
123 #include <sys/mbuf.h>
124 #include <sys/protosw.h>
125 #include <sys/socket.h>
126 #include <sys/socketvar.h>
127 #include <sys/errno.h>
128 #include <sys/domain.h>
129
130 #include <net/if.h>
131 #include <net/route.h>
132
133 #include <netinet/in.h>
134 #include <netinet/in_systm.h>
135 #include <netinet/ip.h>
136 #include <netinet/in_pcb.h>
137 #include <netinet/ip_var.h>
138
139 #ifdef INET6
140 #ifndef INET
141 #include <netinet/in.h>
142 #endif
143 #include <netinet/ip6.h>
144 #include <netinet6/in6_pcb.h>
145 #include <netinet6/ip6_var.h>
146 #endif
147
148 #include <netinet/tcp.h>
149 #define TCPOUTFLAGS
150 #include <netinet/tcp_fsm.h>
151 #include <netinet/tcp_seq.h>
152 #include <netinet/tcp_timer.h>
153 #include <netinet/tcp_var.h>
154 #include <netinet/tcpip.h>
155 #include <netinet/tcp_debug.h>
156
157 #ifdef notyet
158 extern struct mbuf *m_copypack();
159 #endif
160
161 #define MAX_TCPOPTLEN 32 /* max # bytes that go in options */
162
163 /*
164 * Knob to enable Congestion Window Monitoring, and control the
165 * the burst size it allows. Default burst is 4 packets, per
166 * the Internet draft.
167 */
168 int tcp_cwm = 0;
169 int tcp_cwm_burstsize = 4;
170
171 static __inline void tcp_segsize __P((struct tcpcb *, int *, int *));
172 static __inline void
173 tcp_segsize(tp, txsegsizep, rxsegsizep)
174 struct tcpcb *tp;
175 int *txsegsizep, *rxsegsizep;
176 {
177 struct inpcb *inp = tp->t_inpcb;
178 #ifdef INET6
179 struct in6pcb *in6p = tp->t_in6pcb;
180 #endif
181 struct rtentry *rt;
182 struct ifnet *ifp;
183 int size;
184 int iphlen;
185
186 switch (tp->t_family) {
187 case AF_INET:
188 iphlen = sizeof(struct ip);
189 break;
190 #ifdef INET6
191 case AF_INET6:
192 iphlen = sizeof(struct ip6_hdr);
193 break;
194 #endif
195 default:
196 size = tcp_mssdflt;
197 goto out;
198 }
199
200 if (inp)
201 rt = in_pcbrtentry(inp);
202 #if defined(INET6) && !defined(TCP6)
203 else if (in6p)
204 rt = in6_pcbrtentry(in6p);
205 #endif
206 else
207 rt = NULL;
208 if (rt == NULL) {
209 size = tcp_mssdflt;
210 goto out;
211 }
212
213 ifp = rt->rt_ifp;
214
215 size = tcp_mssdflt;
216 if (rt->rt_rmx.rmx_mtu != 0)
217 size = rt->rt_rmx.rmx_mtu - iphlen - sizeof(struct tcphdr);
218 else if (ip_mtudisc || ifp->if_flags & IFF_LOOPBACK)
219 size = ifp->if_mtu - iphlen - sizeof(struct tcphdr);
220 else if (inp && in_localaddr(inp->inp_faddr))
221 size = ifp->if_mtu - iphlen - sizeof(struct tcphdr);
222 #ifdef INET6
223 else if (in6p) {
224 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)) {
225 /* mapped addr case */
226 struct in_addr d;
227 bcopy(&in6p->in6p_faddr.s6_addr32[3], &d, sizeof(d));
228 if (in_localaddr(d))
229 size = ifp->if_mtu - iphlen - sizeof(struct tcphdr);
230 } else {
231 if (in6_localaddr(&in6p->in6p_faddr))
232 size = ifp->if_mtu - iphlen - sizeof(struct tcphdr);
233 }
234 }
235 #endif
236 size -= tcp_optlen(tp);
237 /*
238 * XXX tp->t_ourmss should have the right size, but without this code
239 * fragmentation will occur... need more investigation
240 */
241 if (inp) {
242 #ifdef IPSEC
243 size -= ipsec4_hdrsiz_tcp(tp);
244 #endif
245 size -= ip_optlen(inp);
246 }
247 #ifdef INET6
248 else if (in6p && tp->t_family == AF_INET) {
249 #ifdef IPSEC
250 size -= ipsec4_hdrsiz_tcp(tp);
251 #endif
252 /* XXX size -= ip_optlen(in6p); */
253 }
254 else if (in6p && tp->t_family == AF_INET6) {
255 #if defined(IPSEC) && !defined(TCP6)
256 size -= ipsec6_hdrsiz_tcp(tp);
257 #endif
258 size -= ip6_optlen(in6p);
259 }
260 #endif
261
262 out:
263 /*
264 * *rxsegsizep holds *estimated* inbound segment size (estimation
265 * assumes that path MTU is the same for both ways). this is only
266 * for silly window avoidance, do not use the value for other purposes.
267 *
268 * ipseclen is subtracted from both sides, this may not be right.
269 * I'm not quite sure about this (could someone comment).
270 */
271 *txsegsizep = min(tp->t_peermss, size);
272 *rxsegsizep = min(tp->t_ourmss, size);
273
274 if (*txsegsizep != tp->t_segsz) {
275 /*
276 * If the new segment size is larger, we don't want to
277 * mess up the congestion window, but if it is smaller
278 * we'll have to reduce the congestion window to ensure
279 * that we don't get into trouble with initial windows
280 * and the rest. In any case, if the segment size
281 * has changed, chances are the path has, too, and
282 * our congestion window will be different.
283 */
284 if (*txsegsizep < tp->t_segsz) {
285 tp->snd_cwnd = max((tp->snd_cwnd / tp->t_segsz)
286 * *txsegsizep, *txsegsizep);
287 tp->snd_ssthresh = max((tp->snd_ssthresh / tp->t_segsz)
288 * *txsegsizep, *txsegsizep);
289 }
290 tp->t_segsz = *txsegsizep;
291 }
292 }
293
294 /*
295 * Tcp output routine: figure out what should be sent and send it.
296 */
297 int
298 tcp_output(tp)
299 struct tcpcb *tp;
300 {
301 struct socket *so;
302 struct route *ro;
303 long len, win;
304 int off, flags, error;
305 struct mbuf *m;
306 struct ip *ip;
307 #ifdef INET6
308 struct ip6_hdr *ip6;
309 #endif
310 struct tcphdr *th;
311 u_char opt[MAX_TCPOPTLEN];
312 unsigned optlen, hdrlen;
313 int idle, sendalot, txsegsize, rxsegsize;
314 int maxburst = TCP_MAXBURST;
315 int af; /* address family on the wire */
316 int iphdrlen;
317
318 so = NULL;
319 ro = NULL;
320 if (tp->t_inpcb) {
321 so = tp->t_inpcb->inp_socket;
322 ro = &tp->t_inpcb->inp_route;
323 }
324 #ifdef INET6
325 else if (tp->t_in6pcb) {
326 so = tp->t_in6pcb->in6p_socket;
327 ro = (struct route *)&tp->t_in6pcb->in6p_route;
328 }
329 #endif
330
331 switch (af = tp->t_family) {
332 case AF_INET:
333 if (tp->t_inpcb)
334 break;
335 #ifdef INET6
336 /* mapped addr case */
337 if (tp->t_in6pcb)
338 break;
339 #endif
340 return EINVAL;
341 #ifdef INET6
342 case AF_INET6:
343 if (tp->t_in6pcb)
344 break;
345 return EINVAL;
346 #endif
347 default:
348 return EAFNOSUPPORT;
349 }
350
351 tcp_segsize(tp, &txsegsize, &rxsegsize);
352
353 idle = (tp->snd_max == tp->snd_una);
354
355 /*
356 * Restart Window computation. From draft-floyd-incr-init-win-03:
357 *
358 * Optionally, a TCP MAY set the restart window to the
359 * minimum of the value used for the initial window and
360 * the current value of cwnd (in other words, using a
361 * larger value for the restart window should never increase
362 * the size of cwnd).
363 */
364 if (tcp_cwm) {
365 /*
366 * Hughes/Touch/Heidemann Congestion Window Monitoring.
367 * Count the number of packets currently pending
368 * acknowledgement, and limit our congestion window
369 * to a pre-determined allowed burst size plus that count.
370 * This prevents bursting once all pending packets have
371 * been acknowledged (i.e. transmission is idle).
372 *
373 * XXX Link this to Initial Window?
374 */
375 tp->snd_cwnd = min(tp->snd_cwnd,
376 (tcp_cwm_burstsize * txsegsize) +
377 (tp->snd_nxt - tp->snd_una));
378 } else {
379 if (idle && tp->t_idle >= tp->t_rxtcur) {
380 /*
381 * We have been idle for "a while" and no acks are
382 * expected to clock out any data we send --
383 * slow start to get ack "clock" running again.
384 */
385 tp->snd_cwnd = min(tp->snd_cwnd,
386 TCP_INITIAL_WINDOW(tcp_init_win, txsegsize));
387 }
388 }
389
390 again:
391 /*
392 * Determine length of data that should be transmitted, and
393 * flags that should be used. If there is some data or critical
394 * controls (SYN, RST) to send, then transmit; otherwise,
395 * investigate further.
396 */
397 sendalot = 0;
398 off = tp->snd_nxt - tp->snd_una;
399 win = min(tp->snd_wnd, tp->snd_cwnd);
400
401 flags = tcp_outflags[tp->t_state];
402 /*
403 * If in persist timeout with window of 0, send 1 byte.
404 * Otherwise, if window is small but nonzero
405 * and timer expired, we will send what we can
406 * and go to transmit state.
407 */
408 if (tp->t_force) {
409 if (win == 0) {
410 /*
411 * If we still have some data to send, then
412 * clear the FIN bit. Usually this would
413 * happen below when it realizes that we
414 * aren't sending all the data. However,
415 * if we have exactly 1 byte of unset data,
416 * then it won't clear the FIN bit below,
417 * and if we are in persist state, we wind
418 * up sending the packet without recording
419 * that we sent the FIN bit.
420 *
421 * We can't just blindly clear the FIN bit,
422 * because if we don't have any more data
423 * to send then the probe will be the FIN
424 * itself.
425 */
426 if (off < so->so_snd.sb_cc)
427 flags &= ~TH_FIN;
428 win = 1;
429 } else {
430 TCP_TIMER_DISARM(tp, TCPT_PERSIST);
431 tp->t_rxtshift = 0;
432 }
433 }
434
435 if (win < so->so_snd.sb_cc) {
436 len = win - off;
437 flags &= ~TH_FIN;
438 } else
439 len = so->so_snd.sb_cc - off;
440
441 if (len < 0) {
442 /*
443 * If FIN has been sent but not acked,
444 * but we haven't been called to retransmit,
445 * len will be -1. Otherwise, window shrank
446 * after we sent into it. If window shrank to 0,
447 * cancel pending retransmit, pull snd_nxt back
448 * to (closed) window, and set the persist timer
449 * if it isn't already going. If the window didn't
450 * close completely, just wait for an ACK.
451 *
452 * If we have a pending FIN, either it has already been
453 * transmitted or it is outside the window, so drop it.
454 * If the FIN has been transmitted, but this is not a
455 * retransmission, then len must be -1. Therefore we also
456 * prevent here the sending of `gratuitous FINs'. This
457 * eliminates the need to check for that case below (e.g.
458 * to back up snd_nxt before the FIN so that the sequence
459 * number is correct).
460 */
461 len = 0;
462 flags &= ~TH_FIN;
463 if (win == 0) {
464 TCP_TIMER_DISARM(tp, TCPT_REXMT);
465 tp->t_rxtshift = 0;
466 tp->snd_nxt = tp->snd_una;
467 if (TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0)
468 tcp_setpersist(tp);
469 }
470 }
471 if (len > txsegsize) {
472 len = txsegsize;
473 flags &= ~TH_FIN;
474 sendalot = 1;
475 }
476
477 win = sbspace(&so->so_rcv);
478
479 /*
480 * Sender silly window avoidance. If connection is idle
481 * and can send all data, a maximum segment,
482 * at least a maximum default-size segment do it,
483 * or are forced, do it; otherwise don't bother.
484 * If peer's buffer is tiny, then send
485 * when window is at least half open.
486 * If retransmitting (possibly after persist timer forced us
487 * to send into a small window), then must resend.
488 */
489 if (len) {
490 if (len == txsegsize)
491 goto send;
492 if ((so->so_state & SS_MORETOCOME) == 0 &&
493 ((idle || tp->t_flags & TF_NODELAY) &&
494 len + off >= so->so_snd.sb_cc))
495 goto send;
496 if (tp->t_force)
497 goto send;
498 if (len >= tp->max_sndwnd / 2)
499 goto send;
500 if (SEQ_LT(tp->snd_nxt, tp->snd_max))
501 goto send;
502 }
503
504 /*
505 * Compare available window to amount of window known to peer
506 * (as advertised window less next expected input). If the
507 * difference is at least twice the size of the largest segment
508 * we expect to receive (i.e. two segments) or at least 50% of
509 * the maximum possible window, then want to send a window update
510 * to peer.
511 */
512 if (win > 0) {
513 /*
514 * "adv" is the amount we can increase the window,
515 * taking into account that we are limited by
516 * TCP_MAXWIN << tp->rcv_scale.
517 */
518 long adv = min(win, (long)TCP_MAXWIN << tp->rcv_scale) -
519 (tp->rcv_adv - tp->rcv_nxt);
520
521 if (adv >= (long) (2 * rxsegsize))
522 goto send;
523 if (2 * adv >= (long) so->so_rcv.sb_hiwat)
524 goto send;
525 }
526
527 /*
528 * Send if we owe peer an ACK.
529 */
530 if (tp->t_flags & TF_ACKNOW)
531 goto send;
532 if (flags & (TH_SYN|TH_FIN|TH_RST))
533 goto send;
534 if (SEQ_GT(tp->snd_up, tp->snd_una))
535 goto send;
536
537 /*
538 * TCP window updates are not reliable, rather a polling protocol
539 * using ``persist'' packets is used to insure receipt of window
540 * updates. The three ``states'' for the output side are:
541 * idle not doing retransmits or persists
542 * persisting to move a small or zero window
543 * (re)transmitting and thereby not persisting
544 *
545 * tp->t_timer[TCPT_PERSIST]
546 * is set when we are in persist state.
547 * tp->t_force
548 * is set when we are called to send a persist packet.
549 * tp->t_timer[TCPT_REXMT]
550 * is set when we are retransmitting
551 * The output side is idle when both timers are zero.
552 *
553 * If send window is too small, there is data to transmit, and no
554 * retransmit or persist is pending, then go to persist state.
555 * If nothing happens soon, send when timer expires:
556 * if window is nonzero, transmit what we can,
557 * otherwise force out a byte.
558 */
559 if (so->so_snd.sb_cc && TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0 &&
560 TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0) {
561 tp->t_rxtshift = 0;
562 tcp_setpersist(tp);
563 }
564
565 /*
566 * No reason to send a segment, just return.
567 */
568 return (0);
569
570 send:
571 /*
572 * Before ESTABLISHED, force sending of initial options
573 * unless TCP set not to do any options.
574 * NOTE: we assume that the IP/TCP header plus TCP options
575 * always fit in a single mbuf, leaving room for a maximum
576 * link header, i.e.
577 * max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MCLBYTES
578 */
579 optlen = 0;
580 switch (af) {
581 case AF_INET:
582 iphdrlen = sizeof(struct ip) + sizeof(struct tcphdr);
583 break;
584 #ifdef INET6
585 case AF_INET6:
586 iphdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
587 break;
588 #endif
589 default: /*pacify gcc*/
590 iphdrlen = 0;
591 break;
592 }
593 hdrlen = iphdrlen;
594 if (flags & TH_SYN) {
595 struct rtentry *rt;
596
597 if (tp->t_inpcb)
598 rt = in_pcbrtentry(tp->t_inpcb);
599 #if defined(INET6) && !defined(TCP6)
600 else if (tp->t_in6pcb)
601 rt = in6_pcbrtentry(tp->t_in6pcb);
602 #endif
603 else
604 rt = NULL;
605
606 tp->snd_nxt = tp->iss;
607 tp->t_ourmss = tcp_mss_to_advertise(rt != NULL ?
608 rt->rt_ifp : NULL, af);
609 if ((tp->t_flags & TF_NOOPT) == 0) {
610 opt[0] = TCPOPT_MAXSEG;
611 opt[1] = 4;
612 opt[2] = (tp->t_ourmss >> 8) & 0xff;
613 opt[3] = tp->t_ourmss & 0xff;
614 optlen = 4;
615
616 if ((tp->t_flags & TF_REQ_SCALE) &&
617 ((flags & TH_ACK) == 0 ||
618 (tp->t_flags & TF_RCVD_SCALE))) {
619 *((u_int32_t *) (opt + optlen)) = htonl(
620 TCPOPT_NOP << 24 |
621 TCPOPT_WINDOW << 16 |
622 TCPOLEN_WINDOW << 8 |
623 tp->request_r_scale);
624 optlen += 4;
625 }
626 }
627 }
628
629 /*
630 * Send a timestamp and echo-reply if this is a SYN and our side
631 * wants to use timestamps (TF_REQ_TSTMP is set) or both our side
632 * and our peer have sent timestamps in our SYN's.
633 */
634 if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
635 (flags & TH_RST) == 0 &&
636 ((flags & (TH_SYN|TH_ACK)) == TH_SYN ||
637 (tp->t_flags & TF_RCVD_TSTMP))) {
638 u_int32_t *lp = (u_int32_t *)(opt + optlen);
639
640 /* Form timestamp option as shown in appendix A of RFC 1323. */
641 *lp++ = htonl(TCPOPT_TSTAMP_HDR);
642 *lp++ = htonl(tcp_now);
643 *lp = htonl(tp->ts_recent);
644 optlen += TCPOLEN_TSTAMP_APPA;
645 }
646
647 hdrlen += optlen;
648
649 #ifdef DIAGNOSTIC
650 if (len > txsegsize)
651 panic("tcp data to be sent is larger than segment");
652 if (max_linkhdr + hdrlen > MCLBYTES)
653 panic("tcphdr too big");
654 #endif
655
656 /*
657 * Grab a header mbuf, attaching a copy of data to
658 * be transmitted, and initialize the header from
659 * the template for sends on this connection.
660 */
661 if (len) {
662 if (tp->t_force && len == 1)
663 tcpstat.tcps_sndprobe++;
664 else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
665 tcpstat.tcps_sndrexmitpack++;
666 tcpstat.tcps_sndrexmitbyte += len;
667 } else {
668 tcpstat.tcps_sndpack++;
669 tcpstat.tcps_sndbyte += len;
670 }
671 #ifdef notyet
672 if ((m = m_copypack(so->so_snd.sb_mb, off,
673 (int)len, max_linkhdr + hdrlen)) == 0) {
674 error = ENOBUFS;
675 goto out;
676 }
677 /*
678 * m_copypack left space for our hdr; use it.
679 */
680 m->m_len += hdrlen;
681 m->m_data -= hdrlen;
682 #else
683 MGETHDR(m, M_DONTWAIT, MT_HEADER);
684 if (m != NULL &&
685 (max_linkhdr + hdrlen > MHLEN ||
686 max_linkhdr + hdrlen + len <= MCLBYTES)) {
687 MCLGET(m, M_DONTWAIT);
688 if ((m->m_flags & M_EXT) == 0) {
689 m_freem(m);
690 m = NULL;
691 }
692 }
693 if (m == NULL) {
694 error = ENOBUFS;
695 goto out;
696 }
697 m->m_data += max_linkhdr;
698 m->m_len = hdrlen;
699 if (len <= M_TRAILINGSPACE(m)) {
700 m_copydata(so->so_snd.sb_mb, off, (int) len,
701 mtod(m, caddr_t) + hdrlen);
702 m->m_len += len;
703 } else {
704 m->m_next = m_copy(so->so_snd.sb_mb, off, (int) len);
705 if (m->m_next == NULL) {
706 m_freem(m);
707 error = ENOBUFS;
708 goto out;
709 }
710 }
711 #endif
712 /*
713 * If we're sending everything we've got, set PUSH.
714 * (This will keep happy those implementations which only
715 * give data to the user when a buffer fills or
716 * a PUSH comes in.)
717 */
718 if (off + len == so->so_snd.sb_cc)
719 flags |= TH_PUSH;
720 } else {
721 if (tp->t_flags & TF_ACKNOW)
722 tcpstat.tcps_sndacks++;
723 else if (flags & (TH_SYN|TH_FIN|TH_RST))
724 tcpstat.tcps_sndctrl++;
725 else if (SEQ_GT(tp->snd_up, tp->snd_una))
726 tcpstat.tcps_sndurg++;
727 else
728 tcpstat.tcps_sndwinup++;
729
730 MGETHDR(m, M_DONTWAIT, MT_HEADER);
731 if (m != NULL && max_linkhdr + hdrlen > MHLEN) {
732 MCLGET(m, M_DONTWAIT);
733 if ((m->m_flags & M_EXT) == 0) {
734 m_freem(m);
735 m = NULL;
736 }
737 }
738 if (m == NULL) {
739 error = ENOBUFS;
740 goto out;
741 }
742 m->m_data += max_linkhdr;
743 m->m_len = hdrlen;
744 }
745 m->m_pkthdr.rcvif = (struct ifnet *)0;
746 switch (af) {
747 case AF_INET:
748 ip = mtod(m, struct ip *);
749 #ifdef INET6
750 ip6 = NULL;
751 #endif
752 th = (struct tcphdr *)(ip + 1);
753 break;
754 #ifdef INET6
755 case AF_INET6:
756 ip = NULL;
757 ip6 = mtod(m, struct ip6_hdr *);
758 th = (struct tcphdr *)(ip6 + 1);
759 break;
760 #endif
761 default: /*pacify gcc*/
762 ip = NULL;
763 #ifdef INET6
764 ip6 = NULL;
765 #endif
766 th = NULL;
767 break;
768 }
769 if (tp->t_template == 0)
770 panic("tcp_output");
771 if (tp->t_template->m_len < iphdrlen)
772 panic("tcp_output");
773 bcopy(mtod(tp->t_template, caddr_t), mtod(m, caddr_t), iphdrlen);
774
775 /*
776 * If we are doing retransmissions, then snd_nxt will
777 * not reflect the first unsent octet. For ACK only
778 * packets, we do not want the sequence number of the
779 * retransmitted packet, we want the sequence number
780 * of the next unsent octet. So, if there is no data
781 * (and no SYN or FIN), use snd_max instead of snd_nxt
782 * when filling in ti_seq. But if we are in persist
783 * state, snd_max might reflect one byte beyond the
784 * right edge of the window, so use snd_nxt in that
785 * case, since we know we aren't doing a retransmission.
786 * (retransmit and persist are mutually exclusive...)
787 */
788 if (len || (flags & (TH_SYN|TH_FIN)) ||
789 TCP_TIMER_ISARMED(tp, TCPT_PERSIST))
790 th->th_seq = htonl(tp->snd_nxt);
791 else
792 th->th_seq = htonl(tp->snd_max);
793 th->th_ack = htonl(tp->rcv_nxt);
794 if (optlen) {
795 bcopy((caddr_t)opt, (caddr_t)(th + 1), optlen);
796 th->th_off = (sizeof (struct tcphdr) + optlen) >> 2;
797 }
798 th->th_flags = flags;
799 /*
800 * Calculate receive window. Don't shrink window,
801 * but avoid silly window syndrome.
802 */
803 if (win < (long)(so->so_rcv.sb_hiwat / 4) && win < (long)rxsegsize)
804 win = 0;
805 if (win > (long)TCP_MAXWIN << tp->rcv_scale)
806 win = (long)TCP_MAXWIN << tp->rcv_scale;
807 if (win < (long)(tp->rcv_adv - tp->rcv_nxt))
808 win = (long)(tp->rcv_adv - tp->rcv_nxt);
809 th->th_win = htons((u_int16_t) (win>>tp->rcv_scale));
810 if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
811 u_int32_t urp = tp->snd_up - tp->snd_nxt;
812 if (urp > IP_MAXPACKET)
813 urp = IP_MAXPACKET;
814 th->th_urp = htons((u_int16_t)urp);
815 th->th_flags |= TH_URG;
816 } else
817 /*
818 * If no urgent pointer to send, then we pull
819 * the urgent pointer to the left edge of the send window
820 * so that it doesn't drift into the send window on sequence
821 * number wraparound.
822 */
823 tp->snd_up = tp->snd_una; /* drag it along */
824
825 /*
826 * Put TCP length in extended header, and then
827 * checksum extended header and data.
828 */
829 switch (af) {
830 case AF_INET:
831 {
832 struct ipovly *ipov = (struct ipovly *)ip;
833 if (len + optlen)
834 ipov->ih_len = htons((u_int16_t)(sizeof(struct tcphdr) +
835 optlen + len));
836 bzero(ipov->ih_x1, sizeof(ipov->ih_x1));
837 th->th_sum = 0;
838 th->th_sum = in_cksum(m, (int)(hdrlen + len));
839 break;
840 }
841 #ifdef INET6
842 case AF_INET6:
843 /* equals to hdrlen + len */
844 m->m_pkthdr.len = sizeof(struct ip6_hdr)
845 + sizeof(struct tcphdr) + optlen + len;
846 th->th_sum = 0;
847 th->th_sum = in6_cksum(m, IPPROTO_TCP,
848 sizeof(struct ip6_hdr),
849 sizeof(struct tcphdr) + optlen + len);
850 break;
851 #endif
852 }
853
854 /*
855 * In transmit state, time the transmission and arrange for
856 * the retransmit. In persist state, just set snd_max.
857 */
858 if (tp->t_force == 0 || TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0) {
859 tcp_seq startseq = tp->snd_nxt;
860
861 /*
862 * Advance snd_nxt over sequence space of this segment.
863 * There are no states in which we send both a SYN and a FIN,
864 * so we collapse the tests for these flags.
865 */
866 if (flags & (TH_SYN|TH_FIN))
867 tp->snd_nxt++;
868 tp->snd_nxt += len;
869 if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
870 tp->snd_max = tp->snd_nxt;
871 /*
872 * Time this transmission if not a retransmission and
873 * not currently timing anything.
874 */
875 if (tp->t_rtt == 0) {
876 tp->t_rtt = 1;
877 tp->t_rtseq = startseq;
878 tcpstat.tcps_segstimed++;
879 }
880 }
881
882 /*
883 * Set retransmit timer if not currently set,
884 * and not doing an ack or a keep-alive probe.
885 * Initial value for retransmit timer is smoothed
886 * round-trip time + 2 * round-trip time variance.
887 * Initialize shift counter which is used for backoff
888 * of retransmit time.
889 */
890 if (TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0 &&
891 tp->snd_nxt != tp->snd_una) {
892 TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur);
893 if (TCP_TIMER_ISARMED(tp, TCPT_PERSIST)) {
894 TCP_TIMER_DISARM(tp, TCPT_PERSIST);
895 tp->t_rxtshift = 0;
896 }
897 }
898 } else
899 if (SEQ_GT(tp->snd_nxt + len, tp->snd_max))
900 tp->snd_max = tp->snd_nxt + len;
901
902 /*
903 * Trace.
904 */
905 if (so->so_options & SO_DEBUG) {
906 /*
907 * need to recover version # field, which was overwritten
908 * on ip_cksum computation.
909 */
910 struct ip *sip;
911 sip = mtod(m, struct ip *);
912 switch (af) {
913 case AF_INET:
914 sip->ip_v = 4;
915 break;
916 #ifdef INET6
917 case AF_INET6:
918 sip->ip_v = 6;
919 break;
920 #endif
921 }
922 tcp_trace(TA_OUTPUT, tp->t_state, tp, m, 0);
923 }
924
925 /*
926 * Fill in IP length and desired time to live and
927 * send to IP level. There should be a better way
928 * to handle ttl and tos; we could keep them in
929 * the template, but need a way to checksum without them.
930 */
931 m->m_pkthdr.len = hdrlen + len;
932
933 switch (af) {
934 case AF_INET:
935 ip->ip_len = m->m_pkthdr.len;
936 if (tp->t_inpcb) {
937 ip->ip_ttl = tp->t_inpcb->inp_ip.ip_ttl;
938 ip->ip_tos = tp->t_inpcb->inp_ip.ip_tos;
939 }
940 #ifdef INET6
941 else if (tp->t_in6pcb) {
942 ip->ip_ttl = tp->t_in6pcb->in6p_ip6.ip6_hlim;
943 ip->ip_tos = 0; /*XXX*/
944 }
945 #endif
946 break;
947 #ifdef INET6
948 case AF_INET6:
949 ip6->ip6_nxt = IPPROTO_TCP;
950 if (tp->t_in6pcb) {
951 /*
952 * we separately set hoplimit for every segment, since
953 * the user might want to change the value via
954 * setsockopt. Also, desired default hop limit might
955 * be changed via Neighbor Discovery.
956 */
957 ip6->ip6_hlim = in6_selecthlim(tp->t_in6pcb,
958 ro->ro_rt ? ro->ro_rt->rt_ifp : NULL);
959 }
960 /* ip6->ip6_flow = ??? */
961 /* ip6_plen will be filled in ip6_output(). */
962 break;
963 #endif
964 }
965
966 #ifdef IPSEC
967 ipsec_setsocket(m, so);
968 #endif /*IPSEC*/
969
970 switch (af) {
971 case AF_INET:
972 {
973 struct mbuf *opts;
974
975 if (tp->t_inpcb)
976 opts = tp->t_inpcb->inp_options;
977 else
978 opts = NULL;
979 error = ip_output(m, opts, ro,
980 (ip_mtudisc ? IP_MTUDISC : 0) |
981 (so->so_options & SO_DONTROUTE),
982 0);
983 break;
984 }
985 #ifdef INET6
986 case AF_INET6:
987 {
988 struct ip6_pktopts *opts;
989
990 if (tp->t_in6pcb)
991 opts = tp->t_in6pcb->in6p_outputopts;
992 else
993 opts = NULL;
994 error = ip6_output(m, opts, (struct route_in6 *)ro,
995 so->so_options & SO_DONTROUTE, 0, NULL);
996 break;
997 }
998 #endif
999 default:
1000 error = EAFNOSUPPORT;
1001 break;
1002 }
1003 if (error) {
1004 out:
1005 if (error == ENOBUFS) {
1006 if (tp->t_inpcb)
1007 tcp_quench(tp->t_inpcb, 0);
1008 #ifdef INET6
1009 else if (tp->t_in6pcb)
1010 tcp6_quench(tp->t_in6pcb, 0);
1011 #endif
1012 return (0);
1013 }
1014 if ((error == EHOSTUNREACH || error == ENETDOWN)
1015 && TCPS_HAVERCVDSYN(tp->t_state)) {
1016 tp->t_softerror = error;
1017 return (0);
1018 }
1019 return (error);
1020 }
1021 tcpstat.tcps_sndtotal++;
1022 if (tp->t_flags & TF_DELACK)
1023 tcpstat.tcps_delack++;
1024
1025 /*
1026 * Data sent (as far as we can tell).
1027 * If this advertises a larger window than any other segment,
1028 * then remember the size of the advertised window.
1029 * Any pending ACK has now been sent.
1030 */
1031 if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv))
1032 tp->rcv_adv = tp->rcv_nxt + win;
1033 tp->last_ack_sent = tp->rcv_nxt;
1034 tp->t_flags &= ~TF_ACKNOW;
1035 TCP_CLEAR_DELACK(tp);
1036 #ifdef DIAGNOSTIC
1037 if (maxburst < 0)
1038 printf("tcp_output: maxburst exceeded by %d\n", -maxburst);
1039 #endif
1040 if (sendalot && (!tcp_do_newreno || --maxburst))
1041 goto again;
1042 return (0);
1043 }
1044
1045 void
1046 tcp_setpersist(tp)
1047 struct tcpcb *tp;
1048 {
1049 int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> (1 + 2);
1050 int nticks;
1051
1052 if (TCP_TIMER_ISARMED(tp, TCPT_REXMT))
1053 panic("tcp_output REXMT");
1054 /*
1055 * Start/restart persistance timer.
1056 */
1057 if (t < tp->t_rttmin)
1058 t = tp->t_rttmin;
1059 TCPT_RANGESET(nticks, t * tcp_backoff[tp->t_rxtshift],
1060 TCPTV_PERSMIN, TCPTV_PERSMAX);
1061 TCP_TIMER_ARM(tp, TCPT_PERSIST, nticks);
1062 if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
1063 tp->t_rxtshift++;
1064 }
1065