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