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