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