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