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