xref: /csrg-svn/sys/netinet/tcp_subr.c (revision 31442)
1 /*
2  * Copyright (c) 1982, 1986 Regents of the University of California.
3  * All rights reserved.  The Berkeley software License Agreement
4  * specifies the terms and conditions for redistribution.
5  *
6  *	@(#)tcp_subr.c	7.6 (Berkeley) 06/06/87
7  */
8 
9 #include "param.h"
10 #include "systm.h"
11 #include "mbuf.h"
12 #include "socket.h"
13 #include "socketvar.h"
14 #include "protosw.h"
15 #include "errno.h"
16 
17 #include "../net/route.h"
18 #include "../net/if.h"
19 
20 #include "in.h"
21 #include "in_pcb.h"
22 #include "in_systm.h"
23 #include "ip.h"
24 #include "ip_var.h"
25 #include "ip_icmp.h"
26 #include "tcp.h"
27 #include "tcp_fsm.h"
28 #include "tcp_seq.h"
29 #include "tcp_timer.h"
30 #include "tcp_var.h"
31 #include "tcpip.h"
32 
33 int	tcp_ttl = TCP_TTL;
34 float	tcp_alpha = TCP_ALPHA;
35 float	tcp_beta = TCP_BETA;
36 
37 /*
38  * Tcp initialization
39  */
40 tcp_init()
41 {
42 
43 	tcp_iss = 1;		/* wrong */
44 	tcb.inp_next = tcb.inp_prev = &tcb;
45 }
46 
47 /*
48  * Create template to be used to send tcp packets on a connection.
49  * Call after host entry created, allocates an mbuf and fills
50  * in a skeletal tcp/ip header, minimizing the amount of work
51  * necessary when the connection is used.
52  */
53 struct tcpiphdr *
54 tcp_template(tp)
55 	struct tcpcb *tp;
56 {
57 	register struct inpcb *inp = tp->t_inpcb;
58 	register struct mbuf *m;
59 	register struct tcpiphdr *n;
60 
61 	if ((n = tp->t_template) == 0) {
62 		m = m_get(M_WAIT, MT_HEADER);
63 		if (m == NULL)
64 			return (0);
65 		m->m_off = MMAXOFF - sizeof (struct tcpiphdr);
66 		m->m_len = sizeof (struct tcpiphdr);
67 		n = mtod(m, struct tcpiphdr *);
68 	}
69 	n->ti_next = n->ti_prev = 0;
70 	n->ti_x1 = 0;
71 	n->ti_pr = IPPROTO_TCP;
72 	n->ti_len = htons(sizeof (struct tcpiphdr) - sizeof (struct ip));
73 	n->ti_src = inp->inp_laddr;
74 	n->ti_dst = inp->inp_faddr;
75 	n->ti_sport = inp->inp_lport;
76 	n->ti_dport = inp->inp_fport;
77 	n->ti_seq = 0;
78 	n->ti_ack = 0;
79 	n->ti_x2 = 0;
80 	n->ti_off = 5;
81 	n->ti_flags = 0;
82 	n->ti_win = 0;
83 	n->ti_sum = 0;
84 	n->ti_urp = 0;
85 	return (n);
86 }
87 
88 /*
89  * Send a single message to the TCP at address specified by
90  * the given TCP/IP header.  If flags==0, then we make a copy
91  * of the tcpiphdr at ti and send directly to the addressed host.
92  * This is used to force keep alive messages out using the TCP
93  * template for a connection tp->t_template.  If flags are given
94  * then we send a message back to the TCP which originated the
95  * segment ti, and discard the mbuf containing it and any other
96  * attached mbufs.
97  *
98  * In any case the ack and sequence number of the transmitted
99  * segment are as specified by the parameters.
100  */
101 tcp_respond(tp, ti, ack, seq, flags)
102 	struct tcpcb *tp;
103 	register struct tcpiphdr *ti;
104 	tcp_seq ack, seq;
105 	int flags;
106 {
107 	register struct mbuf *m;
108 	int win = 0, tlen;
109 	struct route *ro = 0;
110 	extern int tcp_keeplen;
111 
112 	if (tp) {
113 		win = sbspace(&tp->t_inpcb->inp_socket->so_rcv);
114 		ro = &tp->t_inpcb->inp_route;
115 	}
116 	if (flags == 0) {
117 		m = m_get(M_DONTWAIT, MT_HEADER);
118 		if (m == NULL)
119 			return;
120 		tlen = tcp_keeplen;
121 		m->m_len = sizeof (struct tcpiphdr) + tlen;
122 		*mtod(m, struct tcpiphdr *) = *ti;
123 		ti = mtod(m, struct tcpiphdr *);
124 		flags = TH_ACK;
125 	} else {
126 		m = dtom(ti);
127 		m_freem(m->m_next);
128 		m->m_next = 0;
129 		m->m_off = (int)ti - (int)m;
130 		tlen = 0;
131 		m->m_len = sizeof (struct tcpiphdr);
132 #define xchg(a,b,type) { type t; t=a; a=b; b=t; }
133 		xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr, u_long);
134 		xchg(ti->ti_dport, ti->ti_sport, u_short);
135 #undef xchg
136 	}
137 	ti->ti_next = ti->ti_prev = 0;
138 	ti->ti_x1 = 0;
139 	ti->ti_len = htons((u_short)(sizeof (struct tcphdr) + tlen));
140 	ti->ti_seq = htonl(seq);
141 	ti->ti_ack = htonl(ack);
142 	ti->ti_x2 = 0;
143 	ti->ti_off = sizeof (struct tcphdr) >> 2;
144 	ti->ti_flags = flags;
145 	ti->ti_win = htons((u_short)win);
146 	ti->ti_urp = 0;
147 	ti->ti_sum = in_cksum(m, sizeof (struct tcpiphdr) + tlen);
148 	((struct ip *)ti)->ip_len = sizeof (struct tcpiphdr) + tlen;
149 	((struct ip *)ti)->ip_ttl = tcp_ttl;
150 	(void) ip_output(m, (struct mbuf *)0, ro, 0);
151 }
152 
153 /*
154  * Create a new TCP control block, making an
155  * empty reassembly queue and hooking it to the argument
156  * protocol control block.
157  */
158 struct tcpcb *
159 tcp_newtcpcb(inp)
160 	struct inpcb *inp;
161 {
162 	struct mbuf *m = m_getclr(M_DONTWAIT, MT_PCB);
163 	register struct tcpcb *tp;
164 
165 	if (m == NULL)
166 		return ((struct tcpcb *)0);
167 	tp = mtod(m, struct tcpcb *);
168 	tp->seg_next = tp->seg_prev = (struct tcpiphdr *)tp;
169 	tp->t_maxseg = TCP_MSS;
170 	tp->t_flags = 0;		/* sends options! */
171 	tp->t_inpcb = inp;
172 	tp->t_srtt = TCPTV_SRTTBASE;
173 	tp->snd_cwnd = sbspace(&inp->inp_socket->so_snd);
174 	inp->inp_ppcb = (caddr_t)tp;
175 	return (tp);
176 }
177 
178 /*
179  * Drop a TCP connection, reporting
180  * the specified error.  If connection is synchronized,
181  * then send a RST to peer.
182  */
183 struct tcpcb *
184 tcp_drop(tp, errno)
185 	register struct tcpcb *tp;
186 	int errno;
187 {
188 	struct socket *so = tp->t_inpcb->inp_socket;
189 
190 	if (TCPS_HAVERCVDSYN(tp->t_state)) {
191 		tp->t_state = TCPS_CLOSED;
192 		(void) tcp_output(tp);
193 		tcpstat.tcps_drops++;
194 	} else
195 		tcpstat.tcps_conndrops++;
196 	so->so_error = errno;
197 	return (tcp_close(tp));
198 }
199 
200 /*
201  * Close a TCP control block:
202  *	discard all space held by the tcp
203  *	discard internet protocol block
204  *	wake up any sleepers
205  */
206 struct tcpcb *
207 tcp_close(tp)
208 	register struct tcpcb *tp;
209 {
210 	register struct tcpiphdr *t;
211 	struct inpcb *inp = tp->t_inpcb;
212 	struct socket *so = inp->inp_socket;
213 	register struct mbuf *m;
214 
215 	t = tp->seg_next;
216 	while (t != (struct tcpiphdr *)tp) {
217 		t = (struct tcpiphdr *)t->ti_next;
218 		m = dtom(t->ti_prev);
219 		remque(t->ti_prev);
220 		m_freem(m);
221 	}
222 	if (tp->t_template)
223 		(void) m_free(dtom(tp->t_template));
224 	if (tp->t_tcpopt)
225 		(void) m_free(dtom(tp->t_tcpopt));
226 	(void) m_free(dtom(tp));
227 	inp->inp_ppcb = 0;
228 	soisdisconnected(so);
229 	in_pcbdetach(inp);
230 	tcpstat.tcps_closed++;
231 	return ((struct tcpcb *)0);
232 }
233 
234 tcp_drain()
235 {
236 
237 }
238 
239 /*
240  * Notify a tcp user of an asynchronous error;
241  * just wake up so that he can collect error status.
242  */
243 tcp_notify(inp)
244 	register struct inpcb *inp;
245 {
246 
247 	wakeup((caddr_t) &inp->inp_socket->so_timeo);
248 	sorwakeup(inp->inp_socket);
249 	sowwakeup(inp->inp_socket);
250 }
251 tcp_ctlinput(cmd, sa)
252 	int cmd;
253 	struct sockaddr *sa;
254 {
255 	extern u_char inetctlerrmap[];
256 	struct sockaddr_in *sin;
257 	int tcp_quench(), in_rtchange();
258 
259 	if ((unsigned)cmd > PRC_NCMDS)
260 		return;
261 	if (sa->sa_family != AF_INET && sa->sa_family != AF_IMPLINK)
262 		return;
263 	sin = (struct sockaddr_in *)sa;
264 	if (sin->sin_addr.s_addr == INADDR_ANY)
265 		return;
266 
267 	switch (cmd) {
268 
269 	case PRC_QUENCH:
270 		in_pcbnotify(&tcb, &sin->sin_addr, 0, tcp_quench);
271 		break;
272 
273 	case PRC_ROUTEDEAD:
274 	case PRC_REDIRECT_NET:
275 	case PRC_REDIRECT_HOST:
276 	case PRC_REDIRECT_TOSNET:
277 	case PRC_REDIRECT_TOSHOST:
278 		in_pcbnotify(&tcb, &sin->sin_addr, 0, in_rtchange);
279 		break;
280 
281 	default:
282 		if (inetctlerrmap[cmd] == 0)
283 			return;		/* XXX */
284 		in_pcbnotify(&tcb, &sin->sin_addr, (int)inetctlerrmap[cmd],
285 			tcp_notify);
286 	}
287 }
288 
289 /*
290  * When a source quench is received, close congestion window
291  * to one segment.  We will gradually open it again as we proceed.
292  */
293 tcp_quench(inp)
294 	struct inpcb *inp;
295 {
296 	struct tcpcb *tp = intotcpcb(inp);
297 
298 	if (tp)
299 		tp->snd_cwnd = tp->t_maxseg;
300 }
301