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