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