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