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