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