Home | History | Annotate | Line # | Download | only in netipsec
ipsec_netbsd.c revision 1.18
      1  1.18  degroote /*	$NetBSD: ipsec_netbsd.c,v 1.18 2007/02/10 09:43:05 degroote Exp $	*/
      2   1.1  jonathan /*	$KAME: esp_input.c,v 1.60 2001/09/04 08:43:19 itojun Exp $	*/
      3   1.1  jonathan /*	$KAME: ah_input.c,v 1.64 2001/09/04 08:43:19 itojun Exp $	*/
      4   1.1  jonathan 
      5   1.1  jonathan /*
      6   1.1  jonathan  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      7   1.1  jonathan  * All rights reserved.
      8   1.1  jonathan  *
      9   1.1  jonathan  * Redistribution and use in source and binary forms, with or without
     10   1.1  jonathan  * modification, are permitted provided that the following conditions
     11   1.1  jonathan  * are met:
     12   1.1  jonathan  * 1. Redistributions of source code must retain the above copyright
     13   1.1  jonathan  *    notice, this list of conditions and the following disclaimer.
     14   1.1  jonathan  * 2. Redistributions in binary form must reproduce the above copyright
     15   1.1  jonathan  *    notice, this list of conditions and the following disclaimer in the
     16   1.1  jonathan  *    documentation and/or other materials provided with the distribution.
     17   1.1  jonathan  * 3. Neither the name of the project nor the names of its contributors
     18   1.1  jonathan  *    may be used to endorse or promote products derived from this software
     19   1.1  jonathan  *    without specific prior written permission.
     20   1.1  jonathan  *
     21   1.1  jonathan  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     22   1.1  jonathan  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23   1.1  jonathan  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24   1.1  jonathan  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     25   1.1  jonathan  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26   1.1  jonathan  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27   1.1  jonathan  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28   1.1  jonathan  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29   1.1  jonathan  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30   1.1  jonathan  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31   1.1  jonathan  * SUCH DAMAGE.
     32   1.1  jonathan  */
     33   1.1  jonathan 
     34   1.1  jonathan #include <sys/cdefs.h>
     35  1.18  degroote __KERNEL_RCSID(0, "$NetBSD: ipsec_netbsd.c,v 1.18 2007/02/10 09:43:05 degroote Exp $");
     36   1.1  jonathan 
     37   1.1  jonathan #include "opt_inet.h"
     38   1.1  jonathan #include "opt_ipsec.h"
     39   1.1  jonathan 
     40   1.1  jonathan #include <sys/param.h>
     41   1.1  jonathan #include <sys/systm.h>
     42   1.1  jonathan #include <sys/malloc.h>
     43   1.1  jonathan #include <sys/mbuf.h>
     44   1.1  jonathan #include <sys/domain.h>
     45   1.1  jonathan #include <sys/protosw.h>
     46   1.1  jonathan #include <sys/socket.h>
     47   1.1  jonathan #include <sys/errno.h>
     48   1.1  jonathan #include <sys/time.h>
     49   1.1  jonathan #include <sys/kernel.h>
     50   1.1  jonathan #include <sys/sysctl.h>
     51   1.1  jonathan 
     52   1.1  jonathan #include <net/if.h>
     53   1.1  jonathan #include <net/route.h>
     54   1.1  jonathan #include <net/netisr.h>
     55   1.1  jonathan #include <machine/cpu.h>
     56   1.1  jonathan 
     57   1.1  jonathan #include <netinet/in.h>
     58   1.1  jonathan #include <netinet/in_systm.h>
     59   1.1  jonathan #include <netinet/in_var.h>
     60   1.1  jonathan #include <netinet/ip.h>
     61   1.1  jonathan #include <netinet/ip_var.h>
     62   1.1  jonathan #include <netinet/ip_ecn.h>
     63   1.1  jonathan #include <netinet/ip_icmp.h>
     64   1.1  jonathan 
     65   1.1  jonathan 
     66   1.1  jonathan #include <netipsec/ipsec.h>
     67  1.10  jonathan #include <netipsec/ipsec_var.h>
     68   1.3       tls #include <netipsec/key.h>
     69   1.3       tls #include <netipsec/keydb.h>
     70   1.3       tls #include <netipsec/key_debug.h>
     71  1.18  degroote #include <netipsec/ah.h>
     72   1.1  jonathan #include <netipsec/ah_var.h>
     73   1.5  jonathan #include <netipsec/esp.h>
     74  1.10  jonathan #include <netipsec/esp_var.h>
     75  1.10  jonathan #include <netipsec/ipip_var.h>
     76  1.10  jonathan #include <netipsec/ipcomp_var.h>
     77   1.5  jonathan 
     78   1.5  jonathan #ifdef INET6
     79   1.5  jonathan #include <netipsec/ipsec6.h>
     80   1.5  jonathan #include <netinet6/ip6protosw.h>
     81   1.5  jonathan #include <netinet/icmp6.h>
     82   1.1  jonathan #endif
     83   1.1  jonathan 
     84   1.1  jonathan #include <machine/stdarg.h>
     85   1.1  jonathan 
     86   1.1  jonathan 
     87   1.1  jonathan 
     88   1.3       tls #include <netipsec/key.h>
     89   1.1  jonathan 
     90   1.1  jonathan /* assumes that ip header and ah header are contiguous on mbuf */
     91   1.1  jonathan void *
     92   1.1  jonathan ah4_ctlinput(cmd, sa, v)
     93   1.1  jonathan 	int cmd;
     94   1.1  jonathan 	struct sockaddr *sa;
     95   1.1  jonathan 	void *v;
     96   1.1  jonathan {
     97   1.1  jonathan 	struct ip *ip = v;
     98   1.1  jonathan 	struct ah *ah;
     99   1.1  jonathan 	struct icmp *icp;
    100   1.1  jonathan 	struct secasvar *sav;
    101   1.1  jonathan 
    102   1.1  jonathan 	if (sa->sa_family != AF_INET ||
    103  1.18  degroote 		sa->sa_len != sizeof(struct sockaddr_in))
    104   1.1  jonathan 		return NULL;
    105   1.1  jonathan 	if ((unsigned)cmd >= PRC_NCMDS)
    106   1.1  jonathan 		return NULL;
    107   1.1  jonathan #ifndef notyet
    108  1.13     perry 	/* jonathan (at) NetBSD.org: XXX FIXME */
    109   1.1  jonathan 	(void) ip; (void) ah; (void) icp; (void) sav;
    110   1.1  jonathan #else
    111   1.1  jonathan 	if (cmd == PRC_MSGSIZE && ip_mtudisc && ip && ip->ip_v == 4) {
    112   1.1  jonathan 		/*
    113   1.1  jonathan 		 * Check to see if we have a valid SA corresponding to
    114   1.1  jonathan 		 * the address in the ICMP message payload.
    115   1.1  jonathan 		 */
    116   1.1  jonathan 		ah = (struct ah *)((caddr_t)ip + (ip->ip_hl << 2));
    117   1.1  jonathan 		if ((sav = key_allocsa(AF_INET,
    118  1.18  degroote 					   (caddr_t) &ip->ip_src,
    119  1.18  degroote 					   (caddr_t) &ip->ip_dst,
    120  1.18  degroote 					   IPPROTO_AH, ah->ah_spi)) == NULL)
    121   1.1  jonathan 			return NULL;
    122   1.1  jonathan 		if (sav->state != SADB_SASTATE_MATURE &&
    123  1.18  degroote 			sav->state != SADB_SASTATE_DYING) {
    124   1.1  jonathan 			key_freesav(sav);
    125   1.1  jonathan 			return NULL;
    126   1.1  jonathan 		}
    127   1.1  jonathan 
    128   1.1  jonathan 		/* XXX Further validation? */
    129   1.1  jonathan 
    130   1.1  jonathan 		key_freesav(sav);
    131   1.1  jonathan 
    132   1.1  jonathan 		/*
    133   1.1  jonathan 		 * Now that we've validated that we are actually communicating
    134   1.1  jonathan 		 * with the host indicated in the ICMP message, locate the
    135   1.1  jonathan 		 * ICMP header, recalculate the new MTU, and create the
    136   1.1  jonathan 		 * corresponding routing entry.
    137   1.1  jonathan 		 */
    138   1.1  jonathan 		icp = (struct icmp *)((caddr_t)ip -
    139  1.18  degroote 			offsetof(struct icmp, icmp_ip));
    140   1.1  jonathan 		icmp_mtudisc(icp, ip->ip_dst);
    141   1.1  jonathan 
    142   1.1  jonathan 		return NULL;
    143   1.1  jonathan 	}
    144   1.1  jonathan #endif
    145   1.1  jonathan 
    146   1.1  jonathan 	return NULL;
    147   1.1  jonathan }
    148   1.1  jonathan 
    149   1.1  jonathan /* assumes that ip header and esp header are contiguous on mbuf */
    150   1.1  jonathan void *
    151   1.1  jonathan esp4_ctlinput(cmd, sa, v)
    152   1.1  jonathan 	int cmd;
    153   1.1  jonathan 	struct sockaddr *sa;
    154   1.1  jonathan 	void *v;
    155   1.1  jonathan {
    156   1.1  jonathan 	struct ip *ip = v;
    157   1.1  jonathan 	struct esp *esp;
    158   1.1  jonathan 	struct icmp *icp;
    159   1.1  jonathan 	struct secasvar *sav;
    160   1.1  jonathan 
    161   1.1  jonathan 	if (sa->sa_family != AF_INET ||
    162   1.1  jonathan 	    sa->sa_len != sizeof(struct sockaddr_in))
    163   1.1  jonathan 		return NULL;
    164   1.1  jonathan 	if ((unsigned)cmd >= PRC_NCMDS)
    165   1.1  jonathan 		return NULL;
    166   1.1  jonathan #ifndef notyet
    167  1.13     perry 	/* jonathan (at) NetBSD.org: XXX FIXME */
    168   1.1  jonathan 	(void) ip; (void) esp; (void) icp; (void) sav;
    169   1.1  jonathan #else
    170   1.1  jonathan 	if (cmd == PRC_MSGSIZE && ip_mtudisc && ip && ip->ip_v == 4) {
    171   1.1  jonathan 		/*
    172   1.1  jonathan 		 * Check to see if we have a valid SA corresponding to
    173   1.1  jonathan 		 * the address in the ICMP message payload.
    174   1.1  jonathan 		 */
    175   1.1  jonathan 		esp = (struct esp *)((caddr_t)ip + (ip->ip_hl << 2));
    176   1.1  jonathan 		if ((sav = key_allocsa(AF_INET,
    177   1.1  jonathan 				       (caddr_t) &ip->ip_src,
    178   1.1  jonathan 				       (caddr_t) &ip->ip_dst,
    179   1.1  jonathan 				       IPPROTO_ESP, esp->esp_spi)) == NULL)
    180   1.1  jonathan 			return NULL;
    181   1.1  jonathan 		if (sav->state != SADB_SASTATE_MATURE &&
    182   1.1  jonathan 		    sav->state != SADB_SASTATE_DYING) {
    183   1.1  jonathan 			key_freesav(sav);
    184   1.1  jonathan 			return NULL;
    185   1.1  jonathan 		}
    186   1.1  jonathan 
    187   1.1  jonathan 		/* XXX Further validation? */
    188   1.1  jonathan 
    189   1.1  jonathan 		key_freesav(sav);
    190   1.1  jonathan 
    191   1.1  jonathan 		/*
    192   1.1  jonathan 		 * Now that we've validated that we are actually communicating
    193   1.1  jonathan 		 * with the host indicated in the ICMP message, locate the
    194   1.1  jonathan 		 * ICMP header, recalculate the new MTU, and create the
    195   1.1  jonathan 		 * corresponding routing entry.
    196   1.1  jonathan 		 */
    197   1.1  jonathan 		icp = (struct icmp *)((caddr_t)ip -
    198   1.1  jonathan 		    offsetof(struct icmp, icmp_ip));
    199   1.1  jonathan 		icmp_mtudisc(icp, ip->ip_dst);
    200   1.1  jonathan 
    201   1.1  jonathan 		return NULL;
    202   1.1  jonathan 	}
    203   1.1  jonathan #endif
    204   1.1  jonathan 
    205   1.1  jonathan 	return NULL;
    206   1.1  jonathan }
    207   1.1  jonathan 
    208   1.1  jonathan #ifdef INET6
    209   1.1  jonathan void
    210  1.18  degroote ah6_ctlinput(cmd, sa, d)
    211  1.18  degroote        int cmd;
    212  1.18  degroote        struct sockaddr *sa;
    213  1.18  degroote        void *d;
    214  1.18  degroote {
    215  1.18  degroote        const struct newah *ahp;
    216  1.18  degroote        struct newah ah;
    217  1.18  degroote        struct secasvar *sav;
    218  1.18  degroote        struct ip6_hdr *ip6;
    219  1.18  degroote        struct mbuf *m;
    220  1.18  degroote        struct ip6ctlparam *ip6cp = NULL;
    221  1.18  degroote        int off;
    222  1.18  degroote 
    223  1.18  degroote        if (sa->sa_family != AF_INET6 ||
    224  1.18  degroote            sa->sa_len != sizeof(struct sockaddr_in6))
    225  1.18  degroote                return;
    226  1.18  degroote        if ((unsigned)cmd >= PRC_NCMDS)
    227  1.18  degroote                return;
    228  1.18  degroote 
    229  1.18  degroote        /* if the parameter is from icmp6, decode it. */
    230  1.18  degroote        if (d != NULL) {
    231  1.18  degroote                ip6cp = (struct ip6ctlparam *)d;
    232  1.18  degroote                m = ip6cp->ip6c_m;
    233  1.18  degroote                ip6 = ip6cp->ip6c_ip6;
    234  1.18  degroote                off = ip6cp->ip6c_off;
    235  1.18  degroote        } else {
    236  1.18  degroote                m = NULL;
    237  1.18  degroote                ip6 = NULL;
    238  1.18  degroote                off = 0;
    239  1.18  degroote        }
    240  1.18  degroote 
    241  1.18  degroote        if (ip6) {
    242  1.18  degroote                /*
    243  1.18  degroote                 * XXX: We assume that when ip6 is non NULL,
    244  1.18  degroote                 * M and OFF are valid.
    245  1.18  degroote                 */
    246  1.18  degroote 
    247  1.18  degroote                /* check if we can safely examine src and dst ports */
    248  1.18  degroote                if (m->m_pkthdr.len < off + sizeof(ah))
    249  1.18  degroote                        return;
    250  1.18  degroote 
    251  1.18  degroote                if (m->m_len < off + sizeof(ah)) {
    252  1.18  degroote                        /*
    253  1.18  degroote                         * this should be rare case,
    254  1.18  degroote                         * so we compromise on this copy...
    255  1.18  degroote                         */
    256  1.18  degroote                        m_copydata(m, off, sizeof(ah), (caddr_t)&ah);
    257  1.18  degroote                        ahp = &ah;
    258  1.18  degroote                } else
    259  1.18  degroote                        ahp = (struct newah *)(mtod(m, caddr_t) + off);
    260  1.18  degroote 
    261  1.18  degroote                if (cmd == PRC_MSGSIZE) {
    262  1.18  degroote                        int valid = 0;
    263  1.18  degroote 
    264  1.18  degroote                        /*
    265  1.18  degroote                         * Check to see if we have a valid SA corresponding
    266  1.18  degroote                         * to the address in the ICMP message payload.
    267  1.18  degroote                         */
    268  1.18  degroote                        sav = KEY_ALLOCSA((union sockaddr_union*)sa,
    269  1.18  degroote                                          IPPROTO_AH, ahp->ah_spi);
    270  1.18  degroote 
    271  1.18  degroote                        if (sav) {
    272  1.18  degroote                                if (sav->state == SADB_SASTATE_MATURE ||
    273  1.18  degroote                                    sav->state == SADB_SASTATE_DYING)
    274  1.18  degroote                                        valid++;
    275  1.18  degroote                                KEY_FREESAV(&sav);
    276  1.18  degroote                        }
    277  1.18  degroote 
    278  1.18  degroote                        /* XXX Further validation? */
    279  1.18  degroote 
    280  1.18  degroote                        /*
    281  1.18  degroote                         * Depending on the value of "valid" and routing
    282  1.18  degroote                         * table size (mtudisc_{hi,lo}wat), we will:
    283  1.18  degroote                         * - recalcurate the new MTU and create the
    284  1.18  degroote                         *   corresponding routing entry, or
    285  1.18  degroote                         * - ignore the MTU change notification.
    286  1.18  degroote                         */
    287  1.18  degroote                        icmp6_mtudisc_update((struct ip6ctlparam *)d,valid);
    288  1.18  degroote                }
    289  1.18  degroote 
    290  1.18  degroote                /* we normally notify single pcb here */
    291  1.18  degroote        } else {
    292  1.18  degroote                /* we normally notify any pcb here */
    293  1.18  degroote        }
    294  1.18  degroote }
    295  1.18  degroote 
    296  1.18  degroote 
    297  1.18  degroote 
    298  1.18  degroote void
    299   1.1  jonathan esp6_ctlinput(cmd, sa, d)
    300   1.1  jonathan 	int cmd;
    301   1.1  jonathan 	struct sockaddr *sa;
    302   1.1  jonathan 	void *d;
    303   1.1  jonathan {
    304   1.1  jonathan 	const struct newesp *espp;
    305   1.1  jonathan 	struct newesp esp;
    306   1.1  jonathan 	struct ip6ctlparam *ip6cp = NULL, ip6cp1;
    307   1.1  jonathan 	struct secasvar *sav;
    308   1.1  jonathan 	struct ip6_hdr *ip6;
    309   1.1  jonathan 	struct mbuf *m;
    310   1.1  jonathan 	int off;
    311   1.1  jonathan 	struct sockaddr_in6 *sa6_src, *sa6_dst;
    312   1.1  jonathan 
    313   1.1  jonathan 	if (sa->sa_family != AF_INET6 ||
    314   1.1  jonathan 	    sa->sa_len != sizeof(struct sockaddr_in6))
    315   1.1  jonathan 		return;
    316   1.1  jonathan 	if ((unsigned)cmd >= PRC_NCMDS)
    317   1.1  jonathan 		return;
    318   1.1  jonathan 
    319   1.1  jonathan 	/* if the parameter is from icmp6, decode it. */
    320   1.1  jonathan 	if (d != NULL) {
    321   1.1  jonathan 		ip6cp = (struct ip6ctlparam *)d;
    322   1.1  jonathan 		m = ip6cp->ip6c_m;
    323   1.1  jonathan 		ip6 = ip6cp->ip6c_ip6;
    324   1.1  jonathan 		off = ip6cp->ip6c_off;
    325   1.1  jonathan 	} else {
    326   1.1  jonathan 		m = NULL;
    327   1.1  jonathan 		ip6 = NULL;
    328   1.7  jonathan 		off = 0;
    329   1.1  jonathan 	}
    330   1.1  jonathan 
    331   1.1  jonathan 	if (ip6) {
    332   1.1  jonathan 		/*
    333   1.1  jonathan 		 * Notify the error to all possible sockets via pfctlinput2.
    334   1.1  jonathan 		 * Since the upper layer information (such as protocol type,
    335   1.1  jonathan 		 * source and destination ports) is embedded in the encrypted
    336   1.1  jonathan 		 * data and might have been cut, we can't directly call
    337   1.1  jonathan 		 * an upper layer ctlinput function. However, the pcbnotify
    338   1.1  jonathan 		 * function will consider source and destination addresses
    339   1.1  jonathan 		 * as well as the flow info value, and may be able to find
    340   1.1  jonathan 		 * some PCB that should be notified.
    341   1.1  jonathan 		 * Although pfctlinput2 will call esp6_ctlinput(), there is
    342   1.1  jonathan 		 * no possibility of an infinite loop of function calls,
    343   1.1  jonathan 		 * because we don't pass the inner IPv6 header.
    344   1.1  jonathan 		 */
    345   1.1  jonathan 		bzero(&ip6cp1, sizeof(ip6cp1));
    346   1.1  jonathan 		ip6cp1.ip6c_src = ip6cp->ip6c_src;
    347   1.1  jonathan 		pfctlinput2(cmd, sa, (void *)&ip6cp1);
    348   1.1  jonathan 
    349   1.1  jonathan 		/*
    350   1.1  jonathan 		 * Then go to special cases that need ESP header information.
    351   1.1  jonathan 		 * XXX: We assume that when ip6 is non NULL,
    352   1.1  jonathan 		 * M and OFF are valid.
    353   1.1  jonathan 		 */
    354   1.1  jonathan 
    355   1.1  jonathan 		/* check if we can safely examine src and dst ports */
    356   1.1  jonathan 		if (m->m_pkthdr.len < off + sizeof(esp))
    357   1.1  jonathan 			return;
    358   1.1  jonathan 
    359   1.1  jonathan 		if (m->m_len < off + sizeof(esp)) {
    360   1.1  jonathan 			/*
    361   1.1  jonathan 			 * this should be rare case,
    362   1.1  jonathan 			 * so we compromise on this copy...
    363   1.1  jonathan 			 */
    364   1.1  jonathan 			m_copydata(m, off, sizeof(esp), (caddr_t)&esp);
    365   1.1  jonathan 			espp = &esp;
    366   1.1  jonathan 		} else
    367   1.1  jonathan 			espp = (struct newesp*)(mtod(m, caddr_t) + off);
    368   1.1  jonathan 
    369   1.1  jonathan 		if (cmd == PRC_MSGSIZE) {
    370   1.1  jonathan 			int valid = 0;
    371   1.1  jonathan 
    372   1.1  jonathan 			/*
    373   1.1  jonathan 			 * Check to see if we have a valid SA corresponding to
    374   1.1  jonathan 			 * the address in the ICMP message payload.
    375   1.1  jonathan 			 */
    376   1.1  jonathan 			sa6_src = ip6cp->ip6c_src;
    377   1.1  jonathan 			sa6_dst = (struct sockaddr_in6 *)sa;
    378   1.7  jonathan #ifdef KAME
    379   1.1  jonathan 			sav = key_allocsa(AF_INET6,
    380   1.1  jonathan 					  (caddr_t)&sa6_src->sin6_addr,
    381   1.1  jonathan 					  (caddr_t)&sa6_dst->sin6_addr,
    382   1.1  jonathan 					  IPPROTO_ESP, espp->esp_spi);
    383   1.7  jonathan #else
    384  1.13     perry 			/* jonathan (at) NetBSD.org: XXX FIXME */
    385   1.7  jonathan 			(void)sa6_src; (void)sa6_dst;
    386   1.7  jonathan 			sav = KEY_ALLOCSA((union sockaddr_union*)sa,
    387   1.7  jonathan 					  IPPROTO_ESP, espp->esp_spi);
    388   1.7  jonathan 
    389   1.7  jonathan #endif
    390   1.1  jonathan 			if (sav) {
    391   1.1  jonathan 				if (sav->state == SADB_SASTATE_MATURE ||
    392   1.1  jonathan 				    sav->state == SADB_SASTATE_DYING)
    393   1.1  jonathan 					valid++;
    394   1.7  jonathan 				KEY_FREESAV(&sav);
    395   1.1  jonathan 			}
    396   1.1  jonathan 
    397   1.1  jonathan 			/* XXX Further validation? */
    398   1.1  jonathan 
    399   1.1  jonathan 			/*
    400   1.1  jonathan 			 * Depending on the value of "valid" and routing table
    401   1.1  jonathan 			 * size (mtudisc_{hi,lo}wat), we will:
    402   1.1  jonathan 			 * - recalcurate the new MTU and create the
    403   1.1  jonathan 			 *   corresponding routing entry, or
    404   1.1  jonathan 			 * - ignore the MTU change notification.
    405   1.1  jonathan 			 */
    406   1.1  jonathan 			icmp6_mtudisc_update((struct ip6ctlparam *)d, valid);
    407   1.1  jonathan 		}
    408   1.1  jonathan 	} else {
    409   1.1  jonathan 		/* we normally notify any pcb here */
    410   1.1  jonathan 	}
    411   1.1  jonathan }
    412   1.1  jonathan #endif /* INET6 */
    413   1.1  jonathan 
    414   1.4    atatat static int
    415   1.4    atatat sysctl_fast_ipsec(SYSCTLFN_ARGS)
    416   1.1  jonathan {
    417   1.4    atatat 	int error, t;
    418   1.4    atatat 	struct sysctlnode node;
    419   1.4    atatat 
    420   1.4    atatat 	node = *rnode;
    421   1.4    atatat 	t = *(int*)rnode->sysctl_data;
    422   1.4    atatat 	node.sysctl_data = &t;
    423   1.4    atatat 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    424   1.4    atatat 	if (error || newp == NULL)
    425   1.4    atatat 		return (error);
    426   1.1  jonathan 
    427   1.4    atatat 	switch (rnode->sysctl_num) {
    428   1.1  jonathan 	case IPSECCTL_DEF_ESP_TRANSLEV:
    429   1.1  jonathan 	case IPSECCTL_DEF_ESP_NETLEV:
    430   1.1  jonathan 	case IPSECCTL_DEF_AH_TRANSLEV:
    431   1.1  jonathan 	case IPSECCTL_DEF_AH_NETLEV:
    432  1.13     perry 		if (t != IPSEC_LEVEL_USE &&
    433   1.4    atatat 		    t != IPSEC_LEVEL_REQUIRE)
    434   1.4    atatat 			return (EINVAL);
    435   1.4    atatat 		ipsec_invalpcbcacheall();
    436   1.1  jonathan 		break;
    437   1.4    atatat       	case IPSECCTL_DEF_POLICY:
    438   1.4    atatat 		if (t != IPSEC_POLICY_DISCARD &&
    439   1.4    atatat 		    t != IPSEC_POLICY_NONE)
    440   1.4    atatat 			return (EINVAL);
    441   1.4    atatat 		ipsec_invalpcbcacheall();
    442   1.1  jonathan 		break;
    443   1.4    atatat 	default:
    444   1.4    atatat 		return (EINVAL);
    445   1.1  jonathan 	}
    446   1.1  jonathan 
    447   1.4    atatat 	*(int*)rnode->sysctl_data = t;
    448   1.4    atatat 
    449   1.4    atatat 	return (0);
    450   1.4    atatat }
    451   1.4    atatat 
    452  1.16    rpaulo #ifdef IPSEC_DEBUG
    453  1.16    rpaulo static int
    454  1.16    rpaulo sysctl_fast_ipsec_test(SYSCTLFN_ARGS)
    455  1.16    rpaulo {
    456  1.16    rpaulo 	int t, error;
    457  1.16    rpaulo 	struct sysctlnode node;
    458  1.16    rpaulo 
    459  1.16    rpaulo 	node = *rnode;
    460  1.16    rpaulo 	t = *(int*)rnode->sysctl_data;
    461  1.16    rpaulo 	node.sysctl_data = &t;
    462  1.16    rpaulo 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    463  1.16    rpaulo 	if (error || newp == NULL)
    464  1.16    rpaulo 		return (error);
    465  1.16    rpaulo 
    466  1.16    rpaulo 	if (t < 0 || t > 1)
    467  1.16    rpaulo 		return EINVAL;
    468  1.16    rpaulo 
    469  1.16    rpaulo 	if (rnode->sysctl_data == &ipsec_replay)
    470  1.16    rpaulo 		printf("fast_ipsec: Anti-Replay service %s\n",
    471  1.16    rpaulo 		    (t == 1) ? "deactivated" : "activated");
    472  1.16    rpaulo 	else if (rnode->sysctl_data == &ipsec_integrity)
    473  1.16    rpaulo 		 printf("fast_ipsec: HMAC corruption %s\n",
    474  1.16    rpaulo 		     (t == 0) ? "deactivated" : "activated");
    475  1.16    rpaulo 
    476  1.16    rpaulo 	*(int*)rnode->sysctl_data = t;
    477  1.16    rpaulo 
    478  1.16    rpaulo 	return 0;
    479  1.16    rpaulo }
    480  1.16    rpaulo #endif
    481  1.16    rpaulo 
    482   1.4    atatat /* XXX will need a different oid at parent */
    483  1.10  jonathan SYSCTL_SETUP(sysctl_net_inet_fast_ipsec_setup, "sysctl net.inet.ipsec subtree setup")
    484   1.4    atatat {
    485  1.14    atatat 	const struct sysctlnode *_ipsec;
    486  1.11    atatat 	int ipproto_ipsec;
    487   1.4    atatat 
    488   1.8    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    489   1.8    atatat 		       CTLFLAG_PERMANENT,
    490   1.4    atatat 		       CTLTYPE_NODE, "net", NULL,
    491   1.4    atatat 		       NULL, 0, NULL, 0,
    492   1.4    atatat 		       CTL_NET, CTL_EOL);
    493   1.8    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    494   1.8    atatat 		       CTLFLAG_PERMANENT,
    495   1.4    atatat 		       CTLTYPE_NODE, "inet", NULL,
    496   1.4    atatat 		       NULL, 0, NULL, 0,
    497   1.4    atatat 		       CTL_NET, PF_INET, CTL_EOL);
    498  1.10  jonathan 
    499  1.11    atatat 	/*
    500  1.11    atatat 	 * in numerical order:
    501  1.11    atatat 	 *
    502  1.11    atatat 	 * net.inet.ipip:	CTL_NET.PF_INET.IPPROTO_IPIP
    503  1.11    atatat 	 * net.inet.esp:	CTL_NET.PF_INET.IPPROTO_ESP
    504  1.11    atatat 	 * net.inet.ah:		CTL_NET.PF_INET.IPPROTO_AH
    505  1.11    atatat 	 * net.inet.ipcomp:	CTL_NET.PF_INET.IPPROTO_IPCOMP
    506  1.11    atatat 	 * net.inet.ipsec:	CTL_NET.PF_INET.CTL_CREATE
    507  1.11    atatat 	 *
    508  1.11    atatat 	 * this creates separate trees by name, but maintains that the
    509  1.11    atatat 	 * ipsec name leads to all the old leaves.
    510  1.11    atatat 	 */
    511  1.11    atatat 
    512  1.11    atatat 	/* create net.inet.ipip */
    513   1.8    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    514   1.8    atatat 		       CTLFLAG_PERMANENT,
    515  1.11    atatat 		       CTLTYPE_NODE, "ipip", NULL,
    516   1.4    atatat 		       NULL, 0, NULL, 0,
    517  1.11    atatat 		       CTL_NET, PF_INET, IPPROTO_IPIP, CTL_EOL);
    518  1.11    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    519  1.11    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READONLY,
    520  1.11    atatat 		       CTLTYPE_STRUCT, "ipip_stats", NULL,
    521  1.11    atatat 		       NULL, 0, &ipipstat, sizeof(ipipstat),
    522  1.11    atatat 		       CTL_NET, PF_INET, IPPROTO_IPIP,
    523  1.11    atatat 		       CTL_CREATE, CTL_EOL);
    524   1.4    atatat 
    525  1.11    atatat 	/* create net.inet.esp subtree under IPPROTO_ESP */
    526  1.11    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    527  1.11    atatat 		       CTLFLAG_PERMANENT,
    528  1.11    atatat 		       CTLTYPE_NODE, "esp", NULL,
    529  1.11    atatat 		       NULL, 0, NULL, 0,
    530  1.11    atatat 		       CTL_NET, PF_INET, IPPROTO_ESP, CTL_EOL);
    531  1.11    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    532  1.11    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    533  1.11    atatat 		       CTLTYPE_INT, "trans_deflev", NULL,
    534  1.11    atatat 		       sysctl_fast_ipsec, 0, &ip4_esp_trans_deflev, 0,
    535  1.11    atatat 		       CTL_NET, PF_INET, IPPROTO_ESP,
    536  1.11    atatat 		       IPSECCTL_DEF_ESP_TRANSLEV, CTL_EOL);
    537  1.11    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    538  1.11    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    539  1.11    atatat 		       CTLTYPE_INT, "net_deflev", NULL,
    540  1.11    atatat 		       sysctl_fast_ipsec, 0, &ip4_esp_net_deflev, 0,
    541  1.11    atatat 		       CTL_NET, PF_INET, IPPROTO_ESP,
    542  1.11    atatat 		       IPSECCTL_DEF_ESP_NETLEV, CTL_EOL);
    543   1.8    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    544  1.10  jonathan 		       CTLFLAG_PERMANENT|CTLFLAG_READONLY,
    545  1.11    atatat 		       CTLTYPE_STRUCT, "esp_stats", NULL,
    546  1.11    atatat 		       NULL, 0, &espstat, sizeof(espstat),
    547  1.11    atatat 		       CTL_NET, PF_INET, IPPROTO_ESP,
    548  1.11    atatat 		       CTL_CREATE, CTL_EOL);
    549  1.10  jonathan 
    550  1.11    atatat 	/* create net.inet.ah subtree under IPPROTO_AH */
    551  1.11    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    552  1.11    atatat 		       CTLFLAG_PERMANENT,
    553  1.11    atatat 		       CTLTYPE_NODE, "ah", NULL,
    554  1.11    atatat 		       NULL, 0, NULL, 0,
    555  1.11    atatat 		       CTL_NET, PF_INET, IPPROTO_AH, CTL_EOL);
    556   1.8    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    557   1.8    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    558  1.10  jonathan 		       CTLTYPE_INT, "cleartos", NULL,
    559  1.10  jonathan 		       NULL, 0, &/*ip4_*/ah_cleartos, 0,
    560   1.4    atatat 		       CTL_NET, PF_INET, IPPROTO_AH,
    561  1.10  jonathan 		       IPSECCTL_AH_CLEARTOS, CTL_EOL);
    562   1.8    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    563   1.8    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    564  1.10  jonathan 		       CTLTYPE_INT, "offsetmask", NULL,
    565  1.10  jonathan 		       NULL, 0, &ip4_ah_offsetmask, 0,
    566   1.4    atatat 		       CTL_NET, PF_INET, IPPROTO_AH,
    567  1.10  jonathan 		       IPSECCTL_AH_OFFSETMASK, CTL_EOL);
    568   1.8    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    569   1.8    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    570  1.10  jonathan 		       CTLTYPE_INT, "trans_deflev", NULL,
    571   1.4    atatat 		       sysctl_fast_ipsec, 0, &ip4_ah_trans_deflev, 0,
    572  1.11    atatat 		       CTL_NET, PF_INET, IPPROTO_AH,
    573   1.4    atatat 		       IPSECCTL_DEF_AH_TRANSLEV, CTL_EOL);
    574   1.8    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    575   1.8    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    576  1.10  jonathan 		       CTLTYPE_INT, "net_deflev", NULL,
    577   1.4    atatat 		       sysctl_fast_ipsec, 0, &ip4_ah_net_deflev, 0,
    578  1.11    atatat 		       CTL_NET, PF_INET, IPPROTO_AH,
    579   1.4    atatat 		       IPSECCTL_DEF_AH_NETLEV, CTL_EOL);
    580  1.10  jonathan 	sysctl_createv(clog, 0, NULL, NULL,
    581  1.10  jonathan 		       CTLFLAG_PERMANENT|CTLFLAG_READONLY,
    582  1.11    atatat 		       CTLTYPE_STRUCT, "ah_stats", NULL,
    583  1.11    atatat 		       NULL, 0, &ahstat, sizeof(ahstat),
    584  1.11    atatat 		       CTL_NET, PF_INET, IPPROTO_AH,
    585  1.11    atatat 		       CTL_CREATE, CTL_EOL);
    586  1.10  jonathan 
    587  1.10  jonathan 	/* create net.inet.ipcomp */
    588  1.10  jonathan 	sysctl_createv(clog, 0, NULL, NULL,
    589  1.10  jonathan 		       CTLFLAG_PERMANENT,
    590  1.10  jonathan 		       CTLTYPE_NODE, "ipcomp", NULL,
    591  1.10  jonathan 		       NULL, 0, NULL, 0,
    592  1.10  jonathan 		       CTL_NET, PF_INET, IPPROTO_IPCOMP, CTL_EOL);
    593  1.10  jonathan 	sysctl_createv(clog, 0, NULL, NULL,
    594  1.10  jonathan 		       CTLFLAG_PERMANENT|CTLFLAG_READONLY,
    595  1.11    atatat 		       CTLTYPE_STRUCT, "ipcomp_stats", NULL,
    596  1.10  jonathan 		       NULL, 0, &ipcompstat, sizeof(ipcompstat),
    597  1.10  jonathan 		       CTL_NET, PF_INET, IPPROTO_IPCOMP,
    598  1.11    atatat 		       CTL_CREATE, CTL_EOL);
    599  1.10  jonathan 
    600  1.11    atatat 	/* create net.inet.ipsec subtree under dynamic oid */
    601  1.11    atatat 	sysctl_createv(clog, 0, NULL, &_ipsec,
    602  1.10  jonathan 		       CTLFLAG_PERMANENT,
    603  1.10  jonathan 		       CTLTYPE_NODE, "ipsec", NULL,
    604  1.10  jonathan 		       NULL, 0, NULL, 0,
    605  1.11    atatat 		       CTL_NET, PF_INET, CTL_CREATE, CTL_EOL);
    606  1.11    atatat 	ipproto_ipsec = (_ipsec != NULL) ? _ipsec->sysctl_num : 0;
    607  1.10  jonathan 
    608   1.8    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    609   1.8    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    610  1.10  jonathan 		       CTLTYPE_INT, "def_policy", NULL,
    611  1.10  jonathan 		       sysctl_fast_ipsec, 0, &ip4_def_policy.policy, 0,
    612  1.11    atatat 		       CTL_NET, PF_INET, ipproto_ipsec,
    613  1.10  jonathan 		       IPSECCTL_DEF_POLICY, CTL_EOL);
    614   1.8    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    615   1.8    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    616  1.11    atatat 		       CTLTYPE_INT, "esp_trans_deflev", NULL,
    617  1.11    atatat 		       sysctl_fast_ipsec, 0, &ip4_esp_trans_deflev, 0,
    618  1.11    atatat 		       CTL_NET, PF_INET, ipproto_ipsec,
    619  1.11    atatat 		       IPSECCTL_DEF_ESP_TRANSLEV, CTL_EOL);
    620  1.11    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    621  1.11    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    622  1.11    atatat 		       CTLTYPE_INT, "esp_net_deflev", NULL,
    623  1.11    atatat 		       sysctl_fast_ipsec, 0, &ip4_esp_net_deflev, 0,
    624  1.11    atatat 		       CTL_NET, PF_INET, ipproto_ipsec,
    625  1.11    atatat 		       IPSECCTL_DEF_ESP_NETLEV, CTL_EOL);
    626  1.11    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    627  1.11    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    628  1.11    atatat 		       CTLTYPE_INT, "ah_trans_deflev", NULL,
    629  1.11    atatat 		       sysctl_fast_ipsec, 0, &ip4_ah_trans_deflev, 0,
    630  1.11    atatat 		       CTL_NET, PF_INET, ipproto_ipsec,
    631  1.11    atatat 		       IPSECCTL_DEF_AH_TRANSLEV, CTL_EOL);
    632  1.11    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    633  1.11    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    634  1.11    atatat 		       CTLTYPE_INT, "ah_net_deflev", NULL,
    635  1.11    atatat 		       sysctl_fast_ipsec, 0, &ip4_ah_net_deflev, 0,
    636  1.11    atatat 		       CTL_NET, PF_INET, ipproto_ipsec,
    637  1.11    atatat 		       IPSECCTL_DEF_AH_NETLEV, CTL_EOL);
    638  1.11    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    639  1.11    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    640  1.11    atatat 		       CTLTYPE_INT, "ah_cleartos", NULL,
    641  1.11    atatat 		       NULL, 0, &/*ip4_*/ah_cleartos, 0,
    642  1.11    atatat 		       CTL_NET, PF_INET, ipproto_ipsec,
    643  1.11    atatat 		       IPSECCTL_AH_CLEARTOS, CTL_EOL);
    644  1.11    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    645  1.11    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    646  1.11    atatat 		       CTLTYPE_INT, "ah_offsetmask", NULL,
    647  1.11    atatat 		       NULL, 0, &ip4_ah_offsetmask, 0,
    648  1.11    atatat 		       CTL_NET, PF_INET, ipproto_ipsec,
    649  1.11    atatat 		       IPSECCTL_AH_OFFSETMASK, CTL_EOL);
    650  1.11    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    651  1.11    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    652   1.4    atatat 		       CTLTYPE_INT, "dfbit", NULL,
    653   1.4    atatat 		       NULL, 0, &ip4_ipsec_dfbit, 0,
    654  1.11    atatat 		       CTL_NET, PF_INET, ipproto_ipsec,
    655   1.4    atatat 		       IPSECCTL_DFBIT, CTL_EOL);
    656   1.8    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    657   1.8    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    658   1.4    atatat 		       CTLTYPE_INT, "ecn", NULL,
    659   1.4    atatat 		       NULL, 0, &ip4_ipsec_ecn, 0,
    660  1.11    atatat 		       CTL_NET, PF_INET, ipproto_ipsec,
    661   1.4    atatat 		       IPSECCTL_ECN, CTL_EOL);
    662   1.8    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    663   1.8    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    664   1.4    atatat 		       CTLTYPE_INT, "debug", NULL,
    665   1.4    atatat 		       NULL, 0, &ipsec_debug, 0,
    666  1.11    atatat 		       CTL_NET, PF_INET, ipproto_ipsec,
    667   1.4    atatat 		       IPSECCTL_DEBUG, CTL_EOL);
    668   1.8    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    669  1.11    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READONLY,
    670  1.11    atatat 		       CTLTYPE_STRUCT, "ipsecstats", NULL,
    671  1.11    atatat 		       NULL, 0, &ipsecstat, sizeof(ipsecstat),
    672  1.11    atatat 		       CTL_NET, PF_INET, ipproto_ipsec,
    673  1.11    atatat 		       CTL_CREATE, CTL_EOL);
    674  1.16    rpaulo #ifdef IPSEC_DEBUG
    675  1.16    rpaulo 	sysctl_createv(clog, 0, NULL, NULL,
    676  1.16    rpaulo 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    677  1.16    rpaulo 		       CTLTYPE_INT, "test_replay",
    678  1.16    rpaulo 		       SYSCTL_DESCR("Emulate replay attack"),
    679  1.16    rpaulo 		       sysctl_fast_ipsec_test, 0, &ipsec_replay, 0,
    680  1.16    rpaulo 		       CTL_NET, PF_INET, ipproto_ipsec,
    681  1.16    rpaulo 		       CTL_CREATE, CTL_EOL);
    682  1.16    rpaulo 	sysctl_createv(clog, 0, NULL, NULL,
    683  1.16    rpaulo 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    684  1.16    rpaulo 		       CTLTYPE_INT, "test_integrity",
    685  1.16    rpaulo 		       SYSCTL_DESCR("Emulate man-in-the-middle attack"),
    686  1.16    rpaulo 		       sysctl_fast_ipsec_test, 0, &ipsec_integrity, 0,
    687  1.16    rpaulo 		       CTL_NET, PF_INET, ipproto_ipsec,
    688  1.16    rpaulo 		       CTL_CREATE, CTL_EOL);
    689  1.16    rpaulo #endif
    690   1.1  jonathan }
    691