xref: /netbsd-src/sys/netipsec/ipsec_netbsd.c (revision d11b170b9000ada93db553723522a63d5deac310)
1 /*	$NetBSD: ipsec_netbsd.c,v 1.36 2014/02/25 18:30:12 pooka 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.36 2014/02/25 18:30:12 pooka 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 <sys/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 
66 #include <netipsec/ipsec.h>
67 #include <netipsec/ipsec_var.h>
68 #include <netipsec/ipsec_private.h>
69 #include <netipsec/key.h>
70 #include <netipsec/keydb.h>
71 #include <netipsec/key_debug.h>
72 #include <netipsec/ah.h>
73 #include <netipsec/ah_var.h>
74 #include <netipsec/esp.h>
75 #include <netipsec/esp_var.h>
76 #include <netipsec/ipip_var.h>
77 #include <netipsec/ipcomp_var.h>
78 
79 #ifdef INET6
80 #include <netipsec/ipsec6.h>
81 #include <netinet6/ip6protosw.h>
82 #include <netinet/icmp6.h>
83 #endif
84 
85 #include <netipsec/key.h>
86 
87 /* assumes that ip header and ah header are contiguous on mbuf */
88 void*
89 ah4_ctlinput(int cmd, const struct sockaddr *sa, void *v)
90 {
91 	struct ip *ip = v;
92 	struct ah *ah;
93 	struct icmp *icp;
94 	struct secasvar *sav;
95 
96 	if (sa->sa_family != AF_INET ||
97 		sa->sa_len != sizeof(struct sockaddr_in))
98 		return NULL;
99 	if ((unsigned)cmd >= PRC_NCMDS)
100 		return NULL;
101 
102 	if (cmd == PRC_MSGSIZE && ip_mtudisc && ip && ip->ip_v == 4) {
103 		/*
104 		 * Check to see if we have a valid SA corresponding to
105 		 * the address in the ICMP message payload.
106 		 */
107 		ah = (struct ah *)((char *)ip + (ip->ip_hl << 2));
108 		sav = KEY_ALLOCSA((const union sockaddr_union *)sa,
109 					   	IPPROTO_AH, ah->ah_spi, 0, 0);
110 
111 		if (sav) {
112         	if (sav->state == SADB_SASTATE_MATURE ||
113                 sav->state == SADB_SASTATE_DYING) {
114 
115 				/*
116 				 * Now that we've validated that we are actually
117 				 * communicating with the host indicated in the
118 				 * ICMP message, locate the ICMP header,
119 				 * recalculate the new MTU, and create the
120 		 		 * corresponding routing entry.
121 		 		 */
122 				icp = (struct icmp *)((char *)ip -
123 									  offsetof(struct icmp, icmp_ip));
124 				icmp_mtudisc(icp, ip->ip_dst);
125 
126 			}
127 			KEY_FREESAV(&sav);
128 		}
129 	}
130 	return NULL;
131 }
132 
133 
134 
135 /* assumes that ip header and esp header are contiguous on mbuf */
136 void*
137 esp4_ctlinput(int cmd, const struct sockaddr *sa, void *v)
138 {
139 	struct ip *ip = v;
140 	struct esp *esp;
141 	struct icmp *icp;
142 	struct secasvar *sav;
143 
144 	if (sa->sa_family != AF_INET ||
145 	    sa->sa_len != sizeof(struct sockaddr_in))
146 		return NULL;
147 	if ((unsigned)cmd >= PRC_NCMDS)
148 		return NULL;
149 
150 	if (cmd == PRC_MSGSIZE && ip_mtudisc && ip && ip->ip_v == 4) {
151 		/*
152 		 * Check to see if we have a valid SA corresponding to
153 		 * the address in the ICMP message payload.
154 		 */
155 		esp = (struct esp *)((char *)ip + (ip->ip_hl << 2));
156 		sav = KEY_ALLOCSA((const union sockaddr_union *)sa,
157 					   	IPPROTO_ESP, esp->esp_spi, 0, 0);
158 
159 		if (sav) {
160         	if (sav->state == SADB_SASTATE_MATURE ||
161                 sav->state == SADB_SASTATE_DYING) {
162 
163 				/*
164 				 * Now that we've validated that we are actually
165 				 * communicating with the host indicated in the
166 				 * ICMP message, locate the ICMP header,
167 				 * recalculate the new MTU, and create the
168 		 		 * corresponding routing entry.
169 		 		 */
170 
171 				icp = (struct icmp *)((char *)ip -
172 									   offsetof(struct icmp, icmp_ip));
173 				icmp_mtudisc(icp, ip->ip_dst);
174 
175 			}
176 			KEY_FREESAV(&sav);
177 		}
178 	}
179 	return NULL;
180 }
181 
182 #ifdef INET6
183 void *
184 ah6_ctlinput(int cmd, const struct sockaddr *sa, void *d)
185 {
186        const struct newah *ahp;
187        struct newah ah;
188        struct secasvar *sav;
189        struct ip6_hdr *ip6;
190        struct mbuf *m;
191        struct ip6ctlparam *ip6cp = NULL;
192        int off;
193 
194        if (sa->sa_family != AF_INET6 ||
195            sa->sa_len != sizeof(struct sockaddr_in6))
196                return NULL;
197        if ((unsigned)cmd >= PRC_NCMDS)
198                return NULL;
199 
200        /* if the parameter is from icmp6, decode it. */
201        if (d != NULL) {
202                ip6cp = (struct ip6ctlparam *)d;
203                m = ip6cp->ip6c_m;
204                ip6 = ip6cp->ip6c_ip6;
205                off = ip6cp->ip6c_off;
206        } else {
207                m = NULL;
208                ip6 = NULL;
209                off = 0;
210        }
211 
212        if (ip6) {
213                /*
214                 * XXX: We assume that when ip6 is non NULL,
215                 * M and OFF are valid.
216                 */
217 
218                /* check if we can safely examine src and dst ports */
219                if (m->m_pkthdr.len < off + sizeof(ah))
220                        return NULL;
221 
222                if (m->m_len < off + sizeof(ah)) {
223                        /*
224                         * this should be rare case,
225                         * so we compromise on this copy...
226                         */
227                        m_copydata(m, off, sizeof(ah), &ah);
228                        ahp = &ah;
229                } else
230                        ahp = (struct newah *)(mtod(m, char *) + off);
231 
232                if (cmd == PRC_MSGSIZE) {
233                        int valid = 0;
234 
235                        /*
236                         * Check to see if we have a valid SA corresponding
237                         * to the address in the ICMP message payload.
238                         */
239                        sav = KEY_ALLOCSA((const union sockaddr_union*)sa,
240                                          IPPROTO_AH, ahp->ah_spi, 0, 0);
241 
242                        if (sav) {
243                                if (sav->state == SADB_SASTATE_MATURE ||
244                                    sav->state == SADB_SASTATE_DYING)
245                                        valid++;
246                                KEY_FREESAV(&sav);
247                        }
248 
249                        /* XXX Further validation? */
250 
251                        /*
252                         * Depending on the value of "valid" and routing
253                         * table size (mtudisc_{hi,lo}wat), we will:
254                         * - recalcurate the new MTU and create the
255                         *   corresponding routing entry, or
256                         * - ignore the MTU change notification.
257                         */
258                        icmp6_mtudisc_update((struct ip6ctlparam *)d,valid);
259                }
260 
261                /* we normally notify single pcb here */
262        } else {
263                /* we normally notify any pcb here */
264        }
265 	   return NULL;
266 }
267 
268 
269 
270 void *
271 esp6_ctlinput(int cmd, const struct sockaddr *sa, void *d)
272 {
273 	const struct newesp *espp;
274 	struct newesp esp;
275 	struct ip6ctlparam *ip6cp = NULL, ip6cp1;
276 	struct secasvar *sav;
277 	struct ip6_hdr *ip6;
278 	struct mbuf *m;
279 	int off;
280 
281 	if (sa->sa_family != AF_INET6 ||
282 	    sa->sa_len != sizeof(struct sockaddr_in6))
283 		return NULL;
284 	if ((unsigned)cmd >= PRC_NCMDS)
285 		return NULL;
286 
287 	/* if the parameter is from icmp6, decode it. */
288 	if (d != NULL) {
289 		ip6cp = (struct ip6ctlparam *)d;
290 		m = ip6cp->ip6c_m;
291 		ip6 = ip6cp->ip6c_ip6;
292 		off = ip6cp->ip6c_off;
293 	} else {
294 		m = NULL;
295 		ip6 = NULL;
296 		off = 0;
297 	}
298 
299 	if (ip6) {
300 		/*
301 		 * Notify the error to all possible sockets via pfctlinput2.
302 		 * Since the upper layer information (such as protocol type,
303 		 * source and destination ports) is embedded in the encrypted
304 		 * data and might have been cut, we can't directly call
305 		 * an upper layer ctlinput function. However, the pcbnotify
306 		 * function will consider source and destination addresses
307 		 * as well as the flow info value, and may be able to find
308 		 * some PCB that should be notified.
309 		 * Although pfctlinput2 will call esp6_ctlinput(), there is
310 		 * no possibility of an infinite loop of function calls,
311 		 * because we don't pass the inner IPv6 header.
312 		 */
313 		memset(&ip6cp1, 0, sizeof(ip6cp1));
314 		ip6cp1.ip6c_src = ip6cp->ip6c_src;
315 		pfctlinput2(cmd, sa, &ip6cp1);
316 
317 		/*
318 		 * Then go to special cases that need ESP header information.
319 		 * XXX: We assume that when ip6 is non NULL,
320 		 * M and OFF are valid.
321 		 */
322 
323 		/* check if we can safely examine src and dst ports */
324 		if (m->m_pkthdr.len < off + sizeof(esp))
325 			return NULL;
326 
327 		if (m->m_len < off + sizeof(esp)) {
328 			/*
329 			 * this should be rare case,
330 			 * so we compromise on this copy...
331 			 */
332 			m_copydata(m, off, sizeof(esp), &esp);
333 			espp = &esp;
334 		} else
335 			espp = (struct newesp*)(mtod(m, char *) + off);
336 
337 		if (cmd == PRC_MSGSIZE) {
338 			int valid = 0;
339 
340 			/*
341 			 * Check to see if we have a valid SA corresponding to
342 			 * the address in the ICMP message payload.
343 			 */
344 
345 			sav = KEY_ALLOCSA((const union sockaddr_union*)sa,
346 					  IPPROTO_ESP, espp->esp_spi, 0, 0);
347 
348 			if (sav) {
349 				if (sav->state == SADB_SASTATE_MATURE ||
350 				    sav->state == SADB_SASTATE_DYING)
351 					valid++;
352 				KEY_FREESAV(&sav);
353 			}
354 
355 			/* XXX Further validation? */
356 
357 			/*
358 			 * Depending on the value of "valid" and routing table
359 			 * size (mtudisc_{hi,lo}wat), we will:
360 			 * - recalcurate the new MTU and create the
361 			 *   corresponding routing entry, or
362 			 * - ignore the MTU change notification.
363 			 */
364 			icmp6_mtudisc_update((struct ip6ctlparam *)d, valid);
365 		}
366 	} else {
367 		/* we normally notify any pcb here */
368 	}
369 	return NULL;
370 }
371 #endif /* INET6 */
372 
373 static int
374 sysctl_ipsec(SYSCTLFN_ARGS)
375 {
376 	int error, t;
377 	struct sysctlnode node;
378 
379 	node = *rnode;
380 	t = *(int*)rnode->sysctl_data;
381 	node.sysctl_data = &t;
382 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
383 	if (error || newp == NULL)
384 		return (error);
385 
386 	switch (rnode->sysctl_num) {
387 	case IPSECCTL_DEF_ESP_TRANSLEV:
388 	case IPSECCTL_DEF_ESP_NETLEV:
389 	case IPSECCTL_DEF_AH_TRANSLEV:
390 	case IPSECCTL_DEF_AH_NETLEV:
391 		if (t != IPSEC_LEVEL_USE &&
392 		    t != IPSEC_LEVEL_REQUIRE)
393 			return (EINVAL);
394 		ipsec_invalpcbcacheall();
395 		break;
396       	case IPSECCTL_DEF_POLICY:
397 		if (t != IPSEC_POLICY_DISCARD &&
398 		    t != IPSEC_POLICY_NONE)
399 			return (EINVAL);
400 		ipsec_invalpcbcacheall();
401 		break;
402 	default:
403 		return (EINVAL);
404 	}
405 
406 	*(int*)rnode->sysctl_data = t;
407 
408 	return (0);
409 }
410 
411 #ifdef IPSEC_DEBUG
412 static int
413 sysctl_ipsec_test(SYSCTLFN_ARGS)
414 {
415 	int t, error;
416 	struct sysctlnode node;
417 
418 	node = *rnode;
419 	t = *(int*)rnode->sysctl_data;
420 	node.sysctl_data = &t;
421 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
422 	if (error || newp == NULL)
423 		return (error);
424 
425 	if (t < 0 || t > 1)
426 		return EINVAL;
427 
428 	if (rnode->sysctl_data == &ipsec_replay)
429 		printf("ipsec: Anti-Replay service %s\n",
430 		    (t == 1) ? "deactivated" : "activated");
431 	else if (rnode->sysctl_data == &ipsec_integrity)
432 		 printf("ipsec: HMAC corruption %s\n",
433 		     (t == 0) ? "deactivated" : "activated");
434 
435 	*(int*)rnode->sysctl_data = t;
436 
437 	return 0;
438 }
439 #endif
440 
441 static int
442 sysctl_net_inet_ipsec_stats(SYSCTLFN_ARGS)
443 {
444 
445 	return (NETSTAT_SYSCTL(ipsecstat_percpu, IPSEC_NSTATS));
446 }
447 
448 static int
449 sysctl_net_inet_ah_stats(SYSCTLFN_ARGS)
450 {
451 
452 	return (NETSTAT_SYSCTL(ahstat_percpu, AH_NSTATS));
453 }
454 
455 static int
456 sysctl_net_inet_esp_stats(SYSCTLFN_ARGS)
457 {
458 
459 	return (NETSTAT_SYSCTL(espstat_percpu, ESP_NSTATS));
460 }
461 
462 static int
463 sysctl_net_inet_ipcomp_stats(SYSCTLFN_ARGS)
464 {
465 
466 	return (NETSTAT_SYSCTL(ipcompstat_percpu, IPCOMP_NSTATS));
467 }
468 
469 static int
470 sysctl_net_inet_ipip_stats(SYSCTLFN_ARGS)
471 {
472 
473 	return (NETSTAT_SYSCTL(ipipstat_percpu, IPIP_NSTATS));
474 }
475 
476 /* XXX will need a different oid at parent */
477 SYSCTL_SETUP(sysctl_net_inet_ipsec_setup, "sysctl net.inet.ipsec subtree setup")
478 {
479 	const struct sysctlnode *_ipsec;
480 	int ipproto_ipsec;
481 
482 	sysctl_createv(clog, 0, NULL, NULL,
483 		       CTLFLAG_PERMANENT,
484 		       CTLTYPE_NODE, "inet", NULL,
485 		       NULL, 0, NULL, 0,
486 		       CTL_NET, PF_INET, CTL_EOL);
487 
488 	/*
489 	 * in numerical order:
490 	 *
491 	 * net.inet.ipip:	CTL_NET.PF_INET.IPPROTO_IPIP
492 	 * net.inet.esp:	CTL_NET.PF_INET.IPPROTO_ESP
493 	 * net.inet.ah:		CTL_NET.PF_INET.IPPROTO_AH
494 	 * net.inet.ipcomp:	CTL_NET.PF_INET.IPPROTO_IPCOMP
495 	 * net.inet.ipsec:	CTL_NET.PF_INET.CTL_CREATE
496 	 *
497 	 * this creates separate trees by name, but maintains that the
498 	 * ipsec name leads to all the old leaves.
499 	 */
500 
501 	/* create net.inet.ipip */
502 	sysctl_createv(clog, 0, NULL, NULL,
503 		       CTLFLAG_PERMANENT,
504 		       CTLTYPE_NODE, "ipip", NULL,
505 		       NULL, 0, NULL, 0,
506 		       CTL_NET, PF_INET, IPPROTO_IPIP, CTL_EOL);
507 	sysctl_createv(clog, 0, NULL, NULL,
508 		       CTLFLAG_PERMANENT|CTLFLAG_READONLY,
509 		       CTLTYPE_STRUCT, "ipip_stats", NULL,
510 		       sysctl_net_inet_ipip_stats, 0, NULL, 0,
511 		       CTL_NET, PF_INET, IPPROTO_IPIP,
512 		       CTL_CREATE, CTL_EOL);
513 
514 	/* create net.inet.esp subtree under IPPROTO_ESP */
515 	sysctl_createv(clog, 0, NULL, NULL,
516 		       CTLFLAG_PERMANENT,
517 		       CTLTYPE_NODE, "esp", NULL,
518 		       NULL, 0, NULL, 0,
519 		       CTL_NET, PF_INET, IPPROTO_ESP, CTL_EOL);
520 	sysctl_createv(clog, 0, NULL, NULL,
521 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
522 		       CTLTYPE_INT, "trans_deflev", NULL,
523 		       sysctl_ipsec, 0, &ip4_esp_trans_deflev, 0,
524 		       CTL_NET, PF_INET, IPPROTO_ESP,
525 		       IPSECCTL_DEF_ESP_TRANSLEV, CTL_EOL);
526 	sysctl_createv(clog, 0, NULL, NULL,
527 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
528 		       CTLTYPE_INT, "net_deflev", NULL,
529 		       sysctl_ipsec, 0, &ip4_esp_net_deflev, 0,
530 		       CTL_NET, PF_INET, IPPROTO_ESP,
531 		       IPSECCTL_DEF_ESP_NETLEV, CTL_EOL);
532 	sysctl_createv(clog, 0, NULL, NULL,
533 		       CTLFLAG_PERMANENT|CTLFLAG_READONLY,
534 		       CTLTYPE_STRUCT, "esp_stats", NULL,
535 		       sysctl_net_inet_esp_stats, 0, NULL, 0,
536 		       CTL_NET, PF_INET, IPPROTO_ESP,
537 		       CTL_CREATE, CTL_EOL);
538 
539 	/* create net.inet.ah subtree under IPPROTO_AH */
540 	sysctl_createv(clog, 0, NULL, NULL,
541 		       CTLFLAG_PERMANENT,
542 		       CTLTYPE_NODE, "ah", NULL,
543 		       NULL, 0, NULL, 0,
544 		       CTL_NET, PF_INET, IPPROTO_AH, CTL_EOL);
545 	sysctl_createv(clog, 0, NULL, NULL,
546 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
547 		       CTLTYPE_INT, "cleartos", NULL,
548 		       NULL, 0, &ip4_ah_cleartos, 0,
549 		       CTL_NET, PF_INET, IPPROTO_AH,
550 		       IPSECCTL_AH_CLEARTOS, CTL_EOL);
551 	sysctl_createv(clog, 0, NULL, NULL,
552 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
553 		       CTLTYPE_INT, "offsetmask", NULL,
554 		       NULL, 0, &ip4_ah_offsetmask, 0,
555 		       CTL_NET, PF_INET, IPPROTO_AH,
556 		       IPSECCTL_AH_OFFSETMASK, CTL_EOL);
557 	sysctl_createv(clog, 0, NULL, NULL,
558 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
559 		       CTLTYPE_INT, "trans_deflev", NULL,
560 		       sysctl_ipsec, 0, &ip4_ah_trans_deflev, 0,
561 		       CTL_NET, PF_INET, IPPROTO_AH,
562 		       IPSECCTL_DEF_AH_TRANSLEV, CTL_EOL);
563 	sysctl_createv(clog, 0, NULL, NULL,
564 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
565 		       CTLTYPE_INT, "net_deflev", NULL,
566 		       sysctl_ipsec, 0, &ip4_ah_net_deflev, 0,
567 		       CTL_NET, PF_INET, IPPROTO_AH,
568 		       IPSECCTL_DEF_AH_NETLEV, CTL_EOL);
569 	sysctl_createv(clog, 0, NULL, NULL,
570 		       CTLFLAG_PERMANENT|CTLFLAG_READONLY,
571 		       CTLTYPE_STRUCT, "ah_stats", NULL,
572 		       sysctl_net_inet_ah_stats, 0, NULL, 0,
573 		       CTL_NET, PF_INET, IPPROTO_AH,
574 		       CTL_CREATE, CTL_EOL);
575 
576 	/* create net.inet.ipcomp */
577 	sysctl_createv(clog, 0, NULL, NULL,
578 		       CTLFLAG_PERMANENT,
579 		       CTLTYPE_NODE, "ipcomp", NULL,
580 		       NULL, 0, NULL, 0,
581 		       CTL_NET, PF_INET, IPPROTO_IPCOMP, CTL_EOL);
582 	sysctl_createv(clog, 0, NULL, NULL,
583 		       CTLFLAG_PERMANENT|CTLFLAG_READONLY,
584 		       CTLTYPE_STRUCT, "ipcomp_stats", NULL,
585 		       sysctl_net_inet_ipcomp_stats, 0, NULL, 0,
586 		       CTL_NET, PF_INET, IPPROTO_IPCOMP,
587 		       CTL_CREATE, CTL_EOL);
588 
589 	/* create net.inet.ipsec subtree under dynamic oid */
590 	sysctl_createv(clog, 0, NULL, &_ipsec,
591 		       CTLFLAG_PERMANENT,
592 		       CTLTYPE_NODE, "ipsec", NULL,
593 		       NULL, 0, NULL, 0,
594 		       CTL_NET, PF_INET, CTL_CREATE, CTL_EOL);
595 	ipproto_ipsec = (_ipsec != NULL) ? _ipsec->sysctl_num : 0;
596 
597 	sysctl_createv(clog, 0, NULL, NULL,
598 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
599 		       CTLTYPE_INT, "def_policy", NULL,
600 		       sysctl_ipsec, 0, &ip4_def_policy.policy, 0,
601 		       CTL_NET, PF_INET, ipproto_ipsec,
602 		       IPSECCTL_DEF_POLICY, CTL_EOL);
603 	sysctl_createv(clog, 0, NULL, NULL,
604 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
605 		       CTLTYPE_INT, "esp_trans_deflev", NULL,
606 		       sysctl_ipsec, 0, &ip4_esp_trans_deflev, 0,
607 		       CTL_NET, PF_INET, ipproto_ipsec,
608 		       IPSECCTL_DEF_ESP_TRANSLEV, CTL_EOL);
609 	sysctl_createv(clog, 0, NULL, NULL,
610 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
611 		       CTLTYPE_INT, "esp_net_deflev", NULL,
612 		       sysctl_ipsec, 0, &ip4_esp_net_deflev, 0,
613 		       CTL_NET, PF_INET, ipproto_ipsec,
614 		       IPSECCTL_DEF_ESP_NETLEV, CTL_EOL);
615 	sysctl_createv(clog, 0, NULL, NULL,
616 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
617 		       CTLTYPE_INT, "ah_trans_deflev", NULL,
618 		       sysctl_ipsec, 0, &ip4_ah_trans_deflev, 0,
619 		       CTL_NET, PF_INET, ipproto_ipsec,
620 		       IPSECCTL_DEF_AH_TRANSLEV, CTL_EOL);
621 	sysctl_createv(clog, 0, NULL, NULL,
622 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
623 		       CTLTYPE_INT, "ah_net_deflev", NULL,
624 		       sysctl_ipsec, 0, &ip4_ah_net_deflev, 0,
625 		       CTL_NET, PF_INET, ipproto_ipsec,
626 		       IPSECCTL_DEF_AH_NETLEV, CTL_EOL);
627 	sysctl_createv(clog, 0, NULL, NULL,
628 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
629 		       CTLTYPE_INT, "ah_cleartos", NULL,
630 		       NULL, 0, &ip4_ah_cleartos, 0,
631 		       CTL_NET, PF_INET, ipproto_ipsec,
632 		       IPSECCTL_AH_CLEARTOS, CTL_EOL);
633 	sysctl_createv(clog, 0, NULL, NULL,
634 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
635 		       CTLTYPE_INT, "ah_offsetmask", NULL,
636 		       NULL, 0, &ip4_ah_offsetmask, 0,
637 		       CTL_NET, PF_INET, ipproto_ipsec,
638 		       IPSECCTL_AH_OFFSETMASK, CTL_EOL);
639 	sysctl_createv(clog, 0, NULL, NULL,
640 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
641 		       CTLTYPE_INT, "dfbit", NULL,
642 		       NULL, 0, &ip4_ipsec_dfbit, 0,
643 		       CTL_NET, PF_INET, ipproto_ipsec,
644 		       IPSECCTL_DFBIT, CTL_EOL);
645 	sysctl_createv(clog, 0, NULL, NULL,
646 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
647 		       CTLTYPE_INT, "ecn", NULL,
648 		       NULL, 0, &ip4_ipsec_ecn, 0,
649 		       CTL_NET, PF_INET, ipproto_ipsec,
650 		       IPSECCTL_ECN, CTL_EOL);
651 	sysctl_createv(clog, 0, NULL, NULL,
652 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
653 		       CTLTYPE_INT, "debug", NULL,
654 		       NULL, 0, &ipsec_debug, 0,
655 		       CTL_NET, PF_INET, ipproto_ipsec,
656 		       IPSECCTL_DEBUG, CTL_EOL);
657 	sysctl_createv(clog, 0, NULL, NULL,
658 		       CTLFLAG_PERMANENT|CTLFLAG_READONLY,
659 		       CTLTYPE_STRUCT, "ipsecstats", NULL,
660 		       sysctl_net_inet_ipsec_stats, 0, NULL, 0,
661 		       CTL_NET, PF_INET, ipproto_ipsec,
662 		       CTL_CREATE, CTL_EOL);
663 #ifdef IPSEC_DEBUG
664 	sysctl_createv(clog, 0, NULL, NULL,
665 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
666 		       CTLTYPE_INT, "test_replay",
667 		       SYSCTL_DESCR("Emulate replay attack"),
668 		       sysctl_ipsec_test, 0, &ipsec_replay, 0,
669 		       CTL_NET, PF_INET, ipproto_ipsec,
670 		       CTL_CREATE, CTL_EOL);
671 	sysctl_createv(clog, 0, NULL, NULL,
672 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
673 		       CTLTYPE_INT, "test_integrity",
674 		       SYSCTL_DESCR("Emulate man-in-the-middle attack"),
675 		       sysctl_ipsec_test, 0, &ipsec_integrity, 0,
676 		       CTL_NET, PF_INET, ipproto_ipsec,
677 		       CTL_CREATE, CTL_EOL);
678 #endif
679 }
680 
681 #ifdef INET6
682 SYSCTL_SETUP(sysctl_net_inet6_ipsec6_setup,
683 	     "sysctl net.inet6.ipsec6 subtree setup")
684 {
685 
686 	sysctl_createv(clog, 0, NULL, NULL,
687 		       CTLFLAG_PERMANENT,
688 		       CTLTYPE_NODE, "inet6", NULL,
689 		       NULL, 0, NULL, 0,
690 		       CTL_NET, PF_INET6, CTL_EOL);
691 	sysctl_createv(clog, 0, NULL, NULL,
692 		       CTLFLAG_PERMANENT,
693 		       CTLTYPE_NODE, "ipsec6",
694 		       SYSCTL_DESCR("IPv6 related IPSec settings"),
695 		       NULL, 0, NULL, 0,
696 		       CTL_NET, PF_INET6, IPPROTO_AH, CTL_EOL);
697 
698 	sysctl_createv(clog, 0, NULL, NULL,
699 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
700 		       CTLTYPE_STRUCT, "stats",
701 		       SYSCTL_DESCR("IPSec statistics and counters"),
702 		       sysctl_net_inet_ipsec_stats, 0, NULL, 0,
703 		       CTL_NET, PF_INET6, IPPROTO_AH,
704 		       IPSECCTL_STATS, CTL_EOL);
705 	sysctl_createv(clog, 0, NULL, NULL,
706 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
707 		       CTLTYPE_INT, "def_policy",
708 		       SYSCTL_DESCR("Default action for non-IPSec packets"),
709 		       sysctl_ipsec, 0, (void *)&ip6_def_policy, 0,
710 		       CTL_NET, PF_INET6, IPPROTO_AH,
711 		       IPSECCTL_DEF_POLICY, CTL_EOL);
712 	sysctl_createv(clog, 0, NULL, NULL,
713 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
714 		       CTLTYPE_INT, "esp_trans_deflev",
715 		       SYSCTL_DESCR("Default required security level for "
716 				    "transport mode traffic"),
717 		       sysctl_ipsec, 0, &ip6_esp_trans_deflev, 0,
718 		       CTL_NET, PF_INET6, IPPROTO_AH,
719 		       IPSECCTL_DEF_ESP_TRANSLEV, CTL_EOL);
720 	sysctl_createv(clog, 0, NULL, NULL,
721 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
722 		       CTLTYPE_INT, "esp_net_deflev",
723 		       SYSCTL_DESCR("Default required security level for "
724 				    "tunneled traffic"),
725 		       sysctl_ipsec, 0, &ip6_esp_net_deflev, 0,
726 		       CTL_NET, PF_INET6, IPPROTO_AH,
727 		       IPSECCTL_DEF_ESP_NETLEV, CTL_EOL);
728 	sysctl_createv(clog, 0, NULL, NULL,
729 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
730 		       CTLTYPE_INT, "ah_trans_deflev",
731 		       SYSCTL_DESCR("Default required security level for "
732 				    "transport mode headers"),
733 		       sysctl_ipsec, 0, &ip6_ah_trans_deflev, 0,
734 		       CTL_NET, PF_INET6, IPPROTO_AH,
735 		       IPSECCTL_DEF_AH_TRANSLEV, CTL_EOL);
736 	sysctl_createv(clog, 0, NULL, NULL,
737 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
738 		       CTLTYPE_INT, "ah_net_deflev",
739 		       SYSCTL_DESCR("Default required security level for "
740 				    "tunneled headers"),
741 		       sysctl_ipsec, 0, &ip6_ah_net_deflev, 0,
742 		       CTL_NET, PF_INET6, IPPROTO_AH,
743 		       IPSECCTL_DEF_AH_NETLEV, CTL_EOL);
744 	sysctl_createv(clog, 0, NULL, NULL,
745 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
746 		       CTLTYPE_INT, "ecn",
747 		       SYSCTL_DESCR("Behavior of ECN for tunneled traffic"),
748 		       NULL, 0, &ip6_ipsec_ecn, 0,
749 		       CTL_NET, PF_INET6, IPPROTO_AH,
750 		       IPSECCTL_ECN, CTL_EOL);
751 	sysctl_createv(clog, 0, NULL, NULL,
752 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
753 		       CTLTYPE_INT, "debug",
754 		       SYSCTL_DESCR("Enable IPSec debugging output"),
755 		       NULL, 0, &ipsec_debug, 0,
756 		       CTL_NET, PF_INET6, IPPROTO_AH,
757 		       IPSECCTL_DEBUG, CTL_EOL);
758 
759 	/*
760 	 * "aliases" for the ipsec6 subtree
761 	 */
762 	sysctl_createv(clog, 0, NULL, NULL,
763 		       CTLFLAG_PERMANENT|CTLFLAG_ALIAS,
764 		       CTLTYPE_NODE, "esp6", NULL,
765 		       NULL, IPPROTO_AH, NULL, 0,
766 		       CTL_NET, PF_INET6, IPPROTO_ESP, CTL_EOL);
767 	sysctl_createv(clog, 0, NULL, NULL,
768 		       CTLFLAG_PERMANENT|CTLFLAG_ALIAS,
769 		       CTLTYPE_NODE, "ipcomp6", NULL,
770 		       NULL, IPPROTO_AH, NULL, 0,
771 		       CTL_NET, PF_INET6, IPPROTO_IPCOMP, CTL_EOL);
772 	sysctl_createv(clog, 0, NULL, NULL,
773 		       CTLFLAG_PERMANENT|CTLFLAG_ALIAS,
774 		       CTLTYPE_NODE, "ah6", NULL,
775 		       NULL, IPPROTO_AH, NULL, 0,
776 		       CTL_NET, PF_INET6, CTL_CREATE, CTL_EOL);
777 }
778 #endif /* INET6 */
779