2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * @(#)tcp_subr.c 8.2 (Berkeley) 5/24/95
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
38 #include "opt_inet6.h"
39 #include "opt_ipsec.h"
40 #include "opt_kern_tls.h"
42 #include <sys/param.h>
43 #include <sys/systm.h>
45 #include <sys/callout.h>
46 #include <sys/eventhandler.h>
48 #include <sys/hhook.h>
50 #include <sys/kernel.h>
52 #include <sys/khelp.h>
57 #include <sys/qmath.h>
58 #include <sys/stats.h>
59 #include <sys/sysctl.h>
61 #include <sys/malloc.h>
62 #include <sys/refcount.h>
67 #include <sys/socket.h>
68 #include <sys/socketvar.h>
69 #include <sys/protosw.h>
70 #include <sys/random.h>
74 #include <net/route.h>
75 #include <net/route/nhop.h>
77 #include <net/if_var.h>
78 #include <net/if_private.h>
81 #include <netinet/in.h>
82 #include <netinet/in_fib.h>
83 #include <netinet/in_kdtrace.h>
84 #include <netinet/in_pcb.h>
85 #include <netinet/in_systm.h>
86 #include <netinet/in_var.h>
87 #include <netinet/ip.h>
88 #include <netinet/ip_icmp.h>
89 #include <netinet/ip_var.h>
91 #include <netinet/icmp6.h>
92 #include <netinet/ip6.h>
93 #include <netinet6/in6_fib.h>
94 #include <netinet6/in6_pcb.h>
95 #include <netinet6/ip6_var.h>
96 #include <netinet6/scope6_var.h>
97 #include <netinet6/nd6.h>
100 #include <netinet/tcp.h>
104 #include <netinet/tcp_fsm.h>
105 #include <netinet/tcp_seq.h>
106 #include <netinet/tcp_timer.h>
107 #include <netinet/tcp_var.h>
108 #include <netinet/tcp_ecn.h>
109 #include <netinet/tcp_log_buf.h>
110 #include <netinet/tcp_syncache.h>
111 #include <netinet/tcp_hpts.h>
112 #include <netinet/tcp_lro.h>
113 #include <netinet/cc/cc.h>
114 #include <netinet/tcpip.h>
115 #include <netinet/tcp_fastopen.h>
116 #include <netinet/tcp_accounting.h>
118 #include <netinet/tcp_pcap.h>
121 #include <netinet/tcp_offload.h>
123 #include <netinet/udp.h>
124 #include <netinet/udp_var.h>
126 #include <netinet6/tcp6_var.h>
129 #include <netipsec/ipsec_support.h>
131 #include <machine/in_cksum.h>
132 #include <crypto/siphash/siphash.h>
134 #include <security/mac/mac_framework.h>
137 static ip6proto_ctlinput_t tcp6_ctlinput;
138 static udp_tun_icmp_t tcp6_ctlinput_viaudp;
141 VNET_DEFINE(int, tcp_mssdflt) = TCP_MSS;
143 VNET_DEFINE(int, tcp_v6mssdflt) = TCP6_MSS;
146 #ifdef TCP_SAD_DETECTION
147 /* Sack attack detection thresholds and such */
148 SYSCTL_NODE(_net_inet_tcp, OID_AUTO, sack_attack,
149 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
150 "Sack Attack detection thresholds");
151 int32_t tcp_force_detection = 0;
152 SYSCTL_INT(_net_inet_tcp_sack_attack, OID_AUTO, force_detection,
154 &tcp_force_detection, 0,
155 "Do we force detection even if the INP has it off?");
156 int32_t tcp_sad_limit = 10000;
157 SYSCTL_INT(_net_inet_tcp_sack_attack, OID_AUTO, limit,
159 &tcp_sad_limit, 10000,
160 "If SaD is enabled, what is the limit to sendmap entries (0 = unlimited)?");
161 int32_t tcp_sack_to_ack_thresh = 700; /* 70 % */
162 SYSCTL_INT(_net_inet_tcp_sack_attack, OID_AUTO, sack_to_ack_thresh,
164 &tcp_sack_to_ack_thresh, 700,
165 "Percentage of sacks to acks we must see above (10.1 percent is 101)?");
166 int32_t tcp_sack_to_move_thresh = 600; /* 60 % */
167 SYSCTL_INT(_net_inet_tcp_sack_attack, OID_AUTO, move_thresh,
169 &tcp_sack_to_move_thresh, 600,
170 "Percentage of sack moves we must see above (10.1 percent is 101)");
171 int32_t tcp_restoral_thresh = 450; /* 45 % (sack:2:ack -25%) (mv:ratio -15%) **/
172 SYSCTL_INT(_net_inet_tcp_sack_attack, OID_AUTO, restore_thresh,
174 &tcp_restoral_thresh, 450,
175 "Percentage of sack to ack percentage we must see below to restore(10.1 percent is 101)");
176 int32_t tcp_sad_decay_val = 800;
177 SYSCTL_INT(_net_inet_tcp_sack_attack, OID_AUTO, decay_per,
179 &tcp_sad_decay_val, 800,
180 "The decay percentage (10.1 percent equals 101 )");
181 int32_t tcp_map_minimum = 500;
182 SYSCTL_INT(_net_inet_tcp_sack_attack, OID_AUTO, nummaps,
184 &tcp_map_minimum, 500,
185 "Number of Map enteries before we start detection");
186 int32_t tcp_sad_pacing_interval = 2000;
187 SYSCTL_INT(_net_inet_tcp_sack_attack, OID_AUTO, sad_pacing_int,
189 &tcp_sad_pacing_interval, 2000,
190 "What is the minimum pacing interval for a classified attacker?");
192 int32_t tcp_sad_low_pps = 100;
193 SYSCTL_INT(_net_inet_tcp_sack_attack, OID_AUTO, sad_low_pps,
195 &tcp_sad_low_pps, 100,
196 "What is the input pps that below which we do not decay?");
198 uint32_t tcp_ack_war_time_window = 1000;
199 SYSCTL_UINT(_net_inet_tcp, OID_AUTO, ack_war_timewindow,
201 &tcp_ack_war_time_window, 1000,
202 "If the tcp_stack does ack-war prevention how many milliseconds are in its time window?");
203 uint32_t tcp_ack_war_cnt = 5;
204 SYSCTL_UINT(_net_inet_tcp, OID_AUTO, ack_war_cnt,
207 "If the tcp_stack does ack-war prevention how many acks can be sent in its time window?");
209 struct rwlock tcp_function_lock;
212 sysctl_net_inet_tcp_mss_check(SYSCTL_HANDLER_ARGS)
217 error = sysctl_handle_int(oidp, &new, 0, req);
218 if (error == 0 && req->newptr) {
219 if (new < TCP_MINMSS)
227 SYSCTL_PROC(_net_inet_tcp, TCPCTL_MSSDFLT, mssdflt,
228 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
229 &VNET_NAME(tcp_mssdflt), 0, &sysctl_net_inet_tcp_mss_check, "I",
230 "Default TCP Maximum Segment Size");
234 sysctl_net_inet_tcp_mss_v6_check(SYSCTL_HANDLER_ARGS)
238 new = V_tcp_v6mssdflt;
239 error = sysctl_handle_int(oidp, &new, 0, req);
240 if (error == 0 && req->newptr) {
241 if (new < TCP_MINMSS)
244 V_tcp_v6mssdflt = new;
249 SYSCTL_PROC(_net_inet_tcp, TCPCTL_V6MSSDFLT, v6mssdflt,
250 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
251 &VNET_NAME(tcp_v6mssdflt), 0, &sysctl_net_inet_tcp_mss_v6_check, "I",
252 "Default TCP Maximum Segment Size for IPv6");
256 * Minimum MSS we accept and use. This prevents DoS attacks where
257 * we are forced to a ridiculous low MSS like 20 and send hundreds
258 * of packets instead of one. The effect scales with the available
259 * bandwidth and quickly saturates the CPU and network interface
260 * with packet generation and sending. Set to zero to disable MINMSS
261 * checking. This setting prevents us from sending too small packets.
263 VNET_DEFINE(int, tcp_minmss) = TCP_MINMSS;
264 SYSCTL_INT(_net_inet_tcp, OID_AUTO, minmss, CTLFLAG_VNET | CTLFLAG_RW,
265 &VNET_NAME(tcp_minmss), 0,
266 "Minimum TCP Maximum Segment Size");
268 VNET_DEFINE(int, tcp_do_rfc1323) = 1;
269 SYSCTL_INT(_net_inet_tcp, TCPCTL_DO_RFC1323, rfc1323, CTLFLAG_VNET | CTLFLAG_RW,
270 &VNET_NAME(tcp_do_rfc1323), 0,
271 "Enable rfc1323 (high performance TCP) extensions");
274 * As of June 2021, several TCP stacks violate RFC 7323 from September 2014.
275 * Some stacks negotiate TS, but never send them after connection setup. Some
276 * stacks negotiate TS, but don't send them when sending keep-alive segments.
277 * These include modern widely deployed TCP stacks.
278 * Therefore tolerating violations for now...
280 VNET_DEFINE(int, tcp_tolerate_missing_ts) = 1;
281 SYSCTL_INT(_net_inet_tcp, OID_AUTO, tolerate_missing_ts, CTLFLAG_VNET | CTLFLAG_RW,
282 &VNET_NAME(tcp_tolerate_missing_ts), 0,
283 "Tolerate missing TCP timestamps");
285 VNET_DEFINE(int, tcp_ts_offset_per_conn) = 1;
286 SYSCTL_INT(_net_inet_tcp, OID_AUTO, ts_offset_per_conn, CTLFLAG_VNET | CTLFLAG_RW,
287 &VNET_NAME(tcp_ts_offset_per_conn), 0,
288 "Initialize TCP timestamps per connection instead of per host pair");
290 /* How many connections are pacing */
291 static volatile uint32_t number_of_tcp_connections_pacing = 0;
292 static uint32_t shadow_num_connections = 0;
293 static counter_u64_t tcp_pacing_failures;
295 static int tcp_pacing_limit = 10000;
296 SYSCTL_INT(_net_inet_tcp, OID_AUTO, pacing_limit, CTLFLAG_RW,
297 &tcp_pacing_limit, 1000,
298 "If the TCP stack does pacing, is there a limit (-1 = no, 0 = no pacing N = number of connections)");
300 SYSCTL_UINT(_net_inet_tcp, OID_AUTO, pacing_count, CTLFLAG_RD,
301 &shadow_num_connections, 0, "Number of TCP connections being paced");
303 SYSCTL_COUNTER_U64(_net_inet_tcp, OID_AUTO, pacing_failures, CTLFLAG_RD,
304 &tcp_pacing_failures, "Number of times we failed to enable pacing to avoid exceeding the limit");
306 static int tcp_log_debug = 0;
307 SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_debug, CTLFLAG_RW,
308 &tcp_log_debug, 0, "Log errors caused by incoming TCP segments");
310 static int tcp_tcbhashsize;
311 SYSCTL_INT(_net_inet_tcp, OID_AUTO, tcbhashsize, CTLFLAG_RDTUN | CTLFLAG_NOFETCH,
312 &tcp_tcbhashsize, 0, "Size of TCP control-block hashtable");
314 static int do_tcpdrain = 1;
315 SYSCTL_INT(_net_inet_tcp, OID_AUTO, do_tcpdrain, CTLFLAG_RW, &do_tcpdrain, 0,
316 "Enable tcp_drain routine for extra help when low on mbufs");
318 SYSCTL_UINT(_net_inet_tcp, OID_AUTO, pcbcount, CTLFLAG_VNET | CTLFLAG_RD,
319 &VNET_NAME(tcbinfo.ipi_count), 0, "Number of active PCBs");
321 VNET_DEFINE_STATIC(int, icmp_may_rst) = 1;
322 #define V_icmp_may_rst VNET(icmp_may_rst)
323 SYSCTL_INT(_net_inet_tcp, OID_AUTO, icmp_may_rst, CTLFLAG_VNET | CTLFLAG_RW,
324 &VNET_NAME(icmp_may_rst), 0,
325 "Certain ICMP unreachable messages may abort connections in SYN_SENT");
327 VNET_DEFINE_STATIC(int, tcp_isn_reseed_interval) = 0;
328 #define V_tcp_isn_reseed_interval VNET(tcp_isn_reseed_interval)
329 SYSCTL_INT(_net_inet_tcp, OID_AUTO, isn_reseed_interval, CTLFLAG_VNET | CTLFLAG_RW,
330 &VNET_NAME(tcp_isn_reseed_interval), 0,
331 "Seconds between reseeding of ISN secret");
333 static int tcp_soreceive_stream;
334 SYSCTL_INT(_net_inet_tcp, OID_AUTO, soreceive_stream, CTLFLAG_RDTUN,
335 &tcp_soreceive_stream, 0, "Using soreceive_stream for TCP sockets");
337 VNET_DEFINE(uma_zone_t, sack_hole_zone);
338 #define V_sack_hole_zone VNET(sack_hole_zone)
339 VNET_DEFINE(uint32_t, tcp_map_entries_limit) = 0; /* unlimited */
341 sysctl_net_inet_tcp_map_limit_check(SYSCTL_HANDLER_ARGS)
346 new = V_tcp_map_entries_limit;
347 error = sysctl_handle_int(oidp, &new, 0, req);
348 if (error == 0 && req->newptr) {
349 /* only allow "0" and value > minimum */
350 if (new > 0 && new < TCP_MIN_MAP_ENTRIES_LIMIT)
353 V_tcp_map_entries_limit = new;
357 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, map_limit,
358 CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
359 &VNET_NAME(tcp_map_entries_limit), 0,
360 &sysctl_net_inet_tcp_map_limit_check, "IU",
361 "Total sendmap entries limit");
363 VNET_DEFINE(uint32_t, tcp_map_split_limit) = 0; /* unlimited */
364 SYSCTL_UINT(_net_inet_tcp, OID_AUTO, split_limit, CTLFLAG_VNET | CTLFLAG_RW,
365 &VNET_NAME(tcp_map_split_limit), 0,
366 "Total sendmap split entries limit");
369 VNET_DEFINE(struct hhook_head *, tcp_hhh[HHOOK_TCP_LAST+1]);
372 #define TS_OFFSET_SECRET_LENGTH SIPHASH_KEY_LENGTH
373 VNET_DEFINE_STATIC(u_char, ts_offset_secret[TS_OFFSET_SECRET_LENGTH]);
374 #define V_ts_offset_secret VNET(ts_offset_secret)
376 static int tcp_default_fb_init(struct tcpcb *tp, void **ptr);
377 static void tcp_default_fb_fini(struct tcpcb *tp, int tcb_is_purged);
378 static int tcp_default_handoff_ok(struct tcpcb *tp);
379 static struct inpcb *tcp_notify(struct inpcb *, int);
380 static struct inpcb *tcp_mtudisc_notify(struct inpcb *, int);
381 static struct inpcb *tcp_mtudisc(struct inpcb *, int);
382 static struct inpcb *tcp_drop_syn_sent(struct inpcb *, int);
383 static char * tcp_log_addr(struct in_conninfo *inc, struct tcphdr *th,
384 const void *ip4hdr, const void *ip6hdr);
385 static void tcp_default_switch_failed(struct tcpcb *tp);
386 static ipproto_ctlinput_t tcp_ctlinput;
387 static udp_tun_icmp_t tcp_ctlinput_viaudp;
389 static struct tcp_function_block tcp_def_funcblk = {
390 .tfb_tcp_block_name = "freebsd",
391 .tfb_tcp_output = tcp_default_output,
392 .tfb_tcp_do_segment = tcp_do_segment,
393 .tfb_tcp_ctloutput = tcp_default_ctloutput,
394 .tfb_tcp_handoff_ok = tcp_default_handoff_ok,
395 .tfb_tcp_fb_init = tcp_default_fb_init,
396 .tfb_tcp_fb_fini = tcp_default_fb_fini,
397 .tfb_switch_failed = tcp_default_switch_failed,
400 static int tcp_fb_cnt = 0;
401 struct tcp_funchead t_functions;
402 VNET_DEFINE_STATIC(struct tcp_function_block *, tcp_func_set_ptr) = &tcp_def_funcblk;
403 #define V_tcp_func_set_ptr VNET(tcp_func_set_ptr)
406 tcp_record_dsack(struct tcpcb *tp, tcp_seq start, tcp_seq end, int tlp)
408 TCPSTAT_INC(tcps_dsack_count);
411 if (SEQ_GT(end, start)) {
412 tp->t_dsack_bytes += (end - start);
413 TCPSTAT_ADD(tcps_dsack_bytes, (end - start));
415 tp->t_dsack_tlp_bytes += (start - end);
416 TCPSTAT_ADD(tcps_dsack_bytes, (start - end));
419 if (SEQ_GT(end, start)) {
420 tp->t_dsack_bytes += (end - start);
421 TCPSTAT_ADD(tcps_dsack_tlp_bytes, (end - start));
423 tp->t_dsack_tlp_bytes += (start - end);
424 TCPSTAT_ADD(tcps_dsack_tlp_bytes, (start - end));
429 static struct tcp_function_block *
430 find_tcp_functions_locked(struct tcp_function_set *fs)
432 struct tcp_function *f;
433 struct tcp_function_block *blk=NULL;
435 TAILQ_FOREACH(f, &t_functions, tf_next) {
436 if (strcmp(f->tf_name, fs->function_set_name) == 0) {
444 static struct tcp_function_block *
445 find_tcp_fb_locked(struct tcp_function_block *blk, struct tcp_function **s)
447 struct tcp_function_block *rblk=NULL;
448 struct tcp_function *f;
450 TAILQ_FOREACH(f, &t_functions, tf_next) {
451 if (f->tf_fb == blk) {
462 struct tcp_function_block *
463 find_and_ref_tcp_functions(struct tcp_function_set *fs)
465 struct tcp_function_block *blk;
467 rw_rlock(&tcp_function_lock);
468 blk = find_tcp_functions_locked(fs);
470 refcount_acquire(&blk->tfb_refcnt);
471 rw_runlock(&tcp_function_lock);
475 struct tcp_function_block *
476 find_and_ref_tcp_fb(struct tcp_function_block *blk)
478 struct tcp_function_block *rblk;
480 rw_rlock(&tcp_function_lock);
481 rblk = find_tcp_fb_locked(blk, NULL);
483 refcount_acquire(&rblk->tfb_refcnt);
484 rw_runlock(&tcp_function_lock);
488 /* Find a matching alias for the given tcp_function_block. */
490 find_tcp_function_alias(struct tcp_function_block *blk,
491 struct tcp_function_set *fs)
493 struct tcp_function *f;
497 rw_rlock(&tcp_function_lock);
498 TAILQ_FOREACH(f, &t_functions, tf_next) {
499 if ((f->tf_fb == blk) &&
500 (strncmp(f->tf_name, blk->tfb_tcp_block_name,
501 TCP_FUNCTION_NAME_LEN_MAX) != 0)) {
502 /* Matching function block with different name. */
503 strncpy(fs->function_set_name, f->tf_name,
504 TCP_FUNCTION_NAME_LEN_MAX);
509 /* Null terminate the string appropriately. */
511 fs->function_set_name[TCP_FUNCTION_NAME_LEN_MAX - 1] = '\0';
513 fs->function_set_name[0] = '\0';
515 rw_runlock(&tcp_function_lock);
519 static struct tcp_function_block *
520 find_and_ref_tcp_default_fb(void)
522 struct tcp_function_block *rblk;
524 rw_rlock(&tcp_function_lock);
525 rblk = V_tcp_func_set_ptr;
526 refcount_acquire(&rblk->tfb_refcnt);
527 rw_runlock(&tcp_function_lock);
532 tcp_switch_back_to_default(struct tcpcb *tp)
534 struct tcp_function_block *tfb;
537 KASSERT(tp->t_fb != &tcp_def_funcblk,
538 ("%s: called by the built-in default stack", __func__));
541 * Now, we'll find a new function block to use.
542 * Start by trying the current user-selected
543 * default, unless this stack is the user-selected
546 tfb = find_and_ref_tcp_default_fb();
547 if (tfb == tp->t_fb) {
548 refcount_release(&tfb->tfb_refcnt);
551 /* Does the stack accept this connection? */
552 if (tfb != NULL && tfb->tfb_tcp_handoff_ok != NULL &&
553 (*tfb->tfb_tcp_handoff_ok)(tp)) {
554 refcount_release(&tfb->tfb_refcnt);
557 /* Try to use that stack. */
559 /* Initialize the new stack. If it succeeds, we are done. */
560 if (tfb->tfb_tcp_fb_init == NULL ||
561 (*tfb->tfb_tcp_fb_init)(tp, &ptr) == 0) {
562 /* Release the old stack */
563 if (tp->t_fb->tfb_tcp_fb_fini != NULL)
564 (*tp->t_fb->tfb_tcp_fb_fini)(tp, 0);
565 refcount_release(&tp->t_fb->tfb_refcnt);
566 /* Now set in all the pointers */
572 * Initialization failed. Release the reference count on
573 * the looked up default stack.
575 refcount_release(&tfb->tfb_refcnt);
579 * If that wasn't feasible, use the built-in default
580 * stack which is not allowed to reject anyone.
582 tfb = find_and_ref_tcp_fb(&tcp_def_funcblk);
584 /* there always should be a default */
585 panic("Can't refer to tcp_def_funcblk");
587 if (tfb->tfb_tcp_handoff_ok != NULL) {
588 if ((*tfb->tfb_tcp_handoff_ok) (tp)) {
589 /* The default stack cannot say no */
590 panic("Default stack rejects a new session?");
593 if (tfb->tfb_tcp_fb_init != NULL &&
594 (*tfb->tfb_tcp_fb_init)(tp, &ptr)) {
595 /* The default stack cannot fail */
596 panic("Default stack initialization failed");
598 /* Now release the old stack */
599 if (tp->t_fb->tfb_tcp_fb_fini != NULL)
600 (*tp->t_fb->tfb_tcp_fb_fini)(tp, 0);
601 refcount_release(&tp->t_fb->tfb_refcnt);
602 /* And set in the pointers to the new */
608 tcp_recv_udp_tunneled_packet(struct mbuf *m, int off, struct inpcb *inp,
609 const struct sockaddr *sa, void *ctx)
620 TCPSTAT_INC(tcps_tunneled_pkts);
621 if ((m->m_flags & M_PKTHDR) == 0) {
622 /* Can't handle one that is not a pkt hdr */
623 TCPSTAT_INC(tcps_tunneled_errs);
626 thlen = sizeof(struct tcphdr);
627 if (m->m_len < off + sizeof(struct udphdr) + thlen &&
628 (m = m_pullup(m, off + sizeof(struct udphdr) + thlen)) == NULL) {
629 TCPSTAT_INC(tcps_tunneled_errs);
632 iph = mtod(m, struct ip *);
633 uh = (struct udphdr *)((caddr_t)iph + off);
634 th = (struct tcphdr *)(uh + 1);
635 thlen = th->th_off << 2;
636 if (m->m_len < off + sizeof(struct udphdr) + thlen) {
637 m = m_pullup(m, off + sizeof(struct udphdr) + thlen);
639 TCPSTAT_INC(tcps_tunneled_errs);
642 iph = mtod(m, struct ip *);
643 uh = (struct udphdr *)((caddr_t)iph + off);
644 th = (struct tcphdr *)(uh + 1);
647 m->m_pkthdr.tcp_tun_port = port = uh->uh_sport;
648 bcopy(th, uh, m->m_len - off);
649 m->m_len -= sizeof(struct udphdr);
650 m->m_pkthdr.len -= sizeof(struct udphdr);
652 * We use the same algorithm for
653 * both UDP and TCP for c-sum. So
654 * the code in tcp_input will skip
655 * the checksum. So we do nothing
656 * with the flag (m->m_pkthdr.csum_flags).
661 iph->ip_len = htons(ntohs(iph->ip_len) - sizeof(struct udphdr));
662 tcp_input_with_port(&m, &off, IPPROTO_TCP, port);
666 case IPV6_VERSION >> 4:
667 ip6 = mtod(m, struct ip6_hdr *);
668 ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) - sizeof(struct udphdr));
669 tcp6_input_with_port(&m, &off, IPPROTO_TCP, port);
684 sysctl_net_inet_default_tcp_functions(SYSCTL_HANDLER_ARGS)
687 struct tcp_function_set fs;
688 struct tcp_function_block *blk;
690 memset(&fs, 0, sizeof(fs));
691 rw_rlock(&tcp_function_lock);
692 blk = find_tcp_fb_locked(V_tcp_func_set_ptr, NULL);
695 strcpy(fs.function_set_name, blk->tfb_tcp_block_name);
696 fs.pcbcnt = blk->tfb_refcnt;
698 rw_runlock(&tcp_function_lock);
699 error = sysctl_handle_string(oidp, fs.function_set_name,
700 sizeof(fs.function_set_name), req);
702 /* Check for error or no change */
703 if (error != 0 || req->newptr == NULL)
706 rw_wlock(&tcp_function_lock);
707 blk = find_tcp_functions_locked(&fs);
709 (blk->tfb_flags & TCP_FUNC_BEING_REMOVED)) {
713 V_tcp_func_set_ptr = blk;
715 rw_wunlock(&tcp_function_lock);
719 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, functions_default,
720 CTLFLAG_VNET | CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
721 NULL, 0, sysctl_net_inet_default_tcp_functions, "A",
722 "Set/get the default TCP functions");
725 sysctl_net_inet_list_available(SYSCTL_HANDLER_ARGS)
727 int error, cnt, linesz;
728 struct tcp_function *f;
734 rw_rlock(&tcp_function_lock);
735 TAILQ_FOREACH(f, &t_functions, tf_next) {
738 rw_runlock(&tcp_function_lock);
740 bufsz = (cnt+2) * ((TCP_FUNCTION_NAME_LEN_MAX * 2) + 13) + 1;
741 buffer = malloc(bufsz, M_TEMP, M_WAITOK);
746 linesz = snprintf(cp, bufsz, "\n%-32s%c %-32s %s\n", "Stack", 'D',
747 "Alias", "PCB count");
752 rw_rlock(&tcp_function_lock);
753 TAILQ_FOREACH(f, &t_functions, tf_next) {
754 alias = (f->tf_name != f->tf_fb->tfb_tcp_block_name);
755 linesz = snprintf(cp, bufsz, "%-32s%c %-32s %u\n",
756 f->tf_fb->tfb_tcp_block_name,
757 (f->tf_fb == V_tcp_func_set_ptr) ? '*' : ' ',
758 alias ? f->tf_name : "-",
759 f->tf_fb->tfb_refcnt);
760 if (linesz >= bufsz) {
768 rw_runlock(&tcp_function_lock);
770 error = sysctl_handle_string(oidp, buffer, outsz + 1, req);
771 free(buffer, M_TEMP);
775 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, functions_available,
776 CTLFLAG_VNET | CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
777 NULL, 0, sysctl_net_inet_list_available, "A",
778 "list available TCP Function sets");
780 VNET_DEFINE(int, tcp_udp_tunneling_port) = TCP_TUNNELING_PORT_DEFAULT;
783 VNET_DEFINE(struct socket *, udp4_tun_socket) = NULL;
784 #define V_udp4_tun_socket VNET(udp4_tun_socket)
787 VNET_DEFINE(struct socket *, udp6_tun_socket) = NULL;
788 #define V_udp6_tun_socket VNET(udp6_tun_socket)
791 static struct sx tcpoudp_lock;
794 tcp_over_udp_stop(void)
797 sx_assert(&tcpoudp_lock, SA_XLOCKED);
800 if (V_udp4_tun_socket != NULL) {
801 soclose(V_udp4_tun_socket);
802 V_udp4_tun_socket = NULL;
806 if (V_udp6_tun_socket != NULL) {
807 soclose(V_udp6_tun_socket);
808 V_udp6_tun_socket = NULL;
814 tcp_over_udp_start(void)
819 struct sockaddr_in sin;
822 struct sockaddr_in6 sin6;
825 sx_assert(&tcpoudp_lock, SA_XLOCKED);
827 port = V_tcp_udp_tunneling_port;
828 if (ntohs(port) == 0) {
829 /* Must have a port set */
833 if (V_udp4_tun_socket != NULL) {
834 /* Already running -- must stop first */
839 if (V_udp6_tun_socket != NULL) {
840 /* Already running -- must stop first */
845 if ((ret = socreate(PF_INET, &V_udp4_tun_socket,
846 SOCK_DGRAM, IPPROTO_UDP,
847 curthread->td_ucred, curthread))) {
851 /* Call the special UDP hook. */
852 if ((ret = udp_set_kernel_tunneling(V_udp4_tun_socket,
853 tcp_recv_udp_tunneled_packet,
859 /* Ok, we have a socket, bind it to the port. */
860 memset(&sin, 0, sizeof(struct sockaddr_in));
861 sin.sin_len = sizeof(struct sockaddr_in);
862 sin.sin_family = AF_INET;
863 sin.sin_port = htons(port);
864 if ((ret = sobind(V_udp4_tun_socket,
865 (struct sockaddr *)&sin, curthread))) {
871 if ((ret = socreate(PF_INET6, &V_udp6_tun_socket,
872 SOCK_DGRAM, IPPROTO_UDP,
873 curthread->td_ucred, curthread))) {
877 /* Call the special UDP hook. */
878 if ((ret = udp_set_kernel_tunneling(V_udp6_tun_socket,
879 tcp_recv_udp_tunneled_packet,
880 tcp6_ctlinput_viaudp,
885 /* Ok, we have a socket, bind it to the port. */
886 memset(&sin6, 0, sizeof(struct sockaddr_in6));
887 sin6.sin6_len = sizeof(struct sockaddr_in6);
888 sin6.sin6_family = AF_INET6;
889 sin6.sin6_port = htons(port);
890 if ((ret = sobind(V_udp6_tun_socket,
891 (struct sockaddr *)&sin6, curthread))) {
900 sysctl_net_inet_tcp_udp_tunneling_port_check(SYSCTL_HANDLER_ARGS)
905 old = V_tcp_udp_tunneling_port;
907 error = sysctl_handle_int(oidp, &new, 0, req);
909 (req->newptr != NULL)) {
910 if ((new < TCP_TUNNELING_PORT_MIN) ||
911 (new > TCP_TUNNELING_PORT_MAX)) {
914 sx_xlock(&tcpoudp_lock);
915 V_tcp_udp_tunneling_port = new;
920 error = tcp_over_udp_start();
922 V_tcp_udp_tunneling_port = 0;
925 sx_xunlock(&tcpoudp_lock);
931 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, udp_tunneling_port,
932 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
933 &VNET_NAME(tcp_udp_tunneling_port),
934 0, &sysctl_net_inet_tcp_udp_tunneling_port_check, "IU",
935 "Tunneling port for tcp over udp");
937 VNET_DEFINE(int, tcp_udp_tunneling_overhead) = TCP_TUNNELING_OVERHEAD_DEFAULT;
940 sysctl_net_inet_tcp_udp_tunneling_overhead_check(SYSCTL_HANDLER_ARGS)
944 new = V_tcp_udp_tunneling_overhead;
945 error = sysctl_handle_int(oidp, &new, 0, req);
946 if (error == 0 && req->newptr) {
947 if ((new < TCP_TUNNELING_OVERHEAD_MIN) ||
948 (new > TCP_TUNNELING_OVERHEAD_MAX))
951 V_tcp_udp_tunneling_overhead = new;
956 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, udp_tunneling_overhead,
957 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
958 &VNET_NAME(tcp_udp_tunneling_overhead),
959 0, &sysctl_net_inet_tcp_udp_tunneling_overhead_check, "IU",
960 "MSS reduction when using tcp over udp");
963 * Exports one (struct tcp_function_info) for each alias/name.
966 sysctl_net_inet_list_func_info(SYSCTL_HANDLER_ARGS)
969 struct tcp_function *f;
970 struct tcp_function_info tfi;
973 * We don't allow writes.
975 if (req->newptr != NULL)
979 * Wire the old buffer so we can directly copy the functions to
980 * user space without dropping the lock.
982 if (req->oldptr != NULL) {
983 error = sysctl_wire_old_buffer(req, 0);
989 * Walk the list and copy out matching entries. If INVARIANTS
990 * is compiled in, also walk the list to verify the length of
991 * the list matches what we have recorded.
993 rw_rlock(&tcp_function_lock);
997 if (req->oldptr == NULL) {
1002 TAILQ_FOREACH(f, &t_functions, tf_next) {
1006 if (req->oldptr != NULL) {
1007 bzero(&tfi, sizeof(tfi));
1008 tfi.tfi_refcnt = f->tf_fb->tfb_refcnt;
1009 tfi.tfi_id = f->tf_fb->tfb_id;
1010 (void)strlcpy(tfi.tfi_alias, f->tf_name,
1011 sizeof(tfi.tfi_alias));
1012 (void)strlcpy(tfi.tfi_name,
1013 f->tf_fb->tfb_tcp_block_name, sizeof(tfi.tfi_name));
1014 error = SYSCTL_OUT(req, &tfi, sizeof(tfi));
1016 * Don't stop on error, as that is the
1017 * mechanism we use to accumulate length
1018 * information if the buffer was too short.
1022 KASSERT(cnt == tcp_fb_cnt,
1023 ("%s: cnt (%d) != tcp_fb_cnt (%d)", __func__, cnt, tcp_fb_cnt));
1027 rw_runlock(&tcp_function_lock);
1028 if (req->oldptr == NULL)
1029 error = SYSCTL_OUT(req, NULL,
1030 (cnt + 1) * sizeof(struct tcp_function_info));
1035 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, function_info,
1036 CTLTYPE_OPAQUE | CTLFLAG_SKIP | CTLFLAG_RD | CTLFLAG_MPSAFE,
1037 NULL, 0, sysctl_net_inet_list_func_info, "S,tcp_function_info",
1038 "List TCP function block name-to-ID mappings");
1041 * tfb_tcp_handoff_ok() function for the default stack.
1042 * Note that we'll basically try to take all comers.
1045 tcp_default_handoff_ok(struct tcpcb *tp)
1052 * tfb_tcp_fb_init() function for the default stack.
1054 * This handles making sure we have appropriate timers set if you are
1055 * transitioning a socket that has some amount of setup done.
1057 * The init() fuction from the default can *never* return non-zero i.e.
1058 * it is required to always succeed since it is the stack of last resort!
1061 tcp_default_fb_init(struct tcpcb *tp, void **ptr)
1063 struct socket *so = tptosocket(tp);
1066 INP_WLOCK_ASSERT(tptoinpcb(tp));
1067 /* We don't use the pointer */
1070 KASSERT(tp->t_state >= 0 && tp->t_state < TCPS_TIME_WAIT,
1071 ("%s: connection %p in unexpected state %d", __func__, tp,
1074 /* Make sure we get no interesting mbuf queuing behavior */
1075 /* All mbuf queue/ack compress flags should be off */
1076 tcp_lro_features_off(tp);
1078 /* Cancel the GP measurement in progress */
1079 tp->t_flags &= ~TF_GPUTINPROG;
1080 /* Validate the timers are not in usec, if they are convert */
1081 tcp_change_time_units(tp, TCP_TMR_GRANULARITY_TICKS);
1082 if ((tp->t_state == TCPS_SYN_SENT) ||
1083 (tp->t_state == TCPS_SYN_RECEIVED))
1084 rexmt = tcp_rexmit_initial * tcp_backoff[tp->t_rxtshift];
1086 rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift];
1087 if (tp->t_rxtshift == 0)
1088 tp->t_rxtcur = rexmt;
1090 TCPT_RANGESET(tp->t_rxtcur, rexmt, tp->t_rttmin, TCPTV_REXMTMAX);
1093 * Nothing to do for ESTABLISHED or LISTEN states. And, we don't
1094 * know what to do for unexpected states (which includes TIME_WAIT).
1096 if (tp->t_state <= TCPS_LISTEN || tp->t_state >= TCPS_TIME_WAIT)
1100 * Make sure some kind of transmission timer is set if there is
1103 if ((!TCPS_HAVEESTABLISHED(tp->t_state) || sbavail(&so->so_snd) ||
1104 tp->snd_una != tp->snd_max) && !(tcp_timer_active(tp, TT_REXMT) ||
1105 tcp_timer_active(tp, TT_PERSIST))) {
1107 * If the session has established and it looks like it should
1108 * be in the persist state, set the persist timer. Otherwise,
1109 * set the retransmit timer.
1111 if (TCPS_HAVEESTABLISHED(tp->t_state) && tp->snd_wnd == 0 &&
1112 (int32_t)(tp->snd_nxt - tp->snd_una) <
1113 (int32_t)sbavail(&so->so_snd))
1116 tcp_timer_activate(tp, TT_REXMT, TP_RXTCUR(tp));
1119 /* All non-embryonic sessions get a keepalive timer. */
1120 if (!tcp_timer_active(tp, TT_KEEP))
1121 tcp_timer_activate(tp, TT_KEEP,
1122 TCPS_HAVEESTABLISHED(tp->t_state) ? TP_KEEPIDLE(tp) :
1126 * Make sure critical variables are initialized
1127 * if transitioning while in Recovery.
1129 if IN_FASTRECOVERY(tp->t_flags) {
1130 if (tp->sackhint.recover_fs == 0)
1131 tp->sackhint.recover_fs = max(1,
1132 tp->snd_nxt - tp->snd_una);
1139 * tfb_tcp_fb_fini() function for the default stack.
1141 * This changes state as necessary (or prudent) to prepare for another stack
1142 * to assume responsibility for the connection.
1145 tcp_default_fb_fini(struct tcpcb *tp, int tcb_is_purged)
1148 INP_WLOCK_ASSERT(tptoinpcb(tp));
1151 tcp_log_flowend(tp);
1158 * Target size of TCP PCB hash tables. Must be a power of two.
1160 * Note that this can be overridden by the kernel environment
1161 * variable net.inet.tcp.tcbhashsize
1164 #define TCBHASHSIZE 0
1167 MALLOC_DEFINE(M_TCPLOG, "tcplog", "TCP address and flags print buffers");
1168 MALLOC_DEFINE(M_TCPFUNCTIONS, "tcpfunc", "TCP function set memory");
1170 static struct mtx isn_mtx;
1172 #define ISN_LOCK_INIT() mtx_init(&isn_mtx, "isn_mtx", NULL, MTX_DEF)
1173 #define ISN_LOCK() mtx_lock(&isn_mtx)
1174 #define ISN_UNLOCK() mtx_unlock(&isn_mtx)
1176 INPCBSTORAGE_DEFINE(tcpcbstor, tcpcb, "tcpinp", "tcp_inpcb", "tcp", "tcphash");
1179 * Take a value and get the next power of 2 that doesn't overflow.
1180 * Used to size the tcp_inpcb hash buckets.
1183 maketcp_hashsize(int size)
1189 * get the next power of 2 higher than maxsockets.
1191 hashsize = 1 << fls(size);
1192 /* catch overflow, and just go one power of 2 smaller */
1193 if (hashsize < size) {
1194 hashsize = 1 << (fls(size) - 1);
1199 static volatile int next_tcp_stack_id = 1;
1202 * Register a TCP function block with the name provided in the names
1203 * array. (Note that this function does NOT automatically register
1204 * blk->tfb_tcp_block_name as a stack name. Therefore, you should
1205 * explicitly include blk->tfb_tcp_block_name in the list of names if
1206 * you wish to register the stack with that name.)
1208 * Either all name registrations will succeed or all will fail. If
1209 * a name registration fails, the function will update the num_names
1210 * argument to point to the array index of the name that encountered
1213 * Returns 0 on success, or an error code on failure.
1216 register_tcp_functions_as_names(struct tcp_function_block *blk, int wait,
1217 const char *names[], int *num_names)
1219 struct tcp_function *n;
1220 struct tcp_function_set fs;
1223 KASSERT(names != NULL && *num_names > 0,
1224 ("%s: Called with 0-length name list", __func__));
1225 KASSERT(names != NULL, ("%s: Called with NULL name list", __func__));
1226 KASSERT(rw_initialized(&tcp_function_lock),
1227 ("%s: called too early", __func__));
1229 if ((blk->tfb_tcp_output == NULL) ||
1230 (blk->tfb_tcp_do_segment == NULL) ||
1231 (blk->tfb_tcp_ctloutput == NULL) ||
1232 (strlen(blk->tfb_tcp_block_name) == 0)) {
1234 * These functions are required and you
1241 if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) {
1246 refcount_init(&blk->tfb_refcnt, 0);
1247 blk->tfb_id = atomic_fetchadd_int(&next_tcp_stack_id, 1);
1248 for (i = 0; i < *num_names; i++) {
1249 n = malloc(sizeof(struct tcp_function), M_TCPFUNCTIONS, wait);
1256 (void)strlcpy(fs.function_set_name, names[i],
1257 sizeof(fs.function_set_name));
1258 rw_wlock(&tcp_function_lock);
1259 if (find_tcp_functions_locked(&fs) != NULL) {
1260 /* Duplicate name space not allowed */
1261 rw_wunlock(&tcp_function_lock);
1262 free(n, M_TCPFUNCTIONS);
1266 (void)strlcpy(n->tf_name, names[i], sizeof(n->tf_name));
1267 TAILQ_INSERT_TAIL(&t_functions, n, tf_next);
1269 rw_wunlock(&tcp_function_lock);
1275 * Deregister the names we just added. Because registration failed
1276 * for names[i], we don't need to deregister that name.
1279 rw_wlock(&tcp_function_lock);
1281 TAILQ_FOREACH(n, &t_functions, tf_next) {
1282 if (!strncmp(n->tf_name, names[i],
1283 TCP_FUNCTION_NAME_LEN_MAX)) {
1284 TAILQ_REMOVE(&t_functions, n, tf_next);
1287 free(n, M_TCPFUNCTIONS);
1292 rw_wunlock(&tcp_function_lock);
1297 * Register a TCP function block using the name provided in the name
1300 * Returns 0 on success, or an error code on failure.
1303 register_tcp_functions_as_name(struct tcp_function_block *blk, const char *name,
1306 const char *name_list[1];
1311 name_list[0] = name;
1313 name_list[0] = blk->tfb_tcp_block_name;
1314 rv = register_tcp_functions_as_names(blk, wait, name_list, &num_names);
1319 * Register a TCP function block using the name defined in
1320 * blk->tfb_tcp_block_name.
1322 * Returns 0 on success, or an error code on failure.
1325 register_tcp_functions(struct tcp_function_block *blk, int wait)
1328 return (register_tcp_functions_as_name(blk, NULL, wait));
1332 * Deregister all names associated with a function block. This
1333 * functionally removes the function block from use within the system.
1335 * When called with a true quiesce argument, mark the function block
1336 * as being removed so no more stacks will use it and determine
1337 * whether the removal would succeed.
1339 * When called with a false quiesce argument, actually attempt the
1342 * When called with a force argument, attempt to switch all TCBs to
1343 * use the default stack instead of returning EBUSY.
1345 * Returns 0 on success (or if the removal would succeed), or an error
1349 deregister_tcp_functions(struct tcp_function_block *blk, bool quiesce,
1352 struct tcp_function *f;
1353 VNET_ITERATOR_DECL(vnet_iter);
1355 if (blk == &tcp_def_funcblk) {
1356 /* You can't un-register the default */
1359 rw_wlock(&tcp_function_lock);
1360 VNET_LIST_RLOCK_NOSLEEP();
1361 VNET_FOREACH(vnet_iter) {
1362 CURVNET_SET(vnet_iter);
1363 if (blk == V_tcp_func_set_ptr) {
1364 /* You can't free the current default in some vnet. */
1366 VNET_LIST_RUNLOCK_NOSLEEP();
1367 rw_wunlock(&tcp_function_lock);
1372 VNET_LIST_RUNLOCK_NOSLEEP();
1373 /* Mark the block so no more stacks can use it. */
1374 blk->tfb_flags |= TCP_FUNC_BEING_REMOVED;
1376 * If TCBs are still attached to the stack, attempt to switch them
1377 * to the default stack.
1379 if (force && blk->tfb_refcnt) {
1382 VNET_ITERATOR_DECL(vnet_iter);
1384 rw_wunlock(&tcp_function_lock);
1387 VNET_FOREACH(vnet_iter) {
1388 CURVNET_SET(vnet_iter);
1389 struct inpcb_iterator inpi = INP_ALL_ITERATOR(&V_tcbinfo,
1390 INPLOOKUP_WLOCKPCB);
1392 while ((inp = inp_next(&inpi)) != NULL) {
1393 tp = intotcpcb(inp);
1394 if (tp == NULL || tp->t_fb != blk)
1396 tcp_switch_back_to_default(tp);
1400 VNET_LIST_RUNLOCK();
1402 rw_wlock(&tcp_function_lock);
1404 if (blk->tfb_refcnt) {
1405 /* TCBs still attached. */
1406 rw_wunlock(&tcp_function_lock);
1411 rw_wunlock(&tcp_function_lock);
1414 /* Remove any function names that map to this function block. */
1415 while (find_tcp_fb_locked(blk, &f) != NULL) {
1416 TAILQ_REMOVE(&t_functions, f, tf_next);
1419 free(f, M_TCPFUNCTIONS);
1421 rw_wunlock(&tcp_function_lock);
1428 struct epoch_tracker et;
1429 VNET_ITERATOR_DECL(vnet_iter);
1434 NET_EPOCH_ENTER(et);
1435 VNET_LIST_RLOCK_NOSLEEP();
1436 VNET_FOREACH(vnet_iter) {
1437 CURVNET_SET(vnet_iter);
1438 struct inpcb_iterator inpi = INP_ALL_ITERATOR(&V_tcbinfo,
1439 INPLOOKUP_WLOCKPCB);
1444 * Walk the tcpbs, if existing, and flush the reassembly queue,
1445 * if there is one...
1446 * XXX: The "Net/3" implementation doesn't imply that the TCP
1447 * reassembly queue should be flushed, but in a situation
1448 * where we're really low on mbufs, this is potentially
1451 while ((inpb = inp_next(&inpi)) != NULL) {
1452 if ((tcpb = intotcpcb(inpb)) != NULL) {
1453 tcp_reass_flush(tcpb);
1454 tcp_clean_sackreport(tcpb);
1456 tcp_log_drain(tcpb);
1459 if (tcp_pcap_aggressive_free) {
1460 /* Free the TCP PCAP queues. */
1461 tcp_pcap_drain(&(tcpb->t_inpkts));
1462 tcp_pcap_drain(&(tcpb->t_outpkts));
1469 VNET_LIST_RUNLOCK_NOSLEEP();
1474 tcp_vnet_init(void *arg __unused)
1478 if (hhook_head_register(HHOOK_TYPE_TCP, HHOOK_TCP_EST_IN,
1479 &V_tcp_hhh[HHOOK_TCP_EST_IN], HHOOK_NOWAIT|HHOOK_HEADISINVNET) != 0)
1480 printf("%s: WARNING: unable to register helper hook\n", __func__);
1481 if (hhook_head_register(HHOOK_TYPE_TCP, HHOOK_TCP_EST_OUT,
1482 &V_tcp_hhh[HHOOK_TCP_EST_OUT], HHOOK_NOWAIT|HHOOK_HEADISINVNET) != 0)
1483 printf("%s: WARNING: unable to register helper hook\n", __func__);
1486 if (tcp_stats_init())
1487 printf("%s: WARNING: unable to initialise TCP stats\n",
1490 in_pcbinfo_init(&V_tcbinfo, &tcpcbstor, tcp_tcbhashsize,
1496 TUNABLE_INT_FETCH("net.inet.tcp.sack.enable", &V_tcp_do_sack);
1497 V_sack_hole_zone = uma_zcreate("sackhole", sizeof(struct sackhole),
1498 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
1500 tcp_fastopen_init();
1502 COUNTER_ARRAY_ALLOC(V_tcps_states, TCP_NSTATES, M_WAITOK);
1503 VNET_PCPUSTAT_ALLOC(tcpstat, M_WAITOK);
1505 V_tcp_msl = TCPTV_MSL;
1507 VNET_SYSINIT(tcp_vnet_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH,
1508 tcp_vnet_init, NULL);
1511 tcp_init(void *arg __unused)
1513 const char *tcbhash_tuneable;
1516 tcp_reass_global_init();
1518 /* XXX virtualize those below? */
1519 tcp_delacktime = TCPTV_DELACK;
1520 tcp_keepinit = TCPTV_KEEP_INIT;
1521 tcp_keepidle = TCPTV_KEEP_IDLE;
1522 tcp_keepintvl = TCPTV_KEEPINTVL;
1523 tcp_maxpersistidle = TCPTV_KEEP_IDLE;
1524 tcp_rexmit_initial = TCPTV_RTOBASE;
1525 if (tcp_rexmit_initial < 1)
1526 tcp_rexmit_initial = 1;
1527 tcp_rexmit_min = TCPTV_MIN;
1528 if (tcp_rexmit_min < 1)
1530 tcp_persmin = TCPTV_PERSMIN;
1531 tcp_persmax = TCPTV_PERSMAX;
1532 tcp_rexmit_slop = TCPTV_CPU_VAR;
1533 tcp_finwait2_timeout = TCPTV_FINWAIT2_TIMEOUT;
1535 /* Setup the tcp function block list */
1536 TAILQ_INIT(&t_functions);
1537 rw_init(&tcp_function_lock, "tcp_func_lock");
1538 register_tcp_functions(&tcp_def_funcblk, M_WAITOK);
1539 sx_init(&tcpoudp_lock, "TCP over UDP configuration");
1541 /* Initialize the TCP logging data. */
1544 arc4rand(&V_ts_offset_secret, sizeof(V_ts_offset_secret), 0);
1546 if (tcp_soreceive_stream) {
1548 tcp_protosw.pr_soreceive = soreceive_stream;
1551 tcp6_protosw.pr_soreceive = soreceive_stream;
1556 max_protohdr_grow(sizeof(struct ip6_hdr) + sizeof(struct tcphdr));
1558 max_protohdr_grow(sizeof(struct tcpiphdr));
1562 EVENTHANDLER_REGISTER(shutdown_pre_sync, tcp_fini, NULL,
1563 SHUTDOWN_PRI_DEFAULT);
1564 EVENTHANDLER_REGISTER(vm_lowmem, tcp_drain, NULL, LOWMEM_PRI_DEFAULT);
1565 EVENTHANDLER_REGISTER(mbuf_lowmem, tcp_drain, NULL, LOWMEM_PRI_DEFAULT);
1567 tcp_inp_lro_direct_queue = counter_u64_alloc(M_WAITOK);
1568 tcp_inp_lro_wokeup_queue = counter_u64_alloc(M_WAITOK);
1569 tcp_inp_lro_compressed = counter_u64_alloc(M_WAITOK);
1570 tcp_inp_lro_locks_taken = counter_u64_alloc(M_WAITOK);
1571 tcp_extra_mbuf = counter_u64_alloc(M_WAITOK);
1572 tcp_would_have_but = counter_u64_alloc(M_WAITOK);
1573 tcp_comp_total = counter_u64_alloc(M_WAITOK);
1574 tcp_uncomp_total = counter_u64_alloc(M_WAITOK);
1575 tcp_bad_csums = counter_u64_alloc(M_WAITOK);
1576 tcp_pacing_failures = counter_u64_alloc(M_WAITOK);
1581 hashsize = TCBHASHSIZE;
1582 tcbhash_tuneable = "net.inet.tcp.tcbhashsize";
1583 TUNABLE_INT_FETCH(tcbhash_tuneable, &hashsize);
1584 if (hashsize == 0) {
1586 * Auto tune the hash size based on maxsockets.
1587 * A perfect hash would have a 1:1 mapping
1588 * (hashsize = maxsockets) however it's been
1589 * suggested that O(2) average is better.
1591 hashsize = maketcp_hashsize(maxsockets / 4);
1593 * Our historical default is 512,
1594 * do not autotune lower than this.
1599 printf("%s: %s auto tuned to %d\n", __func__,
1600 tcbhash_tuneable, hashsize);
1603 * We require a hashsize to be a power of two.
1604 * Previously if it was not a power of two we would just reset it
1605 * back to 512, which could be a nasty surprise if you did not notice
1606 * the error message.
1607 * Instead what we do is clip it to the closest power of two lower
1608 * than the specified hash value.
1610 if (!powerof2(hashsize)) {
1611 int oldhashsize = hashsize;
1613 hashsize = maketcp_hashsize(hashsize);
1614 /* prevent absurdly low value */
1617 printf("%s: WARNING: TCB hash size not a power of 2, "
1618 "clipped from %d to %d.\n", __func__, oldhashsize,
1621 tcp_tcbhashsize = hashsize;
1624 IPPROTO_REGISTER(IPPROTO_TCP, tcp_input, tcp_ctlinput);
1627 IP6PROTO_REGISTER(IPPROTO_TCP, tcp6_input, tcp6_ctlinput);
1630 SYSINIT(tcp_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, tcp_init, NULL);
1634 tcp_destroy(void *unused __unused)
1642 * All our processes are gone, all our sockets should be cleaned
1643 * up, which means, we should be past the tcp_discardcb() calls.
1644 * Sleep to let all tcpcb timers really disappear and cleanup.
1647 INP_INFO_WLOCK(&V_tcbinfo);
1648 n = V_tcbinfo.ipi_count;
1649 INP_INFO_WUNLOCK(&V_tcbinfo);
1652 pause("tcpdes", hz / 10);
1656 in_pcbinfo_destroy(&V_tcbinfo);
1657 /* tcp_discardcb() clears the sack_holes up. */
1658 uma_zdestroy(V_sack_hole_zone);
1661 * Cannot free the zone until all tcpcbs are released as we attach
1662 * the allocations to them.
1664 tcp_fastopen_destroy();
1666 COUNTER_ARRAY_FREE(V_tcps_states, TCP_NSTATES);
1667 VNET_PCPUSTAT_FREE(tcpstat);
1670 error = hhook_head_deregister(V_tcp_hhh[HHOOK_TCP_EST_IN]);
1672 printf("%s: WARNING: unable to deregister helper hook "
1673 "type=%d, id=%d: error %d returned\n", __func__,
1674 HHOOK_TYPE_TCP, HHOOK_TCP_EST_IN, error);
1676 error = hhook_head_deregister(V_tcp_hhh[HHOOK_TCP_EST_OUT]);
1678 printf("%s: WARNING: unable to deregister helper hook "
1679 "type=%d, id=%d: error %d returned\n", __func__,
1680 HHOOK_TYPE_TCP, HHOOK_TCP_EST_OUT, error);
1684 VNET_SYSUNINIT(tcp, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH, tcp_destroy, NULL);
1694 * Fill in the IP and TCP headers for an outgoing packet, given the tcpcb.
1695 * tcp_template used to store this data in mbufs, but we now recopy it out
1696 * of the tcpcb each time to conserve mbufs.
1699 tcpip_fillheaders(struct inpcb *inp, uint16_t port, void *ip_ptr, void *tcp_ptr)
1701 struct tcphdr *th = (struct tcphdr *)tcp_ptr;
1703 INP_WLOCK_ASSERT(inp);
1706 if ((inp->inp_vflag & INP_IPV6) != 0) {
1707 struct ip6_hdr *ip6;
1709 ip6 = (struct ip6_hdr *)ip_ptr;
1710 ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) |
1711 (inp->inp_flow & IPV6_FLOWINFO_MASK);
1712 ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) |
1713 (IPV6_VERSION & IPV6_VERSION_MASK);
1715 ip6->ip6_nxt = IPPROTO_TCP;
1717 ip6->ip6_nxt = IPPROTO_UDP;
1718 ip6->ip6_plen = htons(sizeof(struct tcphdr));
1719 ip6->ip6_src = inp->in6p_laddr;
1720 ip6->ip6_dst = inp->in6p_faddr;
1723 #if defined(INET6) && defined(INET)
1730 ip = (struct ip *)ip_ptr;
1731 ip->ip_v = IPVERSION;
1733 ip->ip_tos = inp->inp_ip_tos;
1737 ip->ip_ttl = inp->inp_ip_ttl;
1740 ip->ip_p = IPPROTO_TCP;
1742 ip->ip_p = IPPROTO_UDP;
1743 ip->ip_src = inp->inp_laddr;
1744 ip->ip_dst = inp->inp_faddr;
1747 th->th_sport = inp->inp_lport;
1748 th->th_dport = inp->inp_fport;
1752 tcp_set_flags(th, 0);
1755 th->th_sum = 0; /* in_pseudo() is called later for ipv4 */
1759 * Create template to be used to send tcp packets on a connection.
1760 * Allocates an mbuf and fills in a skeletal tcp/ip header. The only
1761 * use for this function is in keepalives, which use tcp_respond.
1764 tcpip_maketemplate(struct inpcb *inp)
1768 t = malloc(sizeof(*t), M_TEMP, M_NOWAIT);
1771 tcpip_fillheaders(inp, 0, (void *)&t->tt_ipgen, (void *)&t->tt_t);
1776 * Send a single message to the TCP at address specified by
1777 * the given TCP/IP header. If m == NULL, then we make a copy
1778 * of the tcpiphdr at th and send directly to the addressed host.
1779 * This is used to force keep alive messages out using the TCP
1780 * template for a connection. If flags are given then we send
1781 * a message back to the TCP which originated the segment th,
1782 * and discard the mbuf containing it and any other attached mbufs.
1784 * In any case the ack and sequence number of the transmitted
1785 * segment are as specified by the parameters.
1787 * NOTE: If m != NULL, then th must point to *inside* the mbuf.
1790 tcp_respond(struct tcpcb *tp, void *ipgen, struct tcphdr *th, struct mbuf *m,
1791 tcp_seq ack, tcp_seq seq, uint16_t flags)
1797 struct udphdr *uh = NULL;
1799 struct tcp_log_buffer *lgb;
1802 struct ip6_hdr *ip6;
1805 int optlen, tlen, win, ulen;
1811 int thflags = tcp_get_flags(th);
1814 KASSERT(tp != NULL || m != NULL, ("tcp_respond: tp and m both NULL"));
1818 isipv6 = ((struct ip *)ipgen)->ip_v == (IPV6_VERSION >> 4);
1824 inp = tptoinpcb(tp);
1825 INP_LOCK_ASSERT(inp);
1831 if (isipv6 && ip6 && (ip6->ip6_nxt == IPPROTO_UDP))
1832 port = m->m_pkthdr.tcp_tun_port;
1835 if (ip && (ip->ip_p == IPPROTO_UDP))
1836 port = m->m_pkthdr.tcp_tun_port;
1845 if (!(flags & TH_RST)) {
1846 win = sbspace(&inp->inp_socket->so_rcv);
1847 if (win > TCP_MAXWIN << tp->rcv_scale)
1848 win = TCP_MAXWIN << tp->rcv_scale;
1850 if ((tp->t_flags & TF_NOOPT) == 0)
1854 m = m_gethdr(M_NOWAIT, MT_DATA);
1857 m->m_data += max_linkhdr;
1860 bcopy((caddr_t)ip6, mtod(m, caddr_t),
1861 sizeof(struct ip6_hdr));
1862 ip6 = mtod(m, struct ip6_hdr *);
1863 nth = (struct tcphdr *)(ip6 + 1);
1865 /* Insert a UDP header */
1866 uh = (struct udphdr *)nth;
1867 uh->uh_sport = htons(V_tcp_udp_tunneling_port);
1868 uh->uh_dport = port;
1869 nth = (struct tcphdr *)(uh + 1);
1874 bcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip));
1875 ip = mtod(m, struct ip *);
1876 nth = (struct tcphdr *)(ip + 1);
1878 /* Insert a UDP header */
1879 uh = (struct udphdr *)nth;
1880 uh->uh_sport = htons(V_tcp_udp_tunneling_port);
1881 uh->uh_dport = port;
1882 nth = (struct tcphdr *)(uh + 1);
1885 bcopy((caddr_t)th, (caddr_t)nth, sizeof(struct tcphdr));
1887 } else if ((!M_WRITABLE(m)) || (port != 0)) {
1890 /* Can't reuse 'm', allocate a new mbuf. */
1891 n = m_gethdr(M_NOWAIT, MT_DATA);
1897 if (!m_dup_pkthdr(n, m, M_NOWAIT)) {
1903 n->m_data += max_linkhdr;
1904 /* m_len is set later */
1905 #define xchg(a,b,type) { type t; t=a; a=b; b=t; }
1908 bcopy((caddr_t)ip6, mtod(n, caddr_t),
1909 sizeof(struct ip6_hdr));
1910 ip6 = mtod(n, struct ip6_hdr *);
1911 xchg(ip6->ip6_dst, ip6->ip6_src, struct in6_addr);
1912 nth = (struct tcphdr *)(ip6 + 1);
1914 /* Insert a UDP header */
1915 uh = (struct udphdr *)nth;
1916 uh->uh_sport = htons(V_tcp_udp_tunneling_port);
1917 uh->uh_dport = port;
1918 nth = (struct tcphdr *)(uh + 1);
1923 bcopy((caddr_t)ip, mtod(n, caddr_t), sizeof(struct ip));
1924 ip = mtod(n, struct ip *);
1925 xchg(ip->ip_dst.s_addr, ip->ip_src.s_addr, uint32_t);
1926 nth = (struct tcphdr *)(ip + 1);
1928 /* Insert a UDP header */
1929 uh = (struct udphdr *)nth;
1930 uh->uh_sport = htons(V_tcp_udp_tunneling_port);
1931 uh->uh_dport = port;
1932 nth = (struct tcphdr *)(uh + 1);
1935 bcopy((caddr_t)th, (caddr_t)nth, sizeof(struct tcphdr));
1936 xchg(nth->th_dport, nth->th_sport, uint16_t);
1943 * XXX MRT We inherit the FIB, which is lucky.
1947 m->m_data = (caddr_t)ipgen;
1948 /* clear any receive flags for proper bpf timestamping */
1949 m->m_flags &= ~(M_TSTMP | M_TSTMP_LRO);
1950 /* m_len is set later */
1953 xchg(ip6->ip6_dst, ip6->ip6_src, struct in6_addr);
1954 nth = (struct tcphdr *)(ip6 + 1);
1958 xchg(ip->ip_dst.s_addr, ip->ip_src.s_addr, uint32_t);
1959 nth = (struct tcphdr *)(ip + 1);
1963 * this is usually a case when an extension header
1964 * exists between the IPv6 header and the
1967 nth->th_sport = th->th_sport;
1968 nth->th_dport = th->th_dport;
1970 xchg(nth->th_dport, nth->th_sport, uint16_t);
1976 tlen = sizeof (struct ip6_hdr) + sizeof (struct tcphdr);
1978 #if defined(INET) && defined(INET6)
1982 tlen = sizeof (struct tcpiphdr);
1985 tlen += sizeof (struct udphdr);
1988 KASSERT(M_TRAILINGSPACE(m) >= tlen,
1989 ("Not enough trailing space for message (m=%p, need=%d, have=%ld)",
1990 m, tlen, (long)M_TRAILINGSPACE(m)));
1995 ect = tcp_ecn_output_established(tp, &flags, 0, false);
1996 /* Make sure we have room. */
1997 if (M_TRAILINGSPACE(m) < TCP_MAXOLEN) {
1998 m->m_next = m_get(M_NOWAIT, MT_DATA);
2000 optp = mtod(m->m_next, u_char *);
2005 optp = (u_char *) (nth + 1);
2011 if (tp->t_flags & TF_RCVD_TSTMP) {
2012 to.to_tsval = tcp_ts_getticks() + tp->ts_offset;
2013 to.to_tsecr = tp->ts_recent;
2014 to.to_flags |= TOF_TS;
2016 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
2017 /* TCP-MD5 (RFC2385). */
2018 if (tp->t_flags & TF_SIGNATURE)
2019 to.to_flags |= TOF_SIGNATURE;
2021 /* Add the options. */
2022 tlen += optlen = tcp_addoptions(&to, optp);
2024 /* Update m_len in the correct mbuf. */
2025 optm->m_len += optlen;
2031 ulen = tlen - sizeof(struct ip6_hdr);
2032 uh->uh_ulen = htons(ulen);
2034 ip6->ip6_flow = htonl(ect << IPV6_FLOWLABEL_LEN);
2035 ip6->ip6_vfc = IPV6_VERSION;
2037 ip6->ip6_nxt = IPPROTO_UDP;
2039 ip6->ip6_nxt = IPPROTO_TCP;
2040 ip6->ip6_plen = htons(tlen - sizeof(*ip6));
2043 #if defined(INET) && defined(INET6)
2049 ulen = tlen - sizeof(struct ip);
2050 uh->uh_ulen = htons(ulen);
2052 ip->ip_len = htons(tlen);
2054 ip->ip_tos = inp->inp_ip_tos & ~IPTOS_ECN_MASK;
2055 ip->ip_ttl = inp->inp_ip_ttl;
2058 ip->ip_ttl = V_ip_defttl;
2062 ip->ip_p = IPPROTO_UDP;
2064 ip->ip_p = IPPROTO_TCP;
2066 if (V_path_mtu_discovery)
2067 ip->ip_off |= htons(IP_DF);
2070 m->m_pkthdr.len = tlen;
2071 m->m_pkthdr.rcvif = NULL;
2075 * Packet is associated with a socket, so allow the
2076 * label of the response to reflect the socket label.
2078 INP_LOCK_ASSERT(inp);
2079 mac_inpcb_create_mbuf(inp, m);
2082 * Packet is not associated with a socket, so possibly
2083 * update the label in place.
2085 mac_netinet_tcp_reply(m);
2088 nth->th_seq = htonl(seq);
2089 nth->th_ack = htonl(ack);
2090 nth->th_off = (sizeof (struct tcphdr) + optlen) >> 2;
2091 tcp_set_flags(nth, flags);
2092 if (tp && (flags & TH_RST)) {
2094 tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST);
2097 nth->th_win = htons((u_short) (win >> tp->rcv_scale));
2099 nth->th_win = htons((u_short)win);
2102 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
2103 if (to.to_flags & TOF_SIGNATURE) {
2104 if (!TCPMD5_ENABLED() ||
2105 TCPMD5_OUTPUT(m, nth, to.to_signature) != 0) {
2115 m->m_pkthdr.csum_flags = CSUM_UDP_IPV6;
2116 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
2117 uh->uh_sum = in6_cksum_pseudo(ip6, ulen, IPPROTO_UDP, 0);
2120 m->m_pkthdr.csum_flags = CSUM_TCP_IPV6;
2121 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
2122 nth->th_sum = in6_cksum_pseudo(ip6,
2123 tlen - sizeof(struct ip6_hdr), IPPROTO_TCP, 0);
2125 ip6->ip6_hlim = in6_selecthlim(inp, NULL);
2128 #if defined(INET6) && defined(INET)
2134 uh->uh_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr,
2135 htons(ulen + IPPROTO_UDP));
2136 m->m_pkthdr.csum_flags = CSUM_UDP;
2137 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
2140 m->m_pkthdr.csum_flags = CSUM_TCP;
2141 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
2142 nth->th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr,
2143 htons((u_short)(tlen - sizeof(struct ip) + ip->ip_p)));
2147 TCP_PROBE3(debug__output, tp, th, m);
2149 TCP_PROBE5(accept__refused, NULL, NULL, m, tp, nth);
2151 if ((tp != NULL) && tcp_bblogging_on(tp)) {
2152 if (INP_WLOCKED(inp)) {
2153 union tcp_log_stackspecific log;
2156 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2157 log.u_bbr.inhpts = tcp_in_hpts(tp);
2158 log.u_bbr.flex8 = 4;
2159 log.u_bbr.pkts_out = tp->t_maxseg;
2160 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2161 log.u_bbr.delivered = 0;
2162 lgb = tcp_log_event(tp, nth, NULL, NULL, TCP_LOG_OUT,
2163 ERRNO_UNK, 0, &log, false, NULL, NULL, 0, &tv);
2166 * We can not log the packet, since we only own the
2167 * read lock, but a write lock is needed. The read lock
2168 * is not upgraded to a write lock, since only getting
2169 * the read lock was done intentionally to improve the
2170 * handling of SYN flooding attacks.
2171 * This happens only for pure SYN segments received in
2172 * the initial CLOSED state, or received in a more
2173 * advanced state than listen and the UDP encapsulation
2174 * port is unexpected.
2175 * The incoming SYN segments do not really belong to
2176 * the TCP connection and the handling does not change
2177 * the state of the TCP connection. Therefore, the
2178 * sending of the RST segments is not logged. Please
2179 * note that also the incoming SYN segments are not
2182 * The following code ensures that the above description
2183 * is and stays correct.
2185 KASSERT((thflags & (TH_ACK|TH_SYN)) == TH_SYN &&
2186 (tp->t_state == TCPS_CLOSED ||
2187 (tp->t_state > TCPS_LISTEN && tp->t_port != port)),
2188 ("%s: Logging of TCP segment with flags 0x%b and "
2189 "UDP encapsulation port %u skipped in state %s",
2190 __func__, thflags, PRINT_TH_FLAGS,
2191 ntohs(port), tcpstates[tp->t_state]));
2196 TCPSTAT_INC(tcps_sndacks);
2197 else if (flags & (TH_SYN|TH_FIN|TH_RST))
2198 TCPSTAT_INC(tcps_sndctrl);
2199 TCPSTAT_INC(tcps_sndtotal);
2203 TCP_PROBE5(send, NULL, tp, ip6, tp, nth);
2204 output_ret = ip6_output(m, inp ? inp->in6p_outputopts : NULL,
2205 NULL, 0, NULL, NULL, inp);
2208 #if defined(INET) && defined(INET6)
2213 TCP_PROBE5(send, NULL, tp, ip, tp, nth);
2214 output_ret = ip_output(m, NULL, NULL, 0, NULL, inp);
2218 lgb->tlb_errno = output_ret;
2222 * Create a new TCP control block, making an empty reassembly queue and hooking
2223 * it to the argument protocol control block. The `inp' parameter must have
2224 * come from the zone allocator set up by tcpcbstor declaration.
2227 tcp_newtcpcb(struct inpcb *inp)
2229 struct tcpcb *tp = intotcpcb(inp);
2231 int isipv6 = (inp->inp_vflag & INP_IPV6) != 0;
2235 * Historically allocation was done with M_ZERO. There is a lot of
2236 * code that rely on that. For now take safe approach and zero whole
2237 * tcpcb. This definitely can be optimized.
2239 bzero(&tp->t_start_zero, t_zero_size);
2241 /* Initialise cc_var struct for this tcpcb. */
2242 tp->t_ccv.type = IPPROTO_TCP;
2243 tp->t_ccv.ccvc.tcp = tp;
2244 rw_rlock(&tcp_function_lock);
2245 tp->t_fb = V_tcp_func_set_ptr;
2246 refcount_acquire(&tp->t_fb->tfb_refcnt);
2247 rw_runlock(&tcp_function_lock);
2249 * Use the current system default CC algorithm.
2251 cc_attach(tp, CC_DEFAULT_ALGO());
2253 if (CC_ALGO(tp)->cb_init != NULL)
2254 if (CC_ALGO(tp)->cb_init(&tp->t_ccv, NULL) > 0) {
2256 if (tp->t_fb->tfb_tcp_fb_fini)
2257 (*tp->t_fb->tfb_tcp_fb_fini)(tp, 1);
2258 refcount_release(&tp->t_fb->tfb_refcnt);
2263 if (khelp_init_osd(HELPER_CLASS_TCP, &tp->t_osd)) {
2264 if (tp->t_fb->tfb_tcp_fb_fini)
2265 (*tp->t_fb->tfb_tcp_fb_fini)(tp, 1);
2266 refcount_release(&tp->t_fb->tfb_refcnt);
2271 TAILQ_INIT(&tp->t_segq);
2272 STAILQ_INIT(&tp->t_inqueue);
2275 isipv6 ? V_tcp_v6mssdflt :
2279 /* All mbuf queue/ack compress flags should be off */
2280 tcp_lro_features_off(tp);
2282 callout_init_rw(&tp->t_callout, &inp->inp_lock, CALLOUT_RETURNUNLOCKED);
2283 for (int i = 0; i < TT_N; i++)
2284 tp->t_timers[i] = SBT_MAX;
2286 switch (V_tcp_do_rfc1323) {
2291 tp->t_flags = (TF_REQ_SCALE|TF_REQ_TSTMP);
2294 tp->t_flags = TF_REQ_SCALE;
2297 tp->t_flags = TF_REQ_TSTMP;
2301 tp->t_flags |= TF_SACK_PERMIT;
2302 TAILQ_INIT(&tp->snd_holes);
2305 * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no
2306 * rtt estimate. Set rttvar so that srtt + 4 * rttvar gives
2307 * reasonable initial retransmit time.
2309 tp->t_srtt = TCPTV_SRTTBASE;
2310 tp->t_rttvar = ((tcp_rexmit_initial - TCPTV_SRTTBASE) << TCP_RTTVAR_SHIFT) / 4;
2311 tp->t_rttmin = tcp_rexmit_min;
2312 tp->t_rxtcur = tcp_rexmit_initial;
2313 tp->snd_cwnd = TCP_MAXWIN << TCP_MAX_WINSHIFT;
2314 tp->snd_ssthresh = TCP_MAXWIN << TCP_MAX_WINSHIFT;
2315 tp->t_rcvtime = ticks;
2316 /* We always start with ticks granularity */
2317 tp->t_tmr_granularity = TCP_TMR_GRANULARITY_TICKS;
2319 * IPv4 TTL initialization is necessary for an IPv6 socket as well,
2320 * because the socket may be bound to an IPv6 wildcard address,
2321 * which may match an IPv4-mapped IPv6 address.
2323 inp->inp_ip_ttl = V_ip_defttl;
2329 * Init the TCP PCAP queues.
2331 tcp_pcap_tcpcb_init(tp);
2334 /* Initialize the per-TCPCB log data. */
2335 tcp_log_tcpcbinit(tp);
2337 tp->t_pacing_rate = -1;
2338 if (tp->t_fb->tfb_tcp_fb_init) {
2339 if ((*tp->t_fb->tfb_tcp_fb_init)(tp, &tp->t_fb_ptr)) {
2340 refcount_release(&tp->t_fb->tfb_refcnt);
2345 if (V_tcp_perconn_stats_enable == 1)
2346 tp->t_stats = stats_blob_alloc(V_tcp_perconn_stats_dflt_tpl, 0);
2349 tp->t_flags |= TF_LRD;
2355 * Drop a TCP connection, reporting
2356 * the specified error. If connection is synchronized,
2357 * then send a RST to peer.
2360 tcp_drop(struct tcpcb *tp, int errno)
2362 struct socket *so = tptosocket(tp);
2365 INP_WLOCK_ASSERT(tptoinpcb(tp));
2367 if (TCPS_HAVERCVDSYN(tp->t_state)) {
2368 tcp_state_change(tp, TCPS_CLOSED);
2369 /* Don't use tcp_output() here due to possible recursion. */
2370 (void)tcp_output_nodrop(tp);
2371 TCPSTAT_INC(tcps_drops);
2373 TCPSTAT_INC(tcps_conndrops);
2374 if (errno == ETIMEDOUT && tp->t_softerror)
2375 errno = tp->t_softerror;
2376 so->so_error = errno;
2377 return (tcp_close(tp));
2381 tcp_discardcb(struct tcpcb *tp)
2383 struct inpcb *inp = tptoinpcb(tp);
2384 struct socket *so = tptosocket(tp);
2387 bool isipv6 = (inp->inp_vflag & INP_IPV6) != 0;
2390 INP_WLOCK_ASSERT(inp);
2393 if (tp->t_fb->tfb_tcp_timer_stop_all) {
2394 tp->t_fb->tfb_tcp_timer_stop_all(tp);
2397 /* free the reassembly queue, if any */
2398 tcp_reass_flush(tp);
2401 /* Disconnect offload device, if any. */
2402 if (tp->t_flags & TF_TOE)
2403 tcp_offload_detach(tp);
2406 tcp_free_sackholes(tp);
2409 /* Free the TCP PCAP queues. */
2410 tcp_pcap_drain(&(tp->t_inpkts));
2411 tcp_pcap_drain(&(tp->t_outpkts));
2414 /* Allow the CC algorithm to clean up after itself. */
2415 if (CC_ALGO(tp)->cb_destroy != NULL)
2416 CC_ALGO(tp)->cb_destroy(&tp->t_ccv);
2418 /* Detach from the CC algorithm */
2422 khelp_destroy_osd(&tp->t_osd);
2425 stats_blob_destroy(tp->t_stats);
2429 if ((m = STAILQ_FIRST(&tp->t_inqueue)) != NULL) {
2432 STAILQ_INIT(&tp->t_inqueue);
2433 STAILQ_FOREACH_FROM_SAFE(m, &tp->t_inqueue, m_stailqpkt, prev)
2436 TCPSTATES_DEC(tp->t_state);
2438 if (tp->t_fb->tfb_tcp_fb_fini)
2439 (*tp->t_fb->tfb_tcp_fb_fini)(tp, 1);
2440 MPASS(!tcp_in_hpts(tp));
2442 tcp_log_tcpcbfini(tp);
2446 * If we got enough samples through the srtt filter,
2447 * save the rtt and rttvar in the routing entry.
2448 * 'Enough' is arbitrarily defined as 4 rtt samples.
2449 * 4 samples is enough for the srtt filter to converge
2450 * to within enough % of the correct value; fewer samples
2451 * and we could save a bogus rtt. The danger is not high
2452 * as tcp quickly recovers from everything.
2453 * XXX: Works very well but needs some more statistics!
2455 * XXXRRS: Updating must be after the stack fini() since
2456 * that may be converting some internal representation of
2457 * say srtt etc into the general one used by other stacks.
2458 * Lets also at least protect against the so being NULL
2459 * as RW stated below.
2461 if ((tp->t_rttupdated >= 4) && (so != NULL)) {
2462 struct hc_metrics_lite metrics;
2465 bzero(&metrics, sizeof(metrics));
2467 * Update the ssthresh always when the conditions below
2468 * are satisfied. This gives us better new start value
2469 * for the congestion avoidance for new connections.
2470 * ssthresh is only set if packet loss occurred on a session.
2472 * XXXRW: 'so' may be NULL here, and/or socket buffer may be
2473 * being torn down. Ideally this code would not use 'so'.
2475 ssthresh = tp->snd_ssthresh;
2476 if (ssthresh != 0 && ssthresh < so->so_snd.sb_hiwat / 2) {
2478 * convert the limit from user data bytes to
2479 * packets then to packet data bytes.
2481 ssthresh = (ssthresh + tp->t_maxseg / 2) / tp->t_maxseg;
2484 ssthresh *= (tp->t_maxseg +
2486 (isipv6 ? sizeof (struct ip6_hdr) +
2487 sizeof (struct tcphdr) :
2489 sizeof (struct tcpiphdr)
2496 metrics.rmx_ssthresh = ssthresh;
2498 metrics.rmx_rtt = tp->t_srtt;
2499 metrics.rmx_rttvar = tp->t_rttvar;
2500 metrics.rmx_cwnd = tp->snd_cwnd;
2501 metrics.rmx_sendpipe = 0;
2502 metrics.rmx_recvpipe = 0;
2504 tcp_hc_update(&inp->inp_inc, &metrics);
2507 refcount_release(&tp->t_fb->tfb_refcnt);
2511 * Attempt to close a TCP control block, marking it as dropped, and freeing
2512 * the socket if we hold the only reference.
2515 tcp_close(struct tcpcb *tp)
2517 struct inpcb *inp = tptoinpcb(tp);
2518 struct socket *so = tptosocket(tp);
2520 INP_WLOCK_ASSERT(inp);
2523 if (tp->t_state == TCPS_LISTEN)
2524 tcp_offload_listen_stop(tp);
2527 * This releases the TFO pending counter resource for TFO listen
2528 * sockets as well as passively-created TFO sockets that transition
2529 * from SYN_RECEIVED to CLOSED.
2531 if (tp->t_tfo_pending) {
2532 tcp_fastopen_decrement_counter(tp->t_tfo_pending);
2533 tp->t_tfo_pending = NULL;
2536 tcp_hpts_remove(tp);
2539 TCPSTAT_INC(tcps_closed);
2540 if (tp->t_state != TCPS_CLOSED)
2541 tcp_state_change(tp, TCPS_CLOSED);
2542 KASSERT(inp->inp_socket != NULL, ("tcp_close: inp_socket NULL"));
2543 soisdisconnected(so);
2544 if (inp->inp_flags & INP_SOCKREF) {
2545 inp->inp_flags &= ~INP_SOCKREF;
2554 * Notify a tcp user of an asynchronous error;
2555 * store error as soft error, but wake up user
2556 * (for now, won't do anything until can select for soft error).
2558 * Do not wake up user since there currently is no mechanism for
2559 * reporting soft errors (yet - a kqueue filter may be added).
2561 static struct inpcb *
2562 tcp_notify(struct inpcb *inp, int error)
2566 INP_WLOCK_ASSERT(inp);
2568 tp = intotcpcb(inp);
2569 KASSERT(tp != NULL, ("tcp_notify: tp == NULL"));
2572 * Ignore some errors if we are hooked up.
2573 * If connection hasn't completed, has retransmitted several times,
2574 * and receives a second error, give up now. This is better
2575 * than waiting a long time to establish a connection that
2576 * can never complete.
2578 if (tp->t_state == TCPS_ESTABLISHED &&
2579 (error == EHOSTUNREACH || error == ENETUNREACH ||
2580 error == EHOSTDOWN)) {
2581 if (inp->inp_route.ro_nh) {
2582 NH_FREE(inp->inp_route.ro_nh);
2583 inp->inp_route.ro_nh = (struct nhop_object *)NULL;
2586 } else if (tp->t_state < TCPS_ESTABLISHED && tp->t_rxtshift > 3 &&
2588 tp = tcp_drop(tp, error);
2594 tp->t_softerror = error;
2598 wakeup( &so->so_timeo);
2605 tcp_pcblist(SYSCTL_HANDLER_ARGS)
2607 struct inpcb_iterator inpi = INP_ALL_ITERATOR(&V_tcbinfo,
2608 INPLOOKUP_RLOCKPCB);
2613 if (req->newptr != NULL)
2616 if (req->oldptr == NULL) {
2619 n = V_tcbinfo.ipi_count +
2620 counter_u64_fetch(V_tcps_states[TCPS_SYN_RECEIVED]);
2621 n += imax(n / 8, 10);
2622 req->oldidx = 2 * (sizeof xig) + n * sizeof(struct xtcpcb);
2626 if ((error = sysctl_wire_old_buffer(req, 0)) != 0)
2629 bzero(&xig, sizeof(xig));
2630 xig.xig_len = sizeof xig;
2631 xig.xig_count = V_tcbinfo.ipi_count +
2632 counter_u64_fetch(V_tcps_states[TCPS_SYN_RECEIVED]);
2633 xig.xig_gen = V_tcbinfo.ipi_gencnt;
2634 xig.xig_sogen = so_gencnt;
2635 error = SYSCTL_OUT(req, &xig, sizeof xig);
2639 error = syncache_pcblist(req);
2643 while ((inp = inp_next(&inpi)) != NULL) {
2644 if (inp->inp_gencnt <= xig.xig_gen &&
2645 cr_canseeinpcb(req->td->td_ucred, inp) == 0) {
2648 tcp_inptoxtp(inp, &xt);
2649 error = SYSCTL_OUT(req, &xt, sizeof xt);
2660 * Give the user an updated idea of our state.
2661 * If the generation differs from what we told
2662 * her before, she knows that something happened
2663 * while we were processing this request, and it
2664 * might be necessary to retry.
2666 xig.xig_gen = V_tcbinfo.ipi_gencnt;
2667 xig.xig_sogen = so_gencnt;
2668 xig.xig_count = V_tcbinfo.ipi_count +
2669 counter_u64_fetch(V_tcps_states[TCPS_SYN_RECEIVED]);
2670 error = SYSCTL_OUT(req, &xig, sizeof xig);
2676 SYSCTL_PROC(_net_inet_tcp, TCPCTL_PCBLIST, pcblist,
2677 CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
2678 NULL, 0, tcp_pcblist, "S,xtcpcb",
2679 "List of active TCP connections");
2683 tcp_getcred(SYSCTL_HANDLER_ARGS)
2686 struct sockaddr_in addrs[2];
2687 struct epoch_tracker et;
2691 error = priv_check(req->td, PRIV_NETINET_GETCRED);
2694 error = SYSCTL_IN(req, addrs, sizeof(addrs));
2697 NET_EPOCH_ENTER(et);
2698 inp = in_pcblookup(&V_tcbinfo, addrs[1].sin_addr, addrs[1].sin_port,
2699 addrs[0].sin_addr, addrs[0].sin_port, INPLOOKUP_RLOCKPCB, NULL);
2703 error = cr_canseeinpcb(req->td->td_ucred, inp);
2705 cru2x(inp->inp_cred, &xuc);
2710 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
2714 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, getcred,
2715 CTLTYPE_OPAQUE | CTLFLAG_RW | CTLFLAG_PRISON | CTLFLAG_NEEDGIANT,
2716 0, 0, tcp_getcred, "S,xucred",
2717 "Get the xucred of a TCP connection");
2722 tcp6_getcred(SYSCTL_HANDLER_ARGS)
2724 struct epoch_tracker et;
2726 struct sockaddr_in6 addrs[2];
2733 error = priv_check(req->td, PRIV_NETINET_GETCRED);
2736 error = SYSCTL_IN(req, addrs, sizeof(addrs));
2739 if ((error = sa6_embedscope(&addrs[0], V_ip6_use_defzone)) != 0 ||
2740 (error = sa6_embedscope(&addrs[1], V_ip6_use_defzone)) != 0) {
2743 if (IN6_IS_ADDR_V4MAPPED(&addrs[0].sin6_addr)) {
2745 if (IN6_IS_ADDR_V4MAPPED(&addrs[1].sin6_addr))
2752 NET_EPOCH_ENTER(et);
2755 inp = in_pcblookup(&V_tcbinfo,
2756 *(struct in_addr *)&addrs[1].sin6_addr.s6_addr[12],
2758 *(struct in_addr *)&addrs[0].sin6_addr.s6_addr[12],
2759 addrs[0].sin6_port, INPLOOKUP_RLOCKPCB, NULL);
2762 inp = in6_pcblookup(&V_tcbinfo,
2763 &addrs[1].sin6_addr, addrs[1].sin6_port,
2764 &addrs[0].sin6_addr, addrs[0].sin6_port,
2765 INPLOOKUP_RLOCKPCB, NULL);
2769 error = cr_canseeinpcb(req->td->td_ucred, inp);
2771 cru2x(inp->inp_cred, &xuc);
2776 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
2780 SYSCTL_PROC(_net_inet6_tcp6, OID_AUTO, getcred,
2781 CTLTYPE_OPAQUE | CTLFLAG_RW | CTLFLAG_PRISON | CTLFLAG_NEEDGIANT,
2782 0, 0, tcp6_getcred, "S,xucred",
2783 "Get the xucred of a TCP6 connection");
2787 /* Path MTU to try next when a fragmentation-needed message is received. */
2789 tcp_next_pmtu(const struct icmp *icp, const struct ip *ip)
2791 int mtu = ntohs(icp->icmp_nextmtu);
2793 /* If no alternative MTU was proposed, try the next smaller one. */
2795 mtu = ip_next_mtu(ntohs(ip->ip_len), 1);
2796 if (mtu < V_tcp_minmss + sizeof(struct tcpiphdr))
2797 mtu = V_tcp_minmss + sizeof(struct tcpiphdr);
2803 tcp_ctlinput_with_port(struct icmp *icp, uint16_t port)
2809 struct inpcb *(*notify)(struct inpcb *, int);
2810 struct in_conninfo inc;
2811 tcp_seq icmp_tcp_seq;
2814 errno = icmp_errmap(icp);
2819 notify = tcp_mtudisc_notify;
2823 notify = tcp_drop_syn_sent;
2825 notify = tcp_notify;
2828 if (V_icmp_may_rst && icp->icmp_type == ICMP_TIMXCEED)
2829 notify = tcp_drop_syn_sent;
2831 notify = tcp_notify;
2834 notify = tcp_notify;
2838 th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
2839 icmp_tcp_seq = th->th_seq;
2840 inp = in_pcblookup(&V_tcbinfo, ip->ip_dst, th->th_dport, ip->ip_src,
2841 th->th_sport, INPLOOKUP_WLOCKPCB, NULL);
2843 tp = intotcpcb(inp);
2845 if (tp->t_flags & TF_TOE && errno == EMSGSIZE) {
2847 * MTU discovery for offloaded connections. Let
2848 * the TOE driver verify seq# and process it.
2850 mtu = tcp_next_pmtu(icp, ip);
2851 tcp_offload_pmtu_update(tp, icmp_tcp_seq, mtu);
2855 if (tp->t_port != port)
2857 if (SEQ_GEQ(ntohl(icmp_tcp_seq), tp->snd_una) &&
2858 SEQ_LT(ntohl(icmp_tcp_seq), tp->snd_max)) {
2859 if (errno == EMSGSIZE) {
2861 * MTU discovery: we got a needfrag and
2862 * will potentially try a lower MTU.
2864 mtu = tcp_next_pmtu(icp, ip);
2867 * Only process the offered MTU if it
2868 * is smaller than the current one.
2870 if (mtu < tp->t_maxseg +
2871 sizeof(struct tcpiphdr)) {
2872 bzero(&inc, sizeof(inc));
2873 inc.inc_faddr = ip->ip_dst;
2875 inp->inp_inc.inc_fibnum;
2876 tcp_hc_updatemtu(&inc, mtu);
2877 inp = tcp_mtudisc(inp, mtu);
2880 inp = (*notify)(inp, errno);
2883 bzero(&inc, sizeof(inc));
2884 inc.inc_fport = th->th_dport;
2885 inc.inc_lport = th->th_sport;
2886 inc.inc_faddr = ip->ip_dst;
2887 inc.inc_laddr = ip->ip_src;
2888 syncache_unreach(&inc, icmp_tcp_seq, port);
2896 tcp_ctlinput(struct icmp *icmp)
2898 tcp_ctlinput_with_port(icmp, htons(0));
2902 tcp_ctlinput_viaudp(udp_tun_icmp_param_t param)
2904 /* Its a tunneled TCP over UDP icmp */
2905 struct icmp *icmp = param.icmp;
2906 struct ip *outer_ip, *inner_ip;
2908 struct tcphdr *th, ttemp;
2912 outer_ip = (struct ip *)((caddr_t)icmp - sizeof(struct ip));
2913 inner_ip = &icmp->icmp_ip;
2914 i_hlen = inner_ip->ip_hl << 2;
2915 o_len = ntohs(outer_ip->ip_len);
2917 (sizeof(struct ip) + 8 + i_hlen + sizeof(struct udphdr) + offsetof(struct tcphdr, th_ack))) {
2918 /* Not enough data present */
2921 /* Ok lets strip out the inner udphdr header by copying up on top of it the tcp hdr */
2922 udp = (struct udphdr *)(((caddr_t)inner_ip) + i_hlen);
2923 if (ntohs(udp->uh_sport) != V_tcp_udp_tunneling_port) {
2926 port = udp->uh_dport;
2927 th = (struct tcphdr *)(udp + 1);
2928 memcpy(&ttemp, th, sizeof(struct tcphdr));
2929 memcpy(udp, &ttemp, sizeof(struct tcphdr));
2930 /* Now adjust down the size of the outer IP header */
2931 o_len -= sizeof(struct udphdr);
2932 outer_ip->ip_len = htons(o_len);
2933 /* Now call in to the normal handling code */
2934 tcp_ctlinput_with_port(icmp, port);
2940 tcp6_next_pmtu(const struct icmp6_hdr *icmp6)
2942 int mtu = ntohl(icmp6->icmp6_mtu);
2945 * If no alternative MTU was proposed, or the proposed MTU was too
2946 * small, set to the min.
2948 if (mtu < IPV6_MMTU)
2949 mtu = IPV6_MMTU - 8; /* XXXNP: what is the adjustment for? */
2954 tcp6_ctlinput_with_port(struct ip6ctlparam *ip6cp, uint16_t port)
2956 struct in6_addr *dst;
2957 struct inpcb *(*notify)(struct inpcb *, int);
2958 struct ip6_hdr *ip6;
2962 struct icmp6_hdr *icmp6;
2963 struct in_conninfo inc;
2968 tcp_seq icmp_tcp_seq;
2973 icmp6 = ip6cp->ip6c_icmp6;
2975 ip6 = ip6cp->ip6c_ip6;
2976 off = ip6cp->ip6c_off;
2977 dst = &ip6cp->ip6c_finaldst->sin6_addr;
2979 errno = icmp6_errmap(icmp6);
2984 notify = tcp_mtudisc_notify;
2988 notify = tcp_drop_syn_sent;
2990 notify = tcp_notify;
2994 * There are only four ICMPs that may reset connection:
2995 * - administratively prohibited
2996 * - port unreachable
2997 * - time exceeded in transit
2998 * - unknown next header
3000 if (V_icmp_may_rst &&
3001 ((icmp6->icmp6_type == ICMP6_DST_UNREACH &&
3002 (icmp6->icmp6_code == ICMP6_DST_UNREACH_ADMIN ||
3003 icmp6->icmp6_code == ICMP6_DST_UNREACH_NOPORT)) ||
3004 (icmp6->icmp6_type == ICMP6_TIME_EXCEEDED &&
3005 icmp6->icmp6_code == ICMP6_TIME_EXCEED_TRANSIT) ||
3006 (icmp6->icmp6_type == ICMP6_PARAM_PROB &&
3007 icmp6->icmp6_code == ICMP6_PARAMPROB_NEXTHEADER)))
3008 notify = tcp_drop_syn_sent;
3010 notify = tcp_notify;
3013 notify = tcp_notify;
3016 /* Check if we can safely get the ports from the tcp hdr */
3019 (int32_t) (off + sizeof(struct tcp_ports)))) {
3022 bzero(&t_ports, sizeof(struct tcp_ports));
3023 m_copydata(m, off, sizeof(struct tcp_ports), (caddr_t)&t_ports);
3024 inp = in6_pcblookup(&V_tcbinfo, &ip6->ip6_dst, t_ports.th_dport,
3025 &ip6->ip6_src, t_ports.th_sport, INPLOOKUP_WLOCKPCB, NULL);
3026 off += sizeof(struct tcp_ports);
3027 if (m->m_pkthdr.len < (int32_t) (off + sizeof(tcp_seq))) {
3030 m_copydata(m, off, sizeof(tcp_seq), (caddr_t)&icmp_tcp_seq);
3032 tp = intotcpcb(inp);
3034 if (tp->t_flags & TF_TOE && errno == EMSGSIZE) {
3035 /* MTU discovery for offloaded connections. */
3036 mtu = tcp6_next_pmtu(icmp6);
3037 tcp_offload_pmtu_update(tp, icmp_tcp_seq, mtu);
3041 if (tp->t_port != port)
3043 if (SEQ_GEQ(ntohl(icmp_tcp_seq), tp->snd_una) &&
3044 SEQ_LT(ntohl(icmp_tcp_seq), tp->snd_max)) {
3045 if (errno == EMSGSIZE) {
3048 * If we got a needfrag set the MTU
3049 * in the route to the suggested new
3050 * value (if given) and then notify.
3052 mtu = tcp6_next_pmtu(icmp6);
3054 bzero(&inc, sizeof(inc));
3055 inc.inc_fibnum = M_GETFIB(m);
3056 inc.inc_flags |= INC_ISIPV6;
3057 inc.inc6_faddr = *dst;
3058 if (in6_setscope(&inc.inc6_faddr,
3059 m->m_pkthdr.rcvif, NULL))
3062 * Only process the offered MTU if it
3063 * is smaller than the current one.
3065 if (mtu < tp->t_maxseg +
3066 sizeof (struct tcphdr) +
3067 sizeof (struct ip6_hdr)) {
3068 tcp_hc_updatemtu(&inc, mtu);
3069 tcp_mtudisc(inp, mtu);
3070 ICMP6STAT_INC(icp6s_pmtuchg);
3073 inp = (*notify)(inp, errno);
3076 bzero(&inc, sizeof(inc));
3077 inc.inc_fibnum = M_GETFIB(m);
3078 inc.inc_flags |= INC_ISIPV6;
3079 inc.inc_fport = t_ports.th_dport;
3080 inc.inc_lport = t_ports.th_sport;
3081 inc.inc6_faddr = *dst;
3082 inc.inc6_laddr = ip6->ip6_src;
3083 syncache_unreach(&inc, icmp_tcp_seq, port);
3091 tcp6_ctlinput(struct ip6ctlparam *ctl)
3093 tcp6_ctlinput_with_port(ctl, htons(0));
3097 tcp6_ctlinput_viaudp(udp_tun_icmp_param_t param)
3099 struct ip6ctlparam *ip6cp = param.ip6cp;
3104 m = m_pulldown(ip6cp->ip6c_m, ip6cp->ip6c_off, sizeof(struct udphdr), NULL);
3108 udp = mtod(m, struct udphdr *);
3109 if (ntohs(udp->uh_sport) != V_tcp_udp_tunneling_port) {
3112 port = udp->uh_dport;
3113 m_adj(m, sizeof(struct udphdr));
3114 if ((m->m_flags & M_PKTHDR) == 0) {
3115 ip6cp->ip6c_m->m_pkthdr.len -= sizeof(struct udphdr);
3117 /* Now call in to the normal handling code */
3118 tcp6_ctlinput_with_port(ip6cp, port);
3124 tcp_keyed_hash(struct in_conninfo *inc, u_char *key, u_int len)
3129 KASSERT(len >= SIPHASH_KEY_LENGTH,
3130 ("%s: keylen %u too short ", __func__, len));
3131 SipHash24_Init(&ctx);
3132 SipHash_SetKey(&ctx, (uint8_t *)key);
3133 SipHash_Update(&ctx, &inc->inc_fport, sizeof(uint16_t));
3134 SipHash_Update(&ctx, &inc->inc_lport, sizeof(uint16_t));
3135 switch (inc->inc_flags & INC_ISIPV6) {
3138 SipHash_Update(&ctx, &inc->inc_faddr, sizeof(struct in_addr));
3139 SipHash_Update(&ctx, &inc->inc_laddr, sizeof(struct in_addr));
3144 SipHash_Update(&ctx, &inc->inc6_faddr, sizeof(struct in6_addr));
3145 SipHash_Update(&ctx, &inc->inc6_laddr, sizeof(struct in6_addr));
3149 SipHash_Final((uint8_t *)hash, &ctx);
3151 return (hash[0] ^ hash[1]);
3155 tcp_new_ts_offset(struct in_conninfo *inc)
3157 struct in_conninfo inc_store, *local_inc;
3159 if (!V_tcp_ts_offset_per_conn) {
3160 memcpy(&inc_store, inc, sizeof(struct in_conninfo));
3161 inc_store.inc_lport = 0;
3162 inc_store.inc_fport = 0;
3163 local_inc = &inc_store;
3167 return (tcp_keyed_hash(local_inc, V_ts_offset_secret,
3168 sizeof(V_ts_offset_secret)));
3172 * Following is where TCP initial sequence number generation occurs.
3174 * There are two places where we must use initial sequence numbers:
3175 * 1. In SYN-ACK packets.
3176 * 2. In SYN packets.
3178 * All ISNs for SYN-ACK packets are generated by the syncache. See
3179 * tcp_syncache.c for details.
3181 * The ISNs in SYN packets must be monotonic; TIME_WAIT recycling
3182 * depends on this property. In addition, these ISNs should be
3183 * unguessable so as to prevent connection hijacking. To satisfy
3184 * the requirements of this situation, the algorithm outlined in
3185 * RFC 1948 is used, with only small modifications.
3187 * Implementation details:
3189 * Time is based off the system timer, and is corrected so that it
3190 * increases by one megabyte per second. This allows for proper
3191 * recycling on high speed LANs while still leaving over an hour
3194 * As reading the *exact* system time is too expensive to be done
3195 * whenever setting up a TCP connection, we increment the time
3196 * offset in two ways. First, a small random positive increment
3197 * is added to isn_offset for each connection that is set up.
3198 * Second, the function tcp_isn_tick fires once per clock tick
3199 * and increments isn_offset as necessary so that sequence numbers
3200 * are incremented at approximately ISN_BYTES_PER_SECOND. The
3201 * random positive increments serve only to ensure that the same
3202 * exact sequence number is never sent out twice (as could otherwise
3203 * happen when a port is recycled in less than the system tick
3206 * net.inet.tcp.isn_reseed_interval controls the number of seconds
3207 * between seeding of isn_secret. This is normally set to zero,
3208 * as reseeding should not be necessary.
3210 * Locking of the global variables isn_secret, isn_last_reseed, isn_offset,
3211 * isn_offset_old, and isn_ctx is performed using the ISN lock. In
3212 * general, this means holding an exclusive (write) lock.
3215 #define ISN_BYTES_PER_SECOND 1048576
3216 #define ISN_STATIC_INCREMENT 4096
3217 #define ISN_RANDOM_INCREMENT (4096 - 1)
3218 #define ISN_SECRET_LENGTH SIPHASH_KEY_LENGTH
3220 VNET_DEFINE_STATIC(u_char, isn_secret[ISN_SECRET_LENGTH]);
3221 VNET_DEFINE_STATIC(int, isn_last);
3222 VNET_DEFINE_STATIC(int, isn_last_reseed);
3223 VNET_DEFINE_STATIC(u_int32_t, isn_offset);
3224 VNET_DEFINE_STATIC(u_int32_t, isn_offset_old);
3226 #define V_isn_secret VNET(isn_secret)
3227 #define V_isn_last VNET(isn_last)
3228 #define V_isn_last_reseed VNET(isn_last_reseed)
3229 #define V_isn_offset VNET(isn_offset)
3230 #define V_isn_offset_old VNET(isn_offset_old)
3233 tcp_new_isn(struct in_conninfo *inc)
3236 u_int32_t projected_offset;
3239 /* Seed if this is the first use, reseed if requested. */
3240 if ((V_isn_last_reseed == 0) || ((V_tcp_isn_reseed_interval > 0) &&
3241 (((u_int)V_isn_last_reseed + (u_int)V_tcp_isn_reseed_interval*hz)
3243 arc4rand(&V_isn_secret, sizeof(V_isn_secret), 0);
3244 V_isn_last_reseed = ticks;
3247 /* Compute the hash and return the ISN. */
3248 new_isn = (tcp_seq)tcp_keyed_hash(inc, V_isn_secret,
3249 sizeof(V_isn_secret));
3250 V_isn_offset += ISN_STATIC_INCREMENT +
3251 (arc4random() & ISN_RANDOM_INCREMENT);
3252 if (ticks != V_isn_last) {
3253 projected_offset = V_isn_offset_old +
3254 ISN_BYTES_PER_SECOND / hz * (ticks - V_isn_last);
3255 if (SEQ_GT(projected_offset, V_isn_offset))
3256 V_isn_offset = projected_offset;
3257 V_isn_offset_old = V_isn_offset;
3260 new_isn += V_isn_offset;
3266 * When a specific ICMP unreachable message is received and the
3267 * connection state is SYN-SENT, drop the connection. This behavior
3268 * is controlled by the icmp_may_rst sysctl.
3270 static struct inpcb *
3271 tcp_drop_syn_sent(struct inpcb *inp, int errno)
3276 INP_WLOCK_ASSERT(inp);
3278 tp = intotcpcb(inp);
3279 if (tp->t_state != TCPS_SYN_SENT)
3282 if (IS_FASTOPEN(tp->t_flags))
3283 tcp_fastopen_disable_path(tp);
3285 tp = tcp_drop(tp, errno);
3293 * When `need fragmentation' ICMP is received, update our idea of the MSS
3294 * based on the new value. Also nudge TCP to send something, since we
3295 * know the packet we just sent was dropped.
3296 * This duplicates some code in the tcp_mss() function in tcp_input.c.
3298 static struct inpcb *
3299 tcp_mtudisc_notify(struct inpcb *inp, int error)
3302 return (tcp_mtudisc(inp, -1));
3305 static struct inpcb *
3306 tcp_mtudisc(struct inpcb *inp, int mtuoffer)
3311 INP_WLOCK_ASSERT(inp);
3313 tp = intotcpcb(inp);
3314 KASSERT(tp != NULL, ("tcp_mtudisc: tp == NULL"));
3316 tcp_mss_update(tp, -1, mtuoffer, NULL, NULL);
3318 so = inp->inp_socket;
3319 SOCKBUF_LOCK(&so->so_snd);
3320 /* If the mss is larger than the socket buffer, decrease the mss. */
3321 if (so->so_snd.sb_hiwat < tp->t_maxseg)
3322 tp->t_maxseg = so->so_snd.sb_hiwat;
3323 SOCKBUF_UNLOCK(&so->so_snd);
3325 TCPSTAT_INC(tcps_mturesent);
3327 tp->snd_nxt = tp->snd_una;
3328 tcp_free_sackholes(tp);
3329 tp->snd_recover = tp->snd_max;
3330 if (tp->t_flags & TF_SACK_PERMIT)
3331 EXIT_FASTRECOVERY(tp->t_flags);
3332 if (tp->t_fb->tfb_tcp_mtu_chg != NULL) {
3334 * Conceptually the snd_nxt setting
3335 * and freeing sack holes should
3336 * be done by the default stacks
3337 * own tfb_tcp_mtu_chg().
3339 tp->t_fb->tfb_tcp_mtu_chg(tp);
3341 if (tcp_output(tp) < 0)
3349 * Look-up the routing entry to the peer of this inpcb. If no route
3350 * is found and it cannot be allocated, then return 0. This routine
3351 * is called by TCP routines that access the rmx structure and by
3352 * tcp_mss_update to get the peer/interface MTU.
3355 tcp_maxmtu(struct in_conninfo *inc, struct tcp_ifcap *cap)
3357 struct nhop_object *nh;
3359 uint32_t maxmtu = 0;
3361 KASSERT(inc != NULL, ("tcp_maxmtu with NULL in_conninfo pointer"));
3363 if (inc->inc_faddr.s_addr != INADDR_ANY) {
3364 nh = fib4_lookup(inc->inc_fibnum, inc->inc_faddr, 0, NHR_NONE, 0);
3369 maxmtu = nh->nh_mtu;
3371 /* Report additional interface capabilities. */
3373 if (ifp->if_capenable & IFCAP_TSO4 &&
3374 ifp->if_hwassist & CSUM_TSO) {
3375 cap->ifcap |= CSUM_TSO;
3376 cap->tsomax = ifp->if_hw_tsomax;
3377 cap->tsomaxsegcount = ifp->if_hw_tsomaxsegcount;
3378 cap->tsomaxsegsize = ifp->if_hw_tsomaxsegsize;
3388 tcp_maxmtu6(struct in_conninfo *inc, struct tcp_ifcap *cap)
3390 struct nhop_object *nh;
3391 struct in6_addr dst6;
3394 uint32_t maxmtu = 0;
3396 KASSERT(inc != NULL, ("tcp_maxmtu6 with NULL in_conninfo pointer"));
3398 if (inc->inc_flags & INC_IPV6MINMTU)
3401 if (!IN6_IS_ADDR_UNSPECIFIED(&inc->inc6_faddr)) {
3402 in6_splitscope(&inc->inc6_faddr, &dst6, &scopeid);
3403 nh = fib6_lookup(inc->inc_fibnum, &dst6, scopeid, NHR_NONE, 0);
3408 maxmtu = nh->nh_mtu;
3410 /* Report additional interface capabilities. */
3412 if (ifp->if_capenable & IFCAP_TSO6 &&
3413 ifp->if_hwassist & CSUM_TSO) {
3414 cap->ifcap |= CSUM_TSO;
3415 cap->tsomax = ifp->if_hw_tsomax;
3416 cap->tsomaxsegcount = ifp->if_hw_tsomaxsegcount;
3417 cap->tsomaxsegsize = ifp->if_hw_tsomaxsegsize;
3426 * Handle setsockopt(IPV6_USE_MIN_MTU) by a TCP stack.
3428 * XXXGL: we are updating inpcb here with INC_IPV6MINMTU flag.
3429 * The right place to do that is ip6_setpktopt() that has just been
3430 * executed. By the way it just filled ip6po_minmtu for us.
3433 tcp6_use_min_mtu(struct tcpcb *tp)
3435 struct inpcb *inp = tptoinpcb(tp);
3437 INP_WLOCK_ASSERT(inp);
3439 * In case of the IPV6_USE_MIN_MTU socket
3440 * option, the INC_IPV6MINMTU flag to announce
3441 * a corresponding MSS during the initial
3442 * handshake. If the TCP connection is not in
3443 * the front states, just reduce the MSS being
3444 * used. This avoids the sending of TCP
3445 * segments which will be fragmented at the
3448 inp->inp_inc.inc_flags |= INC_IPV6MINMTU;
3449 if ((tp->t_state >= TCPS_SYN_SENT) &&
3450 (inp->inp_inc.inc_flags & INC_ISIPV6)) {
3451 struct ip6_pktopts *opt;
3453 opt = inp->in6p_outputopts;
3454 if (opt != NULL && opt->ip6po_minmtu == IP6PO_MINMTU_ALL &&
3455 tp->t_maxseg > TCP6_MSS)
3456 tp->t_maxseg = TCP6_MSS;
3462 * Calculate effective SMSS per RFC5681 definition for a given TCP
3463 * connection at its current state, taking into account SACK and etc.
3466 tcp_maxseg(const struct tcpcb *tp)
3470 if (tp->t_flags & TF_NOOPT)
3471 return (tp->t_maxseg);
3474 * Here we have a simplified code from tcp_addoptions(),
3475 * without a proper loop, and having most of paddings hardcoded.
3476 * We might make mistakes with padding here in some edge cases,
3477 * but this is harmless, since result of tcp_maxseg() is used
3478 * only in cwnd and ssthresh estimations.
3480 if (TCPS_HAVEESTABLISHED(tp->t_state)) {
3481 if (tp->t_flags & TF_RCVD_TSTMP)
3482 optlen = TCPOLEN_TSTAMP_APPA;
3485 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
3486 if (tp->t_flags & TF_SIGNATURE)
3487 optlen += PADTCPOLEN(TCPOLEN_SIGNATURE);
3489 if ((tp->t_flags & TF_SACK_PERMIT) && tp->rcv_numsacks > 0) {
3490 optlen += TCPOLEN_SACKHDR;
3491 optlen += tp->rcv_numsacks * TCPOLEN_SACK;
3492 optlen = PADTCPOLEN(optlen);
3495 if (tp->t_flags & TF_REQ_TSTMP)
3496 optlen = TCPOLEN_TSTAMP_APPA;
3498 optlen = PADTCPOLEN(TCPOLEN_MAXSEG);
3499 if (tp->t_flags & TF_REQ_SCALE)
3500 optlen += PADTCPOLEN(TCPOLEN_WINDOW);
3501 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
3502 if (tp->t_flags & TF_SIGNATURE)
3503 optlen += PADTCPOLEN(TCPOLEN_SIGNATURE);
3505 if (tp->t_flags & TF_SACK_PERMIT)
3506 optlen += PADTCPOLEN(TCPOLEN_SACK_PERMITTED);
3509 optlen = min(optlen, TCP_MAXOLEN);
3510 return (tp->t_maxseg - optlen);
3515 tcp_fixed_maxseg(const struct tcpcb *tp)
3519 if (tp->t_flags & TF_NOOPT)
3520 return (tp->t_maxseg);
3523 * Here we have a simplified code from tcp_addoptions(),
3524 * without a proper loop, and having most of paddings hardcoded.
3525 * We only consider fixed options that we would send every
3526 * time I.e. SACK is not considered. This is important
3527 * for cc modules to figure out what the modulo of the
3530 #define PAD(len) ((((len) / 4) + !!((len) % 4)) * 4)
3531 if (TCPS_HAVEESTABLISHED(tp->t_state)) {
3532 if (tp->t_flags & TF_RCVD_TSTMP)
3533 optlen = TCPOLEN_TSTAMP_APPA;
3536 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
3537 if (tp->t_flags & TF_SIGNATURE)
3538 optlen += PAD(TCPOLEN_SIGNATURE);
3541 if (tp->t_flags & TF_REQ_TSTMP)
3542 optlen = TCPOLEN_TSTAMP_APPA;
3544 optlen = PAD(TCPOLEN_MAXSEG);
3545 if (tp->t_flags & TF_REQ_SCALE)
3546 optlen += PAD(TCPOLEN_WINDOW);
3547 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
3548 if (tp->t_flags & TF_SIGNATURE)
3549 optlen += PAD(TCPOLEN_SIGNATURE);
3551 if (tp->t_flags & TF_SACK_PERMIT)
3552 optlen += PAD(TCPOLEN_SACK_PERMITTED);
3555 optlen = min(optlen, TCP_MAXOLEN);
3556 return (tp->t_maxseg - optlen);
3562 sysctl_drop(SYSCTL_HANDLER_ARGS)
3564 /* addrs[0] is a foreign socket, addrs[1] is a local one. */
3565 struct sockaddr_storage addrs[2];
3569 struct sockaddr_in *fin = NULL, *lin = NULL;
3571 struct epoch_tracker et;
3573 struct sockaddr_in6 *fin6, *lin6;
3583 if (req->oldptr != NULL || req->oldlen != 0)
3585 if (req->newptr == NULL)
3587 if (req->newlen < sizeof(addrs))
3589 error = SYSCTL_IN(req, &addrs, sizeof(addrs));
3593 switch (addrs[0].ss_family) {
3596 fin6 = (struct sockaddr_in6 *)&addrs[0];
3597 lin6 = (struct sockaddr_in6 *)&addrs[1];
3598 if (fin6->sin6_len != sizeof(struct sockaddr_in6) ||
3599 lin6->sin6_len != sizeof(struct sockaddr_in6))
3601 if (IN6_IS_ADDR_V4MAPPED(&fin6->sin6_addr)) {
3602 if (!IN6_IS_ADDR_V4MAPPED(&lin6->sin6_addr))
3604 in6_sin6_2_sin_in_sock((struct sockaddr *)&addrs[0]);
3605 in6_sin6_2_sin_in_sock((struct sockaddr *)&addrs[1]);
3607 fin = (struct sockaddr_in *)&addrs[0];
3608 lin = (struct sockaddr_in *)&addrs[1];
3612 error = sa6_embedscope(fin6, V_ip6_use_defzone);
3615 error = sa6_embedscope(lin6, V_ip6_use_defzone);
3622 fin = (struct sockaddr_in *)&addrs[0];
3623 lin = (struct sockaddr_in *)&addrs[1];
3624 if (fin->sin_len != sizeof(struct sockaddr_in) ||
3625 lin->sin_len != sizeof(struct sockaddr_in))
3632 NET_EPOCH_ENTER(et);
3633 switch (addrs[0].ss_family) {
3636 inp = in6_pcblookup(&V_tcbinfo, &fin6->sin6_addr,
3637 fin6->sin6_port, &lin6->sin6_addr, lin6->sin6_port,
3638 INPLOOKUP_WLOCKPCB, NULL);
3643 inp = in_pcblookup(&V_tcbinfo, fin->sin_addr, fin->sin_port,
3644 lin->sin_addr, lin->sin_port, INPLOOKUP_WLOCKPCB, NULL);
3649 if (!SOLISTENING(inp->inp_socket)) {
3650 tp = intotcpcb(inp);
3651 tp = tcp_drop(tp, ECONNABORTED);
3662 SYSCTL_PROC(_net_inet_tcp, TCPCTL_DROP, drop,
3663 CTLFLAG_VNET | CTLTYPE_STRUCT | CTLFLAG_WR | CTLFLAG_SKIP |
3664 CTLFLAG_NEEDGIANT, NULL, 0, sysctl_drop, "",
3665 "Drop TCP connection");
3668 tcp_sysctl_setsockopt(SYSCTL_HANDLER_ARGS)
3670 return (sysctl_setsockopt(oidp, arg1, arg2, req, &V_tcbinfo,
3671 &tcp_ctloutput_set));
3674 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, setsockopt,
3675 CTLFLAG_VNET | CTLTYPE_STRUCT | CTLFLAG_WR | CTLFLAG_SKIP |
3676 CTLFLAG_MPSAFE, NULL, 0, tcp_sysctl_setsockopt, "",
3677 "Set socket option for TCP endpoint");
3681 sysctl_switch_tls(SYSCTL_HANDLER_ARGS)
3683 /* addrs[0] is a foreign socket, addrs[1] is a local one. */
3684 struct sockaddr_storage addrs[2];
3687 struct sockaddr_in *fin = NULL, *lin = NULL;
3689 struct epoch_tracker et;
3691 struct sockaddr_in6 *fin6, *lin6;
3701 if (req->oldptr != NULL || req->oldlen != 0)
3703 if (req->newptr == NULL)
3705 if (req->newlen < sizeof(addrs))
3707 error = SYSCTL_IN(req, &addrs, sizeof(addrs));
3711 switch (addrs[0].ss_family) {
3714 fin6 = (struct sockaddr_in6 *)&addrs[0];
3715 lin6 = (struct sockaddr_in6 *)&addrs[1];
3716 if (fin6->sin6_len != sizeof(struct sockaddr_in6) ||
3717 lin6->sin6_len != sizeof(struct sockaddr_in6))
3719 if (IN6_IS_ADDR_V4MAPPED(&fin6->sin6_addr)) {
3720 if (!IN6_IS_ADDR_V4MAPPED(&lin6->sin6_addr))
3722 in6_sin6_2_sin_in_sock((struct sockaddr *)&addrs[0]);
3723 in6_sin6_2_sin_in_sock((struct sockaddr *)&addrs[1]);
3725 fin = (struct sockaddr_in *)&addrs[0];
3726 lin = (struct sockaddr_in *)&addrs[1];
3730 error = sa6_embedscope(fin6, V_ip6_use_defzone);
3733 error = sa6_embedscope(lin6, V_ip6_use_defzone);
3740 fin = (struct sockaddr_in *)&addrs[0];
3741 lin = (struct sockaddr_in *)&addrs[1];
3742 if (fin->sin_len != sizeof(struct sockaddr_in) ||
3743 lin->sin_len != sizeof(struct sockaddr_in))
3750 NET_EPOCH_ENTER(et);
3751 switch (addrs[0].ss_family) {
3754 inp = in6_pcblookup(&V_tcbinfo, &fin6->sin6_addr,
3755 fin6->sin6_port, &lin6->sin6_addr, lin6->sin6_port,
3756 INPLOOKUP_WLOCKPCB, NULL);
3761 inp = in_pcblookup(&V_tcbinfo, fin->sin_addr, fin->sin_port,
3762 lin->sin_addr, lin->sin_port, INPLOOKUP_WLOCKPCB, NULL);
3770 so = inp->inp_socket;
3772 error = ktls_set_tx_mode(so,
3773 arg2 == 0 ? TCP_TLS_MODE_SW : TCP_TLS_MODE_IFNET);
3781 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, switch_to_sw_tls,
3782 CTLFLAG_VNET | CTLTYPE_STRUCT | CTLFLAG_WR | CTLFLAG_SKIP |
3783 CTLFLAG_NEEDGIANT, NULL, 0, sysctl_switch_tls, "",
3784 "Switch TCP connection to SW TLS");
3785 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, switch_to_ifnet_tls,
3786 CTLFLAG_VNET | CTLTYPE_STRUCT | CTLFLAG_WR | CTLFLAG_SKIP |
3787 CTLFLAG_NEEDGIANT, NULL, 1, sysctl_switch_tls, "",
3788 "Switch TCP connection to ifnet TLS");
3792 * Generate a standardized TCP log line for use throughout the
3793 * tcp subsystem. Memory allocation is done with M_NOWAIT to
3794 * allow use in the interrupt context.
3796 * NB: The caller MUST free(s, M_TCPLOG) the returned string.
3797 * NB: The function may return NULL if memory allocation failed.
3799 * Due to header inclusion and ordering limitations the struct ip
3800 * and ip6_hdr pointers have to be passed as void pointers.
3803 tcp_log_vain(struct in_conninfo *inc, struct tcphdr *th, const void *ip4hdr,
3807 /* Is logging enabled? */
3808 if (V_tcp_log_in_vain == 0)
3811 return (tcp_log_addr(inc, th, ip4hdr, ip6hdr));
3815 tcp_log_addrs(struct in_conninfo *inc, struct tcphdr *th, const void *ip4hdr,
3819 /* Is logging enabled? */
3820 if (tcp_log_debug == 0)
3823 return (tcp_log_addr(inc, th, ip4hdr, ip6hdr));
3827 tcp_log_addr(struct in_conninfo *inc, struct tcphdr *th, const void *ip4hdr,
3833 const struct ip *ip = (const struct ip *)ip4hdr;
3836 const struct ip6_hdr *ip6 = (const struct ip6_hdr *)ip6hdr;
3840 * The log line looks like this:
3841 * "TCP: [1.2.3.4]:50332 to [1.2.3.4]:80 tcpflags 0x2<SYN>"
3843 size = sizeof("TCP: []:12345 to []:12345 tcpflags 0x2<>") +
3844 sizeof(PRINT_TH_FLAGS) + 1 +
3846 2 * INET6_ADDRSTRLEN;
3848 2 * INET_ADDRSTRLEN;
3851 s = malloc(size, M_TCPLOG, M_ZERO|M_NOWAIT);
3855 strcat(s, "TCP: [");
3858 if (inc && ((inc->inc_flags & INC_ISIPV6) == 0)) {
3859 inet_ntoa_r(inc->inc_faddr, sp);
3861 sprintf(sp, "]:%i to [", ntohs(inc->inc_fport));
3863 inet_ntoa_r(inc->inc_laddr, sp);
3865 sprintf(sp, "]:%i", ntohs(inc->inc_lport));
3868 ip6_sprintf(sp, &inc->inc6_faddr);
3870 sprintf(sp, "]:%i to [", ntohs(inc->inc_fport));
3872 ip6_sprintf(sp, &inc->inc6_laddr);
3874 sprintf(sp, "]:%i", ntohs(inc->inc_lport));
3875 } else if (ip6 && th) {
3876 ip6_sprintf(sp, &ip6->ip6_src);
3878 sprintf(sp, "]:%i to [", ntohs(th->th_sport));
3880 ip6_sprintf(sp, &ip6->ip6_dst);
3882 sprintf(sp, "]:%i", ntohs(th->th_dport));
3885 } else if (ip && th) {
3886 inet_ntoa_r(ip->ip_src, sp);
3888 sprintf(sp, "]:%i to [", ntohs(th->th_sport));
3890 inet_ntoa_r(ip->ip_dst, sp);
3892 sprintf(sp, "]:%i", ntohs(th->th_dport));
3900 sprintf(sp, " tcpflags 0x%b", tcp_get_flags(th), PRINT_TH_FLAGS);
3901 if (*(s + size - 1) != '\0')
3902 panic("%s: string too long", __func__);
3907 * A subroutine which makes it easy to track TCP state changes with DTrace.
3908 * This function shouldn't be called for t_state initializations that don't
3909 * correspond to actual TCP state transitions.
3912 tcp_state_change(struct tcpcb *tp, int newstate)
3914 #if defined(KDTRACE_HOOKS)
3915 int pstate = tp->t_state;
3918 TCPSTATES_DEC(tp->t_state);
3919 TCPSTATES_INC(newstate);
3920 tp->t_state = newstate;
3921 TCP_PROBE6(state__change, NULL, tp, NULL, tp, NULL, pstate);
3925 * Create an external-format (``xtcpcb'') structure using the information in
3926 * the kernel-format tcpcb structure pointed to by tp. This is done to
3927 * reduce the spew of irrelevant information over this interface, to isolate
3928 * user code from changes in the kernel structure, and potentially to provide
3929 * information-hiding if we decide that some of this information should be
3930 * hidden from users.
3933 tcp_inptoxtp(const struct inpcb *inp, struct xtcpcb *xt)
3935 struct tcpcb *tp = intotcpcb(inp);
3938 bzero(xt, sizeof(*xt));
3939 xt->t_state = tp->t_state;
3940 xt->t_logstate = tcp_get_bblog_state(tp);
3941 xt->t_flags = tp->t_flags;
3942 xt->t_sndzerowin = tp->t_sndzerowin;
3943 xt->t_sndrexmitpack = tp->t_sndrexmitpack;
3944 xt->t_rcvoopack = tp->t_rcvoopack;
3945 xt->t_rcv_wnd = tp->rcv_wnd;
3946 xt->t_snd_wnd = tp->snd_wnd;
3947 xt->t_snd_cwnd = tp->snd_cwnd;
3948 xt->t_snd_ssthresh = tp->snd_ssthresh;
3949 xt->t_dsack_bytes = tp->t_dsack_bytes;
3950 xt->t_dsack_tlp_bytes = tp->t_dsack_tlp_bytes;
3951 xt->t_dsack_pack = tp->t_dsack_pack;
3952 xt->t_maxseg = tp->t_maxseg;
3953 xt->xt_ecn = (tp->t_flags2 & TF2_ECN_PERMIT) ? 1 : 0 +
3954 (tp->t_flags2 & TF2_ACE_PERMIT) ? 2 : 0;
3956 now = getsbinuptime();
3957 #define COPYTIMER(which,where) do { \
3958 if (tp->t_timers[which] != SBT_MAX) \
3959 xt->where = (tp->t_timers[which] - now) / SBT_1MS; \
3963 COPYTIMER(TT_DELACK, tt_delack);
3964 COPYTIMER(TT_REXMT, tt_rexmt);
3965 COPYTIMER(TT_PERSIST, tt_persist);
3966 COPYTIMER(TT_KEEP, tt_keep);
3967 COPYTIMER(TT_2MSL, tt_2msl);
3969 xt->t_rcvtime = 1000 * (ticks - tp->t_rcvtime) / hz;
3971 xt->xt_encaps_port = tp->t_port;
3972 bcopy(tp->t_fb->tfb_tcp_block_name, xt->xt_stack,
3973 TCP_FUNCTION_NAME_LEN_MAX);
3974 bcopy(CC_ALGO(tp)->name, xt->xt_cc, TCP_CA_NAME_MAX);
3976 (void)tcp_log_get_id(tp, xt->xt_logid);
3979 xt->xt_len = sizeof(struct xtcpcb);
3980 in_pcbtoxinpcb(inp, &xt->xt_inp);
3982 * TCP doesn't use inp_ppcb pointer, we embed inpcb into tcpcb.
3983 * Fixup the pointer that in_pcbtoxinpcb() has set. When printing
3984 * TCP netstat(1) used to use this pointer, so this fixup needs to
3985 * stay for stable/14.
3987 xt->xt_inp.inp_ppcb = (uintptr_t)tp;
3991 tcp_log_end_status(struct tcpcb *tp, uint8_t status)
3996 (status > TCP_EI_STATUS_MAX_VALUE) ||
4001 if (status > (sizeof(uint32_t) * 8)) {
4002 /* Should this be a KASSERT? */
4005 bit = 1U << (status - 1);
4006 if (bit & tp->t_end_info_status) {
4007 /* already logged */
4010 for (i = 0; i < TCP_END_BYTE_INFO; i++) {
4011 if (tp->t_end_info_bytes[i] == TCP_EI_EMPTY_SLOT) {
4012 tp->t_end_info_bytes[i] = status;
4013 tp->t_end_info_status |= bit;
4020 tcp_can_enable_pacing(void)
4023 if ((tcp_pacing_limit == -1) ||
4024 (tcp_pacing_limit > number_of_tcp_connections_pacing)) {
4025 atomic_fetchadd_int(&number_of_tcp_connections_pacing, 1);
4026 shadow_num_connections = number_of_tcp_connections_pacing;
4029 counter_u64_add(tcp_pacing_failures, 1);
4034 static uint8_t tcp_pacing_warning = 0;
4037 tcp_decrement_paced_conn(void)
4041 ret = atomic_fetchadd_int(&number_of_tcp_connections_pacing, -1);
4042 shadow_num_connections = number_of_tcp_connections_pacing;
4043 KASSERT(ret != 0, ("tcp_paced_connection_exits -1 would cause wrap?"));
4045 if (tcp_pacing_limit != -1) {
4046 printf("Warning all pacing is now disabled, count decrements invalidly!\n");
4047 tcp_pacing_limit = 0;
4048 } else if (tcp_pacing_warning == 0) {
4049 printf("Warning pacing count is invalid, invalid decrement\n");
4050 tcp_pacing_warning = 1;
4056 tcp_default_switch_failed(struct tcpcb *tp)
4059 * If a switch fails we only need to
4060 * care about two things:
4063 * b) The timer granularity.
4064 * Timeouts, at least for now, don't use the
4065 * old callout system in the other stacks so
4066 * those are hopefully safe.
4068 tcp_lro_features_off(tp);
4069 tcp_change_time_units(tp, TCP_TMR_GRANULARITY_TICKS);
4072 #ifdef TCP_ACCOUNTING
4074 tcp_do_ack_accounting(struct tcpcb *tp, struct tcphdr *th, struct tcpopt *to, uint32_t tiwin, int mss)
4076 if (SEQ_LT(th->th_ack, tp->snd_una)) {
4077 /* Do we have a SACK? */
4078 if (to->to_flags & TOF_SACK) {
4079 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
4080 tp->tcp_cnt_counters[ACK_SACK]++;
4084 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
4085 tp->tcp_cnt_counters[ACK_BEHIND]++;
4087 return (ACK_BEHIND);
4089 } else if (th->th_ack == tp->snd_una) {
4090 /* Do we have a SACK? */
4091 if (to->to_flags & TOF_SACK) {
4092 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
4093 tp->tcp_cnt_counters[ACK_SACK]++;
4096 } else if (tiwin != tp->snd_wnd) {
4097 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
4098 tp->tcp_cnt_counters[ACK_RWND]++;
4102 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
4103 tp->tcp_cnt_counters[ACK_DUPACK]++;
4105 return (ACK_DUPACK);
4108 if (!SEQ_GT(th->th_ack, tp->snd_max)) {
4109 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
4110 tp->tcp_cnt_counters[CNT_OF_ACKS_IN] += (((th->th_ack - tp->snd_una) + mss - 1)/mss);
4113 if (to->to_flags & TOF_SACK) {
4114 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
4115 tp->tcp_cnt_counters[ACK_CUMACK_SACK]++;
4117 return (ACK_CUMACK_SACK);
4119 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
4120 tp->tcp_cnt_counters[ACK_CUMACK]++;
4122 return (ACK_CUMACK);
4129 tcp_change_time_units(struct tcpcb *tp, int granularity)
4131 if (tp->t_tmr_granularity == granularity) {
4135 if (granularity == TCP_TMR_GRANULARITY_USEC) {
4136 KASSERT((tp->t_tmr_granularity == TCP_TMR_GRANULARITY_TICKS),
4137 ("Granularity is not TICKS its %u in tp:%p",
4138 tp->t_tmr_granularity, tp));
4139 tp->t_rttlow = TICKS_2_USEC(tp->t_rttlow);
4140 if (tp->t_srtt > 1) {
4143 val = tp->t_srtt >> TCP_RTT_SHIFT;
4144 frac = tp->t_srtt & 0x1f;
4145 tp->t_srtt = TICKS_2_USEC(val);
4147 * frac is the fractional part of the srtt (if any)
4148 * but its in ticks and every bit represents
4153 frac = (((uint64_t)frac * (uint64_t)HPTS_USEC_IN_MSEC) / (uint64_t)TCP_RTT_SCALE);
4155 frac = (((uint64_t)frac * (uint64_t)HPTS_USEC_IN_SEC) / ((uint64_t)(hz) * (uint64_t)TCP_RTT_SCALE));
4163 val = tp->t_rttvar >> TCP_RTTVAR_SHIFT;
4164 frac = tp->t_rttvar & 0x1f;
4165 tp->t_rttvar = TICKS_2_USEC(val);
4167 * frac is the fractional part of the srtt (if any)
4168 * but its in ticks and every bit represents
4173 frac = (((uint64_t)frac * (uint64_t)HPTS_USEC_IN_MSEC) / (uint64_t)TCP_RTT_SCALE);
4175 frac = (((uint64_t)frac * (uint64_t)HPTS_USEC_IN_SEC) / ((uint64_t)(hz) * (uint64_t)TCP_RTT_SCALE));
4177 tp->t_rttvar += frac;
4180 tp->t_tmr_granularity = TCP_TMR_GRANULARITY_USEC;
4181 } else if (granularity == TCP_TMR_GRANULARITY_TICKS) {
4182 /* Convert back to ticks, with */
4183 KASSERT((tp->t_tmr_granularity == TCP_TMR_GRANULARITY_USEC),
4184 ("Granularity is not USEC its %u in tp:%p",
4185 tp->t_tmr_granularity, tp));
4186 if (tp->t_srtt > 1) {
4189 val = USEC_2_TICKS(tp->t_srtt);
4190 frac = tp->t_srtt % (HPTS_USEC_IN_SEC / hz);
4191 tp->t_srtt = val << TCP_RTT_SHIFT;
4193 * frac is the fractional part here is left
4194 * over from converting to hz and shifting.
4195 * We need to convert this to the 5 bit
4200 frac = (((uint64_t)frac * (uint64_t)TCP_RTT_SCALE) / (uint64_t)HPTS_USEC_IN_MSEC);
4202 frac = (((uint64_t)frac * (uint64_t)(hz) * (uint64_t)TCP_RTT_SCALE) /(uint64_t)HPTS_USEC_IN_SEC);
4210 val = USEC_2_TICKS(tp->t_rttvar);
4211 frac = tp->t_srtt % (HPTS_USEC_IN_SEC / hz);
4212 tp->t_rttvar = val << TCP_RTTVAR_SHIFT;
4214 * frac is the fractional part here is left
4215 * over from converting to hz and shifting.
4216 * We need to convert this to the 5 bit
4221 frac = (((uint64_t)frac * (uint64_t)TCP_RTT_SCALE) / (uint64_t)HPTS_USEC_IN_MSEC);
4223 frac = (((uint64_t)frac * (uint64_t)(hz) * (uint64_t)TCP_RTT_SCALE) /(uint64_t)HPTS_USEC_IN_SEC);
4225 tp->t_rttvar += frac;
4228 tp->t_rttlow = USEC_2_TICKS(tp->t_rttlow);
4229 tp->t_tmr_granularity = TCP_TMR_GRANULARITY_TICKS;
4233 panic("Unknown granularity:%d tp:%p",
4240 tcp_handle_orphaned_packets(struct tcpcb *tp)
4242 struct mbuf *save, *m, *prev;
4244 * Called when a stack switch is occuring from the fini()
4245 * of the old stack. We assue the init() as already been
4246 * run of the new stack and it has set the t_flags2 to
4247 * what it supports. This function will then deal with any
4248 * differences i.e. cleanup packets that maybe queued that
4249 * the newstack does not support.
4252 if (tp->t_flags2 & TF2_MBUF_L_ACKS)
4254 if ((tp->t_flags2 & TF2_SUPPORTS_MBUFQ) == 0 &&
4255 !STAILQ_EMPTY(&tp->t_inqueue)) {
4257 * It is unsafe to process the packets since a
4258 * reset may be lurking in them (its rare but it
4259 * can occur). If we were to find a RST, then we
4260 * would end up dropping the connection and the
4261 * INP lock, so when we return the caller (tcp_usrreq)
4262 * will blow up when it trys to unlock the inp.
4263 * This new stack does not do any fancy LRO features
4264 * so all we can do is toss the packets.
4266 m = STAILQ_FIRST(&tp->t_inqueue);
4267 STAILQ_INIT(&tp->t_inqueue);
4268 STAILQ_FOREACH_FROM_SAFE(m, &tp->t_inqueue, m_stailqpkt, save)
4272 * Here we have a stack that does mbuf queuing but
4273 * does not support compressed ack's. We must
4274 * walk all the mbufs and discard any compressed acks.
4276 STAILQ_FOREACH_SAFE(m, &tp->t_inqueue, m_stailqpkt, save) {
4277 if (m->m_flags & M_ACKCMP) {
4278 if (m == STAILQ_FIRST(&tp->t_inqueue))
4279 STAILQ_REMOVE_HEAD(&tp->t_inqueue,
4282 STAILQ_REMOVE_AFTER(&tp->t_inqueue,
4291 #ifdef TCP_REQUEST_TRK
4293 tcp_estimate_tls_overhead(struct socket *so, uint64_t tls_usr_bytes)
4296 struct ktls_session *tls;
4297 uint32_t rec_oh, records;
4299 tls = so->so_snd.sb_tls_info;
4303 rec_oh = tls->params.tls_hlen + tls->params.tls_tlen;
4304 records = ((tls_usr_bytes + tls->params.max_frame_len - 1)/tls->params.max_frame_len);
4305 return (records * rec_oh);
4311 extern uint32_t tcp_stale_entry_time;
4312 uint32_t tcp_stale_entry_time = 250000;
4313 SYSCTL_UINT(_net_inet_tcp, OID_AUTO, usrlog_stale, CTLFLAG_RW,
4314 &tcp_stale_entry_time, 250000, "Time that a tcpreq entry without a sendfile ages out");
4317 tcp_req_log_req_info(struct tcpcb *tp, struct tcp_sendfile_track *req,
4318 uint16_t slot, uint8_t val, uint64_t offset, uint64_t nbytes)
4320 if (tcp_bblogging_on(tp)) {
4321 union tcp_log_stackspecific log;
4324 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
4326 log.u_bbr.inhpts = tcp_in_hpts(tp);
4328 log.u_bbr.flex8 = val;
4329 log.u_bbr.rttProp = req->timestamp;
4330 log.u_bbr.delRate = req->start;
4331 log.u_bbr.cur_del_rate = req->end;
4332 log.u_bbr.flex1 = req->start_seq;
4333 log.u_bbr.flex2 = req->end_seq;
4334 log.u_bbr.flex3 = req->flags;
4335 log.u_bbr.flex4 = ((req->localtime >> 32) & 0x00000000ffffffff);
4336 log.u_bbr.flex5 = (req->localtime & 0x00000000ffffffff);
4337 log.u_bbr.flex7 = slot;
4338 log.u_bbr.bw_inuse = offset;
4339 /* nbytes = flex6 | epoch */
4340 log.u_bbr.flex6 = ((nbytes >> 32) & 0x00000000ffffffff);
4341 log.u_bbr.epoch = (nbytes & 0x00000000ffffffff);
4342 /* cspr = lt_epoch | pkts_out */
4343 log.u_bbr.lt_epoch = ((req->cspr >> 32) & 0x00000000ffffffff);
4344 log.u_bbr.pkts_out |= (req->cspr & 0x00000000ffffffff);
4345 log.u_bbr.applimited = tp->t_tcpreq_closed;
4346 log.u_bbr.applimited <<= 8;
4347 log.u_bbr.applimited |= tp->t_tcpreq_open;
4348 log.u_bbr.applimited <<= 8;
4349 log.u_bbr.applimited |= tp->t_tcpreq_req;
4350 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
4351 TCP_LOG_EVENTP(tp, NULL,
4352 &tptosocket(tp)->so_rcv,
4353 &tptosocket(tp)->so_snd,
4355 0, &log, false, &tv);
4360 tcp_req_free_a_slot(struct tcpcb *tp, struct tcp_sendfile_track *ent)
4362 if (tp->t_tcpreq_req > 0)
4364 if (ent->flags & TCP_TRK_TRACK_FLG_OPEN) {
4365 if (tp->t_tcpreq_open > 0)
4366 tp->t_tcpreq_open--;
4368 if (tp->t_tcpreq_closed > 0)
4369 tp->t_tcpreq_closed--;
4371 ent->flags = TCP_TRK_TRACK_FLG_EMPTY;
4375 tcp_req_check_for_stale_entries(struct tcpcb *tp, uint64_t ts, int rm_oldest)
4377 struct tcp_sendfile_track *ent;
4378 uint64_t time_delta, oldest_delta;
4379 int i, oldest, oldest_set = 0, cnt_rm = 0;
4381 for(i = 0; i < MAX_TCP_TRK_REQ; i++) {
4382 ent = &tp->t_tcpreq_info[i];
4383 if (ent->flags != TCP_TRK_TRACK_FLG_USED) {
4385 * We only care about closed end ranges
4386 * that are allocated and have no sendfile
4387 * ever touching them. They would be in
4392 if (ts >= ent->localtime)
4393 time_delta = ts - ent->localtime;
4397 ((oldest_delta < time_delta) || (oldest_set == 0))) {
4400 oldest_delta = time_delta;
4402 if (tcp_stale_entry_time && (time_delta >= tcp_stale_entry_time)) {
4404 * No sendfile in a our time-limit
4408 tcp_req_log_req_info(tp, &tp->t_tcpreq_info[i], i, TCP_TRK_REQ_LOG_STALE,
4410 tcp_req_free_a_slot(tp, ent);
4413 if ((cnt_rm == 0) && rm_oldest && oldest_set) {
4414 ent = &tp->t_tcpreq_info[oldest];
4415 tcp_req_log_req_info(tp, &tp->t_tcpreq_info[i], i, TCP_TRK_REQ_LOG_STALE,
4417 tcp_req_free_a_slot(tp, ent);
4422 tcp_req_check_for_comp(struct tcpcb *tp, tcp_seq ack_point)
4425 struct tcp_sendfile_track *ent;
4427 /* Clean up any old closed end requests that are now completed */
4428 if (tp->t_tcpreq_req == 0)
4430 if (tp->t_tcpreq_closed == 0)
4432 for(i = 0; i < MAX_TCP_TRK_REQ; i++) {
4433 ent = &tp->t_tcpreq_info[i];
4434 /* Skip empty ones */
4435 if (ent->flags == TCP_TRK_TRACK_FLG_EMPTY)
4437 /* Skip open ones */
4438 if (ent->flags & TCP_TRK_TRACK_FLG_OPEN)
4440 if (SEQ_GEQ(ack_point, ent->end_seq)) {
4441 /* We are past it -- free it */
4442 tcp_req_log_req_info(tp, ent,
4443 i, TCP_TRK_REQ_LOG_FREED, 0, 0);
4444 tcp_req_free_a_slot(tp, ent);
4452 tcp_req_is_entry_comp(struct tcpcb *tp, struct tcp_sendfile_track *ent, tcp_seq ack_point)
4454 if (tp->t_tcpreq_req == 0)
4456 if (tp->t_tcpreq_closed == 0)
4458 if (ent->flags == TCP_TRK_TRACK_FLG_EMPTY)
4460 if (SEQ_GEQ(ack_point, ent->end_seq)) {
4466 struct tcp_sendfile_track *
4467 tcp_req_find_a_req_that_is_completed_by(struct tcpcb *tp, tcp_seq th_ack, int *ip)
4470 * Given an ack point (th_ack) walk through our entries and
4471 * return the first one found that th_ack goes past the
4474 struct tcp_sendfile_track *ent;
4477 if (tp->t_tcpreq_req == 0) {
4481 for(i = 0; i < MAX_TCP_TRK_REQ; i++) {
4482 ent = &tp->t_tcpreq_info[i];
4483 if (ent->flags == TCP_TRK_TRACK_FLG_EMPTY)
4485 if ((ent->flags & TCP_TRK_TRACK_FLG_OPEN) == 0) {
4486 if (SEQ_GEQ(th_ack, ent->end_seq)) {
4495 struct tcp_sendfile_track *
4496 tcp_req_find_req_for_seq(struct tcpcb *tp, tcp_seq seq)
4498 struct tcp_sendfile_track *ent;
4501 if (tp->t_tcpreq_req == 0) {
4505 for(i = 0; i < MAX_TCP_TRK_REQ; i++) {
4506 ent = &tp->t_tcpreq_info[i];
4507 tcp_req_log_req_info(tp, ent, i, TCP_TRK_REQ_LOG_SEARCH,
4509 if (ent->flags == TCP_TRK_TRACK_FLG_EMPTY) {
4512 if (ent->flags & TCP_TRK_TRACK_FLG_OPEN) {
4514 * An open end request only needs to
4515 * match the beginning seq or be
4516 * all we have (once we keep going on
4517 * a open end request we may have a seq
4520 if ((SEQ_GEQ(seq, ent->start_seq)) ||
4521 (tp->t_tcpreq_closed == 0))
4525 * For this one we need to
4526 * be a bit more careful if its
4527 * completed at least.
4529 if ((SEQ_GEQ(seq, ent->start_seq)) &&
4530 (SEQ_LT(seq, ent->end_seq))) {
4538 /* Should this be in its own file tcp_req.c ? */
4539 struct tcp_sendfile_track *
4540 tcp_req_alloc_req_full(struct tcpcb *tp, struct tcp_snd_req *req, uint64_t ts, int rec_dups)
4542 struct tcp_sendfile_track *fil;
4545 /* In case the stack does not check for completions do so now */
4546 tcp_req_check_for_comp(tp, tp->snd_una);
4547 /* Check for stale entries */
4548 if (tp->t_tcpreq_req)
4549 tcp_req_check_for_stale_entries(tp, ts,
4550 (tp->t_tcpreq_req >= MAX_TCP_TRK_REQ));
4551 /* Check to see if this is a duplicate of one not started */
4552 if (tp->t_tcpreq_req) {
4553 for(i = 0, allocated = 0; i < MAX_TCP_TRK_REQ; i++) {
4554 fil = &tp->t_tcpreq_info[i];
4555 if (fil->flags != TCP_TRK_TRACK_FLG_USED)
4557 if ((fil->timestamp == req->timestamp) &&
4558 (fil->start == req->start) &&
4559 ((fil->flags & TCP_TRK_TRACK_FLG_OPEN) ||
4560 (fil->end == req->end))) {
4562 * We already have this request
4563 * and it has not been started with sendfile.
4564 * This probably means the user was returned
4565 * a 4xx of some sort and its going to age
4566 * out, lets not duplicate it.
4572 /* Ok if there is no room at the inn we are in trouble */
4573 if (tp->t_tcpreq_req >= MAX_TCP_TRK_REQ) {
4574 tcp_trace_point(tp, TCP_TP_REQ_LOG_FAIL);
4575 for(i = 0; i < MAX_TCP_TRK_REQ; i++) {
4576 tcp_req_log_req_info(tp, &tp->t_tcpreq_info[i],
4577 i, TCP_TRK_REQ_LOG_ALLOCFAIL, 0, 0);
4581 for(i = 0, allocated = 0; i < MAX_TCP_TRK_REQ; i++) {
4582 fil = &tp->t_tcpreq_info[i];
4583 if (fil->flags == TCP_TRK_TRACK_FLG_EMPTY) {
4585 fil->flags = TCP_TRK_TRACK_FLG_USED;
4586 fil->timestamp = req->timestamp;
4587 fil->localtime = ts;
4588 fil->start = req->start;
4589 if (req->flags & TCP_LOG_HTTPD_RANGE_END) {
4590 fil->end = req->end;
4593 fil->flags |= TCP_TRK_TRACK_FLG_OPEN;
4596 * We can set the min boundaries to the TCP Sequence space,
4597 * but it might be found to be further up when sendfile
4598 * actually runs on this range (if it ever does).
4600 fil->sbcc_at_s = tptosocket(tp)->so_snd.sb_ccc;
4601 fil->start_seq = tp->snd_una +
4602 tptosocket(tp)->so_snd.sb_ccc;
4603 fil->end_seq = (fil->start_seq + ((uint32_t)(fil->end - fil->start)));
4604 if (tptosocket(tp)->so_snd.sb_tls_info) {
4606 * This session is doing TLS. Take a swag guess
4609 fil->end_seq += tcp_estimate_tls_overhead(
4610 tptosocket(tp), (fil->end - fil->start));
4613 if (fil->flags & TCP_TRK_TRACK_FLG_OPEN)
4614 tp->t_tcpreq_open++;
4616 tp->t_tcpreq_closed++;
4617 tcp_req_log_req_info(tp, fil, i,
4618 TCP_TRK_REQ_LOG_NEW, 0, 0);
4627 tcp_req_alloc_req(struct tcpcb *tp, union tcp_log_userdata *user, uint64_t ts)
4629 (void)tcp_req_alloc_req_full(tp, &user->tcp_req, ts, 1);
4634 tcp_log_socket_option(struct tcpcb *tp, uint32_t option_num, uint32_t option_val, int err)
4636 if (tcp_bblogging_on(tp)) {
4637 struct tcp_log_buffer *l;
4639 l = tcp_log_event(tp, NULL,
4640 &tptosocket(tp)->so_rcv,
4641 &tptosocket(tp)->so_snd,
4644 NULL, NULL, 0, NULL);
4646 l->tlb_flex1 = option_num;
4647 l->tlb_flex2 = option_val;
4653 tcp_get_srtt(struct tcpcb *tp, int granularity)
4657 KASSERT(granularity == TCP_TMR_GRANULARITY_USEC ||
4658 granularity == TCP_TMR_GRANULARITY_TICKS,
4659 ("%s: called with unexpected granularity %d", __func__,
4665 * We only support two granularities. If the stored granularity
4666 * does not match the granularity requested by the caller,
4667 * convert the stored value to the requested unit of granularity.
4669 if (tp->t_tmr_granularity != granularity) {
4670 if (granularity == TCP_TMR_GRANULARITY_USEC)
4671 srtt = TICKS_2_USEC(srtt);
4673 srtt = USEC_2_TICKS(srtt);
4677 * If the srtt is stored with ticks granularity, we need to
4678 * unshift to get the actual value. We do this after the
4679 * conversion above (if one was necessary) in order to maximize
4682 if (tp->t_tmr_granularity == TCP_TMR_GRANULARITY_TICKS)
4683 srtt = srtt >> TCP_RTT_SHIFT;