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