ip_ecn.c revision 1.2.2.2 1 1.2.2.2 thorpej /*
2 1.2.2.2 thorpej * Copyright (C) 1999 WIDE Project.
3 1.2.2.2 thorpej * All rights reserved.
4 1.2.2.2 thorpej *
5 1.2.2.2 thorpej * Redistribution and use in source and binary forms, with or without
6 1.2.2.2 thorpej * modification, are permitted provided that the following conditions
7 1.2.2.2 thorpej * are met:
8 1.2.2.2 thorpej * 1. Redistributions of source code must retain the above copyright
9 1.2.2.2 thorpej * notice, this list of conditions and the following disclaimer.
10 1.2.2.2 thorpej * 2. Redistributions in binary form must reproduce the above copyright
11 1.2.2.2 thorpej * notice, this list of conditions and the following disclaimer in the
12 1.2.2.2 thorpej * documentation and/or other materials provided with the distribution.
13 1.2.2.2 thorpej * 3. Neither the name of the project nor the names of its contributors
14 1.2.2.2 thorpej * may be used to endorse or promote products derived from this software
15 1.2.2.2 thorpej * without specific prior written permission.
16 1.2.2.2 thorpej *
17 1.2.2.2 thorpej * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 1.2.2.2 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 1.2.2.2 thorpej * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 1.2.2.2 thorpej * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 1.2.2.2 thorpej * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 1.2.2.2 thorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 1.2.2.2 thorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 1.2.2.2 thorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 1.2.2.2 thorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 1.2.2.2 thorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 1.2.2.2 thorpej * SUCH DAMAGE.
28 1.2.2.2 thorpej *
29 1.2.2.2 thorpej * $Id: ip_ecn.c,v 1.2.2.2 1999/07/01 23:47:01 thorpej Exp $
30 1.2.2.2 thorpej */
31 1.2.2.2 thorpej /*
32 1.2.2.2 thorpej * ECN consideration on tunnel ingress/egress operation.
33 1.2.2.2 thorpej * http://www.aciri.org/floyd/papers/draft-ipsec-ecn-00.txt
34 1.2.2.2 thorpej */
35 1.2.2.2 thorpej
36 1.2.2.2 thorpej #if (defined(__FreeBSD__) && __FreeBSD__ >= 3) || defined(__NetBSD__)
37 1.2.2.2 thorpej #include "opt_inet.h"
38 1.2.2.2 thorpej #endif
39 1.2.2.2 thorpej
40 1.2.2.2 thorpej #include <sys/param.h>
41 1.2.2.2 thorpej #include <sys/systm.h>
42 1.2.2.2 thorpej #include <sys/malloc.h>
43 1.2.2.2 thorpej #include <sys/mbuf.h>
44 1.2.2.2 thorpej #include <sys/errno.h>
45 1.2.2.2 thorpej
46 1.2.2.2 thorpej #ifdef INET
47 1.2.2.2 thorpej #include <netinet/in.h>
48 1.2.2.2 thorpej #include <netinet/in_systm.h>
49 1.2.2.2 thorpej #include <netinet/ip.h>
50 1.2.2.2 thorpej #endif
51 1.2.2.2 thorpej
52 1.2.2.2 thorpej #ifdef INET6
53 1.2.2.2 thorpej #ifndef INET
54 1.2.2.2 thorpej #include <netinet/in.h>
55 1.2.2.2 thorpej #endif
56 1.2.2.2 thorpej #include <netinet6/in6_systm.h>
57 1.2.2.2 thorpej #include <netinet/ip6.h>
58 1.2.2.2 thorpej #endif
59 1.2.2.2 thorpej
60 1.2.2.2 thorpej #include <netinet/ip_ecn.h>
61 1.2.2.2 thorpej
62 1.2.2.2 thorpej /*
63 1.2.2.2 thorpej * modify outer ECN (TOS) field on ingress operation (tunnel encapsulation).
64 1.2.2.2 thorpej * call it after you've done the default initialization/copy for the outer.
65 1.2.2.2 thorpej */
66 1.2.2.2 thorpej void
67 1.2.2.2 thorpej ip_ecn_ingress(mode, outer, inner)
68 1.2.2.2 thorpej int mode;
69 1.2.2.2 thorpej u_int8_t *outer;
70 1.2.2.2 thorpej u_int8_t *inner;
71 1.2.2.2 thorpej {
72 1.2.2.2 thorpej if (!outer || !inner)
73 1.2.2.2 thorpej panic("NULL pointer passed to ip_ecn_ingress");
74 1.2.2.2 thorpej
75 1.2.2.2 thorpej switch (mode) {
76 1.2.2.2 thorpej case ECN_ALLOWED: /* ECN allowed */
77 1.2.2.2 thorpej *outer &= ~IPTOS_CE;
78 1.2.2.2 thorpej break;
79 1.2.2.2 thorpej case ECN_FORBIDDEN: /* ECN forbidden */
80 1.2.2.2 thorpej *outer &= ~(IPTOS_ECT | IPTOS_CE);
81 1.2.2.2 thorpej break;
82 1.2.2.2 thorpej case ECN_NOCARE: /* no consideration to ECN */
83 1.2.2.2 thorpej break;
84 1.2.2.2 thorpej }
85 1.2.2.2 thorpej }
86 1.2.2.2 thorpej
87 1.2.2.2 thorpej /*
88 1.2.2.2 thorpej * modify inner ECN (TOS) field on egress operation (tunnel decapsulation).
89 1.2.2.2 thorpej * call it after you've done the default initialization/copy for the inner.
90 1.2.2.2 thorpej */
91 1.2.2.2 thorpej void
92 1.2.2.2 thorpej ip_ecn_egress(mode, outer, inner)
93 1.2.2.2 thorpej int mode;
94 1.2.2.2 thorpej u_int8_t *outer;
95 1.2.2.2 thorpej u_int8_t *inner;
96 1.2.2.2 thorpej {
97 1.2.2.2 thorpej if (!outer || !inner)
98 1.2.2.2 thorpej panic("NULL pointer passed to ip_ecn_egress");
99 1.2.2.2 thorpej
100 1.2.2.2 thorpej switch (mode) {
101 1.2.2.2 thorpej case ECN_ALLOWED:
102 1.2.2.2 thorpej if (*outer & IPTOS_CE)
103 1.2.2.2 thorpej *inner |= IPTOS_CE;
104 1.2.2.2 thorpej break;
105 1.2.2.2 thorpej case ECN_FORBIDDEN: /* ECN forbidden */
106 1.2.2.2 thorpej case ECN_NOCARE: /* no consideration to ECN */
107 1.2.2.2 thorpej break;
108 1.2.2.2 thorpej }
109 1.2.2.2 thorpej }
110 1.2.2.2 thorpej
111 1.2.2.2 thorpej #ifdef INET6
112 1.2.2.2 thorpej void
113 1.2.2.2 thorpej ip6_ecn_ingress(mode, outer, inner)
114 1.2.2.2 thorpej int mode;
115 1.2.2.2 thorpej u_int32_t *outer;
116 1.2.2.2 thorpej u_int32_t *inner;
117 1.2.2.2 thorpej {
118 1.2.2.2 thorpej u_int8_t outer8, inner8;
119 1.2.2.2 thorpej
120 1.2.2.2 thorpej if (!outer || !inner)
121 1.2.2.2 thorpej panic("NULL pointer passed to ip6_ecn_ingress");
122 1.2.2.2 thorpej
123 1.2.2.2 thorpej outer8 = (ntohl(*outer) >> 20) & 0xff;
124 1.2.2.2 thorpej inner8 = (ntohl(*inner) >> 20) & 0xff;
125 1.2.2.2 thorpej ip_ecn_ingress(mode, &outer8, &inner8);
126 1.2.2.2 thorpej *outer &= ~htonl(0xff << 20);
127 1.2.2.2 thorpej *outer |= htonl((u_int32_t)outer8 << 20);
128 1.2.2.2 thorpej }
129 1.2.2.2 thorpej
130 1.2.2.2 thorpej void
131 1.2.2.2 thorpej ip6_ecn_egress(mode, outer, inner)
132 1.2.2.2 thorpej int mode;
133 1.2.2.2 thorpej u_int32_t *outer;
134 1.2.2.2 thorpej u_int32_t *inner;
135 1.2.2.2 thorpej {
136 1.2.2.2 thorpej u_int8_t outer8, inner8;
137 1.2.2.2 thorpej
138 1.2.2.2 thorpej if (!outer || !inner)
139 1.2.2.2 thorpej panic("NULL pointer passed to ip6_ecn_egress");
140 1.2.2.2 thorpej
141 1.2.2.2 thorpej outer8 = (ntohl(*outer) >> 20) & 0xff;
142 1.2.2.2 thorpej inner8 = (ntohl(*inner) >> 20) & 0xff;
143 1.2.2.2 thorpej ip_ecn_egress(mode, &outer8, &inner8);
144 1.2.2.2 thorpej *inner &= ~htonl(0xff << 20);
145 1.2.2.2 thorpej *inner |= htonl((u_int32_t)inner8 << 20);
146 1.2.2.2 thorpej }
147 1.2.2.2 thorpej #endif
148