tcp_subr.c revision 1.213 1 /* $NetBSD: tcp_subr.c,v 1.213 2007/03/12 18:18:36 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.213 2007/03/12 18:18:36 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 /* Initialize the TCPCB template. */
383 tcp_tcpcb_template();
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 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, (struct route_in6 *)ro, 0,
910 (struct ip6_moptions *)0, so, NULL);
911 break;
912 #endif
913 default:
914 error = EAFNOSUPPORT;
915 break;
916 }
917
918 return (error);
919 }
920
921 /*
922 * Template TCPCB. Rather than zeroing a new TCPCB and initializing
923 * a bunch of members individually, we maintain this template for the
924 * static and mostly-static components of the TCPCB, and copy it into
925 * the new TCPCB instead.
926 */
927 static struct tcpcb tcpcb_template = {
928 /*
929 * If TCP_NTIMERS ever changes, we'll need to update this
930 * initializer.
931 */
932 .t_timer = {
933 CALLOUT_INITIALIZER,
934 CALLOUT_INITIALIZER,
935 CALLOUT_INITIALIZER,
936 CALLOUT_INITIALIZER,
937 },
938 .t_delack_ch = CALLOUT_INITIALIZER,
939
940 .t_srtt = TCPTV_SRTTBASE,
941 .t_rttmin = TCPTV_MIN,
942
943 .snd_cwnd = TCP_MAXWIN << TCP_MAX_WINSHIFT,
944 .snd_ssthresh = TCP_MAXWIN << TCP_MAX_WINSHIFT,
945 .snd_numholes = 0,
946
947 .t_partialacks = -1,
948 .t_bytes_acked = 0,
949 };
950
951 /*
952 * Updates the TCPCB template whenever a parameter that would affect
953 * the template is changed.
954 */
955 void
956 tcp_tcpcb_template(void)
957 {
958 struct tcpcb *tp = &tcpcb_template;
959 int flags;
960
961 tp->t_peermss = tcp_mssdflt;
962 tp->t_ourmss = tcp_mssdflt;
963 tp->t_segsz = tcp_mssdflt;
964
965 flags = 0;
966 if (tcp_do_rfc1323 && tcp_do_win_scale)
967 flags |= TF_REQ_SCALE;
968 if (tcp_do_rfc1323 && tcp_do_timestamps)
969 flags |= TF_REQ_TSTMP;
970 tp->t_flags = flags;
971
972 /*
973 * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no
974 * rtt estimate. Set rttvar so that srtt + 2 * rttvar gives
975 * reasonable initial retransmit time.
976 */
977 tp->t_rttvar = tcp_rttdflt * PR_SLOWHZ << (TCP_RTTVAR_SHIFT + 2 - 1);
978 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
979 TCPTV_MIN, TCPTV_REXMTMAX);
980 }
981
982 /*
983 * Create a new TCP control block, making an
984 * empty reassembly queue and hooking it to the argument
985 * protocol control block.
986 */
987 /* family selects inpcb, or in6pcb */
988 struct tcpcb *
989 tcp_newtcpcb(int family, void *aux)
990 {
991 struct tcpcb *tp;
992 int i;
993
994 /* XXX Consider using a pool_cache for speed. */
995 tp = pool_get(&tcpcb_pool, PR_NOWAIT); /* splsoftnet via tcp_usrreq */
996 if (tp == NULL)
997 return (NULL);
998 memcpy(tp, &tcpcb_template, sizeof(*tp));
999 TAILQ_INIT(&tp->segq);
1000 TAILQ_INIT(&tp->timeq);
1001 tp->t_family = family; /* may be overridden later on */
1002 TAILQ_INIT(&tp->snd_holes);
1003 LIST_INIT(&tp->t_sc); /* XXX can template this */
1004
1005 /* Don't sweat this loop; hopefully the compiler will unroll it. */
1006 for (i = 0; i < TCPT_NTIMERS; i++)
1007 TCP_TIMER_INIT(tp, i);
1008
1009 switch (family) {
1010 case AF_INET:
1011 {
1012 struct inpcb *inp = (struct inpcb *)aux;
1013
1014 inp->inp_ip.ip_ttl = ip_defttl;
1015 inp->inp_ppcb = (void *)tp;
1016
1017 tp->t_inpcb = inp;
1018 tp->t_mtudisc = ip_mtudisc;
1019 break;
1020 }
1021 #ifdef INET6
1022 case AF_INET6:
1023 {
1024 struct in6pcb *in6p = (struct in6pcb *)aux;
1025
1026 in6p->in6p_ip6.ip6_hlim = in6_selecthlim(in6p,
1027 in6p->in6p_route.ro_rt ? in6p->in6p_route.ro_rt->rt_ifp
1028 : NULL);
1029 in6p->in6p_ppcb = (void *)tp;
1030
1031 tp->t_in6pcb = in6p;
1032 /* for IPv6, always try to run path MTU discovery */
1033 tp->t_mtudisc = 1;
1034 break;
1035 }
1036 #endif /* INET6 */
1037 default:
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 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 pool_put(&tcpcb_pool, tp); /* splsoftnet via tcp_timer.c */
1115 }
1116 return dead;
1117 }
1118
1119 /*
1120 * Close a TCP control block:
1121 * discard all space held by the tcp
1122 * discard internet protocol block
1123 * wake up any sleepers
1124 */
1125 struct tcpcb *
1126 tcp_close(struct tcpcb *tp)
1127 {
1128 struct inpcb *inp;
1129 #ifdef INET6
1130 struct in6pcb *in6p;
1131 #endif
1132 struct socket *so;
1133 #ifdef RTV_RTT
1134 struct rtentry *rt;
1135 #endif
1136 struct route *ro;
1137
1138 inp = tp->t_inpcb;
1139 #ifdef INET6
1140 in6p = tp->t_in6pcb;
1141 #endif
1142 so = NULL;
1143 ro = NULL;
1144 if (inp) {
1145 so = inp->inp_socket;
1146 ro = &inp->inp_route;
1147 }
1148 #ifdef INET6
1149 else if (in6p) {
1150 so = in6p->in6p_socket;
1151 ro = (struct route *)&in6p->in6p_route;
1152 }
1153 #endif
1154
1155 #ifdef RTV_RTT
1156 /*
1157 * If we sent enough data to get some meaningful characteristics,
1158 * save them in the routing entry. 'Enough' is arbitrarily
1159 * defined as the sendpipesize (default 4K) * 16. This would
1160 * give us 16 rtt samples assuming we only get one sample per
1161 * window (the usual case on a long haul net). 16 samples is
1162 * enough for the srtt filter to converge to within 5% of the correct
1163 * value; fewer samples and we could save a very bogus rtt.
1164 *
1165 * Don't update the default route's characteristics and don't
1166 * update anything that the user "locked".
1167 */
1168 if (SEQ_LT(tp->iss + so->so_snd.sb_hiwat * 16, tp->snd_max) &&
1169 ro && (rt = ro->ro_rt) &&
1170 !in_nullhost(satosin(rt_key(rt))->sin_addr)) {
1171 u_long i = 0;
1172
1173 if ((rt->rt_rmx.rmx_locks & RTV_RTT) == 0) {
1174 i = tp->t_srtt *
1175 ((RTM_RTTUNIT / PR_SLOWHZ) >> (TCP_RTT_SHIFT + 2));
1176 if (rt->rt_rmx.rmx_rtt && i)
1177 /*
1178 * filter this update to half the old & half
1179 * the new values, converting scale.
1180 * See route.h and tcp_var.h for a
1181 * description of the scaling constants.
1182 */
1183 rt->rt_rmx.rmx_rtt =
1184 (rt->rt_rmx.rmx_rtt + i) / 2;
1185 else
1186 rt->rt_rmx.rmx_rtt = i;
1187 }
1188 if ((rt->rt_rmx.rmx_locks & RTV_RTTVAR) == 0) {
1189 i = tp->t_rttvar *
1190 ((RTM_RTTUNIT / PR_SLOWHZ) >> (TCP_RTTVAR_SHIFT + 2));
1191 if (rt->rt_rmx.rmx_rttvar && i)
1192 rt->rt_rmx.rmx_rttvar =
1193 (rt->rt_rmx.rmx_rttvar + i) / 2;
1194 else
1195 rt->rt_rmx.rmx_rttvar = i;
1196 }
1197 /*
1198 * update the pipelimit (ssthresh) if it has been updated
1199 * already or if a pipesize was specified & the threshhold
1200 * got below half the pipesize. I.e., wait for bad news
1201 * before we start updating, then update on both good
1202 * and bad news.
1203 */
1204 if (((rt->rt_rmx.rmx_locks & RTV_SSTHRESH) == 0 &&
1205 (i = tp->snd_ssthresh) && rt->rt_rmx.rmx_ssthresh) ||
1206 i < (rt->rt_rmx.rmx_sendpipe / 2)) {
1207 /*
1208 * convert the limit from user data bytes to
1209 * packets then to packet data bytes.
1210 */
1211 i = (i + tp->t_segsz / 2) / tp->t_segsz;
1212 if (i < 2)
1213 i = 2;
1214 i *= (u_long)(tp->t_segsz + sizeof (struct tcpiphdr));
1215 if (rt->rt_rmx.rmx_ssthresh)
1216 rt->rt_rmx.rmx_ssthresh =
1217 (rt->rt_rmx.rmx_ssthresh + i) / 2;
1218 else
1219 rt->rt_rmx.rmx_ssthresh = i;
1220 }
1221 }
1222 #endif /* RTV_RTT */
1223 /* free the reassembly queue, if any */
1224 TCP_REASS_LOCK(tp);
1225 (void) tcp_freeq(tp);
1226 TCP_REASS_UNLOCK(tp);
1227
1228 /* free the SACK holes list. */
1229 tcp_free_sackholes(tp);
1230
1231 tp->t_congctl->refcnt--;
1232
1233 tcp_canceltimers(tp);
1234 TCP_CLEAR_DELACK(tp);
1235 syn_cache_cleanup(tp);
1236
1237 if (tp->t_template) {
1238 m_free(tp->t_template);
1239 tp->t_template = NULL;
1240 }
1241 if (tcp_timers_invoking(tp))
1242 tp->t_flags |= TF_DEAD;
1243 else
1244 pool_put(&tcpcb_pool, tp);
1245
1246 if (inp) {
1247 inp->inp_ppcb = 0;
1248 soisdisconnected(so);
1249 in_pcbdetach(inp);
1250 }
1251 #ifdef INET6
1252 else if (in6p) {
1253 in6p->in6p_ppcb = 0;
1254 soisdisconnected(so);
1255 in6_pcbdetach(in6p);
1256 }
1257 #endif
1258 tcpstat.tcps_closed++;
1259 return ((struct tcpcb *)0);
1260 }
1261
1262 int
1263 tcp_freeq(tp)
1264 struct tcpcb *tp;
1265 {
1266 struct ipqent *qe;
1267 int rv = 0;
1268 #ifdef TCPREASS_DEBUG
1269 int i = 0;
1270 #endif
1271
1272 TCP_REASS_LOCK_CHECK(tp);
1273
1274 while ((qe = TAILQ_FIRST(&tp->segq)) != NULL) {
1275 #ifdef TCPREASS_DEBUG
1276 printf("tcp_freeq[%p,%d]: %u:%u(%u) 0x%02x\n",
1277 tp, i++, qe->ipqe_seq, qe->ipqe_seq + qe->ipqe_len,
1278 qe->ipqe_len, qe->ipqe_flags & (TH_SYN|TH_FIN|TH_RST));
1279 #endif
1280 TAILQ_REMOVE(&tp->segq, qe, ipqe_q);
1281 TAILQ_REMOVE(&tp->timeq, qe, ipqe_timeq);
1282 m_freem(qe->ipqe_m);
1283 tcpipqent_free(qe);
1284 rv = 1;
1285 }
1286 tp->t_segqlen = 0;
1287 KASSERT(TAILQ_EMPTY(&tp->timeq));
1288 return (rv);
1289 }
1290
1291 /*
1292 * Protocol drain routine. Called when memory is in short supply.
1293 */
1294 void
1295 tcp_drain(void)
1296 {
1297 struct inpcb_hdr *inph;
1298 struct tcpcb *tp;
1299
1300 /*
1301 * Free the sequence queue of all TCP connections.
1302 */
1303 CIRCLEQ_FOREACH(inph, &tcbtable.inpt_queue, inph_queue) {
1304 switch (inph->inph_af) {
1305 case AF_INET:
1306 tp = intotcpcb((struct inpcb *)inph);
1307 break;
1308 #ifdef INET6
1309 case AF_INET6:
1310 tp = in6totcpcb((struct in6pcb *)inph);
1311 break;
1312 #endif
1313 default:
1314 tp = NULL;
1315 break;
1316 }
1317 if (tp != NULL) {
1318 /*
1319 * We may be called from a device's interrupt
1320 * context. If the tcpcb is already busy,
1321 * just bail out now.
1322 */
1323 if (tcp_reass_lock_try(tp) == 0)
1324 continue;
1325 if (tcp_freeq(tp))
1326 tcpstat.tcps_connsdrained++;
1327 TCP_REASS_UNLOCK(tp);
1328 }
1329 }
1330 }
1331
1332 /*
1333 * Notify a tcp user of an asynchronous error;
1334 * store error as soft error, but wake up user
1335 * (for now, won't do anything until can select for soft error).
1336 */
1337 void
1338 tcp_notify(struct inpcb *inp, int error)
1339 {
1340 struct tcpcb *tp = (struct tcpcb *)inp->inp_ppcb;
1341 struct socket *so = inp->inp_socket;
1342
1343 /*
1344 * Ignore some errors if we are hooked up.
1345 * If connection hasn't completed, has retransmitted several times,
1346 * and receives a second error, give up now. This is better
1347 * than waiting a long time to establish a connection that
1348 * can never complete.
1349 */
1350 if (tp->t_state == TCPS_ESTABLISHED &&
1351 (error == EHOSTUNREACH || error == ENETUNREACH ||
1352 error == EHOSTDOWN)) {
1353 return;
1354 } else if (TCPS_HAVEESTABLISHED(tp->t_state) == 0 &&
1355 tp->t_rxtshift > 3 && tp->t_softerror)
1356 so->so_error = error;
1357 else
1358 tp->t_softerror = error;
1359 wakeup((void *) &so->so_timeo);
1360 sorwakeup(so);
1361 sowwakeup(so);
1362 }
1363
1364 #ifdef INET6
1365 void
1366 tcp6_notify(struct in6pcb *in6p, int error)
1367 {
1368 struct tcpcb *tp = (struct tcpcb *)in6p->in6p_ppcb;
1369 struct socket *so = in6p->in6p_socket;
1370
1371 /*
1372 * Ignore some errors if we are hooked up.
1373 * If connection hasn't completed, has retransmitted several times,
1374 * and receives a second error, give up now. This is better
1375 * than waiting a long time to establish a connection that
1376 * can never complete.
1377 */
1378 if (tp->t_state == TCPS_ESTABLISHED &&
1379 (error == EHOSTUNREACH || error == ENETUNREACH ||
1380 error == EHOSTDOWN)) {
1381 return;
1382 } else if (TCPS_HAVEESTABLISHED(tp->t_state) == 0 &&
1383 tp->t_rxtshift > 3 && tp->t_softerror)
1384 so->so_error = error;
1385 else
1386 tp->t_softerror = error;
1387 wakeup((void *) &so->so_timeo);
1388 sorwakeup(so);
1389 sowwakeup(so);
1390 }
1391 #endif
1392
1393 #ifdef INET6
1394 void
1395 tcp6_ctlinput(int cmd, const struct sockaddr *sa, void *d)
1396 {
1397 struct tcphdr th;
1398 void (*notify)(struct in6pcb *, int) = tcp6_notify;
1399 int nmatch;
1400 struct ip6_hdr *ip6;
1401 const struct sockaddr_in6 *sa6_src = NULL;
1402 const struct sockaddr_in6 *sa6 = (const struct sockaddr_in6 *)sa;
1403 struct mbuf *m;
1404 int off;
1405
1406 if (sa->sa_family != AF_INET6 ||
1407 sa->sa_len != sizeof(struct sockaddr_in6))
1408 return;
1409 if ((unsigned)cmd >= PRC_NCMDS)
1410 return;
1411 else if (cmd == PRC_QUENCH) {
1412 /*
1413 * Don't honor ICMP Source Quench messages meant for
1414 * TCP connections.
1415 */
1416 return;
1417 } else if (PRC_IS_REDIRECT(cmd))
1418 notify = in6_rtchange, d = NULL;
1419 else if (cmd == PRC_MSGSIZE)
1420 ; /* special code is present, see below */
1421 else if (cmd == PRC_HOSTDEAD)
1422 d = NULL;
1423 else if (inet6ctlerrmap[cmd] == 0)
1424 return;
1425
1426 /* if the parameter is from icmp6, decode it. */
1427 if (d != NULL) {
1428 struct ip6ctlparam *ip6cp = (struct ip6ctlparam *)d;
1429 m = ip6cp->ip6c_m;
1430 ip6 = ip6cp->ip6c_ip6;
1431 off = ip6cp->ip6c_off;
1432 sa6_src = ip6cp->ip6c_src;
1433 } else {
1434 m = NULL;
1435 ip6 = NULL;
1436 sa6_src = &sa6_any;
1437 off = 0;
1438 }
1439
1440 if (ip6) {
1441 /*
1442 * XXX: We assume that when ip6 is non NULL,
1443 * M and OFF are valid.
1444 */
1445
1446 /* check if we can safely examine src and dst ports */
1447 if (m->m_pkthdr.len < off + sizeof(th)) {
1448 if (cmd == PRC_MSGSIZE)
1449 icmp6_mtudisc_update((struct ip6ctlparam *)d, 0);
1450 return;
1451 }
1452
1453 bzero(&th, sizeof(th));
1454 m_copydata(m, off, sizeof(th), (void *)&th);
1455
1456 if (cmd == PRC_MSGSIZE) {
1457 int valid = 0;
1458
1459 /*
1460 * Check to see if we have a valid TCP connection
1461 * corresponding to the address in the ICMPv6 message
1462 * payload.
1463 */
1464 if (in6_pcblookup_connect(&tcbtable, &sa6->sin6_addr,
1465 th.th_dport,
1466 (const struct in6_addr *)&sa6_src->sin6_addr,
1467 th.th_sport, 0))
1468 valid++;
1469
1470 /*
1471 * Depending on the value of "valid" and routing table
1472 * size (mtudisc_{hi,lo}wat), we will:
1473 * - recalcurate the new MTU and create the
1474 * corresponding routing entry, or
1475 * - ignore the MTU change notification.
1476 */
1477 icmp6_mtudisc_update((struct ip6ctlparam *)d, valid);
1478
1479 /*
1480 * no need to call in6_pcbnotify, it should have been
1481 * called via callback if necessary
1482 */
1483 return;
1484 }
1485
1486 nmatch = in6_pcbnotify(&tcbtable, sa, th.th_dport,
1487 (const struct sockaddr *)sa6_src, th.th_sport, cmd, NULL, notify);
1488 if (nmatch == 0 && syn_cache_count &&
1489 (inet6ctlerrmap[cmd] == EHOSTUNREACH ||
1490 inet6ctlerrmap[cmd] == ENETUNREACH ||
1491 inet6ctlerrmap[cmd] == EHOSTDOWN))
1492 syn_cache_unreach((const struct sockaddr *)sa6_src,
1493 sa, &th);
1494 } else {
1495 (void) in6_pcbnotify(&tcbtable, sa, 0,
1496 (const struct sockaddr *)sa6_src, 0, cmd, NULL, notify);
1497 }
1498 }
1499 #endif
1500
1501 #ifdef INET
1502 /* assumes that ip header and tcp header are contiguous on mbuf */
1503 void *
1504 tcp_ctlinput(int cmd, const struct sockaddr *sa, void *v)
1505 {
1506 struct ip *ip = v;
1507 struct tcphdr *th;
1508 struct icmp *icp;
1509 extern const int inetctlerrmap[];
1510 void (*notify)(struct inpcb *, int) = tcp_notify;
1511 int errno;
1512 int nmatch;
1513 struct tcpcb *tp;
1514 u_int mtu;
1515 tcp_seq seq;
1516 struct inpcb *inp;
1517 #ifdef INET6
1518 struct in6pcb *in6p;
1519 struct in6_addr src6, dst6;
1520 #endif
1521
1522 if (sa->sa_family != AF_INET ||
1523 sa->sa_len != sizeof(struct sockaddr_in))
1524 return NULL;
1525 if ((unsigned)cmd >= PRC_NCMDS)
1526 return NULL;
1527 errno = inetctlerrmap[cmd];
1528 if (cmd == PRC_QUENCH)
1529 /*
1530 * Don't honor ICMP Source Quench messages meant for
1531 * TCP connections.
1532 */
1533 return NULL;
1534 else if (PRC_IS_REDIRECT(cmd))
1535 notify = in_rtchange, ip = 0;
1536 else if (cmd == PRC_MSGSIZE && ip && ip->ip_v == 4) {
1537 /*
1538 * Check to see if we have a valid TCP connection
1539 * corresponding to the address in the ICMP message
1540 * payload.
1541 *
1542 * Boundary check is made in icmp_input(), with ICMP_ADVLENMIN.
1543 */
1544 th = (struct tcphdr *)((char *)ip + (ip->ip_hl << 2));
1545 #ifdef INET6
1546 memset(&src6, 0, sizeof(src6));
1547 memset(&dst6, 0, sizeof(dst6));
1548 src6.s6_addr16[5] = dst6.s6_addr16[5] = 0xffff;
1549 memcpy(&src6.s6_addr32[3], &ip->ip_src, sizeof(struct in_addr));
1550 memcpy(&dst6.s6_addr32[3], &ip->ip_dst, sizeof(struct in_addr));
1551 #endif
1552 if ((inp = in_pcblookup_connect(&tcbtable, ip->ip_dst,
1553 th->th_dport, ip->ip_src, th->th_sport)) != NULL)
1554 #ifdef INET6
1555 in6p = NULL;
1556 #else
1557 ;
1558 #endif
1559 #ifdef INET6
1560 else if ((in6p = in6_pcblookup_connect(&tcbtable, &dst6,
1561 th->th_dport, &src6, th->th_sport, 0)) != NULL)
1562 ;
1563 #endif
1564 else
1565 return NULL;
1566
1567 /*
1568 * Now that we've validated that we are actually communicating
1569 * with the host indicated in the ICMP message, locate the
1570 * ICMP header, recalculate the new MTU, and create the
1571 * corresponding routing entry.
1572 */
1573 icp = (struct icmp *)((char *)ip -
1574 offsetof(struct icmp, icmp_ip));
1575 if (inp) {
1576 if ((tp = intotcpcb(inp)) == NULL)
1577 return NULL;
1578 }
1579 #ifdef INET6
1580 else if (in6p) {
1581 if ((tp = in6totcpcb(in6p)) == NULL)
1582 return NULL;
1583 }
1584 #endif
1585 else
1586 return NULL;
1587 seq = ntohl(th->th_seq);
1588 if (SEQ_LT(seq, tp->snd_una) || SEQ_GT(seq, tp->snd_max))
1589 return NULL;
1590 /*
1591 * If the ICMP message advertises a Next-Hop MTU
1592 * equal or larger than the maximum packet size we have
1593 * ever sent, drop the message.
1594 */
1595 mtu = (u_int)ntohs(icp->icmp_nextmtu);
1596 if (mtu >= tp->t_pmtud_mtu_sent)
1597 return NULL;
1598 if (mtu >= tcp_hdrsz(tp) + tp->t_pmtud_mss_acked) {
1599 /*
1600 * Calculate new MTU, and create corresponding
1601 * route (traditional PMTUD).
1602 */
1603 tp->t_flags &= ~TF_PMTUD_PEND;
1604 icmp_mtudisc(icp, ip->ip_dst);
1605 } else {
1606 /*
1607 * Record the information got in the ICMP
1608 * message; act on it later.
1609 * If we had already recorded an ICMP message,
1610 * replace the old one only if the new message
1611 * refers to an older TCP segment
1612 */
1613 if (tp->t_flags & TF_PMTUD_PEND) {
1614 if (SEQ_LT(tp->t_pmtud_th_seq, seq))
1615 return NULL;
1616 } else
1617 tp->t_flags |= TF_PMTUD_PEND;
1618 tp->t_pmtud_th_seq = seq;
1619 tp->t_pmtud_nextmtu = icp->icmp_nextmtu;
1620 tp->t_pmtud_ip_len = icp->icmp_ip.ip_len;
1621 tp->t_pmtud_ip_hl = icp->icmp_ip.ip_hl;
1622 }
1623 return NULL;
1624 } else if (cmd == PRC_HOSTDEAD)
1625 ip = 0;
1626 else if (errno == 0)
1627 return NULL;
1628 if (ip && ip->ip_v == 4 && sa->sa_family == AF_INET) {
1629 th = (struct tcphdr *)((char *)ip + (ip->ip_hl << 2));
1630 nmatch = in_pcbnotify(&tcbtable, satocsin(sa)->sin_addr,
1631 th->th_dport, ip->ip_src, th->th_sport, errno, notify);
1632 if (nmatch == 0 && syn_cache_count &&
1633 (inetctlerrmap[cmd] == EHOSTUNREACH ||
1634 inetctlerrmap[cmd] == ENETUNREACH ||
1635 inetctlerrmap[cmd] == EHOSTDOWN)) {
1636 struct sockaddr_in sin;
1637 bzero(&sin, sizeof(sin));
1638 sin.sin_len = sizeof(sin);
1639 sin.sin_family = AF_INET;
1640 sin.sin_port = th->th_sport;
1641 sin.sin_addr = ip->ip_src;
1642 syn_cache_unreach((struct sockaddr *)&sin, sa, th);
1643 }
1644
1645 /* XXX mapped address case */
1646 } else
1647 in_pcbnotifyall(&tcbtable, satocsin(sa)->sin_addr, errno,
1648 notify);
1649 return NULL;
1650 }
1651
1652 /*
1653 * When a source quench is received, we are being notified of congestion.
1654 * Close the congestion window down to the Loss Window (one segment).
1655 * We will gradually open it again as we proceed.
1656 */
1657 void
1658 tcp_quench(struct inpcb *inp, int errno)
1659 {
1660 struct tcpcb *tp = intotcpcb(inp);
1661
1662 if (tp) {
1663 tp->snd_cwnd = tp->t_segsz;
1664 tp->t_bytes_acked = 0;
1665 }
1666 }
1667 #endif
1668
1669 #ifdef INET6
1670 void
1671 tcp6_quench(struct in6pcb *in6p, int errno)
1672 {
1673 struct tcpcb *tp = in6totcpcb(in6p);
1674
1675 if (tp) {
1676 tp->snd_cwnd = tp->t_segsz;
1677 tp->t_bytes_acked = 0;
1678 }
1679 }
1680 #endif
1681
1682 #ifdef INET
1683 /*
1684 * Path MTU Discovery handlers.
1685 */
1686 void
1687 tcp_mtudisc_callback(struct in_addr faddr)
1688 {
1689 #ifdef INET6
1690 struct in6_addr in6;
1691 #endif
1692
1693 in_pcbnotifyall(&tcbtable, faddr, EMSGSIZE, tcp_mtudisc);
1694 #ifdef INET6
1695 memset(&in6, 0, sizeof(in6));
1696 in6.s6_addr16[5] = 0xffff;
1697 memcpy(&in6.s6_addr32[3], &faddr, sizeof(struct in_addr));
1698 tcp6_mtudisc_callback(&in6);
1699 #endif
1700 }
1701
1702 /*
1703 * On receipt of path MTU corrections, flush old route and replace it
1704 * with the new one. Retransmit all unacknowledged packets, to ensure
1705 * that all packets will be received.
1706 */
1707 void
1708 tcp_mtudisc(struct inpcb *inp, int errno)
1709 {
1710 struct tcpcb *tp = intotcpcb(inp);
1711 struct rtentry *rt = in_pcbrtentry(inp);
1712
1713 if (tp != 0) {
1714 if (rt != 0) {
1715 /*
1716 * If this was not a host route, remove and realloc.
1717 */
1718 if ((rt->rt_flags & RTF_HOST) == 0) {
1719 in_rtchange(inp, errno);
1720 if ((rt = in_pcbrtentry(inp)) == 0)
1721 return;
1722 }
1723
1724 /*
1725 * Slow start out of the error condition. We
1726 * use the MTU because we know it's smaller
1727 * than the previously transmitted segment.
1728 *
1729 * Note: This is more conservative than the
1730 * suggestion in draft-floyd-incr-init-win-03.
1731 */
1732 if (rt->rt_rmx.rmx_mtu != 0)
1733 tp->snd_cwnd =
1734 TCP_INITIAL_WINDOW(tcp_init_win,
1735 rt->rt_rmx.rmx_mtu);
1736 }
1737
1738 /*
1739 * Resend unacknowledged packets.
1740 */
1741 tp->snd_nxt = tp->snd_una;
1742 tcp_output(tp);
1743 }
1744 }
1745 #endif
1746
1747 #ifdef INET6
1748 /*
1749 * Path MTU Discovery handlers.
1750 */
1751 void
1752 tcp6_mtudisc_callback(struct in6_addr *faddr)
1753 {
1754 struct sockaddr_in6 sin6;
1755
1756 bzero(&sin6, sizeof(sin6));
1757 sin6.sin6_family = AF_INET6;
1758 sin6.sin6_len = sizeof(struct sockaddr_in6);
1759 sin6.sin6_addr = *faddr;
1760 (void) in6_pcbnotify(&tcbtable, (struct sockaddr *)&sin6, 0,
1761 (const struct sockaddr *)&sa6_any, 0, PRC_MSGSIZE, NULL, tcp6_mtudisc);
1762 }
1763
1764 void
1765 tcp6_mtudisc(struct in6pcb *in6p, int errno)
1766 {
1767 struct tcpcb *tp = in6totcpcb(in6p);
1768 struct rtentry *rt = in6_pcbrtentry(in6p);
1769
1770 if (tp != 0) {
1771 if (rt != 0) {
1772 /*
1773 * If this was not a host route, remove and realloc.
1774 */
1775 if ((rt->rt_flags & RTF_HOST) == 0) {
1776 in6_rtchange(in6p, errno);
1777 if ((rt = in6_pcbrtentry(in6p)) == 0)
1778 return;
1779 }
1780
1781 /*
1782 * Slow start out of the error condition. We
1783 * use the MTU because we know it's smaller
1784 * than the previously transmitted segment.
1785 *
1786 * Note: This is more conservative than the
1787 * suggestion in draft-floyd-incr-init-win-03.
1788 */
1789 if (rt->rt_rmx.rmx_mtu != 0)
1790 tp->snd_cwnd =
1791 TCP_INITIAL_WINDOW(tcp_init_win,
1792 rt->rt_rmx.rmx_mtu);
1793 }
1794
1795 /*
1796 * Resend unacknowledged packets.
1797 */
1798 tp->snd_nxt = tp->snd_una;
1799 tcp_output(tp);
1800 }
1801 }
1802 #endif /* INET6 */
1803
1804 /*
1805 * Compute the MSS to advertise to the peer. Called only during
1806 * the 3-way handshake. If we are the server (peer initiated
1807 * connection), we are called with a pointer to the interface
1808 * on which the SYN packet arrived. If we are the client (we
1809 * initiated connection), we are called with a pointer to the
1810 * interface out which this connection should go.
1811 *
1812 * NOTE: Do not subtract IP option/extension header size nor IPsec
1813 * header size from MSS advertisement. MSS option must hold the maximum
1814 * segment size we can accept, so it must always be:
1815 * max(if mtu) - ip header - tcp header
1816 */
1817 u_long
1818 tcp_mss_to_advertise(const struct ifnet *ifp, int af)
1819 {
1820 extern u_long in_maxmtu;
1821 u_long mss = 0;
1822 u_long hdrsiz;
1823
1824 /*
1825 * In order to avoid defeating path MTU discovery on the peer,
1826 * we advertise the max MTU of all attached networks as our MSS,
1827 * per RFC 1191, section 3.1.
1828 *
1829 * We provide the option to advertise just the MTU of
1830 * the interface on which we hope this connection will
1831 * be receiving. If we are responding to a SYN, we
1832 * will have a pretty good idea about this, but when
1833 * initiating a connection there is a bit more doubt.
1834 *
1835 * We also need to ensure that loopback has a large enough
1836 * MSS, as the loopback MTU is never included in in_maxmtu.
1837 */
1838
1839 if (ifp != NULL)
1840 switch (af) {
1841 case AF_INET:
1842 mss = ifp->if_mtu;
1843 break;
1844 #ifdef INET6
1845 case AF_INET6:
1846 mss = IN6_LINKMTU(ifp);
1847 break;
1848 #endif
1849 }
1850
1851 if (tcp_mss_ifmtu == 0)
1852 switch (af) {
1853 case AF_INET:
1854 mss = max(in_maxmtu, mss);
1855 break;
1856 #ifdef INET6
1857 case AF_INET6:
1858 mss = max(in6_maxmtu, mss);
1859 break;
1860 #endif
1861 }
1862
1863 switch (af) {
1864 case AF_INET:
1865 hdrsiz = sizeof(struct ip);
1866 break;
1867 #ifdef INET6
1868 case AF_INET6:
1869 hdrsiz = sizeof(struct ip6_hdr);
1870 break;
1871 #endif
1872 default:
1873 hdrsiz = 0;
1874 break;
1875 }
1876 hdrsiz += sizeof(struct tcphdr);
1877 if (mss > hdrsiz)
1878 mss -= hdrsiz;
1879
1880 mss = max(tcp_mssdflt, mss);
1881 return (mss);
1882 }
1883
1884 /*
1885 * Set connection variables based on the peer's advertised MSS.
1886 * We are passed the TCPCB for the actual connection. If we
1887 * are the server, we are called by the compressed state engine
1888 * when the 3-way handshake is complete. If we are the client,
1889 * we are called when we receive the SYN,ACK from the server.
1890 *
1891 * NOTE: Our advertised MSS value must be initialized in the TCPCB
1892 * before this routine is called!
1893 */
1894 void
1895 tcp_mss_from_peer(struct tcpcb *tp, int offer)
1896 {
1897 struct socket *so;
1898 #if defined(RTV_SPIPE) || defined(RTV_SSTHRESH)
1899 struct rtentry *rt;
1900 #endif
1901 u_long bufsize;
1902 int mss;
1903
1904 #ifdef DIAGNOSTIC
1905 if (tp->t_inpcb && tp->t_in6pcb)
1906 panic("tcp_mss_from_peer: both t_inpcb and t_in6pcb are set");
1907 #endif
1908 so = NULL;
1909 rt = NULL;
1910 #ifdef INET
1911 if (tp->t_inpcb) {
1912 so = tp->t_inpcb->inp_socket;
1913 #if defined(RTV_SPIPE) || defined(RTV_SSTHRESH)
1914 rt = in_pcbrtentry(tp->t_inpcb);
1915 #endif
1916 }
1917 #endif
1918 #ifdef INET6
1919 if (tp->t_in6pcb) {
1920 so = tp->t_in6pcb->in6p_socket;
1921 #if defined(RTV_SPIPE) || defined(RTV_SSTHRESH)
1922 rt = in6_pcbrtentry(tp->t_in6pcb);
1923 #endif
1924 }
1925 #endif
1926
1927 /*
1928 * As per RFC1122, use the default MSS value, unless they
1929 * sent us an offer. Do not accept offers less than 256 bytes.
1930 */
1931 mss = tcp_mssdflt;
1932 if (offer)
1933 mss = offer;
1934 mss = max(mss, 256); /* sanity */
1935 tp->t_peermss = mss;
1936 mss -= tcp_optlen(tp);
1937 #ifdef INET
1938 if (tp->t_inpcb)
1939 mss -= ip_optlen(tp->t_inpcb);
1940 #endif
1941 #ifdef INET6
1942 if (tp->t_in6pcb)
1943 mss -= ip6_optlen(tp->t_in6pcb);
1944 #endif
1945
1946 /*
1947 * If there's a pipesize, change the socket buffer to that size.
1948 * Make the socket buffer an integral number of MSS units. If
1949 * the MSS is larger than the socket buffer, artificially decrease
1950 * the MSS.
1951 */
1952 #ifdef RTV_SPIPE
1953 if (rt != NULL && rt->rt_rmx.rmx_sendpipe != 0)
1954 bufsize = rt->rt_rmx.rmx_sendpipe;
1955 else
1956 #endif
1957 {
1958 KASSERT(so != NULL);
1959 bufsize = so->so_snd.sb_hiwat;
1960 }
1961 if (bufsize < mss)
1962 mss = bufsize;
1963 else {
1964 bufsize = roundup(bufsize, mss);
1965 if (bufsize > sb_max)
1966 bufsize = sb_max;
1967 (void) sbreserve(&so->so_snd, bufsize, so);
1968 }
1969 tp->t_segsz = mss;
1970
1971 #ifdef RTV_SSTHRESH
1972 if (rt != NULL && rt->rt_rmx.rmx_ssthresh) {
1973 /*
1974 * There's some sort of gateway or interface buffer
1975 * limit on the path. Use this to set the slow
1976 * start threshold, but set the threshold to no less
1977 * than 2 * MSS.
1978 */
1979 tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh);
1980 }
1981 #endif
1982 }
1983
1984 /*
1985 * Processing necessary when a TCP connection is established.
1986 */
1987 void
1988 tcp_established(struct tcpcb *tp)
1989 {
1990 struct socket *so;
1991 #ifdef RTV_RPIPE
1992 struct rtentry *rt;
1993 #endif
1994 u_long bufsize;
1995
1996 #ifdef DIAGNOSTIC
1997 if (tp->t_inpcb && tp->t_in6pcb)
1998 panic("tcp_established: both t_inpcb and t_in6pcb are set");
1999 #endif
2000 so = NULL;
2001 rt = NULL;
2002 #ifdef INET
2003 if (tp->t_inpcb) {
2004 so = tp->t_inpcb->inp_socket;
2005 #if defined(RTV_RPIPE)
2006 rt = in_pcbrtentry(tp->t_inpcb);
2007 #endif
2008 }
2009 #endif
2010 #ifdef INET6
2011 if (tp->t_in6pcb) {
2012 so = tp->t_in6pcb->in6p_socket;
2013 #if defined(RTV_RPIPE)
2014 rt = in6_pcbrtentry(tp->t_in6pcb);
2015 #endif
2016 }
2017 #endif
2018
2019 tp->t_state = TCPS_ESTABLISHED;
2020 TCP_TIMER_ARM(tp, TCPT_KEEP, tcp_keepidle);
2021
2022 #ifdef RTV_RPIPE
2023 if (rt != NULL && rt->rt_rmx.rmx_recvpipe != 0)
2024 bufsize = rt->rt_rmx.rmx_recvpipe;
2025 else
2026 #endif
2027 {
2028 KASSERT(so != NULL);
2029 bufsize = so->so_rcv.sb_hiwat;
2030 }
2031 if (bufsize > tp->t_ourmss) {
2032 bufsize = roundup(bufsize, tp->t_ourmss);
2033 if (bufsize > sb_max)
2034 bufsize = sb_max;
2035 (void) sbreserve(&so->so_rcv, bufsize, so);
2036 }
2037 }
2038
2039 /*
2040 * Check if there's an initial rtt or rttvar. Convert from the
2041 * route-table units to scaled multiples of the slow timeout timer.
2042 * Called only during the 3-way handshake.
2043 */
2044 void
2045 tcp_rmx_rtt(struct tcpcb *tp)
2046 {
2047 #ifdef RTV_RTT
2048 struct rtentry *rt = NULL;
2049 int rtt;
2050
2051 #ifdef DIAGNOSTIC
2052 if (tp->t_inpcb && tp->t_in6pcb)
2053 panic("tcp_rmx_rtt: both t_inpcb and t_in6pcb are set");
2054 #endif
2055 #ifdef INET
2056 if (tp->t_inpcb)
2057 rt = in_pcbrtentry(tp->t_inpcb);
2058 #endif
2059 #ifdef INET6
2060 if (tp->t_in6pcb)
2061 rt = in6_pcbrtentry(tp->t_in6pcb);
2062 #endif
2063 if (rt == NULL)
2064 return;
2065
2066 if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) {
2067 /*
2068 * XXX The lock bit for MTU indicates that the value
2069 * is also a minimum value; this is subject to time.
2070 */
2071 if (rt->rt_rmx.rmx_locks & RTV_RTT)
2072 TCPT_RANGESET(tp->t_rttmin,
2073 rtt / (RTM_RTTUNIT / PR_SLOWHZ),
2074 TCPTV_MIN, TCPTV_REXMTMAX);
2075 tp->t_srtt = rtt /
2076 ((RTM_RTTUNIT / PR_SLOWHZ) >> (TCP_RTT_SHIFT + 2));
2077 if (rt->rt_rmx.rmx_rttvar) {
2078 tp->t_rttvar = rt->rt_rmx.rmx_rttvar /
2079 ((RTM_RTTUNIT / PR_SLOWHZ) >>
2080 (TCP_RTTVAR_SHIFT + 2));
2081 } else {
2082 /* Default variation is +- 1 rtt */
2083 tp->t_rttvar =
2084 tp->t_srtt >> (TCP_RTT_SHIFT - TCP_RTTVAR_SHIFT);
2085 }
2086 TCPT_RANGESET(tp->t_rxtcur,
2087 ((tp->t_srtt >> 2) + tp->t_rttvar) >> (1 + 2),
2088 tp->t_rttmin, TCPTV_REXMTMAX);
2089 }
2090 #endif
2091 }
2092
2093 tcp_seq tcp_iss_seq = 0; /* tcp initial seq # */
2094 #if NRND > 0
2095 u_int8_t tcp_iss_secret[16]; /* 128 bits; should be plenty */
2096 #endif
2097
2098 /*
2099 * Get a new sequence value given a tcp control block
2100 */
2101 tcp_seq
2102 tcp_new_iss(struct tcpcb *tp, tcp_seq addin)
2103 {
2104
2105 #ifdef INET
2106 if (tp->t_inpcb != NULL) {
2107 return (tcp_new_iss1(&tp->t_inpcb->inp_laddr,
2108 &tp->t_inpcb->inp_faddr, tp->t_inpcb->inp_lport,
2109 tp->t_inpcb->inp_fport, sizeof(tp->t_inpcb->inp_laddr),
2110 addin));
2111 }
2112 #endif
2113 #ifdef INET6
2114 if (tp->t_in6pcb != NULL) {
2115 return (tcp_new_iss1(&tp->t_in6pcb->in6p_laddr,
2116 &tp->t_in6pcb->in6p_faddr, tp->t_in6pcb->in6p_lport,
2117 tp->t_in6pcb->in6p_fport, sizeof(tp->t_in6pcb->in6p_laddr),
2118 addin));
2119 }
2120 #endif
2121 /* Not possible. */
2122 panic("tcp_new_iss");
2123 }
2124
2125 /*
2126 * This routine actually generates a new TCP initial sequence number.
2127 */
2128 tcp_seq
2129 tcp_new_iss1(void *laddr, void *faddr, u_int16_t lport, u_int16_t fport,
2130 size_t addrsz, tcp_seq addin)
2131 {
2132 tcp_seq tcp_iss;
2133
2134 #if NRND > 0
2135 static int beenhere;
2136
2137 /*
2138 * If we haven't been here before, initialize our cryptographic
2139 * hash secret.
2140 */
2141 if (beenhere == 0) {
2142 rnd_extract_data(tcp_iss_secret, sizeof(tcp_iss_secret),
2143 RND_EXTRACT_ANY);
2144 beenhere = 1;
2145 }
2146
2147 if (tcp_do_rfc1948) {
2148 MD5_CTX ctx;
2149 u_int8_t hash[16]; /* XXX MD5 knowledge */
2150
2151 /*
2152 * Compute the base value of the ISS. It is a hash
2153 * of (saddr, sport, daddr, dport, secret).
2154 */
2155 MD5Init(&ctx);
2156
2157 MD5Update(&ctx, (u_char *) laddr, addrsz);
2158 MD5Update(&ctx, (u_char *) &lport, sizeof(lport));
2159
2160 MD5Update(&ctx, (u_char *) faddr, addrsz);
2161 MD5Update(&ctx, (u_char *) &fport, sizeof(fport));
2162
2163 MD5Update(&ctx, tcp_iss_secret, sizeof(tcp_iss_secret));
2164
2165 MD5Final(hash, &ctx);
2166
2167 memcpy(&tcp_iss, hash, sizeof(tcp_iss));
2168
2169 /*
2170 * Now increment our "timer", and add it in to
2171 * the computed value.
2172 *
2173 * XXX Use `addin'?
2174 * XXX TCP_ISSINCR too large to use?
2175 */
2176 tcp_iss_seq += TCP_ISSINCR;
2177 #ifdef TCPISS_DEBUG
2178 printf("ISS hash 0x%08x, ", tcp_iss);
2179 #endif
2180 tcp_iss += tcp_iss_seq + addin;
2181 #ifdef TCPISS_DEBUG
2182 printf("new ISS 0x%08x\n", tcp_iss);
2183 #endif
2184 } else
2185 #endif /* NRND > 0 */
2186 {
2187 /*
2188 * Randomize.
2189 */
2190 #if NRND > 0
2191 rnd_extract_data(&tcp_iss, sizeof(tcp_iss), RND_EXTRACT_ANY);
2192 #else
2193 tcp_iss = arc4random();
2194 #endif
2195
2196 /*
2197 * If we were asked to add some amount to a known value,
2198 * we will take a random value obtained above, mask off
2199 * the upper bits, and add in the known value. We also
2200 * add in a constant to ensure that we are at least a
2201 * certain distance from the original value.
2202 *
2203 * This is used when an old connection is in timed wait
2204 * and we have a new one coming in, for instance.
2205 */
2206 if (addin != 0) {
2207 #ifdef TCPISS_DEBUG
2208 printf("Random %08x, ", tcp_iss);
2209 #endif
2210 tcp_iss &= TCP_ISS_RANDOM_MASK;
2211 tcp_iss += addin + TCP_ISSINCR;
2212 #ifdef TCPISS_DEBUG
2213 printf("Old ISS %08x, ISS %08x\n", addin, tcp_iss);
2214 #endif
2215 } else {
2216 tcp_iss &= TCP_ISS_RANDOM_MASK;
2217 tcp_iss += tcp_iss_seq;
2218 tcp_iss_seq += TCP_ISSINCR;
2219 #ifdef TCPISS_DEBUG
2220 printf("ISS %08x\n", tcp_iss);
2221 #endif
2222 }
2223 }
2224
2225 if (tcp_compat_42) {
2226 /*
2227 * Limit it to the positive range for really old TCP
2228 * implementations.
2229 * Just AND off the top bit instead of checking if
2230 * is set first - saves a branch 50% of the time.
2231 */
2232 tcp_iss &= 0x7fffffff; /* XXX */
2233 }
2234
2235 return (tcp_iss);
2236 }
2237
2238 #if defined(IPSEC) || defined(FAST_IPSEC)
2239 /* compute ESP/AH header size for TCP, including outer IP header. */
2240 size_t
2241 ipsec4_hdrsiz_tcp(struct tcpcb *tp)
2242 {
2243 struct inpcb *inp;
2244 size_t hdrsiz;
2245
2246 /* XXX mapped addr case (tp->t_in6pcb) */
2247 if (!tp || !tp->t_template || !(inp = tp->t_inpcb))
2248 return 0;
2249 switch (tp->t_family) {
2250 case AF_INET:
2251 /* XXX: should use currect direction. */
2252 hdrsiz = ipsec4_hdrsiz(tp->t_template, IPSEC_DIR_OUTBOUND, inp);
2253 break;
2254 default:
2255 hdrsiz = 0;
2256 break;
2257 }
2258
2259 return hdrsiz;
2260 }
2261
2262 #ifdef INET6
2263 size_t
2264 ipsec6_hdrsiz_tcp(struct tcpcb *tp)
2265 {
2266 struct in6pcb *in6p;
2267 size_t hdrsiz;
2268
2269 if (!tp || !tp->t_template || !(in6p = tp->t_in6pcb))
2270 return 0;
2271 switch (tp->t_family) {
2272 case AF_INET6:
2273 /* XXX: should use currect direction. */
2274 hdrsiz = ipsec6_hdrsiz(tp->t_template, IPSEC_DIR_OUTBOUND, in6p);
2275 break;
2276 case AF_INET:
2277 /* mapped address case - tricky */
2278 default:
2279 hdrsiz = 0;
2280 break;
2281 }
2282
2283 return hdrsiz;
2284 }
2285 #endif
2286 #endif /*IPSEC*/
2287
2288 /*
2289 * Determine the length of the TCP options for this connection.
2290 *
2291 * XXX: What do we do for SACK, when we add that? Just reserve
2292 * all of the space? Otherwise we can't exactly be incrementing
2293 * cwnd by an amount that varies depending on the amount we last
2294 * had to SACK!
2295 */
2296
2297 u_int
2298 tcp_optlen(struct tcpcb *tp)
2299 {
2300 u_int optlen;
2301
2302 optlen = 0;
2303 if ((tp->t_flags & (TF_REQ_TSTMP|TF_RCVD_TSTMP|TF_NOOPT)) ==
2304 (TF_REQ_TSTMP | TF_RCVD_TSTMP))
2305 optlen += TCPOLEN_TSTAMP_APPA;
2306
2307 #ifdef TCP_SIGNATURE
2308 if (tp->t_flags & TF_SIGNATURE)
2309 optlen += TCPOLEN_SIGNATURE + 2;
2310 #endif /* TCP_SIGNATURE */
2311
2312 return optlen;
2313 }
2314
2315 u_int
2316 tcp_hdrsz(struct tcpcb *tp)
2317 {
2318 u_int hlen;
2319
2320 switch (tp->t_family) {
2321 #ifdef INET6
2322 case AF_INET6:
2323 hlen = sizeof(struct ip6_hdr);
2324 break;
2325 #endif
2326 case AF_INET:
2327 hlen = sizeof(struct ip);
2328 break;
2329 default:
2330 hlen = 0;
2331 break;
2332 }
2333 hlen += sizeof(struct tcphdr);
2334
2335 if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
2336 (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP)
2337 hlen += TCPOLEN_TSTAMP_APPA;
2338 #ifdef TCP_SIGNATURE
2339 if (tp->t_flags & TF_SIGNATURE)
2340 hlen += TCPOLEN_SIGLEN;
2341 #endif
2342 return hlen;
2343 }
2344