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