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