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