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