Home | History | Annotate | Line # | Download | only in netipsec
ipsec_netbsd.c revision 1.4
      1 /*	$NetBSD: ipsec_netbsd.c,v 1.4 2003/12/04 19:38:25 atatat Exp $	*/
      2 /*	$KAME: esp_input.c,v 1.60 2001/09/04 08:43:19 itojun Exp $	*/
      3 /*	$KAME: ah_input.c,v 1.64 2001/09/04 08:43:19 itojun Exp $	*/
      4 
      5 /*
      6  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      7  * All rights reserved.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. Neither the name of the project nor the names of its contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 __KERNEL_RCSID(0, "$NetBSD: ipsec_netbsd.c,v 1.4 2003/12/04 19:38:25 atatat Exp $");
     36 
     37 #include "opt_inet.h"
     38 #include "opt_ipsec.h"
     39 
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/malloc.h>
     43 #include <sys/mbuf.h>
     44 #include <sys/domain.h>
     45 #include <sys/protosw.h>
     46 #include <sys/socket.h>
     47 #include <sys/errno.h>
     48 #include <sys/time.h>
     49 #include <sys/kernel.h>
     50 #include <sys/sysctl.h>
     51 
     52 #include <net/if.h>
     53 #include <net/route.h>
     54 #include <net/netisr.h>
     55 #include <machine/cpu.h>
     56 
     57 #include <netinet/in.h>
     58 #include <netinet/in_systm.h>
     59 #include <netinet/in_var.h>
     60 #include <netinet/ip.h>
     61 #include <netinet/ip_var.h>
     62 #include <netinet/ip_ecn.h>
     63 #include <netinet/ip_icmp.h>
     64 
     65 #ifdef IPSEC
     66 #include <netkey/key.h>
     67 #include <netkey/keydb.h>
     68 #include <netkey/key_debug.h>
     69 #endif
     70 
     71 #ifdef FAST_IPSEC
     72 #include <netipsec/ipsec.h>
     73 #include <netipsec/key.h>
     74 #include <netipsec/keydb.h>
     75 #include <netipsec/key_debug.h>
     76 #include <netipsec/ah_var.h>
     77 #endif
     78 
     79 
     80 #include <machine/stdarg.h>
     81 
     82 
     83 
     84 #include <netipsec/key.h>
     85 
     86 /* assumes that ip header and ah header are contiguous on mbuf */
     87 void *
     88 ah4_ctlinput(cmd, sa, v)
     89 	int cmd;
     90 	struct sockaddr *sa;
     91 	void *v;
     92 {
     93 	struct ip *ip = v;
     94 	struct ah *ah;
     95 	struct icmp *icp;
     96 	struct secasvar *sav;
     97 
     98 	if (sa->sa_family != AF_INET ||
     99 	    sa->sa_len != sizeof(struct sockaddr_in))
    100 		return NULL;
    101 	if ((unsigned)cmd >= PRC_NCMDS)
    102 		return NULL;
    103 #ifndef notyet
    104 	(void) ip; (void) ah; (void) icp; (void) sav;
    105 #else
    106 	if (cmd == PRC_MSGSIZE && ip_mtudisc && ip && ip->ip_v == 4) {
    107 		/*
    108 		 * Check to see if we have a valid SA corresponding to
    109 		 * the address in the ICMP message payload.
    110 		 */
    111 		ah = (struct ah *)((caddr_t)ip + (ip->ip_hl << 2));
    112 		if ((sav = key_allocsa(AF_INET,
    113 				       (caddr_t) &ip->ip_src,
    114 				       (caddr_t) &ip->ip_dst,
    115 				       IPPROTO_AH, ah->ah_spi)) == NULL)
    116 			return NULL;
    117 		if (sav->state != SADB_SASTATE_MATURE &&
    118 		    sav->state != SADB_SASTATE_DYING) {
    119 			key_freesav(sav);
    120 			return NULL;
    121 		}
    122 
    123 		/* XXX Further validation? */
    124 
    125 		key_freesav(sav);
    126 
    127 		/*
    128 		 * Now that we've validated that we are actually communicating
    129 		 * with the host indicated in the ICMP message, locate the
    130 		 * ICMP header, recalculate the new MTU, and create the
    131 		 * corresponding routing entry.
    132 		 */
    133 		icp = (struct icmp *)((caddr_t)ip -
    134 		    offsetof(struct icmp, icmp_ip));
    135 		icmp_mtudisc(icp, ip->ip_dst);
    136 
    137 		return NULL;
    138 	}
    139 #endif
    140 
    141 	return NULL;
    142 }
    143 
    144 /* assumes that ip header and esp header are contiguous on mbuf */
    145 void *
    146 esp4_ctlinput(cmd, sa, v)
    147 	int cmd;
    148 	struct sockaddr *sa;
    149 	void *v;
    150 {
    151 	struct ip *ip = v;
    152 	struct esp *esp;
    153 	struct icmp *icp;
    154 	struct secasvar *sav;
    155 
    156 	if (sa->sa_family != AF_INET ||
    157 	    sa->sa_len != sizeof(struct sockaddr_in))
    158 		return NULL;
    159 	if ((unsigned)cmd >= PRC_NCMDS)
    160 		return NULL;
    161 #ifndef notyet
    162 	(void) ip; (void) esp; (void) icp; (void) sav;
    163 #else
    164 	if (cmd == PRC_MSGSIZE && ip_mtudisc && ip && ip->ip_v == 4) {
    165 		/*
    166 		 * Check to see if we have a valid SA corresponding to
    167 		 * the address in the ICMP message payload.
    168 		 */
    169 		esp = (struct esp *)((caddr_t)ip + (ip->ip_hl << 2));
    170 		if ((sav = key_allocsa(AF_INET,
    171 				       (caddr_t) &ip->ip_src,
    172 				       (caddr_t) &ip->ip_dst,
    173 				       IPPROTO_ESP, esp->esp_spi)) == NULL)
    174 			return NULL;
    175 		if (sav->state != SADB_SASTATE_MATURE &&
    176 		    sav->state != SADB_SASTATE_DYING) {
    177 			key_freesav(sav);
    178 			return NULL;
    179 		}
    180 
    181 		/* XXX Further validation? */
    182 
    183 		key_freesav(sav);
    184 
    185 		/*
    186 		 * Now that we've validated that we are actually communicating
    187 		 * with the host indicated in the ICMP message, locate the
    188 		 * ICMP header, recalculate the new MTU, and create the
    189 		 * corresponding routing entry.
    190 		 */
    191 		icp = (struct icmp *)((caddr_t)ip -
    192 		    offsetof(struct icmp, icmp_ip));
    193 		icmp_mtudisc(icp, ip->ip_dst);
    194 
    195 		return NULL;
    196 	}
    197 #endif
    198 
    199 	return NULL;
    200 }
    201 
    202 #ifdef INET6
    203 void
    204 esp6_ctlinput(cmd, sa, d)
    205 	int cmd;
    206 	struct sockaddr *sa;
    207 	void *d;
    208 {
    209 	const struct newesp *espp;
    210 	struct newesp esp;
    211 	struct ip6ctlparam *ip6cp = NULL, ip6cp1;
    212 	struct secasvar *sav;
    213 	struct ip6_hdr *ip6;
    214 	struct mbuf *m;
    215 	int off;
    216 	struct sockaddr_in6 *sa6_src, *sa6_dst;
    217 
    218 	if (sa->sa_family != AF_INET6 ||
    219 	    sa->sa_len != sizeof(struct sockaddr_in6))
    220 		return;
    221 	if ((unsigned)cmd >= PRC_NCMDS)
    222 		return;
    223 
    224 	/* if the parameter is from icmp6, decode it. */
    225 	if (d != NULL) {
    226 		ip6cp = (struct ip6ctlparam *)d;
    227 		m = ip6cp->ip6c_m;
    228 		ip6 = ip6cp->ip6c_ip6;
    229 		off = ip6cp->ip6c_off;
    230 	} else {
    231 		m = NULL;
    232 		ip6 = NULL;
    233 	}
    234 
    235 	if (ip6) {
    236 		/*
    237 		 * Notify the error to all possible sockets via pfctlinput2.
    238 		 * Since the upper layer information (such as protocol type,
    239 		 * source and destination ports) is embedded in the encrypted
    240 		 * data and might have been cut, we can't directly call
    241 		 * an upper layer ctlinput function. However, the pcbnotify
    242 		 * function will consider source and destination addresses
    243 		 * as well as the flow info value, and may be able to find
    244 		 * some PCB that should be notified.
    245 		 * Although pfctlinput2 will call esp6_ctlinput(), there is
    246 		 * no possibility of an infinite loop of function calls,
    247 		 * because we don't pass the inner IPv6 header.
    248 		 */
    249 		bzero(&ip6cp1, sizeof(ip6cp1));
    250 		ip6cp1.ip6c_src = ip6cp->ip6c_src;
    251 		pfctlinput2(cmd, sa, (void *)&ip6cp1);
    252 
    253 		/*
    254 		 * Then go to special cases that need ESP header information.
    255 		 * XXX: We assume that when ip6 is non NULL,
    256 		 * M and OFF are valid.
    257 		 */
    258 
    259 		/* check if we can safely examine src and dst ports */
    260 		if (m->m_pkthdr.len < off + sizeof(esp))
    261 			return;
    262 
    263 		if (m->m_len < off + sizeof(esp)) {
    264 			/*
    265 			 * this should be rare case,
    266 			 * so we compromise on this copy...
    267 			 */
    268 			m_copydata(m, off, sizeof(esp), (caddr_t)&esp);
    269 			espp = &esp;
    270 		} else
    271 			espp = (struct newesp*)(mtod(m, caddr_t) + off);
    272 
    273 		if (cmd == PRC_MSGSIZE) {
    274 			int valid = 0;
    275 
    276 			/*
    277 			 * Check to see if we have a valid SA corresponding to
    278 			 * the address in the ICMP message payload.
    279 			 */
    280 			sa6_src = ip6cp->ip6c_src;
    281 			sa6_dst = (struct sockaddr_in6 *)sa;
    282 			sav = key_allocsa(AF_INET6,
    283 					  (caddr_t)&sa6_src->sin6_addr,
    284 					  (caddr_t)&sa6_dst->sin6_addr,
    285 					  IPPROTO_ESP, espp->esp_spi);
    286 			if (sav) {
    287 				if (sav->state == SADB_SASTATE_MATURE ||
    288 				    sav->state == SADB_SASTATE_DYING)
    289 					valid++;
    290 				key_freesav(sav);
    291 			}
    292 
    293 			/* XXX Further validation? */
    294 
    295 			/*
    296 			 * Depending on the value of "valid" and routing table
    297 			 * size (mtudisc_{hi,lo}wat), we will:
    298 			 * - recalcurate the new MTU and create the
    299 			 *   corresponding routing entry, or
    300 			 * - ignore the MTU change notification.
    301 			 */
    302 			icmp6_mtudisc_update((struct ip6ctlparam *)d, valid);
    303 		}
    304 	} else {
    305 		/* we normally notify any pcb here */
    306 	}
    307 }
    308 #endif /* INET6 */
    309 
    310 
    311 /*FIXME: placebo for invalpcbcacheall. Fast-IPsec has no pcb cache? */
    312 
    313 void	ipsec_invalpcbcacheall(void);
    314 void
    315 ipsec_invalpcbcacheall(void)
    316 {
    317 }
    318 
    319 static int
    320 sysctl_fast_ipsec(SYSCTLFN_ARGS)
    321 {
    322 	int error, t;
    323 	struct sysctlnode node;
    324 
    325 	node = *rnode;
    326 	t = *(int*)rnode->sysctl_data;
    327 	node.sysctl_data = &t;
    328 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    329 	if (error || newp == NULL)
    330 		return (error);
    331 
    332 	switch (rnode->sysctl_num) {
    333 	case IPSECCTL_DEF_ESP_TRANSLEV:
    334 	case IPSECCTL_DEF_ESP_NETLEV:
    335 	case IPSECCTL_DEF_AH_TRANSLEV:
    336 	case IPSECCTL_DEF_AH_NETLEV:
    337 		if (t != IPSEC_LEVEL_USE &&
    338 		    t != IPSEC_LEVEL_REQUIRE)
    339 			return (EINVAL);
    340 		ipsec_invalpcbcacheall();
    341 		break;
    342       	case IPSECCTL_DEF_POLICY:
    343 		if (t != IPSEC_POLICY_DISCARD &&
    344 		    t != IPSEC_POLICY_NONE)
    345 			return (EINVAL);
    346 		ipsec_invalpcbcacheall();
    347 		break;
    348 	default:
    349 		return (EINVAL);
    350 	}
    351 
    352 	*(int*)rnode->sysctl_data = t;
    353 
    354 	return (0);
    355 }
    356 
    357 /* XXX will need a different oid at parent */
    358 /* @@@ i have called it "fast_ipsec" instead of "ipsec" */
    359 SYSCTL_SETUP(sysctl_net_inet_fast_ipsec_setup, "sysctl net.inet.fast_ipsec subtree setup")
    360 {
    361 
    362 	sysctl_createv(SYSCTL_PERMANENT,
    363 		       CTLTYPE_NODE, "net", NULL,
    364 		       NULL, 0, NULL, 0,
    365 		       CTL_NET, CTL_EOL);
    366 	sysctl_createv(SYSCTL_PERMANENT,
    367 		       CTLTYPE_NODE, "inet", NULL,
    368 		       NULL, 0, NULL, 0,
    369 		       CTL_NET, PF_INET, CTL_EOL);
    370 	sysctl_createv(SYSCTL_PERMANENT,
    371 		       CTLTYPE_NODE, "fast_ipsec", NULL,
    372 		       NULL, 0, NULL, 0,
    373 		       CTL_NET, PF_INET, IPPROTO_AH, CTL_EOL);
    374 
    375 	sysctl_createv(SYSCTL_PERMANENT|SYSCTL_READWRITE,
    376 		       CTLTYPE_STRUCT, "stats", NULL,
    377 		       NULL, 0, &ipsecstat, sizeof(ipsecstat),
    378 		       CTL_NET, PF_INET, IPPROTO_AH,
    379 		       IPSECCTL_STATS, CTL_EOL);
    380 	sysctl_createv(SYSCTL_PERMANENT|SYSCTL_READWRITE,
    381 		       CTLTYPE_INT, "def_policy", NULL,
    382 		       sysctl_fast_ipsec, 0, &ip4_def_policy.policy, 0,
    383 		       CTL_NET, PF_INET, IPPROTO_AH,
    384 		       IPSECCTL_DEF_POLICY, CTL_EOL);
    385 	sysctl_createv(SYSCTL_PERMANENT|SYSCTL_READWRITE,
    386 		       CTLTYPE_INT, "esp_trans_deflev", NULL,
    387 		       sysctl_fast_ipsec, 0, &ip4_esp_trans_deflev, 0,
    388 		       CTL_NET, PF_INET, IPPROTO_AH,
    389 		       IPSECCTL_DEF_ESP_TRANSLEV, CTL_EOL);
    390 	sysctl_createv(SYSCTL_PERMANENT|SYSCTL_READWRITE,
    391 		       CTLTYPE_INT, "esp_net_deflev", NULL,
    392 		       sysctl_fast_ipsec, 0, &ip4_esp_net_deflev, 0,
    393 		       CTL_NET, PF_INET, IPPROTO_AH,
    394 		       IPSECCTL_DEF_ESP_NETLEV, CTL_EOL);
    395 	sysctl_createv(SYSCTL_PERMANENT|SYSCTL_READWRITE,
    396 		       CTLTYPE_INT, "ah_trans_deflev", NULL,
    397 		       sysctl_fast_ipsec, 0, &ip4_ah_trans_deflev, 0,
    398 		       CTL_NET, PF_INET, IPPROTO_AH,
    399 		       IPSECCTL_DEF_AH_TRANSLEV, CTL_EOL);
    400 	sysctl_createv(SYSCTL_PERMANENT|SYSCTL_READWRITE,
    401 		       CTLTYPE_INT, "ah_net_deflev", NULL,
    402 		       sysctl_fast_ipsec, 0, &ip4_ah_net_deflev, 0,
    403 		       CTL_NET, PF_INET, IPPROTO_AH,
    404 		       IPSECCTL_DEF_AH_NETLEV, CTL_EOL);
    405 	sysctl_createv(SYSCTL_PERMANENT|SYSCTL_READWRITE,
    406 		       CTLTYPE_INT, "ah_cleartos", NULL,
    407 		       NULL, 0, &/*ip4_*/ah_cleartos, 0,
    408 		       CTL_NET, PF_INET, IPPROTO_AH,
    409 		       IPSECCTL_AH_CLEARTOS, CTL_EOL);
    410 	sysctl_createv(SYSCTL_PERMANENT|SYSCTL_READWRITE,
    411 		       CTLTYPE_INT, "ah_offsetmask", NULL,
    412 		       NULL, 0, &ip4_ah_offsetmask, 0,
    413 		       CTL_NET, PF_INET, IPPROTO_AH,
    414 		       IPSECCTL_AH_OFFSETMASK, CTL_EOL);
    415 	sysctl_createv(SYSCTL_PERMANENT|SYSCTL_READWRITE,
    416 		       CTLTYPE_INT, "dfbit", NULL,
    417 		       NULL, 0, &ip4_ipsec_dfbit, 0,
    418 		       CTL_NET, PF_INET, IPPROTO_AH,
    419 		       IPSECCTL_DFBIT, CTL_EOL);
    420 	sysctl_createv(SYSCTL_PERMANENT|SYSCTL_READWRITE,
    421 		       CTLTYPE_INT, "ecn", NULL,
    422 		       NULL, 0, &ip4_ipsec_ecn, 0,
    423 		       CTL_NET, PF_INET, IPPROTO_AH,
    424 		       IPSECCTL_ECN, CTL_EOL);
    425 	sysctl_createv(SYSCTL_PERMANENT|SYSCTL_READWRITE,
    426 		       CTLTYPE_INT, "debug", NULL,
    427 		       NULL, 0, &ipsec_debug, 0,
    428 		       CTL_NET, PF_INET, IPPROTO_AH,
    429 		       IPSECCTL_DEBUG, CTL_EOL);
    430 
    431 	/*
    432 	 * "aliases" for the fast ipsec subtree
    433 	 */
    434 	sysctl_createv(SYSCTL_PERMANENT|SYSCTL_ALIAS,
    435 		       CTLTYPE_NODE, "fast_esp", NULL,
    436 		       NULL, IPPROTO_AH, NULL, 0,
    437 		       CTL_NET, PF_INET, IPPROTO_ESP, CTL_EOL);
    438 	sysctl_createv(SYSCTL_PERMANENT|SYSCTL_ALIAS,
    439 		       CTLTYPE_NODE, "fast_ipcomp", NULL,
    440 		       NULL, IPPROTO_AH, NULL, 0,
    441 		       CTL_NET, PF_INET, IPPROTO_IPCOMP, CTL_EOL);
    442 	sysctl_createv(SYSCTL_PERMANENT|SYSCTL_ALIAS,
    443 		       CTLTYPE_NODE, "fast_ah", NULL,
    444 		       NULL, IPPROTO_AH, NULL, 0,
    445 		       CTL_NET, PF_INET, CTL_CREATE, CTL_EOL);
    446 }
    447