xref: /csrg-svn/sys/netinet/tcp_subr.c (revision 23193)
1 /*
2  * Copyright (c) 1982 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	6.6 (Berkeley) 06/08/85
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 /*
34  * Tcp initialization
35  */
36 tcp_init()
37 {
38 
39 	tcp_iss = 1;		/* wrong */
40 	tcb.inp_next = tcb.inp_prev = &tcb;
41 	tcp_alpha = TCP_ALPHA;
42 	tcp_beta = TCP_BETA;
43 }
44 
45 /*
46  * Create template to be used to send tcp packets on a connection.
47  * Call after host entry created, allocates an mbuf and fills
48  * in a skeletal tcp/ip header, minimizing the amount of work
49  * necessary when the connection is used.
50  */
51 struct tcpiphdr *
52 tcp_template(tp)
53 	struct tcpcb *tp;
54 {
55 	register struct inpcb *inp = tp->t_inpcb;
56 	register struct mbuf *m;
57 	register struct tcpiphdr *n;
58 
59 	m = m_get(M_WAIT, MT_HEADER);
60 	if (m == NULL)
61 		return (0);
62 	m->m_off = MMAXOFF - sizeof (struct tcpiphdr);
63 	m->m_len = sizeof (struct tcpiphdr);
64 	n = mtod(m, struct tcpiphdr *);
65 	n->ti_next = n->ti_prev = 0;
66 	n->ti_x1 = 0;
67 	n->ti_pr = IPPROTO_TCP;
68 	n->ti_len = htons(sizeof (struct tcpiphdr) - sizeof (struct ip));
69 	n->ti_src = inp->inp_laddr;
70 	n->ti_dst = inp->inp_faddr;
71 	n->ti_sport = inp->inp_lport;
72 	n->ti_dport = inp->inp_fport;
73 	n->ti_seq = 0;
74 	n->ti_ack = 0;
75 	n->ti_x2 = 0;
76 	n->ti_off = 5;
77 	n->ti_flags = 0;
78 	n->ti_win = 0;
79 	n->ti_sum = 0;
80 	n->ti_urp = 0;
81 	return (n);
82 }
83 
84 /*
85  * Send a single message to the TCP at address specified by
86  * the given TCP/IP header.  If flags==0, then we make a copy
87  * of the tcpiphdr at ti and send directly to the addressed host.
88  * This is used to force keep alive messages out using the TCP
89  * template for a connection tp->t_template.  If flags are given
90  * then we send a message back to the TCP which originated the
91  * segment ti, and discard the mbuf containing it and any other
92  * attached mbufs.
93  *
94  * In any case the ack and sequence number of the transmitted
95  * segment are as specified by the parameters.
96  */
97 tcp_respond(tp, ti, ack, seq, flags)
98 	struct tcpcb *tp;
99 	register struct tcpiphdr *ti;
100 	tcp_seq ack, seq;
101 	int flags;
102 {
103 	struct mbuf *m;
104 	int win = 0, tlen;
105 	struct route *ro = 0;
106 
107 	if (tp) {
108 		win = sbspace(&tp->t_inpcb->inp_socket->so_rcv);
109 		ro = &tp->t_inpcb->inp_route;
110 	}
111 	if (flags == 0) {
112 		m = m_get(M_DONTWAIT, MT_HEADER);
113 		if (m == NULL)
114 			return;
115 		m->m_len = sizeof (struct tcpiphdr) + 1;
116 		*mtod(m, struct tcpiphdr *) = *ti;
117 		ti = mtod(m, struct tcpiphdr *);
118 		flags = TH_ACK;
119 		tlen = 1;
120 	} else {
121 		m = dtom(ti);
122 		m_freem(m->m_next);
123 		m->m_next = 0;
124 		m->m_off = (int)ti - (int)m;
125 		m->m_len = sizeof (struct tcpiphdr);
126 #define xchg(a,b,type) { type t; t=a; a=b; b=t; }
127 		xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr, u_long);
128 		xchg(ti->ti_dport, ti->ti_sport, u_short);
129 #undef xchg
130 		tlen = 0;
131 	}
132 	ti->ti_next = ti->ti_prev = 0;
133 	ti->ti_x1 = 0;
134 	ti->ti_len = htons((u_short)(sizeof (struct tcphdr) + tlen));
135 	ti->ti_seq = htonl(seq);
136 	ti->ti_ack = htonl(ack);
137 	ti->ti_x2 = 0;
138 	ti->ti_off = sizeof (struct tcphdr) >> 2;
139 	ti->ti_flags = flags;
140 	ti->ti_win = htons((u_short)win);
141 	ti->ti_urp = 0;
142 	ti->ti_sum = in_cksum(m, sizeof (struct tcpiphdr) + tlen);
143 	((struct ip *)ti)->ip_len = sizeof (struct tcpiphdr) + tlen;
144 	((struct ip *)ti)->ip_ttl = TCP_TTL;
145 	(void) ip_output(m, (struct mbuf *)0, ro, 0);
146 }
147 
148 /*
149  * Create a new TCP control block, making an
150  * empty reassembly queue and hooking it to the argument
151  * protocol control block.
152  */
153 struct tcpcb *
154 tcp_newtcpcb(inp)
155 	struct inpcb *inp;
156 {
157 	struct mbuf *m = m_getclr(M_DONTWAIT, MT_PCB);
158 	register struct tcpcb *tp;
159 
160 	if (m == NULL)
161 		return ((struct tcpcb *)0);
162 	tp = mtod(m, struct tcpcb *);
163 	tp->seg_next = tp->seg_prev = (struct tcpiphdr *)tp;
164 	tp->t_maxseg = TCP_MSS;
165 	tp->t_flags = 0;		/* sends options! */
166 	tp->t_inpcb = inp;
167 	tp->t_srtt = TCPTV_SRTTBASE;
168 	tp->snd_cwnd = sbspace(&inp->inp_socket->so_snd);
169 	inp->inp_ppcb = (caddr_t)tp;
170 	return (tp);
171 }
172 
173 /*
174  * Drop a TCP connection, reporting
175  * the specified error.  If connection is synchronized,
176  * then send a RST to peer.
177  */
178 struct tcpcb *
179 tcp_drop(tp, errno)
180 	register struct tcpcb *tp;
181 	int errno;
182 {
183 	struct socket *so = tp->t_inpcb->inp_socket;
184 
185 	if (TCPS_HAVERCVDSYN(tp->t_state)) {
186 		tp->t_state = TCPS_CLOSED;
187 		(void) tcp_output(tp);
188 	}
189 	so->so_error = errno;
190 	return (tcp_close(tp));
191 }
192 
193 tcp_abort(inp)
194 	struct inpcb *inp;
195 {
196 
197 	(void) tcp_close((struct tcpcb *)inp->inp_ppcb);
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 	if (tp->t_ipopt)
227 		(void) m_free(dtom(tp->t_ipopt));
228 	(void) m_free(dtom(tp));
229 	inp->inp_ppcb = 0;
230 	soisdisconnected(so);
231 	in_pcbdetach(inp);
232 	return ((struct tcpcb *)0);
233 }
234 
235 tcp_drain()
236 {
237 
238 }
239 
240 tcp_ctlinput(cmd, arg)
241 	int cmd;
242 	caddr_t arg;
243 {
244 	struct in_addr *in;
245 	extern u_char inetctlerrmap[];
246 	int tcp_quench(), in_rtchange();
247 
248 	if (cmd < 0 || cmd > PRC_NCMDS)
249 		return;
250 	switch (cmd) {
251 
252 	case PRC_ROUTEDEAD:
253 		break;
254 
255 	case PRC_QUENCH:
256 		in = &((struct icmp *)arg)->icmp_ip.ip_dst;
257 		in_pcbnotify(&tcb, in, 0, tcp_quench);
258 		break;
259 
260 	case PRC_REDIRECT_NET:
261 	case PRC_REDIRECT_HOST:
262 		in = &((struct icmp *)arg)->icmp_ip.ip_dst;
263 		in_pcbnotify(&tcb, in, 0, in_rtchange);
264 		break;
265 
266 	case PRC_IFDOWN:
267 		in = &((struct sockaddr_in *)arg)->sin_addr;
268 		goto notify;
269 
270 	case PRC_HOSTDEAD:
271 	case PRC_HOSTUNREACH:
272 		in = (struct in_addr *)arg;
273 		goto notify;
274 
275 	default:
276 		if (inetctlerrmap[cmd] == 0)
277 			return;		/* XXX */
278 		in = &((struct icmp *)arg)->icmp_ip.ip_dst;
279 notify:
280 		in_pcbnotify(&tcb, in, (int)inetctlerrmap[cmd], tcp_abort);
281 	}
282 }
283 
284 /*
285  * When a source quench is received, close congestion window
286  * to 80% of the outstanding data (but not less than one segment).
287  */
288 tcp_quench(inp)
289 	struct inpcb *inp;
290 {
291 	struct tcpcb *tp = intotcpcb(inp);
292 
293 	tp->snd_cwnd = MAX(8 * (tp->snd_nxt - tp->snd_una) / 10, tp->t_maxseg);
294 }
295