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