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