2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995
5 * The Regents of the University of California. All rights reserved.
6 * Copyright (c) 2007-2008,2010
7 * Swinburne University of Technology, Melbourne, Australia.
8 * Copyright (c) 2009-2010 Lawrence Stewart <lstewart@freebsd.org>
9 * Copyright (c) 2010 The FreeBSD Foundation
10 * Copyright (c) 2010-2011 Juniper Networks, Inc.
11 * Copyright (c) 2019 Richard Scheffenegger <srichard@netapp.com>
12 * All rights reserved.
14 * Portions of this software were developed at the Centre for Advanced Internet
15 * Architectures, Swinburne University of Technology, by Lawrence Stewart,
16 * James Healy and David Hayes, made possible in part by a grant from the Cisco
17 * University Research Program Fund at Community Foundation Silicon Valley.
19 * Portions of this software were developed at the Centre for Advanced
20 * Internet Architectures, Swinburne University of Technology, Melbourne,
21 * Australia by David Hayes under sponsorship from the FreeBSD Foundation.
23 * Portions of this software were developed by Robert N. M. Watson under
24 * contract to Juniper Networks, Inc.
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 * 3. Neither the name of the University nor the names of its contributors
35 * may be used to endorse or promote products derived from this software
36 * without specific prior written permission.
38 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
39 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
41 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
42 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
43 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
44 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
45 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
46 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
47 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * @(#)tcp_ecn.c 8.12 (Berkeley) 5/24/95
54 * Utility functions to deal with Explicit Congestion Notification in TCP
55 * implementing the essential parts of the Accurate ECN extension
56 * https://tools.ietf.org/html/draft-ietf-tcpm-accurate-ecn-09
59 #include <sys/cdefs.h>
60 __FBSDID("$FreeBSD$");
63 #include "opt_inet6.h"
64 #include "opt_tcpdebug.h"
66 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/kernel.h>
69 #include <sys/sysctl.h>
70 #include <sys/malloc.h>
72 #include <sys/socket.h>
73 #include <sys/socketvar.h>
75 #include <machine/cpu.h>
80 #include <net/if_var.h>
81 #include <net/route.h>
84 #include <netinet/in.h>
85 #include <netinet/in_systm.h>
86 #include <netinet/ip.h>
87 #include <netinet/in_var.h>
88 #include <netinet/in_pcb.h>
89 #include <netinet/ip_var.h>
90 #include <netinet/ip6.h>
91 #include <netinet/icmp6.h>
92 #include <netinet6/nd6.h>
93 #include <netinet6/ip6_var.h>
94 #include <netinet6/in6_pcb.h>
95 #include <netinet/tcp.h>
96 #include <netinet/tcp_fsm.h>
97 #include <netinet/tcp_seq.h>
98 #include <netinet/tcp_var.h>
99 #include <netinet/tcp_syncache.h>
100 #include <netinet/tcp_timer.h>
101 #include <netinet/tcpip.h>
102 #include <netinet/tcp_ecn.h>
106 * Process incoming SYN,ACK packet
109 tcp_ecn_input_syn_sent(struct tcpcb *tp, uint16_t thflags, int iptos)
112 if (V_tcp_do_ecn == 0)
114 if ((V_tcp_do_ecn == 1) ||
115 (V_tcp_do_ecn == 2)) {
116 /* RFC3168 ECN handling */
117 if ((thflags & (TH_CWR | TH_ECE)) == (0 | TH_ECE)) {
118 tp->t_flags2 |= TF2_ECN_PERMIT;
119 TCPSTAT_INC(tcps_ecn_shs);
122 /* decoding Accurate ECN according to table in section 3.1.1 */
123 if ((V_tcp_do_ecn == 3) ||
124 (V_tcp_do_ecn == 4)) {
126 * on the SYN,ACK, process the AccECN
127 * flags indicating the state the SYN
129 * Reactions to Path ECN mangling can
132 switch (thflags & (TH_AE | TH_CWR | TH_ECE)) {
135 tp->t_flags2 |= TF2_ECN_PERMIT;
136 TCPSTAT_INC(tcps_ecn_shs);
140 tp->t_flags2 |= TF2_ACE_PERMIT;
142 TCPSTAT_INC(tcps_ecn_shs);
143 TCPSTAT_INC(tcps_ace_nect);
147 tp->t_flags2 |= TF2_ACE_PERMIT;
149 TCPSTAT_INC(tcps_ecn_shs);
150 TCPSTAT_INC(tcps_ace_ect0);
153 case (0|TH_CWR|TH_ECE):
154 tp->t_flags2 |= TF2_ACE_PERMIT;
156 TCPSTAT_INC(tcps_ecn_shs);
157 TCPSTAT_INC(tcps_ace_ect1);
160 case (TH_AE|TH_CWR|0):
161 tp->t_flags2 |= TF2_ACE_PERMIT;
164 * reduce the IW to 2 MSS (to
165 * account for delayed acks) if
166 * the SYN,ACK was CE marked
168 tp->snd_cwnd = 2 * tcp_maxseg(tp);
169 TCPSTAT_INC(tcps_ecn_shs);
170 TCPSTAT_INC(tcps_ace_nect);
176 * Set the AccECN Codepoints on
177 * the outgoing <ACK> to the ECN
178 * state of the <SYN,ACK>
179 * according to table 3 in the
182 switch (iptos & IPTOS_ECN_MASK) {
183 case (IPTOS_ECN_NOTECT):
186 case (IPTOS_ECN_ECT0):
189 case (IPTOS_ECN_ECT1):
200 * Handle parallel SYN for ECN
203 tcp_ecn_input_parallel_syn(struct tcpcb *tp, uint16_t thflags, int iptos)
205 if (thflags & TH_ACK)
207 if (V_tcp_do_ecn == 0)
209 if ((V_tcp_do_ecn == 1) ||
210 (V_tcp_do_ecn == 2)) {
211 /* RFC3168 ECN handling */
212 if ((thflags & (TH_CWR | TH_ECE)) == (TH_CWR | TH_ECE)) {
213 tp->t_flags2 |= TF2_ECN_PERMIT;
214 tp->t_flags2 |= TF2_ECN_SND_ECE;
215 TCPSTAT_INC(tcps_ecn_shs);
218 if ((V_tcp_do_ecn == 3) ||
219 (V_tcp_do_ecn == 4)) {
220 /* AccECN handling */
221 switch (thflags & (TH_AE | TH_CWR | TH_ECE)) {
225 case (0|TH_CWR|TH_ECE):
226 tp->t_flags2 |= TF2_ECN_PERMIT;
227 tp->t_flags2 |= TF2_ECN_SND_ECE;
228 TCPSTAT_INC(tcps_ecn_shs);
230 case (TH_AE|TH_CWR|TH_ECE):
231 tp->t_flags2 |= TF2_ACE_PERMIT;
232 TCPSTAT_INC(tcps_ecn_shs);
234 * Set the AccECN Codepoints on
235 * the outgoing <ACK> to the ECN
236 * state of the <SYN,ACK>
237 * according to table 3 in the
240 switch (iptos & IPTOS_ECN_MASK) {
241 case (IPTOS_ECN_NOTECT):
244 case (IPTOS_ECN_ECT0):
247 case (IPTOS_ECN_ECT1):
260 * TCP ECN processing.
263 tcp_ecn_input_segment(struct tcpcb *tp, uint16_t thflags, int iptos)
267 if (tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT)) {
268 switch (iptos & IPTOS_ECN_MASK) {
270 TCPSTAT_INC(tcps_ecn_ce);
273 TCPSTAT_INC(tcps_ecn_ect0);
276 TCPSTAT_INC(tcps_ecn_ect1);
280 if (tp->t_flags2 & TF2_ACE_PERMIT) {
281 if ((iptos & IPTOS_ECN_MASK) == IPTOS_ECN_CE)
283 if (tp->t_flags2 & TF2_ECN_PERMIT) {
284 delta_ace = (tcp_ecn_get_ace(thflags) + 8 -
285 (tp->t_scep & 0x07)) & 0x07;
286 tp->t_scep += delta_ace;
289 * process the final ACK of the 3WHS
290 * see table 3 in draft-ietf-tcpm-accurate-ecn
292 switch (tcp_ecn_get_ace(thflags)) {
294 /* nonECT SYN or SYN,ACK */
297 /* ECT1 SYN or SYN,ACK */
300 /* ECT0 SYN or SYN,ACK */
304 /* CE SYN or SYN,ACK */
306 tp->snd_cwnd = 2 * tcp_maxseg(tp);
309 /* mangled AccECN handshake */
313 tp->t_flags2 |= TF2_ECN_PERMIT;
316 /* RFC3168 ECN handling */
317 if ((thflags & (TH_SYN | TH_ECE)) == TH_ECE)
319 if (thflags & TH_CWR) {
320 tp->t_flags2 &= ~TF2_ECN_SND_ECE;
321 tp->t_flags |= TF_ACKNOW;
323 if ((iptos & IPTOS_ECN_MASK) == IPTOS_ECN_CE)
324 tp->t_flags2 |= TF2_ECN_SND_ECE;
327 /* Process a packet differently from RFC3168. */
328 cc_ecnpkt_handler_flags(tp, thflags, iptos);
335 * Send ECN setup <SYN> packet header flags
338 tcp_ecn_output_syn_sent(struct tcpcb *tp)
340 uint16_t thflags = 0;
342 if (V_tcp_do_ecn == 0)
344 if (V_tcp_do_ecn == 1) {
345 /* Send a RFC3168 ECN setup <SYN> packet */
346 if (tp->t_rxtshift >= 1) {
347 if (tp->t_rxtshift <= V_tcp_ecn_maxretries)
348 thflags = TH_ECE|TH_CWR;
350 thflags = TH_ECE|TH_CWR;
352 if (V_tcp_do_ecn == 3) {
353 /* Send an Accurate ECN setup <SYN> packet */
354 if (tp->t_rxtshift >= 1) {
355 if (tp->t_rxtshift <= V_tcp_ecn_maxretries)
356 thflags = TH_ECE|TH_CWR|TH_AE;
358 thflags = TH_ECE|TH_CWR|TH_AE;
365 * output processing of ECN feature
366 * returning IP ECN header codepoint
369 tcp_ecn_output_established(struct tcpcb *tp, uint16_t *thflags, int len, bool rxmit)
371 int ipecn = IPTOS_ECN_NOTECT;
375 * If the peer has ECN, mark data packets with
376 * ECN capable transmission (ECT).
377 * Ignore pure control packets, retransmissions
380 newdata = (len > 0 && SEQ_GEQ(tp->snd_nxt, tp->snd_max) &&
382 !((tp->t_flags & TF_FORCEDATA) && len == 1));
383 /* RFC3168 ECN marking, only new data segments */
385 ipecn = IPTOS_ECN_ECT0;
386 TCPSTAT_INC(tcps_ecn_ect0);
389 * Reply with proper ECN notifications.
391 if (tp->t_flags2 & TF2_ACE_PERMIT) {
392 *thflags &= ~(TH_AE|TH_CWR|TH_ECE);
393 if (tp->t_rcep & 0x01)
395 if (tp->t_rcep & 0x02)
397 if (tp->t_rcep & 0x04)
399 if (!(tp->t_flags2 & TF2_ECN_PERMIT)) {
401 * here we process the final
404 if (tp->t_rcep == 0b110) {
409 tp->t_flags2 |= TF2_ECN_PERMIT;
413 (tp->t_flags2 & TF2_ECN_SND_CWR)) {
415 tp->t_flags2 &= ~TF2_ECN_SND_CWR;
417 if (tp->t_flags2 & TF2_ECN_SND_ECE)
425 * Set up the ECN related tcpcb fields from
429 tcp_ecn_syncache_socket(struct tcpcb *tp, struct syncache *sc)
431 if (sc->sc_flags & SCF_ECN_MASK) {
432 switch (sc->sc_flags & SCF_ECN_MASK) {
434 tp->t_flags2 |= TF2_ECN_PERMIT;
441 tp->t_flags2 |= TF2_ACE_PERMIT;
446 tp->t_flags2 |= TF2_ACE_PERMIT;
450 /* undefined SCF codepoint */
458 * Process a <SYN> packets ECN information, and provide the
459 * syncache with the relevant information.
462 tcp_ecn_syncache_add(uint16_t thflags, int iptos)
466 switch (thflags & (TH_AE|TH_CWR|TH_ECE)) {
471 case (0|TH_CWR|TH_ECE):
475 case (TH_AE|TH_CWR|TH_ECE):
476 if ((V_tcp_do_ecn == 3) ||
477 (V_tcp_do_ecn == 4)) {
478 switch (iptos & IPTOS_ECN_MASK) {
480 scflags = SCF_ACE_CE;
488 case IPTOS_ECN_NOTECT:
495 /* Default Case (section 3.1.2) */
497 if ((V_tcp_do_ecn == 3) ||
498 (V_tcp_do_ecn == 4)) {
499 switch (iptos & IPTOS_ECN_MASK) {
501 scflags = SCF_ACE_CE;
509 case IPTOS_ECN_NOTECT:
520 * Set up the ECN information for the <SYN,ACK> from
521 * syncache information.
524 tcp_ecn_syncache_respond(uint16_t thflags, struct syncache *sc)
526 if ((thflags & TH_SYN) &&
527 (sc->sc_flags & SCF_ECN_MASK)) {
528 switch (sc->sc_flags & SCF_ECN_MASK) {
530 thflags |= (0 | 0 | TH_ECE);
531 TCPSTAT_INC(tcps_ecn_shs);
534 thflags |= (0 | TH_CWR | 0);
535 TCPSTAT_INC(tcps_ecn_shs);
536 TCPSTAT_INC(tcps_ace_nect);
539 thflags |= (TH_AE | 0 | 0);
540 TCPSTAT_INC(tcps_ecn_shs);
541 TCPSTAT_INC(tcps_ace_ect0);
544 thflags |= (0 | TH_ECE | TH_CWR);
545 TCPSTAT_INC(tcps_ecn_shs);
546 TCPSTAT_INC(tcps_ace_ect1);
549 thflags |= (TH_AE | TH_CWR | 0);
550 TCPSTAT_INC(tcps_ecn_shs);
551 TCPSTAT_INC(tcps_ace_ce);
553 /* undefined SCF codepoint */
562 tcp_ecn_get_ace(uint16_t thflags)
566 if (thflags & TH_ECE)
568 if (thflags & TH_CWR)