tcp_subr.c revision 1.58 1 /* $NetBSD: tcp_subr.c,v 1.58 1998/09/04 22:29:54 mycroft Exp $ */
2
3 /*-
4 * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe and Kevin M. Lahey of the Numerical Aerospace Simulation
9 * Facility, NASA Ames Research Center.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*
41 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
42 * The Regents of the University of California. All rights reserved.
43 *
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
46 * are met:
47 * 1. Redistributions of source code must retain the above copyright
48 * notice, this list of conditions and the following disclaimer.
49 * 2. Redistributions in binary form must reproduce the above copyright
50 * notice, this list of conditions and the following disclaimer in the
51 * documentation and/or other materials provided with the distribution.
52 * 3. All advertising materials mentioning features or use of this software
53 * must display the following acknowledgement:
54 * This product includes software developed by the University of
55 * California, Berkeley and its contributors.
56 * 4. Neither the name of the University nor the names of its contributors
57 * may be used to endorse or promote products derived from this software
58 * without specific prior written permission.
59 *
60 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
61 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
64 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70 * SUCH DAMAGE.
71 *
72 * @(#)tcp_subr.c 8.2 (Berkeley) 5/24/95
73 */
74
75 #include "opt_tcp_compat_42.h"
76 #include "rnd.h"
77
78 #include <sys/param.h>
79 #include <sys/proc.h>
80 #include <sys/systm.h>
81 #include <sys/malloc.h>
82 #include <sys/mbuf.h>
83 #include <sys/socket.h>
84 #include <sys/socketvar.h>
85 #include <sys/protosw.h>
86 #include <sys/errno.h>
87 #include <sys/kernel.h>
88 #include <sys/pool.h>
89 #if NRND > 0
90 #include <sys/rnd.h>
91 #endif
92
93 #include <net/route.h>
94 #include <net/if.h>
95
96 #include <netinet/in.h>
97 #include <netinet/in_systm.h>
98 #include <netinet/ip.h>
99 #include <netinet/in_pcb.h>
100 #include <netinet/ip_var.h>
101 #include <netinet/ip_icmp.h>
102 #include <netinet/tcp.h>
103 #include <netinet/tcp_fsm.h>
104 #include <netinet/tcp_seq.h>
105 #include <netinet/tcp_timer.h>
106 #include <netinet/tcp_var.h>
107 #include <netinet/tcpip.h>
108
109 /* patchable/settable parameters for tcp */
110 int tcp_mssdflt = TCP_MSS;
111 int tcp_rttdflt = TCPTV_SRTTDFLT / PR_SLOWHZ;
112 int tcp_do_rfc1323 = 1; /* window scaling / timestamps (obsolete) */
113 int tcp_do_sack = 1; /* selective acknowledgement */
114 int tcp_do_win_scale = 1; /* RFC1323 window scaling */
115 int tcp_do_timestamps = 1; /* RFC1323 timestamps */
116 int tcp_ack_on_push = 0; /* set to enable immediate ACK-on-PUSH */
117 int tcp_init_win = 1;
118 int tcp_mss_ifmtu = 0;
119 #ifdef TCP_COMPAT_42
120 int tcp_compat_42 = 1;
121 #else
122 int tcp_compat_42 = 0;
123 #endif
124
125 #ifndef TCBHASHSIZE
126 #define TCBHASHSIZE 128
127 #endif
128 int tcbhashsize = TCBHASHSIZE;
129
130 int tcp_freeq __P((struct tcpcb *));
131
132 struct pool tcpcb_pool;
133
134 /*
135 * Tcp initialization
136 */
137 void
138 tcp_init()
139 {
140
141 pool_init(&tcpcb_pool, sizeof(struct tcpcb), 0, 0, 0, "tcpcbpl",
142 0, NULL, NULL, M_PCB);
143 in_pcbinit(&tcbtable, tcbhashsize, tcbhashsize);
144 LIST_INIT(&tcp_delacks);
145 if (max_protohdr < sizeof(struct tcpiphdr))
146 max_protohdr = sizeof(struct tcpiphdr);
147 if (max_linkhdr + sizeof(struct tcpiphdr) > MHLEN)
148 panic("tcp_init");
149
150 /* Initialize the compressed state engine. */
151 syn_cache_init();
152 }
153
154 /*
155 * Create template to be used to send tcp packets on a connection.
156 * Call after host entry created, allocates an mbuf and fills
157 * in a skeletal tcp/ip header, minimizing the amount of work
158 * necessary when the connection is used.
159 */
160 struct tcpiphdr *
161 tcp_template(tp)
162 struct tcpcb *tp;
163 {
164 register struct inpcb *inp = tp->t_inpcb;
165 register struct tcpiphdr *n;
166
167 if ((n = tp->t_template) == 0) {
168 MALLOC(n, struct tcpiphdr *, sizeof (struct tcpiphdr),
169 M_MBUF, M_NOWAIT);
170 if (n == NULL)
171 return (0);
172 }
173 bzero(n->ti_x1, sizeof n->ti_x1);
174 n->ti_pr = IPPROTO_TCP;
175 n->ti_len = htons(sizeof (struct tcpiphdr) - sizeof (struct ip));
176 n->ti_src = inp->inp_laddr;
177 n->ti_dst = inp->inp_faddr;
178 n->ti_sport = inp->inp_lport;
179 n->ti_dport = inp->inp_fport;
180 n->ti_seq = 0;
181 n->ti_ack = 0;
182 n->ti_x2 = 0;
183 n->ti_off = 5;
184 n->ti_flags = 0;
185 n->ti_win = 0;
186 n->ti_sum = 0;
187 n->ti_urp = 0;
188 return (n);
189 }
190
191 /*
192 * Send a single message to the TCP at address specified by
193 * the given TCP/IP header. If m == 0, then we make a copy
194 * of the tcpiphdr at ti and send directly to the addressed host.
195 * This is used to force keep alive messages out using the TCP
196 * template for a connection tp->t_template. If flags are given
197 * then we send a message back to the TCP which originated the
198 * segment ti, and discard the mbuf containing it and any other
199 * attached mbufs.
200 *
201 * In any case the ack and sequence number of the transmitted
202 * segment are as specified by the parameters.
203 */
204 int
205 tcp_respond(tp, ti, m, ack, seq, flags)
206 struct tcpcb *tp;
207 register struct tcpiphdr *ti;
208 register struct mbuf *m;
209 tcp_seq ack, seq;
210 int flags;
211 {
212 register int tlen;
213 int win = 0;
214 struct route *ro = 0;
215
216 if (tp) {
217 win = sbspace(&tp->t_inpcb->inp_socket->so_rcv);
218 ro = &tp->t_inpcb->inp_route;
219 }
220 if (m == 0) {
221 m = m_gethdr(M_DONTWAIT, MT_HEADER);
222 if (m == NULL)
223 return (ENOBUFS);
224
225 if (tcp_compat_42)
226 tlen = 1;
227 else
228 tlen = 0;
229
230 m->m_data += max_linkhdr;
231 *mtod(m, struct tcpiphdr *) = *ti;
232 ti = mtod(m, struct tcpiphdr *);
233 flags = TH_ACK;
234 } else {
235 m_freem(m->m_next);
236 m->m_next = 0;
237 m->m_data = (caddr_t)ti;
238 m->m_len = sizeof (struct tcpiphdr);
239 tlen = 0;
240 #define xchg(a,b,type) { type t; t=a; a=b; b=t; }
241 xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr, u_int32_t);
242 xchg(ti->ti_dport, ti->ti_sport, u_int16_t);
243 #undef xchg
244 }
245 bzero(ti->ti_x1, sizeof ti->ti_x1);
246 ti->ti_seq = htonl(seq);
247 ti->ti_ack = htonl(ack);
248 ti->ti_x2 = 0;
249 if ((flags & TH_SYN) == 0) {
250 if (tp)
251 ti->ti_win = htons((u_int16_t) (win >> tp->rcv_scale));
252 else
253 ti->ti_win = htons((u_int16_t)win);
254 ti->ti_off = sizeof (struct tcphdr) >> 2;
255 tlen += sizeof (struct tcphdr);
256 } else
257 tlen += ti->ti_off << 2;
258 ti->ti_len = htons((u_int16_t)tlen);
259 tlen += sizeof (struct ip);
260 m->m_len = tlen;
261 m->m_pkthdr.len = tlen;
262 m->m_pkthdr.rcvif = (struct ifnet *) 0;
263 ti->ti_flags = flags;
264 ti->ti_urp = 0;
265 ti->ti_sum = 0;
266 ti->ti_sum = in_cksum(m, tlen);
267 ((struct ip *)ti)->ip_len = tlen;
268 ((struct ip *)ti)->ip_ttl = ip_defttl;
269 return ip_output(m, NULL, ro, 0, NULL);
270 }
271
272 /*
273 * Create a new TCP control block, making an
274 * empty reassembly queue and hooking it to the argument
275 * protocol control block.
276 */
277 struct tcpcb *
278 tcp_newtcpcb(inp)
279 struct inpcb *inp;
280 {
281 register struct tcpcb *tp;
282
283 tp = pool_get(&tcpcb_pool, PR_NOWAIT);
284 if (tp == NULL)
285 return (NULL);
286 bzero((caddr_t)tp, sizeof(struct tcpcb));
287 LIST_INIT(&tp->segq);
288 LIST_INIT(&tp->timeq);
289 tp->t_peermss = tcp_mssdflt;
290 tp->t_ourmss = tcp_mssdflt;
291 tp->t_segsz = tcp_mssdflt;
292
293 tp->t_flags = 0;
294 if (tcp_do_rfc1323 && tcp_do_win_scale)
295 tp->t_flags |= TF_REQ_SCALE;
296 if (tcp_do_rfc1323 && tcp_do_timestamps)
297 tp->t_flags |= TF_REQ_TSTMP;
298 if (tcp_do_sack == 2)
299 tp->t_flags |= TF_WILL_SACK;
300 else if (tcp_do_sack == 1)
301 tp->t_flags |= TF_WILL_SACK|TF_IGNR_RXSACK;
302 tp->t_flags |= TF_CANT_TXSACK;
303 tp->t_inpcb = inp;
304 /*
305 * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no
306 * rtt estimate. Set rttvar so that srtt + 2 * rttvar gives
307 * reasonable initial retransmit time.
308 */
309 tp->t_srtt = TCPTV_SRTTBASE;
310 tp->t_rttvar = tcp_rttdflt * PR_SLOWHZ << (TCP_RTTVAR_SHIFT + 2 - 1);
311 tp->t_rttmin = TCPTV_MIN;
312 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
313 TCPTV_MIN, TCPTV_REXMTMAX);
314 tp->snd_cwnd = TCP_MAXWIN << TCP_MAX_WINSHIFT;
315 tp->snd_ssthresh = TCP_MAXWIN << TCP_MAX_WINSHIFT;
316 inp->inp_ip.ip_ttl = ip_defttl;
317 inp->inp_ppcb = (caddr_t)tp;
318 return (tp);
319 }
320
321 /*
322 * Drop a TCP connection, reporting
323 * the specified error. If connection is synchronized,
324 * then send a RST to peer.
325 */
326 struct tcpcb *
327 tcp_drop(tp, errno)
328 register struct tcpcb *tp;
329 int errno;
330 {
331 struct socket *so = tp->t_inpcb->inp_socket;
332
333 if (TCPS_HAVERCVDSYN(tp->t_state)) {
334 tp->t_state = TCPS_CLOSED;
335 (void) tcp_output(tp);
336 tcpstat.tcps_drops++;
337 } else
338 tcpstat.tcps_conndrops++;
339 if (errno == ETIMEDOUT && tp->t_softerror)
340 errno = tp->t_softerror;
341 so->so_error = errno;
342 return (tcp_close(tp));
343 }
344
345 /*
346 * Close a TCP control block:
347 * discard all space held by the tcp
348 * discard internet protocol block
349 * wake up any sleepers
350 */
351 struct tcpcb *
352 tcp_close(tp)
353 register struct tcpcb *tp;
354 {
355 struct inpcb *inp = tp->t_inpcb;
356 struct socket *so = inp->inp_socket;
357 #ifdef RTV_RTT
358 register struct rtentry *rt;
359
360 /*
361 * If we sent enough data to get some meaningful characteristics,
362 * save them in the routing entry. 'Enough' is arbitrarily
363 * defined as the sendpipesize (default 4K) * 16. This would
364 * give us 16 rtt samples assuming we only get one sample per
365 * window (the usual case on a long haul net). 16 samples is
366 * enough for the srtt filter to converge to within 5% of the correct
367 * value; fewer samples and we could save a very bogus rtt.
368 *
369 * Don't update the default route's characteristics and don't
370 * update anything that the user "locked".
371 */
372 if (SEQ_LT(tp->iss + so->so_snd.sb_hiwat * 16, tp->snd_max) &&
373 (rt = inp->inp_route.ro_rt) &&
374 !in_nullhost(satosin(rt_key(rt))->sin_addr)) {
375 register u_long i = 0;
376
377 if ((rt->rt_rmx.rmx_locks & RTV_RTT) == 0) {
378 i = tp->t_srtt *
379 ((RTM_RTTUNIT / PR_SLOWHZ) >> (TCP_RTT_SHIFT + 2));
380 if (rt->rt_rmx.rmx_rtt && i)
381 /*
382 * filter this update to half the old & half
383 * the new values, converting scale.
384 * See route.h and tcp_var.h for a
385 * description of the scaling constants.
386 */
387 rt->rt_rmx.rmx_rtt =
388 (rt->rt_rmx.rmx_rtt + i) / 2;
389 else
390 rt->rt_rmx.rmx_rtt = i;
391 }
392 if ((rt->rt_rmx.rmx_locks & RTV_RTTVAR) == 0) {
393 i = tp->t_rttvar *
394 ((RTM_RTTUNIT / PR_SLOWHZ) >> (TCP_RTTVAR_SHIFT + 2));
395 if (rt->rt_rmx.rmx_rttvar && i)
396 rt->rt_rmx.rmx_rttvar =
397 (rt->rt_rmx.rmx_rttvar + i) / 2;
398 else
399 rt->rt_rmx.rmx_rttvar = i;
400 }
401 /*
402 * update the pipelimit (ssthresh) if it has been updated
403 * already or if a pipesize was specified & the threshhold
404 * got below half the pipesize. I.e., wait for bad news
405 * before we start updating, then update on both good
406 * and bad news.
407 */
408 if (((rt->rt_rmx.rmx_locks & RTV_SSTHRESH) == 0 &&
409 (i = tp->snd_ssthresh) && rt->rt_rmx.rmx_ssthresh) ||
410 i < (rt->rt_rmx.rmx_sendpipe / 2)) {
411 /*
412 * convert the limit from user data bytes to
413 * packets then to packet data bytes.
414 */
415 i = (i + tp->t_segsz / 2) / tp->t_segsz;
416 if (i < 2)
417 i = 2;
418 i *= (u_long)(tp->t_segsz + sizeof (struct tcpiphdr));
419 if (rt->rt_rmx.rmx_ssthresh)
420 rt->rt_rmx.rmx_ssthresh =
421 (rt->rt_rmx.rmx_ssthresh + i) / 2;
422 else
423 rt->rt_rmx.rmx_ssthresh = i;
424 }
425 }
426 #endif /* RTV_RTT */
427 /* free the reassembly queue, if any */
428 (void) tcp_freeq(tp);
429 TCP_CLEAR_DELACK(tp);
430
431 if (tp->t_template)
432 FREE(tp->t_template, M_MBUF);
433 pool_put(&tcpcb_pool, tp);
434 inp->inp_ppcb = 0;
435 soisdisconnected(so);
436 in_pcbdetach(inp);
437 tcpstat.tcps_closed++;
438 return ((struct tcpcb *)0);
439 }
440
441 int
442 tcp_freeq(tp)
443 struct tcpcb *tp;
444 {
445 register struct ipqent *qe;
446 int rv = 0;
447 #ifdef TCPREASS_DEBUG
448 int i = 0;
449 #endif
450
451 while ((qe = tp->segq.lh_first) != NULL) {
452 #ifdef TCPREASS_DEBUG
453 printf("tcp_freeq[%p,%d]: %u:%u(%u) 0x%02x\n",
454 tp, i++, qe->ipqe_seq, qe->ipqe_seq + qe->ipqe_len,
455 qe->ipqe_len, qe->ipqe_flags & (TH_SYN|TH_FIN|TH_RST));
456 #endif
457 LIST_REMOVE(qe, ipqe_q);
458 LIST_REMOVE(qe, ipqe_timeq);
459 m_freem(qe->ipqe_m);
460 FREE(qe, M_IPQ);
461 rv = 1;
462 }
463 return (rv);
464 }
465
466 /*
467 * Protocol drain routine. Called when memory is in short supply.
468 */
469 void
470 tcp_drain()
471 {
472 register struct inpcb *inp;
473 register struct tcpcb *tp;
474
475 /*
476 * Free the sequence queue of all TCP connections.
477 */
478 inp = tcbtable.inpt_queue.cqh_first;
479 if (inp) /* XXX */
480 for (; inp != (struct inpcb *)&tcbtable.inpt_queue;
481 inp = inp->inp_queue.cqe_next) {
482 if ((tp = intotcpcb(inp)) != NULL) {
483 if (tcp_freeq(tp))
484 tcpstat.tcps_connsdrained++;
485 }
486 }
487 }
488
489 /*
490 * Notify a tcp user of an asynchronous error;
491 * store error as soft error, but wake up user
492 * (for now, won't do anything until can select for soft error).
493 */
494 void
495 tcp_notify(inp, error)
496 struct inpcb *inp;
497 int error;
498 {
499 register struct tcpcb *tp = (struct tcpcb *)inp->inp_ppcb;
500 register struct socket *so = inp->inp_socket;
501
502 /*
503 * Ignore some errors if we are hooked up.
504 * If connection hasn't completed, has retransmitted several times,
505 * and receives a second error, give up now. This is better
506 * than waiting a long time to establish a connection that
507 * can never complete.
508 */
509 if (tp->t_state == TCPS_ESTABLISHED &&
510 (error == EHOSTUNREACH || error == ENETUNREACH ||
511 error == EHOSTDOWN)) {
512 return;
513 } else if (TCPS_HAVEESTABLISHED(tp->t_state) == 0 &&
514 tp->t_rxtshift > 3 && tp->t_softerror)
515 so->so_error = error;
516 else
517 tp->t_softerror = error;
518 wakeup((caddr_t) &so->so_timeo);
519 sorwakeup(so);
520 sowwakeup(so);
521 }
522
523 void *
524 tcp_ctlinput(cmd, sa, v)
525 int cmd;
526 struct sockaddr *sa;
527 register void *v;
528 {
529 register struct ip *ip = v;
530 register struct tcphdr *th;
531 extern int inetctlerrmap[];
532 void (*notify) __P((struct inpcb *, int)) = tcp_notify;
533 int errno;
534 int nmatch;
535
536 if ((unsigned)cmd >= PRC_NCMDS)
537 return NULL;
538 errno = inetctlerrmap[cmd];
539 if (cmd == PRC_QUENCH)
540 notify = tcp_quench;
541 else if (PRC_IS_REDIRECT(cmd))
542 notify = in_rtchange, ip = 0;
543 else if (cmd == PRC_MSGSIZE && ip_mtudisc)
544 notify = tcp_mtudisc, ip = 0;
545 else if (cmd == PRC_HOSTDEAD)
546 ip = 0;
547 else if (errno == 0)
548 return NULL;
549 if (ip) {
550 th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
551 nmatch = in_pcbnotify(&tcbtable, satosin(sa)->sin_addr,
552 th->th_dport, ip->ip_src, th->th_sport, errno, notify);
553 if (nmatch == 0 && syn_cache_count &&
554 (inetctlerrmap[cmd] == EHOSTUNREACH ||
555 inetctlerrmap[cmd] == ENETUNREACH ||
556 inetctlerrmap[cmd] == EHOSTDOWN))
557 syn_cache_unreach(ip, th);
558 } else
559 (void)in_pcbnotifyall(&tcbtable, satosin(sa)->sin_addr, errno,
560 notify);
561 return NULL;
562 }
563
564 /*
565 * When a source quence is received, we are being notifed of congestion.
566 * Close the congestion window down to the Loss Window (one segment).
567 * We will gradually open it again as we proceed.
568 */
569 void
570 tcp_quench(inp, errno)
571 struct inpcb *inp;
572 int errno;
573 {
574 struct tcpcb *tp = intotcpcb(inp);
575
576 if (tp)
577 tp->snd_cwnd = tp->t_segsz;
578 }
579
580 /*
581 * On receipt of path MTU corrections, flush old route and replace it
582 * with the new one. Retransmit all unacknowledged packets, to ensure
583 * that all packets will be received.
584 */
585 void
586 tcp_mtudisc(inp, errno)
587 struct inpcb *inp;
588 int errno;
589 {
590 struct tcpcb *tp = intotcpcb(inp);
591 struct rtentry *rt = in_pcbrtentry(inp);
592
593 if (tp != 0) {
594 if (rt != 0) {
595 /*
596 * If this was not a host route, remove and realloc.
597 */
598 if ((rt->rt_flags & RTF_HOST) == 0) {
599 in_rtchange(inp, errno);
600 if ((rt = in_pcbrtentry(inp)) == 0)
601 return;
602 }
603
604 /*
605 * Slow start out of the error condition. We
606 * use the MTU because we know it's smaller
607 * than the previously transmitted segment.
608 *
609 * Note: This is more conservative than the
610 * suggestion in draft-floyd-incr-init-win-03.
611 */
612 if (rt->rt_rmx.rmx_mtu != 0)
613 tp->snd_cwnd =
614 TCP_INITIAL_WINDOW(tcp_init_win,
615 rt->rt_rmx.rmx_mtu);
616 }
617
618 /*
619 * Resend unacknowledged packets.
620 */
621 tp->snd_nxt = tp->snd_una;
622 tcp_output(tp);
623 }
624 }
625
626
627 /*
628 * Compute the MSS to advertise to the peer. Called only during
629 * the 3-way handshake. If we are the server (peer initiated
630 * connection), we are called with a pointer to the interface
631 * on which the SYN packet arrived. If we are the client (we
632 * initiated connection), we are called with a pointer to the
633 * interface out which this connection should go.
634 */
635 u_long
636 tcp_mss_to_advertise(ifp)
637 const struct ifnet *ifp;
638 {
639 extern u_long in_maxmtu;
640 u_long mss = 0;
641
642 /*
643 * In order to avoid defeating path MTU discovery on the peer,
644 * we advertise the max MTU of all attached networks as our MSS,
645 * per RFC 1191, section 3.1.
646 *
647 * We provide the option to advertise just the MTU of
648 * the interface on which we hope this connection will
649 * be receiving. If we are responding to a SYN, we
650 * will have a pretty good idea about this, but when
651 * initiating a connection there is a bit more doubt.
652 *
653 * We also need to ensure that loopback has a large enough
654 * MSS, as the loopback MTU is never included in in_maxmtu.
655 */
656
657 if (ifp != NULL)
658 mss = ifp->if_mtu;
659
660 if (tcp_mss_ifmtu == 0)
661 mss = max(in_maxmtu, mss);
662
663 if (mss > sizeof(struct tcpiphdr))
664 mss -= sizeof(struct tcpiphdr);
665
666 mss = max(tcp_mssdflt, mss);
667 return (mss);
668 }
669
670 /*
671 * Set connection variables based on the peer's advertised MSS.
672 * We are passed the TCPCB for the actual connection. If we
673 * are the server, we are called by the compressed state engine
674 * when the 3-way handshake is complete. If we are the client,
675 * we are called when we recieve the SYN,ACK from the server.
676 *
677 * NOTE: Our advertised MSS value must be initialized in the TCPCB
678 * before this routine is called!
679 */
680 void
681 tcp_mss_from_peer(tp, offer)
682 struct tcpcb *tp;
683 int offer;
684 {
685 struct inpcb *inp = tp->t_inpcb;
686 struct socket *so = inp->inp_socket;
687 #if defined(RTV_SPIPE) || defined(RTV_SSTHRESH)
688 struct rtentry *rt = in_pcbrtentry(inp);
689 #endif
690 u_long bufsize;
691 int mss;
692
693 /*
694 * As per RFC1122, use the default MSS value, unless they
695 * sent us an offer. Do not accept offers less than 32 bytes.
696 */
697 mss = tcp_mssdflt;
698 if (offer)
699 mss = offer;
700 mss = max(mss, 32); /* sanity */
701 tp->t_peermss = mss;
702 mss -= (tcp_optlen(tp) + ip_optlen(tp->t_inpcb));
703
704 /*
705 * If there's a pipesize, change the socket buffer to that size.
706 * Make the socket buffer an integral number of MSS units. If
707 * the MSS is larger than the socket buffer, artificially decrease
708 * the MSS.
709 */
710 #ifdef RTV_SPIPE
711 if (rt != NULL && rt->rt_rmx.rmx_sendpipe != 0)
712 bufsize = rt->rt_rmx.rmx_sendpipe;
713 else
714 #endif
715 bufsize = so->so_snd.sb_hiwat;
716 if (bufsize < mss)
717 mss = bufsize;
718 else {
719 bufsize = roundup(bufsize, mss);
720 if (bufsize > sb_max)
721 bufsize = sb_max;
722 (void) sbreserve(&so->so_snd, bufsize);
723 }
724 tp->t_segsz = mss;
725
726 #ifdef RTV_SSTHRESH
727 if (rt != NULL && rt->rt_rmx.rmx_ssthresh) {
728 /*
729 * There's some sort of gateway or interface buffer
730 * limit on the path. Use this to set the slow
731 * start threshold, but set the threshold to no less
732 * than 2 * MSS.
733 */
734 tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh);
735 }
736 #endif
737 }
738
739 /*
740 * Processing necessary when a TCP connection is established.
741 */
742 void
743 tcp_established(tp)
744 struct tcpcb *tp;
745 {
746 struct inpcb *inp = tp->t_inpcb;
747 struct socket *so = inp->inp_socket;
748 #ifdef RTV_RPIPE
749 struct rtentry *rt = in_pcbrtentry(inp);
750 #endif
751 u_long bufsize;
752
753 tp->t_state = TCPS_ESTABLISHED;
754 TCP_TIMER_ARM(tp, TCPT_KEEP, tcp_keepidle);
755
756 #ifdef RTV_RPIPE
757 if (rt != NULL && rt->rt_rmx.rmx_recvpipe != 0)
758 bufsize = rt->rt_rmx.rmx_recvpipe;
759 else
760 #endif
761 bufsize = so->so_rcv.sb_hiwat;
762 if (bufsize > tp->t_ourmss) {
763 bufsize = roundup(bufsize, tp->t_ourmss);
764 if (bufsize > sb_max)
765 bufsize = sb_max;
766 (void) sbreserve(&so->so_rcv, bufsize);
767 }
768 }
769
770 /*
771 * Check if there's an initial rtt or rttvar. Convert from the
772 * route-table units to scaled multiples of the slow timeout timer.
773 * Called only during the 3-way handshake.
774 */
775 void
776 tcp_rmx_rtt(tp)
777 struct tcpcb *tp;
778 {
779 #ifdef RTV_RTT
780 struct rtentry *rt;
781 int rtt;
782
783 if ((rt = in_pcbrtentry(tp->t_inpcb)) == NULL)
784 return;
785
786 if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) {
787 /*
788 * XXX The lock bit for MTU indicates that the value
789 * is also a minimum value; this is subject to time.
790 */
791 if (rt->rt_rmx.rmx_locks & RTV_RTT)
792 TCPT_RANGESET(tp->t_rttmin,
793 rtt / (RTM_RTTUNIT / PR_SLOWHZ),
794 TCPTV_MIN, TCPTV_REXMTMAX);
795 tp->t_srtt = rtt /
796 ((RTM_RTTUNIT / PR_SLOWHZ) >> (TCP_RTT_SHIFT + 2));
797 if (rt->rt_rmx.rmx_rttvar) {
798 tp->t_rttvar = rt->rt_rmx.rmx_rttvar /
799 ((RTM_RTTUNIT / PR_SLOWHZ) >>
800 (TCP_RTTVAR_SHIFT + 2));
801 } else {
802 /* Default variation is +- 1 rtt */
803 tp->t_rttvar =
804 tp->t_srtt >> (TCP_RTT_SHIFT - TCP_RTTVAR_SHIFT);
805 }
806 TCPT_RANGESET(tp->t_rxtcur,
807 ((tp->t_srtt >> 2) + tp->t_rttvar) >> (1 + 2),
808 tp->t_rttmin, TCPTV_REXMTMAX);
809 }
810 #endif
811 }
812
813 tcp_seq tcp_iss_seq = 0; /* tcp initial seq # */
814
815 /*
816 * Get a new sequence value given a tcp control block
817 */
818 tcp_seq
819 tcp_new_iss(tp, len, addin)
820 void *tp;
821 u_long len;
822 tcp_seq addin;
823 {
824 tcp_seq tcp_iss;
825
826 /*
827 * add randomness about this connection, but do not estimate
828 * entropy from the timing, since the physical device driver would
829 * have done that for us.
830 */
831 #if NRND > 0
832 if (tp != NULL)
833 rnd_add_data(NULL, tp, len, 0);
834 #endif
835
836 /*
837 * randomize.
838 */
839 #if NRND > 0
840 rnd_extract_data(&tcp_iss, sizeof(tcp_iss), RND_EXTRACT_ANY);
841 #else
842 tcp_iss = random();
843 #endif
844
845 /*
846 * If we were asked to add some amount to a known value,
847 * we will take a random value obtained above, mask off the upper
848 * bits, and add in the known value. We also add in a constant to
849 * ensure that we are at least a certain distance from the original
850 * value.
851 *
852 * This is used when an old connection is in timed wait
853 * and we have a new one coming in, for instance.
854 */
855 if (addin != 0) {
856 #ifdef TCPISS_DEBUG
857 printf("Random %08x, ", tcp_iss);
858 #endif
859 tcp_iss &= TCP_ISS_RANDOM_MASK;
860 tcp_iss += addin + TCP_ISSINCR;
861 #ifdef TCPISS_DEBUG
862 printf("Old ISS %08x, ISS %08x\n", addin, tcp_iss);
863 #endif
864 } else {
865 tcp_iss &= TCP_ISS_RANDOM_MASK;
866 tcp_iss += tcp_iss_seq;
867 tcp_iss_seq += TCP_ISSINCR;
868 #ifdef TCPISS_DEBUG
869 printf("ISS %08x\n", tcp_iss);
870 #endif
871 }
872
873 if (tcp_compat_42) {
874 /*
875 * Limit it to the positive range for really old TCP
876 * implementations.
877 */
878 if (tcp_iss >= 0x80000000)
879 tcp_iss &= 0x7fffffff; /* XXX */
880 }
881
882 return tcp_iss;
883 }
884
885
886 /*
887 * Determine the length of the TCP options for this connection.
888 *
889 * XXX: What do we do for SACK, when we add that? Just reserve
890 * all of the space? Otherwise we can't exactly be incrementing
891 * cwnd by an amount that varies depending on the amount we last
892 * had to SACK!
893 */
894
895 u_int
896 tcp_optlen(tp)
897 struct tcpcb *tp;
898 {
899 if ((tp->t_flags & (TF_REQ_TSTMP|TF_RCVD_TSTMP|TF_NOOPT)) ==
900 (TF_REQ_TSTMP | TF_RCVD_TSTMP))
901 return TCPOLEN_TSTAMP_APPA;
902 else
903 return 0;
904 }
905