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