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