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