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