2 * Copyright (c) 2000-2013 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
30 * The Regents of the University of California. All rights reserved.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * @(#)tcp_subr.c 8.2 (Berkeley) 5/24/95
61 * $FreeBSD: src/sys/netinet/tcp_subr.c,v 1.73.2.22 2001/08/22 00:59:12 silby Exp $
64 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
65 * support for mandatory and extensible security protections. This notice
66 * is included in support of clause 2.2 (b) of the Apple Public License,
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #include <sys/callout.h>
73 #include <sys/kernel.h>
74 #include <sys/sysctl.h>
75 #include <sys/malloc.h>
77 #include <sys/domain.h>
79 #include <sys/kauth.h>
80 #include <sys/socket.h>
81 #include <sys/socketvar.h>
82 #include <sys/protosw.h>
83 #include <sys/random.h>
84 #include <sys/syslog.h>
85 #include <sys/mcache.h>
86 #include <kern/locks.h>
87 #include <kern/zalloc.h>
89 #include <dev/random/randomdev.h>
91 #include <net/route.h>
94 #define tcp_minmssoverload fring
96 #include <netinet/in.h>
97 #include <netinet/in_systm.h>
98 #include <netinet/ip.h>
99 #include <netinet/ip_icmp.h>
101 #include <netinet/ip6.h>
103 #include <netinet/in_pcb.h>
105 #include <netinet6/in6_pcb.h>
107 #include <netinet/in_var.h>
108 #include <netinet/ip_var.h>
109 #include <netinet/icmp_var.h>
111 #include <netinet6/ip6_var.h>
113 #include <netinet/tcp.h>
114 #include <netinet/tcp_fsm.h>
115 #include <netinet/tcp_seq.h>
116 #include <netinet/tcp_timer.h>
117 #include <netinet/tcp_var.h>
118 #include <netinet/tcp_cc.h>
119 #include <kern/thread_call.h>
122 #include <netinet6/tcp6_var.h>
124 #include <netinet/tcpip.h>
126 #include <netinet/tcp_debug.h>
128 #include <netinet6/ip6protosw.h>
131 #include <netinet6/ipsec.h>
133 #include <netinet6/ipsec6.h>
137 #undef tcp_minmssoverload
140 #include <security/mac_framework.h>
143 #include <libkern/crypto/md5.h>
144 #include <sys/kdebug.h>
145 #include <mach/sdt.h>
147 #include <netinet/lro_ext.h>
149 #define DBG_FNC_TCP_CLOSE NETDBG_CODE(DBG_NETTCP, ((5 << 8) | 2))
151 extern int tcp_lq_overflow
;
153 /* temporary: for testing */
155 extern int ipsec_bypass
;
157 extern struct tcptimerlist tcp_timer_list
;
158 extern struct tcptailq tcp_tw_tailq
;
160 int tcp_mssdflt
= TCP_MSS
;
161 SYSCTL_INT(_net_inet_tcp
, TCPCTL_MSSDFLT
, mssdflt
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
162 &tcp_mssdflt
, 0, "Default TCP Maximum Segment Size");
165 int tcp_v6mssdflt
= TCP6_MSS
;
166 SYSCTL_INT(_net_inet_tcp
, TCPCTL_V6MSSDFLT
, v6mssdflt
,
167 CTLFLAG_RW
| CTLFLAG_LOCKED
, &tcp_v6mssdflt
, 0,
168 "Default TCP Maximum Segment Size for IPv6");
171 extern int tcp_do_autorcvbuf
;
174 * Minimum MSS we accept and use. This prevents DoS attacks where
175 * we are forced to a ridiculous low MSS like 20 and send hundreds
176 * of packets instead of one. The effect scales with the available
177 * bandwidth and quickly saturates the CPU and network interface
178 * with packet generation and sending. Set to zero to disable MINMSS
179 * checking. This setting prevents us from sending too small packets.
181 int tcp_minmss
= TCP_MINMSS
;
182 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, minmss
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
183 &tcp_minmss
, 0, "Minmum TCP Maximum Segment Size");
185 static int tcp_do_rfc1323
= 1;
186 SYSCTL_INT(_net_inet_tcp
, TCPCTL_DO_RFC1323
, rfc1323
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
187 &tcp_do_rfc1323
, 0, "Enable rfc1323 (high performance TCP) extensions");
190 static int tcp_do_rfc1644
= 0;
191 SYSCTL_INT(_net_inet_tcp
, TCPCTL_DO_RFC1644
, rfc1644
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
192 &tcp_do_rfc1644
, 0, "Enable rfc1644 (TTCP) extensions");
194 static int do_tcpdrain
= 0;
195 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, do_tcpdrain
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &do_tcpdrain
, 0,
196 "Enable tcp_drain routine for extra help when low on mbufs");
198 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, pcbcount
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
199 &tcbinfo
.ipi_count
, 0, "Number of active PCBs");
201 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, tw_pcbcount
,
202 CTLFLAG_RD
| CTLFLAG_LOCKED
,
203 &tcbinfo
.ipi_twcount
, 0, "Number of pcbs in time-wait state");
205 static int icmp_may_rst
= 1;
206 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, icmp_may_rst
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &icmp_may_rst
, 0,
207 "Certain ICMP unreachable messages may abort connections in SYN_SENT");
209 static int tcp_strict_rfc1948
= 0;
210 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, strict_rfc1948
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
211 &tcp_strict_rfc1948
, 0, "Determines if RFC1948 is followed exactly");
213 static int tcp_isn_reseed_interval
= 0;
214 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, isn_reseed_interval
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
215 &tcp_isn_reseed_interval
, 0, "Seconds between reseeding of ISN secret");
216 static int tcp_background_io_enabled
= 1;
217 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, background_io_enabled
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
218 &tcp_background_io_enabled
, 0, "Background IO Enabled");
220 int tcp_TCPTV_MIN
= 100; /* 100ms minimum RTT */
221 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, rtt_min
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
222 &tcp_TCPTV_MIN
, 0, "min rtt value allowed");
224 int tcp_rexmt_slop
= TCPTV_REXMTSLOP
;
225 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, rexmt_slop
, CTLFLAG_RW
,
226 &tcp_rexmt_slop
, 0, "Slop added to retransmit timeout");
228 __private_extern__
int tcp_use_randomport
= 0;
229 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, randomize_ports
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
230 &tcp_use_randomport
, 0, "Randomize TCP port numbers");
232 extern struct tcp_cc_algo tcp_cc_newreno
;
233 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, newreno_sockets
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
234 &tcp_cc_newreno
.num_sockets
, 0, "Number of sockets using newreno");
236 extern struct tcp_cc_algo tcp_cc_ledbat
;
237 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, background_sockets
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
238 &tcp_cc_ledbat
.num_sockets
, 0, "Number of sockets using background transport");
240 __private_extern__
int tcp_win_scale
= 3;
241 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, win_scale_factor
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
242 &tcp_win_scale
, 0, "Window scaling factor");
244 static void tcp_cleartaocache(void);
245 static void tcp_notify(struct inpcb
*, int);
246 static void tcp_cc_init(void);
248 struct zone
*sack_hole_zone
;
249 struct zone
*tcp_reass_zone
;
250 struct zone
*tcp_bwmeas_zone
;
252 static unsigned int tcp_mptcp_dsnm_sz
;
253 struct zone
*tcp_mptcp_dsnm_zone
;
255 /* The array containing pointers to currently implemented TCP CC algorithms */
256 struct tcp_cc_algo
* tcp_cc_algo_list
[TCP_CC_ALGO_COUNT
];
258 extern int slowlink_wsize
; /* window correction for slow links */
259 extern int path_mtu_discovery
;
261 extern u_int32_t tcp_autorcvbuf_max
;
262 extern u_int32_t tcp_autorcvbuf_inc_shift
;
263 static void tcp_sbrcv_grow_rwin(struct tcpcb
*tp
, struct sockbuf
*sb
);
265 #define TCP_BWMEAS_BURST_MINSIZE 6
266 #define TCP_BWMEAS_BURST_MAXSIZE 25
268 static uint32_t bwmeas_elm_size
;
271 * Target size of TCP PCB hash tables. Must be a power of two.
273 * Note that this can be overridden by the kernel environment
274 * variable net.inet.tcp.tcbhashsize
277 #define TCBHASHSIZE CONFIG_TCBHASHSIZE
280 __private_extern__
int tcp_tcbhashsize
= TCBHASHSIZE
;
281 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, tcbhashsize
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
282 &tcp_tcbhashsize
, 0, "Size of TCP control-block hashtable");
285 * This is the actual shape of what we allocate using the zone
286 * allocator. Doing it this way allows us to protect both structures
287 * using the same generation count, and also eliminates the overhead
288 * of allocating tcpcbs separately. By hiding the structure here,
289 * we avoid changing most of the rest of the code (although it needs
290 * to be changed, eventually, for greater efficiency).
295 struct tcpcb tcb
__attribute__((aligned(ALIGNMENT
)));
299 int get_inpcb_str_size(void);
300 int get_tcp_str_size(void);
302 static void tcpcb_to_otcpcb(struct tcpcb
*, struct otcpcb
*);
304 static lck_attr_t
*tcp_uptime_mtx_attr
= NULL
; /* mutex attributes */
305 static lck_grp_t
*tcp_uptime_mtx_grp
= NULL
; /* mutex group definition */
306 static lck_grp_attr_t
*tcp_uptime_mtx_grp_attr
= NULL
; /* mutex group attributes */
307 int tcp_notsent_lowat_check(struct socket
*so
);
310 int get_inpcb_str_size(void)
312 return sizeof(struct inpcb
);
316 int get_tcp_str_size(void)
318 return sizeof(struct tcpcb
);
321 int tcp_freeq(struct tcpcb
*tp
);
324 * Initialize TCP congestion control algorithms.
330 bzero(&tcp_cc_algo_list
, sizeof(tcp_cc_algo_list
));
331 tcp_cc_algo_list
[TCP_CC_ALGO_NEWRENO_INDEX
] = &tcp_cc_newreno
;
332 tcp_cc_algo_list
[TCP_CC_ALGO_BACKGROUND_INDEX
] = &tcp_cc_ledbat
;
339 tcp_init(struct protosw
*pp
, struct domain
*dp
)
342 static int tcp_initialized
= 0;
344 struct inpcbinfo
*pcbinfo
;
346 VERIFY((pp
->pr_flags
& (PR_INITIALIZED
|PR_ATTACHED
)) == PR_ATTACHED
);
355 tcp_keepinit
= TCPTV_KEEP_INIT
;
356 tcp_keepidle
= TCPTV_KEEP_IDLE
;
357 tcp_keepintvl
= TCPTV_KEEPINTVL
;
358 tcp_keepcnt
= TCPTV_KEEPCNT
;
359 tcp_maxpersistidle
= TCPTV_KEEP_IDLE
;
362 microuptime(&tcp_uptime
);
363 read_random(&tcp_now
, sizeof(tcp_now
));
364 tcp_now
= tcp_now
& 0x3fffffff; /* Starts tcp internal clock at a random value */
367 tcbinfo
.ipi_listhead
= &tcb
;
371 * allocate lock group attribute and group for tcp pcb mutexes
373 pcbinfo
->ipi_lock_grp_attr
= lck_grp_attr_alloc_init();
374 pcbinfo
->ipi_lock_grp
= lck_grp_alloc_init("tcppcb", pcbinfo
->ipi_lock_grp_attr
);
377 * allocate the lock attribute for tcp pcb mutexes
379 pcbinfo
->ipi_lock_attr
= lck_attr_alloc_init();
381 if ((pcbinfo
->ipi_lock
= lck_rw_alloc_init(pcbinfo
->ipi_lock_grp
,
382 pcbinfo
->ipi_lock_attr
)) == NULL
) {
383 panic("%s: unable to allocate PCB lock\n", __func__
);
387 if (!powerof2(tcp_tcbhashsize
)) {
388 printf("WARNING: TCB hash size not a power of 2\n");
389 tcp_tcbhashsize
= 512; /* safe default */
391 tcbinfo
.ipi_hashbase
= hashinit(tcp_tcbhashsize
, M_PCB
, &tcbinfo
.ipi_hashmask
);
392 tcbinfo
.ipi_porthashbase
= hashinit(tcp_tcbhashsize
, M_PCB
,
393 &tcbinfo
.ipi_porthashmask
);
394 str_size
= P2ROUNDUP(sizeof(struct inp_tp
), sizeof(u_int64_t
));
395 tcbinfo
.ipi_zone
= zinit(str_size
, 120000*str_size
, 8192, "tcpcb");
396 zone_change(tcbinfo
.ipi_zone
, Z_CALLERACCT
, FALSE
);
397 zone_change(tcbinfo
.ipi_zone
, Z_EXPAND
, TRUE
);
399 tcbinfo
.ipi_gc
= tcp_gc
;
400 in_pcbinfo_attach(&tcbinfo
);
402 str_size
= P2ROUNDUP(sizeof(struct sackhole
), sizeof(u_int64_t
));
403 sack_hole_zone
= zinit(str_size
, 120000*str_size
, 8192, "sack_hole zone");
404 zone_change(sack_hole_zone
, Z_CALLERACCT
, FALSE
);
405 zone_change(sack_hole_zone
, Z_EXPAND
, TRUE
);
407 tcp_reass_maxseg
= nmbclusters
/ 16;
408 str_size
= P2ROUNDUP(sizeof(struct tseg_qent
), sizeof(u_int64_t
));
409 tcp_reass_zone
= zinit(str_size
, (tcp_reass_maxseg
+ 1) * str_size
,
410 0, "tcp_reass_zone");
411 if (tcp_reass_zone
== NULL
) {
412 panic("%s: failed allocating tcp_reass_zone", __func__
);
415 zone_change(tcp_reass_zone
, Z_CALLERACCT
, FALSE
);
416 zone_change(tcp_reass_zone
, Z_EXPAND
, TRUE
);
418 bwmeas_elm_size
= P2ROUNDUP(sizeof(struct bwmeas
), sizeof(u_int64_t
));
419 tcp_bwmeas_zone
= zinit(bwmeas_elm_size
, (100 * bwmeas_elm_size
), 0, "tcp_bwmeas_zone");
420 if (tcp_bwmeas_zone
== NULL
) {
421 panic("%s: failed allocating tcp_bwmeas_zone", __func__
);
424 zone_change(tcp_bwmeas_zone
, Z_CALLERACCT
, FALSE
);
425 zone_change(tcp_bwmeas_zone
, Z_EXPAND
, TRUE
);
428 #define TCP_MINPROTOHDR (sizeof(struct ip6_hdr) + sizeof(struct tcphdr))
430 #define TCP_MINPROTOHDR (sizeof(struct tcpiphdr))
432 if (max_protohdr
< TCP_MINPROTOHDR
) {
433 _max_protohdr
= TCP_MINPROTOHDR
;
434 _max_protohdr
= max_protohdr
; /* round it up */
436 if (max_linkhdr
+ max_protohdr
> MCLBYTES
)
438 #undef TCP_MINPROTOHDR
440 /* Initialize time wait and timer lists */
441 TAILQ_INIT(&tcp_tw_tailq
);
443 bzero(&tcp_timer_list
, sizeof(tcp_timer_list
));
444 LIST_INIT(&tcp_timer_list
.lhead
);
446 * allocate lock group attribute, group and attribute for the tcp timer list
448 tcp_timer_list
.mtx_grp_attr
= lck_grp_attr_alloc_init();
449 tcp_timer_list
.mtx_grp
= lck_grp_alloc_init("tcptimerlist", tcp_timer_list
.mtx_grp_attr
);
450 tcp_timer_list
.mtx_attr
= lck_attr_alloc_init();
451 if ((tcp_timer_list
.mtx
= lck_mtx_alloc_init(tcp_timer_list
.mtx_grp
, tcp_timer_list
.mtx_attr
)) == NULL
) {
452 panic("failed to allocate memory for tcp_timer_list.mtx\n");
454 tcp_timer_list
.fast_quantum
= TCP_FASTTIMER_QUANTUM
;
455 tcp_timer_list
.slow_quantum
= TCP_SLOWTIMER_QUANTUM
;
456 if ((tcp_timer_list
.call
= thread_call_allocate(tcp_run_timerlist
, NULL
)) == NULL
) {
457 panic("failed to allocate call entry 1 in tcp_init\n");
461 * allocate lock group attribute, group and attribute for tcp_uptime_lock
463 tcp_uptime_mtx_grp_attr
= lck_grp_attr_alloc_init();
464 tcp_uptime_mtx_grp
= lck_grp_alloc_init("tcpuptime", tcp_uptime_mtx_grp_attr
);
465 tcp_uptime_mtx_attr
= lck_attr_alloc_init();
466 tcp_uptime_lock
= lck_spin_alloc_init(tcp_uptime_mtx_grp
, tcp_uptime_mtx_attr
);
468 /* Initialize TCP congestion control algorithms list */
471 /* Initialize TCP LRO data structures */
476 * Fill in the IP and TCP headers for an outgoing packet, given the tcpcb.
477 * tcp_template used to store this data in mbufs, but we now recopy it out
478 * of the tcpcb each time to conserve mbufs.
481 tcp_fillheaders(tp
, ip_ptr
, tcp_ptr
)
486 struct inpcb
*inp
= tp
->t_inpcb
;
487 struct tcphdr
*tcp_hdr
= (struct tcphdr
*)tcp_ptr
;
490 if ((inp
->inp_vflag
& INP_IPV6
) != 0) {
493 ip6
= (struct ip6_hdr
*)ip_ptr
;
494 ip6
->ip6_flow
= (ip6
->ip6_flow
& ~IPV6_FLOWINFO_MASK
) |
495 (inp
->inp_flow
& IPV6_FLOWINFO_MASK
);
496 ip6
->ip6_vfc
= (ip6
->ip6_vfc
& ~IPV6_VERSION_MASK
) |
497 (IPV6_VERSION
& IPV6_VERSION_MASK
);
498 ip6
->ip6_nxt
= IPPROTO_TCP
;
499 ip6
->ip6_plen
= sizeof(struct tcphdr
);
500 ip6
->ip6_src
= inp
->in6p_laddr
;
501 ip6
->ip6_dst
= inp
->in6p_faddr
;
502 tcp_hdr
->th_sum
= in6_pseudo(&inp
->in6p_laddr
, &inp
->in6p_faddr
,
503 htonl(sizeof (struct tcphdr
) + IPPROTO_TCP
));
507 struct ip
*ip
= (struct ip
*) ip_ptr
;
509 ip
->ip_vhl
= IP_VHL_BORING
;
516 ip
->ip_p
= IPPROTO_TCP
;
517 ip
->ip_src
= inp
->inp_laddr
;
518 ip
->ip_dst
= inp
->inp_faddr
;
519 tcp_hdr
->th_sum
= in_pseudo(ip
->ip_src
.s_addr
, ip
->ip_dst
.s_addr
,
520 htons(sizeof(struct tcphdr
) + IPPROTO_TCP
));
523 tcp_hdr
->th_sport
= inp
->inp_lport
;
524 tcp_hdr
->th_dport
= inp
->inp_fport
;
529 tcp_hdr
->th_flags
= 0;
535 * Create template to be used to send tcp packets on a connection.
536 * Allocates an mbuf and fills in a skeletal tcp/ip header. The only
537 * use for this function is in keepalives, which use tcp_respond.
546 m
= m_get(M_DONTWAIT
, MT_HEADER
);
549 m
->m_len
= sizeof(struct tcptemp
);
550 n
= mtod(m
, struct tcptemp
*);
552 tcp_fillheaders(tp
, (void *)&n
->tt_ipgen
, (void *)&n
->tt_t
);
557 * Send a single message to the TCP at address specified by
558 * the given TCP/IP header. If m == 0, then we make a copy
559 * of the tcpiphdr at ti and send directly to the addressed host.
560 * This is used to force keep alive messages out using the TCP
561 * template for a connection. If flags are given then we send
562 * a message back to the TCP which originated the * segment ti,
563 * and discard the mbuf containing it and any other attached mbufs.
565 * In any case the ack and sequence number of the transmitted
566 * segment are as specified by the parameters.
568 * NOTE: If m != NULL, then ti must point to *inside* the mbuf.
574 register struct tcphdr
*th
,
575 register struct mbuf
*m
,
579 unsigned int ifscope
,
585 struct route
*ro
= 0;
590 struct route_in6
*ro6
= 0;
591 struct route_in6 sro6
;
598 isipv6
= IP_VHL_V(((struct ip
*)ipgen
)->ip_vhl
) == 6;
604 if (!(flags
& TH_RST
)) {
605 win
= tcp_sbspace(tp
);
606 if (win
> (int32_t)TCP_MAXWIN
<< tp
->rcv_scale
)
607 win
= (int32_t)TCP_MAXWIN
<< tp
->rcv_scale
;
611 ro6
= &tp
->t_inpcb
->in6p_route
;
614 ro
= &tp
->t_inpcb
->inp_route
;
619 bzero(ro6
, sizeof *ro6
);
624 bzero(ro
, sizeof *ro
);
628 m
= m_gethdr(M_DONTWAIT
, MT_HEADER
); /* MAC-OK */
632 m
->m_data
+= max_linkhdr
;
635 VERIFY((MHLEN
- max_linkhdr
) >=
636 (sizeof (*ip6
) + sizeof (*nth
)));
637 bcopy((caddr_t
)ip6
, mtod(m
, caddr_t
),
638 sizeof(struct ip6_hdr
));
639 ip6
= mtod(m
, struct ip6_hdr
*);
640 nth
= (struct tcphdr
*)(void *)(ip6
+ 1);
644 VERIFY((MHLEN
- max_linkhdr
) >=
645 (sizeof (*ip
) + sizeof (*nth
)));
646 bcopy((caddr_t
)ip
, mtod(m
, caddr_t
), sizeof(struct ip
));
647 ip
= mtod(m
, struct ip
*);
648 nth
= (struct tcphdr
*)(void *)(ip
+ 1);
650 bcopy((caddr_t
)th
, (caddr_t
)nth
, sizeof(struct tcphdr
));
652 if ((tp
) && (tp
->t_mpflags
& TMPF_RESET
))
653 flags
= (TH_RST
| TH_ACK
);
660 m
->m_data
= (caddr_t
)ipgen
;
661 /* m_len is set later */
663 #define xchg(a,b,type) { type t; t=a; a=b; b=t; }
666 /* Expect 32-bit aligned IP on strict-align platforms */
667 IP6_HDR_STRICT_ALIGNMENT_CHECK(ip6
);
668 xchg(ip6
->ip6_dst
, ip6
->ip6_src
, struct in6_addr
);
669 nth
= (struct tcphdr
*)(void *)(ip6
+ 1);
673 /* Expect 32-bit aligned IP on strict-align platforms */
674 IP_HDR_STRICT_ALIGNMENT_CHECK(ip
);
675 xchg(ip
->ip_dst
.s_addr
, ip
->ip_src
.s_addr
, n_long
);
676 nth
= (struct tcphdr
*)(void *)(ip
+ 1);
680 * this is usually a case when an extension header
681 * exists between the IPv6 header and the
684 nth
->th_sport
= th
->th_sport
;
685 nth
->th_dport
= th
->th_dport
;
687 xchg(nth
->th_dport
, nth
->th_sport
, n_short
);
692 ip6
->ip6_plen
= htons((u_short
)(sizeof (struct tcphdr
) +
694 tlen
+= sizeof (struct ip6_hdr
) + sizeof (struct tcphdr
);
698 tlen
+= sizeof (struct tcpiphdr
);
700 ip
->ip_ttl
= ip_defttl
;
703 m
->m_pkthdr
.len
= tlen
;
704 m
->m_pkthdr
.rcvif
= 0;
706 if (tp
!= NULL
&& tp
->t_inpcb
!= NULL
) {
708 * Packet is associated with a socket, so allow the
709 * label of the response to reflect the socket label.
711 mac_mbuf_label_associate_inpcb(tp
->t_inpcb
, m
);
714 * Packet is not associated with a socket, so possibly
715 * update the label in place.
717 mac_netinet_tcp_reply(m
);
721 nth
->th_seq
= htonl(seq
);
722 nth
->th_ack
= htonl(ack
);
724 nth
->th_off
= sizeof (struct tcphdr
) >> 2;
725 nth
->th_flags
= flags
;
727 nth
->th_win
= htons((u_short
) (win
>> tp
->rcv_scale
));
729 nth
->th_win
= htons((u_short
)win
);
734 nth
->th_sum
= in6_pseudo(&ip6
->ip6_src
, &ip6
->ip6_dst
,
735 htonl((tlen
- sizeof (struct ip6_hdr
)) + IPPROTO_TCP
));
736 m
->m_pkthdr
.csum_flags
= CSUM_TCPIPV6
;
737 m
->m_pkthdr
.csum_data
= offsetof(struct tcphdr
, th_sum
);
738 ip6
->ip6_hlim
= in6_selecthlim(tp
? tp
->t_inpcb
: NULL
,
745 nth
->th_sum
= in_pseudo(ip
->ip_src
.s_addr
, ip
->ip_dst
.s_addr
,
746 htons((u_short
)(tlen
- sizeof(struct ip
) + ip
->ip_p
)));
747 m
->m_pkthdr
.csum_flags
= CSUM_TCP
;
748 m
->m_pkthdr
.csum_data
= offsetof(struct tcphdr
, th_sum
);
751 if (tp
== NULL
|| (tp
->t_inpcb
->inp_socket
->so_options
& SO_DEBUG
))
752 tcp_trace(TA_OUTPUT
, 0, tp
, mtod(m
, void *), th
, 0);
755 if (ipsec_bypass
== 0 && ipsec_setsocket(m
, tp
? tp
->t_inpcb
->inp_socket
: NULL
) != 0) {
762 u_int32_t svc_flags
= 0;
764 svc_flags
|= PKT_SCF_IPV6
;
766 set_packet_service_class(m
, tp
->t_inpcb
->inp_socket
,
767 MBUF_SC_UNSPEC
, svc_flags
);
769 /* Embed flowhash and flow control flags */
770 m
->m_pkthdr
.pkt_flowsrc
= FLOWSRC_INPCB
;
771 m
->m_pkthdr
.pkt_flowid
= tp
->t_inpcb
->inp_flowhash
;
772 m
->m_pkthdr
.pkt_flags
|= PKTF_FLOW_ID
| PKTF_FLOW_LOCALSRC
;
774 /* Disable flow advisory when using MPTCP. */
775 if (!(tp
->t_mpflags
& TMPF_MPTCP_TRUE
))
777 m
->m_pkthdr
.pkt_flags
|= PKTF_FLOW_ADV
;
778 m
->m_pkthdr
.pkt_proto
= IPPROTO_TCP
;
783 struct ip6_out_args ip6oa
= { ifscope
, { 0 },
784 IP6OAF_SELECT_SRCIF
| IP6OAF_BOUND_SRCADDR
, 0 };
786 if (ifscope
!= IFSCOPE_NONE
)
787 ip6oa
.ip6oa_flags
|= IP6OAF_BOUND_IF
;
789 ip6oa
.ip6oa_flags
|= IP6OAF_NO_CELLULAR
;
791 (void) ip6_output(m
, NULL
, ro6
, IPV6_OUTARGS
, NULL
,
794 if (tp
!= NULL
&& ro6
!= NULL
&& ro6
->ro_rt
!= NULL
&&
795 (outif
= ro6
->ro_rt
->rt_ifp
) !=
796 tp
->t_inpcb
->in6p_last_outifp
)
797 tp
->t_inpcb
->in6p_last_outifp
= outif
;
804 struct ip_out_args ipoa
= { ifscope
, { 0 },
805 IPOAF_SELECT_SRCIF
| IPOAF_BOUND_SRCADDR
, 0 };
807 if (ifscope
!= IFSCOPE_NONE
)
808 ipoa
.ipoa_flags
|= IPOAF_BOUND_IF
;
810 ipoa
.ipoa_flags
|= IPOAF_NO_CELLULAR
;
813 /* Copy the cached route and take an extra reference */
814 inp_route_copyout(tp
->t_inpcb
, &sro
);
817 * For consistency, pass a local route copy.
819 (void) ip_output(m
, NULL
, &sro
, IP_OUTARGS
, NULL
, &ipoa
);
821 if (tp
!= NULL
&& sro
.ro_rt
!= NULL
&&
822 (outif
= sro
.ro_rt
->rt_ifp
) !=
823 tp
->t_inpcb
->inp_last_outifp
)
824 tp
->t_inpcb
->inp_last_outifp
= outif
;
827 /* Synchronize cached PCB route */
828 inp_route_copyin(tp
->t_inpcb
, &sro
);
836 * Create a new TCP control block, making an
837 * empty reassembly queue and hooking it to the argument
838 * protocol control block. The `inp' parameter must have
839 * come from the zone allocator set up in tcp_init().
846 register struct tcpcb
*tp
;
847 register struct socket
*so
= inp
->inp_socket
;
849 int isipv6
= (inp
->inp_vflag
& INP_IPV6
) != 0;
852 calculate_tcp_clock();
854 if (!so
->cached_in_sock_layer
) {
855 it
= (struct inp_tp
*)(void *)inp
;
858 tp
= (struct tcpcb
*)(void *)inp
->inp_saved_ppcb
;
861 bzero((char *) tp
, sizeof(struct tcpcb
));
862 LIST_INIT(&tp
->t_segq
);
863 tp
->t_maxseg
= tp
->t_maxopd
=
865 isipv6
? tcp_v6mssdflt
:
870 tp
->t_flags
= (TF_REQ_SCALE
|TF_REQ_TSTMP
);
872 tp
->t_flagsext
|= TF_SACK_ENABLE
;
874 TAILQ_INIT(&tp
->snd_holes
);
875 tp
->t_inpcb
= inp
; /* XXX */
877 * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no
878 * rtt estimate. Set rttvar so that srtt + 4 * rttvar gives
879 * reasonable initial retransmit time.
881 tp
->t_srtt
= TCPTV_SRTTBASE
;
882 tp
->t_rttvar
= ((TCPTV_RTOBASE
- TCPTV_SRTTBASE
) << TCP_RTTVAR_SHIFT
) / 4;
883 tp
->t_rttmin
= tcp_TCPTV_MIN
;
884 tp
->t_rxtcur
= TCPTV_RTOBASE
;
886 /* Initialize congestion control algorithm for this connection
887 * to newreno by default
889 tp
->tcp_cc_index
= TCP_CC_ALGO_NEWRENO_INDEX
;
890 if (CC_ALGO(tp
)->init
!= NULL
) {
891 CC_ALGO(tp
)->init(tp
);
894 tp
->snd_cwnd
= TCP_MAXWIN
<< TCP_MAX_WINSHIFT
;
895 tp
->snd_ssthresh
= TCP_MAXWIN
<< TCP_MAX_WINSHIFT
;
896 tp
->snd_ssthresh_prev
= TCP_MAXWIN
<< TCP_MAX_WINSHIFT
;
897 tp
->t_rcvtime
= tcp_now
;
898 tp
->tentry
.timer_start
= tcp_now
;
899 tp
->t_persist_timeout
= tcp_max_persist_timeout
;
900 tp
->t_persist_stop
= 0;
901 tp
->t_flagsext
|= TF_RCVUNACK_WAITSS
;
902 tp
->t_rexmtthresh
= tcprexmtthresh
;
904 /* Clear time wait tailq entry */
905 tp
->t_twentry
.tqe_next
= NULL
;
906 tp
->t_twentry
.tqe_prev
= NULL
;
909 * IPv4 TTL initialization is necessary for an IPv6 socket as well,
910 * because the socket may be bound to an IPv6 wildcard address,
911 * which may match an IPv4-mapped IPv6 address.
913 inp
->inp_ip_ttl
= ip_defttl
;
914 inp
->inp_ppcb
= (caddr_t
)tp
;
915 return (tp
); /* XXX */
919 * Drop a TCP connection, reporting
920 * the specified error. If connection is synchronized,
921 * then send a RST to peer.
925 register struct tcpcb
*tp
;
928 struct socket
*so
= tp
->t_inpcb
->inp_socket
;
930 struct inpcb
*inp
= tp
->t_inpcb
;
933 if (TCPS_HAVERCVDSYN(tp
->t_state
)) {
934 DTRACE_TCP4(state__change
, void, NULL
, struct inpcb
*, inp
,
935 struct tcpcb
*, tp
, int32_t, TCPS_CLOSED
);
936 tp
->t_state
= TCPS_CLOSED
;
937 (void) tcp_output(tp
);
938 tcpstat
.tcps_drops
++;
940 tcpstat
.tcps_conndrops
++;
941 if (errno
== ETIMEDOUT
&& tp
->t_softerror
)
942 errno
= tp
->t_softerror
;
943 so
->so_error
= errno
;
944 return (tcp_close(tp
));
948 tcp_getrt_rtt(struct tcpcb
*tp
, struct rtentry
*rt
)
950 u_int32_t rtt
= rt
->rt_rmx
.rmx_rtt
;
951 int isnetlocal
= (tp
->t_flags
& TF_LOCAL
);
955 * XXX the lock bit for RTT indicates that the value
956 * is also a minimum value; this is subject to time.
958 if (rt
->rt_rmx
.rmx_locks
& RTV_RTT
)
959 tp
->t_rttmin
= rtt
/ (RTM_RTTUNIT
/ TCP_RETRANSHZ
);
961 tp
->t_rttmin
= isnetlocal
? tcp_TCPTV_MIN
: TCPTV_REXMTMIN
;
962 tp
->t_srtt
= rtt
/ (RTM_RTTUNIT
/ (TCP_RETRANSHZ
* TCP_RTT_SCALE
));
963 tcpstat
.tcps_usedrtt
++;
964 if (rt
->rt_rmx
.rmx_rttvar
) {
965 tp
->t_rttvar
= rt
->rt_rmx
.rmx_rttvar
/
966 (RTM_RTTUNIT
/ (TCP_RETRANSHZ
* TCP_RTTVAR_SCALE
));
967 tcpstat
.tcps_usedrttvar
++;
969 /* default variation is +- 1 rtt */
971 tp
->t_srtt
* TCP_RTTVAR_SCALE
/ TCP_RTT_SCALE
;
973 TCPT_RANGESET(tp
->t_rxtcur
,
974 ((tp
->t_srtt
>> 2) + tp
->t_rttvar
) >> 1,
975 tp
->t_rttmin
, TCPTV_REXMTMAX
,
976 TCP_ADD_REXMTSLOP(tp
));
981 * Close a TCP control block:
982 * discard all space held by the tcp
983 * discard internet protocol block
984 * wake up any sleepers
988 register struct tcpcb
*tp
;
990 struct inpcb
*inp
= tp
->t_inpcb
;
991 struct socket
*so
= inp
->inp_socket
;
993 int isipv6
= (inp
->inp_vflag
& INP_IPV6
) != 0;
999 /* tcp_close was called previously, bail */
1000 if ( inp
->inp_ppcb
== NULL
)
1003 tcp_canceltimers(tp
);
1004 KERNEL_DEBUG(DBG_FNC_TCP_CLOSE
| DBG_FUNC_START
, tp
,0,0,0,0);
1007 * If another thread for this tcp is currently in ip (indicated by
1008 * the TF_SENDINPROG flag), defer the cleanup until after it returns
1009 * back to tcp. This is done to serialize the close until after all
1010 * pending output is finished, in order to avoid having the PCB be
1011 * detached and the cached route cleaned, only for ip to cache the
1012 * route back into the PCB again. Note that we've cleared all the
1013 * timers at this point. Set TF_CLOSING to indicate to tcp_output()
1014 * that is should call us again once it returns from ip; at that
1015 * point both flags should be cleared and we can proceed further
1018 if ((tp
->t_flags
& TF_CLOSING
) ||
1019 inp
->inp_sndinprog_cnt
> 0) {
1020 tp
->t_flags
|= TF_CLOSING
;
1024 DTRACE_TCP4(state__change
, void, NULL
, struct inpcb
*, inp
,
1025 struct tcpcb
*, tp
, int32_t, TCPS_CLOSED
);
1027 if (CC_ALGO(tp
)->cleanup
!= NULL
) {
1028 CC_ALGO(tp
)->cleanup(tp
);
1032 ro
= (isipv6
? (struct route
*)&inp
->in6p_route
: &inp
->inp_route
);
1034 ro
= &inp
->inp_route
;
1041 * If we got enough samples through the srtt filter,
1042 * save the rtt and rttvar in the routing entry.
1043 * 'Enough' is arbitrarily defined as the 16 samples.
1044 * 16 samples is enough for the srtt filter to converge
1045 * to within 5% of the correct value; fewer samples and
1046 * we could save a very bogus rtt.
1048 * Don't update the default route's characteristics and don't
1049 * update anything that the user "locked".
1051 if (tp
->t_rttupdated
>= 16) {
1052 register u_int32_t i
= 0;
1056 struct sockaddr_in6
*sin6
;
1060 sin6
= (struct sockaddr_in6
*)(void *)rt_key(rt
);
1061 if (IN6_IS_ADDR_UNSPECIFIED(&sin6
->sin6_addr
))
1066 if (ROUTE_UNUSABLE(ro
) ||
1067 SIN(rt_key(rt
))->sin_addr
.s_addr
== INADDR_ANY
) {
1068 if (tp
->t_state
>= TCPS_CLOSE_WAIT
) {
1069 DTRACE_TCP4(state__change
,
1070 void, NULL
, struct inpcb
*, inp
,
1071 struct tcpcb
*, tp
, int32_t,
1073 tp
->t_state
= TCPS_CLOSING
;
1078 RT_LOCK_ASSERT_HELD(rt
);
1079 if ((rt
->rt_rmx
.rmx_locks
& RTV_RTT
) == 0) {
1081 (RTM_RTTUNIT
/ (TCP_RETRANSHZ
* TCP_RTT_SCALE
));
1082 if (rt
->rt_rmx
.rmx_rtt
&& i
)
1084 * filter this update to half the old & half
1085 * the new values, converting scale.
1086 * See route.h and tcp_var.h for a
1087 * description of the scaling constants.
1089 rt
->rt_rmx
.rmx_rtt
=
1090 (rt
->rt_rmx
.rmx_rtt
+ i
) / 2;
1092 rt
->rt_rmx
.rmx_rtt
= i
;
1093 tcpstat
.tcps_cachedrtt
++;
1095 if ((rt
->rt_rmx
.rmx_locks
& RTV_RTTVAR
) == 0) {
1097 (RTM_RTTUNIT
/ (TCP_RETRANSHZ
* TCP_RTTVAR_SCALE
));
1098 if (rt
->rt_rmx
.rmx_rttvar
&& i
)
1099 rt
->rt_rmx
.rmx_rttvar
=
1100 (rt
->rt_rmx
.rmx_rttvar
+ i
) / 2;
1102 rt
->rt_rmx
.rmx_rttvar
= i
;
1103 tcpstat
.tcps_cachedrttvar
++;
1106 * The old comment here said:
1107 * update the pipelimit (ssthresh) if it has been updated
1108 * already or if a pipesize was specified & the threshhold
1109 * got below half the pipesize. I.e., wait for bad news
1110 * before we start updating, then update on both good
1113 * But we want to save the ssthresh even if no pipesize is
1114 * specified explicitly in the route, because such
1115 * connections still have an implicit pipesize specified
1116 * by the global tcp_sendspace. In the absence of a reliable
1117 * way to calculate the pipesize, it will have to do.
1119 i
= tp
->snd_ssthresh
;
1120 if (rt
->rt_rmx
.rmx_sendpipe
!= 0)
1121 dosavessthresh
= (i
< rt
->rt_rmx
.rmx_sendpipe
/ 2);
1123 dosavessthresh
= (i
< so
->so_snd
.sb_hiwat
/ 2);
1124 if (((rt
->rt_rmx
.rmx_locks
& RTV_SSTHRESH
) == 0 &&
1125 i
!= 0 && rt
->rt_rmx
.rmx_ssthresh
!= 0)
1126 || dosavessthresh
) {
1128 * convert the limit from user data bytes to
1129 * packets then to packet data bytes.
1131 i
= (i
+ tp
->t_maxseg
/ 2) / tp
->t_maxseg
;
1134 i
*= (u_int32_t
)(tp
->t_maxseg
+
1136 (isipv6
? sizeof (struct ip6_hdr
) +
1137 sizeof (struct tcphdr
) :
1139 sizeof (struct tcpiphdr
)
1144 if (rt
->rt_rmx
.rmx_ssthresh
)
1145 rt
->rt_rmx
.rmx_ssthresh
=
1146 (rt
->rt_rmx
.rmx_ssthresh
+ i
) / 2;
1148 rt
->rt_rmx
.rmx_ssthresh
= i
;
1149 tcpstat
.tcps_cachedssthresh
++;
1154 * Mark route for deletion if no information is cached.
1156 if (rt
!= NULL
&& (so
->so_flags
& SOF_OVERFLOW
) && tcp_lq_overflow
) {
1157 if (!(rt
->rt_rmx
.rmx_locks
& RTV_RTT
) &&
1158 rt
->rt_rmx
.rmx_rtt
== 0) {
1159 rt
->rt_flags
|= RTF_DELCLONE
;
1167 /* free the reassembly queue, if any */
1168 (void) tcp_freeq(tp
);
1170 tcp_free_sackholes(tp
);
1171 if (tp
->t_bwmeas
!= NULL
) {
1172 tcp_bwmeas_free(tp
);
1175 /* Free the packet list */
1176 if (tp
->t_pktlist_head
!= NULL
)
1177 m_freem_list(tp
->t_pktlist_head
);
1178 TCP_PKTLIST_CLEAR(tp
);
1181 /* Clear MPTCP state */
1185 if (so
->cached_in_sock_layer
)
1186 inp
->inp_saved_ppcb
= (caddr_t
) tp
;
1188 /* Issue a wakeup before detach so that we don't miss
1191 sodisconnectwakeup(so
);
1194 * Clean up any LRO state
1196 if (tp
->t_flagsext
& TF_LRO_OFFLOADED
) {
1197 tcp_lro_remove_state(inp
->inp_laddr
, inp
->inp_faddr
,
1200 tp
->t_flagsext
&= ~TF_LRO_OFFLOADED
;
1202 tp
->t_state
= TCPS_CLOSED
;
1204 if (SOCK_CHECK_DOM(so
, PF_INET6
))
1210 /* Call soisdisconnected after detach because it might unlock the socket */
1211 soisdisconnected(so
);
1212 tcpstat
.tcps_closed
++;
1213 KERNEL_DEBUG(DBG_FNC_TCP_CLOSE
| DBG_FUNC_END
, tcpstat
.tcps_closed
,0,0,0,0);
1222 register struct tseg_qent
*q
;
1225 while((q
= LIST_FIRST(&tp
->t_segq
)) != NULL
) {
1226 LIST_REMOVE(q
, tqe_q
);
1228 zfree(tcp_reass_zone
, q
);
1243 * Walk the tcpbs, if existing, and flush the reassembly queue,
1244 * if there is one...
1245 * Do it next time if the pcbinfo lock is in use
1247 if (!lck_rw_try_lock_exclusive(tcbinfo
.ipi_lock
))
1250 LIST_FOREACH(inp
, tcbinfo
.ipi_listhead
, inp_list
) {
1251 if (in_pcb_checkstate(inp
, WNT_ACQUIRE
, 0) !=
1253 tcp_lock(inp
->inp_socket
, 1, 0);
1254 if (in_pcb_checkstate(inp
, WNT_RELEASE
, 1)
1256 /* lost a race, try the next one */
1257 tcp_unlock(inp
->inp_socket
, 1, 0);
1260 tp
= intotcpcb(inp
);
1262 tcp_unlock(inp
->inp_socket
, 1, 0);
1265 lck_rw_done(tcbinfo
.ipi_lock
);
1271 * Notify a tcp user of an asynchronous error;
1272 * store error as soft error, but wake up user
1273 * (for now, won't do anything until can select for soft error).
1275 * Do not wake up user since there currently is no mechanism for
1276 * reporting soft errors (yet - a kqueue filter may be added).
1279 tcp_notify(inp
, error
)
1285 if (inp
== NULL
|| (inp
->inp_state
== INPCB_STATE_DEAD
))
1286 return; /* pcb is gone already */
1288 tp
= (struct tcpcb
*)inp
->inp_ppcb
;
1291 * Ignore some errors if we are hooked up.
1292 * If connection hasn't completed, has retransmitted several times,
1293 * and receives a second error, give up now. This is better
1294 * than waiting a long time to establish a connection that
1295 * can never complete.
1297 if (tp
->t_state
== TCPS_ESTABLISHED
&&
1298 (error
== EHOSTUNREACH
|| error
== ENETUNREACH
||
1299 error
== EHOSTDOWN
)) {
1301 } else if (tp
->t_state
< TCPS_ESTABLISHED
&& tp
->t_rxtshift
> 3 &&
1303 tcp_drop(tp
, error
);
1305 tp
->t_softerror
= error
;
1307 wakeup((caddr_t
) &so
->so_timeo
);
1314 tcp_bwmeas_alloc(struct tcpcb
*tp
)
1317 elm
= zalloc(tcp_bwmeas_zone
);
1321 bzero(elm
, bwmeas_elm_size
);
1322 elm
->bw_minsizepkts
= TCP_BWMEAS_BURST_MINSIZE
;
1323 elm
->bw_maxsizepkts
= TCP_BWMEAS_BURST_MAXSIZE
;
1324 elm
->bw_minsize
= elm
->bw_minsizepkts
* tp
->t_maxseg
;
1325 elm
->bw_maxsize
= elm
->bw_maxsizepkts
* tp
->t_maxseg
;
1330 tcp_bwmeas_free(struct tcpcb
* tp
)
1332 zfree(tcp_bwmeas_zone
, tp
->t_bwmeas
);
1333 tp
->t_bwmeas
= NULL
;
1334 tp
->t_flagsext
&= ~(TF_MEASURESNDBW
);
1338 * tcpcb_to_otcpcb copies specific bits of a tcpcb to a otcpcb format.
1339 * The otcpcb data structure is passed to user space and must not change.
1342 tcpcb_to_otcpcb(struct tcpcb
*tp
, struct otcpcb
*otp
)
1346 otp
->t_segq
= (u_int32_t
)(uintptr_t)tp
->t_segq
.lh_first
;
1347 otp
->t_dupacks
= tp
->t_dupacks
;
1348 for (i
= 0; i
< TCPT_NTIMERS_EXT
; i
++)
1349 otp
->t_timer
[i
] = tp
->t_timer
[i
];
1350 otp
->t_inpcb
= (_TCPCB_PTR(struct inpcb
*))(uintptr_t)tp
->t_inpcb
;
1351 otp
->t_state
= tp
->t_state
;
1352 otp
->t_flags
= tp
->t_flags
;
1353 otp
->t_force
= tp
->t_force
;
1354 otp
->snd_una
= tp
->snd_una
;
1355 otp
->snd_max
= tp
->snd_max
;
1356 otp
->snd_nxt
= tp
->snd_nxt
;
1357 otp
->snd_up
= tp
->snd_up
;
1358 otp
->snd_wl1
= tp
->snd_wl1
;
1359 otp
->snd_wl2
= tp
->snd_wl2
;
1362 otp
->rcv_nxt
= tp
->rcv_nxt
;
1363 otp
->rcv_adv
= tp
->rcv_adv
;
1364 otp
->rcv_wnd
= tp
->rcv_wnd
;
1365 otp
->rcv_up
= tp
->rcv_up
;
1366 otp
->snd_wnd
= tp
->snd_wnd
;
1367 otp
->snd_cwnd
= tp
->snd_cwnd
;
1368 otp
->snd_ssthresh
= tp
->snd_ssthresh
;
1369 otp
->t_maxopd
= tp
->t_maxopd
;
1370 otp
->t_rcvtime
= tp
->t_rcvtime
;
1371 otp
->t_starttime
= tp
->t_starttime
;
1372 otp
->t_rtttime
= tp
->t_rtttime
;
1373 otp
->t_rtseq
= tp
->t_rtseq
;
1374 otp
->t_rxtcur
= tp
->t_rxtcur
;
1375 otp
->t_maxseg
= tp
->t_maxseg
;
1376 otp
->t_srtt
= tp
->t_srtt
;
1377 otp
->t_rttvar
= tp
->t_rttvar
;
1378 otp
->t_rxtshift
= tp
->t_rxtshift
;
1379 otp
->t_rttmin
= tp
->t_rttmin
;
1380 otp
->t_rttupdated
= tp
->t_rttupdated
;
1381 otp
->max_sndwnd
= tp
->max_sndwnd
;
1382 otp
->t_softerror
= tp
->t_softerror
;
1383 otp
->t_oobflags
= tp
->t_oobflags
;
1384 otp
->t_iobc
= tp
->t_iobc
;
1385 otp
->snd_scale
= tp
->snd_scale
;
1386 otp
->rcv_scale
= tp
->rcv_scale
;
1387 otp
->request_r_scale
= tp
->request_r_scale
;
1388 otp
->requested_s_scale
= tp
->requested_s_scale
;
1389 otp
->ts_recent
= tp
->ts_recent
;
1390 otp
->ts_recent_age
= tp
->ts_recent_age
;
1391 otp
->last_ack_sent
= tp
->last_ack_sent
;
1392 otp
->cc_send
= tp
->cc_send
;
1393 otp
->cc_recv
= tp
->cc_recv
;
1394 otp
->snd_recover
= tp
->snd_recover
;
1395 otp
->snd_cwnd_prev
= tp
->snd_cwnd_prev
;
1396 otp
->snd_ssthresh_prev
= tp
->snd_ssthresh_prev
;
1397 otp
->t_badrxtwin
= 0;
1401 tcp_pcblist SYSCTL_HANDLER_ARGS
1403 #pragma unused(oidp, arg1, arg2)
1404 int error
, i
= 0, n
;
1405 struct inpcb
*inp
, **inp_list
;
1411 * The process of preparing the TCB list is too time-consuming and
1412 * resource-intensive to repeat twice on every request.
1414 lck_rw_lock_shared(tcbinfo
.ipi_lock
);
1415 if (req
->oldptr
== USER_ADDR_NULL
) {
1416 n
= tcbinfo
.ipi_count
;
1417 req
->oldidx
= 2 * (sizeof xig
)
1418 + (n
+ n
/8) * sizeof(struct xtcpcb
);
1419 lck_rw_done(tcbinfo
.ipi_lock
);
1423 if (req
->newptr
!= USER_ADDR_NULL
) {
1424 lck_rw_done(tcbinfo
.ipi_lock
);
1429 * OK, now we're committed to doing something.
1431 gencnt
= tcbinfo
.ipi_gencnt
;
1432 n
= tcbinfo
.ipi_count
;
1434 bzero(&xig
, sizeof(xig
));
1435 xig
.xig_len
= sizeof xig
;
1437 xig
.xig_gen
= gencnt
;
1438 xig
.xig_sogen
= so_gencnt
;
1439 error
= SYSCTL_OUT(req
, &xig
, sizeof xig
);
1441 lck_rw_done(tcbinfo
.ipi_lock
);
1445 * We are done if there is no pcb
1448 lck_rw_done(tcbinfo
.ipi_lock
);
1452 inp_list
= _MALLOC(n
* sizeof *inp_list
, M_TEMP
, M_WAITOK
);
1453 if (inp_list
== 0) {
1454 lck_rw_done(tcbinfo
.ipi_lock
);
1458 LIST_FOREACH(inp
, tcbinfo
.ipi_listhead
, inp_list
) {
1459 if (inp
->inp_gencnt
<= gencnt
&&
1460 inp
->inp_state
!= INPCB_STATE_DEAD
)
1461 inp_list
[i
++] = inp
;
1465 TAILQ_FOREACH(tp
, &tcp_tw_tailq
, t_twentry
) {
1467 if (inp
->inp_gencnt
<= gencnt
&&
1468 inp
->inp_state
!= INPCB_STATE_DEAD
)
1469 inp_list
[i
++] = inp
;
1476 for (i
= 0; i
< n
; i
++) {
1478 if (inp
->inp_gencnt
<= gencnt
&&
1479 inp
->inp_state
!= INPCB_STATE_DEAD
) {
1483 bzero(&xt
, sizeof(xt
));
1484 xt
.xt_len
= sizeof xt
;
1485 /* XXX should avoid extra copy */
1486 inpcb_to_compat(inp
, &xt
.xt_inp
);
1487 inp_ppcb
= inp
->inp_ppcb
;
1488 if (inp_ppcb
!= NULL
) {
1490 (struct tcpcb
*)(void *)inp_ppcb
,
1493 bzero((char *) &xt
.xt_tp
, sizeof xt
.xt_tp
);
1495 if (inp
->inp_socket
)
1496 sotoxsocket(inp
->inp_socket
, &xt
.xt_socket
);
1497 error
= SYSCTL_OUT(req
, &xt
, sizeof xt
);
1502 * Give the user an updated idea of our state.
1503 * If the generation differs from what we told
1504 * her before, she knows that something happened
1505 * while we were processing this request, and it
1506 * might be necessary to retry.
1508 bzero(&xig
, sizeof(xig
));
1509 xig
.xig_len
= sizeof xig
;
1510 xig
.xig_gen
= tcbinfo
.ipi_gencnt
;
1511 xig
.xig_sogen
= so_gencnt
;
1512 xig
.xig_count
= tcbinfo
.ipi_count
;
1513 error
= SYSCTL_OUT(req
, &xig
, sizeof xig
);
1515 FREE(inp_list
, M_TEMP
);
1516 lck_rw_done(tcbinfo
.ipi_lock
);
1520 SYSCTL_PROC(_net_inet_tcp
, TCPCTL_PCBLIST
, pcblist
, CTLFLAG_RD
| CTLFLAG_LOCKED
, 0, 0,
1521 tcp_pcblist
, "S,xtcpcb", "List of active TCP connections");
1525 tcpcb_to_xtcpcb64(struct tcpcb
*tp
, struct xtcpcb64
*otp
)
1529 otp
->t_segq
= (u_int32_t
)(uintptr_t)tp
->t_segq
.lh_first
;
1530 otp
->t_dupacks
= tp
->t_dupacks
;
1531 for (i
= 0; i
< TCPT_NTIMERS_EXT
; i
++)
1532 otp
->t_timer
[i
] = tp
->t_timer
[i
];
1533 otp
->t_state
= tp
->t_state
;
1534 otp
->t_flags
= tp
->t_flags
;
1535 otp
->t_force
= tp
->t_force
;
1536 otp
->snd_una
= tp
->snd_una
;
1537 otp
->snd_max
= tp
->snd_max
;
1538 otp
->snd_nxt
= tp
->snd_nxt
;
1539 otp
->snd_up
= tp
->snd_up
;
1540 otp
->snd_wl1
= tp
->snd_wl1
;
1541 otp
->snd_wl2
= tp
->snd_wl2
;
1544 otp
->rcv_nxt
= tp
->rcv_nxt
;
1545 otp
->rcv_adv
= tp
->rcv_adv
;
1546 otp
->rcv_wnd
= tp
->rcv_wnd
;
1547 otp
->rcv_up
= tp
->rcv_up
;
1548 otp
->snd_wnd
= tp
->snd_wnd
;
1549 otp
->snd_cwnd
= tp
->snd_cwnd
;
1550 otp
->snd_ssthresh
= tp
->snd_ssthresh
;
1551 otp
->t_maxopd
= tp
->t_maxopd
;
1552 otp
->t_rcvtime
= tp
->t_rcvtime
;
1553 otp
->t_starttime
= tp
->t_starttime
;
1554 otp
->t_rtttime
= tp
->t_rtttime
;
1555 otp
->t_rtseq
= tp
->t_rtseq
;
1556 otp
->t_rxtcur
= tp
->t_rxtcur
;
1557 otp
->t_maxseg
= tp
->t_maxseg
;
1558 otp
->t_srtt
= tp
->t_srtt
;
1559 otp
->t_rttvar
= tp
->t_rttvar
;
1560 otp
->t_rxtshift
= tp
->t_rxtshift
;
1561 otp
->t_rttmin
= tp
->t_rttmin
;
1562 otp
->t_rttupdated
= tp
->t_rttupdated
;
1563 otp
->max_sndwnd
= tp
->max_sndwnd
;
1564 otp
->t_softerror
= tp
->t_softerror
;
1565 otp
->t_oobflags
= tp
->t_oobflags
;
1566 otp
->t_iobc
= tp
->t_iobc
;
1567 otp
->snd_scale
= tp
->snd_scale
;
1568 otp
->rcv_scale
= tp
->rcv_scale
;
1569 otp
->request_r_scale
= tp
->request_r_scale
;
1570 otp
->requested_s_scale
= tp
->requested_s_scale
;
1571 otp
->ts_recent
= tp
->ts_recent
;
1572 otp
->ts_recent_age
= tp
->ts_recent_age
;
1573 otp
->last_ack_sent
= tp
->last_ack_sent
;
1574 otp
->cc_send
= tp
->cc_send
;
1575 otp
->cc_recv
= tp
->cc_recv
;
1576 otp
->snd_recover
= tp
->snd_recover
;
1577 otp
->snd_cwnd_prev
= tp
->snd_cwnd_prev
;
1578 otp
->snd_ssthresh_prev
= tp
->snd_ssthresh_prev
;
1579 otp
->t_badrxtwin
= 0;
1584 tcp_pcblist64 SYSCTL_HANDLER_ARGS
1586 #pragma unused(oidp, arg1, arg2)
1587 int error
, i
= 0, n
;
1588 struct inpcb
*inp
, **inp_list
;
1594 * The process of preparing the TCB list is too time-consuming and
1595 * resource-intensive to repeat twice on every request.
1597 lck_rw_lock_shared(tcbinfo
.ipi_lock
);
1598 if (req
->oldptr
== USER_ADDR_NULL
) {
1599 n
= tcbinfo
.ipi_count
;
1600 req
->oldidx
= 2 * (sizeof xig
)
1601 + (n
+ n
/8) * sizeof(struct xtcpcb64
);
1602 lck_rw_done(tcbinfo
.ipi_lock
);
1606 if (req
->newptr
!= USER_ADDR_NULL
) {
1607 lck_rw_done(tcbinfo
.ipi_lock
);
1612 * OK, now we're committed to doing something.
1614 gencnt
= tcbinfo
.ipi_gencnt
;
1615 n
= tcbinfo
.ipi_count
;
1617 bzero(&xig
, sizeof(xig
));
1618 xig
.xig_len
= sizeof xig
;
1620 xig
.xig_gen
= gencnt
;
1621 xig
.xig_sogen
= so_gencnt
;
1622 error
= SYSCTL_OUT(req
, &xig
, sizeof xig
);
1624 lck_rw_done(tcbinfo
.ipi_lock
);
1628 * We are done if there is no pcb
1631 lck_rw_done(tcbinfo
.ipi_lock
);
1635 inp_list
= _MALLOC(n
* sizeof *inp_list
, M_TEMP
, M_WAITOK
);
1636 if (inp_list
== 0) {
1637 lck_rw_done(tcbinfo
.ipi_lock
);
1641 LIST_FOREACH(inp
, tcbinfo
.ipi_listhead
, inp_list
) {
1642 if (inp
->inp_gencnt
<= gencnt
&&
1643 inp
->inp_state
!= INPCB_STATE_DEAD
)
1644 inp_list
[i
++] = inp
;
1648 TAILQ_FOREACH(tp
, &tcp_tw_tailq
, t_twentry
) {
1650 if (inp
->inp_gencnt
<= gencnt
&&
1651 inp
->inp_state
!= INPCB_STATE_DEAD
)
1652 inp_list
[i
++] = inp
;
1659 for (i
= 0; i
< n
; i
++) {
1661 if (inp
->inp_gencnt
<= gencnt
&& inp
->inp_state
!= INPCB_STATE_DEAD
) {
1664 bzero(&xt
, sizeof(xt
));
1665 xt
.xt_len
= sizeof xt
;
1666 inpcb_to_xinpcb64(inp
, &xt
.xt_inpcb
);
1667 xt
.xt_inpcb
.inp_ppcb
= (u_int64_t
)(uintptr_t)inp
->inp_ppcb
;
1668 if (inp
->inp_ppcb
!= NULL
)
1669 tcpcb_to_xtcpcb64((struct tcpcb
*)inp
->inp_ppcb
, &xt
);
1670 if (inp
->inp_socket
)
1671 sotoxsocket64(inp
->inp_socket
, &xt
.xt_inpcb
.xi_socket
);
1672 error
= SYSCTL_OUT(req
, &xt
, sizeof xt
);
1677 * Give the user an updated idea of our state.
1678 * If the generation differs from what we told
1679 * her before, she knows that something happened
1680 * while we were processing this request, and it
1681 * might be necessary to retry.
1683 bzero(&xig
, sizeof(xig
));
1684 xig
.xig_len
= sizeof xig
;
1685 xig
.xig_gen
= tcbinfo
.ipi_gencnt
;
1686 xig
.xig_sogen
= so_gencnt
;
1687 xig
.xig_count
= tcbinfo
.ipi_count
;
1688 error
= SYSCTL_OUT(req
, &xig
, sizeof xig
);
1690 FREE(inp_list
, M_TEMP
);
1691 lck_rw_done(tcbinfo
.ipi_lock
);
1695 SYSCTL_PROC(_net_inet_tcp
, OID_AUTO
, pcblist64
, CTLFLAG_RD
| CTLFLAG_LOCKED
, 0, 0,
1696 tcp_pcblist64
, "S,xtcpcb64", "List of active TCP connections");
1700 tcp_pcblist_n SYSCTL_HANDLER_ARGS
1702 #pragma unused(oidp, arg1, arg2)
1705 error
= get_pcblist_n(IPPROTO_TCP
, req
, &tcbinfo
);
1711 SYSCTL_PROC(_net_inet_tcp
, OID_AUTO
, pcblist_n
, CTLFLAG_RD
| CTLFLAG_LOCKED
, 0, 0,
1712 tcp_pcblist_n
, "S,xtcpcb_n", "List of active TCP connections");
1715 __private_extern__
void
1716 tcp_get_ports_used(uint32_t ifindex
, int protocol
, uint32_t wildcardok
,
1719 inpcb_get_ports_used(ifindex
, protocol
, wildcardok
, bitfield
, &tcbinfo
);
1722 __private_extern__
uint32_t
1723 tcp_count_opportunistic(unsigned int ifindex
, u_int32_t flags
)
1725 return inpcb_count_opportunistic(ifindex
, &tcbinfo
, flags
);
1728 __private_extern__
uint32_t
1729 tcp_find_anypcb_byaddr(struct ifaddr
*ifa
)
1731 return inpcb_find_anypcb_byaddr(ifa
, &tcbinfo
);
1735 tcp_ctlinput(cmd
, sa
, vip
)
1737 struct sockaddr
*sa
;
1740 tcp_seq icmp_tcp_seq
;
1741 struct ip
*ip
= vip
;
1742 struct in_addr faddr
;
1746 void (*notify
)(struct inpcb
*, int) = tcp_notify
;
1748 faddr
= ((struct sockaddr_in
*)(void *)sa
)->sin_addr
;
1749 if (sa
->sa_family
!= AF_INET
|| faddr
.s_addr
== INADDR_ANY
)
1752 if (cmd
== PRC_MSGSIZE
)
1753 notify
= tcp_mtudisc
;
1754 else if (icmp_may_rst
&& (cmd
== PRC_UNREACH_ADMIN_PROHIB
||
1755 cmd
== PRC_UNREACH_PORT
) && ip
)
1756 notify
= tcp_drop_syn_sent
;
1757 else if (PRC_IS_REDIRECT(cmd
)) {
1759 notify
= in_rtchange
;
1760 } else if (cmd
== PRC_HOSTDEAD
)
1762 /* Source quench is deprecated */
1763 else if (cmd
== PRC_QUENCH
)
1765 else if ((unsigned)cmd
> PRC_NCMDS
|| inetctlerrmap
[cmd
] == 0)
1771 icp
= (struct icmp
*)(void *)
1772 ((caddr_t
)ip
- offsetof(struct icmp
, icmp_ip
));
1773 bcopy(((caddr_t
)ip
+ (IP_VHL_HL(ip
->ip_vhl
) << 2)),
1775 inp
= in_pcblookup_hash(&tcbinfo
, faddr
, th
.th_dport
,
1776 ip
->ip_src
, th
.th_sport
, 0, NULL
);
1777 if (inp
!= NULL
&& inp
->inp_socket
!= NULL
) {
1778 tcp_lock(inp
->inp_socket
, 1, 0);
1779 if (in_pcb_checkstate(inp
, WNT_RELEASE
, 1) == WNT_STOPUSING
) {
1780 tcp_unlock(inp
->inp_socket
, 1, 0);
1783 icmp_tcp_seq
= htonl(th
.th_seq
);
1784 tp
= intotcpcb(inp
);
1785 if (SEQ_GEQ(icmp_tcp_seq
, tp
->snd_una
) &&
1786 SEQ_LT(icmp_tcp_seq
, tp
->snd_max
)) {
1787 if (cmd
== PRC_MSGSIZE
) {
1791 * If we got a needfrag and there is a host route to the
1792 * original destination, and the MTU is not locked, then
1793 * set the MTU in the route to the suggested new value
1794 * (if given) and then notify as usual. The ULPs will
1795 * notice that the MTU has changed and adapt accordingly.
1796 * If no new MTU was suggested, then we guess a new one
1797 * less than the current value. If the new MTU is
1798 * unreasonably small (defined by sysctl tcp_minmss), then
1799 * we reset the MTU to the interface value and enable the
1800 * lock bit, indicating that we are no longer doing MTU
1805 struct sockaddr_in icmpsrc
= { sizeof (struct sockaddr_in
), AF_INET
,
1806 0 , { 0 }, { 0,0,0,0,0,0,0,0 } };
1807 icmpsrc
.sin_addr
= icp
->icmp_ip
.ip_dst
;
1809 rt
= rtalloc1((struct sockaddr
*)&icmpsrc
, 0,
1810 RTF_CLONING
| RTF_PRCLONING
);
1813 if ((rt
->rt_flags
& RTF_HOST
) &&
1814 !(rt
->rt_rmx
.rmx_locks
& RTV_MTU
)) {
1815 mtu
= ntohs(icp
->icmp_nextmtu
);
1817 mtu
= ip_next_mtu(rt
->rt_rmx
.
1820 printf("MTU for %s reduced to %d\n",
1822 &icmpsrc
.sin_addr
, ipv4str
,
1823 sizeof (ipv4str
)), mtu
);
1825 if (mtu
< max(296, (tcp_minmss
+
1826 sizeof (struct tcpiphdr
)))) {
1827 /* rt->rt_rmx.rmx_mtu =
1828 rt->rt_ifp->if_mtu; */
1829 rt
->rt_rmx
.rmx_locks
|= RTV_MTU
;
1830 } else if (rt
->rt_rmx
.rmx_mtu
> mtu
) {
1831 rt
->rt_rmx
.rmx_mtu
= mtu
;
1839 (*notify
)(inp
, inetctlerrmap
[cmd
]);
1841 tcp_unlock(inp
->inp_socket
, 1, 0);
1844 in_pcbnotifyall(&tcbinfo
, faddr
, inetctlerrmap
[cmd
], notify
);
1849 tcp6_ctlinput(cmd
, sa
, d
)
1851 struct sockaddr
*sa
;
1855 void (*notify
)(struct inpcb
*, int) = tcp_notify
;
1856 struct ip6_hdr
*ip6
;
1858 struct ip6ctlparam
*ip6cp
= NULL
;
1859 const struct sockaddr_in6
*sa6_src
= NULL
;
1861 struct tcp_portonly
{
1866 if (sa
->sa_family
!= AF_INET6
||
1867 sa
->sa_len
!= sizeof(struct sockaddr_in6
))
1870 if (cmd
== PRC_MSGSIZE
)
1871 notify
= tcp_mtudisc
;
1872 else if (!PRC_IS_REDIRECT(cmd
) &&
1873 ((unsigned)cmd
> PRC_NCMDS
|| inet6ctlerrmap
[cmd
] == 0))
1875 /* Source quench is deprecated */
1876 else if (cmd
== PRC_QUENCH
)
1879 /* if the parameter is from icmp6, decode it. */
1881 ip6cp
= (struct ip6ctlparam
*)d
;
1883 ip6
= ip6cp
->ip6c_ip6
;
1884 off
= ip6cp
->ip6c_off
;
1885 sa6_src
= ip6cp
->ip6c_src
;
1889 off
= 0; /* fool gcc */
1895 * XXX: We assume that when IPV6 is non NULL,
1896 * M and OFF are valid.
1899 /* check if we can safely examine src and dst ports */
1900 if (m
->m_pkthdr
.len
< off
+ sizeof(*thp
))
1903 bzero(&th
, sizeof(th
));
1904 m_copydata(m
, off
, sizeof(*thp
), (caddr_t
)&th
);
1906 in6_pcbnotify(&tcbinfo
, sa
, th
.th_dport
,
1907 (struct sockaddr
*)ip6cp
->ip6c_src
,
1908 th
.th_sport
, cmd
, NULL
, notify
);
1910 in6_pcbnotify(&tcbinfo
, sa
, 0,
1911 (struct sockaddr
*)(size_t)sa6_src
, 0, cmd
, NULL
, notify
);
1918 * Following is where TCP initial sequence number generation occurs.
1920 * There are two places where we must use initial sequence numbers:
1921 * 1. In SYN-ACK packets.
1922 * 2. In SYN packets.
1924 * The ISNs in SYN-ACK packets have no monotonicity requirement,
1925 * and should be as unpredictable as possible to avoid the possibility
1926 * of spoofing and/or connection hijacking. To satisfy this
1927 * requirement, SYN-ACK ISNs are generated via the arc4random()
1928 * function. If exact RFC 1948 compliance is requested via sysctl,
1929 * these ISNs will be generated just like those in SYN packets.
1931 * The ISNs in SYN packets must be monotonic; TIME_WAIT recycling
1932 * depends on this property. In addition, these ISNs should be
1933 * unguessable so as to prevent connection hijacking. To satisfy
1934 * the requirements of this situation, the algorithm outlined in
1935 * RFC 1948 is used to generate sequence numbers.
1937 * For more information on the theory of operation, please see
1940 * Implementation details:
1942 * Time is based off the system timer, and is corrected so that it
1943 * increases by one megabyte per second. This allows for proper
1944 * recycling on high speed LANs while still leaving over an hour
1947 * Two sysctls control the generation of ISNs:
1949 * net.inet.tcp.isn_reseed_interval controls the number of seconds
1950 * between seeding of isn_secret. This is normally set to zero,
1951 * as reseeding should not be necessary.
1953 * net.inet.tcp.strict_rfc1948 controls whether RFC 1948 is followed
1954 * strictly. When strict compliance is requested, reseeding is
1955 * disabled and SYN-ACKs will be generated in the same manner as
1956 * SYNs. Strict mode is disabled by default.
1960 #define ISN_BYTES_PER_SECOND 1048576
1966 u_int32_t md5_buffer
[4];
1968 struct timeval timenow
;
1969 u_char isn_secret
[32];
1970 int isn_last_reseed
= 0;
1973 /* Use arc4random for SYN-ACKs when not in exact RFC1948 mode. */
1974 if (((tp
->t_state
== TCPS_LISTEN
) || (tp
->t_state
== TCPS_TIME_WAIT
))
1975 && tcp_strict_rfc1948
== 0)
1977 return RandomULong();
1979 return arc4random();
1981 getmicrotime(&timenow
);
1983 /* Seed if this is the first use, reseed if requested. */
1984 if ((isn_last_reseed
== 0) ||
1985 ((tcp_strict_rfc1948
== 0) && (tcp_isn_reseed_interval
> 0) &&
1986 (((u_int
)isn_last_reseed
+ (u_int
)tcp_isn_reseed_interval
*hz
)
1987 < (u_int
)timenow
.tv_sec
))) {
1989 read_random(&isn_secret
, sizeof(isn_secret
));
1991 read_random_unlimited(&isn_secret
, sizeof(isn_secret
));
1993 isn_last_reseed
= timenow
.tv_sec
;
1996 /* Compute the md5 hash and return the ISN. */
1998 MD5Update(&isn_ctx
, (u_char
*) &tp
->t_inpcb
->inp_fport
, sizeof(u_short
));
1999 MD5Update(&isn_ctx
, (u_char
*) &tp
->t_inpcb
->inp_lport
, sizeof(u_short
));
2001 if ((tp
->t_inpcb
->inp_vflag
& INP_IPV6
) != 0) {
2002 MD5Update(&isn_ctx
, (u_char
*) &tp
->t_inpcb
->in6p_faddr
,
2003 sizeof(struct in6_addr
));
2004 MD5Update(&isn_ctx
, (u_char
*) &tp
->t_inpcb
->in6p_laddr
,
2005 sizeof(struct in6_addr
));
2009 MD5Update(&isn_ctx
, (u_char
*) &tp
->t_inpcb
->inp_faddr
,
2010 sizeof(struct in_addr
));
2011 MD5Update(&isn_ctx
, (u_char
*) &tp
->t_inpcb
->inp_laddr
,
2012 sizeof(struct in_addr
));
2014 MD5Update(&isn_ctx
, (u_char
*) &isn_secret
, sizeof(isn_secret
));
2015 MD5Final((u_char
*) &md5_buffer
, &isn_ctx
);
2016 new_isn
= (tcp_seq
) md5_buffer
[0];
2017 new_isn
+= timenow
.tv_sec
* (ISN_BYTES_PER_SECOND
/ hz
);
2023 * When a specific ICMP unreachable message is received and the
2024 * connection state is SYN-SENT, drop the connection. This behavior
2025 * is controlled by the icmp_may_rst sysctl.
2028 tcp_drop_syn_sent(inp
, errno
)
2032 struct tcpcb
*tp
= intotcpcb(inp
);
2034 if (tp
&& tp
->t_state
== TCPS_SYN_SENT
)
2035 tcp_drop(tp
, errno
);
2039 * When `need fragmentation' ICMP is received, update our idea of the MSS
2040 * based on the new value in the route. Also nudge TCP to send something,
2041 * since we know the packet we just sent was dropped.
2042 * This duplicates some code in the tcp_mss() function in tcp_input.c.
2050 struct tcpcb
*tp
= intotcpcb(inp
);
2052 struct rmxp_tao
*taop
;
2053 struct socket
*so
= inp
->inp_socket
;
2057 int isipv6
= (tp
->t_inpcb
->inp_vflag
& INP_IPV6
) != 0;
2063 rt
= tcp_rtlookup6(inp
, IFSCOPE_NONE
);
2066 rt
= tcp_rtlookup(inp
, IFSCOPE_NONE
);
2067 if (!rt
|| !rt
->rt_rmx
.rmx_mtu
) {
2068 tp
->t_maxopd
= tp
->t_maxseg
=
2070 isipv6
? tcp_v6mssdflt
:
2074 /* Route locked during lookup above */
2079 taop
= rmx_taop(rt
->rt_rmx
);
2080 offered
= taop
->tao_mssopt
;
2081 mss
= rt
->rt_rmx
.rmx_mtu
-
2084 sizeof(struct ip6_hdr
) + sizeof(struct tcphdr
) :
2086 sizeof(struct tcpiphdr
)
2092 /* Route locked during lookup above */
2096 mss
= min(mss
, offered
);
2098 * XXX - The above conditional probably violates the TCP
2099 * spec. The problem is that, since we don't know the
2100 * other end's MSS, we are supposed to use a conservative
2101 * default. But, if we do that, then MTU discovery will
2102 * never actually take place, because the conservative
2103 * default is much less than the MTUs typically seen
2104 * on the Internet today. For the moment, we'll sweep
2105 * this under the carpet.
2107 * The conservative default might not actually be a problem
2108 * if the only case this occurs is when sending an initial
2109 * SYN with options and data to a host we've never talked
2110 * to before. Then, they will reply with an MSS value which
2111 * will get recorded and the new parameters should get
2112 * recomputed. For Further Study.
2114 if (tp
->t_maxopd
<= mss
)
2118 if ((tp
->t_flags
& (TF_REQ_TSTMP
|TF_NOOPT
)) == TF_REQ_TSTMP
&&
2119 (tp
->t_flags
& TF_RCVD_TSTMP
) == TF_RCVD_TSTMP
)
2120 mss
-= TCPOLEN_TSTAMP_APPA
;
2123 mss
-= mptcp_adj_mss(tp
, TRUE
);
2125 if (so
->so_snd
.sb_hiwat
< mss
)
2126 mss
= so
->so_snd
.sb_hiwat
;
2131 * Reset the slow-start flight size as it may depends on the new MSS
2133 if (CC_ALGO(tp
)->cwnd_init
!= NULL
)
2134 CC_ALGO(tp
)->cwnd_init(tp
);
2135 tcpstat
.tcps_mturesent
++;
2137 tp
->snd_nxt
= tp
->snd_una
;
2143 * Look-up the routing entry to the peer of this inpcb. If no route
2144 * is found and it cannot be allocated the return NULL. This routine
2145 * is called by TCP routines that access the rmx structure and by tcp_mss
2146 * to get the interface MTU. If a route is found, this routine will
2147 * hold the rtentry lock; the caller is responsible for unlocking.
2150 tcp_rtlookup(inp
, input_ifscope
)
2152 unsigned int input_ifscope
;
2158 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_NOTOWNED
);
2160 ro
= &inp
->inp_route
;
2161 if ((rt
= ro
->ro_rt
) != NULL
)
2164 if (ROUTE_UNUSABLE(ro
)) {
2170 /* No route yet, so try to acquire one */
2171 if (inp
->inp_faddr
.s_addr
!= INADDR_ANY
) {
2172 unsigned int ifscope
;
2174 ro
->ro_dst
.sa_family
= AF_INET
;
2175 ro
->ro_dst
.sa_len
= sizeof(struct sockaddr_in
);
2176 ((struct sockaddr_in
*)(void *)&ro
->ro_dst
)->sin_addr
=
2180 * If the socket was bound to an interface, then
2181 * the bound-to-interface takes precedence over
2182 * the inbound interface passed in by the caller
2183 * (if we get here as part of the output path then
2184 * input_ifscope is IFSCOPE_NONE).
2186 ifscope
= (inp
->inp_flags
& INP_BOUND_IF
) ?
2187 inp
->inp_boundifp
->if_index
: input_ifscope
;
2189 rtalloc_scoped(ro
, ifscope
);
2190 if ((rt
= ro
->ro_rt
) != NULL
)
2195 RT_LOCK_ASSERT_HELD(rt
);
2198 * Update MTU discovery determination. Don't do it if:
2199 * 1) it is disabled via the sysctl
2200 * 2) the route isn't up
2201 * 3) the MTU is locked (if it is, then discovery has been
2205 tp
= intotcpcb(inp
);
2207 if (!path_mtu_discovery
|| ((rt
!= NULL
) &&
2208 (!(rt
->rt_flags
& RTF_UP
) || (rt
->rt_rmx
.rmx_locks
& RTV_MTU
))))
2209 tp
->t_flags
&= ~TF_PMTUD
;
2211 tp
->t_flags
|= TF_PMTUD
;
2213 #if CONFIG_IFEF_NOWINDOWSCALE
2214 if (tcp_obey_ifef_nowindowscale
&&
2215 tp
->t_state
== TCPS_SYN_SENT
&& rt
!= NULL
&& rt
->rt_ifp
!= NULL
&&
2216 (rt
->rt_ifp
->if_eflags
& IFEF_NOWINDOWSCALE
)) {
2217 /* Window scaling is enabled on this interface */
2218 tp
->t_flags
&= ~TF_REQ_SCALE
;
2222 if (rt
!= NULL
&& rt
->rt_ifp
!= NULL
) {
2223 somultipages(inp
->inp_socket
,
2224 (rt
->rt_ifp
->if_hwassist
& IFNET_MULTIPAGES
));
2225 tcp_set_tso(tp
, rt
->rt_ifp
);
2228 /* Note if the peer is local */
2230 (rt
->rt_gateway
->sa_family
== AF_LINK
||
2231 rt
->rt_ifp
->if_flags
& IFF_LOOPBACK
||
2232 in_localaddr(inp
->inp_faddr
))) {
2233 tp
->t_flags
|= TF_LOCAL
;
2237 * Caller needs to call RT_UNLOCK(rt).
2244 tcp_rtlookup6(inp
, input_ifscope
)
2246 unsigned int input_ifscope
;
2248 struct route_in6
*ro6
;
2252 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_NOTOWNED
);
2254 ro6
= &inp
->in6p_route
;
2255 if ((rt
= ro6
->ro_rt
) != NULL
)
2258 if (ROUTE_UNUSABLE(ro6
)) {
2264 /* No route yet, so try to acquire one */
2265 if (!IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_faddr
)) {
2266 struct sockaddr_in6
*dst6
;
2267 unsigned int ifscope
;
2269 dst6
= (struct sockaddr_in6
*)&ro6
->ro_dst
;
2270 dst6
->sin6_family
= AF_INET6
;
2271 dst6
->sin6_len
= sizeof(*dst6
);
2272 dst6
->sin6_addr
= inp
->in6p_faddr
;
2275 * If the socket was bound to an interface, then
2276 * the bound-to-interface takes precedence over
2277 * the inbound interface passed in by the caller
2278 * (if we get here as part of the output path then
2279 * input_ifscope is IFSCOPE_NONE).
2281 ifscope
= (inp
->inp_flags
& INP_BOUND_IF
) ?
2282 inp
->inp_boundifp
->if_index
: input_ifscope
;
2284 rtalloc_scoped((struct route
*)ro6
, ifscope
);
2285 if ((rt
= ro6
->ro_rt
) != NULL
)
2290 RT_LOCK_ASSERT_HELD(rt
);
2293 * Update path MTU Discovery determination
2294 * while looking up the route:
2295 * 1) we have a valid route to the destination
2296 * 2) the MTU is not locked (if it is, then discovery has been
2301 tp
= intotcpcb(inp
);
2304 * Update MTU discovery determination. Don't do it if:
2305 * 1) it is disabled via the sysctl
2306 * 2) the route isn't up
2307 * 3) the MTU is locked (if it is, then discovery has been
2311 if (!path_mtu_discovery
|| ((rt
!= NULL
) &&
2312 (!(rt
->rt_flags
& RTF_UP
) || (rt
->rt_rmx
.rmx_locks
& RTV_MTU
))))
2313 tp
->t_flags
&= ~TF_PMTUD
;
2315 tp
->t_flags
|= TF_PMTUD
;
2317 #if CONFIG_IFEF_NOWINDOWSCALE
2318 if (tcp_obey_ifef_nowindowscale
&&
2319 tp
->t_state
== TCPS_SYN_SENT
&& rt
!= NULL
&& rt
->rt_ifp
!= NULL
&&
2320 (rt
->rt_ifp
->if_eflags
& IFEF_NOWINDOWSCALE
)) {
2321 /* Window scaling is not enabled on this interface */
2322 tp
->t_flags
&= ~TF_REQ_SCALE
;
2326 if (rt
!= NULL
&& rt
->rt_ifp
!= NULL
) {
2327 somultipages(inp
->inp_socket
,
2328 (rt
->rt_ifp
->if_hwassist
& IFNET_MULTIPAGES
));
2329 tcp_set_tso(tp
, rt
->rt_ifp
);
2332 /* Note if the peer is local */
2334 (IN6_IS_ADDR_LOOPBACK(&inp
->in6p_faddr
) ||
2335 IN6_IS_ADDR_LINKLOCAL(&inp
->in6p_faddr
) ||
2336 rt
->rt_gateway
->sa_family
== AF_LINK
||
2337 in6_localaddr(&inp
->in6p_faddr
))) {
2338 tp
->t_flags
|= TF_LOCAL
;
2342 * Caller needs to call RT_UNLOCK(rt).
2349 /* compute ESP/AH header size for TCP, including outer IP header. */
2351 ipsec_hdrsiz_tcp(tp
)
2359 struct ip6_hdr
*ip6
= NULL
;
2363 if ((tp
== NULL
) || ((inp
= tp
->t_inpcb
) == NULL
))
2365 MGETHDR(m
, M_DONTWAIT
, MT_DATA
); /* MAC-OK */
2370 if ((inp
->inp_vflag
& INP_IPV6
) != 0) {
2371 ip6
= mtod(m
, struct ip6_hdr
*);
2372 th
= (struct tcphdr
*)(void *)(ip6
+ 1);
2373 m
->m_pkthdr
.len
= m
->m_len
=
2374 sizeof(struct ip6_hdr
) + sizeof(struct tcphdr
);
2375 tcp_fillheaders(tp
, ip6
, th
);
2376 hdrsiz
= ipsec6_hdrsiz(m
, IPSEC_DIR_OUTBOUND
, inp
);
2380 ip
= mtod(m
, struct ip
*);
2381 th
= (struct tcphdr
*)(ip
+ 1);
2382 m
->m_pkthdr
.len
= m
->m_len
= sizeof(struct tcpiphdr
);
2383 tcp_fillheaders(tp
, ip
, th
);
2384 hdrsiz
= ipsec4_hdrsiz(m
, IPSEC_DIR_OUTBOUND
, inp
);
2392 * Return a pointer to the cached information about the remote host.
2393 * The cached information is stored in the protocol specific part of
2394 * the route metrics.
2397 tcp_gettaocache(inp
)
2401 struct rmxp_tao
*taop
;
2404 if ((inp
->inp_vflag
& INP_IPV6
) != 0)
2405 rt
= tcp_rtlookup6(inp
, IFSCOPE_NONE
);
2408 rt
= tcp_rtlookup(inp
, IFSCOPE_NONE
);
2410 /* Make sure this is a host route and is up. */
2412 (rt
->rt_flags
& (RTF_UP
|RTF_HOST
)) != (RTF_UP
|RTF_HOST
)) {
2413 /* Route locked during lookup above */
2419 taop
= rmx_taop(rt
->rt_rmx
);
2420 /* Route locked during lookup above */
2426 * Clear all the TAO cache entries, called from tcp_init.
2429 * This routine is just an empty one, because we assume that the routing
2430 * routing tables are initialized at the same time when TCP, so there is
2431 * nothing in the cache left over.
2439 tcp_lock(struct socket
*so
, int refcount
, void *lr
)
2444 lr_saved
= __builtin_return_address(0);
2448 if (so
->so_pcb
!= NULL
) {
2449 lck_mtx_lock(&((struct inpcb
*)so
->so_pcb
)->inpcb_mtx
);
2451 panic("tcp_lock: so=%p NO PCB! lr=%p lrh= %s\n",
2452 so
, lr_saved
, solockhistory_nr(so
));
2456 if (so
->so_usecount
< 0) {
2457 panic("tcp_lock: so=%p so_pcb=%p lr=%p ref=%x lrh= %s\n",
2458 so
, so
->so_pcb
, lr_saved
, so
->so_usecount
, solockhistory_nr(so
));
2463 so
->lock_lr
[so
->next_lock_lr
] = lr_saved
;
2464 so
->next_lock_lr
= (so
->next_lock_lr
+1) % SO_LCKDBG_MAX
;
2469 tcp_unlock(struct socket
*so
, int refcount
, void *lr
)
2474 lr_saved
= __builtin_return_address(0);
2478 #ifdef MORE_TCPLOCK_DEBUG
2479 printf("tcp_unlock: so=0x%llx sopcb=0x%llx lock=0x%llx ref=%x "
2480 "lr=0x%llx\n", (uint64_t)VM_KERNEL_ADDRPERM(so
),
2481 (uint64_t)VM_KERNEL_ADDRPERM(so
->so_pcb
),
2482 (uint64_t)VM_KERNEL_ADDRPERM(&(sotoinpcb(so
)->inpcb_mtx
)),
2483 so
->so_usecount
, (uint64_t)VM_KERNEL_ADDRPERM(lr_saved
));
2488 if (so
->so_usecount
< 0) {
2489 panic("tcp_unlock: so=%p usecount=%x lrh= %s\n",
2490 so
, so
->so_usecount
, solockhistory_nr(so
));
2493 if (so
->so_pcb
== NULL
) {
2494 panic("tcp_unlock: so=%p NO PCB usecount=%x lr=%p lrh= %s\n",
2495 so
, so
->so_usecount
, lr_saved
, solockhistory_nr(so
));
2498 lck_mtx_assert(&((struct inpcb
*)so
->so_pcb
)->inpcb_mtx
,
2499 LCK_MTX_ASSERT_OWNED
);
2500 so
->unlock_lr
[so
->next_unlock_lr
] = lr_saved
;
2501 so
->next_unlock_lr
= (so
->next_unlock_lr
+1) % SO_LCKDBG_MAX
;
2502 lck_mtx_unlock(&((struct inpcb
*)so
->so_pcb
)->inpcb_mtx
);
2510 __unused
int locktype
)
2512 struct inpcb
*inp
= sotoinpcb(so
);
2515 if (so
->so_usecount
< 0)
2516 panic("tcp_getlock: so=%p usecount=%x lrh= %s\n",
2517 so
, so
->so_usecount
, solockhistory_nr(so
));
2518 return(&inp
->inpcb_mtx
);
2521 panic("tcp_getlock: so=%p NULL so_pcb %s\n",
2522 so
, solockhistory_nr(so
));
2523 return (so
->so_proto
->pr_domain
->dom_mtx
);
2527 /* Determine if we can grow the recieve socket buffer to avoid sending
2528 * a zero window update to the peer. We allow even socket buffers that
2529 * have fixed size (set by the application) to grow if the resource
2530 * constraints are met. They will also be trimmed after the application
2534 tcp_sbrcv_grow_rwin(struct tcpcb
*tp
, struct sockbuf
*sb
) {
2535 u_int32_t rcvbufinc
= tp
->t_maxseg
<< tcp_autorcvbuf_inc_shift
;
2536 u_int32_t rcvbuf
= sb
->sb_hiwat
;
2537 struct socket
*so
= tp
->t_inpcb
->inp_socket
;
2540 * If message delivery is enabled, do not count
2541 * unordered bytes in receive buffer towards hiwat
2543 if (so
->so_flags
& SOF_ENABLE_MSGS
)
2544 rcvbuf
= rcvbuf
- so
->so_msg_state
->msg_uno_bytes
;
2546 if (tcp_do_autorcvbuf
== 1 &&
2547 tcp_cansbgrow(sb
) &&
2548 (tp
->t_flags
& TF_SLOWLINK
) == 0 &&
2549 (rcvbuf
- sb
->sb_cc
) < rcvbufinc
&&
2550 (rcvbuf
< tcp_autorcvbuf_max
)) {
2551 sbreserve(sb
, (sb
->sb_hiwat
+ rcvbufinc
));
2556 tcp_sbspace(struct tcpcb
*tp
)
2558 struct sockbuf
*sb
= &tp
->t_inpcb
->inp_socket
->so_rcv
;
2559 u_int32_t rcvbuf
= sb
->sb_hiwat
;
2561 struct socket
*so
= tp
->t_inpcb
->inp_socket
;
2564 * If message delivery is enabled, do not count
2565 * unordered bytes in receive buffer towards hiwat mark.
2566 * This value is used to return correct rwnd that does
2567 * not reflect the extra unordered bytes added to the
2568 * receive socket buffer.
2570 if (so
->so_flags
& SOF_ENABLE_MSGS
)
2571 rcvbuf
= rcvbuf
- so
->so_msg_state
->msg_uno_bytes
;
2573 tcp_sbrcv_grow_rwin(tp
, sb
);
2575 space
= ((int32_t) imin((rcvbuf
- sb
->sb_cc
),
2576 (sb
->sb_mbmax
- sb
->sb_mbcnt
)));
2580 /* Avoid increasing window size if the current window
2581 * is already very low, we could be in "persist" mode and
2582 * we could break some apps (see rdar://5409343)
2585 if (space
< tp
->t_maxseg
)
2588 /* Clip window size for slower link */
2590 if (((tp
->t_flags
& TF_SLOWLINK
) != 0) && slowlink_wsize
> 0 )
2591 return imin(space
, slowlink_wsize
);
2596 * Checks TCP Segment Offloading capability for a given connection and interface pair.
2599 tcp_set_tso(struct tcpcb
*tp
, struct ifnet
*ifp
)
2603 * We can't use TSO if this tcpcb belongs to an MPTCP session.
2605 if (tp
->t_mpflags
& TMPF_MPTCP_TRUE
) {
2606 tp
->t_flags
&= ~TF_TSO
;
2611 struct inpcb
*inp
= tp
->t_inpcb
;
2612 int isipv6
= (inp
->inp_vflag
& INP_IPV6
) != 0;
2615 if (ifp
&& (ifp
->if_hwassist
& IFNET_TSO_IPV6
)) {
2616 tp
->t_flags
|= TF_TSO
;
2617 if (ifp
->if_tso_v6_mtu
!= 0)
2618 tp
->tso_max_segment_size
= ifp
->if_tso_v6_mtu
;
2620 tp
->tso_max_segment_size
= TCP_MAXWIN
;
2622 tp
->t_flags
&= ~TF_TSO
;
2628 if (ifp
&& (ifp
->if_hwassist
& IFNET_TSO_IPV4
)) {
2629 tp
->t_flags
|= TF_TSO
;
2630 if (ifp
->if_tso_v4_mtu
!= 0)
2631 tp
->tso_max_segment_size
= ifp
->if_tso_v4_mtu
;
2633 tp
->tso_max_segment_size
= TCP_MAXWIN
;
2635 tp
->t_flags
&= ~TF_TSO
;
2639 #define TIMEVAL_TO_TCPHZ(_tv_) ((_tv_).tv_sec * TCP_RETRANSHZ + (_tv_).tv_usec / TCP_RETRANSHZ_TO_USEC)
2641 /* Function to calculate the tcp clock. The tcp clock will get updated
2642 * at the boundaries of the tcp layer. This is done at 3 places:
2643 * 1. Right before processing an input tcp packet
2644 * 2. Whenever a connection wants to access the network using tcp_usrreqs
2645 * 3. When a tcp timer fires or before tcp slow timeout
2650 calculate_tcp_clock()
2652 struct timeval tv
= tcp_uptime
;
2653 struct timeval interval
= {0, TCP_RETRANSHZ_TO_USEC
};
2654 struct timeval now
, hold_now
;
2660 * Update coarse-grained networking timestamp (in sec.); the idea
2661 * is to update the counter returnable via net_uptime() when
2664 net_update_uptime_secs(now
.tv_sec
);
2666 timevaladd(&tv
, &interval
);
2667 if (timevalcmp(&now
, &tv
, >)) {
2668 /* time to update the clock */
2669 lck_spin_lock(tcp_uptime_lock
);
2670 if (timevalcmp(&tcp_uptime
, &now
, >=)) {
2671 /* clock got updated while we were waiting for the lock */
2672 lck_spin_unlock(tcp_uptime_lock
);
2679 timevalsub(&now
, &tv
);
2681 incr
= TIMEVAL_TO_TCPHZ(now
);
2683 tcp_uptime
= hold_now
;
2687 lck_spin_unlock(tcp_uptime_lock
);
2692 /* Compute receive window scaling that we are going to request
2693 * for this connection based on sb_hiwat. Try to leave some
2694 * room to potentially increase the window size upto a maximum
2695 * defined by the constant tcp_autorcvbuf_max.
2698 tcp_set_max_rwinscale(struct tcpcb
*tp
, struct socket
*so
) {
2699 u_int32_t maxsockbufsize
;
2701 tp
->request_r_scale
= max(tcp_win_scale
, tp
->request_r_scale
);
2702 maxsockbufsize
= ((so
->so_rcv
.sb_flags
& SB_USRSIZE
) != 0) ?
2703 so
->so_rcv
.sb_hiwat
: tcp_autorcvbuf_max
;
2705 while (tp
->request_r_scale
< TCP_MAX_WINSHIFT
&&
2706 (TCP_MAXWIN
<< tp
->request_r_scale
) < maxsockbufsize
)
2707 tp
->request_r_scale
++;
2708 tp
->request_r_scale
= min(tp
->request_r_scale
, TCP_MAX_WINSHIFT
);
2713 tcp_notsent_lowat_check(struct socket
*so
) {
2714 struct inpcb
*inp
= sotoinpcb(so
);
2715 struct tcpcb
*tp
= NULL
;
2718 tp
= intotcpcb(inp
);
2721 notsent
= so
->so_snd
.sb_cc
-
2722 (tp
->snd_nxt
- tp
->snd_una
);
2724 /* When we send a FIN or SYN, not_sent can be negative.
2725 * In that case also we need to send a write event to the
2726 * process if it is waiting. In the FIN case, it will
2727 * get an error from send because cantsendmore will be set.
2729 if (notsent
<= tp
->t_notsent_lowat
) {
2733 /* When Nagle's algorithm is not disabled, it is better
2734 * to wakeup the client until there is atleast one
2735 * maxseg of data to write.
2737 if ((tp
->t_flags
& TF_NODELAY
) == 0 &&
2738 notsent
> 0 && notsent
< tp
->t_maxseg
) {
2745 /* DSEP Review Done pl-20051213-v02 @3253,@3391,@3400 */