xref: /xnu-12377.41.6/bsd/netkey/keydb.c (revision bbb1b6f9e71b8cdde6e5cd6f4841f207dee3d828)
1*bbb1b6f9SApple OSS Distributions /*
2*bbb1b6f9SApple OSS Distributions  * Copyright (c) 2016 Apple Inc. All rights reserved.
3*bbb1b6f9SApple OSS Distributions  *
4*bbb1b6f9SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*bbb1b6f9SApple OSS Distributions  *
6*bbb1b6f9SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*bbb1b6f9SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*bbb1b6f9SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*bbb1b6f9SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*bbb1b6f9SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*bbb1b6f9SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*bbb1b6f9SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*bbb1b6f9SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*bbb1b6f9SApple OSS Distributions  *
15*bbb1b6f9SApple OSS Distributions  * Please obtain a copy of the License at
16*bbb1b6f9SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*bbb1b6f9SApple OSS Distributions  *
18*bbb1b6f9SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*bbb1b6f9SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*bbb1b6f9SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*bbb1b6f9SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*bbb1b6f9SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*bbb1b6f9SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*bbb1b6f9SApple OSS Distributions  * limitations under the License.
25*bbb1b6f9SApple OSS Distributions  *
26*bbb1b6f9SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*bbb1b6f9SApple OSS Distributions  */
28*bbb1b6f9SApple OSS Distributions 
29*bbb1b6f9SApple OSS Distributions /*	$KAME: keydb.c,v 1.61 2000/03/25 07:24:13 sumikawa Exp $	*/
30*bbb1b6f9SApple OSS Distributions 
31*bbb1b6f9SApple OSS Distributions /*
32*bbb1b6f9SApple OSS Distributions  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
33*bbb1b6f9SApple OSS Distributions  * All rights reserved.
34*bbb1b6f9SApple OSS Distributions  *
35*bbb1b6f9SApple OSS Distributions  * Redistribution and use in source and binary forms, with or without
36*bbb1b6f9SApple OSS Distributions  * modification, are permitted provided that the following conditions
37*bbb1b6f9SApple OSS Distributions  * are met:
38*bbb1b6f9SApple OSS Distributions  * 1. Redistributions of source code must retain the above copyright
39*bbb1b6f9SApple OSS Distributions  *    notice, this list of conditions and the following disclaimer.
40*bbb1b6f9SApple OSS Distributions  * 2. Redistributions in binary form must reproduce the above copyright
41*bbb1b6f9SApple OSS Distributions  *    notice, this list of conditions and the following disclaimer in the
42*bbb1b6f9SApple OSS Distributions  *    documentation and/or other materials provided with the distribution.
43*bbb1b6f9SApple OSS Distributions  * 3. Neither the name of the project nor the names of its contributors
44*bbb1b6f9SApple OSS Distributions  *    may be used to endorse or promote products derived from this software
45*bbb1b6f9SApple OSS Distributions  *    without specific prior written permission.
46*bbb1b6f9SApple OSS Distributions  *
47*bbb1b6f9SApple OSS Distributions  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
48*bbb1b6f9SApple OSS Distributions  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49*bbb1b6f9SApple OSS Distributions  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50*bbb1b6f9SApple OSS Distributions  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
51*bbb1b6f9SApple OSS Distributions  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52*bbb1b6f9SApple OSS Distributions  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53*bbb1b6f9SApple OSS Distributions  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54*bbb1b6f9SApple OSS Distributions  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55*bbb1b6f9SApple OSS Distributions  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56*bbb1b6f9SApple OSS Distributions  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57*bbb1b6f9SApple OSS Distributions  * SUCH DAMAGE.
58*bbb1b6f9SApple OSS Distributions  */
59*bbb1b6f9SApple OSS Distributions 
60*bbb1b6f9SApple OSS Distributions #include <sys/types.h>
61*bbb1b6f9SApple OSS Distributions #include <sys/socket.h>
62*bbb1b6f9SApple OSS Distributions #include <sys/param.h>
63*bbb1b6f9SApple OSS Distributions #include <sys/systm.h>
64*bbb1b6f9SApple OSS Distributions #include <sys/kernel.h>
65*bbb1b6f9SApple OSS Distributions #include <sys/malloc.h>
66*bbb1b6f9SApple OSS Distributions #include <sys/errno.h>
67*bbb1b6f9SApple OSS Distributions #include <sys/queue.h>
68*bbb1b6f9SApple OSS Distributions 
69*bbb1b6f9SApple OSS Distributions #include <net/if.h>
70*bbb1b6f9SApple OSS Distributions #include <net/route.h>
71*bbb1b6f9SApple OSS Distributions 
72*bbb1b6f9SApple OSS Distributions #include <netinet/in.h>
73*bbb1b6f9SApple OSS Distributions 
74*bbb1b6f9SApple OSS Distributions #include <net/pfkeyv2.h>
75*bbb1b6f9SApple OSS Distributions #include <netkey/key.h>
76*bbb1b6f9SApple OSS Distributions #include <netkey/keydb.h>
77*bbb1b6f9SApple OSS Distributions #include <netinet6/ipsec.h>
78*bbb1b6f9SApple OSS Distributions 
79*bbb1b6f9SApple OSS Distributions #include <net/net_osdep.h>
80*bbb1b6f9SApple OSS Distributions 
81*bbb1b6f9SApple OSS Distributions // static void keydb_delsecasvar(struct secasvar *); // not used
82*bbb1b6f9SApple OSS Distributions 
83*bbb1b6f9SApple OSS Distributions /*
84*bbb1b6f9SApple OSS Distributions  * secpolicy management
85*bbb1b6f9SApple OSS Distributions  */
86*bbb1b6f9SApple OSS Distributions struct secpolicy *
keydb_newsecpolicy(void)87*bbb1b6f9SApple OSS Distributions keydb_newsecpolicy(void)
88*bbb1b6f9SApple OSS Distributions {
89*bbb1b6f9SApple OSS Distributions 	LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_NOTOWNED);
90*bbb1b6f9SApple OSS Distributions 
91*bbb1b6f9SApple OSS Distributions 	return kalloc_type(struct secpolicy, Z_WAITOK | Z_ZERO);
92*bbb1b6f9SApple OSS Distributions }
93*bbb1b6f9SApple OSS Distributions 
94*bbb1b6f9SApple OSS Distributions void
keydb_delsecpolicy(struct secpolicy * p)95*bbb1b6f9SApple OSS Distributions keydb_delsecpolicy(struct secpolicy *p)
96*bbb1b6f9SApple OSS Distributions {
97*bbb1b6f9SApple OSS Distributions 	kfree_type(struct secpolicy, p);
98*bbb1b6f9SApple OSS Distributions }
99*bbb1b6f9SApple OSS Distributions 
100*bbb1b6f9SApple OSS Distributions /*
101*bbb1b6f9SApple OSS Distributions  * secashead management
102*bbb1b6f9SApple OSS Distributions  */
103*bbb1b6f9SApple OSS Distributions struct secashead *
keydb_newsecashead(void)104*bbb1b6f9SApple OSS Distributions keydb_newsecashead(void)
105*bbb1b6f9SApple OSS Distributions {
106*bbb1b6f9SApple OSS Distributions 	struct secashead *p;
107*bbb1b6f9SApple OSS Distributions 
108*bbb1b6f9SApple OSS Distributions 	LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_OWNED);
109*bbb1b6f9SApple OSS Distributions 
110*bbb1b6f9SApple OSS Distributions 	p = kalloc_type(struct secashead, Z_NOWAIT | Z_ZERO);
111*bbb1b6f9SApple OSS Distributions 	if (!p) {
112*bbb1b6f9SApple OSS Distributions 		lck_mtx_unlock(sadb_mutex);
113*bbb1b6f9SApple OSS Distributions 		p = kalloc_type(struct secashead, Z_WAITOK | Z_ZERO | Z_NOFAIL);
114*bbb1b6f9SApple OSS Distributions 		lck_mtx_lock(sadb_mutex);
115*bbb1b6f9SApple OSS Distributions 	}
116*bbb1b6f9SApple OSS Distributions 	for (size_t i = 0; i < ARRAY_COUNT(p->savtree); i++) {
117*bbb1b6f9SApple OSS Distributions 		LIST_INIT(&p->savtree[i]);
118*bbb1b6f9SApple OSS Distributions 	}
119*bbb1b6f9SApple OSS Distributions 	return p;
120*bbb1b6f9SApple OSS Distributions }
121*bbb1b6f9SApple OSS Distributions 
122*bbb1b6f9SApple OSS Distributions /*
123*bbb1b6f9SApple OSS Distributions  * secreplay management
124*bbb1b6f9SApple OSS Distributions  */
125*bbb1b6f9SApple OSS Distributions struct secreplay *
keydb_newsecreplay(u_int8_t wsize)126*bbb1b6f9SApple OSS Distributions keydb_newsecreplay(u_int8_t wsize)
127*bbb1b6f9SApple OSS Distributions {
128*bbb1b6f9SApple OSS Distributions 	struct secreplay *p;
129*bbb1b6f9SApple OSS Distributions 	caddr_t tmp_bitmap = NULL;
130*bbb1b6f9SApple OSS Distributions 
131*bbb1b6f9SApple OSS Distributions 	LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_OWNED);
132*bbb1b6f9SApple OSS Distributions 
133*bbb1b6f9SApple OSS Distributions 	p = kalloc_type(struct secreplay, Z_NOWAIT | Z_ZERO);
134*bbb1b6f9SApple OSS Distributions 	if (!p) {
135*bbb1b6f9SApple OSS Distributions 		lck_mtx_unlock(sadb_mutex);
136*bbb1b6f9SApple OSS Distributions 		p = kalloc_type(struct secreplay, Z_WAITOK | Z_ZERO | Z_NOFAIL);
137*bbb1b6f9SApple OSS Distributions 		lck_mtx_lock(sadb_mutex);
138*bbb1b6f9SApple OSS Distributions 	}
139*bbb1b6f9SApple OSS Distributions 
140*bbb1b6f9SApple OSS Distributions 	if (wsize != 0) {
141*bbb1b6f9SApple OSS Distributions 		tmp_bitmap = (caddr_t)kalloc_data(wsize, Z_NOWAIT | Z_ZERO);
142*bbb1b6f9SApple OSS Distributions 		if (!tmp_bitmap) {
143*bbb1b6f9SApple OSS Distributions 			lck_mtx_unlock(sadb_mutex);
144*bbb1b6f9SApple OSS Distributions 			tmp_bitmap = (caddr_t)kalloc_data(wsize, Z_WAITOK | Z_ZERO | Z_NOFAIL);
145*bbb1b6f9SApple OSS Distributions 			lck_mtx_lock(sadb_mutex);
146*bbb1b6f9SApple OSS Distributions 		}
147*bbb1b6f9SApple OSS Distributions 
148*bbb1b6f9SApple OSS Distributions 		p->bitmap = tmp_bitmap;
149*bbb1b6f9SApple OSS Distributions 		p->wsize = wsize;
150*bbb1b6f9SApple OSS Distributions 	}
151*bbb1b6f9SApple OSS Distributions 	return p;
152*bbb1b6f9SApple OSS Distributions }
153*bbb1b6f9SApple OSS Distributions 
154*bbb1b6f9SApple OSS Distributions void
keydb_delsecreplay(struct secreplay * p)155*bbb1b6f9SApple OSS Distributions keydb_delsecreplay(struct secreplay *p)
156*bbb1b6f9SApple OSS Distributions {
157*bbb1b6f9SApple OSS Distributions 	if (p->bitmap) {
158*bbb1b6f9SApple OSS Distributions 		kfree_data_sized_by(p->bitmap, p->wsize);
159*bbb1b6f9SApple OSS Distributions 	}
160*bbb1b6f9SApple OSS Distributions 	kfree_type(struct secreplay, p);
161*bbb1b6f9SApple OSS Distributions }
162