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