]>
Commit | Line | Data |
---|---|---|
b0d623f7 | 1 | /* |
eb6b6ca3 | 2 | * Copyright (c) 2008-2020 Apple Inc. All rights reserved. |
b0d623f7 A |
3 | * |
4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ | |
39236c6e | 5 | * |
b0d623f7 A |
6 | * This file contains Original Code and/or Modifications of Original Code |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. The rights granted to you under the License | |
10 | * may not be used to create, or enable the creation or redistribution of, | |
11 | * unlawful or unlicensed copies of an Apple operating system, or to | |
12 | * circumvent, violate, or enable the circumvention or violation of, any | |
13 | * terms of an Apple operating system software license agreement. | |
39236c6e | 14 | * |
b0d623f7 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
39236c6e | 17 | * |
b0d623f7 A |
18 | * The Original Code and all software distributed under the License are |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. | |
23 | * Please see the License for the specific language governing rights and | |
24 | * limitations under the License. | |
39236c6e | 25 | * |
b0d623f7 A |
26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
27 | */ | |
28 | ||
55e303ae A |
29 | /* $FreeBSD: src/sys/netkey/key.c,v 1.16.2.13 2002/07/24 18:17:40 ume Exp $ */ |
30 | /* $KAME: key.c,v 1.191 2001/06/27 10:46:49 sakane Exp $ */ | |
1c79356b A |
31 | |
32 | /* | |
33 | * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. | |
34 | * All rights reserved. | |
35 | * | |
36 | * Redistribution and use in source and binary forms, with or without | |
37 | * modification, are permitted provided that the following conditions | |
38 | * are met: | |
39 | * 1. Redistributions of source code must retain the above copyright | |
40 | * notice, this list of conditions and the following disclaimer. | |
41 | * 2. Redistributions in binary form must reproduce the above copyright | |
42 | * notice, this list of conditions and the following disclaimer in the | |
43 | * documentation and/or other materials provided with the distribution. | |
44 | * 3. Neither the name of the project nor the names of its contributors | |
45 | * may be used to endorse or promote products derived from this software | |
46 | * without specific prior written permission. | |
47 | * | |
48 | * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND | |
49 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
50 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
51 | * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE | |
52 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
53 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
54 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
55 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
56 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
57 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
58 | * SUCH DAMAGE. | |
59 | */ | |
60 | ||
61 | /* | |
62 | * This code is referd to RFC 2367 | |
63 | */ | |
64 | ||
2d21ac55 | 65 | #include <machine/endian.h> |
1c79356b A |
66 | #include <sys/types.h> |
67 | #include <sys/param.h> | |
68 | #include <sys/systm.h> | |
69 | #include <sys/kernel.h> | |
70 | #include <sys/mbuf.h> | |
71 | #include <sys/domain.h> | |
72 | #include <sys/protosw.h> | |
73 | #include <sys/malloc.h> | |
74 | #include <sys/socket.h> | |
75 | #include <sys/socketvar.h> | |
1c79356b | 76 | #include <sys/sysctl.h> |
1c79356b A |
77 | #include <sys/errno.h> |
78 | #include <sys/proc.h> | |
79 | #include <sys/queue.h> | |
55e303ae | 80 | #include <sys/syslog.h> |
6d2010ae | 81 | #include <sys/mcache.h> |
1c79356b | 82 | |
91447636 A |
83 | #include <kern/locks.h> |
84 | ||
1c79356b A |
85 | #include <net/if.h> |
86 | #include <net/route.h> | |
87 | #include <net/raw_cb.h> | |
88 | ||
89 | #include <netinet/in.h> | |
90 | #include <netinet/in_systm.h> | |
91 | #include <netinet/ip.h> | |
92 | #include <netinet/in_var.h> | |
93 | ||
1c79356b A |
94 | #include <netinet/ip6.h> |
95 | #include <netinet6/in6_var.h> | |
96 | #include <netinet6/ip6_var.h> | |
1c79356b | 97 | |
1c79356b A |
98 | #include <net/pfkeyv2.h> |
99 | #include <netkey/keydb.h> | |
100 | #include <netkey/key.h> | |
101 | #include <netkey/keysock.h> | |
102 | #include <netkey/key_debug.h> | |
9bccf70c | 103 | #include <stdarg.h> |
d190cdc3 | 104 | #include <libkern/crypto/rand.h> |
1c79356b A |
105 | |
106 | #include <netinet6/ipsec.h> | |
9bccf70c | 107 | #include <netinet6/ipsec6.h> |
1c79356b | 108 | #include <netinet6/ah.h> |
9bccf70c | 109 | #include <netinet6/ah6.h> |
1c79356b A |
110 | #if IPSEC_ESP |
111 | #include <netinet6/esp.h> | |
9bccf70c A |
112 | #include <netinet6/esp6.h> |
113 | #endif | |
9bccf70c A |
114 | |
115 | ||
116 | /* randomness */ | |
117 | #include <sys/random.h> | |
1c79356b A |
118 | |
119 | #include <net/net_osdep.h> | |
120 | ||
0a7de745 | 121 | #define FULLMASK 0xff |
55e303ae | 122 | |
91447636 A |
123 | lck_grp_t *sadb_mutex_grp; |
124 | lck_grp_attr_t *sadb_mutex_grp_attr; | |
125 | lck_attr_t *sadb_mutex_attr; | |
316670eb A |
126 | decl_lck_mtx_data(, sadb_mutex_data); |
127 | lck_mtx_t *sadb_mutex = &sadb_mutex_data; | |
2d21ac55 A |
128 | |
129 | lck_grp_t *pfkey_stat_mutex_grp; | |
130 | lck_grp_attr_t *pfkey_stat_mutex_grp_attr; | |
131 | lck_attr_t *pfkey_stat_mutex_attr; | |
316670eb A |
132 | decl_lck_mtx_data(, pfkey_stat_mutex_data); |
133 | lck_mtx_t *pfkey_stat_mutex = &pfkey_stat_mutex_data; | |
2d21ac55 | 134 | |
1c79356b A |
135 | /* |
136 | * Note on SA reference counting: | |
137 | * - SAs that are not in DEAD state will have (total external reference + 1) | |
138 | * following value in reference count field. they cannot be freed and are | |
139 | * referenced from SA header. | |
140 | * - SAs that are in DEAD state will have (total external reference) | |
141 | * in reference count field. they are ready to be freed. reference from | |
142 | * SA header will be removed in key_delsav(), when the reference count | |
143 | * field hits 0 (= no external reference other than from SA header. | |
144 | */ | |
145 | ||
146 | u_int32_t key_debug_level = 0; //### our sysctl is not dynamic | |
39236c6e | 147 | static int key_timehandler_running = 0; |
1c79356b A |
148 | static u_int key_spi_trycnt = 1000; |
149 | static u_int32_t key_spi_minval = 0x100; | |
0a7de745 | 150 | static u_int32_t key_spi_maxval = 0x0fffffff; /* XXX */ |
1c79356b | 151 | static u_int32_t policy_id = 0; |
0a7de745 A |
152 | static u_int key_int_random = 60; /*interval to initialize randseed,1(m)*/ |
153 | static u_int key_larval_lifetime = 30; /* interval to expire acquiring, 30(s)*/ | |
154 | static int key_blockacq_count = 10; /* counter for blocking SADB_ACQUIRE.*/ | |
155 | static int key_blockacq_lifetime = 20; /* lifetime for blocking SADB_ACQUIRE.*/ | |
156 | static int key_preferred_oldsa = 0; /* preferred old sa rather than new sa.*/ | |
157 | __private_extern__ int natt_keepalive_interval = 20; /* interval between natt keepalives.*/ | |
eb6b6ca3 A |
158 | static u_int32_t ipsec_policy_count = 0; |
159 | static u_int32_t ipsec_sav_count = 0; | |
1c79356b A |
160 | |
161 | static u_int32_t acq_seq = 0; | |
162 | static int key_tick_init_random = 0; | |
39037602 A |
163 | static u_int64_t up_time = 0; |
164 | __private_extern__ u_int64_t natt_now = 0; | |
1c79356b | 165 | |
0a7de745 A |
166 | static LIST_HEAD(_sptree, secpolicy) sptree[IPSEC_DIR_MAX]; /* SPD */ |
167 | static LIST_HEAD(_sahtree, secashead) sahtree; /* SAD */ | |
1c79356b | 168 | static LIST_HEAD(_regtree, secreg) regtree[SADB_SATYPE_MAX + 1]; |
ea3f0419 | 169 | static LIST_HEAD(_custom_sahtree, secashead) custom_sahtree; |
39236c6e A |
170 | /* registed list */ |
171 | ||
0a7de745 A |
172 | #define SPIHASHSIZE 128 |
173 | #define SPIHASH(x) (((x) ^ ((x) >> 16)) % SPIHASHSIZE) | |
91447636 A |
174 | static LIST_HEAD(_spihash, secasvar) spihash[SPIHASHSIZE]; |
175 | ||
1c79356b | 176 | #ifndef IPSEC_NONBLOCK_ACQUIRE |
0a7de745 | 177 | static LIST_HEAD(_acqtree, secacq) acqtree; /* acquiring list */ |
1c79356b | 178 | #endif |
0a7de745 | 179 | static LIST_HEAD(_spacqtree, secspacq) spacqtree; /* SP acquiring list */ |
1c79356b A |
180 | |
181 | struct key_cb key_cb; | |
182 | ||
183 | /* search order for SAs */ | |
55e303ae | 184 | static const u_int saorder_state_valid_prefer_old[] = { |
1c79356b | 185 | SADB_SASTATE_DYING, SADB_SASTATE_MATURE, |
1c79356b | 186 | }; |
55e303ae A |
187 | static const u_int saorder_state_valid_prefer_new[] = { |
188 | SADB_SASTATE_MATURE, SADB_SASTATE_DYING, | |
189 | }; | |
190 | static const u_int saorder_state_alive[] = { | |
1c79356b A |
191 | /* except DEAD */ |
192 | SADB_SASTATE_MATURE, SADB_SASTATE_DYING, SADB_SASTATE_LARVAL | |
193 | }; | |
55e303ae | 194 | static const u_int saorder_state_any[] = { |
1c79356b A |
195 | SADB_SASTATE_MATURE, SADB_SASTATE_DYING, |
196 | SADB_SASTATE_LARVAL, SADB_SASTATE_DEAD | |
197 | }; | |
198 | ||
9bccf70c | 199 | static const int minsize[] = { |
0a7de745 A |
200 | sizeof(struct sadb_msg), /* SADB_EXT_RESERVED */ |
201 | sizeof(struct sadb_sa), /* SADB_EXT_SA */ | |
202 | sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_CURRENT */ | |
203 | sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_HARD */ | |
204 | sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_SOFT */ | |
205 | sizeof(struct sadb_address), /* SADB_EXT_ADDRESS_SRC */ | |
206 | sizeof(struct sadb_address), /* SADB_EXT_ADDRESS_DST */ | |
207 | sizeof(struct sadb_address), /* SADB_EXT_ADDRESS_PROXY */ | |
208 | sizeof(struct sadb_key), /* SADB_EXT_KEY_AUTH */ | |
209 | sizeof(struct sadb_key), /* SADB_EXT_KEY_ENCRYPT */ | |
210 | sizeof(struct sadb_ident), /* SADB_EXT_IDENTITY_SRC */ | |
211 | sizeof(struct sadb_ident), /* SADB_EXT_IDENTITY_DST */ | |
212 | sizeof(struct sadb_sens), /* SADB_EXT_SENSITIVITY */ | |
213 | sizeof(struct sadb_prop), /* SADB_EXT_PROPOSAL */ | |
214 | sizeof(struct sadb_supported), /* SADB_EXT_SUPPORTED_AUTH */ | |
215 | sizeof(struct sadb_supported), /* SADB_EXT_SUPPORTED_ENCRYPT */ | |
216 | sizeof(struct sadb_spirange), /* SADB_EXT_SPIRANGE */ | |
217 | 0, /* SADB_X_EXT_KMPRIVATE */ | |
218 | sizeof(struct sadb_x_policy), /* SADB_X_EXT_POLICY */ | |
219 | sizeof(struct sadb_x_sa2), /* SADB_X_SA2 */ | |
b0d623f7 A |
220 | sizeof(struct sadb_session_id), /* SADB_EXT_SESSION_ID */ |
221 | sizeof(struct sadb_sastat), /* SADB_EXT_SASTAT */ | |
0a7de745 A |
222 | sizeof(struct sadb_x_ipsecif), /* SADB_X_EXT_IPSECIF */ |
223 | sizeof(struct sadb_address), /* SADB_X_EXT_ADDR_RANGE_SRC_START */ | |
224 | sizeof(struct sadb_address), /* SADB_X_EXT_ADDR_RANGE_SRC_END */ | |
225 | sizeof(struct sadb_address), /* SADB_X_EXT_ADDR_RANGE_DST_START */ | |
226 | sizeof(struct sadb_address), /* SADB_X_EXT_ADDR_RANGE_DST_END */ | |
227 | sizeof(struct sadb_address), /* SADB_EXT_MIGRATE_ADDRESS_SRC */ | |
228 | sizeof(struct sadb_address), /* SADB_EXT_MIGRATE_ADDRESS_DST */ | |
3e170ce0 | 229 | sizeof(struct sadb_x_ipsecif), /* SADB_X_EXT_MIGRATE_IPSECIF */ |
9bccf70c A |
230 | }; |
231 | static const int maxsize[] = { | |
0a7de745 A |
232 | sizeof(struct sadb_msg), /* SADB_EXT_RESERVED */ |
233 | sizeof(struct sadb_sa_2), /* SADB_EXT_SA */ | |
234 | sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_CURRENT */ | |
235 | sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_HARD */ | |
236 | sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_SOFT */ | |
237 | 0, /* SADB_EXT_ADDRESS_SRC */ | |
238 | 0, /* SADB_EXT_ADDRESS_DST */ | |
239 | 0, /* SADB_EXT_ADDRESS_PROXY */ | |
240 | 0, /* SADB_EXT_KEY_AUTH */ | |
241 | 0, /* SADB_EXT_KEY_ENCRYPT */ | |
242 | 0, /* SADB_EXT_IDENTITY_SRC */ | |
243 | 0, /* SADB_EXT_IDENTITY_DST */ | |
244 | 0, /* SADB_EXT_SENSITIVITY */ | |
245 | 0, /* SADB_EXT_PROPOSAL */ | |
246 | 0, /* SADB_EXT_SUPPORTED_AUTH */ | |
247 | 0, /* SADB_EXT_SUPPORTED_ENCRYPT */ | |
248 | sizeof(struct sadb_spirange), /* SADB_EXT_SPIRANGE */ | |
249 | 0, /* SADB_X_EXT_KMPRIVATE */ | |
250 | 0, /* SADB_X_EXT_POLICY */ | |
251 | sizeof(struct sadb_x_sa2), /* SADB_X_SA2 */ | |
b0d623f7 A |
252 | 0, /* SADB_EXT_SESSION_ID */ |
253 | 0, /* SADB_EXT_SASTAT */ | |
0a7de745 A |
254 | sizeof(struct sadb_x_ipsecif), /* SADB_X_EXT_IPSECIF */ |
255 | 0, /* SADB_X_EXT_ADDR_RANGE_SRC_START */ | |
39236c6e | 256 | 0, /* SADB_X_EXT_ADDR_RANGE_SRC_END */ |
0a7de745 | 257 | 0, /* SADB_X_EXT_ADDR_RANGE_DST_START */ |
39236c6e | 258 | 0, /* SADB_X_EXT_ADDR_RANGE_DST_END */ |
3e170ce0 A |
259 | 0, /* SADB_EXT_MIGRATE_ADDRESS_SRC */ |
260 | 0, /* SADB_EXT_MIGRATE_ADDRESS_DST */ | |
261 | sizeof(struct sadb_x_ipsecif), /* SADB_X_EXT_MIGRATE_IPSECIF */ | |
9bccf70c A |
262 | }; |
263 | ||
264 | static int ipsec_esp_keymin = 256; | |
265 | static int ipsec_esp_auth = 0; | |
266 | static int ipsec_ah_keymin = 128; | |
267 | ||
1c79356b | 268 | SYSCTL_DECL(_net_key); |
6d2010ae | 269 | /* Thread safe: no accumulated state */ |
0a7de745 A |
270 | SYSCTL_INT(_net_key, KEYCTL_DEBUG_LEVEL, debug, CTLFLAG_RW | CTLFLAG_LOCKED, \ |
271 | &key_debug_level, 0, ""); | |
9bccf70c | 272 | |
1c79356b A |
273 | |
274 | /* max count of trial for the decision of spi value */ | |
0a7de745 A |
275 | SYSCTL_INT(_net_key, KEYCTL_SPI_TRY, spi_trycnt, CTLFLAG_RW | CTLFLAG_LOCKED, \ |
276 | &key_spi_trycnt, 0, ""); | |
1c79356b A |
277 | |
278 | /* minimum spi value to allocate automatically. */ | |
0a7de745 A |
279 | SYSCTL_INT(_net_key, KEYCTL_SPI_MIN_VALUE, spi_minval, CTLFLAG_RW | CTLFLAG_LOCKED, \ |
280 | &key_spi_minval, 0, ""); | |
1c79356b A |
281 | |
282 | /* maximun spi value to allocate automatically. */ | |
0a7de745 A |
283 | SYSCTL_INT(_net_key, KEYCTL_SPI_MAX_VALUE, spi_maxval, CTLFLAG_RW | CTLFLAG_LOCKED, \ |
284 | &key_spi_maxval, 0, ""); | |
1c79356b A |
285 | |
286 | /* interval to initialize randseed */ | |
0a7de745 A |
287 | SYSCTL_INT(_net_key, KEYCTL_RANDOM_INT, int_random, CTLFLAG_RW | CTLFLAG_LOCKED, \ |
288 | &key_int_random, 0, ""); | |
1c79356b | 289 | |
6d2010ae | 290 | /* lifetime for larval SA; thread safe due to > compare */ |
0a7de745 A |
291 | SYSCTL_INT(_net_key, KEYCTL_LARVAL_LIFETIME, larval_lifetime, CTLFLAG_RW | CTLFLAG_LOCKED, \ |
292 | &key_larval_lifetime, 0, ""); | |
1c79356b A |
293 | |
294 | /* counter for blocking to send SADB_ACQUIRE to IKEd */ | |
0a7de745 A |
295 | SYSCTL_INT(_net_key, KEYCTL_BLOCKACQ_COUNT, blockacq_count, CTLFLAG_RW | CTLFLAG_LOCKED, \ |
296 | &key_blockacq_count, 0, ""); | |
1c79356b | 297 | |
6d2010ae | 298 | /* lifetime for blocking to send SADB_ACQUIRE to IKEd: Thread safe, > compare */ |
0a7de745 A |
299 | SYSCTL_INT(_net_key, KEYCTL_BLOCKACQ_LIFETIME, blockacq_lifetime, CTLFLAG_RW | CTLFLAG_LOCKED, \ |
300 | &key_blockacq_lifetime, 0, ""); | |
1c79356b | 301 | |
55e303ae | 302 | /* ESP auth */ |
0a7de745 A |
303 | SYSCTL_INT(_net_key, KEYCTL_ESP_AUTH, esp_auth, CTLFLAG_RW | CTLFLAG_LOCKED, \ |
304 | &ipsec_esp_auth, 0, ""); | |
55e303ae | 305 | |
9bccf70c | 306 | /* minimum ESP key length */ |
0a7de745 A |
307 | SYSCTL_INT(_net_key, KEYCTL_ESP_KEYMIN, esp_keymin, CTLFLAG_RW | CTLFLAG_LOCKED, \ |
308 | &ipsec_esp_keymin, 0, ""); | |
9bccf70c A |
309 | |
310 | /* minimum AH key length */ | |
0a7de745 A |
311 | SYSCTL_INT(_net_key, KEYCTL_AH_KEYMIN, ah_keymin, CTLFLAG_RW | CTLFLAG_LOCKED, \ |
312 | &ipsec_ah_keymin, 0, ""); | |
1c79356b | 313 | |
55e303ae | 314 | /* perfered old SA rather than new SA */ |
0a7de745 A |
315 | SYSCTL_INT(_net_key, KEYCTL_PREFERED_OLDSA, prefered_oldsa, CTLFLAG_RW | CTLFLAG_LOCKED, \ |
316 | &key_preferred_oldsa, 0, ""); | |
55e303ae A |
317 | |
318 | /* time between NATT keepalives in seconds, 0 disabled */ | |
0a7de745 A |
319 | SYSCTL_INT(_net_key, KEYCTL_NATT_KEEPALIVE_INTERVAL, natt_keepalive_interval, CTLFLAG_RW | CTLFLAG_LOCKED, \ |
320 | &natt_keepalive_interval, 0, ""); | |
55e303ae | 321 | |
91447636 | 322 | /* PF_KEY statistics */ |
0a7de745 A |
323 | SYSCTL_STRUCT(_net_key, KEYCTL_PFKEYSTAT, pfkeystat, CTLFLAG_RD | CTLFLAG_LOCKED, \ |
324 | &pfkeystat, pfkeystat, ""); | |
91447636 | 325 | |
1c79356b A |
326 | #ifndef LIST_FOREACH |
327 | #define LIST_FOREACH(elm, head, field) \ | |
39236c6e | 328 | for (elm = LIST_FIRST(head); elm; elm = LIST_NEXT(elm, field)) |
1c79356b A |
329 | #endif |
330 | #define __LIST_CHAINED(elm) \ | |
39236c6e | 331 | (!((elm)->chain.le_next == NULL && (elm)->chain.le_prev == NULL)) |
1c79356b A |
332 | #define LIST_INSERT_TAIL(head, elm, type, field) \ |
333 | do {\ | |
39236c6e A |
334 | struct type *curelm = LIST_FIRST(head); \ |
335 | if (curelm == NULL) {\ | |
336 | LIST_INSERT_HEAD(head, elm, field); \ | |
337 | } else { \ | |
338 | while (LIST_NEXT(curelm, field)) \ | |
339 | curelm = LIST_NEXT(curelm, field);\ | |
340 | LIST_INSERT_AFTER(curelm, elm, field);\ | |
341 | }\ | |
1c79356b A |
342 | } while (0) |
343 | ||
344 | #define KEY_CHKSASTATE(head, sav, name) \ | |
345 | do { \ | |
0a7de745 | 346 | if ((head) != (sav)) { \ |
39236c6e | 347 | ipseclog((LOG_DEBUG, "%s: state mismatched (TREE=%d SA=%d)\n", \ |
0a7de745 A |
348 | (name), (head), (sav))); \ |
349 | continue; \ | |
350 | } \ | |
1c79356b A |
351 | } while (0) |
352 | ||
353 | #define KEY_CHKSPDIR(head, sp, name) \ | |
354 | do { \ | |
0a7de745 | 355 | if ((head) != (sp)) { \ |
39236c6e | 356 | ipseclog((LOG_DEBUG, "%s: direction mismatched (TREE=%d SP=%d), " \ |
0a7de745 A |
357 | "anyway continue.\n", \ |
358 | (name), (head), (sp))); \ | |
359 | } \ | |
1c79356b A |
360 | } while (0) |
361 | ||
362 | #if 1 | |
2d21ac55 | 363 | #define KMALLOC_WAIT(p, t, n) \ |
eb6b6ca3 | 364 | ((p) = (t) _MALLOC((n), M_SECA, M_WAITOK)) |
2d21ac55 | 365 | #define KMALLOC_NOWAIT(p, t, n) \ |
eb6b6ca3 | 366 | ((p) = (t) _MALLOC((n), M_SECA, M_NOWAIT)) |
1c79356b | 367 | #define KFREE(p) \ |
39236c6e | 368 | _FREE((caddr_t)(p), M_SECA); |
1c79356b | 369 | #else |
2d21ac55 | 370 | #define KMALLOC_WAIT(p, t, n) \ |
1c79356b | 371 | do { \ |
39236c6e A |
372 | ((p) = (t)_MALLOC((u_int32_t)(n), M_SECA, M_WAITOK)); \ |
373 | printf("%s %d: %p <- KMALLOC_WAIT(%s, %d)\n", \ | |
374 | __FILE__, __LINE__, (p), #t, n); \ | |
1c79356b | 375 | } while (0) |
2d21ac55 | 376 | #define KMALLOC_NOWAIT(p, t, n) \ |
39236c6e A |
377 | do { \ |
378 | ((p) = (t)_MALLOC((u_int32_t)(n), M_SECA, M_NOWAIT)); \ | |
379 | printf("%s %d: %p <- KMALLOC_NOWAIT(%s, %d)\n", \ | |
380 | __FILE__, __LINE__, (p), #t, n); \ | |
381 | } while (0) | |
1c79356b A |
382 | |
383 | #define KFREE(p) \ | |
39236c6e A |
384 | do { \ |
385 | printf("%s %d: %p -> KFREE()\n", __FILE__, __LINE__, (p)); \ | |
386 | _FREE((caddr_t)(p), M_SECA); \ | |
387 | } while (0) | |
1c79356b A |
388 | #endif |
389 | ||
390 | /* | |
391 | * set parameters into secpolicyindex buffer. | |
392 | * Must allocate secpolicyindex buffer passed to this function. | |
393 | */ | |
39236c6e | 394 | #define KEY_SETSECSPIDX(_dir, s, d, ps, pd, ulp, ifp, s_s, s_e, d_s, d_e, idx) \ |
1c79356b | 395 | do { \ |
39236c6e A |
396 | bzero((idx), sizeof(struct secpolicyindex)); \ |
397 | (idx)->dir = (_dir); \ | |
398 | (idx)->prefs = (ps); \ | |
399 | (idx)->prefd = (pd); \ | |
400 | (idx)->ul_proto = (ulp); \ | |
401 | (idx)->internal_if = (ifp); \ | |
402 | if (s) bcopy((s), &(idx)->src, ((struct sockaddr *)(s))->sa_len); \ | |
403 | if (d) bcopy((d), &(idx)->dst, ((struct sockaddr *)(d))->sa_len); \ | |
404 | if (s_s) bcopy((s_s), &(idx)->src_range.start, ((struct sockaddr *)(s_s))->sa_len); \ | |
405 | if (s_e) bcopy((s_e), &(idx)->src_range.end, ((struct sockaddr *)(s_e))->sa_len); \ | |
406 | if (d_s) bcopy((d_s), &(idx)->dst_range.start, ((struct sockaddr *)(d_s))->sa_len); \ | |
407 | if (d_e) bcopy((d_e), &(idx)->dst_range.end, ((struct sockaddr *)(d_e))->sa_len); \ | |
1c79356b A |
408 | } while (0) |
409 | ||
410 | /* | |
411 | * set parameters into secasindex buffer. | |
412 | * Must allocate secasindex buffer before calling this function. | |
413 | */ | |
fe8ab488 | 414 | #define KEY_SETSECASIDX(p, m, r, s, d, ifi, idx) \ |
1c79356b | 415 | do { \ |
39236c6e A |
416 | bzero((idx), sizeof(struct secasindex)); \ |
417 | (idx)->proto = (p); \ | |
418 | (idx)->mode = (m); \ | |
419 | (idx)->reqid = (r); \ | |
420 | bcopy((s), &(idx)->src, ((const struct sockaddr *)(s))->sa_len); \ | |
421 | bcopy((d), &(idx)->dst, ((const struct sockaddr *)(d))->sa_len); \ | |
0a7de745 | 422 | (idx)->ipsec_ifindex = (ifi); \ |
1c79356b A |
423 | } while (0) |
424 | ||
425 | /* key statistics */ | |
426 | struct _keystat { | |
b0d623f7 | 427 | u_int32_t getspi_count; /* the avarage of count to try to get new SPI */ |
1c79356b A |
428 | } keystat; |
429 | ||
9bccf70c A |
430 | struct sadb_msghdr { |
431 | struct sadb_msg *msg; | |
432 | struct sadb_ext *ext[SADB_EXT_MAX + 1]; | |
433 | int extoff[SADB_EXT_MAX + 1]; | |
434 | int extlen[SADB_EXT_MAX + 1]; | |
435 | }; | |
436 | ||
39236c6e | 437 | static struct secpolicy *__key_getspbyid(u_int32_t id); |
2d21ac55 A |
438 | static struct secasvar *key_do_allocsa_policy(struct secashead *, u_int, u_int16_t); |
439 | static int key_do_get_translated_port(struct secashead *, struct secasvar *, u_int); | |
91447636 A |
440 | static void key_delsp(struct secpolicy *); |
441 | static struct secpolicy *key_getsp(struct secpolicyindex *); | |
f427ee49 | 442 | static u_int16_t key_newreqid(void); |
91447636 | 443 | static struct mbuf *key_gather_mbuf(struct mbuf *, |
0a7de745 | 444 | const struct sadb_msghdr *, int, int, int *); |
91447636 | 445 | static int key_spdadd(struct socket *, struct mbuf *, |
0a7de745 | 446 | const struct sadb_msghdr *); |
91447636 A |
447 | static u_int32_t key_getnewspid(void); |
448 | static int key_spddelete(struct socket *, struct mbuf *, | |
0a7de745 | 449 | const struct sadb_msghdr *); |
91447636 | 450 | static int key_spddelete2(struct socket *, struct mbuf *, |
0a7de745 | 451 | const struct sadb_msghdr *); |
39236c6e | 452 | static int key_spdenable(struct socket *, struct mbuf *, |
0a7de745 | 453 | const struct sadb_msghdr *); |
39236c6e | 454 | static int key_spddisable(struct socket *, struct mbuf *, |
0a7de745 | 455 | const struct sadb_msghdr *); |
91447636 | 456 | static int key_spdget(struct socket *, struct mbuf *, |
0a7de745 | 457 | const struct sadb_msghdr *); |
91447636 | 458 | static int key_spdflush(struct socket *, struct mbuf *, |
0a7de745 | 459 | const struct sadb_msghdr *); |
91447636 | 460 | static int key_spddump(struct socket *, struct mbuf *, |
0a7de745 | 461 | const struct sadb_msghdr *); |
91447636 | 462 | static struct mbuf *key_setdumpsp(struct secpolicy *, |
0a7de745 | 463 | u_int8_t, u_int32_t, u_int32_t); |
91447636 A |
464 | static u_int key_getspreqmsglen(struct secpolicy *); |
465 | static int key_spdexpire(struct secpolicy *); | |
ea3f0419 | 466 | static struct secashead *key_newsah(struct secasindex *, ifnet_t, u_int, u_int8_t, u_int16_t); |
91447636 | 467 | static struct secasvar *key_newsav(struct mbuf *, |
0a7de745 A |
468 | const struct sadb_msghdr *, struct secashead *, int *, |
469 | struct socket *); | |
ea3f0419 | 470 | static struct secashead *key_getsah(struct secasindex *, u_int16_t); |
91447636 A |
471 | static struct secasvar *key_checkspidup(struct secasindex *, u_int32_t); |
472 | static void key_setspi __P((struct secasvar *, u_int32_t)); | |
473 | static struct secasvar *key_getsavbyspi(struct secashead *, u_int32_t); | |
474 | static int key_setsaval(struct secasvar *, struct mbuf *, | |
0a7de745 | 475 | const struct sadb_msghdr *); |
91447636 A |
476 | static int key_mature(struct secasvar *); |
477 | static struct mbuf *key_setdumpsa(struct secasvar *, u_int8_t, | |
0a7de745 | 478 | u_int8_t, u_int32_t, u_int32_t); |
91447636 | 479 | static struct mbuf *key_setsadbmsg(u_int8_t, u_int16_t, u_int8_t, |
0a7de745 | 480 | u_int32_t, pid_t, u_int16_t); |
91447636 A |
481 | static struct mbuf *key_setsadbsa(struct secasvar *); |
482 | static struct mbuf *key_setsadbaddr(u_int16_t, | |
f427ee49 A |
483 | struct sockaddr *, size_t, u_int8_t); |
484 | static struct mbuf *key_setsadbipsecif(ifnet_t, ifnet_t, ifnet_t, u_int8_t); | |
fe8ab488 | 485 | static struct mbuf *key_setsadbxsa2(u_int8_t, u_int32_t, u_int32_t, u_int16_t); |
91447636 | 486 | static struct mbuf *key_setsadbxpolicy(u_int16_t, u_int8_t, |
0a7de745 | 487 | u_int32_t); |
91447636 | 488 | static void *key_newbuf(const void *, u_int); |
91447636 | 489 | static int key_ismyaddr6(struct sockaddr_in6 *); |
b0d623f7 | 490 | static void key_update_natt_keepalive_timestamp(struct secasvar *, struct secasvar *); |
55e303ae A |
491 | |
492 | /* flags for key_cmpsaidx() */ | |
0a7de745 A |
493 | #define CMP_HEAD 0x1 /* protocol, addresses. */ |
494 | #define CMP_PORT 0x2 /* additionally HEAD, reqid, mode. */ | |
495 | #define CMP_REQID 0x4 /* additionally HEAD, reqid. */ | |
2d21ac55 | 496 | #define CMP_MODE 0x8 /* additionally mode. */ |
0a7de745 | 497 | #define CMP_EXACTLY 0xF /* all elements. */ |
91447636 A |
498 | static int key_cmpsaidx(struct secasindex *, struct secasindex *, int); |
499 | ||
500 | static int key_cmpspidx_exactly(struct secpolicyindex *, | |
0a7de745 | 501 | struct secpolicyindex *); |
91447636 | 502 | static int key_cmpspidx_withmask(struct secpolicyindex *, |
0a7de745 | 503 | struct secpolicyindex *); |
91447636 | 504 | static int key_sockaddrcmp(struct sockaddr *, struct sockaddr *, int); |
39236c6e | 505 | static int key_is_addr_in_range(struct sockaddr_storage *, struct secpolicyaddrrange *); |
91447636 A |
506 | static int key_bbcmp(caddr_t, caddr_t, u_int); |
507 | static void key_srandom(void); | |
f427ee49 | 508 | static u_int8_t key_satype2proto(u_int8_t); |
91447636 A |
509 | static u_int8_t key_proto2satype(u_int16_t); |
510 | ||
511 | static int key_getspi(struct socket *, struct mbuf *, | |
0a7de745 | 512 | const struct sadb_msghdr *); |
91447636 A |
513 | static u_int32_t key_do_getnewspi(struct sadb_spirange *, struct secasindex *); |
514 | static int key_update(struct socket *, struct mbuf *, | |
0a7de745 | 515 | const struct sadb_msghdr *); |
91447636 | 516 | static int key_add(struct socket *, struct mbuf *, const struct sadb_msghdr *); |
91447636 A |
517 | static struct mbuf *key_getmsgbuf_x1(struct mbuf *, const struct sadb_msghdr *); |
518 | static int key_delete(struct socket *, struct mbuf *, | |
0a7de745 | 519 | const struct sadb_msghdr *); |
91447636 A |
520 | static int key_get(struct socket *, struct mbuf *, const struct sadb_msghdr *); |
521 | ||
522 | static void key_getcomb_setlifetime(struct sadb_comb *); | |
9bccf70c | 523 | #if IPSEC_ESP |
91447636 | 524 | static struct mbuf *key_getcomb_esp(void); |
9bccf70c | 525 | #endif |
91447636 | 526 | static struct mbuf *key_getcomb_ah(void); |
91447636 | 527 | static struct mbuf *key_getprop(const struct secasindex *); |
9bccf70c | 528 | |
91447636 | 529 | static int key_acquire(struct secasindex *, struct secpolicy *); |
9bccf70c | 530 | #ifndef IPSEC_NONBLOCK_ACQUIRE |
91447636 A |
531 | static struct secacq *key_newacq(struct secasindex *); |
532 | static struct secacq *key_getacq(struct secasindex *); | |
533 | static struct secacq *key_getacqbyseq(u_int32_t); | |
9bccf70c | 534 | #endif |
91447636 A |
535 | static struct secspacq *key_newspacq(struct secpolicyindex *); |
536 | static struct secspacq *key_getspacq(struct secpolicyindex *); | |
537 | static int key_acquire2(struct socket *, struct mbuf *, | |
0a7de745 | 538 | const struct sadb_msghdr *); |
91447636 | 539 | static int key_register(struct socket *, struct mbuf *, |
0a7de745 | 540 | const struct sadb_msghdr *); |
91447636 A |
541 | static int key_expire(struct secasvar *); |
542 | static int key_flush(struct socket *, struct mbuf *, | |
0a7de745 | 543 | const struct sadb_msghdr *); |
91447636 A |
544 | static int key_dump(struct socket *, struct mbuf *, const struct sadb_msghdr *); |
545 | static int key_promisc(struct socket *, struct mbuf *, | |
0a7de745 | 546 | const struct sadb_msghdr *); |
91447636 A |
547 | static int key_senderror(struct socket *, struct mbuf *, int); |
548 | static int key_validate_ext(const struct sadb_ext *, int); | |
549 | static int key_align(struct mbuf *, struct sadb_msghdr *); | |
91447636 | 550 | static struct mbuf *key_alloc_mbuf(int); |
0a7de745 A |
551 | static int key_getsastat(struct socket *, struct mbuf *, const struct sadb_msghdr *); |
552 | static int key_migrate(struct socket *, struct mbuf *, const struct sadb_msghdr *); | |
ecc0ceb4 | 553 | static void bzero_keys(const struct sadb_msghdr *); |
9bccf70c A |
554 | |
555 | extern int ipsec_bypass; | |
b0d623f7 A |
556 | extern int esp_udp_encap_port; |
557 | int ipsec_send_natt_keepalive(struct secasvar *sav); | |
3e170ce0 | 558 | bool ipsec_fill_offload_frame(ifnet_t ifp, struct secasvar *sav, struct ifnet_keepalive_offload_frame *frame, size_t frame_data_offset); |
1c79356b | 559 | |
39236c6e | 560 | void key_init(struct protosw *, struct domain *); |
91447636 A |
561 | |
562 | /* | |
563 | * PF_KEY init | |
6d2010ae | 564 | * setup locks, call raw_init(), and then init timer and associated data |
91447636 A |
565 | * |
566 | */ | |
567 | void | |
39236c6e | 568 | key_init(struct protosw *pp, struct domain *dp) |
91447636 | 569 | { |
39236c6e | 570 | static int key_initialized = 0; |
91447636 | 571 | int i; |
0a7de745 A |
572 | |
573 | VERIFY((pp->pr_flags & (PR_INITIALIZED | PR_ATTACHED)) == PR_ATTACHED); | |
99c3a104 A |
574 | |
575 | _CASSERT(PFKEY_ALIGN8(sizeof(struct sadb_msg)) <= _MHLEN); | |
cb323159 | 576 | _CASSERT(MAX_REPLAY_WINDOWS == MBUF_TC_MAX); |
0a7de745 A |
577 | |
578 | if (key_initialized) { | |
39236c6e | 579 | return; |
0a7de745 | 580 | } |
39236c6e | 581 | key_initialized = 1; |
99c3a104 | 582 | |
91447636 A |
583 | sadb_mutex_grp_attr = lck_grp_attr_alloc_init(); |
584 | sadb_mutex_grp = lck_grp_alloc_init("sadb", sadb_mutex_grp_attr); | |
585 | sadb_mutex_attr = lck_attr_alloc_init(); | |
0a7de745 | 586 | |
39236c6e A |
587 | lck_mtx_init(sadb_mutex, sadb_mutex_grp, sadb_mutex_attr); |
588 | ||
2d21ac55 A |
589 | pfkey_stat_mutex_grp_attr = lck_grp_attr_alloc_init(); |
590 | pfkey_stat_mutex_grp = lck_grp_alloc_init("pfkey_stat", pfkey_stat_mutex_grp_attr); | |
591 | pfkey_stat_mutex_attr = lck_attr_alloc_init(); | |
0a7de745 | 592 | |
316670eb | 593 | lck_mtx_init(pfkey_stat_mutex, pfkey_stat_mutex_grp, pfkey_stat_mutex_attr); |
0a7de745 A |
594 | |
595 | for (i = 0; i < SPIHASHSIZE; i++) { | |
91447636 | 596 | LIST_INIT(&spihash[i]); |
0a7de745 A |
597 | } |
598 | ||
39236c6e | 599 | raw_init(pp, dp); |
0a7de745 | 600 | |
39236c6e A |
601 | bzero((caddr_t)&key_cb, sizeof(key_cb)); |
602 | ||
6d2010ae A |
603 | for (i = 0; i < IPSEC_DIR_MAX; i++) { |
604 | LIST_INIT(&sptree[i]); | |
605 | } | |
606 | ipsec_policy_count = 0; | |
0a7de745 | 607 | |
6d2010ae | 608 | LIST_INIT(&sahtree); |
ea3f0419 | 609 | LIST_INIT(&custom_sahtree); |
0a7de745 | 610 | |
6d2010ae A |
611 | for (i = 0; i <= SADB_SATYPE_MAX; i++) { |
612 | LIST_INIT(®tree[i]); | |
613 | } | |
614 | ipsec_sav_count = 0; | |
0a7de745 | 615 | |
6d2010ae A |
616 | #ifndef IPSEC_NONBLOCK_ACQUIRE |
617 | LIST_INIT(&acqtree); | |
618 | #endif | |
619 | LIST_INIT(&spacqtree); | |
0a7de745 | 620 | |
6d2010ae A |
621 | /* system default */ |
622 | #if INET | |
623 | ip4_def_policy.policy = IPSEC_POLICY_NONE; | |
0a7de745 | 624 | ip4_def_policy.refcnt++; /*never reclaim this*/ |
6d2010ae | 625 | #endif |
6d2010ae | 626 | ip6_def_policy.policy = IPSEC_POLICY_NONE; |
0a7de745 | 627 | ip6_def_policy.refcnt++; /*never reclaim this*/ |
0a7de745 | 628 | |
39236c6e | 629 | key_timehandler_running = 0; |
0a7de745 | 630 | |
6d2010ae A |
631 | /* initialize key statistics */ |
632 | keystat.getspi_count = 1; | |
0a7de745 | 633 | |
cb323159 | 634 | esp_init(); |
6d2010ae A |
635 | #ifndef __APPLE__ |
636 | printf("IPsec: Initialized Security Association Processing.\n"); | |
637 | #endif | |
91447636 A |
638 | } |
639 | ||
39236c6e A |
640 | static void |
641 | key_start_timehandler(void) | |
642 | { | |
643 | /* must be called while locked */ | |
5ba3f43e | 644 | LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_OWNED); |
39236c6e A |
645 | if (key_timehandler_running == 0) { |
646 | key_timehandler_running = 1; | |
647 | (void)timeout((void *)key_timehandler, (void *)0, hz); | |
648 | } | |
0a7de745 | 649 | |
fe8ab488 | 650 | /* Turn off the ipsec bypass */ |
0a7de745 | 651 | if (ipsec_bypass != 0) { |
fe8ab488 | 652 | ipsec_bypass = 0; |
0a7de745 | 653 | } |
39236c6e | 654 | } |
91447636 | 655 | |
1c79356b A |
656 | /* %%% IPsec policy management */ |
657 | /* | |
658 | * allocating a SP for OUTBOUND or INBOUND packet. | |
659 | * Must call key_freesp() later. | |
660 | * OUT: NULL: not found | |
661 | * others: found and return the pointer. | |
662 | */ | |
663 | struct secpolicy * | |
6d2010ae | 664 | key_allocsp( |
0a7de745 A |
665 | struct secpolicyindex *spidx, |
666 | u_int dir) | |
1c79356b A |
667 | { |
668 | struct secpolicy *sp; | |
9bccf70c | 669 | struct timeval tv; |
0a7de745 | 670 | |
5ba3f43e | 671 | LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_NOTOWNED); |
1c79356b | 672 | /* sanity check */ |
0a7de745 | 673 | if (spidx == NULL) { |
1c79356b | 674 | panic("key_allocsp: NULL pointer is passed.\n"); |
0a7de745 A |
675 | } |
676 | ||
1c79356b A |
677 | /* check direction */ |
678 | switch (dir) { | |
0a7de745 A |
679 | case IPSEC_DIR_INBOUND: |
680 | case IPSEC_DIR_OUTBOUND: | |
681 | break; | |
682 | default: | |
683 | panic("key_allocsp: Invalid direction is passed.\n"); | |
1c79356b | 684 | } |
0a7de745 | 685 | |
1c79356b | 686 | /* get a SP entry */ |
1c79356b | 687 | KEYDEBUG(KEYDEBUG_IPSEC_DATA, |
0a7de745 A |
688 | printf("*** objects\n"); |
689 | kdebug_secpolicyindex(spidx)); | |
690 | ||
2d21ac55 | 691 | lck_mtx_lock(sadb_mutex); |
1c79356b A |
692 | LIST_FOREACH(sp, &sptree[dir], chain) { |
693 | KEYDEBUG(KEYDEBUG_IPSEC_DATA, | |
0a7de745 A |
694 | printf("*** in SPD\n"); |
695 | kdebug_secpolicyindex(&sp->spidx)); | |
696 | ||
697 | if (sp->state == IPSEC_SPSTATE_DEAD) { | |
1c79356b | 698 | continue; |
0a7de745 A |
699 | } |
700 | ||
39236c6e | 701 | /* If the policy is disabled, skip */ |
0a7de745 | 702 | if (sp->disabled > 0) { |
39236c6e | 703 | continue; |
0a7de745 A |
704 | } |
705 | ||
706 | /* If the incoming spidx specifies bound if, | |
707 | * ignore unbound policies*/ | |
708 | if (spidx->internal_if != NULL | |
709 | && (sp->spidx.internal_if == NULL || sp->ipsec_if == NULL)) { | |
710 | continue; | |
711 | } | |
712 | ||
713 | if (key_cmpspidx_withmask(&sp->spidx, spidx)) { | |
1c79356b | 714 | goto found; |
0a7de745 | 715 | } |
1c79356b | 716 | } |
2d21ac55 | 717 | lck_mtx_unlock(sadb_mutex); |
1c79356b | 718 | return NULL; |
0a7de745 | 719 | |
1c79356b | 720 | found: |
0a7de745 | 721 | |
1c79356b | 722 | /* found a SPD entry */ |
9bccf70c A |
723 | microtime(&tv); |
724 | sp->lastused = tv.tv_sec; | |
1c79356b | 725 | sp->refcnt++; |
2d21ac55 | 726 | lck_mtx_unlock(sadb_mutex); |
0a7de745 | 727 | |
2d21ac55 A |
728 | /* sanity check */ |
729 | KEY_CHKSPDIR(sp->spidx.dir, dir, "key_allocsp"); | |
1c79356b | 730 | KEYDEBUG(KEYDEBUG_IPSEC_STAMP, |
39236c6e A |
731 | printf("DP key_allocsp cause refcnt++:%d SP:0x%llx\n", |
732 | sp->refcnt, (uint64_t)VM_KERNEL_ADDRPERM(sp))); | |
1c79356b A |
733 | return sp; |
734 | } | |
735 | ||
736 | /* | |
9bccf70c A |
737 | * return a policy that matches this particular inbound packet. |
738 | * XXX slow | |
739 | */ | |
740 | struct secpolicy * | |
6d2010ae | 741 | key_gettunnel( |
0a7de745 A |
742 | struct sockaddr *osrc, |
743 | struct sockaddr *odst, | |
744 | struct sockaddr *isrc, | |
745 | struct sockaddr *idst) | |
9bccf70c A |
746 | { |
747 | struct secpolicy *sp; | |
748 | const int dir = IPSEC_DIR_INBOUND; | |
749 | struct timeval tv; | |
9bccf70c A |
750 | struct ipsecrequest *r1, *r2, *p; |
751 | struct sockaddr *os, *od, *is, *id; | |
752 | struct secpolicyindex spidx; | |
0a7de745 | 753 | |
55e303ae A |
754 | if (isrc->sa_family != idst->sa_family) { |
755 | ipseclog((LOG_ERR, "protocol family mismatched %d != %d\n.", | |
0a7de745 | 756 | isrc->sa_family, idst->sa_family)); |
55e303ae A |
757 | return NULL; |
758 | } | |
0a7de745 | 759 | |
2d21ac55 | 760 | lck_mtx_lock(sadb_mutex); |
9bccf70c | 761 | LIST_FOREACH(sp, &sptree[dir], chain) { |
0a7de745 | 762 | if (sp->state == IPSEC_SPSTATE_DEAD) { |
9bccf70c | 763 | continue; |
0a7de745 A |
764 | } |
765 | ||
9bccf70c A |
766 | r1 = r2 = NULL; |
767 | for (p = sp->req; p; p = p->next) { | |
0a7de745 | 768 | if (p->saidx.mode != IPSEC_MODE_TUNNEL) { |
9bccf70c | 769 | continue; |
0a7de745 A |
770 | } |
771 | ||
9bccf70c A |
772 | r1 = r2; |
773 | r2 = p; | |
0a7de745 | 774 | |
9bccf70c A |
775 | if (!r1) { |
776 | /* here we look at address matches only */ | |
777 | spidx = sp->spidx; | |
778 | if (isrc->sa_len > sizeof(spidx.src) || | |
0a7de745 | 779 | idst->sa_len > sizeof(spidx.dst)) { |
9bccf70c | 780 | continue; |
0a7de745 | 781 | } |
9bccf70c A |
782 | bcopy(isrc, &spidx.src, isrc->sa_len); |
783 | bcopy(idst, &spidx.dst, idst->sa_len); | |
0a7de745 | 784 | if (!key_cmpspidx_withmask(&sp->spidx, &spidx)) { |
39236c6e | 785 | continue; |
0a7de745 | 786 | } |
9bccf70c A |
787 | } else { |
788 | is = (struct sockaddr *)&r1->saidx.src; | |
789 | id = (struct sockaddr *)&r1->saidx.dst; | |
790 | if (key_sockaddrcmp(is, isrc, 0) || | |
0a7de745 | 791 | key_sockaddrcmp(id, idst, 0)) { |
9bccf70c | 792 | continue; |
0a7de745 | 793 | } |
9bccf70c | 794 | } |
0a7de745 | 795 | |
9bccf70c A |
796 | os = (struct sockaddr *)&r2->saidx.src; |
797 | od = (struct sockaddr *)&r2->saidx.dst; | |
798 | if (key_sockaddrcmp(os, osrc, 0) || | |
0a7de745 | 799 | key_sockaddrcmp(od, odst, 0)) { |
9bccf70c | 800 | continue; |
0a7de745 A |
801 | } |
802 | ||
9bccf70c A |
803 | goto found; |
804 | } | |
805 | } | |
2d21ac55 | 806 | lck_mtx_unlock(sadb_mutex); |
9bccf70c | 807 | return NULL; |
0a7de745 | 808 | |
9bccf70c A |
809 | found: |
810 | microtime(&tv); | |
811 | sp->lastused = tv.tv_sec; | |
812 | sp->refcnt++; | |
2d21ac55 | 813 | lck_mtx_unlock(sadb_mutex); |
9bccf70c A |
814 | return sp; |
815 | } | |
816 | ||
0a7de745 A |
817 | struct secasvar * |
818 | key_alloc_outbound_sav_for_interface(ifnet_t interface, int family, | |
819 | struct sockaddr *src, | |
820 | struct sockaddr *dst) | |
fe8ab488 A |
821 | { |
822 | struct secashead *sah; | |
823 | struct secasvar *sav; | |
824 | u_int stateidx; | |
825 | u_int state; | |
826 | const u_int *saorder_state_valid; | |
827 | int arraysize; | |
828 | struct sockaddr_in *sin; | |
829 | u_int16_t dstport; | |
d9a64523 | 830 | bool strict = true; |
0a7de745 | 831 | |
d9a64523 | 832 | if (interface == NULL) { |
0a7de745 | 833 | return NULL; |
d9a64523 | 834 | } |
0a7de745 | 835 | |
5ba3f43e | 836 | LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_NOTOWNED); |
0a7de745 | 837 | |
fe8ab488 | 838 | lck_mtx_lock(sadb_mutex); |
d9a64523 A |
839 | |
840 | do { | |
841 | LIST_FOREACH(sah, &sahtree, chain) { | |
842 | if (sah->state == SADB_SASTATE_DEAD) { | |
843 | continue; | |
fe8ab488 | 844 | } |
d9a64523 | 845 | if (sah->ipsec_if == interface && |
0a7de745 A |
846 | (family == AF_INET6 || family == AF_INET) && |
847 | sah->dir == IPSEC_DIR_OUTBOUND) { | |
d9a64523 | 848 | if (strict && |
0a7de745 A |
849 | sah->saidx.mode == IPSEC_MODE_TRANSPORT && |
850 | src != NULL && dst != NULL) { | |
d9a64523 A |
851 | // Validate addresses for transport mode |
852 | if (key_sockaddrcmp((struct sockaddr *)&sah->saidx.src, src, 0) != 0) { | |
853 | // Source doesn't match | |
854 | continue; | |
855 | } | |
856 | ||
857 | if (key_sockaddrcmp((struct sockaddr *)&sah->saidx.dst, dst, 0) != 0) { | |
858 | // Destination doesn't match | |
859 | continue; | |
860 | } | |
fe8ab488 | 861 | } |
d9a64523 | 862 | |
cb323159 | 863 | /* This SAH is linked to the IPsec interface, and the right family. We found it! */ |
d9a64523 A |
864 | if (key_preferred_oldsa) { |
865 | saorder_state_valid = saorder_state_valid_prefer_old; | |
866 | arraysize = _ARRAYLEN(saorder_state_valid_prefer_old); | |
867 | } else { | |
868 | saorder_state_valid = saorder_state_valid_prefer_new; | |
869 | arraysize = _ARRAYLEN(saorder_state_valid_prefer_new); | |
870 | } | |
871 | ||
872 | sin = (struct sockaddr_in *)&sah->saidx.dst; | |
873 | dstport = sin->sin_port; | |
874 | if (sah->saidx.mode == IPSEC_MODE_TRANSPORT) { | |
875 | sin->sin_port = IPSEC_PORT_ANY; | |
876 | } | |
877 | ||
878 | for (stateidx = 0; stateidx < arraysize; stateidx++) { | |
879 | state = saorder_state_valid[stateidx]; | |
880 | sav = key_do_allocsa_policy(sah, state, dstport); | |
881 | if (sav != NULL) { | |
882 | lck_mtx_unlock(sadb_mutex); | |
883 | return sav; | |
884 | } | |
885 | } | |
886 | ||
887 | break; | |
fe8ab488 | 888 | } |
d9a64523 A |
889 | } |
890 | if (strict) { | |
891 | // If we didn't find anything, try again without strict | |
892 | strict = false; | |
893 | } else { | |
894 | // We already were on the second try, bail | |
fe8ab488 A |
895 | break; |
896 | } | |
d9a64523 | 897 | } while (true); |
0a7de745 | 898 | |
fe8ab488 A |
899 | lck_mtx_unlock(sadb_mutex); |
900 | return NULL; | |
901 | } | |
902 | ||
9bccf70c A |
903 | /* |
904 | * allocating an SA entry for an *OUTBOUND* packet. | |
905 | * checking each request entries in SP, and acquire an SA if need. | |
1c79356b A |
906 | * OUT: 0: there are valid requests. |
907 | * ENOENT: policy may be valid, but SA with REQUIRE is on acquiring. | |
908 | */ | |
909 | int | |
6d2010ae | 910 | key_checkrequest( |
0a7de745 A |
911 | struct ipsecrequest *isr, |
912 | struct secasindex *saidx, | |
913 | struct secasvar **sav) | |
1c79356b A |
914 | { |
915 | u_int level; | |
916 | int error; | |
2d21ac55 | 917 | struct sockaddr_in *sin; |
0a7de745 | 918 | |
5ba3f43e | 919 | LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_NOTOWNED); |
0a7de745 | 920 | |
2d21ac55 | 921 | *sav = NULL; |
0a7de745 | 922 | |
1c79356b | 923 | /* sanity check */ |
0a7de745 | 924 | if (isr == NULL || saidx == NULL) { |
1c79356b | 925 | panic("key_checkrequest: NULL pointer is passed.\n"); |
0a7de745 A |
926 | } |
927 | ||
1c79356b A |
928 | /* check mode */ |
929 | switch (saidx->mode) { | |
0a7de745 A |
930 | case IPSEC_MODE_TRANSPORT: |
931 | case IPSEC_MODE_TUNNEL: | |
932 | break; | |
933 | case IPSEC_MODE_ANY: | |
934 | default: | |
935 | panic("key_checkrequest: Invalid policy defined.\n"); | |
1c79356b | 936 | } |
0a7de745 | 937 | |
1c79356b A |
938 | /* get current level */ |
939 | level = ipsec_get_reqlevel(isr); | |
0a7de745 A |
940 | |
941 | ||
1c79356b | 942 | /* |
1c79356b A |
943 | * key_allocsa_policy should allocate the oldest SA available. |
944 | * See key_do_allocsa_policy(), and draft-jenkins-ipsec-rekeying-03.txt. | |
945 | */ | |
0a7de745 | 946 | if (*sav == NULL) { |
2d21ac55 | 947 | *sav = key_allocsa_policy(saidx); |
0a7de745 A |
948 | } |
949 | ||
1c79356b | 950 | /* When there is SA. */ |
0a7de745 | 951 | if (*sav != NULL) { |
1c79356b | 952 | return 0; |
0a7de745 A |
953 | } |
954 | ||
2d21ac55 A |
955 | /* There is no SA. |
956 | * | |
957 | * Remove dst port - used for special natt support - don't call | |
958 | * key_acquire with it. | |
959 | */ | |
960 | if (saidx->mode == IPSEC_MODE_TRANSPORT) { | |
961 | sin = (struct sockaddr_in *)&saidx->dst; | |
962 | sin->sin_port = IPSEC_PORT_ANY; | |
963 | } | |
1c79356b | 964 | if ((error = key_acquire(saidx, isr->sp)) != 0) { |
55e303ae A |
965 | /* XXX What should I do ? */ |
966 | ipseclog((LOG_DEBUG, "key_checkrequest: error %d returned " | |
0a7de745 | 967 | "from key_acquire.\n", error)); |
1c79356b A |
968 | return error; |
969 | } | |
0a7de745 | 970 | |
1c79356b A |
971 | return level == IPSEC_LEVEL_REQUIRE ? ENOENT : 0; |
972 | } | |
973 | ||
974 | /* | |
975 | * allocating a SA for policy entry from SAD. | |
976 | * NOTE: searching SAD of aliving state. | |
977 | * OUT: NULL: not found. | |
978 | * others: found and return the pointer. | |
979 | */ | |
e2fac8b1 A |
980 | u_int32_t sah_search_calls = 0; |
981 | u_int32_t sah_search_count = 0; | |
2d21ac55 | 982 | struct secasvar * |
6d2010ae | 983 | key_allocsa_policy( |
0a7de745 | 984 | struct secasindex *saidx) |
1c79356b A |
985 | { |
986 | struct secashead *sah; | |
987 | struct secasvar *sav; | |
988 | u_int stateidx, state; | |
55e303ae A |
989 | const u_int *saorder_state_valid; |
990 | int arraysize; | |
2d21ac55 | 991 | struct sockaddr_in *sin; |
0a7de745 A |
992 | u_int16_t dstport; |
993 | ||
2d21ac55 | 994 | lck_mtx_lock(sadb_mutex); |
e2fac8b1 | 995 | sah_search_calls++; |
1c79356b | 996 | LIST_FOREACH(sah, &sahtree, chain) { |
39236c6e | 997 | sah_search_count++; |
0a7de745 | 998 | if (sah->state == SADB_SASTATE_DEAD) { |
1c79356b | 999 | continue; |
0a7de745 A |
1000 | } |
1001 | if (key_cmpsaidx(&sah->saidx, saidx, CMP_MODE | CMP_REQID)) { | |
1c79356b | 1002 | goto found; |
0a7de745 | 1003 | } |
1c79356b | 1004 | } |
2d21ac55 | 1005 | lck_mtx_unlock(sadb_mutex); |
1c79356b | 1006 | return NULL; |
0a7de745 | 1007 | |
39236c6e | 1008 | found: |
0a7de745 | 1009 | |
55e303ae A |
1010 | /* |
1011 | * search a valid state list for outbound packet. | |
1012 | * This search order is important. | |
1013 | */ | |
1014 | if (key_preferred_oldsa) { | |
1015 | saorder_state_valid = saorder_state_valid_prefer_old; | |
1016 | arraysize = _ARRAYLEN(saorder_state_valid_prefer_old); | |
1017 | } else { | |
1018 | saorder_state_valid = saorder_state_valid_prefer_new; | |
1019 | arraysize = _ARRAYLEN(saorder_state_valid_prefer_new); | |
1020 | } | |
0a7de745 A |
1021 | |
1022 | ||
2d21ac55 A |
1023 | sin = (struct sockaddr_in *)&saidx->dst; |
1024 | dstport = sin->sin_port; | |
0a7de745 | 1025 | if (saidx->mode == IPSEC_MODE_TRANSPORT) { |
2d21ac55 | 1026 | sin->sin_port = IPSEC_PORT_ANY; |
0a7de745 A |
1027 | } |
1028 | ||
55e303ae | 1029 | for (stateidx = 0; stateidx < arraysize; stateidx++) { |
1c79356b | 1030 | state = saorder_state_valid[stateidx]; |
0a7de745 | 1031 | |
2d21ac55 A |
1032 | sav = key_do_allocsa_policy(sah, state, dstport); |
1033 | if (sav != NULL) { | |
1034 | lck_mtx_unlock(sadb_mutex); | |
1c79356b | 1035 | return sav; |
2d21ac55 | 1036 | } |
1c79356b | 1037 | } |
2d21ac55 | 1038 | lck_mtx_unlock(sadb_mutex); |
1c79356b A |
1039 | return NULL; |
1040 | } | |
1041 | ||
39236c6e | 1042 | static void |
0a7de745 | 1043 | key_send_delete(struct secasvar *sav) |
39236c6e A |
1044 | { |
1045 | struct mbuf *m, *result; | |
1046 | u_int8_t satype; | |
0a7de745 | 1047 | |
39236c6e | 1048 | key_sa_chgstate(sav, SADB_SASTATE_DEAD); |
0a7de745 A |
1049 | |
1050 | if ((satype = key_proto2satype(sav->sah->saidx.proto)) == 0) { | |
39236c6e | 1051 | panic("key_do_allocsa_policy: invalid proto is passed.\n"); |
0a7de745 A |
1052 | } |
1053 | ||
39236c6e | 1054 | m = key_setsadbmsg(SADB_DELETE, 0, |
f427ee49 | 1055 | satype, 0, 0, (u_int16_t)(sav->refcnt - 1)); |
0a7de745 | 1056 | if (!m) { |
39236c6e | 1057 | goto msgfail; |
0a7de745 | 1058 | } |
39236c6e | 1059 | result = m; |
0a7de745 | 1060 | |
39236c6e A |
1061 | /* set sadb_address for saidx's. */ |
1062 | m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC, | |
0a7de745 A |
1063 | (struct sockaddr *)&sav->sah->saidx.src, |
1064 | sav->sah->saidx.src.ss_len << 3, | |
1065 | IPSEC_ULPROTO_ANY); | |
1066 | if (!m) { | |
39236c6e | 1067 | goto msgfail; |
0a7de745 | 1068 | } |
39236c6e | 1069 | m_cat(result, m); |
0a7de745 | 1070 | |
39236c6e A |
1071 | /* set sadb_address for saidx's. */ |
1072 | m = key_setsadbaddr(SADB_EXT_ADDRESS_DST, | |
0a7de745 A |
1073 | (struct sockaddr *)&sav->sah->saidx.dst, |
1074 | sav->sah->saidx.src.ss_len << 3, | |
1075 | IPSEC_ULPROTO_ANY); | |
1076 | if (!m) { | |
39236c6e | 1077 | goto msgfail; |
0a7de745 | 1078 | } |
39236c6e | 1079 | m_cat(result, m); |
0a7de745 | 1080 | |
39236c6e A |
1081 | /* create SA extension */ |
1082 | m = key_setsadbsa(sav); | |
0a7de745 | 1083 | if (!m) { |
39236c6e | 1084 | goto msgfail; |
0a7de745 | 1085 | } |
39236c6e | 1086 | m_cat(result, m); |
0a7de745 | 1087 | |
39236c6e A |
1088 | if (result->m_len < sizeof(struct sadb_msg)) { |
1089 | result = m_pullup(result, | |
0a7de745 A |
1090 | sizeof(struct sadb_msg)); |
1091 | if (result == NULL) { | |
39236c6e | 1092 | goto msgfail; |
0a7de745 | 1093 | } |
39236c6e | 1094 | } |
0a7de745 | 1095 | |
39236c6e | 1096 | result->m_pkthdr.len = 0; |
0a7de745 | 1097 | for (m = result; m; m = m->m_next) { |
39236c6e | 1098 | result->m_pkthdr.len += m->m_len; |
0a7de745 | 1099 | } |
f427ee49 A |
1100 | |
1101 | VERIFY(PFKEY_UNIT64(result->m_pkthdr.len) <= UINT16_MAX); | |
39236c6e | 1102 | mtod(result, struct sadb_msg *)->sadb_msg_len = |
f427ee49 | 1103 | (u_int16_t)PFKEY_UNIT64(result->m_pkthdr.len); |
0a7de745 | 1104 | |
39236c6e | 1105 | if (key_sendup_mbuf(NULL, result, |
0a7de745 | 1106 | KEY_SENDUP_REGISTERED)) { |
39236c6e | 1107 | goto msgfail; |
0a7de745 | 1108 | } |
39236c6e A |
1109 | msgfail: |
1110 | key_freesav(sav, KEY_SADB_LOCKED); | |
1111 | } | |
1112 | ||
1c79356b A |
1113 | /* |
1114 | * searching SAD with direction, protocol, mode and state. | |
1115 | * called by key_allocsa_policy(). | |
1116 | * OUT: | |
1117 | * NULL : not found | |
1118 | * others : found, pointer to a SA. | |
1119 | */ | |
1120 | static struct secasvar * | |
6d2010ae | 1121 | key_do_allocsa_policy( |
0a7de745 A |
1122 | struct secashead *sah, |
1123 | u_int state, | |
1124 | u_int16_t dstport) | |
1c79356b | 1125 | { |
2d21ac55 | 1126 | struct secasvar *sav, *nextsav, *candidate, *natt_candidate, *no_natt_candidate, *d; |
0a7de745 | 1127 | |
5ba3f43e | 1128 | LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_OWNED); |
0a7de745 | 1129 | |
2d21ac55 | 1130 | /* initialize */ |
1c79356b | 1131 | candidate = NULL; |
2d21ac55 A |
1132 | natt_candidate = NULL; |
1133 | no_natt_candidate = NULL; | |
0a7de745 | 1134 | |
55e303ae | 1135 | for (sav = LIST_FIRST(&sah->savtree[state]); |
0a7de745 A |
1136 | sav != NULL; |
1137 | sav = nextsav) { | |
55e303ae | 1138 | nextsav = LIST_NEXT(sav, chain); |
0a7de745 | 1139 | |
1c79356b A |
1140 | /* sanity check */ |
1141 | KEY_CHKSASTATE(sav->state, state, "key_do_allocsa_policy"); | |
0a7de745 | 1142 | |
2d21ac55 A |
1143 | if (sah->saidx.mode == IPSEC_MODE_TUNNEL && dstport && |
1144 | ((sav->flags & SADB_X_EXT_NATT) != 0) && | |
0a7de745 | 1145 | ntohs(dstport) != sav->remote_ike_port) { |
1c79356b | 1146 | continue; |
0a7de745 A |
1147 | } |
1148 | ||
2d21ac55 A |
1149 | if (sah->saidx.mode == IPSEC_MODE_TRANSPORT && |
1150 | ((sav->flags & SADB_X_EXT_NATT_MULTIPLEUSERS) != 0) && | |
0a7de745 A |
1151 | ntohs(dstport) != sav->remote_ike_port) { |
1152 | continue; /* skip this one - not a match - or not UDP */ | |
1153 | } | |
2d21ac55 | 1154 | if ((sah->saidx.mode == IPSEC_MODE_TUNNEL && |
0a7de745 | 1155 | ((sav->flags & SADB_X_EXT_NATT) != 0)) || |
2d21ac55 | 1156 | (sah->saidx.mode == IPSEC_MODE_TRANSPORT && |
0a7de745 A |
1157 | ((sav->flags & SADB_X_EXT_NATT_MULTIPLEUSERS) != 0))) { |
1158 | if (natt_candidate == NULL) { | |
1159 | natt_candidate = sav; | |
1160 | continue; | |
39236c6e | 1161 | } else { |
0a7de745 A |
1162 | candidate = natt_candidate; |
1163 | } | |
1164 | } else { | |
1165 | if (no_natt_candidate == NULL) { | |
1166 | no_natt_candidate = sav; | |
1167 | continue; | |
1168 | } else { | |
1169 | candidate = no_natt_candidate; | |
39236c6e | 1170 | } |
0a7de745 A |
1171 | } |
1172 | ||
1c79356b | 1173 | /* Which SA is the better ? */ |
0a7de745 | 1174 | |
1c79356b | 1175 | /* sanity check 2 */ |
0a7de745 | 1176 | if (candidate->lft_c == NULL || sav->lft_c == NULL) { |
1c79356b | 1177 | panic("key_do_allocsa_policy: " |
0a7de745 A |
1178 | "lifetime_current is NULL.\n"); |
1179 | } | |
1180 | ||
55e303ae A |
1181 | /* What the best method is to compare ? */ |
1182 | if (key_preferred_oldsa) { | |
1183 | if (candidate->lft_c->sadb_lifetime_addtime > | |
0a7de745 A |
1184 | sav->lft_c->sadb_lifetime_addtime) { |
1185 | if ((sav->flags & SADB_X_EXT_NATT_MULTIPLEUSERS) != 0) { | |
2d21ac55 | 1186 | natt_candidate = sav; |
0a7de745 | 1187 | } else { |
2d21ac55 | 1188 | no_natt_candidate = sav; |
0a7de745 | 1189 | } |
39236c6e | 1190 | } |
55e303ae A |
1191 | continue; |
1192 | /*NOTREACHED*/ | |
1193 | } | |
0a7de745 | 1194 | |
55e303ae A |
1195 | /* prefered new sa rather than old sa */ |
1196 | if (candidate->lft_c->sadb_lifetime_addtime < | |
0a7de745 | 1197 | sav->lft_c->sadb_lifetime_addtime) { |
55e303ae | 1198 | d = candidate; |
a39ff7e2 | 1199 | if ((sah->saidx.mode == IPSEC_MODE_TUNNEL && |
0a7de745 A |
1200 | ((sav->flags & SADB_X_EXT_NATT) != 0)) || |
1201 | (sah->saidx.mode == IPSEC_MODE_TRANSPORT && | |
1202 | ((sav->flags & SADB_X_EXT_NATT_MULTIPLEUSERS) != 0))) { | |
2d21ac55 | 1203 | natt_candidate = sav; |
a39ff7e2 | 1204 | } else { |
2d21ac55 | 1205 | no_natt_candidate = sav; |
a39ff7e2 A |
1206 | } |
1207 | } else { | |
55e303ae | 1208 | d = sav; |
a39ff7e2 | 1209 | } |
0a7de745 | 1210 | |
55e303ae A |
1211 | /* |
1212 | * prepared to delete the SA when there is more | |
1213 | * suitable candidate and the lifetime of the SA is not | |
1214 | * permanent. | |
1215 | */ | |
1216 | if (d->lft_c->sadb_lifetime_addtime != 0) { | |
39236c6e | 1217 | key_send_delete(d); |
1c79356b A |
1218 | } |
1219 | } | |
0a7de745 | 1220 | |
2d21ac55 | 1221 | /* choose latest if both types present */ |
0a7de745 | 1222 | if (natt_candidate == NULL) { |
2d21ac55 | 1223 | candidate = no_natt_candidate; |
0a7de745 | 1224 | } else if (no_natt_candidate == NULL) { |
2d21ac55 | 1225 | candidate = natt_candidate; |
0a7de745 | 1226 | } else if (sah->saidx.mode == IPSEC_MODE_TUNNEL && dstport) { |
2d21ac55 | 1227 | candidate = natt_candidate; |
0a7de745 A |
1228 | } else if (natt_candidate->lft_c->sadb_lifetime_addtime > |
1229 | no_natt_candidate->lft_c->sadb_lifetime_addtime) { | |
2d21ac55 | 1230 | candidate = natt_candidate; |
0a7de745 | 1231 | } else { |
2d21ac55 | 1232 | candidate = no_natt_candidate; |
0a7de745 A |
1233 | } |
1234 | ||
1c79356b A |
1235 | if (candidate) { |
1236 | candidate->refcnt++; | |
1237 | KEYDEBUG(KEYDEBUG_IPSEC_STAMP, | |
39236c6e A |
1238 | printf("DP allocsa_policy cause " |
1239 | "refcnt++:%d SA:0x%llx\n", candidate->refcnt, | |
1240 | (uint64_t)VM_KERNEL_ADDRPERM(candidate))); | |
1c79356b A |
1241 | } |
1242 | return candidate; | |
1243 | } | |
1244 | ||
1245 | /* | |
1246 | * allocating a SA entry for a *INBOUND* packet. | |
1247 | * Must call key_freesav() later. | |
1248 | * OUT: positive: pointer to a sav. | |
91447636 | 1249 | * NULL: not found, or error occurred. |
1c79356b A |
1250 | * |
1251 | * In the comparison, source address will be ignored for RFC2401 conformance. | |
1252 | * To quote, from section 4.1: | |
1253 | * A security association is uniquely identified by a triple consisting | |
1254 | * of a Security Parameter Index (SPI), an IP Destination Address, and a | |
1255 | * security protocol (AH or ESP) identifier. | |
1256 | * Note that, however, we do need to keep source address in IPsec SA. | |
9bccf70c | 1257 | * IKE specification and PF_KEY specification do assume that we |
1c79356b A |
1258 | * keep source address in IPsec SA. We see a tricky situation here. |
1259 | */ | |
1260 | struct secasvar * | |
6d2010ae | 1261 | key_allocsa( |
0a7de745 A |
1262 | u_int family, |
1263 | caddr_t src, | |
1264 | caddr_t dst, | |
1265 | u_int proto, | |
1266 | u_int32_t spi) | |
5ba3f43e A |
1267 | { |
1268 | return key_allocsa_extended(family, src, dst, proto, spi, NULL); | |
1269 | } | |
1270 | ||
1271 | struct secasvar * | |
1272 | key_allocsa_extended(u_int family, | |
0a7de745 A |
1273 | caddr_t src, |
1274 | caddr_t dst, | |
1275 | u_int proto, | |
1276 | u_int32_t spi, | |
1277 | ifnet_t interface) | |
1c79356b | 1278 | { |
91447636 A |
1279 | struct secasvar *sav, *match; |
1280 | u_int stateidx, state, tmpidx, matchidx; | |
9bccf70c A |
1281 | struct sockaddr_in sin; |
1282 | struct sockaddr_in6 sin6; | |
55e303ae A |
1283 | const u_int *saorder_state_valid; |
1284 | int arraysize; | |
0a7de745 | 1285 | |
5ba3f43e | 1286 | LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_NOTOWNED); |
0a7de745 | 1287 | |
1c79356b | 1288 | /* sanity check */ |
0a7de745 | 1289 | if (src == NULL || dst == NULL) { |
1c79356b | 1290 | panic("key_allocsa: NULL pointer is passed.\n"); |
0a7de745 A |
1291 | } |
1292 | ||
55e303ae A |
1293 | /* |
1294 | * when both systems employ similar strategy to use a SA. | |
1295 | * the search order is important even in the inbound case. | |
1296 | */ | |
1297 | if (key_preferred_oldsa) { | |
1298 | saorder_state_valid = saorder_state_valid_prefer_old; | |
1299 | arraysize = _ARRAYLEN(saorder_state_valid_prefer_old); | |
1300 | } else { | |
1301 | saorder_state_valid = saorder_state_valid_prefer_new; | |
1302 | arraysize = _ARRAYLEN(saorder_state_valid_prefer_new); | |
1303 | } | |
0a7de745 | 1304 | |
1c79356b A |
1305 | /* |
1306 | * searching SAD. | |
1307 | * XXX: to be checked internal IP header somewhere. Also when | |
1308 | * IPsec tunnel packet is received. But ESP tunnel mode is | |
1309 | * encrypted so we can't check internal IP header. | |
1310 | */ | |
91447636 A |
1311 | /* |
1312 | * search a valid state list for inbound packet. | |
1313 | * the search order is not important. | |
1314 | */ | |
1315 | match = NULL; | |
1316 | matchidx = arraysize; | |
2d21ac55 | 1317 | lck_mtx_lock(sadb_mutex); |
91447636 | 1318 | LIST_FOREACH(sav, &spihash[SPIHASH(spi)], spihash) { |
0a7de745 | 1319 | if (sav->spi != spi) { |
91447636 | 1320 | continue; |
0a7de745 | 1321 | } |
5ba3f43e | 1322 | if (interface != NULL && |
0a7de745 | 1323 | sav->sah->ipsec_if != interface) { |
5ba3f43e A |
1324 | continue; |
1325 | } | |
0a7de745 | 1326 | if (proto != sav->sah->saidx.proto) { |
91447636 | 1327 | continue; |
0a7de745 | 1328 | } |
91447636 | 1329 | if (family != sav->sah->saidx.src.ss_family || |
0a7de745 | 1330 | family != sav->sah->saidx.dst.ss_family) { |
91447636 | 1331 | continue; |
0a7de745 | 1332 | } |
91447636 A |
1333 | tmpidx = arraysize; |
1334 | for (stateidx = 0; stateidx < matchidx; stateidx++) { | |
1c79356b | 1335 | state = saorder_state_valid[stateidx]; |
91447636 A |
1336 | if (sav->state == state) { |
1337 | tmpidx = stateidx; | |
1338 | break; | |
1339 | } | |
1340 | } | |
0a7de745 | 1341 | if (tmpidx >= matchidx) { |
91447636 | 1342 | continue; |
0a7de745 A |
1343 | } |
1344 | ||
91447636 A |
1345 | /* check dst address */ |
1346 | switch (family) { | |
0a7de745 A |
1347 | case AF_INET: |
1348 | bzero(&sin, sizeof(sin)); | |
1349 | sin.sin_family = AF_INET; | |
1350 | sin.sin_len = sizeof(sin); | |
1351 | bcopy(dst, &sin.sin_addr, | |
1352 | sizeof(sin.sin_addr)); | |
1353 | if (key_sockaddrcmp((struct sockaddr*)&sin, | |
1354 | (struct sockaddr *)&sav->sah->saidx.dst, 0) != 0) { | |
1355 | continue; | |
1356 | } | |
1357 | ||
1358 | break; | |
1359 | case AF_INET6: | |
1360 | bzero(&sin6, sizeof(sin6)); | |
1361 | sin6.sin6_family = AF_INET6; | |
1362 | sin6.sin6_len = sizeof(sin6); | |
1363 | bcopy(dst, &sin6.sin6_addr, | |
1364 | sizeof(sin6.sin6_addr)); | |
1365 | if (IN6_IS_SCOPE_LINKLOCAL(&sin6.sin6_addr)) { | |
1366 | /* kame fake scopeid */ | |
1367 | sin6.sin6_scope_id = | |
1368 | ntohs(sin6.sin6_addr.s6_addr16[1]); | |
1369 | sin6.sin6_addr.s6_addr16[1] = 0; | |
1370 | } | |
1371 | if (key_sockaddrcmp((struct sockaddr*)&sin6, | |
1372 | (struct sockaddr *)&sav->sah->saidx.dst, 0) != 0) { | |
91447636 | 1373 | continue; |
0a7de745 A |
1374 | } |
1375 | break; | |
1376 | default: | |
1377 | ipseclog((LOG_DEBUG, "key_allocsa: " | |
1378 | "unknown address family=%d.\n", family)); | |
1379 | continue; | |
1c79356b | 1380 | } |
0a7de745 | 1381 | |
91447636 A |
1382 | match = sav; |
1383 | matchidx = tmpidx; | |
1c79356b | 1384 | } |
0a7de745 | 1385 | if (match) { |
91447636 | 1386 | goto found; |
0a7de745 A |
1387 | } |
1388 | ||
1c79356b | 1389 | /* not found */ |
2d21ac55 | 1390 | lck_mtx_unlock(sadb_mutex); |
1c79356b | 1391 | return NULL; |
0a7de745 | 1392 | |
1c79356b | 1393 | found: |
2d21ac55 A |
1394 | match->refcnt++; |
1395 | lck_mtx_unlock(sadb_mutex); | |
1c79356b | 1396 | KEYDEBUG(KEYDEBUG_IPSEC_STAMP, |
39236c6e A |
1397 | printf("DP allocsa cause refcnt++:%d SA:0x%llx\n", |
1398 | match->refcnt, (uint64_t)VM_KERNEL_ADDRPERM(match))); | |
91447636 | 1399 | return match; |
1c79356b A |
1400 | } |
1401 | ||
94ff46dc A |
1402 | /* |
1403 | * This function checks whether a UDP packet with a random local port | |
1404 | * and a remote port of 4500 matches an SA in the kernel. If does match, | |
1405 | * send the packet to the ESP engine. If not, send the packet to the UDP protocol. | |
1406 | */ | |
1407 | bool | |
1408 | key_checksa_present(u_int family, | |
1409 | caddr_t local_addr, | |
1410 | caddr_t remote_addr, | |
1411 | u_int16_t local_port, | |
1412 | u_int16_t remote_port) | |
1413 | { | |
1414 | LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_NOTOWNED); | |
1415 | ||
1416 | /* sanity check */ | |
1417 | if (local_addr == NULL || remote_addr == NULL) { | |
1418 | panic("key_allocsa: NULL pointer is passed.\n"); | |
1419 | } | |
1420 | ||
1421 | /* | |
1422 | * searching SAD. | |
1423 | * XXX: to be checked internal IP header somewhere. Also when | |
1424 | * IPsec tunnel packet is received. But ESP tunnel mode is | |
1425 | * encrypted so we can't check internal IP header. | |
1426 | */ | |
1427 | /* | |
1428 | * search a valid state list for inbound packet. | |
1429 | * the search order is not important. | |
1430 | */ | |
1431 | struct secashead *sah = NULL; | |
1432 | bool found_sa = false; | |
1433 | ||
1434 | lck_mtx_lock(sadb_mutex); | |
1435 | LIST_FOREACH(sah, &sahtree, chain) { | |
1436 | if (sah->state == SADB_SASTATE_DEAD) { | |
1437 | continue; | |
1438 | } | |
1439 | ||
1440 | if (sah->dir != IPSEC_DIR_OUTBOUND) { | |
1441 | continue; | |
1442 | } | |
1443 | ||
1444 | if (family != sah->saidx.src.ss_family) { | |
1445 | continue; | |
1446 | } | |
1447 | ||
1448 | struct sockaddr_in src_in = {}; | |
1449 | struct sockaddr_in6 src_in6 = {}; | |
1450 | ||
1451 | /* check src address */ | |
1452 | switch (family) { | |
1453 | case AF_INET: | |
1454 | src_in.sin_family = AF_INET; | |
1455 | src_in.sin_len = sizeof(src_in); | |
1456 | memcpy(&src_in.sin_addr, local_addr, sizeof(src_in.sin_addr)); | |
1457 | if (key_sockaddrcmp((struct sockaddr*)&src_in, | |
1458 | (struct sockaddr *)&sah->saidx.src, 0) != 0) { | |
1459 | continue; | |
1460 | } | |
1461 | break; | |
1462 | case AF_INET6: | |
1463 | src_in6.sin6_family = AF_INET6; | |
1464 | src_in6.sin6_len = sizeof(src_in6); | |
1465 | memcpy(&src_in6.sin6_addr, local_addr, sizeof(src_in6.sin6_addr)); | |
1466 | if (IN6_IS_SCOPE_LINKLOCAL(&src_in6.sin6_addr)) { | |
1467 | /* kame fake scopeid */ | |
1468 | src_in6.sin6_scope_id = | |
1469 | ntohs(src_in6.sin6_addr.s6_addr16[1]); | |
1470 | src_in6.sin6_addr.s6_addr16[1] = 0; | |
1471 | } | |
1472 | if (key_sockaddrcmp((struct sockaddr*)&src_in6, | |
1473 | (struct sockaddr *)&sah->saidx.src, 0) != 0) { | |
1474 | continue; | |
1475 | } | |
1476 | break; | |
1477 | default: | |
1478 | ipseclog((LOG_DEBUG, "key_checksa_present: " | |
1479 | "unknown address family=%d.\n", | |
1480 | family)); | |
1481 | continue; | |
1482 | } | |
1483 | ||
1484 | struct sockaddr_in dest_in = {}; | |
1485 | struct sockaddr_in6 dest_in6 = {}; | |
1486 | ||
1487 | /* check dst address */ | |
1488 | switch (family) { | |
1489 | case AF_INET: | |
1490 | dest_in.sin_family = AF_INET; | |
1491 | dest_in.sin_len = sizeof(dest_in); | |
1492 | memcpy(&dest_in.sin_addr, remote_addr, sizeof(dest_in.sin_addr)); | |
1493 | if (key_sockaddrcmp((struct sockaddr*)&dest_in, | |
1494 | (struct sockaddr *)&sah->saidx.dst, 0) != 0) { | |
1495 | continue; | |
1496 | } | |
1497 | ||
1498 | break; | |
1499 | case AF_INET6: | |
1500 | dest_in6.sin6_family = AF_INET6; | |
1501 | dest_in6.sin6_len = sizeof(dest_in6); | |
1502 | memcpy(&dest_in6.sin6_addr, remote_addr, sizeof(dest_in6.sin6_addr)); | |
1503 | if (IN6_IS_SCOPE_LINKLOCAL(&dest_in6.sin6_addr)) { | |
1504 | /* kame fake scopeid */ | |
1505 | dest_in6.sin6_scope_id = | |
1506 | ntohs(dest_in6.sin6_addr.s6_addr16[1]); | |
1507 | dest_in6.sin6_addr.s6_addr16[1] = 0; | |
1508 | } | |
1509 | if (key_sockaddrcmp((struct sockaddr*)&dest_in6, | |
1510 | (struct sockaddr *)&sah->saidx.dst, 0) != 0) { | |
1511 | continue; | |
1512 | } | |
1513 | ||
1514 | break; | |
1515 | default: | |
1516 | ipseclog((LOG_DEBUG, "key_checksa_present: " | |
1517 | "unknown address family=%d.\n", family)); | |
1518 | continue; | |
1519 | } | |
1520 | ||
1521 | struct secasvar *nextsav = NULL; | |
1522 | for (u_int stateidx = 0; stateidx < _ARRAYLEN(saorder_state_alive); stateidx++) { | |
1523 | u_int state = saorder_state_alive[stateidx]; | |
1524 | for (struct secasvar *sav = LIST_FIRST(&sah->savtree[state]); sav != NULL; sav = nextsav) { | |
1525 | nextsav = LIST_NEXT(sav, chain); | |
1526 | /* sanity check */ | |
1527 | if (sav->state != state) { | |
1528 | ipseclog((LOG_DEBUG, "key_checksa_present: " | |
1529 | "invalid sav->state " | |
1530 | "(state: %d SA: %d)\n", | |
1531 | state, sav->state)); | |
1532 | continue; | |
1533 | } | |
1534 | ||
1535 | if (sav->remote_ike_port != ntohs(remote_port)) { | |
1536 | continue; | |
1537 | } | |
1538 | ||
1539 | if (sav->natt_encapsulated_src_port != local_port) { | |
1540 | continue; | |
1541 | } | |
1542 | found_sa = true;; | |
1543 | break; | |
1544 | } | |
1545 | } | |
1546 | } | |
1547 | ||
1548 | /* not found */ | |
1549 | lck_mtx_unlock(sadb_mutex); | |
1550 | return found_sa; | |
1551 | } | |
1552 | ||
2d21ac55 | 1553 | u_int16_t |
6d2010ae | 1554 | key_natt_get_translated_port( |
0a7de745 | 1555 | struct secasvar *outsav) |
2d21ac55 | 1556 | { |
2d21ac55 A |
1557 | struct secasindex saidx; |
1558 | struct secashead *sah; | |
1559 | u_int stateidx, state; | |
1560 | const u_int *saorder_state_valid; | |
1561 | int arraysize; | |
0a7de745 | 1562 | |
2d21ac55 A |
1563 | /* get sa for incoming */ |
1564 | saidx.mode = outsav->sah->saidx.mode; | |
1565 | saidx.reqid = 0; | |
1566 | saidx.proto = outsav->sah->saidx.proto; | |
1567 | bcopy(&outsav->sah->saidx.src, &saidx.dst, sizeof(struct sockaddr_in)); | |
1568 | bcopy(&outsav->sah->saidx.dst, &saidx.src, sizeof(struct sockaddr_in)); | |
0a7de745 | 1569 | |
2d21ac55 A |
1570 | lck_mtx_lock(sadb_mutex); |
1571 | LIST_FOREACH(sah, &sahtree, chain) { | |
0a7de745 | 1572 | if (sah->state == SADB_SASTATE_DEAD) { |
2d21ac55 | 1573 | continue; |
0a7de745 A |
1574 | } |
1575 | if (key_cmpsaidx(&sah->saidx, &saidx, CMP_MODE)) { | |
2d21ac55 | 1576 | goto found; |
0a7de745 | 1577 | } |
2d21ac55 A |
1578 | } |
1579 | lck_mtx_unlock(sadb_mutex); | |
1580 | return 0; | |
0a7de745 | 1581 | |
2d21ac55 | 1582 | found: |
39236c6e | 1583 | /* |
2d21ac55 A |
1584 | * Found sah - now go thru list of SAs and find |
1585 | * matching remote ike port. If found - set | |
1586 | * sav->natt_encapsulated_src_port and return the port. | |
1587 | */ | |
1588 | /* | |
1589 | * search a valid state list for outbound packet. | |
1590 | * This search order is important. | |
1591 | */ | |
1592 | if (key_preferred_oldsa) { | |
1593 | saorder_state_valid = saorder_state_valid_prefer_old; | |
1594 | arraysize = _ARRAYLEN(saorder_state_valid_prefer_old); | |
1595 | } else { | |
1596 | saorder_state_valid = saorder_state_valid_prefer_new; | |
1597 | arraysize = _ARRAYLEN(saorder_state_valid_prefer_new); | |
1598 | } | |
0a7de745 | 1599 | |
2d21ac55 A |
1600 | for (stateidx = 0; stateidx < arraysize; stateidx++) { |
1601 | state = saorder_state_valid[stateidx]; | |
1602 | if (key_do_get_translated_port(sah, outsav, state)) { | |
1603 | lck_mtx_unlock(sadb_mutex); | |
1604 | return outsav->natt_encapsulated_src_port; | |
1605 | } | |
1606 | } | |
1607 | lck_mtx_unlock(sadb_mutex); | |
1608 | return 0; | |
1609 | } | |
1610 | ||
1611 | static int | |
6d2010ae | 1612 | key_do_get_translated_port( |
0a7de745 A |
1613 | struct secashead *sah, |
1614 | struct secasvar *outsav, | |
1615 | u_int state) | |
2d21ac55 A |
1616 | { |
1617 | struct secasvar *currsav, *nextsav, *candidate; | |
0a7de745 A |
1618 | |
1619 | ||
5ba3f43e | 1620 | LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_OWNED); |
0a7de745 | 1621 | |
2d21ac55 A |
1622 | /* initilize */ |
1623 | candidate = NULL; | |
0a7de745 | 1624 | |
2d21ac55 | 1625 | for (currsav = LIST_FIRST(&sah->savtree[state]); |
0a7de745 A |
1626 | currsav != NULL; |
1627 | currsav = nextsav) { | |
2d21ac55 | 1628 | nextsav = LIST_NEXT(currsav, chain); |
0a7de745 | 1629 | |
2d21ac55 A |
1630 | /* sanity check */ |
1631 | KEY_CHKSASTATE(currsav->state, state, "key_do_get_translated_port"); | |
0a7de745 | 1632 | |
2d21ac55 | 1633 | if ((currsav->flags & SADB_X_EXT_NATT_MULTIPLEUSERS) == 0 || |
0a7de745 | 1634 | currsav->remote_ike_port != outsav->remote_ike_port) { |
2d21ac55 | 1635 | continue; |
0a7de745 A |
1636 | } |
1637 | ||
2d21ac55 A |
1638 | if (candidate == NULL) { |
1639 | candidate = currsav; | |
1640 | continue; | |
1641 | } | |
0a7de745 | 1642 | |
2d21ac55 | 1643 | /* Which SA is the better ? */ |
0a7de745 | 1644 | |
2d21ac55 | 1645 | /* sanity check 2 */ |
0a7de745 | 1646 | if (candidate->lft_c == NULL || currsav->lft_c == NULL) { |
2d21ac55 | 1647 | panic("key_do_get_translated_port: " |
0a7de745 A |
1648 | "lifetime_current is NULL.\n"); |
1649 | } | |
1650 | ||
2d21ac55 A |
1651 | /* What the best method is to compare ? */ |
1652 | if (key_preferred_oldsa) { | |
1653 | if (candidate->lft_c->sadb_lifetime_addtime > | |
0a7de745 | 1654 | currsav->lft_c->sadb_lifetime_addtime) { |
2d21ac55 A |
1655 | candidate = currsav; |
1656 | } | |
1657 | continue; | |
1658 | /*NOTREACHED*/ | |
1659 | } | |
0a7de745 | 1660 | |
2d21ac55 A |
1661 | /* prefered new sa rather than old sa */ |
1662 | if (candidate->lft_c->sadb_lifetime_addtime < | |
0a7de745 | 1663 | currsav->lft_c->sadb_lifetime_addtime) { |
2d21ac55 | 1664 | candidate = currsav; |
0a7de745 | 1665 | } |
2d21ac55 | 1666 | } |
0a7de745 | 1667 | |
39236c6e | 1668 | if (candidate) { |
2d21ac55 A |
1669 | outsav->natt_encapsulated_src_port = candidate->natt_encapsulated_src_port; |
1670 | return 1; | |
1671 | } | |
0a7de745 | 1672 | |
2d21ac55 A |
1673 | return 0; |
1674 | } | |
1675 | ||
1c79356b A |
1676 | /* |
1677 | * Must be called after calling key_allocsp(). | |
1c79356b A |
1678 | */ |
1679 | void | |
6d2010ae | 1680 | key_freesp( |
0a7de745 A |
1681 | struct secpolicy *sp, |
1682 | int locked) | |
1c79356b A |
1683 | { |
1684 | /* sanity check */ | |
0a7de745 | 1685 | if (sp == NULL) { |
1c79356b | 1686 | panic("key_freesp: NULL pointer is passed.\n"); |
0a7de745 A |
1687 | } |
1688 | ||
1689 | if (!locked) { | |
2d21ac55 | 1690 | lck_mtx_lock(sadb_mutex); |
0a7de745 | 1691 | } else { |
5ba3f43e | 1692 | LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_OWNED); |
0a7de745 | 1693 | } |
1c79356b A |
1694 | sp->refcnt--; |
1695 | KEYDEBUG(KEYDEBUG_IPSEC_STAMP, | |
39236c6e A |
1696 | printf("DP freesp cause refcnt--:%d SP:0x%llx\n", |
1697 | sp->refcnt, (uint64_t)VM_KERNEL_ADDRPERM(sp))); | |
0a7de745 A |
1698 | |
1699 | if (sp->refcnt == 0) { | |
1c79356b | 1700 | key_delsp(sp); |
0a7de745 A |
1701 | } |
1702 | if (!locked) { | |
2d21ac55 | 1703 | lck_mtx_unlock(sadb_mutex); |
0a7de745 | 1704 | } |
1c79356b A |
1705 | return; |
1706 | } | |
1707 | ||
1c79356b A |
1708 | /* |
1709 | * Must be called after calling key_allocsa(). | |
1710 | * This function is called by key_freesp() to free some SA allocated | |
1711 | * for a policy. | |
1712 | */ | |
1713 | void | |
6d2010ae | 1714 | key_freesav( |
0a7de745 A |
1715 | struct secasvar *sav, |
1716 | int locked) | |
1c79356b A |
1717 | { |
1718 | /* sanity check */ | |
0a7de745 | 1719 | if (sav == NULL) { |
1c79356b | 1720 | panic("key_freesav: NULL pointer is passed.\n"); |
0a7de745 A |
1721 | } |
1722 | ||
1723 | if (!locked) { | |
2d21ac55 | 1724 | lck_mtx_lock(sadb_mutex); |
0a7de745 | 1725 | } else { |
5ba3f43e | 1726 | LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_OWNED); |
0a7de745 | 1727 | } |
1c79356b A |
1728 | sav->refcnt--; |
1729 | KEYDEBUG(KEYDEBUG_IPSEC_STAMP, | |
39236c6e A |
1730 | printf("DP freesav cause refcnt--:%d SA:0x%llx SPI %u\n", |
1731 | sav->refcnt, (uint64_t)VM_KERNEL_ADDRPERM(sav), | |
1732 | (u_int32_t)ntohl(sav->spi))); | |
0a7de745 A |
1733 | |
1734 | if (sav->refcnt == 0) { | |
1c79356b | 1735 | key_delsav(sav); |
0a7de745 A |
1736 | } |
1737 | if (!locked) { | |
2d21ac55 | 1738 | lck_mtx_unlock(sadb_mutex); |
0a7de745 | 1739 | } |
1c79356b A |
1740 | return; |
1741 | } | |
1742 | ||
1743 | /* %%% SPD management */ | |
1744 | /* | |
1745 | * free security policy entry. | |
1746 | */ | |
1747 | static void | |
6d2010ae | 1748 | key_delsp( |
0a7de745 | 1749 | struct secpolicy *sp) |
1c79356b | 1750 | { |
1c79356b | 1751 | /* sanity check */ |
0a7de745 | 1752 | if (sp == NULL) { |
1c79356b | 1753 | panic("key_delsp: NULL pointer is passed.\n"); |
0a7de745 A |
1754 | } |
1755 | ||
5ba3f43e | 1756 | LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_OWNED); |
1c79356b | 1757 | sp->state = IPSEC_SPSTATE_DEAD; |
0a7de745 A |
1758 | |
1759 | if (sp->refcnt > 0) { | |
1c79356b | 1760 | return; /* can't free */ |
0a7de745 | 1761 | } |
1c79356b | 1762 | /* remove from SP index */ |
2d21ac55 | 1763 | if (__LIST_CHAINED(sp)) { |
1c79356b | 1764 | LIST_REMOVE(sp, chain); |
2d21ac55 A |
1765 | ipsec_policy_count--; |
1766 | } | |
0a7de745 A |
1767 | |
1768 | if (sp->spidx.internal_if) { | |
1769 | ifnet_release(sp->spidx.internal_if); | |
1770 | sp->spidx.internal_if = NULL; | |
1771 | } | |
1772 | ||
1773 | if (sp->ipsec_if) { | |
1774 | ifnet_release(sp->ipsec_if); | |
1775 | sp->ipsec_if = NULL; | |
1776 | } | |
1777 | ||
1778 | if (sp->outgoing_if) { | |
1779 | ifnet_release(sp->outgoing_if); | |
1780 | sp->outgoing_if = NULL; | |
1781 | } | |
1782 | ||
1783 | { | |
2d21ac55 | 1784 | struct ipsecrequest *isr = sp->req, *nextisr; |
0a7de745 | 1785 | |
2d21ac55 A |
1786 | while (isr != NULL) { |
1787 | nextisr = isr->next; | |
1788 | KFREE(isr); | |
1789 | isr = nextisr; | |
0a7de745 | 1790 | } |
1c79356b | 1791 | } |
1c79356b | 1792 | keydb_delsecpolicy(sp); |
0a7de745 | 1793 | |
1c79356b A |
1794 | return; |
1795 | } | |
1796 | ||
1797 | /* | |
1798 | * search SPD | |
1799 | * OUT: NULL : not found | |
1800 | * others : found, pointer to a SP. | |
1801 | */ | |
1802 | static struct secpolicy * | |
6d2010ae | 1803 | key_getsp( |
0a7de745 | 1804 | struct secpolicyindex *spidx) |
1c79356b A |
1805 | { |
1806 | struct secpolicy *sp; | |
0a7de745 | 1807 | |
5ba3f43e | 1808 | LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_OWNED); |
0a7de745 | 1809 | |
1c79356b | 1810 | /* sanity check */ |
0a7de745 | 1811 | if (spidx == NULL) { |
1c79356b | 1812 | panic("key_getsp: NULL pointer is passed.\n"); |
0a7de745 A |
1813 | } |
1814 | ||
1c79356b | 1815 | LIST_FOREACH(sp, &sptree[spidx->dir], chain) { |
0a7de745 | 1816 | if (sp->state == IPSEC_SPSTATE_DEAD) { |
1c79356b | 1817 | continue; |
0a7de745 | 1818 | } |
1c79356b A |
1819 | if (key_cmpspidx_exactly(spidx, &sp->spidx)) { |
1820 | sp->refcnt++; | |
1821 | return sp; | |
1822 | } | |
1823 | } | |
0a7de745 | 1824 | |
1c79356b A |
1825 | return NULL; |
1826 | } | |
1827 | ||
1828 | /* | |
1829 | * get SP by index. | |
1830 | * OUT: NULL : not found | |
1831 | * others : found, pointer to a SP. | |
1832 | */ | |
39236c6e | 1833 | struct secpolicy * |
6d2010ae | 1834 | key_getspbyid( |
0a7de745 | 1835 | u_int32_t id) |
1c79356b A |
1836 | { |
1837 | struct secpolicy *sp; | |
0a7de745 A |
1838 | |
1839 | LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_NOTOWNED); | |
1840 | ||
1841 | lck_mtx_lock(sadb_mutex); | |
1842 | sp = __key_getspbyid(id); | |
1843 | lck_mtx_unlock(sadb_mutex); | |
1844 | ||
39236c6e A |
1845 | return sp; |
1846 | } | |
1c79356b | 1847 | |
39236c6e A |
1848 | static struct secpolicy * |
1849 | __key_getspbyid(u_int32_t id) | |
1850 | { | |
1851 | struct secpolicy *sp; | |
0a7de745 | 1852 | |
5ba3f43e | 1853 | LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_OWNED); |
0a7de745 | 1854 | |
1c79356b | 1855 | LIST_FOREACH(sp, &sptree[IPSEC_DIR_INBOUND], chain) { |
0a7de745 | 1856 | if (sp->state == IPSEC_SPSTATE_DEAD) { |
1c79356b | 1857 | continue; |
0a7de745 | 1858 | } |
1c79356b A |
1859 | if (sp->id == id) { |
1860 | sp->refcnt++; | |
1861 | return sp; | |
1862 | } | |
1863 | } | |
0a7de745 | 1864 | |
1c79356b | 1865 | LIST_FOREACH(sp, &sptree[IPSEC_DIR_OUTBOUND], chain) { |
0a7de745 | 1866 | if (sp->state == IPSEC_SPSTATE_DEAD) { |
1c79356b | 1867 | continue; |
0a7de745 | 1868 | } |
1c79356b A |
1869 | if (sp->id == id) { |
1870 | sp->refcnt++; | |
1871 | return sp; | |
1872 | } | |
1873 | } | |
0a7de745 | 1874 | |
1c79356b A |
1875 | return NULL; |
1876 | } | |
1877 | ||
1878 | struct secpolicy * | |
6d2010ae | 1879 | key_newsp(void) |
1c79356b A |
1880 | { |
1881 | struct secpolicy *newsp = NULL; | |
0a7de745 | 1882 | |
5ba3f43e | 1883 | LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_NOTOWNED); |
1c79356b | 1884 | newsp = keydb_newsecpolicy(); |
0a7de745 | 1885 | if (!newsp) { |
1c79356b | 1886 | return newsp; |
0a7de745 A |
1887 | } |
1888 | ||
1c79356b A |
1889 | newsp->refcnt = 1; |
1890 | newsp->req = NULL; | |
0a7de745 | 1891 | |
1c79356b A |
1892 | return newsp; |
1893 | } | |
1894 | ||
1895 | /* | |
1896 | * create secpolicy structure from sadb_x_policy structure. | |
1897 | * NOTE: `state', `secpolicyindex' in secpolicy structure are not set, | |
1898 | * so must be set properly later. | |
1899 | */ | |
1900 | struct secpolicy * | |
6d2010ae | 1901 | key_msg2sp( |
0a7de745 A |
1902 | struct sadb_x_policy *xpl0, |
1903 | size_t len, | |
1904 | int *error) | |
1c79356b A |
1905 | { |
1906 | struct secpolicy *newsp; | |
0a7de745 | 1907 | |
5ba3f43e | 1908 | LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_NOTOWNED); |
0a7de745 | 1909 | |
1c79356b | 1910 | /* sanity check */ |
0a7de745 | 1911 | if (xpl0 == NULL) { |
1c79356b | 1912 | panic("key_msg2sp: NULL pointer was passed.\n"); |
0a7de745 A |
1913 | } |
1914 | if (len < sizeof(*xpl0)) { | |
1c79356b | 1915 | panic("key_msg2sp: invalid length.\n"); |
0a7de745 | 1916 | } |
1c79356b | 1917 | if (len != PFKEY_EXTLEN(xpl0)) { |
55e303ae | 1918 | ipseclog((LOG_DEBUG, "key_msg2sp: Invalid msg length.\n")); |
1c79356b A |
1919 | *error = EINVAL; |
1920 | return NULL; | |
1921 | } | |
0a7de745 | 1922 | |
1c79356b A |
1923 | if ((newsp = key_newsp()) == NULL) { |
1924 | *error = ENOBUFS; | |
1925 | return NULL; | |
1926 | } | |
0a7de745 | 1927 | |
1c79356b A |
1928 | newsp->spidx.dir = xpl0->sadb_x_policy_dir; |
1929 | newsp->policy = xpl0->sadb_x_policy_type; | |
0a7de745 | 1930 | |
1c79356b A |
1931 | /* check policy */ |
1932 | switch (xpl0->sadb_x_policy_type) { | |
0a7de745 A |
1933 | case IPSEC_POLICY_DISCARD: |
1934 | case IPSEC_POLICY_GENERATE: | |
1935 | case IPSEC_POLICY_NONE: | |
1936 | case IPSEC_POLICY_ENTRUST: | |
1937 | case IPSEC_POLICY_BYPASS: | |
1938 | newsp->req = NULL; | |
1939 | break; | |
1940 | ||
1941 | case IPSEC_POLICY_IPSEC: | |
1942 | { | |
1943 | int tlen; | |
1944 | struct sadb_x_ipsecrequest *xisr; | |
1945 | struct ipsecrequest **p_isr = &newsp->req; | |
1946 | ||
1947 | /* validity check */ | |
1948 | if (PFKEY_EXTLEN(xpl0) < sizeof(*xpl0)) { | |
1949 | ipseclog((LOG_DEBUG, | |
1950 | "key_msg2sp: Invalid msg length.\n")); | |
1951 | key_freesp(newsp, KEY_SADB_UNLOCKED); | |
1952 | *error = EINVAL; | |
1953 | return NULL; | |
1954 | } | |
1955 | ||
1956 | tlen = PFKEY_EXTLEN(xpl0) - sizeof(*xpl0); | |
1957 | xisr = (struct sadb_x_ipsecrequest *)(xpl0 + 1); | |
1958 | ||
1959 | while (tlen > 0) { | |
1960 | if (tlen < sizeof(*xisr)) { | |
1961 | ipseclog((LOG_DEBUG, "key_msg2sp: " | |
1962 | "invalid ipsecrequest.\n")); | |
1963 | key_freesp(newsp, KEY_SADB_UNLOCKED); | |
1964 | *error = EINVAL; | |
1965 | return NULL; | |
1966 | } | |
1967 | ||
1968 | /* length check */ | |
1969 | if (xisr->sadb_x_ipsecrequest_len < sizeof(*xisr)) { | |
1970 | ipseclog((LOG_DEBUG, "key_msg2sp: " | |
1971 | "invalid ipsecrequest length.\n")); | |
1972 | key_freesp(newsp, KEY_SADB_UNLOCKED); | |
1973 | *error = EINVAL; | |
1974 | return NULL; | |
1975 | } | |
1976 | ||
1977 | /* allocate request buffer */ | |
1978 | KMALLOC_WAIT(*p_isr, struct ipsecrequest *, sizeof(**p_isr)); | |
1979 | if ((*p_isr) == NULL) { | |
1980 | ipseclog((LOG_DEBUG, | |
1981 | "key_msg2sp: No more memory.\n")); | |
1982 | key_freesp(newsp, KEY_SADB_UNLOCKED); | |
1983 | *error = ENOBUFS; | |
1984 | return NULL; | |
1985 | } | |
1986 | bzero(*p_isr, sizeof(**p_isr)); | |
1987 | ||
1988 | /* set values */ | |
1989 | (*p_isr)->next = NULL; | |
1990 | ||
1991 | switch (xisr->sadb_x_ipsecrequest_proto) { | |
1992 | case IPPROTO_ESP: | |
1993 | case IPPROTO_AH: | |
0a7de745 A |
1994 | break; |
1995 | default: | |
1996 | ipseclog((LOG_DEBUG, | |
1997 | "key_msg2sp: invalid proto type=%u\n", | |
1998 | xisr->sadb_x_ipsecrequest_proto)); | |
1999 | key_freesp(newsp, KEY_SADB_UNLOCKED); | |
2000 | *error = EPROTONOSUPPORT; | |
2001 | return NULL; | |
2002 | } | |
2003 | (*p_isr)->saidx.proto = xisr->sadb_x_ipsecrequest_proto; | |
2004 | ||
2005 | switch (xisr->sadb_x_ipsecrequest_mode) { | |
2006 | case IPSEC_MODE_TRANSPORT: | |
2007 | case IPSEC_MODE_TUNNEL: | |
2008 | break; | |
2009 | case IPSEC_MODE_ANY: | |
2010 | default: | |
55e303ae | 2011 | ipseclog((LOG_DEBUG, |
0a7de745 A |
2012 | "key_msg2sp: invalid mode=%u\n", |
2013 | xisr->sadb_x_ipsecrequest_mode)); | |
2014 | key_freesp(newsp, KEY_SADB_UNLOCKED); | |
2015 | *error = EINVAL; | |
2016 | return NULL; | |
2017 | } | |
2018 | (*p_isr)->saidx.mode = xisr->sadb_x_ipsecrequest_mode; | |
2019 | ||
2020 | switch (xisr->sadb_x_ipsecrequest_level) { | |
2021 | case IPSEC_LEVEL_DEFAULT: | |
2022 | case IPSEC_LEVEL_USE: | |
2023 | case IPSEC_LEVEL_REQUIRE: | |
2024 | break; | |
2025 | case IPSEC_LEVEL_UNIQUE: | |
2026 | /* validity check */ | |
2027 | /* | |
2028 | * If range violation of reqid, kernel will | |
2029 | * update it, don't refuse it. | |
2030 | */ | |
2031 | if (xisr->sadb_x_ipsecrequest_reqid | |
2032 | > IPSEC_MANUAL_REQID_MAX) { | |
2033 | ipseclog((LOG_DEBUG, | |
2034 | "key_msg2sp: reqid=%d range " | |
2035 | "violation, updated by kernel.\n", | |
2036 | xisr->sadb_x_ipsecrequest_reqid)); | |
2037 | xisr->sadb_x_ipsecrequest_reqid = 0; | |
2038 | } | |
2039 | ||
2040 | /* allocate new reqid id if reqid is zero. */ | |
2041 | if (xisr->sadb_x_ipsecrequest_reqid == 0) { | |
f427ee49 | 2042 | u_int16_t reqid; |
0a7de745 A |
2043 | if ((reqid = key_newreqid()) == 0) { |
2044 | key_freesp(newsp, KEY_SADB_UNLOCKED); | |
2045 | *error = ENOBUFS; | |
2046 | return NULL; | |
2047 | } | |
2048 | (*p_isr)->saidx.reqid = reqid; | |
2049 | xisr->sadb_x_ipsecrequest_reqid = reqid; | |
2050 | } else { | |
2051 | /* set it for manual keying. */ | |
2052 | (*p_isr)->saidx.reqid = | |
2053 | xisr->sadb_x_ipsecrequest_reqid; | |
2054 | } | |
2055 | break; | |
2056 | ||
2057 | default: | |
2058 | ipseclog((LOG_DEBUG, "key_msg2sp: invalid level=%u\n", | |
2059 | xisr->sadb_x_ipsecrequest_level)); | |
2d21ac55 | 2060 | key_freesp(newsp, KEY_SADB_UNLOCKED); |
1c79356b A |
2061 | *error = EINVAL; |
2062 | return NULL; | |
2063 | } | |
0a7de745 A |
2064 | (*p_isr)->level = xisr->sadb_x_ipsecrequest_level; |
2065 | ||
2066 | /* set IP addresses if there */ | |
2067 | if (xisr->sadb_x_ipsecrequest_len > sizeof(*xisr)) { | |
2068 | struct sockaddr *paddr; | |
2069 | ||
2070 | if (tlen < xisr->sadb_x_ipsecrequest_len) { | |
2071 | ipseclog((LOG_DEBUG, "key_msg2sp: invalid request " | |
2072 | "address length.\n")); | |
5ba3f43e A |
2073 | key_freesp(newsp, KEY_SADB_UNLOCKED); |
2074 | *error = EINVAL; | |
2075 | return NULL; | |
2076 | } | |
0a7de745 A |
2077 | |
2078 | paddr = (struct sockaddr *)(xisr + 1); | |
2079 | uint8_t src_len = paddr->sa_len; | |
2080 | ||
94ff46dc A |
2081 | /* +sizeof(uint8_t) for dst_len below */ |
2082 | if (xisr->sadb_x_ipsecrequest_len < sizeof(*xisr) + src_len + sizeof(uint8_t)) { | |
0a7de745 A |
2083 | ipseclog((LOG_DEBUG, "key_msg2sp: invalid request " |
2084 | "invalid source address length.\n")); | |
39236c6e A |
2085 | key_freesp(newsp, KEY_SADB_UNLOCKED); |
2086 | *error = EINVAL; | |
2087 | return NULL; | |
2088 | } | |
0a7de745 A |
2089 | |
2090 | /* validity check */ | |
2091 | if (paddr->sa_len | |
2092 | > sizeof((*p_isr)->saidx.src)) { | |
2093 | ipseclog((LOG_DEBUG, "key_msg2sp: invalid request " | |
2094 | "address length.\n")); | |
39236c6e | 2095 | key_freesp(newsp, KEY_SADB_UNLOCKED); |
0a7de745 | 2096 | *error = EINVAL; |
39236c6e | 2097 | return NULL; |
1c79356b | 2098 | } |
5ba3f43e | 2099 | |
0a7de745 A |
2100 | bcopy(paddr, &(*p_isr)->saidx.src, |
2101 | MIN(paddr->sa_len, sizeof((*p_isr)->saidx.src))); | |
5ba3f43e | 2102 | |
0a7de745 A |
2103 | paddr = (struct sockaddr *)((caddr_t)paddr + paddr->sa_len); |
2104 | uint8_t dst_len = paddr->sa_len; | |
5ba3f43e | 2105 | |
94ff46dc | 2106 | if (xisr->sadb_x_ipsecrequest_len < sizeof(*xisr) + src_len + dst_len) { |
0a7de745 A |
2107 | ipseclog((LOG_DEBUG, "key_msg2sp: invalid request " |
2108 | "invalid dest address length.\n")); | |
2109 | key_freesp(newsp, KEY_SADB_UNLOCKED); | |
2110 | *error = EINVAL; | |
2111 | return NULL; | |
39236c6e | 2112 | } |
0a7de745 | 2113 | |
1c79356b | 2114 | /* validity check */ |
0a7de745 A |
2115 | if (paddr->sa_len |
2116 | > sizeof((*p_isr)->saidx.dst)) { | |
2117 | ipseclog((LOG_DEBUG, "key_msg2sp: invalid request " | |
2118 | "address length.\n")); | |
2d21ac55 | 2119 | key_freesp(newsp, KEY_SADB_UNLOCKED); |
1c79356b A |
2120 | *error = EINVAL; |
2121 | return NULL; | |
2122 | } | |
0a7de745 A |
2123 | |
2124 | bcopy(paddr, &(*p_isr)->saidx.dst, | |
2125 | MIN(paddr->sa_len, sizeof((*p_isr)->saidx.dst))); | |
39236c6e | 2126 | } |
0a7de745 A |
2127 | |
2128 | (*p_isr)->sp = newsp; | |
2129 | ||
2130 | /* initialization for the next. */ | |
2131 | p_isr = &(*p_isr)->next; | |
2132 | tlen -= xisr->sadb_x_ipsecrequest_len; | |
2133 | ||
2134 | /* validity check */ | |
2135 | if (tlen < 0) { | |
2136 | ipseclog((LOG_DEBUG, "key_msg2sp: becoming tlen < 0.\n")); | |
2137 | key_freesp(newsp, KEY_SADB_UNLOCKED); | |
2138 | *error = EINVAL; | |
2139 | return NULL; | |
2140 | } | |
2141 | ||
2142 | xisr = (struct sadb_x_ipsecrequest *)(void *) | |
2143 | ((caddr_t)xisr + xisr->sadb_x_ipsecrequest_len); | |
2144 | } | |
2145 | } | |
2146 | break; | |
2147 | default: | |
2148 | ipseclog((LOG_DEBUG, "key_msg2sp: invalid policy type.\n")); | |
2149 | key_freesp(newsp, KEY_SADB_UNLOCKED); | |
2150 | *error = EINVAL; | |
2151 | return NULL; | |
1c79356b | 2152 | } |
0a7de745 | 2153 | |
1c79356b A |
2154 | *error = 0; |
2155 | return newsp; | |
2156 | } | |
2157 | ||
f427ee49 | 2158 | static u_int16_t |
6d2010ae | 2159 | key_newreqid(void) |
1c79356b | 2160 | { |
2d21ac55 | 2161 | lck_mtx_lock(sadb_mutex); |
f427ee49 | 2162 | static u_int16_t auto_reqid = IPSEC_MANUAL_REQID_MAX + 1; |
316670eb | 2163 | int done = 0; |
0a7de745 | 2164 | |
316670eb | 2165 | /* The reqid must be limited to 16 bits because the PF_KEY message format only uses |
0a7de745 A |
2166 | * 16 bits for this field. Once it becomes larger than 16 bits - ipsec fails to |
2167 | * work anymore. Changing the PF_KEY message format would introduce compatibility | |
2168 | * issues. This code now tests to see if the tentative reqid is in use */ | |
2169 | ||
316670eb A |
2170 | while (!done) { |
2171 | struct secpolicy *sp; | |
39236c6e | 2172 | struct ipsecrequest *isr; |
316670eb | 2173 | int dir; |
0a7de745 | 2174 | |
316670eb | 2175 | auto_reqid = (auto_reqid == 0xFFFF |
0a7de745 A |
2176 | ? IPSEC_MANUAL_REQID_MAX + 1 : auto_reqid + 1); |
2177 | ||
316670eb A |
2178 | /* check for uniqueness */ |
2179 | done = 1; | |
2180 | for (dir = 0; dir < IPSEC_DIR_MAX; dir++) { | |
2181 | LIST_FOREACH(sp, &sptree[dir], chain) { | |
2182 | for (isr = sp->req; isr != NULL; isr = isr->next) { | |
2183 | if (isr->saidx.reqid == auto_reqid) { | |
2184 | done = 0; | |
2185 | break; | |
2186 | } | |
2187 | } | |
0a7de745 | 2188 | if (done == 0) { |
316670eb | 2189 | break; |
0a7de745 | 2190 | } |
316670eb | 2191 | } |
0a7de745 | 2192 | if (done == 0) { |
316670eb | 2193 | break; |
0a7de745 | 2194 | } |
39236c6e | 2195 | } |
316670eb | 2196 | } |
0a7de745 | 2197 | |
316670eb | 2198 | lck_mtx_unlock(sadb_mutex); |
1c79356b A |
2199 | return auto_reqid; |
2200 | } | |
2201 | ||
2202 | /* | |
2203 | * copy secpolicy struct to sadb_x_policy structure indicated. | |
2204 | */ | |
2205 | struct mbuf * | |
6d2010ae | 2206 | key_sp2msg( |
0a7de745 | 2207 | struct secpolicy *sp) |
1c79356b A |
2208 | { |
2209 | struct sadb_x_policy *xpl; | |
f427ee49 | 2210 | u_int tlen; |
1c79356b A |
2211 | caddr_t p; |
2212 | struct mbuf *m; | |
0a7de745 | 2213 | |
1c79356b | 2214 | /* sanity check. */ |
0a7de745 | 2215 | if (sp == NULL) { |
1c79356b | 2216 | panic("key_sp2msg: NULL pointer was passed.\n"); |
0a7de745 A |
2217 | } |
2218 | ||
1c79356b | 2219 | tlen = key_getspreqmsglen(sp); |
f427ee49 A |
2220 | if (PFKEY_UNIT64(tlen) > UINT16_MAX) { |
2221 | ipseclog((LOG_ERR, "key_getspreqmsglen returned length %u\n", | |
2222 | tlen)); | |
2223 | return NULL; | |
2224 | } | |
0a7de745 | 2225 | |
9bccf70c | 2226 | m = key_alloc_mbuf(tlen); |
0a7de745 A |
2227 | if (!m || m->m_next) { /*XXX*/ |
2228 | if (m) { | |
9bccf70c | 2229 | m_freem(m); |
0a7de745 | 2230 | } |
1c79356b A |
2231 | return NULL; |
2232 | } | |
0a7de745 | 2233 | |
1c79356b A |
2234 | m->m_len = tlen; |
2235 | m->m_next = NULL; | |
2236 | xpl = mtod(m, struct sadb_x_policy *); | |
2237 | bzero(xpl, tlen); | |
0a7de745 | 2238 | |
f427ee49 | 2239 | xpl->sadb_x_policy_len = (u_int16_t)PFKEY_UNIT64(tlen); |
1c79356b | 2240 | xpl->sadb_x_policy_exttype = SADB_X_EXT_POLICY; |
f427ee49 | 2241 | xpl->sadb_x_policy_type = (u_int16_t)sp->policy; |
1c79356b A |
2242 | xpl->sadb_x_policy_dir = sp->spidx.dir; |
2243 | xpl->sadb_x_policy_id = sp->id; | |
2244 | p = (caddr_t)xpl + sizeof(*xpl); | |
0a7de745 | 2245 | |
1c79356b A |
2246 | /* if is the policy for ipsec ? */ |
2247 | if (sp->policy == IPSEC_POLICY_IPSEC) { | |
2248 | struct sadb_x_ipsecrequest *xisr; | |
2249 | struct ipsecrequest *isr; | |
0a7de745 | 2250 | |
1c79356b | 2251 | for (isr = sp->req; isr != NULL; isr = isr->next) { |
316670eb | 2252 | xisr = (struct sadb_x_ipsecrequest *)(void *)p; |
0a7de745 | 2253 | |
1c79356b A |
2254 | xisr->sadb_x_ipsecrequest_proto = isr->saidx.proto; |
2255 | xisr->sadb_x_ipsecrequest_mode = isr->saidx.mode; | |
f427ee49 A |
2256 | xisr->sadb_x_ipsecrequest_level = (u_int8_t)isr->level; |
2257 | xisr->sadb_x_ipsecrequest_reqid = (u_int16_t)isr->saidx.reqid; | |
0a7de745 | 2258 | |
1c79356b A |
2259 | p += sizeof(*xisr); |
2260 | bcopy(&isr->saidx.src, p, isr->saidx.src.ss_len); | |
2261 | p += isr->saidx.src.ss_len; | |
2262 | bcopy(&isr->saidx.dst, p, isr->saidx.dst.ss_len); | |
2263 | p += isr->saidx.src.ss_len; | |
0a7de745 | 2264 | |
1c79356b | 2265 | xisr->sadb_x_ipsecrequest_len = |
0a7de745 A |
2266 | PFKEY_ALIGN8(sizeof(*xisr) |
2267 | + isr->saidx.src.ss_len | |
2268 | + isr->saidx.dst.ss_len); | |
1c79356b A |
2269 | } |
2270 | } | |
0a7de745 | 2271 | |
1c79356b A |
2272 | return m; |
2273 | } | |
2274 | ||
9bccf70c A |
2275 | /* m will not be freed nor modified */ |
2276 | static struct mbuf * | |
2277 | key_gather_mbuf(struct mbuf *m, const struct sadb_msghdr *mhp, | |
0a7de745 | 2278 | int ndeep, int nitem, int *items) |
9bccf70c A |
2279 | { |
2280 | int idx; | |
2281 | int i; | |
2282 | struct mbuf *result = NULL, *n; | |
2283 | int len; | |
0a7de745 A |
2284 | |
2285 | if (m == NULL || mhp == NULL) { | |
9bccf70c | 2286 | panic("null pointer passed to key_gather"); |
0a7de745 A |
2287 | } |
2288 | ||
9bccf70c A |
2289 | for (i = 0; i < nitem; i++) { |
2290 | idx = items[i]; | |
0a7de745 | 2291 | if (idx < 0 || idx > SADB_EXT_MAX) { |
9bccf70c | 2292 | goto fail; |
0a7de745 | 2293 | } |
9bccf70c | 2294 | /* don't attempt to pull empty extension */ |
0a7de745 | 2295 | if (idx == SADB_EXT_RESERVED && mhp->msg == NULL) { |
9bccf70c | 2296 | continue; |
0a7de745 A |
2297 | } |
2298 | if (idx != SADB_EXT_RESERVED && | |
2299 | (mhp->ext[idx] == NULL || mhp->extlen[idx] == 0)) { | |
9bccf70c | 2300 | continue; |
0a7de745 A |
2301 | } |
2302 | ||
9bccf70c A |
2303 | if (idx == SADB_EXT_RESERVED) { |
2304 | len = PFKEY_ALIGN8(sizeof(struct sadb_msg)); | |
39236c6e | 2305 | MGETHDR(n, M_WAITOK, MT_DATA); // sadb_msg len < MHLEN - enforced by _CASSERT |
0a7de745 | 2306 | if (!n) { |
9bccf70c | 2307 | goto fail; |
0a7de745 | 2308 | } |
9bccf70c A |
2309 | n->m_len = len; |
2310 | n->m_next = NULL; | |
2311 | m_copydata(m, 0, sizeof(struct sadb_msg), | |
0a7de745 | 2312 | mtod(n, caddr_t)); |
9bccf70c A |
2313 | } else if (i < ndeep) { |
2314 | len = mhp->extlen[idx]; | |
2315 | n = key_alloc_mbuf(len); | |
0a7de745 A |
2316 | if (!n || n->m_next) { /*XXX*/ |
2317 | if (n) { | |
9bccf70c | 2318 | m_freem(n); |
0a7de745 | 2319 | } |
9bccf70c A |
2320 | goto fail; |
2321 | } | |
2322 | m_copydata(m, mhp->extoff[idx], mhp->extlen[idx], | |
0a7de745 | 2323 | mtod(n, caddr_t)); |
9bccf70c A |
2324 | } else { |
2325 | n = m_copym(m, mhp->extoff[idx], mhp->extlen[idx], | |
0a7de745 | 2326 | M_WAITOK); |
9bccf70c | 2327 | } |
0a7de745 | 2328 | if (n == NULL) { |
9bccf70c | 2329 | goto fail; |
0a7de745 A |
2330 | } |
2331 | ||
2332 | if (result) { | |
9bccf70c | 2333 | m_cat(result, n); |
0a7de745 | 2334 | } else { |
9bccf70c | 2335 | result = n; |
0a7de745 | 2336 | } |
9bccf70c | 2337 | } |
0a7de745 | 2338 | |
9bccf70c A |
2339 | if ((result->m_flags & M_PKTHDR) != 0) { |
2340 | result->m_pkthdr.len = 0; | |
0a7de745 | 2341 | for (n = result; n; n = n->m_next) { |
9bccf70c | 2342 | result->m_pkthdr.len += n->m_len; |
0a7de745 | 2343 | } |
9bccf70c | 2344 | } |
0a7de745 | 2345 | |
9bccf70c | 2346 | return result; |
0a7de745 | 2347 | |
9bccf70c A |
2348 | fail: |
2349 | m_freem(result); | |
2350 | return NULL; | |
2351 | } | |
2352 | ||
1c79356b A |
2353 | /* |
2354 | * SADB_X_SPDADD, SADB_X_SPDSETIDX or SADB_X_SPDUPDATE processing | |
2355 | * add a entry to SP database, when received | |
9bccf70c | 2356 | * <base, address(SD), (lifetime(H),) policy> |
1c79356b A |
2357 | * from the user(?). |
2358 | * Adding to SP database, | |
2359 | * and send | |
9bccf70c | 2360 | * <base, address(SD), (lifetime(H),) policy> |
1c79356b A |
2361 | * to the socket which was send. |
2362 | * | |
2363 | * SPDADD set a unique policy entry. | |
2364 | * SPDSETIDX like SPDADD without a part of policy requests. | |
2365 | * SPDUPDATE replace a unique policy entry. | |
2366 | * | |
9bccf70c | 2367 | * m will always be freed. |
1c79356b | 2368 | */ |
9bccf70c | 2369 | static int |
6d2010ae | 2370 | key_spdadd( |
0a7de745 A |
2371 | struct socket *so, |
2372 | struct mbuf *m, | |
2373 | const struct sadb_msghdr *mhp) | |
1c79356b | 2374 | { |
5ba3f43e | 2375 | struct sadb_address *src0, *dst0, *src1 = NULL, *dst1 = NULL; |
9bccf70c A |
2376 | struct sadb_x_policy *xpl0, *xpl; |
2377 | struct sadb_lifetime *lft = NULL; | |
1c79356b A |
2378 | struct secpolicyindex spidx; |
2379 | struct secpolicy *newsp; | |
9bccf70c | 2380 | struct timeval tv; |
0a7de745 A |
2381 | ifnet_t internal_if = NULL; |
2382 | char *outgoing_if = NULL; | |
2383 | char *ipsec_if = NULL; | |
2384 | struct sadb_x_ipsecif *ipsecifopts = NULL; | |
1c79356b | 2385 | int error; |
39236c6e A |
2386 | int use_src_range = 0; |
2387 | int use_dst_range = 0; | |
2388 | int init_disabled = 0; | |
2389 | int address_family, address_len; | |
0a7de745 | 2390 | |
5ba3f43e | 2391 | LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_NOTOWNED); |
0a7de745 | 2392 | |
1c79356b | 2393 | /* sanity check */ |
0a7de745 | 2394 | if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL) { |
1c79356b | 2395 | panic("key_spdadd: NULL pointer is passed.\n"); |
0a7de745 A |
2396 | } |
2397 | ||
2398 | if (mhp->ext[SADB_X_EXT_ADDR_RANGE_SRC_START] != NULL && mhp->ext[SADB_X_EXT_ADDR_RANGE_SRC_END] != NULL) { | |
2399 | use_src_range = 1; | |
2400 | } | |
2401 | if (mhp->ext[SADB_X_EXT_ADDR_RANGE_DST_START] != NULL && mhp->ext[SADB_X_EXT_ADDR_RANGE_DST_END] != NULL) { | |
2402 | use_dst_range = 1; | |
2403 | } | |
2404 | ||
39236c6e | 2405 | if ((!use_src_range && mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL) || |
0a7de745 | 2406 | (!use_dst_range && mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) || |
9bccf70c | 2407 | mhp->ext[SADB_X_EXT_POLICY] == NULL) { |
55e303ae | 2408 | ipseclog((LOG_DEBUG, "key_spdadd: invalid message is passed.\n")); |
9bccf70c A |
2409 | return key_senderror(so, m, EINVAL); |
2410 | } | |
39236c6e | 2411 | if ((use_src_range && (mhp->extlen[SADB_X_EXT_ADDR_RANGE_SRC_START] < sizeof(struct sadb_address) |
0a7de745 A |
2412 | || mhp->extlen[SADB_X_EXT_ADDR_RANGE_SRC_END] < sizeof(struct sadb_address))) || |
2413 | (!use_src_range && mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address)) || | |
2414 | (use_dst_range && (mhp->extlen[SADB_X_EXT_ADDR_RANGE_DST_START] < sizeof(struct sadb_address) | |
2415 | || mhp->extlen[SADB_X_EXT_ADDR_RANGE_DST_END] < sizeof(struct sadb_address))) || | |
2416 | (!use_dst_range && mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) || | |
9bccf70c | 2417 | mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) { |
55e303ae | 2418 | ipseclog((LOG_DEBUG, "key_spdadd: invalid message is passed.\n")); |
9bccf70c A |
2419 | return key_senderror(so, m, EINVAL); |
2420 | } | |
2421 | if (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL) { | |
2422 | if (mhp->extlen[SADB_EXT_LIFETIME_HARD] | |
0a7de745 | 2423 | < sizeof(struct sadb_lifetime)) { |
55e303ae | 2424 | ipseclog((LOG_DEBUG, "key_spdadd: invalid message is passed.\n")); |
9bccf70c A |
2425 | return key_senderror(so, m, EINVAL); |
2426 | } | |
316670eb | 2427 | lft = (struct sadb_lifetime *) |
0a7de745 | 2428 | (void *)mhp->ext[SADB_EXT_LIFETIME_HARD]; |
1c79356b | 2429 | } |
0a7de745 A |
2430 | if (mhp->ext[SADB_X_EXT_IPSECIF] != NULL) { |
2431 | if (mhp->extlen[SADB_X_EXT_IPSECIF] < sizeof(struct sadb_x_ipsecif)) { | |
2432 | ipseclog((LOG_DEBUG, "key_spdadd: invalid message is passed.\n")); | |
39236c6e | 2433 | return key_senderror(so, m, EINVAL); |
0a7de745 A |
2434 | } |
2435 | } | |
2436 | ||
2437 | if (use_src_range) { | |
2438 | src0 = (struct sadb_address *)mhp->ext[SADB_X_EXT_ADDR_RANGE_SRC_START]; | |
2439 | src1 = (struct sadb_address *)mhp->ext[SADB_X_EXT_ADDR_RANGE_SRC_END]; | |
2440 | } else { | |
2441 | src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC]; | |
2442 | } | |
2443 | if (use_dst_range) { | |
2444 | dst0 = (struct sadb_address *)mhp->ext[SADB_X_EXT_ADDR_RANGE_DST_START]; | |
2445 | dst1 = (struct sadb_address *)mhp->ext[SADB_X_EXT_ADDR_RANGE_DST_END]; | |
2446 | } else { | |
2447 | dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST]; | |
2448 | } | |
316670eb | 2449 | xpl0 = (struct sadb_x_policy *)(void *)mhp->ext[SADB_X_EXT_POLICY]; |
0a7de745 A |
2450 | ipsecifopts = (struct sadb_x_ipsecif *)(void *)mhp->ext[SADB_X_EXT_IPSECIF]; |
2451 | ||
2452 | /* check addresses */ | |
2453 | address_family = ((struct sockaddr *)(src0 + 1))->sa_family; | |
2454 | address_len = ((struct sockaddr *)(src0 + 1))->sa_len; | |
2455 | if (use_src_range) { | |
2456 | if (((struct sockaddr *)(src1 + 1))->sa_family != address_family || | |
2457 | ((struct sockaddr *)(src1 + 1))->sa_len != address_len) { | |
2458 | return key_senderror(so, m, EINVAL); | |
2459 | } | |
2460 | } | |
2461 | if (((struct sockaddr *)(dst0 + 1))->sa_family != address_family || | |
2462 | ((struct sockaddr *)(dst0 + 1))->sa_len != address_len) { | |
2463 | return key_senderror(so, m, EINVAL); | |
2464 | } | |
2465 | if (use_dst_range) { | |
2466 | if (((struct sockaddr *)(dst1 + 1))->sa_family != address_family || | |
2467 | ((struct sockaddr *)(dst1 + 1))->sa_len != address_len) { | |
2468 | return key_senderror(so, m, EINVAL); | |
2469 | } | |
2470 | } | |
2471 | ||
2472 | /* checking the direction. */ | |
1c79356b | 2473 | switch (xpl0->sadb_x_policy_dir) { |
0a7de745 A |
2474 | case IPSEC_DIR_INBOUND: |
2475 | case IPSEC_DIR_OUTBOUND: | |
2476 | break; | |
2477 | default: | |
2478 | ipseclog((LOG_DEBUG, "key_spdadd: Invalid SP direction.\n")); | |
2479 | mhp->msg->sadb_msg_errno = EINVAL; | |
2480 | return 0; | |
2481 | } | |
2482 | ||
2483 | /* check policy */ | |
1c79356b A |
2484 | /* key_spdadd() accepts DISCARD, NONE and IPSEC. */ |
2485 | if (xpl0->sadb_x_policy_type == IPSEC_POLICY_ENTRUST | |
0a7de745 | 2486 | || xpl0->sadb_x_policy_type == IPSEC_POLICY_BYPASS) { |
55e303ae | 2487 | ipseclog((LOG_DEBUG, "key_spdadd: Invalid policy type.\n")); |
9bccf70c | 2488 | return key_senderror(so, m, EINVAL); |
1c79356b | 2489 | } |
0a7de745 | 2490 | |
1c79356b | 2491 | /* policy requests are mandatory when action is ipsec. */ |
0a7de745 A |
2492 | if (mhp->msg->sadb_msg_type != SADB_X_SPDSETIDX |
2493 | && xpl0->sadb_x_policy_type == IPSEC_POLICY_IPSEC | |
2494 | && mhp->extlen[SADB_X_EXT_POLICY] <= sizeof(*xpl0)) { | |
55e303ae | 2495 | ipseclog((LOG_DEBUG, "key_spdadd: some policy requests part required.\n")); |
9bccf70c | 2496 | return key_senderror(so, m, EINVAL); |
1c79356b | 2497 | } |
0a7de745 A |
2498 | |
2499 | /* Process interfaces */ | |
2500 | if (ipsecifopts != NULL) { | |
2501 | if (ipsecifopts->sadb_x_ipsecif_internal_if[0]) { | |
2502 | ifnet_find_by_name(ipsecifopts->sadb_x_ipsecif_internal_if, &internal_if); | |
2503 | } | |
2504 | if (ipsecifopts->sadb_x_ipsecif_outgoing_if[0]) { | |
2505 | outgoing_if = ipsecifopts->sadb_x_ipsecif_outgoing_if; | |
2506 | } | |
2507 | if (ipsecifopts->sadb_x_ipsecif_ipsec_if[0]) { | |
2508 | ipsec_if = ipsecifopts->sadb_x_ipsecif_ipsec_if; | |
2509 | } | |
39236c6e | 2510 | init_disabled = ipsecifopts->sadb_x_ipsecif_init_disabled; |
0a7de745 A |
2511 | } |
2512 | ||
39236c6e A |
2513 | /* make secindex */ |
2514 | /* XXX boundary check against sa_len */ | |
2515 | KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir, | |
0a7de745 A |
2516 | src0 + 1, |
2517 | dst0 + 1, | |
2518 | src0->sadb_address_prefixlen, | |
2519 | dst0->sadb_address_prefixlen, | |
2520 | src0->sadb_address_proto, | |
2521 | internal_if, | |
2522 | use_src_range ? src0 + 1 : NULL, | |
2523 | use_src_range ? src1 + 1 : NULL, | |
2524 | use_dst_range ? dst0 + 1 : NULL, | |
2525 | use_dst_range ? dst1 + 1 : NULL, | |
2526 | &spidx); | |
2527 | ||
1c79356b A |
2528 | /* |
2529 | * checking there is SP already or not. | |
55e303ae A |
2530 | * SPDUPDATE doesn't depend on whether there is a SP or not. |
2531 | * If the type is either SPDADD or SPDSETIDX AND a SP is found, | |
2532 | * then error. | |
1c79356b | 2533 | */ |
2d21ac55 | 2534 | lck_mtx_lock(sadb_mutex); |
1c79356b | 2535 | newsp = key_getsp(&spidx); |
9bccf70c | 2536 | if (mhp->msg->sadb_msg_type == SADB_X_SPDUPDATE) { |
55e303ae A |
2537 | if (newsp) { |
2538 | newsp->state = IPSEC_SPSTATE_DEAD; | |
2d21ac55 | 2539 | key_freesp(newsp, KEY_SADB_LOCKED); |
1c79356b | 2540 | } |
1c79356b A |
2541 | } else { |
2542 | if (newsp != NULL) { | |
2d21ac55 | 2543 | key_freesp(newsp, KEY_SADB_LOCKED); |
55e303ae | 2544 | ipseclog((LOG_DEBUG, "key_spdadd: a SP entry exists already.\n")); |
2d21ac55 | 2545 | lck_mtx_unlock(sadb_mutex); |
0a7de745 A |
2546 | if (internal_if) { |
2547 | ifnet_release(internal_if); | |
2548 | internal_if = NULL; | |
2549 | } | |
9bccf70c | 2550 | return key_senderror(so, m, EEXIST); |
1c79356b A |
2551 | } |
2552 | } | |
2d21ac55 | 2553 | lck_mtx_unlock(sadb_mutex); |
0a7de745 | 2554 | |
1c79356b A |
2555 | /* allocation new SP entry */ |
2556 | if ((newsp = key_msg2sp(xpl0, PFKEY_EXTLEN(xpl0), &error)) == NULL) { | |
0a7de745 A |
2557 | if (internal_if) { |
2558 | ifnet_release(internal_if); | |
2559 | internal_if = NULL; | |
2560 | } | |
9bccf70c | 2561 | return key_senderror(so, m, error); |
1c79356b | 2562 | } |
0a7de745 | 2563 | |
1c79356b | 2564 | if ((newsp->id = key_getnewspid()) == 0) { |
1c79356b | 2565 | keydb_delsecpolicy(newsp); |
0a7de745 A |
2566 | if (internal_if) { |
2567 | ifnet_release(internal_if); | |
2568 | internal_if = NULL; | |
2569 | } | |
9bccf70c | 2570 | return key_senderror(so, m, ENOBUFS); |
1c79356b | 2571 | } |
0a7de745 | 2572 | |
9bccf70c | 2573 | /* XXX boundary check against sa_len */ |
1c79356b | 2574 | KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir, |
0a7de745 A |
2575 | src0 + 1, |
2576 | dst0 + 1, | |
2577 | src0->sadb_address_prefixlen, | |
2578 | dst0->sadb_address_prefixlen, | |
2579 | src0->sadb_address_proto, | |
2580 | internal_if, | |
2581 | use_src_range ? src0 + 1 : NULL, | |
2582 | use_src_range ? src1 + 1 : NULL, | |
2583 | use_dst_range ? dst0 + 1 : NULL, | |
2584 | use_dst_range ? dst1 + 1 : NULL, | |
2585 | &newsp->spidx); | |
2586 | ||
1c79356b | 2587 | #if 1 |
39236c6e | 2588 | /* |
490019cf | 2589 | * allow IPv6 over IPv4 or IPv4 over IPv6 tunnels using ESP - |
39236c6e | 2590 | * otherwise reject if inner and outer address families not equal |
2d21ac55 | 2591 | */ |
1c79356b A |
2592 | if (newsp->req && newsp->req->saidx.src.ss_family) { |
2593 | struct sockaddr *sa; | |
2594 | sa = (struct sockaddr *)(src0 + 1); | |
2595 | if (sa->sa_family != newsp->req->saidx.src.ss_family) { | |
490019cf | 2596 | if (newsp->req->saidx.mode != IPSEC_MODE_TUNNEL || newsp->req->saidx.proto != IPPROTO_ESP) { |
2d21ac55 | 2597 | keydb_delsecpolicy(newsp); |
0a7de745 A |
2598 | if (internal_if) { |
2599 | ifnet_release(internal_if); | |
2600 | internal_if = NULL; | |
2601 | } | |
2d21ac55 A |
2602 | return key_senderror(so, m, EINVAL); |
2603 | } | |
1c79356b A |
2604 | } |
2605 | } | |
2606 | if (newsp->req && newsp->req->saidx.dst.ss_family) { | |
2607 | struct sockaddr *sa; | |
2608 | sa = (struct sockaddr *)(dst0 + 1); | |
2609 | if (sa->sa_family != newsp->req->saidx.dst.ss_family) { | |
490019cf | 2610 | if (newsp->req->saidx.mode != IPSEC_MODE_TUNNEL || newsp->req->saidx.proto != IPPROTO_ESP) { |
2d21ac55 | 2611 | keydb_delsecpolicy(newsp); |
0a7de745 A |
2612 | if (internal_if) { |
2613 | ifnet_release(internal_if); | |
2614 | internal_if = NULL; | |
2615 | } | |
2d21ac55 A |
2616 | return key_senderror(so, m, EINVAL); |
2617 | } | |
1c79356b A |
2618 | } |
2619 | } | |
2620 | #endif | |
0a7de745 | 2621 | |
9bccf70c A |
2622 | microtime(&tv); |
2623 | newsp->created = tv.tv_sec; | |
2624 | newsp->lastused = tv.tv_sec; | |
f427ee49 A |
2625 | newsp->lifetime = (long)(lft ? lft->sadb_lifetime_addtime : 0); |
2626 | newsp->validtime = (long)(lft ? lft->sadb_lifetime_usetime : 0); | |
0a7de745 A |
2627 | |
2628 | if (outgoing_if != NULL) { | |
2629 | ifnet_find_by_name(outgoing_if, &newsp->outgoing_if); | |
2630 | } | |
2631 | if (ipsec_if != NULL) { | |
2632 | ifnet_find_by_name(ipsec_if, &newsp->ipsec_if); | |
2633 | } | |
39236c6e A |
2634 | if (init_disabled > 0) { |
2635 | newsp->disabled = 1; | |
2636 | } | |
0a7de745 A |
2637 | |
2638 | newsp->refcnt = 1; /* do not reclaim until I say I do */ | |
1c79356b | 2639 | newsp->state = IPSEC_SPSTATE_ALIVE; |
2d21ac55 A |
2640 | lck_mtx_lock(sadb_mutex); |
2641 | /* | |
2642 | * policies of type generate should be at the end of the SPD | |
2643 | * because they function as default discard policies | |
39236c6e A |
2644 | * Don't start timehandler for generate policies |
2645 | */ | |
0a7de745 | 2646 | if (newsp->policy == IPSEC_POLICY_GENERATE) { |
2d21ac55 | 2647 | LIST_INSERT_TAIL(&sptree[newsp->spidx.dir], newsp, secpolicy, chain); |
0a7de745 | 2648 | } else { /* XXX until we have policy ordering in the kernel */ |
2d21ac55 | 2649 | struct secpolicy *tmpsp; |
0a7de745 | 2650 | |
2d21ac55 | 2651 | LIST_FOREACH(tmpsp, &sptree[newsp->spidx.dir], chain) |
0a7de745 | 2652 | if (tmpsp->policy == IPSEC_POLICY_GENERATE) { |
39236c6e | 2653 | break; |
0a7de745 A |
2654 | } |
2655 | if (tmpsp) { | |
2d21ac55 | 2656 | LIST_INSERT_BEFORE(tmpsp, newsp, chain); |
0a7de745 | 2657 | } else { |
2d21ac55 | 2658 | LIST_INSERT_TAIL(&sptree[newsp->spidx.dir], newsp, secpolicy, chain); |
0a7de745 | 2659 | } |
39236c6e | 2660 | key_start_timehandler(); |
2d21ac55 | 2661 | } |
0a7de745 | 2662 | |
2d21ac55 | 2663 | ipsec_policy_count++; |
9bccf70c | 2664 | /* Turn off the ipsec bypass */ |
0a7de745 | 2665 | if (ipsec_bypass != 0) { |
9bccf70c | 2666 | ipsec_bypass = 0; |
0a7de745 A |
2667 | } |
2668 | ||
1c79356b | 2669 | /* delete the entry in spacqtree */ |
9bccf70c | 2670 | if (mhp->msg->sadb_msg_type == SADB_X_SPDUPDATE) { |
1c79356b A |
2671 | struct secspacq *spacq; |
2672 | if ((spacq = key_getspacq(&spidx)) != NULL) { | |
9bccf70c A |
2673 | /* reset counter in order to deletion by timehandler. */ |
2674 | microtime(&tv); | |
2675 | spacq->created = tv.tv_sec; | |
1c79356b A |
2676 | spacq->count = 0; |
2677 | } | |
0a7de745 | 2678 | } |
2d21ac55 | 2679 | lck_mtx_unlock(sadb_mutex); |
0a7de745 A |
2680 | |
2681 | { | |
39236c6e A |
2682 | struct mbuf *n, *mpolicy; |
2683 | struct sadb_msg *newmsg; | |
2684 | int off; | |
0a7de745 | 2685 | |
39236c6e A |
2686 | /* create new sadb_msg to reply. */ |
2687 | if (lft) { | |
0a7de745 A |
2688 | int mbufItems[] = {SADB_EXT_RESERVED, SADB_X_EXT_POLICY, |
2689 | SADB_EXT_LIFETIME_HARD, SADB_EXT_ADDRESS_SRC, | |
2690 | SADB_EXT_ADDRESS_DST, SADB_X_EXT_ADDR_RANGE_SRC_START, SADB_X_EXT_ADDR_RANGE_SRC_END, | |
2691 | SADB_X_EXT_ADDR_RANGE_DST_START, SADB_X_EXT_ADDR_RANGE_DST_END}; | |
2692 | n = key_gather_mbuf(m, mhp, 2, sizeof(mbufItems) / sizeof(int), mbufItems); | |
39236c6e | 2693 | } else { |
0a7de745 A |
2694 | int mbufItems[] = {SADB_EXT_RESERVED, SADB_X_EXT_POLICY, |
2695 | SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST, | |
2696 | SADB_X_EXT_ADDR_RANGE_SRC_START, SADB_X_EXT_ADDR_RANGE_SRC_END, | |
2697 | SADB_X_EXT_ADDR_RANGE_DST_START, SADB_X_EXT_ADDR_RANGE_DST_END}; | |
2698 | n = key_gather_mbuf(m, mhp, 2, sizeof(mbufItems) / sizeof(int), mbufItems); | |
39236c6e | 2699 | } |
0a7de745 | 2700 | if (!n) { |
9bccf70c | 2701 | return key_senderror(so, m, ENOBUFS); |
0a7de745 A |
2702 | } |
2703 | ||
39236c6e A |
2704 | if (n->m_len < sizeof(*newmsg)) { |
2705 | n = m_pullup(n, sizeof(*newmsg)); | |
0a7de745 | 2706 | if (!n) { |
39236c6e | 2707 | return key_senderror(so, m, ENOBUFS); |
0a7de745 | 2708 | } |
39236c6e A |
2709 | } |
2710 | newmsg = mtod(n, struct sadb_msg *); | |
2711 | newmsg->sadb_msg_errno = 0; | |
f427ee49 A |
2712 | |
2713 | VERIFY(PFKEY_UNIT64(n->m_pkthdr.len) <= UINT16_MAX); | |
2714 | newmsg->sadb_msg_len = (u_int16_t)PFKEY_UNIT64(n->m_pkthdr.len); | |
0a7de745 | 2715 | |
39236c6e A |
2716 | off = 0; |
2717 | mpolicy = m_pulldown(n, PFKEY_ALIGN8(sizeof(struct sadb_msg)), | |
0a7de745 | 2718 | sizeof(*xpl), &off); |
39236c6e A |
2719 | if (mpolicy == NULL) { |
2720 | /* n is already freed */ | |
2721 | return key_senderror(so, m, ENOBUFS); | |
2722 | } | |
2723 | xpl = (struct sadb_x_policy *)(void *)(mtod(mpolicy, caddr_t) + off); | |
2724 | if (xpl->sadb_x_policy_exttype != SADB_X_EXT_POLICY) { | |
2725 | m_freem(n); | |
2726 | return key_senderror(so, m, EINVAL); | |
2727 | } | |
2728 | xpl->sadb_x_policy_id = newsp->id; | |
0a7de745 | 2729 | |
39236c6e A |
2730 | m_freem(m); |
2731 | return key_sendup_mbuf(so, n, KEY_SENDUP_ALL); | |
0a7de745 | 2732 | } |
1c79356b A |
2733 | } |
2734 | ||
2735 | /* | |
2736 | * get new policy id. | |
2737 | * OUT: | |
2738 | * 0: failure. | |
2739 | * others: success. | |
2740 | */ | |
2741 | static u_int32_t | |
6d2010ae | 2742 | key_getnewspid(void) |
1c79356b A |
2743 | { |
2744 | u_int32_t newid = 0; | |
0a7de745 | 2745 | int count = key_spi_trycnt; /* XXX */ |
1c79356b | 2746 | struct secpolicy *sp; |
0a7de745 | 2747 | |
1c79356b | 2748 | /* when requesting to allocate spi ranged */ |
2d21ac55 | 2749 | lck_mtx_lock(sadb_mutex); |
1c79356b | 2750 | while (count--) { |
55e303ae | 2751 | newid = (policy_id = (policy_id == ~0 ? 1 : policy_id + 1)); |
0a7de745 A |
2752 | |
2753 | if ((sp = __key_getspbyid(newid)) == NULL) { | |
1c79356b | 2754 | break; |
0a7de745 A |
2755 | } |
2756 | ||
2d21ac55 | 2757 | key_freesp(sp, KEY_SADB_LOCKED); |
1c79356b | 2758 | } |
2d21ac55 | 2759 | lck_mtx_unlock(sadb_mutex); |
1c79356b | 2760 | if (count == 0 || newid == 0) { |
55e303ae | 2761 | ipseclog((LOG_DEBUG, "key_getnewspid: to allocate policy id is failed.\n")); |
1c79356b A |
2762 | return 0; |
2763 | } | |
0a7de745 | 2764 | |
1c79356b A |
2765 | return newid; |
2766 | } | |
2767 | ||
2768 | /* | |
2769 | * SADB_SPDDELETE processing | |
2770 | * receive | |
2771 | * <base, address(SD), policy(*)> | |
2772 | * from the user(?), and set SADB_SASTATE_DEAD, | |
2773 | * and send, | |
2774 | * <base, address(SD), policy(*)> | |
2775 | * to the ikmpd. | |
2776 | * policy(*) including direction of policy. | |
2777 | * | |
9bccf70c | 2778 | * m will always be freed. |
1c79356b | 2779 | */ |
9bccf70c | 2780 | static int |
6d2010ae | 2781 | key_spddelete( |
0a7de745 A |
2782 | struct socket *so, |
2783 | struct mbuf *m, | |
2784 | const struct sadb_msghdr *mhp) | |
1c79356b | 2785 | { |
5ba3f43e | 2786 | struct sadb_address *src0, *dst0, *src1 = NULL, *dst1 = NULL; |
1c79356b A |
2787 | struct sadb_x_policy *xpl0; |
2788 | struct secpolicyindex spidx; | |
2789 | struct secpolicy *sp; | |
0a7de745 A |
2790 | ifnet_t internal_if = NULL; |
2791 | struct sadb_x_ipsecif *ipsecifopts = NULL; | |
2792 | int use_src_range = 0; | |
2793 | int use_dst_range = 0; | |
2794 | ||
5ba3f43e | 2795 | LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_NOTOWNED); |
0a7de745 | 2796 | |
1c79356b | 2797 | /* sanity check */ |
0a7de745 | 2798 | if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL) { |
1c79356b | 2799 | panic("key_spddelete: NULL pointer is passed.\n"); |
0a7de745 A |
2800 | } |
2801 | ||
2802 | if (mhp->ext[SADB_X_EXT_ADDR_RANGE_SRC_START] != NULL && mhp->ext[SADB_X_EXT_ADDR_RANGE_SRC_END] != NULL) { | |
2803 | use_src_range = 1; | |
2804 | } | |
2805 | if (mhp->ext[SADB_X_EXT_ADDR_RANGE_DST_START] != NULL && mhp->ext[SADB_X_EXT_ADDR_RANGE_DST_END] != NULL) { | |
2806 | use_dst_range = 1; | |
2807 | } | |
2808 | ||
39236c6e | 2809 | if ((!use_src_range && mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL) || |
0a7de745 | 2810 | (!use_dst_range && mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) || |
9bccf70c | 2811 | mhp->ext[SADB_X_EXT_POLICY] == NULL) { |
55e303ae | 2812 | ipseclog((LOG_DEBUG, "key_spddelete: invalid message is passed.\n")); |
9bccf70c A |
2813 | return key_senderror(so, m, EINVAL); |
2814 | } | |
39236c6e | 2815 | if ((use_src_range && (mhp->extlen[SADB_X_EXT_ADDR_RANGE_SRC_START] < sizeof(struct sadb_address) |
0a7de745 A |
2816 | || mhp->extlen[SADB_X_EXT_ADDR_RANGE_SRC_END] < sizeof(struct sadb_address))) || |
2817 | (!use_src_range && mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address)) || | |
2818 | (use_dst_range && (mhp->extlen[SADB_X_EXT_ADDR_RANGE_DST_START] < sizeof(struct sadb_address) | |
2819 | || mhp->extlen[SADB_X_EXT_ADDR_RANGE_DST_END] < sizeof(struct sadb_address))) || | |
2820 | (!use_dst_range && mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) || | |
9bccf70c | 2821 | mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) { |
55e303ae | 2822 | ipseclog((LOG_DEBUG, "key_spddelete: invalid message is passed.\n")); |
9bccf70c | 2823 | return key_senderror(so, m, EINVAL); |
1c79356b | 2824 | } |
0a7de745 A |
2825 | |
2826 | if (use_src_range) { | |
2827 | src0 = (struct sadb_address *)mhp->ext[SADB_X_EXT_ADDR_RANGE_SRC_START]; | |
2828 | src1 = (struct sadb_address *)mhp->ext[SADB_X_EXT_ADDR_RANGE_SRC_END]; | |
2829 | } else { | |
2830 | src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC]; | |
2831 | } | |
2832 | if (use_dst_range) { | |
2833 | dst0 = (struct sadb_address *)mhp->ext[SADB_X_EXT_ADDR_RANGE_DST_START]; | |
2834 | dst1 = (struct sadb_address *)mhp->ext[SADB_X_EXT_ADDR_RANGE_DST_END]; | |
2835 | } else { | |
2836 | dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST]; | |
2837 | } | |
316670eb | 2838 | xpl0 = (struct sadb_x_policy *)(void *)mhp->ext[SADB_X_EXT_POLICY]; |
0a7de745 A |
2839 | ipsecifopts = (struct sadb_x_ipsecif *)(void *)mhp->ext[SADB_X_EXT_IPSECIF]; |
2840 | ||
2841 | /* checking the direction. */ | |
39236c6e | 2842 | switch (xpl0->sadb_x_policy_dir) { |
0a7de745 A |
2843 | case IPSEC_DIR_INBOUND: |
2844 | case IPSEC_DIR_OUTBOUND: | |
2845 | break; | |
2846 | default: | |
2847 | ipseclog((LOG_DEBUG, "key_spddelete: Invalid SP direction.\n")); | |
2848 | return key_senderror(so, m, EINVAL); | |
2849 | } | |
2850 | ||
2851 | /* Process interfaces */ | |
2852 | if (ipsecifopts != NULL) { | |
2853 | if (ipsecifopts->sadb_x_ipsecif_internal_if[0]) { | |
2854 | ifnet_find_by_name(ipsecifopts->sadb_x_ipsecif_internal_if, &internal_if); | |
2855 | } | |
2856 | } | |
2857 | ||
1c79356b | 2858 | /* make secindex */ |
9bccf70c | 2859 | /* XXX boundary check against sa_len */ |
1c79356b | 2860 | KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir, |
0a7de745 A |
2861 | src0 + 1, |
2862 | dst0 + 1, | |
2863 | src0->sadb_address_prefixlen, | |
2864 | dst0->sadb_address_prefixlen, | |
2865 | src0->sadb_address_proto, | |
2866 | internal_if, | |
2867 | use_src_range ? src0 + 1 : NULL, | |
2868 | use_src_range ? src1 + 1 : NULL, | |
2869 | use_dst_range ? dst0 + 1 : NULL, | |
2870 | use_dst_range ? dst1 + 1 : NULL, | |
2871 | &spidx); | |
2872 | ||
1c79356b | 2873 | /* Is there SP in SPD ? */ |
2d21ac55 | 2874 | lck_mtx_lock(sadb_mutex); |
1c79356b | 2875 | if ((sp = key_getsp(&spidx)) == NULL) { |
55e303ae | 2876 | ipseclog((LOG_DEBUG, "key_spddelete: no SP found.\n")); |
2d21ac55 | 2877 | lck_mtx_unlock(sadb_mutex); |
0a7de745 A |
2878 | if (internal_if) { |
2879 | ifnet_release(internal_if); | |
2880 | internal_if = NULL; | |
2881 | } | |
9bccf70c | 2882 | return key_senderror(so, m, EINVAL); |
1c79356b | 2883 | } |
0a7de745 A |
2884 | |
2885 | if (internal_if) { | |
2886 | ifnet_release(internal_if); | |
2887 | internal_if = NULL; | |
2888 | } | |
2889 | ||
1c79356b A |
2890 | /* save policy id to buffer to be returned. */ |
2891 | xpl0->sadb_x_policy_id = sp->id; | |
0a7de745 | 2892 | |
1c79356b | 2893 | sp->state = IPSEC_SPSTATE_DEAD; |
2d21ac55 A |
2894 | key_freesp(sp, KEY_SADB_LOCKED); |
2895 | lck_mtx_unlock(sadb_mutex); | |
0a7de745 A |
2896 | |
2897 | ||
2898 | { | |
39236c6e A |
2899 | struct mbuf *n; |
2900 | struct sadb_msg *newmsg; | |
0a7de745 A |
2901 | int mbufItems[] = {SADB_EXT_RESERVED, SADB_X_EXT_POLICY, |
2902 | SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST, | |
2903 | SADB_X_EXT_ADDR_RANGE_SRC_START, SADB_X_EXT_ADDR_RANGE_SRC_END, | |
2904 | SADB_X_EXT_ADDR_RANGE_DST_START, SADB_X_EXT_ADDR_RANGE_DST_END}; | |
2905 | ||
39236c6e | 2906 | /* create new sadb_msg to reply. */ |
0a7de745 A |
2907 | n = key_gather_mbuf(m, mhp, 1, sizeof(mbufItems) / sizeof(int), mbufItems); |
2908 | if (!n) { | |
39236c6e | 2909 | return key_senderror(so, m, ENOBUFS); |
0a7de745 A |
2910 | } |
2911 | ||
39236c6e A |
2912 | newmsg = mtod(n, struct sadb_msg *); |
2913 | newmsg->sadb_msg_errno = 0; | |
f427ee49 A |
2914 | VERIFY(PFKEY_UNIT64(n->m_pkthdr.len) <= UINT16_MAX); |
2915 | newmsg->sadb_msg_len = (u_int16_t)PFKEY_UNIT64(n->m_pkthdr.len); | |
0a7de745 | 2916 | |
39236c6e A |
2917 | m_freem(m); |
2918 | return key_sendup_mbuf(so, n, KEY_SENDUP_ALL); | |
0a7de745 | 2919 | } |
1c79356b A |
2920 | } |
2921 | ||
2922 | /* | |
2923 | * SADB_SPDDELETE2 processing | |
2924 | * receive | |
2925 | * <base, policy(*)> | |
2926 | * from the user(?), and set SADB_SASTATE_DEAD, | |
2927 | * and send, | |
2928 | * <base, policy(*)> | |
2929 | * to the ikmpd. | |
2930 | * policy(*) including direction of policy. | |
2931 | * | |
9bccf70c | 2932 | * m will always be freed. |
1c79356b | 2933 | */ |
9bccf70c | 2934 | static int |
6d2010ae | 2935 | key_spddelete2( |
0a7de745 A |
2936 | struct socket *so, |
2937 | struct mbuf *m, | |
2938 | const struct sadb_msghdr *mhp) | |
1c79356b | 2939 | { |
1c79356b A |
2940 | u_int32_t id; |
2941 | struct secpolicy *sp; | |
0a7de745 | 2942 | |
5ba3f43e | 2943 | LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_NOTOWNED); |
0a7de745 | 2944 | |
1c79356b | 2945 | /* sanity check */ |
0a7de745 | 2946 | if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL) { |
1c79356b | 2947 | panic("key_spddelete2: NULL pointer is passed.\n"); |
0a7de745 A |
2948 | } |
2949 | ||
9bccf70c A |
2950 | if (mhp->ext[SADB_X_EXT_POLICY] == NULL || |
2951 | mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) { | |
55e303ae | 2952 | ipseclog((LOG_DEBUG, "key_spddelete2: invalid message is passed.\n")); |
9bccf70c A |
2953 | key_senderror(so, m, EINVAL); |
2954 | return 0; | |
1c79356b | 2955 | } |
0a7de745 | 2956 | |
316670eb | 2957 | id = ((struct sadb_x_policy *) |
0a7de745 A |
2958 | (void *)mhp->ext[SADB_X_EXT_POLICY])->sadb_x_policy_id; |
2959 | ||
1c79356b | 2960 | /* Is there SP in SPD ? */ |
2d21ac55 | 2961 | lck_mtx_lock(sadb_mutex); |
39236c6e | 2962 | if ((sp = __key_getspbyid(id)) == NULL) { |
2d21ac55 | 2963 | lck_mtx_unlock(sadb_mutex); |
55e303ae | 2964 | ipseclog((LOG_DEBUG, "key_spddelete2: no SP found id:%u.\n", id)); |
2d21ac55 | 2965 | return key_senderror(so, m, EINVAL); |
1c79356b | 2966 | } |
0a7de745 | 2967 | |
1c79356b | 2968 | sp->state = IPSEC_SPSTATE_DEAD; |
2d21ac55 A |
2969 | key_freesp(sp, KEY_SADB_LOCKED); |
2970 | lck_mtx_unlock(sadb_mutex); | |
0a7de745 A |
2971 | |
2972 | { | |
39236c6e A |
2973 | struct mbuf *n, *nn; |
2974 | struct sadb_msg *newmsg; | |
2975 | int off, len; | |
0a7de745 | 2976 | |
39236c6e A |
2977 | /* create new sadb_msg to reply. */ |
2978 | len = PFKEY_ALIGN8(sizeof(struct sadb_msg)); | |
0a7de745 A |
2979 | |
2980 | if (len > MCLBYTES) { | |
39236c6e | 2981 | return key_senderror(so, m, ENOBUFS); |
0a7de745 | 2982 | } |
39236c6e A |
2983 | MGETHDR(n, M_WAITOK, MT_DATA); |
2984 | if (n && len > MHLEN) { | |
2985 | MCLGET(n, M_WAITOK); | |
2986 | if ((n->m_flags & M_EXT) == 0) { | |
2987 | m_freem(n); | |
2988 | n = NULL; | |
2989 | } | |
9bccf70c | 2990 | } |
0a7de745 | 2991 | if (!n) { |
39236c6e | 2992 | return key_senderror(so, m, ENOBUFS); |
0a7de745 A |
2993 | } |
2994 | ||
39236c6e A |
2995 | n->m_len = len; |
2996 | n->m_next = NULL; | |
2997 | off = 0; | |
0a7de745 | 2998 | |
39236c6e A |
2999 | m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off); |
3000 | off += PFKEY_ALIGN8(sizeof(struct sadb_msg)); | |
0a7de745 | 3001 | |
9bccf70c | 3002 | #if DIAGNOSTIC |
0a7de745 | 3003 | if (off != len) { |
39236c6e | 3004 | panic("length inconsistency in key_spddelete2"); |
0a7de745 | 3005 | } |
1c79356b | 3006 | #endif |
0a7de745 | 3007 | |
39236c6e | 3008 | n->m_next = m_copym(m, mhp->extoff[SADB_X_EXT_POLICY], |
0a7de745 | 3009 | mhp->extlen[SADB_X_EXT_POLICY], M_WAITOK); |
39236c6e A |
3010 | if (!n->m_next) { |
3011 | m_freem(n); | |
3012 | return key_senderror(so, m, ENOBUFS); | |
3013 | } | |
0a7de745 | 3014 | |
39236c6e | 3015 | n->m_pkthdr.len = 0; |
0a7de745 | 3016 | for (nn = n; nn; nn = nn->m_next) { |
39236c6e | 3017 | n->m_pkthdr.len += nn->m_len; |
0a7de745 A |
3018 | } |
3019 | ||
39236c6e A |
3020 | newmsg = mtod(n, struct sadb_msg *); |
3021 | newmsg->sadb_msg_errno = 0; | |
f427ee49 A |
3022 | VERIFY(PFKEY_UNIT64(n->m_pkthdr.len) <= UINT16_MAX); |
3023 | newmsg->sadb_msg_len = (u_int16_t)PFKEY_UNIT64(n->m_pkthdr.len); | |
0a7de745 | 3024 | |
39236c6e A |
3025 | m_freem(m); |
3026 | return key_sendup_mbuf(so, n, KEY_SENDUP_ALL); | |
0a7de745 | 3027 | } |
39236c6e | 3028 | } |
9bccf70c | 3029 | |
39236c6e A |
3030 | static int |
3031 | key_spdenable( | |
0a7de745 A |
3032 | struct socket *so, |
3033 | struct mbuf *m, | |
3034 | const struct sadb_msghdr *mhp) | |
39236c6e A |
3035 | { |
3036 | u_int32_t id; | |
3037 | struct secpolicy *sp; | |
0a7de745 | 3038 | |
5ba3f43e | 3039 | LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_NOTOWNED); |
0a7de745 | 3040 | |
39236c6e | 3041 | /* sanity check */ |
0a7de745 | 3042 | if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL) { |
39236c6e | 3043 | panic("key_spdenable: NULL pointer is passed.\n"); |
0a7de745 A |
3044 | } |
3045 | ||
39236c6e A |
3046 | if (mhp->ext[SADB_X_EXT_POLICY] == NULL || |
3047 | mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) { | |
3048 | ipseclog((LOG_DEBUG, "key_spdenable: invalid message is passed.\n")); | |
3049 | key_senderror(so, m, EINVAL); | |
3050 | return 0; | |
3051 | } | |
0a7de745 | 3052 | |
39236c6e | 3053 | id = ((struct sadb_x_policy *) |
0a7de745 A |
3054 | (void *)mhp->ext[SADB_X_EXT_POLICY])->sadb_x_policy_id; |
3055 | ||
39236c6e A |
3056 | /* Is there SP in SPD ? */ |
3057 | lck_mtx_lock(sadb_mutex); | |
3058 | if ((sp = __key_getspbyid(id)) == NULL) { | |
3059 | lck_mtx_unlock(sadb_mutex); | |
3060 | ipseclog((LOG_DEBUG, "key_spdenable: no SP found id:%u.\n", id)); | |
3061 | return key_senderror(so, m, EINVAL); | |
3062 | } | |
0a7de745 | 3063 | |
39236c6e | 3064 | sp->disabled = 0; |
eb6b6ca3 | 3065 | key_freesp(sp, KEY_SADB_LOCKED); |
39236c6e | 3066 | lck_mtx_unlock(sadb_mutex); |
0a7de745 | 3067 | |
39236c6e A |
3068 | { |
3069 | struct mbuf *n; | |
3070 | struct sadb_msg *newmsg; | |
3071 | int mbufItems[] = {SADB_EXT_RESERVED, SADB_X_EXT_POLICY}; | |
0a7de745 | 3072 | |
39236c6e | 3073 | /* create new sadb_msg to reply. */ |
0a7de745 A |
3074 | n = key_gather_mbuf(m, mhp, 1, sizeof(mbufItems) / sizeof(int), mbufItems); |
3075 | if (!n) { | |
39236c6e | 3076 | return key_senderror(so, m, ENOBUFS); |
0a7de745 A |
3077 | } |
3078 | ||
39236c6e A |
3079 | if (n->m_len < sizeof(struct sadb_msg)) { |
3080 | n = m_pullup(n, sizeof(struct sadb_msg)); | |
0a7de745 | 3081 | if (n == NULL) { |
39236c6e | 3082 | return key_senderror(so, m, ENOBUFS); |
0a7de745 | 3083 | } |
39236c6e A |
3084 | } |
3085 | newmsg = mtod(n, struct sadb_msg *); | |
3086 | newmsg->sadb_msg_errno = 0; | |
f427ee49 A |
3087 | VERIFY(PFKEY_UNIT64(n->m_pkthdr.len) <= UINT16_MAX); |
3088 | newmsg->sadb_msg_len = (u_int16_t)PFKEY_UNIT64(n->m_pkthdr.len); | |
0a7de745 | 3089 | |
39236c6e A |
3090 | m_freem(m); |
3091 | return key_sendup_mbuf(so, n, KEY_SENDUP_ALL); | |
0a7de745 | 3092 | } |
39236c6e | 3093 | } |
1c79356b | 3094 | |
39236c6e A |
3095 | static int |
3096 | key_spddisable( | |
0a7de745 A |
3097 | struct socket *so, |
3098 | struct mbuf *m, | |
3099 | const struct sadb_msghdr *mhp) | |
39236c6e A |
3100 | { |
3101 | u_int32_t id; | |
3102 | struct secpolicy *sp; | |
0a7de745 | 3103 | |
5ba3f43e | 3104 | LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_NOTOWNED); |
0a7de745 | 3105 | |
39236c6e | 3106 | /* sanity check */ |
0a7de745 | 3107 | if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL) { |
39236c6e | 3108 | panic("key_spddisable: NULL pointer is passed.\n"); |
0a7de745 A |
3109 | } |
3110 | ||
39236c6e A |
3111 | if (mhp->ext[SADB_X_EXT_POLICY] == NULL || |
3112 | mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) { | |
3113 | ipseclog((LOG_DEBUG, "key_spddisable: invalid message is passed.\n")); | |
3114 | key_senderror(so, m, EINVAL); | |
3115 | return 0; | |
3116 | } | |
0a7de745 | 3117 | |
39236c6e | 3118 | id = ((struct sadb_x_policy *) |
0a7de745 A |
3119 | (void *)mhp->ext[SADB_X_EXT_POLICY])->sadb_x_policy_id; |
3120 | ||
39236c6e A |
3121 | /* Is there SP in SPD ? */ |
3122 | lck_mtx_lock(sadb_mutex); | |
3123 | if ((sp = __key_getspbyid(id)) == NULL) { | |
3124 | lck_mtx_unlock(sadb_mutex); | |
3125 | ipseclog((LOG_DEBUG, "key_spddisable: no SP found id:%u.\n", id)); | |
3126 | return key_senderror(so, m, EINVAL); | |
3127 | } | |
0a7de745 | 3128 | |
39236c6e | 3129 | sp->disabled = 1; |
eb6b6ca3 | 3130 | key_freesp(sp, KEY_SADB_LOCKED); |
39236c6e | 3131 | lck_mtx_unlock(sadb_mutex); |
0a7de745 | 3132 | |
39236c6e A |
3133 | { |
3134 | struct mbuf *n; | |
3135 | struct sadb_msg *newmsg; | |
3136 | int mbufItems[] = {SADB_EXT_RESERVED, SADB_X_EXT_POLICY}; | |
0a7de745 | 3137 | |
39236c6e | 3138 | /* create new sadb_msg to reply. */ |
0a7de745 A |
3139 | n = key_gather_mbuf(m, mhp, 1, sizeof(mbufItems) / sizeof(int), mbufItems); |
3140 | if (!n) { | |
39236c6e | 3141 | return key_senderror(so, m, ENOBUFS); |
0a7de745 A |
3142 | } |
3143 | ||
39236c6e A |
3144 | if (n->m_len < sizeof(struct sadb_msg)) { |
3145 | n = m_pullup(n, sizeof(struct sadb_msg)); | |
0a7de745 | 3146 | if (n == NULL) { |
39236c6e | 3147 | return key_senderror(so, m, ENOBUFS); |
0a7de745 | 3148 | } |
39236c6e A |
3149 | } |
3150 | newmsg = mtod(n, struct sadb_msg *); | |
3151 | newmsg->sadb_msg_errno = 0; | |
f427ee49 A |
3152 | VERIFY(PFKEY_UNIT64(n->m_pkthdr.len) <= UINT16_MAX); |
3153 | newmsg->sadb_msg_len = (u_int16_t)PFKEY_UNIT64(n->m_pkthdr.len); | |
0a7de745 | 3154 | |
39236c6e A |
3155 | m_freem(m); |
3156 | return key_sendup_mbuf(so, n, KEY_SENDUP_ALL); | |
0a7de745 | 3157 | } |
1c79356b A |
3158 | } |
3159 | ||
3160 | /* | |
3161 | * SADB_X_GET processing | |
3162 | * receive | |
3163 | * <base, policy(*)> | |
3164 | * from the user(?), | |
3165 | * and send, | |
3166 | * <base, address(SD), policy> | |
3167 | * to the ikmpd. | |
3168 | * policy(*) including direction of policy. | |
3169 | * | |
9bccf70c | 3170 | * m will always be freed. |
1c79356b A |
3171 | */ |
3172 | static int | |
6d2010ae | 3173 | key_spdget( |
0a7de745 A |
3174 | struct socket *so, |
3175 | struct mbuf *m, | |
3176 | const struct sadb_msghdr *mhp) | |
1c79356b | 3177 | { |
1c79356b A |
3178 | u_int32_t id; |
3179 | struct secpolicy *sp; | |
9bccf70c | 3180 | struct mbuf *n; |
0a7de745 | 3181 | |
5ba3f43e | 3182 | LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_NOTOWNED); |
0a7de745 | 3183 | |
1c79356b | 3184 | /* sanity check */ |
0a7de745 | 3185 | if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL) { |
1c79356b | 3186 | panic("key_spdget: NULL pointer is passed.\n"); |
0a7de745 A |
3187 | } |
3188 | ||
9bccf70c A |
3189 | if (mhp->ext[SADB_X_EXT_POLICY] == NULL || |
3190 | mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) { | |
55e303ae | 3191 | ipseclog((LOG_DEBUG, "key_spdget: invalid message is passed.\n")); |
9bccf70c | 3192 | return key_senderror(so, m, EINVAL); |
1c79356b | 3193 | } |
0a7de745 | 3194 | |
316670eb | 3195 | id = ((struct sadb_x_policy *) |
0a7de745 A |
3196 | (void *)mhp->ext[SADB_X_EXT_POLICY])->sadb_x_policy_id; |
3197 | ||
1c79356b | 3198 | /* Is there SP in SPD ? */ |
2d21ac55 | 3199 | lck_mtx_lock(sadb_mutex); |
39236c6e | 3200 | if ((sp = __key_getspbyid(id)) == NULL) { |
55e303ae | 3201 | ipseclog((LOG_DEBUG, "key_spdget: no SP found id:%u.\n", id)); |
2d21ac55 | 3202 | lck_mtx_unlock(sadb_mutex); |
9bccf70c | 3203 | return key_senderror(so, m, ENOENT); |
1c79356b | 3204 | } |
2d21ac55 | 3205 | lck_mtx_unlock(sadb_mutex); |
9bccf70c | 3206 | n = key_setdumpsp(sp, SADB_X_SPDGET, 0, mhp->msg->sadb_msg_pid); |
eb6b6ca3 | 3207 | key_freesp(sp, KEY_SADB_UNLOCKED); |
9bccf70c A |
3208 | if (n != NULL) { |
3209 | m_freem(m); | |
3210 | return key_sendup_mbuf(so, n, KEY_SENDUP_ONE); | |
0a7de745 | 3211 | } else { |
9bccf70c | 3212 | return key_senderror(so, m, ENOBUFS); |
0a7de745 | 3213 | } |
1c79356b A |
3214 | } |
3215 | ||
3216 | /* | |
3217 | * SADB_X_SPDACQUIRE processing. | |
3218 | * Acquire policy and SA(s) for a *OUTBOUND* packet. | |
3219 | * send | |
3220 | * <base, policy(*)> | |
3221 | * to KMD, and expect to receive | |
91447636 | 3222 | * <base> with SADB_X_SPDACQUIRE if error occurred, |
1c79356b A |
3223 | * or |
3224 | * <base, policy> | |
3225 | * with SADB_X_SPDUPDATE from KMD by PF_KEY. | |
3226 | * policy(*) is without policy requests. | |
3227 | * | |
3228 | * 0 : succeed | |
3229 | * others: error number | |
3230 | */ | |
3231 | int | |
6d2010ae | 3232 | key_spdacquire( |
0a7de745 | 3233 | struct secpolicy *sp) |
1c79356b | 3234 | { |
9bccf70c | 3235 | struct mbuf *result = NULL, *m; |
1c79356b A |
3236 | struct secspacq *newspacq; |
3237 | int error; | |
0a7de745 | 3238 | |
5ba3f43e | 3239 | LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_NOTOWNED); |
0a7de745 | 3240 | |
1c79356b | 3241 | /* sanity check */ |
0a7de745 | 3242 | if (sp == NULL) { |
1c79356b | 3243 | panic("key_spdacquire: NULL pointer is passed.\n"); |
0a7de745 A |
3244 | } |
3245 | if (sp->req != NULL) { | |
1c79356b | 3246 | panic("key_spdacquire: called but there is request.\n"); |
0a7de745 A |
3247 | } |
3248 | if (sp->policy != IPSEC_POLICY_IPSEC) { | |
1c79356b | 3249 | panic("key_spdacquire: policy mismathed. IPsec is expected.\n"); |
0a7de745 A |
3250 | } |
3251 | ||
1c79356b | 3252 | /* get a entry to check whether sent message or not. */ |
2d21ac55 | 3253 | lck_mtx_lock(sadb_mutex); |
f427ee49 | 3254 | sp->refcnt++; |
1c79356b | 3255 | if ((newspacq = key_getspacq(&sp->spidx)) != NULL) { |
f427ee49 | 3256 | key_freesp(sp, KEY_SADB_LOCKED); |
1c79356b A |
3257 | if (key_blockacq_count < newspacq->count) { |
3258 | /* reset counter and do send message. */ | |
3259 | newspacq->count = 0; | |
3260 | } else { | |
3261 | /* increment counter and do nothing. */ | |
3262 | newspacq->count++; | |
2d21ac55 | 3263 | lck_mtx_unlock(sadb_mutex); |
1c79356b A |
3264 | return 0; |
3265 | } | |
3266 | } else { | |
3267 | /* make new entry for blocking to send SADB_ACQUIRE. */ | |
2d21ac55 | 3268 | if ((newspacq = key_newspacq(&sp->spidx)) == NULL) { |
f427ee49 | 3269 | key_freesp(sp, KEY_SADB_LOCKED); |
2d21ac55 | 3270 | lck_mtx_unlock(sadb_mutex); |
1c79356b | 3271 | return ENOBUFS; |
2d21ac55 | 3272 | } |
f427ee49 | 3273 | key_freesp(sp, KEY_SADB_LOCKED); |
1c79356b A |
3274 | /* add to acqtree */ |
3275 | LIST_INSERT_HEAD(&spacqtree, newspacq, chain); | |
39236c6e | 3276 | key_start_timehandler(); |
1c79356b | 3277 | } |
2d21ac55 | 3278 | lck_mtx_unlock(sadb_mutex); |
1c79356b | 3279 | /* create new sadb_msg to reply. */ |
9bccf70c A |
3280 | m = key_setsadbmsg(SADB_X_SPDACQUIRE, 0, 0, 0, 0, 0); |
3281 | if (!m) { | |
3282 | error = ENOBUFS; | |
3283 | goto fail; | |
1c79356b | 3284 | } |
9bccf70c | 3285 | result = m; |
0a7de745 | 3286 | |
9bccf70c | 3287 | result->m_pkthdr.len = 0; |
0a7de745 | 3288 | for (m = result; m; m = m->m_next) { |
9bccf70c | 3289 | result->m_pkthdr.len += m->m_len; |
0a7de745 A |
3290 | } |
3291 | ||
f427ee49 | 3292 | VERIFY(PFKEY_UNIT64(result->m_pkthdr.len) <= UINT16_MAX); |
9bccf70c | 3293 | mtod(result, struct sadb_msg *)->sadb_msg_len = |
f427ee49 | 3294 | (u_int16_t)PFKEY_UNIT64(result->m_pkthdr.len); |
0a7de745 | 3295 | |
9bccf70c | 3296 | return key_sendup_mbuf(NULL, m, KEY_SENDUP_REGISTERED); |
0a7de745 | 3297 | |
9bccf70c | 3298 | fail: |
0a7de745 | 3299 | if (result) { |
9bccf70c | 3300 | m_freem(result); |
0a7de745 | 3301 | } |
9bccf70c A |
3302 | return error; |
3303 | } | |
3304 | ||
3305 | /* | |
1c79356b A |
3306 | * SADB_SPDFLUSH processing |
3307 | * receive | |
3308 | * <base> | |
3309 | * from the user, and free all entries in secpctree. | |
3310 | * and send, | |
3311 | * <base> | |
3312 | * to the user. | |
3313 | * NOTE: what to do is only marking SADB_SASTATE_DEAD. | |
3314 | * | |
9bccf70c | 3315 | * m will always be freed. |
1c79356b | 3316 | */ |
9bccf70c | 3317 | static int |
6d2010ae | 3318 | key_spdflush( |
0a7de745 A |
3319 | struct socket *so, |
3320 | struct mbuf *m, | |
3321 | const struct sadb_msghdr *mhp) | |
1c79356b | 3322 | { |
9bccf70c | 3323 | struct sadb_msg *newmsg; |
1c79356b A |
3324 | struct secpolicy *sp; |
3325 | u_int dir; | |
0a7de745 | 3326 | |
1c79356b | 3327 | /* sanity check */ |
0a7de745 | 3328 | if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL) { |
1c79356b | 3329 | panic("key_spdflush: NULL pointer is passed.\n"); |
0a7de745 A |
3330 | } |
3331 | ||
3332 | if (m->m_len != PFKEY_ALIGN8(sizeof(struct sadb_msg))) { | |
9bccf70c | 3333 | return key_senderror(so, m, EINVAL); |
0a7de745 A |
3334 | } |
3335 | ||
2d21ac55 | 3336 | lck_mtx_lock(sadb_mutex); |
1c79356b A |
3337 | for (dir = 0; dir < IPSEC_DIR_MAX; dir++) { |
3338 | LIST_FOREACH(sp, &sptree[dir], chain) { | |
3339 | sp->state = IPSEC_SPSTATE_DEAD; | |
3340 | } | |
3341 | } | |
2d21ac55 | 3342 | lck_mtx_unlock(sadb_mutex); |
0a7de745 | 3343 | |
9bccf70c | 3344 | if (sizeof(struct sadb_msg) > m->m_len + M_TRAILINGSPACE(m)) { |
55e303ae | 3345 | ipseclog((LOG_DEBUG, "key_spdflush: No more memory.\n")); |
9bccf70c | 3346 | return key_senderror(so, m, ENOBUFS); |
1c79356b | 3347 | } |
0a7de745 A |
3348 | |
3349 | if (m->m_next) { | |
9bccf70c | 3350 | m_freem(m->m_next); |
0a7de745 | 3351 | } |
9bccf70c A |
3352 | m->m_next = NULL; |
3353 | m->m_pkthdr.len = m->m_len = PFKEY_ALIGN8(sizeof(struct sadb_msg)); | |
3354 | newmsg = mtod(m, struct sadb_msg *); | |
1c79356b | 3355 | newmsg->sadb_msg_errno = 0; |
f427ee49 | 3356 | newmsg->sadb_msg_len = (u_int16_t)PFKEY_UNIT64(m->m_pkthdr.len); |
0a7de745 | 3357 | |
9bccf70c | 3358 | return key_sendup_mbuf(so, m, KEY_SENDUP_ALL); |
1c79356b A |
3359 | } |
3360 | ||
3361 | /* | |
3362 | * SADB_SPDDUMP processing | |
3363 | * receive | |
3364 | * <base> | |
3365 | * from the user, and dump all SP leaves | |
3366 | * and send, | |
3367 | * <base> ..... | |
3368 | * to the ikmpd. | |
3369 | * | |
9bccf70c | 3370 | * m will always be freed. |
1c79356b | 3371 | */ |
39236c6e | 3372 | |
1c79356b | 3373 | static int |
6d2010ae | 3374 | key_spddump( |
0a7de745 A |
3375 | struct socket *so, |
3376 | struct mbuf *m, | |
3377 | const struct sadb_msghdr *mhp) | |
1c79356b | 3378 | { |
2d21ac55 | 3379 | struct secpolicy *sp, **spbuf = NULL, **sp_ptr; |
eb6b6ca3 A |
3380 | u_int32_t cnt = 0, bufcount = 0; |
3381 | size_t total_req_size = 0; | |
1c79356b | 3382 | u_int dir; |
9bccf70c | 3383 | struct mbuf *n; |
2d21ac55 | 3384 | int error = 0; |
0a7de745 | 3385 | |
1c79356b | 3386 | /* sanity check */ |
0a7de745 | 3387 | if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL) { |
1c79356b | 3388 | panic("key_spddump: NULL pointer is passed.\n"); |
0a7de745 A |
3389 | } |
3390 | ||
2d21ac55 A |
3391 | if ((bufcount = ipsec_policy_count) == 0) { |
3392 | error = ENOENT; | |
3393 | goto end; | |
3394 | } | |
eb6b6ca3 A |
3395 | |
3396 | if (os_add_overflow(bufcount, 256, &bufcount)) { | |
3397 | ipseclog((LOG_DEBUG, "key_spddump: bufcount overflow, ipsec policy count %u.\n", ipsec_policy_count)); | |
3398 | bufcount = ipsec_policy_count; | |
3399 | } | |
3400 | ||
3401 | if (os_mul_overflow(bufcount, sizeof(struct secpolicy *), &total_req_size)) { | |
3402 | panic("key_spddump spbuf requested memory overflow %u\n", bufcount); | |
3403 | } | |
3404 | ||
3405 | KMALLOC_WAIT(spbuf, struct secpolicy**, total_req_size); | |
2d21ac55 A |
3406 | if (spbuf == NULL) { |
3407 | ipseclog((LOG_DEBUG, "key_spddump: No more memory.\n")); | |
3408 | error = ENOMEM; | |
3409 | goto end; | |
3410 | } | |
3411 | lck_mtx_lock(sadb_mutex); | |
3412 | /* search SPD entry, make list. */ | |
3413 | sp_ptr = spbuf; | |
1c79356b A |
3414 | for (dir = 0; dir < IPSEC_DIR_MAX; dir++) { |
3415 | LIST_FOREACH(sp, &sptree[dir], chain) { | |
0a7de745 A |
3416 | if (cnt == bufcount) { |
3417 | break; /* buffer full */ | |
3418 | } | |
2d21ac55 A |
3419 | *sp_ptr++ = sp; |
3420 | sp->refcnt++; | |
1c79356b A |
3421 | cnt++; |
3422 | } | |
3423 | } | |
2d21ac55 | 3424 | lck_mtx_unlock(sadb_mutex); |
0a7de745 | 3425 | |
2d21ac55 A |
3426 | if (cnt == 0) { |
3427 | error = ENOENT; | |
3428 | goto end; | |
3429 | } | |
0a7de745 | 3430 | |
2d21ac55 A |
3431 | sp_ptr = spbuf; |
3432 | while (cnt) { | |
3433 | --cnt; | |
3434 | n = key_setdumpsp(*sp_ptr++, SADB_X_SPDDUMP, cnt, | |
0a7de745 A |
3435 | mhp->msg->sadb_msg_pid); |
3436 | ||
3437 | if (n) { | |
2d21ac55 | 3438 | key_sendup_mbuf(so, n, KEY_SENDUP_ONE); |
0a7de745 | 3439 | } |
1c79356b | 3440 | } |
0a7de745 | 3441 | |
2d21ac55 | 3442 | lck_mtx_lock(sadb_mutex); |
0a7de745 | 3443 | while (sp_ptr > spbuf) { |
2d21ac55 | 3444 | key_freesp(*(--sp_ptr), KEY_SADB_LOCKED); |
0a7de745 | 3445 | } |
2d21ac55 | 3446 | lck_mtx_unlock(sadb_mutex); |
0a7de745 | 3447 | |
39236c6e | 3448 | end: |
0a7de745 | 3449 | if (spbuf) { |
2d21ac55 | 3450 | KFREE(spbuf); |
0a7de745 A |
3451 | } |
3452 | if (error) { | |
2d21ac55 | 3453 | return key_senderror(so, m, error); |
0a7de745 A |
3454 | } |
3455 | ||
9bccf70c | 3456 | m_freem(m); |
1c79356b A |
3457 | return 0; |
3458 | } | |
3459 | ||
3460 | static struct mbuf * | |
6d2010ae | 3461 | key_setdumpsp( |
0a7de745 | 3462 | struct secpolicy *sp, |
f427ee49 | 3463 | u_int8_t msg_type, |
0a7de745 A |
3464 | u_int32_t seq, |
3465 | u_int32_t pid) | |
1c79356b | 3466 | { |
9bccf70c | 3467 | struct mbuf *result = NULL, *m; |
0a7de745 | 3468 | |
f427ee49 | 3469 | m = key_setsadbmsg(msg_type, 0, SADB_SATYPE_UNSPEC, seq, pid, (u_int16_t)sp->refcnt); |
0a7de745 | 3470 | if (!m) { |
9bccf70c | 3471 | goto fail; |
0a7de745 | 3472 | } |
9bccf70c | 3473 | result = m; |
0a7de745 A |
3474 | |
3475 | if (sp->spidx.src_range.start.ss_len > 0) { | |
3476 | m = key_setsadbaddr(SADB_X_EXT_ADDR_RANGE_SRC_START, | |
3477 | (struct sockaddr *)&sp->spidx.src_range.start, sp->spidx.prefs, | |
3478 | sp->spidx.ul_proto); | |
3479 | if (!m) { | |
3480 | goto fail; | |
3481 | } | |
3482 | m_cat(result, m); | |
3483 | ||
3484 | m = key_setsadbaddr(SADB_X_EXT_ADDR_RANGE_SRC_END, | |
3485 | (struct sockaddr *)&sp->spidx.src_range.end, sp->spidx.prefs, | |
3486 | sp->spidx.ul_proto); | |
3487 | if (!m) { | |
3488 | goto fail; | |
3489 | } | |
3490 | m_cat(result, m); | |
3491 | } else { | |
3492 | m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC, | |
3493 | (struct sockaddr *)&sp->spidx.src, sp->spidx.prefs, | |
3494 | sp->spidx.ul_proto); | |
3495 | if (!m) { | |
3496 | goto fail; | |
3497 | } | |
3498 | m_cat(result, m); | |
3499 | } | |
3500 | ||
3501 | if (sp->spidx.dst_range.start.ss_len > 0) { | |
3502 | m = key_setsadbaddr(SADB_X_EXT_ADDR_RANGE_DST_START, | |
3503 | (struct sockaddr *)&sp->spidx.dst_range.start, sp->spidx.prefd, | |
3504 | sp->spidx.ul_proto); | |
3505 | if (!m) { | |
3506 | goto fail; | |
3507 | } | |
3508 | m_cat(result, m); | |
3509 | ||
3510 | m = key_setsadbaddr(SADB_X_EXT_ADDR_RANGE_DST_END, | |
3511 | (struct sockaddr *)&sp->spidx.dst_range.end, sp->spidx.prefd, | |
3512 | sp->spidx.ul_proto); | |
3513 | if (!m) { | |
3514 | goto fail; | |
3515 | } | |
3516 | m_cat(result, m); | |
3517 | } else { | |
3518 | m = key_setsadbaddr(SADB_EXT_ADDRESS_DST, | |
3519 | (struct sockaddr *)&sp->spidx.dst, sp->spidx.prefd, | |
3520 | sp->spidx.ul_proto); | |
3521 | if (!m) { | |
3522 | goto fail; | |
3523 | } | |
3524 | m_cat(result, m); | |
3525 | } | |
3526 | ||
3527 | if (sp->spidx.internal_if || sp->outgoing_if || sp->ipsec_if || sp->disabled) { | |
3528 | m = key_setsadbipsecif(sp->spidx.internal_if, sp->outgoing_if, sp->ipsec_if, sp->disabled); | |
3529 | if (!m) { | |
3530 | goto fail; | |
3531 | } | |
3532 | m_cat(result, m); | |
3533 | } | |
3534 | ||
9bccf70c | 3535 | m = key_sp2msg(sp); |
0a7de745 | 3536 | if (!m) { |
9bccf70c | 3537 | goto fail; |
0a7de745 | 3538 | } |
9bccf70c | 3539 | m_cat(result, m); |
0a7de745 A |
3540 | |
3541 | if ((result->m_flags & M_PKTHDR) == 0) { | |
9bccf70c | 3542 | goto fail; |
0a7de745 A |
3543 | } |
3544 | ||
9bccf70c A |
3545 | if (result->m_len < sizeof(struct sadb_msg)) { |
3546 | result = m_pullup(result, sizeof(struct sadb_msg)); | |
0a7de745 | 3547 | if (result == NULL) { |
9bccf70c | 3548 | goto fail; |
0a7de745 | 3549 | } |
1c79356b | 3550 | } |
0a7de745 | 3551 | |
9bccf70c | 3552 | result->m_pkthdr.len = 0; |
0a7de745 | 3553 | for (m = result; m; m = m->m_next) { |
9bccf70c | 3554 | result->m_pkthdr.len += m->m_len; |
0a7de745 A |
3555 | } |
3556 | ||
f427ee49 A |
3557 | if (PFKEY_UNIT64(result->m_pkthdr.len) >= UINT16_MAX) { |
3558 | ipseclog((LOG_DEBUG, "key_setdumpsp: packet header length > UINT16_MAX\n")); | |
3559 | goto fail; | |
3560 | } | |
3561 | ||
9bccf70c | 3562 | mtod(result, struct sadb_msg *)->sadb_msg_len = |
f427ee49 | 3563 | (u_int16_t)PFKEY_UNIT64(result->m_pkthdr.len); |
0a7de745 | 3564 | |
9bccf70c | 3565 | return result; |
0a7de745 | 3566 | |
9bccf70c A |
3567 | fail: |
3568 | m_freem(result); | |
3569 | return NULL; | |
1c79356b A |
3570 | } |
3571 | ||
3572 | /* | |
3573 | * get PFKEY message length for security policy and request. | |
3574 | */ | |
3575 | static u_int | |
6d2010ae | 3576 | key_getspreqmsglen( |
0a7de745 | 3577 | struct secpolicy *sp) |
1c79356b A |
3578 | { |
3579 | u_int tlen; | |
0a7de745 | 3580 | |
1c79356b | 3581 | tlen = sizeof(struct sadb_x_policy); |
0a7de745 | 3582 | |
1c79356b | 3583 | /* if is the policy for ipsec ? */ |
0a7de745 | 3584 | if (sp->policy != IPSEC_POLICY_IPSEC) { |
1c79356b | 3585 | return tlen; |
0a7de745 A |
3586 | } |
3587 | ||
1c79356b | 3588 | /* get length of ipsec requests */ |
0a7de745 | 3589 | { |
39236c6e A |
3590 | struct ipsecrequest *isr; |
3591 | int len; | |
0a7de745 | 3592 | |
39236c6e A |
3593 | for (isr = sp->req; isr != NULL; isr = isr->next) { |
3594 | len = sizeof(struct sadb_x_ipsecrequest) | |
0a7de745 A |
3595 | + isr->saidx.src.ss_len |
3596 | + isr->saidx.dst.ss_len; | |
3597 | ||
39236c6e A |
3598 | tlen += PFKEY_ALIGN8(len); |
3599 | } | |
0a7de745 A |
3600 | } |
3601 | ||
1c79356b A |
3602 | return tlen; |
3603 | } | |
3604 | ||
9bccf70c A |
3605 | /* |
3606 | * SADB_SPDEXPIRE processing | |
3607 | * send | |
3608 | * <base, address(SD), lifetime(CH), policy> | |
3609 | * to KMD by PF_KEY. | |
3610 | * | |
3611 | * OUT: 0 : succeed | |
3612 | * others : error number | |
3613 | */ | |
3614 | static int | |
6d2010ae | 3615 | key_spdexpire( |
0a7de745 | 3616 | struct secpolicy *sp) |
9bccf70c | 3617 | { |
9bccf70c A |
3618 | struct mbuf *result = NULL, *m; |
3619 | int len; | |
6d2010ae | 3620 | int error = EINVAL; |
9bccf70c | 3621 | struct sadb_lifetime *lt; |
0a7de745 | 3622 | |
5ba3f43e | 3623 | LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_NOTOWNED); |
0a7de745 | 3624 | |
9bccf70c | 3625 | /* sanity check */ |
0a7de745 | 3626 | if (sp == NULL) { |
9bccf70c | 3627 | panic("key_spdexpire: NULL pointer is passed.\n"); |
0a7de745 A |
3628 | } |
3629 | ||
9bccf70c A |
3630 | /* set msg header */ |
3631 | m = key_setsadbmsg(SADB_X_SPDEXPIRE, 0, 0, 0, 0, 0); | |
3632 | if (!m) { | |
3633 | error = ENOBUFS; | |
3634 | goto fail; | |
3635 | } | |
3636 | result = m; | |
0a7de745 | 3637 | |
9bccf70c A |
3638 | /* create lifetime extension (current and hard) */ |
3639 | len = PFKEY_ALIGN8(sizeof(*lt)) * 2; | |
3640 | m = key_alloc_mbuf(len); | |
0a7de745 A |
3641 | if (!m || m->m_next) { /*XXX*/ |
3642 | if (m) { | |
9bccf70c | 3643 | m_freem(m); |
0a7de745 | 3644 | } |
9bccf70c A |
3645 | error = ENOBUFS; |
3646 | goto fail; | |
3647 | } | |
3648 | bzero(mtod(m, caddr_t), len); | |
3649 | lt = mtod(m, struct sadb_lifetime *); | |
3650 | lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime)); | |
3651 | lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT; | |
3652 | lt->sadb_lifetime_allocations = 0; | |
3653 | lt->sadb_lifetime_bytes = 0; | |
3654 | lt->sadb_lifetime_addtime = sp->created; | |
3655 | lt->sadb_lifetime_usetime = sp->lastused; | |
316670eb | 3656 | lt = (struct sadb_lifetime *)(void *)(mtod(m, caddr_t) + len / 2); |
9bccf70c A |
3657 | lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime)); |
3658 | lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD; | |
3659 | lt->sadb_lifetime_allocations = 0; | |
3660 | lt->sadb_lifetime_bytes = 0; | |
3661 | lt->sadb_lifetime_addtime = sp->lifetime; | |
3662 | lt->sadb_lifetime_usetime = sp->validtime; | |
3663 | m_cat(result, m); | |
0a7de745 A |
3664 | |
3665 | /* set sadb_address(es) for source */ | |
3666 | if (sp->spidx.src_range.start.ss_len > 0) { | |
3667 | m = key_setsadbaddr(SADB_X_EXT_ADDR_RANGE_SRC_START, | |
3668 | (struct sockaddr *)&sp->spidx.src_range.start, sp->spidx.prefs, | |
3669 | sp->spidx.ul_proto); | |
3670 | if (!m) { | |
3671 | error = ENOBUFS; | |
3672 | goto fail; | |
3673 | } | |
3674 | m_cat(result, m); | |
3675 | ||
3676 | m = key_setsadbaddr(SADB_X_EXT_ADDR_RANGE_SRC_END, | |
3677 | (struct sockaddr *)&sp->spidx.src_range.end, sp->spidx.prefs, | |
3678 | sp->spidx.ul_proto); | |
3679 | if (!m) { | |
3680 | error = ENOBUFS; | |
3681 | goto fail; | |
3682 | } | |
3683 | m_cat(result, m); | |
3684 | } else { | |
3685 | m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC, | |
3686 | (struct sockaddr *)&sp->spidx.src, sp->spidx.prefs, | |
3687 | sp->spidx.ul_proto); | |
3688 | if (!m) { | |
3689 | error = ENOBUFS; | |
3690 | goto fail; | |
3691 | } | |
3692 | m_cat(result, m); | |
3693 | } | |
3694 | ||
3695 | /* set sadb_address(es) for dest */ | |
3696 | if (sp->spidx.dst_range.start.ss_len > 0) { | |
3697 | m = key_setsadbaddr(SADB_X_EXT_ADDR_RANGE_DST_START, | |
3698 | (struct sockaddr *)&sp->spidx.dst_range.start, sp->spidx.prefd, | |
3699 | sp->spidx.ul_proto); | |
3700 | if (!m) { | |
3701 | error = ENOBUFS; | |
3702 | goto fail; | |
3703 | } | |
3704 | m_cat(result, m); | |
3705 | ||
3706 | m = key_setsadbaddr(SADB_X_EXT_ADDR_RANGE_DST_END, | |
3707 | (struct sockaddr *)&sp->spidx.dst_range.end, sp->spidx.prefd, | |
3708 | sp->spidx.ul_proto); | |
3709 | if (!m) { | |
3710 | error = ENOBUFS; | |
3711 | goto fail; | |
3712 | } | |
3713 | m_cat(result, m); | |
3714 | } else { | |
3715 | m = key_setsadbaddr(SADB_EXT_ADDRESS_DST, | |
3716 | (struct sockaddr *)&sp->spidx.dst, sp->spidx.prefd, | |
3717 | sp->spidx.ul_proto); | |
3718 | if (!m) { | |
3719 | error = ENOBUFS; | |
3720 | goto fail; | |
3721 | } | |
3722 | m_cat(result, m); | |
3723 | } | |
3724 | ||
9bccf70c A |
3725 | /* set secpolicy */ |
3726 | m = key_sp2msg(sp); | |
3727 | if (!m) { | |
3728 | error = ENOBUFS; | |
3729 | goto fail; | |
3730 | } | |
3731 | m_cat(result, m); | |
0a7de745 | 3732 | |
9bccf70c A |
3733 | if ((result->m_flags & M_PKTHDR) == 0) { |
3734 | error = EINVAL; | |
3735 | goto fail; | |
3736 | } | |
0a7de745 | 3737 | |
9bccf70c A |
3738 | if (result->m_len < sizeof(struct sadb_msg)) { |
3739 | result = m_pullup(result, sizeof(struct sadb_msg)); | |
3740 | if (result == NULL) { | |
3741 | error = ENOBUFS; | |
3742 | goto fail; | |
3743 | } | |
3744 | } | |
0a7de745 | 3745 | |
9bccf70c | 3746 | result->m_pkthdr.len = 0; |
0a7de745 | 3747 | for (m = result; m; m = m->m_next) { |
9bccf70c | 3748 | result->m_pkthdr.len += m->m_len; |
0a7de745 A |
3749 | } |
3750 | ||
f427ee49 A |
3751 | if (PFKEY_UNIT64(result->m_pkthdr.len) >= UINT16_MAX) { |
3752 | ipseclog((LOG_DEBUG, "key_setdumpsp: packet header length > UINT16_MAX\n")); | |
3753 | goto fail; | |
3754 | } | |
3755 | ||
9bccf70c | 3756 | mtod(result, struct sadb_msg *)->sadb_msg_len = |
f427ee49 | 3757 | (u_int16_t)PFKEY_UNIT64(result->m_pkthdr.len); |
0a7de745 | 3758 | |
9bccf70c | 3759 | return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED); |
0a7de745 | 3760 | |
39236c6e | 3761 | fail: |
0a7de745 | 3762 | if (result) { |
9bccf70c | 3763 | m_freem(result); |
0a7de745 | 3764 | } |
9bccf70c A |
3765 | return error; |
3766 | } | |
3767 | ||
1c79356b A |
3768 | /* %%% SAD management */ |
3769 | /* | |
3770 | * allocating a memory for new SA head, and copy from the values of mhp. | |
3771 | * OUT: NULL : failure due to the lack of memory. | |
3772 | * others : pointer to new SA head. | |
3773 | */ | |
3774 | static struct secashead * | |
fe8ab488 | 3775 | key_newsah(struct secasindex *saidx, |
0a7de745 A |
3776 | ifnet_t ipsec_if, |
3777 | u_int outgoing_if, | |
ea3f0419 A |
3778 | u_int8_t dir, |
3779 | u_int16_t flags) | |
1c79356b A |
3780 | { |
3781 | struct secashead *newsah; | |
0a7de745 | 3782 | |
1c79356b | 3783 | /* sanity check */ |
0a7de745 | 3784 | if (saidx == NULL) { |
1c79356b | 3785 | panic("key_newsaidx: NULL pointer is passed.\n"); |
0a7de745 A |
3786 | } |
3787 | ||
ea3f0419 A |
3788 | VERIFY(flags == SECURITY_ASSOCIATION_PFKEY || flags == SECURITY_ASSOCIATION_CUSTOM_IPSEC); |
3789 | ||
1c79356b | 3790 | newsah = keydb_newsecashead(); |
0a7de745 | 3791 | if (newsah == NULL) { |
1c79356b | 3792 | return NULL; |
0a7de745 A |
3793 | } |
3794 | ||
1c79356b | 3795 | bcopy(saidx, &newsah->saidx, sizeof(newsah->saidx)); |
0a7de745 | 3796 | |
2d21ac55 A |
3797 | /* remove the ports */ |
3798 | switch (saidx->src.ss_family) { | |
0a7de745 A |
3799 | case AF_INET: |
3800 | ((struct sockaddr_in *)(&newsah->saidx.src))->sin_port = IPSEC_PORT_ANY; | |
3801 | break; | |
3802 | case AF_INET6: | |
3803 | ((struct sockaddr_in6 *)(&newsah->saidx.src))->sin6_port = IPSEC_PORT_ANY; | |
3804 | break; | |
3805 | default: | |
3806 | break; | |
2d21ac55 A |
3807 | } |
3808 | switch (saidx->dst.ss_family) { | |
0a7de745 A |
3809 | case AF_INET: |
3810 | ((struct sockaddr_in *)(&newsah->saidx.dst))->sin_port = IPSEC_PORT_ANY; | |
3811 | break; | |
3812 | case AF_INET6: | |
3813 | ((struct sockaddr_in6 *)(&newsah->saidx.dst))->sin6_port = IPSEC_PORT_ANY; | |
3814 | break; | |
3815 | default: | |
3816 | break; | |
39236c6e | 3817 | } |
0a7de745 | 3818 | |
fe8ab488 A |
3819 | newsah->outgoing_if = outgoing_if; |
3820 | if (ipsec_if) { | |
3821 | ifnet_reference(ipsec_if); | |
3822 | newsah->ipsec_if = ipsec_if; | |
3823 | } | |
b0d623f7 | 3824 | newsah->dir = dir; |
1c79356b A |
3825 | /* add to saidxtree */ |
3826 | newsah->state = SADB_SASTATE_MATURE; | |
ea3f0419 A |
3827 | newsah->flags = flags; |
3828 | ||
3829 | if (flags == SECURITY_ASSOCIATION_PFKEY) { | |
3830 | LIST_INSERT_HEAD(&sahtree, newsah, chain); | |
3831 | } else { | |
3832 | LIST_INSERT_HEAD(&custom_sahtree, newsah, chain); | |
3833 | } | |
39236c6e | 3834 | key_start_timehandler(); |
fe8ab488 | 3835 | |
0a7de745 | 3836 | return newsah; |
1c79356b A |
3837 | } |
3838 | ||
3839 | /* | |
3840 | * delete SA index and all SA registerd. | |
3841 | */ | |
316670eb | 3842 | void |
6d2010ae | 3843 | key_delsah( |
0a7de745 | 3844 | struct secashead *sah) |
1c79356b A |
3845 | { |
3846 | struct secasvar *sav, *nextsav; | |
3847 | u_int stateidx, state; | |
1c79356b | 3848 | int zombie = 0; |
0a7de745 | 3849 | |
5ba3f43e | 3850 | LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_OWNED); |
0a7de745 | 3851 | |
1c79356b | 3852 | /* sanity check */ |
0a7de745 | 3853 | if (sah == NULL) { |
1c79356b | 3854 | panic("key_delsah: NULL pointer is passed.\n"); |
0a7de745 A |
3855 | } |
3856 | ||
f427ee49 A |
3857 | if (sah->use_count > 0) { |
3858 | return; | |
3859 | } | |
3860 | ||
1c79356b A |
3861 | /* searching all SA registerd in the secindex. */ |
3862 | for (stateidx = 0; | |
0a7de745 A |
3863 | stateidx < _ARRAYLEN(saorder_state_any); |
3864 | stateidx++) { | |
1c79356b A |
3865 | state = saorder_state_any[stateidx]; |
3866 | for (sav = (struct secasvar *)LIST_FIRST(&sah->savtree[state]); | |
0a7de745 A |
3867 | sav != NULL; |
3868 | sav = nextsav) { | |
1c79356b | 3869 | nextsav = LIST_NEXT(sav, chain); |
0a7de745 | 3870 | |
1c79356b A |
3871 | if (sav->refcnt > 0) { |
3872 | /* give up to delete this sa */ | |
3873 | zombie++; | |
3874 | continue; | |
3875 | } | |
0a7de745 | 3876 | |
1c79356b A |
3877 | /* sanity check */ |
3878 | KEY_CHKSASTATE(state, sav->state, "key_delsah"); | |
0a7de745 | 3879 | |
2d21ac55 | 3880 | key_freesav(sav, KEY_SADB_LOCKED); |
0a7de745 | 3881 | |
1c79356b A |
3882 | /* remove back pointer */ |
3883 | sav->sah = NULL; | |
3884 | sav = NULL; | |
3885 | } | |
3886 | } | |
0a7de745 | 3887 | |
1c79356b | 3888 | /* don't delete sah only if there are savs. */ |
0a7de745 | 3889 | if (zombie) { |
1c79356b | 3890 | return; |
0a7de745 A |
3891 | } |
3892 | ||
39236c6e | 3893 | ROUTE_RELEASE(&sah->sa_route); |
0a7de745 | 3894 | |
fe8ab488 A |
3895 | if (sah->ipsec_if) { |
3896 | ifnet_release(sah->ipsec_if); | |
3897 | sah->ipsec_if = NULL; | |
3898 | } | |
0a7de745 | 3899 | |
1c79356b | 3900 | /* remove from tree of SA index */ |
0a7de745 | 3901 | if (__LIST_CHAINED(sah)) { |
1c79356b | 3902 | LIST_REMOVE(sah, chain); |
0a7de745 A |
3903 | } |
3904 | ||
1c79356b | 3905 | KFREE(sah); |
0a7de745 | 3906 | |
1c79356b A |
3907 | return; |
3908 | } | |
3909 | ||
3910 | /* | |
3911 | * allocating a new SA with LARVAL state. key_add() and key_getspi() call, | |
3912 | * and copy the values of mhp into new buffer. | |
3913 | * When SAD message type is GETSPI: | |
3914 | * to set sequence number from acq_seq++, | |
3915 | * to set zero to SPI. | |
3916 | * not to call key_setsava(). | |
3917 | * OUT: NULL : fail | |
3918 | * others : pointer to new secasvar. | |
9bccf70c A |
3919 | * |
3920 | * does not modify mbuf. does not free mbuf on error. | |
1c79356b A |
3921 | */ |
3922 | static struct secasvar * | |
6d2010ae | 3923 | key_newsav( |
0a7de745 A |
3924 | struct mbuf *m, |
3925 | const struct sadb_msghdr *mhp, | |
3926 | struct secashead *sah, | |
3927 | int *errp, | |
3928 | struct socket *so) | |
1c79356b A |
3929 | { |
3930 | struct secasvar *newsav; | |
9bccf70c | 3931 | const struct sadb_sa *xsa; |
0a7de745 | 3932 | |
5ba3f43e | 3933 | LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_OWNED); |
0a7de745 | 3934 | |
1c79356b | 3935 | /* sanity check */ |
0a7de745 | 3936 | if (m == NULL || mhp == NULL || mhp->msg == NULL || sah == NULL) { |
1c79356b | 3937 | panic("key_newsa: NULL pointer is passed.\n"); |
0a7de745 A |
3938 | } |
3939 | ||
2d21ac55 | 3940 | KMALLOC_NOWAIT(newsav, struct secasvar *, sizeof(struct secasvar)); |
1c79356b | 3941 | if (newsav == NULL) { |
2d21ac55 A |
3942 | lck_mtx_unlock(sadb_mutex); |
3943 | KMALLOC_WAIT(newsav, struct secasvar *, sizeof(struct secasvar)); | |
3944 | lck_mtx_lock(sadb_mutex); | |
3945 | if (newsav == NULL) { | |
3946 | ipseclog((LOG_DEBUG, "key_newsa: No more memory.\n")); | |
3947 | *errp = ENOBUFS; | |
3948 | return NULL; | |
3949 | } | |
1c79356b A |
3950 | } |
3951 | bzero((caddr_t)newsav, sizeof(struct secasvar)); | |
0a7de745 | 3952 | |
9bccf70c | 3953 | switch (mhp->msg->sadb_msg_type) { |
0a7de745 A |
3954 | case SADB_GETSPI: |
3955 | key_setspi(newsav, 0); | |
0a7de745 A |
3956 | newsav->seq = mhp->msg->sadb_msg_seq; |
3957 | break; | |
3958 | ||
3959 | case SADB_ADD: | |
3960 | /* sanity check */ | |
3961 | if (mhp->ext[SADB_EXT_SA] == NULL) { | |
fe8ab488 | 3962 | key_delsav(newsav); |
0a7de745 | 3963 | ipseclog((LOG_DEBUG, "key_newsa: invalid message is passed.\n")); |
9bccf70c | 3964 | *errp = EINVAL; |
1c79356b | 3965 | return NULL; |
0a7de745 A |
3966 | } |
3967 | xsa = (struct sadb_sa *)(void *)mhp->ext[SADB_EXT_SA]; | |
3968 | key_setspi(newsav, xsa->sadb_sa_spi); | |
3969 | newsav->seq = mhp->msg->sadb_msg_seq; | |
3970 | break; | |
3971 | default: | |
3972 | key_delsav(newsav); | |
3973 | *errp = EINVAL; | |
3974 | return NULL; | |
1c79356b | 3975 | } |
0a7de745 | 3976 | |
39236c6e | 3977 | if (mhp->ext[SADB_X_EXT_SA2] != NULL) { |
0a7de745 | 3978 | if (((struct sadb_x_sa2 *)(void *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_alwaysexpire) { |
39236c6e | 3979 | newsav->always_expire = 1; |
0a7de745 | 3980 | } |
fe8ab488 A |
3981 | newsav->flags2 = ((struct sadb_x_sa2 *)(void *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_flags; |
3982 | if (newsav->flags2 & SADB_X_EXT_SA2_DELETE_ON_DETACH) { | |
3983 | newsav->so = so; | |
3984 | } | |
39236c6e | 3985 | } |
0a7de745 | 3986 | |
1c79356b | 3987 | /* copy sav values */ |
9bccf70c A |
3988 | if (mhp->msg->sadb_msg_type != SADB_GETSPI) { |
3989 | *errp = key_setsaval(newsav, m, mhp); | |
3990 | if (*errp) { | |
fe8ab488 | 3991 | key_delsav(newsav); |
9bccf70c A |
3992 | return NULL; |
3993 | } | |
39236c6e A |
3994 | } else { |
3995 | /* For get SPI, if has a hard lifetime, apply */ | |
3996 | const struct sadb_lifetime *lft0; | |
3997 | struct timeval tv; | |
0a7de745 | 3998 | |
39236c6e A |
3999 | lft0 = (struct sadb_lifetime *)(void *)mhp->ext[SADB_EXT_LIFETIME_HARD]; |
4000 | if (lft0 != NULL) { | |
4001 | /* make lifetime for CURRENT */ | |
4002 | KMALLOC_NOWAIT(newsav->lft_c, struct sadb_lifetime *, | |
0a7de745 | 4003 | sizeof(struct sadb_lifetime)); |
39236c6e A |
4004 | if (newsav->lft_c == NULL) { |
4005 | lck_mtx_unlock(sadb_mutex); | |
4006 | KMALLOC_WAIT(newsav->lft_c, struct sadb_lifetime *, | |
0a7de745 | 4007 | sizeof(struct sadb_lifetime)); |
39236c6e A |
4008 | lck_mtx_lock(sadb_mutex); |
4009 | if (newsav->lft_c == NULL) { | |
4010 | ipseclog((LOG_DEBUG, "key_newsa: No more memory.\n")); | |
fe8ab488 | 4011 | key_delsav(newsav); |
39236c6e A |
4012 | *errp = ENOBUFS; |
4013 | return NULL; | |
4014 | } | |
4015 | } | |
0a7de745 | 4016 | |
39236c6e | 4017 | microtime(&tv); |
0a7de745 | 4018 | |
39236c6e A |
4019 | newsav->lft_c->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime)); |
4020 | newsav->lft_c->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT; | |
4021 | newsav->lft_c->sadb_lifetime_allocations = 0; | |
4022 | newsav->lft_c->sadb_lifetime_bytes = 0; | |
4023 | newsav->lft_c->sadb_lifetime_addtime = tv.tv_sec; | |
4024 | newsav->lft_c->sadb_lifetime_usetime = 0; | |
0a7de745 | 4025 | |
39236c6e A |
4026 | if (mhp->extlen[SADB_EXT_LIFETIME_HARD] < sizeof(*lft0)) { |
4027 | ipseclog((LOG_DEBUG, "key_newsa: invalid hard lifetime ext len.\n")); | |
fe8ab488 | 4028 | key_delsav(newsav); |
39236c6e A |
4029 | *errp = EINVAL; |
4030 | return NULL; | |
4031 | } | |
4032 | newsav->lft_h = (struct sadb_lifetime *)key_newbuf(lft0, sizeof(*lft0)); | |
4033 | if (newsav->lft_h == NULL) { | |
4034 | ipseclog((LOG_DEBUG, "key_newsa: No more memory.\n")); | |
fe8ab488 | 4035 | key_delsav(newsav); |
39236c6e A |
4036 | *errp = ENOBUFS; |
4037 | return NULL; | |
4038 | } | |
4039 | } | |
1c79356b | 4040 | } |
0a7de745 | 4041 | |
9bccf70c | 4042 | /* reset created */ |
0a7de745 | 4043 | { |
39236c6e A |
4044 | struct timeval tv; |
4045 | microtime(&tv); | |
4046 | newsav->created = tv.tv_sec; | |
0a7de745 A |
4047 | } |
4048 | ||
9bccf70c | 4049 | newsav->pid = mhp->msg->sadb_msg_pid; |
0a7de745 | 4050 | |
1c79356b A |
4051 | /* add to satree */ |
4052 | newsav->sah = sah; | |
4053 | newsav->refcnt = 1; | |
4054 | newsav->state = SADB_SASTATE_LARVAL; | |
4055 | LIST_INSERT_TAIL(&sah->savtree[SADB_SASTATE_LARVAL], newsav, | |
0a7de745 | 4056 | secasvar, chain); |
2d21ac55 | 4057 | ipsec_sav_count++; |
cb323159 | 4058 | ipsec_monitor_sleep_wake(); |
0a7de745 | 4059 | |
1c79356b A |
4060 | return newsav; |
4061 | } | |
4062 | ||
3e170ce0 A |
4063 | static int |
4064 | key_migratesav(struct secasvar *sav, | |
0a7de745 | 4065 | struct secashead *newsah) |
3e170ce0 A |
4066 | { |
4067 | if (sav == NULL || newsah == NULL || sav->state != SADB_SASTATE_MATURE) { | |
4068 | return EINVAL; | |
4069 | } | |
0a7de745 | 4070 | |
3e170ce0 | 4071 | /* remove from SA header */ |
0a7de745 | 4072 | if (__LIST_CHAINED(sav)) { |
3e170ce0 | 4073 | LIST_REMOVE(sav, chain); |
0a7de745 A |
4074 | } |
4075 | ||
3e170ce0 A |
4076 | sav->sah = newsah; |
4077 | LIST_INSERT_TAIL(&newsah->savtree[SADB_SASTATE_MATURE], sav, secasvar, chain); | |
4078 | return 0; | |
4079 | } | |
4080 | ||
f427ee49 A |
4081 | static void |
4082 | key_reset_sav(struct secasvar *sav) | |
1c79356b | 4083 | { |
5ba3f43e | 4084 | LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_OWNED); |
0a7de745 | 4085 | |
1c79356b | 4086 | /* sanity check */ |
0a7de745 | 4087 | if (sav == NULL) { |
1c79356b | 4088 | panic("key_delsav: NULL pointer is passed.\n"); |
0a7de745 A |
4089 | } |
4090 | ||
f427ee49 A |
4091 | sav->remote_ike_port = 0; |
4092 | sav->natt_encapsulated_src_port = 0; | |
0a7de745 | 4093 | |
9bccf70c A |
4094 | if (sav->key_auth != NULL) { |
4095 | bzero(_KEYBUF(sav->key_auth), _KEYLEN(sav->key_auth)); | |
1c79356b | 4096 | KFREE(sav->key_auth); |
9bccf70c A |
4097 | sav->key_auth = NULL; |
4098 | } | |
4099 | if (sav->key_enc != NULL) { | |
4100 | bzero(_KEYBUF(sav->key_enc), _KEYLEN(sav->key_enc)); | |
1c79356b | 4101 | KFREE(sav->key_enc); |
9bccf70c A |
4102 | sav->key_enc = NULL; |
4103 | } | |
4104 | if (sav->sched) { | |
4105 | bzero(sav->sched, sav->schedlen); | |
4106 | KFREE(sav->sched); | |
4107 | sav->sched = NULL; | |
f427ee49 | 4108 | sav->schedlen = 0; |
9bccf70c | 4109 | } |
cb323159 A |
4110 | |
4111 | for (int i = 0; i < MAX_REPLAY_WINDOWS; i++) { | |
4112 | if (sav->replay[i] != NULL) { | |
4113 | keydb_delsecreplay(sav->replay[i]); | |
4114 | sav->replay[i] = NULL; | |
4115 | } | |
9bccf70c A |
4116 | } |
4117 | if (sav->lft_c != NULL) { | |
1c79356b | 4118 | KFREE(sav->lft_c); |
9bccf70c A |
4119 | sav->lft_c = NULL; |
4120 | } | |
4121 | if (sav->lft_h != NULL) { | |
1c79356b | 4122 | KFREE(sav->lft_h); |
9bccf70c A |
4123 | sav->lft_h = NULL; |
4124 | } | |
4125 | if (sav->lft_s != NULL) { | |
1c79356b | 4126 | KFREE(sav->lft_s); |
9bccf70c A |
4127 | sav->lft_s = NULL; |
4128 | } | |
4129 | if (sav->iv != NULL) { | |
1c79356b | 4130 | KFREE(sav->iv); |
9bccf70c A |
4131 | sav->iv = NULL; |
4132 | } | |
0a7de745 | 4133 | |
f427ee49 A |
4134 | return; |
4135 | } | |
4136 | ||
4137 | /* | |
4138 | * free() SA variable entry. | |
4139 | */ | |
4140 | void | |
4141 | key_delsav( | |
4142 | struct secasvar *sav) | |
4143 | { | |
4144 | LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_OWNED); | |
4145 | ||
4146 | /* sanity check */ | |
4147 | if (sav == NULL) { | |
4148 | panic("key_delsav: NULL pointer is passed.\n"); | |
4149 | } | |
4150 | ||
4151 | if (sav->refcnt > 0) { | |
4152 | return; /* can't free */ | |
4153 | } | |
4154 | /* remove from SA header */ | |
4155 | if (__LIST_CHAINED(sav)) { | |
4156 | LIST_REMOVE(sav, chain); | |
4157 | ipsec_sav_count--; | |
4158 | } | |
4159 | ||
4160 | if (sav->spihash.le_prev || sav->spihash.le_next) { | |
4161 | LIST_REMOVE(sav, spihash); | |
4162 | } | |
4163 | ||
4164 | key_reset_sav(sav); | |
4165 | ||
1c79356b | 4166 | KFREE(sav); |
0a7de745 | 4167 | |
1c79356b A |
4168 | return; |
4169 | } | |
4170 | ||
4171 | /* | |
4172 | * search SAD. | |
4173 | * OUT: | |
4174 | * NULL : not found | |
4175 | * others : found, pointer to a SA. | |
4176 | */ | |
4177 | static struct secashead * | |
ea3f0419 | 4178 | key_getsah(struct secasindex *saidx, u_int16_t flags) |
1c79356b A |
4179 | { |
4180 | struct secashead *sah; | |
0a7de745 | 4181 | |
5ba3f43e | 4182 | LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_OWNED); |
0a7de745 | 4183 | |
ea3f0419 A |
4184 | if ((flags & SECURITY_ASSOCIATION_ANY) == SECURITY_ASSOCIATION_ANY || |
4185 | (flags & SECURITY_ASSOCIATION_PFKEY) == SECURITY_ASSOCIATION_PFKEY) { | |
4186 | LIST_FOREACH(sah, &sahtree, chain) { | |
4187 | if (sah->state == SADB_SASTATE_DEAD) { | |
4188 | continue; | |
4189 | } | |
4190 | if (key_cmpsaidx(&sah->saidx, saidx, CMP_REQID)) { | |
4191 | return sah; | |
4192 | } | |
0a7de745 | 4193 | } |
ea3f0419 A |
4194 | } |
4195 | ||
4196 | if ((flags & SECURITY_ASSOCIATION_ANY) == SECURITY_ASSOCIATION_ANY || | |
4197 | (flags & SECURITY_ASSOCIATION_PFKEY) == SECURITY_ASSOCIATION_CUSTOM_IPSEC) { | |
4198 | LIST_FOREACH(sah, &custom_sahtree, chain) { | |
4199 | if (sah->state == SADB_SASTATE_DEAD) { | |
4200 | continue; | |
4201 | } | |
4202 | if (key_cmpsaidx(&sah->saidx, saidx, 0)) { | |
4203 | return sah; | |
4204 | } | |
0a7de745 | 4205 | } |
1c79356b | 4206 | } |
0a7de745 | 4207 | |
1c79356b A |
4208 | return NULL; |
4209 | } | |
4210 | ||
316670eb | 4211 | struct secashead * |
0a7de745 A |
4212 | key_newsah2(struct secasindex *saidx, |
4213 | u_int8_t dir) | |
316670eb A |
4214 | { |
4215 | struct secashead *sah; | |
0a7de745 | 4216 | |
5ba3f43e | 4217 | LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_OWNED); |
0a7de745 | 4218 | |
ea3f0419 | 4219 | sah = key_getsah(saidx, SECURITY_ASSOCIATION_ANY); |
316670eb | 4220 | if (!sah) { |
ea3f0419 | 4221 | return key_newsah(saidx, NULL, 0, dir, SECURITY_ASSOCIATION_PFKEY); |
316670eb A |
4222 | } |
4223 | return sah; | |
4224 | } | |
4225 | ||
1c79356b A |
4226 | /* |
4227 | * check not to be duplicated SPI. | |
4228 | * NOTE: this function is too slow due to searching all SAD. | |
4229 | * OUT: | |
4230 | * NULL : not found | |
4231 | * others : found, pointer to a SA. | |
4232 | */ | |
4233 | static struct secasvar * | |
6d2010ae | 4234 | key_checkspidup( |
0a7de745 A |
4235 | struct secasindex *saidx, |
4236 | u_int32_t spi) | |
1c79356b | 4237 | { |
1c79356b | 4238 | struct secasvar *sav; |
91447636 | 4239 | u_int stateidx, state; |
0a7de745 | 4240 | |
5ba3f43e | 4241 | LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_OWNED); |
0a7de745 | 4242 | |
1c79356b A |
4243 | /* check address family */ |
4244 | if (saidx->src.ss_family != saidx->dst.ss_family) { | |
55e303ae | 4245 | ipseclog((LOG_DEBUG, "key_checkspidup: address family mismatched.\n")); |
1c79356b A |
4246 | return NULL; |
4247 | } | |
0a7de745 | 4248 | |
1c79356b | 4249 | /* check all SAD */ |
91447636 | 4250 | LIST_FOREACH(sav, &spihash[SPIHASH(spi)], spihash) { |
0a7de745 | 4251 | if (sav->spi != spi) { |
1c79356b | 4252 | continue; |
0a7de745 | 4253 | } |
91447636 | 4254 | for (stateidx = 0; |
0a7de745 A |
4255 | stateidx < _ARRAYLEN(saorder_state_alive); |
4256 | stateidx++) { | |
91447636 A |
4257 | state = saorder_state_alive[stateidx]; |
4258 | if (sav->state == state && | |
0a7de745 | 4259 | key_ismyaddr((struct sockaddr *)&sav->sah->saidx.dst)) { |
91447636 | 4260 | return sav; |
0a7de745 | 4261 | } |
91447636 | 4262 | } |
1c79356b | 4263 | } |
0a7de745 | 4264 | |
1c79356b A |
4265 | return NULL; |
4266 | } | |
4267 | ||
91447636 | 4268 | static void |
6d2010ae | 4269 | key_setspi( |
0a7de745 A |
4270 | struct secasvar *sav, |
4271 | u_int32_t spi) | |
91447636 | 4272 | { |
5ba3f43e | 4273 | LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_OWNED); |
91447636 | 4274 | sav->spi = spi; |
0a7de745 | 4275 | if (sav->spihash.le_prev || sav->spihash.le_next) { |
91447636 | 4276 | LIST_REMOVE(sav, spihash); |
0a7de745 | 4277 | } |
91447636 A |
4278 | LIST_INSERT_HEAD(&spihash[SPIHASH(spi)], sav, spihash); |
4279 | } | |
4280 | ||
4281 | ||
1c79356b A |
4282 | /* |
4283 | * search SAD litmited alive SA, protocol, SPI. | |
4284 | * OUT: | |
4285 | * NULL : not found | |
4286 | * others : found, pointer to a SA. | |
4287 | */ | |
4288 | static struct secasvar * | |
6d2010ae | 4289 | key_getsavbyspi( |
0a7de745 A |
4290 | struct secashead *sah, |
4291 | u_int32_t spi) | |
1c79356b | 4292 | { |
91447636 A |
4293 | struct secasvar *sav, *match; |
4294 | u_int stateidx, state, matchidx; | |
0a7de745 | 4295 | |
5ba3f43e | 4296 | LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_OWNED); |
91447636 A |
4297 | match = NULL; |
4298 | matchidx = _ARRAYLEN(saorder_state_alive); | |
4299 | LIST_FOREACH(sav, &spihash[SPIHASH(spi)], spihash) { | |
0a7de745 | 4300 | if (sav->spi != spi) { |
91447636 | 4301 | continue; |
0a7de745 A |
4302 | } |
4303 | if (sav->sah != sah) { | |
91447636 | 4304 | continue; |
0a7de745 | 4305 | } |
91447636 A |
4306 | for (stateidx = 0; stateidx < matchidx; stateidx++) { |
4307 | state = saorder_state_alive[stateidx]; | |
4308 | if (sav->state == state) { | |
4309 | match = sav; | |
4310 | matchidx = stateidx; | |
4311 | break; | |
1c79356b | 4312 | } |
1c79356b A |
4313 | } |
4314 | } | |
0a7de745 | 4315 | |
91447636 | 4316 | return match; |
1c79356b A |
4317 | } |
4318 | ||
4319 | /* | |
4320 | * copy SA values from PF_KEY message except *SPI, SEQ, PID, STATE and TYPE*. | |
4321 | * You must update these if need. | |
4322 | * OUT: 0: success. | |
9bccf70c A |
4323 | * !0: failure. |
4324 | * | |
4325 | * does not modify mbuf. does not free mbuf on error. | |
1c79356b A |
4326 | */ |
4327 | static int | |
6d2010ae | 4328 | key_setsaval( |
0a7de745 A |
4329 | struct secasvar *sav, |
4330 | struct mbuf *m, | |
4331 | const struct sadb_msghdr *mhp) | |
1c79356b | 4332 | { |
9bccf70c A |
4333 | #if IPSEC_ESP |
4334 | const struct esp_algorithm *algo; | |
4335 | #endif | |
1c79356b | 4336 | int error = 0; |
9bccf70c | 4337 | struct timeval tv; |
0a7de745 | 4338 | |
5ba3f43e | 4339 | LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_OWNED); |
0a7de745 | 4340 | |
1c79356b | 4341 | /* sanity check */ |
0a7de745 | 4342 | if (m == NULL || mhp == NULL || mhp->msg == NULL) { |
1c79356b | 4343 | panic("key_setsaval: NULL pointer is passed.\n"); |
0a7de745 A |
4344 | } |
4345 | ||
1c79356b | 4346 | /* initialization */ |
f427ee49 | 4347 | key_reset_sav(sav); |
55e303ae | 4348 | sav->natt_last_activity = natt_now; |
0a7de745 | 4349 | |
1c79356b | 4350 | /* SA */ |
9bccf70c A |
4351 | if (mhp->ext[SADB_EXT_SA] != NULL) { |
4352 | const struct sadb_sa *sa0; | |
0a7de745 | 4353 | |
316670eb | 4354 | sa0 = (struct sadb_sa *)(void *)mhp->ext[SADB_EXT_SA]; |
9bccf70c | 4355 | if (mhp->extlen[SADB_EXT_SA] < sizeof(*sa0)) { |
3a60a9f5 | 4356 | ipseclog((LOG_DEBUG, "key_setsaval: invalid message size.\n")); |
9bccf70c A |
4357 | error = EINVAL; |
4358 | goto fail; | |
4359 | } | |
0a7de745 | 4360 | |
1c79356b A |
4361 | sav->alg_auth = sa0->sadb_sa_auth; |
4362 | sav->alg_enc = sa0->sadb_sa_encrypt; | |
4363 | sav->flags = sa0->sadb_sa_flags; | |
0a7de745 | 4364 | |
55e303ae A |
4365 | /* |
4366 | * Verify that a nat-traversal port was specified if | |
4367 | * the nat-traversal flag is set. | |
4368 | */ | |
4369 | if ((sav->flags & SADB_X_EXT_NATT) != 0) { | |
4370 | if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa_2) || | |
0a7de745 | 4371 | ((const struct sadb_sa_2*)(sa0))->sadb_sa_natt_port == 0) { |
3a60a9f5 | 4372 | ipseclog((LOG_DEBUG, "key_setsaval: natt port not set.\n")); |
55e303ae A |
4373 | error = EINVAL; |
4374 | goto fail; | |
4375 | } | |
cb323159 | 4376 | sav->natt_encapsulated_src_port = ((const struct sadb_sa_2*)(sa0))->sadb_sa_natt_src_port; |
b0d623f7 | 4377 | sav->remote_ike_port = ((const struct sadb_sa_2*)(sa0))->sadb_sa_natt_port; |
fe8ab488 | 4378 | sav->natt_interval = ((const struct sadb_sa_2*)(sa0))->sadb_sa_natt_interval; |
3e170ce0 | 4379 | sav->natt_offload_interval = ((const struct sadb_sa_2*)(sa0))->sadb_sa_natt_offload_interval; |
55e303ae | 4380 | } |
0a7de745 | 4381 | |
2d21ac55 A |
4382 | /* |
4383 | * Verify if SADB_X_EXT_NATT_MULTIPLEUSERS flag is set that | |
39236c6e | 4384 | * SADB_X_EXT_NATT is set and SADB_X_EXT_NATT_KEEPALIVE is not |
2d21ac55 A |
4385 | * set (we're not behind nat) - otherwise clear it. |
4386 | */ | |
0a7de745 | 4387 | if ((sav->flags & SADB_X_EXT_NATT_MULTIPLEUSERS) != 0) { |
2d21ac55 | 4388 | if ((sav->flags & SADB_X_EXT_NATT) == 0 || |
0a7de745 | 4389 | (sav->flags & SADB_X_EXT_NATT_KEEPALIVE) != 0) { |
2d21ac55 | 4390 | sav->flags &= ~SADB_X_EXT_NATT_MULTIPLEUSERS; |
0a7de745 A |
4391 | } |
4392 | } | |
4393 | ||
1c79356b A |
4394 | /* replay window */ |
4395 | if ((sa0->sadb_sa_flags & SADB_X_EXT_OLD) == 0) { | |
cb323159 A |
4396 | if ((sav->flags2 & SADB_X_EXT_SA2_SEQ_PER_TRAFFIC_CLASS) == |
4397 | SADB_X_EXT_SA2_SEQ_PER_TRAFFIC_CLASS) { | |
4398 | uint32_t range = (1ULL << (sizeof(((struct secreplay *)0)->count) * 8)) / MAX_REPLAY_WINDOWS; | |
4399 | for (int i = 0; i < MAX_REPLAY_WINDOWS; i++) { | |
4400 | sav->replay[i] = keydb_newsecreplay(sa0->sadb_sa_replay); | |
4401 | if (sav->replay[i] == NULL) { | |
4402 | ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n")); | |
4403 | error = ENOBUFS; | |
4404 | goto fail; | |
4405 | } | |
4406 | /* Allowed range for sequence per traffic class */ | |
4407 | sav->replay[i]->count = i * range; | |
4408 | sav->replay[i]->lastseq = ((i + 1) * range) - 1; | |
4409 | } | |
4410 | } else { | |
4411 | sav->replay[0] = keydb_newsecreplay(sa0->sadb_sa_replay); | |
4412 | if (sav->replay[0] == NULL) { | |
4413 | ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n")); | |
4414 | error = ENOBUFS; | |
4415 | goto fail; | |
4416 | } | |
4417 | sav->replay[0]->lastseq = ~0; | |
1c79356b A |
4418 | } |
4419 | } | |
4420 | } | |
0a7de745 | 4421 | |
1c79356b | 4422 | /* Authentication keys */ |
9bccf70c A |
4423 | if (mhp->ext[SADB_EXT_KEY_AUTH] != NULL) { |
4424 | const struct sadb_key *key0; | |
4425 | int len; | |
0a7de745 | 4426 | |
9bccf70c A |
4427 | key0 = (const struct sadb_key *)mhp->ext[SADB_EXT_KEY_AUTH]; |
4428 | len = mhp->extlen[SADB_EXT_KEY_AUTH]; | |
0a7de745 | 4429 | |
1c79356b | 4430 | error = 0; |
9bccf70c | 4431 | if (len < sizeof(*key0)) { |
3a60a9f5 | 4432 | ipseclog((LOG_DEBUG, "key_setsaval: invalid auth key ext len. len = %d\n", len)); |
1c79356b | 4433 | error = EINVAL; |
9bccf70c A |
4434 | goto fail; |
4435 | } | |
4436 | switch (mhp->msg->sadb_msg_satype) { | |
0a7de745 A |
4437 | case SADB_SATYPE_AH: |
4438 | case SADB_SATYPE_ESP: | |
4439 | if (len == PFKEY_ALIGN8(sizeof(struct sadb_key)) && | |
4440 | sav->alg_auth != SADB_X_AALG_NULL) { | |
1c79356b | 4441 | error = EINVAL; |
0a7de745 A |
4442 | } |
4443 | break; | |
0a7de745 A |
4444 | default: |
4445 | error = EINVAL; | |
4446 | break; | |
1c79356b A |
4447 | } |
4448 | if (error) { | |
55e303ae | 4449 | ipseclog((LOG_DEBUG, "key_setsaval: invalid key_auth values.\n")); |
9bccf70c | 4450 | goto fail; |
1c79356b | 4451 | } |
0a7de745 | 4452 | |
1c79356b A |
4453 | sav->key_auth = (struct sadb_key *)key_newbuf(key0, len); |
4454 | if (sav->key_auth == NULL) { | |
55e303ae | 4455 | ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n")); |
1c79356b | 4456 | error = ENOBUFS; |
9bccf70c | 4457 | goto fail; |
1c79356b | 4458 | } |
1c79356b | 4459 | } |
0a7de745 | 4460 | |
1c79356b | 4461 | /* Encryption key */ |
9bccf70c A |
4462 | if (mhp->ext[SADB_EXT_KEY_ENCRYPT] != NULL) { |
4463 | const struct sadb_key *key0; | |
4464 | int len; | |
0a7de745 | 4465 | |
9bccf70c A |
4466 | key0 = (const struct sadb_key *)mhp->ext[SADB_EXT_KEY_ENCRYPT]; |
4467 | len = mhp->extlen[SADB_EXT_KEY_ENCRYPT]; | |
0a7de745 | 4468 | |
1c79356b | 4469 | error = 0; |
9bccf70c | 4470 | if (len < sizeof(*key0)) { |
3a60a9f5 | 4471 | ipseclog((LOG_DEBUG, "key_setsaval: invalid encryption key ext len. len = %d\n", len)); |
1c79356b | 4472 | error = EINVAL; |
9bccf70c A |
4473 | goto fail; |
4474 | } | |
4475 | switch (mhp->msg->sadb_msg_satype) { | |
0a7de745 A |
4476 | case SADB_SATYPE_ESP: |
4477 | if (len == PFKEY_ALIGN8(sizeof(struct sadb_key)) && | |
4478 | sav->alg_enc != SADB_EALG_NULL) { | |
4479 | ipseclog((LOG_DEBUG, "key_setsaval: invalid ESP algorithm.\n")); | |
9bccf70c | 4480 | error = EINVAL; |
39236c6e | 4481 | break; |
0a7de745 A |
4482 | } |
4483 | sav->key_enc = (struct sadb_key *)key_newbuf(key0, len); | |
4484 | if (sav->key_enc == NULL) { | |
4485 | ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n")); | |
4486 | error = ENOBUFS; | |
4487 | goto fail; | |
4488 | } | |
4489 | break; | |
0a7de745 A |
4490 | case SADB_SATYPE_AH: |
4491 | default: | |
4492 | error = EINVAL; | |
4493 | break; | |
1c79356b A |
4494 | } |
4495 | if (error) { | |
3a60a9f5 | 4496 | ipseclog((LOG_DEBUG, "key_setsaval: invalid key_enc value.\n")); |
9bccf70c | 4497 | goto fail; |
1c79356b | 4498 | } |
1c79356b | 4499 | } |
0a7de745 | 4500 | |
1c79356b A |
4501 | /* set iv */ |
4502 | sav->ivlen = 0; | |
0a7de745 | 4503 | |
9bccf70c | 4504 | switch (mhp->msg->sadb_msg_satype) { |
0a7de745 | 4505 | case SADB_SATYPE_ESP: |
1c79356b | 4506 | #if IPSEC_ESP |
0a7de745 A |
4507 | algo = esp_algorithm_lookup(sav->alg_enc); |
4508 | if (algo && algo->ivlen) { | |
4509 | sav->ivlen = (*algo->ivlen)(algo, sav); | |
4510 | } | |
4511 | if (sav->ivlen == 0) { | |
4512 | break; | |
4513 | } | |
4514 | KMALLOC_NOWAIT(sav->iv, caddr_t, sav->ivlen); | |
4515 | if (sav->iv == 0) { | |
4516 | lck_mtx_unlock(sadb_mutex); | |
4517 | KMALLOC_WAIT(sav->iv, caddr_t, sav->ivlen); | |
4518 | lck_mtx_lock(sadb_mutex); | |
2d21ac55 | 4519 | if (sav->iv == 0) { |
0a7de745 A |
4520 | ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n")); |
4521 | error = ENOBUFS; | |
4522 | goto fail; | |
d190cdc3 | 4523 | } |
0a7de745 A |
4524 | } |
4525 | ||
4526 | /* initialize */ | |
4527 | if (sav->alg_enc == SADB_X_EALG_AES_GCM) { | |
4528 | bzero(sav->iv, sav->ivlen); | |
4529 | } else { | |
4530 | key_randomfill(sav->iv, sav->ivlen); | |
4531 | } | |
1c79356b | 4532 | #endif |
0a7de745 A |
4533 | break; |
4534 | case SADB_SATYPE_AH: | |
0a7de745 A |
4535 | break; |
4536 | default: | |
4537 | ipseclog((LOG_DEBUG, "key_setsaval: invalid SA type.\n")); | |
4538 | error = EINVAL; | |
4539 | goto fail; | |
1c79356b | 4540 | } |
0a7de745 | 4541 | |
9bccf70c A |
4542 | /* reset created */ |
4543 | microtime(&tv); | |
4544 | sav->created = tv.tv_sec; | |
0a7de745 | 4545 | |
1c79356b | 4546 | /* make lifetime for CURRENT */ |
2d21ac55 | 4547 | KMALLOC_NOWAIT(sav->lft_c, struct sadb_lifetime *, |
0a7de745 | 4548 | sizeof(struct sadb_lifetime)); |
1c79356b | 4549 | if (sav->lft_c == NULL) { |
2d21ac55 A |
4550 | lck_mtx_unlock(sadb_mutex); |
4551 | KMALLOC_WAIT(sav->lft_c, struct sadb_lifetime *, | |
0a7de745 A |
4552 | sizeof(struct sadb_lifetime)); |
4553 | lck_mtx_lock(sadb_mutex); | |
2d21ac55 A |
4554 | if (sav->lft_c == NULL) { |
4555 | ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n")); | |
4556 | error = ENOBUFS; | |
4557 | goto fail; | |
4558 | } | |
1c79356b | 4559 | } |
0a7de745 | 4560 | |
1c79356b | 4561 | microtime(&tv); |
0a7de745 | 4562 | |
1c79356b | 4563 | sav->lft_c->sadb_lifetime_len = |
0a7de745 | 4564 | PFKEY_UNIT64(sizeof(struct sadb_lifetime)); |
1c79356b A |
4565 | sav->lft_c->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT; |
4566 | sav->lft_c->sadb_lifetime_allocations = 0; | |
4567 | sav->lft_c->sadb_lifetime_bytes = 0; | |
4568 | sav->lft_c->sadb_lifetime_addtime = tv.tv_sec; | |
4569 | sav->lft_c->sadb_lifetime_usetime = 0; | |
0a7de745 | 4570 | |
1c79356b | 4571 | /* lifetimes for HARD and SOFT */ |
0a7de745 | 4572 | { |
39236c6e | 4573 | const struct sadb_lifetime *lft0; |
0a7de745 | 4574 | |
39236c6e | 4575 | lft0 = (struct sadb_lifetime *) |
0a7de745 | 4576 | (void *)mhp->ext[SADB_EXT_LIFETIME_HARD]; |
39236c6e A |
4577 | if (lft0 != NULL) { |
4578 | if (mhp->extlen[SADB_EXT_LIFETIME_HARD] < sizeof(*lft0)) { | |
4579 | ipseclog((LOG_DEBUG, "key_setsaval: invalid hard lifetime ext len.\n")); | |
4580 | error = EINVAL; | |
4581 | goto fail; | |
4582 | } | |
4583 | sav->lft_h = (struct sadb_lifetime *)key_newbuf(lft0, | |
0a7de745 | 4584 | sizeof(*lft0)); |
39236c6e A |
4585 | if (sav->lft_h == NULL) { |
4586 | ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n")); | |
4587 | error = ENOBUFS; | |
4588 | goto fail; | |
4589 | } | |
4590 | /* to be initialize ? */ | |
1c79356b | 4591 | } |
0a7de745 | 4592 | |
39236c6e | 4593 | lft0 = (struct sadb_lifetime *) |
0a7de745 | 4594 | (void *)mhp->ext[SADB_EXT_LIFETIME_SOFT]; |
39236c6e A |
4595 | if (lft0 != NULL) { |
4596 | if (mhp->extlen[SADB_EXT_LIFETIME_SOFT] < sizeof(*lft0)) { | |
4597 | ipseclog((LOG_DEBUG, "key_setsaval: invalid soft lifetime ext len.\n")); | |
4598 | error = EINVAL; | |
4599 | goto fail; | |
4600 | } | |
4601 | sav->lft_s = (struct sadb_lifetime *)key_newbuf(lft0, | |
0a7de745 | 4602 | sizeof(*lft0)); |
39236c6e A |
4603 | if (sav->lft_s == NULL) { |
4604 | ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n")); | |
4605 | error = ENOBUFS; | |
4606 | goto fail; | |
4607 | } | |
4608 | /* to be initialize ? */ | |
1c79356b | 4609 | } |
0a7de745 A |
4610 | } |
4611 | ||
1c79356b | 4612 | return 0; |
0a7de745 | 4613 | |
39236c6e | 4614 | fail: |
f427ee49 | 4615 | key_reset_sav(sav); |
316670eb A |
4616 | return error; |
4617 | } | |
4618 | ||
1c79356b A |
4619 | /* |
4620 | * validation with a secasvar entry, and set SADB_SATYPE_MATURE. | |
4621 | * OUT: 0: valid | |
4622 | * other: errno | |
4623 | */ | |
4624 | static int | |
6d2010ae | 4625 | key_mature( |
0a7de745 | 4626 | struct secasvar *sav) |
1c79356b A |
4627 | { |
4628 | int mature; | |
0a7de745 A |
4629 | int checkmask = 0; /* 2^0: ealg 2^1: aalg 2^2: calg */ |
4630 | int mustmask = 0; /* 2^0: ealg 2^1: aalg 2^2: calg */ | |
4631 | ||
1c79356b | 4632 | mature = 0; |
0a7de745 | 4633 | |
5ba3f43e | 4634 | LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_OWNED); |
0a7de745 | 4635 | |
1c79356b | 4636 | /* check SPI value */ |
9bccf70c | 4637 | switch (sav->sah->saidx.proto) { |
0a7de745 A |
4638 | case IPPROTO_ESP: |
4639 | case IPPROTO_AH: | |
4640 | ||
4641 | /* No reason to test if this is >= 0, because ntohl(sav->spi) is unsigned. */ | |
4642 | if (ntohl(sav->spi) <= 255) { | |
4643 | ipseclog((LOG_DEBUG, | |
4644 | "key_mature: illegal range of SPI %u.\n", | |
4645 | (u_int32_t)ntohl(sav->spi))); | |
4646 | return EINVAL; | |
4647 | } | |
4648 | break; | |
1c79356b | 4649 | } |
0a7de745 | 4650 | |
1c79356b A |
4651 | /* check satype */ |
4652 | switch (sav->sah->saidx.proto) { | |
0a7de745 A |
4653 | case IPPROTO_ESP: |
4654 | /* check flags */ | |
4655 | if ((sav->flags & SADB_X_EXT_OLD) | |
4656 | && (sav->flags & SADB_X_EXT_DERIV)) { | |
4657 | ipseclog((LOG_DEBUG, "key_mature: " | |
4658 | "invalid flag (derived) given to old-esp.\n")); | |
4659 | return EINVAL; | |
4660 | } | |
4661 | if (sav->alg_auth == SADB_AALG_NONE) { | |
4662 | checkmask = 1; | |
4663 | } else { | |
4664 | checkmask = 3; | |
4665 | } | |
4666 | mustmask = 1; | |
4667 | break; | |
4668 | case IPPROTO_AH: | |
4669 | /* check flags */ | |
4670 | if (sav->flags & SADB_X_EXT_DERIV) { | |
4671 | ipseclog((LOG_DEBUG, "key_mature: " | |
4672 | "invalid flag (derived) given to AH SA.\n")); | |
4673 | return EINVAL; | |
4674 | } | |
4675 | if (sav->alg_enc != SADB_EALG_NONE) { | |
4676 | ipseclog((LOG_DEBUG, "key_mature: " | |
4677 | "protocol and algorithm mismated.\n")); | |
4678 | return EINVAL; | |
4679 | } | |
4680 | checkmask = 2; | |
4681 | mustmask = 2; | |
4682 | break; | |
0a7de745 A |
4683 | default: |
4684 | ipseclog((LOG_DEBUG, "key_mature: Invalid satype.\n")); | |
4685 | return EPROTONOSUPPORT; | |
1c79356b | 4686 | } |
0a7de745 | 4687 | |
1c79356b A |
4688 | /* check authentication algorithm */ |
4689 | if ((checkmask & 2) != 0) { | |
9bccf70c | 4690 | const struct ah_algorithm *algo; |
1c79356b | 4691 | int keylen; |
0a7de745 | 4692 | |
9bccf70c A |
4693 | algo = ah_algorithm_lookup(sav->alg_auth); |
4694 | if (!algo) { | |
0a7de745 A |
4695 | ipseclog((LOG_DEBUG, "key_mature: " |
4696 | "unknown authentication algorithm.\n")); | |
1c79356b A |
4697 | return EINVAL; |
4698 | } | |
0a7de745 | 4699 | |
1c79356b | 4700 | /* algorithm-dependent check */ |
0a7de745 | 4701 | if (sav->key_auth) { |
1c79356b | 4702 | keylen = sav->key_auth->sadb_key_bits; |
0a7de745 | 4703 | } else { |
1c79356b | 4704 | keylen = 0; |
0a7de745 | 4705 | } |
1c79356b | 4706 | if (keylen < algo->keymin || algo->keymax < keylen) { |
55e303ae | 4707 | ipseclog((LOG_DEBUG, |
0a7de745 A |
4708 | "key_mature: invalid AH key length %d " |
4709 | "(%d-%d allowed)\n", | |
4710 | keylen, algo->keymin, algo->keymax)); | |
1c79356b A |
4711 | return EINVAL; |
4712 | } | |
0a7de745 | 4713 | |
1c79356b A |
4714 | if (algo->mature) { |
4715 | if ((*algo->mature)(sav)) { | |
4716 | /* message generated in per-algorithm function*/ | |
4717 | return EINVAL; | |
0a7de745 | 4718 | } else { |
1c79356b | 4719 | mature = SADB_SATYPE_AH; |
0a7de745 | 4720 | } |
1c79356b | 4721 | } |
0a7de745 A |
4722 | |
4723 | if ((mustmask & 2) != 0 && mature != SADB_SATYPE_AH) { | |
55e303ae | 4724 | ipseclog((LOG_DEBUG, "key_mature: no satisfy algorithm for AH\n")); |
1c79356b A |
4725 | return EINVAL; |
4726 | } | |
4727 | } | |
0a7de745 | 4728 | |
1c79356b A |
4729 | /* check encryption algorithm */ |
4730 | if ((checkmask & 1) != 0) { | |
4731 | #if IPSEC_ESP | |
9bccf70c | 4732 | const struct esp_algorithm *algo; |
1c79356b | 4733 | int keylen; |
0a7de745 | 4734 | |
9bccf70c A |
4735 | algo = esp_algorithm_lookup(sav->alg_enc); |
4736 | if (!algo) { | |
55e303ae | 4737 | ipseclog((LOG_DEBUG, "key_mature: unknown encryption algorithm.\n")); |
1c79356b A |
4738 | return EINVAL; |
4739 | } | |
0a7de745 | 4740 | |
1c79356b | 4741 | /* algorithm-dependent check */ |
0a7de745 | 4742 | if (sav->key_enc) { |
1c79356b | 4743 | keylen = sav->key_enc->sadb_key_bits; |
0a7de745 | 4744 | } else { |
1c79356b | 4745 | keylen = 0; |
0a7de745 | 4746 | } |
1c79356b | 4747 | if (keylen < algo->keymin || algo->keymax < keylen) { |
55e303ae | 4748 | ipseclog((LOG_DEBUG, |
0a7de745 A |
4749 | "key_mature: invalid ESP key length %d " |
4750 | "(%d-%d allowed)\n", | |
4751 | keylen, algo->keymin, algo->keymax)); | |
1c79356b A |
4752 | return EINVAL; |
4753 | } | |
0a7de745 | 4754 | |
1c79356b A |
4755 | if (algo->mature) { |
4756 | if ((*algo->mature)(sav)) { | |
4757 | /* message generated in per-algorithm function*/ | |
4758 | return EINVAL; | |
0a7de745 | 4759 | } else { |
1c79356b | 4760 | mature = SADB_SATYPE_ESP; |
0a7de745 | 4761 | } |
1c79356b | 4762 | } |
0a7de745 A |
4763 | |
4764 | if ((mustmask & 1) != 0 && mature != SADB_SATYPE_ESP) { | |
55e303ae | 4765 | ipseclog((LOG_DEBUG, "key_mature: no satisfy algorithm for ESP\n")); |
1c79356b A |
4766 | return EINVAL; |
4767 | } | |
4768 | #else /*IPSEC_ESP*/ | |
55e303ae | 4769 | ipseclog((LOG_DEBUG, "key_mature: ESP not supported in this configuration\n")); |
1c79356b A |
4770 | return EINVAL; |
4771 | #endif | |
4772 | } | |
0a7de745 | 4773 | |
1c79356b | 4774 | key_sa_chgstate(sav, SADB_SASTATE_MATURE); |
0a7de745 | 4775 | |
1c79356b A |
4776 | return 0; |
4777 | } | |
4778 | ||
4779 | /* | |
4780 | * subroutine for SADB_GET and SADB_DUMP. | |
1c79356b | 4781 | */ |
9bccf70c | 4782 | static struct mbuf * |
6d2010ae | 4783 | key_setdumpsa( |
0a7de745 A |
4784 | struct secasvar *sav, |
4785 | u_int8_t type, | |
4786 | u_int8_t satype, | |
4787 | u_int32_t seq, | |
4788 | u_int32_t pid) | |
1c79356b | 4789 | { |
9bccf70c A |
4790 | struct mbuf *result = NULL, *tres = NULL, *m; |
4791 | int l = 0; | |
1c79356b | 4792 | int i; |
9bccf70c A |
4793 | void *p; |
4794 | int dumporder[] = { | |
4795 | SADB_EXT_SA, SADB_X_EXT_SA2, | |
4796 | SADB_EXT_LIFETIME_HARD, SADB_EXT_LIFETIME_SOFT, | |
4797 | SADB_EXT_LIFETIME_CURRENT, SADB_EXT_ADDRESS_SRC, | |
4798 | SADB_EXT_ADDRESS_DST, SADB_EXT_ADDRESS_PROXY, SADB_EXT_KEY_AUTH, | |
4799 | SADB_EXT_KEY_ENCRYPT, SADB_EXT_IDENTITY_SRC, | |
4800 | SADB_EXT_IDENTITY_DST, SADB_EXT_SENSITIVITY, | |
4801 | }; | |
0a7de745 | 4802 | |
f427ee49 | 4803 | m = key_setsadbmsg(type, 0, satype, seq, pid, (u_int16_t)sav->refcnt); |
0a7de745 | 4804 | if (m == NULL) { |
9bccf70c | 4805 | goto fail; |
0a7de745 | 4806 | } |
9bccf70c | 4807 | result = m; |
0a7de745 A |
4808 | |
4809 | for (i = sizeof(dumporder) / sizeof(dumporder[0]) - 1; i >= 0; i--) { | |
9bccf70c A |
4810 | m = NULL; |
4811 | p = NULL; | |
4812 | switch (dumporder[i]) { | |
0a7de745 A |
4813 | case SADB_EXT_SA: |
4814 | m = key_setsadbsa(sav); | |
4815 | if (!m) { | |
4816 | goto fail; | |
4817 | } | |
4818 | break; | |
4819 | ||
4820 | case SADB_X_EXT_SA2: | |
4821 | m = key_setsadbxsa2(sav->sah->saidx.mode, | |
cb323159 | 4822 | sav->replay[0] ? sav->replay[0]->count : 0, |
0a7de745 A |
4823 | sav->sah->saidx.reqid, |
4824 | sav->flags2); | |
4825 | if (!m) { | |
4826 | goto fail; | |
4827 | } | |
4828 | break; | |
4829 | ||
4830 | case SADB_EXT_ADDRESS_SRC: | |
4831 | m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC, | |
4832 | (struct sockaddr *)&sav->sah->saidx.src, | |
4833 | FULLMASK, IPSEC_ULPROTO_ANY); | |
4834 | if (!m) { | |
4835 | goto fail; | |
4836 | } | |
4837 | break; | |
4838 | ||
4839 | case SADB_EXT_ADDRESS_DST: | |
4840 | m = key_setsadbaddr(SADB_EXT_ADDRESS_DST, | |
4841 | (struct sockaddr *)&sav->sah->saidx.dst, | |
4842 | FULLMASK, IPSEC_ULPROTO_ANY); | |
4843 | if (!m) { | |
4844 | goto fail; | |
4845 | } | |
4846 | break; | |
4847 | ||
4848 | case SADB_EXT_KEY_AUTH: | |
4849 | if (!sav->key_auth) { | |
9bccf70c | 4850 | continue; |
0a7de745 A |
4851 | } |
4852 | l = PFKEY_UNUNIT64(sav->key_auth->sadb_key_len); | |
4853 | p = sav->key_auth; | |
4854 | break; | |
4855 | ||
4856 | case SADB_EXT_KEY_ENCRYPT: | |
4857 | if (!sav->key_enc) { | |
4858 | continue; | |
4859 | } | |
4860 | l = PFKEY_UNUNIT64(sav->key_enc->sadb_key_len); | |
4861 | p = sav->key_enc; | |
4862 | break; | |
4863 | ||
4864 | case SADB_EXT_LIFETIME_CURRENT: | |
4865 | if (!sav->lft_c) { | |
4866 | continue; | |
4867 | } | |
4868 | l = PFKEY_UNUNIT64(((struct sadb_ext *)sav->lft_c)->sadb_ext_len); | |
4869 | p = sav->lft_c; | |
4870 | break; | |
4871 | ||
4872 | case SADB_EXT_LIFETIME_HARD: | |
4873 | if (!sav->lft_h) { | |
4874 | continue; | |
4875 | } | |
4876 | l = PFKEY_UNUNIT64(((struct sadb_ext *)sav->lft_h)->sadb_ext_len); | |
4877 | p = sav->lft_h; | |
4878 | break; | |
4879 | ||
4880 | case SADB_EXT_LIFETIME_SOFT: | |
4881 | if (!sav->lft_s) { | |
4882 | continue; | |
4883 | } | |
4884 | l = PFKEY_UNUNIT64(((struct sadb_ext *)sav->lft_s)->sadb_ext_len); | |
4885 | p = sav->lft_s; | |
4886 | break; | |
4887 | ||
4888 | case SADB_EXT_ADDRESS_PROXY: | |
4889 | case SADB_EXT_IDENTITY_SRC: | |
4890 | case SADB_EXT_IDENTITY_DST: | |
4891 | /* XXX: should we brought from SPD ? */ | |
4892 | case SADB_EXT_SENSITIVITY: | |
4893 | default: | |
4894 | continue; | |
9bccf70c | 4895 | } |
0a7de745 A |
4896 | |
4897 | if ((!m && !p) || (m && p)) { | |
9bccf70c | 4898 | goto fail; |
0a7de745 | 4899 | } |
9bccf70c | 4900 | if (p && tres) { |
3e170ce0 | 4901 | M_PREPEND(tres, l, M_WAITOK, 1); |
0a7de745 | 4902 | if (!tres) { |
9bccf70c | 4903 | goto fail; |
0a7de745 | 4904 | } |
9bccf70c A |
4905 | bcopy(p, mtod(tres, caddr_t), l); |
4906 | continue; | |
1c79356b | 4907 | } |
9bccf70c A |
4908 | if (p) { |
4909 | m = key_alloc_mbuf(l); | |
0a7de745 | 4910 | if (!m) { |
9bccf70c | 4911 | goto fail; |
0a7de745 | 4912 | } |
9bccf70c A |
4913 | m_copyback(m, 0, l, p); |
4914 | } | |
0a7de745 A |
4915 | |
4916 | if (tres) { | |
9bccf70c | 4917 | m_cat(m, tres); |
0a7de745 | 4918 | } |
9bccf70c | 4919 | tres = m; |
1c79356b | 4920 | } |
0a7de745 | 4921 | |
9bccf70c | 4922 | m_cat(result, tres); |
0a7de745 | 4923 | |
fe8ab488 | 4924 | if (sav->sah && (sav->sah->outgoing_if || sav->sah->ipsec_if)) { |
0a7de745 A |
4925 | m = key_setsadbipsecif(NULL, ifindex2ifnet[sav->sah->outgoing_if], sav->sah->ipsec_if, 0); |
4926 | if (!m) { | |
4927 | goto fail; | |
4928 | } | |
4929 | m_cat(result, m); | |
4930 | } | |
4931 | ||
9bccf70c A |
4932 | if (result->m_len < sizeof(struct sadb_msg)) { |
4933 | result = m_pullup(result, sizeof(struct sadb_msg)); | |
0a7de745 | 4934 | if (result == NULL) { |
9bccf70c | 4935 | goto fail; |
0a7de745 | 4936 | } |
9bccf70c | 4937 | } |
0a7de745 | 4938 | |
9bccf70c | 4939 | result->m_pkthdr.len = 0; |
0a7de745 | 4940 | for (m = result; m; m = m->m_next) { |
9bccf70c | 4941 | result->m_pkthdr.len += m->m_len; |
0a7de745 A |
4942 | } |
4943 | ||
f427ee49 | 4944 | VERIFY(PFKEY_UNIT64(result->m_pkthdr.len) <= UINT16_MAX); |
9bccf70c | 4945 | mtod(result, struct sadb_msg *)->sadb_msg_len = |
f427ee49 | 4946 | (u_int16_t)PFKEY_UNIT64(result->m_pkthdr.len); |
0a7de745 | 4947 | |
9bccf70c | 4948 | return result; |
0a7de745 | 4949 | |
9bccf70c A |
4950 | fail: |
4951 | m_freem(result); | |
4952 | m_freem(tres); | |
4953 | return NULL; | |
1c79356b | 4954 | } |
1c79356b A |
4955 | |
4956 | /* | |
4957 | * set data into sadb_msg. | |
1c79356b | 4958 | */ |
9bccf70c | 4959 | static struct mbuf * |
6d2010ae | 4960 | key_setsadbmsg( |
0a7de745 A |
4961 | u_int8_t type, |
4962 | u_int16_t tlen, | |
4963 | u_int8_t satype, | |
4964 | u_int32_t seq, | |
4965 | pid_t pid, | |
4966 | u_int16_t reserved) | |
1c79356b | 4967 | { |
9bccf70c | 4968 | struct mbuf *m; |
1c79356b | 4969 | struct sadb_msg *p; |
9bccf70c | 4970 | int len; |
0a7de745 | 4971 | |
9bccf70c | 4972 | len = PFKEY_ALIGN8(sizeof(struct sadb_msg)); |
0a7de745 | 4973 | if (len > MCLBYTES) { |
9bccf70c | 4974 | return NULL; |
0a7de745 | 4975 | } |
9bccf70c A |
4976 | MGETHDR(m, M_DONTWAIT, MT_DATA); |
4977 | if (m && len > MHLEN) { | |
4978 | MCLGET(m, M_DONTWAIT); | |
4979 | if ((m->m_flags & M_EXT) == 0) { | |
4980 | m_freem(m); | |
4981 | m = NULL; | |
4982 | } | |
4983 | } | |
0a7de745 | 4984 | if (!m) { |
9bccf70c | 4985 | return NULL; |
0a7de745 | 4986 | } |
9bccf70c A |
4987 | m->m_pkthdr.len = m->m_len = len; |
4988 | m->m_next = NULL; | |
0a7de745 | 4989 | |
9bccf70c | 4990 | p = mtod(m, struct sadb_msg *); |
0a7de745 | 4991 | |
1c79356b A |
4992 | bzero(p, len); |
4993 | p->sadb_msg_version = PF_KEY_V2; | |
4994 | p->sadb_msg_type = type; | |
4995 | p->sadb_msg_errno = 0; | |
4996 | p->sadb_msg_satype = satype; | |
4997 | p->sadb_msg_len = PFKEY_UNIT64(tlen); | |
9bccf70c | 4998 | p->sadb_msg_reserved = reserved; |
1c79356b A |
4999 | p->sadb_msg_seq = seq; |
5000 | p->sadb_msg_pid = (u_int32_t)pid; | |
0a7de745 | 5001 | |
9bccf70c | 5002 | return m; |
1c79356b A |
5003 | } |
5004 | ||
5005 | /* | |
5006 | * copy secasvar data into sadb_address. | |
1c79356b | 5007 | */ |
9bccf70c | 5008 | static struct mbuf * |
6d2010ae | 5009 | key_setsadbsa( |
0a7de745 | 5010 | struct secasvar *sav) |
1c79356b | 5011 | { |
9bccf70c | 5012 | struct mbuf *m; |
1c79356b | 5013 | struct sadb_sa *p; |
f427ee49 | 5014 | u_int16_t len; |
0a7de745 | 5015 | |
9bccf70c A |
5016 | len = PFKEY_ALIGN8(sizeof(struct sadb_sa)); |
5017 | m = key_alloc_mbuf(len); | |
0a7de745 A |
5018 | if (!m || m->m_next) { /*XXX*/ |
5019 | if (m) { | |
9bccf70c | 5020 | m_freem(m); |
0a7de745 | 5021 | } |
9bccf70c A |
5022 | return NULL; |
5023 | } | |
0a7de745 | 5024 | |
9bccf70c | 5025 | p = mtod(m, struct sadb_sa *); |
0a7de745 | 5026 | |
1c79356b A |
5027 | bzero(p, len); |
5028 | p->sadb_sa_len = PFKEY_UNIT64(len); | |
5029 | p->sadb_sa_exttype = SADB_EXT_SA; | |
5030 | p->sadb_sa_spi = sav->spi; | |
cb323159 | 5031 | p->sadb_sa_replay = (sav->replay[0] != NULL ? sav->replay[0]->wsize : 0); |
1c79356b A |
5032 | p->sadb_sa_state = sav->state; |
5033 | p->sadb_sa_auth = sav->alg_auth; | |
5034 | p->sadb_sa_encrypt = sav->alg_enc; | |
5035 | p->sadb_sa_flags = sav->flags; | |
0a7de745 | 5036 | |
9bccf70c | 5037 | return m; |
1c79356b | 5038 | } |
1c79356b A |
5039 | |
5040 | /* | |
5041 | * set data into sadb_address. | |
1c79356b | 5042 | */ |
9bccf70c | 5043 | static struct mbuf * |
6d2010ae | 5044 | key_setsadbaddr( |
0a7de745 A |
5045 | u_int16_t exttype, |
5046 | struct sockaddr *saddr, | |
f427ee49 A |
5047 | size_t prefixlen, |
5048 | u_int8_t ul_proto) | |
1c79356b | 5049 | { |
9bccf70c | 5050 | struct mbuf *m; |
1c79356b | 5051 | struct sadb_address *p; |
f427ee49 | 5052 | u_int16_t len; |
0a7de745 | 5053 | |
9bccf70c | 5054 | len = PFKEY_ALIGN8(sizeof(struct sadb_address)) + |
0a7de745 | 5055 | PFKEY_ALIGN8(saddr->sa_len); |
9bccf70c | 5056 | m = key_alloc_mbuf(len); |
0a7de745 A |
5057 | if (!m || m->m_next) { /*XXX*/ |
5058 | if (m) { | |
9bccf70c | 5059 | m_freem(m); |
0a7de745 | 5060 | } |
9bccf70c A |
5061 | return NULL; |
5062 | } | |
0a7de745 | 5063 | |
9bccf70c | 5064 | p = mtod(m, struct sadb_address *); |
0a7de745 | 5065 | |
1c79356b A |
5066 | bzero(p, len); |
5067 | p->sadb_address_len = PFKEY_UNIT64(len); | |
5068 | p->sadb_address_exttype = exttype; | |
5069 | p->sadb_address_proto = ul_proto; | |
55e303ae A |
5070 | if (prefixlen == FULLMASK) { |
5071 | switch (saddr->sa_family) { | |
0a7de745 A |
5072 | case AF_INET: |
5073 | prefixlen = sizeof(struct in_addr) << 3; | |
5074 | break; | |
5075 | case AF_INET6: | |
5076 | prefixlen = sizeof(struct in6_addr) << 3; | |
5077 | break; | |
5078 | default: | |
5079 | ; /*XXX*/ | |
55e303ae A |
5080 | } |
5081 | } | |
f427ee49 A |
5082 | if (prefixlen >= UINT8_MAX) { |
5083 | ipseclog((LOG_ERR, "key_setsadbaddr: bad prefix length %zu", prefixlen)); | |
5084 | m_freem(m); | |
5085 | return NULL; | |
5086 | } | |
5087 | p->sadb_address_prefixlen = (u_int8_t)prefixlen; | |
1c79356b | 5088 | p->sadb_address_reserved = 0; |
0a7de745 | 5089 | |
9bccf70c | 5090 | bcopy(saddr, |
0a7de745 A |
5091 | mtod(m, caddr_t) + PFKEY_ALIGN8(sizeof(struct sadb_address)), |
5092 | saddr->sa_len); | |
5093 | ||
39236c6e A |
5094 | return m; |
5095 | } | |
1c79356b | 5096 | |
39236c6e A |
5097 | static struct mbuf * |
5098 | key_setsadbipsecif(ifnet_t internal_if, | |
0a7de745 A |
5099 | ifnet_t outgoing_if, |
5100 | ifnet_t ipsec_if, | |
f427ee49 | 5101 | u_int8_t init_disabled) |
39236c6e | 5102 | { |
0a7de745 A |
5103 | struct mbuf *m; |
5104 | struct sadb_x_ipsecif *p; | |
f427ee49 | 5105 | u_int16_t len; |
0a7de745 | 5106 | |
39236c6e | 5107 | len = PFKEY_ALIGN8(sizeof(struct sadb_x_ipsecif)); |
0a7de745 A |
5108 | m = key_alloc_mbuf(len); |
5109 | if (!m || m->m_next) { /*XXX*/ | |
5110 | if (m) { | |
39236c6e | 5111 | m_freem(m); |
0a7de745 | 5112 | } |
39236c6e A |
5113 | return NULL; |
5114 | } | |
0a7de745 A |
5115 | |
5116 | p = mtod(m, struct sadb_x_ipsecif *); | |
5117 | ||
39236c6e | 5118 | bzero(p, len); |
0a7de745 | 5119 | p->sadb_x_ipsecif_len = PFKEY_UNIT64(len); |
39236c6e | 5120 | p->sadb_x_ipsecif_exttype = SADB_X_EXT_IPSECIF; |
0a7de745 A |
5121 | |
5122 | if (internal_if && internal_if->if_xname) { | |
5123 | strlcpy(p->sadb_x_ipsecif_internal_if, internal_if->if_xname, IFXNAMSIZ); | |
5124 | } | |
5125 | if (outgoing_if && outgoing_if->if_xname) { | |
5126 | strlcpy(p->sadb_x_ipsecif_outgoing_if, outgoing_if->if_xname, IFXNAMSIZ); | |
5127 | } | |
5128 | if (ipsec_if && ipsec_if->if_xname) { | |
5129 | strlcpy(p->sadb_x_ipsecif_ipsec_if, ipsec_if->if_xname, IFXNAMSIZ); | |
5130 | } | |
5131 | ||
39236c6e | 5132 | p->sadb_x_ipsecif_init_disabled = init_disabled; |
0a7de745 | 5133 | |
9bccf70c | 5134 | return m; |
1c79356b A |
5135 | } |
5136 | ||
b0d623f7 A |
5137 | /* |
5138 | * set data into sadb_session_id | |
5139 | */ | |
5140 | static struct mbuf * | |
0a7de745 | 5141 | key_setsadbsession_id(u_int64_t session_ids[]) |
b0d623f7 A |
5142 | { |
5143 | struct mbuf *m; | |
5144 | struct sadb_session_id *p; | |
f427ee49 | 5145 | u_int16_t len; |
0a7de745 | 5146 | |
b0d623f7 A |
5147 | len = PFKEY_ALIGN8(sizeof(*p)); |
5148 | m = key_alloc_mbuf(len); | |
0a7de745 A |
5149 | if (!m || m->m_next) { /*XXX*/ |
5150 | if (m) { | |
b0d623f7 | 5151 | m_freem(m); |
0a7de745 | 5152 | } |
b0d623f7 A |
5153 | return NULL; |
5154 | } | |
0a7de745 | 5155 | |
b0d623f7 | 5156 | p = mtod(m, __typeof__(p)); |
0a7de745 | 5157 | |
b0d623f7 A |
5158 | bzero(p, len); |
5159 | p->sadb_session_id_len = PFKEY_UNIT64(len); | |
5160 | p->sadb_session_id_exttype = SADB_EXT_SESSION_ID; | |
5161 | p->sadb_session_id_v[0] = session_ids[0]; | |
5162 | p->sadb_session_id_v[1] = session_ids[1]; | |
0a7de745 | 5163 | |
b0d623f7 A |
5164 | return m; |
5165 | } | |
5166 | ||
5167 | /* | |
5168 | * copy stats data into sadb_sastat type. | |
5169 | */ | |
5170 | static struct mbuf * | |
0a7de745 A |
5171 | key_setsadbsastat(u_int32_t dir, |
5172 | struct sastat *stats, | |
5173 | u_int32_t max_stats) | |
b0d623f7 | 5174 | { |
f427ee49 | 5175 | struct mbuf *m; |
b0d623f7 | 5176 | struct sadb_sastat *p; |
f427ee49 | 5177 | size_t list_len, len; |
0a7de745 | 5178 | |
b0d623f7 | 5179 | if (!stats) { |
39236c6e | 5180 | return NULL; |
b0d623f7 | 5181 | } |
0a7de745 | 5182 | |
b0d623f7 A |
5183 | list_len = sizeof(*stats) * max_stats; |
5184 | len = PFKEY_ALIGN8(sizeof(*p)) + PFKEY_ALIGN8(list_len); | |
f427ee49 A |
5185 | if (PFKEY_UNIT64(len) >= UINT16_MAX) { |
5186 | ipseclog((LOG_ERR, "key_setsadbsastat: length is too big: %zu\n", len)); | |
b0d623f7 A |
5187 | return NULL; |
5188 | } | |
0a7de745 | 5189 | |
f427ee49 | 5190 | m = key_alloc_mbuf((int)len); |
0a7de745 A |
5191 | if (!m || m->m_next) { /*XXX*/ |
5192 | if (m) { | |
9bccf70c | 5193 | m_freem(m); |
0a7de745 | 5194 | } |
9bccf70c A |
5195 | return NULL; |
5196 | } | |
0a7de745 | 5197 | |
f427ee49 | 5198 | p = mtod(m, __typeof__(p)); |
0a7de745 | 5199 | |
1c79356b | 5200 | bzero(p, len); |
f427ee49 A |
5201 | p->sadb_sastat_len = (u_int16_t)PFKEY_UNIT64(len); |
5202 | p->sadb_sastat_exttype = SADB_EXT_SASTAT; | |
5203 | p->sadb_sastat_dir = dir; | |
5204 | p->sadb_sastat_list_len = max_stats; | |
5205 | if (list_len) { | |
5206 | bcopy(stats, | |
5207 | mtod(m, caddr_t) + PFKEY_ALIGN8(sizeof(*p)), | |
5208 | list_len); | |
5209 | } | |
0a7de745 | 5210 | |
9bccf70c A |
5211 | return m; |
5212 | } | |
9bccf70c A |
5213 | |
5214 | /* | |
5215 | * set data into sadb_x_sa2. | |
5216 | */ | |
5217 | static struct mbuf * | |
6d2010ae | 5218 | key_setsadbxsa2( |
0a7de745 A |
5219 | u_int8_t mode, |
5220 | u_int32_t seq, | |
5221 | u_int32_t reqid, | |
5222 | u_int16_t flags) | |
9bccf70c A |
5223 | { |
5224 | struct mbuf *m; | |
5225 | struct sadb_x_sa2 *p; | |
f427ee49 | 5226 | u_int16_t len; |
0a7de745 | 5227 | |
9bccf70c A |
5228 | len = PFKEY_ALIGN8(sizeof(struct sadb_x_sa2)); |
5229 | m = key_alloc_mbuf(len); | |
0a7de745 A |
5230 | if (!m || m->m_next) { /*XXX*/ |
5231 | if (m) { | |
9bccf70c | 5232 | m_freem(m); |
0a7de745 | 5233 | } |
9bccf70c A |
5234 | return NULL; |
5235 | } | |
0a7de745 | 5236 | |
9bccf70c | 5237 | p = mtod(m, struct sadb_x_sa2 *); |
0a7de745 | 5238 | |
9bccf70c A |
5239 | bzero(p, len); |
5240 | p->sadb_x_sa2_len = PFKEY_UNIT64(len); | |
5241 | p->sadb_x_sa2_exttype = SADB_X_EXT_SA2; | |
5242 | p->sadb_x_sa2_mode = mode; | |
5243 | p->sadb_x_sa2_reserved1 = 0; | |
5244 | p->sadb_x_sa2_reserved2 = 0; | |
55e303ae | 5245 | p->sadb_x_sa2_sequence = seq; |
9bccf70c | 5246 | p->sadb_x_sa2_reqid = reqid; |
fe8ab488 | 5247 | p->sadb_x_sa2_flags = flags; |
0a7de745 | 5248 | |
9bccf70c | 5249 | return m; |
1c79356b A |
5250 | } |
5251 | ||
5252 | /* | |
5253 | * set data into sadb_x_policy | |
1c79356b | 5254 | */ |
9bccf70c | 5255 | static struct mbuf * |
6d2010ae | 5256 | key_setsadbxpolicy( |
0a7de745 A |
5257 | u_int16_t type, |
5258 | u_int8_t dir, | |
5259 | u_int32_t id) | |
1c79356b | 5260 | { |
9bccf70c | 5261 | struct mbuf *m; |
1c79356b | 5262 | struct sadb_x_policy *p; |
f427ee49 | 5263 | u_int16_t len; |
0a7de745 | 5264 | |
9bccf70c A |
5265 | len = PFKEY_ALIGN8(sizeof(struct sadb_x_policy)); |
5266 | m = key_alloc_mbuf(len); | |
0a7de745 A |
5267 | if (!m || m->m_next) { /*XXX*/ |
5268 | if (m) { | |
9bccf70c | 5269 | m_freem(m); |
0a7de745 | 5270 | } |
9bccf70c A |
5271 | return NULL; |
5272 | } | |
0a7de745 | 5273 | |
9bccf70c | 5274 | p = mtod(m, struct sadb_x_policy *); |
0a7de745 | 5275 | |
1c79356b A |
5276 | bzero(p, len); |
5277 | p->sadb_x_policy_len = PFKEY_UNIT64(len); | |
5278 | p->sadb_x_policy_exttype = SADB_X_EXT_POLICY; | |
5279 | p->sadb_x_policy_type = type; | |
5280 | p->sadb_x_policy_dir = dir; | |
5281 | p->sadb_x_policy_id = id; | |
0a7de745 | 5282 | |
9bccf70c | 5283 | return m; |
1c79356b A |
5284 | } |
5285 | ||
5286 | /* %%% utilities */ | |
5287 | /* | |
5288 | * copy a buffer into the new buffer allocated. | |
5289 | */ | |
5290 | static void * | |
6d2010ae | 5291 | key_newbuf( |
0a7de745 A |
5292 | const void *src, |
5293 | u_int len) | |
1c79356b A |
5294 | { |
5295 | caddr_t new; | |
0a7de745 | 5296 | |
5ba3f43e | 5297 | LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_OWNED); |
2d21ac55 | 5298 | KMALLOC_NOWAIT(new, caddr_t, len); |
1c79356b | 5299 | if (new == NULL) { |
2d21ac55 A |
5300 | lck_mtx_unlock(sadb_mutex); |
5301 | KMALLOC_WAIT(new, caddr_t, len); | |
5302 | lck_mtx_lock(sadb_mutex); | |
5303 | if (new == NULL) { | |
5304 | ipseclog((LOG_DEBUG, "key_newbuf: No more memory.\n")); | |
5305 | return NULL; | |
5306 | } | |
1c79356b | 5307 | } |
9bccf70c | 5308 | bcopy(src, new, len); |
0a7de745 | 5309 | |
1c79356b A |
5310 | return new; |
5311 | } | |
5312 | ||
5313 | /* compare my own address | |
5314 | * OUT: 1: true, i.e. my address. | |
5315 | * 0: false | |
5316 | */ | |
5317 | int | |
6d2010ae | 5318 | key_ismyaddr( |
0a7de745 | 5319 | struct sockaddr *sa) |
1c79356b | 5320 | { |
9bccf70c A |
5321 | #if INET |
5322 | struct sockaddr_in *sin; | |
5323 | struct in_ifaddr *ia; | |
5324 | #endif | |
0a7de745 | 5325 | |
1c79356b | 5326 | /* sanity check */ |
0a7de745 | 5327 | if (sa == NULL) { |
1c79356b | 5328 | panic("key_ismyaddr: NULL pointer is passed.\n"); |
0a7de745 A |
5329 | } |
5330 | ||
9bccf70c A |
5331 | switch (sa->sa_family) { |
5332 | #if INET | |
0a7de745 A |
5333 | case AF_INET: |
5334 | lck_rw_lock_shared(in_ifaddr_rwlock); | |
5335 | sin = (struct sockaddr_in *)(void *)sa; | |
5336 | for (ia = in_ifaddrhead.tqh_first; ia; | |
5337 | ia = ia->ia_link.tqe_next) { | |
5338 | IFA_LOCK_SPIN(&ia->ia_ifa); | |
5339 | if (sin->sin_family == ia->ia_addr.sin_family && | |
5340 | sin->sin_len == ia->ia_addr.sin_len && | |
5341 | sin->sin_addr.s_addr == ia->ia_addr.sin_addr.s_addr) { | |
6d2010ae | 5342 | IFA_UNLOCK(&ia->ia_ifa); |
0a7de745 A |
5343 | lck_rw_done(in_ifaddr_rwlock); |
5344 | return 1; | |
9bccf70c | 5345 | } |
0a7de745 A |
5346 | IFA_UNLOCK(&ia->ia_ifa); |
5347 | } | |
5348 | lck_rw_done(in_ifaddr_rwlock); | |
5349 | break; | |
9bccf70c | 5350 | #endif |
0a7de745 A |
5351 | case AF_INET6: |
5352 | return key_ismyaddr6((struct sockaddr_in6 *)(void *)sa); | |
1c79356b | 5353 | } |
0a7de745 | 5354 | |
1c79356b A |
5355 | return 0; |
5356 | } | |
5357 | ||
1c79356b A |
5358 | /* |
5359 | * compare my own address for IPv6. | |
5360 | * 1: ours | |
5361 | * 0: other | |
5362 | * NOTE: derived ip6_input() in KAME. This is necessary to modify more. | |
5363 | */ | |
5364 | #include <netinet6/in6_var.h> | |
5365 | ||
5366 | static int | |
6d2010ae | 5367 | key_ismyaddr6( |
0a7de745 | 5368 | struct sockaddr_in6 *sin6) |
1c79356b | 5369 | { |
1c79356b | 5370 | struct in6_ifaddr *ia; |
9bccf70c | 5371 | struct in6_multi *in6m; |
0a7de745 | 5372 | |
6d2010ae | 5373 | lck_rw_lock_shared(&in6_ifaddr_rwlock); |
eb6b6ca3 | 5374 | TAILQ_FOREACH(ia, &in6_ifaddrhead, ia6_link) { |
6d2010ae | 5375 | IFA_LOCK(&ia->ia_ifa); |
9bccf70c | 5376 | if (key_sockaddrcmp((struct sockaddr *)&sin6, |
0a7de745 | 5377 | (struct sockaddr *)&ia->ia_addr, 0) == 0) { |
6d2010ae A |
5378 | IFA_UNLOCK(&ia->ia_ifa); |
5379 | lck_rw_done(&in6_ifaddr_rwlock); | |
1c79356b | 5380 | return 1; |
91447636 | 5381 | } |
6d2010ae | 5382 | IFA_UNLOCK(&ia->ia_ifa); |
0a7de745 | 5383 | |
9bccf70c A |
5384 | /* |
5385 | * XXX Multicast | |
5386 | * XXX why do we care about multlicast here while we don't care | |
5387 | * about IPv4 multicast?? | |
5388 | * XXX scope | |
5389 | */ | |
5390 | in6m = NULL; | |
6d2010ae A |
5391 | in6_multihead_lock_shared(); |
5392 | IN6_LOOKUP_MULTI(&sin6->sin6_addr, ia->ia_ifp, in6m); | |
5393 | in6_multihead_lock_done(); | |
5394 | if (in6m != NULL) { | |
5395 | lck_rw_done(&in6_ifaddr_rwlock); | |
5396 | IN6M_REMREF(in6m); | |
1c79356b | 5397 | return 1; |
91447636 | 5398 | } |
1c79356b | 5399 | } |
6d2010ae | 5400 | lck_rw_done(&in6_ifaddr_rwlock); |
0a7de745 | 5401 | |
1c79356b | 5402 | /* loopback, just for safety */ |
0a7de745 | 5403 | if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr)) { |
1c79356b | 5404 | return 1; |
0a7de745 A |
5405 | } |
5406 | ||
1c79356b A |
5407 | return 0; |
5408 | } | |
1c79356b | 5409 | |
1c79356b | 5410 | /* |
55e303ae A |
5411 | * compare two secasindex structure. |
5412 | * flag can specify to compare 2 saidxes. | |
5413 | * compare two secasindex structure without both mode and reqid. | |
9bccf70c | 5414 | * don't compare port. |
39236c6e | 5415 | * IN: |
55e303ae A |
5416 | * saidx0: source, it can be in SAD. |
5417 | * saidx1: object. | |
39236c6e | 5418 | * OUT: |
55e303ae A |
5419 | * 1 : equal |
5420 | * 0 : not equal | |
1c79356b A |
5421 | */ |
5422 | static int | |
6d2010ae | 5423 | key_cmpsaidx( |
0a7de745 A |
5424 | struct secasindex *saidx0, |
5425 | struct secasindex *saidx1, | |
5426 | int flag) | |
1c79356b A |
5427 | { |
5428 | /* sanity */ | |
0a7de745 | 5429 | if (saidx0 == NULL && saidx1 == NULL) { |
1c79356b | 5430 | return 1; |
0a7de745 A |
5431 | } |
5432 | ||
5433 | if (saidx0 == NULL || saidx1 == NULL) { | |
1c79356b | 5434 | return 0; |
0a7de745 A |
5435 | } |
5436 | ||
5437 | if (saidx0->ipsec_ifindex != 0 && saidx0->ipsec_ifindex != saidx1->ipsec_ifindex) { | |
fe8ab488 | 5438 | return 0; |
0a7de745 A |
5439 | } |
5440 | ||
5441 | if (saidx0->proto != saidx1->proto) { | |
9bccf70c | 5442 | return 0; |
0a7de745 A |
5443 | } |
5444 | ||
55e303ae | 5445 | if (flag == CMP_EXACTLY) { |
0a7de745 | 5446 | if (saidx0->mode != saidx1->mode) { |
55e303ae | 5447 | return 0; |
0a7de745 A |
5448 | } |
5449 | if (saidx0->reqid != saidx1->reqid) { | |
55e303ae | 5450 | return 0; |
0a7de745 | 5451 | } |
55e303ae | 5452 | if (bcmp(&saidx0->src, &saidx1->src, saidx0->src.ss_len) != 0 || |
0a7de745 | 5453 | bcmp(&saidx0->dst, &saidx1->dst, saidx0->dst.ss_len) != 0) { |
55e303ae | 5454 | return 0; |
0a7de745 | 5455 | } |
55e303ae | 5456 | } else { |
55e303ae | 5457 | /* CMP_MODE_REQID, CMP_REQID, CMP_HEAD */ |
2d21ac55 | 5458 | if (flag & CMP_REQID) { |
55e303ae A |
5459 | /* |
5460 | * If reqid of SPD is non-zero, unique SA is required. | |
5461 | * The result must be of same reqid in this case. | |
5462 | */ | |
0a7de745 | 5463 | if (saidx1->reqid != 0 && saidx0->reqid != saidx1->reqid) { |
55e303ae | 5464 | return 0; |
0a7de745 | 5465 | } |
55e303ae | 5466 | } |
0a7de745 | 5467 | |
2d21ac55 | 5468 | if (flag & CMP_MODE) { |
55e303ae | 5469 | if (saidx0->mode != IPSEC_MODE_ANY |
0a7de745 | 5470 | && saidx0->mode != saidx1->mode) { |
55e303ae | 5471 | return 0; |
0a7de745 | 5472 | } |
55e303ae | 5473 | } |
0a7de745 | 5474 | |
55e303ae | 5475 | if (key_sockaddrcmp((struct sockaddr *)&saidx0->src, |
0a7de745 | 5476 | (struct sockaddr *)&saidx1->src, flag & CMP_PORT ? 1 : 0) != 0) { |
55e303ae A |
5477 | return 0; |
5478 | } | |
5479 | if (key_sockaddrcmp((struct sockaddr *)&saidx0->dst, | |
0a7de745 | 5480 | (struct sockaddr *)&saidx1->dst, flag & CMP_PORT ? 1 : 0) != 0) { |
55e303ae A |
5481 | return 0; |
5482 | } | |
9bccf70c | 5483 | } |
0a7de745 A |
5484 | |
5485 | return 1; | |
5486 | } | |
5487 | ||
5488 | /* | |
5489 | * compare two secindex structure exactly. | |
5490 | * IN: | |
5491 | * spidx0: source, it is often in SPD. | |
5492 | * spidx1: object, it is often from PFKEY message. | |
5493 | * OUT: | |
5494 | * 1 : equal | |
5495 | * 0 : not equal | |
5496 | */ | |
5497 | static int | |
5498 | key_cmpspidx_exactly( | |
5499 | struct secpolicyindex *spidx0, | |
5500 | struct secpolicyindex *spidx1) | |
5501 | { | |
5502 | /* sanity */ | |
5503 | if (spidx0 == NULL && spidx1 == NULL) { | |
5504 | return 1; | |
5505 | } | |
5506 | ||
5507 | if (spidx0 == NULL || spidx1 == NULL) { | |
5508 | return 0; | |
5509 | } | |
5510 | ||
5511 | if (spidx0->prefs != spidx1->prefs | |
5512 | || spidx0->prefd != spidx1->prefd | |
5513 | || spidx0->ul_proto != spidx1->ul_proto | |
5514 | || spidx0->internal_if != spidx1->internal_if) { | |
5515 | return 0; | |
5516 | } | |
5517 | ||
5518 | if (key_sockaddrcmp((struct sockaddr *)&spidx0->src, | |
5519 | (struct sockaddr *)&spidx1->src, 1) != 0) { | |
5520 | return 0; | |
5521 | } | |
5522 | if (key_sockaddrcmp((struct sockaddr *)&spidx0->dst, | |
5523 | (struct sockaddr *)&spidx1->dst, 1) != 0) { | |
5524 | return 0; | |
5525 | } | |
5526 | ||
5527 | if (key_sockaddrcmp((struct sockaddr *)&spidx0->src_range.start, | |
5528 | (struct sockaddr *)&spidx1->src_range.start, 1) != 0) { | |
5529 | return 0; | |
5530 | } | |
5531 | if (key_sockaddrcmp((struct sockaddr *)&spidx0->src_range.end, | |
5532 | (struct sockaddr *)&spidx1->src_range.end, 1) != 0) { | |
5533 | return 0; | |
5534 | } | |
5535 | if (key_sockaddrcmp((struct sockaddr *)&spidx0->dst_range.start, | |
5536 | (struct sockaddr *)&spidx1->dst_range.start, 1) != 0) { | |
5537 | return 0; | |
5538 | } | |
5539 | if (key_sockaddrcmp((struct sockaddr *)&spidx0->dst_range.end, | |
5540 | (struct sockaddr *)&spidx1->dst_range.end, 1) != 0) { | |
5541 | return 0; | |
5542 | } | |
5543 | ||
5544 | return 1; | |
5545 | } | |
5546 | ||
5547 | /* | |
5548 | * compare two secindex structure with mask. | |
5549 | * IN: | |
5550 | * spidx0: source, it is often in SPD. | |
5551 | * spidx1: object, it is often from IP header. | |
5552 | * OUT: | |
5553 | * 1 : equal | |
5554 | * 0 : not equal | |
5555 | */ | |
5556 | static int | |
5557 | key_cmpspidx_withmask( | |
5558 | struct secpolicyindex *spidx0, | |
5559 | struct secpolicyindex *spidx1) | |
5560 | { | |
5561 | int spidx0_src_is_range = 0; | |
5562 | int spidx0_dst_is_range = 0; | |
5563 | ||
5564 | /* sanity */ | |
5565 | if (spidx0 == NULL && spidx1 == NULL) { | |
5566 | return 1; | |
5567 | } | |
5568 | ||
5569 | if (spidx0 == NULL || spidx1 == NULL) { | |
5570 | return 0; | |
5571 | } | |
5572 | ||
5573 | if (spidx0->src_range.start.ss_len > 0) { | |
5574 | spidx0_src_is_range = 1; | |
5575 | } | |
5576 | ||
5577 | if (spidx0->dst_range.start.ss_len > 0) { | |
5578 | spidx0_dst_is_range = 1; | |
5579 | } | |
5580 | ||
5581 | if ((spidx0_src_is_range ? spidx0->src_range.start.ss_family : spidx0->src.ss_family) != spidx1->src.ss_family || | |
5582 | (spidx0_dst_is_range ? spidx0->dst_range.start.ss_family : spidx0->dst.ss_family) != spidx1->dst.ss_family || | |
5583 | (spidx0_src_is_range ? spidx0->src_range.start.ss_len : spidx0->src.ss_len) != spidx1->src.ss_len || | |
5584 | (spidx0_dst_is_range ? spidx0->dst_range.start.ss_len : spidx0->dst.ss_len) != spidx1->dst.ss_len) { | |
5585 | return 0; | |
5586 | } | |
5587 | ||
5588 | /* if spidx.ul_proto == IPSEC_ULPROTO_ANY, ignore. */ | |
5589 | if (spidx0->ul_proto != (u_int16_t)IPSEC_ULPROTO_ANY | |
5590 | && spidx0->ul_proto != spidx1->ul_proto) { | |
5591 | return 0; | |
5592 | } | |
5593 | ||
5594 | /* If spidx1 specifies interface, ignore src addr */ | |
5595 | if (spidx1->internal_if != NULL) { | |
5596 | if (spidx0->internal_if == NULL | |
5597 | || spidx0->internal_if != spidx1->internal_if) { | |
5598 | return 0; | |
5599 | } | |
5600 | ||
5601 | /* Still check ports */ | |
5602 | switch (spidx0->src.ss_family) { | |
5603 | case AF_INET: | |
5604 | if (spidx0_src_is_range && | |
5605 | (satosin(&spidx1->src)->sin_port < satosin(&spidx0->src_range.start)->sin_port | |
5606 | || satosin(&spidx1->src)->sin_port > satosin(&spidx0->src_range.end)->sin_port)) { | |
5607 | return 0; | |
5608 | } else if (satosin(&spidx0->src)->sin_port != IPSEC_PORT_ANY | |
5609 | && satosin(&spidx0->src)->sin_port != | |
5610 | satosin(&spidx1->src)->sin_port) { | |
5611 | return 0; | |
5612 | } | |
5613 | break; | |
5614 | case AF_INET6: | |
5615 | if (spidx0_src_is_range && | |
5616 | (satosin6(&spidx1->src)->sin6_port < satosin6(&spidx0->src_range.start)->sin6_port | |
5617 | || satosin6(&spidx1->src)->sin6_port > satosin6(&spidx0->src_range.end)->sin6_port)) { | |
5618 | return 0; | |
5619 | } else if (satosin6(&spidx0->src)->sin6_port != IPSEC_PORT_ANY | |
5620 | && satosin6(&spidx0->src)->sin6_port != | |
5621 | satosin6(&spidx1->src)->sin6_port) { | |
5622 | return 0; | |
5623 | } | |
5624 | break; | |
5625 | default: | |
5626 | break; | |
5627 | } | |
5628 | } else if (spidx0_src_is_range) { | |
5629 | if (!key_is_addr_in_range(&spidx1->src, &spidx0->src_range)) { | |
5630 | return 0; | |
5631 | } | |
5632 | } else { | |
5633 | switch (spidx0->src.ss_family) { | |
5634 | case AF_INET: | |
5635 | if (satosin(&spidx0->src)->sin_port != IPSEC_PORT_ANY | |
5636 | && satosin(&spidx0->src)->sin_port != | |
5637 | satosin(&spidx1->src)->sin_port) { | |
5638 | return 0; | |
5639 | } | |
5640 | if (!key_bbcmp((caddr_t)&satosin(&spidx0->src)->sin_addr, | |
5641 | (caddr_t)&satosin(&spidx1->src)->sin_addr, spidx0->prefs)) { | |
5642 | return 0; | |
5643 | } | |
5644 | break; | |
5645 | case AF_INET6: | |
5646 | if (satosin6(&spidx0->src)->sin6_port != IPSEC_PORT_ANY | |
5647 | && satosin6(&spidx0->src)->sin6_port != | |
5648 | satosin6(&spidx1->src)->sin6_port) { | |
5649 | return 0; | |
5650 | } | |
5651 | /* | |
5652 | * scope_id check. if sin6_scope_id is 0, we regard it | |
5653 | * as a wildcard scope, which matches any scope zone ID. | |
5654 | */ | |
5655 | if (satosin6(&spidx0->src)->sin6_scope_id && | |
5656 | satosin6(&spidx1->src)->sin6_scope_id && | |
5657 | satosin6(&spidx0->src)->sin6_scope_id != | |
5658 | satosin6(&spidx1->src)->sin6_scope_id) { | |
5659 | return 0; | |
5660 | } | |
5661 | if (!key_bbcmp((caddr_t)&satosin6(&spidx0->src)->sin6_addr, | |
5662 | (caddr_t)&satosin6(&spidx1->src)->sin6_addr, spidx0->prefs)) { | |
5663 | return 0; | |
5664 | } | |
5665 | break; | |
5666 | default: | |
5667 | /* XXX */ | |
5668 | if (bcmp(&spidx0->src, &spidx1->src, spidx0->src.ss_len) != 0) { | |
5669 | return 0; | |
5670 | } | |
5671 | break; | |
5672 | } | |
5673 | } | |
5674 | ||
5675 | if (spidx0_dst_is_range) { | |
5676 | if (!key_is_addr_in_range(&spidx1->dst, &spidx0->dst_range)) { | |
5677 | return 0; | |
5678 | } | |
5679 | } else { | |
5680 | switch (spidx0->dst.ss_family) { | |
5681 | case AF_INET: | |
5682 | if (satosin(&spidx0->dst)->sin_port != IPSEC_PORT_ANY | |
5683 | && satosin(&spidx0->dst)->sin_port != | |
5684 | satosin(&spidx1->dst)->sin_port) { | |
5685 | return 0; | |
5686 | } | |
5687 | if (!key_bbcmp((caddr_t)&satosin(&spidx0->dst)->sin_addr, | |
5688 | (caddr_t)&satosin(&spidx1->dst)->sin_addr, spidx0->prefd)) { | |
5689 | return 0; | |
5690 | } | |
5691 | break; | |
5692 | case AF_INET6: | |
5693 | if (satosin6(&spidx0->dst)->sin6_port != IPSEC_PORT_ANY | |
5694 | && satosin6(&spidx0->dst)->sin6_port != | |
5695 | satosin6(&spidx1->dst)->sin6_port) { | |
5696 | return 0; | |
5697 | } | |
5698 | /* | |
5699 | * scope_id check. if sin6_scope_id is 0, we regard it | |
5700 | * as a wildcard scope, which matches any scope zone ID. | |
5701 | */ | |
5702 | if (satosin6(&spidx0->src)->sin6_scope_id && | |
5703 | satosin6(&spidx1->src)->sin6_scope_id && | |
5704 | satosin6(&spidx0->dst)->sin6_scope_id != | |
5705 | satosin6(&spidx1->dst)->sin6_scope_id) { | |
5706 | return 0; | |
5707 | } | |
5708 | if (!key_bbcmp((caddr_t)&satosin6(&spidx0->dst)->sin6_addr, | |
5709 | (caddr_t)&satosin6(&spidx1->dst)->sin6_addr, spidx0->prefd)) { | |
5710 | return 0; | |
5711 | } | |
5712 | break; | |
5713 | default: | |
5714 | /* XXX */ | |
5715 | if (bcmp(&spidx0->dst, &spidx1->dst, spidx0->dst.ss_len) != 0) { | |
5716 | return 0; | |
5717 | } | |
5718 | break; | |
5719 | } | |
5720 | } | |
5721 | ||
5722 | /* XXX Do we check other field ? e.g. flowinfo */ | |
5723 | ||
9bccf70c A |
5724 | return 1; |
5725 | } | |
5726 | ||
9bccf70c | 5727 | static int |
0a7de745 | 5728 | key_is_addr_in_range(struct sockaddr_storage *addr, struct secpolicyaddrrange *addr_range) |
9bccf70c | 5729 | { |
0a7de745 A |
5730 | int cmp = 0; |
5731 | ||
5732 | if (addr == NULL || addr_range == NULL) { | |
39236c6e A |
5733 | return 0; |
5734 | } | |
0a7de745 A |
5735 | |
5736 | /* Must be greater than or equal to start */ | |
5737 | cmp = key_sockaddrcmp((struct sockaddr *)addr, (struct sockaddr *)&addr_range->start, 1); | |
5738 | if (cmp != 0 && cmp != 1) { | |
39236c6e A |
5739 | return 0; |
5740 | } | |
0a7de745 A |
5741 | |
5742 | /* Must be less than or equal to end */ | |
5743 | cmp = key_sockaddrcmp((struct sockaddr *)addr, (struct sockaddr *)&addr_range->end, 1); | |
5744 | if (cmp != 0 && cmp != -1) { | |
39236c6e A |
5745 | return 0; |
5746 | } | |
9bccf70c | 5747 | |
1c79356b A |
5748 | return 1; |
5749 | } | |
5750 | ||
39236c6e | 5751 | /* |
0a7de745 A |
5752 | * Return values: |
5753 | * -1: sa1 < sa2 | |
5754 | * 0: sa1 == sa2 | |
5755 | * 1: sa1 > sa2 | |
5756 | * 2: Not comparable or error | |
39236c6e | 5757 | */ |
9bccf70c | 5758 | static int |
6d2010ae | 5759 | key_sockaddrcmp( |
0a7de745 A |
5760 | struct sockaddr *sa1, |
5761 | struct sockaddr *sa2, | |
5762 | int port) | |
5763 | { | |
5764 | int result = 0; | |
5765 | int port_result = 0; | |
5766 | ||
5767 | if (sa1->sa_family != sa2->sa_family || sa1->sa_len != sa2->sa_len) { | |
39236c6e | 5768 | return 2; |
0a7de745 A |
5769 | } |
5770 | ||
5771 | if (sa1->sa_len == 0) { | |
5772 | return 0; | |
5773 | } | |
5774 | ||
9bccf70c | 5775 | switch (sa1->sa_family) { |
0a7de745 A |
5776 | case AF_INET: |
5777 | if (sa1->sa_len != sizeof(struct sockaddr_in)) { | |
5778 | return 2; | |
5779 | } | |
5780 | ||
5781 | result = memcmp(&satosin(sa1)->sin_addr.s_addr, &satosin(sa2)->sin_addr.s_addr, sizeof(satosin(sa1)->sin_addr.s_addr)); | |
5782 | ||
5783 | if (port) { | |
5784 | if (satosin(sa1)->sin_port < satosin(sa2)->sin_port) { | |
5785 | port_result = -1; | |
5786 | } else if (satosin(sa1)->sin_port > satosin(sa2)->sin_port) { | |
5787 | port_result = 1; | |
5788 | } | |
5789 | ||
5790 | if (result == 0) { | |
5791 | result = port_result; | |
5792 | } else if ((result > 0 && port_result < 0) || (result < 0 && port_result > 0)) { | |
5793 | return 2; | |
5794 | } | |
5795 | } | |
5796 | ||
5797 | break; | |
5798 | case AF_INET6: | |
5799 | if (sa1->sa_len != sizeof(struct sockaddr_in6)) { | |
5800 | return 2; /*EINVAL*/ | |
5801 | } | |
5802 | if (satosin6(sa1)->sin6_scope_id != | |
5803 | satosin6(sa2)->sin6_scope_id) { | |
5804 | return 2; | |
5805 | } | |
5806 | ||
5807 | result = memcmp(&satosin6(sa1)->sin6_addr.s6_addr[0], &satosin6(sa2)->sin6_addr.s6_addr[0], sizeof(struct in6_addr)); | |
5808 | ||
5809 | if (port) { | |
5810 | if (satosin6(sa1)->sin6_port < satosin6(sa2)->sin6_port) { | |
5811 | port_result = -1; | |
5812 | } else if (satosin6(sa1)->sin6_port > satosin6(sa2)->sin6_port) { | |
5813 | port_result = 1; | |
5814 | } | |
5815 | ||
5816 | if (result == 0) { | |
5817 | result = port_result; | |
5818 | } else if ((result > 0 && port_result < 0) || (result < 0 && port_result > 0)) { | |
5819 | return 2; | |
5820 | } | |
5821 | } | |
5822 | ||
5823 | break; | |
5824 | default: | |
5825 | result = memcmp(sa1, sa2, sa1->sa_len); | |
5826 | break; | |
5827 | } | |
5828 | ||
5829 | if (result < 0) { | |
5830 | result = -1; | |
5831 | } else if (result > 0) { | |
5832 | result = 1; | |
5833 | } | |
5834 | ||
39236c6e | 5835 | return result; |
9bccf70c A |
5836 | } |
5837 | ||
1c79356b A |
5838 | /* |
5839 | * compare two buffers with mask. | |
5840 | * IN: | |
5841 | * addr1: source | |
5842 | * addr2: object | |
5843 | * bits: Number of bits to compare | |
5844 | * OUT: | |
5845 | * 1 : equal | |
5846 | * 0 : not equal | |
5847 | */ | |
5848 | static int | |
6d2010ae | 5849 | key_bbcmp( |
0a7de745 A |
5850 | caddr_t p1, |
5851 | caddr_t p2, | |
5852 | u_int bits) | |
1c79356b A |
5853 | { |
5854 | u_int8_t mask; | |
0a7de745 | 5855 | |
1c79356b A |
5856 | /* XXX: This could be considerably faster if we compare a word |
5857 | * at a time, but it is complicated on LSB Endian machines */ | |
0a7de745 | 5858 | |
1c79356b | 5859 | /* Handle null pointers */ |
0a7de745 A |
5860 | if (p1 == NULL || p2 == NULL) { |
5861 | return p1 == p2; | |
5862 | } | |
5863 | ||
1c79356b | 5864 | while (bits >= 8) { |
0a7de745 | 5865 | if (*p1++ != *p2++) { |
1c79356b | 5866 | return 0; |
0a7de745 | 5867 | } |
1c79356b A |
5868 | bits -= 8; |
5869 | } | |
0a7de745 | 5870 | |
1c79356b | 5871 | if (bits > 0) { |
f427ee49 | 5872 | mask = (u_int8_t)(~((1 << (8 - bits)) - 1)); |
0a7de745 | 5873 | if ((*p1 & mask) != (*p2 & mask)) { |
1c79356b | 5874 | return 0; |
0a7de745 | 5875 | } |
1c79356b | 5876 | } |
0a7de745 | 5877 | return 1; /* Match! */ |
1c79356b A |
5878 | } |
5879 | ||
5880 | /* | |
5881 | * time handler. | |
5882 | * scanning SPD and SAD to check status for each entries, | |
5883 | * and do to remove or to expire. | |
9bccf70c | 5884 | * XXX: year 2038 problem may remain. |
1c79356b | 5885 | */ |
e2fac8b1 A |
5886 | int key_timehandler_debug = 0; |
5887 | u_int32_t spd_count = 0, sah_count = 0, dead_sah_count = 0, empty_sah_count = 0, larval_sav_count = 0, mature_sav_count = 0, dying_sav_count = 0, dead_sav_count = 0; | |
5888 | u_int64_t total_sav_count = 0; | |
1c79356b A |
5889 | void |
5890 | key_timehandler(void) | |
5891 | { | |
5892 | u_int dir; | |
9bccf70c | 5893 | struct timeval tv; |
2d21ac55 A |
5894 | struct secpolicy **spbuf = NULL, **spptr = NULL; |
5895 | struct secasvar **savexbuf = NULL, **savexptr = NULL; | |
5896 | struct secasvar **savkabuf = NULL, **savkaptr = NULL; | |
f427ee49 | 5897 | size_t total_req_size = 0; |
eb6b6ca3 | 5898 | u_int32_t spbufcount = 0, savbufcount = 0, spcount = 0, savexcount = 0, savkacount = 0, cnt; |
39236c6e | 5899 | int stop_handler = 1; /* stop the timehandler */ |
1c79356b | 5900 | |
39236c6e | 5901 | microtime(&tv); |
0a7de745 | 5902 | |
2d21ac55 A |
5903 | /* pre-allocate buffers before taking the lock */ |
5904 | /* if allocation failures occur - portions of the processing will be skipped */ | |
5905 | if ((spbufcount = ipsec_policy_count) != 0) { | |
eb6b6ca3 A |
5906 | if (os_add_overflow(spbufcount, 256, &spbufcount)) { |
5907 | ipseclog((LOG_DEBUG, "key_timehandler: spbufcount overflow, ipsec policy count %u.\n", ipsec_policy_count)); | |
5908 | spbufcount = ipsec_policy_count; | |
5909 | } | |
5910 | ||
5911 | if (os_mul_overflow(spbufcount, sizeof(struct secpolicy *), &total_req_size)) { | |
5912 | panic("key_timehandler spbuf requested memory overflow %u\n", spbufcount); | |
5913 | } | |
5914 | KMALLOC_WAIT(spbuf, struct secpolicy **, total_req_size); | |
0a7de745 | 5915 | if (spbuf) { |
2d21ac55 | 5916 | spptr = spbuf; |
0a7de745 | 5917 | } |
2d21ac55 A |
5918 | } |
5919 | if ((savbufcount = ipsec_sav_count) != 0) { | |
eb6b6ca3 A |
5920 | if (os_add_overflow(savbufcount, 512, &savbufcount)) { |
5921 | ipseclog((LOG_DEBUG, "key_timehandler: savbufcount overflow, ipsec sa count %u.\n", ipsec_sav_count)); | |
5922 | savbufcount = ipsec_sav_count; | |
5923 | } | |
5924 | if (os_mul_overflow(savbufcount, sizeof(struct secasvar *), &total_req_size)) { | |
5925 | panic("key_timehandler savexbuf requested memory overflow %u\n", savbufcount); | |
5926 | } | |
5927 | KMALLOC_WAIT(savexbuf, struct secasvar **, total_req_size); | |
0a7de745 | 5928 | if (savexbuf) { |
2d21ac55 | 5929 | savexptr = savexbuf; |
0a7de745 | 5930 | } |
eb6b6ca3 | 5931 | KMALLOC_WAIT(savkabuf, struct secasvar **, total_req_size); |
0a7de745 | 5932 | if (savkabuf) { |
2d21ac55 | 5933 | savkaptr = savkabuf; |
0a7de745 | 5934 | } |
2d21ac55 | 5935 | } |
91447636 | 5936 | lck_mtx_lock(sadb_mutex); |
1c79356b | 5937 | /* SPD */ |
2d21ac55 | 5938 | if (spbuf) { |
2d21ac55 | 5939 | struct secpolicy *sp, *nextsp; |
0a7de745 | 5940 | |
2d21ac55 A |
5941 | for (dir = 0; dir < IPSEC_DIR_MAX; dir++) { |
5942 | for (sp = LIST_FIRST(&sptree[dir]); | |
0a7de745 A |
5943 | sp != NULL; |
5944 | sp = nextsp) { | |
39236c6e | 5945 | /* don't prevent timehandler from stopping for generate policy */ |
0a7de745 | 5946 | if (sp->policy != IPSEC_POLICY_GENERATE) { |
39236c6e | 5947 | stop_handler = 0; |
0a7de745 | 5948 | } |
39236c6e | 5949 | spd_count++; |
2d21ac55 | 5950 | nextsp = LIST_NEXT(sp, chain); |
0a7de745 | 5951 | |
2d21ac55 A |
5952 | if (sp->state == IPSEC_SPSTATE_DEAD) { |
5953 | key_freesp(sp, KEY_SADB_LOCKED); | |
5954 | continue; | |
5955 | } | |
0a7de745 A |
5956 | |
5957 | if (sp->lifetime == 0 && sp->validtime == 0) { | |
2d21ac55 | 5958 | continue; |
0a7de745 | 5959 | } |
2d21ac55 A |
5960 | if (spbuf && spcount < spbufcount) { |
5961 | /* the deletion will occur next time */ | |
5962 | if ((sp->lifetime | |
0a7de745 | 5963 | && tv.tv_sec - sp->created > sp->lifetime) |
2d21ac55 | 5964 | || (sp->validtime |
0a7de745 A |
5965 | && tv.tv_sec - sp->lastused > sp->validtime)) { |
5966 | //key_spdexpire(sp); | |
5967 | sp->state = IPSEC_SPSTATE_DEAD; | |
5968 | sp->refcnt++; | |
5969 | *spptr++ = sp; | |
5970 | spcount++; | |
5971 | } | |
2d21ac55 | 5972 | } |
9bccf70c | 5973 | } |
1c79356b A |
5974 | } |
5975 | } | |
0a7de745 | 5976 | |
1c79356b | 5977 | /* SAD */ |
39236c6e | 5978 | { |
2d21ac55 A |
5979 | struct secashead *sah, *nextsah; |
5980 | struct secasvar *sav, *nextsav; | |
0a7de745 | 5981 | |
2d21ac55 | 5982 | for (sah = LIST_FIRST(&sahtree); |
0a7de745 A |
5983 | sah != NULL; |
5984 | sah = nextsah) { | |
39236c6e | 5985 | sah_count++; |
2d21ac55 | 5986 | nextsah = LIST_NEXT(sah, chain); |
0a7de745 | 5987 | |
2d21ac55 A |
5988 | /* if sah has been dead, then delete it and process next sah. */ |
5989 | if (sah->state == SADB_SASTATE_DEAD) { | |
5990 | key_delsah(sah); | |
e2fac8b1 A |
5991 | dead_sah_count++; |
5992 | continue; | |
5993 | } | |
0a7de745 | 5994 | |
e2fac8b1 | 5995 | if (LIST_FIRST(&sah->savtree[SADB_SASTATE_LARVAL]) == NULL && |
39236c6e A |
5996 | LIST_FIRST(&sah->savtree[SADB_SASTATE_MATURE]) == NULL && |
5997 | LIST_FIRST(&sah->savtree[SADB_SASTATE_DYING]) == NULL && | |
e2fac8b1 | 5998 | LIST_FIRST(&sah->savtree[SADB_SASTATE_DEAD]) == NULL) { |
39236c6e | 5999 | key_delsah(sah); |
e2fac8b1 | 6000 | empty_sah_count++; |
1c79356b A |
6001 | continue; |
6002 | } | |
0a7de745 | 6003 | |
39236c6e A |
6004 | if (savbufcount == 0) { |
6005 | continue; | |
6006 | } | |
0a7de745 | 6007 | |
39236c6e | 6008 | stop_handler = 0; |
0a7de745 | 6009 | |
2d21ac55 A |
6010 | /* if LARVAL entry doesn't become MATURE, delete it. */ |
6011 | for (sav = LIST_FIRST(&sah->savtree[SADB_SASTATE_LARVAL]); | |
0a7de745 A |
6012 | sav != NULL; |
6013 | sav = nextsav) { | |
39236c6e | 6014 | larval_sav_count++; |
e2fac8b1 | 6015 | total_sav_count++; |
2d21ac55 | 6016 | nextsav = LIST_NEXT(sav, chain); |
0a7de745 | 6017 | |
39236c6e A |
6018 | if (sav->lft_h != NULL) { |
6019 | /* If a hard lifetime is defined for the LARVAL SA, use it */ | |
6020 | if (sav->lft_h->sadb_lifetime_addtime != 0 | |
0a7de745 | 6021 | && tv.tv_sec - sav->created > sav->lft_h->sadb_lifetime_addtime) { |
39236c6e A |
6022 | if (sav->always_expire) { |
6023 | key_send_delete(sav); | |
6024 | sav = NULL; | |
6025 | } else { | |
6026 | key_sa_chgstate(sav, SADB_SASTATE_DEAD); | |
6027 | key_freesav(sav, KEY_SADB_LOCKED); | |
6028 | sav = NULL; | |
6029 | } | |
6030 | } | |
6031 | } else { | |
6032 | if (tv.tv_sec - sav->created > key_larval_lifetime) { | |
6033 | key_freesav(sav, KEY_SADB_LOCKED); | |
6034 | } | |
2d21ac55 A |
6035 | } |
6036 | } | |
0a7de745 | 6037 | |
2d21ac55 A |
6038 | /* |
6039 | * If this is a NAT traversal SA with no activity, | |
6040 | * we need to send a keep alive. | |
6041 | * | |
6042 | * Performed outside of the loop before so we will | |
6043 | * only ever send one keepalive. The first SA on | |
6044 | * the list is the one that will be used for sending | |
6045 | * traffic, so this is the one we use for determining | |
6046 | * when to send the keepalive. | |
6047 | */ | |
6048 | if (savkabuf && savkacount < savbufcount) { | |
0a7de745 | 6049 | sav = LIST_FIRST(&sah->savtree[SADB_SASTATE_MATURE]); //%%% should we check dying list if this is empty??? |
fe8ab488 | 6050 | if (sav && (natt_keepalive_interval || sav->natt_interval) && |
0a7de745 | 6051 | (sav->flags & (SADB_X_EXT_NATT_KEEPALIVE | SADB_X_EXT_ESP_KEEPALIVE)) != 0) { |
2d21ac55 A |
6052 | sav->refcnt++; |
6053 | *savkaptr++ = sav; | |
6054 | savkacount++; | |
6055 | } | |
6056 | } | |
0a7de745 | 6057 | |
2d21ac55 A |
6058 | /* |
6059 | * check MATURE entry to start to send expire message | |
6060 | * whether or not. | |
6061 | */ | |
6062 | for (sav = LIST_FIRST(&sah->savtree[SADB_SASTATE_MATURE]); | |
0a7de745 A |
6063 | sav != NULL; |
6064 | sav = nextsav) { | |
39236c6e | 6065 | mature_sav_count++; |
e2fac8b1 | 6066 | total_sav_count++; |
2d21ac55 | 6067 | nextsav = LIST_NEXT(sav, chain); |
0a7de745 | 6068 | |
2d21ac55 | 6069 | /* we don't need to check. */ |
0a7de745 | 6070 | if (sav->lft_s == NULL) { |
2d21ac55 | 6071 | continue; |
0a7de745 A |
6072 | } |
6073 | ||
2d21ac55 A |
6074 | /* sanity check */ |
6075 | if (sav->lft_c == NULL) { | |
0a7de745 A |
6076 | ipseclog((LOG_DEBUG, "key_timehandler: " |
6077 | "There is no CURRENT time, why?\n")); | |
2d21ac55 A |
6078 | continue; |
6079 | } | |
0a7de745 | 6080 | |
2d21ac55 A |
6081 | /* check SOFT lifetime */ |
6082 | if (sav->lft_s->sadb_lifetime_addtime != 0 | |
0a7de745 | 6083 | && tv.tv_sec - sav->created > sav->lft_s->sadb_lifetime_addtime) { |
2d21ac55 | 6084 | /* |
39236c6e A |
6085 | * If always_expire is set, expire. Otherwise, |
6086 | * if the SA has not been used, delete immediately. | |
2d21ac55 | 6087 | */ |
39236c6e | 6088 | if (sav->lft_c->sadb_lifetime_usetime == 0 |
0a7de745 | 6089 | && sav->always_expire == 0) { |
2d21ac55 A |
6090 | key_sa_chgstate(sav, SADB_SASTATE_DEAD); |
6091 | key_freesav(sav, KEY_SADB_LOCKED); | |
6092 | sav = NULL; | |
6093 | } else if (savexbuf && savexcount < savbufcount) { | |
39236c6e | 6094 | key_sa_chgstate(sav, SADB_SASTATE_DYING); |
2d21ac55 A |
6095 | sav->refcnt++; |
6096 | *savexptr++ = sav; | |
6097 | savexcount++; | |
6098 | } | |
6099 | } | |
2d21ac55 | 6100 | /* check SOFT lifetime by bytes */ |
1c79356b | 6101 | /* |
2d21ac55 A |
6102 | * XXX I don't know the way to delete this SA |
6103 | * when new SA is installed. Caution when it's | |
6104 | * installed too big lifetime by time. | |
1c79356b | 6105 | */ |
2d21ac55 | 6106 | else if (savexbuf && savexcount < savbufcount |
0a7de745 A |
6107 | && sav->lft_s->sadb_lifetime_bytes != 0 |
6108 | && sav->lft_s->sadb_lifetime_bytes < sav->lft_c->sadb_lifetime_bytes) { | |
1c79356b A |
6109 | /* |
6110 | * XXX If we keep to send expire | |
6111 | * message in the status of | |
6112 | * DYING. Do remove below code. | |
6113 | */ | |
2d21ac55 A |
6114 | //key_expire(sav); |
6115 | key_sa_chgstate(sav, SADB_SASTATE_DYING); | |
6116 | sav->refcnt++; | |
6117 | *savexptr++ = sav; | |
6118 | savexcount++; | |
1c79356b A |
6119 | } |
6120 | } | |
0a7de745 | 6121 | |
2d21ac55 A |
6122 | /* check DYING entry to change status to DEAD. */ |
6123 | for (sav = LIST_FIRST(&sah->savtree[SADB_SASTATE_DYING]); | |
0a7de745 A |
6124 | sav != NULL; |
6125 | sav = nextsav) { | |
39236c6e | 6126 | dying_sav_count++; |
e2fac8b1 | 6127 | total_sav_count++; |
2d21ac55 | 6128 | nextsav = LIST_NEXT(sav, chain); |
0a7de745 | 6129 | |
2d21ac55 | 6130 | /* we don't need to check. */ |
0a7de745 | 6131 | if (sav->lft_h == NULL) { |
2d21ac55 | 6132 | continue; |
0a7de745 A |
6133 | } |
6134 | ||
2d21ac55 A |
6135 | /* sanity check */ |
6136 | if (sav->lft_c == NULL) { | |
6137 | ipseclog((LOG_DEBUG, "key_timehandler: " | |
0a7de745 | 6138 | "There is no CURRENT time, why?\n")); |
2d21ac55 A |
6139 | continue; |
6140 | } | |
0a7de745 | 6141 | |
2d21ac55 | 6142 | if (sav->lft_h->sadb_lifetime_addtime != 0 |
0a7de745 | 6143 | && tv.tv_sec - sav->created > sav->lft_h->sadb_lifetime_addtime) { |
39236c6e A |
6144 | if (sav->always_expire) { |
6145 | key_send_delete(sav); | |
6146 | sav = NULL; | |
6147 | } else { | |
6148 | key_sa_chgstate(sav, SADB_SASTATE_DEAD); | |
6149 | key_freesav(sav, KEY_SADB_LOCKED); | |
6150 | sav = NULL; | |
6151 | } | |
2d21ac55 | 6152 | } |
2d21ac55 A |
6153 | /* check HARD lifetime by bytes */ |
6154 | else if (sav->lft_h->sadb_lifetime_bytes != 0 | |
0a7de745 | 6155 | && sav->lft_h->sadb_lifetime_bytes < sav->lft_c->sadb_lifetime_bytes) { |
2d21ac55 A |
6156 | key_sa_chgstate(sav, SADB_SASTATE_DEAD); |
6157 | key_freesav(sav, KEY_SADB_LOCKED); | |
6158 | sav = NULL; | |
6159 | } | |
1c79356b | 6160 | } |
0a7de745 | 6161 | |
2d21ac55 A |
6162 | /* delete entry in DEAD */ |
6163 | for (sav = LIST_FIRST(&sah->savtree[SADB_SASTATE_DEAD]); | |
0a7de745 A |
6164 | sav != NULL; |
6165 | sav = nextsav) { | |
39236c6e | 6166 | dead_sav_count++; |
e2fac8b1 | 6167 | total_sav_count++; |
2d21ac55 | 6168 | nextsav = LIST_NEXT(sav, chain); |
0a7de745 | 6169 | |
2d21ac55 A |
6170 | /* sanity check */ |
6171 | if (sav->state != SADB_SASTATE_DEAD) { | |
6172 | ipseclog((LOG_DEBUG, "key_timehandler: " | |
0a7de745 A |
6173 | "invalid sav->state " |
6174 | "(queue: %d SA: %d): " | |
6175 | "kill it anyway\n", | |
6176 | SADB_SASTATE_DEAD, sav->state)); | |
2d21ac55 | 6177 | } |
0a7de745 | 6178 | |
2d21ac55 A |
6179 | /* |
6180 | * do not call key_freesav() here. | |
6181 | * sav should already be freed, and sav->refcnt | |
6182 | * shows other references to sav | |
6183 | * (such as from SPD). | |
6184 | */ | |
1c79356b | 6185 | } |
1c79356b | 6186 | } |
39236c6e | 6187 | } |
0a7de745 | 6188 | |
39236c6e A |
6189 | if (++key_timehandler_debug >= 300) { |
6190 | if (key_debug_level) { | |
6191 | printf("%s: total stats for %u calls\n", __FUNCTION__, key_timehandler_debug); | |
6192 | printf("%s: walked %u SPDs\n", __FUNCTION__, spd_count); | |
6193 | printf("%s: walked %llu SAs: LARVAL SAs %u, MATURE SAs %u, DYING SAs %u, DEAD SAs %u\n", __FUNCTION__, | |
0a7de745 | 6194 | total_sav_count, larval_sav_count, mature_sav_count, dying_sav_count, dead_sav_count); |
39236c6e | 6195 | printf("%s: walked %u SAHs: DEAD SAHs %u, EMPTY SAHs %u\n", __FUNCTION__, |
0a7de745 | 6196 | sah_count, dead_sah_count, empty_sah_count); |
39236c6e A |
6197 | if (sah_search_calls) { |
6198 | printf("%s: SAH search cost %d iters per call\n", __FUNCTION__, | |
0a7de745 | 6199 | (sah_search_count / sah_search_calls)); |
39236c6e A |
6200 | } |
6201 | } | |
6202 | spd_count = 0; | |
6203 | sah_count = 0; | |
6204 | dead_sah_count = 0; | |
6205 | empty_sah_count = 0; | |
6206 | larval_sav_count = 0; | |
6207 | mature_sav_count = 0; | |
6208 | dying_sav_count = 0; | |
6209 | dead_sav_count = 0; | |
6210 | total_sav_count = 0; | |
6211 | sah_search_count = 0; | |
6212 | sah_search_calls = 0; | |
6213 | key_timehandler_debug = 0; | |
6214 | } | |
1c79356b A |
6215 | #ifndef IPSEC_NONBLOCK_ACQUIRE |
6216 | /* ACQ tree */ | |
0a7de745 | 6217 | { |
39236c6e | 6218 | struct secacq *acq, *nextacq; |
0a7de745 | 6219 | |
39236c6e | 6220 | for (acq = LIST_FIRST(&acqtree); |
0a7de745 A |
6221 | acq != NULL; |
6222 | acq = nextacq) { | |
39236c6e A |
6223 | stop_handler = 0; |
6224 | nextacq = LIST_NEXT(acq, chain); | |
0a7de745 | 6225 | |
39236c6e | 6226 | if (tv.tv_sec - acq->created > key_blockacq_lifetime |
0a7de745 | 6227 | && __LIST_CHAINED(acq)) { |
39236c6e A |
6228 | LIST_REMOVE(acq, chain); |
6229 | KFREE(acq); | |
6230 | } | |
1c79356b | 6231 | } |
0a7de745 | 6232 | } |
1c79356b | 6233 | #endif |
0a7de745 | 6234 | |
1c79356b | 6235 | /* SP ACQ tree */ |
0a7de745 | 6236 | { |
39236c6e | 6237 | struct secspacq *acq, *nextacq; |
0a7de745 | 6238 | |
39236c6e | 6239 | for (acq = LIST_FIRST(&spacqtree); |
0a7de745 A |
6240 | acq != NULL; |
6241 | acq = nextacq) { | |
39236c6e A |
6242 | stop_handler = 0; |
6243 | nextacq = LIST_NEXT(acq, chain); | |
0a7de745 | 6244 | |
39236c6e | 6245 | if (tv.tv_sec - acq->created > key_blockacq_lifetime |
0a7de745 | 6246 | && __LIST_CHAINED(acq)) { |
39236c6e A |
6247 | LIST_REMOVE(acq, chain); |
6248 | KFREE(acq); | |
6249 | } | |
1c79356b | 6250 | } |
0a7de745 A |
6251 | } |
6252 | ||
1c79356b A |
6253 | /* initialize random seed */ |
6254 | if (key_tick_init_random++ > key_int_random) { | |
6255 | key_tick_init_random = 0; | |
6256 | key_srandom(); | |
6257 | } | |
39037602 A |
6258 | |
6259 | uint64_t acc_sleep_time = 0; | |
6260 | absolutetime_to_nanoseconds(mach_absolutetime_asleep, &acc_sleep_time); | |
6261 | natt_now = ++up_time + (acc_sleep_time / NSEC_PER_SEC); | |
0a7de745 | 6262 | |
91447636 | 6263 | lck_mtx_unlock(sadb_mutex); |
0a7de745 | 6264 | |
2d21ac55 A |
6265 | /* send messages outside of sadb_mutex */ |
6266 | if (spbuf && spcount > 0) { | |
6267 | cnt = spcount; | |
0a7de745 | 6268 | while (cnt--) { |
2d21ac55 | 6269 | key_spdexpire(*(--spptr)); |
0a7de745 | 6270 | } |
2d21ac55 A |
6271 | } |
6272 | if (savkabuf && savkacount > 0) { | |
b0d623f7 | 6273 | struct secasvar **savkaptr_sav = savkaptr; |
eb6b6ca3 | 6274 | u_int32_t cnt_send = savkacount; |
0a7de745 | 6275 | |
b0d623f7 A |
6276 | while (cnt_send--) { |
6277 | if (ipsec_send_natt_keepalive(*(--savkaptr))) { | |
6278 | // <rdar://6768487> iterate (all over again) and update timestamps | |
6279 | struct secasvar **savkaptr_update = savkaptr_sav; | |
eb6b6ca3 | 6280 | u_int32_t cnt_update = savkacount; |
b0d623f7 A |
6281 | while (cnt_update--) { |
6282 | key_update_natt_keepalive_timestamp(*savkaptr, | |
0a7de745 | 6283 | *(--savkaptr_update)); |
b0d623f7 A |
6284 | } |
6285 | } | |
6286 | } | |
2d21ac55 A |
6287 | } |
6288 | if (savexbuf && savexcount > 0) { | |
6289 | cnt = savexcount; | |
0a7de745 | 6290 | while (cnt--) { |
2d21ac55 | 6291 | key_expire(*(--savexptr)); |
0a7de745 | 6292 | } |
2d21ac55 | 6293 | } |
0a7de745 | 6294 | |
2d21ac55 A |
6295 | /* decrement ref counts and free buffers */ |
6296 | lck_mtx_lock(sadb_mutex); | |
6297 | if (spbuf) { | |
0a7de745 | 6298 | while (spcount--) { |
2d21ac55 | 6299 | key_freesp(*spptr++, KEY_SADB_LOCKED); |
0a7de745 | 6300 | } |
2d21ac55 A |
6301 | KFREE(spbuf); |
6302 | } | |
6303 | if (savkabuf) { | |
0a7de745 | 6304 | while (savkacount--) { |
2d21ac55 | 6305 | key_freesav(*savkaptr++, KEY_SADB_LOCKED); |
0a7de745 | 6306 | } |
2d21ac55 A |
6307 | KFREE(savkabuf); |
6308 | } | |
6309 | if (savexbuf) { | |
0a7de745 | 6310 | while (savexcount--) { |
2d21ac55 | 6311 | key_freesav(*savexptr++, KEY_SADB_LOCKED); |
0a7de745 | 6312 | } |
2d21ac55 A |
6313 | KFREE(savexbuf); |
6314 | } | |
0a7de745 | 6315 | |
fe8ab488 | 6316 | if (stop_handler) { |
39236c6e | 6317 | key_timehandler_running = 0; |
fe8ab488 A |
6318 | /* Turn on the ipsec bypass */ |
6319 | ipsec_bypass = 1; | |
6320 | } else { | |
39236c6e A |
6321 | /* do exchange to tick time !! */ |
6322 | (void)timeout((void *)key_timehandler, (void *)0, hz); | |
6323 | } | |
1c79356b | 6324 | |
39236c6e | 6325 | lck_mtx_unlock(sadb_mutex); |
1c79356b A |
6326 | return; |
6327 | } | |
6328 | ||
6329 | /* | |
6330 | * to initialize a seed for random() | |
6331 | */ | |
9bccf70c | 6332 | static void |
6d2010ae | 6333 | key_srandom(void) |
1c79356b | 6334 | { |
9bccf70c A |
6335 | #ifdef __APPLE__ |
6336 | /* Our PRNG is based on Yarrow and doesn't need to be seeded */ | |
6337 | random(); | |
6338 | #else | |
1c79356b | 6339 | struct timeval tv; |
0a7de745 | 6340 | |
1c79356b | 6341 | microtime(&tv); |
0a7de745 | 6342 | |
1c79356b | 6343 | srandom(tv.tv_usec); |
1c79356b | 6344 | #endif |
0a7de745 | 6345 | |
1c79356b A |
6346 | return; |
6347 | } | |
6348 | ||
b0d623f7 | 6349 | u_int32_t |
6d2010ae | 6350 | key_random(void) |
9bccf70c | 6351 | { |
b0d623f7 | 6352 | u_int32_t value; |
0a7de745 | 6353 | |
9bccf70c A |
6354 | key_randomfill(&value, sizeof(value)); |
6355 | return value; | |
6356 | } | |
6357 | ||
6358 | void | |
6d2010ae | 6359 | key_randomfill( |
0a7de745 A |
6360 | void *p, |
6361 | size_t l) | |
9bccf70c | 6362 | { |
9bccf70c | 6363 | #ifdef __APPLE__ |
0a7de745 | 6364 | cc_rand_generate(p, l); |
9bccf70c | 6365 | #else |
2d21ac55 | 6366 | size_t n; |
b0d623f7 | 6367 | u_int32_t v; |
2d21ac55 | 6368 | static int warn = 1; |
0a7de745 | 6369 | |
9bccf70c A |
6370 | n = 0; |
6371 | n = (size_t)read_random(p, (u_int)l); | |
6372 | /* last resort */ | |
6373 | while (n < l) { | |
6374 | v = random(); | |
6375 | bcopy(&v, (u_int8_t *)p + n, | |
0a7de745 | 6376 | l - n < sizeof(v) ? l - n : sizeof(v)); |
9bccf70c | 6377 | n += sizeof(v); |
0a7de745 | 6378 | |
9bccf70c A |
6379 | if (warn) { |
6380 | printf("WARNING: pseudo-random number generator " | |
0a7de745 | 6381 | "used for IPsec processing\n"); |
9bccf70c A |
6382 | warn = 0; |
6383 | } | |
6384 | } | |
6385 | #endif | |
6386 | } | |
6387 | ||
1c79356b A |
6388 | /* |
6389 | * map SADB_SATYPE_* to IPPROTO_*. | |
6390 | * if satype == SADB_SATYPE then satype is mapped to ~0. | |
6391 | * OUT: | |
6392 | * 0: invalid satype. | |
6393 | */ | |
f427ee49 | 6394 | static u_int8_t |
6d2010ae | 6395 | key_satype2proto( |
0a7de745 | 6396 | u_int8_t satype) |
1c79356b A |
6397 | { |
6398 | switch (satype) { | |
0a7de745 A |
6399 | case SADB_SATYPE_UNSPEC: |
6400 | return IPSEC_PROTO_ANY; | |
6401 | case SADB_SATYPE_AH: | |
6402 | return IPPROTO_AH; | |
6403 | case SADB_SATYPE_ESP: | |
6404 | return IPPROTO_ESP; | |
0a7de745 A |
6405 | default: |
6406 | return 0; | |
1c79356b A |
6407 | } |
6408 | /* NOTREACHED */ | |
6409 | } | |
6410 | ||
6411 | /* | |
6412 | * map IPPROTO_* to SADB_SATYPE_* | |
6413 | * OUT: | |
6414 | * 0: invalid protocol type. | |
6415 | */ | |
6416 | static u_int8_t | |
6d2010ae | 6417 | key_proto2satype( |
0a7de745 | 6418 | u_int16_t proto) |
1c79356b A |
6419 | { |
6420 | switch (proto) { | |
0a7de745 A |
6421 | case IPPROTO_AH: |
6422 | return SADB_SATYPE_AH; | |
6423 | case IPPROTO_ESP: | |
6424 | return SADB_SATYPE_ESP; | |
0a7de745 A |
6425 | default: |
6426 | return 0; | |
1c79356b A |
6427 | } |
6428 | /* NOTREACHED */ | |
6429 | } | |
6430 | ||
fe8ab488 | 6431 | static ifnet_t |
0a7de745 | 6432 | key_get_ipsec_if_from_message(const struct sadb_msghdr *mhp, int message_type) |
fe8ab488 A |
6433 | { |
6434 | struct sadb_x_ipsecif *ipsecifopts = NULL; | |
6435 | ifnet_t ipsec_if = NULL; | |
0a7de745 | 6436 | |
3e170ce0 | 6437 | ipsecifopts = (struct sadb_x_ipsecif *)(void *)mhp->ext[message_type]; |
fe8ab488 | 6438 | if (ipsecifopts != NULL) { |
3e170ce0 | 6439 | if (ipsecifopts->sadb_x_ipsecif_ipsec_if[0]) { |
f427ee49 | 6440 | ipsecifopts->sadb_x_ipsecif_ipsec_if[IFXNAMSIZ - 1] = '\0'; |
fe8ab488 A |
6441 | ifnet_find_by_name(ipsecifopts->sadb_x_ipsecif_ipsec_if, &ipsec_if); |
6442 | } | |
6443 | } | |
0a7de745 | 6444 | |
fe8ab488 A |
6445 | return ipsec_if; |
6446 | } | |
6447 | ||
6448 | static u_int | |
0a7de745 | 6449 | key_get_outgoing_ifindex_from_message(const struct sadb_msghdr *mhp, int message_type) |
fe8ab488 A |
6450 | { |
6451 | struct sadb_x_ipsecif *ipsecifopts = NULL; | |
6452 | ifnet_t outgoing_if = NULL; | |
0a7de745 | 6453 | |
3e170ce0 | 6454 | ipsecifopts = (struct sadb_x_ipsecif *)(void *)mhp->ext[message_type]; |
fe8ab488 | 6455 | if (ipsecifopts != NULL) { |
3e170ce0 | 6456 | if (ipsecifopts->sadb_x_ipsecif_outgoing_if[0]) { |
f427ee49 | 6457 | ipsecifopts->sadb_x_ipsecif_outgoing_if[IFXNAMSIZ - 1] = '\0'; |
fe8ab488 | 6458 | ifnet_find_by_name(ipsecifopts->sadb_x_ipsecif_outgoing_if, &outgoing_if); |
0a7de745 A |
6459 | } |
6460 | } | |
6461 | ||
f427ee49 A |
6462 | u_int outgoing_if_index = 0; |
6463 | if (outgoing_if != NULL) { | |
6464 | outgoing_if_index = outgoing_if->if_index; | |
6465 | ifnet_release(outgoing_if); | |
6466 | } | |
6467 | ||
6468 | return outgoing_if_index; | |
fe8ab488 A |
6469 | } |
6470 | ||
1c79356b A |
6471 | /* %%% PF_KEY */ |
6472 | /* | |
6473 | * SADB_GETSPI processing is to receive | |
9bccf70c | 6474 | * <base, (SA2), src address, dst address, (SPI range)> |
1c79356b A |
6475 | * from the IKMPd, to assign a unique spi value, to hang on the INBOUND |
6476 | * tree with the status of LARVAL, and send | |
6477 | * <base, SA(*), address(SD)> | |
6478 | * to the IKMPd. | |
6479 | * | |
6480 | * IN: mhp: pointer to the pointer to each header. | |
6481 | * OUT: NULL if fail. | |
6482 | * other if success, return pointer to the message to send. | |
6483 | */ | |
9bccf70c | 6484 | static int |
6d2010ae | 6485 | key_getspi( |
0a7de745 A |
6486 | struct socket *so, |
6487 | struct mbuf *m, | |
6488 | const struct sadb_msghdr *mhp) | |
1c79356b | 6489 | { |
1c79356b A |
6490 | struct sadb_address *src0, *dst0; |
6491 | struct secasindex saidx; | |
6492 | struct secashead *newsah; | |
6493 | struct secasvar *newsav; | |
fe8ab488 | 6494 | ifnet_t ipsec_if = NULL; |
1c79356b A |
6495 | u_int8_t proto; |
6496 | u_int32_t spi; | |
9bccf70c A |
6497 | u_int8_t mode; |
6498 | u_int32_t reqid; | |
6499 | int error; | |
0a7de745 | 6500 | |
5ba3f43e | 6501 | LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_NOTOWNED); |
0a7de745 | 6502 | |
1c79356b | 6503 | /* sanity check */ |
0a7de745 | 6504 | if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL) { |
1c79356b | 6505 | panic("key_getspi: NULL pointer is passed.\n"); |
0a7de745 A |
6506 | } |
6507 | ||
9bccf70c A |
6508 | if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL || |
6509 | mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) { | |
55e303ae | 6510 | ipseclog((LOG_DEBUG, "key_getspi: invalid message is passed.\n")); |
9bccf70c A |
6511 | return key_senderror(so, m, EINVAL); |
6512 | } | |
6513 | if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) || | |
6514 | mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) { | |
55e303ae | 6515 | ipseclog((LOG_DEBUG, "key_getspi: invalid message is passed.\n")); |
9bccf70c A |
6516 | return key_senderror(so, m, EINVAL); |
6517 | } | |
6518 | if (mhp->ext[SADB_X_EXT_SA2] != NULL) { | |
316670eb | 6519 | mode = ((struct sadb_x_sa2 *) |
0a7de745 | 6520 | (void *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode; |
316670eb | 6521 | reqid = ((struct sadb_x_sa2 *) |
0a7de745 | 6522 | (void *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid; |
9bccf70c A |
6523 | } else { |
6524 | mode = IPSEC_MODE_ANY; | |
6525 | reqid = 0; | |
1c79356b | 6526 | } |
0a7de745 | 6527 | |
9bccf70c A |
6528 | src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]); |
6529 | dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]); | |
0a7de745 | 6530 | |
1c79356b | 6531 | /* map satype to proto */ |
9bccf70c | 6532 | if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) { |
55e303ae | 6533 | ipseclog((LOG_DEBUG, "key_getspi: invalid satype is passed.\n")); |
9bccf70c A |
6534 | return key_senderror(so, m, EINVAL); |
6535 | } | |
0a7de745 | 6536 | |
9bccf70c A |
6537 | /* make sure if port number is zero. */ |
6538 | switch (((struct sockaddr *)(src0 + 1))->sa_family) { | |
0a7de745 A |
6539 | case AF_INET: |
6540 | if (((struct sockaddr *)(src0 + 1))->sa_len != | |
6541 | sizeof(struct sockaddr_in)) { | |
6542 | return key_senderror(so, m, EINVAL); | |
6543 | } | |
6544 | ((struct sockaddr_in *)(void *)(src0 + 1))->sin_port = 0; | |
6545 | break; | |
6546 | case AF_INET6: | |
6547 | if (((struct sockaddr *)(src0 + 1))->sa_len != | |
6548 | sizeof(struct sockaddr_in6)) { | |
6549 | return key_senderror(so, m, EINVAL); | |
6550 | } | |
6551 | ((struct sockaddr_in6 *)(void *)(src0 + 1))->sin6_port = 0; | |
6552 | break; | |
6553 | default: | |
6554 | ; /*???*/ | |
9bccf70c A |
6555 | } |
6556 | switch (((struct sockaddr *)(dst0 + 1))->sa_family) { | |
0a7de745 A |
6557 | case AF_INET: |
6558 | if (((struct sockaddr *)(dst0 + 1))->sa_len != | |
6559 | sizeof(struct sockaddr_in)) { | |
6560 | return key_senderror(so, m, EINVAL); | |
6561 | } | |
6562 | ((struct sockaddr_in *)(void *)(dst0 + 1))->sin_port = 0; | |
6563 | break; | |
6564 | case AF_INET6: | |
6565 | if (((struct sockaddr *)(dst0 + 1))->sa_len != | |
6566 | sizeof(struct sockaddr_in6)) { | |
6567 | return key_senderror(so, m, EINVAL); | |
6568 | } | |
6569 | ((struct sockaddr_in6 *)(void *)(dst0 + 1))->sin6_port = 0; | |
6570 | break; | |
6571 | default: | |
6572 | ; /*???*/ | |
1c79356b | 6573 | } |
0a7de745 | 6574 | |
f427ee49 A |
6575 | ipsec_if = key_get_ipsec_if_from_message(mhp, SADB_X_EXT_IPSECIF); |
6576 | ||
9bccf70c | 6577 | /* XXX boundary check against sa_len */ |
fe8ab488 | 6578 | KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, ipsec_if ? ipsec_if->if_index : 0, &saidx); |
0a7de745 | 6579 | |
2d21ac55 | 6580 | lck_mtx_lock(sadb_mutex); |
0a7de745 | 6581 | |
1c79356b | 6582 | /* SPI allocation */ |
316670eb | 6583 | spi = key_do_getnewspi((struct sadb_spirange *) |
0a7de745 | 6584 | (void *)mhp->ext[SADB_EXT_SPIRANGE], &saidx); |
2d21ac55 A |
6585 | if (spi == 0) { |
6586 | lck_mtx_unlock(sadb_mutex); | |
f427ee49 A |
6587 | if (ipsec_if != NULL) { |
6588 | ifnet_release(ipsec_if); | |
6589 | } | |
9bccf70c | 6590 | return key_senderror(so, m, EINVAL); |
2d21ac55 | 6591 | } |
0a7de745 | 6592 | |
1c79356b | 6593 | /* get a SA index */ |
ea3f0419 | 6594 | if ((newsah = key_getsah(&saidx, SECURITY_ASSOCIATION_ANY)) == NULL) { |
b0d623f7 | 6595 | /* create a new SA index: key_addspi is always used for inbound spi */ |
ea3f0419 | 6596 | if ((newsah = key_newsah(&saidx, ipsec_if, key_get_outgoing_ifindex_from_message(mhp, SADB_X_EXT_IPSECIF), IPSEC_DIR_INBOUND, SECURITY_ASSOCIATION_PFKEY)) == NULL) { |
2d21ac55 | 6597 | lck_mtx_unlock(sadb_mutex); |
f427ee49 A |
6598 | if (ipsec_if != NULL) { |
6599 | ifnet_release(ipsec_if); | |
6600 | } | |
55e303ae | 6601 | ipseclog((LOG_DEBUG, "key_getspi: No more memory.\n")); |
9bccf70c | 6602 | return key_senderror(so, m, ENOBUFS); |
1c79356b A |
6603 | } |
6604 | } | |
0a7de745 | 6605 | |
f427ee49 A |
6606 | if (ipsec_if != NULL) { |
6607 | ifnet_release(ipsec_if); | |
6608 | ipsec_if = NULL; | |
6609 | } | |
6610 | ||
6611 | // Increment use count, since key_newsav() could release sadb_mutex lock | |
6612 | newsah->use_count++; | |
6613 | ||
ea3f0419 | 6614 | if ((newsah->flags & SECURITY_ASSOCIATION_CUSTOM_IPSEC) == SECURITY_ASSOCIATION_CUSTOM_IPSEC) { |
f427ee49 | 6615 | newsah->use_count--; |
ea3f0419 A |
6616 | lck_mtx_unlock(sadb_mutex); |
6617 | ipseclog((LOG_ERR, "key_getspi: custom ipsec exists\n")); | |
6618 | return key_senderror(so, m, EEXIST); | |
6619 | } | |
6620 | ||
1c79356b | 6621 | /* get a new SA */ |
9bccf70c | 6622 | /* XXX rewrite */ |
fe8ab488 | 6623 | newsav = key_newsav(m, mhp, newsah, &error, so); |
9bccf70c | 6624 | if (newsav == NULL) { |
1c79356b | 6625 | /* XXX don't free new SA index allocated in above. */ |
f427ee49 | 6626 | newsah->use_count--; |
2d21ac55 | 6627 | lck_mtx_unlock(sadb_mutex); |
9bccf70c | 6628 | return key_senderror(so, m, error); |
1c79356b | 6629 | } |
0a7de745 | 6630 | |
f427ee49 A |
6631 | if (newsah->state == SADB_SASTATE_DEAD) { |
6632 | newsah->use_count--; | |
6633 | key_sa_chgstate(newsav, SADB_SASTATE_DEAD); | |
6634 | key_freesav(newsav, KEY_SADB_LOCKED); | |
6635 | lck_mtx_unlock(sadb_mutex); | |
6636 | ipseclog((LOG_ERR, "key_getspi: security association head is dead\n")); | |
6637 | return key_senderror(so, m, EINVAL); | |
6638 | } | |
6639 | ||
1c79356b | 6640 | /* set spi */ |
91447636 | 6641 | key_setspi(newsav, htonl(spi)); |
0a7de745 | 6642 | |
1c79356b A |
6643 | #ifndef IPSEC_NONBLOCK_ACQUIRE |
6644 | /* delete the entry in acqtree */ | |
9bccf70c | 6645 | if (mhp->msg->sadb_msg_seq != 0) { |
1c79356b | 6646 | struct secacq *acq; |
9bccf70c A |
6647 | if ((acq = key_getacqbyseq(mhp->msg->sadb_msg_seq)) != NULL) { |
6648 | /* reset counter in order to deletion by timehandler. */ | |
6649 | struct timeval tv; | |
6650 | microtime(&tv); | |
6651 | acq->created = tv.tv_sec; | |
1c79356b A |
6652 | acq->count = 0; |
6653 | } | |
39236c6e | 6654 | } |
1c79356b | 6655 | #endif |
f427ee49 | 6656 | newsah->use_count--; |
2d21ac55 | 6657 | lck_mtx_unlock(sadb_mutex); |
0a7de745 A |
6658 | |
6659 | { | |
39236c6e A |
6660 | struct mbuf *n, *nn; |
6661 | struct sadb_sa *m_sa; | |
6662 | struct sadb_msg *newmsg; | |
6663 | int off, len; | |
0a7de745 | 6664 | |
39236c6e A |
6665 | /* create new sadb_msg to reply. */ |
6666 | len = PFKEY_ALIGN8(sizeof(struct sadb_msg)) + | |
0a7de745 A |
6667 | PFKEY_ALIGN8(sizeof(struct sadb_sa)); |
6668 | if (len > MCLBYTES) { | |
39236c6e | 6669 | return key_senderror(so, m, ENOBUFS); |
0a7de745 A |
6670 | } |
6671 | ||
39236c6e A |
6672 | MGETHDR(n, M_WAITOK, MT_DATA); |
6673 | if (n && len > MHLEN) { | |
6674 | MCLGET(n, M_WAITOK); | |
6675 | if ((n->m_flags & M_EXT) == 0) { | |
6676 | m_freem(n); | |
6677 | n = NULL; | |
6678 | } | |
9bccf70c | 6679 | } |
0a7de745 | 6680 | if (!n) { |
39236c6e | 6681 | return key_senderror(so, m, ENOBUFS); |
0a7de745 A |
6682 | } |
6683 | ||
39236c6e A |
6684 | n->m_len = len; |
6685 | n->m_next = NULL; | |
6686 | off = 0; | |
0a7de745 | 6687 | |
39236c6e A |
6688 | m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off); |
6689 | off += PFKEY_ALIGN8(sizeof(struct sadb_msg)); | |
0a7de745 | 6690 | |
39236c6e | 6691 | m_sa = (struct sadb_sa *)(void *)(mtod(n, caddr_t) + off); |
eb6b6ca3 | 6692 | memset(m_sa, 0, PFKEY_ALIGN8(sizeof(struct sadb_sa))); |
39236c6e A |
6693 | m_sa->sadb_sa_len = PFKEY_UNIT64(sizeof(struct sadb_sa)); |
6694 | m_sa->sadb_sa_exttype = SADB_EXT_SA; | |
6695 | m_sa->sadb_sa_spi = htonl(spi); | |
6696 | off += PFKEY_ALIGN8(sizeof(struct sadb_sa)); | |
0a7de745 | 6697 | |
9bccf70c | 6698 | #if DIAGNOSTIC |
0a7de745 | 6699 | if (off != len) { |
39236c6e | 6700 | panic("length inconsistency in key_getspi"); |
0a7de745 | 6701 | } |
9bccf70c | 6702 | #endif |
39236c6e A |
6703 | { |
6704 | int mbufItems[] = {SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST}; | |
0a7de745 | 6705 | n->m_next = key_gather_mbuf(m, mhp, 0, sizeof(mbufItems) / sizeof(int), mbufItems); |
39236c6e A |
6706 | if (!n->m_next) { |
6707 | m_freem(n); | |
6708 | return key_senderror(so, m, ENOBUFS); | |
6709 | } | |
6710 | } | |
0a7de745 | 6711 | |
39236c6e A |
6712 | if (n->m_len < sizeof(struct sadb_msg)) { |
6713 | n = m_pullup(n, sizeof(struct sadb_msg)); | |
0a7de745 | 6714 | if (n == NULL) { |
39236c6e | 6715 | return key_sendup_mbuf(so, m, KEY_SENDUP_ONE); |
0a7de745 | 6716 | } |
39236c6e | 6717 | } |
0a7de745 | 6718 | |
39236c6e | 6719 | n->m_pkthdr.len = 0; |
0a7de745 | 6720 | for (nn = n; nn; nn = nn->m_next) { |
39236c6e | 6721 | n->m_pkthdr.len += nn->m_len; |
0a7de745 A |
6722 | } |
6723 | ||
39236c6e A |
6724 | newmsg = mtod(n, struct sadb_msg *); |
6725 | newmsg->sadb_msg_seq = newsav->seq; | |
6726 | newmsg->sadb_msg_errno = 0; | |
f427ee49 A |
6727 | VERIFY(PFKEY_UNIT64(n->m_pkthdr.len) <= UINT16_MAX); |
6728 | newmsg->sadb_msg_len = (u_int16_t)PFKEY_UNIT64(n->m_pkthdr.len); | |
0a7de745 | 6729 | |
39236c6e A |
6730 | m_freem(m); |
6731 | return key_sendup_mbuf(so, n, KEY_SENDUP_ONE); | |
0a7de745 | 6732 | } |
1c79356b A |
6733 | } |
6734 | ||
6735 | /* | |
6736 | * allocating new SPI | |
f427ee49 | 6737 | * called by key_getspi(). |
1c79356b A |
6738 | * OUT: |
6739 | * 0: failure. | |
6740 | * others: success. | |
6741 | */ | |
6742 | static u_int32_t | |
6d2010ae | 6743 | key_do_getnewspi( |
0a7de745 A |
6744 | struct sadb_spirange *spirange, |
6745 | struct secasindex *saidx) | |
1c79356b A |
6746 | { |
6747 | u_int32_t newspi; | |
2d21ac55 | 6748 | u_int32_t keymin, keymax; |
1c79356b | 6749 | int count = key_spi_trycnt; |
0a7de745 | 6750 | |
5ba3f43e | 6751 | LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_OWNED); |
0a7de745 | 6752 | |
1c79356b A |
6753 | /* set spi range to allocate */ |
6754 | if (spirange != NULL) { | |
2d21ac55 A |
6755 | keymin = spirange->sadb_spirange_min; |
6756 | keymax = spirange->sadb_spirange_max; | |
1c79356b | 6757 | } else { |
2d21ac55 A |
6758 | keymin = key_spi_minval; |
6759 | keymax = key_spi_maxval; | |
1c79356b | 6760 | } |
2d21ac55 A |
6761 | if (keymin == keymax) { |
6762 | if (key_checkspidup(saidx, keymin) != NULL) { | |
6763 | ipseclog((LOG_DEBUG, "key_do_getnewspi: SPI %u exists already.\n", keymin)); | |
1c79356b A |
6764 | return 0; |
6765 | } | |
0a7de745 | 6766 | |
1c79356b | 6767 | count--; /* taking one cost. */ |
2d21ac55 | 6768 | newspi = keymin; |
1c79356b | 6769 | } else { |
b0d623f7 | 6770 | u_int32_t range = keymax - keymin + 1; /* overflow value of zero means full range */ |
0a7de745 | 6771 | |
1c79356b A |
6772 | /* init SPI */ |
6773 | newspi = 0; | |
0a7de745 | 6774 | |
1c79356b A |
6775 | /* when requesting to allocate spi ranged */ |
6776 | while (count--) { | |
b0d623f7 | 6777 | u_int32_t rand_val = key_random(); |
0a7de745 | 6778 | |
1c79356b | 6779 | /* generate pseudo-random SPI value ranged. */ |
2d21ac55 | 6780 | newspi = (range == 0 ? rand_val : keymin + (rand_val % range)); |
0a7de745 A |
6781 | |
6782 | if (key_checkspidup(saidx, newspi) == NULL) { | |
1c79356b | 6783 | break; |
0a7de745 | 6784 | } |
1c79356b | 6785 | } |
0a7de745 | 6786 | |
1c79356b | 6787 | if (count == 0 || newspi == 0) { |
55e303ae | 6788 | ipseclog((LOG_DEBUG, "key_do_getnewspi: to allocate spi is failed.\n")); |
1c79356b A |
6789 | return 0; |
6790 | } | |
6791 | } | |
0a7de745 | 6792 | |
1c79356b A |
6793 | /* statistics */ |
6794 | keystat.getspi_count = | |
0a7de745 A |
6795 | (keystat.getspi_count + key_spi_trycnt - count) / 2; |
6796 | ||
1c79356b A |
6797 | return newspi; |
6798 | } | |
6799 | ||
6800 | /* | |
6801 | * SADB_UPDATE processing | |
6802 | * receive | |
9bccf70c | 6803 | * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),) |
1c79356b A |
6804 | * key(AE), (identity(SD),) (sensitivity)> |
6805 | * from the ikmpd, and update a secasvar entry whose status is SADB_SASTATE_LARVAL. | |
6806 | * and send | |
9bccf70c | 6807 | * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),) |
1c79356b A |
6808 | * (identity(SD),) (sensitivity)> |
6809 | * to the ikmpd. | |
6810 | * | |
9bccf70c | 6811 | * m will always be freed. |
1c79356b | 6812 | */ |
9bccf70c | 6813 | static int |
6d2010ae | 6814 | key_update( |
0a7de745 A |
6815 | struct socket *so, |
6816 | struct mbuf *m, | |
6817 | const struct sadb_msghdr *mhp) | |
1c79356b | 6818 | { |
f427ee49 A |
6819 | struct sadb_sa *sa0 = NULL; |
6820 | struct sadb_address *src0 = NULL, *dst0 = NULL; | |
fe8ab488 | 6821 | ifnet_t ipsec_if = NULL; |
1c79356b | 6822 | struct secasindex saidx; |
f427ee49 A |
6823 | struct secashead *sah = NULL; |
6824 | struct secasvar *sav = NULL; | |
6825 | u_int8_t proto; | |
9bccf70c A |
6826 | u_int8_t mode; |
6827 | u_int32_t reqid; | |
fe8ab488 | 6828 | u_int16_t flags2; |
9bccf70c | 6829 | int error; |
0a7de745 | 6830 | |
5ba3f43e | 6831 | LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_NOTOWNED); |
0a7de745 | 6832 | |
1c79356b | 6833 | /* sanity check */ |
0a7de745 | 6834 | if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL) { |
1c79356b | 6835 | panic("key_update: NULL pointer is passed.\n"); |
0a7de745 A |
6836 | } |
6837 | ||
1c79356b | 6838 | /* map satype to proto */ |
9bccf70c | 6839 | if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) { |
55e303ae | 6840 | ipseclog((LOG_DEBUG, "key_update: invalid satype is passed.\n")); |
f427ee49 | 6841 | bzero_keys(mhp); |
9bccf70c | 6842 | return key_senderror(so, m, EINVAL); |
1c79356b | 6843 | } |
0a7de745 | 6844 | |
9bccf70c A |
6845 | if (mhp->ext[SADB_EXT_SA] == NULL || |
6846 | mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL || | |
6847 | mhp->ext[SADB_EXT_ADDRESS_DST] == NULL || | |
6848 | (mhp->msg->sadb_msg_satype == SADB_SATYPE_ESP && | |
0a7de745 | 6849 | mhp->ext[SADB_EXT_KEY_ENCRYPT] == NULL) || |
9bccf70c | 6850 | (mhp->msg->sadb_msg_satype == SADB_SATYPE_AH && |
0a7de745 | 6851 | mhp->ext[SADB_EXT_KEY_AUTH] == NULL) || |
9bccf70c | 6852 | (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL && |
0a7de745 | 6853 | mhp->ext[SADB_EXT_LIFETIME_SOFT] == NULL) || |
9bccf70c | 6854 | (mhp->ext[SADB_EXT_LIFETIME_HARD] == NULL && |
0a7de745 A |
6855 | mhp->ext[SADB_EXT_LIFETIME_SOFT] != NULL)) { |
6856 | ipseclog((LOG_DEBUG, "key_update: invalid message is passed.\n")); | |
f427ee49 | 6857 | bzero_keys(mhp); |
0a7de745 A |
6858 | return key_senderror(so, m, EINVAL); |
6859 | } | |
9bccf70c A |
6860 | if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) || |
6861 | mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) || | |
6862 | mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) { | |
55e303ae | 6863 | ipseclog((LOG_DEBUG, "key_update: invalid message is passed.\n")); |
f427ee49 | 6864 | bzero_keys(mhp); |
9bccf70c A |
6865 | return key_senderror(so, m, EINVAL); |
6866 | } | |
6867 | if (mhp->ext[SADB_X_EXT_SA2] != NULL) { | |
316670eb | 6868 | mode = ((struct sadb_x_sa2 *) |
0a7de745 | 6869 | (void *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode; |
316670eb | 6870 | reqid = ((struct sadb_x_sa2 *) |
0a7de745 | 6871 | (void *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid; |
fe8ab488 | 6872 | flags2 = ((struct sadb_x_sa2 *)(void *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_flags; |
9bccf70c A |
6873 | } else { |
6874 | mode = IPSEC_MODE_ANY; | |
6875 | reqid = 0; | |
fe8ab488 | 6876 | flags2 = 0; |
9bccf70c A |
6877 | } |
6878 | /* XXX boundary checking for other extensions */ | |
0a7de745 | 6879 | |
316670eb | 6880 | sa0 = (struct sadb_sa *)(void *)mhp->ext[SADB_EXT_SA]; |
9bccf70c A |
6881 | src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]); |
6882 | dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]); | |
3e170ce0 | 6883 | ipsec_if = key_get_ipsec_if_from_message(mhp, SADB_X_EXT_IPSECIF); |
0a7de745 | 6884 | |
f427ee49 A |
6885 | u_int ipsec_if_index = 0; |
6886 | if (ipsec_if != NULL) { | |
6887 | ipsec_if_index = ipsec_if->if_index; | |
6888 | ifnet_release(ipsec_if); | |
6889 | ipsec_if = NULL; | |
6890 | } | |
6891 | ||
9bccf70c | 6892 | /* XXX boundary check against sa_len */ |
f427ee49 | 6893 | KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, ipsec_if_index, &saidx); |
0a7de745 | 6894 | |
2d21ac55 | 6895 | lck_mtx_lock(sadb_mutex); |
0a7de745 | 6896 | |
1c79356b | 6897 | /* get a SA header */ |
ea3f0419 | 6898 | if ((sah = key_getsah(&saidx, SECURITY_ASSOCIATION_PFKEY)) == NULL) { |
2d21ac55 | 6899 | lck_mtx_unlock(sadb_mutex); |
55e303ae | 6900 | ipseclog((LOG_DEBUG, "key_update: no SA index found.\n")); |
f427ee49 | 6901 | bzero_keys(mhp); |
9bccf70c | 6902 | return key_senderror(so, m, ENOENT); |
1c79356b | 6903 | } |
0a7de745 | 6904 | |
f427ee49 A |
6905 | // Increment use count, since key_setsaval() could release sadb_mutex lock |
6906 | sah->use_count++; | |
0a7de745 | 6907 | |
1c79356b | 6908 | if ((sav = key_getsavbyspi(sah, sa0->sadb_sa_spi)) == NULL) { |
55e303ae | 6909 | ipseclog((LOG_DEBUG, |
0a7de745 A |
6910 | "key_update: no such a SA found (spi:%u)\n", |
6911 | (u_int32_t)ntohl(sa0->sadb_sa_spi))); | |
f427ee49 A |
6912 | error = EINVAL; |
6913 | goto fail; | |
1c79356b | 6914 | } |
0a7de745 | 6915 | |
f427ee49 A |
6916 | // Increment reference count, since key_setsaval() could release sadb_mutex lock |
6917 | sav->refcnt++; | |
6918 | ||
1c79356b A |
6919 | /* validity check */ |
6920 | if (sav->sah->saidx.proto != proto) { | |
55e303ae | 6921 | ipseclog((LOG_DEBUG, |
0a7de745 A |
6922 | "key_update: protocol mismatched (DB=%u param=%u)\n", |
6923 | sav->sah->saidx.proto, proto)); | |
f427ee49 A |
6924 | error = EINVAL; |
6925 | goto fail; | |
1c79356b | 6926 | } |
eb6b6ca3 | 6927 | |
9bccf70c | 6928 | if (sav->pid != mhp->msg->sadb_msg_pid) { |
55e303ae | 6929 | ipseclog((LOG_DEBUG, |
0a7de745 A |
6930 | "key_update: pid mismatched (DB:%u param:%u)\n", |
6931 | sav->pid, mhp->msg->sadb_msg_pid)); | |
f427ee49 A |
6932 | error = EINVAL; |
6933 | goto fail; | |
1c79356b | 6934 | } |
0a7de745 | 6935 | |
1c79356b | 6936 | /* copy sav values */ |
9bccf70c A |
6937 | error = key_setsaval(sav, m, mhp); |
6938 | if (error) { | |
f427ee49 A |
6939 | goto fail; |
6940 | } | |
6941 | ||
6942 | if (sah->state == SADB_SASTATE_DEAD) { | |
6943 | ipseclog((LOG_ERR, | |
6944 | "key_update: security association head is dead\n")); | |
6945 | error = EINVAL; | |
6946 | goto fail; | |
1c79356b | 6947 | } |
fe8ab488 A |
6948 | |
6949 | sav->flags2 = flags2; | |
6950 | if (flags2 & SADB_X_EXT_SA2_DELETE_ON_DETACH) { | |
6951 | sav->so = so; | |
6952 | } | |
6953 | ||
2d21ac55 A |
6954 | /* |
6955 | * Verify if SADB_X_EXT_NATT_MULTIPLEUSERS flag is set that | |
6956 | * this SA is for transport mode - otherwise clear it. | |
6957 | */ | |
6958 | if ((sav->flags & SADB_X_EXT_NATT_MULTIPLEUSERS) != 0 && | |
0a7de745 A |
6959 | (sav->sah->saidx.mode != IPSEC_MODE_TRANSPORT || |
6960 | sav->sah->saidx.src.ss_family != AF_INET)) { | |
2d21ac55 | 6961 | sav->flags &= ~SADB_X_EXT_NATT_MULTIPLEUSERS; |
0a7de745 A |
6962 | } |
6963 | ||
1c79356b | 6964 | /* check SA values to be mature. */ |
91447636 | 6965 | if ((error = key_mature(sav)) != 0) { |
f427ee49 | 6966 | goto fail; |
1c79356b | 6967 | } |
0a7de745 | 6968 | |
f427ee49 A |
6969 | key_freesav(sav, KEY_SADB_LOCKED); |
6970 | sah->use_count--; | |
2d21ac55 | 6971 | lck_mtx_unlock(sadb_mutex); |
0a7de745 A |
6972 | |
6973 | { | |
39236c6e | 6974 | struct mbuf *n; |
0a7de745 | 6975 | |
39236c6e A |
6976 | /* set msg buf from mhp */ |
6977 | n = key_getmsgbuf_x1(m, mhp); | |
6978 | if (n == NULL) { | |
6979 | ipseclog((LOG_DEBUG, "key_update: No more memory.\n")); | |
6980 | return key_senderror(so, m, ENOBUFS); | |
6981 | } | |
0a7de745 | 6982 | |
f427ee49 | 6983 | bzero_keys(mhp); |
39236c6e A |
6984 | m_freem(m); |
6985 | return key_sendup_mbuf(so, n, KEY_SENDUP_ALL); | |
0a7de745 | 6986 | } |
f427ee49 A |
6987 | fail: |
6988 | if (sav != NULL) { | |
6989 | key_freesav(sav, KEY_SADB_LOCKED); | |
6990 | } | |
6991 | if (sah != NULL) { | |
6992 | sah->use_count--; | |
6993 | } | |
6994 | ||
6995 | lck_mtx_unlock(sadb_mutex); | |
6996 | bzero_keys(mhp); | |
6997 | return key_senderror(so, m, error); | |
1c79356b A |
6998 | } |
6999 | ||
3e170ce0 A |
7000 | static int |
7001 | key_migrate(struct socket *so, | |
0a7de745 A |
7002 | struct mbuf *m, |
7003 | const struct sadb_msghdr *mhp) | |
3e170ce0 A |
7004 | { |
7005 | struct sadb_sa *sa0 = NULL; | |
7006 | struct sadb_address *src0 = NULL; | |
7007 | struct sadb_address *dst0 = NULL; | |
7008 | struct sadb_address *src1 = NULL; | |
7009 | struct sadb_address *dst1 = NULL; | |
7010 | ifnet_t ipsec_if0 = NULL; | |
7011 | ifnet_t ipsec_if1 = NULL; | |
7012 | struct secasindex saidx0; | |
7013 | struct secasindex saidx1; | |
7014 | struct secashead *sah = NULL; | |
7015 | struct secashead *newsah = NULL; | |
7016 | struct secasvar *sav = NULL; | |
f427ee49 | 7017 | u_int8_t proto; |
0a7de745 | 7018 | |
5ba3f43e | 7019 | LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_NOTOWNED); |
0a7de745 | 7020 | |
3e170ce0 | 7021 | /* sanity check */ |
0a7de745 | 7022 | if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL) { |
3e170ce0 | 7023 | panic("key_migrate: NULL pointer is passed.\n"); |
0a7de745 A |
7024 | } |
7025 | ||
3e170ce0 A |
7026 | /* map satype to proto */ |
7027 | if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) { | |
7028 | ipseclog((LOG_DEBUG, "key_migrate: invalid satype is passed.\n")); | |
7029 | return key_senderror(so, m, EINVAL); | |
7030 | } | |
0a7de745 | 7031 | |
3e170ce0 | 7032 | if (mhp->ext[SADB_EXT_SA] == NULL || |
0a7de745 A |
7033 | mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL || |
7034 | mhp->ext[SADB_EXT_ADDRESS_DST] == NULL || | |
7035 | mhp->ext[SADB_EXT_MIGRATE_ADDRESS_SRC] == NULL || | |
7036 | mhp->ext[SADB_EXT_MIGRATE_ADDRESS_DST] == NULL) { | |
3e170ce0 A |
7037 | ipseclog((LOG_DEBUG, "key_migrate: invalid message is passed.\n")); |
7038 | return key_senderror(so, m, EINVAL); | |
7039 | } | |
0a7de745 | 7040 | |
3e170ce0 | 7041 | if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) || |
0a7de745 A |
7042 | mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) || |
7043 | mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) || | |
7044 | mhp->extlen[SADB_EXT_MIGRATE_ADDRESS_SRC] < sizeof(struct sadb_address) || | |
7045 | mhp->extlen[SADB_EXT_MIGRATE_ADDRESS_DST] < sizeof(struct sadb_address)) { | |
3e170ce0 A |
7046 | ipseclog((LOG_DEBUG, "key_migrate: invalid message is passed.\n")); |
7047 | return key_senderror(so, m, EINVAL); | |
7048 | } | |
0a7de745 | 7049 | |
3e170ce0 | 7050 | lck_mtx_lock(sadb_mutex); |
0a7de745 | 7051 | |
3e170ce0 A |
7052 | sa0 = (struct sadb_sa *)(void *)mhp->ext[SADB_EXT_SA]; |
7053 | src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]); | |
7054 | dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]); | |
7055 | src1 = (struct sadb_address *)(mhp->ext[SADB_EXT_MIGRATE_ADDRESS_SRC]); | |
7056 | dst1 = (struct sadb_address *)(mhp->ext[SADB_EXT_MIGRATE_ADDRESS_DST]); | |
7057 | ipsec_if0 = key_get_ipsec_if_from_message(mhp, SADB_X_EXT_IPSECIF); | |
7058 | ipsec_if1 = key_get_ipsec_if_from_message(mhp, SADB_X_EXT_MIGRATE_IPSECIF); | |
0a7de745 | 7059 | |
f427ee49 A |
7060 | u_int ipsec_if0_index = 0; |
7061 | if (ipsec_if0 != NULL) { | |
7062 | ipsec_if0_index = ipsec_if0->if_index; | |
7063 | ifnet_release(ipsec_if0); | |
7064 | ipsec_if0 = NULL; | |
7065 | } | |
7066 | ||
3e170ce0 | 7067 | /* Find existing SAH and SAV */ |
f427ee49 | 7068 | KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, ipsec_if0_index, &saidx0); |
0a7de745 | 7069 | |
3e170ce0 | 7070 | LIST_FOREACH(sah, &sahtree, chain) { |
0a7de745 | 7071 | if (sah->state != SADB_SASTATE_MATURE) { |
3e170ce0 | 7072 | continue; |
0a7de745 A |
7073 | } |
7074 | if (key_cmpsaidx(&sah->saidx, &saidx0, CMP_HEAD) == 0) { | |
3e170ce0 | 7075 | continue; |
0a7de745 A |
7076 | } |
7077 | ||
3e170ce0 | 7078 | sav = key_getsavbyspi(sah, sa0->sadb_sa_spi); |
0a7de745 | 7079 | if (sav && sav->state == SADB_SASTATE_MATURE) { |
3e170ce0 | 7080 | break; |
0a7de745 | 7081 | } |
3e170ce0 A |
7082 | } |
7083 | if (sah == NULL) { | |
7084 | lck_mtx_unlock(sadb_mutex); | |
f427ee49 A |
7085 | if (ipsec_if1 != NULL) { |
7086 | ifnet_release(ipsec_if1); | |
7087 | } | |
3e170ce0 A |
7088 | ipseclog((LOG_DEBUG, "key_migrate: no mature SAH found.\n")); |
7089 | return key_senderror(so, m, ENOENT); | |
7090 | } | |
0a7de745 | 7091 | |
3e170ce0 A |
7092 | if (sav == NULL) { |
7093 | lck_mtx_unlock(sadb_mutex); | |
f427ee49 A |
7094 | if (ipsec_if1 != NULL) { |
7095 | ifnet_release(ipsec_if1); | |
7096 | } | |
3e170ce0 A |
7097 | ipseclog((LOG_DEBUG, "key_migrate: no SA found.\n")); |
7098 | return key_senderror(so, m, ENOENT); | |
7099 | } | |
0a7de745 | 7100 | |
3e170ce0 A |
7101 | /* Find or create new SAH */ |
7102 | KEY_SETSECASIDX(proto, sah->saidx.mode, sah->saidx.reqid, src1 + 1, dst1 + 1, ipsec_if1 ? ipsec_if1->if_index : 0, &saidx1); | |
0a7de745 | 7103 | |
ea3f0419 A |
7104 | if ((newsah = key_getsah(&saidx1, SECURITY_ASSOCIATION_ANY)) == NULL) { |
7105 | if ((newsah = key_newsah(&saidx1, ipsec_if1, key_get_outgoing_ifindex_from_message(mhp, SADB_X_EXT_MIGRATE_IPSECIF), sah->dir, SECURITY_ASSOCIATION_PFKEY)) == NULL) { | |
3e170ce0 | 7106 | lck_mtx_unlock(sadb_mutex); |
f427ee49 A |
7107 | if (ipsec_if1 != NULL) { |
7108 | ifnet_release(ipsec_if1); | |
7109 | } | |
3e170ce0 A |
7110 | ipseclog((LOG_DEBUG, "key_migrate: No more memory.\n")); |
7111 | return key_senderror(so, m, ENOBUFS); | |
7112 | } | |
7113 | } | |
0a7de745 | 7114 | |
f427ee49 A |
7115 | if (ipsec_if1 != NULL) { |
7116 | ifnet_release(ipsec_if1); | |
7117 | ipsec_if1 = NULL; | |
7118 | } | |
7119 | ||
ea3f0419 A |
7120 | if ((newsah->flags & SECURITY_ASSOCIATION_CUSTOM_IPSEC) == SECURITY_ASSOCIATION_CUSTOM_IPSEC) { |
7121 | lck_mtx_unlock(sadb_mutex); | |
7122 | ipseclog((LOG_ERR, "key_migrate: custom ipsec exists\n")); | |
7123 | return key_senderror(so, m, EEXIST); | |
7124 | } | |
7125 | ||
3e170ce0 A |
7126 | /* Migrate SAV in to new SAH */ |
7127 | if (key_migratesav(sav, newsah) != 0) { | |
7128 | lck_mtx_unlock(sadb_mutex); | |
7129 | ipseclog((LOG_DEBUG, "key_migrate: Failed to migrate SA to new SAH.\n")); | |
7130 | return key_senderror(so, m, EINVAL); | |
7131 | } | |
0a7de745 | 7132 | |
3e170ce0 A |
7133 | /* Reset NAT values */ |
7134 | sav->flags = sa0->sadb_sa_flags; | |
cb323159 | 7135 | sav->natt_encapsulated_src_port = ((const struct sadb_sa_2*)(sa0))->sadb_sa_natt_src_port; |
3e170ce0 A |
7136 | sav->remote_ike_port = ((const struct sadb_sa_2*)(sa0))->sadb_sa_natt_port; |
7137 | sav->natt_interval = ((const struct sadb_sa_2*)(sa0))->sadb_sa_natt_interval; | |
7138 | sav->natt_offload_interval = ((const struct sadb_sa_2*)(sa0))->sadb_sa_natt_offload_interval; | |
7139 | sav->natt_last_activity = natt_now; | |
39037602 | 7140 | |
3e170ce0 A |
7141 | /* |
7142 | * Verify if SADB_X_EXT_NATT_MULTIPLEUSERS flag is set that | |
7143 | * SADB_X_EXT_NATT is set and SADB_X_EXT_NATT_KEEPALIVE is not | |
7144 | * set (we're not behind nat) - otherwise clear it. | |
7145 | */ | |
0a7de745 | 7146 | if ((sav->flags & SADB_X_EXT_NATT_MULTIPLEUSERS) != 0) { |
3e170ce0 | 7147 | if ((sav->flags & SADB_X_EXT_NATT) == 0 || |
0a7de745 | 7148 | (sav->flags & SADB_X_EXT_NATT_KEEPALIVE) != 0) { |
3e170ce0 | 7149 | sav->flags &= ~SADB_X_EXT_NATT_MULTIPLEUSERS; |
0a7de745 A |
7150 | } |
7151 | } | |
7152 | ||
3e170ce0 A |
7153 | lck_mtx_unlock(sadb_mutex); |
7154 | { | |
7155 | struct mbuf *n; | |
7156 | struct sadb_msg *newmsg; | |
7157 | int mbufItems[] = {SADB_EXT_RESERVED, SADB_EXT_SA, | |
0a7de745 A |
7158 | SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST, SADB_X_EXT_IPSECIF, |
7159 | SADB_EXT_MIGRATE_ADDRESS_SRC, SADB_EXT_MIGRATE_ADDRESS_DST, SADB_X_EXT_MIGRATE_IPSECIF}; | |
7160 | ||
3e170ce0 | 7161 | /* create new sadb_msg to reply. */ |
0a7de745 A |
7162 | n = key_gather_mbuf(m, mhp, 1, sizeof(mbufItems) / sizeof(int), mbufItems); |
7163 | if (!n) { | |
3e170ce0 | 7164 | return key_senderror(so, m, ENOBUFS); |
0a7de745 A |
7165 | } |
7166 | ||
3e170ce0 A |
7167 | if (n->m_len < sizeof(struct sadb_msg)) { |
7168 | n = m_pullup(n, sizeof(struct sadb_msg)); | |
0a7de745 | 7169 | if (n == NULL) { |
3e170ce0 | 7170 | return key_senderror(so, m, ENOBUFS); |
0a7de745 | 7171 | } |
3e170ce0 A |
7172 | } |
7173 | newmsg = mtod(n, struct sadb_msg *); | |
7174 | newmsg->sadb_msg_errno = 0; | |
f427ee49 A |
7175 | VERIFY(PFKEY_UNIT64(n->m_pkthdr.len) <= UINT16_MAX); |
7176 | newmsg->sadb_msg_len = (u_int16_t)PFKEY_UNIT64(n->m_pkthdr.len); | |
0a7de745 | 7177 | |
3e170ce0 A |
7178 | m_freem(m); |
7179 | return key_sendup_mbuf(so, n, KEY_SENDUP_ALL); | |
7180 | } | |
7181 | } | |
7182 | ||
1c79356b A |
7183 | /* |
7184 | * SADB_ADD processing | |
7185 | * add a entry to SA database, when received | |
9bccf70c | 7186 | * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),) |
1c79356b A |
7187 | * key(AE), (identity(SD),) (sensitivity)> |
7188 | * from the ikmpd, | |
7189 | * and send | |
9bccf70c | 7190 | * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),) |
1c79356b A |
7191 | * (identity(SD),) (sensitivity)> |
7192 | * to the ikmpd. | |
7193 | * | |
7194 | * IGNORE identity and sensitivity messages. | |
7195 | * | |
9bccf70c | 7196 | * m will always be freed. |
1c79356b | 7197 | */ |
9bccf70c | 7198 | static int |
6d2010ae | 7199 | key_add( |
0a7de745 A |
7200 | struct socket *so, |
7201 | struct mbuf *m, | |
7202 | const struct sadb_msghdr *mhp) | |
9bccf70c | 7203 | { |
f427ee49 A |
7204 | struct sadb_sa *sa0 = NULL; |
7205 | struct sadb_address *src0 = NULL, *dst0 = NULL; | |
fe8ab488 | 7206 | ifnet_t ipsec_if = NULL; |
1c79356b | 7207 | struct secasindex saidx; |
f427ee49 A |
7208 | struct secashead *newsah = NULL; |
7209 | struct secasvar *newsav = NULL; | |
7210 | u_int8_t proto; | |
9bccf70c A |
7211 | u_int8_t mode; |
7212 | u_int32_t reqid; | |
7213 | int error; | |
0a7de745 | 7214 | |
5ba3f43e | 7215 | LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_NOTOWNED); |
0a7de745 | 7216 | |
1c79356b | 7217 | /* sanity check */ |
0a7de745 | 7218 | if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL) { |
1c79356b | 7219 | panic("key_add: NULL pointer is passed.\n"); |
0a7de745 A |
7220 | } |
7221 | ||
1c79356b | 7222 | /* map satype to proto */ |
9bccf70c | 7223 | if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) { |
55e303ae | 7224 | ipseclog((LOG_DEBUG, "key_add: invalid satype is passed.\n")); |
ecc0ceb4 | 7225 | bzero_keys(mhp); |
9bccf70c | 7226 | return key_senderror(so, m, EINVAL); |
1c79356b | 7227 | } |
0a7de745 | 7228 | |
9bccf70c A |
7229 | if (mhp->ext[SADB_EXT_SA] == NULL || |
7230 | mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL || | |
7231 | mhp->ext[SADB_EXT_ADDRESS_DST] == NULL || | |
7232 | (mhp->msg->sadb_msg_satype == SADB_SATYPE_ESP && | |
0a7de745 | 7233 | mhp->ext[SADB_EXT_KEY_ENCRYPT] == NULL) || |
9bccf70c | 7234 | (mhp->msg->sadb_msg_satype == SADB_SATYPE_AH && |
0a7de745 | 7235 | mhp->ext[SADB_EXT_KEY_AUTH] == NULL) || |
9bccf70c | 7236 | (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL && |
0a7de745 | 7237 | mhp->ext[SADB_EXT_LIFETIME_SOFT] == NULL) || |
9bccf70c | 7238 | (mhp->ext[SADB_EXT_LIFETIME_HARD] == NULL && |
0a7de745 A |
7239 | mhp->ext[SADB_EXT_LIFETIME_SOFT] != NULL)) { |
7240 | ipseclog((LOG_DEBUG, "key_add: invalid message is passed.\n")); | |
7241 | bzero_keys(mhp); | |
7242 | return key_senderror(so, m, EINVAL); | |
7243 | } | |
9bccf70c A |
7244 | if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) || |
7245 | mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) || | |
7246 | mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) { | |
7247 | /* XXX need more */ | |
55e303ae | 7248 | ipseclog((LOG_DEBUG, "key_add: invalid message is passed.\n")); |
ecc0ceb4 | 7249 | bzero_keys(mhp); |
9bccf70c A |
7250 | return key_senderror(so, m, EINVAL); |
7251 | } | |
7252 | if (mhp->ext[SADB_X_EXT_SA2] != NULL) { | |
316670eb | 7253 | mode = ((struct sadb_x_sa2 *) |
0a7de745 | 7254 | (void *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode; |
316670eb | 7255 | reqid = ((struct sadb_x_sa2 *) |
0a7de745 | 7256 | (void *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid; |
9bccf70c A |
7257 | } else { |
7258 | mode = IPSEC_MODE_ANY; | |
7259 | reqid = 0; | |
1c79356b | 7260 | } |
0a7de745 | 7261 | |
316670eb | 7262 | sa0 = (struct sadb_sa *)(void *)mhp->ext[SADB_EXT_SA]; |
9bccf70c A |
7263 | src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC]; |
7264 | dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST]; | |
3e170ce0 | 7265 | ipsec_if = key_get_ipsec_if_from_message(mhp, SADB_X_EXT_IPSECIF); |
0a7de745 | 7266 | |
9bccf70c | 7267 | /* XXX boundary check against sa_len */ |
fe8ab488 | 7268 | KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, ipsec_if ? ipsec_if->if_index : 0, &saidx); |
0a7de745 | 7269 | |
2d21ac55 | 7270 | lck_mtx_lock(sadb_mutex); |
0a7de745 | 7271 | |
1c79356b | 7272 | /* get a SA header */ |
ea3f0419 | 7273 | if ((newsah = key_getsah(&saidx, SECURITY_ASSOCIATION_ANY)) == NULL) { |
b0d623f7 | 7274 | /* create a new SA header: key_addspi is always used for outbound spi */ |
ea3f0419 | 7275 | if ((newsah = key_newsah(&saidx, ipsec_if, key_get_outgoing_ifindex_from_message(mhp, SADB_X_EXT_IPSECIF), IPSEC_DIR_OUTBOUND, SECURITY_ASSOCIATION_PFKEY)) == NULL) { |
55e303ae | 7276 | ipseclog((LOG_DEBUG, "key_add: No more memory.\n")); |
f427ee49 A |
7277 | error = ENOBUFS; |
7278 | goto fail; | |
1c79356b A |
7279 | } |
7280 | } | |
0a7de745 | 7281 | |
f427ee49 A |
7282 | if (ipsec_if != NULL) { |
7283 | ifnet_release(ipsec_if); | |
7284 | ipsec_if = NULL; | |
ea3f0419 A |
7285 | } |
7286 | ||
f427ee49 A |
7287 | // Increment use count, since key_newsav() could release sadb_mutex lock |
7288 | newsah->use_count++; | |
7289 | ||
7290 | if ((newsah->flags & SECURITY_ASSOCIATION_CUSTOM_IPSEC) == SECURITY_ASSOCIATION_CUSTOM_IPSEC) { | |
7291 | ipseclog((LOG_ERR, "key_add: custom ipsec exists\n")); | |
7292 | error = EEXIST; | |
7293 | goto fail; | |
9bccf70c | 7294 | } |
0a7de745 | 7295 | |
1c79356b | 7296 | /* create new SA entry. */ |
2d21ac55 | 7297 | /* We can create new SA only if SPI is different. */ |
1c79356b | 7298 | if (key_getsavbyspi(newsah, sa0->sadb_sa_spi)) { |
55e303ae | 7299 | ipseclog((LOG_DEBUG, "key_add: SA already exists.\n")); |
f427ee49 A |
7300 | error = EEXIST; |
7301 | goto fail; | |
9bccf70c | 7302 | } |
fe8ab488 | 7303 | newsav = key_newsav(m, mhp, newsah, &error, so); |
9bccf70c | 7304 | if (newsav == NULL) { |
f427ee49 A |
7305 | goto fail; |
7306 | } | |
7307 | ||
7308 | if (newsah->state == SADB_SASTATE_DEAD) { | |
7309 | ipseclog((LOG_ERR, "key_add: security association head is dead\n")); | |
7310 | error = EINVAL; | |
7311 | goto fail; | |
1c79356b | 7312 | } |
0a7de745 | 7313 | |
2d21ac55 A |
7314 | /* |
7315 | * Verify if SADB_X_EXT_NATT_MULTIPLEUSERS flag is set that | |
7316 | * this SA is for transport mode - otherwise clear it. | |
7317 | */ | |
7318 | if ((newsav->flags & SADB_X_EXT_NATT_MULTIPLEUSERS) != 0 && | |
0a7de745 A |
7319 | (newsah->saidx.mode != IPSEC_MODE_TRANSPORT || |
7320 | newsah->saidx.dst.ss_family != AF_INET)) { | |
2d21ac55 | 7321 | newsav->flags &= ~SADB_X_EXT_NATT_MULTIPLEUSERS; |
0a7de745 A |
7322 | } |
7323 | ||
1c79356b | 7324 | /* check SA values to be mature. */ |
9bccf70c | 7325 | if ((error = key_mature(newsav)) != 0) { |
f427ee49 | 7326 | goto fail; |
1c79356b | 7327 | } |
0a7de745 | 7328 | |
f427ee49 | 7329 | newsah->use_count--; |
2d21ac55 | 7330 | lck_mtx_unlock(sadb_mutex); |
0a7de745 | 7331 | |
1c79356b A |
7332 | /* |
7333 | * don't call key_freesav() here, as we would like to keep the SA | |
7334 | * in the database on success. | |
7335 | */ | |
0a7de745 A |
7336 | |
7337 | { | |
39236c6e | 7338 | struct mbuf *n; |
0a7de745 | 7339 | |
39236c6e A |
7340 | /* set msg buf from mhp */ |
7341 | n = key_getmsgbuf_x1(m, mhp); | |
7342 | if (n == NULL) { | |
7343 | ipseclog((LOG_DEBUG, "key_update: No more memory.\n")); | |
ecc0ceb4 | 7344 | bzero_keys(mhp); |
39236c6e A |
7345 | return key_senderror(so, m, ENOBUFS); |
7346 | } | |
0a7de745 | 7347 | |
ecc0ceb4 A |
7348 | // mh.ext points to the mbuf content. |
7349 | // Zero out Encryption and Integrity keys if present. | |
7350 | bzero_keys(mhp); | |
39236c6e A |
7351 | m_freem(m); |
7352 | return key_sendup_mbuf(so, n, KEY_SENDUP_ALL); | |
0a7de745 | 7353 | } |
f427ee49 A |
7354 | fail: |
7355 | if (newsav != NULL) { | |
7356 | key_sa_chgstate(newsav, SADB_SASTATE_DEAD); | |
7357 | key_freesav(newsav, KEY_SADB_LOCKED); | |
1c79356b | 7358 | } |
f427ee49 A |
7359 | if (newsah != NULL) { |
7360 | newsah->use_count--; | |
1c79356b | 7361 | } |
f427ee49 A |
7362 | lck_mtx_unlock(sadb_mutex); |
7363 | if (ipsec_if != NULL) { | |
7364 | ifnet_release(ipsec_if); | |
1c79356b | 7365 | } |
f427ee49 A |
7366 | bzero_keys(mhp); |
7367 | return key_senderror(so, m, error); | |
1c79356b A |
7368 | } |
7369 | ||
9bccf70c A |
7370 | /* |
7371 | * m will not be freed on return. | |
39236c6e | 7372 | * it is caller's responsibility to free the result. |
9bccf70c A |
7373 | */ |
7374 | static struct mbuf * | |
6d2010ae | 7375 | key_getmsgbuf_x1( |
0a7de745 A |
7376 | struct mbuf *m, |
7377 | const struct sadb_msghdr *mhp) | |
1c79356b | 7378 | { |
9bccf70c A |
7379 | struct mbuf *n; |
7380 | int mbufItems[] = {SADB_EXT_RESERVED, SADB_EXT_SA, | |
0a7de745 A |
7381 | SADB_X_EXT_SA2, SADB_EXT_ADDRESS_SRC, |
7382 | SADB_EXT_ADDRESS_DST, SADB_EXT_LIFETIME_HARD, | |
7383 | SADB_EXT_LIFETIME_SOFT, SADB_EXT_IDENTITY_SRC, | |
7384 | SADB_EXT_IDENTITY_DST}; | |
7385 | ||
1c79356b | 7386 | /* sanity check */ |
0a7de745 | 7387 | if (m == NULL || mhp == NULL || mhp->msg == NULL) { |
1c79356b | 7388 | panic("key_getmsgbuf_x1: NULL pointer is passed.\n"); |
0a7de745 A |
7389 | } |
7390 | ||
1c79356b | 7391 | /* create new sadb_msg to reply. */ |
0a7de745 A |
7392 | n = key_gather_mbuf(m, mhp, 1, sizeof(mbufItems) / sizeof(int), mbufItems); |
7393 | if (!n) { | |
1c79356b | 7394 | return NULL; |
0a7de745 A |
7395 | } |
7396 | ||
9bccf70c A |
7397 | if (n->m_len < sizeof(struct sadb_msg)) { |
7398 | n = m_pullup(n, sizeof(struct sadb_msg)); | |
0a7de745 | 7399 | if (n == NULL) { |
9bccf70c | 7400 | return NULL; |
0a7de745 | 7401 | } |
9bccf70c A |
7402 | } |
7403 | mtod(n, struct sadb_msg *)->sadb_msg_errno = 0; | |
f427ee49 | 7404 | VERIFY(PFKEY_UNIT64(n->m_pkthdr.len) <= UINT16_MAX); |
9bccf70c | 7405 | mtod(n, struct sadb_msg *)->sadb_msg_len = |
f427ee49 | 7406 | (u_int16_t)PFKEY_UNIT64(n->m_pkthdr.len); |
0a7de745 | 7407 | |
9bccf70c | 7408 | return n; |
1c79356b A |
7409 | } |
7410 | ||
91447636 | 7411 | static int key_delete_all(struct socket *, struct mbuf *, |
0a7de745 | 7412 | const struct sadb_msghdr *, u_int16_t); |
9bccf70c | 7413 | |
1c79356b A |
7414 | /* |
7415 | * SADB_DELETE processing | |
7416 | * receive | |
7417 | * <base, SA(*), address(SD)> | |
7418 | * from the ikmpd, and set SADB_SASTATE_DEAD, | |
7419 | * and send, | |
7420 | * <base, SA(*), address(SD)> | |
7421 | * to the ikmpd. | |
7422 | * | |
9bccf70c | 7423 | * m will always be freed. |
1c79356b | 7424 | */ |
9bccf70c | 7425 | static int |
6d2010ae | 7426 | key_delete( |
0a7de745 A |
7427 | struct socket *so, |
7428 | struct mbuf *m, | |
7429 | const struct sadb_msghdr *mhp) | |
1c79356b | 7430 | { |
1c79356b A |
7431 | struct sadb_sa *sa0; |
7432 | struct sadb_address *src0, *dst0; | |
fe8ab488 | 7433 | ifnet_t ipsec_if = NULL; |
1c79356b A |
7434 | struct secasindex saidx; |
7435 | struct secashead *sah; | |
9bccf70c | 7436 | struct secasvar *sav = NULL; |
1c79356b | 7437 | u_int16_t proto; |
0a7de745 | 7438 | |
5ba3f43e | 7439 | LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_NOTOWNED); |
0a7de745 | 7440 | |
1c79356b | 7441 | /* sanity check */ |
0a7de745 | 7442 | if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL) { |
1c79356b | 7443 | panic("key_delete: NULL pointer is passed.\n"); |
0a7de745 A |
7444 | } |
7445 | ||
1c79356b | 7446 | /* map satype to proto */ |
9bccf70c | 7447 | if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) { |
55e303ae | 7448 | ipseclog((LOG_DEBUG, "key_delete: invalid satype is passed.\n")); |
9bccf70c | 7449 | return key_senderror(so, m, EINVAL); |
1c79356b | 7450 | } |
0a7de745 | 7451 | |
9bccf70c A |
7452 | if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL || |
7453 | mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) { | |
55e303ae | 7454 | ipseclog((LOG_DEBUG, "key_delete: invalid message is passed.\n")); |
9bccf70c | 7455 | return key_senderror(so, m, EINVAL); |
1c79356b | 7456 | } |
0a7de745 | 7457 | |
9bccf70c A |
7458 | if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) || |
7459 | mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) { | |
55e303ae | 7460 | ipseclog((LOG_DEBUG, "key_delete: invalid message is passed.\n")); |
9bccf70c A |
7461 | return key_senderror(so, m, EINVAL); |
7462 | } | |
0a7de745 | 7463 | |
2d21ac55 | 7464 | lck_mtx_lock(sadb_mutex); |
0a7de745 | 7465 | |
9bccf70c A |
7466 | if (mhp->ext[SADB_EXT_SA] == NULL) { |
7467 | /* | |
7468 | * Caller wants us to delete all non-LARVAL SAs | |
7469 | * that match the src/dst. This is used during | |
7470 | * IKE INITIAL-CONTACT. | |
7471 | */ | |
55e303ae | 7472 | ipseclog((LOG_DEBUG, "key_delete: doing delete all.\n")); |
2d21ac55 | 7473 | /* key_delete_all will unlock sadb_mutex */ |
39236c6e | 7474 | return key_delete_all(so, m, mhp, proto); |
9bccf70c | 7475 | } else if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa)) { |
2d21ac55 | 7476 | lck_mtx_unlock(sadb_mutex); |
55e303ae | 7477 | ipseclog((LOG_DEBUG, "key_delete: invalid message is passed.\n")); |
9bccf70c | 7478 | return key_senderror(so, m, EINVAL); |
1c79356b | 7479 | } |
0a7de745 | 7480 | |
316670eb | 7481 | sa0 = (struct sadb_sa *)(void *)mhp->ext[SADB_EXT_SA]; |
9bccf70c A |
7482 | src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]); |
7483 | dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]); | |
3e170ce0 | 7484 | ipsec_if = key_get_ipsec_if_from_message(mhp, SADB_X_EXT_IPSECIF); |
0a7de745 | 7485 | |
f427ee49 A |
7486 | u_int ipsec_if_index = 0; |
7487 | if (ipsec_if != NULL) { | |
7488 | ipsec_if_index = ipsec_if->if_index; | |
7489 | ifnet_release(ipsec_if); | |
7490 | ipsec_if = NULL; | |
7491 | } | |
7492 | ||
9bccf70c | 7493 | /* XXX boundary check against sa_len */ |
f427ee49 A |
7494 | KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, ipsec_if_index, &saidx); |
7495 | ||
0a7de745 | 7496 | |
9bccf70c A |
7497 | /* get a SA header */ |
7498 | LIST_FOREACH(sah, &sahtree, chain) { | |
0a7de745 | 7499 | if (sah->state == SADB_SASTATE_DEAD) { |
9bccf70c | 7500 | continue; |
0a7de745 A |
7501 | } |
7502 | if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0) { | |
9bccf70c | 7503 | continue; |
0a7de745 A |
7504 | } |
7505 | ||
9bccf70c A |
7506 | /* get a SA with SPI. */ |
7507 | sav = key_getsavbyspi(sah, sa0->sadb_sa_spi); | |
0a7de745 | 7508 | if (sav) { |
9bccf70c | 7509 | break; |
0a7de745 | 7510 | } |
9bccf70c A |
7511 | } |
7512 | if (sah == NULL) { | |
2d21ac55 | 7513 | lck_mtx_unlock(sadb_mutex); |
55e303ae | 7514 | ipseclog((LOG_DEBUG, "key_delete: no SA found.\n")); |
9bccf70c | 7515 | return key_senderror(so, m, ENOENT); |
1c79356b | 7516 | } |
0a7de745 | 7517 | |
1c79356b | 7518 | key_sa_chgstate(sav, SADB_SASTATE_DEAD); |
2d21ac55 | 7519 | key_freesav(sav, KEY_SADB_LOCKED); |
0a7de745 | 7520 | |
2d21ac55 | 7521 | lck_mtx_unlock(sadb_mutex); |
1c79356b | 7522 | sav = NULL; |
0a7de745 A |
7523 | |
7524 | { | |
39236c6e A |
7525 | struct mbuf *n; |
7526 | struct sadb_msg *newmsg; | |
7527 | int mbufItems[] = {SADB_EXT_RESERVED, SADB_EXT_SA, | |
0a7de745 A |
7528 | SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST}; |
7529 | ||
39236c6e | 7530 | /* create new sadb_msg to reply. */ |
0a7de745 A |
7531 | n = key_gather_mbuf(m, mhp, 1, sizeof(mbufItems) / sizeof(int), mbufItems); |
7532 | if (!n) { | |
9bccf70c | 7533 | return key_senderror(so, m, ENOBUFS); |
0a7de745 A |
7534 | } |
7535 | ||
39236c6e A |
7536 | if (n->m_len < sizeof(struct sadb_msg)) { |
7537 | n = m_pullup(n, sizeof(struct sadb_msg)); | |
0a7de745 | 7538 | if (n == NULL) { |
39236c6e | 7539 | return key_senderror(so, m, ENOBUFS); |
0a7de745 | 7540 | } |
39236c6e A |
7541 | } |
7542 | newmsg = mtod(n, struct sadb_msg *); | |
7543 | newmsg->sadb_msg_errno = 0; | |
f427ee49 A |
7544 | VERIFY(PFKEY_UNIT64(n->m_pkthdr.len) <= UINT16_MAX); |
7545 | newmsg->sadb_msg_len = (u_int16_t)PFKEY_UNIT64(n->m_pkthdr.len); | |
0a7de745 | 7546 | |
39236c6e A |
7547 | m_freem(m); |
7548 | return key_sendup_mbuf(so, n, KEY_SENDUP_ALL); | |
0a7de745 | 7549 | } |
9bccf70c A |
7550 | } |
7551 | ||
7552 | /* | |
7553 | * delete all SAs for src/dst. Called from key_delete(). | |
7554 | */ | |
7555 | static int | |
6d2010ae | 7556 | key_delete_all( |
0a7de745 A |
7557 | struct socket *so, |
7558 | struct mbuf *m, | |
7559 | const struct sadb_msghdr *mhp, | |
7560 | u_int16_t proto) | |
9bccf70c A |
7561 | { |
7562 | struct sadb_address *src0, *dst0; | |
fe8ab488 | 7563 | ifnet_t ipsec_if = NULL; |
9bccf70c A |
7564 | struct secasindex saidx; |
7565 | struct secashead *sah; | |
7566 | struct secasvar *sav, *nextsav; | |
7567 | u_int stateidx, state; | |
0a7de745 | 7568 | |
5ba3f43e | 7569 | LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_OWNED); |
0a7de745 | 7570 | |
9bccf70c A |
7571 | src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]); |
7572 | dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]); | |
3e170ce0 | 7573 | ipsec_if = key_get_ipsec_if_from_message(mhp, SADB_X_EXT_IPSECIF); |
0a7de745 | 7574 | |
f427ee49 A |
7575 | u_int ipsec_if_index = 0; |
7576 | if (ipsec_if != NULL) { | |
7577 | ipsec_if_index = ipsec_if->if_index; | |
7578 | ifnet_release(ipsec_if); | |
7579 | ipsec_if = NULL; | |
7580 | } | |
7581 | ||
9bccf70c | 7582 | /* XXX boundary check against sa_len */ |
f427ee49 | 7583 | KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, ipsec_if_index, &saidx); |
0a7de745 | 7584 | |
9bccf70c | 7585 | LIST_FOREACH(sah, &sahtree, chain) { |
0a7de745 | 7586 | if (sah->state == SADB_SASTATE_DEAD) { |
9bccf70c | 7587 | continue; |
0a7de745 A |
7588 | } |
7589 | if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0) { | |
9bccf70c | 7590 | continue; |
0a7de745 A |
7591 | } |
7592 | ||
9bccf70c A |
7593 | /* Delete all non-LARVAL SAs. */ |
7594 | for (stateidx = 0; | |
0a7de745 A |
7595 | stateidx < _ARRAYLEN(saorder_state_alive); |
7596 | stateidx++) { | |
9bccf70c | 7597 | state = saorder_state_alive[stateidx]; |
0a7de745 | 7598 | if (state == SADB_SASTATE_LARVAL) { |
9bccf70c | 7599 | continue; |
0a7de745 | 7600 | } |
9bccf70c | 7601 | for (sav = LIST_FIRST(&sah->savtree[state]); |
0a7de745 | 7602 | sav != NULL; sav = nextsav) { |
9bccf70c A |
7603 | nextsav = LIST_NEXT(sav, chain); |
7604 | /* sanity check */ | |
7605 | if (sav->state != state) { | |
55e303ae | 7606 | ipseclog((LOG_DEBUG, "key_delete_all: " |
0a7de745 A |
7607 | "invalid sav->state " |
7608 | "(queue: %d SA: %d)\n", | |
7609 | state, sav->state)); | |
9bccf70c A |
7610 | continue; |
7611 | } | |
0a7de745 | 7612 | |
9bccf70c | 7613 | key_sa_chgstate(sav, SADB_SASTATE_DEAD); |
2d21ac55 | 7614 | key_freesav(sav, KEY_SADB_LOCKED); |
9bccf70c A |
7615 | } |
7616 | } | |
1c79356b | 7617 | } |
2d21ac55 | 7618 | lck_mtx_unlock(sadb_mutex); |
0a7de745 A |
7619 | |
7620 | { | |
39236c6e A |
7621 | struct mbuf *n; |
7622 | struct sadb_msg *newmsg; | |
7623 | int mbufItems[] = {SADB_EXT_RESERVED, SADB_EXT_ADDRESS_SRC, | |
0a7de745 A |
7624 | SADB_EXT_ADDRESS_DST}; |
7625 | ||
39236c6e | 7626 | /* create new sadb_msg to reply. */ |
0a7de745 A |
7627 | n = key_gather_mbuf(m, mhp, 1, sizeof(mbufItems) / sizeof(int), mbufItems); |
7628 | if (!n) { | |
9bccf70c | 7629 | return key_senderror(so, m, ENOBUFS); |
0a7de745 A |
7630 | } |
7631 | ||
39236c6e A |
7632 | if (n->m_len < sizeof(struct sadb_msg)) { |
7633 | n = m_pullup(n, sizeof(struct sadb_msg)); | |
0a7de745 | 7634 | if (n == NULL) { |
39236c6e | 7635 | return key_senderror(so, m, ENOBUFS); |
0a7de745 | 7636 | } |
39236c6e A |
7637 | } |
7638 | newmsg = mtod(n, struct sadb_msg *); | |
7639 | newmsg->sadb_msg_errno = 0; | |
f427ee49 A |
7640 | VERIFY(PFKEY_UNIT64(n->m_pkthdr.len) <= UINT16_MAX); |
7641 | newmsg->sadb_msg_len = (u_int16_t)PFKEY_UNIT64(n->m_pkthdr.len); | |
0a7de745 | 7642 | |
39236c6e A |
7643 | m_freem(m); |
7644 | return key_sendup_mbuf(so, n, KEY_SENDUP_ALL); | |
0a7de745 | 7645 | } |
1c79356b A |
7646 | } |
7647 | ||
7648 | /* | |
7649 | * SADB_GET processing | |
7650 | * receive | |
7651 | * <base, SA(*), address(SD)> | |
7652 | * from the ikmpd, and get a SP and a SA to respond, | |
7653 | * and send, | |
7654 | * <base, SA, (lifetime(HSC),) address(SD), (address(P),) key(AE), | |
7655 | * (identity(SD),) (sensitivity)> | |
7656 | * to the ikmpd. | |
7657 | * | |
9bccf70c | 7658 | * m will always be freed. |
1c79356b | 7659 | */ |
9bccf70c | 7660 | static int |
6d2010ae | 7661 | key_get( |
0a7de745 A |
7662 | struct socket *so, |
7663 | struct mbuf *m, | |
7664 | const struct sadb_msghdr *mhp) | |
1c79356b | 7665 | { |
1c79356b A |
7666 | struct sadb_sa *sa0; |
7667 | struct sadb_address *src0, *dst0; | |
fe8ab488 | 7668 | ifnet_t ipsec_if = NULL; |
1c79356b A |
7669 | struct secasindex saidx; |
7670 | struct secashead *sah; | |
9bccf70c | 7671 | struct secasvar *sav = NULL; |
1c79356b | 7672 | u_int16_t proto; |
0a7de745 | 7673 | |
5ba3f43e | 7674 | LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_NOTOWNED); |
0a7de745 | 7675 | |
1c79356b | 7676 | /* sanity check */ |
0a7de745 | 7677 | if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL) { |
1c79356b | 7678 | panic("key_get: NULL pointer is passed.\n"); |
0a7de745 A |
7679 | } |
7680 | ||
1c79356b | 7681 | /* map satype to proto */ |
9bccf70c | 7682 | if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) { |
55e303ae | 7683 | ipseclog((LOG_DEBUG, "key_get: invalid satype is passed.\n")); |
9bccf70c | 7684 | return key_senderror(so, m, EINVAL); |
1c79356b | 7685 | } |
0a7de745 | 7686 | |
9bccf70c A |
7687 | if (mhp->ext[SADB_EXT_SA] == NULL || |
7688 | mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL || | |
7689 | mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) { | |
55e303ae | 7690 | ipseclog((LOG_DEBUG, "key_get: invalid message is passed.\n")); |
9bccf70c | 7691 | return key_senderror(so, m, EINVAL); |
1c79356b | 7692 | } |
9bccf70c A |
7693 | if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) || |
7694 | mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) || | |
7695 | mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) { | |
55e303ae | 7696 | ipseclog((LOG_DEBUG, "key_get: invalid message is passed.\n")); |
9bccf70c | 7697 | return key_senderror(so, m, EINVAL); |
1c79356b | 7698 | } |
0a7de745 | 7699 | |
316670eb | 7700 | sa0 = (struct sadb_sa *)(void *)mhp->ext[SADB_EXT_SA]; |
9bccf70c A |
7701 | src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC]; |
7702 | dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST]; | |
3e170ce0 | 7703 | ipsec_if = key_get_ipsec_if_from_message(mhp, SADB_X_EXT_IPSECIF); |
0a7de745 | 7704 | |
f427ee49 A |
7705 | u_int ipsec_if_index = 0; |
7706 | if (ipsec_if != NULL) { | |
7707 | ipsec_if_index = ipsec_if->if_index; | |
7708 | ifnet_release(ipsec_if); | |
7709 | ipsec_if = NULL; | |
7710 | } | |
7711 | ||
9bccf70c | 7712 | /* XXX boundary check against sa_len */ |
f427ee49 | 7713 | KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, ipsec_if_index, &saidx); |
0a7de745 | 7714 | |
2d21ac55 | 7715 | lck_mtx_lock(sadb_mutex); |
0a7de745 | 7716 | |
9bccf70c A |
7717 | /* get a SA header */ |
7718 | LIST_FOREACH(sah, &sahtree, chain) { | |
0a7de745 | 7719 | if (sah->state == SADB_SASTATE_DEAD) { |
9bccf70c | 7720 | continue; |
0a7de745 A |
7721 | } |
7722 | if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0) { | |
9bccf70c | 7723 | continue; |
0a7de745 A |
7724 | } |
7725 | ||
9bccf70c A |
7726 | /* get a SA with SPI. */ |
7727 | sav = key_getsavbyspi(sah, sa0->sadb_sa_spi); | |
0a7de745 | 7728 | if (sav) { |
9bccf70c | 7729 | break; |
0a7de745 | 7730 | } |
9bccf70c A |
7731 | } |
7732 | if (sah == NULL) { | |
2d21ac55 | 7733 | lck_mtx_unlock(sadb_mutex); |
55e303ae | 7734 | ipseclog((LOG_DEBUG, "key_get: no SA found.\n")); |
9bccf70c | 7735 | return key_senderror(so, m, ENOENT); |
1c79356b | 7736 | } |
0a7de745 A |
7737 | |
7738 | { | |
39236c6e A |
7739 | struct mbuf *n; |
7740 | u_int8_t satype; | |
0a7de745 | 7741 | |
39236c6e A |
7742 | /* map proto to satype */ |
7743 | if ((satype = key_proto2satype(sah->saidx.proto)) == 0) { | |
7744 | lck_mtx_unlock(sadb_mutex); | |
7745 | ipseclog((LOG_DEBUG, "key_get: there was invalid proto in SAD.\n")); | |
7746 | return key_senderror(so, m, EINVAL); | |
7747 | } | |
2d21ac55 | 7748 | lck_mtx_unlock(sadb_mutex); |
0a7de745 | 7749 | |
39236c6e A |
7750 | /* create new sadb_msg to reply. */ |
7751 | n = key_setdumpsa(sav, SADB_GET, satype, mhp->msg->sadb_msg_seq, | |
0a7de745 A |
7752 | mhp->msg->sadb_msg_pid); |
7753 | ||
7754 | ||
7755 | ||
7756 | if (!n) { | |
39236c6e | 7757 | return key_senderror(so, m, ENOBUFS); |
0a7de745 A |
7758 | } |
7759 | ||
39236c6e A |
7760 | m_freem(m); |
7761 | return key_sendup_mbuf(so, n, KEY_SENDUP_ONE); | |
0a7de745 | 7762 | } |
9bccf70c A |
7763 | } |
7764 | ||
b0d623f7 A |
7765 | /* |
7766 | * get SA stats by spi. | |
7767 | * OUT: -1 : not found | |
7768 | * 0 : found, arg pointer to a SA stats is updated. | |
7769 | */ | |
7770 | static int | |
0a7de745 A |
7771 | key_getsastatbyspi_one(u_int32_t spi, |
7772 | struct sastat *stat) | |
b0d623f7 A |
7773 | { |
7774 | struct secashead *sah; | |
7775 | struct secasvar *sav = NULL; | |
0a7de745 | 7776 | |
b0d623f7 | 7777 | if ((void *)stat == NULL) { |
39236c6e | 7778 | return -1; |
b0d623f7 | 7779 | } |
0a7de745 | 7780 | |
b0d623f7 | 7781 | lck_mtx_lock(sadb_mutex); |
0a7de745 | 7782 | |
b0d623f7 A |
7783 | /* get a SA header */ |
7784 | LIST_FOREACH(sah, &sahtree, chain) { | |
0a7de745 | 7785 | if (sah->state == SADB_SASTATE_DEAD) { |
b0d623f7 | 7786 | continue; |
0a7de745 A |
7787 | } |
7788 | ||
b0d623f7 A |
7789 | /* get a SA with SPI. */ |
7790 | sav = key_getsavbyspi(sah, spi); | |
7791 | if (sav) { | |
39236c6e | 7792 | stat->spi = sav->spi; |
f427ee49 | 7793 | stat->created = (u_int32_t)sav->created; |
b0d623f7 | 7794 | if (sav->lft_c) { |
0a7de745 | 7795 | bcopy(sav->lft_c, &stat->lft_c, sizeof(stat->lft_c)); |
b0d623f7 | 7796 | } else { |
39236c6e | 7797 | bzero(&stat->lft_c, sizeof(stat->lft_c)); |
b0d623f7 A |
7798 | } |
7799 | lck_mtx_unlock(sadb_mutex); | |
7800 | return 0; | |
7801 | } | |
7802 | } | |
0a7de745 | 7803 | |
b0d623f7 | 7804 | lck_mtx_unlock(sadb_mutex); |
0a7de745 | 7805 | |
b0d623f7 A |
7806 | return -1; |
7807 | } | |
7808 | ||
7809 | /* | |
7810 | * get SA stats collection by indices. | |
7811 | * OUT: -1 : not found | |
7812 | * 0 : found, arg pointers to a SA stats and 'maximum stats' are updated. | |
7813 | */ | |
7814 | static int | |
0a7de745 A |
7815 | key_getsastatbyspi(struct sastat *stat_arg, |
7816 | u_int32_t max_stat_arg, | |
7817 | struct sastat *stat_res, | |
f427ee49 | 7818 | u_int64_t stat_res_size, |
0a7de745 | 7819 | u_int32_t *max_stat_res) |
b0d623f7 | 7820 | { |
f427ee49 | 7821 | u_int32_t cur, found = 0; |
0a7de745 | 7822 | |
b0d623f7 | 7823 | if (stat_arg == NULL || |
39236c6e | 7824 | stat_res == NULL || |
b0d623f7 | 7825 | max_stat_res == NULL) { |
39236c6e | 7826 | return -1; |
b0d623f7 | 7827 | } |
b226f5e5 | 7828 | |
f427ee49 | 7829 | u_int64_t max_stats = stat_res_size / (sizeof(struct sastat)); |
b226f5e5 | 7830 | max_stats = ((max_stat_arg <= max_stats) ? max_stat_arg : max_stats); |
0a7de745 | 7831 | |
b226f5e5 | 7832 | for (cur = 0; cur < max_stats; cur++) { |
39236c6e | 7833 | if (key_getsastatbyspi_one(stat_arg[cur].spi, |
0a7de745 | 7834 | &stat_res[found]) == 0) { |
39236c6e | 7835 | found++; |
b0d623f7 A |
7836 | } |
7837 | } | |
7838 | *max_stat_res = found; | |
0a7de745 | 7839 | |
b0d623f7 | 7840 | if (found) { |
39236c6e | 7841 | return 0; |
b0d623f7 A |
7842 | } |
7843 | return -1; | |
7844 | } | |
7845 | ||
9bccf70c A |
7846 | /* XXX make it sysctl-configurable? */ |
7847 | static void | |
6d2010ae | 7848 | key_getcomb_setlifetime( |
0a7de745 | 7849 | struct sadb_comb *comb) |
9bccf70c | 7850 | { |
9bccf70c A |
7851 | comb->sadb_comb_soft_allocations = 1; |
7852 | comb->sadb_comb_hard_allocations = 1; | |
7853 | comb->sadb_comb_soft_bytes = 0; | |
7854 | comb->sadb_comb_hard_bytes = 0; | |
0a7de745 | 7855 | comb->sadb_comb_hard_addtime = 86400; /* 1 day */ |
9bccf70c | 7856 | comb->sadb_comb_soft_addtime = comb->sadb_comb_soft_addtime * 80 / 100; |
0a7de745 | 7857 | comb->sadb_comb_soft_usetime = 28800; /* 8 hours */ |
9bccf70c A |
7858 | comb->sadb_comb_hard_usetime = comb->sadb_comb_hard_usetime * 80 / 100; |
7859 | } | |
7860 | ||
7861 | #if IPSEC_ESP | |
7862 | /* | |
7863 | * XXX reorder combinations by preference | |
7864 | * XXX no idea if the user wants ESP authentication or not | |
7865 | */ | |
7866 | static struct mbuf * | |
6d2010ae | 7867 | key_getcomb_esp(void) |
9bccf70c A |
7868 | { |
7869 | struct sadb_comb *comb; | |
7870 | const struct esp_algorithm *algo; | |
7871 | struct mbuf *result = NULL, *m, *n; | |
f427ee49 A |
7872 | u_int16_t encmin; |
7873 | int off, o; | |
9bccf70c | 7874 | int totlen; |
f427ee49 | 7875 | u_int8_t i; |
9bccf70c | 7876 | const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb)); |
0a7de745 | 7877 | |
9bccf70c A |
7878 | m = NULL; |
7879 | for (i = 1; i <= SADB_EALG_MAX; i++) { | |
7880 | algo = esp_algorithm_lookup(i); | |
0a7de745 | 7881 | if (!algo) { |
9bccf70c | 7882 | continue; |
0a7de745 A |
7883 | } |
7884 | ||
7885 | if (algo->keymax < ipsec_esp_keymin) { | |
9bccf70c | 7886 | continue; |
0a7de745 A |
7887 | } |
7888 | if (algo->keymin < ipsec_esp_keymin) { | |
f427ee49 | 7889 | encmin = (u_int16_t)ipsec_esp_keymin; |
0a7de745 | 7890 | } else { |
9bccf70c | 7891 | encmin = algo->keymin; |
0a7de745 A |
7892 | } |
7893 | ||
7894 | if (ipsec_esp_auth) { | |
9bccf70c | 7895 | m = key_getcomb_ah(); |
0a7de745 | 7896 | } else { |
9bccf70c | 7897 | #if DIAGNOSTIC |
0a7de745 | 7898 | if (l > MLEN) { |
9bccf70c | 7899 | panic("assumption failed in key_getcomb_esp"); |
0a7de745 | 7900 | } |
9bccf70c | 7901 | #endif |
316670eb | 7902 | MGET(m, M_WAITOK, MT_DATA); |
9bccf70c A |
7903 | if (m) { |
7904 | M_ALIGN(m, l); | |
7905 | m->m_len = l; | |
7906 | m->m_next = NULL; | |
7907 | bzero(mtod(m, caddr_t), m->m_len); | |
7908 | } | |
7909 | } | |
0a7de745 | 7910 | if (!m) { |
9bccf70c | 7911 | goto fail; |
0a7de745 A |
7912 | } |
7913 | ||
9bccf70c | 7914 | totlen = 0; |
0a7de745 | 7915 | for (n = m; n; n = n->m_next) { |
9bccf70c | 7916 | totlen += n->m_len; |
0a7de745 | 7917 | } |
9bccf70c | 7918 | #if DIAGNOSTIC |
0a7de745 | 7919 | if (totlen % l) { |
9bccf70c | 7920 | panic("assumption failed in key_getcomb_esp"); |
0a7de745 | 7921 | } |
9bccf70c | 7922 | #endif |
0a7de745 | 7923 | |
9bccf70c A |
7924 | for (off = 0; off < totlen; off += l) { |
7925 | n = m_pulldown(m, off, l, &o); | |
7926 | if (!n) { | |
7927 | /* m is already freed */ | |
7928 | goto fail; | |
7929 | } | |
316670eb | 7930 | comb = (struct sadb_comb *) |
0a7de745 | 7931 | (void *)(mtod(n, caddr_t) + o); |
9bccf70c A |
7932 | bzero(comb, sizeof(*comb)); |
7933 | key_getcomb_setlifetime(comb); | |
7934 | comb->sadb_comb_encrypt = i; | |
7935 | comb->sadb_comb_encrypt_minbits = encmin; | |
7936 | comb->sadb_comb_encrypt_maxbits = algo->keymax; | |
7937 | } | |
0a7de745 A |
7938 | |
7939 | if (!result) { | |
9bccf70c | 7940 | result = m; |
0a7de745 | 7941 | } else { |
9bccf70c | 7942 | m_cat(result, m); |
0a7de745 | 7943 | } |
9bccf70c | 7944 | } |
0a7de745 | 7945 | |
9bccf70c | 7946 | return result; |
0a7de745 | 7947 | |
39236c6e | 7948 | fail: |
0a7de745 | 7949 | if (result) { |
9bccf70c | 7950 | m_freem(result); |
0a7de745 | 7951 | } |
9bccf70c A |
7952 | return NULL; |
7953 | } | |
1c79356b | 7954 | #endif |
9bccf70c A |
7955 | |
7956 | /* | |
7957 | * XXX reorder combinations by preference | |
7958 | */ | |
7959 | static struct mbuf * | |
6d2010ae | 7960 | key_getcomb_ah(void) |
9bccf70c A |
7961 | { |
7962 | struct sadb_comb *comb; | |
7963 | const struct ah_algorithm *algo; | |
7964 | struct mbuf *m; | |
f427ee49 A |
7965 | u_int16_t keymin; |
7966 | u_int8_t i; | |
9bccf70c | 7967 | const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb)); |
0a7de745 | 7968 | |
9bccf70c A |
7969 | m = NULL; |
7970 | for (i = 1; i <= SADB_AALG_MAX; i++) { | |
7971 | #if 1 | |
7972 | /* we prefer HMAC algorithms, not old algorithms */ | |
0a7de745 | 7973 | if (i != SADB_AALG_SHA1HMAC && i != SADB_AALG_MD5HMAC) { |
9bccf70c | 7974 | continue; |
0a7de745 | 7975 | } |
9bccf70c A |
7976 | #endif |
7977 | algo = ah_algorithm_lookup(i); | |
0a7de745 | 7978 | if (!algo) { |
9bccf70c | 7979 | continue; |
0a7de745 A |
7980 | } |
7981 | ||
7982 | if (algo->keymax < ipsec_ah_keymin) { | |
9bccf70c | 7983 | continue; |
0a7de745 A |
7984 | } |
7985 | if (algo->keymin < ipsec_ah_keymin) { | |
f427ee49 | 7986 | keymin = (u_int16_t)ipsec_ah_keymin; |
0a7de745 | 7987 | } else { |
2d21ac55 | 7988 | keymin = algo->keymin; |
0a7de745 A |
7989 | } |
7990 | ||
9bccf70c A |
7991 | if (!m) { |
7992 | #if DIAGNOSTIC | |
0a7de745 | 7993 | if (l > MLEN) { |
9bccf70c | 7994 | panic("assumption failed in key_getcomb_ah"); |
0a7de745 | 7995 | } |
9bccf70c | 7996 | #endif |
316670eb | 7997 | MGET(m, M_WAITOK, MT_DATA); |
9bccf70c A |
7998 | if (m) { |
7999 | M_ALIGN(m, l); | |
8000 | m->m_len = l; | |
8001 | m->m_next = NULL; | |
8002 | } | |
0a7de745 | 8003 | } else { |
3e170ce0 | 8004 | M_PREPEND(m, l, M_WAITOK, 1); |
0a7de745 A |
8005 | } |
8006 | if (!m) { | |
9bccf70c | 8007 | return NULL; |
0a7de745 A |
8008 | } |
8009 | ||
9bccf70c A |
8010 | comb = mtod(m, struct sadb_comb *); |
8011 | bzero(comb, sizeof(*comb)); | |
8012 | key_getcomb_setlifetime(comb); | |
8013 | comb->sadb_comb_auth = i; | |
2d21ac55 | 8014 | comb->sadb_comb_auth_minbits = keymin; |
9bccf70c A |
8015 | comb->sadb_comb_auth_maxbits = algo->keymax; |
8016 | } | |
0a7de745 | 8017 | |
9bccf70c A |
8018 | return m; |
8019 | } | |
8020 | ||
9bccf70c A |
8021 | /* |
8022 | * XXX no way to pass mode (transport/tunnel) to userland | |
8023 | * XXX replay checking? | |
8024 | * XXX sysctl interface to ipsec_{ah,esp}_keymin | |
8025 | */ | |
8026 | static struct mbuf * | |
6d2010ae | 8027 | key_getprop( |
0a7de745 | 8028 | const struct secasindex *saidx) |
9bccf70c A |
8029 | { |
8030 | struct sadb_prop *prop; | |
8031 | struct mbuf *m, *n; | |
8032 | const int l = PFKEY_ALIGN8(sizeof(struct sadb_prop)); | |
8033 | int totlen; | |
0a7de745 A |
8034 | |
8035 | switch (saidx->proto) { | |
9bccf70c | 8036 | #if IPSEC_ESP |
0a7de745 A |
8037 | case IPPROTO_ESP: |
8038 | m = key_getcomb_esp(); | |
8039 | break; | |
9bccf70c | 8040 | #endif |
0a7de745 A |
8041 | case IPPROTO_AH: |
8042 | m = key_getcomb_ah(); | |
8043 | break; | |
0a7de745 A |
8044 | default: |
8045 | return NULL; | |
1c79356b | 8046 | } |
0a7de745 A |
8047 | |
8048 | if (!m) { | |
9bccf70c | 8049 | return NULL; |
0a7de745 | 8050 | } |
3e170ce0 | 8051 | M_PREPEND(m, l, M_WAITOK, 1); |
0a7de745 | 8052 | if (!m) { |
9bccf70c | 8053 | return NULL; |
0a7de745 A |
8054 | } |
8055 | ||
9bccf70c | 8056 | totlen = 0; |
0a7de745 | 8057 | for (n = m; n; n = n->m_next) { |
9bccf70c | 8058 | totlen += n->m_len; |
0a7de745 A |
8059 | } |
8060 | ||
9bccf70c A |
8061 | prop = mtod(m, struct sadb_prop *); |
8062 | bzero(prop, sizeof(*prop)); | |
f427ee49 A |
8063 | VERIFY(totlen <= UINT16_MAX); |
8064 | prop->sadb_prop_len = (u_int16_t)PFKEY_UNIT64(totlen); | |
9bccf70c | 8065 | prop->sadb_prop_exttype = SADB_EXT_PROPOSAL; |
0a7de745 A |
8066 | prop->sadb_prop_replay = 32; /* XXX */ |
8067 | ||
9bccf70c | 8068 | return m; |
1c79356b A |
8069 | } |
8070 | ||
8071 | /* | |
8072 | * SADB_ACQUIRE processing called by key_checkrequest() and key_acquire2(). | |
8073 | * send | |
9bccf70c | 8074 | * <base, SA, address(SD), (address(P)), x_policy, |
1c79356b A |
8075 | * (identity(SD),) (sensitivity,) proposal> |
8076 | * to KMD, and expect to receive | |
91447636 | 8077 | * <base> with SADB_ACQUIRE if error occurred, |
1c79356b A |
8078 | * or |
8079 | * <base, src address, dst address, (SPI range)> with SADB_GETSPI | |
8080 | * from KMD by PF_KEY. | |
8081 | * | |
9bccf70c A |
8082 | * XXX x_policy is outside of RFC2367 (KAME extension). |
8083 | * XXX sensitivity is not supported. | |
1c79356b A |
8084 | * |
8085 | * OUT: | |
8086 | * 0 : succeed | |
8087 | * others: error number | |
8088 | */ | |
8089 | static int | |
6d2010ae | 8090 | key_acquire( |
0a7de745 A |
8091 | struct secasindex *saidx, |
8092 | struct secpolicy *sp) | |
1c79356b | 8093 | { |
9bccf70c | 8094 | struct mbuf *result = NULL, *m; |
1c79356b A |
8095 | #ifndef IPSEC_NONBLOCK_ACQUIRE |
8096 | struct secacq *newacq; | |
8097 | #endif | |
1c79356b | 8098 | u_int8_t satype; |
9bccf70c A |
8099 | int error = -1; |
8100 | u_int32_t seq; | |
0a7de745 | 8101 | |
5ba3f43e | 8102 | LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_NOTOWNED); |
0a7de745 | 8103 | |
1c79356b | 8104 | /* sanity check */ |
0a7de745 | 8105 | if (saidx == NULL) { |
1c79356b | 8106 | panic("key_acquire: NULL pointer is passed.\n"); |
0a7de745 A |
8107 | } |
8108 | if ((satype = key_proto2satype(saidx->proto)) == 0) { | |
1c79356b | 8109 | panic("key_acquire: invalid proto is passed.\n"); |
0a7de745 A |
8110 | } |
8111 | ||
1c79356b A |
8112 | #ifndef IPSEC_NONBLOCK_ACQUIRE |
8113 | /* | |
8114 | * We never do anything about acquirng SA. There is anather | |
8115 | * solution that kernel blocks to send SADB_ACQUIRE message until | |
8116 | * getting something message from IKEd. In later case, to be | |
8117 | * managed with ACQUIRING list. | |
8118 | */ | |
8119 | /* get a entry to check whether sending message or not. */ | |
2d21ac55 | 8120 | lck_mtx_lock(sadb_mutex); |
1c79356b A |
8121 | if ((newacq = key_getacq(saidx)) != NULL) { |
8122 | if (key_blockacq_count < newacq->count) { | |
8123 | /* reset counter and do send message. */ | |
8124 | newacq->count = 0; | |
8125 | } else { | |
8126 | /* increment counter and do nothing. */ | |
8127 | newacq->count++; | |
2d21ac55 | 8128 | lck_mtx_unlock(sadb_mutex); |
1c79356b A |
8129 | return 0; |
8130 | } | |
8131 | } else { | |
8132 | /* make new entry for blocking to send SADB_ACQUIRE. */ | |
2d21ac55 A |
8133 | if ((newacq = key_newacq(saidx)) == NULL) { |
8134 | lck_mtx_unlock(sadb_mutex); | |
1c79356b | 8135 | return ENOBUFS; |
2d21ac55 | 8136 | } |
0a7de745 | 8137 | |
1c79356b A |
8138 | /* add to acqtree */ |
8139 | LIST_INSERT_HEAD(&acqtree, newacq, chain); | |
39236c6e | 8140 | key_start_timehandler(); |
1c79356b | 8141 | } |
9bccf70c | 8142 | seq = newacq->seq; |
2d21ac55 | 8143 | lck_mtx_unlock(sadb_mutex); |
0a7de745 | 8144 | |
1c79356b | 8145 | #else |
9bccf70c | 8146 | seq = (acq_seq = (acq_seq == ~0 ? 1 : ++acq_seq)); |
1c79356b | 8147 | #endif |
9bccf70c A |
8148 | m = key_setsadbmsg(SADB_ACQUIRE, 0, satype, seq, 0, 0); |
8149 | if (!m) { | |
8150 | error = ENOBUFS; | |
8151 | goto fail; | |
8152 | } | |
8153 | result = m; | |
0a7de745 | 8154 | |
1c79356b | 8155 | /* set sadb_address for saidx's. */ |
9bccf70c | 8156 | m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC, |
0a7de745 | 8157 | (struct sockaddr *)&saidx->src, FULLMASK, IPSEC_ULPROTO_ANY); |
9bccf70c A |
8158 | if (!m) { |
8159 | error = ENOBUFS; | |
8160 | goto fail; | |
8161 | } | |
8162 | m_cat(result, m); | |
0a7de745 | 8163 | |
9bccf70c | 8164 | m = key_setsadbaddr(SADB_EXT_ADDRESS_DST, |
0a7de745 | 8165 | (struct sockaddr *)&saidx->dst, FULLMASK, IPSEC_ULPROTO_ANY); |
9bccf70c A |
8166 | if (!m) { |
8167 | error = ENOBUFS; | |
8168 | goto fail; | |
8169 | } | |
8170 | m_cat(result, m); | |
0a7de745 | 8171 | |
9bccf70c | 8172 | /* XXX proxy address (optional) */ |
0a7de745 | 8173 | |
9bccf70c A |
8174 | /* set sadb_x_policy */ |
8175 | if (sp) { | |
f427ee49 | 8176 | m = key_setsadbxpolicy((u_int16_t)sp->policy, sp->spidx.dir, sp->id); |
9bccf70c A |
8177 | if (!m) { |
8178 | error = ENOBUFS; | |
8179 | goto fail; | |
8180 | } | |
8181 | m_cat(result, m); | |
8182 | } | |
0a7de745 | 8183 | |
9bccf70c | 8184 | /* XXX sensitivity (optional) */ |
0a7de745 | 8185 | |
9bccf70c A |
8186 | /* create proposal/combination extension */ |
8187 | m = key_getprop(saidx); | |
9bccf70c A |
8188 | /* |
8189 | * outside of spec; make proposal/combination extension optional. | |
8190 | */ | |
0a7de745 | 8191 | if (m) { |
9bccf70c | 8192 | m_cat(result, m); |
0a7de745 | 8193 | } |
0a7de745 | 8194 | |
9bccf70c A |
8195 | if ((result->m_flags & M_PKTHDR) == 0) { |
8196 | error = EINVAL; | |
8197 | goto fail; | |
8198 | } | |
0a7de745 | 8199 | |
9bccf70c A |
8200 | if (result->m_len < sizeof(struct sadb_msg)) { |
8201 | result = m_pullup(result, sizeof(struct sadb_msg)); | |
8202 | if (result == NULL) { | |
8203 | error = ENOBUFS; | |
8204 | goto fail; | |
8205 | } | |
8206 | } | |
0a7de745 | 8207 | |
9bccf70c | 8208 | result->m_pkthdr.len = 0; |
0a7de745 | 8209 | for (m = result; m; m = m->m_next) { |
9bccf70c | 8210 | result->m_pkthdr.len += m->m_len; |
0a7de745 A |
8211 | } |
8212 | ||
f427ee49 | 8213 | VERIFY(PFKEY_UNIT64(result->m_pkthdr.len) <= UINT16_MAX); |
9bccf70c | 8214 | mtod(result, struct sadb_msg *)->sadb_msg_len = |
f427ee49 | 8215 | (u_int16_t)PFKEY_UNIT64(result->m_pkthdr.len); |
0a7de745 | 8216 | |
9bccf70c | 8217 | return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED); |
0a7de745 | 8218 | |
39236c6e | 8219 | fail: |
0a7de745 | 8220 | if (result) { |
9bccf70c | 8221 | m_freem(result); |
0a7de745 | 8222 | } |
9bccf70c | 8223 | return error; |
1c79356b A |
8224 | } |
8225 | ||
8226 | #ifndef IPSEC_NONBLOCK_ACQUIRE | |
8227 | static struct secacq * | |
6d2010ae | 8228 | key_newacq( |
0a7de745 | 8229 | struct secasindex *saidx) |
1c79356b A |
8230 | { |
8231 | struct secacq *newacq; | |
9bccf70c | 8232 | struct timeval tv; |
0a7de745 | 8233 | |
1c79356b | 8234 | /* get new entry */ |
2d21ac55 | 8235 | KMALLOC_NOWAIT(newacq, struct secacq *, sizeof(struct secacq)); |
1c79356b | 8236 | if (newacq == NULL) { |
2d21ac55 A |
8237 | lck_mtx_unlock(sadb_mutex); |
8238 | KMALLOC_WAIT(newacq, struct secacq *, sizeof(struct secacq)); | |
8239 | lck_mtx_lock(sadb_mutex); | |
39236c6e | 8240 | if (newacq == NULL) { |
2d21ac55 A |
8241 | ipseclog((LOG_DEBUG, "key_newacq: No more memory.\n")); |
8242 | return NULL; | |
8243 | } | |
1c79356b A |
8244 | } |
8245 | bzero(newacq, sizeof(*newacq)); | |
0a7de745 | 8246 | |
1c79356b A |
8247 | /* copy secindex */ |
8248 | bcopy(saidx, &newacq->saidx, sizeof(newacq->saidx)); | |
8249 | newacq->seq = (acq_seq == ~0 ? 1 : ++acq_seq); | |
9bccf70c A |
8250 | microtime(&tv); |
8251 | newacq->created = tv.tv_sec; | |
1c79356b | 8252 | newacq->count = 0; |
0a7de745 | 8253 | |
1c79356b A |
8254 | return newacq; |
8255 | } | |
8256 | ||
8257 | static struct secacq * | |
6d2010ae | 8258 | key_getacq( |
0a7de745 | 8259 | struct secasindex *saidx) |
1c79356b A |
8260 | { |
8261 | struct secacq *acq; | |
0a7de745 | 8262 | |
5ba3f43e | 8263 | LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_OWNED); |
0a7de745 | 8264 | |
1c79356b | 8265 | LIST_FOREACH(acq, &acqtree, chain) { |
0a7de745 | 8266 | if (key_cmpsaidx(saidx, &acq->saidx, CMP_EXACTLY)) { |
1c79356b | 8267 | return acq; |
0a7de745 | 8268 | } |
1c79356b | 8269 | } |
0a7de745 | 8270 | |
1c79356b A |
8271 | return NULL; |
8272 | } | |
8273 | ||
8274 | static struct secacq * | |
6d2010ae | 8275 | key_getacqbyseq( |
0a7de745 | 8276 | u_int32_t seq) |
1c79356b A |
8277 | { |
8278 | struct secacq *acq; | |
0a7de745 | 8279 | |
5ba3f43e | 8280 | LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_OWNED); |
0a7de745 | 8281 | |
1c79356b | 8282 | LIST_FOREACH(acq, &acqtree, chain) { |
0a7de745 | 8283 | if (acq->seq == seq) { |
1c79356b | 8284 | return acq; |
0a7de745 | 8285 | } |
1c79356b | 8286 | } |
0a7de745 | 8287 | |
1c79356b A |
8288 | return NULL; |
8289 | } | |
8290 | #endif | |
8291 | ||
8292 | static struct secspacq * | |
6d2010ae | 8293 | key_newspacq( |
0a7de745 | 8294 | struct secpolicyindex *spidx) |
1c79356b A |
8295 | { |
8296 | struct secspacq *acq; | |
9bccf70c | 8297 | struct timeval tv; |
0a7de745 | 8298 | |
1c79356b | 8299 | /* get new entry */ |
2d21ac55 | 8300 | KMALLOC_NOWAIT(acq, struct secspacq *, sizeof(struct secspacq)); |
1c79356b | 8301 | if (acq == NULL) { |
2d21ac55 A |
8302 | lck_mtx_unlock(sadb_mutex); |
8303 | KMALLOC_WAIT(acq, struct secspacq *, sizeof(struct secspacq)); | |
8304 | lck_mtx_lock(sadb_mutex); | |
8305 | if (acq == NULL) { | |
8306 | ipseclog((LOG_DEBUG, "key_newspacq: No more memory.\n")); | |
8307 | return NULL; | |
8308 | } | |
1c79356b A |
8309 | } |
8310 | bzero(acq, sizeof(*acq)); | |
0a7de745 | 8311 | |
1c79356b A |
8312 | /* copy secindex */ |
8313 | bcopy(spidx, &acq->spidx, sizeof(acq->spidx)); | |
9bccf70c A |
8314 | microtime(&tv); |
8315 | acq->created = tv.tv_sec; | |
1c79356b | 8316 | acq->count = 0; |
0a7de745 | 8317 | |
1c79356b A |
8318 | return acq; |
8319 | } | |
8320 | ||
8321 | static struct secspacq * | |
6d2010ae | 8322 | key_getspacq( |
0a7de745 | 8323 | struct secpolicyindex *spidx) |
1c79356b A |
8324 | { |
8325 | struct secspacq *acq; | |
0a7de745 | 8326 | |
5ba3f43e | 8327 | LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_OWNED); |
0a7de745 | 8328 | |
1c79356b | 8329 | LIST_FOREACH(acq, &spacqtree, chain) { |
0a7de745 | 8330 | if (key_cmpspidx_exactly(spidx, &acq->spidx)) { |
1c79356b | 8331 | return acq; |
0a7de745 | 8332 | } |
1c79356b | 8333 | } |
0a7de745 | 8334 | |
1c79356b A |
8335 | return NULL; |
8336 | } | |
8337 | ||
8338 | /* | |
8339 | * SADB_ACQUIRE processing, | |
8340 | * in first situation, is receiving | |
8341 | * <base> | |
8342 | * from the ikmpd, and clear sequence of its secasvar entry. | |
8343 | * | |
8344 | * In second situation, is receiving | |
8345 | * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal> | |
8346 | * from a user land process, and return | |
8347 | * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal> | |
8348 | * to the socket. | |
8349 | * | |
9bccf70c | 8350 | * m will always be freed. |
1c79356b | 8351 | */ |
9bccf70c | 8352 | static int |
6d2010ae | 8353 | key_acquire2( |
0a7de745 A |
8354 | struct socket *so, |
8355 | struct mbuf *m, | |
8356 | const struct sadb_msghdr *mhp) | |
1c79356b | 8357 | { |
9bccf70c | 8358 | const struct sadb_address *src0, *dst0; |
fe8ab488 | 8359 | ifnet_t ipsec_if = NULL; |
1c79356b A |
8360 | struct secasindex saidx; |
8361 | struct secashead *sah; | |
8362 | u_int16_t proto; | |
9bccf70c | 8363 | int error; |
0a7de745 A |
8364 | |
8365 | ||
1c79356b | 8366 | /* sanity check */ |
0a7de745 | 8367 | if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL) { |
1c79356b | 8368 | panic("key_acquire2: NULL pointer is passed.\n"); |
0a7de745 A |
8369 | } |
8370 | ||
1c79356b A |
8371 | /* |
8372 | * Error message from KMd. | |
91447636 | 8373 | * We assume that if error was occurred in IKEd, the length of PFKEY |
1c79356b | 8374 | * message is equal to the size of sadb_msg structure. |
91447636 | 8375 | * We do not raise error even if error occurred in this function. |
1c79356b | 8376 | */ |
39236c6e | 8377 | lck_mtx_lock(sadb_mutex); |
0a7de745 | 8378 | |
9bccf70c | 8379 | if (mhp->msg->sadb_msg_len == PFKEY_UNIT64(sizeof(struct sadb_msg))) { |
1c79356b A |
8380 | #ifndef IPSEC_NONBLOCK_ACQUIRE |
8381 | struct secacq *acq; | |
9bccf70c | 8382 | struct timeval tv; |
0a7de745 | 8383 | |
1c79356b | 8384 | /* check sequence number */ |
9bccf70c | 8385 | if (mhp->msg->sadb_msg_seq == 0) { |
2d21ac55 | 8386 | lck_mtx_unlock(sadb_mutex); |
55e303ae | 8387 | ipseclog((LOG_DEBUG, "key_acquire2: must specify sequence number.\n")); |
9bccf70c A |
8388 | m_freem(m); |
8389 | return 0; | |
1c79356b | 8390 | } |
0a7de745 | 8391 | |
9bccf70c A |
8392 | if ((acq = key_getacqbyseq(mhp->msg->sadb_msg_seq)) == NULL) { |
8393 | /* | |
8394 | * the specified larval SA is already gone, or we got | |
8395 | * a bogus sequence number. we can silently ignore it. | |
8396 | */ | |
2d21ac55 | 8397 | lck_mtx_unlock(sadb_mutex); |
9bccf70c A |
8398 | m_freem(m); |
8399 | return 0; | |
1c79356b | 8400 | } |
0a7de745 | 8401 | |
1c79356b | 8402 | /* reset acq counter in order to deletion by timehander. */ |
9bccf70c A |
8403 | microtime(&tv); |
8404 | acq->created = tv.tv_sec; | |
1c79356b A |
8405 | acq->count = 0; |
8406 | #endif | |
2d21ac55 | 8407 | lck_mtx_unlock(sadb_mutex); |
9bccf70c A |
8408 | m_freem(m); |
8409 | return 0; | |
1c79356b | 8410 | } |
0a7de745 | 8411 | |
1c79356b A |
8412 | /* |
8413 | * This message is from user land. | |
8414 | */ | |
0a7de745 | 8415 | |
1c79356b | 8416 | /* map satype to proto */ |
9bccf70c | 8417 | if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) { |
2d21ac55 | 8418 | lck_mtx_unlock(sadb_mutex); |
55e303ae | 8419 | ipseclog((LOG_DEBUG, "key_acquire2: invalid satype is passed.\n")); |
9bccf70c | 8420 | return key_senderror(so, m, EINVAL); |
1c79356b | 8421 | } |
0a7de745 | 8422 | |
9bccf70c A |
8423 | if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL || |
8424 | mhp->ext[SADB_EXT_ADDRESS_DST] == NULL || | |
8425 | mhp->ext[SADB_EXT_PROPOSAL] == NULL) { | |
1c79356b | 8426 | /* error */ |
2d21ac55 | 8427 | lck_mtx_unlock(sadb_mutex); |
55e303ae | 8428 | ipseclog((LOG_DEBUG, "key_acquire2: invalid message is passed.\n")); |
9bccf70c | 8429 | return key_senderror(so, m, EINVAL); |
1c79356b | 8430 | } |
9bccf70c A |
8431 | if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) || |
8432 | mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) || | |
8433 | mhp->extlen[SADB_EXT_PROPOSAL] < sizeof(struct sadb_prop)) { | |
8434 | /* error */ | |
2d21ac55 | 8435 | lck_mtx_unlock(sadb_mutex); |
55e303ae | 8436 | ipseclog((LOG_DEBUG, "key_acquire2: invalid message is passed.\n")); |
9bccf70c A |
8437 | return key_senderror(so, m, EINVAL); |
8438 | } | |
0a7de745 | 8439 | |
b0d623f7 A |
8440 | src0 = (const struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC]; |
8441 | dst0 = (const struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST]; | |
3e170ce0 | 8442 | ipsec_if = key_get_ipsec_if_from_message(mhp, SADB_X_EXT_IPSECIF); |
0a7de745 | 8443 | |
f427ee49 A |
8444 | u_int ipsec_if_index = 0; |
8445 | if (ipsec_if != NULL) { | |
8446 | ipsec_if_index = ipsec_if->if_index; | |
8447 | ifnet_release(ipsec_if); | |
8448 | ipsec_if = NULL; | |
8449 | } | |
8450 | ||
9bccf70c | 8451 | /* XXX boundary check against sa_len */ |
b0d623f7 | 8452 | /* cast warnings */ |
f427ee49 | 8453 | KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, ipsec_if_index, &saidx); |
0a7de745 | 8454 | |
1c79356b | 8455 | /* get a SA index */ |
9bccf70c | 8456 | LIST_FOREACH(sah, &sahtree, chain) { |
0a7de745 | 8457 | if (sah->state == SADB_SASTATE_DEAD) { |
9bccf70c | 8458 | continue; |
0a7de745 A |
8459 | } |
8460 | if (key_cmpsaidx(&sah->saidx, &saidx, CMP_MODE | CMP_REQID)) { | |
9bccf70c | 8461 | break; |
0a7de745 | 8462 | } |
9bccf70c A |
8463 | } |
8464 | if (sah != NULL) { | |
2d21ac55 | 8465 | lck_mtx_unlock(sadb_mutex); |
55e303ae | 8466 | ipseclog((LOG_DEBUG, "key_acquire2: a SA exists already.\n")); |
9bccf70c | 8467 | return key_senderror(so, m, EEXIST); |
1c79356b | 8468 | } |
2d21ac55 | 8469 | lck_mtx_unlock(sadb_mutex); |
9bccf70c A |
8470 | error = key_acquire(&saidx, NULL); |
8471 | if (error != 0) { | |
55e303ae | 8472 | ipseclog((LOG_DEBUG, "key_acquire2: error %d returned " |
0a7de745 | 8473 | "from key_acquire.\n", mhp->msg->sadb_msg_errno)); |
9bccf70c | 8474 | return key_senderror(so, m, error); |
1c79356b | 8475 | } |
0a7de745 | 8476 | |
9bccf70c | 8477 | return key_sendup_mbuf(so, m, KEY_SENDUP_REGISTERED); |
1c79356b A |
8478 | } |
8479 | ||
8480 | /* | |
8481 | * SADB_REGISTER processing. | |
2d21ac55 | 8482 | * If SATYPE_UNSPEC has been passed as satype, only return sadb_supported. |
1c79356b A |
8483 | * receive |
8484 | * <base> | |
8485 | * from the ikmpd, and register a socket to send PF_KEY messages, | |
8486 | * and send | |
8487 | * <base, supported> | |
8488 | * to KMD by PF_KEY. | |
8489 | * If socket is detached, must free from regnode. | |
9bccf70c A |
8490 | * |
8491 | * m will always be freed. | |
1c79356b | 8492 | */ |
9bccf70c | 8493 | static int |
6d2010ae | 8494 | key_register( |
0a7de745 A |
8495 | struct socket *so, |
8496 | struct mbuf *m, | |
8497 | const struct sadb_msghdr *mhp) | |
1c79356b | 8498 | { |
1c79356b | 8499 | struct secreg *reg, *newreg = 0; |
0a7de745 | 8500 | |
1c79356b | 8501 | /* sanity check */ |
0a7de745 | 8502 | if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL) { |
1c79356b | 8503 | panic("key_register: NULL pointer is passed.\n"); |
0a7de745 A |
8504 | } |
8505 | ||
1c79356b | 8506 | /* check for invalid register message */ |
0a7de745 | 8507 | if (mhp->msg->sadb_msg_satype >= sizeof(regtree) / sizeof(regtree[0])) { |
9bccf70c | 8508 | return key_senderror(so, m, EINVAL); |
0a7de745 A |
8509 | } |
8510 | ||
2d21ac55 | 8511 | /* When SATYPE_UNSPEC is specified, only return sadb_supported. */ |
0a7de745 | 8512 | if (mhp->msg->sadb_msg_satype == SADB_SATYPE_UNSPEC) { |
1c79356b | 8513 | goto setmsg; |
0a7de745 A |
8514 | } |
8515 | ||
2d21ac55 A |
8516 | /* create regnode */ |
8517 | KMALLOC_WAIT(newreg, struct secreg *, sizeof(*newreg)); | |
39236c6e A |
8518 | if (newreg == NULL) { |
8519 | ipseclog((LOG_DEBUG, "key_register: No more memory.\n")); | |
8520 | return key_senderror(so, m, ENOBUFS); | |
8521 | } | |
8522 | bzero((caddr_t)newreg, sizeof(*newreg)); | |
0a7de745 | 8523 | |
2d21ac55 | 8524 | lck_mtx_lock(sadb_mutex); |
1c79356b | 8525 | /* check whether existing or not */ |
9bccf70c | 8526 | LIST_FOREACH(reg, ®tree[mhp->msg->sadb_msg_satype], chain) { |
1c79356b | 8527 | if (reg->so == so) { |
2d21ac55 | 8528 | lck_mtx_unlock(sadb_mutex); |
55e303ae | 8529 | ipseclog((LOG_DEBUG, "key_register: socket exists already.\n")); |
2d21ac55 | 8530 | KFREE(newreg); |
9bccf70c | 8531 | return key_senderror(so, m, EEXIST); |
1c79356b A |
8532 | } |
8533 | } | |
0a7de745 | 8534 | |
91447636 | 8535 | socket_lock(so, 1); |
1c79356b A |
8536 | newreg->so = so; |
8537 | ((struct keycb *)sotorawcb(so))->kp_registered++; | |
91447636 | 8538 | socket_unlock(so, 1); |
0a7de745 | 8539 | |
1c79356b | 8540 | /* add regnode to regtree. */ |
9bccf70c | 8541 | LIST_INSERT_HEAD(®tree[mhp->msg->sadb_msg_satype], newreg, chain); |
2d21ac55 | 8542 | lck_mtx_unlock(sadb_mutex); |
39236c6e | 8543 | setmsg: |
0a7de745 | 8544 | { |
39236c6e A |
8545 | struct mbuf *n; |
8546 | struct sadb_msg *newmsg; | |
8547 | struct sadb_supported *sup; | |
f427ee49 | 8548 | u_int16_t len, alen, elen; |
39236c6e | 8549 | int off; |
f427ee49 | 8550 | u_int8_t i; |
39236c6e | 8551 | struct sadb_alg *alg; |
0a7de745 | 8552 | |
39236c6e A |
8553 | /* create new sadb_msg to reply. */ |
8554 | alen = 0; | |
8555 | for (i = 1; i <= SADB_AALG_MAX; i++) { | |
0a7de745 | 8556 | if (ah_algorithm_lookup(i)) { |
39236c6e | 8557 | alen += sizeof(struct sadb_alg); |
0a7de745 | 8558 | } |
39236c6e | 8559 | } |
0a7de745 | 8560 | if (alen) { |
39236c6e | 8561 | alen += sizeof(struct sadb_supported); |
0a7de745 | 8562 | } |
39236c6e | 8563 | elen = 0; |
9bccf70c | 8564 | #if IPSEC_ESP |
39236c6e | 8565 | for (i = 1; i <= SADB_EALG_MAX; i++) { |
0a7de745 | 8566 | if (esp_algorithm_lookup(i)) { |
39236c6e | 8567 | elen += sizeof(struct sadb_alg); |
0a7de745 | 8568 | } |
39236c6e | 8569 | } |
0a7de745 | 8570 | if (elen) { |
39236c6e | 8571 | elen += sizeof(struct sadb_supported); |
0a7de745 | 8572 | } |
1c79356b | 8573 | #endif |
0a7de745 | 8574 | |
39236c6e | 8575 | len = sizeof(struct sadb_msg) + alen + elen; |
0a7de745 A |
8576 | |
8577 | if (len > MCLBYTES) { | |
39236c6e | 8578 | return key_senderror(so, m, ENOBUFS); |
0a7de745 A |
8579 | } |
8580 | ||
39236c6e A |
8581 | MGETHDR(n, M_WAITOK, MT_DATA); |
8582 | if (n && len > MHLEN) { | |
8583 | MCLGET(n, M_WAITOK); | |
8584 | if ((n->m_flags & M_EXT) == 0) { | |
8585 | m_freem(n); | |
8586 | n = NULL; | |
8587 | } | |
9bccf70c | 8588 | } |
0a7de745 | 8589 | if (!n) { |
39236c6e | 8590 | return key_senderror(so, m, ENOBUFS); |
0a7de745 A |
8591 | } |
8592 | ||
39236c6e A |
8593 | n->m_pkthdr.len = n->m_len = len; |
8594 | n->m_next = NULL; | |
8595 | off = 0; | |
0a7de745 | 8596 | |
39236c6e A |
8597 | m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off); |
8598 | newmsg = mtod(n, struct sadb_msg *); | |
8599 | newmsg->sadb_msg_errno = 0; | |
f427ee49 A |
8600 | VERIFY(PFKEY_UNIT64(len) <= UINT16_MAX); |
8601 | newmsg->sadb_msg_len = (u_int16_t)PFKEY_UNIT64(len); | |
39236c6e | 8602 | off += PFKEY_ALIGN8(sizeof(struct sadb_msg)); |
0a7de745 | 8603 | |
39236c6e A |
8604 | /* for authentication algorithm */ |
8605 | if (alen) { | |
8606 | sup = (struct sadb_supported *)(void *)(mtod(n, caddr_t) + off); | |
f427ee49 | 8607 | sup->sadb_supported_len = (u_int16_t)PFKEY_UNIT64(alen); |
39236c6e A |
8608 | sup->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH; |
8609 | off += PFKEY_ALIGN8(sizeof(*sup)); | |
0a7de745 | 8610 | |
39236c6e A |
8611 | for (i = 1; i <= SADB_AALG_MAX; i++) { |
8612 | const struct ah_algorithm *aalgo; | |
0a7de745 | 8613 | |
39236c6e | 8614 | aalgo = ah_algorithm_lookup(i); |
0a7de745 | 8615 | if (!aalgo) { |
39236c6e | 8616 | continue; |
0a7de745 | 8617 | } |
39236c6e | 8618 | alg = (struct sadb_alg *) |
0a7de745 | 8619 | (void *)(mtod(n, caddr_t) + off); |
39236c6e A |
8620 | alg->sadb_alg_id = i; |
8621 | alg->sadb_alg_ivlen = 0; | |
8622 | alg->sadb_alg_minbits = aalgo->keymin; | |
8623 | alg->sadb_alg_maxbits = aalgo->keymax; | |
8624 | off += PFKEY_ALIGN8(sizeof(*alg)); | |
8625 | } | |
9bccf70c | 8626 | } |
0a7de745 | 8627 | |
1c79356b | 8628 | #if IPSEC_ESP |
39236c6e A |
8629 | /* for encryption algorithm */ |
8630 | if (elen) { | |
8631 | sup = (struct sadb_supported *)(void *)(mtod(n, caddr_t) + off); | |
8632 | sup->sadb_supported_len = PFKEY_UNIT64(elen); | |
8633 | sup->sadb_supported_exttype = SADB_EXT_SUPPORTED_ENCRYPT; | |
8634 | off += PFKEY_ALIGN8(sizeof(*sup)); | |
0a7de745 | 8635 | |
39236c6e A |
8636 | for (i = 1; i <= SADB_EALG_MAX; i++) { |
8637 | const struct esp_algorithm *ealgo; | |
0a7de745 | 8638 | |
39236c6e | 8639 | ealgo = esp_algorithm_lookup(i); |
0a7de745 | 8640 | if (!ealgo) { |
39236c6e | 8641 | continue; |
0a7de745 | 8642 | } |
39236c6e | 8643 | alg = (struct sadb_alg *) |
0a7de745 | 8644 | (void *)(mtod(n, caddr_t) + off); |
39236c6e A |
8645 | alg->sadb_alg_id = i; |
8646 | if (ealgo && ealgo->ivlen) { | |
8647 | /* | |
8648 | * give NULL to get the value preferred by | |
8649 | * algorithm XXX SADB_X_EXT_DERIV ? | |
8650 | */ | |
f427ee49 | 8651 | VERIFY((*ealgo->ivlen)(ealgo, NULL) <= UINT8_MAX); |
39236c6e | 8652 | alg->sadb_alg_ivlen = |
f427ee49 | 8653 | (u_int8_t)((*ealgo->ivlen)(ealgo, NULL)); |
0a7de745 | 8654 | } else { |
39236c6e | 8655 | alg->sadb_alg_ivlen = 0; |
0a7de745 | 8656 | } |
39236c6e A |
8657 | alg->sadb_alg_minbits = ealgo->keymin; |
8658 | alg->sadb_alg_maxbits = ealgo->keymax; | |
8659 | off += PFKEY_ALIGN8(sizeof(struct sadb_alg)); | |
8660 | } | |
9bccf70c | 8661 | } |
1c79356b | 8662 | #endif |
0a7de745 | 8663 | |
cb323159 | 8664 | #if DIAGNOSTIC |
0a7de745 | 8665 | if (off != len) { |
39236c6e | 8666 | panic("length assumption failed in key_register"); |
0a7de745 | 8667 | } |
9bccf70c | 8668 | #endif |
0a7de745 | 8669 | |
39236c6e A |
8670 | m_freem(m); |
8671 | return key_sendup_mbuf(so, n, KEY_SENDUP_REGISTERED); | |
0a7de745 | 8672 | } |
1c79356b A |
8673 | } |
8674 | ||
fe8ab488 | 8675 | static void |
0a7de745 | 8676 | key_delete_all_for_socket(struct socket *so) |
fe8ab488 A |
8677 | { |
8678 | struct secashead *sah, *nextsah; | |
8679 | struct secasvar *sav, *nextsav; | |
8680 | u_int stateidx; | |
8681 | u_int state; | |
8682 | ||
8683 | for (sah = LIST_FIRST(&sahtree); | |
0a7de745 A |
8684 | sah != NULL; |
8685 | sah = nextsah) { | |
fe8ab488 A |
8686 | nextsah = LIST_NEXT(sah, chain); |
8687 | for (stateidx = 0; stateidx < _ARRAYLEN(saorder_state_alive); stateidx++) { | |
8688 | state = saorder_state_any[stateidx]; | |
8689 | for (sav = LIST_FIRST(&sah->savtree[state]); sav != NULL; sav = nextsav) { | |
8690 | nextsav = LIST_NEXT(sav, chain); | |
8691 | if (sav->flags2 & SADB_X_EXT_SA2_DELETE_ON_DETACH && | |
0a7de745 | 8692 | sav->so == so) { |
fe8ab488 A |
8693 | key_sa_chgstate(sav, SADB_SASTATE_DEAD); |
8694 | key_freesav(sav, KEY_SADB_LOCKED); | |
8695 | } | |
8696 | } | |
8697 | } | |
8698 | } | |
8699 | } | |
8700 | ||
1c79356b A |
8701 | /* |
8702 | * free secreg entry registered. | |
8703 | * XXX: I want to do free a socket marked done SADB_RESIGER to socket. | |
8704 | */ | |
8705 | void | |
6d2010ae | 8706 | key_freereg( |
0a7de745 | 8707 | struct socket *so) |
1c79356b A |
8708 | { |
8709 | struct secreg *reg; | |
8710 | int i; | |
0a7de745 | 8711 | |
1c79356b | 8712 | /* sanity check */ |
0a7de745 | 8713 | if (so == NULL) { |
1c79356b | 8714 | panic("key_freereg: NULL pointer is passed.\n"); |
0a7de745 A |
8715 | } |
8716 | ||
1c79356b A |
8717 | /* |
8718 | * check whether existing or not. | |
8719 | * check all type of SA, because there is a potential that | |
8720 | * one socket is registered to multiple type of SA. | |
8721 | */ | |
2d21ac55 | 8722 | lck_mtx_lock(sadb_mutex); |
fe8ab488 | 8723 | key_delete_all_for_socket(so); |
1c79356b A |
8724 | for (i = 0; i <= SADB_SATYPE_MAX; i++) { |
8725 | LIST_FOREACH(reg, ®tree[i], chain) { | |
8726 | if (reg->so == so | |
0a7de745 | 8727 | && __LIST_CHAINED(reg)) { |
1c79356b A |
8728 | LIST_REMOVE(reg, chain); |
8729 | KFREE(reg); | |
8730 | break; | |
8731 | } | |
8732 | } | |
8733 | } | |
2d21ac55 | 8734 | lck_mtx_unlock(sadb_mutex); |
1c79356b A |
8735 | return; |
8736 | } | |
8737 | ||
8738 | /* | |
8739 | * SADB_EXPIRE processing | |
8740 | * send | |
9bccf70c | 8741 | * <base, SA, SA2, lifetime(C and one of HS), address(SD)> |
1c79356b A |
8742 | * to KMD by PF_KEY. |
8743 | * NOTE: We send only soft lifetime extension. | |
8744 | * | |
8745 | * OUT: 0 : succeed | |
8746 | * others : error number | |
8747 | */ | |
8748 | static int | |
6d2010ae | 8749 | key_expire( |
0a7de745 | 8750 | struct secasvar *sav) |
1c79356b | 8751 | { |
f427ee49 | 8752 | u_int8_t satype; |
9bccf70c A |
8753 | struct mbuf *result = NULL, *m; |
8754 | int len; | |
8755 | int error = -1; | |
8756 | struct sadb_lifetime *lt; | |
0a7de745 | 8757 | |
5ba3f43e | 8758 | LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_NOTOWNED); |
0a7de745 | 8759 | |
1c79356b | 8760 | /* sanity check */ |
0a7de745 | 8761 | if (sav == NULL) { |
1c79356b | 8762 | panic("key_expire: NULL pointer is passed.\n"); |
0a7de745 A |
8763 | } |
8764 | if (sav->sah == NULL) { | |
1c79356b | 8765 | panic("key_expire: Why was SA index in SA NULL.\n"); |
0a7de745 A |
8766 | } |
8767 | if ((satype = key_proto2satype(sav->sah->saidx.proto)) == 0) { | |
1c79356b | 8768 | panic("key_expire: invalid proto is passed.\n"); |
0a7de745 A |
8769 | } |
8770 | ||
9bccf70c | 8771 | /* set msg header */ |
f427ee49 | 8772 | m = key_setsadbmsg(SADB_EXPIRE, 0, satype, sav->seq, 0, (u_int16_t)sav->refcnt); |
9bccf70c A |
8773 | if (!m) { |
8774 | error = ENOBUFS; | |
8775 | goto fail; | |
1c79356b | 8776 | } |
9bccf70c | 8777 | result = m; |
0a7de745 | 8778 | |
9bccf70c A |
8779 | /* create SA extension */ |
8780 | m = key_setsadbsa(sav); | |
8781 | if (!m) { | |
8782 | error = ENOBUFS; | |
8783 | goto fail; | |
8784 | } | |
8785 | m_cat(result, m); | |
0a7de745 | 8786 | |
1c79356b | 8787 | /* create SA extension */ |
55e303ae | 8788 | m = key_setsadbxsa2(sav->sah->saidx.mode, |
cb323159 | 8789 | sav->replay[0] ? sav->replay[0]->count : 0, |
0a7de745 A |
8790 | sav->sah->saidx.reqid, |
8791 | sav->flags2); | |
9bccf70c A |
8792 | if (!m) { |
8793 | error = ENOBUFS; | |
8794 | goto fail; | |
8795 | } | |
8796 | m_cat(result, m); | |
0a7de745 | 8797 | |
9bccf70c A |
8798 | /* create lifetime extension (current and soft) */ |
8799 | len = PFKEY_ALIGN8(sizeof(*lt)) * 2; | |
8800 | m = key_alloc_mbuf(len); | |
0a7de745 A |
8801 | if (!m || m->m_next) { /*XXX*/ |
8802 | if (m) { | |
9bccf70c | 8803 | m_freem(m); |
0a7de745 | 8804 | } |
9bccf70c A |
8805 | error = ENOBUFS; |
8806 | goto fail; | |
8807 | } | |
8808 | bzero(mtod(m, caddr_t), len); | |
8809 | lt = mtod(m, struct sadb_lifetime *); | |
8810 | lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime)); | |
8811 | lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT; | |
8812 | lt->sadb_lifetime_allocations = sav->lft_c->sadb_lifetime_allocations; | |
8813 | lt->sadb_lifetime_bytes = sav->lft_c->sadb_lifetime_bytes; | |
8814 | lt->sadb_lifetime_addtime = sav->lft_c->sadb_lifetime_addtime; | |
8815 | lt->sadb_lifetime_usetime = sav->lft_c->sadb_lifetime_usetime; | |
316670eb | 8816 | lt = (struct sadb_lifetime *)(void *)(mtod(m, caddr_t) + len / 2); |
9bccf70c A |
8817 | bcopy(sav->lft_s, lt, sizeof(*lt)); |
8818 | m_cat(result, m); | |
0a7de745 | 8819 | |
1c79356b | 8820 | /* set sadb_address for source */ |
9bccf70c | 8821 | m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC, |
0a7de745 A |
8822 | (struct sockaddr *)&sav->sah->saidx.src, |
8823 | FULLMASK, IPSEC_ULPROTO_ANY); | |
9bccf70c A |
8824 | if (!m) { |
8825 | error = ENOBUFS; | |
8826 | goto fail; | |
8827 | } | |
8828 | m_cat(result, m); | |
0a7de745 | 8829 | |
1c79356b | 8830 | /* set sadb_address for destination */ |
9bccf70c | 8831 | m = key_setsadbaddr(SADB_EXT_ADDRESS_DST, |
0a7de745 A |
8832 | (struct sockaddr *)&sav->sah->saidx.dst, |
8833 | FULLMASK, IPSEC_ULPROTO_ANY); | |
9bccf70c A |
8834 | if (!m) { |
8835 | error = ENOBUFS; | |
8836 | goto fail; | |
8837 | } | |
8838 | m_cat(result, m); | |
0a7de745 | 8839 | |
9bccf70c A |
8840 | if ((result->m_flags & M_PKTHDR) == 0) { |
8841 | error = EINVAL; | |
8842 | goto fail; | |
8843 | } | |
0a7de745 | 8844 | |
9bccf70c A |
8845 | if (result->m_len < sizeof(struct sadb_msg)) { |
8846 | result = m_pullup(result, sizeof(struct sadb_msg)); | |
8847 | if (result == NULL) { | |
8848 | error = ENOBUFS; | |
8849 | goto fail; | |
8850 | } | |
8851 | } | |
0a7de745 | 8852 | |
9bccf70c | 8853 | result->m_pkthdr.len = 0; |
0a7de745 | 8854 | for (m = result; m; m = m->m_next) { |
9bccf70c | 8855 | result->m_pkthdr.len += m->m_len; |
0a7de745 A |
8856 | } |
8857 | ||
f427ee49 | 8858 | VERIFY(PFKEY_UNIT64(result->m_pkthdr.len) <= UINT16_MAX); |
9bccf70c | 8859 | mtod(result, struct sadb_msg *)->sadb_msg_len = |
f427ee49 | 8860 | (u_int16_t)PFKEY_UNIT64(result->m_pkthdr.len); |
0a7de745 | 8861 | |
9bccf70c | 8862 | return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED); |
0a7de745 | 8863 | |
39236c6e | 8864 | fail: |
0a7de745 | 8865 | if (result) { |
9bccf70c | 8866 | m_freem(result); |
0a7de745 | 8867 | } |
1c79356b | 8868 | return error; |
1c79356b A |
8869 | } |
8870 | ||
8871 | /* | |
8872 | * SADB_FLUSH processing | |
8873 | * receive | |
8874 | * <base> | |
8875 | * from the ikmpd, and free all entries in secastree. | |
8876 | * and send, | |
8877 | * <base> | |
8878 | * to the ikmpd. | |
8879 | * NOTE: to do is only marking SADB_SASTATE_DEAD. | |
8880 | * | |
9bccf70c | 8881 | * m will always be freed. |
1c79356b | 8882 | */ |
9bccf70c | 8883 | static int |
6d2010ae | 8884 | key_flush( |
0a7de745 A |
8885 | struct socket *so, |
8886 | struct mbuf *m, | |
8887 | const struct sadb_msghdr *mhp) | |
1c79356b | 8888 | { |
9bccf70c | 8889 | struct sadb_msg *newmsg; |
1c79356b A |
8890 | struct secashead *sah, *nextsah; |
8891 | struct secasvar *sav, *nextsav; | |
8892 | u_int16_t proto; | |
f427ee49 | 8893 | u_int state; |
1c79356b | 8894 | u_int stateidx; |
0a7de745 | 8895 | |
1c79356b | 8896 | /* sanity check */ |
0a7de745 | 8897 | if (so == NULL || mhp == NULL || mhp->msg == NULL) { |
1c79356b | 8898 | panic("key_flush: NULL pointer is passed.\n"); |
0a7de745 A |
8899 | } |
8900 | ||
1c79356b | 8901 | /* map satype to proto */ |
9bccf70c | 8902 | if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) { |
55e303ae | 8903 | ipseclog((LOG_DEBUG, "key_flush: invalid satype is passed.\n")); |
9bccf70c | 8904 | return key_senderror(so, m, EINVAL); |
1c79356b | 8905 | } |
0a7de745 | 8906 | |
2d21ac55 | 8907 | lck_mtx_lock(sadb_mutex); |
0a7de745 | 8908 | |
1c79356b A |
8909 | /* no SATYPE specified, i.e. flushing all SA. */ |
8910 | for (sah = LIST_FIRST(&sahtree); | |
0a7de745 A |
8911 | sah != NULL; |
8912 | sah = nextsah) { | |
1c79356b | 8913 | nextsah = LIST_NEXT(sah, chain); |
0a7de745 | 8914 | |
9bccf70c | 8915 | if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC |
0a7de745 | 8916 | && proto != sah->saidx.proto) { |
1c79356b | 8917 | continue; |
0a7de745 A |
8918 | } |
8919 | ||
1c79356b | 8920 | for (stateidx = 0; |
0a7de745 A |
8921 | stateidx < _ARRAYLEN(saorder_state_alive); |
8922 | stateidx++) { | |
1c79356b A |
8923 | state = saorder_state_any[stateidx]; |
8924 | for (sav = LIST_FIRST(&sah->savtree[state]); | |
0a7de745 A |
8925 | sav != NULL; |
8926 | sav = nextsav) { | |
1c79356b | 8927 | nextsav = LIST_NEXT(sav, chain); |
0a7de745 | 8928 | |
1c79356b | 8929 | key_sa_chgstate(sav, SADB_SASTATE_DEAD); |
2d21ac55 | 8930 | key_freesav(sav, KEY_SADB_LOCKED); |
1c79356b A |
8931 | } |
8932 | } | |
0a7de745 | 8933 | |
1c79356b A |
8934 | sah->state = SADB_SASTATE_DEAD; |
8935 | } | |
2d21ac55 | 8936 | lck_mtx_unlock(sadb_mutex); |
0a7de745 | 8937 | |
9bccf70c A |
8938 | if (m->m_len < sizeof(struct sadb_msg) || |
8939 | sizeof(struct sadb_msg) > m->m_len + M_TRAILINGSPACE(m)) { | |
55e303ae | 8940 | ipseclog((LOG_DEBUG, "key_flush: No more memory.\n")); |
9bccf70c | 8941 | return key_senderror(so, m, ENOBUFS); |
1c79356b | 8942 | } |
0a7de745 A |
8943 | |
8944 | if (m->m_next) { | |
9bccf70c | 8945 | m_freem(m->m_next); |
0a7de745 | 8946 | } |
9bccf70c A |
8947 | m->m_next = NULL; |
8948 | m->m_pkthdr.len = m->m_len = sizeof(struct sadb_msg); | |
8949 | newmsg = mtod(m, struct sadb_msg *); | |
1c79356b | 8950 | newmsg->sadb_msg_errno = 0; |
f427ee49 A |
8951 | VERIFY(PFKEY_UNIT64(m->m_pkthdr.len) <= UINT16_MAX); |
8952 | newmsg->sadb_msg_len = (uint16_t)PFKEY_UNIT64(m->m_pkthdr.len); | |
0a7de745 | 8953 | |
9bccf70c | 8954 | return key_sendup_mbuf(so, m, KEY_SENDUP_ALL); |
1c79356b A |
8955 | } |
8956 | ||
8957 | /* | |
8958 | * SADB_DUMP processing | |
8959 | * dump all entries including status of DEAD in SAD. | |
8960 | * receive | |
8961 | * <base> | |
8962 | * from the ikmpd, and dump all secasvar leaves | |
8963 | * and send, | |
8964 | * <base> ..... | |
8965 | * to the ikmpd. | |
8966 | * | |
9bccf70c | 8967 | * m will always be freed. |
1c79356b | 8968 | */ |
39236c6e | 8969 | |
2d21ac55 A |
8970 | struct sav_dump_elem { |
8971 | struct secasvar *sav; | |
8972 | u_int8_t satype; | |
8973 | }; | |
8974 | ||
1c79356b | 8975 | static int |
6d2010ae | 8976 | key_dump( |
0a7de745 A |
8977 | struct socket *so, |
8978 | struct mbuf *m, | |
8979 | const struct sadb_msghdr *mhp) | |
1c79356b | 8980 | { |
1c79356b A |
8981 | struct secashead *sah; |
8982 | struct secasvar *sav; | |
2d21ac55 | 8983 | struct sav_dump_elem *savbuf = NULL, *elem_ptr; |
eb6b6ca3 A |
8984 | size_t total_req_size = 0; |
8985 | u_int32_t bufcount = 0, cnt = 0, cnt2 = 0; | |
1c79356b A |
8986 | u_int16_t proto; |
8987 | u_int stateidx; | |
8988 | u_int8_t satype; | |
f427ee49 | 8989 | u_int state; |
9bccf70c | 8990 | struct mbuf *n; |
2d21ac55 | 8991 | int error = 0; |
0a7de745 | 8992 | |
5ba3f43e | 8993 | LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_NOTOWNED); |
0a7de745 | 8994 | |
1c79356b | 8995 | /* sanity check */ |
0a7de745 | 8996 | if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL) { |
1c79356b | 8997 | panic("key_dump: NULL pointer is passed.\n"); |
0a7de745 A |
8998 | } |
8999 | ||
1c79356b | 9000 | /* map satype to proto */ |
9bccf70c | 9001 | if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) { |
55e303ae | 9002 | ipseclog((LOG_DEBUG, "key_dump: invalid satype is passed.\n")); |
9bccf70c | 9003 | return key_senderror(so, m, EINVAL); |
1c79356b | 9004 | } |
0a7de745 | 9005 | |
eb6b6ca3 | 9006 | if ((bufcount = ipsec_sav_count) == 0) { |
2d21ac55 A |
9007 | error = ENOENT; |
9008 | goto end; | |
9009 | } | |
eb6b6ca3 A |
9010 | |
9011 | if (os_add_overflow(bufcount, 512, &bufcount)) { | |
9012 | ipseclog((LOG_DEBUG, "key_dump: bufcount overflow, ipsec sa count %u.\n", ipsec_sav_count)); | |
9013 | bufcount = ipsec_sav_count; | |
9014 | } | |
9015 | ||
9016 | if (os_mul_overflow(bufcount, sizeof(struct sav_dump_elem), &total_req_size)) { | |
9017 | panic("key_dump savbuf requested memory overflow %u\n", bufcount); | |
9018 | } | |
9019 | ||
9020 | KMALLOC_WAIT(savbuf, struct sav_dump_elem*, total_req_size); | |
2d21ac55 A |
9021 | if (savbuf == NULL) { |
9022 | ipseclog((LOG_DEBUG, "key_dump: No more memory.\n")); | |
9023 | error = ENOMEM; | |
9024 | goto end; | |
9025 | } | |
0a7de745 | 9026 | |
1c79356b | 9027 | /* count sav entries to be sent to the userland. */ |
2d21ac55 A |
9028 | lck_mtx_lock(sadb_mutex); |
9029 | elem_ptr = savbuf; | |
1c79356b | 9030 | LIST_FOREACH(sah, &sahtree, chain) { |
9bccf70c | 9031 | if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC |
0a7de745 | 9032 | && proto != sah->saidx.proto) { |
1c79356b | 9033 | continue; |
0a7de745 A |
9034 | } |
9035 | ||
2d21ac55 A |
9036 | /* map proto to satype */ |
9037 | if ((satype = key_proto2satype(sah->saidx.proto)) == 0) { | |
9038 | lck_mtx_unlock(sadb_mutex); | |
9039 | ipseclog((LOG_DEBUG, "key_dump: there was invalid proto in SAD.\n")); | |
9040 | error = EINVAL; | |
9041 | goto end; | |
9042 | } | |
0a7de745 | 9043 | |
1c79356b | 9044 | for (stateidx = 0; |
0a7de745 A |
9045 | stateidx < _ARRAYLEN(saorder_state_any); |
9046 | stateidx++) { | |
1c79356b A |
9047 | state = saorder_state_any[stateidx]; |
9048 | LIST_FOREACH(sav, &sah->savtree[state], chain) { | |
0a7de745 A |
9049 | if (cnt == bufcount) { |
9050 | break; /* out of buffer space */ | |
9051 | } | |
2d21ac55 A |
9052 | elem_ptr->sav = sav; |
9053 | elem_ptr->satype = satype; | |
9054 | sav->refcnt++; | |
9055 | elem_ptr++; | |
39236c6e | 9056 | cnt++; |
1c79356b A |
9057 | } |
9058 | } | |
9059 | } | |
2d21ac55 | 9060 | lck_mtx_unlock(sadb_mutex); |
0a7de745 | 9061 | |
2d21ac55 A |
9062 | if (cnt == 0) { |
9063 | error = ENOENT; | |
9064 | goto end; | |
9065 | } | |
0a7de745 | 9066 | |
1c79356b | 9067 | /* send this to the userland, one at a time. */ |
2d21ac55 A |
9068 | elem_ptr = savbuf; |
9069 | cnt2 = cnt; | |
9070 | while (cnt2) { | |
9071 | n = key_setdumpsa(elem_ptr->sav, SADB_DUMP, elem_ptr->satype, | |
0a7de745 A |
9072 | --cnt2, mhp->msg->sadb_msg_pid); |
9073 | ||
2d21ac55 A |
9074 | if (!n) { |
9075 | error = ENOBUFS; | |
9076 | goto end; | |
1c79356b | 9077 | } |
0a7de745 | 9078 | |
2d21ac55 A |
9079 | key_sendup_mbuf(so, n, KEY_SENDUP_ONE); |
9080 | elem_ptr++; | |
9081 | } | |
0a7de745 | 9082 | |
2d21ac55 A |
9083 | end: |
9084 | if (savbuf) { | |
9085 | if (cnt) { | |
9086 | elem_ptr = savbuf; | |
9087 | lck_mtx_lock(sadb_mutex); | |
0a7de745 | 9088 | while (cnt--) { |
2d21ac55 | 9089 | key_freesav((elem_ptr++)->sav, KEY_SADB_LOCKED); |
0a7de745 | 9090 | } |
2d21ac55 | 9091 | lck_mtx_unlock(sadb_mutex); |
1c79356b | 9092 | } |
2d21ac55 | 9093 | KFREE(savbuf); |
1c79356b | 9094 | } |
0a7de745 A |
9095 | |
9096 | if (error) { | |
2d21ac55 | 9097 | return key_senderror(so, m, error); |
0a7de745 A |
9098 | } |
9099 | ||
9bccf70c | 9100 | m_freem(m); |
1c79356b A |
9101 | return 0; |
9102 | } | |
9103 | ||
9104 | /* | |
9105 | * SADB_X_PROMISC processing | |
9bccf70c A |
9106 | * |
9107 | * m will always be freed. | |
1c79356b | 9108 | */ |
9bccf70c | 9109 | static int |
6d2010ae | 9110 | key_promisc( |
0a7de745 A |
9111 | struct socket *so, |
9112 | struct mbuf *m, | |
9113 | const struct sadb_msghdr *mhp) | |
1c79356b | 9114 | { |
1c79356b | 9115 | int olen; |
0a7de745 | 9116 | |
1c79356b | 9117 | /* sanity check */ |
0a7de745 | 9118 | if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL) { |
1c79356b | 9119 | panic("key_promisc: NULL pointer is passed.\n"); |
0a7de745 A |
9120 | } |
9121 | ||
9bccf70c | 9122 | olen = PFKEY_UNUNIT64(mhp->msg->sadb_msg_len); |
0a7de745 | 9123 | |
1c79356b | 9124 | if (olen < sizeof(struct sadb_msg)) { |
9bccf70c A |
9125 | #if 1 |
9126 | return key_senderror(so, m, EINVAL); | |
9127 | #else | |
9128 | m_freem(m); | |
9129 | return 0; | |
9130 | #endif | |
1c79356b A |
9131 | } else if (olen == sizeof(struct sadb_msg)) { |
9132 | /* enable/disable promisc mode */ | |
9133 | struct keycb *kp; | |
0a7de745 | 9134 | |
91447636 | 9135 | socket_lock(so, 1); |
0a7de745 | 9136 | if ((kp = (struct keycb *)sotorawcb(so)) == NULL) { |
9bccf70c | 9137 | return key_senderror(so, m, EINVAL); |
0a7de745 | 9138 | } |
9bccf70c A |
9139 | mhp->msg->sadb_msg_errno = 0; |
9140 | switch (mhp->msg->sadb_msg_satype) { | |
0a7de745 A |
9141 | case 0: |
9142 | case 1: | |
9143 | kp->kp_promisc = mhp->msg->sadb_msg_satype; | |
9144 | break; | |
9145 | default: | |
9146 | socket_unlock(so, 1); | |
9147 | return key_senderror(so, m, EINVAL); | |
1c79356b | 9148 | } |
91447636 | 9149 | socket_unlock(so, 1); |
0a7de745 | 9150 | |
1c79356b | 9151 | /* send the original message back to everyone */ |
9bccf70c A |
9152 | mhp->msg->sadb_msg_errno = 0; |
9153 | return key_sendup_mbuf(so, m, KEY_SENDUP_ALL); | |
1c79356b A |
9154 | } else { |
9155 | /* send packet as is */ | |
0a7de745 | 9156 | |
9bccf70c | 9157 | m_adj(m, PFKEY_ALIGN8(sizeof(struct sadb_msg))); |
0a7de745 | 9158 | |
9bccf70c A |
9159 | /* TODO: if sadb_msg_seq is specified, send to specific pid */ |
9160 | return key_sendup_mbuf(so, m, KEY_SENDUP_ALL); | |
1c79356b A |
9161 | } |
9162 | } | |
9163 | ||
0a7de745 A |
9164 | static int(*const key_typesw[])(struct socket *, struct mbuf *, |
9165 | const struct sadb_msghdr *) = { | |
9166 | NULL, /* SADB_RESERVED */ | |
9167 | key_getspi, /* SADB_GETSPI */ | |
9168 | key_update, /* SADB_UPDATE */ | |
9169 | key_add, /* SADB_ADD */ | |
9170 | key_delete, /* SADB_DELETE */ | |
9171 | key_get, /* SADB_GET */ | |
9172 | key_acquire2, /* SADB_ACQUIRE */ | |
9173 | key_register, /* SADB_REGISTER */ | |
9174 | NULL, /* SADB_EXPIRE */ | |
9175 | key_flush, /* SADB_FLUSH */ | |
9176 | key_dump, /* SADB_DUMP */ | |
9177 | key_promisc, /* SADB_X_PROMISC */ | |
9178 | NULL, /* SADB_X_PCHANGE */ | |
9179 | key_spdadd, /* SADB_X_SPDUPDATE */ | |
9180 | key_spdadd, /* SADB_X_SPDADD */ | |
9181 | key_spddelete, /* SADB_X_SPDDELETE */ | |
9182 | key_spdget, /* SADB_X_SPDGET */ | |
9183 | NULL, /* SADB_X_SPDACQUIRE */ | |
9184 | key_spddump, /* SADB_X_SPDDUMP */ | |
9185 | key_spdflush, /* SADB_X_SPDFLUSH */ | |
9186 | key_spdadd, /* SADB_X_SPDSETIDX */ | |
9187 | NULL, /* SADB_X_SPDEXPIRE */ | |
9188 | key_spddelete2, /* SADB_X_SPDDELETE2 */ | |
b0d623f7 | 9189 | key_getsastat, /* SADB_GETSASTAT */ |
39236c6e A |
9190 | key_spdenable, /* SADB_X_SPDENABLE */ |
9191 | key_spddisable, /* SADB_X_SPDDISABLE */ | |
3e170ce0 | 9192 | key_migrate, /* SADB_MIGRATE */ |
9bccf70c | 9193 | }; |
1c79356b | 9194 | |
3e170ce0 A |
9195 | static void |
9196 | bzero_mbuf(struct mbuf *m) | |
9197 | { | |
9198 | struct mbuf *mptr = m; | |
9199 | struct sadb_msg *msg = NULL; | |
9200 | int offset = 0; | |
9201 | ||
9202 | if (!mptr) { | |
9203 | return; | |
9204 | } | |
9205 | ||
9206 | if (mptr->m_len >= sizeof(struct sadb_msg)) { | |
9207 | msg = mtod(mptr, struct sadb_msg *); | |
9208 | if (msg->sadb_msg_type != SADB_ADD && | |
9209 | msg->sadb_msg_type != SADB_UPDATE) { | |
9210 | return; | |
9211 | } | |
9212 | offset = sizeof(struct sadb_msg); | |
9213 | } | |
0a7de745 | 9214 | bzero(mptr->m_data + offset, mptr->m_len - offset); |
3e170ce0 A |
9215 | mptr = mptr->m_next; |
9216 | while (mptr != NULL) { | |
9217 | bzero(mptr->m_data, mptr->m_len); | |
9218 | mptr = mptr->m_next; | |
9219 | } | |
9220 | } | |
9221 | ||
9222 | static void | |
ecc0ceb4 | 9223 | bzero_keys(const struct sadb_msghdr *mh) |
3e170ce0 A |
9224 | { |
9225 | int extlen = 0; | |
9226 | int offset = 0; | |
9227 | ||
9228 | if (!mh) { | |
9229 | return; | |
9230 | } | |
9231 | offset = sizeof(struct sadb_key); | |
9232 | ||
9233 | if (mh->ext[SADB_EXT_KEY_ENCRYPT]) { | |
9234 | struct sadb_key *key = (struct sadb_key*)mh->ext[SADB_EXT_KEY_ENCRYPT]; | |
9235 | extlen = key->sadb_key_bits >> 3; | |
9236 | ||
9237 | if (mh->extlen[SADB_EXT_KEY_ENCRYPT] >= offset + extlen) { | |
0a7de745 | 9238 | bzero((uint8_t *)mh->ext[SADB_EXT_KEY_ENCRYPT] + offset, extlen); |
3e170ce0 A |
9239 | } else { |
9240 | bzero(mh->ext[SADB_EXT_KEY_ENCRYPT], mh->extlen[SADB_EXT_KEY_ENCRYPT]); | |
9241 | } | |
9242 | } | |
9243 | if (mh->ext[SADB_EXT_KEY_AUTH]) { | |
9244 | struct sadb_key *key = (struct sadb_key*)mh->ext[SADB_EXT_KEY_AUTH]; | |
9245 | extlen = key->sadb_key_bits >> 3; | |
9246 | ||
9247 | if (mh->extlen[SADB_EXT_KEY_AUTH] >= offset + extlen) { | |
0a7de745 | 9248 | bzero((uint8_t *)mh->ext[SADB_EXT_KEY_AUTH] + offset, extlen); |
3e170ce0 A |
9249 | } else { |
9250 | bzero(mh->ext[SADB_EXT_KEY_AUTH], mh->extlen[SADB_EXT_KEY_AUTH]); | |
9251 | } | |
9252 | } | |
9253 | } | |
9254 | ||
4d15aeb1 A |
9255 | static int |
9256 | key_validate_address_pair(struct sadb_address *src0, | |
0a7de745 | 9257 | struct sadb_address *dst0) |
4d15aeb1 A |
9258 | { |
9259 | u_int plen = 0; | |
9260 | ||
9261 | /* check upper layer protocol */ | |
9262 | if (src0->sadb_address_proto != dst0->sadb_address_proto) { | |
9263 | ipseclog((LOG_DEBUG, "key_parse: upper layer protocol mismatched.\n")); | |
9264 | PFKEY_STAT_INCREMENT(pfkeystat.out_invaddr); | |
0a7de745 | 9265 | return EINVAL; |
4d15aeb1 A |
9266 | } |
9267 | ||
9268 | /* check family */ | |
9269 | if (PFKEY_ADDR_SADDR(src0)->sa_family != | |
0a7de745 | 9270 | PFKEY_ADDR_SADDR(dst0)->sa_family) { |
4d15aeb1 A |
9271 | ipseclog((LOG_DEBUG, "key_parse: address family mismatched.\n")); |
9272 | PFKEY_STAT_INCREMENT(pfkeystat.out_invaddr); | |
0a7de745 | 9273 | return EINVAL; |
4d15aeb1 A |
9274 | } |
9275 | if (PFKEY_ADDR_SADDR(src0)->sa_len != | |
0a7de745 | 9276 | PFKEY_ADDR_SADDR(dst0)->sa_len) { |
4d15aeb1 | 9277 | ipseclog((LOG_DEBUG, |
0a7de745 | 9278 | "key_parse: address struct size mismatched.\n")); |
4d15aeb1 | 9279 | PFKEY_STAT_INCREMENT(pfkeystat.out_invaddr); |
0a7de745 | 9280 | return EINVAL; |
4d15aeb1 A |
9281 | } |
9282 | ||
9283 | switch (PFKEY_ADDR_SADDR(src0)->sa_family) { | |
0a7de745 A |
9284 | case AF_INET: |
9285 | if (PFKEY_ADDR_SADDR(src0)->sa_len != sizeof(struct sockaddr_in)) { | |
4d15aeb1 | 9286 | PFKEY_STAT_INCREMENT(pfkeystat.out_invaddr); |
0a7de745 A |
9287 | return EINVAL; |
9288 | } | |
9289 | break; | |
9290 | case AF_INET6: | |
9291 | if (PFKEY_ADDR_SADDR(src0)->sa_len != sizeof(struct sockaddr_in6)) { | |
9292 | PFKEY_STAT_INCREMENT(pfkeystat.out_invaddr); | |
9293 | return EINVAL; | |
9294 | } | |
9295 | break; | |
9296 | default: | |
9297 | ipseclog((LOG_DEBUG, | |
9298 | "key_parse: unsupported address family.\n")); | |
9299 | PFKEY_STAT_INCREMENT(pfkeystat.out_invaddr); | |
9300 | return EAFNOSUPPORT; | |
4d15aeb1 A |
9301 | } |
9302 | ||
9303 | switch (PFKEY_ADDR_SADDR(src0)->sa_family) { | |
0a7de745 A |
9304 | case AF_INET: |
9305 | plen = sizeof(struct in_addr) << 3; | |
9306 | break; | |
9307 | case AF_INET6: | |
9308 | plen = sizeof(struct in6_addr) << 3; | |
9309 | break; | |
9310 | default: | |
9311 | plen = 0; /*fool gcc*/ | |
9312 | break; | |
4d15aeb1 A |
9313 | } |
9314 | ||
9315 | /* check max prefix length */ | |
9316 | if (src0->sadb_address_prefixlen > plen || | |
0a7de745 | 9317 | dst0->sadb_address_prefixlen > plen) { |
4d15aeb1 | 9318 | ipseclog((LOG_DEBUG, |
0a7de745 | 9319 | "key_parse: illegal prefixlen.\n")); |
4d15aeb1 | 9320 | PFKEY_STAT_INCREMENT(pfkeystat.out_invaddr); |
0a7de745 | 9321 | return EINVAL; |
4d15aeb1 A |
9322 | } |
9323 | ||
9324 | /* | |
9325 | * prefixlen == 0 is valid because there can be a case when | |
9326 | * all addresses are matched. | |
9327 | */ | |
0a7de745 | 9328 | return 0; |
4d15aeb1 A |
9329 | } |
9330 | ||
1c79356b A |
9331 | /* |
9332 | * parse sadb_msg buffer to process PFKEYv2, | |
9333 | * and create a data to response if needed. | |
9334 | * I think to be dealed with mbuf directly. | |
9335 | * IN: | |
9336 | * msgp : pointer to pointer to a received buffer pulluped. | |
9337 | * This is rewrited to response. | |
9338 | * so : pointer to socket. | |
9339 | * OUT: | |
9340 | * length for buffer to send to user process. | |
9341 | */ | |
9342 | int | |
6d2010ae | 9343 | key_parse( |
0a7de745 A |
9344 | struct mbuf *m, |
9345 | struct socket *so) | |
1c79356b | 9346 | { |
9bccf70c A |
9347 | struct sadb_msg *msg; |
9348 | struct sadb_msghdr mh; | |
1c79356b A |
9349 | u_int orglen; |
9350 | int error; | |
9bccf70c | 9351 | int target; |
3e170ce0 A |
9352 | Boolean keyAligned = FALSE; |
9353 | ||
5ba3f43e | 9354 | LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_NOTOWNED); |
0a7de745 | 9355 | |
1c79356b | 9356 | /* sanity check */ |
0a7de745 | 9357 | if (m == NULL || so == NULL) { |
1c79356b | 9358 | panic("key_parse: NULL pointer is passed.\n"); |
0a7de745 A |
9359 | } |
9360 | ||
9361 | #if 0 /*kdebug_sadb assumes msg in linear buffer*/ | |
1c79356b | 9362 | KEYDEBUG(KEYDEBUG_KEY_DUMP, |
0a7de745 A |
9363 | ipseclog((LOG_DEBUG, "key_parse: passed sadb_msg\n")); |
9364 | kdebug_sadb(msg)); | |
9bccf70c | 9365 | #endif |
0a7de745 | 9366 | |
9bccf70c A |
9367 | if (m->m_len < sizeof(struct sadb_msg)) { |
9368 | m = m_pullup(m, sizeof(struct sadb_msg)); | |
0a7de745 | 9369 | if (!m) { |
9bccf70c | 9370 | return ENOBUFS; |
0a7de745 | 9371 | } |
9bccf70c A |
9372 | } |
9373 | msg = mtod(m, struct sadb_msg *); | |
1c79356b | 9374 | orglen = PFKEY_UNUNIT64(msg->sadb_msg_len); |
9bccf70c | 9375 | target = KEY_SENDUP_ONE; |
0a7de745 | 9376 | |
9bccf70c | 9377 | if ((m->m_flags & M_PKTHDR) == 0 || |
cb323159 | 9378 | m->m_pkthdr.len != orglen) { |
55e303ae | 9379 | ipseclog((LOG_DEBUG, "key_parse: invalid message length.\n")); |
2d21ac55 | 9380 | PFKEY_STAT_INCREMENT(pfkeystat.out_invlen); |
9bccf70c A |
9381 | error = EINVAL; |
9382 | goto senderror; | |
9383 | } | |
0a7de745 | 9384 | |
1c79356b | 9385 | if (msg->sadb_msg_version != PF_KEY_V2) { |
55e303ae | 9386 | ipseclog((LOG_DEBUG, |
0a7de745 A |
9387 | "key_parse: PF_KEY version %u is mismatched.\n", |
9388 | msg->sadb_msg_version)); | |
2d21ac55 | 9389 | PFKEY_STAT_INCREMENT(pfkeystat.out_invver); |
9bccf70c A |
9390 | error = EINVAL; |
9391 | goto senderror; | |
1c79356b | 9392 | } |
0a7de745 | 9393 | |
1c79356b | 9394 | if (msg->sadb_msg_type > SADB_MAX) { |
55e303ae | 9395 | ipseclog((LOG_DEBUG, "key_parse: invalid type %u is passed.\n", |
0a7de745 | 9396 | msg->sadb_msg_type)); |
2d21ac55 | 9397 | PFKEY_STAT_INCREMENT(pfkeystat.out_invmsgtype); |
9bccf70c A |
9398 | error = EINVAL; |
9399 | goto senderror; | |
9400 | } | |
0a7de745 | 9401 | |
9bccf70c A |
9402 | /* for old-fashioned code - should be nuked */ |
9403 | if (m->m_pkthdr.len > MCLBYTES) { | |
9404 | m_freem(m); | |
9405 | return ENOBUFS; | |
9406 | } | |
9407 | if (m->m_next) { | |
9408 | struct mbuf *n; | |
0a7de745 | 9409 | |
316670eb | 9410 | MGETHDR(n, M_WAITOK, MT_DATA); |
9bccf70c | 9411 | if (n && m->m_pkthdr.len > MHLEN) { |
316670eb | 9412 | MCLGET(n, M_WAITOK); |
9bccf70c A |
9413 | if ((n->m_flags & M_EXT) == 0) { |
9414 | m_free(n); | |
9415 | n = NULL; | |
9416 | } | |
9417 | } | |
9418 | if (!n) { | |
3e170ce0 | 9419 | bzero_mbuf(m); |
9bccf70c A |
9420 | m_freem(m); |
9421 | return ENOBUFS; | |
9422 | } | |
9423 | m_copydata(m, 0, m->m_pkthdr.len, mtod(n, caddr_t)); | |
9424 | n->m_pkthdr.len = n->m_len = m->m_pkthdr.len; | |
9425 | n->m_next = NULL; | |
3e170ce0 | 9426 | bzero_mbuf(m); |
9bccf70c A |
9427 | m_freem(m); |
9428 | m = n; | |
1c79356b | 9429 | } |
0a7de745 | 9430 | |
9bccf70c A |
9431 | /* align the mbuf chain so that extensions are in contiguous region. */ |
9432 | error = key_align(m, &mh); | |
0a7de745 | 9433 | if (error) { |
9bccf70c | 9434 | return error; |
0a7de745 A |
9435 | } |
9436 | ||
9437 | if (m->m_next) { /*XXX*/ | |
3e170ce0 | 9438 | bzero_mbuf(m); |
9bccf70c A |
9439 | m_freem(m); |
9440 | return ENOBUFS; | |
1c79356b | 9441 | } |
0a7de745 | 9442 | |
3e170ce0 | 9443 | keyAligned = TRUE; |
9bccf70c | 9444 | msg = mh.msg; |
0a7de745 | 9445 | |
1c79356b A |
9446 | /* check SA type */ |
9447 | switch (msg->sadb_msg_satype) { | |
0a7de745 A |
9448 | case SADB_SATYPE_UNSPEC: |
9449 | switch (msg->sadb_msg_type) { | |
9450 | case SADB_GETSPI: | |
9451 | case SADB_UPDATE: | |
9452 | case SADB_ADD: | |
9453 | case SADB_DELETE: | |
9454 | case SADB_GET: | |
9455 | case SADB_ACQUIRE: | |
9456 | case SADB_EXPIRE: | |
9457 | ipseclog((LOG_DEBUG, "key_parse: must specify satype " | |
9458 | "when msg type=%u.\n", msg->sadb_msg_type)); | |
2d21ac55 | 9459 | PFKEY_STAT_INCREMENT(pfkeystat.out_invsatype); |
0a7de745 | 9460 | error = EINVAL; |
9bccf70c | 9461 | goto senderror; |
0a7de745 A |
9462 | } |
9463 | break; | |
9464 | case SADB_SATYPE_AH: | |
9465 | case SADB_SATYPE_ESP: | |
0a7de745 A |
9466 | switch (msg->sadb_msg_type) { |
9467 | case SADB_X_SPDADD: | |
9468 | case SADB_X_SPDDELETE: | |
9469 | case SADB_X_SPDGET: | |
9470 | case SADB_X_SPDDUMP: | |
9471 | case SADB_X_SPDFLUSH: | |
9472 | case SADB_X_SPDSETIDX: | |
9473 | case SADB_X_SPDUPDATE: | |
9474 | case SADB_X_SPDDELETE2: | |
9475 | case SADB_X_SPDENABLE: | |
9476 | case SADB_X_SPDDISABLE: | |
9477 | ipseclog((LOG_DEBUG, "key_parse: illegal satype=%u\n", | |
9478 | msg->sadb_msg_type)); | |
2d21ac55 | 9479 | PFKEY_STAT_INCREMENT(pfkeystat.out_invsatype); |
9bccf70c A |
9480 | error = EINVAL; |
9481 | goto senderror; | |
0a7de745 A |
9482 | } |
9483 | break; | |
9484 | case SADB_SATYPE_RSVP: | |
9485 | case SADB_SATYPE_OSPFV2: | |
9486 | case SADB_SATYPE_RIPV2: | |
9487 | case SADB_SATYPE_MIP: | |
9488 | ipseclog((LOG_DEBUG, "key_parse: type %u isn't supported.\n", | |
9489 | msg->sadb_msg_satype)); | |
9490 | PFKEY_STAT_INCREMENT(pfkeystat.out_invsatype); | |
9491 | error = EOPNOTSUPP; | |
9492 | goto senderror; | |
9493 | case 1: /* XXX: What does it do? */ | |
9494 | if (msg->sadb_msg_type == SADB_X_PROMISC) { | |
9495 | break; | |
9496 | } | |
f427ee49 | 9497 | OS_FALLTHROUGH; |
0a7de745 A |
9498 | default: |
9499 | ipseclog((LOG_DEBUG, "key_parse: invalid type %u is passed.\n", | |
9500 | msg->sadb_msg_satype)); | |
9501 | PFKEY_STAT_INCREMENT(pfkeystat.out_invsatype); | |
9502 | error = EINVAL; | |
9503 | goto senderror; | |
1c79356b | 9504 | } |
0a7de745 | 9505 | |
4d15aeb1 A |
9506 | /* Validate address fields for matching families, lengths, etc. */ |
9507 | void *src0 = mh.ext[SADB_EXT_ADDRESS_SRC]; | |
9508 | void *dst0 = mh.ext[SADB_EXT_ADDRESS_DST]; | |
9509 | if (mh.ext[SADB_X_EXT_ADDR_RANGE_SRC_START] != NULL && | |
0a7de745 | 9510 | mh.ext[SADB_X_EXT_ADDR_RANGE_SRC_END] != NULL) { |
4d15aeb1 | 9511 | error = key_validate_address_pair((struct sadb_address *)(mh.ext[SADB_X_EXT_ADDR_RANGE_SRC_START]), |
0a7de745 | 9512 | (struct sadb_address *)(mh.ext[SADB_X_EXT_ADDR_RANGE_SRC_END])); |
4d15aeb1 | 9513 | if (error != 0) { |
9bccf70c | 9514 | goto senderror; |
1c79356b | 9515 | } |
4d15aeb1 A |
9516 | |
9517 | if (src0 == NULL) { | |
9518 | src0 = mh.ext[SADB_X_EXT_ADDR_RANGE_SRC_START]; | |
1c79356b | 9519 | } |
4d15aeb1 A |
9520 | } |
9521 | if (mh.ext[SADB_X_EXT_ADDR_RANGE_DST_START] != NULL && | |
0a7de745 | 9522 | mh.ext[SADB_X_EXT_ADDR_RANGE_DST_END] != NULL) { |
4d15aeb1 | 9523 | error = key_validate_address_pair((struct sadb_address *)(mh.ext[SADB_X_EXT_ADDR_RANGE_DST_START]), |
0a7de745 | 9524 | (struct sadb_address *)(mh.ext[SADB_X_EXT_ADDR_RANGE_DST_END])); |
4d15aeb1 | 9525 | if (error != 0) { |
9bccf70c | 9526 | goto senderror; |
1c79356b | 9527 | } |
4d15aeb1 A |
9528 | |
9529 | if (dst0 == NULL) { | |
9530 | dst0 = mh.ext[SADB_X_EXT_ADDR_RANGE_DST_START]; | |
1c79356b | 9531 | } |
4d15aeb1 A |
9532 | } |
9533 | if (src0 != NULL && dst0 != NULL) { | |
9534 | error = key_validate_address_pair((struct sadb_address *)(src0), | |
0a7de745 | 9535 | (struct sadb_address *)(dst0)); |
4d15aeb1 | 9536 | if (error != 0) { |
9bccf70c | 9537 | goto senderror; |
1c79356b | 9538 | } |
9bccf70c | 9539 | } |
0a7de745 | 9540 | |
eb6b6ca3 A |
9541 | void *migrate_src = mh.ext[SADB_EXT_MIGRATE_ADDRESS_SRC]; |
9542 | void *migrate_dst = mh.ext[SADB_EXT_MIGRATE_ADDRESS_DST]; | |
9543 | if (migrate_src != NULL && migrate_dst != NULL) { | |
9544 | error = key_validate_address_pair((struct sadb_address *)(migrate_src), | |
9545 | (struct sadb_address *)(migrate_dst)); | |
9546 | if (error != 0) { | |
9547 | goto senderror; | |
9548 | } | |
9549 | } | |
9550 | ||
0a7de745 | 9551 | if (msg->sadb_msg_type >= sizeof(key_typesw) / sizeof(key_typesw[0]) || |
9bccf70c | 9552 | key_typesw[msg->sadb_msg_type] == NULL) { |
2d21ac55 | 9553 | PFKEY_STAT_INCREMENT(pfkeystat.out_invmsgtype); |
9bccf70c A |
9554 | error = EINVAL; |
9555 | goto senderror; | |
9556 | } | |
0a7de745 | 9557 | |
3e170ce0 A |
9558 | error = (*key_typesw[msg->sadb_msg_type])(so, m, &mh); |
9559 | ||
3e170ce0 A |
9560 | return error; |
9561 | ||
9bccf70c | 9562 | senderror: |
3e170ce0 A |
9563 | if (keyAligned) { |
9564 | bzero_keys(&mh); | |
9565 | } else { | |
9566 | bzero_mbuf(m); | |
9567 | } | |
f427ee49 | 9568 | msg->sadb_msg_errno = (u_int8_t)error; |
9bccf70c A |
9569 | return key_sendup_mbuf(so, m, target); |
9570 | } | |
1c79356b | 9571 | |
9bccf70c | 9572 | static int |
6d2010ae | 9573 | key_senderror( |
0a7de745 A |
9574 | struct socket *so, |
9575 | struct mbuf *m, | |
9576 | int code) | |
9bccf70c A |
9577 | { |
9578 | struct sadb_msg *msg; | |
0a7de745 | 9579 | |
5ba3f43e | 9580 | LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_NOTOWNED); |
0a7de745 A |
9581 | |
9582 | if (m->m_len < sizeof(struct sadb_msg)) { | |
9bccf70c | 9583 | panic("invalid mbuf passed to key_senderror"); |
0a7de745 A |
9584 | } |
9585 | ||
9bccf70c | 9586 | msg = mtod(m, struct sadb_msg *); |
f427ee49 | 9587 | msg->sadb_msg_errno = (u_int8_t)code; |
9bccf70c | 9588 | return key_sendup_mbuf(so, m, KEY_SENDUP_ONE); |
1c79356b A |
9589 | } |
9590 | ||
9591 | /* | |
9592 | * set the pointer to each header into message buffer. | |
9bccf70c A |
9593 | * m will be freed on error. |
9594 | * XXX larger-than-MCLBYTES extension? | |
1c79356b A |
9595 | */ |
9596 | static int | |
6d2010ae | 9597 | key_align( |
0a7de745 A |
9598 | struct mbuf *m, |
9599 | struct sadb_msghdr *mhp) | |
1c79356b | 9600 | { |
9bccf70c | 9601 | struct mbuf *n; |
1c79356b | 9602 | struct sadb_ext *ext; |
f427ee49 A |
9603 | size_t end; |
9604 | int off, extlen; | |
9bccf70c | 9605 | int toff; |
0a7de745 | 9606 | |
1c79356b | 9607 | /* sanity check */ |
0a7de745 | 9608 | if (m == NULL || mhp == NULL) { |
1c79356b | 9609 | panic("key_align: NULL pointer is passed.\n"); |
0a7de745 A |
9610 | } |
9611 | if (m->m_len < sizeof(struct sadb_msg)) { | |
9bccf70c | 9612 | panic("invalid mbuf passed to key_align"); |
0a7de745 A |
9613 | } |
9614 | ||
1c79356b | 9615 | /* initialize */ |
9bccf70c | 9616 | bzero(mhp, sizeof(*mhp)); |
0a7de745 | 9617 | |
9bccf70c | 9618 | mhp->msg = mtod(m, struct sadb_msg *); |
0a7de745 A |
9619 | mhp->ext[0] = (struct sadb_ext *)mhp->msg; /*XXX backward compat */ |
9620 | ||
9bccf70c | 9621 | end = PFKEY_UNUNIT64(mhp->msg->sadb_msg_len); |
f427ee49 | 9622 | extlen = (int)end; /*just in case extlen is not updated*/ |
9bccf70c A |
9623 | for (off = sizeof(struct sadb_msg); off < end; off += extlen) { |
9624 | n = m_pulldown(m, off, sizeof(struct sadb_ext), &toff); | |
9625 | if (!n) { | |
9626 | /* m is already freed */ | |
9627 | return ENOBUFS; | |
9628 | } | |
316670eb | 9629 | ext = (struct sadb_ext *)(void *)(mtod(n, caddr_t) + toff); |
0a7de745 | 9630 | |
1c79356b A |
9631 | /* set pointer */ |
9632 | switch (ext->sadb_ext_type) { | |
0a7de745 A |
9633 | case SADB_EXT_SA: |
9634 | case SADB_EXT_ADDRESS_SRC: | |
9635 | case SADB_EXT_ADDRESS_DST: | |
9636 | case SADB_EXT_ADDRESS_PROXY: | |
9637 | case SADB_EXT_LIFETIME_CURRENT: | |
9638 | case SADB_EXT_LIFETIME_HARD: | |
9639 | case SADB_EXT_LIFETIME_SOFT: | |
9640 | case SADB_EXT_KEY_AUTH: | |
9641 | case SADB_EXT_KEY_ENCRYPT: | |
9642 | case SADB_EXT_IDENTITY_SRC: | |
9643 | case SADB_EXT_IDENTITY_DST: | |
9644 | case SADB_EXT_SENSITIVITY: | |
9645 | case SADB_EXT_PROPOSAL: | |
9646 | case SADB_EXT_SUPPORTED_AUTH: | |
9647 | case SADB_EXT_SUPPORTED_ENCRYPT: | |
9648 | case SADB_EXT_SPIRANGE: | |
9649 | case SADB_X_EXT_POLICY: | |
9650 | case SADB_X_EXT_SA2: | |
9651 | case SADB_EXT_SESSION_ID: | |
9652 | case SADB_EXT_SASTAT: | |
9653 | case SADB_X_EXT_IPSECIF: | |
9654 | case SADB_X_EXT_ADDR_RANGE_SRC_START: | |
9655 | case SADB_X_EXT_ADDR_RANGE_SRC_END: | |
9656 | case SADB_X_EXT_ADDR_RANGE_DST_START: | |
9657 | case SADB_X_EXT_ADDR_RANGE_DST_END: | |
9658 | case SADB_EXT_MIGRATE_ADDRESS_SRC: | |
9659 | case SADB_EXT_MIGRATE_ADDRESS_DST: | |
9660 | case SADB_X_EXT_MIGRATE_IPSECIF: | |
9661 | /* duplicate check */ | |
9662 | /* | |
9663 | * XXX Are there duplication payloads of either | |
9664 | * KEY_AUTH or KEY_ENCRYPT ? | |
9665 | */ | |
9666 | if (mhp->ext[ext->sadb_ext_type] != NULL) { | |
55e303ae | 9667 | ipseclog((LOG_DEBUG, |
0a7de745 A |
9668 | "key_align: duplicate ext_type %u " |
9669 | "is passed.\n", ext->sadb_ext_type)); | |
3e170ce0 | 9670 | bzero_mbuf(m); |
9bccf70c | 9671 | m_freem(m); |
0a7de745 | 9672 | PFKEY_STAT_INCREMENT(pfkeystat.out_dupext); |
1c79356b | 9673 | return EINVAL; |
0a7de745 A |
9674 | } |
9675 | break; | |
9676 | default: | |
9677 | ipseclog((LOG_DEBUG, | |
9678 | "key_align: invalid ext_type %u is passed.\n", | |
9679 | ext->sadb_ext_type)); | |
9680 | bzero_mbuf(m); | |
9681 | m_freem(m); | |
9682 | PFKEY_STAT_INCREMENT(pfkeystat.out_invexttype); | |
9683 | return EINVAL; | |
1c79356b | 9684 | } |
0a7de745 | 9685 | |
1c79356b | 9686 | extlen = PFKEY_UNUNIT64(ext->sadb_ext_len); |
eb6b6ca3 A |
9687 | if (off + extlen > end) { |
9688 | ipseclog((LOG_DEBUG, | |
9689 | "key_align: ext type %u invalid ext length %d " | |
f427ee49 | 9690 | "offset %d sadb message total len %zu is passed.\n", |
eb6b6ca3 A |
9691 | ext->sadb_ext_type, extlen, off, end)); |
9692 | bzero_mbuf(m); | |
9693 | m_freem(m); | |
9694 | PFKEY_STAT_INCREMENT(pfkeystat.out_invlen); | |
9695 | return EINVAL; | |
9696 | } | |
0a7de745 | 9697 | |
9bccf70c | 9698 | if (key_validate_ext(ext, extlen)) { |
3e170ce0 | 9699 | bzero_mbuf(m); |
9bccf70c | 9700 | m_freem(m); |
2d21ac55 | 9701 | PFKEY_STAT_INCREMENT(pfkeystat.out_invlen); |
9bccf70c A |
9702 | return EINVAL; |
9703 | } | |
0a7de745 | 9704 | |
9bccf70c A |
9705 | n = m_pulldown(m, off, extlen, &toff); |
9706 | if (!n) { | |
9707 | /* m is already freed */ | |
9708 | return ENOBUFS; | |
9709 | } | |
316670eb | 9710 | ext = (struct sadb_ext *)(void *)(mtod(n, caddr_t) + toff); |
0a7de745 | 9711 | |
9bccf70c A |
9712 | mhp->ext[ext->sadb_ext_type] = ext; |
9713 | mhp->extoff[ext->sadb_ext_type] = off; | |
9714 | mhp->extlen[ext->sadb_ext_type] = extlen; | |
9715 | } | |
0a7de745 | 9716 | |
9bccf70c | 9717 | if (off != end) { |
3e170ce0 | 9718 | bzero_mbuf(m); |
9bccf70c | 9719 | m_freem(m); |
2d21ac55 | 9720 | PFKEY_STAT_INCREMENT(pfkeystat.out_invlen); |
9bccf70c A |
9721 | return EINVAL; |
9722 | } | |
0a7de745 | 9723 | |
9bccf70c A |
9724 | return 0; |
9725 | } | |
9726 | ||
9727 | static int | |
6d2010ae | 9728 | key_validate_ext( |
0a7de745 A |
9729 | const struct sadb_ext *ext, |
9730 | int len) | |
9bccf70c A |
9731 | { |
9732 | struct sockaddr *sa; | |
9733 | enum { NONE, ADDR } checktype = NONE; | |
5ba3f43e | 9734 | int baselen = 0; |
9bccf70c | 9735 | const int sal = offsetof(struct sockaddr, sa_len) + sizeof(sa->sa_len); |
0a7de745 A |
9736 | |
9737 | if (len != PFKEY_UNUNIT64(ext->sadb_ext_len)) { | |
9bccf70c | 9738 | return EINVAL; |
0a7de745 A |
9739 | } |
9740 | ||
9bccf70c A |
9741 | /* if it does not match minimum/maximum length, bail */ |
9742 | if (ext->sadb_ext_type >= sizeof(minsize) / sizeof(minsize[0]) || | |
0a7de745 | 9743 | ext->sadb_ext_type >= sizeof(maxsize) / sizeof(maxsize[0])) { |
9bccf70c | 9744 | return EINVAL; |
0a7de745 A |
9745 | } |
9746 | if (!minsize[ext->sadb_ext_type] || len < minsize[ext->sadb_ext_type]) { | |
9bccf70c | 9747 | return EINVAL; |
0a7de745 A |
9748 | } |
9749 | if (maxsize[ext->sadb_ext_type] && len > maxsize[ext->sadb_ext_type]) { | |
9bccf70c | 9750 | return EINVAL; |
0a7de745 A |
9751 | } |
9752 | ||
9bccf70c A |
9753 | /* more checks based on sadb_ext_type XXX need more */ |
9754 | switch (ext->sadb_ext_type) { | |
0a7de745 A |
9755 | case SADB_EXT_ADDRESS_SRC: |
9756 | case SADB_EXT_ADDRESS_DST: | |
9757 | case SADB_EXT_ADDRESS_PROXY: | |
9758 | case SADB_X_EXT_ADDR_RANGE_SRC_START: | |
9759 | case SADB_X_EXT_ADDR_RANGE_SRC_END: | |
9760 | case SADB_X_EXT_ADDR_RANGE_DST_START: | |
9761 | case SADB_X_EXT_ADDR_RANGE_DST_END: | |
9762 | case SADB_EXT_MIGRATE_ADDRESS_SRC: | |
9763 | case SADB_EXT_MIGRATE_ADDRESS_DST: | |
9764 | baselen = PFKEY_ALIGN8(sizeof(struct sadb_address)); | |
9765 | checktype = ADDR; | |
9766 | break; | |
9767 | case SADB_EXT_IDENTITY_SRC: | |
9768 | case SADB_EXT_IDENTITY_DST: | |
9769 | if (((struct sadb_ident *)(uintptr_t)(size_t)ext)-> | |
9770 | sadb_ident_type == SADB_X_IDENTTYPE_ADDR) { | |
9771 | baselen = PFKEY_ALIGN8(sizeof(struct sadb_ident)); | |
9bccf70c | 9772 | checktype = ADDR; |
0a7de745 | 9773 | } else { |
9bccf70c | 9774 | checktype = NONE; |
0a7de745 A |
9775 | } |
9776 | break; | |
9777 | default: | |
9778 | checktype = NONE; | |
9779 | break; | |
9bccf70c | 9780 | } |
0a7de745 | 9781 | |
9bccf70c | 9782 | switch (checktype) { |
0a7de745 A |
9783 | case NONE: |
9784 | break; | |
9785 | case ADDR: | |
9786 | sa = (struct sockaddr *)((caddr_t)(uintptr_t)ext + baselen); | |
9787 | ||
9788 | if (len < baselen + sal) { | |
9789 | return EINVAL; | |
9790 | } | |
9791 | if (baselen + PFKEY_ALIGN8(sa->sa_len) != len) { | |
9792 | return EINVAL; | |
9793 | } | |
9794 | break; | |
1c79356b | 9795 | } |
b226f5e5 A |
9796 | |
9797 | /* check key bits length */ | |
9798 | if (ext->sadb_ext_type == SADB_EXT_KEY_AUTH || | |
9799 | ext->sadb_ext_type == SADB_EXT_KEY_ENCRYPT) { | |
9800 | struct sadb_key *key = (struct sadb_key *)(uintptr_t)ext; | |
9801 | if (len < (sizeof(struct sadb_key) + _KEYLEN(key))) { | |
9802 | return EINVAL; | |
9803 | } | |
9804 | } | |
0a7de745 | 9805 | |
1c79356b A |
9806 | return 0; |
9807 | } | |
9808 | ||
1c79356b A |
9809 | /* |
9810 | * XXX: maybe This function is called after INBOUND IPsec processing. | |
9811 | * | |
9812 | * Special check for tunnel-mode packets. | |
9813 | * We must make some checks for consistency between inner and outer IP header. | |
9814 | * | |
9815 | * xxx more checks to be provided | |
9816 | */ | |
9817 | int | |
2d21ac55 | 9818 | key_checktunnelsanity( |
0a7de745 A |
9819 | struct secasvar *sav, |
9820 | __unused u_int family, | |
9821 | __unused caddr_t src, | |
9822 | __unused caddr_t dst) | |
1c79356b A |
9823 | { |
9824 | /* sanity check */ | |
0a7de745 | 9825 | if (sav->sah == NULL) { |
1c79356b | 9826 | panic("sav->sah == NULL at key_checktunnelsanity"); |
0a7de745 A |
9827 | } |
9828 | ||
1c79356b | 9829 | /* XXX: check inner IP header */ |
0a7de745 | 9830 | |
1c79356b A |
9831 | return 1; |
9832 | } | |
9833 | ||
1c79356b A |
9834 | /* record data transfer on SA, and update timestamps */ |
9835 | void | |
6d2010ae | 9836 | key_sa_recordxfer( |
0a7de745 A |
9837 | struct secasvar *sav, |
9838 | struct mbuf *m) | |
1c79356b | 9839 | { |
0a7de745 | 9840 | if (!sav) { |
1c79356b | 9841 | panic("key_sa_recordxfer called with sav == NULL"); |
0a7de745 A |
9842 | } |
9843 | if (!m) { | |
1c79356b | 9844 | panic("key_sa_recordxfer called with m == NULL"); |
0a7de745 A |
9845 | } |
9846 | if (!sav->lft_c) { | |
1c79356b | 9847 | return; |
0a7de745 A |
9848 | } |
9849 | ||
2d21ac55 | 9850 | lck_mtx_lock(sadb_mutex); |
1c79356b A |
9851 | /* |
9852 | * XXX Currently, there is a difference of bytes size | |
9853 | * between inbound and outbound processing. | |
9854 | */ | |
9855 | sav->lft_c->sadb_lifetime_bytes += m->m_pkthdr.len; | |
9856 | /* to check bytes lifetime is done in key_timehandler(). */ | |
0a7de745 | 9857 | |
1c79356b A |
9858 | /* |
9859 | * We use the number of packets as the unit of | |
9860 | * sadb_lifetime_allocations. We increment the variable | |
9861 | * whenever {esp,ah}_{in,out}put is called. | |
9862 | */ | |
9863 | sav->lft_c->sadb_lifetime_allocations++; | |
9864 | /* XXX check for expires? */ | |
0a7de745 | 9865 | |
1c79356b A |
9866 | /* |
9867 | * NOTE: We record CURRENT sadb_lifetime_usetime by using wall clock, | |
9868 | * in seconds. HARD and SOFT lifetime are measured by the time | |
9869 | * difference (again in seconds) from sadb_lifetime_usetime. | |
9870 | * | |
9871 | * usetime | |
9872 | * v expire expire | |
9873 | * -----+-----+--------+---> t | |
9874 | * <--------------> HARD | |
9875 | * <-----> SOFT | |
9876 | */ | |
0a7de745 | 9877 | { |
39236c6e A |
9878 | struct timeval tv; |
9879 | microtime(&tv); | |
9880 | sav->lft_c->sadb_lifetime_usetime = tv.tv_sec; | |
9881 | /* XXX check for expires? */ | |
0a7de745 | 9882 | } |
2d21ac55 | 9883 | lck_mtx_unlock(sadb_mutex); |
0a7de745 | 9884 | |
1c79356b A |
9885 | return; |
9886 | } | |
9887 | ||
9888 | /* dumb version */ | |
9889 | void | |
6d2010ae | 9890 | key_sa_routechange( |
0a7de745 | 9891 | struct sockaddr *dst) |
1c79356b A |
9892 | { |
9893 | struct secashead *sah; | |
9894 | struct route *ro; | |
0a7de745 | 9895 | |
91447636 | 9896 | lck_mtx_lock(sadb_mutex); |
1c79356b | 9897 | LIST_FOREACH(sah, &sahtree, chain) { |
5c9f4661 | 9898 | ro = (struct route *)&sah->sa_route; |
1c79356b | 9899 | if (ro->ro_rt && dst->sa_len == ro->ro_dst.sa_len |
0a7de745 | 9900 | && bcmp(dst, &ro->ro_dst, dst->sa_len) == 0) { |
39236c6e | 9901 | ROUTE_RELEASE(ro); |
1c79356b A |
9902 | } |
9903 | } | |
91447636 | 9904 | lck_mtx_unlock(sadb_mutex); |
0a7de745 | 9905 | |
1c79356b A |
9906 | return; |
9907 | } | |
9908 | ||
316670eb | 9909 | void |
6d2010ae | 9910 | key_sa_chgstate( |
0a7de745 A |
9911 | struct secasvar *sav, |
9912 | u_int8_t state) | |
1c79356b | 9913 | { |
0a7de745 | 9914 | if (sav == NULL) { |
1c79356b | 9915 | panic("key_sa_chgstate called with sav == NULL"); |
0a7de745 A |
9916 | } |
9917 | ||
9918 | if (sav->state == state) { | |
1c79356b | 9919 | return; |
0a7de745 A |
9920 | } |
9921 | ||
5ba3f43e | 9922 | LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_OWNED); |
0a7de745 A |
9923 | |
9924 | if (__LIST_CHAINED(sav)) { | |
1c79356b | 9925 | LIST_REMOVE(sav, chain); |
0a7de745 A |
9926 | } |
9927 | ||
1c79356b A |
9928 | sav->state = state; |
9929 | LIST_INSERT_HEAD(&sav->sah->savtree[state], sav, chain); | |
9930 | } | |
9931 | ||
9bccf70c | 9932 | void |
6d2010ae | 9933 | key_sa_stir_iv( |
0a7de745 | 9934 | struct secasvar *sav) |
1c79356b | 9935 | { |
2d21ac55 | 9936 | lck_mtx_lock(sadb_mutex); |
0a7de745 | 9937 | if (!sav->iv) { |
9bccf70c | 9938 | panic("key_sa_stir_iv called with sav == NULL"); |
0a7de745 | 9939 | } |
9bccf70c | 9940 | key_randomfill(sav->iv, sav->ivlen); |
2d21ac55 | 9941 | lck_mtx_unlock(sadb_mutex); |
1c79356b A |
9942 | } |
9943 | ||
9bccf70c A |
9944 | /* XXX too much? */ |
9945 | static struct mbuf * | |
6d2010ae | 9946 | key_alloc_mbuf( |
0a7de745 | 9947 | int l) |
1c79356b | 9948 | { |
9bccf70c A |
9949 | struct mbuf *m = NULL, *n; |
9950 | int len, t; | |
0a7de745 | 9951 | |
9bccf70c A |
9952 | len = l; |
9953 | while (len > 0) { | |
9954 | MGET(n, M_DONTWAIT, MT_DATA); | |
0a7de745 | 9955 | if (n && len > MLEN) { |
9bccf70c | 9956 | MCLGET(n, M_DONTWAIT); |
0a7de745 | 9957 | } |
9bccf70c A |
9958 | if (!n) { |
9959 | m_freem(m); | |
9960 | return NULL; | |
9961 | } | |
0a7de745 | 9962 | |
9bccf70c A |
9963 | n->m_next = NULL; |
9964 | n->m_len = 0; | |
f427ee49 | 9965 | n->m_len = (int)M_TRAILINGSPACE(n); |
9bccf70c A |
9966 | /* use the bottom of mbuf, hoping we can prepend afterwards */ |
9967 | if (n->m_len > len) { | |
9968 | t = (n->m_len - len) & ~(sizeof(long) - 1); | |
9969 | n->m_data += t; | |
9970 | n->m_len = len; | |
9971 | } | |
0a7de745 | 9972 | |
9bccf70c | 9973 | len -= n->m_len; |
0a7de745 A |
9974 | |
9975 | if (m) { | |
9bccf70c | 9976 | m_cat(m, n); |
0a7de745 | 9977 | } else { |
9bccf70c | 9978 | m = n; |
0a7de745 | 9979 | } |
1c79356b | 9980 | } |
0a7de745 | 9981 | |
9bccf70c | 9982 | return m; |
1c79356b | 9983 | } |
593a1d5f | 9984 | |
b0d623f7 | 9985 | static struct mbuf * |
0a7de745 A |
9986 | key_setdumpsastats(u_int32_t dir, |
9987 | struct sastat *stats, | |
9988 | u_int32_t max_stats, | |
9989 | u_int64_t session_ids[], | |
9990 | u_int32_t seq, | |
9991 | u_int32_t pid) | |
b0d623f7 | 9992 | { |
39236c6e | 9993 | struct mbuf *result = NULL, *m = NULL; |
0a7de745 | 9994 | |
39236c6e A |
9995 | m = key_setsadbmsg(SADB_GETSASTAT, 0, 0, seq, pid, 0); |
9996 | if (!m) { | |
9997 | goto fail; | |
b0d623f7 | 9998 | } |
39236c6e | 9999 | result = m; |
0a7de745 | 10000 | |
b0d623f7 A |
10001 | m = key_setsadbsession_id(session_ids); |
10002 | if (!m) { | |
39236c6e | 10003 | goto fail; |
b0d623f7 | 10004 | } |
39236c6e | 10005 | m_cat(result, m); |
0a7de745 | 10006 | |
b0d623f7 | 10007 | m = key_setsadbsastat(dir, |
0a7de745 A |
10008 | stats, |
10009 | max_stats); | |
b0d623f7 | 10010 | if (!m) { |
39236c6e | 10011 | goto fail; |
b0d623f7 | 10012 | } |
39236c6e | 10013 | m_cat(result, m); |
0a7de745 | 10014 | |
39236c6e | 10015 | if ((result->m_flags & M_PKTHDR) == 0) { |
b0d623f7 | 10016 | goto fail; |
39236c6e | 10017 | } |
0a7de745 | 10018 | |
39236c6e A |
10019 | if (result->m_len < sizeof(struct sadb_msg)) { |
10020 | result = m_pullup(result, sizeof(struct sadb_msg)); | |
b0d623f7 A |
10021 | if (result == NULL) { |
10022 | goto fail; | |
10023 | } | |
593a1d5f | 10024 | } |
0a7de745 | 10025 | |
39236c6e A |
10026 | result->m_pkthdr.len = 0; |
10027 | for (m = result; m; m = m->m_next) { | |
10028 | result->m_pkthdr.len += m->m_len; | |
10029 | } | |
0a7de745 | 10030 | |
f427ee49 A |
10031 | if (PFKEY_UNIT64(result->m_pkthdr.len) > UINT16_MAX) { |
10032 | ipseclog((LOG_ERR, "key_setdumpsastats: length too nbug: %u", result->m_pkthdr.len)); | |
10033 | goto fail; | |
10034 | } | |
10035 | ||
39236c6e | 10036 | mtod(result, struct sadb_msg *)->sadb_msg_len = |
f427ee49 | 10037 | (u_int16_t)PFKEY_UNIT64(result->m_pkthdr.len); |
0a7de745 | 10038 | |
39236c6e | 10039 | return result; |
0a7de745 | 10040 | |
39236c6e | 10041 | fail: |
b0d623f7 | 10042 | if (result) { |
39236c6e | 10043 | m_freem(result); |
593a1d5f | 10044 | } |
39236c6e | 10045 | return NULL; |
593a1d5f A |
10046 | } |
10047 | ||
10048 | /* | |
b0d623f7 A |
10049 | * SADB_GETSASTAT processing |
10050 | * dump all stats for matching entries in SAD. | |
10051 | * | |
10052 | * m will always be freed. | |
593a1d5f | 10053 | */ |
39236c6e | 10054 | |
b0d623f7 | 10055 | static int |
0a7de745 A |
10056 | key_getsastat(struct socket *so, |
10057 | struct mbuf *m, | |
10058 | const struct sadb_msghdr *mhp) | |
b0d623f7 A |
10059 | { |
10060 | struct sadb_session_id *session_id; | |
f427ee49 | 10061 | size_t bufsize = 0; |
eb6b6ca3 | 10062 | u_int32_t arg_count, res_count; |
b0d623f7 A |
10063 | struct sadb_sastat *sa_stats_arg; |
10064 | struct sastat *sa_stats_sav = NULL; | |
10065 | struct mbuf *n; | |
10066 | int error = 0; | |
0a7de745 | 10067 | |
b0d623f7 | 10068 | /* sanity check */ |
0a7de745 | 10069 | if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL) { |
39236c6e | 10070 | panic("%s: NULL pointer is passed.\n", __FUNCTION__); |
0a7de745 A |
10071 | } |
10072 | ||
39236c6e A |
10073 | if (mhp->ext[SADB_EXT_SESSION_ID] == NULL) { |
10074 | printf("%s: invalid message is passed. missing session-id.\n", __FUNCTION__); | |
b0d623f7 | 10075 | return key_senderror(so, m, EINVAL); |
39236c6e | 10076 | } |
b0d623f7 | 10077 | if (mhp->extlen[SADB_EXT_SESSION_ID] < sizeof(struct sadb_session_id)) { |
39236c6e | 10078 | printf("%s: invalid message is passed. short session-id.\n", __FUNCTION__); |
b0d623f7 | 10079 | return key_senderror(so, m, EINVAL); |
39236c6e | 10080 | } |
b0d623f7 | 10081 | if (mhp->ext[SADB_EXT_SASTAT] == NULL) { |
39236c6e | 10082 | printf("%s: invalid message is passed. missing stat args.\n", __FUNCTION__); |
b0d623f7 | 10083 | return key_senderror(so, m, EINVAL); |
39236c6e A |
10084 | } |
10085 | if (mhp->extlen[SADB_EXT_SASTAT] < sizeof(*sa_stats_arg)) { | |
10086 | printf("%s: invalid message is passed. short stat args.\n", __FUNCTION__); | |
b0d623f7 | 10087 | return key_senderror(so, m, EINVAL); |
39236c6e | 10088 | } |
0a7de745 | 10089 | |
5ba3f43e | 10090 | LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_NOTOWNED); |
0a7de745 | 10091 | |
b0d623f7 | 10092 | // exit early if there are no active SAs |
eb6b6ca3 | 10093 | if (ipsec_sav_count == 0) { |
39236c6e | 10094 | printf("%s: No active SAs.\n", __FUNCTION__); |
b0d623f7 A |
10095 | error = ENOENT; |
10096 | goto end; | |
593a1d5f | 10097 | } |
eb6b6ca3 A |
10098 | |
10099 | if (os_mul_overflow(ipsec_sav_count + 1, sizeof(*sa_stats_sav), &bufsize)) { | |
10100 | panic("key_getsastat bufsize requested memory overflow %u\n", ipsec_sav_count); | |
10101 | } | |
0a7de745 | 10102 | |
b0d623f7 A |
10103 | KMALLOC_WAIT(sa_stats_sav, __typeof__(sa_stats_sav), bufsize); |
10104 | if (sa_stats_sav == NULL) { | |
39236c6e | 10105 | printf("%s: No more memory.\n", __FUNCTION__); |
b0d623f7 A |
10106 | error = ENOMEM; |
10107 | goto end; | |
593a1d5f | 10108 | } |
b0d623f7 | 10109 | bzero(sa_stats_sav, bufsize); |
0a7de745 | 10110 | |
39236c6e | 10111 | sa_stats_arg = (__typeof__(sa_stats_arg)) |
0a7de745 | 10112 | (void *)mhp->ext[SADB_EXT_SASTAT]; |
b0d623f7 A |
10113 | arg_count = sa_stats_arg->sadb_sastat_list_len; |
10114 | // exit early if there are no requested SAs | |
10115 | if (arg_count == 0) { | |
39236c6e | 10116 | printf("%s: No SAs requested.\n", __FUNCTION__); |
b0d623f7 A |
10117 | error = ENOENT; |
10118 | goto end; | |
593a1d5f | 10119 | } |
eb6b6ca3 A |
10120 | if (PFKEY_UNUNIT64(sa_stats_arg->sadb_sastat_len) < (sizeof(*sa_stats_arg) + |
10121 | (arg_count * sizeof(struct sastat)))) { | |
10122 | printf("%s: invalid message is passed. sa stat extlen shorter than requested stat length.\n", __FUNCTION__); | |
10123 | error = EINVAL; | |
10124 | goto end; | |
10125 | } | |
10126 | ||
b0d623f7 | 10127 | res_count = 0; |
0a7de745 | 10128 | |
b0d623f7 | 10129 | if (key_getsastatbyspi((struct sastat *)(sa_stats_arg + 1), |
0a7de745 A |
10130 | arg_count, |
10131 | sa_stats_sav, | |
10132 | bufsize, | |
10133 | &res_count)) { | |
39236c6e | 10134 | printf("%s: Error finding SAs.\n", __FUNCTION__); |
b0d623f7 A |
10135 | error = ENOENT; |
10136 | goto end; | |
593a1d5f | 10137 | } |
b0d623f7 | 10138 | if (!res_count) { |
39236c6e | 10139 | printf("%s: No SAs found.\n", __FUNCTION__); |
b0d623f7 A |
10140 | error = ENOENT; |
10141 | goto end; | |
593a1d5f | 10142 | } |
0a7de745 | 10143 | |
316670eb | 10144 | session_id = (__typeof__(session_id)) |
0a7de745 A |
10145 | (void *)mhp->ext[SADB_EXT_SESSION_ID]; |
10146 | ||
b0d623f7 A |
10147 | /* send this to the userland. */ |
10148 | n = key_setdumpsastats(sa_stats_arg->sadb_sastat_dir, | |
0a7de745 A |
10149 | sa_stats_sav, |
10150 | res_count, | |
10151 | session_id->sadb_session_id_v, | |
10152 | mhp->msg->sadb_msg_seq, | |
10153 | mhp->msg->sadb_msg_pid); | |
39236c6e A |
10154 | if (!n) { |
10155 | printf("%s: No bufs to dump stats.\n", __FUNCTION__); | |
b0d623f7 A |
10156 | error = ENOBUFS; |
10157 | goto end; | |
593a1d5f | 10158 | } |
0a7de745 | 10159 | |
39236c6e | 10160 | key_sendup_mbuf(so, n, KEY_SENDUP_ALL); |
b0d623f7 A |
10161 | end: |
10162 | if (sa_stats_sav) { | |
10163 | KFREE(sa_stats_sav); | |
593a1d5f | 10164 | } |
0a7de745 A |
10165 | |
10166 | if (error) { | |
b0d623f7 | 10167 | return key_senderror(so, m, error); |
0a7de745 A |
10168 | } |
10169 | ||
b0d623f7 | 10170 | m_freem(m); |
593a1d5f A |
10171 | return 0; |
10172 | } | |
10173 | ||
10174 | static void | |
0a7de745 A |
10175 | key_update_natt_keepalive_timestamp(struct secasvar *sav_sent, |
10176 | struct secasvar *sav_update) | |
593a1d5f | 10177 | { |
b0d623f7 | 10178 | struct secasindex saidx_swap_sent_addr; |
0a7de745 | 10179 | |
b0d623f7 A |
10180 | // exit early if two SAs are identical, or if sav_update is current |
10181 | if (sav_sent == sav_update || | |
10182 | sav_update->natt_last_activity == natt_now) { | |
10183 | return; | |
10184 | } | |
0a7de745 | 10185 | |
b0d623f7 | 10186 | // assuming that (sav_update->remote_ike_port != 0 && (esp_udp_encap_port & 0xFFFF) != 0) |
0a7de745 | 10187 | |
b0d623f7 A |
10188 | bzero(&saidx_swap_sent_addr, sizeof(saidx_swap_sent_addr)); |
10189 | memcpy(&saidx_swap_sent_addr.src, &sav_sent->sah->saidx.dst, sizeof(saidx_swap_sent_addr.src)); | |
10190 | memcpy(&saidx_swap_sent_addr.dst, &sav_sent->sah->saidx.src, sizeof(saidx_swap_sent_addr.dst)); | |
10191 | saidx_swap_sent_addr.proto = sav_sent->sah->saidx.proto; | |
10192 | saidx_swap_sent_addr.mode = sav_sent->sah->saidx.mode; | |
10193 | // we ignore reqid for split-tunnel setups | |
0a7de745 | 10194 | |
b0d623f7 A |
10195 | if (key_cmpsaidx(&sav_sent->sah->saidx, &sav_update->sah->saidx, CMP_MODE | CMP_PORT) || |
10196 | key_cmpsaidx(&saidx_swap_sent_addr, &sav_update->sah->saidx, CMP_MODE | CMP_PORT)) { | |
10197 | sav_update->natt_last_activity = natt_now; | |
593a1d5f | 10198 | } |
593a1d5f | 10199 | } |
39236c6e A |
10200 | |
10201 | static int | |
0a7de745 A |
10202 | key_send_delsp(struct secpolicy *sp) |
10203 | { | |
10204 | struct mbuf *result = NULL, *m; | |
10205 | ||
10206 | if (sp == NULL) { | |
10207 | goto fail; | |
10208 | } | |
10209 | ||
10210 | /* set msg header */ | |
10211 | m = key_setsadbmsg(SADB_X_SPDDELETE, 0, 0, 0, 0, 0); | |
10212 | if (!m) { | |
10213 | goto fail; | |
10214 | } | |
10215 | result = m; | |
10216 | ||
10217 | /* set sadb_address(es) for source */ | |
10218 | if (sp->spidx.src_range.start.ss_len > 0) { | |
10219 | m = key_setsadbaddr(SADB_X_EXT_ADDR_RANGE_SRC_START, | |
10220 | (struct sockaddr *)&sp->spidx.src_range.start, sp->spidx.prefs, | |
10221 | sp->spidx.ul_proto); | |
10222 | if (!m) { | |
10223 | goto fail; | |
10224 | } | |
10225 | m_cat(result, m); | |
10226 | ||
10227 | m = key_setsadbaddr(SADB_X_EXT_ADDR_RANGE_SRC_END, | |
10228 | (struct sockaddr *)&sp->spidx.src_range.end, sp->spidx.prefs, | |
10229 | sp->spidx.ul_proto); | |
10230 | if (!m) { | |
10231 | goto fail; | |
10232 | } | |
10233 | m_cat(result, m); | |
10234 | } else { | |
10235 | m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC, | |
10236 | (struct sockaddr *)&sp->spidx.src, sp->spidx.prefs, | |
10237 | sp->spidx.ul_proto); | |
10238 | if (!m) { | |
10239 | goto fail; | |
10240 | } | |
10241 | m_cat(result, m); | |
10242 | } | |
10243 | ||
10244 | /* set sadb_address(es) for destination */ | |
10245 | if (sp->spidx.dst_range.start.ss_len > 0) { | |
10246 | m = key_setsadbaddr(SADB_X_EXT_ADDR_RANGE_DST_START, | |
10247 | (struct sockaddr *)&sp->spidx.dst_range.start, sp->spidx.prefd, | |
10248 | sp->spidx.ul_proto); | |
10249 | if (!m) { | |
10250 | goto fail; | |
10251 | } | |
10252 | m_cat(result, m); | |
10253 | ||
10254 | m = key_setsadbaddr(SADB_X_EXT_ADDR_RANGE_DST_END, | |
10255 | (struct sockaddr *)&sp->spidx.dst_range.end, sp->spidx.prefd, | |
10256 | sp->spidx.ul_proto); | |
10257 | if (!m) { | |
10258 | goto fail; | |
10259 | } | |
10260 | m_cat(result, m); | |
10261 | } else { | |
10262 | m = key_setsadbaddr(SADB_EXT_ADDRESS_DST, | |
10263 | (struct sockaddr *)&sp->spidx.dst, sp->spidx.prefd, | |
10264 | sp->spidx.ul_proto); | |
10265 | if (!m) { | |
10266 | goto fail; | |
10267 | } | |
10268 | m_cat(result, m); | |
10269 | } | |
10270 | ||
10271 | /* set secpolicy */ | |
10272 | m = key_sp2msg(sp); | |
10273 | if (!m) { | |
10274 | goto fail; | |
10275 | } | |
10276 | m_cat(result, m); | |
10277 | ||
10278 | if ((result->m_flags & M_PKTHDR) == 0) { | |
10279 | goto fail; | |
10280 | } | |
10281 | ||
10282 | if (result->m_len < sizeof(struct sadb_msg)) { | |
10283 | result = m_pullup(result, sizeof(struct sadb_msg)); | |
10284 | if (result == NULL) { | |
10285 | goto fail; | |
10286 | } | |
10287 | } | |
10288 | ||
39236c6e | 10289 | result->m_pkthdr.len = 0; |
0a7de745 | 10290 | for (m = result; m; m = m->m_next) { |
39236c6e | 10291 | result->m_pkthdr.len += m->m_len; |
0a7de745 A |
10292 | } |
10293 | ||
f427ee49 A |
10294 | if (PFKEY_UNIT64(result->m_pkthdr.len) >= UINT16_MAX) { |
10295 | ipseclog((LOG_ERR, "key_send_delsp: length too big: %d", result->m_pkthdr.len)); | |
10296 | goto fail; | |
10297 | } | |
10298 | ||
10299 | mtod(result, struct sadb_msg *)->sadb_msg_len = (u_int16_t)PFKEY_UNIT64(result->m_pkthdr.len); | |
0a7de745 | 10300 | |
39236c6e | 10301 | return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED); |
0a7de745 | 10302 | |
39236c6e | 10303 | fail: |
0a7de745 | 10304 | if (result) { |
39236c6e | 10305 | m_free(result); |
0a7de745 | 10306 | } |
39236c6e A |
10307 | return -1; |
10308 | } | |
10309 | ||
10310 | void | |
0a7de745 | 10311 | key_delsp_for_ipsec_if(ifnet_t ipsec_if) |
39236c6e | 10312 | { |
fe8ab488 A |
10313 | struct secashead *sah; |
10314 | struct secasvar *sav, *nextsav; | |
10315 | u_int stateidx; | |
10316 | u_int state; | |
39236c6e A |
10317 | struct secpolicy *sp, *nextsp; |
10318 | int dir; | |
0a7de745 A |
10319 | |
10320 | if (ipsec_if == NULL) { | |
10321 | return; | |
10322 | } | |
10323 | ||
5ba3f43e | 10324 | LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_NOTOWNED); |
0a7de745 | 10325 | |
39236c6e | 10326 | lck_mtx_lock(sadb_mutex); |
0a7de745 | 10327 | |
39236c6e A |
10328 | for (dir = 0; dir < IPSEC_DIR_MAX; dir++) { |
10329 | for (sp = LIST_FIRST(&sptree[dir]); | |
0a7de745 A |
10330 | sp != NULL; |
10331 | sp = nextsp) { | |
39236c6e | 10332 | nextsp = LIST_NEXT(sp, chain); |
0a7de745 | 10333 | |
39236c6e A |
10334 | if (sp->ipsec_if == ipsec_if) { |
10335 | ifnet_release(sp->ipsec_if); | |
10336 | sp->ipsec_if = NULL; | |
0a7de745 | 10337 | |
39236c6e | 10338 | key_send_delsp(sp); |
0a7de745 | 10339 | |
39236c6e | 10340 | sp->state = IPSEC_SPSTATE_DEAD; |
0a7de745 | 10341 | key_freesp(sp, KEY_SADB_LOCKED); |
39236c6e A |
10342 | } |
10343 | } | |
10344 | } | |
0a7de745 | 10345 | |
fe8ab488 A |
10346 | LIST_FOREACH(sah, &sahtree, chain) { |
10347 | if (sah->ipsec_if == ipsec_if) { | |
cb323159 | 10348 | /* This SAH is linked to the IPsec interface. It now needs to close. */ |
fe8ab488 A |
10349 | ifnet_release(sah->ipsec_if); |
10350 | sah->ipsec_if = NULL; | |
0a7de745 | 10351 | |
fe8ab488 A |
10352 | for (stateidx = 0; stateidx < _ARRAYLEN(saorder_state_alive); stateidx++) { |
10353 | state = saorder_state_any[stateidx]; | |
10354 | for (sav = LIST_FIRST(&sah->savtree[state]); sav != NULL; sav = nextsav) { | |
10355 | nextsav = LIST_NEXT(sav, chain); | |
0a7de745 | 10356 | |
fe8ab488 A |
10357 | key_sa_chgstate(sav, SADB_SASTATE_DEAD); |
10358 | key_freesav(sav, KEY_SADB_LOCKED); | |
10359 | } | |
10360 | } | |
0a7de745 | 10361 | |
fe8ab488 A |
10362 | sah->state = SADB_SASTATE_DEAD; |
10363 | } | |
10364 | } | |
0a7de745 | 10365 | |
39236c6e | 10366 | lck_mtx_unlock(sadb_mutex); |
fe8ab488 A |
10367 | } |
10368 | ||
10369 | __private_extern__ u_int32_t | |
0a7de745 | 10370 | key_fill_offload_frames_for_savs(ifnet_t ifp, |
3e170ce0 A |
10371 | struct ifnet_keepalive_offload_frame *frames_array, |
10372 | u_int32_t frames_array_count, | |
10373 | size_t frame_data_offset) | |
fe8ab488 A |
10374 | { |
10375 | struct secashead *sah = NULL; | |
10376 | struct secasvar *sav = NULL; | |
3e170ce0 | 10377 | struct ifnet_keepalive_offload_frame *frame = frames_array; |
fe8ab488 A |
10378 | u_int32_t frame_index = 0; |
10379 | ||
10380 | if (frame == NULL || frames_array_count == 0) { | |
0a7de745 | 10381 | return frame_index; |
fe8ab488 A |
10382 | } |
10383 | ||
10384 | lck_mtx_lock(sadb_mutex); | |
10385 | LIST_FOREACH(sah, &sahtree, chain) { | |
10386 | LIST_FOREACH(sav, &sah->savtree[SADB_SASTATE_MATURE], chain) { | |
10387 | if (ipsec_fill_offload_frame(ifp, sav, frame, frame_data_offset)) { | |
10388 | frame_index++; | |
10389 | if (frame_index >= frames_array_count) { | |
10390 | lck_mtx_unlock(sadb_mutex); | |
0a7de745 | 10391 | return frame_index; |
fe8ab488 A |
10392 | } |
10393 | frame = &(frames_array[frame_index]); | |
10394 | } | |
10395 | } | |
10396 | } | |
10397 | lck_mtx_unlock(sadb_mutex); | |
10398 | ||
0a7de745 | 10399 | return frame_index; |
39236c6e | 10400 | } |
ea3f0419 A |
10401 | |
10402 | #pragma mark Custom IPsec | |
10403 | ||
10404 | __private_extern__ bool | |
10405 | key_custom_ipsec_token_is_valid(void *ipsec_token) | |
10406 | { | |
10407 | if (ipsec_token == NULL) { | |
10408 | return false; | |
10409 | } | |
10410 | ||
10411 | struct secashead *sah = (struct secashead *)ipsec_token; | |
10412 | ||
10413 | return (sah->flags & SECURITY_ASSOCIATION_CUSTOM_IPSEC) == SECURITY_ASSOCIATION_CUSTOM_IPSEC; | |
10414 | } | |
10415 | ||
10416 | __private_extern__ int | |
10417 | key_reserve_custom_ipsec(void **ipsec_token, union sockaddr_in_4_6 *src, union sockaddr_in_4_6 *dst, | |
10418 | u_int8_t proto) | |
10419 | { | |
10420 | if (src == NULL || dst == NULL) { | |
10421 | ipseclog((LOG_ERR, "register custom ipsec: invalid address\n")); | |
10422 | return EINVAL; | |
10423 | } | |
10424 | ||
10425 | if (src->sa.sa_family != dst->sa.sa_family) { | |
10426 | ipseclog((LOG_ERR, "register custom ipsec: address family mismatched\n")); | |
10427 | return EINVAL; | |
10428 | } | |
10429 | ||
10430 | if (src->sa.sa_len != dst->sa.sa_len) { | |
10431 | ipseclog((LOG_ERR, "register custom ipsec: address struct size mismatched\n")); | |
10432 | return EINVAL; | |
10433 | } | |
10434 | ||
10435 | if (ipsec_token == NULL) { | |
10436 | ipseclog((LOG_ERR, "register custom ipsec: invalid ipsec token\n")); | |
10437 | return EINVAL; | |
10438 | } | |
10439 | ||
10440 | switch (src->sa.sa_family) { | |
10441 | case AF_INET: | |
10442 | if (src->sa.sa_len != sizeof(struct sockaddr_in)) { | |
10443 | ipseclog((LOG_ERR, "register custom esp: invalid address length\n")); | |
10444 | return EINVAL; | |
10445 | } | |
10446 | break; | |
10447 | case AF_INET6: | |
10448 | if (src->sa.sa_len != sizeof(struct sockaddr_in6)) { | |
10449 | ipseclog((LOG_ERR, "register custom esp: invalid address length\n")); | |
10450 | return EINVAL; | |
10451 | } | |
10452 | break; | |
10453 | default: | |
10454 | ipseclog((LOG_ERR, "register custom esp: invalid address length\n")); | |
10455 | return EAFNOSUPPORT; | |
10456 | } | |
10457 | ||
10458 | if (proto != IPPROTO_ESP && proto != IPPROTO_AH) { | |
10459 | ipseclog((LOG_ERR, "register custom esp: invalid proto %u\n", proto)); | |
10460 | return EINVAL; | |
10461 | } | |
10462 | ||
10463 | struct secasindex saidx = {}; | |
10464 | KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, &src->sa, &dst->sa, 0, &saidx); | |
10465 | ||
10466 | lck_mtx_lock(sadb_mutex); | |
10467 | ||
10468 | struct secashead *sah = NULL; | |
10469 | if ((sah = key_getsah(&saidx, SECURITY_ASSOCIATION_ANY)) != NULL) { | |
10470 | lck_mtx_unlock(sadb_mutex); | |
10471 | ipseclog((LOG_ERR, "register custom esp: SA exists\n")); | |
10472 | return EEXIST; | |
10473 | } | |
10474 | ||
10475 | if ((sah = key_newsah(&saidx, NULL, 0, IPSEC_DIR_ANY, SECURITY_ASSOCIATION_CUSTOM_IPSEC)) == NULL) { | |
10476 | lck_mtx_unlock(sadb_mutex); | |
10477 | ipseclog((LOG_DEBUG, "register custom esp: No more memory.\n")); | |
10478 | return ENOBUFS; | |
10479 | } | |
10480 | ||
10481 | *ipsec_token = (void *)sah; | |
10482 | ||
10483 | lck_mtx_unlock(sadb_mutex); | |
10484 | return 0; | |
10485 | } | |
10486 | ||
10487 | __private_extern__ void | |
10488 | key_release_custom_ipsec(void **ipsec_token) | |
10489 | { | |
10490 | struct secashead *sah = *ipsec_token; | |
10491 | VERIFY(sah != NULL); | |
10492 | ||
10493 | lck_mtx_lock(sadb_mutex); | |
10494 | ||
10495 | VERIFY((sah->flags & SECURITY_ASSOCIATION_CUSTOM_IPSEC) == SECURITY_ASSOCIATION_CUSTOM_IPSEC); | |
10496 | ||
10497 | bool sa_present = true; | |
10498 | if (LIST_FIRST(&sah->savtree[SADB_SASTATE_LARVAL]) == NULL && | |
10499 | LIST_FIRST(&sah->savtree[SADB_SASTATE_MATURE]) == NULL && | |
10500 | LIST_FIRST(&sah->savtree[SADB_SASTATE_DYING]) == NULL && | |
10501 | LIST_FIRST(&sah->savtree[SADB_SASTATE_DEAD]) == NULL) { | |
10502 | sa_present = false; | |
10503 | } | |
10504 | VERIFY(sa_present == false); | |
10505 | ||
10506 | key_delsah(sah); | |
10507 | ||
10508 | lck_mtx_unlock(sadb_mutex); | |
10509 | ||
10510 | *ipsec_token = NULL; | |
10511 | return; | |
10512 | } |