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