1 /* 2 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)tcp_subr.c 8.2 (Berkeley) 5/24/95 34 * $Id: tcp_subr.c,v 1.20 1995/10/16 18:21:20 wollman Exp $ 35 */ 36 37 #include <sys/param.h> 38 #include <sys/proc.h> 39 #include <sys/systm.h> 40 #include <sys/kernel.h> 41 #include <sys/sysctl.h> 42 #include <sys/malloc.h> 43 #include <sys/mbuf.h> 44 #include <sys/socket.h> 45 #include <sys/socketvar.h> 46 #include <sys/protosw.h> 47 #include <sys/errno.h> 48 #include <sys/queue.h> 49 50 #include <net/route.h> 51 #include <net/if.h> 52 53 #include <netinet/in.h> 54 #include <netinet/in_systm.h> 55 #include <netinet/ip.h> 56 #include <netinet/in_pcb.h> 57 #include <netinet/in_var.h> 58 #include <netinet/ip_var.h> 59 #include <netinet/ip_icmp.h> 60 #include <netinet/tcp.h> 61 #include <netinet/tcp_fsm.h> 62 #include <netinet/tcp_seq.h> 63 #include <netinet/tcp_timer.h> 64 #include <netinet/tcp_var.h> 65 #include <netinet/tcpip.h> 66 #ifdef TCPDEBUG 67 #include <netinet/tcp_debug.h> 68 #endif 69 70 /* patchable/settable parameters for tcp */ 71 int tcp_mssdflt = TCP_MSS; 72 SYSCTL_INT(_net_inet_tcp, TCPCTL_MSSDFLT, mssdflt, 73 CTLFLAG_RW, &tcp_mssdflt , 0, ""); 74 int tcp_rttdflt = TCPTV_SRTTDFLT / PR_SLOWHZ; 75 SYSCTL_INT(_net_inet_tcp, TCPCTL_RTTDFLT, rttdflt, 76 CTLFLAG_RW, &tcp_rttdflt , 0, ""); 77 int tcp_do_rfc1323 = 1; 78 SYSCTL_INT(_net_inet_tcp, TCPCTL_DO_RFC1323, rfc1323, 79 CTLFLAG_RW, &tcp_do_rfc1323 , 0, ""); 80 int tcp_do_rfc1644 = 1; 81 SYSCTL_INT(_net_inet_tcp, TCPCTL_DO_RFC1644, rfc1644, 82 CTLFLAG_RW, &tcp_do_rfc1644 , 0, ""); 83 static void tcp_cleartaocache(void); 84 85 /* 86 * Target size of TCP PCB hash table. Will be rounded down to a prime 87 * number. 88 */ 89 #ifndef TCBHASHSIZE 90 #define TCBHASHSIZE 128 91 #endif 92 93 /* 94 * Tcp initialization 95 */ 96 void 97 tcp_init() 98 { 99 100 tcp_iss = random(); /* wrong, but better than a constant */ 101 tcp_ccgen = 1; 102 tcp_cleartaocache(); 103 LIST_INIT(&tcb); 104 tcbinfo.listhead = &tcb; 105 tcbinfo.hashbase = phashinit(TCBHASHSIZE, M_PCB, &tcbinfo.hashsize); 106 if (max_protohdr < sizeof(struct tcpiphdr)) 107 max_protohdr = sizeof(struct tcpiphdr); 108 if (max_linkhdr + sizeof(struct tcpiphdr) > MHLEN) 109 panic("tcp_init"); 110 } 111 112 /* 113 * Create template to be used to send tcp packets on a connection. 114 * Call after host entry created, allocates an mbuf and fills 115 * in a skeletal tcp/ip header, minimizing the amount of work 116 * necessary when the connection is used. 117 */ 118 struct tcpiphdr * 119 tcp_template(tp) 120 struct tcpcb *tp; 121 { 122 register struct inpcb *inp = tp->t_inpcb; 123 register struct mbuf *m; 124 register struct tcpiphdr *n; 125 126 if ((n = tp->t_template) == 0) { 127 m = m_get(M_DONTWAIT, MT_HEADER); 128 if (m == NULL) 129 return (0); 130 m->m_len = sizeof (struct tcpiphdr); 131 n = mtod(m, struct tcpiphdr *); 132 } 133 n->ti_next = n->ti_prev = 0; 134 n->ti_x1 = 0; 135 n->ti_pr = IPPROTO_TCP; 136 n->ti_len = htons(sizeof (struct tcpiphdr) - sizeof (struct ip)); 137 n->ti_src = inp->inp_laddr; 138 n->ti_dst = inp->inp_faddr; 139 n->ti_sport = inp->inp_lport; 140 n->ti_dport = inp->inp_fport; 141 n->ti_seq = 0; 142 n->ti_ack = 0; 143 n->ti_x2 = 0; 144 n->ti_off = 5; 145 n->ti_flags = 0; 146 n->ti_win = 0; 147 n->ti_sum = 0; 148 n->ti_urp = 0; 149 return (n); 150 } 151 152 /* 153 * Send a single message to the TCP at address specified by 154 * the given TCP/IP header. If m == 0, then we make a copy 155 * of the tcpiphdr at ti and send directly to the addressed host. 156 * This is used to force keep alive messages out using the TCP 157 * template for a connection tp->t_template. If flags are given 158 * then we send a message back to the TCP which originated the 159 * segment ti, and discard the mbuf containing it and any other 160 * attached mbufs. 161 * 162 * In any case the ack and sequence number of the transmitted 163 * segment are as specified by the parameters. 164 */ 165 void 166 tcp_respond(tp, ti, m, ack, seq, flags) 167 struct tcpcb *tp; 168 register struct tcpiphdr *ti; 169 register struct mbuf *m; 170 tcp_seq ack, seq; 171 int flags; 172 { 173 register int tlen; 174 int win = 0; 175 struct route *ro = 0; 176 177 if (tp) { 178 win = sbspace(&tp->t_inpcb->inp_socket->so_rcv); 179 ro = &tp->t_inpcb->inp_route; 180 } 181 if (m == 0) { 182 m = m_gethdr(M_DONTWAIT, MT_HEADER); 183 if (m == NULL) 184 return; 185 #ifdef TCP_COMPAT_42 186 tlen = 1; 187 #else 188 tlen = 0; 189 #endif 190 m->m_data += max_linkhdr; 191 *mtod(m, struct tcpiphdr *) = *ti; 192 ti = mtod(m, struct tcpiphdr *); 193 flags = TH_ACK; 194 } else { 195 m_freem(m->m_next); 196 m->m_next = 0; 197 m->m_data = (caddr_t)ti; 198 m->m_len = sizeof (struct tcpiphdr); 199 tlen = 0; 200 #define xchg(a,b,type) { type t; t=a; a=b; b=t; } 201 xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr, u_long); 202 xchg(ti->ti_dport, ti->ti_sport, u_short); 203 #undef xchg 204 } 205 ti->ti_len = htons((u_short)(sizeof (struct tcphdr) + tlen)); 206 tlen += sizeof (struct tcpiphdr); 207 m->m_len = tlen; 208 m->m_pkthdr.len = tlen; 209 m->m_pkthdr.rcvif = (struct ifnet *) 0; 210 ti->ti_next = ti->ti_prev = 0; 211 ti->ti_x1 = 0; 212 ti->ti_seq = htonl(seq); 213 ti->ti_ack = htonl(ack); 214 ti->ti_x2 = 0; 215 ti->ti_off = sizeof (struct tcphdr) >> 2; 216 ti->ti_flags = flags; 217 if (tp) 218 ti->ti_win = htons((u_short) (win >> tp->rcv_scale)); 219 else 220 ti->ti_win = htons((u_short)win); 221 ti->ti_urp = 0; 222 ti->ti_sum = 0; 223 ti->ti_sum = in_cksum(m, tlen); 224 ((struct ip *)ti)->ip_len = tlen; 225 ((struct ip *)ti)->ip_ttl = ip_defttl; 226 #ifdef TCPDEBUG 227 if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 228 tcp_trace(TA_OUTPUT, 0, tp, ti, 0); 229 #endif 230 (void) ip_output(m, NULL, ro, 0, NULL); 231 } 232 233 /* 234 * Create a new TCP control block, making an 235 * empty reassembly queue and hooking it to the argument 236 * protocol control block. 237 */ 238 struct tcpcb * 239 tcp_newtcpcb(inp) 240 struct inpcb *inp; 241 { 242 register struct tcpcb *tp; 243 244 tp = malloc(sizeof(*tp), M_PCB, M_NOWAIT); 245 if (tp == NULL) 246 return ((struct tcpcb *)0); 247 bzero((char *) tp, sizeof(struct tcpcb)); 248 tp->seg_next = tp->seg_prev = (struct tcpiphdr *)tp; 249 tp->t_maxseg = tp->t_maxopd = tcp_mssdflt; 250 251 if (tcp_do_rfc1323) 252 tp->t_flags = (TF_REQ_SCALE|TF_REQ_TSTMP); 253 if (tcp_do_rfc1644) 254 tp->t_flags |= TF_REQ_CC; 255 tp->t_inpcb = inp; 256 /* 257 * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no 258 * rtt estimate. Set rttvar so that srtt + 2 * rttvar gives 259 * reasonable initial retransmit time. 260 */ 261 tp->t_srtt = TCPTV_SRTTBASE; 262 tp->t_rttvar = tcp_rttdflt * PR_SLOWHZ << 2; 263 tp->t_rttmin = TCPTV_MIN; 264 TCPT_RANGESET(tp->t_rxtcur, 265 ((TCPTV_SRTTBASE >> 2) + (TCPTV_SRTTDFLT << 2)) >> 1, 266 TCPTV_MIN, TCPTV_REXMTMAX); 267 tp->snd_cwnd = TCP_MAXWIN << TCP_MAX_WINSHIFT; 268 tp->snd_ssthresh = TCP_MAXWIN << TCP_MAX_WINSHIFT; 269 inp->inp_ip.ip_ttl = ip_defttl; 270 inp->inp_ppcb = (caddr_t)tp; 271 return (tp); 272 } 273 274 /* 275 * Drop a TCP connection, reporting 276 * the specified error. If connection is synchronized, 277 * then send a RST to peer. 278 */ 279 struct tcpcb * 280 tcp_drop(tp, errno) 281 register struct tcpcb *tp; 282 int errno; 283 { 284 struct socket *so = tp->t_inpcb->inp_socket; 285 286 if (TCPS_HAVERCVDSYN(tp->t_state)) { 287 tp->t_state = TCPS_CLOSED; 288 (void) tcp_output(tp); 289 tcpstat.tcps_drops++; 290 } else 291 tcpstat.tcps_conndrops++; 292 if (errno == ETIMEDOUT && tp->t_softerror) 293 errno = tp->t_softerror; 294 so->so_error = errno; 295 return (tcp_close(tp)); 296 } 297 298 /* 299 * Close a TCP control block: 300 * discard all space held by the tcp 301 * discard internet protocol block 302 * wake up any sleepers 303 */ 304 struct tcpcb * 305 tcp_close(tp) 306 register struct tcpcb *tp; 307 { 308 register struct tcpiphdr *t; 309 struct inpcb *inp = tp->t_inpcb; 310 struct socket *so = inp->inp_socket; 311 register struct mbuf *m; 312 #ifdef RTV_RTT 313 register struct rtentry *rt; 314 315 /* 316 * If we got enough samples through the srtt filter, 317 * save the rtt and rttvar in the routing entry. 318 * 'Enough' is arbitrarily defined as the 16 samples. 319 * 16 samples is enough for the srtt filter to converge 320 * to within 5% of the correct value; fewer samples and 321 * we could save a very bogus rtt. 322 * 323 * Don't update the default route's characteristics and don't 324 * update anything that the user "locked". 325 */ 326 if (tp->t_rttupdated >= 16 && 327 (rt = inp->inp_route.ro_rt) && 328 ((struct sockaddr_in *)rt_key(rt))->sin_addr.s_addr != INADDR_ANY) { 329 register u_long i = 0; 330 331 if ((rt->rt_rmx.rmx_locks & RTV_RTT) == 0) { 332 i = tp->t_srtt * 333 (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTT_SCALE)); 334 if (rt->rt_rmx.rmx_rtt && i) 335 /* 336 * filter this update to half the old & half 337 * the new values, converting scale. 338 * See route.h and tcp_var.h for a 339 * description of the scaling constants. 340 */ 341 rt->rt_rmx.rmx_rtt = 342 (rt->rt_rmx.rmx_rtt + i) / 2; 343 else 344 rt->rt_rmx.rmx_rtt = i; 345 tcpstat.tcps_cachedrtt++; 346 } 347 if ((rt->rt_rmx.rmx_locks & RTV_RTTVAR) == 0) { 348 i = tp->t_rttvar * 349 (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTTVAR_SCALE)); 350 if (rt->rt_rmx.rmx_rttvar && i) 351 rt->rt_rmx.rmx_rttvar = 352 (rt->rt_rmx.rmx_rttvar + i) / 2; 353 else 354 rt->rt_rmx.rmx_rttvar = i; 355 tcpstat.tcps_cachedrttvar++; 356 } 357 /* 358 * update the pipelimit (ssthresh) if it has been updated 359 * already or if a pipesize was specified & the threshhold 360 * got below half the pipesize. I.e., wait for bad news 361 * before we start updating, then update on both good 362 * and bad news. 363 */ 364 if (((rt->rt_rmx.rmx_locks & RTV_SSTHRESH) == 0 && 365 ((i = tp->snd_ssthresh) != 0) && rt->rt_rmx.rmx_ssthresh) || 366 i < (rt->rt_rmx.rmx_sendpipe / 2)) { 367 /* 368 * convert the limit from user data bytes to 369 * packets then to packet data bytes. 370 */ 371 i = (i + tp->t_maxseg / 2) / tp->t_maxseg; 372 if (i < 2) 373 i = 2; 374 i *= (u_long)(tp->t_maxseg + sizeof (struct tcpiphdr)); 375 if (rt->rt_rmx.rmx_ssthresh) 376 rt->rt_rmx.rmx_ssthresh = 377 (rt->rt_rmx.rmx_ssthresh + i) / 2; 378 else 379 rt->rt_rmx.rmx_ssthresh = i; 380 tcpstat.tcps_cachedssthresh++; 381 } 382 } 383 #endif /* RTV_RTT */ 384 /* free the reassembly queue, if any */ 385 t = tp->seg_next; 386 while (t != (struct tcpiphdr *)tp) { 387 t = (struct tcpiphdr *)t->ti_next; 388 m = REASS_MBUF((struct tcpiphdr *)t->ti_prev); 389 remque(t->ti_prev); 390 m_freem(m); 391 } 392 if (tp->t_template) 393 (void) m_free(dtom(tp->t_template)); 394 free(tp, M_PCB); 395 inp->inp_ppcb = 0; 396 soisdisconnected(so); 397 in_pcbdetach(inp); 398 tcpstat.tcps_closed++; 399 return ((struct tcpcb *)0); 400 } 401 402 void 403 tcp_drain() 404 { 405 406 } 407 408 /* 409 * Notify a tcp user of an asynchronous error; 410 * store error as soft error, but wake up user 411 * (for now, won't do anything until can select for soft error). 412 */ 413 void 414 tcp_notify(inp, error) 415 struct inpcb *inp; 416 int error; 417 { 418 register struct tcpcb *tp = (struct tcpcb *)inp->inp_ppcb; 419 register struct socket *so = inp->inp_socket; 420 421 /* 422 * Ignore some errors if we are hooked up. 423 * If connection hasn't completed, has retransmitted several times, 424 * and receives a second error, give up now. This is better 425 * than waiting a long time to establish a connection that 426 * can never complete. 427 */ 428 if (tp->t_state == TCPS_ESTABLISHED && 429 (error == EHOSTUNREACH || error == ENETUNREACH || 430 error == EHOSTDOWN)) { 431 return; 432 } else if (tp->t_state < TCPS_ESTABLISHED && tp->t_rxtshift > 3 && 433 tp->t_softerror) 434 so->so_error = error; 435 else 436 tp->t_softerror = error; 437 wakeup((caddr_t) &so->so_timeo); 438 sorwakeup(so); 439 sowwakeup(so); 440 } 441 442 void 443 tcp_ctlinput(cmd, sa, ip) 444 int cmd; 445 struct sockaddr *sa; 446 register struct ip *ip; 447 { 448 register struct tcphdr *th; 449 void (*notify) __P((struct inpcb *, int)) = tcp_notify; 450 451 if (cmd == PRC_QUENCH) 452 notify = tcp_quench; 453 #ifdef MTUDISC 454 else if (cmd == PRC_MSGSIZE) 455 notify = tcp_mtudisc; 456 #endif /* MTUDISC */ 457 else if (!PRC_IS_REDIRECT(cmd) && 458 ((unsigned)cmd > PRC_NCMDS || inetctlerrmap[cmd] == 0)) 459 return; 460 if (ip) { 461 th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2)); 462 in_pcbnotify(&tcb, sa, th->th_dport, ip->ip_src, th->th_sport, 463 cmd, notify); 464 } else 465 in_pcbnotify(&tcb, sa, 0, zeroin_addr, 0, cmd, notify); 466 } 467 468 /* 469 * When a source quench is received, close congestion window 470 * to one segment. We will gradually open it again as we proceed. 471 */ 472 void 473 tcp_quench(inp, errno) 474 struct inpcb *inp; 475 int errno; 476 { 477 struct tcpcb *tp = intotcpcb(inp); 478 479 if (tp) 480 tp->snd_cwnd = tp->t_maxseg; 481 } 482 483 #ifdef MTUDISC 484 /* 485 * When `need fragmentation' ICMP is received, update our idea of the MSS 486 * based on the new value in the route. Also nudge TCP to send something, 487 * since we know the packet we just sent was dropped. 488 * This duplicates some code in the tcp_mss() function in tcp_input.c. 489 */ 490 void 491 tcp_mtudisc(inp, errno) 492 struct inpcb *inp; 493 int errno; 494 { 495 struct tcpcb *tp = intotcpcb(inp); 496 struct rtentry *rt; 497 struct rmxp_tao *taop; 498 struct socket *so = inp->inp_socket; 499 int offered; 500 int mss; 501 502 if (tp) { 503 rt = tcp_rtlookup(inp); 504 if (!rt || !rt->rt_rmx.rmx_mtu) { 505 tp->t_maxopd = tp->t_maxseg = tcp_mssdflt; 506 return; 507 } 508 taop = rmx_taop(rt->rt_rmx); 509 offered = taop->tao_mssopt; 510 mss = rt->rt_rmx.rmx_mtu - sizeof(struct tcpiphdr); 511 mss = min(mss, offered); 512 if (tp->t_maxopd <= mss) 513 return; 514 tp->t_maxopd = mss; 515 516 if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP && 517 (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP) 518 mss -= TCPOLEN_TSTAMP_APPA; 519 if ((tp->t_flags & (TF_REQ_CC|TF_NOOPT)) == TF_REQ_CC && 520 (tp->t_flags & TF_RCVD_CC) == TF_RCVD_CC) 521 mss -= TCPOLEN_CC_APPA; 522 #if (MCLBYTES & (MCLBYTES - 1)) == 0 523 if (mss > MCLBYTES) 524 mss &= ~(MCLBYTES-1); 525 #else 526 if (mss > MCLBYTES) 527 mss = mss / MCLBYTES * MCLBYTES; 528 #endif 529 if (so->so_snd.sb_hiwat < mss) 530 mss = so->so_snd.sb_hiwat; 531 532 tp->t_maxseg = mss; 533 534 tcpstat.tcps_mturesent++; 535 tp->t_rtt = 0; 536 tp->snd_nxt = tp->snd_una; 537 tcp_output(tp); 538 } 539 } 540 #endif /* MTUDISC */ 541 542 /* 543 * Look-up the routing entry to the peer of this inpcb. If no route 544 * is found and it cannot be allocated the return NULL. This routine 545 * is called by TCP routines that access the rmx structure and by tcp_mss 546 * to get the interface MTU. 547 */ 548 struct rtentry * 549 tcp_rtlookup(inp) 550 struct inpcb *inp; 551 { 552 struct route *ro; 553 struct rtentry *rt; 554 555 ro = &inp->inp_route; 556 rt = ro->ro_rt; 557 if (rt == NULL || !(rt->rt_flags & RTF_UP)) { 558 /* No route yet, so try to acquire one */ 559 if (inp->inp_faddr.s_addr != INADDR_ANY) { 560 ro->ro_dst.sa_family = AF_INET; 561 ro->ro_dst.sa_len = sizeof(ro->ro_dst); 562 ((struct sockaddr_in *) &ro->ro_dst)->sin_addr = 563 inp->inp_faddr; 564 rtalloc(ro); 565 rt = ro->ro_rt; 566 } 567 } 568 return rt; 569 } 570 571 /* 572 * Return a pointer to the cached information about the remote host. 573 * The cached information is stored in the protocol specific part of 574 * the route metrics. 575 */ 576 struct rmxp_tao * 577 tcp_gettaocache(inp) 578 struct inpcb *inp; 579 { 580 struct rtentry *rt = tcp_rtlookup(inp); 581 582 /* Make sure this is a host route and is up. */ 583 if (rt == NULL || 584 (rt->rt_flags & (RTF_UP|RTF_HOST)) != (RTF_UP|RTF_HOST)) 585 return NULL; 586 587 return rmx_taop(rt->rt_rmx); 588 } 589 590 /* 591 * Clear all the TAO cache entries, called from tcp_init. 592 * 593 * XXX 594 * This routine is just an empty one, because we assume that the routing 595 * routing tables are initialized at the same time when TCP, so there is 596 * nothing in the cache left over. 597 */ 598 static void 599 tcp_cleartaocache(void) 600 { } 601