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