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