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