1 /* $NetBSD: tcp_subr.c,v 1.63 1998/12/18 21:38:03 thorpej Exp $ */ 2 3 /*- 4 * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jason R. Thorpe and Kevin M. Lahey of the Numerical Aerospace Simulation 9 * Facility, NASA Ames Research Center. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the NetBSD 22 * Foundation, Inc. and its contributors. 23 * 4. Neither the name of The NetBSD Foundation nor the names of its 24 * contributors may be used to endorse or promote products derived 25 * from this software without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 37 * POSSIBILITY OF SUCH DAMAGE. 38 */ 39 40 /* 41 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995 42 * The Regents of the University of California. All rights reserved. 43 * 44 * Redistribution and use in source and binary forms, with or without 45 * modification, are permitted provided that the following conditions 46 * are met: 47 * 1. Redistributions of source code must retain the above copyright 48 * notice, this list of conditions and the following disclaimer. 49 * 2. Redistributions in binary form must reproduce the above copyright 50 * notice, this list of conditions and the following disclaimer in the 51 * documentation and/or other materials provided with the distribution. 52 * 3. All advertising materials mentioning features or use of this software 53 * must display the following acknowledgement: 54 * This product includes software developed by the University of 55 * California, Berkeley and its contributors. 56 * 4. Neither the name of the University nor the names of its contributors 57 * may be used to endorse or promote products derived from this software 58 * without specific prior written permission. 59 * 60 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 61 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 62 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 63 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 64 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 65 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 66 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 67 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 68 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 69 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 70 * SUCH DAMAGE. 71 * 72 * @(#)tcp_subr.c 8.2 (Berkeley) 5/24/95 73 */ 74 75 #include "opt_tcp_compat_42.h" 76 #include "rnd.h" 77 78 #include <sys/param.h> 79 #include <sys/proc.h> 80 #include <sys/systm.h> 81 #include <sys/malloc.h> 82 #include <sys/mbuf.h> 83 #include <sys/socket.h> 84 #include <sys/socketvar.h> 85 #include <sys/protosw.h> 86 #include <sys/errno.h> 87 #include <sys/kernel.h> 88 #include <sys/pool.h> 89 #if NRND > 0 90 #include <sys/rnd.h> 91 #endif 92 93 #include <net/route.h> 94 #include <net/if.h> 95 96 #include <netinet/in.h> 97 #include <netinet/in_systm.h> 98 #include <netinet/ip.h> 99 #include <netinet/in_pcb.h> 100 #include <netinet/ip_var.h> 101 #include <netinet/ip_icmp.h> 102 #include <netinet/tcp.h> 103 #include <netinet/tcp_fsm.h> 104 #include <netinet/tcp_seq.h> 105 #include <netinet/tcp_timer.h> 106 #include <netinet/tcp_var.h> 107 #include <netinet/tcpip.h> 108 109 /* patchable/settable parameters for tcp */ 110 int tcp_mssdflt = TCP_MSS; 111 int tcp_rttdflt = TCPTV_SRTTDFLT / PR_SLOWHZ; 112 int tcp_do_rfc1323 = 1; /* window scaling / timestamps (obsolete) */ 113 int tcp_do_sack = 1; /* selective acknowledgement */ 114 int tcp_do_win_scale = 1; /* RFC1323 window scaling */ 115 int tcp_do_timestamps = 1; /* RFC1323 timestamps */ 116 int tcp_do_newreno = 0; /* Use the New Reno algorithms */ 117 int tcp_ack_on_push = 0; /* set to enable immediate ACK-on-PUSH */ 118 int tcp_init_win = 1; 119 int tcp_mss_ifmtu = 0; 120 #ifdef TCP_COMPAT_42 121 int tcp_compat_42 = 1; 122 #else 123 int tcp_compat_42 = 0; 124 #endif 125 126 #ifndef TCBHASHSIZE 127 #define TCBHASHSIZE 128 128 #endif 129 int tcbhashsize = TCBHASHSIZE; 130 131 int tcp_freeq __P((struct tcpcb *)); 132 133 struct pool tcpcb_pool; 134 struct pool tcp_template_pool; 135 136 /* 137 * Tcp initialization 138 */ 139 void 140 tcp_init() 141 { 142 143 pool_init(&tcpcb_pool, sizeof(struct tcpcb), 0, 0, 0, "tcpcbpl", 144 0, NULL, NULL, M_PCB); 145 pool_init(&tcp_template_pool, sizeof(struct tcpiphdr), 0, 0, 0, 146 "tcptmpl", 0, NULL, NULL, M_MBUF); 147 in_pcbinit(&tcbtable, tcbhashsize, tcbhashsize); 148 LIST_INIT(&tcp_delacks); 149 if (max_protohdr < sizeof(struct tcpiphdr)) 150 max_protohdr = sizeof(struct tcpiphdr); 151 if (max_linkhdr + sizeof(struct tcpiphdr) > MHLEN) 152 panic("tcp_init"); 153 154 /* Initialize the compressed state engine. */ 155 syn_cache_init(); 156 } 157 158 /* 159 * Create template to be used to send tcp packets on a connection. 160 * Call after host entry created, allocates an mbuf and fills 161 * in a skeletal tcp/ip header, minimizing the amount of work 162 * necessary when the connection is used. 163 */ 164 struct tcpiphdr * 165 tcp_template(tp) 166 struct tcpcb *tp; 167 { 168 register struct inpcb *inp = tp->t_inpcb; 169 register struct tcpiphdr *n; 170 171 if ((n = tp->t_template) == 0) { 172 n = pool_get(&tcp_template_pool, PR_NOWAIT); 173 if (n == NULL) 174 return (NULL); 175 } 176 bzero(n->ti_x1, sizeof n->ti_x1); 177 n->ti_pr = IPPROTO_TCP; 178 n->ti_len = htons(sizeof (struct tcpiphdr) - sizeof (struct ip)); 179 n->ti_src = inp->inp_laddr; 180 n->ti_dst = inp->inp_faddr; 181 n->ti_sport = inp->inp_lport; 182 n->ti_dport = inp->inp_fport; 183 n->ti_seq = 0; 184 n->ti_ack = 0; 185 n->ti_x2 = 0; 186 n->ti_off = 5; 187 n->ti_flags = 0; 188 n->ti_win = 0; 189 n->ti_sum = 0; 190 n->ti_urp = 0; 191 return (n); 192 } 193 194 /* 195 * Send a single message to the TCP at address specified by 196 * the given TCP/IP header. If m == 0, then we make a copy 197 * of the tcpiphdr at ti and send directly to the addressed host. 198 * This is used to force keep alive messages out using the TCP 199 * template for a connection tp->t_template. If flags are given 200 * then we send a message back to the TCP which originated the 201 * segment ti, and discard the mbuf containing it and any other 202 * attached mbufs. 203 * 204 * In any case the ack and sequence number of the transmitted 205 * segment are as specified by the parameters. 206 */ 207 int 208 tcp_respond(tp, ti, m, ack, seq, flags) 209 struct tcpcb *tp; 210 register struct tcpiphdr *ti; 211 register struct mbuf *m; 212 tcp_seq ack, seq; 213 int flags; 214 { 215 register int tlen; 216 int win = 0; 217 struct route *ro = 0; 218 219 if (tp) { 220 if ((flags & TH_RST) == 0) 221 win = sbspace(&tp->t_inpcb->inp_socket->so_rcv); 222 ro = &tp->t_inpcb->inp_route; 223 } 224 if (m == 0) { 225 m = m_gethdr(M_DONTWAIT, MT_HEADER); 226 if (m == NULL) 227 return (ENOBUFS); 228 229 if (tcp_compat_42) 230 tlen = 1; 231 else 232 tlen = 0; 233 234 m->m_data += max_linkhdr; 235 *mtod(m, struct tcpiphdr *) = *ti; 236 ti = mtod(m, struct tcpiphdr *); 237 flags = TH_ACK; 238 } else { 239 m_freem(m->m_next); 240 m->m_next = 0; 241 m->m_data = (caddr_t)ti; 242 m->m_len = sizeof (struct tcpiphdr); 243 tlen = 0; 244 #define xchg(a,b,type) { type t; t=a; a=b; b=t; } 245 xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr, u_int32_t); 246 xchg(ti->ti_dport, ti->ti_sport, u_int16_t); 247 #undef xchg 248 } 249 bzero(ti->ti_x1, sizeof ti->ti_x1); 250 ti->ti_seq = htonl(seq); 251 ti->ti_ack = htonl(ack); 252 ti->ti_x2 = 0; 253 if ((flags & TH_SYN) == 0) { 254 if (tp) 255 ti->ti_win = htons((u_int16_t) (win >> tp->rcv_scale)); 256 else 257 ti->ti_win = htons((u_int16_t)win); 258 ti->ti_off = sizeof (struct tcphdr) >> 2; 259 tlen += sizeof (struct tcphdr); 260 } else 261 tlen += ti->ti_off << 2; 262 ti->ti_len = htons((u_int16_t)tlen); 263 tlen += sizeof (struct ip); 264 m->m_len = tlen; 265 m->m_pkthdr.len = tlen; 266 m->m_pkthdr.rcvif = (struct ifnet *) 0; 267 ti->ti_flags = flags; 268 ti->ti_urp = 0; 269 ti->ti_sum = 0; 270 ti->ti_sum = in_cksum(m, tlen); 271 ((struct ip *)ti)->ip_len = tlen; 272 ((struct ip *)ti)->ip_ttl = ip_defttl; 273 return ip_output(m, NULL, ro, 0, NULL); 274 } 275 276 /* 277 * Create a new TCP control block, making an 278 * empty reassembly queue and hooking it to the argument 279 * protocol control block. 280 */ 281 struct tcpcb * 282 tcp_newtcpcb(inp) 283 struct inpcb *inp; 284 { 285 register struct tcpcb *tp; 286 287 tp = pool_get(&tcpcb_pool, PR_NOWAIT); 288 if (tp == NULL) 289 return (NULL); 290 bzero((caddr_t)tp, sizeof(struct tcpcb)); 291 LIST_INIT(&tp->segq); 292 LIST_INIT(&tp->timeq); 293 tp->t_peermss = tcp_mssdflt; 294 tp->t_ourmss = tcp_mssdflt; 295 tp->t_segsz = tcp_mssdflt; 296 297 tp->t_flags = 0; 298 if (tcp_do_rfc1323 && tcp_do_win_scale) 299 tp->t_flags |= TF_REQ_SCALE; 300 if (tcp_do_rfc1323 && tcp_do_timestamps) 301 tp->t_flags |= TF_REQ_TSTMP; 302 if (tcp_do_sack == 2) 303 tp->t_flags |= TF_WILL_SACK; 304 else if (tcp_do_sack == 1) 305 tp->t_flags |= TF_WILL_SACK|TF_IGNR_RXSACK; 306 tp->t_flags |= TF_CANT_TXSACK; 307 tp->t_inpcb = inp; 308 /* 309 * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no 310 * rtt estimate. Set rttvar so that srtt + 2 * rttvar gives 311 * reasonable initial retransmit time. 312 */ 313 tp->t_srtt = TCPTV_SRTTBASE; 314 tp->t_rttvar = tcp_rttdflt * PR_SLOWHZ << (TCP_RTTVAR_SHIFT + 2 - 1); 315 tp->t_rttmin = TCPTV_MIN; 316 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 317 TCPTV_MIN, TCPTV_REXMTMAX); 318 tp->snd_cwnd = TCP_MAXWIN << TCP_MAX_WINSHIFT; 319 tp->snd_ssthresh = TCP_MAXWIN << TCP_MAX_WINSHIFT; 320 inp->inp_ip.ip_ttl = ip_defttl; 321 inp->inp_ppcb = (caddr_t)tp; 322 return (tp); 323 } 324 325 /* 326 * Drop a TCP connection, reporting 327 * the specified error. If connection is synchronized, 328 * then send a RST to peer. 329 */ 330 struct tcpcb * 331 tcp_drop(tp, errno) 332 register struct tcpcb *tp; 333 int errno; 334 { 335 struct socket *so = tp->t_inpcb->inp_socket; 336 337 if (TCPS_HAVERCVDSYN(tp->t_state)) { 338 tp->t_state = TCPS_CLOSED; 339 (void) tcp_output(tp); 340 tcpstat.tcps_drops++; 341 } else 342 tcpstat.tcps_conndrops++; 343 if (errno == ETIMEDOUT && tp->t_softerror) 344 errno = tp->t_softerror; 345 so->so_error = errno; 346 return (tcp_close(tp)); 347 } 348 349 /* 350 * Close a TCP control block: 351 * discard all space held by the tcp 352 * discard internet protocol block 353 * wake up any sleepers 354 */ 355 struct tcpcb * 356 tcp_close(tp) 357 register struct tcpcb *tp; 358 { 359 struct inpcb *inp = tp->t_inpcb; 360 struct socket *so = inp->inp_socket; 361 #ifdef RTV_RTT 362 register struct rtentry *rt; 363 364 /* 365 * If we sent enough data to get some meaningful characteristics, 366 * save them in the routing entry. 'Enough' is arbitrarily 367 * defined as the sendpipesize (default 4K) * 16. This would 368 * give us 16 rtt samples assuming we only get one sample per 369 * window (the usual case on a long haul net). 16 samples is 370 * enough for the srtt filter to converge to within 5% of the correct 371 * value; fewer samples and we could save a very bogus rtt. 372 * 373 * Don't update the default route's characteristics and don't 374 * update anything that the user "locked". 375 */ 376 if (SEQ_LT(tp->iss + so->so_snd.sb_hiwat * 16, tp->snd_max) && 377 (rt = inp->inp_route.ro_rt) && 378 !in_nullhost(satosin(rt_key(rt))->sin_addr)) { 379 register u_long i = 0; 380 381 if ((rt->rt_rmx.rmx_locks & RTV_RTT) == 0) { 382 i = tp->t_srtt * 383 ((RTM_RTTUNIT / PR_SLOWHZ) >> (TCP_RTT_SHIFT + 2)); 384 if (rt->rt_rmx.rmx_rtt && i) 385 /* 386 * filter this update to half the old & half 387 * the new values, converting scale. 388 * See route.h and tcp_var.h for a 389 * description of the scaling constants. 390 */ 391 rt->rt_rmx.rmx_rtt = 392 (rt->rt_rmx.rmx_rtt + i) / 2; 393 else 394 rt->rt_rmx.rmx_rtt = i; 395 } 396 if ((rt->rt_rmx.rmx_locks & RTV_RTTVAR) == 0) { 397 i = tp->t_rttvar * 398 ((RTM_RTTUNIT / PR_SLOWHZ) >> (TCP_RTTVAR_SHIFT + 2)); 399 if (rt->rt_rmx.rmx_rttvar && i) 400 rt->rt_rmx.rmx_rttvar = 401 (rt->rt_rmx.rmx_rttvar + i) / 2; 402 else 403 rt->rt_rmx.rmx_rttvar = i; 404 } 405 /* 406 * update the pipelimit (ssthresh) if it has been updated 407 * already or if a pipesize was specified & the threshhold 408 * got below half the pipesize. I.e., wait for bad news 409 * before we start updating, then update on both good 410 * and bad news. 411 */ 412 if (((rt->rt_rmx.rmx_locks & RTV_SSTHRESH) == 0 && 413 (i = tp->snd_ssthresh) && rt->rt_rmx.rmx_ssthresh) || 414 i < (rt->rt_rmx.rmx_sendpipe / 2)) { 415 /* 416 * convert the limit from user data bytes to 417 * packets then to packet data bytes. 418 */ 419 i = (i + tp->t_segsz / 2) / tp->t_segsz; 420 if (i < 2) 421 i = 2; 422 i *= (u_long)(tp->t_segsz + sizeof (struct tcpiphdr)); 423 if (rt->rt_rmx.rmx_ssthresh) 424 rt->rt_rmx.rmx_ssthresh = 425 (rt->rt_rmx.rmx_ssthresh + i) / 2; 426 else 427 rt->rt_rmx.rmx_ssthresh = i; 428 } 429 } 430 #endif /* RTV_RTT */ 431 /* free the reassembly queue, if any */ 432 TCP_REASS_LOCK(tp); 433 (void) tcp_freeq(tp); 434 TCP_REASS_UNLOCK(tp); 435 436 TCP_CLEAR_DELACK(tp); 437 438 if (tp->t_template) 439 pool_put(&tcp_template_pool, tp->t_template); 440 pool_put(&tcpcb_pool, tp); 441 inp->inp_ppcb = 0; 442 soisdisconnected(so); 443 in_pcbdetach(inp); 444 tcpstat.tcps_closed++; 445 return ((struct tcpcb *)0); 446 } 447 448 int 449 tcp_freeq(tp) 450 struct tcpcb *tp; 451 { 452 register struct ipqent *qe; 453 int rv = 0; 454 #ifdef TCPREASS_DEBUG 455 int i = 0; 456 #endif 457 458 TCP_REASS_LOCK_CHECK(tp); 459 460 while ((qe = tp->segq.lh_first) != NULL) { 461 #ifdef TCPREASS_DEBUG 462 printf("tcp_freeq[%p,%d]: %u:%u(%u) 0x%02x\n", 463 tp, i++, qe->ipqe_seq, qe->ipqe_seq + qe->ipqe_len, 464 qe->ipqe_len, qe->ipqe_flags & (TH_SYN|TH_FIN|TH_RST)); 465 #endif 466 LIST_REMOVE(qe, ipqe_q); 467 LIST_REMOVE(qe, ipqe_timeq); 468 m_freem(qe->ipqe_m); 469 pool_put(&ipqent_pool, qe); 470 rv = 1; 471 } 472 return (rv); 473 } 474 475 /* 476 * Protocol drain routine. Called when memory is in short supply. 477 */ 478 void 479 tcp_drain() 480 { 481 register struct inpcb *inp; 482 register struct tcpcb *tp; 483 484 /* 485 * Free the sequence queue of all TCP connections. 486 */ 487 inp = tcbtable.inpt_queue.cqh_first; 488 if (inp) /* XXX */ 489 for (; inp != (struct inpcb *)&tcbtable.inpt_queue; 490 inp = inp->inp_queue.cqe_next) { 491 if ((tp = intotcpcb(inp)) != NULL) { 492 /* 493 * We may be called from a device's interrupt 494 * context. If the tcpcb is already busy, 495 * just bail out now. 496 */ 497 if (tcp_reass_lock_try(tp) == 0) 498 continue; 499 if (tcp_freeq(tp)) 500 tcpstat.tcps_connsdrained++; 501 TCP_REASS_UNLOCK(tp); 502 } 503 } 504 } 505 506 /* 507 * Notify a tcp user of an asynchronous error; 508 * store error as soft error, but wake up user 509 * (for now, won't do anything until can select for soft error). 510 */ 511 void 512 tcp_notify(inp, error) 513 struct inpcb *inp; 514 int error; 515 { 516 register struct tcpcb *tp = (struct tcpcb *)inp->inp_ppcb; 517 register struct socket *so = inp->inp_socket; 518 519 /* 520 * Ignore some errors if we are hooked up. 521 * If connection hasn't completed, has retransmitted several times, 522 * and receives a second error, give up now. This is better 523 * than waiting a long time to establish a connection that 524 * can never complete. 525 */ 526 if (tp->t_state == TCPS_ESTABLISHED && 527 (error == EHOSTUNREACH || error == ENETUNREACH || 528 error == EHOSTDOWN)) { 529 return; 530 } else if (TCPS_HAVEESTABLISHED(tp->t_state) == 0 && 531 tp->t_rxtshift > 3 && tp->t_softerror) 532 so->so_error = error; 533 else 534 tp->t_softerror = error; 535 wakeup((caddr_t) &so->so_timeo); 536 sorwakeup(so); 537 sowwakeup(so); 538 } 539 540 void * 541 tcp_ctlinput(cmd, sa, v) 542 int cmd; 543 struct sockaddr *sa; 544 register void *v; 545 { 546 register struct ip *ip = v; 547 register struct tcphdr *th; 548 extern int inetctlerrmap[]; 549 void (*notify) __P((struct inpcb *, int)) = tcp_notify; 550 int errno; 551 int nmatch; 552 553 if ((unsigned)cmd >= PRC_NCMDS) 554 return NULL; 555 errno = inetctlerrmap[cmd]; 556 if (cmd == PRC_QUENCH) 557 notify = tcp_quench; 558 else if (PRC_IS_REDIRECT(cmd)) 559 notify = in_rtchange, ip = 0; 560 else if (cmd == PRC_MSGSIZE && ip_mtudisc) 561 notify = tcp_mtudisc, ip = 0; 562 else if (cmd == PRC_HOSTDEAD) 563 ip = 0; 564 else if (errno == 0) 565 return NULL; 566 if (ip) { 567 th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2)); 568 nmatch = in_pcbnotify(&tcbtable, satosin(sa)->sin_addr, 569 th->th_dport, ip->ip_src, th->th_sport, errno, notify); 570 if (nmatch == 0 && syn_cache_count && 571 (inetctlerrmap[cmd] == EHOSTUNREACH || 572 inetctlerrmap[cmd] == ENETUNREACH || 573 inetctlerrmap[cmd] == EHOSTDOWN)) 574 syn_cache_unreach(ip, th); 575 } else 576 (void)in_pcbnotifyall(&tcbtable, satosin(sa)->sin_addr, errno, 577 notify); 578 return NULL; 579 } 580 581 /* 582 * When a source quence is received, we are being notifed of congestion. 583 * Close the congestion window down to the Loss Window (one segment). 584 * We will gradually open it again as we proceed. 585 */ 586 void 587 tcp_quench(inp, errno) 588 struct inpcb *inp; 589 int errno; 590 { 591 struct tcpcb *tp = intotcpcb(inp); 592 593 if (tp) 594 tp->snd_cwnd = tp->t_segsz; 595 } 596 597 /* 598 * On receipt of path MTU corrections, flush old route and replace it 599 * with the new one. Retransmit all unacknowledged packets, to ensure 600 * that all packets will be received. 601 */ 602 void 603 tcp_mtudisc(inp, errno) 604 struct inpcb *inp; 605 int errno; 606 { 607 struct tcpcb *tp = intotcpcb(inp); 608 struct rtentry *rt = in_pcbrtentry(inp); 609 610 if (tp != 0) { 611 if (rt != 0) { 612 /* 613 * If this was not a host route, remove and realloc. 614 */ 615 if ((rt->rt_flags & RTF_HOST) == 0) { 616 in_rtchange(inp, errno); 617 if ((rt = in_pcbrtentry(inp)) == 0) 618 return; 619 } 620 621 /* 622 * Slow start out of the error condition. We 623 * use the MTU because we know it's smaller 624 * than the previously transmitted segment. 625 * 626 * Note: This is more conservative than the 627 * suggestion in draft-floyd-incr-init-win-03. 628 */ 629 if (rt->rt_rmx.rmx_mtu != 0) 630 tp->snd_cwnd = 631 TCP_INITIAL_WINDOW(tcp_init_win, 632 rt->rt_rmx.rmx_mtu); 633 } 634 635 /* 636 * Resend unacknowledged packets. 637 */ 638 tp->snd_nxt = tp->snd_una; 639 tcp_output(tp); 640 } 641 } 642 643 644 /* 645 * Compute the MSS to advertise to the peer. Called only during 646 * the 3-way handshake. If we are the server (peer initiated 647 * connection), we are called with a pointer to the interface 648 * on which the SYN packet arrived. If we are the client (we 649 * initiated connection), we are called with a pointer to the 650 * interface out which this connection should go. 651 */ 652 u_long 653 tcp_mss_to_advertise(ifp) 654 const struct ifnet *ifp; 655 { 656 extern u_long in_maxmtu; 657 u_long mss = 0; 658 659 /* 660 * In order to avoid defeating path MTU discovery on the peer, 661 * we advertise the max MTU of all attached networks as our MSS, 662 * per RFC 1191, section 3.1. 663 * 664 * We provide the option to advertise just the MTU of 665 * the interface on which we hope this connection will 666 * be receiving. If we are responding to a SYN, we 667 * will have a pretty good idea about this, but when 668 * initiating a connection there is a bit more doubt. 669 * 670 * We also need to ensure that loopback has a large enough 671 * MSS, as the loopback MTU is never included in in_maxmtu. 672 */ 673 674 if (ifp != NULL) 675 mss = ifp->if_mtu; 676 677 if (tcp_mss_ifmtu == 0) 678 mss = max(in_maxmtu, mss); 679 680 if (mss > sizeof(struct tcpiphdr)) 681 mss -= sizeof(struct tcpiphdr); 682 683 mss = max(tcp_mssdflt, mss); 684 return (mss); 685 } 686 687 /* 688 * Set connection variables based on the peer's advertised MSS. 689 * We are passed the TCPCB for the actual connection. If we 690 * are the server, we are called by the compressed state engine 691 * when the 3-way handshake is complete. If we are the client, 692 * we are called when we recieve the SYN,ACK from the server. 693 * 694 * NOTE: Our advertised MSS value must be initialized in the TCPCB 695 * before this routine is called! 696 */ 697 void 698 tcp_mss_from_peer(tp, offer) 699 struct tcpcb *tp; 700 int offer; 701 { 702 struct inpcb *inp = tp->t_inpcb; 703 struct socket *so = inp->inp_socket; 704 #if defined(RTV_SPIPE) || defined(RTV_SSTHRESH) 705 struct rtentry *rt = in_pcbrtentry(inp); 706 #endif 707 u_long bufsize; 708 int mss; 709 710 /* 711 * As per RFC1122, use the default MSS value, unless they 712 * sent us an offer. Do not accept offers less than 32 bytes. 713 */ 714 mss = tcp_mssdflt; 715 if (offer) 716 mss = offer; 717 mss = max(mss, 32); /* sanity */ 718 tp->t_peermss = mss; 719 mss -= (tcp_optlen(tp) + ip_optlen(tp->t_inpcb)); 720 721 /* 722 * If there's a pipesize, change the socket buffer to that size. 723 * Make the socket buffer an integral number of MSS units. If 724 * the MSS is larger than the socket buffer, artificially decrease 725 * the MSS. 726 */ 727 #ifdef RTV_SPIPE 728 if (rt != NULL && rt->rt_rmx.rmx_sendpipe != 0) 729 bufsize = rt->rt_rmx.rmx_sendpipe; 730 else 731 #endif 732 bufsize = so->so_snd.sb_hiwat; 733 if (bufsize < mss) 734 mss = bufsize; 735 else { 736 bufsize = roundup(bufsize, mss); 737 if (bufsize > sb_max) 738 bufsize = sb_max; 739 (void) sbreserve(&so->so_snd, bufsize); 740 } 741 tp->t_segsz = mss; 742 743 #ifdef RTV_SSTHRESH 744 if (rt != NULL && rt->rt_rmx.rmx_ssthresh) { 745 /* 746 * There's some sort of gateway or interface buffer 747 * limit on the path. Use this to set the slow 748 * start threshold, but set the threshold to no less 749 * than 2 * MSS. 750 */ 751 tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh); 752 } 753 #endif 754 } 755 756 /* 757 * Processing necessary when a TCP connection is established. 758 */ 759 void 760 tcp_established(tp) 761 struct tcpcb *tp; 762 { 763 struct inpcb *inp = tp->t_inpcb; 764 struct socket *so = inp->inp_socket; 765 #ifdef RTV_RPIPE 766 struct rtentry *rt = in_pcbrtentry(inp); 767 #endif 768 u_long bufsize; 769 770 tp->t_state = TCPS_ESTABLISHED; 771 TCP_TIMER_ARM(tp, TCPT_KEEP, tcp_keepidle); 772 773 #ifdef RTV_RPIPE 774 if (rt != NULL && rt->rt_rmx.rmx_recvpipe != 0) 775 bufsize = rt->rt_rmx.rmx_recvpipe; 776 else 777 #endif 778 bufsize = so->so_rcv.sb_hiwat; 779 if (bufsize > tp->t_ourmss) { 780 bufsize = roundup(bufsize, tp->t_ourmss); 781 if (bufsize > sb_max) 782 bufsize = sb_max; 783 (void) sbreserve(&so->so_rcv, bufsize); 784 } 785 } 786 787 /* 788 * Check if there's an initial rtt or rttvar. Convert from the 789 * route-table units to scaled multiples of the slow timeout timer. 790 * Called only during the 3-way handshake. 791 */ 792 void 793 tcp_rmx_rtt(tp) 794 struct tcpcb *tp; 795 { 796 #ifdef RTV_RTT 797 struct rtentry *rt; 798 int rtt; 799 800 if ((rt = in_pcbrtentry(tp->t_inpcb)) == NULL) 801 return; 802 803 if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) { 804 /* 805 * XXX The lock bit for MTU indicates that the value 806 * is also a minimum value; this is subject to time. 807 */ 808 if (rt->rt_rmx.rmx_locks & RTV_RTT) 809 TCPT_RANGESET(tp->t_rttmin, 810 rtt / (RTM_RTTUNIT / PR_SLOWHZ), 811 TCPTV_MIN, TCPTV_REXMTMAX); 812 tp->t_srtt = rtt / 813 ((RTM_RTTUNIT / PR_SLOWHZ) >> (TCP_RTT_SHIFT + 2)); 814 if (rt->rt_rmx.rmx_rttvar) { 815 tp->t_rttvar = rt->rt_rmx.rmx_rttvar / 816 ((RTM_RTTUNIT / PR_SLOWHZ) >> 817 (TCP_RTTVAR_SHIFT + 2)); 818 } else { 819 /* Default variation is +- 1 rtt */ 820 tp->t_rttvar = 821 tp->t_srtt >> (TCP_RTT_SHIFT - TCP_RTTVAR_SHIFT); 822 } 823 TCPT_RANGESET(tp->t_rxtcur, 824 ((tp->t_srtt >> 2) + tp->t_rttvar) >> (1 + 2), 825 tp->t_rttmin, TCPTV_REXMTMAX); 826 } 827 #endif 828 } 829 830 tcp_seq tcp_iss_seq = 0; /* tcp initial seq # */ 831 832 /* 833 * Get a new sequence value given a tcp control block 834 */ 835 tcp_seq 836 tcp_new_iss(tp, len, addin) 837 void *tp; 838 u_long len; 839 tcp_seq addin; 840 { 841 tcp_seq tcp_iss; 842 843 /* 844 * add randomness about this connection, but do not estimate 845 * entropy from the timing, since the physical device driver would 846 * have done that for us. 847 */ 848 #if NRND > 0 849 if (tp != NULL) 850 rnd_add_data(NULL, tp, len, 0); 851 #endif 852 853 /* 854 * randomize. 855 */ 856 #if NRND > 0 857 rnd_extract_data(&tcp_iss, sizeof(tcp_iss), RND_EXTRACT_ANY); 858 #else 859 tcp_iss = random(); 860 #endif 861 862 /* 863 * If we were asked to add some amount to a known value, 864 * we will take a random value obtained above, mask off the upper 865 * bits, and add in the known value. We also add in a constant to 866 * ensure that we are at least a certain distance from the original 867 * value. 868 * 869 * This is used when an old connection is in timed wait 870 * and we have a new one coming in, for instance. 871 */ 872 if (addin != 0) { 873 #ifdef TCPISS_DEBUG 874 printf("Random %08x, ", tcp_iss); 875 #endif 876 tcp_iss &= TCP_ISS_RANDOM_MASK; 877 tcp_iss += addin + TCP_ISSINCR; 878 #ifdef TCPISS_DEBUG 879 printf("Old ISS %08x, ISS %08x\n", addin, tcp_iss); 880 #endif 881 } else { 882 tcp_iss &= TCP_ISS_RANDOM_MASK; 883 tcp_iss += tcp_iss_seq; 884 tcp_iss_seq += TCP_ISSINCR; 885 #ifdef TCPISS_DEBUG 886 printf("ISS %08x\n", tcp_iss); 887 #endif 888 } 889 890 if (tcp_compat_42) { 891 /* 892 * Limit it to the positive range for really old TCP 893 * implementations. 894 */ 895 if (tcp_iss >= 0x80000000) 896 tcp_iss &= 0x7fffffff; /* XXX */ 897 } 898 899 return tcp_iss; 900 } 901 902 903 /* 904 * Determine the length of the TCP options for this connection. 905 * 906 * XXX: What do we do for SACK, when we add that? Just reserve 907 * all of the space? Otherwise we can't exactly be incrementing 908 * cwnd by an amount that varies depending on the amount we last 909 * had to SACK! 910 */ 911 912 u_int 913 tcp_optlen(tp) 914 struct tcpcb *tp; 915 { 916 if ((tp->t_flags & (TF_REQ_TSTMP|TF_RCVD_TSTMP|TF_NOOPT)) == 917 (TF_REQ_TSTMP | TF_RCVD_TSTMP)) 918 return TCPOLEN_TSTAMP_APPA; 919 else 920 return 0; 921 } 922