1 /* $KAME: ip_ecn.c,v 1.12 2002/01/07 11:34:47 kjc Exp $ */
4 * Copyright (C) 1999 WIDE Project.
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 project 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 PROJECT 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 PROJECT 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
33 * ECN consideration on tunnel ingress/egress operation.
34 * http://www.aciri.org/floyd/papers/draft-ipsec-ecn-00.txt
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
41 #include "opt_inet6.h"
43 #include <sys/param.h>
44 #include <sys/systm.h>
46 #include <sys/errno.h>
48 #include <netinet/in.h>
49 #include <netinet/in_systm.h>
50 #include <netinet/ip.h>
52 #include <netinet/ip6.h>
55 #include <netinet/ip_ecn.h>
57 #include <netinet6/ip6_ecn.h>
61 * ECN and TOS (or TCLASS) processing rules at tunnel encapsulation and
62 * decapsulation from RFC3168:
64 * Outer Hdr at Inner Hdr at
65 * Encapsulator Decapsulator
66 * Header fields: -------------------- ------------
67 * DS Field copied from inner hdr no change
68 * ECN Field constructed by (I) constructed by (E)
70 * ECN_ALLOWED (full functionality):
71 * (I) if the ECN field in the inner header is set to CE, then set the
72 * ECN field in the outer header to ECT(0).
73 * otherwise, copy the ECN field to the outer header.
75 * (E) if the ECN field in the outer header is set to CE and the ECN
76 * field of the inner header is not-ECT, drop the packet.
77 * if the ECN field in the inner header is set to ECT(0) or ECT(1)
78 * and the ECN field in the outer header is set to CE, then copy CE to
79 * the inner header. otherwise, make no change to the inner header.
81 * ECN_FORBIDDEN (limited functionality):
82 * (I) set the ECN field to not-ECT in the outer header.
84 * (E) if the ECN field in the outer header is set to CE, drop the packet.
85 * otherwise, make no change to the ECN field in the inner header.
87 * the drop rule is for backward compatibility and protection against
92 * modify outer ECN (TOS) field on ingress operation (tunnel encapsulation).
95 ip_ecn_ingress(int mode, u_int8_t *outer, const u_int8_t *inner)
99 panic("NULL pointer passed to ip_ecn_ingress");
103 case ECN_ALLOWED: /* ECN allowed */
105 * full-functionality: if the inner is CE, set ECT(0)
106 * to the outer. otherwise, copy the ECN field.
108 if ((*inner & IPTOS_ECN_MASK) == IPTOS_ECN_CE)
109 *outer &= ~IPTOS_ECN_ECT1;
111 case ECN_FORBIDDEN: /* ECN forbidden */
113 * limited-functionality: set not-ECT to the outer
115 *outer &= ~IPTOS_ECN_MASK;
117 case ECN_NOCARE: /* no consideration to ECN */
123 * modify inner ECN (TOS) field on egress operation (tunnel decapsulation).
124 * the caller should drop the packet if the return value is 0.
127 ip_ecn_egress(int mode, const u_int8_t *outer, u_int8_t *inner)
130 if (!outer || !inner)
131 panic("NULL pointer passed to ip_ecn_egress");
136 * full-functionality: if the outer is CE and the inner is
137 * not-ECT, should drop it. otherwise, copy CE.
139 if ((*outer & IPTOS_ECN_MASK) == IPTOS_ECN_CE) {
140 if ((*inner & IPTOS_ECN_MASK) == IPTOS_ECN_NOTECT)
142 *inner |= IPTOS_ECN_CE;
145 case ECN_FORBIDDEN: /* ECN forbidden */
147 * limited-functionality: if the outer is CE, should drop it.
148 * otherwise, leave the inner.
150 if ((*outer & IPTOS_ECN_MASK) == IPTOS_ECN_CE)
153 case ECN_NOCARE: /* no consideration to ECN */
161 ip6_ecn_ingress(int mode, u_int32_t *outer, const u_int32_t *inner)
163 u_int8_t outer8, inner8;
165 if (!outer || !inner)
166 panic("NULL pointer passed to ip6_ecn_ingress");
168 inner8 = (ntohl(*inner) >> 20) & 0xff;
169 ip_ecn_ingress(mode, &outer8, &inner8);
170 *outer &= ~htonl(0xff << 20);
171 *outer |= htonl((u_int32_t)outer8 << 20);
175 ip6_ecn_egress(int mode, const u_int32_t *outer, u_int32_t *inner)
177 u_int8_t outer8, inner8, oinner8;
179 if (!outer || !inner)
180 panic("NULL pointer passed to ip6_ecn_egress");
182 outer8 = (ntohl(*outer) >> 20) & 0xff;
183 inner8 = oinner8 = (ntohl(*inner) >> 20) & 0xff;
184 if (ip_ecn_egress(mode, &outer8, &inner8) == 0)
186 if (inner8 != oinner8) {
187 *inner &= ~htonl(0xff << 20);
188 *inner |= htonl((u_int32_t)inner8 << 20);