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 */ 35 36 #include <sys/param.h> 37 #include <sys/proc.h> 38 #include <sys/systm.h> 39 #include <sys/malloc.h> 40 #include <sys/mbuf.h> 41 #include <sys/socket.h> 42 #include <sys/socketvar.h> 43 #include <sys/protosw.h> 44 #include <sys/errno.h> 45 46 #include <net/route.h> 47 #include <net/if.h> 48 49 #include <netinet/in.h> 50 #include <netinet/in_systm.h> 51 #include <netinet/ip.h> 52 #include <netinet/in_pcb.h> 53 #include <netinet/ip_var.h> 54 #include <netinet/ip_icmp.h> 55 #include <netinet/tcp.h> 56 #include <netinet/tcp_fsm.h> 57 #include <netinet/tcp_seq.h> 58 #include <netinet/tcp_timer.h> 59 #include <netinet/tcp_var.h> 60 #include <netinet/tcpip.h> 61 62 /* patchable/settable parameters for tcp */ 63 int tcp_mssdflt = TCP_MSS; 64 int tcp_rttdflt = TCPTV_SRTTDFLT / PR_SLOWHZ; 65 int tcp_do_rfc1323 = 1; 66 67 extern struct inpcb *tcp_last_inpcb; 68 69 /* 70 * Tcp initialization 71 */ 72 void 73 tcp_init() 74 { 75 76 tcp_iss = 1; /* wrong */ 77 tcb.inp_next = tcb.inp_prev = &tcb; 78 if (max_protohdr < sizeof(struct tcpiphdr)) 79 max_protohdr = sizeof(struct tcpiphdr); 80 if (max_linkhdr + sizeof(struct tcpiphdr) > MHLEN) 81 panic("tcp_init"); 82 } 83 84 /* 85 * Create template to be used to send tcp packets on a connection. 86 * Call after host entry created, allocates an mbuf and fills 87 * in a skeletal tcp/ip header, minimizing the amount of work 88 * necessary when the connection is used. 89 */ 90 struct tcpiphdr * 91 tcp_template(tp) 92 struct tcpcb *tp; 93 { 94 register struct inpcb *inp = tp->t_inpcb; 95 register struct mbuf *m; 96 register struct tcpiphdr *n; 97 98 if ((n = tp->t_template) == 0) { 99 m = m_get(M_DONTWAIT, MT_HEADER); 100 if (m == NULL) 101 return (0); 102 m->m_len = sizeof (struct tcpiphdr); 103 n = mtod(m, struct tcpiphdr *); 104 } 105 n->ti_next = n->ti_prev = 0; 106 n->ti_x1 = 0; 107 n->ti_pr = IPPROTO_TCP; 108 n->ti_len = htons(sizeof (struct tcpiphdr) - sizeof (struct ip)); 109 n->ti_src = inp->inp_laddr; 110 n->ti_dst = inp->inp_faddr; 111 n->ti_sport = inp->inp_lport; 112 n->ti_dport = inp->inp_fport; 113 n->ti_seq = 0; 114 n->ti_ack = 0; 115 n->ti_x2 = 0; 116 n->ti_off = 5; 117 n->ti_flags = 0; 118 n->ti_win = 0; 119 n->ti_sum = 0; 120 n->ti_urp = 0; 121 return (n); 122 } 123 124 /* 125 * Send a single message to the TCP at address specified by 126 * the given TCP/IP header. If m == 0, then we make a copy 127 * of the tcpiphdr at ti and send directly to the addressed host. 128 * This is used to force keep alive messages out using the TCP 129 * template for a connection tp->t_template. If flags are given 130 * then we send a message back to the TCP which originated the 131 * segment ti, and discard the mbuf containing it and any other 132 * attached mbufs. 133 * 134 * In any case the ack and sequence number of the transmitted 135 * segment are as specified by the parameters. 136 */ 137 void 138 tcp_respond(tp, ti, m, ack, seq, flags) 139 struct tcpcb *tp; 140 register struct tcpiphdr *ti; 141 register struct mbuf *m; 142 tcp_seq ack, seq; 143 int flags; 144 { 145 register int tlen; 146 int win = 0; 147 struct route *ro = 0; 148 149 if (tp) { 150 win = sbspace(&tp->t_inpcb->inp_socket->so_rcv); 151 ro = &tp->t_inpcb->inp_route; 152 } 153 if (m == 0) { 154 m = m_gethdr(M_DONTWAIT, MT_HEADER); 155 if (m == NULL) 156 return; 157 #ifdef TCP_COMPAT_42 158 tlen = 1; 159 #else 160 tlen = 0; 161 #endif 162 m->m_data += max_linkhdr; 163 *mtod(m, struct tcpiphdr *) = *ti; 164 ti = mtod(m, struct tcpiphdr *); 165 flags = TH_ACK; 166 } else { 167 m_freem(m->m_next); 168 m->m_next = 0; 169 m->m_data = (caddr_t)ti; 170 m->m_len = sizeof (struct tcpiphdr); 171 tlen = 0; 172 #define xchg(a,b,type) { type t; t=a; a=b; b=t; } 173 xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr, u_long); 174 xchg(ti->ti_dport, ti->ti_sport, u_short); 175 #undef xchg 176 } 177 ti->ti_len = htons((u_short)(sizeof (struct tcphdr) + tlen)); 178 tlen += sizeof (struct tcpiphdr); 179 m->m_len = tlen; 180 m->m_pkthdr.len = tlen; 181 m->m_pkthdr.rcvif = (struct ifnet *) 0; 182 ti->ti_next = ti->ti_prev = 0; 183 ti->ti_x1 = 0; 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_short) (win >> tp->rcv_scale)); 191 else 192 ti->ti_win = htons((u_short)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 tp->seg_next = tp->seg_prev = (struct tcpiphdr *)tp; 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 << 2; 228 tp->t_rttmin = TCPTV_MIN; 229 TCPT_RANGESET(tp->t_rxtcur, 230 ((TCPTV_SRTTBASE >> 2) + (TCPTV_SRTTDFLT << 2)) >> 1, 231 TCPTV_MIN, TCPTV_REXMTMAX); 232 tp->snd_cwnd = TCP_MAXWIN << TCP_MAX_WINSHIFT; 233 tp->snd_ssthresh = TCP_MAXWIN << TCP_MAX_WINSHIFT; 234 inp->inp_ip.ip_ttl = ip_defttl; 235 inp->inp_ppcb = (caddr_t)tp; 236 return (tp); 237 } 238 239 /* 240 * Drop a TCP connection, reporting 241 * the specified error. If connection is synchronized, 242 * then send a RST to peer. 243 */ 244 struct tcpcb * 245 tcp_drop(tp, errno) 246 register struct tcpcb *tp; 247 int errno; 248 { 249 struct socket *so = tp->t_inpcb->inp_socket; 250 251 if (TCPS_HAVERCVDSYN(tp->t_state)) { 252 tp->t_state = TCPS_CLOSED; 253 (void) tcp_output(tp); 254 tcpstat.tcps_drops++; 255 } else 256 tcpstat.tcps_conndrops++; 257 if (errno == ETIMEDOUT && tp->t_softerror) 258 errno = tp->t_softerror; 259 so->so_error = errno; 260 return (tcp_close(tp)); 261 } 262 263 /* 264 * Close a TCP control block: 265 * discard all space held by the tcp 266 * discard internet protocol block 267 * wake up any sleepers 268 */ 269 struct tcpcb * 270 tcp_close(tp) 271 register struct tcpcb *tp; 272 { 273 register struct tcpiphdr *t; 274 struct inpcb *inp = tp->t_inpcb; 275 struct socket *so = inp->inp_socket; 276 register struct mbuf *m; 277 #ifdef RTV_RTT 278 register struct rtentry *rt; 279 280 /* 281 * If we sent enough data to get some meaningful characteristics, 282 * save them in the routing entry. 'Enough' is arbitrarily 283 * defined as the sendpipesize (default 4K) * 16. This would 284 * give us 16 rtt samples assuming we only get one sample per 285 * window (the usual case on a long haul net). 16 samples is 286 * enough for the srtt filter to converge to within 5% of the correct 287 * value; fewer samples and we could save a very bogus rtt. 288 * 289 * Don't update the default route's characteristics and don't 290 * update anything that the user "locked". 291 */ 292 if (SEQ_LT(tp->iss + so->so_snd.sb_hiwat * 16, tp->snd_max) && 293 (rt = inp->inp_route.ro_rt) && 294 ((struct sockaddr_in *)rt_key(rt))->sin_addr.s_addr != INADDR_ANY) { 295 register u_long i = 0; 296 297 if ((rt->rt_rmx.rmx_locks & RTV_RTT) == 0) { 298 i = tp->t_srtt * 299 (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTT_SCALE)); 300 if (rt->rt_rmx.rmx_rtt && i) 301 /* 302 * filter this update to half the old & half 303 * the new values, converting scale. 304 * See route.h and tcp_var.h for a 305 * description of the scaling constants. 306 */ 307 rt->rt_rmx.rmx_rtt = 308 (rt->rt_rmx.rmx_rtt + i) / 2; 309 else 310 rt->rt_rmx.rmx_rtt = i; 311 } 312 if ((rt->rt_rmx.rmx_locks & RTV_RTTVAR) == 0) { 313 i = tp->t_rttvar * 314 (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTTVAR_SCALE)); 315 if (rt->rt_rmx.rmx_rttvar && i) 316 rt->rt_rmx.rmx_rttvar = 317 (rt->rt_rmx.rmx_rttvar + i) / 2; 318 else 319 rt->rt_rmx.rmx_rttvar = i; 320 } 321 /* 322 * update the pipelimit (ssthresh) if it has been updated 323 * already or if a pipesize was specified & the threshhold 324 * got below half the pipesize. I.e., wait for bad news 325 * before we start updating, then update on both good 326 * and bad news. 327 */ 328 if ((rt->rt_rmx.rmx_locks & RTV_SSTHRESH) == 0 && 329 (i = tp->snd_ssthresh) && rt->rt_rmx.rmx_ssthresh || 330 i < (rt->rt_rmx.rmx_sendpipe / 2)) { 331 /* 332 * convert the limit from user data bytes to 333 * packets then to packet data bytes. 334 */ 335 i = (i + tp->t_maxseg / 2) / tp->t_maxseg; 336 if (i < 2) 337 i = 2; 338 i *= (u_long)(tp->t_maxseg + sizeof (struct tcpiphdr)); 339 if (rt->rt_rmx.rmx_ssthresh) 340 rt->rt_rmx.rmx_ssthresh = 341 (rt->rt_rmx.rmx_ssthresh + i) / 2; 342 else 343 rt->rt_rmx.rmx_ssthresh = i; 344 } 345 } 346 #endif /* RTV_RTT */ 347 /* free the reassembly queue, if any */ 348 t = tp->seg_next; 349 while (t != (struct tcpiphdr *)tp) { 350 t = (struct tcpiphdr *)t->ti_next; 351 m = REASS_MBUF((struct tcpiphdr *)t->ti_prev); 352 remque(t->ti_prev); 353 m_freem(m); 354 } 355 if (tp->t_template) 356 (void) m_free(dtom(tp->t_template)); 357 free(tp, M_PCB); 358 inp->inp_ppcb = 0; 359 soisdisconnected(so); 360 /* clobber input pcb cache if we're closing the cached connection */ 361 if (inp == tcp_last_inpcb) 362 tcp_last_inpcb = &tcb; 363 in_pcbdetach(inp); 364 tcpstat.tcps_closed++; 365 return ((struct tcpcb *)0); 366 } 367 368 void 369 tcp_drain() 370 { 371 372 } 373 374 /* 375 * Notify a tcp user of an asynchronous error; 376 * store error as soft error, but wake up user 377 * (for now, won't do anything until can select for soft error). 378 */ 379 void 380 tcp_notify(inp, error) 381 struct inpcb *inp; 382 int error; 383 { 384 register struct tcpcb *tp = (struct tcpcb *)inp->inp_ppcb; 385 register struct socket *so = inp->inp_socket; 386 387 /* 388 * Ignore some errors if we are hooked up. 389 * If connection hasn't completed, has retransmitted several times, 390 * and receives a second error, give up now. This is better 391 * than waiting a long time to establish a connection that 392 * can never complete. 393 */ 394 if (tp->t_state == TCPS_ESTABLISHED && 395 (error == EHOSTUNREACH || error == ENETUNREACH || 396 error == EHOSTDOWN)) { 397 return; 398 } else if (tp->t_state < TCPS_ESTABLISHED && tp->t_rxtshift > 3 && 399 tp->t_softerror) 400 so->so_error = error; 401 else 402 tp->t_softerror = error; 403 wakeup((caddr_t) &so->so_timeo); 404 sorwakeup(so); 405 sowwakeup(so); 406 } 407 408 void 409 tcp_ctlinput(cmd, sa, ip) 410 int cmd; 411 struct sockaddr *sa; 412 register struct ip *ip; 413 { 414 register struct tcphdr *th; 415 extern struct in_addr zeroin_addr; 416 extern u_char inetctlerrmap[]; 417 void (*notify) __P((struct inpcb *, int)) = tcp_notify; 418 419 if (cmd == PRC_QUENCH) 420 notify = tcp_quench; 421 else if (!PRC_IS_REDIRECT(cmd) && 422 ((unsigned)cmd > PRC_NCMDS || inetctlerrmap[cmd] == 0)) 423 return; 424 if (ip) { 425 th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2)); 426 in_pcbnotify(&tcb, sa, th->th_dport, ip->ip_src, th->th_sport, 427 cmd, notify); 428 } else 429 in_pcbnotify(&tcb, sa, 0, zeroin_addr, 0, cmd, notify); 430 } 431 432 /* 433 * When a source quench is received, close congestion window 434 * to one segment. We will gradually open it again as we proceed. 435 */ 436 void 437 tcp_quench(inp, errno) 438 struct inpcb *inp; 439 int errno; 440 { 441 struct tcpcb *tp = intotcpcb(inp); 442 443 if (tp) 444 tp->snd_cwnd = tp->t_maxseg; 445 } 446