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