1 /* $KAME: ip_ecn.c,v 1.12 2002/01/07 11:34:47 kjc Exp $ */
4 * SPDX-License-Identifier: BSD-3-Clause
6 * Copyright (C) 1999 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * ECN consideration on tunnel ingress/egress operation.
36 * http://www.aciri.org/floyd/papers/draft-ipsec-ecn-00.txt
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
43 #include "opt_inet6.h"
45 #include <sys/param.h>
46 #include <sys/systm.h>
48 #include <sys/errno.h>
50 #include <netinet/in.h>
51 #include <netinet/in_systm.h>
52 #include <netinet/ip.h>
54 #include <netinet/ip6.h>
57 #include <netinet/ip_ecn.h>
59 #include <netinet6/ip6_ecn.h>
63 * ECN and TOS (or TCLASS) processing rules at tunnel encapsulation and
64 * decapsulation from RFC3168:
66 * Outer Hdr at Inner Hdr at
67 * Encapsulator Decapsulator
68 * Header fields: -------------------- ------------
69 * DS Field copied from inner hdr no change
70 * ECN Field constructed by (I) constructed by (E)
72 * ECN_ALLOWED (full functionality):
73 * (I) if the ECN field in the inner header is set to CE, then set the
74 * ECN field in the outer header to ECT(0).
75 * otherwise, copy the ECN field to the outer header.
77 * (E) if the ECN field in the outer header is set to CE and the ECN
78 * field of the inner header is not-ECT, drop the packet.
79 * if the ECN field in the inner header is set to ECT(0) or ECT(1)
80 * and the ECN field in the outer header is set to CE, then copy CE to
81 * the inner header. otherwise, make no change to the inner header.
83 * ECN_FORBIDDEN (limited functionality):
84 * (I) set the ECN field to not-ECT in the outer header.
86 * (E) if the ECN field in the outer header is set to CE, drop the packet.
87 * otherwise, make no change to the ECN field in the inner header.
89 * the drop rule is for backward compatibility and protection against
94 * modify outer ECN (TOS) field on ingress operation (tunnel encapsulation).
97 ip_ecn_ingress(int mode, u_int8_t *outer, const u_int8_t *inner)
100 if (!outer || !inner)
101 panic("NULL pointer passed to ip_ecn_ingress");
105 case ECN_ALLOWED: /* ECN allowed */
107 * full-functionality: if the inner is CE, set ECT(0)
108 * to the outer. otherwise, copy the ECN field.
110 if ((*inner & IPTOS_ECN_MASK) == IPTOS_ECN_CE)
111 *outer &= ~IPTOS_ECN_ECT1;
113 case ECN_FORBIDDEN: /* ECN forbidden */
115 * limited-functionality: set not-ECT to the outer
117 *outer &= ~IPTOS_ECN_MASK;
119 case ECN_NOCARE: /* no consideration to ECN */
125 * modify inner ECN (TOS) field on egress operation (tunnel decapsulation).
126 * the caller should drop the packet if the return value is 0.
129 ip_ecn_egress(int mode, const u_int8_t *outer, u_int8_t *inner)
132 if (!outer || !inner)
133 panic("NULL pointer passed to ip_ecn_egress");
138 * full-functionality: if the outer is CE and the inner is
139 * not-ECT, should drop it. otherwise, copy CE.
141 if ((*outer & IPTOS_ECN_MASK) == IPTOS_ECN_CE) {
142 if ((*inner & IPTOS_ECN_MASK) == IPTOS_ECN_NOTECT)
144 *inner |= IPTOS_ECN_CE;
147 case ECN_FORBIDDEN: /* ECN forbidden */
149 * limited-functionality: if the outer is CE, should drop it.
150 * otherwise, leave the inner.
152 if ((*outer & IPTOS_ECN_MASK) == IPTOS_ECN_CE)
155 case ECN_NOCARE: /* no consideration to ECN */
163 ip6_ecn_ingress(int mode, u_int32_t *outer, const u_int32_t *inner)
165 u_int8_t outer8, inner8;
167 if (!outer || !inner)
168 panic("NULL pointer passed to ip6_ecn_ingress");
170 inner8 = (ntohl(*inner) >> 20) & 0xff;
171 ip_ecn_ingress(mode, &outer8, &inner8);
172 *outer &= ~htonl(0xff << 20);
173 *outer |= htonl((u_int32_t)outer8 << 20);
177 ip6_ecn_egress(int mode, const u_int32_t *outer, u_int32_t *inner)
179 u_int8_t outer8, inner8, oinner8;
181 if (!outer || !inner)
182 panic("NULL pointer passed to ip6_ecn_egress");
184 outer8 = (ntohl(*outer) >> 20) & 0xff;
185 inner8 = oinner8 = (ntohl(*inner) >> 20) & 0xff;
186 if (ip_ecn_egress(mode, &outer8, &inner8) == 0)
188 if (inner8 != oinner8) {
189 *inner &= ~htonl(0xff << 20);
190 *inner |= htonl((u_int32_t)inner8 << 20);