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