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