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/kauth.h>
71 #include <sys/socket.h>
72 #include <sys/socketvar.h>
73 #include <sys/protosw.h>
74 #include <sys/random.h>
75 #include <sys/syslog.h>
76 #include <kern/locks.h>
80 #include <net/route.h>
84 #include <netinet/in.h>
85 #include <netinet/in_systm.h>
86 #include <netinet/ip.h>
88 #include <netinet/ip6.h>
90 #include <netinet/in_pcb.h>
92 #include <netinet6/in6_pcb.h>
94 #include <netinet/in_var.h>
95 #include <netinet/ip_var.h>
97 #include <netinet6/ip6_var.h>
99 #include <netinet/tcp.h>
100 #include <netinet/tcp_fsm.h>
101 #include <netinet/tcp_seq.h>
102 #include <netinet/tcp_timer.h>
103 #include <netinet/tcp_var.h>
105 #include <netinet6/tcp6_var.h>
107 #include <netinet/tcpip.h>
109 #include <netinet/tcp_debug.h>
111 #include <netinet6/ip6protosw.h>
114 #include <netinet6/ipsec.h>
116 #include <netinet6/ipsec6.h>
121 #include <sys/kdebug.h>
123 #define DBG_FNC_TCP_CLOSE NETDBG_CODE(DBG_NETTCP, ((5 << 8) | 2))
125 extern int tcp_lq_overflow
;
127 /* temporary: for testing */
129 extern int ipsec_bypass
;
130 extern lck_mtx_t
*sadb_mutex
;
133 int tcp_mssdflt
= TCP_MSS
;
134 SYSCTL_INT(_net_inet_tcp
, TCPCTL_MSSDFLT
, mssdflt
, CTLFLAG_RW
,
135 &tcp_mssdflt
, 0, "Default TCP Maximum Segment Size");
138 int tcp_v6mssdflt
= TCP6_MSS
;
139 SYSCTL_INT(_net_inet_tcp
, TCPCTL_V6MSSDFLT
, v6mssdflt
,
140 CTLFLAG_RW
, &tcp_v6mssdflt
, 0,
141 "Default TCP Maximum Segment Size for IPv6");
145 * Minimum MSS we accept and use. This prevents DoS attacks where
146 * we are forced to a ridiculous low MSS like 20 and send hundreds
147 * of packets instead of one. The effect scales with the available
148 * bandwidth and quickly saturates the CPU and network interface
149 * with packet generation and sending. Set to zero to disable MINMSS
150 * checking. This setting prevents us from sending too small packets.
152 int tcp_minmss
= TCP_MINMSS
;
153 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, minmss
, CTLFLAG_RW
,
154 &tcp_minmss
, 0, "Minmum TCP Maximum Segment Size");
157 * Number of TCP segments per second we accept from remote host
158 * before we start to calculate average segment size. If average
159 * segment size drops below the minimum TCP MSS we assume a DoS
160 * attack and reset+drop the connection. Care has to be taken not to
161 * set this value too small to not kill interactive type connections
162 * (telnet, SSH) which send many small packets.
164 #ifdef FIX_WORKAROUND_FOR_3894301
165 __private_extern__
int tcp_minmssoverload
= TCP_MINMSSOVERLOAD
;
167 __private_extern__
int tcp_minmssoverload
= 0;
169 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, minmssoverload
, CTLFLAG_RW
,
170 &tcp_minmssoverload
, 0, "Number of TCP Segments per Second allowed to"
171 "be under the MINMSS Size");
173 static int tcp_do_rfc1323
= 1;
174 SYSCTL_INT(_net_inet_tcp
, TCPCTL_DO_RFC1323
, rfc1323
, CTLFLAG_RW
,
175 &tcp_do_rfc1323
, 0, "Enable rfc1323 (high performance TCP) extensions");
177 static int tcp_do_rfc1644
= 0;
178 SYSCTL_INT(_net_inet_tcp
, TCPCTL_DO_RFC1644
, rfc1644
, CTLFLAG_RW
,
179 &tcp_do_rfc1644
, 0, "Enable rfc1644 (TTCP) extensions");
181 static int tcp_tcbhashsize
= 0;
182 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, tcbhashsize
, CTLFLAG_RD
,
183 &tcp_tcbhashsize
, 0, "Size of TCP control-block hashtable");
185 static int do_tcpdrain
= 0;
186 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, do_tcpdrain
, CTLFLAG_RW
, &do_tcpdrain
, 0,
187 "Enable tcp_drain routine for extra help when low on mbufs");
189 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, pcbcount
, CTLFLAG_RD
,
190 &tcbinfo
.ipi_count
, 0, "Number of active PCBs");
192 static int icmp_may_rst
= 1;
193 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, icmp_may_rst
, CTLFLAG_RW
, &icmp_may_rst
, 0,
194 "Certain ICMP unreachable messages may abort connections in SYN_SENT");
196 static int tcp_strict_rfc1948
= 0;
197 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, strict_rfc1948
, CTLFLAG_RW
,
198 &tcp_strict_rfc1948
, 0, "Determines if RFC1948 is followed exactly");
200 static int tcp_isn_reseed_interval
= 0;
201 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, isn_reseed_interval
, CTLFLAG_RW
,
202 &tcp_isn_reseed_interval
, 0, "Seconds between reseeding of ISN secret");
204 static void tcp_cleartaocache(void);
205 static void tcp_notify(struct inpcb
*, int);
208 * Target size of TCP PCB hash tables. Must be a power of two.
210 * Note that this can be overridden by the kernel environment
211 * variable net.inet.tcp.tcbhashsize
214 #define TCBHASHSIZE 4096
218 * This is the actual shape of what we allocate using the zone
219 * allocator. Doing it this way allows us to protect both structures
220 * using the same generation count, and also eliminates the overhead
221 * of allocating tcpcbs separately. By hiding the structure here,
222 * we avoid changing most of the rest of the code (although it needs
223 * to be changed, eventually, for greater efficiency).
226 #define ALIGNM1 (ALIGNMENT - 1)
230 char align
[(sizeof(struct inpcb
) + ALIGNM1
) & ~ALIGNM1
];
234 struct callout inp_tp_rexmt
, inp_tp_persist
, inp_tp_keep
, inp_tp_2msl
;
235 struct callout inp_tp_delack
;
241 static struct tcpcb dummy_tcb
;
244 extern struct inpcbhead time_wait_slots
[];
245 extern int cur_tw_slot
;
246 extern u_long
*delack_bitmask
;
247 extern u_long route_generation
;
250 int get_inpcb_str_size()
252 return sizeof(struct inpcb
);
256 int get_tcp_str_size()
258 return sizeof(struct tcpcb
);
261 int tcp_freeq(struct tcpcb
*tp
);
270 int hashsize
= TCBHASHSIZE
;
273 struct inpcbinfo
*pcbinfo
;
278 tcp_delacktime
= TCPTV_DELACK
;
279 tcp_keepinit
= TCPTV_KEEP_INIT
;
280 tcp_keepidle
= TCPTV_KEEP_IDLE
;
281 tcp_keepintvl
= TCPTV_KEEPINTVL
;
282 tcp_maxpersistidle
= TCPTV_KEEP_IDLE
;
284 read_random(&tcp_now
, sizeof(tcp_now
));
285 tcp_now
= tcp_now
& 0x7fffffff; /* Starts tcp internal 500ms clock at a random value */
289 tcbinfo
.listhead
= &tcb
;
292 TUNABLE_INT_FETCH("net.inet.tcp.tcbhashsize", &hashsize
);
294 if (!powerof2(hashsize
)) {
295 printf("WARNING: TCB hash size not a power of 2\n");
296 hashsize
= 512; /* safe default */
298 tcp_tcbhashsize
= hashsize
;
299 tcbinfo
.hashsize
= hashsize
;
300 tcbinfo
.hashbase
= hashinit(hashsize
, M_PCB
, &tcbinfo
.hashmask
);
301 tcbinfo
.porthashbase
= hashinit(hashsize
, M_PCB
,
302 &tcbinfo
.porthashmask
);
304 str_size
= (vm_size_t
) sizeof(struct inp_tp
);
305 tcbinfo
.ipi_zone
= (void *) zinit(str_size
, 120000*str_size
, 8192, "tcpcb");
307 tcbinfo
.ipi_zone
= zinit("tcpcb", sizeof(struct inp_tp
), maxsockets
,
311 tcp_reass_maxseg
= nmbclusters
/ 16;
313 TUNABLE_INT_FETCH("net.inet.tcp.reass.maxsegments",
318 #define TCP_MINPROTOHDR (sizeof(struct ip6_hdr) + sizeof(struct tcphdr))
320 #define TCP_MINPROTOHDR (sizeof(struct tcpiphdr))
322 if (max_protohdr
< TCP_MINPROTOHDR
)
323 max_protohdr
= TCP_MINPROTOHDR
;
324 if (max_linkhdr
+ TCP_MINPROTOHDR
> MHLEN
)
326 #undef TCP_MINPROTOHDR
327 dummy_tcb
.t_state
= TCP_NSTATES
;
328 dummy_tcb
.t_flags
= 0;
329 tcbinfo
.dummy_cb
= (caddr_t
) &dummy_tcb
;
332 * allocate lock group attribute and group for tcp pcb mutexes
334 pcbinfo
->mtx_grp_attr
= lck_grp_attr_alloc_init();
335 pcbinfo
->mtx_grp
= lck_grp_alloc_init("tcppcb", pcbinfo
->mtx_grp_attr
);
338 * allocate the lock attribute for tcp pcb mutexes
340 pcbinfo
->mtx_attr
= lck_attr_alloc_init();
342 if ((pcbinfo
->mtx
= lck_rw_alloc_init(pcbinfo
->mtx_grp
, pcbinfo
->mtx_attr
)) == NULL
) {
343 printf("tcp_init: mutex not alloced!\n");
344 return; /* pretty much dead if this fails... */
348 in_pcb_nat_init(&tcbinfo
, AF_INET
, IPPROTO_TCP
, SOCK_STREAM
);
350 delack_bitmask
= _MALLOC((4 * hashsize
)/32, M_PCB
, M_WAITOK
);
351 if (delack_bitmask
== 0)
352 panic("Delack Memory");
354 for (i
=0; i
< (tcbinfo
.hashsize
/ 32); i
++)
355 delack_bitmask
[i
] = 0;
357 for (i
=0; i
< N_TIME_WAIT_SLOTS
; i
++) {
358 LIST_INIT(&time_wait_slots
[i
]);
363 * Fill in the IP and TCP headers for an outgoing packet, given the tcpcb.
364 * tcp_template used to store this data in mbufs, but we now recopy it out
365 * of the tcpcb each time to conserve mbufs.
368 tcp_fillheaders(tp
, ip_ptr
, tcp_ptr
)
373 struct inpcb
*inp
= tp
->t_inpcb
;
374 struct tcphdr
*tcp_hdr
= (struct tcphdr
*)tcp_ptr
;
377 if ((inp
->inp_vflag
& INP_IPV6
) != 0) {
380 ip6
= (struct ip6_hdr
*)ip_ptr
;
381 ip6
->ip6_flow
= (ip6
->ip6_flow
& ~IPV6_FLOWINFO_MASK
) |
382 (inp
->in6p_flowinfo
& IPV6_FLOWINFO_MASK
);
383 ip6
->ip6_vfc
= (ip6
->ip6_vfc
& ~IPV6_VERSION_MASK
) |
384 (IPV6_VERSION
& IPV6_VERSION_MASK
);
385 ip6
->ip6_nxt
= IPPROTO_TCP
;
386 ip6
->ip6_plen
= sizeof(struct tcphdr
);
387 ip6
->ip6_src
= inp
->in6p_laddr
;
388 ip6
->ip6_dst
= inp
->in6p_faddr
;
393 struct ip
*ip
= (struct ip
*) ip_ptr
;
395 ip
->ip_vhl
= IP_VHL_BORING
;
402 ip
->ip_p
= IPPROTO_TCP
;
403 ip
->ip_src
= inp
->inp_laddr
;
404 ip
->ip_dst
= inp
->inp_faddr
;
405 tcp_hdr
->th_sum
= in_pseudo(ip
->ip_src
.s_addr
, ip
->ip_dst
.s_addr
,
406 htons(sizeof(struct tcphdr
) + IPPROTO_TCP
));
409 tcp_hdr
->th_sport
= inp
->inp_lport
;
410 tcp_hdr
->th_dport
= inp
->inp_fport
;
415 tcp_hdr
->th_flags
= 0;
421 * Create template to be used to send tcp packets on a connection.
422 * Allocates an mbuf and fills in a skeletal tcp/ip header. The only
423 * use for this function is in keepalives, which use tcp_respond.
432 m
= m_get(M_DONTWAIT
, MT_HEADER
);
435 m
->m_len
= sizeof(struct tcptemp
);
436 n
= mtod(m
, struct tcptemp
*);
438 tcp_fillheaders(tp
, (void *)&n
->tt_ipgen
, (void *)&n
->tt_t
);
443 * Send a single message to the TCP at address specified by
444 * the given TCP/IP header. If m == 0, then we make a copy
445 * of the tcpiphdr at ti and send directly to the addressed host.
446 * This is used to force keep alive messages out using the TCP
447 * template for a connection. If flags are given then we send
448 * a message back to the TCP which originated the * segment ti,
449 * and discard the mbuf containing it and any other attached mbufs.
451 * In any case the ack and sequence number of the transmitted
452 * segment are as specified by the parameters.
454 * NOTE: If m != NULL, then ti must point to *inside* the mbuf.
457 tcp_respond(tp
, ipgen
, th
, m
, ack
, seq
, flags
)
460 register struct tcphdr
*th
;
461 register struct mbuf
*m
;
467 struct route
*ro
= 0;
472 struct route_in6
*ro6
= 0;
473 struct route_in6 sro6
;
480 isipv6
= IP_VHL_V(((struct ip
*)ipgen
)->ip_vhl
) == 6;
486 if (!(flags
& TH_RST
)) {
487 win
= sbspace(&tp
->t_inpcb
->inp_socket
->so_rcv
);
488 if (win
> (long)TCP_MAXWIN
<< tp
->rcv_scale
)
489 win
= (long)TCP_MAXWIN
<< tp
->rcv_scale
;
493 ro6
= &tp
->t_inpcb
->in6p_route
;
496 ro
= &tp
->t_inpcb
->inp_route
;
501 bzero(ro6
, sizeof *ro6
);
506 bzero(ro
, sizeof *ro
);
510 m
= m_gethdr(M_DONTWAIT
, MT_HEADER
);
514 m
->m_data
+= max_linkhdr
;
517 bcopy((caddr_t
)ip6
, mtod(m
, caddr_t
),
518 sizeof(struct ip6_hdr
));
519 ip6
= mtod(m
, struct ip6_hdr
*);
520 nth
= (struct tcphdr
*)(ip6
+ 1);
524 bcopy((caddr_t
)ip
, mtod(m
, caddr_t
), sizeof(struct ip
));
525 ip
= mtod(m
, struct ip
*);
526 nth
= (struct tcphdr
*)(ip
+ 1);
528 bcopy((caddr_t
)th
, (caddr_t
)nth
, sizeof(struct tcphdr
));
533 m
->m_data
= (caddr_t
)ipgen
;
534 /* m_len is set later */
536 #define xchg(a,b,type) { type t; t=a; a=b; b=t; }
539 xchg(ip6
->ip6_dst
, ip6
->ip6_src
, struct in6_addr
);
540 nth
= (struct tcphdr
*)(ip6
+ 1);
544 xchg(ip
->ip_dst
.s_addr
, ip
->ip_src
.s_addr
, n_long
);
545 nth
= (struct tcphdr
*)(ip
+ 1);
549 * this is usually a case when an extension header
550 * exists between the IPv6 header and the
553 nth
->th_sport
= th
->th_sport
;
554 nth
->th_dport
= th
->th_dport
;
556 xchg(nth
->th_dport
, nth
->th_sport
, n_short
);
561 ip6
->ip6_plen
= htons((u_short
)(sizeof (struct tcphdr
) +
563 tlen
+= sizeof (struct ip6_hdr
) + sizeof (struct tcphdr
);
567 tlen
+= sizeof (struct tcpiphdr
);
569 ip
->ip_ttl
= ip_defttl
;
572 m
->m_pkthdr
.len
= tlen
;
573 m
->m_pkthdr
.rcvif
= 0;
574 nth
->th_seq
= htonl(seq
);
575 nth
->th_ack
= htonl(ack
);
577 nth
->th_off
= sizeof (struct tcphdr
) >> 2;
578 nth
->th_flags
= flags
;
580 nth
->th_win
= htons((u_short
) (win
>> tp
->rcv_scale
));
582 nth
->th_win
= htons((u_short
)win
);
587 nth
->th_sum
= in6_cksum(m
, IPPROTO_TCP
,
588 sizeof(struct ip6_hdr
),
589 tlen
- sizeof(struct ip6_hdr
));
590 ip6
->ip6_hlim
= in6_selecthlim(tp
? tp
->t_inpcb
: NULL
,
597 nth
->th_sum
= in_pseudo(ip
->ip_src
.s_addr
, ip
->ip_dst
.s_addr
,
598 htons((u_short
)(tlen
- sizeof(struct ip
) + ip
->ip_p
)));
599 m
->m_pkthdr
.csum_flags
= CSUM_TCP
;
600 m
->m_pkthdr
.csum_data
= offsetof(struct tcphdr
, th_sum
);
603 if (tp
== NULL
|| (tp
->t_inpcb
->inp_socket
->so_options
& SO_DEBUG
))
604 tcp_trace(TA_OUTPUT
, 0, tp
, mtod(m
, void *), th
, 0);
607 if (ipsec_bypass
== 0 && ipsec_setsocket(m
, tp
? tp
->t_inpcb
->inp_socket
: NULL
) != 0) {
614 (void)ip6_output(m
, NULL
, ro6
, ipflags
, NULL
, NULL
, 0);
615 if (ro6
== &sro6
&& ro6
->ro_rt
) {
622 (void) ip_output_list(m
, 0, NULL
, ro
, ipflags
, NULL
);
623 if (ro
== &sro
&& ro
->ro_rt
) {
631 * Create a new TCP control block, making an
632 * empty reassembly queue and hooking it to the argument
633 * protocol control block. The `inp' parameter must have
634 * come from the zone allocator set up in tcp_init().
641 register struct tcpcb
*tp
;
642 register struct socket
*so
= inp
->inp_socket
;
644 int isipv6
= (inp
->inp_vflag
& INP_IPV6
) != 0;
647 if (so
->cached_in_sock_layer
== 0) {
648 it
= (struct inp_tp
*)inp
;
652 tp
= (struct tcpcb
*) inp
->inp_saved_ppcb
;
654 bzero((char *) tp
, sizeof(struct tcpcb
));
655 LIST_INIT(&tp
->t_segq
);
656 tp
->t_maxseg
= tp
->t_maxopd
=
658 isipv6
? tcp_v6mssdflt
:
663 /* Set up our timeouts. */
664 callout_init(tp
->tt_rexmt
= &it
->inp_tp_rexmt
);
665 callout_init(tp
->tt_persist
= &it
->inp_tp_persist
);
666 callout_init(tp
->tt_keep
= &it
->inp_tp_keep
);
667 callout_init(tp
->tt_2msl
= &it
->inp_tp_2msl
);
668 callout_init(tp
->tt_delack
= &it
->inp_tp_delack
);
672 tp
->t_flags
= (TF_REQ_SCALE
|TF_REQ_TSTMP
);
674 tp
->t_flags
|= TF_REQ_CC
;
675 tp
->t_inpcb
= inp
; /* XXX */
677 * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no
678 * rtt estimate. Set rttvar so that srtt + 4 * rttvar gives
679 * reasonable initial retransmit time.
681 tp
->t_srtt
= TCPTV_SRTTBASE
;
682 tp
->t_rttvar
= ((TCPTV_RTOBASE
- TCPTV_SRTTBASE
) << TCP_RTTVAR_SHIFT
) / 4;
683 tp
->t_rttmin
= TCPTV_MIN
;
684 tp
->t_rxtcur
= TCPTV_RTOBASE
;
685 tp
->snd_cwnd
= TCP_MAXWIN
<< TCP_MAX_WINSHIFT
;
686 tp
->snd_ssthresh
= TCP_MAXWIN
<< TCP_MAX_WINSHIFT
;
688 * IPv4 TTL initialization is necessary for an IPv6 socket as well,
689 * because the socket may be bound to an IPv6 wildcard address,
690 * which may match an IPv4-mapped IPv6 address.
692 inp
->inp_ip_ttl
= ip_defttl
;
693 inp
->inp_ppcb
= (caddr_t
)tp
;
694 return (tp
); /* XXX */
698 * Drop a TCP connection, reporting
699 * the specified error. If connection is synchronized,
700 * then send a RST to peer.
704 register struct tcpcb
*tp
;
707 struct socket
*so
= tp
->t_inpcb
->inp_socket
;
712 case TCPS_ESTABLISHED
:
713 case TCPS_FIN_WAIT_1
:
715 case TCPS_CLOSE_WAIT
:
721 if (TCPS_HAVERCVDSYN(tp
->t_state
)) {
722 tp
->t_state
= TCPS_CLOSED
;
723 (void) tcp_output(tp
);
724 tcpstat
.tcps_drops
++;
726 tcpstat
.tcps_conndrops
++;
727 if (errno
== ETIMEDOUT
&& tp
->t_softerror
)
728 errno
= tp
->t_softerror
;
729 so
->so_error
= errno
;
730 return (tcp_close(tp
));
734 * Close a TCP control block:
735 * discard all space held by the tcp
736 * discard internet protocol block
737 * wake up any sleepers
741 register struct tcpcb
*tp
;
743 register struct tseg_qent
*q
;
744 struct inpcb
*inp
= tp
->t_inpcb
;
745 struct socket
*so
= inp
->inp_socket
;
747 int isipv6
= (inp
->inp_vflag
& INP_IPV6
) != 0;
749 register struct rtentry
*rt
;
752 if ( inp
->inp_ppcb
== NULL
) /* tcp_close was called previously, bail */
757 * Make sure that all of our timers are stopped before we
760 callout_stop(tp
->tt_rexmt
);
761 callout_stop(tp
->tt_persist
);
762 callout_stop(tp
->tt_keep
);
763 callout_stop(tp
->tt_2msl
);
764 callout_stop(tp
->tt_delack
);
766 /* Clear the timers before we delete the PCB. */
769 for (i
= 0; i
< TCPT_NTIMERS
; i
++) {
775 KERNEL_DEBUG(DBG_FNC_TCP_CLOSE
| DBG_FUNC_START
, tp
,0,0,0,0);
778 case TCPS_ESTABLISHED
:
779 case TCPS_FIN_WAIT_1
:
781 case TCPS_CLOSE_WAIT
:
788 * If we got enough samples through the srtt filter,
789 * save the rtt and rttvar in the routing entry.
790 * 'Enough' is arbitrarily defined as the 16 samples.
791 * 16 samples is enough for the srtt filter to converge
792 * to within 5% of the correct value; fewer samples and
793 * we could save a very bogus rtt.
795 * Don't update the default route's characteristics and don't
796 * update anything that the user "locked".
798 if (tp
->t_rttupdated
>= 16) {
799 register u_long i
= 0;
802 struct sockaddr_in6
*sin6
;
804 if ((rt
= inp
->in6p_route
.ro_rt
) == NULL
)
806 sin6
= (struct sockaddr_in6
*)rt_key(rt
);
807 if (IN6_IS_ADDR_UNSPECIFIED(&sin6
->sin6_addr
))
812 rt
= inp
->inp_route
.ro_rt
;
814 ((struct sockaddr_in
*)rt_key(rt
))->sin_addr
.s_addr
815 == INADDR_ANY
|| rt
->generation_id
!= route_generation
) {
816 if (tp
->t_state
>= TCPS_CLOSE_WAIT
)
817 tp
->t_state
= TCPS_CLOSING
;
822 if ((rt
->rt_rmx
.rmx_locks
& RTV_RTT
) == 0) {
824 (RTM_RTTUNIT
/ (PR_SLOWHZ
* TCP_RTT_SCALE
));
825 if (rt
->rt_rmx
.rmx_rtt
&& i
)
827 * filter this update to half the old & half
828 * the new values, converting scale.
829 * See route.h and tcp_var.h for a
830 * description of the scaling constants.
833 (rt
->rt_rmx
.rmx_rtt
+ i
) / 2;
835 rt
->rt_rmx
.rmx_rtt
= i
;
836 tcpstat
.tcps_cachedrtt
++;
838 if ((rt
->rt_rmx
.rmx_locks
& RTV_RTTVAR
) == 0) {
840 (RTM_RTTUNIT
/ (PR_SLOWHZ
* TCP_RTTVAR_SCALE
));
841 if (rt
->rt_rmx
.rmx_rttvar
&& i
)
842 rt
->rt_rmx
.rmx_rttvar
=
843 (rt
->rt_rmx
.rmx_rttvar
+ i
) / 2;
845 rt
->rt_rmx
.rmx_rttvar
= i
;
846 tcpstat
.tcps_cachedrttvar
++;
849 * The old comment here said:
850 * update the pipelimit (ssthresh) if it has been updated
851 * already or if a pipesize was specified & the threshhold
852 * got below half the pipesize. I.e., wait for bad news
853 * before we start updating, then update on both good
856 * But we want to save the ssthresh even if no pipesize is
857 * specified explicitly in the route, because such
858 * connections still have an implicit pipesize specified
859 * by the global tcp_sendspace. In the absence of a reliable
860 * way to calculate the pipesize, it will have to do.
862 i
= tp
->snd_ssthresh
;
863 if (rt
->rt_rmx
.rmx_sendpipe
!= 0)
864 dosavessthresh
= (i
< rt
->rt_rmx
.rmx_sendpipe
/ 2);
866 dosavessthresh
= (i
< so
->so_snd
.sb_hiwat
/ 2);
867 if (((rt
->rt_rmx
.rmx_locks
& RTV_SSTHRESH
) == 0 &&
868 i
!= 0 && rt
->rt_rmx
.rmx_ssthresh
!= 0)
871 * convert the limit from user data bytes to
872 * packets then to packet data bytes.
874 i
= (i
+ tp
->t_maxseg
/ 2) / tp
->t_maxseg
;
877 i
*= (u_long
)(tp
->t_maxseg
+
879 (isipv6
? sizeof (struct ip6_hdr
) +
880 sizeof (struct tcphdr
) :
882 sizeof (struct tcpiphdr
)
887 if (rt
->rt_rmx
.rmx_ssthresh
)
888 rt
->rt_rmx
.rmx_ssthresh
=
889 (rt
->rt_rmx
.rmx_ssthresh
+ i
) / 2;
891 rt
->rt_rmx
.rmx_ssthresh
= i
;
892 tcpstat
.tcps_cachedssthresh
++;
895 rt
= inp
->inp_route
.ro_rt
;
898 * mark route for deletion if no information is
901 if ((tp
->t_flags
& TF_LQ_OVERFLOW
) && tcp_lq_overflow
&&
902 ((rt
->rt_rmx
.rmx_locks
& RTV_RTT
) == 0)){
903 if (rt
->rt_rmx
.rmx_rtt
== 0)
904 rt
->rt_flags
|= RTF_DELCLONE
;
908 /* free the reassembly queue, if any */
909 (void) tcp_freeq(tp
);
912 if (so
->cached_in_sock_layer
)
913 inp
->inp_saved_ppcb
= (caddr_t
) tp
;
916 soisdisconnected(so
);
918 if (INP_CHECK_SOCKAF(so
, AF_INET6
))
923 tcpstat
.tcps_closed
++;
924 KERNEL_DEBUG(DBG_FNC_TCP_CLOSE
| DBG_FUNC_END
, tcpstat
.tcps_closed
,0,0,0,0);
925 return ((struct tcpcb
*)0);
933 register struct tseg_qent
*q
;
936 while((q
= LIST_FIRST(&tp
->t_segq
)) != NULL
) {
937 LIST_REMOVE(q
, tqe_q
);
950 * ###LD 05/19/04 locking issue, tcpdrain is disabled, deadlock situation with tcbinfo.mtx
956 struct tseg_qent
*te
;
959 * Walk the tcpbs, if existing, and flush the reassembly queue,
961 * XXX: The "Net/3" implementation doesn't imply that the TCP
962 * reassembly queue should be flushed, but in a situation
963 * where we're really low on mbufs, this is potentially
966 lck_rw_lock_exclusive(tcbinfo
.mtx
);
967 for (inpb
= LIST_FIRST(tcbinfo
.listhead
); inpb
;
968 inpb
= LIST_NEXT(inpb
, inp_list
)) {
969 if ((tcpb
= intotcpcb(inpb
))) {
970 while ((te
= LIST_FIRST(&tcpb
->t_segq
))
972 LIST_REMOVE(te
, tqe_q
);
979 lck_rw_done(tcbinfo
.mtx
);
985 * Notify a tcp user of an asynchronous error;
986 * store error as soft error, but wake up user
987 * (for now, won't do anything until can select for soft error).
989 * Do not wake up user since there currently is no mechanism for
990 * reporting soft errors (yet - a kqueue filter may be added).
993 tcp_notify(inp
, error
)
999 if (inp
== NULL
|| (inp
->inp_state
== INPCB_STATE_DEAD
))
1000 return; /* pcb is gone already */
1002 tp
= (struct tcpcb
*)inp
->inp_ppcb
;
1005 * Ignore some errors if we are hooked up.
1006 * If connection hasn't completed, has retransmitted several times,
1007 * and receives a second error, give up now. This is better
1008 * than waiting a long time to establish a connection that
1009 * can never complete.
1011 if (tp
->t_state
== TCPS_ESTABLISHED
&&
1012 (error
== EHOSTUNREACH
|| error
== ENETUNREACH
||
1013 error
== EHOSTDOWN
)) {
1015 } else if (tp
->t_state
< TCPS_ESTABLISHED
&& tp
->t_rxtshift
> 3 &&
1017 tcp_drop(tp
, error
);
1019 tp
->t_softerror
= error
;
1021 wakeup((caddr_t
) &so
->so_timeo
);
1028 tcp_pcblist SYSCTL_HANDLER_ARGS
1031 struct inpcb
*inp
, **inp_list
;
1036 * The process of preparing the TCB list is too time-consuming and
1037 * resource-intensive to repeat twice on every request.
1039 lck_rw_lock_shared(tcbinfo
.mtx
);
1040 if (req
->oldptr
== USER_ADDR_NULL
) {
1041 n
= tcbinfo
.ipi_count
;
1042 req
->oldidx
= 2 * (sizeof xig
)
1043 + (n
+ n
/8) * sizeof(struct xtcpcb
);
1044 lck_rw_done(tcbinfo
.mtx
);
1048 if (req
->newptr
!= USER_ADDR_NULL
) {
1049 lck_rw_done(tcbinfo
.mtx
);
1054 * OK, now we're committed to doing something.
1056 gencnt
= tcbinfo
.ipi_gencnt
;
1057 n
= tcbinfo
.ipi_count
;
1059 bzero(&xig
, sizeof(xig
));
1060 xig
.xig_len
= sizeof xig
;
1062 xig
.xig_gen
= gencnt
;
1063 xig
.xig_sogen
= so_gencnt
;
1064 error
= SYSCTL_OUT(req
, &xig
, sizeof xig
);
1066 lck_rw_done(tcbinfo
.mtx
);
1070 * We are done if there is no pcb
1073 lck_rw_done(tcbinfo
.mtx
);
1077 inp_list
= _MALLOC(n
* sizeof *inp_list
, M_TEMP
, M_WAITOK
);
1078 if (inp_list
== 0) {
1079 lck_rw_done(tcbinfo
.mtx
);
1083 for (inp
= LIST_FIRST(tcbinfo
.listhead
), i
= 0; inp
&& i
< n
;
1084 inp
= LIST_NEXT(inp
, inp_list
)) {
1086 if (inp
->inp_gencnt
<= gencnt
&& inp
->inp_state
!= INPCB_STATE_DEAD
)
1088 if (inp
->inp_gencnt
<= gencnt
&& !prison_xinpcb(req
->p
, inp
))
1090 inp_list
[i
++] = inp
;
1095 for (i
= 0; i
< n
; i
++) {
1097 if (inp
->inp_gencnt
<= gencnt
&& inp
->inp_state
!= INPCB_STATE_DEAD
) {
1101 bzero(&xt
, sizeof(xt
));
1102 xt
.xt_len
= sizeof xt
;
1103 /* XXX should avoid extra copy */
1104 inpcb_to_compat(inp
, &xt
.xt_inp
);
1105 inp_ppcb
= inp
->inp_ppcb
;
1106 if (inp_ppcb
!= NULL
) {
1107 bcopy(inp_ppcb
, &xt
.xt_tp
, sizeof xt
.xt_tp
);
1110 bzero((char *) &xt
.xt_tp
, sizeof xt
.xt_tp
);
1111 if (inp
->inp_socket
)
1112 sotoxsocket(inp
->inp_socket
, &xt
.xt_socket
);
1113 error
= SYSCTL_OUT(req
, &xt
, sizeof xt
);
1118 * Give the user an updated idea of our state.
1119 * If the generation differs from what we told
1120 * her before, she knows that something happened
1121 * while we were processing this request, and it
1122 * might be necessary to retry.
1124 bzero(&xig
, sizeof(xig
));
1125 xig
.xig_len
= sizeof xig
;
1126 xig
.xig_gen
= tcbinfo
.ipi_gencnt
;
1127 xig
.xig_sogen
= so_gencnt
;
1128 xig
.xig_count
= tcbinfo
.ipi_count
;
1129 error
= SYSCTL_OUT(req
, &xig
, sizeof xig
);
1131 FREE(inp_list
, M_TEMP
);
1132 lck_rw_done(tcbinfo
.mtx
);
1136 SYSCTL_PROC(_net_inet_tcp
, TCPCTL_PCBLIST
, pcblist
, CTLFLAG_RD
, 0, 0,
1137 tcp_pcblist
, "S,xtcpcb", "List of active TCP connections");
1141 tcp_getcred(SYSCTL_HANDLER_ARGS
)
1143 struct sockaddr_in addrs
[2];
1147 error
= suser(req
->p
);
1150 error
= SYSCTL_IN(req
, addrs
, sizeof(addrs
));
1154 inp
= in_pcblookup_hash(&tcbinfo
, addrs
[1].sin_addr
, addrs
[1].sin_port
,
1155 addrs
[0].sin_addr
, addrs
[0].sin_port
, 0, NULL
);
1156 if (inp
== NULL
|| inp
->inp_socket
== NULL
) {
1160 error
= SYSCTL_OUT(req
, inp
->inp_socket
->so_cred
, sizeof(*(kauth_cred_t
)0);
1166 SYSCTL_PROC(_net_inet_tcp
, OID_AUTO
, getcred
, CTLTYPE_OPAQUE
|CTLFLAG_RW
,
1167 0, 0, tcp_getcred
, "S,ucred", "Get the ucred of a TCP connection");
1171 tcp6_getcred(SYSCTL_HANDLER_ARGS
)
1173 struct sockaddr_in6 addrs
[2];
1175 int error
, s
, mapped
= 0;
1177 error
= suser(req
->p
);
1180 error
= SYSCTL_IN(req
, addrs
, sizeof(addrs
));
1183 if (IN6_IS_ADDR_V4MAPPED(&addrs
[0].sin6_addr
)) {
1184 if (IN6_IS_ADDR_V4MAPPED(&addrs
[1].sin6_addr
))
1191 inp
= in_pcblookup_hash(&tcbinfo
,
1192 *(struct in_addr
*)&addrs
[1].sin6_addr
.s6_addr
[12],
1194 *(struct in_addr
*)&addrs
[0].sin6_addr
.s6_addr
[12],
1198 inp
= in6_pcblookup_hash(&tcbinfo
, &addrs
[1].sin6_addr
,
1200 &addrs
[0].sin6_addr
, addrs
[0].sin6_port
,
1202 if (inp
== NULL
|| inp
->inp_socket
== NULL
) {
1206 error
= SYSCTL_OUT(req
, inp
->inp_socket
->so_cred
,
1207 sizeof(*(kauth_cred_t
)0);
1213 SYSCTL_PROC(_net_inet6_tcp6
, OID_AUTO
, getcred
, CTLTYPE_OPAQUE
|CTLFLAG_RW
,
1215 tcp6_getcred
, "S,ucred", "Get the ucred of a TCP6 connection");
1217 #endif /* __APPLE__*/
1220 tcp_ctlinput(cmd
, sa
, vip
)
1222 struct sockaddr
*sa
;
1225 struct ip
*ip
= vip
;
1227 struct in_addr faddr
;
1230 void (*notify
)(struct inpcb
*, int) = tcp_notify
;
1234 faddr
= ((struct sockaddr_in
*)sa
)->sin_addr
;
1235 if (sa
->sa_family
!= AF_INET
|| faddr
.s_addr
== INADDR_ANY
)
1238 if (cmd
== PRC_QUENCH
)
1239 notify
= tcp_quench
;
1240 else if (icmp_may_rst
&& (cmd
== PRC_UNREACH_ADMIN_PROHIB
||
1241 cmd
== PRC_UNREACH_PORT
) && ip
)
1242 notify
= tcp_drop_syn_sent
;
1243 else if (cmd
== PRC_MSGSIZE
)
1244 notify
= tcp_mtudisc
;
1245 else if (PRC_IS_REDIRECT(cmd
)) {
1247 notify
= in_rtchange
;
1248 } else if (cmd
== PRC_HOSTDEAD
)
1250 else if ((unsigned)cmd
> PRC_NCMDS
|| inetctlerrmap
[cmd
] == 0)
1253 th
= (struct tcphdr
*)((caddr_t
)ip
1254 + (IP_VHL_HL(ip
->ip_vhl
) << 2));
1255 inp
= in_pcblookup_hash(&tcbinfo
, faddr
, th
->th_dport
,
1256 ip
->ip_src
, th
->th_sport
, 0, NULL
);
1257 if (inp
!= NULL
&& inp
->inp_socket
!= NULL
) {
1258 tcp_lock(inp
->inp_socket
, 1, 0);
1259 if (in_pcb_checkstate(inp
, WNT_RELEASE
, 1) == WNT_STOPUSING
) {
1260 tcp_unlock(inp
->inp_socket
, 1, 0);
1263 icmp_seq
= htonl(th
->th_seq
);
1264 tp
= intotcpcb(inp
);
1265 if (SEQ_GEQ(icmp_seq
, tp
->snd_una
) &&
1266 SEQ_LT(icmp_seq
, tp
->snd_max
))
1267 (*notify
)(inp
, inetctlerrmap
[cmd
]);
1268 tcp_unlock(inp
->inp_socket
, 1, 0);
1271 in_pcbnotifyall(&tcbinfo
, faddr
, inetctlerrmap
[cmd
], notify
);
1276 tcp6_ctlinput(cmd
, sa
, d
)
1278 struct sockaddr
*sa
;
1282 void (*notify
)(struct inpcb
*, int) = tcp_notify
;
1283 struct ip6_hdr
*ip6
;
1285 struct ip6ctlparam
*ip6cp
= NULL
;
1286 const struct sockaddr_in6
*sa6_src
= NULL
;
1288 struct tcp_portonly
{
1293 if (sa
->sa_family
!= AF_INET6
||
1294 sa
->sa_len
!= sizeof(struct sockaddr_in6
))
1297 if (cmd
== PRC_QUENCH
)
1298 notify
= tcp_quench
;
1299 else if (cmd
== PRC_MSGSIZE
)
1300 notify
= tcp_mtudisc
;
1301 else if (!PRC_IS_REDIRECT(cmd
) &&
1302 ((unsigned)cmd
> PRC_NCMDS
|| inet6ctlerrmap
[cmd
] == 0))
1305 /* if the parameter is from icmp6, decode it. */
1307 ip6cp
= (struct ip6ctlparam
*)d
;
1309 ip6
= ip6cp
->ip6c_ip6
;
1310 off
= ip6cp
->ip6c_off
;
1311 sa6_src
= ip6cp
->ip6c_src
;
1315 off
= 0; /* fool gcc */
1321 * XXX: We assume that when IPV6 is non NULL,
1322 * M and OFF are valid.
1325 /* check if we can safely examine src and dst ports */
1326 if (m
->m_pkthdr
.len
< off
+ sizeof(*thp
))
1329 bzero(&th
, sizeof(th
));
1330 m_copydata(m
, off
, sizeof(*thp
), (caddr_t
)&th
);
1332 in6_pcbnotify(&tcbinfo
, sa
, th
.th_dport
,
1333 (struct sockaddr
*)ip6cp
->ip6c_src
,
1334 th
.th_sport
, cmd
, notify
);
1336 in6_pcbnotify(&tcbinfo
, sa
, 0, (struct sockaddr
*)sa6_src
,
1343 * Following is where TCP initial sequence number generation occurs.
1345 * There are two places where we must use initial sequence numbers:
1346 * 1. In SYN-ACK packets.
1347 * 2. In SYN packets.
1349 * The ISNs in SYN-ACK packets have no monotonicity requirement,
1350 * and should be as unpredictable as possible to avoid the possibility
1351 * of spoofing and/or connection hijacking. To satisfy this
1352 * requirement, SYN-ACK ISNs are generated via the arc4random()
1353 * function. If exact RFC 1948 compliance is requested via sysctl,
1354 * these ISNs will be generated just like those in SYN packets.
1356 * The ISNs in SYN packets must be monotonic; TIME_WAIT recycling
1357 * depends on this property. In addition, these ISNs should be
1358 * unguessable so as to prevent connection hijacking. To satisfy
1359 * the requirements of this situation, the algorithm outlined in
1360 * RFC 1948 is used to generate sequence numbers.
1362 * For more information on the theory of operation, please see
1365 * Implementation details:
1367 * Time is based off the system timer, and is corrected so that it
1368 * increases by one megabyte per second. This allows for proper
1369 * recycling on high speed LANs while still leaving over an hour
1372 * Two sysctls control the generation of ISNs:
1374 * net.inet.tcp.isn_reseed_interval controls the number of seconds
1375 * between seeding of isn_secret. This is normally set to zero,
1376 * as reseeding should not be necessary.
1378 * net.inet.tcp.strict_rfc1948 controls whether RFC 1948 is followed
1379 * strictly. When strict compliance is requested, reseeding is
1380 * disabled and SYN-ACKs will be generated in the same manner as
1381 * SYNs. Strict mode is disabled by default.
1385 #define ISN_BYTES_PER_SECOND 1048576
1387 u_char isn_secret
[32];
1388 int isn_last_reseed
;
1395 u_int32_t md5_buffer
[4];
1397 struct timeval time
;
1399 /* Use arc4random for SYN-ACKs when not in exact RFC1948 mode. */
1400 if (((tp
->t_state
== TCPS_LISTEN
) || (tp
->t_state
== TCPS_TIME_WAIT
))
1401 && tcp_strict_rfc1948
== 0)
1405 return arc4random();
1408 /* Seed if this is the first use, reseed if requested. */
1409 if ((isn_last_reseed
== 0) ||
1410 ((tcp_strict_rfc1948
== 0) && (tcp_isn_reseed_interval
> 0) &&
1411 (((u_int
)isn_last_reseed
+ (u_int
)tcp_isn_reseed_interval
*hz
)
1412 < (u_int
)time
.tv_sec
))) {
1414 read_random(&isn_secret
, sizeof(isn_secret
));
1416 read_random_unlimited(&isn_secret
, sizeof(isn_secret
));
1418 isn_last_reseed
= time
.tv_sec
;
1421 /* Compute the md5 hash and return the ISN. */
1423 MD5Update(&isn_ctx
, (u_char
*) &tp
->t_inpcb
->inp_fport
, sizeof(u_short
));
1424 MD5Update(&isn_ctx
, (u_char
*) &tp
->t_inpcb
->inp_lport
, sizeof(u_short
));
1426 if ((tp
->t_inpcb
->inp_vflag
& INP_IPV6
) != 0) {
1427 MD5Update(&isn_ctx
, (u_char
*) &tp
->t_inpcb
->in6p_faddr
,
1428 sizeof(struct in6_addr
));
1429 MD5Update(&isn_ctx
, (u_char
*) &tp
->t_inpcb
->in6p_laddr
,
1430 sizeof(struct in6_addr
));
1434 MD5Update(&isn_ctx
, (u_char
*) &tp
->t_inpcb
->inp_faddr
,
1435 sizeof(struct in_addr
));
1436 MD5Update(&isn_ctx
, (u_char
*) &tp
->t_inpcb
->inp_laddr
,
1437 sizeof(struct in_addr
));
1439 MD5Update(&isn_ctx
, (u_char
*) &isn_secret
, sizeof(isn_secret
));
1440 MD5Final((u_char
*) &md5_buffer
, &isn_ctx
);
1441 new_isn
= (tcp_seq
) md5_buffer
[0];
1442 new_isn
+= time
.tv_sec
* (ISN_BYTES_PER_SECOND
/ hz
);
1447 * When a source quench is received, close congestion window
1448 * to one segment. We will gradually open it again as we proceed.
1451 tcp_quench(inp
, errno
)
1455 struct tcpcb
*tp
= intotcpcb(inp
);
1458 tp
->snd_cwnd
= tp
->t_maxseg
;
1462 * When a specific ICMP unreachable message is received and the
1463 * connection state is SYN-SENT, drop the connection. This behavior
1464 * is controlled by the icmp_may_rst sysctl.
1467 tcp_drop_syn_sent(inp
, errno
)
1471 struct tcpcb
*tp
= intotcpcb(inp
);
1473 if (tp
&& tp
->t_state
== TCPS_SYN_SENT
)
1474 tcp_drop(tp
, errno
);
1478 * When `need fragmentation' ICMP is received, update our idea of the MSS
1479 * based on the new value in the route. Also nudge TCP to send something,
1480 * since we know the packet we just sent was dropped.
1481 * This duplicates some code in the tcp_mss() function in tcp_input.c.
1484 tcp_mtudisc(inp
, errno
)
1488 struct tcpcb
*tp
= intotcpcb(inp
);
1490 struct rmxp_tao
*taop
;
1491 struct socket
*so
= inp
->inp_socket
;
1495 int isipv6
= (tp
->t_inpcb
->inp_vflag
& INP_IPV6
) != 0;
1501 rt
= tcp_rtlookup6(inp
);
1504 rt
= tcp_rtlookup(inp
);
1505 if (!rt
|| !rt
->rt_rmx
.rmx_mtu
) {
1506 tp
->t_maxopd
= tp
->t_maxseg
=
1508 isipv6
? tcp_v6mssdflt
:
1513 taop
= rmx_taop(rt
->rt_rmx
);
1514 offered
= taop
->tao_mssopt
;
1515 mss
= rt
->rt_rmx
.rmx_mtu
-
1518 sizeof(struct ip6_hdr
) + sizeof(struct tcphdr
) :
1520 sizeof(struct tcpiphdr
)
1527 mss
= min(mss
, offered
);
1529 * XXX - The above conditional probably violates the TCP
1530 * spec. The problem is that, since we don't know the
1531 * other end's MSS, we are supposed to use a conservative
1532 * default. But, if we do that, then MTU discovery will
1533 * never actually take place, because the conservative
1534 * default is much less than the MTUs typically seen
1535 * on the Internet today. For the moment, we'll sweep
1536 * this under the carpet.
1538 * The conservative default might not actually be a problem
1539 * if the only case this occurs is when sending an initial
1540 * SYN with options and data to a host we've never talked
1541 * to before. Then, they will reply with an MSS value which
1542 * will get recorded and the new parameters should get
1543 * recomputed. For Further Study.
1545 if (tp
->t_maxopd
<= mss
)
1549 if ((tp
->t_flags
& (TF_REQ_TSTMP
|TF_NOOPT
)) == TF_REQ_TSTMP
&&
1550 (tp
->t_flags
& TF_RCVD_TSTMP
) == TF_RCVD_TSTMP
)
1551 mss
-= TCPOLEN_TSTAMP_APPA
;
1552 if ((tp
->t_flags
& (TF_REQ_CC
|TF_NOOPT
)) == TF_REQ_CC
&&
1553 (tp
->t_flags
& TF_RCVD_CC
) == TF_RCVD_CC
)
1554 mss
-= TCPOLEN_CC_APPA
;
1556 if (so
->so_snd
.sb_hiwat
< mss
)
1557 mss
= so
->so_snd
.sb_hiwat
;
1561 tcpstat
.tcps_mturesent
++;
1563 tp
->snd_nxt
= tp
->snd_una
;
1569 * Look-up the routing entry to the peer of this inpcb. If no route
1570 * is found and it cannot be allocated the return NULL. This routine
1571 * is called by TCP routines that access the rmx structure and by tcp_mss
1572 * to get the interface MTU.
1581 ro
= &inp
->inp_route
;
1585 if (rt
== NULL
|| !(rt
->rt_flags
& RTF_UP
) || rt
->generation_id
!= route_generation
) {
1586 /* No route yet, so try to acquire one */
1587 if (inp
->inp_faddr
.s_addr
!= INADDR_ANY
) {
1588 ro
->ro_dst
.sa_family
= AF_INET
;
1589 ro
->ro_dst
.sa_len
= sizeof(struct sockaddr_in
);
1590 ((struct sockaddr_in
*) &ro
->ro_dst
)->sin_addr
=
1604 struct route_in6
*ro6
;
1607 ro6
= &inp
->in6p_route
;
1609 if (rt
== NULL
|| !(rt
->rt_flags
& RTF_UP
)) {
1610 /* No route yet, so try to acquire one */
1611 if (!IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_faddr
)) {
1612 struct sockaddr_in6
*dst6
;
1614 dst6
= (struct sockaddr_in6
*)&ro6
->ro_dst
;
1615 dst6
->sin6_family
= AF_INET6
;
1616 dst6
->sin6_len
= sizeof(*dst6
);
1617 dst6
->sin6_addr
= inp
->in6p_faddr
;
1618 rtalloc((struct route
*)ro6
);
1627 /* compute ESP/AH header size for TCP, including outer IP header. */
1629 ipsec_hdrsiz_tcp(tp
)
1637 struct ip6_hdr
*ip6
= NULL
;
1641 if ((tp
== NULL
) || ((inp
= tp
->t_inpcb
) == NULL
))
1643 MGETHDR(m
, M_DONTWAIT
, MT_DATA
);
1647 lck_mtx_lock(sadb_mutex
);
1649 if ((inp
->inp_vflag
& INP_IPV6
) != 0) {
1650 ip6
= mtod(m
, struct ip6_hdr
*);
1651 th
= (struct tcphdr
*)(ip6
+ 1);
1652 m
->m_pkthdr
.len
= m
->m_len
=
1653 sizeof(struct ip6_hdr
) + sizeof(struct tcphdr
);
1654 tcp_fillheaders(tp
, ip6
, th
);
1655 hdrsiz
= ipsec6_hdrsiz(m
, IPSEC_DIR_OUTBOUND
, inp
);
1659 ip
= mtod(m
, struct ip
*);
1660 th
= (struct tcphdr
*)(ip
+ 1);
1661 m
->m_pkthdr
.len
= m
->m_len
= sizeof(struct tcpiphdr
);
1662 tcp_fillheaders(tp
, ip
, th
);
1663 hdrsiz
= ipsec4_hdrsiz(m
, IPSEC_DIR_OUTBOUND
, inp
);
1665 lck_mtx_unlock(sadb_mutex
);
1672 * Return a pointer to the cached information about the remote host.
1673 * The cached information is stored in the protocol specific part of
1674 * the route metrics.
1677 tcp_gettaocache(inp
)
1683 if ((inp
->inp_vflag
& INP_IPV6
) != 0)
1684 rt
= tcp_rtlookup6(inp
);
1687 rt
= tcp_rtlookup(inp
);
1689 /* Make sure this is a host route and is up. */
1691 (rt
->rt_flags
& (RTF_UP
|RTF_HOST
)) != (RTF_UP
|RTF_HOST
))
1694 return rmx_taop(rt
->rt_rmx
);
1698 * Clear all the TAO cache entries, called from tcp_init.
1701 * This routine is just an empty one, because we assume that the routing
1702 * routing tables are initialized at the same time when TCP, so there is
1703 * nothing in the cache left over.
1711 tcp_lock(so
, refcount
, lr
)
1718 lr_saved
= (unsigned int) __builtin_return_address(0);
1722 lck_mtx_lock(((struct inpcb
*)so
->so_pcb
)->inpcb_mtx
);
1725 panic("tcp_lock: so=%x NO PCB! lr=%x\n", so
, lr_saved
);
1726 lck_mtx_lock(so
->so_proto
->pr_domain
->dom_mtx
);
1729 if (so
->so_usecount
< 0)
1730 panic("tcp_lock: so=%x so_pcb=%x lr=%x ref=%x\n",
1731 so
, so
->so_pcb
, lr_saved
, so
->so_usecount
);
1735 so
->lock_lr
[so
->next_lock_lr
] = (u_int32_t
*)lr_saved
;
1736 so
->next_lock_lr
= (so
->next_lock_lr
+1) % SO_LCKDBG_MAX
;
1741 tcp_unlock(so
, refcount
, lr
)
1748 lr_saved
= (unsigned int) __builtin_return_address(0);
1751 #ifdef MORE_TCPLOCK_DEBUG
1752 printf("tcp_unlock: so=%x sopcb=%x lock=%x ref=%x lr=%x\n",
1753 so
, so
->so_pcb
, ((struct inpcb
*)so
->so_pcb
)->inpcb_mtx
, so
->so_usecount
, lr_saved
);
1758 if (so
->so_usecount
< 0)
1759 panic("tcp_unlock: so=%x usecount=%x\n", so
, so
->so_usecount
);
1760 if (so
->so_pcb
== NULL
)
1761 panic("tcp_unlock: so=%x NO PCB usecount=%x lr=%x\n", so
, so
->so_usecount
, lr_saved
);
1763 lck_mtx_assert(((struct inpcb
*)so
->so_pcb
)->inpcb_mtx
, LCK_MTX_ASSERT_OWNED
);
1764 so
->unlock_lr
[so
->next_unlock_lr
] = (u_int
*)lr_saved
;
1765 so
->next_unlock_lr
= (so
->next_unlock_lr
+1) % SO_LCKDBG_MAX
;
1766 lck_mtx_unlock(((struct inpcb
*)so
->so_pcb
)->inpcb_mtx
);
1772 tcp_getlock(so
, locktype
)
1776 struct inpcb
*inp
= sotoinpcb(so
);
1779 if (so
->so_usecount
< 0)
1780 panic("tcp_getlock: so=%x usecount=%x\n", so
, so
->so_usecount
);
1781 return(inp
->inpcb_mtx
);
1784 panic("tcp_getlock: so=%x NULL so_pcb\n", so
);
1785 return (so
->so_proto
->pr_domain
->dom_mtx
);