2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
24 * The Regents of the University of California. All rights reserved.
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 * 3. All advertising materials mentioning features or use of this software
35 * must display the following acknowledgement:
36 * This product includes software developed by the University of
37 * California, Berkeley and its contributors.
38 * 4. Neither the name of the University nor the names of its contributors
39 * may be used to endorse or promote products derived from this software
40 * without specific prior written permission.
42 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * @(#)tcp_subr.c 8.2 (Berkeley) 5/24/95
55 * $FreeBSD: src/sys/netinet/tcp_subr.c,v 1.73.2.22 2001/08/22 00:59:12 silby Exp $
59 #include <sys/param.h>
60 #include <sys/systm.h>
61 #include <sys/callout.h>
62 #include <sys/kernel.h>
63 #include <sys/sysctl.h>
64 #include <sys/malloc.h>
67 #include <sys/domain.h>
70 #include <sys/socket.h>
71 #include <sys/socketvar.h>
72 #include <sys/protosw.h>
73 #include <sys/random.h>
74 #include <sys/syslog.h>
78 #include <net/route.h>
82 #include <netinet/in.h>
83 #include <netinet/in_systm.h>
84 #include <netinet/ip.h>
86 #include <netinet/ip6.h>
88 #include <netinet/in_pcb.h>
90 #include <netinet6/in6_pcb.h>
92 #include <netinet/in_var.h>
93 #include <netinet/ip_var.h>
95 #include <netinet6/ip6_var.h>
97 #include <netinet/tcp.h>
98 #include <netinet/tcp_fsm.h>
99 #include <netinet/tcp_seq.h>
100 #include <netinet/tcp_timer.h>
101 #include <netinet/tcp_var.h>
103 #include <netinet6/tcp6_var.h>
105 #include <netinet/tcpip.h>
107 #include <netinet/tcp_debug.h>
109 #include <netinet6/ip6protosw.h>
112 #include <netinet6/ipsec.h>
114 #include <netinet6/ipsec6.h>
119 #include <sys/kdebug.h>
121 #define DBG_FNC_TCP_CLOSE NETDBG_CODE(DBG_NETTCP, ((5 << 8) | 2))
124 /* temporary: for testing */
126 extern int ipsec_bypass
;
129 int tcp_mssdflt
= TCP_MSS
;
130 SYSCTL_INT(_net_inet_tcp
, TCPCTL_MSSDFLT
, mssdflt
, CTLFLAG_RW
,
131 &tcp_mssdflt
, 0, "Default TCP Maximum Segment Size");
134 int tcp_v6mssdflt
= TCP6_MSS
;
135 SYSCTL_INT(_net_inet_tcp
, TCPCTL_V6MSSDFLT
, v6mssdflt
,
136 CTLFLAG_RW
, &tcp_v6mssdflt
, 0,
137 "Default TCP Maximum Segment Size for IPv6");
140 static int tcp_do_rfc1323
= 1;
141 SYSCTL_INT(_net_inet_tcp
, TCPCTL_DO_RFC1323
, rfc1323
, CTLFLAG_RW
,
142 &tcp_do_rfc1323
, 0, "Enable rfc1323 (high performance TCP) extensions");
144 static int tcp_do_rfc1644
= 0;
145 SYSCTL_INT(_net_inet_tcp
, TCPCTL_DO_RFC1644
, rfc1644
, CTLFLAG_RW
,
146 &tcp_do_rfc1644
, 0, "Enable rfc1644 (TTCP) extensions");
148 static int tcp_tcbhashsize
= 0;
149 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, tcbhashsize
, CTLFLAG_RD
,
150 &tcp_tcbhashsize
, 0, "Size of TCP control-block hashtable");
152 static int do_tcpdrain
= 1;
153 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, do_tcpdrain
, CTLFLAG_RW
, &do_tcpdrain
, 0,
154 "Enable tcp_drain routine for extra help when low on mbufs");
156 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, pcbcount
, CTLFLAG_RD
,
157 &tcbinfo
.ipi_count
, 0, "Number of active PCBs");
159 static int icmp_may_rst
= 1;
160 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, icmp_may_rst
, CTLFLAG_RW
, &icmp_may_rst
, 0,
161 "Certain ICMP unreachable messages may abort connections in SYN_SENT");
163 static int tcp_strict_rfc1948
= 0;
164 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, strict_rfc1948
, CTLFLAG_RW
,
165 &tcp_strict_rfc1948
, 0, "Determines if RFC1948 is followed exactly");
167 static int tcp_isn_reseed_interval
= 0;
168 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, isn_reseed_interval
, CTLFLAG_RW
,
169 &tcp_isn_reseed_interval
, 0, "Seconds between reseeding of ISN secret");
171 static void tcp_cleartaocache
__P((void));
172 static void tcp_notify
__P((struct inpcb
*, int));
175 * Target size of TCP PCB hash tables. Must be a power of two.
177 * Note that this can be overridden by the kernel environment
178 * variable net.inet.tcp.tcbhashsize
181 #define TCBHASHSIZE 4096
185 * This is the actual shape of what we allocate using the zone
186 * allocator. Doing it this way allows us to protect both structures
187 * using the same generation count, and also eliminates the overhead
188 * of allocating tcpcbs separately. By hiding the structure here,
189 * we avoid changing most of the rest of the code (although it needs
190 * to be changed, eventually, for greater efficiency).
193 #define ALIGNM1 (ALIGNMENT - 1)
197 char align
[(sizeof(struct inpcb
) + ALIGNM1
) & ~ALIGNM1
];
201 struct callout inp_tp_rexmt
, inp_tp_persist
, inp_tp_keep
, inp_tp_2msl
;
202 struct callout inp_tp_delack
;
208 static struct tcpcb dummy_tcb
;
211 extern struct inpcbhead time_wait_slots
[];
212 extern int cur_tw_slot
;
213 extern u_long
*delack_bitmask
;
216 int get_inpcb_str_size()
218 return sizeof(struct inpcb
);
222 int get_tcp_str_size()
224 return sizeof(struct tcpcb
);
227 int tcp_freeq
__P((struct tcpcb
*tp
));
236 int hashsize
= TCBHASHSIZE
;
243 tcp_delacktime
= TCPTV_DELACK
;
244 tcp_keepinit
= TCPTV_KEEP_INIT
;
245 tcp_keepidle
= TCPTV_KEEP_IDLE
;
246 tcp_keepintvl
= TCPTV_KEEPINTVL
;
247 tcp_maxpersistidle
= TCPTV_KEEP_IDLE
;
251 tcbinfo
.listhead
= &tcb
;
253 TUNABLE_INT_FETCH("net.inet.tcp.tcbhashsize", &hashsize
);
255 if (!powerof2(hashsize
)) {
256 printf("WARNING: TCB hash size not a power of 2\n");
257 hashsize
= 512; /* safe default */
259 tcp_tcbhashsize
= hashsize
;
260 tcbinfo
.hashsize
= hashsize
;
261 tcbinfo
.hashbase
= hashinit(hashsize
, M_PCB
, &tcbinfo
.hashmask
);
262 tcbinfo
.porthashbase
= hashinit(hashsize
, M_PCB
,
263 &tcbinfo
.porthashmask
);
265 str_size
= (vm_size_t
) sizeof(struct inp_tp
);
266 tcbinfo
.ipi_zone
= (void *) zinit(str_size
, 120000*str_size
, 8192, "tcpcb");
268 tcbinfo
.ipi_zone
= zinit("tcpcb", sizeof(struct inp_tp
), maxsockets
,
272 #define TCP_MINPROTOHDR (sizeof(struct ip6_hdr) + sizeof(struct tcphdr))
274 #define TCP_MINPROTOHDR (sizeof(struct tcpiphdr))
276 if (max_protohdr
< TCP_MINPROTOHDR
)
277 max_protohdr
= TCP_MINPROTOHDR
;
278 if (max_linkhdr
+ TCP_MINPROTOHDR
> MHLEN
)
280 #undef TCP_MINPROTOHDR
281 tcbinfo
.last_pcb
= 0;
282 dummy_tcb
.t_state
= TCP_NSTATES
;
283 dummy_tcb
.t_flags
= 0;
284 tcbinfo
.dummy_cb
= (caddr_t
) &dummy_tcb
;
285 in_pcb_nat_init(&tcbinfo
, AF_INET
, IPPROTO_TCP
, SOCK_STREAM
);
287 delack_bitmask
= _MALLOC((4 * hashsize
)/32, M_PCB
, M_WAITOK
);
288 if (delack_bitmask
== 0)
289 panic("Delack Memory");
291 for (i
=0; i
< (tcbinfo
.hashsize
/ 32); i
++)
292 delack_bitmask
[i
] = 0;
294 for (i
=0; i
< N_TIME_WAIT_SLOTS
; i
++) {
295 LIST_INIT(&time_wait_slots
[i
]);
300 * Fill in the IP and TCP headers for an outgoing packet, given the tcpcb.
301 * tcp_template used to store this data in mbufs, but we now recopy it out
302 * of the tcpcb each time to conserve mbufs.
305 tcp_fillheaders(tp
, ip_ptr
, tcp_ptr
)
310 struct inpcb
*inp
= tp
->t_inpcb
;
311 struct tcphdr
*tcp_hdr
= (struct tcphdr
*)tcp_ptr
;
314 if ((inp
->inp_vflag
& INP_IPV6
) != 0) {
317 ip6
= (struct ip6_hdr
*)ip_ptr
;
318 ip6
->ip6_flow
= (ip6
->ip6_flow
& ~IPV6_FLOWINFO_MASK
) |
319 (inp
->in6p_flowinfo
& IPV6_FLOWINFO_MASK
);
320 ip6
->ip6_vfc
= (ip6
->ip6_vfc
& ~IPV6_VERSION_MASK
) |
321 (IPV6_VERSION
& IPV6_VERSION_MASK
);
322 ip6
->ip6_nxt
= IPPROTO_TCP
;
323 ip6
->ip6_plen
= sizeof(struct tcphdr
);
324 ip6
->ip6_src
= inp
->in6p_laddr
;
325 ip6
->ip6_dst
= inp
->in6p_faddr
;
330 struct ip
*ip
= (struct ip
*) ip_ptr
;
332 ip
->ip_vhl
= IP_VHL_BORING
;
339 ip
->ip_p
= IPPROTO_TCP
;
340 ip
->ip_src
= inp
->inp_laddr
;
341 ip
->ip_dst
= inp
->inp_faddr
;
342 tcp_hdr
->th_sum
= in_pseudo(ip
->ip_src
.s_addr
, ip
->ip_dst
.s_addr
,
343 htons(sizeof(struct tcphdr
) + IPPROTO_TCP
));
346 tcp_hdr
->th_sport
= inp
->inp_lport
;
347 tcp_hdr
->th_dport
= inp
->inp_fport
;
352 tcp_hdr
->th_flags
= 0;
358 * Create template to be used to send tcp packets on a connection.
359 * Allocates an mbuf and fills in a skeletal tcp/ip header. The only
360 * use for this function is in keepalives, which use tcp_respond.
369 m
= m_get(M_DONTWAIT
, MT_HEADER
);
372 m
->m_len
= sizeof(struct tcptemp
);
373 n
= mtod(m
, struct tcptemp
*);
375 tcp_fillheaders(tp
, (void *)&n
->tt_ipgen
, (void *)&n
->tt_t
);
380 * Send a single message to the TCP at address specified by
381 * the given TCP/IP header. If m == 0, then we make a copy
382 * of the tcpiphdr at ti and send directly to the addressed host.
383 * This is used to force keep alive messages out using the TCP
384 * template for a connection. If flags are given then we send
385 * a message back to the TCP which originated the * segment ti,
386 * and discard the mbuf containing it and any other attached mbufs.
388 * In any case the ack and sequence number of the transmitted
389 * segment are as specified by the parameters.
391 * NOTE: If m != NULL, then ti must point to *inside* the mbuf.
394 tcp_respond(tp
, ipgen
, th
, m
, ack
, seq
, flags
)
397 register struct tcphdr
*th
;
398 register struct mbuf
*m
;
404 struct route
*ro
= 0;
409 struct route_in6
*ro6
= 0;
410 struct route_in6 sro6
;
417 isipv6
= IP_VHL_V(((struct ip
*)ipgen
)->ip_vhl
) == 6;
423 if (!(flags
& TH_RST
)) {
424 win
= sbspace(&tp
->t_inpcb
->inp_socket
->so_rcv
);
425 if (win
> (long)TCP_MAXWIN
<< tp
->rcv_scale
)
426 win
= (long)TCP_MAXWIN
<< tp
->rcv_scale
;
430 ro6
= &tp
->t_inpcb
->in6p_route
;
433 ro
= &tp
->t_inpcb
->inp_route
;
438 bzero(ro6
, sizeof *ro6
);
443 bzero(ro
, sizeof *ro
);
447 m
= m_gethdr(M_DONTWAIT
, MT_HEADER
);
451 m
->m_data
+= max_linkhdr
;
454 bcopy((caddr_t
)ip6
, mtod(m
, caddr_t
),
455 sizeof(struct ip6_hdr
));
456 ip6
= mtod(m
, struct ip6_hdr
*);
457 nth
= (struct tcphdr
*)(ip6
+ 1);
461 bcopy((caddr_t
)ip
, mtod(m
, caddr_t
), sizeof(struct ip
));
462 ip
= mtod(m
, struct ip
*);
463 nth
= (struct tcphdr
*)(ip
+ 1);
465 bcopy((caddr_t
)th
, (caddr_t
)nth
, sizeof(struct tcphdr
));
470 m
->m_data
= (caddr_t
)ipgen
;
471 /* m_len is set later */
473 #define xchg(a,b,type) { type t; t=a; a=b; b=t; }
476 xchg(ip6
->ip6_dst
, ip6
->ip6_src
, struct in6_addr
);
477 nth
= (struct tcphdr
*)(ip6
+ 1);
481 xchg(ip
->ip_dst
.s_addr
, ip
->ip_src
.s_addr
, n_long
);
482 nth
= (struct tcphdr
*)(ip
+ 1);
486 * this is usually a case when an extension header
487 * exists between the IPv6 header and the
490 nth
->th_sport
= th
->th_sport
;
491 nth
->th_dport
= th
->th_dport
;
493 xchg(nth
->th_dport
, nth
->th_sport
, n_short
);
498 ip6
->ip6_plen
= htons((u_short
)(sizeof (struct tcphdr
) +
500 tlen
+= sizeof (struct ip6_hdr
) + sizeof (struct tcphdr
);
504 tlen
+= sizeof (struct tcpiphdr
);
506 ip
->ip_ttl
= ip_defttl
;
509 m
->m_pkthdr
.len
= tlen
;
510 m
->m_pkthdr
.rcvif
= (struct ifnet
*) 0;
511 nth
->th_seq
= htonl(seq
);
512 nth
->th_ack
= htonl(ack
);
514 nth
->th_off
= sizeof (struct tcphdr
) >> 2;
515 nth
->th_flags
= flags
;
517 nth
->th_win
= htons((u_short
) (win
>> tp
->rcv_scale
));
519 nth
->th_win
= htons((u_short
)win
);
524 nth
->th_sum
= in6_cksum(m
, IPPROTO_TCP
,
525 sizeof(struct ip6_hdr
),
526 tlen
- sizeof(struct ip6_hdr
));
527 ip6
->ip6_hlim
= in6_selecthlim(tp
? tp
->t_inpcb
: NULL
,
534 nth
->th_sum
= in_pseudo(ip
->ip_src
.s_addr
, ip
->ip_dst
.s_addr
,
535 htons((u_short
)(tlen
- sizeof(struct ip
) + ip
->ip_p
)));
536 m
->m_pkthdr
.csum_flags
= CSUM_TCP
;
537 m
->m_pkthdr
.csum_data
= offsetof(struct tcphdr
, th_sum
);
540 if (tp
== NULL
|| (tp
->t_inpcb
->inp_socket
->so_options
& SO_DEBUG
))
541 tcp_trace(TA_OUTPUT
, 0, tp
, mtod(m
, void *), th
, 0);
544 if (ipsec_bypass
== 0 && ipsec_setsocket(m
, tp
? tp
->t_inpcb
->inp_socket
: NULL
) != 0) {
551 (void)ip6_output(m
, NULL
, ro6
, ipflags
, NULL
, NULL
);
552 if (ro6
== &sro6
&& ro6
->ro_rt
) {
559 (void) ip_output(m
, NULL
, ro
, ipflags
, NULL
);
560 if (ro
== &sro
&& ro
->ro_rt
) {
568 * Create a new TCP control block, making an
569 * empty reassembly queue and hooking it to the argument
570 * protocol control block. The `inp' parameter must have
571 * come from the zone allocator set up in tcp_init().
578 register struct tcpcb
*tp
;
579 register struct socket
*so
= inp
->inp_socket
;
581 int isipv6
= (inp
->inp_vflag
& INP_IPV6
) != 0;
584 if (so
->cached_in_sock_layer
== 0) {
585 it
= (struct inp_tp
*)inp
;
589 tp
= (struct tcpcb
*) inp
->inp_saved_ppcb
;
591 bzero((char *) tp
, sizeof(struct tcpcb
));
592 LIST_INIT(&tp
->t_segq
);
593 tp
->t_maxseg
= tp
->t_maxopd
=
595 isipv6
? tcp_v6mssdflt
:
600 /* Set up our timeouts. */
601 callout_init(tp
->tt_rexmt
= &it
->inp_tp_rexmt
);
602 callout_init(tp
->tt_persist
= &it
->inp_tp_persist
);
603 callout_init(tp
->tt_keep
= &it
->inp_tp_keep
);
604 callout_init(tp
->tt_2msl
= &it
->inp_tp_2msl
);
605 callout_init(tp
->tt_delack
= &it
->inp_tp_delack
);
609 tp
->t_flags
= (TF_REQ_SCALE
|TF_REQ_TSTMP
);
611 tp
->t_flags
|= TF_REQ_CC
;
612 tp
->t_inpcb
= inp
; /* XXX */
614 * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no
615 * rtt estimate. Set rttvar so that srtt + 4 * rttvar gives
616 * reasonable initial retransmit time.
618 tp
->t_srtt
= TCPTV_SRTTBASE
;
619 tp
->t_rttvar
= ((TCPTV_RTOBASE
- TCPTV_SRTTBASE
) << TCP_RTTVAR_SHIFT
) / 4;
620 tp
->t_rttmin
= TCPTV_MIN
;
621 tp
->t_rxtcur
= TCPTV_RTOBASE
;
622 tp
->snd_cwnd
= TCP_MAXWIN
<< TCP_MAX_WINSHIFT
;
623 tp
->snd_ssthresh
= TCP_MAXWIN
<< TCP_MAX_WINSHIFT
;
625 * IPv4 TTL initialization is necessary for an IPv6 socket as well,
626 * because the socket may be bound to an IPv6 wildcard address,
627 * which may match an IPv4-mapped IPv6 address.
629 inp
->inp_ip_ttl
= ip_defttl
;
630 inp
->inp_ppcb
= (caddr_t
)tp
;
631 return (tp
); /* XXX */
635 * Drop a TCP connection, reporting
636 * the specified error. If connection is synchronized,
637 * then send a RST to peer.
641 register struct tcpcb
*tp
;
644 struct socket
*so
= tp
->t_inpcb
->inp_socket
;
649 case TCPS_ESTABLISHED
:
650 case TCPS_FIN_WAIT_1
:
652 case TCPS_CLOSE_WAIT
:
658 if (TCPS_HAVERCVDSYN(tp
->t_state
)) {
659 tp
->t_state
= TCPS_CLOSED
;
660 (void) tcp_output(tp
);
661 tcpstat
.tcps_drops
++;
663 tcpstat
.tcps_conndrops
++;
664 if (errno
== ETIMEDOUT
&& tp
->t_softerror
)
665 errno
= tp
->t_softerror
;
666 so
->so_error
= errno
;
667 return (tcp_close(tp
));
671 * Close a TCP control block:
672 * discard all space held by the tcp
673 * discard internet protocol block
674 * wake up any sleepers
678 register struct tcpcb
*tp
;
680 register struct tseg_qent
*q
;
681 struct inpcb
*inp
= tp
->t_inpcb
;
682 struct socket
*so
= inp
->inp_socket
;
684 int isipv6
= (inp
->inp_vflag
& INP_IPV6
) != 0;
686 register struct rtentry
*rt
;
691 * Make sure that all of our timers are stopped before we
694 callout_stop(tp
->tt_rexmt
);
695 callout_stop(tp
->tt_persist
);
696 callout_stop(tp
->tt_keep
);
697 callout_stop(tp
->tt_2msl
);
698 callout_stop(tp
->tt_delack
);
701 KERNEL_DEBUG(DBG_FNC_TCP_CLOSE
| DBG_FUNC_START
, tp
,0,0,0,0);
704 case TCPS_ESTABLISHED
:
705 case TCPS_FIN_WAIT_1
:
707 case TCPS_CLOSE_WAIT
:
714 * If we got enough samples through the srtt filter,
715 * save the rtt and rttvar in the routing entry.
716 * 'Enough' is arbitrarily defined as the 16 samples.
717 * 16 samples is enough for the srtt filter to converge
718 * to within 5% of the correct value; fewer samples and
719 * we could save a very bogus rtt.
721 * Don't update the default route's characteristics and don't
722 * update anything that the user "locked".
724 if (tp
->t_rttupdated
>= 16) {
725 register u_long i
= 0;
728 struct sockaddr_in6
*sin6
;
730 if ((rt
= inp
->in6p_route
.ro_rt
) == NULL
)
732 sin6
= (struct sockaddr_in6
*)rt_key(rt
);
733 if (IN6_IS_ADDR_UNSPECIFIED(&sin6
->sin6_addr
))
738 if ((rt
= inp
->inp_route
.ro_rt
) == NULL
||
739 ((struct sockaddr_in
*)rt_key(rt
))->sin_addr
.s_addr
743 if ((rt
->rt_rmx
.rmx_locks
& RTV_RTT
) == 0) {
745 (RTM_RTTUNIT
/ (PR_SLOWHZ
* TCP_RTT_SCALE
));
746 if (rt
->rt_rmx
.rmx_rtt
&& i
)
748 * filter this update to half the old & half
749 * the new values, converting scale.
750 * See route.h and tcp_var.h for a
751 * description of the scaling constants.
754 (rt
->rt_rmx
.rmx_rtt
+ i
) / 2;
756 rt
->rt_rmx
.rmx_rtt
= i
;
757 tcpstat
.tcps_cachedrtt
++;
759 if ((rt
->rt_rmx
.rmx_locks
& RTV_RTTVAR
) == 0) {
761 (RTM_RTTUNIT
/ (PR_SLOWHZ
* TCP_RTTVAR_SCALE
));
762 if (rt
->rt_rmx
.rmx_rttvar
&& i
)
763 rt
->rt_rmx
.rmx_rttvar
=
764 (rt
->rt_rmx
.rmx_rttvar
+ i
) / 2;
766 rt
->rt_rmx
.rmx_rttvar
= i
;
767 tcpstat
.tcps_cachedrttvar
++;
770 * The old comment here said:
771 * update the pipelimit (ssthresh) if it has been updated
772 * already or if a pipesize was specified & the threshhold
773 * got below half the pipesize. I.e., wait for bad news
774 * before we start updating, then update on both good
777 * But we want to save the ssthresh even if no pipesize is
778 * specified explicitly in the route, because such
779 * connections still have an implicit pipesize specified
780 * by the global tcp_sendspace. In the absence of a reliable
781 * way to calculate the pipesize, it will have to do.
783 i
= tp
->snd_ssthresh
;
784 if (rt
->rt_rmx
.rmx_sendpipe
!= 0)
785 dosavessthresh
= (i
< rt
->rt_rmx
.rmx_sendpipe
/ 2);
787 dosavessthresh
= (i
< so
->so_snd
.sb_hiwat
/ 2);
788 if (((rt
->rt_rmx
.rmx_locks
& RTV_SSTHRESH
) == 0 &&
789 i
!= 0 && rt
->rt_rmx
.rmx_ssthresh
!= 0)
792 * convert the limit from user data bytes to
793 * packets then to packet data bytes.
795 i
= (i
+ tp
->t_maxseg
/ 2) / tp
->t_maxseg
;
798 i
*= (u_long
)(tp
->t_maxseg
+
800 (isipv6
? sizeof (struct ip6_hdr
) +
801 sizeof (struct tcphdr
) :
803 sizeof (struct tcpiphdr
)
808 if (rt
->rt_rmx
.rmx_ssthresh
)
809 rt
->rt_rmx
.rmx_ssthresh
=
810 (rt
->rt_rmx
.rmx_ssthresh
+ i
) / 2;
812 rt
->rt_rmx
.rmx_ssthresh
= i
;
813 tcpstat
.tcps_cachedssthresh
++;
816 rt
= inp
->inp_route
.ro_rt
;
819 * mark route for deletion if no information is
822 if ((tp
->t_flags
& TF_LQ_OVERFLOW
) &&
823 ((rt
->rt_rmx
.rmx_locks
& RTV_RTT
) == 0)){
824 if (rt
->rt_rmx
.rmx_rtt
== 0)
825 rt
->rt_flags
|= RTF_DELCLONE
;
829 /* free the reassembly queue, if any */
830 (void) tcp_freeq(tp
);
833 if (so
->cached_in_sock_layer
)
834 inp
->inp_saved_ppcb
= (caddr_t
) tp
;
837 inp
->inp_ppcb
= NULL
;
838 soisdisconnected(so
);
840 if (INP_CHECK_SOCKAF(so
, AF_INET6
))
845 tcpstat
.tcps_closed
++;
846 KERNEL_DEBUG(DBG_FNC_TCP_CLOSE
| DBG_FUNC_END
, tcpstat
.tcps_closed
,0,0,0,0);
847 return ((struct tcpcb
*)0);
855 register struct tseg_qent
*q
;
858 while((q
= LIST_FIRST(&tp
->t_segq
)) != NULL
) {
859 LIST_REMOVE(q
, tqe_q
);
874 struct tseg_qent
*te
;
877 * Walk the tcpbs, if existing, and flush the reassembly queue,
879 * XXX: The "Net/3" implementation doesn't imply that the TCP
880 * reassembly queue should be flushed, but in a situation
881 * where we're really low on mbufs, this is potentially
884 for (inpb
= LIST_FIRST(tcbinfo
.listhead
); inpb
;
885 inpb
= LIST_NEXT(inpb
, inp_list
)) {
886 if ((tcpb
= intotcpcb(inpb
))) {
887 while ((te
= LIST_FIRST(&tcpb
->t_segq
))
889 LIST_REMOVE(te
, tqe_q
);
900 * Notify a tcp user of an asynchronous error;
901 * store error as soft error, but wake up user
902 * (for now, won't do anything until can select for soft error).
904 * Do not wake up user since there currently is no mechanism for
905 * reporting soft errors (yet - a kqueue filter may be added).
908 tcp_notify(inp
, error
)
912 struct tcpcb
*tp
= (struct tcpcb
*)inp
->inp_ppcb
;
915 * Ignore some errors if we are hooked up.
916 * If connection hasn't completed, has retransmitted several times,
917 * and receives a second error, give up now. This is better
918 * than waiting a long time to establish a connection that
919 * can never complete.
921 if (tp
->t_state
== TCPS_ESTABLISHED
&&
922 (error
== EHOSTUNREACH
|| error
== ENETUNREACH
||
923 error
== EHOSTDOWN
)) {
925 } else if (tp
->t_state
< TCPS_ESTABLISHED
&& tp
->t_rxtshift
> 3 &&
929 tp
->t_softerror
= error
;
931 wakeup((caddr_t
) &so
->so_timeo
);
938 tcp_pcblist SYSCTL_HANDLER_ARGS
941 struct inpcb
*inp
, **inp_list
;
946 * The process of preparing the TCB list is too time-consuming and
947 * resource-intensive to repeat twice on every request.
949 if (req
->oldptr
== 0) {
950 n
= tcbinfo
.ipi_count
;
951 req
->oldidx
= 2 * (sizeof xig
)
952 + (n
+ n
/8) * sizeof(struct xtcpcb
);
956 if (req
->newptr
!= 0)
960 * OK, now we're committed to doing something.
963 gencnt
= tcbinfo
.ipi_gencnt
;
964 n
= tcbinfo
.ipi_count
;
967 xig
.xig_len
= sizeof xig
;
969 xig
.xig_gen
= gencnt
;
970 xig
.xig_sogen
= so_gencnt
;
971 error
= SYSCTL_OUT(req
, &xig
, sizeof xig
);
975 * We are done if there is no pcb
980 inp_list
= _MALLOC(n
* sizeof *inp_list
, M_TEMP
, M_WAITOK
);
985 for (inp
= LIST_FIRST(tcbinfo
.listhead
), i
= 0; inp
&& i
< n
;
986 inp
= LIST_NEXT(inp
, inp_list
)) {
988 if (inp
->inp_gencnt
<= gencnt
)
990 if (inp
->inp_gencnt
<= gencnt
&& !prison_xinpcb(req
->p
, inp
))
998 for (i
= 0; i
< n
; i
++) {
1000 if (inp
->inp_gencnt
<= gencnt
) {
1003 xt
.xt_len
= sizeof xt
;
1004 /* XXX should avoid extra copy */
1005 bcopy(inp
, &xt
.xt_inp
, sizeof *inp
);
1006 inp_ppcb
= inp
->inp_ppcb
;
1007 if (inp_ppcb
!= NULL
)
1008 bcopy(inp_ppcb
, &xt
.xt_tp
, sizeof xt
.xt_tp
);
1010 bzero((char *) &xt
.xt_tp
, sizeof xt
.xt_tp
);
1011 if (inp
->inp_socket
)
1012 sotoxsocket(inp
->inp_socket
, &xt
.xt_socket
);
1013 error
= SYSCTL_OUT(req
, &xt
, sizeof xt
);
1018 * Give the user an updated idea of our state.
1019 * If the generation differs from what we told
1020 * her before, she knows that something happened
1021 * while we were processing this request, and it
1022 * might be necessary to retry.
1025 xig
.xig_gen
= tcbinfo
.ipi_gencnt
;
1026 xig
.xig_sogen
= so_gencnt
;
1027 xig
.xig_count
= tcbinfo
.ipi_count
;
1029 error
= SYSCTL_OUT(req
, &xig
, sizeof xig
);
1031 FREE(inp_list
, M_TEMP
);
1035 SYSCTL_PROC(_net_inet_tcp
, TCPCTL_PCBLIST
, pcblist
, CTLFLAG_RD
, 0, 0,
1036 tcp_pcblist
, "S,xtcpcb", "List of active TCP connections");
1040 tcp_getcred(SYSCTL_HANDLER_ARGS
)
1042 struct sockaddr_in addrs
[2];
1046 error
= suser(req
->p
);
1049 error
= SYSCTL_IN(req
, addrs
, sizeof(addrs
));
1053 inp
= in_pcblookup_hash(&tcbinfo
, addrs
[1].sin_addr
, addrs
[1].sin_port
,
1054 addrs
[0].sin_addr
, addrs
[0].sin_port
, 0, NULL
);
1055 if (inp
== NULL
|| inp
->inp_socket
== NULL
) {
1059 error
= SYSCTL_OUT(req
, inp
->inp_socket
->so_cred
, sizeof(struct ucred
));
1065 SYSCTL_PROC(_net_inet_tcp
, OID_AUTO
, getcred
, CTLTYPE_OPAQUE
|CTLFLAG_RW
,
1066 0, 0, tcp_getcred
, "S,ucred", "Get the ucred of a TCP connection");
1070 tcp6_getcred(SYSCTL_HANDLER_ARGS
)
1072 struct sockaddr_in6 addrs
[2];
1074 int error
, s
, mapped
= 0;
1076 error
= suser(req
->p
);
1079 error
= SYSCTL_IN(req
, addrs
, sizeof(addrs
));
1082 if (IN6_IS_ADDR_V4MAPPED(&addrs
[0].sin6_addr
)) {
1083 if (IN6_IS_ADDR_V4MAPPED(&addrs
[1].sin6_addr
))
1090 inp
= in_pcblookup_hash(&tcbinfo
,
1091 *(struct in_addr
*)&addrs
[1].sin6_addr
.s6_addr
[12],
1093 *(struct in_addr
*)&addrs
[0].sin6_addr
.s6_addr
[12],
1097 inp
= in6_pcblookup_hash(&tcbinfo
, &addrs
[1].sin6_addr
,
1099 &addrs
[0].sin6_addr
, addrs
[0].sin6_port
,
1101 if (inp
== NULL
|| inp
->inp_socket
== NULL
) {
1105 error
= SYSCTL_OUT(req
, inp
->inp_socket
->so_cred
,
1106 sizeof(struct ucred
));
1112 SYSCTL_PROC(_net_inet6_tcp6
, OID_AUTO
, getcred
, CTLTYPE_OPAQUE
|CTLFLAG_RW
,
1114 tcp6_getcred
, "S,ucred", "Get the ucred of a TCP6 connection");
1116 #endif /* __APPLE__*/
1119 tcp_ctlinput(cmd
, sa
, vip
)
1121 struct sockaddr
*sa
;
1124 struct ip
*ip
= vip
;
1126 struct in_addr faddr
;
1129 void (*notify
) __P((struct inpcb
*, int)) = tcp_notify
;
1133 faddr
= ((struct sockaddr_in
*)sa
)->sin_addr
;
1134 if (sa
->sa_family
!= AF_INET
|| faddr
.s_addr
== INADDR_ANY
)
1137 if (cmd
== PRC_QUENCH
)
1138 notify
= tcp_quench
;
1139 else if (icmp_may_rst
&& (cmd
== PRC_UNREACH_ADMIN_PROHIB
||
1140 cmd
== PRC_UNREACH_PORT
) && ip
)
1141 notify
= tcp_drop_syn_sent
;
1142 else if (cmd
== PRC_MSGSIZE
)
1143 notify
= tcp_mtudisc
;
1144 else if (PRC_IS_REDIRECT(cmd
)) {
1146 notify
= in_rtchange
;
1147 } else if (cmd
== PRC_HOSTDEAD
)
1149 else if ((unsigned)cmd
> PRC_NCMDS
|| inetctlerrmap
[cmd
] == 0)
1153 th
= (struct tcphdr
*)((caddr_t
)ip
1154 + (IP_VHL_HL(ip
->ip_vhl
) << 2));
1155 inp
= in_pcblookup_hash(&tcbinfo
, faddr
, th
->th_dport
,
1156 ip
->ip_src
, th
->th_sport
, 0, NULL
);
1157 if (inp
!= NULL
&& inp
->inp_socket
!= NULL
) {
1158 icmp_seq
= htonl(th
->th_seq
);
1159 tp
= intotcpcb(inp
);
1160 if (SEQ_GEQ(icmp_seq
, tp
->snd_una
) &&
1161 SEQ_LT(icmp_seq
, tp
->snd_max
))
1162 (*notify
)(inp
, inetctlerrmap
[cmd
]);
1166 in_pcbnotifyall(&tcb
, faddr
, inetctlerrmap
[cmd
], notify
);
1171 tcp6_ctlinput(cmd
, sa
, d
)
1173 struct sockaddr
*sa
;
1177 void (*notify
) __P((struct inpcb
*, int)) = tcp_notify
;
1178 struct ip6_hdr
*ip6
;
1180 struct ip6ctlparam
*ip6cp
= NULL
;
1181 const struct sockaddr_in6
*sa6_src
= NULL
;
1183 struct tcp_portonly
{
1188 if (sa
->sa_family
!= AF_INET6
||
1189 sa
->sa_len
!= sizeof(struct sockaddr_in6
))
1192 if (cmd
== PRC_QUENCH
)
1193 notify
= tcp_quench
;
1194 else if (cmd
== PRC_MSGSIZE
)
1195 notify
= tcp_mtudisc
;
1196 else if (!PRC_IS_REDIRECT(cmd
) &&
1197 ((unsigned)cmd
> PRC_NCMDS
|| inet6ctlerrmap
[cmd
] == 0))
1200 /* if the parameter is from icmp6, decode it. */
1202 ip6cp
= (struct ip6ctlparam
*)d
;
1204 ip6
= ip6cp
->ip6c_ip6
;
1205 off
= ip6cp
->ip6c_off
;
1206 sa6_src
= ip6cp
->ip6c_src
;
1210 off
= 0; /* fool gcc */
1216 * XXX: We assume that when IPV6 is non NULL,
1217 * M and OFF are valid.
1220 /* check if we can safely examine src and dst ports */
1221 if (m
->m_pkthdr
.len
< off
+ sizeof(*thp
))
1224 bzero(&th
, sizeof(th
));
1225 m_copydata(m
, off
, sizeof(*thp
), (caddr_t
)&th
);
1227 in6_pcbnotify(&tcb
, sa
, th
.th_dport
,
1228 (struct sockaddr
*)ip6cp
->ip6c_src
,
1229 th
.th_sport
, cmd
, notify
);
1231 in6_pcbnotify(&tcb
, sa
, 0, (struct sockaddr
*)sa6_src
,
1238 * Following is where TCP initial sequence number generation occurs.
1240 * There are two places where we must use initial sequence numbers:
1241 * 1. In SYN-ACK packets.
1242 * 2. In SYN packets.
1244 * The ISNs in SYN-ACK packets have no monotonicity requirement,
1245 * and should be as unpredictable as possible to avoid the possibility
1246 * of spoofing and/or connection hijacking. To satisfy this
1247 * requirement, SYN-ACK ISNs are generated via the arc4random()
1248 * function. If exact RFC 1948 compliance is requested via sysctl,
1249 * these ISNs will be generated just like those in SYN packets.
1251 * The ISNs in SYN packets must be monotonic; TIME_WAIT recycling
1252 * depends on this property. In addition, these ISNs should be
1253 * unguessable so as to prevent connection hijacking. To satisfy
1254 * the requirements of this situation, the algorithm outlined in
1255 * RFC 1948 is used to generate sequence numbers.
1257 * For more information on the theory of operation, please see
1260 * Implementation details:
1262 * Time is based off the system timer, and is corrected so that it
1263 * increases by one megabyte per second. This allows for proper
1264 * recycling on high speed LANs while still leaving over an hour
1267 * Two sysctls control the generation of ISNs:
1269 * net.inet.tcp.isn_reseed_interval controls the number of seconds
1270 * between seeding of isn_secret. This is normally set to zero,
1271 * as reseeding should not be necessary.
1273 * net.inet.tcp.strict_rfc1948 controls whether RFC 1948 is followed
1274 * strictly. When strict compliance is requested, reseeding is
1275 * disabled and SYN-ACKs will be generated in the same manner as
1276 * SYNs. Strict mode is disabled by default.
1280 #define ISN_BYTES_PER_SECOND 1048576
1282 u_char isn_secret
[32];
1283 int isn_last_reseed
;
1290 u_int32_t md5_buffer
[4];
1292 struct timeval time
;
1294 /* Use arc4random for SYN-ACKs when not in exact RFC1948 mode. */
1295 if (((tp
->t_state
== TCPS_LISTEN
) || (tp
->t_state
== TCPS_TIME_WAIT
))
1296 && tcp_strict_rfc1948
== 0)
1300 return arc4random();
1303 /* Seed if this is the first use, reseed if requested. */
1304 if ((isn_last_reseed
== 0) ||
1305 ((tcp_strict_rfc1948
== 0) && (tcp_isn_reseed_interval
> 0) &&
1306 (((u_int
)isn_last_reseed
+ (u_int
)tcp_isn_reseed_interval
*hz
)
1307 < (u_int
)time
.tv_sec
))) {
1309 read_random(&isn_secret
, sizeof(isn_secret
));
1311 read_random_unlimited(&isn_secret
, sizeof(isn_secret
));
1313 isn_last_reseed
= time
.tv_sec
;
1316 /* Compute the md5 hash and return the ISN. */
1318 MD5Update(&isn_ctx
, (u_char
*) &tp
->t_inpcb
->inp_fport
, sizeof(u_short
));
1319 MD5Update(&isn_ctx
, (u_char
*) &tp
->t_inpcb
->inp_lport
, sizeof(u_short
));
1321 if ((tp
->t_inpcb
->inp_vflag
& INP_IPV6
) != 0) {
1322 MD5Update(&isn_ctx
, (u_char
*) &tp
->t_inpcb
->in6p_faddr
,
1323 sizeof(struct in6_addr
));
1324 MD5Update(&isn_ctx
, (u_char
*) &tp
->t_inpcb
->in6p_laddr
,
1325 sizeof(struct in6_addr
));
1329 MD5Update(&isn_ctx
, (u_char
*) &tp
->t_inpcb
->inp_faddr
,
1330 sizeof(struct in_addr
));
1331 MD5Update(&isn_ctx
, (u_char
*) &tp
->t_inpcb
->inp_laddr
,
1332 sizeof(struct in_addr
));
1334 MD5Update(&isn_ctx
, (u_char
*) &isn_secret
, sizeof(isn_secret
));
1335 MD5Final((u_char
*) &md5_buffer
, &isn_ctx
);
1336 new_isn
= (tcp_seq
) md5_buffer
[0];
1337 new_isn
+= time
.tv_sec
* (ISN_BYTES_PER_SECOND
/ hz
);
1342 * When a source quench is received, close congestion window
1343 * to one segment. We will gradually open it again as we proceed.
1346 tcp_quench(inp
, errno
)
1350 struct tcpcb
*tp
= intotcpcb(inp
);
1353 tp
->snd_cwnd
= tp
->t_maxseg
;
1357 * When a specific ICMP unreachable message is received and the
1358 * connection state is SYN-SENT, drop the connection. This behavior
1359 * is controlled by the icmp_may_rst sysctl.
1362 tcp_drop_syn_sent(inp
, errno
)
1366 struct tcpcb
*tp
= intotcpcb(inp
);
1368 if (tp
&& tp
->t_state
== TCPS_SYN_SENT
)
1369 tcp_drop(tp
, errno
);
1373 * When `need fragmentation' ICMP is received, update our idea of the MSS
1374 * based on the new value in the route. Also nudge TCP to send something,
1375 * since we know the packet we just sent was dropped.
1376 * This duplicates some code in the tcp_mss() function in tcp_input.c.
1379 tcp_mtudisc(inp
, errno
)
1383 struct tcpcb
*tp
= intotcpcb(inp
);
1385 struct rmxp_tao
*taop
;
1386 struct socket
*so
= inp
->inp_socket
;
1390 int isipv6
= (tp
->t_inpcb
->inp_vflag
& INP_IPV6
) != 0;
1396 rt
= tcp_rtlookup6(inp
);
1399 rt
= tcp_rtlookup(inp
);
1400 if (!rt
|| !rt
->rt_rmx
.rmx_mtu
) {
1401 tp
->t_maxopd
= tp
->t_maxseg
=
1403 isipv6
? tcp_v6mssdflt
:
1408 taop
= rmx_taop(rt
->rt_rmx
);
1409 offered
= taop
->tao_mssopt
;
1410 mss
= rt
->rt_rmx
.rmx_mtu
-
1413 sizeof(struct ip6_hdr
) + sizeof(struct tcphdr
) :
1415 sizeof(struct tcpiphdr
)
1422 mss
= min(mss
, offered
);
1424 * XXX - The above conditional probably violates the TCP
1425 * spec. The problem is that, since we don't know the
1426 * other end's MSS, we are supposed to use a conservative
1427 * default. But, if we do that, then MTU discovery will
1428 * never actually take place, because the conservative
1429 * default is much less than the MTUs typically seen
1430 * on the Internet today. For the moment, we'll sweep
1431 * this under the carpet.
1433 * The conservative default might not actually be a problem
1434 * if the only case this occurs is when sending an initial
1435 * SYN with options and data to a host we've never talked
1436 * to before. Then, they will reply with an MSS value which
1437 * will get recorded and the new parameters should get
1438 * recomputed. For Further Study.
1440 if (tp
->t_maxopd
<= mss
)
1444 if ((tp
->t_flags
& (TF_REQ_TSTMP
|TF_NOOPT
)) == TF_REQ_TSTMP
&&
1445 (tp
->t_flags
& TF_RCVD_TSTMP
) == TF_RCVD_TSTMP
)
1446 mss
-= TCPOLEN_TSTAMP_APPA
;
1447 if ((tp
->t_flags
& (TF_REQ_CC
|TF_NOOPT
)) == TF_REQ_CC
&&
1448 (tp
->t_flags
& TF_RCVD_CC
) == TF_RCVD_CC
)
1449 mss
-= TCPOLEN_CC_APPA
;
1450 #if (MCLBYTES & (MCLBYTES - 1)) == 0
1452 mss
&= ~(MCLBYTES
-1);
1455 mss
= mss
/ MCLBYTES
* MCLBYTES
;
1457 if (so
->so_snd
.sb_hiwat
< mss
)
1458 mss
= so
->so_snd
.sb_hiwat
;
1462 tcpstat
.tcps_mturesent
++;
1464 tp
->snd_nxt
= tp
->snd_una
;
1470 * Look-up the routing entry to the peer of this inpcb. If no route
1471 * is found and it cannot be allocated the return NULL. This routine
1472 * is called by TCP routines that access the rmx structure and by tcp_mss
1473 * to get the interface MTU.
1482 ro
= &inp
->inp_route
;
1486 if (rt
== NULL
|| !(rt
->rt_flags
& RTF_UP
)) {
1487 /* No route yet, so try to acquire one */
1488 if (inp
->inp_faddr
.s_addr
!= INADDR_ANY
) {
1489 ro
->ro_dst
.sa_family
= AF_INET
;
1490 ro
->ro_dst
.sa_len
= sizeof(struct sockaddr_in
);
1491 ((struct sockaddr_in
*) &ro
->ro_dst
)->sin_addr
=
1505 struct route_in6
*ro6
;
1508 ro6
= &inp
->in6p_route
;
1510 if (rt
== NULL
|| !(rt
->rt_flags
& RTF_UP
)) {
1511 /* No route yet, so try to acquire one */
1512 if (!IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_faddr
)) {
1513 struct sockaddr_in6
*dst6
;
1515 dst6
= (struct sockaddr_in6
*)&ro6
->ro_dst
;
1516 dst6
->sin6_family
= AF_INET6
;
1517 dst6
->sin6_len
= sizeof(*dst6
);
1518 dst6
->sin6_addr
= inp
->in6p_faddr
;
1519 rtalloc((struct route
*)ro6
);
1528 /* compute ESP/AH header size for TCP, including outer IP header. */
1530 ipsec_hdrsiz_tcp(tp
)
1538 struct ip6_hdr
*ip6
= NULL
;
1542 if ((tp
== NULL
) || ((inp
= tp
->t_inpcb
) == NULL
))
1544 MGETHDR(m
, M_DONTWAIT
, MT_DATA
);
1549 if ((inp
->inp_vflag
& INP_IPV6
) != 0) {
1550 ip6
= mtod(m
, struct ip6_hdr
*);
1551 th
= (struct tcphdr
*)(ip6
+ 1);
1552 m
->m_pkthdr
.len
= m
->m_len
=
1553 sizeof(struct ip6_hdr
) + sizeof(struct tcphdr
);
1554 tcp_fillheaders(tp
, ip6
, th
);
1555 hdrsiz
= ipsec6_hdrsiz(m
, IPSEC_DIR_OUTBOUND
, inp
);
1559 ip
= mtod(m
, struct ip
*);
1560 th
= (struct tcphdr
*)(ip
+ 1);
1561 m
->m_pkthdr
.len
= m
->m_len
= sizeof(struct tcpiphdr
);
1562 tcp_fillheaders(tp
, ip
, th
);
1563 hdrsiz
= ipsec4_hdrsiz(m
, IPSEC_DIR_OUTBOUND
, inp
);
1572 * Return a pointer to the cached information about the remote host.
1573 * The cached information is stored in the protocol specific part of
1574 * the route metrics.
1577 tcp_gettaocache(inp
)
1583 if ((inp
->inp_vflag
& INP_IPV6
) != 0)
1584 rt
= tcp_rtlookup6(inp
);
1587 rt
= tcp_rtlookup(inp
);
1589 /* Make sure this is a host route and is up. */
1591 (rt
->rt_flags
& (RTF_UP
|RTF_HOST
)) != (RTF_UP
|RTF_HOST
))
1594 return rmx_taop(rt
->rt_rmx
);
1598 * Clear all the TAO cache entries, called from tcp_init.
1601 * This routine is just an empty one, because we assume that the routing
1602 * routing tables are initialized at the same time when TCP, so there is
1603 * nothing in the cache left over.