tcp_subr.c revision 1.24 1 /* $NetBSD: tcp_subr.c,v 1.24 1996/09/15 18:11:10 mycroft Exp $ */
2
3 /*
4 * Copyright (c) 1982, 1986, 1988, 1990, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * @(#)tcp_subr.c 8.1 (Berkeley) 6/10/93
36 */
37
38 #include <sys/param.h>
39 #include <sys/proc.h>
40 #include <sys/systm.h>
41 #include <sys/malloc.h>
42 #include <sys/mbuf.h>
43 #include <sys/socket.h>
44 #include <sys/socketvar.h>
45 #include <sys/protosw.h>
46 #include <sys/errno.h>
47
48 #include <net/route.h>
49 #include <net/if.h>
50
51 #include <netinet/in.h>
52 #include <netinet/in_systm.h>
53 #include <netinet/ip.h>
54 #include <netinet/in_pcb.h>
55 #include <netinet/ip_var.h>
56 #include <netinet/ip_icmp.h>
57 #include <netinet/tcp.h>
58 #include <netinet/tcp_fsm.h>
59 #include <netinet/tcp_seq.h>
60 #include <netinet/tcp_timer.h>
61 #include <netinet/tcp_var.h>
62 #include <netinet/tcpip.h>
63
64 /* patchable/settable parameters for tcp */
65 int tcp_mssdflt = TCP_MSS;
66 int tcp_rttdflt = TCPTV_SRTTDFLT / PR_SLOWHZ;
67 int tcp_do_rfc1323 = 1;
68
69 #ifndef TCBHASHSIZE
70 #define TCBHASHSIZE 128
71 #endif
72 int tcbhashsize = TCBHASHSIZE;
73
74 /*
75 * Tcp initialization
76 */
77 void
78 tcp_init()
79 {
80
81 tcp_iss = 1; /* XXX wrong */
82 in_pcbinit(&tcbtable, tcbhashsize, tcbhashsize);
83 if (max_protohdr < sizeof(struct tcpiphdr))
84 max_protohdr = sizeof(struct tcpiphdr);
85 if (max_linkhdr + sizeof(struct tcpiphdr) > MHLEN)
86 panic("tcp_init");
87 }
88
89 /*
90 * Create template to be used to send tcp packets on a connection.
91 * Call after host entry created, allocates an mbuf and fills
92 * in a skeletal tcp/ip header, minimizing the amount of work
93 * necessary when the connection is used.
94 */
95 struct tcpiphdr *
96 tcp_template(tp)
97 struct tcpcb *tp;
98 {
99 register struct inpcb *inp = tp->t_inpcb;
100 register struct mbuf *m;
101 register struct tcpiphdr *n;
102
103 if ((n = tp->t_template) == 0) {
104 m = m_get(M_DONTWAIT, MT_HEADER);
105 if (m == NULL)
106 return (0);
107 m->m_len = sizeof (struct tcpiphdr);
108 n = mtod(m, struct tcpiphdr *);
109 }
110 bzero(n->ti_x1, sizeof n->ti_x1);
111 n->ti_pr = IPPROTO_TCP;
112 n->ti_len = htons(sizeof (struct tcpiphdr) - sizeof (struct ip));
113 n->ti_src = inp->inp_laddr;
114 n->ti_dst = inp->inp_faddr;
115 n->ti_sport = inp->inp_lport;
116 n->ti_dport = inp->inp_fport;
117 n->ti_seq = 0;
118 n->ti_ack = 0;
119 n->ti_x2 = 0;
120 n->ti_off = 5;
121 n->ti_flags = 0;
122 n->ti_win = 0;
123 n->ti_sum = 0;
124 n->ti_urp = 0;
125 return (n);
126 }
127
128 /*
129 * Send a single message to the TCP at address specified by
130 * the given TCP/IP header. If m == 0, then we make a copy
131 * of the tcpiphdr at ti and send directly to the addressed host.
132 * This is used to force keep alive messages out using the TCP
133 * template for a connection tp->t_template. If flags are given
134 * then we send a message back to the TCP which originated the
135 * segment ti, and discard the mbuf containing it and any other
136 * attached mbufs.
137 *
138 * In any case the ack and sequence number of the transmitted
139 * segment are as specified by the parameters.
140 */
141 void
142 tcp_respond(tp, ti, m, ack, seq, flags)
143 struct tcpcb *tp;
144 register struct tcpiphdr *ti;
145 register struct mbuf *m;
146 tcp_seq ack, seq;
147 int flags;
148 {
149 register int tlen;
150 int win = 0;
151 struct route *ro = 0;
152
153 if (tp) {
154 win = sbspace(&tp->t_inpcb->inp_socket->so_rcv);
155 ro = &tp->t_inpcb->inp_route;
156 }
157 if (m == 0) {
158 m = m_gethdr(M_DONTWAIT, MT_HEADER);
159 if (m == NULL)
160 return;
161 #ifdef TCP_COMPAT_42
162 tlen = 1;
163 #else
164 tlen = 0;
165 #endif
166 m->m_data += max_linkhdr;
167 *mtod(m, struct tcpiphdr *) = *ti;
168 ti = mtod(m, struct tcpiphdr *);
169 flags = TH_ACK;
170 } else {
171 m_freem(m->m_next);
172 m->m_next = 0;
173 m->m_data = (caddr_t)ti;
174 m->m_len = sizeof (struct tcpiphdr);
175 tlen = 0;
176 #define xchg(a,b,type) { type t; t=a; a=b; b=t; }
177 xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr, u_int32_t);
178 xchg(ti->ti_dport, ti->ti_sport, u_int16_t);
179 #undef xchg
180 }
181 ti->ti_len = htons((u_int16_t)(sizeof (struct tcphdr) + tlen));
182 tlen += sizeof (struct tcpiphdr);
183 m->m_len = tlen;
184 m->m_pkthdr.len = tlen;
185 m->m_pkthdr.rcvif = (struct ifnet *) 0;
186 bzero(ti->ti_x1, sizeof ti->ti_x1);
187 ti->ti_seq = htonl(seq);
188 ti->ti_ack = htonl(ack);
189 ti->ti_x2 = 0;
190 ti->ti_off = sizeof (struct tcphdr) >> 2;
191 ti->ti_flags = flags;
192 if (tp)
193 ti->ti_win = htons((u_int16_t) (win >> tp->rcv_scale));
194 else
195 ti->ti_win = htons((u_int16_t)win);
196 ti->ti_urp = 0;
197 ti->ti_sum = 0;
198 ti->ti_sum = in_cksum(m, tlen);
199 ((struct ip *)ti)->ip_len = tlen;
200 ((struct ip *)ti)->ip_ttl = ip_defttl;
201 (void) ip_output(m, NULL, ro, 0, NULL);
202 }
203
204 /*
205 * Create a new TCP control block, making an
206 * empty reassembly queue and hooking it to the argument
207 * protocol control block.
208 */
209 struct tcpcb *
210 tcp_newtcpcb(inp)
211 struct inpcb *inp;
212 {
213 register struct tcpcb *tp;
214
215 tp = malloc(sizeof(*tp), M_PCB, M_NOWAIT);
216 if (tp == NULL)
217 return ((struct tcpcb *)0);
218 bzero((caddr_t)tp, sizeof(struct tcpcb));
219 LIST_INIT(&tp->segq);
220 tp->t_maxseg = tcp_mssdflt;
221
222 tp->t_flags = tcp_do_rfc1323 ? (TF_REQ_SCALE|TF_REQ_TSTMP) : 0;
223 tp->t_inpcb = inp;
224 /*
225 * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no
226 * rtt estimate. Set rttvar so that srtt + 2 * rttvar gives
227 * reasonable initial retransmit time.
228 */
229 tp->t_srtt = TCPTV_SRTTBASE;
230 tp->t_rttvar = tcp_rttdflt * PR_SLOWHZ << (TCP_RTTVAR_SHIFT + 2 - 1);
231 tp->t_rttmin = TCPTV_MIN;
232 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
233 TCPTV_MIN, TCPTV_REXMTMAX);
234 tp->snd_cwnd = TCP_MAXWIN << TCP_MAX_WINSHIFT;
235 tp->snd_ssthresh = TCP_MAXWIN << TCP_MAX_WINSHIFT;
236 inp->inp_ip.ip_ttl = ip_defttl;
237 inp->inp_ppcb = (caddr_t)tp;
238 return (tp);
239 }
240
241 /*
242 * Drop a TCP connection, reporting
243 * the specified error. If connection is synchronized,
244 * then send a RST to peer.
245 */
246 struct tcpcb *
247 tcp_drop(tp, errno)
248 register struct tcpcb *tp;
249 int errno;
250 {
251 struct socket *so = tp->t_inpcb->inp_socket;
252
253 if (TCPS_HAVERCVDSYN(tp->t_state)) {
254 tp->t_state = TCPS_CLOSED;
255 (void) tcp_output(tp);
256 tcpstat.tcps_drops++;
257 } else
258 tcpstat.tcps_conndrops++;
259 if (errno == ETIMEDOUT && tp->t_softerror)
260 errno = tp->t_softerror;
261 so->so_error = errno;
262 return (tcp_close(tp));
263 }
264
265 /*
266 * Close a TCP control block:
267 * discard all space held by the tcp
268 * discard internet protocol block
269 * wake up any sleepers
270 */
271 struct tcpcb *
272 tcp_close(tp)
273 register struct tcpcb *tp;
274 {
275 register struct ipqent *qe;
276 struct inpcb *inp = tp->t_inpcb;
277 struct socket *so = inp->inp_socket;
278 #ifdef RTV_RTT
279 register struct rtentry *rt;
280
281 /*
282 * If we sent enough data to get some meaningful characteristics,
283 * save them in the routing entry. 'Enough' is arbitrarily
284 * defined as the sendpipesize (default 4K) * 16. This would
285 * give us 16 rtt samples assuming we only get one sample per
286 * window (the usual case on a long haul net). 16 samples is
287 * enough for the srtt filter to converge to within 5% of the correct
288 * value; fewer samples and we could save a very bogus rtt.
289 *
290 * Don't update the default route's characteristics and don't
291 * update anything that the user "locked".
292 */
293 if (SEQ_LT(tp->iss + so->so_snd.sb_hiwat * 16, tp->snd_max) &&
294 (rt = inp->inp_route.ro_rt) &&
295 !in_nullhost(satosin(rt_key(rt))->sin_addr)) {
296 register u_long i = 0;
297
298 if ((rt->rt_rmx.rmx_locks & RTV_RTT) == 0) {
299 i = tp->t_srtt *
300 (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTT_SCALE));
301 if (rt->rt_rmx.rmx_rtt && i)
302 /*
303 * filter this update to half the old & half
304 * the new values, converting scale.
305 * See route.h and tcp_var.h for a
306 * description of the scaling constants.
307 */
308 rt->rt_rmx.rmx_rtt =
309 (rt->rt_rmx.rmx_rtt + i) / 2;
310 else
311 rt->rt_rmx.rmx_rtt = i;
312 }
313 if ((rt->rt_rmx.rmx_locks & RTV_RTTVAR) == 0) {
314 i = tp->t_rttvar *
315 (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTTVAR_SCALE));
316 if (rt->rt_rmx.rmx_rttvar && i)
317 rt->rt_rmx.rmx_rttvar =
318 (rt->rt_rmx.rmx_rttvar + i) / 2;
319 else
320 rt->rt_rmx.rmx_rttvar = i;
321 }
322 /*
323 * update the pipelimit (ssthresh) if it has been updated
324 * already or if a pipesize was specified & the threshhold
325 * got below half the pipesize. I.e., wait for bad news
326 * before we start updating, then update on both good
327 * and bad news.
328 */
329 if (((rt->rt_rmx.rmx_locks & RTV_SSTHRESH) == 0 &&
330 (i = tp->snd_ssthresh) && rt->rt_rmx.rmx_ssthresh) ||
331 i < (rt->rt_rmx.rmx_sendpipe / 2)) {
332 /*
333 * convert the limit from user data bytes to
334 * packets then to packet data bytes.
335 */
336 i = (i + tp->t_maxseg / 2) / tp->t_maxseg;
337 if (i < 2)
338 i = 2;
339 i *= (u_long)(tp->t_maxseg + sizeof (struct tcpiphdr));
340 if (rt->rt_rmx.rmx_ssthresh)
341 rt->rt_rmx.rmx_ssthresh =
342 (rt->rt_rmx.rmx_ssthresh + i) / 2;
343 else
344 rt->rt_rmx.rmx_ssthresh = i;
345 }
346 }
347 #endif /* RTV_RTT */
348 /* free the reassembly queue, if any */
349 while ((qe = tp->segq.lh_first) != NULL) {
350 LIST_REMOVE(qe, ipqe_q);
351 m_freem(qe->ipqe_m);
352 FREE(qe, M_IPQ);
353 }
354 if (tp->t_template)
355 (void) m_free(dtom(tp->t_template));
356 free(tp, M_PCB);
357 inp->inp_ppcb = 0;
358 soisdisconnected(so);
359 in_pcbdetach(inp);
360 tcpstat.tcps_closed++;
361 return ((struct tcpcb *)0);
362 }
363
364 void
365 tcp_drain()
366 {
367
368 }
369
370 /*
371 * Notify a tcp user of an asynchronous error;
372 * store error as soft error, but wake up user
373 * (for now, won't do anything until can select for soft error).
374 */
375 void
376 tcp_notify(inp, error)
377 struct inpcb *inp;
378 int error;
379 {
380 register struct tcpcb *tp = (struct tcpcb *)inp->inp_ppcb;
381 register struct socket *so = inp->inp_socket;
382
383 /*
384 * Ignore some errors if we are hooked up.
385 * If connection hasn't completed, has retransmitted several times,
386 * and receives a second error, give up now. This is better
387 * than waiting a long time to establish a connection that
388 * can never complete.
389 */
390 if (tp->t_state == TCPS_ESTABLISHED &&
391 (error == EHOSTUNREACH || error == ENETUNREACH ||
392 error == EHOSTDOWN)) {
393 return;
394 } else if (TCPS_HAVEESTABLISHED(tp->t_state) == 0 &&
395 tp->t_rxtshift > 3 && tp->t_softerror)
396 so->so_error = error;
397 else
398 tp->t_softerror = error;
399 wakeup((caddr_t) &so->so_timeo);
400 sorwakeup(so);
401 sowwakeup(so);
402 }
403
404 void *
405 tcp_ctlinput(cmd, sa, v)
406 int cmd;
407 struct sockaddr *sa;
408 register void *v;
409 {
410 register struct ip *ip = v;
411 register struct tcphdr *th;
412 extern int inetctlerrmap[];
413 void (*notify) __P((struct inpcb *, int)) = tcp_notify;
414 int errno;
415
416 if ((unsigned)cmd >= PRC_NCMDS)
417 return NULL;
418 errno = inetctlerrmap[cmd];
419 if (cmd == PRC_QUENCH)
420 notify = tcp_quench;
421 else if (PRC_IS_REDIRECT(cmd))
422 notify = in_rtchange, ip = 0;
423 else if (cmd == PRC_HOSTDEAD)
424 ip = 0;
425 else if (errno == 0)
426 return NULL;
427 if (ip) {
428 th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
429 in_pcbnotify(&tcbtable, satosin(sa)->sin_addr, th->th_dport,
430 ip->ip_src, th->th_sport, errno, notify);
431 } else
432 in_pcbnotifyall(&tcbtable, satosin(sa)->sin_addr, errno,
433 notify);
434 return NULL;
435 }
436
437 /*
438 * When a source quench is received, close congestion window
439 * to one segment. We will gradually open it again as we proceed.
440 */
441 void
442 tcp_quench(inp, errno)
443 struct inpcb *inp;
444 int errno;
445 {
446 struct tcpcb *tp = intotcpcb(inp);
447
448 if (tp)
449 tp->snd_cwnd = tp->t_maxseg;
450 }
451