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