tcp_subr.c revision 1.204 1 /* $NetBSD: tcp_subr.c,v 1.204 2006/10/12 01:32:38 christos Exp $ */
2
3 /*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * 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. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 /*-
33 * Copyright (c) 1997, 1998, 2000, 2001 The NetBSD Foundation, Inc.
34 * All rights reserved.
35 *
36 * This code is derived from software contributed to The NetBSD Foundation
37 * by Jason R. Thorpe and Kevin M. Lahey of the Numerical Aerospace Simulation
38 * Facility, NASA Ames Research Center.
39 *
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
42 * are met:
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. All advertising materials mentioning features or use of this software
49 * must display the following acknowledgement:
50 * This product includes software developed by the NetBSD
51 * Foundation, Inc. and its contributors.
52 * 4. Neither the name of The NetBSD Foundation nor the names of its
53 * contributors may be used to endorse or promote products derived
54 * from this software without specific prior written permission.
55 *
56 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
57 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
58 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
59 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
60 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
61 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
62 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
63 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
64 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
65 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
66 * POSSIBILITY OF SUCH DAMAGE.
67 */
68
69 /*
70 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
71 * The Regents of the University of California. All rights reserved.
72 *
73 * Redistribution and use in source and binary forms, with or without
74 * modification, are permitted provided that the following conditions
75 * are met:
76 * 1. Redistributions of source code must retain the above copyright
77 * notice, this list of conditions and the following disclaimer.
78 * 2. Redistributions in binary form must reproduce the above copyright
79 * notice, this list of conditions and the following disclaimer in the
80 * documentation and/or other materials provided with the distribution.
81 * 3. Neither the name of the University nor the names of its contributors
82 * may be used to endorse or promote products derived from this software
83 * without specific prior written permission.
84 *
85 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
86 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
87 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
88 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
89 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
90 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
91 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
92 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
93 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
94 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
95 * SUCH DAMAGE.
96 *
97 * @(#)tcp_subr.c 8.2 (Berkeley) 5/24/95
98 */
99
100 #include <sys/cdefs.h>
101 __KERNEL_RCSID(0, "$NetBSD: tcp_subr.c,v 1.204 2006/10/12 01:32:38 christos Exp $");
102
103 #include "opt_inet.h"
104 #include "opt_ipsec.h"
105 #include "opt_tcp_compat_42.h"
106 #include "opt_inet_csum.h"
107 #include "opt_mbuftrace.h"
108 #include "rnd.h"
109
110 #include <sys/param.h>
111 #include <sys/proc.h>
112 #include <sys/systm.h>
113 #include <sys/malloc.h>
114 #include <sys/mbuf.h>
115 #include <sys/socket.h>
116 #include <sys/socketvar.h>
117 #include <sys/protosw.h>
118 #include <sys/errno.h>
119 #include <sys/kernel.h>
120 #include <sys/pool.h>
121 #if NRND > 0
122 #include <sys/md5.h>
123 #include <sys/rnd.h>
124 #endif
125
126 #include <net/route.h>
127 #include <net/if.h>
128
129 #include <netinet/in.h>
130 #include <netinet/in_systm.h>
131 #include <netinet/ip.h>
132 #include <netinet/in_pcb.h>
133 #include <netinet/ip_var.h>
134 #include <netinet/ip_icmp.h>
135
136 #ifdef INET6
137 #ifndef INET
138 #include <netinet/in.h>
139 #endif
140 #include <netinet/ip6.h>
141 #include <netinet6/in6_pcb.h>
142 #include <netinet6/ip6_var.h>
143 #include <netinet6/in6_var.h>
144 #include <netinet6/ip6protosw.h>
145 #include <netinet/icmp6.h>
146 #include <netinet6/nd6.h>
147 #endif
148
149 #include <netinet/tcp.h>
150 #include <netinet/tcp_fsm.h>
151 #include <netinet/tcp_seq.h>
152 #include <netinet/tcp_timer.h>
153 #include <netinet/tcp_var.h>
154 #include <netinet/tcp_congctl.h>
155 #include <netinet/tcpip.h>
156
157 #ifdef IPSEC
158 #include <netinet6/ipsec.h>
159 #include <netkey/key.h>
160 #endif /*IPSEC*/
161
162 #ifdef FAST_IPSEC
163 #include <netipsec/ipsec.h>
164 #include <netipsec/xform.h>
165 #ifdef INET6
166 #include <netipsec/ipsec6.h>
167 #endif
168 #include <netipsec/key.h>
169 #endif /* FAST_IPSEC*/
170
171
172 struct inpcbtable tcbtable; /* head of queue of active tcpcb's */
173 struct tcpstat tcpstat; /* tcp statistics */
174 u_int32_t tcp_now; /* for RFC 1323 timestamps */
175
176 /* patchable/settable parameters for tcp */
177 int tcp_mssdflt = TCP_MSS;
178 int tcp_rttdflt = TCPTV_SRTTDFLT / PR_SLOWHZ;
179 int tcp_do_rfc1323 = 1; /* window scaling / timestamps (obsolete) */
180 #if NRND > 0
181 int tcp_do_rfc1948 = 0; /* ISS by cryptographic hash */
182 #endif
183 int tcp_do_sack = 1; /* selective acknowledgement */
184 int tcp_do_win_scale = 1; /* RFC1323 window scaling */
185 int tcp_do_timestamps = 1; /* RFC1323 timestamps */
186 int tcp_ack_on_push = 0; /* set to enable immediate ACK-on-PUSH */
187 int tcp_do_ecn = 0; /* Explicit Congestion Notification */
188 #ifndef TCP_INIT_WIN
189 #define TCP_INIT_WIN 0 /* initial slow start window */
190 #endif
191 #ifndef TCP_INIT_WIN_LOCAL
192 #define TCP_INIT_WIN_LOCAL 4 /* initial slow start window for local nets */
193 #endif
194 int tcp_init_win = TCP_INIT_WIN;
195 int tcp_init_win_local = TCP_INIT_WIN_LOCAL;
196 int tcp_mss_ifmtu = 0;
197 #ifdef TCP_COMPAT_42
198 int tcp_compat_42 = 1;
199 #else
200 int tcp_compat_42 = 0;
201 #endif
202 int tcp_rst_ppslim = 100; /* 100pps */
203 int tcp_ackdrop_ppslim = 100; /* 100pps */
204 int tcp_do_loopback_cksum = 0;
205 int tcp_sack_tp_maxholes = 32;
206 int tcp_sack_globalmaxholes = 1024;
207 int tcp_sack_globalholes = 0;
208 int tcp_ecn_maxretries = 1;
209
210 /* tcb hash */
211 #ifndef TCBHASHSIZE
212 #define TCBHASHSIZE 128
213 #endif
214 int tcbhashsize = TCBHASHSIZE;
215
216 /* syn hash parameters */
217 #define TCP_SYN_HASH_SIZE 293
218 #define TCP_SYN_BUCKET_SIZE 35
219 int tcp_syn_cache_size = TCP_SYN_HASH_SIZE;
220 int tcp_syn_cache_limit = TCP_SYN_HASH_SIZE*TCP_SYN_BUCKET_SIZE;
221 int tcp_syn_bucket_limit = 3*TCP_SYN_BUCKET_SIZE;
222 struct syn_cache_head tcp_syn_cache[TCP_SYN_HASH_SIZE];
223
224 int tcp_freeq(struct tcpcb *);
225
226 #ifdef INET
227 void tcp_mtudisc_callback(struct in_addr);
228 #endif
229 #ifdef INET6
230 void tcp6_mtudisc_callback(struct in6_addr *);
231 #endif
232
233 #ifdef INET6
234 void tcp6_mtudisc(struct in6pcb *, int);
235 #endif
236
237 POOL_INIT(tcpcb_pool, sizeof(struct tcpcb), 0, 0, 0, "tcpcbpl", NULL);
238
239 #ifdef TCP_CSUM_COUNTERS
240 #include <sys/device.h>
241
242 #if defined(INET)
243 struct evcnt tcp_hwcsum_bad = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
244 NULL, "tcp", "hwcsum bad");
245 struct evcnt tcp_hwcsum_ok = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
246 NULL, "tcp", "hwcsum ok");
247 struct evcnt tcp_hwcsum_data = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
248 NULL, "tcp", "hwcsum data");
249 struct evcnt tcp_swcsum = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
250 NULL, "tcp", "swcsum");
251
252 EVCNT_ATTACH_STATIC(tcp_hwcsum_bad);
253 EVCNT_ATTACH_STATIC(tcp_hwcsum_ok);
254 EVCNT_ATTACH_STATIC(tcp_hwcsum_data);
255 EVCNT_ATTACH_STATIC(tcp_swcsum);
256 #endif /* defined(INET) */
257
258 #if defined(INET6)
259 struct evcnt tcp6_hwcsum_bad = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
260 NULL, "tcp6", "hwcsum bad");
261 struct evcnt tcp6_hwcsum_ok = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
262 NULL, "tcp6", "hwcsum ok");
263 struct evcnt tcp6_hwcsum_data = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
264 NULL, "tcp6", "hwcsum data");
265 struct evcnt tcp6_swcsum = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
266 NULL, "tcp6", "swcsum");
267
268 EVCNT_ATTACH_STATIC(tcp6_hwcsum_bad);
269 EVCNT_ATTACH_STATIC(tcp6_hwcsum_ok);
270 EVCNT_ATTACH_STATIC(tcp6_hwcsum_data);
271 EVCNT_ATTACH_STATIC(tcp6_swcsum);
272 #endif /* defined(INET6) */
273 #endif /* TCP_CSUM_COUNTERS */
274
275
276 #ifdef TCP_OUTPUT_COUNTERS
277 #include <sys/device.h>
278
279 struct evcnt tcp_output_bigheader = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
280 NULL, "tcp", "output big header");
281 struct evcnt tcp_output_predict_hit = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
282 NULL, "tcp", "output predict hit");
283 struct evcnt tcp_output_predict_miss = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
284 NULL, "tcp", "output predict miss");
285 struct evcnt tcp_output_copysmall = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
286 NULL, "tcp", "output copy small");
287 struct evcnt tcp_output_copybig = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
288 NULL, "tcp", "output copy big");
289 struct evcnt tcp_output_refbig = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
290 NULL, "tcp", "output reference big");
291
292 EVCNT_ATTACH_STATIC(tcp_output_bigheader);
293 EVCNT_ATTACH_STATIC(tcp_output_predict_hit);
294 EVCNT_ATTACH_STATIC(tcp_output_predict_miss);
295 EVCNT_ATTACH_STATIC(tcp_output_copysmall);
296 EVCNT_ATTACH_STATIC(tcp_output_copybig);
297 EVCNT_ATTACH_STATIC(tcp_output_refbig);
298
299 #endif /* TCP_OUTPUT_COUNTERS */
300
301 #ifdef TCP_REASS_COUNTERS
302 #include <sys/device.h>
303
304 struct evcnt tcp_reass_ = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
305 NULL, "tcp_reass", "calls");
306 struct evcnt tcp_reass_empty = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
307 &tcp_reass_, "tcp_reass", "insert into empty queue");
308 struct evcnt tcp_reass_iteration[8] = {
309 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, &tcp_reass_, "tcp_reass", ">7 iterations"),
310 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, &tcp_reass_, "tcp_reass", "1 iteration"),
311 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, &tcp_reass_, "tcp_reass", "2 iterations"),
312 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, &tcp_reass_, "tcp_reass", "3 iterations"),
313 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, &tcp_reass_, "tcp_reass", "4 iterations"),
314 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, &tcp_reass_, "tcp_reass", "5 iterations"),
315 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, &tcp_reass_, "tcp_reass", "6 iterations"),
316 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, &tcp_reass_, "tcp_reass", "7 iterations"),
317 };
318 struct evcnt tcp_reass_prependfirst = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
319 &tcp_reass_, "tcp_reass", "prepend to first");
320 struct evcnt tcp_reass_prepend = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
321 &tcp_reass_, "tcp_reass", "prepend");
322 struct evcnt tcp_reass_insert = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
323 &tcp_reass_, "tcp_reass", "insert");
324 struct evcnt tcp_reass_inserttail = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
325 &tcp_reass_, "tcp_reass", "insert at tail");
326 struct evcnt tcp_reass_append = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
327 &tcp_reass_, "tcp_reass", "append");
328 struct evcnt tcp_reass_appendtail = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
329 &tcp_reass_, "tcp_reass", "append to tail fragment");
330 struct evcnt tcp_reass_overlaptail = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
331 &tcp_reass_, "tcp_reass", "overlap at end");
332 struct evcnt tcp_reass_overlapfront = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
333 &tcp_reass_, "tcp_reass", "overlap at start");
334 struct evcnt tcp_reass_segdup = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
335 &tcp_reass_, "tcp_reass", "duplicate segment");
336 struct evcnt tcp_reass_fragdup = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
337 &tcp_reass_, "tcp_reass", "duplicate fragment");
338
339 EVCNT_ATTACH_STATIC(tcp_reass_);
340 EVCNT_ATTACH_STATIC(tcp_reass_empty);
341 EVCNT_ATTACH_STATIC2(tcp_reass_iteration, 0);
342 EVCNT_ATTACH_STATIC2(tcp_reass_iteration, 1);
343 EVCNT_ATTACH_STATIC2(tcp_reass_iteration, 2);
344 EVCNT_ATTACH_STATIC2(tcp_reass_iteration, 3);
345 EVCNT_ATTACH_STATIC2(tcp_reass_iteration, 4);
346 EVCNT_ATTACH_STATIC2(tcp_reass_iteration, 5);
347 EVCNT_ATTACH_STATIC2(tcp_reass_iteration, 6);
348 EVCNT_ATTACH_STATIC2(tcp_reass_iteration, 7);
349 EVCNT_ATTACH_STATIC(tcp_reass_prependfirst);
350 EVCNT_ATTACH_STATIC(tcp_reass_prepend);
351 EVCNT_ATTACH_STATIC(tcp_reass_insert);
352 EVCNT_ATTACH_STATIC(tcp_reass_inserttail);
353 EVCNT_ATTACH_STATIC(tcp_reass_append);
354 EVCNT_ATTACH_STATIC(tcp_reass_appendtail);
355 EVCNT_ATTACH_STATIC(tcp_reass_overlaptail);
356 EVCNT_ATTACH_STATIC(tcp_reass_overlapfront);
357 EVCNT_ATTACH_STATIC(tcp_reass_segdup);
358 EVCNT_ATTACH_STATIC(tcp_reass_fragdup);
359
360 #endif /* TCP_REASS_COUNTERS */
361
362 #ifdef MBUFTRACE
363 struct mowner tcp_mowner = MOWNER_INIT("tcp", "");
364 struct mowner tcp_rx_mowner = MOWNER_INIT("tcp", "rx");
365 struct mowner tcp_tx_mowner = MOWNER_INIT("tcp", "tx");
366 #endif
367
368 /*
369 * Tcp initialization
370 */
371 void
372 tcp_init(void)
373 {
374 int hlen;
375
376 /* Initialize the TCPCB template. */
377 tcp_tcpcb_template();
378
379 in_pcbinit(&tcbtable, tcbhashsize, tcbhashsize);
380
381 hlen = sizeof(struct ip) + sizeof(struct tcphdr);
382 #ifdef INET6
383 if (sizeof(struct ip) < sizeof(struct ip6_hdr))
384 hlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
385 #endif
386 if (max_protohdr < hlen)
387 max_protohdr = hlen;
388 if (max_linkhdr + hlen > MHLEN)
389 panic("tcp_init");
390
391 #ifdef INET
392 icmp_mtudisc_callback_register(tcp_mtudisc_callback);
393 #endif
394 #ifdef INET6
395 icmp6_mtudisc_callback_register(tcp6_mtudisc_callback);
396 #endif
397
398 /* Initialize timer state. */
399 tcp_timer_init();
400
401 /* Initialize the compressed state engine. */
402 syn_cache_init();
403
404 /* Initialize the congestion control algorithms. */
405 tcp_congctl_init();
406
407 MOWNER_ATTACH(&tcp_tx_mowner);
408 MOWNER_ATTACH(&tcp_rx_mowner);
409 MOWNER_ATTACH(&tcp_mowner);
410 }
411
412 /*
413 * Create template to be used to send tcp packets on a connection.
414 * Call after host entry created, allocates an mbuf and fills
415 * in a skeletal tcp/ip header, minimizing the amount of work
416 * necessary when the connection is used.
417 */
418 struct mbuf *
419 tcp_template(struct tcpcb *tp)
420 {
421 struct inpcb *inp = tp->t_inpcb;
422 #ifdef INET6
423 struct in6pcb *in6p = tp->t_in6pcb;
424 #endif
425 struct tcphdr *n;
426 struct mbuf *m;
427 int hlen;
428
429 switch (tp->t_family) {
430 case AF_INET:
431 hlen = sizeof(struct ip);
432 if (inp)
433 break;
434 #ifdef INET6
435 if (in6p) {
436 /* mapped addr case */
437 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)
438 && IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr))
439 break;
440 }
441 #endif
442 return NULL; /*EINVAL*/
443 #ifdef INET6
444 case AF_INET6:
445 hlen = sizeof(struct ip6_hdr);
446 if (in6p) {
447 /* more sainty check? */
448 break;
449 }
450 return NULL; /*EINVAL*/
451 #endif
452 default:
453 hlen = 0; /*pacify gcc*/
454 return NULL; /*EAFNOSUPPORT*/
455 }
456 #ifdef DIAGNOSTIC
457 if (hlen + sizeof(struct tcphdr) > MCLBYTES)
458 panic("mclbytes too small for t_template");
459 #endif
460 m = tp->t_template;
461 if (m && m->m_len == hlen + sizeof(struct tcphdr))
462 ;
463 else {
464 if (m)
465 m_freem(m);
466 m = tp->t_template = NULL;
467 MGETHDR(m, M_DONTWAIT, MT_HEADER);
468 if (m && hlen + sizeof(struct tcphdr) > MHLEN) {
469 MCLGET(m, M_DONTWAIT);
470 if ((m->m_flags & M_EXT) == 0) {
471 m_free(m);
472 m = NULL;
473 }
474 }
475 if (m == NULL)
476 return NULL;
477 MCLAIM(m, &tcp_mowner);
478 m->m_pkthdr.len = m->m_len = hlen + sizeof(struct tcphdr);
479 }
480
481 bzero(mtod(m, caddr_t), m->m_len);
482
483 n = (struct tcphdr *)(mtod(m, caddr_t) + hlen);
484
485 switch (tp->t_family) {
486 case AF_INET:
487 {
488 struct ipovly *ipov;
489 mtod(m, struct ip *)->ip_v = 4;
490 mtod(m, struct ip *)->ip_hl = hlen >> 2;
491 ipov = mtod(m, struct ipovly *);
492 ipov->ih_pr = IPPROTO_TCP;
493 ipov->ih_len = htons(sizeof(struct tcphdr));
494 if (inp) {
495 ipov->ih_src = inp->inp_laddr;
496 ipov->ih_dst = inp->inp_faddr;
497 }
498 #ifdef INET6
499 else if (in6p) {
500 /* mapped addr case */
501 bcopy(&in6p->in6p_laddr.s6_addr32[3], &ipov->ih_src,
502 sizeof(ipov->ih_src));
503 bcopy(&in6p->in6p_faddr.s6_addr32[3], &ipov->ih_dst,
504 sizeof(ipov->ih_dst));
505 }
506 #endif
507 /*
508 * Compute the pseudo-header portion of the checksum
509 * now. We incrementally add in the TCP option and
510 * payload lengths later, and then compute the TCP
511 * checksum right before the packet is sent off onto
512 * the wire.
513 */
514 n->th_sum = in_cksum_phdr(ipov->ih_src.s_addr,
515 ipov->ih_dst.s_addr,
516 htons(sizeof(struct tcphdr) + IPPROTO_TCP));
517 break;
518 }
519 #ifdef INET6
520 case AF_INET6:
521 {
522 struct ip6_hdr *ip6;
523 mtod(m, struct ip *)->ip_v = 6;
524 ip6 = mtod(m, struct ip6_hdr *);
525 ip6->ip6_nxt = IPPROTO_TCP;
526 ip6->ip6_plen = htons(sizeof(struct tcphdr));
527 ip6->ip6_src = in6p->in6p_laddr;
528 ip6->ip6_dst = in6p->in6p_faddr;
529 ip6->ip6_flow = in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK;
530 if (ip6_auto_flowlabel) {
531 ip6->ip6_flow &= ~IPV6_FLOWLABEL_MASK;
532 ip6->ip6_flow |=
533 (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
534 }
535 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
536 ip6->ip6_vfc |= IPV6_VERSION;
537
538 /*
539 * Compute the pseudo-header portion of the checksum
540 * now. We incrementally add in the TCP option and
541 * payload lengths later, and then compute the TCP
542 * checksum right before the packet is sent off onto
543 * the wire.
544 */
545 n->th_sum = in6_cksum_phdr(&in6p->in6p_laddr,
546 &in6p->in6p_faddr, htonl(sizeof(struct tcphdr)),
547 htonl(IPPROTO_TCP));
548 break;
549 }
550 #endif
551 }
552 if (inp) {
553 n->th_sport = inp->inp_lport;
554 n->th_dport = inp->inp_fport;
555 }
556 #ifdef INET6
557 else if (in6p) {
558 n->th_sport = in6p->in6p_lport;
559 n->th_dport = in6p->in6p_fport;
560 }
561 #endif
562 n->th_seq = 0;
563 n->th_ack = 0;
564 n->th_x2 = 0;
565 n->th_off = 5;
566 n->th_flags = 0;
567 n->th_win = 0;
568 n->th_urp = 0;
569 return (m);
570 }
571
572 /*
573 * Send a single message to the TCP at address specified by
574 * the given TCP/IP header. If m == 0, then we make a copy
575 * of the tcpiphdr at ti and send directly to the addressed host.
576 * This is used to force keep alive messages out using the TCP
577 * template for a connection tp->t_template. If flags are given
578 * then we send a message back to the TCP which originated the
579 * segment ti, and discard the mbuf containing it and any other
580 * attached mbufs.
581 *
582 * In any case the ack and sequence number of the transmitted
583 * segment are as specified by the parameters.
584 */
585 int
586 tcp_respond(struct tcpcb *tp, struct mbuf *template, struct mbuf *m,
587 struct tcphdr *th0, tcp_seq ack, tcp_seq seq, int flags)
588 {
589 struct route *ro;
590 int error, tlen, win = 0;
591 int hlen;
592 struct ip *ip;
593 #ifdef INET6
594 struct ip6_hdr *ip6;
595 #endif
596 int family; /* family on packet, not inpcb/in6pcb! */
597 struct tcphdr *th;
598 struct socket *so;
599
600 if (tp != NULL && (flags & TH_RST) == 0) {
601 #ifdef DIAGNOSTIC
602 if (tp->t_inpcb && tp->t_in6pcb)
603 panic("tcp_respond: both t_inpcb and t_in6pcb are set");
604 #endif
605 #ifdef INET
606 if (tp->t_inpcb)
607 win = sbspace(&tp->t_inpcb->inp_socket->so_rcv);
608 #endif
609 #ifdef INET6
610 if (tp->t_in6pcb)
611 win = sbspace(&tp->t_in6pcb->in6p_socket->so_rcv);
612 #endif
613 }
614
615 th = NULL; /* Quell uninitialized warning */
616 ip = NULL;
617 #ifdef INET6
618 ip6 = NULL;
619 #endif
620 if (m == 0) {
621 if (!template)
622 return EINVAL;
623
624 /* get family information from template */
625 switch (mtod(template, struct ip *)->ip_v) {
626 case 4:
627 family = AF_INET;
628 hlen = sizeof(struct ip);
629 break;
630 #ifdef INET6
631 case 6:
632 family = AF_INET6;
633 hlen = sizeof(struct ip6_hdr);
634 break;
635 #endif
636 default:
637 return EAFNOSUPPORT;
638 }
639
640 MGETHDR(m, M_DONTWAIT, MT_HEADER);
641 if (m) {
642 MCLAIM(m, &tcp_tx_mowner);
643 MCLGET(m, M_DONTWAIT);
644 if ((m->m_flags & M_EXT) == 0) {
645 m_free(m);
646 m = NULL;
647 }
648 }
649 if (m == NULL)
650 return (ENOBUFS);
651
652 if (tcp_compat_42)
653 tlen = 1;
654 else
655 tlen = 0;
656
657 m->m_data += max_linkhdr;
658 bcopy(mtod(template, caddr_t), mtod(m, caddr_t),
659 template->m_len);
660 switch (family) {
661 case AF_INET:
662 ip = mtod(m, struct ip *);
663 th = (struct tcphdr *)(ip + 1);
664 break;
665 #ifdef INET6
666 case AF_INET6:
667 ip6 = mtod(m, struct ip6_hdr *);
668 th = (struct tcphdr *)(ip6 + 1);
669 break;
670 #endif
671 #if 0
672 default:
673 /* noone will visit here */
674 m_freem(m);
675 return EAFNOSUPPORT;
676 #endif
677 }
678 flags = TH_ACK;
679 } else {
680
681 if ((m->m_flags & M_PKTHDR) == 0) {
682 #if 0
683 printf("non PKTHDR to tcp_respond\n");
684 #endif
685 m_freem(m);
686 return EINVAL;
687 }
688 #ifdef DIAGNOSTIC
689 if (!th0)
690 panic("th0 == NULL in tcp_respond");
691 #endif
692
693 /* get family information from m */
694 switch (mtod(m, struct ip *)->ip_v) {
695 case 4:
696 family = AF_INET;
697 hlen = sizeof(struct ip);
698 ip = mtod(m, struct ip *);
699 break;
700 #ifdef INET6
701 case 6:
702 family = AF_INET6;
703 hlen = sizeof(struct ip6_hdr);
704 ip6 = mtod(m, struct ip6_hdr *);
705 break;
706 #endif
707 default:
708 m_freem(m);
709 return EAFNOSUPPORT;
710 }
711 /* clear h/w csum flags inherited from rx packet */
712 m->m_pkthdr.csum_flags = 0;
713
714 if ((flags & TH_SYN) == 0 || sizeof(*th0) > (th0->th_off << 2))
715 tlen = sizeof(*th0);
716 else
717 tlen = th0->th_off << 2;
718
719 if (m->m_len > hlen + tlen && (m->m_flags & M_EXT) == 0 &&
720 mtod(m, caddr_t) + hlen == (caddr_t)th0) {
721 m->m_len = hlen + tlen;
722 m_freem(m->m_next);
723 m->m_next = NULL;
724 } else {
725 struct mbuf *n;
726
727 #ifdef DIAGNOSTIC
728 if (max_linkhdr + hlen + tlen > MCLBYTES) {
729 m_freem(m);
730 return EMSGSIZE;
731 }
732 #endif
733 MGETHDR(n, M_DONTWAIT, MT_HEADER);
734 if (n && max_linkhdr + hlen + tlen > MHLEN) {
735 MCLGET(n, M_DONTWAIT);
736 if ((n->m_flags & M_EXT) == 0) {
737 m_freem(n);
738 n = NULL;
739 }
740 }
741 if (!n) {
742 m_freem(m);
743 return ENOBUFS;
744 }
745
746 MCLAIM(n, &tcp_tx_mowner);
747 n->m_data += max_linkhdr;
748 n->m_len = hlen + tlen;
749 m_copyback(n, 0, hlen, mtod(m, caddr_t));
750 m_copyback(n, hlen, tlen, (caddr_t)th0);
751
752 m_freem(m);
753 m = n;
754 n = NULL;
755 }
756
757 #define xchg(a,b,type) { type t; t=a; a=b; b=t; }
758 switch (family) {
759 case AF_INET:
760 ip = mtod(m, struct ip *);
761 th = (struct tcphdr *)(ip + 1);
762 ip->ip_p = IPPROTO_TCP;
763 xchg(ip->ip_dst, ip->ip_src, struct in_addr);
764 ip->ip_p = IPPROTO_TCP;
765 break;
766 #ifdef INET6
767 case AF_INET6:
768 ip6 = mtod(m, struct ip6_hdr *);
769 th = (struct tcphdr *)(ip6 + 1);
770 ip6->ip6_nxt = IPPROTO_TCP;
771 xchg(ip6->ip6_dst, ip6->ip6_src, struct in6_addr);
772 ip6->ip6_nxt = IPPROTO_TCP;
773 break;
774 #endif
775 #if 0
776 default:
777 /* noone will visit here */
778 m_freem(m);
779 return EAFNOSUPPORT;
780 #endif
781 }
782 xchg(th->th_dport, th->th_sport, u_int16_t);
783 #undef xchg
784 tlen = 0; /*be friendly with the following code*/
785 }
786 th->th_seq = htonl(seq);
787 th->th_ack = htonl(ack);
788 th->th_x2 = 0;
789 if ((flags & TH_SYN) == 0) {
790 if (tp)
791 win >>= tp->rcv_scale;
792 if (win > TCP_MAXWIN)
793 win = TCP_MAXWIN;
794 th->th_win = htons((u_int16_t)win);
795 th->th_off = sizeof (struct tcphdr) >> 2;
796 tlen += sizeof(*th);
797 } else
798 tlen += th->th_off << 2;
799 m->m_len = hlen + tlen;
800 m->m_pkthdr.len = hlen + tlen;
801 m->m_pkthdr.rcvif = (struct ifnet *) 0;
802 th->th_flags = flags;
803 th->th_urp = 0;
804
805 switch (family) {
806 #ifdef INET
807 case AF_INET:
808 {
809 struct ipovly *ipov = (struct ipovly *)ip;
810 bzero(ipov->ih_x1, sizeof ipov->ih_x1);
811 ipov->ih_len = htons((u_int16_t)tlen);
812
813 th->th_sum = 0;
814 th->th_sum = in_cksum(m, hlen + tlen);
815 ip->ip_len = htons(hlen + tlen);
816 ip->ip_ttl = ip_defttl;
817 break;
818 }
819 #endif
820 #ifdef INET6
821 case AF_INET6:
822 {
823 th->th_sum = 0;
824 th->th_sum = in6_cksum(m, IPPROTO_TCP, sizeof(struct ip6_hdr),
825 tlen);
826 ip6->ip6_plen = htons(tlen);
827 if (tp && tp->t_in6pcb) {
828 struct ifnet *oifp;
829 ro = (struct route *)&tp->t_in6pcb->in6p_route;
830 oifp = ro->ro_rt ? ro->ro_rt->rt_ifp : NULL;
831 ip6->ip6_hlim = in6_selecthlim(tp->t_in6pcb, oifp);
832 } else
833 ip6->ip6_hlim = ip6_defhlim;
834 ip6->ip6_flow &= ~IPV6_FLOWINFO_MASK;
835 if (ip6_auto_flowlabel) {
836 ip6->ip6_flow |=
837 (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
838 }
839 break;
840 }
841 #endif
842 }
843
844 if (tp && tp->t_inpcb)
845 so = tp->t_inpcb->inp_socket;
846 #ifdef INET6
847 else if (tp && tp->t_in6pcb)
848 so = tp->t_in6pcb->in6p_socket;
849 #endif
850 else
851 so = NULL;
852
853 if (tp != NULL && tp->t_inpcb != NULL) {
854 ro = &tp->t_inpcb->inp_route;
855 #ifdef DIAGNOSTIC
856 if (family != AF_INET)
857 panic("tcp_respond: address family mismatch");
858 if (!in_hosteq(ip->ip_dst, tp->t_inpcb->inp_faddr)) {
859 panic("tcp_respond: ip_dst %x != inp_faddr %x",
860 ntohl(ip->ip_dst.s_addr),
861 ntohl(tp->t_inpcb->inp_faddr.s_addr));
862 }
863 #endif
864 }
865 #ifdef INET6
866 else if (tp != NULL && tp->t_in6pcb != NULL) {
867 ro = (struct route *)&tp->t_in6pcb->in6p_route;
868 #ifdef DIAGNOSTIC
869 if (family == AF_INET) {
870 if (!IN6_IS_ADDR_V4MAPPED(&tp->t_in6pcb->in6p_faddr))
871 panic("tcp_respond: not mapped addr");
872 if (bcmp(&ip->ip_dst,
873 &tp->t_in6pcb->in6p_faddr.s6_addr32[3],
874 sizeof(ip->ip_dst)) != 0) {
875 panic("tcp_respond: ip_dst != in6p_faddr");
876 }
877 } else if (family == AF_INET6) {
878 if (!IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
879 &tp->t_in6pcb->in6p_faddr))
880 panic("tcp_respond: ip6_dst != in6p_faddr");
881 } else
882 panic("tcp_respond: address family mismatch");
883 #endif
884 }
885 #endif
886 else
887 ro = NULL;
888
889 switch (family) {
890 #ifdef INET
891 case AF_INET:
892 error = ip_output(m, NULL, ro,
893 (tp && tp->t_mtudisc ? IP_MTUDISC : 0),
894 (struct ip_moptions *)0, so);
895 break;
896 #endif
897 #ifdef INET6
898 case AF_INET6:
899 error = ip6_output(m, NULL, (struct route_in6 *)ro, 0,
900 (struct ip6_moptions *)0, so, NULL);
901 break;
902 #endif
903 default:
904 error = EAFNOSUPPORT;
905 break;
906 }
907
908 return (error);
909 }
910
911 /*
912 * Template TCPCB. Rather than zeroing a new TCPCB and initializing
913 * a bunch of members individually, we maintain this template for the
914 * static and mostly-static components of the TCPCB, and copy it into
915 * the new TCPCB instead.
916 */
917 static struct tcpcb tcpcb_template = {
918 /*
919 * If TCP_NTIMERS ever changes, we'll need to update this
920 * initializer.
921 */
922 .t_timer = {
923 CALLOUT_INITIALIZER,
924 CALLOUT_INITIALIZER,
925 CALLOUT_INITIALIZER,
926 CALLOUT_INITIALIZER,
927 },
928 .t_delack_ch = CALLOUT_INITIALIZER,
929
930 .t_srtt = TCPTV_SRTTBASE,
931 .t_rttmin = TCPTV_MIN,
932
933 .snd_cwnd = TCP_MAXWIN << TCP_MAX_WINSHIFT,
934 .snd_ssthresh = TCP_MAXWIN << TCP_MAX_WINSHIFT,
935 .snd_numholes = 0,
936
937 .t_partialacks = -1,
938 };
939
940 /*
941 * Updates the TCPCB template whenever a parameter that would affect
942 * the template is changed.
943 */
944 void
945 tcp_tcpcb_template(void)
946 {
947 struct tcpcb *tp = &tcpcb_template;
948 int flags;
949
950 tp->t_peermss = tcp_mssdflt;
951 tp->t_ourmss = tcp_mssdflt;
952 tp->t_segsz = tcp_mssdflt;
953
954 flags = 0;
955 if (tcp_do_rfc1323 && tcp_do_win_scale)
956 flags |= TF_REQ_SCALE;
957 if (tcp_do_rfc1323 && tcp_do_timestamps)
958 flags |= TF_REQ_TSTMP;
959 tp->t_flags = flags;
960
961 /*
962 * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no
963 * rtt estimate. Set rttvar so that srtt + 2 * rttvar gives
964 * reasonable initial retransmit time.
965 */
966 tp->t_rttvar = tcp_rttdflt * PR_SLOWHZ << (TCP_RTTVAR_SHIFT + 2 - 1);
967 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
968 TCPTV_MIN, TCPTV_REXMTMAX);
969 }
970
971 /*
972 * Create a new TCP control block, making an
973 * empty reassembly queue and hooking it to the argument
974 * protocol control block.
975 */
976 /* family selects inpcb, or in6pcb */
977 struct tcpcb *
978 tcp_newtcpcb(int family, void *aux)
979 {
980 struct tcpcb *tp;
981 int i;
982
983 /* XXX Consider using a pool_cache for speed. */
984 tp = pool_get(&tcpcb_pool, PR_NOWAIT); /* splsoftnet via tcp_usrreq */
985 if (tp == NULL)
986 return (NULL);
987 memcpy(tp, &tcpcb_template, sizeof(*tp));
988 TAILQ_INIT(&tp->segq);
989 TAILQ_INIT(&tp->timeq);
990 tp->t_family = family; /* may be overridden later on */
991 TAILQ_INIT(&tp->snd_holes);
992 LIST_INIT(&tp->t_sc); /* XXX can template this */
993
994 /* Don't sweat this loop; hopefully the compiler will unroll it. */
995 for (i = 0; i < TCPT_NTIMERS; i++)
996 TCP_TIMER_INIT(tp, i);
997
998 switch (family) {
999 case AF_INET:
1000 {
1001 struct inpcb *inp = (struct inpcb *)aux;
1002
1003 inp->inp_ip.ip_ttl = ip_defttl;
1004 inp->inp_ppcb = (caddr_t)tp;
1005
1006 tp->t_inpcb = inp;
1007 tp->t_mtudisc = ip_mtudisc;
1008 break;
1009 }
1010 #ifdef INET6
1011 case AF_INET6:
1012 {
1013 struct in6pcb *in6p = (struct in6pcb *)aux;
1014
1015 in6p->in6p_ip6.ip6_hlim = in6_selecthlim(in6p,
1016 in6p->in6p_route.ro_rt ? in6p->in6p_route.ro_rt->rt_ifp
1017 : NULL);
1018 in6p->in6p_ppcb = (caddr_t)tp;
1019
1020 tp->t_in6pcb = in6p;
1021 /* for IPv6, always try to run path MTU discovery */
1022 tp->t_mtudisc = 1;
1023 break;
1024 }
1025 #endif /* INET6 */
1026 default:
1027 pool_put(&tcpcb_pool, tp); /* splsoftnet via tcp_usrreq */
1028 return (NULL);
1029 }
1030
1031 /*
1032 * Initialize our timebase. When we send timestamps, we take
1033 * the delta from tcp_now -- this means each connection always
1034 * gets a timebase of 0, which makes it, among other things,
1035 * more difficult to determine how long a system has been up,
1036 * and thus how many TCP sequence increments have occurred.
1037 */
1038 tp->ts_timebase = tcp_now;
1039
1040 tp->t_congctl = tcp_congctl_global;
1041 tp->t_congctl->refcnt++;
1042
1043 return (tp);
1044 }
1045
1046 /*
1047 * Drop a TCP connection, reporting
1048 * the specified error. If connection is synchronized,
1049 * then send a RST to peer.
1050 */
1051 struct tcpcb *
1052 tcp_drop(struct tcpcb *tp, int errno)
1053 {
1054 struct socket *so = NULL;
1055
1056 #ifdef DIAGNOSTIC
1057 if (tp->t_inpcb && tp->t_in6pcb)
1058 panic("tcp_drop: both t_inpcb and t_in6pcb are set");
1059 #endif
1060 #ifdef INET
1061 if (tp->t_inpcb)
1062 so = tp->t_inpcb->inp_socket;
1063 #endif
1064 #ifdef INET6
1065 if (tp->t_in6pcb)
1066 so = tp->t_in6pcb->in6p_socket;
1067 #endif
1068 if (!so)
1069 return NULL;
1070
1071 if (TCPS_HAVERCVDSYN(tp->t_state)) {
1072 tp->t_state = TCPS_CLOSED;
1073 (void) tcp_output(tp);
1074 tcpstat.tcps_drops++;
1075 } else
1076 tcpstat.tcps_conndrops++;
1077 if (errno == ETIMEDOUT && tp->t_softerror)
1078 errno = tp->t_softerror;
1079 so->so_error = errno;
1080 return (tcp_close(tp));
1081 }
1082
1083 /*
1084 * Return whether this tcpcb is marked as dead, indicating
1085 * to the calling timer function that no further action should
1086 * be taken, as we are about to release this tcpcb. The release
1087 * of the storage will be done if this is the last timer running.
1088 *
1089 * This should be called from the callout handler function after
1090 * callout_ack() is done, so that the number of invoking timer
1091 * functions is 0.
1092 */
1093 int
1094 tcp_isdead(struct tcpcb *tp)
1095 {
1096 int dead = (tp->t_flags & TF_DEAD);
1097
1098 if (__predict_false(dead)) {
1099 if (tcp_timers_invoking(tp) > 0)
1100 /* not quite there yet -- count separately? */
1101 return dead;
1102 tcpstat.tcps_delayed_free++;
1103 pool_put(&tcpcb_pool, tp); /* splsoftnet via tcp_timer.c */
1104 }
1105 return dead;
1106 }
1107
1108 /*
1109 * Close a TCP control block:
1110 * discard all space held by the tcp
1111 * discard internet protocol block
1112 * wake up any sleepers
1113 */
1114 struct tcpcb *
1115 tcp_close(struct tcpcb *tp)
1116 {
1117 struct inpcb *inp;
1118 #ifdef INET6
1119 struct in6pcb *in6p;
1120 #endif
1121 struct socket *so;
1122 #ifdef RTV_RTT
1123 struct rtentry *rt;
1124 #endif
1125 struct route *ro;
1126
1127 inp = tp->t_inpcb;
1128 #ifdef INET6
1129 in6p = tp->t_in6pcb;
1130 #endif
1131 so = NULL;
1132 ro = NULL;
1133 if (inp) {
1134 so = inp->inp_socket;
1135 ro = &inp->inp_route;
1136 }
1137 #ifdef INET6
1138 else if (in6p) {
1139 so = in6p->in6p_socket;
1140 ro = (struct route *)&in6p->in6p_route;
1141 }
1142 #endif
1143
1144 #ifdef RTV_RTT
1145 /*
1146 * If we sent enough data to get some meaningful characteristics,
1147 * save them in the routing entry. 'Enough' is arbitrarily
1148 * defined as the sendpipesize (default 4K) * 16. This would
1149 * give us 16 rtt samples assuming we only get one sample per
1150 * window (the usual case on a long haul net). 16 samples is
1151 * enough for the srtt filter to converge to within 5% of the correct
1152 * value; fewer samples and we could save a very bogus rtt.
1153 *
1154 * Don't update the default route's characteristics and don't
1155 * update anything that the user "locked".
1156 */
1157 if (SEQ_LT(tp->iss + so->so_snd.sb_hiwat * 16, tp->snd_max) &&
1158 ro && (rt = ro->ro_rt) &&
1159 !in_nullhost(satosin(rt_key(rt))->sin_addr)) {
1160 u_long i = 0;
1161
1162 if ((rt->rt_rmx.rmx_locks & RTV_RTT) == 0) {
1163 i = tp->t_srtt *
1164 ((RTM_RTTUNIT / PR_SLOWHZ) >> (TCP_RTT_SHIFT + 2));
1165 if (rt->rt_rmx.rmx_rtt && i)
1166 /*
1167 * filter this update to half the old & half
1168 * the new values, converting scale.
1169 * See route.h and tcp_var.h for a
1170 * description of the scaling constants.
1171 */
1172 rt->rt_rmx.rmx_rtt =
1173 (rt->rt_rmx.rmx_rtt + i) / 2;
1174 else
1175 rt->rt_rmx.rmx_rtt = i;
1176 }
1177 if ((rt->rt_rmx.rmx_locks & RTV_RTTVAR) == 0) {
1178 i = tp->t_rttvar *
1179 ((RTM_RTTUNIT / PR_SLOWHZ) >> (TCP_RTTVAR_SHIFT + 2));
1180 if (rt->rt_rmx.rmx_rttvar && i)
1181 rt->rt_rmx.rmx_rttvar =
1182 (rt->rt_rmx.rmx_rttvar + i) / 2;
1183 else
1184 rt->rt_rmx.rmx_rttvar = i;
1185 }
1186 /*
1187 * update the pipelimit (ssthresh) if it has been updated
1188 * already or if a pipesize was specified & the threshhold
1189 * got below half the pipesize. I.e., wait for bad news
1190 * before we start updating, then update on both good
1191 * and bad news.
1192 */
1193 if (((rt->rt_rmx.rmx_locks & RTV_SSTHRESH) == 0 &&
1194 (i = tp->snd_ssthresh) && rt->rt_rmx.rmx_ssthresh) ||
1195 i < (rt->rt_rmx.rmx_sendpipe / 2)) {
1196 /*
1197 * convert the limit from user data bytes to
1198 * packets then to packet data bytes.
1199 */
1200 i = (i + tp->t_segsz / 2) / tp->t_segsz;
1201 if (i < 2)
1202 i = 2;
1203 i *= (u_long)(tp->t_segsz + sizeof (struct tcpiphdr));
1204 if (rt->rt_rmx.rmx_ssthresh)
1205 rt->rt_rmx.rmx_ssthresh =
1206 (rt->rt_rmx.rmx_ssthresh + i) / 2;
1207 else
1208 rt->rt_rmx.rmx_ssthresh = i;
1209 }
1210 }
1211 #endif /* RTV_RTT */
1212 /* free the reassembly queue, if any */
1213 TCP_REASS_LOCK(tp);
1214 (void) tcp_freeq(tp);
1215 TCP_REASS_UNLOCK(tp);
1216
1217 /* free the SACK holes list. */
1218 tcp_free_sackholes(tp);
1219
1220 tp->t_congctl->refcnt--;
1221
1222 tcp_canceltimers(tp);
1223 TCP_CLEAR_DELACK(tp);
1224 syn_cache_cleanup(tp);
1225
1226 if (tp->t_template) {
1227 m_free(tp->t_template);
1228 tp->t_template = NULL;
1229 }
1230 if (tcp_timers_invoking(tp))
1231 tp->t_flags |= TF_DEAD;
1232 else
1233 pool_put(&tcpcb_pool, tp);
1234
1235 if (inp) {
1236 inp->inp_ppcb = 0;
1237 soisdisconnected(so);
1238 in_pcbdetach(inp);
1239 }
1240 #ifdef INET6
1241 else if (in6p) {
1242 in6p->in6p_ppcb = 0;
1243 soisdisconnected(so);
1244 in6_pcbdetach(in6p);
1245 }
1246 #endif
1247 tcpstat.tcps_closed++;
1248 return ((struct tcpcb *)0);
1249 }
1250
1251 int
1252 tcp_freeq(tp)
1253 struct tcpcb *tp;
1254 {
1255 struct ipqent *qe;
1256 int rv = 0;
1257 #ifdef TCPREASS_DEBUG
1258 int i = 0;
1259 #endif
1260
1261 TCP_REASS_LOCK_CHECK(tp);
1262
1263 while ((qe = TAILQ_FIRST(&tp->segq)) != NULL) {
1264 #ifdef TCPREASS_DEBUG
1265 printf("tcp_freeq[%p,%d]: %u:%u(%u) 0x%02x\n",
1266 tp, i++, qe->ipqe_seq, qe->ipqe_seq + qe->ipqe_len,
1267 qe->ipqe_len, qe->ipqe_flags & (TH_SYN|TH_FIN|TH_RST));
1268 #endif
1269 TAILQ_REMOVE(&tp->segq, qe, ipqe_q);
1270 TAILQ_REMOVE(&tp->timeq, qe, ipqe_timeq);
1271 m_freem(qe->ipqe_m);
1272 tcpipqent_free(qe);
1273 rv = 1;
1274 }
1275 tp->t_segqlen = 0;
1276 KASSERT(TAILQ_EMPTY(&tp->timeq));
1277 return (rv);
1278 }
1279
1280 /*
1281 * Protocol drain routine. Called when memory is in short supply.
1282 */
1283 void
1284 tcp_drain(void)
1285 {
1286 struct inpcb_hdr *inph;
1287 struct tcpcb *tp;
1288
1289 /*
1290 * Free the sequence queue of all TCP connections.
1291 */
1292 CIRCLEQ_FOREACH(inph, &tcbtable.inpt_queue, inph_queue) {
1293 switch (inph->inph_af) {
1294 case AF_INET:
1295 tp = intotcpcb((struct inpcb *)inph);
1296 break;
1297 #ifdef INET6
1298 case AF_INET6:
1299 tp = in6totcpcb((struct in6pcb *)inph);
1300 break;
1301 #endif
1302 default:
1303 tp = NULL;
1304 break;
1305 }
1306 if (tp != NULL) {
1307 /*
1308 * We may be called from a device's interrupt
1309 * context. If the tcpcb is already busy,
1310 * just bail out now.
1311 */
1312 if (tcp_reass_lock_try(tp) == 0)
1313 continue;
1314 if (tcp_freeq(tp))
1315 tcpstat.tcps_connsdrained++;
1316 TCP_REASS_UNLOCK(tp);
1317 }
1318 }
1319 }
1320
1321 /*
1322 * Notify a tcp user of an asynchronous error;
1323 * store error as soft error, but wake up user
1324 * (for now, won't do anything until can select for soft error).
1325 */
1326 void
1327 tcp_notify(struct inpcb *inp, int error)
1328 {
1329 struct tcpcb *tp = (struct tcpcb *)inp->inp_ppcb;
1330 struct socket *so = inp->inp_socket;
1331
1332 /*
1333 * Ignore some errors if we are hooked up.
1334 * If connection hasn't completed, has retransmitted several times,
1335 * and receives a second error, give up now. This is better
1336 * than waiting a long time to establish a connection that
1337 * can never complete.
1338 */
1339 if (tp->t_state == TCPS_ESTABLISHED &&
1340 (error == EHOSTUNREACH || error == ENETUNREACH ||
1341 error == EHOSTDOWN)) {
1342 return;
1343 } else if (TCPS_HAVEESTABLISHED(tp->t_state) == 0 &&
1344 tp->t_rxtshift > 3 && tp->t_softerror)
1345 so->so_error = error;
1346 else
1347 tp->t_softerror = error;
1348 wakeup((caddr_t) &so->so_timeo);
1349 sorwakeup(so);
1350 sowwakeup(so);
1351 }
1352
1353 #ifdef INET6
1354 void
1355 tcp6_notify(struct in6pcb *in6p, int error)
1356 {
1357 struct tcpcb *tp = (struct tcpcb *)in6p->in6p_ppcb;
1358 struct socket *so = in6p->in6p_socket;
1359
1360 /*
1361 * Ignore some errors if we are hooked up.
1362 * If connection hasn't completed, has retransmitted several times,
1363 * and receives a second error, give up now. This is better
1364 * than waiting a long time to establish a connection that
1365 * can never complete.
1366 */
1367 if (tp->t_state == TCPS_ESTABLISHED &&
1368 (error == EHOSTUNREACH || error == ENETUNREACH ||
1369 error == EHOSTDOWN)) {
1370 return;
1371 } else if (TCPS_HAVEESTABLISHED(tp->t_state) == 0 &&
1372 tp->t_rxtshift > 3 && tp->t_softerror)
1373 so->so_error = error;
1374 else
1375 tp->t_softerror = error;
1376 wakeup((caddr_t) &so->so_timeo);
1377 sorwakeup(so);
1378 sowwakeup(so);
1379 }
1380 #endif
1381
1382 #ifdef INET6
1383 void
1384 tcp6_ctlinput(int cmd, struct sockaddr *sa, void *d)
1385 {
1386 struct tcphdr th;
1387 void (*notify)(struct in6pcb *, int) = tcp6_notify;
1388 int nmatch;
1389 struct ip6_hdr *ip6;
1390 const struct sockaddr_in6 *sa6_src = NULL;
1391 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)sa;
1392 struct mbuf *m;
1393 int off;
1394
1395 if (sa->sa_family != AF_INET6 ||
1396 sa->sa_len != sizeof(struct sockaddr_in6))
1397 return;
1398 if ((unsigned)cmd >= PRC_NCMDS)
1399 return;
1400 else if (cmd == PRC_QUENCH) {
1401 /*
1402 * Don't honor ICMP Source Quench messages meant for
1403 * TCP connections.
1404 */
1405 return;
1406 } else if (PRC_IS_REDIRECT(cmd))
1407 notify = in6_rtchange, d = NULL;
1408 else if (cmd == PRC_MSGSIZE)
1409 ; /* special code is present, see below */
1410 else if (cmd == PRC_HOSTDEAD)
1411 d = NULL;
1412 else if (inet6ctlerrmap[cmd] == 0)
1413 return;
1414
1415 /* if the parameter is from icmp6, decode it. */
1416 if (d != NULL) {
1417 struct ip6ctlparam *ip6cp = (struct ip6ctlparam *)d;
1418 m = ip6cp->ip6c_m;
1419 ip6 = ip6cp->ip6c_ip6;
1420 off = ip6cp->ip6c_off;
1421 sa6_src = ip6cp->ip6c_src;
1422 } else {
1423 m = NULL;
1424 ip6 = NULL;
1425 sa6_src = &sa6_any;
1426 off = 0;
1427 }
1428
1429 if (ip6) {
1430 /*
1431 * XXX: We assume that when ip6 is non NULL,
1432 * M and OFF are valid.
1433 */
1434
1435 /* check if we can safely examine src and dst ports */
1436 if (m->m_pkthdr.len < off + sizeof(th)) {
1437 if (cmd == PRC_MSGSIZE)
1438 icmp6_mtudisc_update((struct ip6ctlparam *)d, 0);
1439 return;
1440 }
1441
1442 bzero(&th, sizeof(th));
1443 m_copydata(m, off, sizeof(th), (caddr_t)&th);
1444
1445 if (cmd == PRC_MSGSIZE) {
1446 int valid = 0;
1447
1448 /*
1449 * Check to see if we have a valid TCP connection
1450 * corresponding to the address in the ICMPv6 message
1451 * payload.
1452 */
1453 if (in6_pcblookup_connect(&tcbtable, &sa6->sin6_addr,
1454 th.th_dport, (const struct in6_addr *)&sa6_src->sin6_addr,
1455 th.th_sport, 0))
1456 valid++;
1457
1458 /*
1459 * Depending on the value of "valid" and routing table
1460 * size (mtudisc_{hi,lo}wat), we will:
1461 * - recalcurate the new MTU and create the
1462 * corresponding routing entry, or
1463 * - ignore the MTU change notification.
1464 */
1465 icmp6_mtudisc_update((struct ip6ctlparam *)d, valid);
1466
1467 /*
1468 * no need to call in6_pcbnotify, it should have been
1469 * called via callback if necessary
1470 */
1471 return;
1472 }
1473
1474 nmatch = in6_pcbnotify(&tcbtable, sa, th.th_dport,
1475 (const struct sockaddr *)sa6_src, th.th_sport, cmd, NULL, notify);
1476 if (nmatch == 0 && syn_cache_count &&
1477 (inet6ctlerrmap[cmd] == EHOSTUNREACH ||
1478 inet6ctlerrmap[cmd] == ENETUNREACH ||
1479 inet6ctlerrmap[cmd] == EHOSTDOWN))
1480 syn_cache_unreach((const struct sockaddr *)sa6_src,
1481 sa, &th);
1482 } else {
1483 (void) in6_pcbnotify(&tcbtable, sa, 0,
1484 (const struct sockaddr *)sa6_src, 0, cmd, NULL, notify);
1485 }
1486 }
1487 #endif
1488
1489 #ifdef INET
1490 /* assumes that ip header and tcp header are contiguous on mbuf */
1491 void *
1492 tcp_ctlinput(int cmd, struct sockaddr *sa, void *v)
1493 {
1494 struct ip *ip = v;
1495 struct tcphdr *th;
1496 struct icmp *icp;
1497 extern const int inetctlerrmap[];
1498 void (*notify)(struct inpcb *, int) = tcp_notify;
1499 int errno;
1500 int nmatch;
1501 struct tcpcb *tp;
1502 u_int mtu;
1503 tcp_seq seq;
1504 struct inpcb *inp;
1505 #ifdef INET6
1506 struct in6pcb *in6p;
1507 struct in6_addr src6, dst6;
1508 #endif
1509
1510 if (sa->sa_family != AF_INET ||
1511 sa->sa_len != sizeof(struct sockaddr_in))
1512 return NULL;
1513 if ((unsigned)cmd >= PRC_NCMDS)
1514 return NULL;
1515 errno = inetctlerrmap[cmd];
1516 if (cmd == PRC_QUENCH)
1517 /*
1518 * Don't honor ICMP Source Quench messages meant for
1519 * TCP connections.
1520 */
1521 return NULL;
1522 else if (PRC_IS_REDIRECT(cmd))
1523 notify = in_rtchange, ip = 0;
1524 else if (cmd == PRC_MSGSIZE && ip && ip->ip_v == 4) {
1525 /*
1526 * Check to see if we have a valid TCP connection
1527 * corresponding to the address in the ICMP message
1528 * payload.
1529 *
1530 * Boundary check is made in icmp_input(), with ICMP_ADVLENMIN.
1531 */
1532 th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
1533 #ifdef INET6
1534 memset(&src6, 0, sizeof(src6));
1535 memset(&dst6, 0, sizeof(dst6));
1536 src6.s6_addr16[5] = dst6.s6_addr16[5] = 0xffff;
1537 memcpy(&src6.s6_addr32[3], &ip->ip_src, sizeof(struct in_addr));
1538 memcpy(&dst6.s6_addr32[3], &ip->ip_dst, sizeof(struct in_addr));
1539 #endif
1540 if ((inp = in_pcblookup_connect(&tcbtable, ip->ip_dst,
1541 th->th_dport, ip->ip_src, th->th_sport)) != NULL)
1542 #ifdef INET6
1543 in6p = NULL;
1544 #else
1545 ;
1546 #endif
1547 #ifdef INET6
1548 else if ((in6p = in6_pcblookup_connect(&tcbtable, &dst6,
1549 th->th_dport, &src6, th->th_sport, 0)) != NULL)
1550 ;
1551 #endif
1552 else
1553 return NULL;
1554
1555 /*
1556 * Now that we've validated that we are actually communicating
1557 * with the host indicated in the ICMP message, locate the
1558 * ICMP header, recalculate the new MTU, and create the
1559 * corresponding routing entry.
1560 */
1561 icp = (struct icmp *)((caddr_t)ip -
1562 offsetof(struct icmp, icmp_ip));
1563 if (inp) {
1564 if ((tp = intotcpcb(inp)) == NULL)
1565 return NULL;
1566 }
1567 #ifdef INET6
1568 else if (in6p) {
1569 if ((tp = in6totcpcb(in6p)) == NULL)
1570 return NULL;
1571 }
1572 #endif
1573 else
1574 return NULL;
1575 seq = ntohl(th->th_seq);
1576 if (SEQ_LT(seq, tp->snd_una) || SEQ_GT(seq, tp->snd_max))
1577 return NULL;
1578 /*
1579 * If the ICMP message advertises a Next-Hop MTU
1580 * equal or larger than the maximum packet size we have
1581 * ever sent, drop the message.
1582 */
1583 mtu = (u_int)ntohs(icp->icmp_nextmtu);
1584 if (mtu >= tp->t_pmtud_mtu_sent)
1585 return NULL;
1586 if (mtu >= tcp_hdrsz(tp) + tp->t_pmtud_mss_acked) {
1587 /*
1588 * Calculate new MTU, and create corresponding
1589 * route (traditional PMTUD).
1590 */
1591 tp->t_flags &= ~TF_PMTUD_PEND;
1592 icmp_mtudisc(icp, ip->ip_dst);
1593 } else {
1594 /*
1595 * Record the information got in the ICMP
1596 * message; act on it later.
1597 * If we had already recorded an ICMP message,
1598 * replace the old one only if the new message
1599 * refers to an older TCP segment
1600 */
1601 if (tp->t_flags & TF_PMTUD_PEND) {
1602 if (SEQ_LT(tp->t_pmtud_th_seq, seq))
1603 return NULL;
1604 } else
1605 tp->t_flags |= TF_PMTUD_PEND;
1606 tp->t_pmtud_th_seq = seq;
1607 tp->t_pmtud_nextmtu = icp->icmp_nextmtu;
1608 tp->t_pmtud_ip_len = icp->icmp_ip.ip_len;
1609 tp->t_pmtud_ip_hl = icp->icmp_ip.ip_hl;
1610 }
1611 return NULL;
1612 } else if (cmd == PRC_HOSTDEAD)
1613 ip = 0;
1614 else if (errno == 0)
1615 return NULL;
1616 if (ip && ip->ip_v == 4 && sa->sa_family == AF_INET) {
1617 th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
1618 nmatch = in_pcbnotify(&tcbtable, satosin(sa)->sin_addr,
1619 th->th_dport, ip->ip_src, th->th_sport, errno, notify);
1620 if (nmatch == 0 && syn_cache_count &&
1621 (inetctlerrmap[cmd] == EHOSTUNREACH ||
1622 inetctlerrmap[cmd] == ENETUNREACH ||
1623 inetctlerrmap[cmd] == EHOSTDOWN)) {
1624 struct sockaddr_in sin;
1625 bzero(&sin, sizeof(sin));
1626 sin.sin_len = sizeof(sin);
1627 sin.sin_family = AF_INET;
1628 sin.sin_port = th->th_sport;
1629 sin.sin_addr = ip->ip_src;
1630 syn_cache_unreach((struct sockaddr *)&sin, sa, th);
1631 }
1632
1633 /* XXX mapped address case */
1634 } else
1635 in_pcbnotifyall(&tcbtable, satosin(sa)->sin_addr, errno,
1636 notify);
1637 return NULL;
1638 }
1639
1640 /*
1641 * When a source quench is received, we are being notified of congestion.
1642 * Close the congestion window down to the Loss Window (one segment).
1643 * We will gradually open it again as we proceed.
1644 */
1645 void
1646 tcp_quench(struct inpcb *inp, int errno __unused)
1647 {
1648 struct tcpcb *tp = intotcpcb(inp);
1649
1650 if (tp)
1651 tp->snd_cwnd = tp->t_segsz;
1652 }
1653 #endif
1654
1655 #ifdef INET6
1656 void
1657 tcp6_quench(struct in6pcb *in6p, int errno __unused)
1658 {
1659 struct tcpcb *tp = in6totcpcb(in6p);
1660
1661 if (tp)
1662 tp->snd_cwnd = tp->t_segsz;
1663 }
1664 #endif
1665
1666 #ifdef INET
1667 /*
1668 * Path MTU Discovery handlers.
1669 */
1670 void
1671 tcp_mtudisc_callback(struct in_addr faddr)
1672 {
1673 #ifdef INET6
1674 struct in6_addr in6;
1675 #endif
1676
1677 in_pcbnotifyall(&tcbtable, faddr, EMSGSIZE, tcp_mtudisc);
1678 #ifdef INET6
1679 memset(&in6, 0, sizeof(in6));
1680 in6.s6_addr16[5] = 0xffff;
1681 memcpy(&in6.s6_addr32[3], &faddr, sizeof(struct in_addr));
1682 tcp6_mtudisc_callback(&in6);
1683 #endif
1684 }
1685
1686 /*
1687 * On receipt of path MTU corrections, flush old route and replace it
1688 * with the new one. Retransmit all unacknowledged packets, to ensure
1689 * that all packets will be received.
1690 */
1691 void
1692 tcp_mtudisc(struct inpcb *inp, int errno)
1693 {
1694 struct tcpcb *tp = intotcpcb(inp);
1695 struct rtentry *rt = in_pcbrtentry(inp);
1696
1697 if (tp != 0) {
1698 if (rt != 0) {
1699 /*
1700 * If this was not a host route, remove and realloc.
1701 */
1702 if ((rt->rt_flags & RTF_HOST) == 0) {
1703 in_rtchange(inp, errno);
1704 if ((rt = in_pcbrtentry(inp)) == 0)
1705 return;
1706 }
1707
1708 /*
1709 * Slow start out of the error condition. We
1710 * use the MTU because we know it's smaller
1711 * than the previously transmitted segment.
1712 *
1713 * Note: This is more conservative than the
1714 * suggestion in draft-floyd-incr-init-win-03.
1715 */
1716 if (rt->rt_rmx.rmx_mtu != 0)
1717 tp->snd_cwnd =
1718 TCP_INITIAL_WINDOW(tcp_init_win,
1719 rt->rt_rmx.rmx_mtu);
1720 }
1721
1722 /*
1723 * Resend unacknowledged packets.
1724 */
1725 tp->snd_nxt = tp->snd_una;
1726 tcp_output(tp);
1727 }
1728 }
1729 #endif
1730
1731 #ifdef INET6
1732 /*
1733 * Path MTU Discovery handlers.
1734 */
1735 void
1736 tcp6_mtudisc_callback(struct in6_addr *faddr)
1737 {
1738 struct sockaddr_in6 sin6;
1739
1740 bzero(&sin6, sizeof(sin6));
1741 sin6.sin6_family = AF_INET6;
1742 sin6.sin6_len = sizeof(struct sockaddr_in6);
1743 sin6.sin6_addr = *faddr;
1744 (void) in6_pcbnotify(&tcbtable, (struct sockaddr *)&sin6, 0,
1745 (const struct sockaddr *)&sa6_any, 0, PRC_MSGSIZE, NULL, tcp6_mtudisc);
1746 }
1747
1748 void
1749 tcp6_mtudisc(struct in6pcb *in6p, int errno)
1750 {
1751 struct tcpcb *tp = in6totcpcb(in6p);
1752 struct rtentry *rt = in6_pcbrtentry(in6p);
1753
1754 if (tp != 0) {
1755 if (rt != 0) {
1756 /*
1757 * If this was not a host route, remove and realloc.
1758 */
1759 if ((rt->rt_flags & RTF_HOST) == 0) {
1760 in6_rtchange(in6p, errno);
1761 if ((rt = in6_pcbrtentry(in6p)) == 0)
1762 return;
1763 }
1764
1765 /*
1766 * Slow start out of the error condition. We
1767 * use the MTU because we know it's smaller
1768 * than the previously transmitted segment.
1769 *
1770 * Note: This is more conservative than the
1771 * suggestion in draft-floyd-incr-init-win-03.
1772 */
1773 if (rt->rt_rmx.rmx_mtu != 0)
1774 tp->snd_cwnd =
1775 TCP_INITIAL_WINDOW(tcp_init_win,
1776 rt->rt_rmx.rmx_mtu);
1777 }
1778
1779 /*
1780 * Resend unacknowledged packets.
1781 */
1782 tp->snd_nxt = tp->snd_una;
1783 tcp_output(tp);
1784 }
1785 }
1786 #endif /* INET6 */
1787
1788 /*
1789 * Compute the MSS to advertise to the peer. Called only during
1790 * the 3-way handshake. If we are the server (peer initiated
1791 * connection), we are called with a pointer to the interface
1792 * on which the SYN packet arrived. If we are the client (we
1793 * initiated connection), we are called with a pointer to the
1794 * interface out which this connection should go.
1795 *
1796 * NOTE: Do not subtract IP option/extension header size nor IPsec
1797 * header size from MSS advertisement. MSS option must hold the maximum
1798 * segment size we can accept, so it must always be:
1799 * max(if mtu) - ip header - tcp header
1800 */
1801 u_long
1802 tcp_mss_to_advertise(const struct ifnet *ifp, int af)
1803 {
1804 extern u_long in_maxmtu;
1805 u_long mss = 0;
1806 u_long hdrsiz;
1807
1808 /*
1809 * In order to avoid defeating path MTU discovery on the peer,
1810 * we advertise the max MTU of all attached networks as our MSS,
1811 * per RFC 1191, section 3.1.
1812 *
1813 * We provide the option to advertise just the MTU of
1814 * the interface on which we hope this connection will
1815 * be receiving. If we are responding to a SYN, we
1816 * will have a pretty good idea about this, but when
1817 * initiating a connection there is a bit more doubt.
1818 *
1819 * We also need to ensure that loopback has a large enough
1820 * MSS, as the loopback MTU is never included in in_maxmtu.
1821 */
1822
1823 if (ifp != NULL)
1824 switch (af) {
1825 case AF_INET:
1826 mss = ifp->if_mtu;
1827 break;
1828 #ifdef INET6
1829 case AF_INET6:
1830 mss = IN6_LINKMTU(ifp);
1831 break;
1832 #endif
1833 }
1834
1835 if (tcp_mss_ifmtu == 0)
1836 switch (af) {
1837 case AF_INET:
1838 mss = max(in_maxmtu, mss);
1839 break;
1840 #ifdef INET6
1841 case AF_INET6:
1842 mss = max(in6_maxmtu, mss);
1843 break;
1844 #endif
1845 }
1846
1847 switch (af) {
1848 case AF_INET:
1849 hdrsiz = sizeof(struct ip);
1850 break;
1851 #ifdef INET6
1852 case AF_INET6:
1853 hdrsiz = sizeof(struct ip6_hdr);
1854 break;
1855 #endif
1856 default:
1857 hdrsiz = 0;
1858 break;
1859 }
1860 hdrsiz += sizeof(struct tcphdr);
1861 if (mss > hdrsiz)
1862 mss -= hdrsiz;
1863
1864 mss = max(tcp_mssdflt, mss);
1865 return (mss);
1866 }
1867
1868 /*
1869 * Set connection variables based on the peer's advertised MSS.
1870 * We are passed the TCPCB for the actual connection. If we
1871 * are the server, we are called by the compressed state engine
1872 * when the 3-way handshake is complete. If we are the client,
1873 * we are called when we receive the SYN,ACK from the server.
1874 *
1875 * NOTE: Our advertised MSS value must be initialized in the TCPCB
1876 * before this routine is called!
1877 */
1878 void
1879 tcp_mss_from_peer(struct tcpcb *tp, int offer)
1880 {
1881 struct socket *so;
1882 #if defined(RTV_SPIPE) || defined(RTV_SSTHRESH)
1883 struct rtentry *rt;
1884 #endif
1885 u_long bufsize;
1886 int mss;
1887
1888 #ifdef DIAGNOSTIC
1889 if (tp->t_inpcb && tp->t_in6pcb)
1890 panic("tcp_mss_from_peer: both t_inpcb and t_in6pcb are set");
1891 #endif
1892 so = NULL;
1893 rt = NULL;
1894 #ifdef INET
1895 if (tp->t_inpcb) {
1896 so = tp->t_inpcb->inp_socket;
1897 #if defined(RTV_SPIPE) || defined(RTV_SSTHRESH)
1898 rt = in_pcbrtentry(tp->t_inpcb);
1899 #endif
1900 }
1901 #endif
1902 #ifdef INET6
1903 if (tp->t_in6pcb) {
1904 so = tp->t_in6pcb->in6p_socket;
1905 #if defined(RTV_SPIPE) || defined(RTV_SSTHRESH)
1906 rt = in6_pcbrtentry(tp->t_in6pcb);
1907 #endif
1908 }
1909 #endif
1910
1911 /*
1912 * As per RFC1122, use the default MSS value, unless they
1913 * sent us an offer. Do not accept offers less than 256 bytes.
1914 */
1915 mss = tcp_mssdflt;
1916 if (offer)
1917 mss = offer;
1918 mss = max(mss, 256); /* sanity */
1919 tp->t_peermss = mss;
1920 mss -= tcp_optlen(tp);
1921 #ifdef INET
1922 if (tp->t_inpcb)
1923 mss -= ip_optlen(tp->t_inpcb);
1924 #endif
1925 #ifdef INET6
1926 if (tp->t_in6pcb)
1927 mss -= ip6_optlen(tp->t_in6pcb);
1928 #endif
1929
1930 /*
1931 * If there's a pipesize, change the socket buffer to that size.
1932 * Make the socket buffer an integral number of MSS units. If
1933 * the MSS is larger than the socket buffer, artificially decrease
1934 * the MSS.
1935 */
1936 #ifdef RTV_SPIPE
1937 if (rt != NULL && rt->rt_rmx.rmx_sendpipe != 0)
1938 bufsize = rt->rt_rmx.rmx_sendpipe;
1939 else
1940 #endif
1941 {
1942 KASSERT(so != NULL);
1943 bufsize = so->so_snd.sb_hiwat;
1944 }
1945 if (bufsize < mss)
1946 mss = bufsize;
1947 else {
1948 bufsize = roundup(bufsize, mss);
1949 if (bufsize > sb_max)
1950 bufsize = sb_max;
1951 (void) sbreserve(&so->so_snd, bufsize, so);
1952 }
1953 tp->t_segsz = mss;
1954
1955 #ifdef RTV_SSTHRESH
1956 if (rt != NULL && rt->rt_rmx.rmx_ssthresh) {
1957 /*
1958 * There's some sort of gateway or interface buffer
1959 * limit on the path. Use this to set the slow
1960 * start threshold, but set the threshold to no less
1961 * than 2 * MSS.
1962 */
1963 tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh);
1964 }
1965 #endif
1966 }
1967
1968 /*
1969 * Processing necessary when a TCP connection is established.
1970 */
1971 void
1972 tcp_established(struct tcpcb *tp)
1973 {
1974 struct socket *so;
1975 #ifdef RTV_RPIPE
1976 struct rtentry *rt;
1977 #endif
1978 u_long bufsize;
1979
1980 #ifdef DIAGNOSTIC
1981 if (tp->t_inpcb && tp->t_in6pcb)
1982 panic("tcp_established: both t_inpcb and t_in6pcb are set");
1983 #endif
1984 so = NULL;
1985 rt = NULL;
1986 #ifdef INET
1987 if (tp->t_inpcb) {
1988 so = tp->t_inpcb->inp_socket;
1989 #if defined(RTV_RPIPE)
1990 rt = in_pcbrtentry(tp->t_inpcb);
1991 #endif
1992 }
1993 #endif
1994 #ifdef INET6
1995 if (tp->t_in6pcb) {
1996 so = tp->t_in6pcb->in6p_socket;
1997 #if defined(RTV_RPIPE)
1998 rt = in6_pcbrtentry(tp->t_in6pcb);
1999 #endif
2000 }
2001 #endif
2002
2003 tp->t_state = TCPS_ESTABLISHED;
2004 TCP_TIMER_ARM(tp, TCPT_KEEP, tcp_keepidle);
2005
2006 #ifdef RTV_RPIPE
2007 if (rt != NULL && rt->rt_rmx.rmx_recvpipe != 0)
2008 bufsize = rt->rt_rmx.rmx_recvpipe;
2009 else
2010 #endif
2011 {
2012 KASSERT(so != NULL);
2013 bufsize = so->so_rcv.sb_hiwat;
2014 }
2015 if (bufsize > tp->t_ourmss) {
2016 bufsize = roundup(bufsize, tp->t_ourmss);
2017 if (bufsize > sb_max)
2018 bufsize = sb_max;
2019 (void) sbreserve(&so->so_rcv, bufsize, so);
2020 }
2021 }
2022
2023 /*
2024 * Check if there's an initial rtt or rttvar. Convert from the
2025 * route-table units to scaled multiples of the slow timeout timer.
2026 * Called only during the 3-way handshake.
2027 */
2028 void
2029 tcp_rmx_rtt(struct tcpcb *tp)
2030 {
2031 #ifdef RTV_RTT
2032 struct rtentry *rt = NULL;
2033 int rtt;
2034
2035 #ifdef DIAGNOSTIC
2036 if (tp->t_inpcb && tp->t_in6pcb)
2037 panic("tcp_rmx_rtt: both t_inpcb and t_in6pcb are set");
2038 #endif
2039 #ifdef INET
2040 if (tp->t_inpcb)
2041 rt = in_pcbrtentry(tp->t_inpcb);
2042 #endif
2043 #ifdef INET6
2044 if (tp->t_in6pcb)
2045 rt = in6_pcbrtentry(tp->t_in6pcb);
2046 #endif
2047 if (rt == NULL)
2048 return;
2049
2050 if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) {
2051 /*
2052 * XXX The lock bit for MTU indicates that the value
2053 * is also a minimum value; this is subject to time.
2054 */
2055 if (rt->rt_rmx.rmx_locks & RTV_RTT)
2056 TCPT_RANGESET(tp->t_rttmin,
2057 rtt / (RTM_RTTUNIT / PR_SLOWHZ),
2058 TCPTV_MIN, TCPTV_REXMTMAX);
2059 tp->t_srtt = rtt /
2060 ((RTM_RTTUNIT / PR_SLOWHZ) >> (TCP_RTT_SHIFT + 2));
2061 if (rt->rt_rmx.rmx_rttvar) {
2062 tp->t_rttvar = rt->rt_rmx.rmx_rttvar /
2063 ((RTM_RTTUNIT / PR_SLOWHZ) >>
2064 (TCP_RTTVAR_SHIFT + 2));
2065 } else {
2066 /* Default variation is +- 1 rtt */
2067 tp->t_rttvar =
2068 tp->t_srtt >> (TCP_RTT_SHIFT - TCP_RTTVAR_SHIFT);
2069 }
2070 TCPT_RANGESET(tp->t_rxtcur,
2071 ((tp->t_srtt >> 2) + tp->t_rttvar) >> (1 + 2),
2072 tp->t_rttmin, TCPTV_REXMTMAX);
2073 }
2074 #endif
2075 }
2076
2077 tcp_seq tcp_iss_seq = 0; /* tcp initial seq # */
2078 #if NRND > 0
2079 u_int8_t tcp_iss_secret[16]; /* 128 bits; should be plenty */
2080 #endif
2081
2082 /*
2083 * Get a new sequence value given a tcp control block
2084 */
2085 tcp_seq
2086 tcp_new_iss(struct tcpcb *tp, tcp_seq addin)
2087 {
2088
2089 #ifdef INET
2090 if (tp->t_inpcb != NULL) {
2091 return (tcp_new_iss1(&tp->t_inpcb->inp_laddr,
2092 &tp->t_inpcb->inp_faddr, tp->t_inpcb->inp_lport,
2093 tp->t_inpcb->inp_fport, sizeof(tp->t_inpcb->inp_laddr),
2094 addin));
2095 }
2096 #endif
2097 #ifdef INET6
2098 if (tp->t_in6pcb != NULL) {
2099 return (tcp_new_iss1(&tp->t_in6pcb->in6p_laddr,
2100 &tp->t_in6pcb->in6p_faddr, tp->t_in6pcb->in6p_lport,
2101 tp->t_in6pcb->in6p_fport, sizeof(tp->t_in6pcb->in6p_laddr),
2102 addin));
2103 }
2104 #endif
2105 /* Not possible. */
2106 panic("tcp_new_iss");
2107 }
2108
2109 /*
2110 * This routine actually generates a new TCP initial sequence number.
2111 */
2112 tcp_seq
2113 tcp_new_iss1(void *laddr, void *faddr, u_int16_t lport, u_int16_t fport,
2114 size_t addrsz, tcp_seq addin)
2115 {
2116 tcp_seq tcp_iss;
2117
2118 #if NRND > 0
2119 static int beenhere;
2120
2121 /*
2122 * If we haven't been here before, initialize our cryptographic
2123 * hash secret.
2124 */
2125 if (beenhere == 0) {
2126 rnd_extract_data(tcp_iss_secret, sizeof(tcp_iss_secret),
2127 RND_EXTRACT_ANY);
2128 beenhere = 1;
2129 }
2130
2131 if (tcp_do_rfc1948) {
2132 MD5_CTX ctx;
2133 u_int8_t hash[16]; /* XXX MD5 knowledge */
2134
2135 /*
2136 * Compute the base value of the ISS. It is a hash
2137 * of (saddr, sport, daddr, dport, secret).
2138 */
2139 MD5Init(&ctx);
2140
2141 MD5Update(&ctx, (u_char *) laddr, addrsz);
2142 MD5Update(&ctx, (u_char *) &lport, sizeof(lport));
2143
2144 MD5Update(&ctx, (u_char *) faddr, addrsz);
2145 MD5Update(&ctx, (u_char *) &fport, sizeof(fport));
2146
2147 MD5Update(&ctx, tcp_iss_secret, sizeof(tcp_iss_secret));
2148
2149 MD5Final(hash, &ctx);
2150
2151 memcpy(&tcp_iss, hash, sizeof(tcp_iss));
2152
2153 /*
2154 * Now increment our "timer", and add it in to
2155 * the computed value.
2156 *
2157 * XXX Use `addin'?
2158 * XXX TCP_ISSINCR too large to use?
2159 */
2160 tcp_iss_seq += TCP_ISSINCR;
2161 #ifdef TCPISS_DEBUG
2162 printf("ISS hash 0x%08x, ", tcp_iss);
2163 #endif
2164 tcp_iss += tcp_iss_seq + addin;
2165 #ifdef TCPISS_DEBUG
2166 printf("new ISS 0x%08x\n", tcp_iss);
2167 #endif
2168 } else
2169 #endif /* NRND > 0 */
2170 {
2171 /*
2172 * Randomize.
2173 */
2174 #if NRND > 0
2175 rnd_extract_data(&tcp_iss, sizeof(tcp_iss), RND_EXTRACT_ANY);
2176 #else
2177 tcp_iss = arc4random();
2178 #endif
2179
2180 /*
2181 * If we were asked to add some amount to a known value,
2182 * we will take a random value obtained above, mask off
2183 * the upper bits, and add in the known value. We also
2184 * add in a constant to ensure that we are at least a
2185 * certain distance from the original value.
2186 *
2187 * This is used when an old connection is in timed wait
2188 * and we have a new one coming in, for instance.
2189 */
2190 if (addin != 0) {
2191 #ifdef TCPISS_DEBUG
2192 printf("Random %08x, ", tcp_iss);
2193 #endif
2194 tcp_iss &= TCP_ISS_RANDOM_MASK;
2195 tcp_iss += addin + TCP_ISSINCR;
2196 #ifdef TCPISS_DEBUG
2197 printf("Old ISS %08x, ISS %08x\n", addin, tcp_iss);
2198 #endif
2199 } else {
2200 tcp_iss &= TCP_ISS_RANDOM_MASK;
2201 tcp_iss += tcp_iss_seq;
2202 tcp_iss_seq += TCP_ISSINCR;
2203 #ifdef TCPISS_DEBUG
2204 printf("ISS %08x\n", tcp_iss);
2205 #endif
2206 }
2207 }
2208
2209 if (tcp_compat_42) {
2210 /*
2211 * Limit it to the positive range for really old TCP
2212 * implementations.
2213 * Just AND off the top bit instead of checking if
2214 * is set first - saves a branch 50% of the time.
2215 */
2216 tcp_iss &= 0x7fffffff; /* XXX */
2217 }
2218
2219 return (tcp_iss);
2220 }
2221
2222 #if defined(IPSEC) || defined(FAST_IPSEC)
2223 /* compute ESP/AH header size for TCP, including outer IP header. */
2224 size_t
2225 ipsec4_hdrsiz_tcp(struct tcpcb *tp)
2226 {
2227 struct inpcb *inp;
2228 size_t hdrsiz;
2229
2230 /* XXX mapped addr case (tp->t_in6pcb) */
2231 if (!tp || !tp->t_template || !(inp = tp->t_inpcb))
2232 return 0;
2233 switch (tp->t_family) {
2234 case AF_INET:
2235 /* XXX: should use currect direction. */
2236 hdrsiz = ipsec4_hdrsiz(tp->t_template, IPSEC_DIR_OUTBOUND, inp);
2237 break;
2238 default:
2239 hdrsiz = 0;
2240 break;
2241 }
2242
2243 return hdrsiz;
2244 }
2245
2246 #ifdef INET6
2247 size_t
2248 ipsec6_hdrsiz_tcp(struct tcpcb *tp)
2249 {
2250 struct in6pcb *in6p;
2251 size_t hdrsiz;
2252
2253 if (!tp || !tp->t_template || !(in6p = tp->t_in6pcb))
2254 return 0;
2255 switch (tp->t_family) {
2256 case AF_INET6:
2257 /* XXX: should use currect direction. */
2258 hdrsiz = ipsec6_hdrsiz(tp->t_template, IPSEC_DIR_OUTBOUND, in6p);
2259 break;
2260 case AF_INET:
2261 /* mapped address case - tricky */
2262 default:
2263 hdrsiz = 0;
2264 break;
2265 }
2266
2267 return hdrsiz;
2268 }
2269 #endif
2270 #endif /*IPSEC*/
2271
2272 /*
2273 * Determine the length of the TCP options for this connection.
2274 *
2275 * XXX: What do we do for SACK, when we add that? Just reserve
2276 * all of the space? Otherwise we can't exactly be incrementing
2277 * cwnd by an amount that varies depending on the amount we last
2278 * had to SACK!
2279 */
2280
2281 u_int
2282 tcp_optlen(struct tcpcb *tp)
2283 {
2284 u_int optlen;
2285
2286 optlen = 0;
2287 if ((tp->t_flags & (TF_REQ_TSTMP|TF_RCVD_TSTMP|TF_NOOPT)) ==
2288 (TF_REQ_TSTMP | TF_RCVD_TSTMP))
2289 optlen += TCPOLEN_TSTAMP_APPA;
2290
2291 #ifdef TCP_SIGNATURE
2292 #if defined(INET6) && defined(FAST_IPSEC)
2293 if (tp->t_family == AF_INET)
2294 #endif
2295 if (tp->t_flags & TF_SIGNATURE)
2296 optlen += TCPOLEN_SIGNATURE + 2;
2297 #endif /* TCP_SIGNATURE */
2298
2299 return optlen;
2300 }
2301
2302 u_int
2303 tcp_hdrsz(struct tcpcb *tp)
2304 {
2305 u_int hlen;
2306
2307 switch (tp->t_family) {
2308 #ifdef INET6
2309 case AF_INET6:
2310 hlen = sizeof(struct ip6_hdr);
2311 break;
2312 #endif
2313 case AF_INET:
2314 hlen = sizeof(struct ip);
2315 break;
2316 default:
2317 hlen = 0;
2318 break;
2319 }
2320 hlen += sizeof(struct tcphdr);
2321
2322 if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
2323 (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP)
2324 hlen += TCPOLEN_TSTAMP_APPA;
2325 #ifdef TCP_SIGNATURE
2326 if (tp->t_flags & TF_SIGNATURE)
2327 hlen += TCPOLEN_SIGLEN;
2328 #endif
2329 return hlen;
2330 }
2331