1 /* $NetBSD: tcp_subr.c,v 1.20 1995/11/21 01:07:41 cgd Exp $ */ 2 3 /* 4 * Copyright (c) 1982, 1986, 1988, 1990, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 * @(#)tcp_subr.c 8.1 (Berkeley) 6/10/93 36 */ 37 38 #include <sys/param.h> 39 #include <sys/proc.h> 40 #include <sys/systm.h> 41 #include <sys/malloc.h> 42 #include <sys/mbuf.h> 43 #include <sys/socket.h> 44 #include <sys/socketvar.h> 45 #include <sys/protosw.h> 46 #include <sys/errno.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/ip_var.h> 56 #include <netinet/ip_icmp.h> 57 #include <netinet/tcp.h> 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 64 /* patchable/settable parameters for tcp */ 65 int tcp_mssdflt = TCP_MSS; 66 int tcp_rttdflt = TCPTV_SRTTDFLT / PR_SLOWHZ; 67 int tcp_do_rfc1323 = 1; 68 69 extern struct inpcb *tcp_last_inpcb; 70 71 /* 72 * Tcp initialization 73 */ 74 void 75 tcp_init() 76 { 77 78 tcp_iss = 1; /* wrong */ 79 in_pcbinit(&tcbtable); 80 if (max_protohdr < sizeof(struct tcpiphdr)) 81 max_protohdr = sizeof(struct tcpiphdr); 82 if (max_linkhdr + sizeof(struct tcpiphdr) > MHLEN) 83 panic("tcp_init"); 84 } 85 86 /* 87 * Create template to be used to send tcp packets on a connection. 88 * Call after host entry created, allocates an mbuf and fills 89 * in a skeletal tcp/ip header, minimizing the amount of work 90 * necessary when the connection is used. 91 */ 92 struct tcpiphdr * 93 tcp_template(tp) 94 struct tcpcb *tp; 95 { 96 register struct inpcb *inp = tp->t_inpcb; 97 register struct mbuf *m; 98 register struct tcpiphdr *n; 99 100 if ((n = tp->t_template) == 0) { 101 m = m_get(M_DONTWAIT, MT_HEADER); 102 if (m == NULL) 103 return (0); 104 m->m_len = sizeof (struct tcpiphdr); 105 n = mtod(m, struct tcpiphdr *); 106 } 107 bzero(n->ti_x1, sizeof n->ti_x1); 108 n->ti_pr = IPPROTO_TCP; 109 n->ti_len = htons(sizeof (struct tcpiphdr) - sizeof (struct ip)); 110 n->ti_src = inp->inp_laddr; 111 n->ti_dst = inp->inp_faddr; 112 n->ti_sport = inp->inp_lport; 113 n->ti_dport = inp->inp_fport; 114 n->ti_seq = 0; 115 n->ti_ack = 0; 116 n->ti_x2 = 0; 117 n->ti_off = 5; 118 n->ti_flags = 0; 119 n->ti_win = 0; 120 n->ti_sum = 0; 121 n->ti_urp = 0; 122 return (n); 123 } 124 125 /* 126 * Send a single message to the TCP at address specified by 127 * the given TCP/IP header. If m == 0, then we make a copy 128 * of the tcpiphdr at ti and send directly to the addressed host. 129 * This is used to force keep alive messages out using the TCP 130 * template for a connection tp->t_template. If flags are given 131 * then we send a message back to the TCP which originated the 132 * segment ti, and discard the mbuf containing it and any other 133 * attached mbufs. 134 * 135 * In any case the ack and sequence number of the transmitted 136 * segment are as specified by the parameters. 137 */ 138 void 139 tcp_respond(tp, ti, m, ack, seq, flags) 140 struct tcpcb *tp; 141 register struct tcpiphdr *ti; 142 register struct mbuf *m; 143 tcp_seq ack, seq; 144 int flags; 145 { 146 register int tlen; 147 int win = 0; 148 struct route *ro = 0; 149 150 if (tp) { 151 win = sbspace(&tp->t_inpcb->inp_socket->so_rcv); 152 ro = &tp->t_inpcb->inp_route; 153 } 154 if (m == 0) { 155 m = m_gethdr(M_DONTWAIT, MT_HEADER); 156 if (m == NULL) 157 return; 158 #ifdef TCP_COMPAT_42 159 tlen = 1; 160 #else 161 tlen = 0; 162 #endif 163 m->m_data += max_linkhdr; 164 *mtod(m, struct tcpiphdr *) = *ti; 165 ti = mtod(m, struct tcpiphdr *); 166 flags = TH_ACK; 167 } else { 168 m_freem(m->m_next); 169 m->m_next = 0; 170 m->m_data = (caddr_t)ti; 171 m->m_len = sizeof (struct tcpiphdr); 172 tlen = 0; 173 #define xchg(a,b,type) { type t; t=a; a=b; b=t; } 174 xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr, u_int32_t); 175 xchg(ti->ti_dport, ti->ti_sport, u_int16_t); 176 #undef xchg 177 } 178 ti->ti_len = htons((u_int16_t)(sizeof (struct tcphdr) + tlen)); 179 tlen += sizeof (struct tcpiphdr); 180 m->m_len = tlen; 181 m->m_pkthdr.len = tlen; 182 m->m_pkthdr.rcvif = (struct ifnet *) 0; 183 bzero(ti->ti_x1, sizeof ti->ti_x1); 184 ti->ti_seq = htonl(seq); 185 ti->ti_ack = htonl(ack); 186 ti->ti_x2 = 0; 187 ti->ti_off = sizeof (struct tcphdr) >> 2; 188 ti->ti_flags = flags; 189 if (tp) 190 ti->ti_win = htons((u_int16_t) (win >> tp->rcv_scale)); 191 else 192 ti->ti_win = htons((u_int16_t)win); 193 ti->ti_urp = 0; 194 ti->ti_sum = 0; 195 ti->ti_sum = in_cksum(m, tlen); 196 ((struct ip *)ti)->ip_len = tlen; 197 ((struct ip *)ti)->ip_ttl = ip_defttl; 198 (void) ip_output(m, NULL, ro, 0, NULL); 199 } 200 201 /* 202 * Create a new TCP control block, making an 203 * empty reassembly queue and hooking it to the argument 204 * protocol control block. 205 */ 206 struct tcpcb * 207 tcp_newtcpcb(inp) 208 struct inpcb *inp; 209 { 210 register struct tcpcb *tp; 211 212 tp = malloc(sizeof(*tp), M_PCB, M_NOWAIT); 213 if (tp == NULL) 214 return ((struct tcpcb *)0); 215 bzero((char *) tp, sizeof(struct tcpcb)); 216 LIST_INIT(&tp->segq); 217 tp->t_maxseg = tcp_mssdflt; 218 219 tp->t_flags = tcp_do_rfc1323 ? (TF_REQ_SCALE|TF_REQ_TSTMP) : 0; 220 tp->t_inpcb = inp; 221 /* 222 * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no 223 * rtt estimate. Set rttvar so that srtt + 2 * rttvar gives 224 * reasonable initial retransmit time. 225 */ 226 tp->t_srtt = TCPTV_SRTTBASE; 227 tp->t_rttvar = tcp_rttdflt * PR_SLOWHZ << (TCP_RTTVAR_SHIFT + 2 - 1); 228 tp->t_rttmin = TCPTV_MIN; 229 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 230 TCPTV_MIN, TCPTV_REXMTMAX); 231 tp->snd_cwnd = TCP_MAXWIN << TCP_MAX_WINSHIFT; 232 tp->snd_ssthresh = TCP_MAXWIN << TCP_MAX_WINSHIFT; 233 inp->inp_ip.ip_ttl = ip_defttl; 234 inp->inp_ppcb = (caddr_t)tp; 235 return (tp); 236 } 237 238 /* 239 * Drop a TCP connection, reporting 240 * the specified error. If connection is synchronized, 241 * then send a RST to peer. 242 */ 243 struct tcpcb * 244 tcp_drop(tp, errno) 245 register struct tcpcb *tp; 246 int errno; 247 { 248 struct socket *so = tp->t_inpcb->inp_socket; 249 250 if (TCPS_HAVERCVDSYN(tp->t_state)) { 251 tp->t_state = TCPS_CLOSED; 252 (void) tcp_output(tp); 253 tcpstat.tcps_drops++; 254 } else 255 tcpstat.tcps_conndrops++; 256 if (errno == ETIMEDOUT && tp->t_softerror) 257 errno = tp->t_softerror; 258 so->so_error = errno; 259 return (tcp_close(tp)); 260 } 261 262 /* 263 * Close a TCP control block: 264 * discard all space held by the tcp 265 * discard internet protocol block 266 * wake up any sleepers 267 */ 268 struct tcpcb * 269 tcp_close(tp) 270 register struct tcpcb *tp; 271 { 272 register struct ipqent *qe; 273 struct inpcb *inp = tp->t_inpcb; 274 struct socket *so = inp->inp_socket; 275 register struct mbuf *m; 276 #ifdef RTV_RTT 277 register struct rtentry *rt; 278 279 /* 280 * If we sent enough data to get some meaningful characteristics, 281 * save them in the routing entry. 'Enough' is arbitrarily 282 * defined as the sendpipesize (default 4K) * 16. This would 283 * give us 16 rtt samples assuming we only get one sample per 284 * window (the usual case on a long haul net). 16 samples is 285 * enough for the srtt filter to converge to within 5% of the correct 286 * value; fewer samples and we could save a very bogus rtt. 287 * 288 * Don't update the default route's characteristics and don't 289 * update anything that the user "locked". 290 */ 291 if (SEQ_LT(tp->iss + so->so_snd.sb_hiwat * 16, tp->snd_max) && 292 (rt = inp->inp_route.ro_rt) && 293 satosin(rt_key(rt))->sin_addr.s_addr != INADDR_ANY) { 294 register u_long i; 295 296 if ((rt->rt_rmx.rmx_locks & RTV_RTT) == 0) { 297 i = tp->t_srtt * 298 (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTT_SCALE)); 299 if (rt->rt_rmx.rmx_rtt && i) 300 /* 301 * filter this update to half the old & half 302 * the new values, converting scale. 303 * See route.h and tcp_var.h for a 304 * description of the scaling constants. 305 */ 306 rt->rt_rmx.rmx_rtt = 307 (rt->rt_rmx.rmx_rtt + i) / 2; 308 else 309 rt->rt_rmx.rmx_rtt = i; 310 } 311 if ((rt->rt_rmx.rmx_locks & RTV_RTTVAR) == 0) { 312 i = tp->t_rttvar * 313 (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTTVAR_SCALE)); 314 if (rt->rt_rmx.rmx_rttvar && i) 315 rt->rt_rmx.rmx_rttvar = 316 (rt->rt_rmx.rmx_rttvar + i) / 2; 317 else 318 rt->rt_rmx.rmx_rttvar = i; 319 } 320 /* 321 * update the pipelimit (ssthresh) if it has been updated 322 * already or if a pipesize was specified & the threshhold 323 * got below half the pipesize. I.e., wait for bad news 324 * before we start updating, then update on both good 325 * and bad news. 326 */ 327 if ((rt->rt_rmx.rmx_locks & RTV_SSTHRESH) == 0 && 328 (i = tp->snd_ssthresh) && rt->rt_rmx.rmx_ssthresh || 329 i < (rt->rt_rmx.rmx_sendpipe / 2)) { 330 /* 331 * convert the limit from user data bytes to 332 * packets then to packet data bytes. 333 */ 334 i = (i + tp->t_maxseg / 2) / tp->t_maxseg; 335 if (i < 2) 336 i = 2; 337 i *= (u_long)(tp->t_maxseg + sizeof (struct tcpiphdr)); 338 if (rt->rt_rmx.rmx_ssthresh) 339 rt->rt_rmx.rmx_ssthresh = 340 (rt->rt_rmx.rmx_ssthresh + i) / 2; 341 else 342 rt->rt_rmx.rmx_ssthresh = i; 343 } 344 } 345 #endif /* RTV_RTT */ 346 /* free the reassembly queue, if any */ 347 while ((qe = tp->segq.lh_first) != NULL) { 348 LIST_REMOVE(qe, ipqe_q); 349 m_freem(qe->ipqe_m); 350 FREE(qe, M_IPQ); 351 } 352 if (tp->t_template) 353 (void) m_free(dtom(tp->t_template)); 354 free(tp, M_PCB); 355 inp->inp_ppcb = 0; 356 soisdisconnected(so); 357 /* clobber input pcb cache if we're closing the cached connection */ 358 if (inp == tcp_last_inpcb) 359 tcp_last_inpcb = 0; 360 in_pcbdetach(inp); 361 tcpstat.tcps_closed++; 362 return ((struct tcpcb *)0); 363 } 364 365 void 366 tcp_drain() 367 { 368 369 } 370 371 /* 372 * Notify a tcp user of an asynchronous error; 373 * store error as soft error, but wake up user 374 * (for now, won't do anything until can select for soft error). 375 */ 376 void 377 tcp_notify(inp, error) 378 struct inpcb *inp; 379 int error; 380 { 381 register struct tcpcb *tp = (struct tcpcb *)inp->inp_ppcb; 382 register struct socket *so = inp->inp_socket; 383 384 /* 385 * Ignore some errors if we are hooked up. 386 * If connection hasn't completed, has retransmitted several times, 387 * and receives a second error, give up now. This is better 388 * than waiting a long time to establish a connection that 389 * can never complete. 390 */ 391 if (tp->t_state == TCPS_ESTABLISHED && 392 (error == EHOSTUNREACH || error == ENETUNREACH || 393 error == EHOSTDOWN)) { 394 return; 395 } else if (TCPS_HAVEESTABLISHED(tp->t_state) == 0 && 396 tp->t_rxtshift > 3 && tp->t_softerror) 397 so->so_error = error; 398 else 399 tp->t_softerror = error; 400 wakeup((caddr_t) &so->so_timeo); 401 sorwakeup(so); 402 sowwakeup(so); 403 } 404 405 void 406 tcp_ctlinput(cmd, sa, ip) 407 int cmd; 408 struct sockaddr *sa; 409 register struct ip *ip; 410 { 411 register struct tcphdr *th; 412 extern struct in_addr zeroin_addr; 413 extern int inetctlerrmap[]; 414 void (*notify) __P((struct inpcb *, int)) = tcp_notify; 415 int errno; 416 417 if ((unsigned)cmd >= PRC_NCMDS) 418 return; 419 errno = inetctlerrmap[cmd]; 420 if (cmd == PRC_QUENCH) 421 notify = tcp_quench; 422 else if (PRC_IS_REDIRECT(cmd)) 423 notify = in_rtchange, ip = 0; 424 else if (cmd == PRC_HOSTDEAD) 425 ip = 0; 426 else if (errno == 0) 427 return; 428 if (ip) { 429 th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2)); 430 in_pcbnotify(&tcbtable, sa, th->th_dport, ip->ip_src, 431 th->th_sport, errno, notify); 432 } else 433 in_pcbnotifyall(&tcbtable, sa, errno, notify); 434 } 435 436 /* 437 * When a source quench is received, close congestion window 438 * to one segment. We will gradually open it again as we proceed. 439 */ 440 void 441 tcp_quench(inp, errno) 442 struct inpcb *inp; 443 int errno; 444 { 445 struct tcpcb *tp = intotcpcb(inp); 446 447 if (tp) 448 tp->snd_cwnd = tp->t_maxseg; 449 } 450