tcp_output.c revision 1.173.8.1 1 /* $NetBSD: tcp_output.c,v 1.173.8.1 2014/11/03 23:05:59 msaitoh 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, 2005, 2006 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 * This code is derived from software contributed to The NetBSD Foundation
80 * by Charles M. Hannum.
81 * This code is derived from software contributed to The NetBSD Foundation
82 * by Rui Paulo.
83 *
84 * Redistribution and use in source and binary forms, with or without
85 * modification, are permitted provided that the following conditions
86 * are met:
87 * 1. Redistributions of source code must retain the above copyright
88 * notice, this list of conditions and the following disclaimer.
89 * 2. Redistributions in binary form must reproduce the above copyright
90 * notice, this list of conditions and the following disclaimer in the
91 * documentation and/or other materials provided with the distribution.
92 *
93 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
94 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
95 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
96 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
97 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
98 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
99 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
100 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
101 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
103 * POSSIBILITY OF SUCH DAMAGE.
104 */
105
106 /*
107 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
108 * The Regents of the University of California. All rights reserved.
109 *
110 * Redistribution and use in source and binary forms, with or without
111 * modification, are permitted provided that the following conditions
112 * are met:
113 * 1. Redistributions of source code must retain the above copyright
114 * notice, this list of conditions and the following disclaimer.
115 * 2. Redistributions in binary form must reproduce the above copyright
116 * notice, this list of conditions and the following disclaimer in the
117 * documentation and/or other materials provided with the distribution.
118 * 3. Neither the name of the University nor the names of its contributors
119 * may be used to endorse or promote products derived from this software
120 * without specific prior written permission.
121 *
122 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
123 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
124 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
125 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
126 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
127 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
128 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
129 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
130 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
131 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
132 * SUCH DAMAGE.
133 *
134 * @(#)tcp_output.c 8.4 (Berkeley) 5/24/95
135 */
136
137 #include <sys/cdefs.h>
138 __KERNEL_RCSID(0, "$NetBSD: tcp_output.c,v 1.173.8.1 2014/11/03 23:05:59 msaitoh Exp $");
139
140 #include "opt_inet.h"
141 #include "opt_ipsec.h"
142 #include "opt_tcp_debug.h"
143
144 #include <sys/param.h>
145 #include <sys/systm.h>
146 #include <sys/malloc.h>
147 #include <sys/mbuf.h>
148 #include <sys/protosw.h>
149 #include <sys/socket.h>
150 #include <sys/socketvar.h>
151 #include <sys/errno.h>
152 #include <sys/domain.h>
153 #include <sys/kernel.h>
154 #ifdef TCP_SIGNATURE
155 #include <sys/md5.h>
156 #endif
157
158 #include <net/if.h>
159 #include <net/route.h>
160
161 #include <netinet/in.h>
162 #include <netinet/in_systm.h>
163 #include <netinet/ip.h>
164 #include <netinet/in_pcb.h>
165 #include <netinet/ip_var.h>
166
167 #ifdef INET6
168 #ifndef INET
169 #include <netinet/in.h>
170 #endif
171 #include <netinet/ip6.h>
172 #include <netinet6/in6_var.h>
173 #include <netinet6/ip6_var.h>
174 #include <netinet6/in6_pcb.h>
175 #include <netinet6/nd6.h>
176 #endif
177
178 #ifdef FAST_IPSEC
179 #include <netipsec/ipsec.h>
180 #include <netipsec/key.h>
181 #ifdef INET6
182 #include <netipsec/ipsec6.h>
183 #endif
184 #endif /* FAST_IPSEC*/
185 #ifdef KAME_IPSEC
186 #include <netinet6/ipsec.h>
187 #endif
188
189 #include <netinet/tcp.h>
190 #define TCPOUTFLAGS
191 #include <netinet/tcp_fsm.h>
192 #include <netinet/tcp_seq.h>
193 #include <netinet/tcp_timer.h>
194 #include <netinet/tcp_var.h>
195 #include <netinet/tcp_private.h>
196 #include <netinet/tcp_congctl.h>
197 #include <netinet/tcpip.h>
198 #include <netinet/tcp_debug.h>
199 #include <netinet/in_offload.h>
200 #include <netinet6/in6_offload.h>
201
202 #ifdef KAME_IPSEC
203 #include <netkey/key.h>
204 #endif
205
206 #ifdef notyet
207 extern struct mbuf *m_copypack();
208 #endif
209
210 /*
211 * Knob to enable Congestion Window Monitoring, and control
212 * the burst size it allows. Default burst is 4 packets, per
213 * the Internet draft.
214 */
215 int tcp_cwm = 0;
216 int tcp_cwm_burstsize = 4;
217
218 int tcp_do_autosndbuf = 1;
219 int tcp_autosndbuf_inc = 8 * 1024;
220 int tcp_autosndbuf_max = 256 * 1024;
221
222 #ifdef TCP_OUTPUT_COUNTERS
223 #include <sys/device.h>
224
225 extern struct evcnt tcp_output_bigheader;
226 extern struct evcnt tcp_output_predict_hit;
227 extern struct evcnt tcp_output_predict_miss;
228 extern struct evcnt tcp_output_copysmall;
229 extern struct evcnt tcp_output_copybig;
230 extern struct evcnt tcp_output_refbig;
231
232 #define TCP_OUTPUT_COUNTER_INCR(ev) (ev)->ev_count++
233 #else
234
235 #define TCP_OUTPUT_COUNTER_INCR(ev) /* nothing */
236
237 #endif /* TCP_OUTPUT_COUNTERS */
238
239 static
240 #ifndef GPROF
241 inline
242 #endif
243 int
244 tcp_segsize(struct tcpcb *tp, int *txsegsizep, int *rxsegsizep,
245 bool *alwaysfragp)
246 {
247 #ifdef INET
248 struct inpcb *inp = tp->t_inpcb;
249 #endif
250 #ifdef INET6
251 struct in6pcb *in6p = tp->t_in6pcb;
252 #endif
253 struct socket *so = NULL;
254 struct rtentry *rt;
255 struct ifnet *ifp;
256 int size;
257 int hdrlen;
258 int optlen;
259
260 *alwaysfragp = false;
261
262 #ifdef DIAGNOSTIC
263 if (tp->t_inpcb && tp->t_in6pcb)
264 panic("tcp_segsize: both t_inpcb and t_in6pcb are set");
265 #endif
266 switch (tp->t_family) {
267 #ifdef INET
268 case AF_INET:
269 hdrlen = sizeof(struct ip) + sizeof(struct tcphdr);
270 break;
271 #endif
272 #ifdef INET6
273 case AF_INET6:
274 hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
275 break;
276 #endif
277 default:
278 size = tcp_mssdflt;
279 goto out;
280 }
281
282 rt = NULL;
283 #ifdef INET
284 if (inp) {
285 rt = in_pcbrtentry(inp);
286 so = inp->inp_socket;
287 }
288 #endif
289 #ifdef INET6
290 if (in6p) {
291 rt = in6_pcbrtentry(in6p);
292 so = in6p->in6p_socket;
293 }
294 #endif
295 if (rt == NULL) {
296 size = tcp_mssdflt;
297 goto out;
298 }
299
300 ifp = rt->rt_ifp;
301
302 size = tcp_mssdflt;
303 if (tp->t_mtudisc && rt->rt_rmx.rmx_mtu != 0) {
304 #ifdef INET6
305 if (in6p && rt->rt_rmx.rmx_mtu < IPV6_MMTU) {
306 /*
307 * RFC2460 section 5, last paragraph: if path MTU is
308 * smaller than 1280, use 1280 as packet size and
309 * attach fragment header.
310 */
311 size = IPV6_MMTU - hdrlen - sizeof(struct ip6_frag);
312 *alwaysfragp = true;
313 } else
314 size = rt->rt_rmx.rmx_mtu - hdrlen;
315 #else
316 size = rt->rt_rmx.rmx_mtu - hdrlen;
317 #endif
318 } else if (ifp->if_flags & IFF_LOOPBACK)
319 size = ifp->if_mtu - hdrlen;
320 #ifdef INET
321 else if (inp && tp->t_mtudisc)
322 size = ifp->if_mtu - hdrlen;
323 else if (inp && in_localaddr(inp->inp_faddr))
324 size = ifp->if_mtu - hdrlen;
325 #endif
326 #ifdef INET6
327 else if (in6p) {
328 #ifdef INET
329 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)) {
330 /* mapped addr case */
331 struct in_addr d;
332 bcopy(&in6p->in6p_faddr.s6_addr32[3], &d, sizeof(d));
333 if (tp->t_mtudisc || in_localaddr(d))
334 size = ifp->if_mtu - hdrlen;
335 } else
336 #endif
337 {
338 /*
339 * for IPv6, path MTU discovery is always turned on,
340 * or the node must use packet size <= 1280.
341 */
342 size = tp->t_mtudisc ? IN6_LINKMTU(ifp) : IPV6_MMTU;
343 size -= hdrlen;
344 }
345 }
346 #endif
347 out:
348 /*
349 * Now we must make room for whatever extra TCP/IP options are in
350 * the packet.
351 */
352 optlen = tcp_optlen(tp);
353
354 /*
355 * XXX tp->t_ourmss should have the right size, but without this code
356 * fragmentation will occur... need more investigation
357 */
358 #ifdef INET
359 if (inp) {
360 #if defined(KAME_IPSEC) || defined(FAST_IPSEC)
361 if (! IPSEC_PCB_SKIP_IPSEC(inp->inp_sp, IPSEC_DIR_OUTBOUND))
362 optlen += ipsec4_hdrsiz_tcp(tp);
363 #endif
364 optlen += ip_optlen(inp);
365 }
366 #endif
367 #ifdef INET6
368 #ifdef INET
369 if (in6p && tp->t_family == AF_INET) {
370 #if defined(KAME_IPSEC) || defined(FAST_IPSEC)
371 if (! IPSEC_PCB_SKIP_IPSEC(in6p->in6p_sp, IPSEC_DIR_OUTBOUND))
372 optlen += ipsec4_hdrsiz_tcp(tp);
373 #endif
374 /* XXX size -= ip_optlen(in6p); */
375 } else
376 #endif
377 if (in6p && tp->t_family == AF_INET6) {
378 #if defined(KAME_IPSEC) || defined(FAST_IPSEC)
379 if (! IPSEC_PCB_SKIP_IPSEC(in6p->in6p_sp, IPSEC_DIR_OUTBOUND))
380 optlen += ipsec6_hdrsiz_tcp(tp);
381 #endif
382 optlen += ip6_optlen(in6p);
383 }
384 #endif
385 size -= optlen;
386
387 /* there may not be any room for data if mtu is too small */
388 if (size < 0)
389 return (EMSGSIZE);
390
391 /*
392 * *rxsegsizep holds *estimated* inbound segment size (estimation
393 * assumes that path MTU is the same for both ways). this is only
394 * for silly window avoidance, do not use the value for other purposes.
395 *
396 * ipseclen is subtracted from both sides, this may not be right.
397 * I'm not quite sure about this (could someone comment).
398 */
399 *txsegsizep = min(tp->t_peermss - optlen, size);
400 /*
401 * Never send more than half a buffer full. This insures that we can
402 * always keep 2 packets on the wire, no matter what SO_SNDBUF is, and
403 * therefore acks will never be delayed unless we run out of data to
404 * transmit.
405 */
406 if (so)
407 *txsegsizep = min(so->so_snd.sb_hiwat >> 1, *txsegsizep);
408 *rxsegsizep = min(tp->t_ourmss - optlen, size);
409
410 if (*txsegsizep != tp->t_segsz) {
411 /*
412 * If the new segment size is larger, we don't want to
413 * mess up the congestion window, but if it is smaller
414 * we'll have to reduce the congestion window to ensure
415 * that we don't get into trouble with initial windows
416 * and the rest. In any case, if the segment size
417 * has changed, chances are the path has, too, and
418 * our congestion window will be different.
419 */
420 if (*txsegsizep < tp->t_segsz) {
421 tp->snd_cwnd = max((tp->snd_cwnd / tp->t_segsz)
422 * *txsegsizep, *txsegsizep);
423 tp->snd_ssthresh = max((tp->snd_ssthresh / tp->t_segsz)
424 * *txsegsizep, *txsegsizep);
425 }
426 tp->t_segsz = *txsegsizep;
427 }
428
429 return (0);
430 }
431
432 static
433 #ifndef GPROF
434 inline
435 #endif
436 int
437 tcp_build_datapkt(struct tcpcb *tp, struct socket *so, int off,
438 long len, int hdrlen, struct mbuf **mp)
439 {
440 struct mbuf *m, *m0;
441 uint64_t *tcps;
442
443 tcps = TCP_STAT_GETREF();
444 if (tp->t_force && len == 1)
445 tcps[TCP_STAT_SNDPROBE]++;
446 else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
447 tcps[TCP_STAT_SNDREXMITPACK]++;
448 tcps[TCP_STAT_SNDREXMITBYTE] += len;
449 } else {
450 tcps[TCP_STAT_SNDPACK]++;
451 tcps[TCP_STAT_SNDBYTE] += len;
452 }
453 TCP_STAT_PUTREF();
454 #ifdef notyet
455 if ((m = m_copypack(so->so_snd.sb_mb, off,
456 (int)len, max_linkhdr + hdrlen)) == 0)
457 return (ENOBUFS);
458 /*
459 * m_copypack left space for our hdr; use it.
460 */
461 m->m_len += hdrlen;
462 m->m_data -= hdrlen;
463 #else
464 MGETHDR(m, M_DONTWAIT, MT_HEADER);
465 if (__predict_false(m == NULL))
466 return (ENOBUFS);
467 MCLAIM(m, &tcp_tx_mowner);
468
469 /*
470 * XXX Because other code assumes headers will fit in
471 * XXX one header mbuf.
472 *
473 * (This code should almost *never* be run.)
474 */
475 if (__predict_false((max_linkhdr + hdrlen) > MHLEN)) {
476 TCP_OUTPUT_COUNTER_INCR(&tcp_output_bigheader);
477 MCLGET(m, M_DONTWAIT);
478 if ((m->m_flags & M_EXT) == 0) {
479 m_freem(m);
480 return (ENOBUFS);
481 }
482 }
483
484 m->m_data += max_linkhdr;
485 m->m_len = hdrlen;
486
487 /*
488 * To avoid traversing the whole sb_mb chain for correct
489 * data to send, remember last sent mbuf, its offset and
490 * the sent size. When called the next time, see if the
491 * data to send is directly following the previous transfer.
492 * This is important for large TCP windows.
493 */
494 if (off == 0 || tp->t_lastm == NULL ||
495 (tp->t_lastoff + tp->t_lastlen) != off) {
496 TCP_OUTPUT_COUNTER_INCR(&tcp_output_predict_miss);
497 /*
498 * Either a new packet or a retransmit.
499 * Start from the beginning.
500 */
501 tp->t_lastm = so->so_snd.sb_mb;
502 tp->t_inoff = off;
503 } else {
504 TCP_OUTPUT_COUNTER_INCR(&tcp_output_predict_hit);
505 tp->t_inoff += tp->t_lastlen;
506 }
507
508 /* Traverse forward to next packet */
509 while (tp->t_inoff > 0) {
510 if (tp->t_lastm == NULL)
511 panic("tp->t_lastm == NULL");
512 if (tp->t_inoff < tp->t_lastm->m_len)
513 break;
514 tp->t_inoff -= tp->t_lastm->m_len;
515 tp->t_lastm = tp->t_lastm->m_next;
516 }
517
518 tp->t_lastoff = off;
519 tp->t_lastlen = len;
520 m0 = tp->t_lastm;
521 off = tp->t_inoff;
522
523 if (len <= M_TRAILINGSPACE(m)) {
524 m_copydata(m0, off, (int) len, mtod(m, char *) + hdrlen);
525 m->m_len += len;
526 TCP_OUTPUT_COUNTER_INCR(&tcp_output_copysmall);
527 } else {
528 m->m_next = m_copym(m0, off, (int) len, M_DONTWAIT);
529 if (m->m_next == NULL) {
530 m_freem(m);
531 return (ENOBUFS);
532 }
533 #ifdef TCP_OUTPUT_COUNTERS
534 if (m->m_next->m_flags & M_EXT)
535 TCP_OUTPUT_COUNTER_INCR(&tcp_output_refbig);
536 else
537 TCP_OUTPUT_COUNTER_INCR(&tcp_output_copybig);
538 #endif /* TCP_OUTPUT_COUNTERS */
539 }
540 #endif
541
542 *mp = m;
543 return (0);
544 }
545
546 /*
547 * Tcp output routine: figure out what should be sent and send it.
548 */
549 int
550 tcp_output(struct tcpcb *tp)
551 {
552 struct rtentry *rt;
553 struct socket *so;
554 struct route *ro;
555 long len, win;
556 int off, flags, error;
557 struct mbuf *m;
558 struct ip *ip;
559 #ifdef INET6
560 struct ip6_hdr *ip6;
561 #endif
562 struct tcphdr *th;
563 u_char opt[MAX_TCPOPTLEN];
564 #define OPT_FITS(more) ((optlen + (more)) < sizeof(opt))
565 unsigned optlen, hdrlen, packetlen;
566 unsigned int sack_numblks;
567 int idle, sendalot, txsegsize, rxsegsize;
568 int txsegsize_nosack;
569 int maxburst = TCP_MAXBURST;
570 int af; /* address family on the wire */
571 int iphdrlen;
572 int has_tso4, has_tso6;
573 int has_tso, use_tso;
574 bool alwaysfrag;
575 int sack_rxmit;
576 int sack_bytes_rxmt;
577 int ecn_tos;
578 struct sackhole *p;
579 #ifdef TCP_SIGNATURE
580 int sigoff = 0;
581 #endif
582 uint64_t *tcps;
583
584 #ifdef DIAGNOSTIC
585 if (tp->t_inpcb && tp->t_in6pcb)
586 panic("tcp_output: both t_inpcb and t_in6pcb are set");
587 #endif
588 so = NULL;
589 ro = NULL;
590 if (tp->t_inpcb) {
591 so = tp->t_inpcb->inp_socket;
592 ro = &tp->t_inpcb->inp_route;
593 }
594 #ifdef INET6
595 else if (tp->t_in6pcb) {
596 so = tp->t_in6pcb->in6p_socket;
597 ro = &tp->t_in6pcb->in6p_route;
598 }
599 #endif
600
601 switch (af = tp->t_family) {
602 #ifdef INET
603 case AF_INET:
604 if (tp->t_inpcb)
605 break;
606 #ifdef INET6
607 /* mapped addr case */
608 if (tp->t_in6pcb)
609 break;
610 #endif
611 return (EINVAL);
612 #endif
613 #ifdef INET6
614 case AF_INET6:
615 if (tp->t_in6pcb)
616 break;
617 return (EINVAL);
618 #endif
619 default:
620 return (EAFNOSUPPORT);
621 }
622
623 if (tcp_segsize(tp, &txsegsize, &rxsegsize, &alwaysfrag))
624 return (EMSGSIZE);
625
626 idle = (tp->snd_max == tp->snd_una);
627
628 /*
629 * Determine if we can use TCP segmentation offload:
630 * - If we're using IPv4
631 * - If there is not an IPsec policy that prevents it
632 * - If the interface can do it
633 */
634 has_tso4 = has_tso6 = false;
635 #if defined(INET)
636 has_tso4 = tp->t_inpcb != NULL &&
637 #if defined(KAME_IPSEC) || defined(FAST_IPSEC)
638 IPSEC_PCB_SKIP_IPSEC(tp->t_inpcb->inp_sp,
639 IPSEC_DIR_OUTBOUND) &&
640 #endif
641 (rt = rtcache_validate(&tp->t_inpcb->inp_route)) != NULL &&
642 (rt->rt_ifp->if_capenable & IFCAP_TSOv4) != 0;
643 #endif /* defined(INET) */
644 #if defined(INET6)
645 has_tso6 = tp->t_in6pcb != NULL &&
646 #if defined(KAME_IPSEC) || defined(FAST_IPSEC)
647 IPSEC_PCB_SKIP_IPSEC(tp->t_in6pcb->in6p_sp,
648 IPSEC_DIR_OUTBOUND) &&
649 #endif
650 (rt = rtcache_validate(&tp->t_in6pcb->in6p_route)) != NULL &&
651 (rt->rt_ifp->if_capenable & IFCAP_TSOv6) != 0;
652 #endif /* defined(INET6) */
653 has_tso = (has_tso4 || has_tso6) && !alwaysfrag;
654
655 /*
656 * Restart Window computation. From draft-floyd-incr-init-win-03:
657 *
658 * Optionally, a TCP MAY set the restart window to the
659 * minimum of the value used for the initial window and
660 * the current value of cwnd (in other words, using a
661 * larger value for the restart window should never increase
662 * the size of cwnd).
663 */
664 if (tcp_cwm) {
665 /*
666 * Hughes/Touch/Heidemann Congestion Window Monitoring.
667 * Count the number of packets currently pending
668 * acknowledgement, and limit our congestion window
669 * to a pre-determined allowed burst size plus that count.
670 * This prevents bursting once all pending packets have
671 * been acknowledged (i.e. transmission is idle).
672 *
673 * XXX Link this to Initial Window?
674 */
675 tp->snd_cwnd = min(tp->snd_cwnd,
676 (tcp_cwm_burstsize * txsegsize) +
677 (tp->snd_nxt - tp->snd_una));
678 } else {
679 if (idle && (tcp_now - tp->t_rcvtime) >= tp->t_rxtcur) {
680 /*
681 * We have been idle for "a while" and no acks are
682 * expected to clock out any data we send --
683 * slow start to get ack "clock" running again.
684 */
685 int ss = tcp_init_win;
686 #ifdef INET
687 if (tp->t_inpcb &&
688 in_localaddr(tp->t_inpcb->inp_faddr))
689 ss = tcp_init_win_local;
690 #endif
691 #ifdef INET6
692 if (tp->t_in6pcb &&
693 in6_localaddr(&tp->t_in6pcb->in6p_faddr))
694 ss = tcp_init_win_local;
695 #endif
696 tp->snd_cwnd = min(tp->snd_cwnd,
697 TCP_INITIAL_WINDOW(ss, txsegsize));
698 }
699 }
700
701 txsegsize_nosack = txsegsize;
702 again:
703 ecn_tos = 0;
704 use_tso = has_tso;
705 if ((tp->t_flags & (TF_ECN_SND_CWR|TF_ECN_SND_ECE)) != 0) {
706 /* don't duplicate CWR/ECE. */
707 use_tso = 0;
708 }
709 TCP_REASS_LOCK(tp);
710 sack_numblks = tcp_sack_numblks(tp);
711 if (sack_numblks) {
712 int sackoptlen;
713
714 sackoptlen = TCP_SACK_OPTLEN(sack_numblks);
715 if (sackoptlen > txsegsize_nosack) {
716 sack_numblks = 0; /* give up SACK */
717 txsegsize = txsegsize_nosack;
718 } else {
719 if ((tp->rcv_sack_flags & TCPSACK_HAVED) != 0) {
720 /* don't duplicate D-SACK. */
721 use_tso = 0;
722 }
723 txsegsize = txsegsize_nosack - sackoptlen;
724 }
725 } else {
726 txsegsize = txsegsize_nosack;
727 }
728
729 /*
730 * Determine length of data that should be transmitted, and
731 * flags that should be used. If there is some data or critical
732 * controls (SYN, RST) to send, then transmit; otherwise,
733 * investigate further.
734 *
735 * Readjust SACK information to avoid resending duplicate data.
736 */
737 if (TCP_SACK_ENABLED(tp) && SEQ_LT(tp->snd_nxt, tp->snd_max))
738 tcp_sack_adjust(tp);
739 sendalot = 0;
740 off = tp->snd_nxt - tp->snd_una;
741 win = min(tp->snd_wnd, tp->snd_cwnd);
742
743 flags = tcp_outflags[tp->t_state];
744
745 /*
746 * Send any SACK-generated retransmissions. If we're explicitly trying
747 * to send out new data (when sendalot is 1), bypass this function.
748 * If we retransmit in fast recovery mode, decrement snd_cwnd, since
749 * we're replacing a (future) new transmission with a retransmission
750 * now, and we previously incremented snd_cwnd in tcp_input().
751 */
752 /*
753 * Still in sack recovery , reset rxmit flag to zero.
754 */
755 sack_rxmit = 0;
756 sack_bytes_rxmt = 0;
757 len = 0;
758 p = NULL;
759 do {
760 long cwin;
761 if (!TCP_SACK_ENABLED(tp))
762 break;
763 if (tp->t_partialacks < 0)
764 break;
765 p = tcp_sack_output(tp, &sack_bytes_rxmt);
766 if (p == NULL)
767 break;
768
769 cwin = min(tp->snd_wnd, tp->snd_cwnd) - sack_bytes_rxmt;
770 if (cwin < 0)
771 cwin = 0;
772 /* Do not retransmit SACK segments beyond snd_recover */
773 if (SEQ_GT(p->end, tp->snd_recover)) {
774 /*
775 * (At least) part of sack hole extends beyond
776 * snd_recover. Check to see if we can rexmit data
777 * for this hole.
778 */
779 if (SEQ_GEQ(p->rxmit, tp->snd_recover)) {
780 /*
781 * Can't rexmit any more data for this hole.
782 * That data will be rexmitted in the next
783 * sack recovery episode, when snd_recover
784 * moves past p->rxmit.
785 */
786 p = NULL;
787 break;
788 }
789 /* Can rexmit part of the current hole */
790 len = ((long)ulmin(cwin, tp->snd_recover - p->rxmit));
791 } else
792 len = ((long)ulmin(cwin, p->end - p->rxmit));
793 off = p->rxmit - tp->snd_una;
794 if (off + len > so->so_snd.sb_cc) {
795 /* 1 for TH_FIN */
796 KASSERT(off + len == so->so_snd.sb_cc + 1);
797 KASSERT(p->rxmit + len == tp->snd_max);
798 len = so->so_snd.sb_cc - off;
799 }
800 if (len > 0) {
801 sack_rxmit = 1;
802 sendalot = 1;
803 }
804 } while (/*CONSTCOND*/0);
805
806 /*
807 * If in persist timeout with window of 0, send 1 byte.
808 * Otherwise, if window is small but nonzero
809 * and timer expired, we will send what we can
810 * and go to transmit state.
811 */
812 if (tp->t_force) {
813 if (win == 0) {
814 /*
815 * If we still have some data to send, then
816 * clear the FIN bit. Usually this would
817 * happen below when it realizes that we
818 * aren't sending all the data. However,
819 * if we have exactly 1 byte of unset data,
820 * then it won't clear the FIN bit below,
821 * and if we are in persist state, we wind
822 * up sending the packet without recording
823 * that we sent the FIN bit.
824 *
825 * We can't just blindly clear the FIN bit,
826 * because if we don't have any more data
827 * to send then the probe will be the FIN
828 * itself.
829 */
830 if (off < so->so_snd.sb_cc)
831 flags &= ~TH_FIN;
832 win = 1;
833 } else {
834 TCP_TIMER_DISARM(tp, TCPT_PERSIST);
835 tp->t_rxtshift = 0;
836 }
837 }
838
839 if (sack_rxmit == 0) {
840 if (TCP_SACK_ENABLED(tp) && tp->t_partialacks >= 0) {
841 long cwin;
842
843 /*
844 * We are inside of a SACK recovery episode and are
845 * sending new data, having retransmitted all the
846 * data possible in the scoreboard.
847 */
848 if (tp->snd_wnd < so->so_snd.sb_cc) {
849 len = tp->snd_wnd - off;
850 flags &= ~TH_FIN;
851 } else {
852 len = so->so_snd.sb_cc - off;
853 }
854
855 /*
856 * From FreeBSD:
857 * Don't remove this (len > 0) check !
858 * We explicitly check for len > 0 here (although it
859 * isn't really necessary), to work around a gcc
860 * optimization issue - to force gcc to compute
861 * len above. Without this check, the computation
862 * of len is bungled by the optimizer.
863 */
864 if (len > 0) {
865 cwin = tp->snd_cwnd -
866 (tp->snd_nxt - tp->sack_newdata) -
867 sack_bytes_rxmt;
868 if (cwin < 0)
869 cwin = 0;
870 if (cwin < len) {
871 len = cwin;
872 flags &= ~TH_FIN;
873 }
874 }
875 } else if (win < so->so_snd.sb_cc) {
876 len = win - off;
877 flags &= ~TH_FIN;
878 } else {
879 len = so->so_snd.sb_cc - off;
880 }
881 }
882
883 if (len < 0) {
884 /*
885 * If FIN has been sent but not acked,
886 * but we haven't been called to retransmit,
887 * len will be -1. Otherwise, window shrank
888 * after we sent into it. If window shrank to 0,
889 * cancel pending retransmit, pull snd_nxt back
890 * to (closed) window, and set the persist timer
891 * if it isn't already going. If the window didn't
892 * close completely, just wait for an ACK.
893 *
894 * If we have a pending FIN, either it has already been
895 * transmitted or it is outside the window, so drop it.
896 * If the FIN has been transmitted, but this is not a
897 * retransmission, then len must be -1. Therefore we also
898 * prevent here the sending of `gratuitous FINs'. This
899 * eliminates the need to check for that case below (e.g.
900 * to back up snd_nxt before the FIN so that the sequence
901 * number is correct).
902 */
903 len = 0;
904 flags &= ~TH_FIN;
905 if (win == 0) {
906 TCP_TIMER_DISARM(tp, TCPT_REXMT);
907 tp->t_rxtshift = 0;
908 tp->snd_nxt = tp->snd_una;
909 if (TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0)
910 tcp_setpersist(tp);
911 }
912 }
913
914 /*
915 * Automatic sizing enables the performance of large buffers
916 * and most of the efficiency of small ones by only allocating
917 * space when it is needed.
918 *
919 * The criteria to step up the send buffer one notch are:
920 * 1. receive window of remote host is larger than send buffer
921 * (with a fudge factor of 5/4th);
922 * 2. send buffer is filled to 7/8th with data (so we actually
923 * have data to make use of it);
924 * 3. send buffer fill has not hit maximal automatic size;
925 * 4. our send window (slow start and cogestion controlled) is
926 * larger than sent but unacknowledged data in send buffer.
927 *
928 * The remote host receive window scaling factor may limit the
929 * growing of the send buffer before it reaches its allowed
930 * maximum.
931 *
932 * It scales directly with slow start or congestion window
933 * and does at most one step per received ACK. This fast
934 * scaling has the drawback of growing the send buffer beyond
935 * what is strictly necessary to make full use of a given
936 * delay*bandwith product. However testing has shown this not
937 * to be much of an problem. At worst we are trading wasting
938 * of available bandwith (the non-use of it) for wasting some
939 * socket buffer memory.
940 *
941 * TODO: Shrink send buffer during idle periods together
942 * with congestion window. Requires another timer.
943 */
944 if (tcp_do_autosndbuf && so->so_snd.sb_flags & SB_AUTOSIZE) {
945 if ((tp->snd_wnd / 4 * 5) >= so->so_snd.sb_hiwat &&
946 so->so_snd.sb_cc >= (so->so_snd.sb_hiwat / 8 * 7) &&
947 so->so_snd.sb_cc < tcp_autosndbuf_max &&
948 win >= (so->so_snd.sb_cc - (tp->snd_nxt - tp->snd_una))) {
949 if (!sbreserve(&so->so_snd,
950 min(so->so_snd.sb_hiwat + tcp_autosndbuf_inc,
951 tcp_autosndbuf_max), so))
952 so->so_snd.sb_flags &= ~SB_AUTOSIZE;
953 }
954 }
955
956 if (len > txsegsize) {
957 if (use_tso) {
958 /*
959 * Truncate TSO transfers to IP_MAXPACKET, and make
960 * sure that we send equal size transfers down the
961 * stack (rather than big-small-big-small-...).
962 */
963 #ifdef INET6
964 CTASSERT(IPV6_MAXPACKET == IP_MAXPACKET);
965 #endif
966 len = (min(len, IP_MAXPACKET) / txsegsize) * txsegsize;
967 if (len <= txsegsize) {
968 use_tso = 0;
969 }
970 } else
971 len = txsegsize;
972 flags &= ~TH_FIN;
973 sendalot = 1;
974 } else
975 use_tso = 0;
976 if (sack_rxmit) {
977 if (SEQ_LT(p->rxmit + len, tp->snd_una + so->so_snd.sb_cc))
978 flags &= ~TH_FIN;
979 }
980
981 win = sbspace(&so->so_rcv);
982
983 /*
984 * Sender silly window avoidance. If connection is idle
985 * and can send all data, a maximum segment,
986 * at least a maximum default-size segment do it,
987 * or are forced, do it; otherwise don't bother.
988 * If peer's buffer is tiny, then send
989 * when window is at least half open.
990 * If retransmitting (possibly after persist timer forced us
991 * to send into a small window), then must resend.
992 */
993 if (len) {
994 if (len >= txsegsize)
995 goto send;
996 if ((so->so_state & SS_MORETOCOME) == 0 &&
997 ((idle || tp->t_flags & TF_NODELAY) &&
998 len + off >= so->so_snd.sb_cc))
999 goto send;
1000 if (tp->t_force)
1001 goto send;
1002 if (len >= tp->max_sndwnd / 2)
1003 goto send;
1004 if (SEQ_LT(tp->snd_nxt, tp->snd_max))
1005 goto send;
1006 if (sack_rxmit)
1007 goto send;
1008 }
1009
1010 /*
1011 * Compare available window to amount of window known to peer
1012 * (as advertised window less next expected input). If the
1013 * difference is at least twice the size of the largest segment
1014 * we expect to receive (i.e. two segments) or at least 50% of
1015 * the maximum possible window, then want to send a window update
1016 * to peer.
1017 */
1018 if (win > 0) {
1019 /*
1020 * "adv" is the amount we can increase the window,
1021 * taking into account that we are limited by
1022 * TCP_MAXWIN << tp->rcv_scale.
1023 */
1024 long adv = min(win, (long)TCP_MAXWIN << tp->rcv_scale) -
1025 (tp->rcv_adv - tp->rcv_nxt);
1026
1027 if (adv >= (long) (2 * rxsegsize))
1028 goto send;
1029 if (2 * adv >= (long) so->so_rcv.sb_hiwat)
1030 goto send;
1031 }
1032
1033 /*
1034 * Send if we owe peer an ACK.
1035 */
1036 if (tp->t_flags & TF_ACKNOW)
1037 goto send;
1038 if (flags & (TH_SYN|TH_FIN|TH_RST))
1039 goto send;
1040 if (SEQ_GT(tp->snd_up, tp->snd_una))
1041 goto send;
1042 /*
1043 * In SACK, it is possible for tcp_output to fail to send a segment
1044 * after the retransmission timer has been turned off. Make sure
1045 * that the retransmission timer is set.
1046 */
1047 if (TCP_SACK_ENABLED(tp) && SEQ_GT(tp->snd_max, tp->snd_una) &&
1048 !TCP_TIMER_ISARMED(tp, TCPT_REXMT) &&
1049 !TCP_TIMER_ISARMED(tp, TCPT_PERSIST)) {
1050 TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur);
1051 goto just_return;
1052 }
1053
1054 /*
1055 * TCP window updates are not reliable, rather a polling protocol
1056 * using ``persist'' packets is used to insure receipt of window
1057 * updates. The three ``states'' for the output side are:
1058 * idle not doing retransmits or persists
1059 * persisting to move a small or zero window
1060 * (re)transmitting and thereby not persisting
1061 *
1062 * tp->t_timer[TCPT_PERSIST]
1063 * is set when we are in persist state.
1064 * tp->t_force
1065 * is set when we are called to send a persist packet.
1066 * tp->t_timer[TCPT_REXMT]
1067 * is set when we are retransmitting
1068 * The output side is idle when both timers are zero.
1069 *
1070 * If send window is too small, there is data to transmit, and no
1071 * retransmit or persist is pending, then go to persist state.
1072 * If nothing happens soon, send when timer expires:
1073 * if window is nonzero, transmit what we can,
1074 * otherwise force out a byte.
1075 */
1076 if (so->so_snd.sb_cc && TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0 &&
1077 TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0) {
1078 tp->t_rxtshift = 0;
1079 tcp_setpersist(tp);
1080 }
1081
1082 /*
1083 * No reason to send a segment, just return.
1084 */
1085 just_return:
1086 TCP_REASS_UNLOCK(tp);
1087 return (0);
1088
1089 send:
1090 /*
1091 * Before ESTABLISHED, force sending of initial options
1092 * unless TCP set not to do any options.
1093 * NOTE: we assume that the IP/TCP header plus TCP options
1094 * always fit in a single mbuf, leaving room for a maximum
1095 * link header, i.e.
1096 * max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MCLBYTES
1097 */
1098 optlen = 0;
1099 switch (af) {
1100 #ifdef INET
1101 case AF_INET:
1102 iphdrlen = sizeof(struct ip) + sizeof(struct tcphdr);
1103 break;
1104 #endif
1105 #ifdef INET6
1106 case AF_INET6:
1107 iphdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
1108 break;
1109 #endif
1110 default: /*pacify gcc*/
1111 iphdrlen = 0;
1112 break;
1113 }
1114 hdrlen = iphdrlen;
1115 if (flags & TH_SYN) {
1116 struct rtentry *synrt;
1117
1118 synrt = NULL;
1119 #ifdef INET
1120 if (tp->t_inpcb)
1121 synrt = in_pcbrtentry(tp->t_inpcb);
1122 #endif
1123 #ifdef INET6
1124 if (tp->t_in6pcb)
1125 synrt = in6_pcbrtentry(tp->t_in6pcb);
1126 #endif
1127
1128 tp->snd_nxt = tp->iss;
1129 tp->t_ourmss = tcp_mss_to_advertise(synrt != NULL ?
1130 synrt->rt_ifp : NULL, af);
1131 if ((tp->t_flags & TF_NOOPT) == 0 && OPT_FITS(4)) {
1132 opt[0] = TCPOPT_MAXSEG;
1133 opt[1] = 4;
1134 opt[2] = (tp->t_ourmss >> 8) & 0xff;
1135 opt[3] = tp->t_ourmss & 0xff;
1136 optlen = 4;
1137
1138 if ((tp->t_flags & TF_REQ_SCALE) &&
1139 ((flags & TH_ACK) == 0 ||
1140 (tp->t_flags & TF_RCVD_SCALE)) &&
1141 OPT_FITS(4)) {
1142 *((u_int32_t *) (opt + optlen)) = htonl(
1143 TCPOPT_NOP << 24 |
1144 TCPOPT_WINDOW << 16 |
1145 TCPOLEN_WINDOW << 8 |
1146 tp->request_r_scale);
1147 optlen += 4;
1148 }
1149 if (tcp_do_sack && OPT_FITS(4)) {
1150 u_int8_t *cp = (u_int8_t *)(opt + optlen);
1151
1152 cp[0] = TCPOPT_SACK_PERMITTED;
1153 cp[1] = 2;
1154 cp[2] = TCPOPT_NOP;
1155 cp[3] = TCPOPT_NOP;
1156 optlen += 4;
1157 }
1158 }
1159 }
1160
1161 /*
1162 * Send a timestamp and echo-reply if this is a SYN and our side
1163 * wants to use timestamps (TF_REQ_TSTMP is set) or both our side
1164 * and our peer have sent timestamps in our SYN's.
1165 */
1166 if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
1167 (flags & TH_RST) == 0 &&
1168 ((flags & (TH_SYN|TH_ACK)) == TH_SYN ||
1169 (tp->t_flags & TF_RCVD_TSTMP)) && OPT_FITS(TCPOLEN_TSTAMP_APPA)) {
1170 u_int32_t *lp = (u_int32_t *)(opt + optlen);
1171
1172 /* Form timestamp option as shown in appendix A of RFC 1323. */
1173 *lp++ = htonl(TCPOPT_TSTAMP_HDR);
1174 *lp++ = htonl(TCP_TIMESTAMP(tp));
1175 *lp = htonl(tp->ts_recent);
1176 optlen += TCPOLEN_TSTAMP_APPA;
1177
1178 /* Set receive buffer autosizing timestamp. */
1179 if (tp->rfbuf_ts == 0 && (so->so_rcv.sb_flags & SB_AUTOSIZE))
1180 tp->rfbuf_ts = TCP_TIMESTAMP(tp);
1181 }
1182
1183 /*
1184 * Tack on the SACK block if it is necessary.
1185 */
1186 if (sack_numblks) {
1187 int sack_len;
1188 u_char *bp = (u_char *)(opt + optlen);
1189 u_int32_t *lp = (u_int32_t *)(bp + 4);
1190 struct ipqent *tiqe;
1191
1192 sack_len = sack_numblks * 8 + 2;
1193 if (OPT_FITS(sack_len + 2)) {
1194 bp[0] = TCPOPT_NOP;
1195 bp[1] = TCPOPT_NOP;
1196 bp[2] = TCPOPT_SACK;
1197 bp[3] = sack_len;
1198 if ((tp->rcv_sack_flags & TCPSACK_HAVED) != 0) {
1199 sack_numblks--;
1200 *lp++ = htonl(tp->rcv_dsack_block.left);
1201 *lp++ = htonl(tp->rcv_dsack_block.right);
1202 tp->rcv_sack_flags &= ~TCPSACK_HAVED;
1203 }
1204 for (tiqe = TAILQ_FIRST(&tp->timeq);
1205 sack_numblks > 0;
1206 tiqe = TAILQ_NEXT(tiqe, ipqe_timeq)) {
1207 KASSERT(tiqe != NULL);
1208 sack_numblks--;
1209 *lp++ = htonl(tiqe->ipqe_seq);
1210 *lp++ = htonl(tiqe->ipqe_seq + tiqe->ipqe_len +
1211 ((tiqe->ipqe_flags & TH_FIN) != 0 ? 1 : 0));
1212 }
1213 optlen += sack_len + 2;
1214 }
1215 }
1216 TCP_REASS_UNLOCK(tp);
1217
1218 #ifdef TCP_SIGNATURE
1219 if ((tp->t_flags & TF_SIGNATURE) && OPT_FITS(TCPOLEN_SIGNATURE + 2)) {
1220 u_char *bp;
1221 /*
1222 * Initialize TCP-MD5 option (RFC2385)
1223 */
1224 bp = (u_char *)opt + optlen;
1225 *bp++ = TCPOPT_SIGNATURE;
1226 *bp++ = TCPOLEN_SIGNATURE;
1227 sigoff = optlen + 2;
1228 memset(bp, 0, TCP_SIGLEN);
1229 bp += TCP_SIGLEN;
1230 optlen += TCPOLEN_SIGNATURE;
1231 /*
1232 * Terminate options list and maintain 32-bit alignment.
1233 */
1234 *bp++ = TCPOPT_NOP;
1235 *bp++ = TCPOPT_EOL;
1236 optlen += 2;
1237 }
1238 #endif /* TCP_SIGNATURE */
1239
1240 hdrlen += optlen;
1241
1242 #ifdef DIAGNOSTIC
1243 if (!use_tso && len > txsegsize)
1244 panic("tcp data to be sent is larger than segment");
1245 else if (use_tso && len > IP_MAXPACKET)
1246 panic("tcp data to be sent is larger than max TSO size");
1247 if (max_linkhdr + hdrlen > MCLBYTES)
1248 panic("tcphdr too big");
1249 #endif
1250
1251 /*
1252 * Grab a header mbuf, attaching a copy of data to
1253 * be transmitted, and initialize the header from
1254 * the template for sends on this connection.
1255 */
1256 if (len) {
1257 error = tcp_build_datapkt(tp, so, off, len, hdrlen, &m);
1258 if (error)
1259 goto out;
1260 /*
1261 * If we're sending everything we've got, set PUSH.
1262 * (This will keep happy those implementations which only
1263 * give data to the user when a buffer fills or
1264 * a PUSH comes in.)
1265 */
1266 if (off + len == so->so_snd.sb_cc)
1267 flags |= TH_PUSH;
1268 } else {
1269 tcps = TCP_STAT_GETREF();
1270 if (tp->t_flags & TF_ACKNOW)
1271 tcps[TCP_STAT_SNDACKS]++;
1272 else if (flags & (TH_SYN|TH_FIN|TH_RST))
1273 tcps[TCP_STAT_SNDCTRL]++;
1274 else if (SEQ_GT(tp->snd_up, tp->snd_una))
1275 tcps[TCP_STAT_SNDURG]++;
1276 else
1277 tcps[TCP_STAT_SNDWINUP]++;
1278 TCP_STAT_PUTREF();
1279
1280 MGETHDR(m, M_DONTWAIT, MT_HEADER);
1281 if (m != NULL && max_linkhdr + hdrlen > MHLEN) {
1282 MCLGET(m, M_DONTWAIT);
1283 if ((m->m_flags & M_EXT) == 0) {
1284 m_freem(m);
1285 m = NULL;
1286 }
1287 }
1288 if (m == NULL) {
1289 error = ENOBUFS;
1290 goto out;
1291 }
1292 MCLAIM(m, &tcp_tx_mowner);
1293 m->m_data += max_linkhdr;
1294 m->m_len = hdrlen;
1295 }
1296 m->m_pkthdr.rcvif = NULL;
1297 switch (af) {
1298 #ifdef INET
1299 case AF_INET:
1300 ip = mtod(m, struct ip *);
1301 #ifdef INET6
1302 ip6 = NULL;
1303 #endif
1304 th = (struct tcphdr *)(ip + 1);
1305 break;
1306 #endif
1307 #ifdef INET6
1308 case AF_INET6:
1309 ip = NULL;
1310 ip6 = mtod(m, struct ip6_hdr *);
1311 th = (struct tcphdr *)(ip6 + 1);
1312 break;
1313 #endif
1314 default: /*pacify gcc*/
1315 ip = NULL;
1316 #ifdef INET6
1317 ip6 = NULL;
1318 #endif
1319 th = NULL;
1320 break;
1321 }
1322 if (tp->t_template == 0)
1323 panic("tcp_output");
1324 if (tp->t_template->m_len < iphdrlen)
1325 panic("tcp_output");
1326 bcopy(mtod(tp->t_template, void *), mtod(m, void *), iphdrlen);
1327
1328 /*
1329 * If we are starting a connection, send ECN setup
1330 * SYN packet. If we are on a retransmit, we may
1331 * resend those bits a number of times as per
1332 * RFC 3168.
1333 */
1334 if (tp->t_state == TCPS_SYN_SENT && tcp_do_ecn) {
1335 if (tp->t_flags & TF_SYN_REXMT) {
1336 if (tp->t_ecn_retries--)
1337 flags |= TH_ECE|TH_CWR;
1338 } else {
1339 flags |= TH_ECE|TH_CWR;
1340 tp->t_ecn_retries = tcp_ecn_maxretries;
1341 }
1342 }
1343
1344 if (TCP_ECN_ALLOWED(tp)) {
1345 /*
1346 * If the peer has ECN, mark data packets
1347 * ECN capable. Ignore pure ack packets, retransmissions
1348 * and window probes.
1349 */
1350 if (len > 0 && SEQ_GEQ(tp->snd_nxt, tp->snd_max) &&
1351 !(tp->t_force && len == 1)) {
1352 ecn_tos = IPTOS_ECN_ECT0;
1353 TCP_STATINC(TCP_STAT_ECN_ECT);
1354 }
1355
1356 /*
1357 * Reply with proper ECN notifications.
1358 */
1359 if (tp->t_flags & TF_ECN_SND_CWR) {
1360 flags |= TH_CWR;
1361 tp->t_flags &= ~TF_ECN_SND_CWR;
1362 }
1363 if (tp->t_flags & TF_ECN_SND_ECE) {
1364 flags |= TH_ECE;
1365 }
1366 }
1367
1368
1369 /*
1370 * If we are doing retransmissions, then snd_nxt will
1371 * not reflect the first unsent octet. For ACK only
1372 * packets, we do not want the sequence number of the
1373 * retransmitted packet, we want the sequence number
1374 * of the next unsent octet. So, if there is no data
1375 * (and no SYN or FIN), use snd_max instead of snd_nxt
1376 * when filling in ti_seq. But if we are in persist
1377 * state, snd_max might reflect one byte beyond the
1378 * right edge of the window, so use snd_nxt in that
1379 * case, since we know we aren't doing a retransmission.
1380 * (retransmit and persist are mutually exclusive...)
1381 */
1382 if (TCP_SACK_ENABLED(tp) && sack_rxmit) {
1383 th->th_seq = htonl(p->rxmit);
1384 p->rxmit += len;
1385 } else {
1386 if (len || (flags & (TH_SYN|TH_FIN)) ||
1387 TCP_TIMER_ISARMED(tp, TCPT_PERSIST))
1388 th->th_seq = htonl(tp->snd_nxt);
1389 else
1390 th->th_seq = htonl(tp->snd_max);
1391 }
1392 th->th_ack = htonl(tp->rcv_nxt);
1393 if (optlen) {
1394 bcopy((void *)opt, (void *)(th + 1), optlen);
1395 th->th_off = (sizeof (struct tcphdr) + optlen) >> 2;
1396 }
1397 th->th_flags = flags;
1398 /*
1399 * Calculate receive window. Don't shrink window,
1400 * but avoid silly window syndrome.
1401 */
1402 if (win < (long)(so->so_rcv.sb_hiwat / 4) && win < (long)rxsegsize)
1403 win = 0;
1404 if (win > (long)TCP_MAXWIN << tp->rcv_scale)
1405 win = (long)TCP_MAXWIN << tp->rcv_scale;
1406 if (win < (long)(int32_t)(tp->rcv_adv - tp->rcv_nxt))
1407 win = (long)(int32_t)(tp->rcv_adv - tp->rcv_nxt);
1408 th->th_win = htons((u_int16_t) (win>>tp->rcv_scale));
1409 if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
1410 u_int32_t urp = tp->snd_up - tp->snd_nxt;
1411 if (urp > IP_MAXPACKET)
1412 urp = IP_MAXPACKET;
1413 th->th_urp = htons((u_int16_t)urp);
1414 th->th_flags |= TH_URG;
1415 } else
1416 /*
1417 * If no urgent pointer to send, then we pull
1418 * the urgent pointer to the left edge of the send window
1419 * so that it doesn't drift into the send window on sequence
1420 * number wraparound.
1421 */
1422 tp->snd_up = tp->snd_una; /* drag it along */
1423
1424 #ifdef TCP_SIGNATURE
1425 if (sigoff && (tp->t_flags & TF_SIGNATURE)) {
1426 struct secasvar *sav;
1427 u_int8_t *sigp;
1428
1429 sav = tcp_signature_getsav(m, th);
1430
1431 if (sav == NULL) {
1432 if (m)
1433 m_freem(m);
1434 return (EPERM);
1435 }
1436
1437 m->m_pkthdr.len = hdrlen + len;
1438 sigp = (char *)th + sizeof(*th) + sigoff;
1439 tcp_signature(m, th, (char *)th - mtod(m, char *), sav, sigp);
1440
1441 key_sa_recordxfer(sav, m);
1442 #ifdef FAST_IPSEC
1443 KEY_FREESAV(&sav);
1444 #else
1445 key_freesav(sav);
1446 #endif
1447 }
1448 #endif
1449
1450 /*
1451 * Set ourselves up to be checksummed just before the packet
1452 * hits the wire.
1453 */
1454 switch (af) {
1455 #ifdef INET
1456 case AF_INET:
1457 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
1458 if (use_tso) {
1459 m->m_pkthdr.segsz = txsegsize;
1460 m->m_pkthdr.csum_flags = M_CSUM_TSOv4;
1461 } else {
1462 m->m_pkthdr.csum_flags = M_CSUM_TCPv4;
1463 if (len + optlen) {
1464 /* Fixup the pseudo-header checksum. */
1465 /* XXXJRT Not IP Jumbogram safe. */
1466 th->th_sum = in_cksum_addword(th->th_sum,
1467 htons((u_int16_t) (len + optlen)));
1468 }
1469 }
1470 break;
1471 #endif
1472 #ifdef INET6
1473 case AF_INET6:
1474 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
1475 if (use_tso) {
1476 m->m_pkthdr.segsz = txsegsize;
1477 m->m_pkthdr.csum_flags = M_CSUM_TSOv6;
1478 } else {
1479 m->m_pkthdr.csum_flags = M_CSUM_TCPv6;
1480 if (len + optlen) {
1481 /* Fixup the pseudo-header checksum. */
1482 /* XXXJRT: Not IPv6 Jumbogram safe. */
1483 th->th_sum = in_cksum_addword(th->th_sum,
1484 htons((u_int16_t) (len + optlen)));
1485 }
1486 }
1487 break;
1488 #endif
1489 }
1490
1491 /*
1492 * In transmit state, time the transmission and arrange for
1493 * the retransmit. In persist state, just set snd_max.
1494 */
1495 if (tp->t_force == 0 || TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0) {
1496 tcp_seq startseq = tp->snd_nxt;
1497
1498 /*
1499 * Advance snd_nxt over sequence space of this segment.
1500 * There are no states in which we send both a SYN and a FIN,
1501 * so we collapse the tests for these flags.
1502 */
1503 if (flags & (TH_SYN|TH_FIN))
1504 tp->snd_nxt++;
1505 if (sack_rxmit)
1506 goto timer;
1507 tp->snd_nxt += len;
1508 if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
1509 tp->snd_max = tp->snd_nxt;
1510 /*
1511 * Time this transmission if not a retransmission and
1512 * not currently timing anything.
1513 */
1514 if (tp->t_rtttime == 0) {
1515 tp->t_rtttime = tcp_now;
1516 tp->t_rtseq = startseq;
1517 TCP_STATINC(TCP_STAT_SEGSTIMED);
1518 }
1519 }
1520
1521 /*
1522 * Set retransmit timer if not currently set,
1523 * and not doing an ack or a keep-alive probe.
1524 * Initial value for retransmit timer is smoothed
1525 * round-trip time + 2 * round-trip time variance.
1526 * Initialize shift counter which is used for backoff
1527 * of retransmit time.
1528 */
1529 timer:
1530 if (TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0 &&
1531 ((sack_rxmit && tp->snd_nxt != tp->snd_max) ||
1532 tp->snd_nxt != tp->snd_una)) {
1533 if (TCP_TIMER_ISARMED(tp, TCPT_PERSIST)) {
1534 TCP_TIMER_DISARM(tp, TCPT_PERSIST);
1535 tp->t_rxtshift = 0;
1536 }
1537 TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur);
1538 }
1539 } else
1540 if (SEQ_GT(tp->snd_nxt + len, tp->snd_max))
1541 tp->snd_max = tp->snd_nxt + len;
1542
1543 #ifdef TCP_DEBUG
1544 /*
1545 * Trace.
1546 */
1547 if (so->so_options & SO_DEBUG)
1548 tcp_trace(TA_OUTPUT, tp->t_state, tp, m, 0);
1549 #endif
1550
1551 /*
1552 * Fill in IP length and desired time to live and
1553 * send to IP level. There should be a better way
1554 * to handle ttl and tos; we could keep them in
1555 * the template, but need a way to checksum without them.
1556 */
1557 m->m_pkthdr.len = hdrlen + len;
1558
1559 switch (af) {
1560 #ifdef INET
1561 case AF_INET:
1562 ip->ip_len = htons(m->m_pkthdr.len);
1563 packetlen = m->m_pkthdr.len;
1564 if (tp->t_inpcb) {
1565 ip->ip_ttl = tp->t_inpcb->inp_ip.ip_ttl;
1566 ip->ip_tos = tp->t_inpcb->inp_ip.ip_tos | ecn_tos;
1567 }
1568 #ifdef INET6
1569 else if (tp->t_in6pcb) {
1570 ip->ip_ttl = in6_selecthlim(tp->t_in6pcb, NULL); /*XXX*/
1571 ip->ip_tos = ecn_tos; /*XXX*/
1572 }
1573 #endif
1574 break;
1575 #endif
1576 #ifdef INET6
1577 case AF_INET6:
1578 packetlen = m->m_pkthdr.len;
1579 ip6->ip6_nxt = IPPROTO_TCP;
1580 if (tp->t_in6pcb) {
1581 /*
1582 * we separately set hoplimit for every segment, since
1583 * the user might want to change the value via
1584 * setsockopt. Also, desired default hop limit might
1585 * be changed via Neighbor Discovery.
1586 */
1587 ip6->ip6_hlim = in6_selecthlim(tp->t_in6pcb,
1588 (rt = rtcache_validate(ro)) != NULL ? rt->rt_ifp
1589 : NULL);
1590 }
1591 ip6->ip6_flow |= htonl(ecn_tos << 20);
1592 /* ip6->ip6_flow = ??? (from template) */
1593 /* ip6_plen will be filled in ip6_output(). */
1594 break;
1595 #endif
1596 default: /*pacify gcc*/
1597 packetlen = 0;
1598 break;
1599 }
1600
1601 switch (af) {
1602 #ifdef INET
1603 case AF_INET:
1604 {
1605 struct mbuf *opts;
1606
1607 if (tp->t_inpcb)
1608 opts = tp->t_inpcb->inp_options;
1609 else
1610 opts = NULL;
1611 error = ip_output(m, opts, ro,
1612 (tp->t_mtudisc ? IP_MTUDISC : 0) |
1613 (so->so_options & SO_DONTROUTE), NULL, so);
1614 break;
1615 }
1616 #endif
1617 #ifdef INET6
1618 case AF_INET6:
1619 {
1620 struct ip6_pktopts *opts;
1621
1622 if (tp->t_in6pcb)
1623 opts = tp->t_in6pcb->in6p_outputopts;
1624 else
1625 opts = NULL;
1626 error = ip6_output(m, opts, ro, so->so_options & SO_DONTROUTE,
1627 NULL, so, NULL);
1628 break;
1629 }
1630 #endif
1631 default:
1632 error = EAFNOSUPPORT;
1633 break;
1634 }
1635 if (error) {
1636 out:
1637 if (error == ENOBUFS) {
1638 TCP_STATINC(TCP_STAT_SELFQUENCH);
1639 #ifdef INET
1640 if (tp->t_inpcb)
1641 tcp_quench(tp->t_inpcb, 0);
1642 #endif
1643 #ifdef INET6
1644 if (tp->t_in6pcb)
1645 tcp6_quench(tp->t_in6pcb, 0);
1646 #endif
1647 error = 0;
1648 } else if ((error == EHOSTUNREACH || error == ENETDOWN) &&
1649 TCPS_HAVERCVDSYN(tp->t_state)) {
1650 tp->t_softerror = error;
1651 error = 0;
1652 }
1653
1654 /* Back out the seqence number advance. */
1655 if (sack_rxmit)
1656 p->rxmit -= len;
1657
1658 /* Restart the delayed ACK timer, if necessary. */
1659 if (tp->t_flags & TF_DELACK)
1660 TCP_RESTART_DELACK(tp);
1661
1662 return (error);
1663 }
1664
1665 if (packetlen > tp->t_pmtud_mtu_sent)
1666 tp->t_pmtud_mtu_sent = packetlen;
1667
1668 tcps = TCP_STAT_GETREF();
1669 tcps[TCP_STAT_SNDTOTAL]++;
1670 if (tp->t_flags & TF_DELACK)
1671 tcps[TCP_STAT_DELACK]++;
1672 TCP_STAT_PUTREF();
1673
1674 /*
1675 * Data sent (as far as we can tell).
1676 * If this advertises a larger window than any other segment,
1677 * then remember the size of the advertised window.
1678 * Any pending ACK has now been sent.
1679 */
1680 if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv))
1681 tp->rcv_adv = tp->rcv_nxt + win;
1682 tp->last_ack_sent = tp->rcv_nxt;
1683 tp->t_flags &= ~TF_ACKNOW;
1684 TCP_CLEAR_DELACK(tp);
1685 #ifdef DIAGNOSTIC
1686 if (maxburst < 0)
1687 printf("tcp_output: maxburst exceeded by %d\n", -maxburst);
1688 #endif
1689 if (sendalot && (tp->t_congctl == &tcp_reno_ctl || --maxburst))
1690 goto again;
1691 return (0);
1692 }
1693
1694 void
1695 tcp_setpersist(struct tcpcb *tp)
1696 {
1697 int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> (1 + 2);
1698 int nticks;
1699
1700 if (TCP_TIMER_ISARMED(tp, TCPT_REXMT))
1701 panic("tcp_output REXMT");
1702 /*
1703 * Start/restart persistance timer.
1704 */
1705 if (t < tp->t_rttmin)
1706 t = tp->t_rttmin;
1707 TCPT_RANGESET(nticks, t * tcp_backoff[tp->t_rxtshift],
1708 TCPTV_PERSMIN, TCPTV_PERSMAX);
1709 TCP_TIMER_ARM(tp, TCPT_PERSIST, nticks);
1710 if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
1711 tp->t_rxtshift++;
1712 }
1713