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