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