xref: /csrg-svn/sys/netinet/tcp_subr.c (revision 44378)
1 /*
2  * Copyright (c) 1982, 1986, 1988, 1990 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  *
17  *	@(#)tcp_subr.c	7.17 (Berkeley) 06/28/90
18  */
19 
20 #include "param.h"
21 #include "systm.h"
22 #include "malloc.h"
23 #include "mbuf.h"
24 #include "socket.h"
25 #include "socketvar.h"
26 #include "protosw.h"
27 #include "errno.h"
28 
29 #include "../net/route.h"
30 #include "../net/if.h"
31 
32 #include "in.h"
33 #include "in_systm.h"
34 #include "ip.h"
35 #include "in_pcb.h"
36 #include "ip_var.h"
37 #include "ip_icmp.h"
38 #include "tcp.h"
39 #include "tcp_fsm.h"
40 #include "tcp_seq.h"
41 #include "tcp_timer.h"
42 #include "tcp_var.h"
43 #include "tcpip.h"
44 
45 /* patchable/settable parameters for tcp */
46 int	tcp_ttl = TCP_TTL;
47 int 	tcp_mssdflt = TCP_MSS;
48 int 	tcp_rttdflt = TCPTV_SRTTDFLT / PR_SLOWHZ;
49 
50 extern	struct inpcb *tcp_last_inpcb;
51 
52 /*
53  * Tcp initialization
54  */
55 tcp_init()
56 {
57 
58 	tcp_iss = 1;		/* wrong */
59 	tcb.inp_next = tcb.inp_prev = &tcb;
60 	if (max_protohdr < sizeof(struct tcpiphdr))
61 		max_protohdr = sizeof(struct tcpiphdr);
62 	if (max_linkhdr + sizeof(struct tcpiphdr) > MHLEN)
63 		panic("tcp_init");
64 }
65 
66 /*
67  * Create template to be used to send tcp packets on a connection.
68  * Call after host entry created, allocates an mbuf and fills
69  * in a skeletal tcp/ip header, minimizing the amount of work
70  * necessary when the connection is used.
71  */
72 struct tcpiphdr *
73 tcp_template(tp)
74 	struct tcpcb *tp;
75 {
76 	register struct inpcb *inp = tp->t_inpcb;
77 	register struct mbuf *m;
78 	register struct tcpiphdr *n;
79 
80 	if ((n = tp->t_template) == 0) {
81 		m = m_get(M_DONTWAIT, MT_HEADER);
82 		if (m == NULL)
83 			return (0);
84 		m->m_len = sizeof (struct tcpiphdr);
85 		n = mtod(m, struct tcpiphdr *);
86 	}
87 	n->ti_next = n->ti_prev = 0;
88 	n->ti_x1 = 0;
89 	n->ti_pr = IPPROTO_TCP;
90 	n->ti_len = htons(sizeof (struct tcpiphdr) - sizeof (struct ip));
91 	n->ti_src = inp->inp_laddr;
92 	n->ti_dst = inp->inp_faddr;
93 	n->ti_sport = inp->inp_lport;
94 	n->ti_dport = inp->inp_fport;
95 	n->ti_seq = 0;
96 	n->ti_ack = 0;
97 	n->ti_x2 = 0;
98 	n->ti_off = 5;
99 	n->ti_flags = 0;
100 	n->ti_win = 0;
101 	n->ti_sum = 0;
102 	n->ti_urp = 0;
103 	return (n);
104 }
105 
106 /*
107  * Send a single message to the TCP at address specified by
108  * the given TCP/IP header.  If m == 0, then we make a copy
109  * of the tcpiphdr at ti and send directly to the addressed host.
110  * This is used to force keep alive messages out using the TCP
111  * template for a connection tp->t_template.  If flags are given
112  * then we send a message back to the TCP which originated the
113  * segment ti, and discard the mbuf containing it and any other
114  * attached mbufs.
115  *
116  * In any case the ack and sequence number of the transmitted
117  * segment are as specified by the parameters.
118  */
119 tcp_respond(tp, ti, m, ack, seq, flags)
120 	struct tcpcb *tp;
121 	register struct tcpiphdr *ti;
122 	register struct mbuf *m;
123 	tcp_seq ack, seq;
124 	int flags;
125 {
126 	int win = 0, tlen;
127 	struct route *ro = 0;
128 
129 	if (tp) {
130 		win = sbspace(&tp->t_inpcb->inp_socket->so_rcv);
131 		ro = &tp->t_inpcb->inp_route;
132 	}
133 	if (m == 0) {
134 		m = m_gethdr(M_DONTWAIT, MT_HEADER);
135 		if (m == NULL)
136 			return;
137 #ifdef TCP_COMPAT_42
138 		tlen = 1;
139 #else
140 		tlen = 0;
141 #endif
142 		m->m_data += max_linkhdr;
143 		*mtod(m, struct tcpiphdr *) = *ti;
144 		ti = mtod(m, struct tcpiphdr *);
145 		flags = TH_ACK;
146 	} else {
147 		m_freem(m->m_next);
148 		m->m_next = 0;
149 		m->m_data = (caddr_t)ti;
150 		m->m_len = sizeof (struct tcpiphdr);
151 		tlen = 0;
152 #define xchg(a,b,type) { type t; t=a; a=b; b=t; }
153 		xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr, u_long);
154 		xchg(ti->ti_dport, ti->ti_sport, u_short);
155 #undef xchg
156 	}
157 	m->m_len = sizeof (struct tcpiphdr) + tlen;
158 	ti->ti_next = ti->ti_prev = 0;
159 	ti->ti_x1 = 0;
160 	ti->ti_len = htons((u_short)(sizeof (struct tcphdr) + tlen));
161 	ti->ti_seq = htonl(seq);
162 	ti->ti_ack = htonl(ack);
163 	ti->ti_x2 = 0;
164 	ti->ti_off = sizeof (struct tcphdr) >> 2;
165 	ti->ti_flags = flags;
166 	ti->ti_win = htons((u_short)win);
167 	ti->ti_urp = 0;
168 	ti->ti_sum = in_cksum(m, sizeof (struct tcpiphdr) + tlen);
169 	((struct ip *)ti)->ip_len = sizeof (struct tcpiphdr) + tlen;
170 	((struct ip *)ti)->ip_ttl = tcp_ttl;
171 	(void) ip_output(m, (struct mbuf *)0, ro, 0);
172 }
173 
174 /*
175  * Create a new TCP control block, making an
176  * empty reassembly queue and hooking it to the argument
177  * protocol control block.
178  */
179 struct tcpcb *
180 tcp_newtcpcb(inp)
181 	struct inpcb *inp;
182 {
183 	struct mbuf *m = m_getclr(M_DONTWAIT, MT_PCB);
184 	register struct tcpcb *tp;
185 
186 	if (m == NULL)
187 		return ((struct tcpcb *)0);
188 	tp = mtod(m, struct tcpcb *);
189 	tp->seg_next = tp->seg_prev = (struct tcpiphdr *)tp;
190 	tp->t_maxseg = tcp_mssdflt;
191 
192 	tp->t_flags = 0;		/* sends options! */
193 	tp->t_inpcb = inp;
194 	/*
195 	 * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no
196 	 * rtt estimate.  Set rttvar so that srtt + 2 * rttvar gives
197 	 * reasonable initial retransmit time.
198 	 */
199 	tp->t_srtt = TCPTV_SRTTBASE;
200 	tp->t_rttvar = tcp_rttdflt * PR_SLOWHZ << 2;
201 	tp->t_rttmin = TCPTV_MIN;
202 	TCPT_RANGESET(tp->t_rxtcur,
203 	    ((TCPTV_SRTTBASE >> 2) + (TCPTV_SRTTDFLT << 2)) >> 1,
204 	    TCPTV_MIN, TCPTV_REXMTMAX);
205 	tp->snd_cwnd = TCP_MAXWIN;
206 	tp->snd_ssthresh = TCP_MAXWIN;
207 	inp->inp_ip.ip_ttl = tcp_ttl;
208 	inp->inp_ppcb = (caddr_t)tp;
209 	return (tp);
210 }
211 
212 /*
213  * Drop a TCP connection, reporting
214  * the specified error.  If connection is synchronized,
215  * then send a RST to peer.
216  */
217 struct tcpcb *
218 tcp_drop(tp, errno)
219 	register struct tcpcb *tp;
220 	int errno;
221 {
222 	struct socket *so = tp->t_inpcb->inp_socket;
223 
224 	if (TCPS_HAVERCVDSYN(tp->t_state)) {
225 		tp->t_state = TCPS_CLOSED;
226 		(void) tcp_output(tp);
227 		tcpstat.tcps_drops++;
228 	} else
229 		tcpstat.tcps_conndrops++;
230 	if (errno == ETIMEDOUT && tp->t_softerror)
231 		errno = tp->t_softerror;
232 	so->so_error = errno;
233 	return (tcp_close(tp));
234 }
235 
236 /*
237  * Close a TCP control block:
238  *	discard all space held by the tcp
239  *	discard internet protocol block
240  *	wake up any sleepers
241  */
242 struct tcpcb *
243 tcp_close(tp)
244 	register struct tcpcb *tp;
245 {
246 	register struct tcpiphdr *t;
247 	struct inpcb *inp = tp->t_inpcb;
248 	struct socket *so = inp->inp_socket;
249 	register struct mbuf *m;
250 #ifdef RTV_RTT
251 	register struct rtentry *rt;
252 
253 	/*
254 	 * If we sent enough data to get some meaningful characteristics,
255 	 * save them in the routing entry.  'Enough' is arbitrarily
256 	 * defined as 4K (default tcp_sendspace) * 16.  This would
257 	 * give us 16 rtt samples assuming we only get one sample per
258 	 * window (the usual case on a long haul net).  16 samples is
259 	 * enough for the srtt filter to converge to within 5% of the correct
260 	 * value; fewer samples and we could save a very bogus rtt.
261 	 *
262 	 * Don't update the default route's characteristics and don't
263 	 * update anything that the user "locked".
264 	 */
265 	if (SEQ_LT(tp->iss+(4096*16), tp->snd_max) &&
266 	    (rt = inp->inp_route.ro_rt) &&
267 	    ((struct sockaddr_in *) rt_key(rt))->sin_addr.s_addr !=
268 	    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 	(void) m_free(dtom(tp));
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 tcp_drain()
343 {
344 
345 }
346 
347 /*
348  * Notify a tcp user of an asynchronous error;
349  * store error as soft error, but wake up user
350  * (for now, won't do anything until can select for soft error).
351  */
352 tcp_notify(inp, error)
353 	register struct inpcb *inp;
354 	int error;
355 {
356 
357 	((struct tcpcb *)inp->inp_ppcb)->t_softerror = error;
358 	wakeup((caddr_t) &inp->inp_socket->so_timeo);
359 	sorwakeup(inp->inp_socket);
360 	sowwakeup(inp->inp_socket);
361 }
362 
363 tcp_ctlinput(cmd, sa, ip)
364 	int cmd;
365 	struct sockaddr *sa;
366 	register struct ip *ip;
367 {
368 	register struct tcphdr *th;
369 	extern struct in_addr zeroin_addr;
370 	extern u_char inetctlerrmap[];
371 	int (*notify)() = tcp_notify, tcp_quench();
372 
373 	if (cmd == PRC_QUENCH)
374 		notify = tcp_quench;
375 	else if ((unsigned)cmd > PRC_NCMDS || inetctlerrmap[cmd] == 0)
376 		return;
377 	if (ip) {
378 		th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
379 		in_pcbnotify(&tcb, sa, th->th_dport, ip->ip_src, th->th_sport,
380 			cmd, notify);
381 	} else
382 		in_pcbnotify(&tcb, sa, 0, zeroin_addr, 0, cmd, notify);
383 }
384 
385 /*
386  * When a source quench is received, close congestion window
387  * to one segment.  We will gradually open it again as we proceed.
388  */
389 tcp_quench(inp)
390 	struct inpcb *inp;
391 {
392 	struct tcpcb *tp = intotcpcb(inp);
393 
394 	if (tp)
395 		tp->snd_cwnd = tp->t_maxseg;
396 }
397