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