1 /* $NetBSD: tcp_subr.c,v 1.66 1999/02/28 13:41:24 explorer 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 struct route iproute, *ro; 216 struct rtentry *rt; 217 struct sockaddr_in *dst; 218 int error, tlen, win = 0; 219 220 if (tp != NULL && (flags & TH_RST) == 0) 221 win = sbspace(&tp->t_inpcb->inp_socket->so_rcv); 222 223 if (m == 0) { 224 m = m_gethdr(M_DONTWAIT, MT_HEADER); 225 if (m == NULL) 226 return (ENOBUFS); 227 228 if (tcp_compat_42) 229 tlen = 1; 230 else 231 tlen = 0; 232 233 m->m_data += max_linkhdr; 234 *mtod(m, struct tcpiphdr *) = *ti; 235 ti = mtod(m, struct tcpiphdr *); 236 flags = TH_ACK; 237 } else { 238 m_freem(m->m_next); 239 m->m_next = 0; 240 m->m_data = (caddr_t)ti; 241 m->m_len = sizeof (struct tcpiphdr); 242 tlen = 0; 243 #define xchg(a,b,type) { type t; t=a; a=b; b=t; } 244 xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr, u_int32_t); 245 xchg(ti->ti_dport, ti->ti_sport, u_int16_t); 246 #undef xchg 247 } 248 bzero(ti->ti_x1, sizeof ti->ti_x1); 249 ti->ti_seq = htonl(seq); 250 ti->ti_ack = htonl(ack); 251 ti->ti_x2 = 0; 252 if ((flags & TH_SYN) == 0) { 253 if (tp) 254 ti->ti_win = htons((u_int16_t) (win >> tp->rcv_scale)); 255 else 256 ti->ti_win = htons((u_int16_t)win); 257 ti->ti_off = sizeof (struct tcphdr) >> 2; 258 tlen += sizeof (struct tcphdr); 259 } else 260 tlen += ti->ti_off << 2; 261 ti->ti_len = htons((u_int16_t)tlen); 262 tlen += sizeof (struct ip); 263 m->m_len = tlen; 264 m->m_pkthdr.len = tlen; 265 m->m_pkthdr.rcvif = (struct ifnet *) 0; 266 ti->ti_flags = flags; 267 ti->ti_urp = 0; 268 ti->ti_sum = 0; 269 ti->ti_sum = in_cksum(m, tlen); 270 ((struct ip *)ti)->ip_len = tlen; 271 ((struct ip *)ti)->ip_ttl = ip_defttl; 272 273 /* 274 * If we're doing Path MTU discovery, we need to set DF unless 275 * the route's MTU is locked. If we lack a route, we need to 276 * look it up now. 277 * 278 * ip_output() could do this for us, but it's convenient to just 279 * do it here unconditionally. 280 */ 281 if (tp != NULL) { 282 ro = &tp->t_inpcb->inp_route; 283 #ifdef DIAGNOSTIC 284 if (!in_hosteq(ti->ti_dst, tp->t_inpcb->inp_faddr)) 285 panic("tcp_respond: ti_dst %x != inp_faddr %x", 286 ntohl(ti->ti_dst.s_addr), 287 ntohl(tp->t_inpcb->inp_faddr.s_addr)); 288 #endif 289 } else { 290 ro = &iproute; 291 bzero(ro, sizeof(*ro)); 292 } 293 if ((rt = ro->ro_rt) == NULL || (rt->rt_flags & RTF_UP) == 0) { 294 if (ro->ro_rt != NULL) { 295 RTFREE(ro->ro_rt); 296 ro->ro_rt = NULL; 297 } 298 dst = satosin(&ro->ro_dst); 299 dst->sin_family = AF_INET; 300 dst->sin_len = sizeof(*dst); 301 dst->sin_addr = ti->ti_dst; 302 rtalloc(ro); 303 if ((rt = ro->ro_rt) == NULL) { 304 m_freem(m); 305 ipstat.ips_noroute++; 306 return (EHOSTUNREACH); 307 } 308 } 309 if (ip_mtudisc != 0 && (rt->rt_rmx.rmx_locks & RTV_MTU) == 0) 310 ((struct ip *)ti)->ip_off |= IP_DF; 311 312 error = ip_output(m, NULL, ro, 0, NULL); 313 314 if (ro == &iproute) { 315 RTFREE(ro->ro_rt); 316 ro->ro_rt = NULL; 317 } 318 319 return (error); 320 } 321 322 /* 323 * Create a new TCP control block, making an 324 * empty reassembly queue and hooking it to the argument 325 * protocol control block. 326 */ 327 struct tcpcb * 328 tcp_newtcpcb(inp) 329 struct inpcb *inp; 330 { 331 register struct tcpcb *tp; 332 333 tp = pool_get(&tcpcb_pool, PR_NOWAIT); 334 if (tp == NULL) 335 return (NULL); 336 bzero((caddr_t)tp, sizeof(struct tcpcb)); 337 LIST_INIT(&tp->segq); 338 LIST_INIT(&tp->timeq); 339 tp->t_peermss = tcp_mssdflt; 340 tp->t_ourmss = tcp_mssdflt; 341 tp->t_segsz = tcp_mssdflt; 342 343 tp->t_flags = 0; 344 if (tcp_do_rfc1323 && tcp_do_win_scale) 345 tp->t_flags |= TF_REQ_SCALE; 346 if (tcp_do_rfc1323 && tcp_do_timestamps) 347 tp->t_flags |= TF_REQ_TSTMP; 348 if (tcp_do_sack == 2) 349 tp->t_flags |= TF_WILL_SACK; 350 else if (tcp_do_sack == 1) 351 tp->t_flags |= TF_WILL_SACK|TF_IGNR_RXSACK; 352 tp->t_flags |= TF_CANT_TXSACK; 353 tp->t_inpcb = inp; 354 /* 355 * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no 356 * rtt estimate. Set rttvar so that srtt + 2 * rttvar gives 357 * reasonable initial retransmit time. 358 */ 359 tp->t_srtt = TCPTV_SRTTBASE; 360 tp->t_rttvar = tcp_rttdflt * PR_SLOWHZ << (TCP_RTTVAR_SHIFT + 2 - 1); 361 tp->t_rttmin = TCPTV_MIN; 362 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 363 TCPTV_MIN, TCPTV_REXMTMAX); 364 tp->snd_cwnd = TCP_MAXWIN << TCP_MAX_WINSHIFT; 365 tp->snd_ssthresh = TCP_MAXWIN << TCP_MAX_WINSHIFT; 366 inp->inp_ip.ip_ttl = ip_defttl; 367 inp->inp_ppcb = (caddr_t)tp; 368 return (tp); 369 } 370 371 /* 372 * Drop a TCP connection, reporting 373 * the specified error. If connection is synchronized, 374 * then send a RST to peer. 375 */ 376 struct tcpcb * 377 tcp_drop(tp, errno) 378 register struct tcpcb *tp; 379 int errno; 380 { 381 struct socket *so = tp->t_inpcb->inp_socket; 382 383 if (TCPS_HAVERCVDSYN(tp->t_state)) { 384 tp->t_state = TCPS_CLOSED; 385 (void) tcp_output(tp); 386 tcpstat.tcps_drops++; 387 } else 388 tcpstat.tcps_conndrops++; 389 if (errno == ETIMEDOUT && tp->t_softerror) 390 errno = tp->t_softerror; 391 so->so_error = errno; 392 return (tcp_close(tp)); 393 } 394 395 /* 396 * Close a TCP control block: 397 * discard all space held by the tcp 398 * discard internet protocol block 399 * wake up any sleepers 400 */ 401 struct tcpcb * 402 tcp_close(tp) 403 register struct tcpcb *tp; 404 { 405 struct inpcb *inp = tp->t_inpcb; 406 struct socket *so = inp->inp_socket; 407 #ifdef RTV_RTT 408 register struct rtentry *rt; 409 410 /* 411 * If we sent enough data to get some meaningful characteristics, 412 * save them in the routing entry. 'Enough' is arbitrarily 413 * defined as the sendpipesize (default 4K) * 16. This would 414 * give us 16 rtt samples assuming we only get one sample per 415 * window (the usual case on a long haul net). 16 samples is 416 * enough for the srtt filter to converge to within 5% of the correct 417 * value; fewer samples and we could save a very bogus rtt. 418 * 419 * Don't update the default route's characteristics and don't 420 * update anything that the user "locked". 421 */ 422 if (SEQ_LT(tp->iss + so->so_snd.sb_hiwat * 16, tp->snd_max) && 423 (rt = inp->inp_route.ro_rt) && 424 !in_nullhost(satosin(rt_key(rt))->sin_addr)) { 425 register u_long i = 0; 426 427 if ((rt->rt_rmx.rmx_locks & RTV_RTT) == 0) { 428 i = tp->t_srtt * 429 ((RTM_RTTUNIT / PR_SLOWHZ) >> (TCP_RTT_SHIFT + 2)); 430 if (rt->rt_rmx.rmx_rtt && i) 431 /* 432 * filter this update to half the old & half 433 * the new values, converting scale. 434 * See route.h and tcp_var.h for a 435 * description of the scaling constants. 436 */ 437 rt->rt_rmx.rmx_rtt = 438 (rt->rt_rmx.rmx_rtt + i) / 2; 439 else 440 rt->rt_rmx.rmx_rtt = i; 441 } 442 if ((rt->rt_rmx.rmx_locks & RTV_RTTVAR) == 0) { 443 i = tp->t_rttvar * 444 ((RTM_RTTUNIT / PR_SLOWHZ) >> (TCP_RTTVAR_SHIFT + 2)); 445 if (rt->rt_rmx.rmx_rttvar && i) 446 rt->rt_rmx.rmx_rttvar = 447 (rt->rt_rmx.rmx_rttvar + i) / 2; 448 else 449 rt->rt_rmx.rmx_rttvar = i; 450 } 451 /* 452 * update the pipelimit (ssthresh) if it has been updated 453 * already or if a pipesize was specified & the threshhold 454 * got below half the pipesize. I.e., wait for bad news 455 * before we start updating, then update on both good 456 * and bad news. 457 */ 458 if (((rt->rt_rmx.rmx_locks & RTV_SSTHRESH) == 0 && 459 (i = tp->snd_ssthresh) && rt->rt_rmx.rmx_ssthresh) || 460 i < (rt->rt_rmx.rmx_sendpipe / 2)) { 461 /* 462 * convert the limit from user data bytes to 463 * packets then to packet data bytes. 464 */ 465 i = (i + tp->t_segsz / 2) / tp->t_segsz; 466 if (i < 2) 467 i = 2; 468 i *= (u_long)(tp->t_segsz + sizeof (struct tcpiphdr)); 469 if (rt->rt_rmx.rmx_ssthresh) 470 rt->rt_rmx.rmx_ssthresh = 471 (rt->rt_rmx.rmx_ssthresh + i) / 2; 472 else 473 rt->rt_rmx.rmx_ssthresh = i; 474 } 475 } 476 #endif /* RTV_RTT */ 477 /* free the reassembly queue, if any */ 478 TCP_REASS_LOCK(tp); 479 (void) tcp_freeq(tp); 480 TCP_REASS_UNLOCK(tp); 481 482 TCP_CLEAR_DELACK(tp); 483 484 if (tp->t_template) 485 pool_put(&tcp_template_pool, tp->t_template); 486 pool_put(&tcpcb_pool, tp); 487 inp->inp_ppcb = 0; 488 soisdisconnected(so); 489 in_pcbdetach(inp); 490 tcpstat.tcps_closed++; 491 return ((struct tcpcb *)0); 492 } 493 494 int 495 tcp_freeq(tp) 496 struct tcpcb *tp; 497 { 498 register struct ipqent *qe; 499 int rv = 0; 500 #ifdef TCPREASS_DEBUG 501 int i = 0; 502 #endif 503 504 TCP_REASS_LOCK_CHECK(tp); 505 506 while ((qe = tp->segq.lh_first) != NULL) { 507 #ifdef TCPREASS_DEBUG 508 printf("tcp_freeq[%p,%d]: %u:%u(%u) 0x%02x\n", 509 tp, i++, qe->ipqe_seq, qe->ipqe_seq + qe->ipqe_len, 510 qe->ipqe_len, qe->ipqe_flags & (TH_SYN|TH_FIN|TH_RST)); 511 #endif 512 LIST_REMOVE(qe, ipqe_q); 513 LIST_REMOVE(qe, ipqe_timeq); 514 m_freem(qe->ipqe_m); 515 pool_put(&ipqent_pool, qe); 516 rv = 1; 517 } 518 return (rv); 519 } 520 521 /* 522 * Protocol drain routine. Called when memory is in short supply. 523 */ 524 void 525 tcp_drain() 526 { 527 register struct inpcb *inp; 528 register struct tcpcb *tp; 529 530 /* 531 * Free the sequence queue of all TCP connections. 532 */ 533 inp = tcbtable.inpt_queue.cqh_first; 534 if (inp) /* XXX */ 535 for (; inp != (struct inpcb *)&tcbtable.inpt_queue; 536 inp = inp->inp_queue.cqe_next) { 537 if ((tp = intotcpcb(inp)) != NULL) { 538 /* 539 * We may be called from a device's interrupt 540 * context. If the tcpcb is already busy, 541 * just bail out now. 542 */ 543 if (tcp_reass_lock_try(tp) == 0) 544 continue; 545 if (tcp_freeq(tp)) 546 tcpstat.tcps_connsdrained++; 547 TCP_REASS_UNLOCK(tp); 548 } 549 } 550 } 551 552 /* 553 * Notify a tcp user of an asynchronous error; 554 * store error as soft error, but wake up user 555 * (for now, won't do anything until can select for soft error). 556 */ 557 void 558 tcp_notify(inp, error) 559 struct inpcb *inp; 560 int error; 561 { 562 register struct tcpcb *tp = (struct tcpcb *)inp->inp_ppcb; 563 register struct socket *so = inp->inp_socket; 564 565 /* 566 * Ignore some errors if we are hooked up. 567 * If connection hasn't completed, has retransmitted several times, 568 * and receives a second error, give up now. This is better 569 * than waiting a long time to establish a connection that 570 * can never complete. 571 */ 572 if (tp->t_state == TCPS_ESTABLISHED && 573 (error == EHOSTUNREACH || error == ENETUNREACH || 574 error == EHOSTDOWN)) { 575 return; 576 } else if (TCPS_HAVEESTABLISHED(tp->t_state) == 0 && 577 tp->t_rxtshift > 3 && tp->t_softerror) 578 so->so_error = error; 579 else 580 tp->t_softerror = error; 581 wakeup((caddr_t) &so->so_timeo); 582 sorwakeup(so); 583 sowwakeup(so); 584 } 585 586 void * 587 tcp_ctlinput(cmd, sa, v) 588 int cmd; 589 struct sockaddr *sa; 590 register void *v; 591 { 592 register struct ip *ip = v; 593 register struct tcphdr *th; 594 extern int inetctlerrmap[]; 595 void (*notify) __P((struct inpcb *, int)) = tcp_notify; 596 int errno; 597 int nmatch; 598 599 if ((unsigned)cmd >= PRC_NCMDS) 600 return NULL; 601 errno = inetctlerrmap[cmd]; 602 if (cmd == PRC_QUENCH) 603 notify = tcp_quench; 604 else if (PRC_IS_REDIRECT(cmd)) 605 notify = in_rtchange, ip = 0; 606 else if (cmd == PRC_MSGSIZE && ip_mtudisc) 607 notify = tcp_mtudisc, ip = 0; 608 else if (cmd == PRC_HOSTDEAD) 609 ip = 0; 610 else if (errno == 0) 611 return NULL; 612 if (ip) { 613 th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2)); 614 nmatch = in_pcbnotify(&tcbtable, satosin(sa)->sin_addr, 615 th->th_dport, ip->ip_src, th->th_sport, errno, notify); 616 if (nmatch == 0 && syn_cache_count && 617 (inetctlerrmap[cmd] == EHOSTUNREACH || 618 inetctlerrmap[cmd] == ENETUNREACH || 619 inetctlerrmap[cmd] == EHOSTDOWN)) 620 syn_cache_unreach(ip, th); 621 } else 622 (void)in_pcbnotifyall(&tcbtable, satosin(sa)->sin_addr, errno, 623 notify); 624 return NULL; 625 } 626 627 /* 628 * When a source quence is received, we are being notifed of congestion. 629 * Close the congestion window down to the Loss Window (one segment). 630 * We will gradually open it again as we proceed. 631 */ 632 void 633 tcp_quench(inp, errno) 634 struct inpcb *inp; 635 int errno; 636 { 637 struct tcpcb *tp = intotcpcb(inp); 638 639 if (tp) 640 tp->snd_cwnd = tp->t_segsz; 641 } 642 643 /* 644 * On receipt of path MTU corrections, flush old route and replace it 645 * with the new one. Retransmit all unacknowledged packets, to ensure 646 * that all packets will be received. 647 */ 648 void 649 tcp_mtudisc(inp, errno) 650 struct inpcb *inp; 651 int errno; 652 { 653 struct tcpcb *tp = intotcpcb(inp); 654 struct rtentry *rt = in_pcbrtentry(inp); 655 656 if (tp != 0) { 657 if (rt != 0) { 658 /* 659 * If this was not a host route, remove and realloc. 660 */ 661 if ((rt->rt_flags & RTF_HOST) == 0) { 662 in_rtchange(inp, errno); 663 if ((rt = in_pcbrtentry(inp)) == 0) 664 return; 665 } 666 667 /* 668 * Slow start out of the error condition. We 669 * use the MTU because we know it's smaller 670 * than the previously transmitted segment. 671 * 672 * Note: This is more conservative than the 673 * suggestion in draft-floyd-incr-init-win-03. 674 */ 675 if (rt->rt_rmx.rmx_mtu != 0) 676 tp->snd_cwnd = 677 TCP_INITIAL_WINDOW(tcp_init_win, 678 rt->rt_rmx.rmx_mtu); 679 } 680 681 /* 682 * Resend unacknowledged packets. 683 */ 684 tp->snd_nxt = tp->snd_una; 685 tcp_output(tp); 686 } 687 } 688 689 690 /* 691 * Compute the MSS to advertise to the peer. Called only during 692 * the 3-way handshake. If we are the server (peer initiated 693 * connection), we are called with a pointer to the interface 694 * on which the SYN packet arrived. If we are the client (we 695 * initiated connection), we are called with a pointer to the 696 * interface out which this connection should go. 697 */ 698 u_long 699 tcp_mss_to_advertise(ifp) 700 const struct ifnet *ifp; 701 { 702 extern u_long in_maxmtu; 703 u_long mss = 0; 704 705 /* 706 * In order to avoid defeating path MTU discovery on the peer, 707 * we advertise the max MTU of all attached networks as our MSS, 708 * per RFC 1191, section 3.1. 709 * 710 * We provide the option to advertise just the MTU of 711 * the interface on which we hope this connection will 712 * be receiving. If we are responding to a SYN, we 713 * will have a pretty good idea about this, but when 714 * initiating a connection there is a bit more doubt. 715 * 716 * We also need to ensure that loopback has a large enough 717 * MSS, as the loopback MTU is never included in in_maxmtu. 718 */ 719 720 if (ifp != NULL) 721 mss = ifp->if_mtu; 722 723 if (tcp_mss_ifmtu == 0) 724 mss = max(in_maxmtu, mss); 725 726 if (mss > sizeof(struct tcpiphdr)) 727 mss -= sizeof(struct tcpiphdr); 728 729 mss = max(tcp_mssdflt, mss); 730 return (mss); 731 } 732 733 /* 734 * Set connection variables based on the peer's advertised MSS. 735 * We are passed the TCPCB for the actual connection. If we 736 * are the server, we are called by the compressed state engine 737 * when the 3-way handshake is complete. If we are the client, 738 * we are called when we recieve the SYN,ACK from the server. 739 * 740 * NOTE: Our advertised MSS value must be initialized in the TCPCB 741 * before this routine is called! 742 */ 743 void 744 tcp_mss_from_peer(tp, offer) 745 struct tcpcb *tp; 746 int offer; 747 { 748 struct inpcb *inp = tp->t_inpcb; 749 struct socket *so = inp->inp_socket; 750 #if defined(RTV_SPIPE) || defined(RTV_SSTHRESH) 751 struct rtentry *rt = in_pcbrtentry(inp); 752 #endif 753 u_long bufsize; 754 int mss; 755 756 /* 757 * As per RFC1122, use the default MSS value, unless they 758 * sent us an offer. Do not accept offers less than 32 bytes. 759 */ 760 mss = tcp_mssdflt; 761 if (offer) 762 mss = offer; 763 mss = max(mss, 32); /* sanity */ 764 tp->t_peermss = mss; 765 mss -= (tcp_optlen(tp) + ip_optlen(tp->t_inpcb)); 766 767 /* 768 * If there's a pipesize, change the socket buffer to that size. 769 * Make the socket buffer an integral number of MSS units. If 770 * the MSS is larger than the socket buffer, artificially decrease 771 * the MSS. 772 */ 773 #ifdef RTV_SPIPE 774 if (rt != NULL && rt->rt_rmx.rmx_sendpipe != 0) 775 bufsize = rt->rt_rmx.rmx_sendpipe; 776 else 777 #endif 778 bufsize = so->so_snd.sb_hiwat; 779 if (bufsize < mss) 780 mss = bufsize; 781 else { 782 bufsize = roundup(bufsize, mss); 783 if (bufsize > sb_max) 784 bufsize = sb_max; 785 (void) sbreserve(&so->so_snd, bufsize); 786 } 787 tp->t_segsz = mss; 788 789 #ifdef RTV_SSTHRESH 790 if (rt != NULL && rt->rt_rmx.rmx_ssthresh) { 791 /* 792 * There's some sort of gateway or interface buffer 793 * limit on the path. Use this to set the slow 794 * start threshold, but set the threshold to no less 795 * than 2 * MSS. 796 */ 797 tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh); 798 } 799 #endif 800 } 801 802 /* 803 * Processing necessary when a TCP connection is established. 804 */ 805 void 806 tcp_established(tp) 807 struct tcpcb *tp; 808 { 809 struct inpcb *inp = tp->t_inpcb; 810 struct socket *so = inp->inp_socket; 811 #ifdef RTV_RPIPE 812 struct rtentry *rt = in_pcbrtentry(inp); 813 #endif 814 u_long bufsize; 815 816 tp->t_state = TCPS_ESTABLISHED; 817 TCP_TIMER_ARM(tp, TCPT_KEEP, tcp_keepidle); 818 819 #ifdef RTV_RPIPE 820 if (rt != NULL && rt->rt_rmx.rmx_recvpipe != 0) 821 bufsize = rt->rt_rmx.rmx_recvpipe; 822 else 823 #endif 824 bufsize = so->so_rcv.sb_hiwat; 825 if (bufsize > tp->t_ourmss) { 826 bufsize = roundup(bufsize, tp->t_ourmss); 827 if (bufsize > sb_max) 828 bufsize = sb_max; 829 (void) sbreserve(&so->so_rcv, bufsize); 830 } 831 } 832 833 /* 834 * Check if there's an initial rtt or rttvar. Convert from the 835 * route-table units to scaled multiples of the slow timeout timer. 836 * Called only during the 3-way handshake. 837 */ 838 void 839 tcp_rmx_rtt(tp) 840 struct tcpcb *tp; 841 { 842 #ifdef RTV_RTT 843 struct rtentry *rt; 844 int rtt; 845 846 if ((rt = in_pcbrtentry(tp->t_inpcb)) == NULL) 847 return; 848 849 if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) { 850 /* 851 * XXX The lock bit for MTU indicates that the value 852 * is also a minimum value; this is subject to time. 853 */ 854 if (rt->rt_rmx.rmx_locks & RTV_RTT) 855 TCPT_RANGESET(tp->t_rttmin, 856 rtt / (RTM_RTTUNIT / PR_SLOWHZ), 857 TCPTV_MIN, TCPTV_REXMTMAX); 858 tp->t_srtt = rtt / 859 ((RTM_RTTUNIT / PR_SLOWHZ) >> (TCP_RTT_SHIFT + 2)); 860 if (rt->rt_rmx.rmx_rttvar) { 861 tp->t_rttvar = rt->rt_rmx.rmx_rttvar / 862 ((RTM_RTTUNIT / PR_SLOWHZ) >> 863 (TCP_RTTVAR_SHIFT + 2)); 864 } else { 865 /* Default variation is +- 1 rtt */ 866 tp->t_rttvar = 867 tp->t_srtt >> (TCP_RTT_SHIFT - TCP_RTTVAR_SHIFT); 868 } 869 TCPT_RANGESET(tp->t_rxtcur, 870 ((tp->t_srtt >> 2) + tp->t_rttvar) >> (1 + 2), 871 tp->t_rttmin, TCPTV_REXMTMAX); 872 } 873 #endif 874 } 875 876 tcp_seq tcp_iss_seq = 0; /* tcp initial seq # */ 877 878 /* 879 * Get a new sequence value given a tcp control block 880 */ 881 tcp_seq 882 tcp_new_iss(tp, len, addin) 883 void *tp; 884 u_long len; 885 tcp_seq addin; 886 { 887 tcp_seq tcp_iss; 888 889 /* 890 * Randomize. 891 */ 892 #if NRND > 0 893 rnd_extract_data(&tcp_iss, sizeof(tcp_iss), RND_EXTRACT_ANY); 894 #else 895 tcp_iss = random(); 896 #endif 897 898 /* 899 * If we were asked to add some amount to a known value, 900 * we will take a random value obtained above, mask off the upper 901 * bits, and add in the known value. We also add in a constant to 902 * ensure that we are at least a certain distance from the original 903 * value. 904 * 905 * This is used when an old connection is in timed wait 906 * and we have a new one coming in, for instance. 907 */ 908 if (addin != 0) { 909 #ifdef TCPISS_DEBUG 910 printf("Random %08x, ", tcp_iss); 911 #endif 912 tcp_iss &= TCP_ISS_RANDOM_MASK; 913 tcp_iss += addin + TCP_ISSINCR; 914 #ifdef TCPISS_DEBUG 915 printf("Old ISS %08x, ISS %08x\n", addin, tcp_iss); 916 #endif 917 } else { 918 tcp_iss &= TCP_ISS_RANDOM_MASK; 919 tcp_iss += tcp_iss_seq; 920 tcp_iss_seq += TCP_ISSINCR; 921 #ifdef TCPISS_DEBUG 922 printf("ISS %08x\n", tcp_iss); 923 #endif 924 } 925 926 if (tcp_compat_42) { 927 /* 928 * Limit it to the positive range for really old TCP 929 * implementations. 930 */ 931 if (tcp_iss >= 0x80000000) 932 tcp_iss &= 0x7fffffff; /* XXX */ 933 } 934 935 return tcp_iss; 936 } 937 938 939 /* 940 * Determine the length of the TCP options for this connection. 941 * 942 * XXX: What do we do for SACK, when we add that? Just reserve 943 * all of the space? Otherwise we can't exactly be incrementing 944 * cwnd by an amount that varies depending on the amount we last 945 * had to SACK! 946 */ 947 948 u_int 949 tcp_optlen(tp) 950 struct tcpcb *tp; 951 { 952 if ((tp->t_flags & (TF_REQ_TSTMP|TF_RCVD_TSTMP|TF_NOOPT)) == 953 (TF_REQ_TSTMP | TF_RCVD_TSTMP)) 954 return TCPOLEN_TSTAMP_APPA; 955 else 956 return 0; 957 } 958