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.4 1994/10/02 17:48:44 phk 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 #include <netinet/tcp_fsm.h> 58 #include <netinet/tcp_seq.h> 59 #include <netinet/tcp_timer.h> 60 #include <netinet/tcp_var.h> 61 #include <netinet/tcpip.h> 62 63 /* patchable/settable parameters for tcp */ 64 int tcp_mssdflt = TCP_MSS; 65 int tcp_rttdflt = TCPTV_SRTTDFLT / PR_SLOWHZ; 66 int tcp_do_rfc1323 = 1; 67 68 extern struct inpcb *tcp_last_inpcb; 69 70 /* 71 * Tcp initialization 72 */ 73 void 74 tcp_init() 75 { 76 77 tcp_iss = 1; /* wrong */ 78 tcb.inp_next = tcb.inp_prev = &tcb; 79 if (max_protohdr < sizeof(struct tcpiphdr)) 80 max_protohdr = sizeof(struct tcpiphdr); 81 if (max_linkhdr + sizeof(struct tcpiphdr) > MHLEN) 82 panic("tcp_init"); 83 } 84 85 /* 86 * Create template to be used to send tcp packets on a connection. 87 * Call after host entry created, allocates an mbuf and fills 88 * in a skeletal tcp/ip header, minimizing the amount of work 89 * necessary when the connection is used. 90 */ 91 struct tcpiphdr * 92 tcp_template(tp) 93 struct tcpcb *tp; 94 { 95 register struct inpcb *inp = tp->t_inpcb; 96 register struct mbuf *m; 97 register struct tcpiphdr *n; 98 99 if ((n = tp->t_template) == 0) { 100 m = m_get(M_DONTWAIT, MT_HEADER); 101 if (m == NULL) 102 return (0); 103 m->m_len = sizeof (struct tcpiphdr); 104 n = mtod(m, struct tcpiphdr *); 105 } 106 n->ti_next = n->ti_prev = 0; 107 n->ti_x1 = 0; 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_long); 175 xchg(ti->ti_dport, ti->ti_sport, u_short); 176 #undef xchg 177 } 178 ti->ti_len = htons((u_short)(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 ti->ti_next = ti->ti_prev = 0; 184 ti->ti_x1 = 0; 185 ti->ti_seq = htonl(seq); 186 ti->ti_ack = htonl(ack); 187 ti->ti_x2 = 0; 188 ti->ti_off = sizeof (struct tcphdr) >> 2; 189 ti->ti_flags = flags; 190 if (tp) 191 ti->ti_win = htons((u_short) (win >> tp->rcv_scale)); 192 else 193 ti->ti_win = htons((u_short)win); 194 ti->ti_urp = 0; 195 ti->ti_sum = 0; 196 ti->ti_sum = in_cksum(m, tlen); 197 ((struct ip *)ti)->ip_len = tlen; 198 ((struct ip *)ti)->ip_ttl = ip_defttl; 199 (void) ip_output(m, NULL, ro, 0, NULL); 200 } 201 202 /* 203 * Create a new TCP control block, making an 204 * empty reassembly queue and hooking it to the argument 205 * protocol control block. 206 */ 207 struct tcpcb * 208 tcp_newtcpcb(inp) 209 struct inpcb *inp; 210 { 211 register struct tcpcb *tp; 212 213 tp = malloc(sizeof(*tp), M_PCB, M_NOWAIT); 214 if (tp == NULL) 215 return ((struct tcpcb *)0); 216 bzero((char *) tp, sizeof(struct tcpcb)); 217 tp->seg_next = tp->seg_prev = (struct tcpiphdr *)tp; 218 tp->t_maxseg = tcp_mssdflt; 219 220 tp->t_flags = tcp_do_rfc1323 ? (TF_REQ_SCALE|TF_REQ_TSTMP) : 0; 221 tp->t_inpcb = inp; 222 /* 223 * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no 224 * rtt estimate. Set rttvar so that srtt + 2 * rttvar gives 225 * reasonable initial retransmit time. 226 */ 227 tp->t_srtt = TCPTV_SRTTBASE; 228 tp->t_rttvar = tcp_rttdflt * PR_SLOWHZ << 2; 229 tp->t_rttmin = TCPTV_MIN; 230 TCPT_RANGESET(tp->t_rxtcur, 231 ((TCPTV_SRTTBASE >> 2) + (TCPTV_SRTTDFLT << 2)) >> 1, 232 TCPTV_MIN, TCPTV_REXMTMAX); 233 tp->snd_cwnd = TCP_MAXWIN << TCP_MAX_WINSHIFT; 234 tp->snd_ssthresh = TCP_MAXWIN << TCP_MAX_WINSHIFT; 235 inp->inp_ip.ip_ttl = ip_defttl; 236 inp->inp_ppcb = (caddr_t)tp; 237 return (tp); 238 } 239 240 /* 241 * Drop a TCP connection, reporting 242 * the specified error. If connection is synchronized, 243 * then send a RST to peer. 244 */ 245 struct tcpcb * 246 tcp_drop(tp, errno) 247 register struct tcpcb *tp; 248 int errno; 249 { 250 struct socket *so = tp->t_inpcb->inp_socket; 251 252 if (TCPS_HAVERCVDSYN(tp->t_state)) { 253 tp->t_state = TCPS_CLOSED; 254 (void) tcp_output(tp); 255 tcpstat.tcps_drops++; 256 } else 257 tcpstat.tcps_conndrops++; 258 if (errno == ETIMEDOUT && tp->t_softerror) 259 errno = tp->t_softerror; 260 so->so_error = errno; 261 return (tcp_close(tp)); 262 } 263 264 /* 265 * Close a TCP control block: 266 * discard all space held by the tcp 267 * discard internet protocol block 268 * wake up any sleepers 269 */ 270 struct tcpcb * 271 tcp_close(tp) 272 register struct tcpcb *tp; 273 { 274 register struct tcpiphdr *t; 275 struct inpcb *inp = tp->t_inpcb; 276 struct socket *so = inp->inp_socket; 277 register struct mbuf *m; 278 #ifdef RTV_RTT 279 register struct rtentry *rt; 280 281 /* 282 * If we sent enough data to get some meaningful characteristics, 283 * save them in the routing entry. 'Enough' is arbitrarily 284 * defined as the sendpipesize (default 4K) * 16. This would 285 * give us 16 rtt samples assuming we only get one sample per 286 * window (the usual case on a long haul net). 16 samples is 287 * enough for the srtt filter to converge to within 5% of the correct 288 * value; fewer samples and we could save a very bogus rtt. 289 * 290 * Don't update the default route's characteristics and don't 291 * update anything that the user "locked". 292 */ 293 if (SEQ_LT(tp->iss + so->so_snd.sb_hiwat * 16, tp->snd_max) && 294 (rt = inp->inp_route.ro_rt) && 295 ((struct sockaddr_in *)rt_key(rt))->sin_addr.s_addr != INADDR_ANY) { 296 register u_long i = 0; 297 298 if ((rt->rt_rmx.rmx_locks & RTV_RTT) == 0) { 299 i = tp->t_srtt * 300 (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTT_SCALE)); 301 if (rt->rt_rmx.rmx_rtt && i) 302 /* 303 * filter this update to half the old & half 304 * the new values, converting scale. 305 * See route.h and tcp_var.h for a 306 * description of the scaling constants. 307 */ 308 rt->rt_rmx.rmx_rtt = 309 (rt->rt_rmx.rmx_rtt + i) / 2; 310 else 311 rt->rt_rmx.rmx_rtt = i; 312 } 313 if ((rt->rt_rmx.rmx_locks & RTV_RTTVAR) == 0) { 314 i = tp->t_rttvar * 315 (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTTVAR_SCALE)); 316 if (rt->rt_rmx.rmx_rttvar && i) 317 rt->rt_rmx.rmx_rttvar = 318 (rt->rt_rmx.rmx_rttvar + i) / 2; 319 else 320 rt->rt_rmx.rmx_rttvar = i; 321 } 322 /* 323 * update the pipelimit (ssthresh) if it has been updated 324 * already or if a pipesize was specified & the threshhold 325 * got below half the pipesize. I.e., wait for bad news 326 * before we start updating, then update on both good 327 * and bad news. 328 */ 329 if (((rt->rt_rmx.rmx_locks & RTV_SSTHRESH) == 0 && 330 ((i = tp->snd_ssthresh) != 0) && rt->rt_rmx.rmx_ssthresh) || 331 i < (rt->rt_rmx.rmx_sendpipe / 2)) { 332 /* 333 * convert the limit from user data bytes to 334 * packets then to packet data bytes. 335 */ 336 i = (i + tp->t_maxseg / 2) / tp->t_maxseg; 337 if (i < 2) 338 i = 2; 339 i *= (u_long)(tp->t_maxseg + sizeof (struct tcpiphdr)); 340 if (rt->rt_rmx.rmx_ssthresh) 341 rt->rt_rmx.rmx_ssthresh = 342 (rt->rt_rmx.rmx_ssthresh + i) / 2; 343 else 344 rt->rt_rmx.rmx_ssthresh = i; 345 } 346 } 347 #endif /* RTV_RTT */ 348 /* free the reassembly queue, if any */ 349 t = tp->seg_next; 350 while (t != (struct tcpiphdr *)tp) { 351 t = (struct tcpiphdr *)t->ti_next; 352 m = REASS_MBUF((struct tcpiphdr *)t->ti_prev); 353 remque(t->ti_prev); 354 m_freem(m); 355 } 356 if (tp->t_template) 357 (void) m_free(dtom(tp->t_template)); 358 free(tp, M_PCB); 359 inp->inp_ppcb = 0; 360 soisdisconnected(so); 361 /* clobber input pcb cache if we're closing the cached connection */ 362 if (inp == tcp_last_inpcb) 363 tcp_last_inpcb = &tcb; 364 in_pcbdetach(inp); 365 tcpstat.tcps_closed++; 366 return ((struct tcpcb *)0); 367 } 368 369 void 370 tcp_drain() 371 { 372 373 } 374 375 /* 376 * Notify a tcp user of an asynchronous error; 377 * store error as soft error, but wake up user 378 * (for now, won't do anything until can select for soft error). 379 */ 380 void 381 tcp_notify(inp, error) 382 struct inpcb *inp; 383 int error; 384 { 385 register struct tcpcb *tp = (struct tcpcb *)inp->inp_ppcb; 386 register struct socket *so = inp->inp_socket; 387 388 /* 389 * Ignore some errors if we are hooked up. 390 * If connection hasn't completed, has retransmitted several times, 391 * and receives a second error, give up now. This is better 392 * than waiting a long time to establish a connection that 393 * can never complete. 394 */ 395 if (tp->t_state == TCPS_ESTABLISHED && 396 (error == EHOSTUNREACH || error == ENETUNREACH || 397 error == EHOSTDOWN)) { 398 return; 399 } else if (tp->t_state < TCPS_ESTABLISHED && tp->t_rxtshift > 3 && 400 tp->t_softerror) 401 so->so_error = error; 402 else 403 tp->t_softerror = error; 404 wakeup((caddr_t) &so->so_timeo); 405 sorwakeup(so); 406 sowwakeup(so); 407 } 408 409 void 410 tcp_ctlinput(cmd, sa, ip) 411 int cmd; 412 struct sockaddr *sa; 413 register struct ip *ip; 414 { 415 register struct tcphdr *th; 416 extern struct in_addr zeroin_addr; 417 extern u_char inetctlerrmap[]; 418 void (*notify) __P((struct inpcb *, int)) = tcp_notify; 419 420 if (cmd == PRC_QUENCH) 421 notify = tcp_quench; 422 else if (!PRC_IS_REDIRECT(cmd) && 423 ((unsigned)cmd > PRC_NCMDS || inetctlerrmap[cmd] == 0)) 424 return; 425 if (ip) { 426 th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2)); 427 in_pcbnotify(&tcb, sa, th->th_dport, ip->ip_src, th->th_sport, 428 cmd, notify); 429 } else 430 in_pcbnotify(&tcb, sa, 0, zeroin_addr, 0, cmd, notify); 431 } 432 433 /* 434 * When a source quench is received, close congestion window 435 * to one segment. We will gradually open it again as we proceed. 436 */ 437 void 438 tcp_quench(inp, errno) 439 struct inpcb *inp; 440 int errno; 441 { 442 struct tcpcb *tp = intotcpcb(inp); 443 444 if (tp) 445 tp->snd_cwnd = tp->t_maxseg; 446 } 447