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