key.c revision 1.148 1 /* $NetBSD: key.c,v 1.148 2017/05/30 01:31:07 ozaki-r Exp $ */
2 /* $FreeBSD: src/sys/netipsec/key.c,v 1.3.2.3 2004/02/14 22:23:23 bms Exp $ */
3 /* $KAME: key.c,v 1.191 2001/06/27 10:46:49 sakane Exp $ */
4
5 /*
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: key.c,v 1.148 2017/05/30 01:31:07 ozaki-r Exp $");
36
37 /*
38 * This code is referd to RFC 2367
39 */
40
41 #if defined(_KERNEL_OPT)
42 #include "opt_inet.h"
43 #include "opt_ipsec.h"
44 #include "opt_gateway.h"
45 #endif
46
47 #include <sys/types.h>
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/callout.h>
51 #include <sys/kernel.h>
52 #include <sys/mbuf.h>
53 #include <sys/domain.h>
54 #include <sys/protosw.h>
55 #include <sys/socket.h>
56 #include <sys/socketvar.h>
57 #include <sys/sysctl.h>
58 #include <sys/errno.h>
59 #include <sys/proc.h>
60 #include <sys/queue.h>
61 #include <sys/syslog.h>
62 #include <sys/once.h>
63 #include <sys/cprng.h>
64 #include <sys/psref.h>
65 #include <sys/lwp.h>
66 #include <sys/workqueue.h>
67 #include <sys/kmem.h>
68 #include <sys/cpu.h>
69 #include <sys/atomic.h>
70
71 #include <net/if.h>
72 #include <net/route.h>
73 #include <net/raw_cb.h>
74
75 #include <netinet/in.h>
76 #include <netinet/in_systm.h>
77 #include <netinet/ip.h>
78 #include <netinet/in_var.h>
79 #ifdef INET
80 #include <netinet/ip_var.h>
81 #endif
82
83 #ifdef INET6
84 #include <netinet/ip6.h>
85 #include <netinet6/in6_var.h>
86 #include <netinet6/ip6_var.h>
87 #endif /* INET6 */
88
89 #ifdef INET
90 #include <netinet/in_pcb.h>
91 #endif
92 #ifdef INET6
93 #include <netinet6/in6_pcb.h>
94 #endif /* INET6 */
95
96 #include <net/pfkeyv2.h>
97 #include <netipsec/keydb.h>
98 #include <netipsec/key.h>
99 #include <netipsec/keysock.h>
100 #include <netipsec/key_debug.h>
101
102 #include <netipsec/ipsec.h>
103 #ifdef INET6
104 #include <netipsec/ipsec6.h>
105 #endif
106 #include <netipsec/ipsec_private.h>
107
108 #include <netipsec/xform.h>
109 #include <netipsec/ipcomp.h>
110
111
112 #include <net/net_osdep.h>
113
114 #define FULLMASK 0xff
115 #define _BITS(bytes) ((bytes) << 3)
116
117 #define PORT_NONE 0
118 #define PORT_LOOSE 1
119 #define PORT_STRICT 2
120
121 percpu_t *pfkeystat_percpu;
122
123 /*
124 * Note on SA reference counting:
125 * - SAs that are not in DEAD state will have (total external reference + 1)
126 * following value in reference count field. they cannot be freed and are
127 * referenced from SA header.
128 * - SAs that are in DEAD state will have (total external reference)
129 * in reference count field. they are ready to be freed. reference from
130 * SA header will be removed in key_delsav(), when the reference count
131 * field hits 0 (= no external reference other than from SA header.
132 */
133
134 u_int32_t key_debug_level = 0;
135 static u_int key_spi_trycnt = 1000;
136 static u_int32_t key_spi_minval = 0x100;
137 static u_int32_t key_spi_maxval = 0x0fffffff; /* XXX */
138 static u_int32_t policy_id = 0;
139 static u_int key_int_random = 60; /*interval to initialize randseed,1(m)*/
140 static u_int key_larval_lifetime = 30; /* interval to expire acquiring, 30(s)*/
141 static int key_blockacq_count = 10; /* counter for blocking SADB_ACQUIRE.*/
142 static int key_blockacq_lifetime = 20; /* lifetime for blocking SADB_ACQUIRE.*/
143 static int key_prefered_oldsa = 0; /* prefered old sa rather than new sa.*/
144
145 static u_int32_t acq_seq = 0;
146
147 static LIST_HEAD(_sptree, secpolicy) sptree[IPSEC_DIR_MAX]; /* SPD */
148 static LIST_HEAD(_sahtree, secashead) sahtree; /* SAD */
149 static LIST_HEAD(_regtree, secreg) regtree[SADB_SATYPE_MAX + 1];
150 /* registed list */
151 #ifndef IPSEC_NONBLOCK_ACQUIRE
152 static LIST_HEAD(_acqtree, secacq) acqtree; /* acquiring list */
153 #endif
154 #ifdef notyet
155 static LIST_HEAD(_spacqtree, secspacq) spacqtree; /* SP acquiring list */
156 #endif
157
158 /*
159 * Protect regtree, acqtree and items stored in the lists.
160 */
161 static kmutex_t key_mtx __cacheline_aligned;
162
163 /* search order for SAs */
164 /*
165 * This order is important because we must select the oldest SA
166 * for outbound processing. For inbound, This is not important.
167 */
168 static const u_int saorder_state_valid_prefer_old[] = {
169 SADB_SASTATE_DYING, SADB_SASTATE_MATURE,
170 };
171 static const u_int saorder_state_valid_prefer_new[] = {
172 SADB_SASTATE_MATURE, SADB_SASTATE_DYING,
173 };
174
175 static const u_int saorder_state_alive[] = {
176 /* except DEAD */
177 SADB_SASTATE_MATURE, SADB_SASTATE_DYING, SADB_SASTATE_LARVAL
178 };
179 static const u_int saorder_state_any[] = {
180 SADB_SASTATE_MATURE, SADB_SASTATE_DYING,
181 SADB_SASTATE_LARVAL, SADB_SASTATE_DEAD
182 };
183
184 #define SASTATE_ALIVE_FOREACH(s) \
185 for (int _i = 0; \
186 _i < __arraycount(saorder_state_alive) ? \
187 (s) = saorder_state_alive[_i], true : false; \
188 _i++)
189 #define SASTATE_ANY_FOREACH(s) \
190 for (int _i = 0; \
191 _i < __arraycount(saorder_state_any) ? \
192 (s) = saorder_state_any[_i], true : false; \
193 _i++)
194
195 static const int minsize[] = {
196 sizeof(struct sadb_msg), /* SADB_EXT_RESERVED */
197 sizeof(struct sadb_sa), /* SADB_EXT_SA */
198 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_CURRENT */
199 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_HARD */
200 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_SOFT */
201 sizeof(struct sadb_address), /* SADB_EXT_ADDRESS_SRC */
202 sizeof(struct sadb_address), /* SADB_EXT_ADDRESS_DST */
203 sizeof(struct sadb_address), /* SADB_EXT_ADDRESS_PROXY */
204 sizeof(struct sadb_key), /* SADB_EXT_KEY_AUTH */
205 sizeof(struct sadb_key), /* SADB_EXT_KEY_ENCRYPT */
206 sizeof(struct sadb_ident), /* SADB_EXT_IDENTITY_SRC */
207 sizeof(struct sadb_ident), /* SADB_EXT_IDENTITY_DST */
208 sizeof(struct sadb_sens), /* SADB_EXT_SENSITIVITY */
209 sizeof(struct sadb_prop), /* SADB_EXT_PROPOSAL */
210 sizeof(struct sadb_supported), /* SADB_EXT_SUPPORTED_AUTH */
211 sizeof(struct sadb_supported), /* SADB_EXT_SUPPORTED_ENCRYPT */
212 sizeof(struct sadb_spirange), /* SADB_EXT_SPIRANGE */
213 0, /* SADB_X_EXT_KMPRIVATE */
214 sizeof(struct sadb_x_policy), /* SADB_X_EXT_POLICY */
215 sizeof(struct sadb_x_sa2), /* SADB_X_SA2 */
216 sizeof(struct sadb_x_nat_t_type), /* SADB_X_EXT_NAT_T_TYPE */
217 sizeof(struct sadb_x_nat_t_port), /* SADB_X_EXT_NAT_T_SPORT */
218 sizeof(struct sadb_x_nat_t_port), /* SADB_X_EXT_NAT_T_DPORT */
219 sizeof(struct sadb_address), /* SADB_X_EXT_NAT_T_OAI */
220 sizeof(struct sadb_address), /* SADB_X_EXT_NAT_T_OAR */
221 sizeof(struct sadb_x_nat_t_frag), /* SADB_X_EXT_NAT_T_FRAG */
222 };
223 static const int maxsize[] = {
224 sizeof(struct sadb_msg), /* SADB_EXT_RESERVED */
225 sizeof(struct sadb_sa), /* SADB_EXT_SA */
226 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_CURRENT */
227 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_HARD */
228 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_SOFT */
229 0, /* SADB_EXT_ADDRESS_SRC */
230 0, /* SADB_EXT_ADDRESS_DST */
231 0, /* SADB_EXT_ADDRESS_PROXY */
232 0, /* SADB_EXT_KEY_AUTH */
233 0, /* SADB_EXT_KEY_ENCRYPT */
234 0, /* SADB_EXT_IDENTITY_SRC */
235 0, /* SADB_EXT_IDENTITY_DST */
236 0, /* SADB_EXT_SENSITIVITY */
237 0, /* SADB_EXT_PROPOSAL */
238 0, /* SADB_EXT_SUPPORTED_AUTH */
239 0, /* SADB_EXT_SUPPORTED_ENCRYPT */
240 sizeof(struct sadb_spirange), /* SADB_EXT_SPIRANGE */
241 0, /* SADB_X_EXT_KMPRIVATE */
242 0, /* SADB_X_EXT_POLICY */
243 sizeof(struct sadb_x_sa2), /* SADB_X_SA2 */
244 sizeof(struct sadb_x_nat_t_type), /* SADB_X_EXT_NAT_T_TYPE */
245 sizeof(struct sadb_x_nat_t_port), /* SADB_X_EXT_NAT_T_SPORT */
246 sizeof(struct sadb_x_nat_t_port), /* SADB_X_EXT_NAT_T_DPORT */
247 0, /* SADB_X_EXT_NAT_T_OAI */
248 0, /* SADB_X_EXT_NAT_T_OAR */
249 sizeof(struct sadb_x_nat_t_frag), /* SADB_X_EXT_NAT_T_FRAG */
250 };
251
252 static int ipsec_esp_keymin = 256;
253 static int ipsec_esp_auth = 0;
254 static int ipsec_ah_keymin = 128;
255
256 #ifdef SYSCTL_DECL
257 SYSCTL_DECL(_net_key);
258 #endif
259
260 #ifdef SYSCTL_INT
261 SYSCTL_INT(_net_key, KEYCTL_DEBUG_LEVEL, debug, CTLFLAG_RW, \
262 &key_debug_level, 0, "");
263
264 /* max count of trial for the decision of spi value */
265 SYSCTL_INT(_net_key, KEYCTL_SPI_TRY, spi_trycnt, CTLFLAG_RW, \
266 &key_spi_trycnt, 0, "");
267
268 /* minimum spi value to allocate automatically. */
269 SYSCTL_INT(_net_key, KEYCTL_SPI_MIN_VALUE, spi_minval, CTLFLAG_RW, \
270 &key_spi_minval, 0, "");
271
272 /* maximun spi value to allocate automatically. */
273 SYSCTL_INT(_net_key, KEYCTL_SPI_MAX_VALUE, spi_maxval, CTLFLAG_RW, \
274 &key_spi_maxval, 0, "");
275
276 /* interval to initialize randseed */
277 SYSCTL_INT(_net_key, KEYCTL_RANDOM_INT, int_random, CTLFLAG_RW, \
278 &key_int_random, 0, "");
279
280 /* lifetime for larval SA */
281 SYSCTL_INT(_net_key, KEYCTL_LARVAL_LIFETIME, larval_lifetime, CTLFLAG_RW, \
282 &key_larval_lifetime, 0, "");
283
284 /* counter for blocking to send SADB_ACQUIRE to IKEd */
285 SYSCTL_INT(_net_key, KEYCTL_BLOCKACQ_COUNT, blockacq_count, CTLFLAG_RW, \
286 &key_blockacq_count, 0, "");
287
288 /* lifetime for blocking to send SADB_ACQUIRE to IKEd */
289 SYSCTL_INT(_net_key, KEYCTL_BLOCKACQ_LIFETIME, blockacq_lifetime, CTLFLAG_RW, \
290 &key_blockacq_lifetime, 0, "");
291
292 /* ESP auth */
293 SYSCTL_INT(_net_key, KEYCTL_ESP_AUTH, esp_auth, CTLFLAG_RW, \
294 &ipsec_esp_auth, 0, "");
295
296 /* minimum ESP key length */
297 SYSCTL_INT(_net_key, KEYCTL_ESP_KEYMIN, esp_keymin, CTLFLAG_RW, \
298 &ipsec_esp_keymin, 0, "");
299
300 /* minimum AH key length */
301 SYSCTL_INT(_net_key, KEYCTL_AH_KEYMIN, ah_keymin, CTLFLAG_RW, \
302 &ipsec_ah_keymin, 0, "");
303
304 /* perfered old SA rather than new SA */
305 SYSCTL_INT(_net_key, KEYCTL_PREFERED_OLDSA, prefered_oldsa, CTLFLAG_RW,\
306 &key_prefered_oldsa, 0, "");
307 #endif /* SYSCTL_INT */
308
309 #define __LIST_CHAINED(elm) \
310 (!((elm)->chain.le_next == NULL && (elm)->chain.le_prev == NULL))
311 #define LIST_INSERT_TAIL(head, elm, type, field) \
312 do {\
313 struct type *curelm = LIST_FIRST(head); \
314 if (curelm == NULL) {\
315 LIST_INSERT_HEAD(head, elm, field); \
316 } else { \
317 while (LIST_NEXT(curelm, field)) \
318 curelm = LIST_NEXT(curelm, field);\
319 LIST_INSERT_AFTER(curelm, elm, field);\
320 }\
321 } while (0)
322
323 #define KEY_CHKSASTATE(head, sav) \
324 /* do */ { \
325 if ((head) != (sav)) { \
326 IPSECLOG(LOG_DEBUG, \
327 "state mismatched (TREE=%d SA=%d)\n", \
328 (head), (sav)); \
329 continue; \
330 } \
331 } /* while (0) */
332
333 #define KEY_CHKSPDIR(head, sp) \
334 do { \
335 if ((head) != (sp)) { \
336 IPSECLOG(LOG_DEBUG, \
337 "direction mismatched (TREE=%d SP=%d), anyway continue.\n",\
338 (head), (sp)); \
339 } \
340 } while (0)
341
342 /*
343 * set parameters into secpolicyindex buffer.
344 * Must allocate secpolicyindex buffer passed to this function.
345 */
346 #define KEY_SETSECSPIDX(_dir, s, d, ps, pd, ulp, idx) \
347 do { \
348 memset((idx), 0, sizeof(struct secpolicyindex)); \
349 (idx)->dir = (_dir); \
350 (idx)->prefs = (ps); \
351 (idx)->prefd = (pd); \
352 (idx)->ul_proto = (ulp); \
353 memcpy(&(idx)->src, (s), ((const struct sockaddr *)(s))->sa_len); \
354 memcpy(&(idx)->dst, (d), ((const struct sockaddr *)(d))->sa_len); \
355 } while (0)
356
357 /*
358 * set parameters into secasindex buffer.
359 * Must allocate secasindex buffer before calling this function.
360 */
361 static int
362 key_setsecasidx (int, int, int, const struct sadb_address *,
363 const struct sadb_address *, struct secasindex *);
364
365 /* key statistics */
366 struct _keystat {
367 u_long getspi_count; /* the avarage of count to try to get new SPI */
368 } keystat;
369
370 struct sadb_msghdr {
371 struct sadb_msg *msg;
372 struct sadb_ext *ext[SADB_EXT_MAX + 1];
373 int extoff[SADB_EXT_MAX + 1];
374 int extlen[SADB_EXT_MAX + 1];
375 };
376
377 static struct secasvar *key_allocsa_policy (const struct secasindex *);
378 #if 0
379 static void key_freeso(struct socket *);
380 static void key_freesp_so(struct secpolicy **);
381 #endif
382 static struct secasvar *key_do_allocsa_policy (struct secashead *, u_int);
383 static void key_delsp (struct secpolicy *);
384 static struct secpolicy *key_getsp (const struct secpolicyindex *);
385 static struct secpolicy *key_getspbyid (u_int32_t);
386 static u_int16_t key_newreqid (void);
387 static struct mbuf *key_gather_mbuf (struct mbuf *,
388 const struct sadb_msghdr *, int, int, ...);
389 static int key_spdadd (struct socket *, struct mbuf *,
390 const struct sadb_msghdr *);
391 static u_int32_t key_getnewspid (void);
392 static int key_spddelete (struct socket *, struct mbuf *,
393 const struct sadb_msghdr *);
394 static int key_spddelete2 (struct socket *, struct mbuf *,
395 const struct sadb_msghdr *);
396 static int key_spdget (struct socket *, struct mbuf *,
397 const struct sadb_msghdr *);
398 static int key_spdflush (struct socket *, struct mbuf *,
399 const struct sadb_msghdr *);
400 static int key_spddump (struct socket *, struct mbuf *,
401 const struct sadb_msghdr *);
402 static struct mbuf * key_setspddump (int *errorp, pid_t);
403 static struct mbuf * key_setspddump_chain (int *errorp, int *lenp, pid_t pid);
404 static int key_nat_map (struct socket *, struct mbuf *,
405 const struct sadb_msghdr *);
406 static struct mbuf *key_setdumpsp (struct secpolicy *,
407 u_int8_t, u_int32_t, pid_t);
408 static u_int key_getspreqmsglen (const struct secpolicy *);
409 static int key_spdexpire (struct secpolicy *);
410 static struct secashead *key_newsah (const struct secasindex *);
411 static void key_delsah (struct secashead *);
412 static struct secasvar *key_newsav (struct mbuf *,
413 const struct sadb_msghdr *, struct secashead *, int *,
414 const char*, int);
415 #define KEY_NEWSAV(m, sadb, sah, e) \
416 key_newsav(m, sadb, sah, e, __func__, __LINE__)
417 static void key_delsav (struct secasvar *);
418 static struct secashead *key_getsah (const struct secasindex *);
419 static struct secasvar *key_checkspidup (const struct secasindex *, u_int32_t);
420 static struct secasvar *key_getsavbyspi (struct secashead *, u_int32_t);
421 static int key_setsaval (struct secasvar *, struct mbuf *,
422 const struct sadb_msghdr *);
423 static void key_freesaval(struct secasvar *);
424 static int key_mature (struct secasvar *);
425 static struct mbuf *key_setdumpsa (struct secasvar *, u_int8_t,
426 u_int8_t, u_int32_t, u_int32_t);
427 static struct mbuf *key_setsadbxport (u_int16_t, u_int16_t);
428 static struct mbuf *key_setsadbxtype (u_int16_t);
429 static struct mbuf *key_setsadbxfrag (u_int16_t);
430 static void key_porttosaddr (union sockaddr_union *, u_int16_t);
431 static int key_checksalen (const union sockaddr_union *);
432 static struct mbuf *key_setsadbmsg (u_int8_t, u_int16_t, u_int8_t,
433 u_int32_t, pid_t, u_int16_t);
434 static struct mbuf *key_setsadbsa (struct secasvar *);
435 static struct mbuf *key_setsadbaddr (u_int16_t,
436 const struct sockaddr *, u_int8_t, u_int16_t);
437 #if 0
438 static struct mbuf *key_setsadbident (u_int16_t, u_int16_t, void *,
439 int, u_int64_t);
440 #endif
441 static struct mbuf *key_setsadbxsa2 (u_int8_t, u_int32_t, u_int16_t);
442 static struct mbuf *key_setsadbxpolicy (u_int16_t, u_int8_t,
443 u_int32_t);
444 static void *key_newbuf (const void *, u_int);
445 #ifdef INET6
446 static int key_ismyaddr6 (const struct sockaddr_in6 *);
447 #endif
448
449 static void sysctl_net_keyv2_setup(struct sysctllog **);
450 static void sysctl_net_key_compat_setup(struct sysctllog **);
451
452 /* flags for key_saidx_match() */
453 #define CMP_HEAD 1 /* protocol, addresses. */
454 #define CMP_MODE_REQID 2 /* additionally HEAD, reqid, mode. */
455 #define CMP_REQID 3 /* additionally HEAD, reaid. */
456 #define CMP_EXACTLY 4 /* all elements. */
457 static int key_saidx_match(const struct secasindex *,
458 const struct secasindex *, int);
459
460 static int key_sockaddr_match(const struct sockaddr *,
461 const struct sockaddr *, int);
462 static int key_bb_match_withmask(const void *, const void *, u_int);
463 static u_int16_t key_satype2proto (u_int8_t);
464 static u_int8_t key_proto2satype (u_int16_t);
465
466 static int key_spidx_match_exactly(const struct secpolicyindex *,
467 const struct secpolicyindex *);
468 static int key_spidx_match_withmask(const struct secpolicyindex *,
469 const struct secpolicyindex *);
470
471 static int key_getspi (struct socket *, struct mbuf *,
472 const struct sadb_msghdr *);
473 static u_int32_t key_do_getnewspi (const struct sadb_spirange *,
474 const struct secasindex *);
475 static int key_handle_natt_info (struct secasvar *,
476 const struct sadb_msghdr *);
477 static int key_set_natt_ports (union sockaddr_union *,
478 union sockaddr_union *,
479 const struct sadb_msghdr *);
480 static int key_update (struct socket *, struct mbuf *,
481 const struct sadb_msghdr *);
482 #ifdef IPSEC_DOSEQCHECK
483 static struct secasvar *key_getsavbyseq (struct secashead *, u_int32_t);
484 #endif
485 static int key_add (struct socket *, struct mbuf *,
486 const struct sadb_msghdr *);
487 static int key_setident (struct secashead *, struct mbuf *,
488 const struct sadb_msghdr *);
489 static struct mbuf *key_getmsgbuf_x1 (struct mbuf *,
490 const struct sadb_msghdr *);
491 static int key_delete (struct socket *, struct mbuf *,
492 const struct sadb_msghdr *);
493 static int key_get (struct socket *, struct mbuf *,
494 const struct sadb_msghdr *);
495
496 static void key_getcomb_setlifetime (struct sadb_comb *);
497 static struct mbuf *key_getcomb_esp (void);
498 static struct mbuf *key_getcomb_ah (void);
499 static struct mbuf *key_getcomb_ipcomp (void);
500 static struct mbuf *key_getprop (const struct secasindex *);
501
502 static int key_acquire (const struct secasindex *, struct secpolicy *);
503 #ifndef IPSEC_NONBLOCK_ACQUIRE
504 static struct secacq *key_newacq (const struct secasindex *);
505 static struct secacq *key_getacq (const struct secasindex *);
506 static struct secacq *key_getacqbyseq (u_int32_t);
507 #endif
508 #ifdef notyet
509 static struct secspacq *key_newspacq (const struct secpolicyindex *);
510 static struct secspacq *key_getspacq (const struct secpolicyindex *);
511 #endif
512 static int key_acquire2 (struct socket *, struct mbuf *,
513 const struct sadb_msghdr *);
514 static int key_register (struct socket *, struct mbuf *,
515 const struct sadb_msghdr *);
516 static int key_expire (struct secasvar *);
517 static int key_flush (struct socket *, struct mbuf *,
518 const struct sadb_msghdr *);
519 static struct mbuf *key_setdump_chain (u_int8_t req_satype, int *errorp,
520 int *lenp, pid_t pid);
521 static int key_dump (struct socket *, struct mbuf *,
522 const struct sadb_msghdr *);
523 static int key_promisc (struct socket *, struct mbuf *,
524 const struct sadb_msghdr *);
525 static int key_senderror (struct socket *, struct mbuf *, int);
526 static int key_validate_ext (const struct sadb_ext *, int);
527 static int key_align (struct mbuf *, struct sadb_msghdr *);
528 #if 0
529 static const char *key_getfqdn (void);
530 static const char *key_getuserfqdn (void);
531 #endif
532 static void key_sa_chgstate (struct secasvar *, u_int8_t);
533 static inline void key_sp_dead (struct secpolicy *);
534 static void key_sp_unlink (struct secpolicy *sp);
535
536 static struct mbuf *key_alloc_mbuf (int);
537
538 static void key_timehandler(void *);
539 static void key_timehandler_work(struct work *, void *);
540 static struct callout key_timehandler_ch;
541 static struct workqueue *key_timehandler_wq;
542 static struct work key_timehandler_wk;
543
544 #ifdef IPSEC_REF_DEBUG
545 #define REFLOG(label, p, where, tag) \
546 log(LOG_DEBUG, "%s:%d: " label " : refcnt=%d (%p)\n.", \
547 (where), (tag), (p)->refcnt, (p))
548 #else
549 #define REFLOG(label, p, where, tag) do {} while (0)
550 #endif
551
552 #define SA_ADDREF(p) do { \
553 atomic_inc_uint(&(p)->refcnt); \
554 REFLOG("SA_ADDREF", (p), __func__, __LINE__); \
555 KASSERTMSG((p)->refcnt != 0, "SA refcnt overflow"); \
556 } while (0)
557 #define SA_ADDREF2(p, where, tag) do { \
558 atomic_inc_uint(&(p)->refcnt); \
559 REFLOG("SA_ADDREF", (p), (where), (tag)); \
560 KASSERTMSG((p)->refcnt != 0, "SA refcnt overflow"); \
561 } while (0)
562 #define SA_DELREF(p) do { \
563 KASSERTMSG((p)->refcnt > 0, "SA refcnt underflow"); \
564 atomic_dec_uint(&(p)->refcnt); \
565 REFLOG("SA_DELREF", (p), __func__, __LINE__); \
566 } while (0)
567 #define SA_DELREF2(p, nv, where, tag) do { \
568 KASSERTMSG((p)->refcnt > 0, "SA refcnt underflow"); \
569 nv = atomic_dec_uint_nv(&(p)->refcnt); \
570 REFLOG("SA_DELREF", (p), (where), (tag)); \
571 } while (0)
572
573 #define SP_ADDREF(p) do { \
574 atomic_inc_uint(&(p)->refcnt); \
575 REFLOG("SP_ADDREF", (p), __func__, __LINE__); \
576 KASSERTMSG((p)->refcnt != 0, "SP refcnt overflow"); \
577 } while (0)
578 #define SP_ADDREF2(p, where, tag) do { \
579 atomic_inc_uint(&(p)->refcnt); \
580 REFLOG("SP_ADDREF", (p), (where), (tag)); \
581 KASSERTMSG((p)->refcnt != 0, "SP refcnt overflow"); \
582 } while (0)
583 #define SP_DELREF(p) do { \
584 KASSERTMSG((p)->refcnt > 0, "SP refcnt underflow"); \
585 atomic_dec_uint(&(p)->refcnt); \
586 REFLOG("SP_DELREF", (p), __func__, __LINE__); \
587 } while (0)
588 #define SP_DELREF2(p, nv, where, tag) do { \
589 KASSERTMSG((p)->refcnt > 0, "SP refcnt underflow"); \
590 nv = atomic_dec_uint_nv(&(p)->refcnt); \
591 REFLOG("SP_DELREF", (p), (where), (tag)); \
592 } while (0)
593
594
595 static inline void
596 key_sp_dead(struct secpolicy *sp)
597 {
598
599 /* mark the SP dead */
600 sp->state = IPSEC_SPSTATE_DEAD;
601 }
602
603 static void
604 key_sp_unlink(struct secpolicy *sp)
605 {
606
607 /* remove from SP index */
608 KASSERT(__LIST_CHAINED(sp));
609 LIST_REMOVE(sp, chain);
610 /* Release refcount held just for being on chain */
611 KEY_FREESP(&sp);
612 }
613
614
615 /*
616 * Return 0 when there are known to be no SP's for the specified
617 * direction. Otherwise return 1. This is used by IPsec code
618 * to optimize performance.
619 */
620 int
621 key_havesp(u_int dir)
622 {
623 return (dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND ?
624 !LIST_EMPTY(&sptree[dir]) : 1);
625 }
626
627 /* %%% IPsec policy management */
628 /*
629 * allocating a SP for OUTBOUND or INBOUND packet.
630 * Must call key_freesp() later.
631 * OUT: NULL: not found
632 * others: found and return the pointer.
633 */
634 struct secpolicy *
635 key_allocsp(const struct secpolicyindex *spidx, u_int dir, const char* where, int tag)
636 {
637 struct secpolicy *sp;
638 int s;
639
640 KASSERT(spidx != NULL);
641 KASSERTMSG(IPSEC_DIR_IS_INOROUT(dir), "invalid direction %u", dir);
642
643 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP, "DP from %s:%u\n", where, tag);
644
645 /* get a SP entry */
646 s = splsoftnet(); /*called from softclock()*/
647 if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DATA)) {
648 printf("*** objects\n");
649 kdebug_secpolicyindex(spidx);
650 }
651
652 LIST_FOREACH(sp, &sptree[dir], chain) {
653 if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DATA)) {
654 printf("*** in SPD\n");
655 kdebug_secpolicyindex(&sp->spidx);
656 }
657
658 if (sp->state == IPSEC_SPSTATE_DEAD)
659 continue;
660 if (key_spidx_match_withmask(&sp->spidx, spidx))
661 goto found;
662 }
663 sp = NULL;
664 found:
665 if (sp) {
666 /* sanity check */
667 KEY_CHKSPDIR(sp->spidx.dir, dir);
668
669 /* found a SPD entry */
670 sp->lastused = time_uptime;
671 SP_ADDREF2(sp, where, tag);
672 }
673 splx(s);
674
675 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP,
676 "DP return SP:%p (ID=%u) refcnt %u\n",
677 sp, sp ? sp->id : 0, sp ? sp->refcnt : 0);
678 return sp;
679 }
680
681 /*
682 * allocating a SP for OUTBOUND or INBOUND packet.
683 * Must call key_freesp() later.
684 * OUT: NULL: not found
685 * others: found and return the pointer.
686 */
687 struct secpolicy *
688 key_allocsp2(u_int32_t spi,
689 const union sockaddr_union *dst,
690 u_int8_t proto,
691 u_int dir,
692 const char* where, int tag)
693 {
694 struct secpolicy *sp;
695 int s;
696
697 KASSERT(dst != NULL);
698 KASSERTMSG(IPSEC_DIR_IS_INOROUT(dir), "invalid direction %u", dir);
699
700 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP, "DP from %s:%u\n", where, tag);
701
702 /* get a SP entry */
703 s = splsoftnet(); /*called from softclock()*/
704 if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DATA)) {
705 printf("*** objects\n");
706 printf("spi %u proto %u dir %u\n", spi, proto, dir);
707 kdebug_sockaddr(&dst->sa);
708 }
709
710 LIST_FOREACH(sp, &sptree[dir], chain) {
711 if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DATA)) {
712 printf("*** in SPD\n");
713 kdebug_secpolicyindex(&sp->spidx);
714 }
715
716 if (sp->state == IPSEC_SPSTATE_DEAD)
717 continue;
718 /* compare simple values, then dst address */
719 if (sp->spidx.ul_proto != proto)
720 continue;
721 /* NB: spi's must exist and match */
722 if (!sp->req || !sp->req->sav || sp->req->sav->spi != spi)
723 continue;
724 if (key_sockaddr_match(&sp->spidx.dst.sa, &dst->sa, PORT_STRICT))
725 goto found;
726 }
727 sp = NULL;
728 found:
729 if (sp) {
730 /* sanity check */
731 KEY_CHKSPDIR(sp->spidx.dir, dir);
732
733 /* found a SPD entry */
734 sp->lastused = time_uptime;
735 SP_ADDREF2(sp, where, tag);
736 }
737 splx(s);
738
739 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP,
740 "DP return SP:%p (ID=%u) refcnt %u\n",
741 sp, sp ? sp->id : 0, sp ? sp->refcnt : 0);
742 return sp;
743 }
744
745 /*
746 * return a policy that matches this particular inbound packet.
747 * XXX slow
748 */
749 struct secpolicy *
750 key_gettunnel(const struct sockaddr *osrc,
751 const struct sockaddr *odst,
752 const struct sockaddr *isrc,
753 const struct sockaddr *idst,
754 const char* where, int tag)
755 {
756 struct secpolicy *sp;
757 const int dir = IPSEC_DIR_INBOUND;
758 int s;
759 struct ipsecrequest *r1, *r2, *p;
760 struct secpolicyindex spidx;
761
762 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP, "DP from %s:%u\n", where, tag);
763
764 if (isrc->sa_family != idst->sa_family) {
765 IPSECLOG(LOG_ERR, "protocol family mismatched %d != %d\n.",
766 isrc->sa_family, idst->sa_family);
767 sp = NULL;
768 goto done;
769 }
770
771 s = splsoftnet(); /*called from softclock()*/
772 LIST_FOREACH(sp, &sptree[dir], chain) {
773 if (sp->state == IPSEC_SPSTATE_DEAD)
774 continue;
775
776 r1 = r2 = NULL;
777 for (p = sp->req; p; p = p->next) {
778 if (p->saidx.mode != IPSEC_MODE_TUNNEL)
779 continue;
780
781 r1 = r2;
782 r2 = p;
783
784 if (!r1) {
785 /* here we look at address matches only */
786 spidx = sp->spidx;
787 if (isrc->sa_len > sizeof(spidx.src) ||
788 idst->sa_len > sizeof(spidx.dst))
789 continue;
790 memcpy(&spidx.src, isrc, isrc->sa_len);
791 memcpy(&spidx.dst, idst, idst->sa_len);
792 if (!key_spidx_match_withmask(&sp->spidx, &spidx))
793 continue;
794 } else {
795 if (!key_sockaddr_match(&r1->saidx.src.sa, isrc, PORT_NONE) ||
796 !key_sockaddr_match(&r1->saidx.dst.sa, idst, PORT_NONE))
797 continue;
798 }
799
800 if (!key_sockaddr_match(&r2->saidx.src.sa, osrc, PORT_NONE) ||
801 !key_sockaddr_match(&r2->saidx.dst.sa, odst, PORT_NONE))
802 continue;
803
804 goto found;
805 }
806 }
807 sp = NULL;
808 found:
809 if (sp) {
810 sp->lastused = time_uptime;
811 SP_ADDREF2(sp, where, tag);
812 }
813 splx(s);
814 done:
815 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP,
816 "DP return SP:%p (ID=%u) refcnt %u\n",
817 sp, sp ? sp->id : 0, sp ? sp->refcnt : 0);
818 return sp;
819 }
820
821 /*
822 * allocating an SA entry for an *OUTBOUND* packet.
823 * checking each request entries in SP, and acquire an SA if need.
824 * OUT: 0: there are valid requests.
825 * ENOENT: policy may be valid, but SA with REQUIRE is on acquiring.
826 */
827 int
828 key_checkrequest(struct ipsecrequest *isr, const struct secasindex *saidx)
829 {
830 u_int level;
831 int error;
832 struct secasvar *oldsav = NULL;
833
834 KASSERT(isr != NULL);
835 KASSERT(saidx != NULL);
836 KASSERTMSG(saidx->mode == IPSEC_MODE_TRANSPORT ||
837 saidx->mode == IPSEC_MODE_TUNNEL,
838 "unexpected policy %u", saidx->mode);
839
840 /* get current level */
841 level = ipsec_get_reqlevel(isr);
842
843 /*
844 * XXX guard against protocol callbacks from the crypto
845 * thread as they reference ipsecrequest.sav which we
846 * temporarily null out below. Need to rethink how we
847 * handle bundled SA's in the callback thread.
848 */
849 IPSEC_SPLASSERT_SOFTNET("key_checkrequest");
850 #if 0
851 /*
852 * We do allocate new SA only if the state of SA in the holder is
853 * SADB_SASTATE_DEAD. The SA for outbound must be the oldest.
854 */
855 if (isr->sav != NULL) {
856 if (isr->sav->sah == NULL)
857 panic("key_checkrequest: sah is null");
858 if (isr->sav == (struct secasvar *)LIST_FIRST(
859 &isr->sav->sah->savtree[SADB_SASTATE_DEAD])) {
860 KEY_FREESAV(&isr->sav);
861 isr->sav = NULL;
862 }
863 }
864 #else
865 /*
866 * we free any SA stashed in the IPsec request because a different
867 * SA may be involved each time this request is checked, either
868 * because new SAs are being configured, or this request is
869 * associated with an unconnected datagram socket, or this request
870 * is associated with a system default policy.
871 *
872 * The operation may have negative impact to performance. We may
873 * want to check cached SA carefully, rather than picking new SA
874 * every time.
875 */
876 if (isr->sav != NULL)
877 oldsav = isr->sav;
878 #endif
879
880 /*
881 * new SA allocation if no SA found.
882 * key_allocsa_policy should allocate the oldest SA available.
883 * See key_do_allocsa_policy(), and draft-jenkins-ipsec-rekeying-03.txt.
884 */
885 isr->sav = key_allocsa_policy(saidx);
886 membar_producer();
887 if (oldsav != NULL)
888 KEY_FREESAV(&oldsav);
889
890 /* When there is SA. */
891 if (isr->sav != NULL) {
892 if (isr->sav->state != SADB_SASTATE_MATURE &&
893 isr->sav->state != SADB_SASTATE_DYING)
894 return EINVAL;
895 return 0;
896 }
897
898 /* there is no SA */
899 error = key_acquire(saidx, isr->sp);
900 if (error != 0) {
901 /* XXX What should I do ? */
902 IPSECLOG(LOG_DEBUG, "error %d returned from key_acquire.\n",
903 error);
904 return error;
905 }
906
907 if (level != IPSEC_LEVEL_REQUIRE) {
908 /* XXX sigh, the interface to this routine is botched */
909 KASSERTMSG(isr->sav == NULL, "unexpected SA");
910 return 0;
911 } else {
912 return ENOENT;
913 }
914 }
915
916 /*
917 * allocating a SA for policy entry from SAD.
918 * NOTE: searching SAD of aliving state.
919 * OUT: NULL: not found.
920 * others: found and return the pointer.
921 */
922 static struct secasvar *
923 key_allocsa_policy(const struct secasindex *saidx)
924 {
925 struct secashead *sah;
926 struct secasvar *sav;
927 u_int stateidx, state;
928 const u_int *saorder_state_valid;
929 int arraysize;
930
931 LIST_FOREACH(sah, &sahtree, chain) {
932 if (sah->state == SADB_SASTATE_DEAD)
933 continue;
934 if (key_saidx_match(&sah->saidx, saidx, CMP_MODE_REQID))
935 goto found;
936 }
937
938 return NULL;
939
940 found:
941
942 /*
943 * search a valid state list for outbound packet.
944 * This search order is important.
945 */
946 if (key_prefered_oldsa) {
947 saorder_state_valid = saorder_state_valid_prefer_old;
948 arraysize = _ARRAYLEN(saorder_state_valid_prefer_old);
949 } else {
950 saorder_state_valid = saorder_state_valid_prefer_new;
951 arraysize = _ARRAYLEN(saorder_state_valid_prefer_new);
952 }
953
954 /* search valid state */
955 for (stateidx = 0;
956 stateidx < arraysize;
957 stateidx++) {
958
959 state = saorder_state_valid[stateidx];
960
961 sav = key_do_allocsa_policy(sah, state);
962 if (sav != NULL)
963 return sav;
964 }
965
966 return NULL;
967 }
968
969 /*
970 * searching SAD with direction, protocol, mode and state.
971 * called by key_allocsa_policy().
972 * OUT:
973 * NULL : not found
974 * others : found, pointer to a SA.
975 */
976 static struct secasvar *
977 key_do_allocsa_policy(struct secashead *sah, u_int state)
978 {
979 struct secasvar *sav, *candidate, *d;
980
981 /* initilize */
982 candidate = NULL;
983
984 LIST_FOREACH(sav, &sah->savtree[state], chain) {
985 /* sanity check */
986 KEY_CHKSASTATE(sav->state, state);
987
988 /* initialize */
989 if (candidate == NULL) {
990 candidate = sav;
991 continue;
992 }
993
994 /* Which SA is the better ? */
995
996 /* sanity check 2 */
997 KASSERT(candidate->lft_c != NULL);
998 KASSERT(sav->lft_c != NULL);
999
1000 /* What the best method is to compare ? */
1001 if (key_prefered_oldsa) {
1002 if (candidate->lft_c->sadb_lifetime_addtime >
1003 sav->lft_c->sadb_lifetime_addtime) {
1004 candidate = sav;
1005 }
1006 continue;
1007 /*NOTREACHED*/
1008 }
1009
1010 /* prefered new sa rather than old sa */
1011 if (candidate->lft_c->sadb_lifetime_addtime <
1012 sav->lft_c->sadb_lifetime_addtime) {
1013 d = candidate;
1014 candidate = sav;
1015 } else
1016 d = sav;
1017
1018 /*
1019 * prepared to delete the SA when there is more
1020 * suitable candidate and the lifetime of the SA is not
1021 * permanent.
1022 */
1023 if (d->lft_c->sadb_lifetime_addtime != 0) {
1024 struct mbuf *m, *result = 0;
1025 uint8_t satype;
1026
1027 key_sa_chgstate(d, SADB_SASTATE_DEAD);
1028
1029 KASSERT(d->refcnt > 0);
1030
1031 satype = key_proto2satype(d->sah->saidx.proto);
1032 if (satype == 0)
1033 goto msgfail;
1034
1035 m = key_setsadbmsg(SADB_DELETE, 0,
1036 satype, 0, 0, d->refcnt - 1);
1037 if (!m)
1038 goto msgfail;
1039 result = m;
1040
1041 /* set sadb_address for saidx's. */
1042 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
1043 &d->sah->saidx.src.sa,
1044 d->sah->saidx.src.sa.sa_len << 3,
1045 IPSEC_ULPROTO_ANY);
1046 if (!m)
1047 goto msgfail;
1048 m_cat(result, m);
1049
1050 /* set sadb_address for saidx's. */
1051 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
1052 &d->sah->saidx.src.sa,
1053 d->sah->saidx.src.sa.sa_len << 3,
1054 IPSEC_ULPROTO_ANY);
1055 if (!m)
1056 goto msgfail;
1057 m_cat(result, m);
1058
1059 /* create SA extension */
1060 m = key_setsadbsa(d);
1061 if (!m)
1062 goto msgfail;
1063 m_cat(result, m);
1064
1065 if (result->m_len < sizeof(struct sadb_msg)) {
1066 result = m_pullup(result,
1067 sizeof(struct sadb_msg));
1068 if (result == NULL)
1069 goto msgfail;
1070 }
1071
1072 result->m_pkthdr.len = 0;
1073 for (m = result; m; m = m->m_next)
1074 result->m_pkthdr.len += m->m_len;
1075 mtod(result, struct sadb_msg *)->sadb_msg_len =
1076 PFKEY_UNIT64(result->m_pkthdr.len);
1077
1078 key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
1079 result = 0;
1080 msgfail:
1081 if (result)
1082 m_freem(result);
1083 KEY_FREESAV(&d);
1084 }
1085 }
1086
1087 if (candidate) {
1088 SA_ADDREF(candidate);
1089 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP,
1090 "DP cause refcnt++:%d SA:%p\n",
1091 candidate->refcnt, candidate);
1092 }
1093 return candidate;
1094 }
1095
1096 /*
1097 * allocating a usable SA entry for a *INBOUND* packet.
1098 * Must call key_freesav() later.
1099 * OUT: positive: pointer to a usable sav (i.e. MATURE or DYING state).
1100 * NULL: not found, or error occurred.
1101 *
1102 * In the comparison, no source address is used--for RFC2401 conformance.
1103 * To quote, from section 4.1:
1104 * A security association is uniquely identified by a triple consisting
1105 * of a Security Parameter Index (SPI), an IP Destination Address, and a
1106 * security protocol (AH or ESP) identifier.
1107 * Note that, however, we do need to keep source address in IPsec SA.
1108 * IKE specification and PF_KEY specification do assume that we
1109 * keep source address in IPsec SA. We see a tricky situation here.
1110 *
1111 * sport and dport are used for NAT-T. network order is always used.
1112 */
1113 struct secasvar *
1114 key_allocsa(
1115 const union sockaddr_union *dst,
1116 u_int proto,
1117 u_int32_t spi,
1118 u_int16_t sport,
1119 u_int16_t dport,
1120 const char* where, int tag)
1121 {
1122 struct secashead *sah;
1123 struct secasvar *sav;
1124 u_int stateidx, state;
1125 const u_int *saorder_state_valid;
1126 int arraysize, chkport;
1127 int s;
1128
1129 int must_check_spi = 1;
1130 int must_check_alg = 0;
1131 u_int16_t cpi = 0;
1132 u_int8_t algo = 0;
1133
1134 if ((sport != 0) && (dport != 0))
1135 chkport = PORT_STRICT;
1136 else
1137 chkport = PORT_NONE;
1138
1139 KASSERT(dst != NULL);
1140
1141 /*
1142 * XXX IPCOMP case
1143 * We use cpi to define spi here. In the case where cpi <=
1144 * IPCOMP_CPI_NEGOTIATE_MIN, cpi just define the algorithm used, not
1145 * the real spi. In this case, don't check the spi but check the
1146 * algorithm
1147 */
1148
1149 if (proto == IPPROTO_IPCOMP) {
1150 u_int32_t tmp;
1151 tmp = ntohl(spi);
1152 cpi = (u_int16_t) tmp;
1153 if (cpi < IPCOMP_CPI_NEGOTIATE_MIN) {
1154 algo = (u_int8_t) cpi;
1155 must_check_spi = 0;
1156 must_check_alg = 1;
1157 }
1158 }
1159 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP,
1160 "DP from %s:%u check_spi=%d, check_alg=%d\n",
1161 where, tag, must_check_spi, must_check_alg);
1162
1163
1164 /*
1165 * searching SAD.
1166 * XXX: to be checked internal IP header somewhere. Also when
1167 * IPsec tunnel packet is received. But ESP tunnel mode is
1168 * encrypted so we can't check internal IP header.
1169 */
1170 s = splsoftnet(); /*called from softclock()*/
1171 if (key_prefered_oldsa) {
1172 saorder_state_valid = saorder_state_valid_prefer_old;
1173 arraysize = _ARRAYLEN(saorder_state_valid_prefer_old);
1174 } else {
1175 saorder_state_valid = saorder_state_valid_prefer_new;
1176 arraysize = _ARRAYLEN(saorder_state_valid_prefer_new);
1177 }
1178 LIST_FOREACH(sah, &sahtree, chain) {
1179 /* search valid state */
1180 for (stateidx = 0; stateidx < arraysize; stateidx++) {
1181 state = saorder_state_valid[stateidx];
1182 LIST_FOREACH(sav, &sah->savtree[state], chain) {
1183 KEYDEBUG_PRINTF(KEYDEBUG_MATCH,
1184 "try match spi %#x, %#x\n",
1185 ntohl(spi), ntohl(sav->spi));
1186 /* sanity check */
1187 KEY_CHKSASTATE(sav->state, state);
1188 /* do not return entries w/ unusable state */
1189 if (sav->state != SADB_SASTATE_MATURE &&
1190 sav->state != SADB_SASTATE_DYING) {
1191 KEYDEBUG_PRINTF(KEYDEBUG_MATCH,
1192 "bad state %d\n", sav->state);
1193 continue;
1194 }
1195 if (proto != sav->sah->saidx.proto) {
1196 KEYDEBUG_PRINTF(KEYDEBUG_MATCH,
1197 "proto fail %d != %d\n",
1198 proto, sav->sah->saidx.proto);
1199 continue;
1200 }
1201 if (must_check_spi && spi != sav->spi) {
1202 KEYDEBUG_PRINTF(KEYDEBUG_MATCH,
1203 "spi fail %#x != %#x\n",
1204 ntohl(spi), ntohl(sav->spi));
1205 continue;
1206 }
1207 /* XXX only on the ipcomp case */
1208 if (must_check_alg && algo != sav->alg_comp) {
1209 KEYDEBUG_PRINTF(KEYDEBUG_MATCH,
1210 "algo fail %d != %d\n",
1211 algo, sav->alg_comp);
1212 continue;
1213 }
1214
1215 #if 0 /* don't check src */
1216 /* Fix port in src->sa */
1217
1218 /* check src address */
1219 if (!key_sockaddr_match(&src->sa, &sav->sah->saidx.src.sa, PORT_NONE))
1220 continue;
1221 #endif
1222 /* fix port of dst address XXX*/
1223 key_porttosaddr(__UNCONST(dst), dport);
1224 /* check dst address */
1225 if (!key_sockaddr_match(&dst->sa, &sav->sah->saidx.dst.sa, chkport))
1226 continue;
1227 SA_ADDREF2(sav, where, tag);
1228 goto done;
1229 }
1230 }
1231 }
1232 sav = NULL;
1233 done:
1234 splx(s);
1235
1236 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP,
1237 "DP return SA:%p; refcnt %u\n", sav, sav ? sav->refcnt : 0);
1238 return sav;
1239 }
1240
1241 void
1242 key_sp_ref(struct secpolicy *sp, const char* where, int tag)
1243 {
1244
1245 SP_ADDREF2(sp, where, tag);
1246
1247 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP,
1248 "DP SP:%p (ID=%u) from %s:%u; refcnt now %u\n",
1249 sp, sp->id, where, tag, sp->refcnt);
1250 }
1251
1252 /*
1253 * Must be called after calling key_allocsp().
1254 * For both the packet without socket and key_freeso().
1255 */
1256 void
1257 _key_freesp(struct secpolicy **spp, const char* where, int tag)
1258 {
1259 struct secpolicy *sp = *spp;
1260 unsigned int nv;
1261
1262 KASSERT(sp != NULL);
1263
1264 SP_DELREF2(sp, nv, where, tag);
1265
1266 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP,
1267 "DP SP:%p (ID=%u) from %s:%u; refcnt now %u\n",
1268 sp, sp->id, where, tag, nv);
1269
1270 if (nv == 0) {
1271 *spp = NULL;
1272 key_delsp(sp);
1273 }
1274 }
1275
1276 #if 0
1277 /*
1278 * Must be called after calling key_allocsp().
1279 * For the packet with socket.
1280 */
1281 static void
1282 key_freeso(struct socket *so)
1283 {
1284 /* sanity check */
1285 KASSERT(so != NULL);
1286
1287 switch (so->so_proto->pr_domain->dom_family) {
1288 #ifdef INET
1289 case PF_INET:
1290 {
1291 struct inpcb *pcb = sotoinpcb(so);
1292
1293 /* Does it have a PCB ? */
1294 if (pcb == NULL)
1295 return;
1296
1297 struct inpcbpolicy *sp = pcb->inp_sp;
1298 key_freesp_so(&sp->sp_in);
1299 key_freesp_so(&sp->sp_out);
1300 }
1301 break;
1302 #endif
1303 #ifdef INET6
1304 case PF_INET6:
1305 {
1306 #ifdef HAVE_NRL_INPCB
1307 struct inpcb *pcb = sotoinpcb(so);
1308 struct inpcbpolicy *sp = pcb->inp_sp;
1309
1310 /* Does it have a PCB ? */
1311 if (pcb == NULL)
1312 return;
1313 key_freesp_so(&sp->sp_in);
1314 key_freesp_so(&sp->sp_out);
1315 #else
1316 struct in6pcb *pcb = sotoin6pcb(so);
1317
1318 /* Does it have a PCB ? */
1319 if (pcb == NULL)
1320 return;
1321 key_freesp_so(&pcb->in6p_sp->sp_in);
1322 key_freesp_so(&pcb->in6p_sp->sp_out);
1323 #endif
1324 }
1325 break;
1326 #endif /* INET6 */
1327 default:
1328 IPSECLOG(LOG_DEBUG, "unknown address family=%d.\n",
1329 so->so_proto->pr_domain->dom_family);
1330 return;
1331 }
1332 }
1333
1334 static void
1335 key_freesp_so(struct secpolicy **sp)
1336 {
1337
1338 KASSERT(sp != NULL);
1339 KASSERT(*sp != NULL);
1340
1341 if ((*sp)->policy == IPSEC_POLICY_ENTRUST ||
1342 (*sp)->policy == IPSEC_POLICY_BYPASS)
1343 return;
1344
1345 KASSERTMSG((*sp)->policy == IPSEC_POLICY_IPSEC,
1346 "invalid policy %u", (*sp)->policy);
1347 KEY_FREESP(sp);
1348 }
1349 #endif
1350
1351 /*
1352 * Must be called after calling key_allocsa().
1353 * This function is called by key_freesp() to free some SA allocated
1354 * for a policy.
1355 */
1356 void
1357 key_freesav(struct secasvar **psav, const char* where, int tag)
1358 {
1359 struct secasvar *sav = *psav;
1360 unsigned int nv;
1361
1362 KASSERT(sav != NULL);
1363
1364 SA_DELREF2(sav, nv, where, tag);
1365
1366 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP,
1367 "DP SA:%p (SPI %lu) from %s:%u; refcnt now %u\n",
1368 sav, (u_long)ntohl(sav->spi), where, tag, nv);
1369
1370 if (nv == 0) {
1371 *psav = NULL;
1372 key_delsav(sav);
1373 }
1374 }
1375
1376 /* %%% SPD management */
1377 /*
1378 * free security policy entry.
1379 */
1380 static void
1381 key_delsp(struct secpolicy *sp)
1382 {
1383 int s;
1384
1385 KASSERT(sp != NULL);
1386
1387 key_sp_dead(sp);
1388
1389 KASSERTMSG(sp->refcnt == 0,
1390 "SP with references deleted (refcnt %u)", sp->refcnt);
1391
1392 s = splsoftnet(); /*called from softclock()*/
1393
1394 {
1395 struct ipsecrequest *isr = sp->req, *nextisr;
1396
1397 while (isr != NULL) {
1398 if (isr->sav != NULL) {
1399 KEY_FREESAV(&isr->sav);
1400 isr->sav = NULL;
1401 }
1402
1403 nextisr = isr->next;
1404 kmem_intr_free(isr, sizeof(*isr));
1405 isr = nextisr;
1406 }
1407 }
1408
1409 kmem_intr_free(sp, sizeof(*sp));
1410
1411 splx(s);
1412 }
1413
1414 /*
1415 * search SPD
1416 * OUT: NULL : not found
1417 * others : found, pointer to a SP.
1418 */
1419 static struct secpolicy *
1420 key_getsp(const struct secpolicyindex *spidx)
1421 {
1422 struct secpolicy *sp;
1423
1424 KASSERT(spidx != NULL);
1425
1426 LIST_FOREACH(sp, &sptree[spidx->dir], chain) {
1427 if (sp->state == IPSEC_SPSTATE_DEAD)
1428 continue;
1429 if (key_spidx_match_exactly(spidx, &sp->spidx)) {
1430 SP_ADDREF(sp);
1431 return sp;
1432 }
1433 }
1434
1435 return NULL;
1436 }
1437
1438 /*
1439 * get SP by index.
1440 * OUT: NULL : not found
1441 * others : found, pointer to a SP.
1442 */
1443 static struct secpolicy *
1444 key_getspbyid(u_int32_t id)
1445 {
1446 struct secpolicy *sp;
1447
1448 LIST_FOREACH(sp, &sptree[IPSEC_DIR_INBOUND], chain) {
1449 if (sp->state == IPSEC_SPSTATE_DEAD)
1450 continue;
1451 if (sp->id == id) {
1452 SP_ADDREF(sp);
1453 return sp;
1454 }
1455 }
1456
1457 LIST_FOREACH(sp, &sptree[IPSEC_DIR_OUTBOUND], chain) {
1458 if (sp->state == IPSEC_SPSTATE_DEAD)
1459 continue;
1460 if (sp->id == id) {
1461 SP_ADDREF(sp);
1462 return sp;
1463 }
1464 }
1465
1466 return NULL;
1467 }
1468
1469 struct secpolicy *
1470 key_newsp(const char* where, int tag)
1471 {
1472 struct secpolicy *newsp = NULL;
1473
1474 newsp = kmem_intr_zalloc(sizeof(struct secpolicy), KM_NOSLEEP);
1475 if (newsp != NULL)
1476 newsp->refcnt = 1;
1477
1478 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP,
1479 "DP from %s:%u return SP:%p\n", where, tag, newsp);
1480 return newsp;
1481 }
1482
1483 /*
1484 * create secpolicy structure from sadb_x_policy structure.
1485 * NOTE: `state', `secpolicyindex' in secpolicy structure are not set,
1486 * so must be set properly later.
1487 */
1488 struct secpolicy *
1489 key_msg2sp(const struct sadb_x_policy *xpl0, size_t len, int *error)
1490 {
1491 struct secpolicy *newsp;
1492
1493 KASSERT(!cpu_softintr_p());
1494 KASSERT(xpl0 != NULL);
1495 KASSERT(len >= sizeof(*xpl0));
1496
1497 if (len != PFKEY_EXTLEN(xpl0)) {
1498 IPSECLOG(LOG_DEBUG, "Invalid msg length.\n");
1499 *error = EINVAL;
1500 return NULL;
1501 }
1502
1503 newsp = KEY_NEWSP();
1504 if (newsp == NULL) {
1505 *error = ENOBUFS;
1506 return NULL;
1507 }
1508
1509 newsp->spidx.dir = xpl0->sadb_x_policy_dir;
1510 newsp->policy = xpl0->sadb_x_policy_type;
1511
1512 /* check policy */
1513 switch (xpl0->sadb_x_policy_type) {
1514 case IPSEC_POLICY_DISCARD:
1515 case IPSEC_POLICY_NONE:
1516 case IPSEC_POLICY_ENTRUST:
1517 case IPSEC_POLICY_BYPASS:
1518 newsp->req = NULL;
1519 *error = 0;
1520 return newsp;
1521
1522 case IPSEC_POLICY_IPSEC:
1523 /* Continued */
1524 break;
1525 default:
1526 IPSECLOG(LOG_DEBUG, "invalid policy type.\n");
1527 KEY_FREESP(&newsp);
1528 *error = EINVAL;
1529 return NULL;
1530 }
1531
1532 /* IPSEC_POLICY_IPSEC */
1533 {
1534 int tlen;
1535 const struct sadb_x_ipsecrequest *xisr;
1536 uint16_t xisr_reqid;
1537 struct ipsecrequest **p_isr = &newsp->req;
1538
1539 /* validity check */
1540 if (PFKEY_EXTLEN(xpl0) < sizeof(*xpl0)) {
1541 IPSECLOG(LOG_DEBUG, "Invalid msg length.\n");
1542 *error = EINVAL;
1543 goto free_exit;
1544 }
1545
1546 tlen = PFKEY_EXTLEN(xpl0) - sizeof(*xpl0);
1547 xisr = (const struct sadb_x_ipsecrequest *)(xpl0 + 1);
1548
1549 while (tlen > 0) {
1550 /* length check */
1551 if (xisr->sadb_x_ipsecrequest_len < sizeof(*xisr)) {
1552 IPSECLOG(LOG_DEBUG, "invalid ipsecrequest length.\n");
1553 *error = EINVAL;
1554 goto free_exit;
1555 }
1556
1557 /* allocate request buffer */
1558 *p_isr = kmem_zalloc(sizeof(**p_isr), KM_SLEEP);
1559
1560 /* set values */
1561 (*p_isr)->next = NULL;
1562
1563 switch (xisr->sadb_x_ipsecrequest_proto) {
1564 case IPPROTO_ESP:
1565 case IPPROTO_AH:
1566 case IPPROTO_IPCOMP:
1567 break;
1568 default:
1569 IPSECLOG(LOG_DEBUG, "invalid proto type=%u\n",
1570 xisr->sadb_x_ipsecrequest_proto);
1571 *error = EPROTONOSUPPORT;
1572 goto free_exit;
1573 }
1574 (*p_isr)->saidx.proto = xisr->sadb_x_ipsecrequest_proto;
1575
1576 switch (xisr->sadb_x_ipsecrequest_mode) {
1577 case IPSEC_MODE_TRANSPORT:
1578 case IPSEC_MODE_TUNNEL:
1579 break;
1580 case IPSEC_MODE_ANY:
1581 default:
1582 IPSECLOG(LOG_DEBUG, "invalid mode=%u\n",
1583 xisr->sadb_x_ipsecrequest_mode);
1584 *error = EINVAL;
1585 goto free_exit;
1586 }
1587 (*p_isr)->saidx.mode = xisr->sadb_x_ipsecrequest_mode;
1588
1589 switch (xisr->sadb_x_ipsecrequest_level) {
1590 case IPSEC_LEVEL_DEFAULT:
1591 case IPSEC_LEVEL_USE:
1592 case IPSEC_LEVEL_REQUIRE:
1593 break;
1594 case IPSEC_LEVEL_UNIQUE:
1595 xisr_reqid = xisr->sadb_x_ipsecrequest_reqid;
1596 /* validity check */
1597 /*
1598 * If range violation of reqid, kernel will
1599 * update it, don't refuse it.
1600 */
1601 if (xisr_reqid > IPSEC_MANUAL_REQID_MAX) {
1602 IPSECLOG(LOG_DEBUG,
1603 "reqid=%d range "
1604 "violation, updated by kernel.\n",
1605 xisr_reqid);
1606 xisr_reqid = 0;
1607 }
1608
1609 /* allocate new reqid id if reqid is zero. */
1610 if (xisr_reqid == 0) {
1611 u_int16_t reqid = key_newreqid();
1612 if (reqid == 0) {
1613 *error = ENOBUFS;
1614 goto free_exit;
1615 }
1616 (*p_isr)->saidx.reqid = reqid;
1617 } else {
1618 /* set it for manual keying. */
1619 (*p_isr)->saidx.reqid = xisr_reqid;
1620 }
1621 break;
1622
1623 default:
1624 IPSECLOG(LOG_DEBUG, "invalid level=%u\n",
1625 xisr->sadb_x_ipsecrequest_level);
1626 *error = EINVAL;
1627 goto free_exit;
1628 }
1629 (*p_isr)->level = xisr->sadb_x_ipsecrequest_level;
1630
1631 /* set IP addresses if there */
1632 if (xisr->sadb_x_ipsecrequest_len > sizeof(*xisr)) {
1633 const struct sockaddr *paddr;
1634
1635 paddr = (const struct sockaddr *)(xisr + 1);
1636
1637 /* validity check */
1638 if (paddr->sa_len > sizeof((*p_isr)->saidx.src)) {
1639 IPSECLOG(LOG_DEBUG, "invalid request "
1640 "address length.\n");
1641 *error = EINVAL;
1642 goto free_exit;
1643 }
1644 memcpy(&(*p_isr)->saidx.src, paddr, paddr->sa_len);
1645
1646 paddr = (const struct sockaddr *)((const char *)paddr
1647 + paddr->sa_len);
1648
1649 /* validity check */
1650 if (paddr->sa_len > sizeof((*p_isr)->saidx.dst)) {
1651 IPSECLOG(LOG_DEBUG, "invalid request "
1652 "address length.\n");
1653 *error = EINVAL;
1654 goto free_exit;
1655 }
1656 memcpy(&(*p_isr)->saidx.dst, paddr, paddr->sa_len);
1657 }
1658
1659 (*p_isr)->sav = NULL;
1660 (*p_isr)->sp = newsp;
1661
1662 /* initialization for the next. */
1663 p_isr = &(*p_isr)->next;
1664 tlen -= xisr->sadb_x_ipsecrequest_len;
1665
1666 /* validity check */
1667 if (tlen < 0) {
1668 IPSECLOG(LOG_DEBUG, "becoming tlen < 0.\n");
1669 *error = EINVAL;
1670 goto free_exit;
1671 }
1672
1673 xisr = (const struct sadb_x_ipsecrequest *)((const char *)xisr +
1674 xisr->sadb_x_ipsecrequest_len);
1675 }
1676 }
1677
1678 *error = 0;
1679 return newsp;
1680
1681 free_exit:
1682 KEY_FREESP(&newsp);
1683 return NULL;
1684 }
1685
1686 static u_int16_t
1687 key_newreqid(void)
1688 {
1689 static u_int16_t auto_reqid = IPSEC_MANUAL_REQID_MAX + 1;
1690
1691 auto_reqid = (auto_reqid == 0xffff ?
1692 IPSEC_MANUAL_REQID_MAX + 1 : auto_reqid + 1);
1693
1694 /* XXX should be unique check */
1695
1696 return auto_reqid;
1697 }
1698
1699 /*
1700 * copy secpolicy struct to sadb_x_policy structure indicated.
1701 */
1702 struct mbuf *
1703 key_sp2msg(const struct secpolicy *sp)
1704 {
1705 struct sadb_x_policy *xpl;
1706 int tlen;
1707 char *p;
1708 struct mbuf *m;
1709
1710 KASSERT(sp != NULL);
1711
1712 tlen = key_getspreqmsglen(sp);
1713
1714 m = key_alloc_mbuf(tlen);
1715 if (!m || m->m_next) { /*XXX*/
1716 if (m)
1717 m_freem(m);
1718 return NULL;
1719 }
1720
1721 m->m_len = tlen;
1722 m->m_next = NULL;
1723 xpl = mtod(m, struct sadb_x_policy *);
1724 memset(xpl, 0, tlen);
1725
1726 xpl->sadb_x_policy_len = PFKEY_UNIT64(tlen);
1727 xpl->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
1728 xpl->sadb_x_policy_type = sp->policy;
1729 xpl->sadb_x_policy_dir = sp->spidx.dir;
1730 xpl->sadb_x_policy_id = sp->id;
1731 p = (char *)xpl + sizeof(*xpl);
1732
1733 /* if is the policy for ipsec ? */
1734 if (sp->policy == IPSEC_POLICY_IPSEC) {
1735 struct sadb_x_ipsecrequest *xisr;
1736 struct ipsecrequest *isr;
1737
1738 for (isr = sp->req; isr != NULL; isr = isr->next) {
1739
1740 xisr = (struct sadb_x_ipsecrequest *)p;
1741
1742 xisr->sadb_x_ipsecrequest_proto = isr->saidx.proto;
1743 xisr->sadb_x_ipsecrequest_mode = isr->saidx.mode;
1744 xisr->sadb_x_ipsecrequest_level = isr->level;
1745 xisr->sadb_x_ipsecrequest_reqid = isr->saidx.reqid;
1746
1747 p += sizeof(*xisr);
1748 memcpy(p, &isr->saidx.src, isr->saidx.src.sa.sa_len);
1749 p += isr->saidx.src.sa.sa_len;
1750 memcpy(p, &isr->saidx.dst, isr->saidx.dst.sa.sa_len);
1751 p += isr->saidx.src.sa.sa_len;
1752
1753 xisr->sadb_x_ipsecrequest_len =
1754 PFKEY_ALIGN8(sizeof(*xisr)
1755 + isr->saidx.src.sa.sa_len
1756 + isr->saidx.dst.sa.sa_len);
1757 }
1758 }
1759
1760 return m;
1761 }
1762
1763 /* m will not be freed nor modified */
1764 static struct mbuf *
1765 key_gather_mbuf(struct mbuf *m, const struct sadb_msghdr *mhp,
1766 int ndeep, int nitem, ...)
1767 {
1768 va_list ap;
1769 int idx;
1770 int i;
1771 struct mbuf *result = NULL, *n;
1772 int len;
1773
1774 KASSERT(m != NULL);
1775 KASSERT(mhp != NULL);
1776
1777 va_start(ap, nitem);
1778 for (i = 0; i < nitem; i++) {
1779 idx = va_arg(ap, int);
1780 if (idx < 0 || idx > SADB_EXT_MAX)
1781 goto fail;
1782 /* don't attempt to pull empty extension */
1783 if (idx == SADB_EXT_RESERVED && mhp->msg == NULL)
1784 continue;
1785 if (idx != SADB_EXT_RESERVED &&
1786 (mhp->ext[idx] == NULL || mhp->extlen[idx] == 0))
1787 continue;
1788
1789 if (idx == SADB_EXT_RESERVED) {
1790 CTASSERT(PFKEY_ALIGN8(sizeof(struct sadb_msg)) <= MHLEN);
1791 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
1792 MGETHDR(n, M_DONTWAIT, MT_DATA);
1793 if (!n)
1794 goto fail;
1795 n->m_len = len;
1796 n->m_next = NULL;
1797 m_copydata(m, 0, sizeof(struct sadb_msg),
1798 mtod(n, void *));
1799 } else if (i < ndeep) {
1800 len = mhp->extlen[idx];
1801 n = key_alloc_mbuf(len);
1802 if (!n || n->m_next) { /*XXX*/
1803 if (n)
1804 m_freem(n);
1805 goto fail;
1806 }
1807 m_copydata(m, mhp->extoff[idx], mhp->extlen[idx],
1808 mtod(n, void *));
1809 } else {
1810 n = m_copym(m, mhp->extoff[idx], mhp->extlen[idx],
1811 M_DONTWAIT);
1812 }
1813 if (n == NULL)
1814 goto fail;
1815
1816 if (result)
1817 m_cat(result, n);
1818 else
1819 result = n;
1820 }
1821 va_end(ap);
1822
1823 if (result && (result->m_flags & M_PKTHDR) != 0) {
1824 result->m_pkthdr.len = 0;
1825 for (n = result; n; n = n->m_next)
1826 result->m_pkthdr.len += n->m_len;
1827 }
1828
1829 return result;
1830
1831 fail:
1832 va_end(ap);
1833 m_freem(result);
1834 return NULL;
1835 }
1836
1837 /*
1838 * SADB_X_SPDADD, SADB_X_SPDSETIDX or SADB_X_SPDUPDATE processing
1839 * add an entry to SP database, when received
1840 * <base, address(SD), (lifetime(H),) policy>
1841 * from the user(?).
1842 * Adding to SP database,
1843 * and send
1844 * <base, address(SD), (lifetime(H),) policy>
1845 * to the socket which was send.
1846 *
1847 * SPDADD set a unique policy entry.
1848 * SPDSETIDX like SPDADD without a part of policy requests.
1849 * SPDUPDATE replace a unique policy entry.
1850 *
1851 * m will always be freed.
1852 */
1853 static int
1854 key_spdadd(struct socket *so, struct mbuf *m,
1855 const struct sadb_msghdr *mhp)
1856 {
1857 const struct sadb_address *src0, *dst0;
1858 const struct sadb_x_policy *xpl0;
1859 struct sadb_x_policy *xpl;
1860 const struct sadb_lifetime *lft = NULL;
1861 struct secpolicyindex spidx;
1862 struct secpolicy *newsp;
1863 int error;
1864
1865 KASSERT(!cpu_softintr_p());
1866 KASSERT(so != NULL);
1867 KASSERT(m != NULL);
1868 KASSERT(mhp != NULL);
1869 KASSERT(mhp->msg != NULL);
1870
1871 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
1872 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
1873 mhp->ext[SADB_X_EXT_POLICY] == NULL) {
1874 IPSECLOG(LOG_DEBUG, "invalid message is passed.\n");
1875 return key_senderror(so, m, EINVAL);
1876 }
1877 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
1878 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
1879 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
1880 IPSECLOG(LOG_DEBUG, "invalid message is passed.\n");
1881 return key_senderror(so, m, EINVAL);
1882 }
1883 if (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL) {
1884 if (mhp->extlen[SADB_EXT_LIFETIME_HARD] <
1885 sizeof(struct sadb_lifetime)) {
1886 IPSECLOG(LOG_DEBUG, "invalid message is passed.\n");
1887 return key_senderror(so, m, EINVAL);
1888 }
1889 lft = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_HARD];
1890 }
1891
1892 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
1893 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
1894 xpl0 = (struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY];
1895
1896 /* make secindex */
1897 /* XXX boundary check against sa_len */
1898 KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
1899 src0 + 1,
1900 dst0 + 1,
1901 src0->sadb_address_prefixlen,
1902 dst0->sadb_address_prefixlen,
1903 src0->sadb_address_proto,
1904 &spidx);
1905
1906 /* checking the direciton. */
1907 switch (xpl0->sadb_x_policy_dir) {
1908 case IPSEC_DIR_INBOUND:
1909 case IPSEC_DIR_OUTBOUND:
1910 break;
1911 default:
1912 IPSECLOG(LOG_DEBUG, "Invalid SP direction.\n");
1913 mhp->msg->sadb_msg_errno = EINVAL;
1914 return 0;
1915 }
1916
1917 /* check policy */
1918 /* key_spdadd() accepts DISCARD, NONE and IPSEC. */
1919 if (xpl0->sadb_x_policy_type == IPSEC_POLICY_ENTRUST ||
1920 xpl0->sadb_x_policy_type == IPSEC_POLICY_BYPASS) {
1921 IPSECLOG(LOG_DEBUG, "Invalid policy type.\n");
1922 return key_senderror(so, m, EINVAL);
1923 }
1924
1925 /* policy requests are mandatory when action is ipsec. */
1926 if (mhp->msg->sadb_msg_type != SADB_X_SPDSETIDX &&
1927 xpl0->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
1928 mhp->extlen[SADB_X_EXT_POLICY] <= sizeof(*xpl0)) {
1929 IPSECLOG(LOG_DEBUG, "some policy requests part required.\n");
1930 return key_senderror(so, m, EINVAL);
1931 }
1932
1933 /*
1934 * checking there is SP already or not.
1935 * SPDUPDATE doesn't depend on whether there is a SP or not.
1936 * If the type is either SPDADD or SPDSETIDX AND a SP is found,
1937 * then error.
1938 */
1939 newsp = key_getsp(&spidx);
1940 if (mhp->msg->sadb_msg_type == SADB_X_SPDUPDATE) {
1941 if (newsp) {
1942 key_sp_dead(newsp);
1943 key_sp_unlink(newsp); /* XXX jrs ordering */
1944 KEY_FREESP(&newsp);
1945 newsp = NULL;
1946 }
1947 } else {
1948 if (newsp != NULL) {
1949 KEY_FREESP(&newsp);
1950 IPSECLOG(LOG_DEBUG, "a SP entry exists already.\n");
1951 return key_senderror(so, m, EEXIST);
1952 }
1953 }
1954
1955 /* allocation new SP entry */
1956 newsp = key_msg2sp(xpl0, PFKEY_EXTLEN(xpl0), &error);
1957 if (newsp == NULL) {
1958 return key_senderror(so, m, error);
1959 }
1960
1961 newsp->id = key_getnewspid();
1962 if (newsp->id == 0) {
1963 kmem_free(newsp, sizeof(*newsp));
1964 return key_senderror(so, m, ENOBUFS);
1965 }
1966
1967 /* XXX boundary check against sa_len */
1968 KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
1969 src0 + 1,
1970 dst0 + 1,
1971 src0->sadb_address_prefixlen,
1972 dst0->sadb_address_prefixlen,
1973 src0->sadb_address_proto,
1974 &newsp->spidx);
1975
1976 /* sanity check on addr pair */
1977 if (((const struct sockaddr *)(src0 + 1))->sa_family !=
1978 ((const struct sockaddr *)(dst0+ 1))->sa_family) {
1979 kmem_free(newsp, sizeof(*newsp));
1980 return key_senderror(so, m, EINVAL);
1981 }
1982 if (((const struct sockaddr *)(src0 + 1))->sa_len !=
1983 ((const struct sockaddr *)(dst0+ 1))->sa_len) {
1984 kmem_free(newsp, sizeof(*newsp));
1985 return key_senderror(so, m, EINVAL);
1986 }
1987
1988 newsp->created = time_uptime;
1989 newsp->lastused = newsp->created;
1990 newsp->lifetime = lft ? lft->sadb_lifetime_addtime : 0;
1991 newsp->validtime = lft ? lft->sadb_lifetime_usetime : 0;
1992
1993 newsp->refcnt = 1; /* do not reclaim until I say I do */
1994 newsp->state = IPSEC_SPSTATE_ALIVE;
1995 if (newsp->policy == IPSEC_POLICY_IPSEC)
1996 KASSERT(newsp->req != NULL);
1997 LIST_INSERT_TAIL(&sptree[newsp->spidx.dir], newsp, secpolicy, chain);
1998
1999 #ifdef notyet
2000 /* delete the entry in spacqtree */
2001 if (mhp->msg->sadb_msg_type == SADB_X_SPDUPDATE) {
2002 struct secspacq *spacq = key_getspacq(&spidx);
2003 if (spacq != NULL) {
2004 /* reset counter in order to deletion by timehandler. */
2005 spacq->created = time_uptime;
2006 spacq->count = 0;
2007 }
2008 }
2009 #endif
2010
2011 /* Invalidate all cached SPD pointers in the PCBs. */
2012 ipsec_invalpcbcacheall();
2013
2014 #if defined(GATEWAY)
2015 /* Invalidate the ipflow cache, as well. */
2016 ipflow_invalidate_all(0);
2017 #ifdef INET6
2018 if (in6_present)
2019 ip6flow_invalidate_all(0);
2020 #endif /* INET6 */
2021 #endif /* GATEWAY */
2022
2023 {
2024 struct mbuf *n, *mpolicy;
2025 struct sadb_msg *newmsg;
2026 int off;
2027
2028 /* create new sadb_msg to reply. */
2029 if (lft) {
2030 n = key_gather_mbuf(m, mhp, 2, 5, SADB_EXT_RESERVED,
2031 SADB_X_EXT_POLICY, SADB_EXT_LIFETIME_HARD,
2032 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
2033 } else {
2034 n = key_gather_mbuf(m, mhp, 2, 4, SADB_EXT_RESERVED,
2035 SADB_X_EXT_POLICY,
2036 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
2037 }
2038 if (!n)
2039 return key_senderror(so, m, ENOBUFS);
2040
2041 if (n->m_len < sizeof(*newmsg)) {
2042 n = m_pullup(n, sizeof(*newmsg));
2043 if (!n)
2044 return key_senderror(so, m, ENOBUFS);
2045 }
2046 newmsg = mtod(n, struct sadb_msg *);
2047 newmsg->sadb_msg_errno = 0;
2048 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
2049
2050 off = 0;
2051 mpolicy = m_pulldown(n, PFKEY_ALIGN8(sizeof(struct sadb_msg)),
2052 sizeof(*xpl), &off);
2053 if (mpolicy == NULL) {
2054 /* n is already freed */
2055 return key_senderror(so, m, ENOBUFS);
2056 }
2057 xpl = (struct sadb_x_policy *)(mtod(mpolicy, char *) + off);
2058 if (xpl->sadb_x_policy_exttype != SADB_X_EXT_POLICY) {
2059 m_freem(n);
2060 return key_senderror(so, m, EINVAL);
2061 }
2062 xpl->sadb_x_policy_id = newsp->id;
2063
2064 m_freem(m);
2065 key_update_used();
2066 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
2067 }
2068 }
2069
2070 /*
2071 * get new policy id.
2072 * OUT:
2073 * 0: failure.
2074 * others: success.
2075 */
2076 static u_int32_t
2077 key_getnewspid(void)
2078 {
2079 u_int32_t newid = 0;
2080 int count = key_spi_trycnt; /* XXX */
2081 struct secpolicy *sp;
2082
2083 /* when requesting to allocate spi ranged */
2084 while (count--) {
2085 newid = (policy_id = (policy_id == ~0 ? 1 : policy_id + 1));
2086
2087 sp = key_getspbyid(newid);
2088 if (sp == NULL)
2089 break;
2090
2091 KEY_FREESP(&sp);
2092 }
2093
2094 if (count == 0 || newid == 0) {
2095 IPSECLOG(LOG_DEBUG, "to allocate policy id is failed.\n");
2096 return 0;
2097 }
2098
2099 return newid;
2100 }
2101
2102 /*
2103 * SADB_SPDDELETE processing
2104 * receive
2105 * <base, address(SD), policy(*)>
2106 * from the user(?), and set SADB_SASTATE_DEAD,
2107 * and send,
2108 * <base, address(SD), policy(*)>
2109 * to the ikmpd.
2110 * policy(*) including direction of policy.
2111 *
2112 * m will always be freed.
2113 */
2114 static int
2115 key_spddelete(struct socket *so, struct mbuf *m,
2116 const struct sadb_msghdr *mhp)
2117 {
2118 struct sadb_address *src0, *dst0;
2119 struct sadb_x_policy *xpl0;
2120 struct secpolicyindex spidx;
2121 struct secpolicy *sp;
2122
2123 KASSERT(so != NULL);
2124 KASSERT(m != NULL);
2125 KASSERT(mhp != NULL);
2126 KASSERT(mhp->msg != NULL);
2127
2128 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
2129 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
2130 mhp->ext[SADB_X_EXT_POLICY] == NULL) {
2131 IPSECLOG(LOG_DEBUG, "invalid message is passed.\n");
2132 return key_senderror(so, m, EINVAL);
2133 }
2134 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
2135 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
2136 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
2137 IPSECLOG(LOG_DEBUG, "invalid message is passed.\n");
2138 return key_senderror(so, m, EINVAL);
2139 }
2140
2141 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
2142 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
2143 xpl0 = (struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY];
2144
2145 /* make secindex */
2146 /* XXX boundary check against sa_len */
2147 KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
2148 src0 + 1,
2149 dst0 + 1,
2150 src0->sadb_address_prefixlen,
2151 dst0->sadb_address_prefixlen,
2152 src0->sadb_address_proto,
2153 &spidx);
2154
2155 /* checking the direciton. */
2156 switch (xpl0->sadb_x_policy_dir) {
2157 case IPSEC_DIR_INBOUND:
2158 case IPSEC_DIR_OUTBOUND:
2159 break;
2160 default:
2161 IPSECLOG(LOG_DEBUG, "Invalid SP direction.\n");
2162 return key_senderror(so, m, EINVAL);
2163 }
2164
2165 /* Is there SP in SPD ? */
2166 sp = key_getsp(&spidx);
2167 if (sp == NULL) {
2168 IPSECLOG(LOG_DEBUG, "no SP found.\n");
2169 return key_senderror(so, m, EINVAL);
2170 }
2171
2172 /* save policy id to buffer to be returned. */
2173 xpl0->sadb_x_policy_id = sp->id;
2174
2175 key_sp_dead(sp);
2176 key_sp_unlink(sp); /* XXX jrs ordering */
2177 KEY_FREESP(&sp); /* ref gained by key_getspbyid */
2178
2179 /* Invalidate all cached SPD pointers in the PCBs. */
2180 ipsec_invalpcbcacheall();
2181
2182 /* We're deleting policy; no need to invalidate the ipflow cache. */
2183
2184 {
2185 struct mbuf *n;
2186 struct sadb_msg *newmsg;
2187
2188 /* create new sadb_msg to reply. */
2189 n = key_gather_mbuf(m, mhp, 1, 4, SADB_EXT_RESERVED,
2190 SADB_X_EXT_POLICY, SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
2191 if (!n)
2192 return key_senderror(so, m, ENOBUFS);
2193
2194 newmsg = mtod(n, struct sadb_msg *);
2195 newmsg->sadb_msg_errno = 0;
2196 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
2197
2198 m_freem(m);
2199 key_update_used();
2200 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
2201 }
2202 }
2203
2204 /*
2205 * SADB_SPDDELETE2 processing
2206 * receive
2207 * <base, policy(*)>
2208 * from the user(?), and set SADB_SASTATE_DEAD,
2209 * and send,
2210 * <base, policy(*)>
2211 * to the ikmpd.
2212 * policy(*) including direction of policy.
2213 *
2214 * m will always be freed.
2215 */
2216 static int
2217 key_spddelete2(struct socket *so, struct mbuf *m,
2218 const struct sadb_msghdr *mhp)
2219 {
2220 u_int32_t id;
2221 struct secpolicy *sp;
2222
2223 KASSERT(so != NULL);
2224 KASSERT(m != NULL);
2225 KASSERT(mhp != NULL);
2226 KASSERT(mhp->msg != NULL);
2227
2228 if (mhp->ext[SADB_X_EXT_POLICY] == NULL ||
2229 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
2230 IPSECLOG(LOG_DEBUG, "invalid message is passed.\n");
2231 key_senderror(so, m, EINVAL);
2232 return 0;
2233 }
2234
2235 id = ((struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY])->sadb_x_policy_id;
2236
2237 /* Is there SP in SPD ? */
2238 sp = key_getspbyid(id);
2239 if (sp == NULL) {
2240 IPSECLOG(LOG_DEBUG, "no SP found id:%u.\n", id);
2241 return key_senderror(so, m, EINVAL);
2242 }
2243
2244 key_sp_dead(sp);
2245 key_sp_unlink(sp); /* XXX jrs ordering */
2246 KEY_FREESP(&sp); /* ref gained by key_getsp */
2247 sp = NULL;
2248
2249 /* Invalidate all cached SPD pointers in the PCBs. */
2250 ipsec_invalpcbcacheall();
2251
2252 /* We're deleting policy; no need to invalidate the ipflow cache. */
2253
2254 {
2255 struct mbuf *n, *nn;
2256 struct sadb_msg *newmsg;
2257 int off, len;
2258
2259 /* create new sadb_msg to reply. */
2260 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
2261
2262 if (len > MCLBYTES)
2263 return key_senderror(so, m, ENOBUFS);
2264 MGETHDR(n, M_DONTWAIT, MT_DATA);
2265 if (n && len > MHLEN) {
2266 MCLGET(n, M_DONTWAIT);
2267 if ((n->m_flags & M_EXT) == 0) {
2268 m_freem(n);
2269 n = NULL;
2270 }
2271 }
2272 if (!n)
2273 return key_senderror(so, m, ENOBUFS);
2274
2275 n->m_len = len;
2276 n->m_next = NULL;
2277 off = 0;
2278
2279 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, char *) + off);
2280 off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
2281
2282 KASSERTMSG(off == len, "length inconsistency");
2283
2284 n->m_next = m_copym(m, mhp->extoff[SADB_X_EXT_POLICY],
2285 mhp->extlen[SADB_X_EXT_POLICY], M_DONTWAIT);
2286 if (!n->m_next) {
2287 m_freem(n);
2288 return key_senderror(so, m, ENOBUFS);
2289 }
2290
2291 n->m_pkthdr.len = 0;
2292 for (nn = n; nn; nn = nn->m_next)
2293 n->m_pkthdr.len += nn->m_len;
2294
2295 newmsg = mtod(n, struct sadb_msg *);
2296 newmsg->sadb_msg_errno = 0;
2297 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
2298
2299 m_freem(m);
2300 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
2301 }
2302 }
2303
2304 /*
2305 * SADB_X_GET processing
2306 * receive
2307 * <base, policy(*)>
2308 * from the user(?),
2309 * and send,
2310 * <base, address(SD), policy>
2311 * to the ikmpd.
2312 * policy(*) including direction of policy.
2313 *
2314 * m will always be freed.
2315 */
2316 static int
2317 key_spdget(struct socket *so, struct mbuf *m,
2318 const struct sadb_msghdr *mhp)
2319 {
2320 u_int32_t id;
2321 struct secpolicy *sp;
2322 struct mbuf *n;
2323
2324 KASSERT(so != NULL);
2325 KASSERT(m != NULL);
2326 KASSERT(mhp != NULL);
2327 KASSERT(mhp->msg != NULL);
2328
2329 if (mhp->ext[SADB_X_EXT_POLICY] == NULL ||
2330 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
2331 IPSECLOG(LOG_DEBUG, "invalid message is passed.\n");
2332 return key_senderror(so, m, EINVAL);
2333 }
2334
2335 id = ((struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY])->sadb_x_policy_id;
2336
2337 /* Is there SP in SPD ? */
2338 sp = key_getspbyid(id);
2339 if (sp == NULL) {
2340 IPSECLOG(LOG_DEBUG, "no SP found id:%u.\n", id);
2341 return key_senderror(so, m, ENOENT);
2342 }
2343
2344 n = key_setdumpsp(sp, SADB_X_SPDGET, mhp->msg->sadb_msg_seq,
2345 mhp->msg->sadb_msg_pid);
2346 KEY_FREESP(&sp); /* ref gained by key_getspbyid */
2347 if (n != NULL) {
2348 m_freem(m);
2349 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
2350 } else
2351 return key_senderror(so, m, ENOBUFS);
2352 }
2353
2354 #ifdef notyet
2355 /*
2356 * SADB_X_SPDACQUIRE processing.
2357 * Acquire policy and SA(s) for a *OUTBOUND* packet.
2358 * send
2359 * <base, policy(*)>
2360 * to KMD, and expect to receive
2361 * <base> with SADB_X_SPDACQUIRE if error occurred,
2362 * or
2363 * <base, policy>
2364 * with SADB_X_SPDUPDATE from KMD by PF_KEY.
2365 * policy(*) is without policy requests.
2366 *
2367 * 0 : succeed
2368 * others: error number
2369 */
2370 int
2371 key_spdacquire(const struct secpolicy *sp)
2372 {
2373 struct mbuf *result = NULL, *m;
2374 struct secspacq *newspacq;
2375 int error;
2376
2377 KASSERT(sp != NULL);
2378 KASSERTMSG(sp->req == NULL, "called but there is request");
2379 KASSERTMSG(sp->policy == IPSEC_POLICY_IPSEC,
2380 "policy mismathed. IPsec is expected");
2381
2382 /* Get an entry to check whether sent message or not. */
2383 newspacq = key_getspacq(&sp->spidx);
2384 if (newspacq != NULL) {
2385 if (key_blockacq_count < newspacq->count) {
2386 /* reset counter and do send message. */
2387 newspacq->count = 0;
2388 } else {
2389 /* increment counter and do nothing. */
2390 newspacq->count++;
2391 return 0;
2392 }
2393 } else {
2394 /* make new entry for blocking to send SADB_ACQUIRE. */
2395 newspacq = key_newspacq(&sp->spidx);
2396 if (newspacq == NULL)
2397 return ENOBUFS;
2398
2399 /* add to acqtree */
2400 LIST_INSERT_HEAD(&spacqtree, newspacq, chain);
2401 }
2402
2403 /* create new sadb_msg to reply. */
2404 m = key_setsadbmsg(SADB_X_SPDACQUIRE, 0, 0, 0, 0, 0);
2405 if (!m) {
2406 error = ENOBUFS;
2407 goto fail;
2408 }
2409 result = m;
2410
2411 result->m_pkthdr.len = 0;
2412 for (m = result; m; m = m->m_next)
2413 result->m_pkthdr.len += m->m_len;
2414
2415 mtod(result, struct sadb_msg *)->sadb_msg_len =
2416 PFKEY_UNIT64(result->m_pkthdr.len);
2417
2418 return key_sendup_mbuf(NULL, m, KEY_SENDUP_REGISTERED);
2419
2420 fail:
2421 if (result)
2422 m_freem(result);
2423 return error;
2424 }
2425 #endif /* notyet */
2426
2427 /*
2428 * SADB_SPDFLUSH processing
2429 * receive
2430 * <base>
2431 * from the user, and free all entries in secpctree.
2432 * and send,
2433 * <base>
2434 * to the user.
2435 * NOTE: what to do is only marking SADB_SASTATE_DEAD.
2436 *
2437 * m will always be freed.
2438 */
2439 static int
2440 key_spdflush(struct socket *so, struct mbuf *m,
2441 const struct sadb_msghdr *mhp)
2442 {
2443 struct sadb_msg *newmsg;
2444 struct secpolicy *sp;
2445 u_int dir;
2446
2447 KASSERT(so != NULL);
2448 KASSERT(m != NULL);
2449 KASSERT(mhp != NULL);
2450 KASSERT(mhp->msg != NULL);
2451
2452 if (m->m_len != PFKEY_ALIGN8(sizeof(struct sadb_msg)))
2453 return key_senderror(so, m, EINVAL);
2454
2455 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2456 struct secpolicy * nextsp;
2457 LIST_FOREACH_SAFE(sp, &sptree[dir], chain, nextsp) {
2458 if (sp->state == IPSEC_SPSTATE_DEAD)
2459 continue;
2460 key_sp_dead(sp);
2461 key_sp_unlink(sp);
2462 /* 'sp' dead; continue transfers to 'sp = nextsp' */
2463 continue;
2464 }
2465 }
2466
2467 /* Invalidate all cached SPD pointers in the PCBs. */
2468 ipsec_invalpcbcacheall();
2469
2470 /* We're deleting policy; no need to invalidate the ipflow cache. */
2471
2472 if (sizeof(struct sadb_msg) > m->m_len + M_TRAILINGSPACE(m)) {
2473 IPSECLOG(LOG_DEBUG, "No more memory.\n");
2474 return key_senderror(so, m, ENOBUFS);
2475 }
2476
2477 if (m->m_next)
2478 m_freem(m->m_next);
2479 m->m_next = NULL;
2480 m->m_pkthdr.len = m->m_len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
2481 newmsg = mtod(m, struct sadb_msg *);
2482 newmsg->sadb_msg_errno = 0;
2483 newmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
2484
2485 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
2486 }
2487
2488 static struct sockaddr key_src = {
2489 .sa_len = 2,
2490 .sa_family = PF_KEY,
2491 };
2492
2493 static struct mbuf *
2494 key_setspddump_chain(int *errorp, int *lenp, pid_t pid)
2495 {
2496 struct secpolicy *sp;
2497 int cnt;
2498 u_int dir;
2499 struct mbuf *m, *n, *prev;
2500 int totlen;
2501
2502 *lenp = 0;
2503
2504 /* search SPD entry and get buffer size. */
2505 cnt = 0;
2506 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2507 LIST_FOREACH(sp, &sptree[dir], chain) {
2508 cnt++;
2509 }
2510 }
2511
2512 if (cnt == 0) {
2513 *errorp = ENOENT;
2514 return (NULL);
2515 }
2516
2517 m = NULL;
2518 prev = m;
2519 totlen = 0;
2520 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2521 LIST_FOREACH(sp, &sptree[dir], chain) {
2522 --cnt;
2523 n = key_setdumpsp(sp, SADB_X_SPDDUMP, cnt, pid);
2524
2525 if (!n) {
2526 *errorp = ENOBUFS;
2527 if (m)
2528 m_freem(m);
2529 return (NULL);
2530 }
2531
2532 totlen += n->m_pkthdr.len;
2533 if (!m) {
2534 m = n;
2535 } else {
2536 prev->m_nextpkt = n;
2537 }
2538 prev = n;
2539 }
2540 }
2541
2542 *lenp = totlen;
2543 *errorp = 0;
2544 return (m);
2545 }
2546
2547 /*
2548 * SADB_SPDDUMP processing
2549 * receive
2550 * <base>
2551 * from the user, and dump all SP leaves
2552 * and send,
2553 * <base> .....
2554 * to the ikmpd.
2555 *
2556 * m will always be freed.
2557 */
2558 static int
2559 key_spddump(struct socket *so, struct mbuf *m0,
2560 const struct sadb_msghdr *mhp)
2561 {
2562 struct mbuf *n;
2563 int error, len;
2564 int ok, s;
2565 pid_t pid;
2566
2567 KASSERT(so != NULL);
2568 KASSERT(m0 != NULL);
2569 KASSERT(mhp != NULL);
2570 KASSERT(mhp->msg != NULL);
2571
2572 pid = mhp->msg->sadb_msg_pid;
2573 /*
2574 * If the requestor has insufficient socket-buffer space
2575 * for the entire chain, nobody gets any response to the DUMP.
2576 * XXX For now, only the requestor ever gets anything.
2577 * Moreover, if the requestor has any space at all, they receive
2578 * the entire chain, otherwise the request is refused with ENOBUFS.
2579 */
2580 if (sbspace(&so->so_rcv) <= 0) {
2581 return key_senderror(so, m0, ENOBUFS);
2582 }
2583
2584 s = splsoftnet();
2585 n = key_setspddump_chain(&error, &len, pid);
2586 splx(s);
2587
2588 if (n == NULL) {
2589 return key_senderror(so, m0, ENOENT);
2590 }
2591 {
2592 uint64_t *ps = PFKEY_STAT_GETREF();
2593 ps[PFKEY_STAT_IN_TOTAL]++;
2594 ps[PFKEY_STAT_IN_BYTES] += len;
2595 PFKEY_STAT_PUTREF();
2596 }
2597
2598 /*
2599 * PF_KEY DUMP responses are no longer broadcast to all PF_KEY sockets.
2600 * The requestor receives either the entire chain, or an
2601 * error message with ENOBUFS.
2602 */
2603
2604 /*
2605 * sbappendchainwith record takes the chain of entries, one
2606 * packet-record per SPD entry, prepends the key_src sockaddr
2607 * to each packet-record, links the sockaddr mbufs into a new
2608 * list of records, then appends the entire resulting
2609 * list to the requesting socket.
2610 */
2611 ok = sbappendaddrchain(&so->so_rcv, (struct sockaddr *)&key_src, n,
2612 SB_PRIO_ONESHOT_OVERFLOW);
2613
2614 if (!ok) {
2615 PFKEY_STATINC(PFKEY_STAT_IN_NOMEM);
2616 m_freem(n);
2617 return key_senderror(so, m0, ENOBUFS);
2618 }
2619
2620 m_freem(m0);
2621 return error;
2622 }
2623
2624 /*
2625 * SADB_X_NAT_T_NEW_MAPPING. Unused by racoon as of 2005/04/23
2626 */
2627 static int
2628 key_nat_map(struct socket *so, struct mbuf *m,
2629 const struct sadb_msghdr *mhp)
2630 {
2631 struct sadb_x_nat_t_type *type;
2632 struct sadb_x_nat_t_port *sport;
2633 struct sadb_x_nat_t_port *dport;
2634 struct sadb_address *iaddr, *raddr;
2635 struct sadb_x_nat_t_frag *frag;
2636
2637 KASSERT(so != NULL);
2638 KASSERT(m != NULL);
2639 KASSERT(mhp != NULL);
2640 KASSERT(mhp->msg != NULL);
2641
2642 if (mhp->ext[SADB_X_EXT_NAT_T_TYPE] == NULL ||
2643 mhp->ext[SADB_X_EXT_NAT_T_SPORT] == NULL ||
2644 mhp->ext[SADB_X_EXT_NAT_T_DPORT] == NULL) {
2645 IPSECLOG(LOG_DEBUG, "invalid message.\n");
2646 return key_senderror(so, m, EINVAL);
2647 }
2648 if ((mhp->extlen[SADB_X_EXT_NAT_T_TYPE] < sizeof(*type)) ||
2649 (mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport)) ||
2650 (mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport))) {
2651 IPSECLOG(LOG_DEBUG, "invalid message.\n");
2652 return key_senderror(so, m, EINVAL);
2653 }
2654
2655 if ((mhp->ext[SADB_X_EXT_NAT_T_OAI] != NULL) &&
2656 (mhp->extlen[SADB_X_EXT_NAT_T_OAI] < sizeof(*iaddr))) {
2657 IPSECLOG(LOG_DEBUG, "invalid message\n");
2658 return key_senderror(so, m, EINVAL);
2659 }
2660
2661 if ((mhp->ext[SADB_X_EXT_NAT_T_OAR] != NULL) &&
2662 (mhp->extlen[SADB_X_EXT_NAT_T_OAR] < sizeof(*raddr))) {
2663 IPSECLOG(LOG_DEBUG, "invalid message\n");
2664 return key_senderror(so, m, EINVAL);
2665 }
2666
2667 if ((mhp->ext[SADB_X_EXT_NAT_T_FRAG] != NULL) &&
2668 (mhp->extlen[SADB_X_EXT_NAT_T_FRAG] < sizeof(*frag))) {
2669 IPSECLOG(LOG_DEBUG, "invalid message\n");
2670 return key_senderror(so, m, EINVAL);
2671 }
2672
2673 type = (struct sadb_x_nat_t_type *)mhp->ext[SADB_X_EXT_NAT_T_TYPE];
2674 sport = (struct sadb_x_nat_t_port *)mhp->ext[SADB_X_EXT_NAT_T_SPORT];
2675 dport = (struct sadb_x_nat_t_port *)mhp->ext[SADB_X_EXT_NAT_T_DPORT];
2676 iaddr = (struct sadb_address *)mhp->ext[SADB_X_EXT_NAT_T_OAI];
2677 raddr = (struct sadb_address *)mhp->ext[SADB_X_EXT_NAT_T_OAR];
2678 frag = (struct sadb_x_nat_t_frag *) mhp->ext[SADB_X_EXT_NAT_T_FRAG];
2679
2680 /*
2681 * XXX handle that, it should also contain a SA, or anything
2682 * that enable to update the SA information.
2683 */
2684
2685 return 0;
2686 }
2687
2688 static struct mbuf *
2689 key_setdumpsp(struct secpolicy *sp, u_int8_t type, u_int32_t seq, pid_t pid)
2690 {
2691 struct mbuf *result = NULL, *m;
2692
2693 m = key_setsadbmsg(type, 0, SADB_SATYPE_UNSPEC, seq, pid, sp->refcnt);
2694 if (!m)
2695 goto fail;
2696 result = m;
2697
2698 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
2699 &sp->spidx.src.sa, sp->spidx.prefs, sp->spidx.ul_proto);
2700 if (!m)
2701 goto fail;
2702 m_cat(result, m);
2703
2704 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
2705 &sp->spidx.dst.sa, sp->spidx.prefd, sp->spidx.ul_proto);
2706 if (!m)
2707 goto fail;
2708 m_cat(result, m);
2709
2710 m = key_sp2msg(sp);
2711 if (!m)
2712 goto fail;
2713 m_cat(result, m);
2714
2715 if ((result->m_flags & M_PKTHDR) == 0)
2716 goto fail;
2717
2718 if (result->m_len < sizeof(struct sadb_msg)) {
2719 result = m_pullup(result, sizeof(struct sadb_msg));
2720 if (result == NULL)
2721 goto fail;
2722 }
2723
2724 result->m_pkthdr.len = 0;
2725 for (m = result; m; m = m->m_next)
2726 result->m_pkthdr.len += m->m_len;
2727
2728 mtod(result, struct sadb_msg *)->sadb_msg_len =
2729 PFKEY_UNIT64(result->m_pkthdr.len);
2730
2731 return result;
2732
2733 fail:
2734 m_freem(result);
2735 return NULL;
2736 }
2737
2738 /*
2739 * get PFKEY message length for security policy and request.
2740 */
2741 static u_int
2742 key_getspreqmsglen(const struct secpolicy *sp)
2743 {
2744 u_int tlen;
2745
2746 tlen = sizeof(struct sadb_x_policy);
2747
2748 /* if is the policy for ipsec ? */
2749 if (sp->policy != IPSEC_POLICY_IPSEC)
2750 return tlen;
2751
2752 /* get length of ipsec requests */
2753 {
2754 const struct ipsecrequest *isr;
2755 int len;
2756
2757 for (isr = sp->req; isr != NULL; isr = isr->next) {
2758 len = sizeof(struct sadb_x_ipsecrequest)
2759 + isr->saidx.src.sa.sa_len + isr->saidx.dst.sa.sa_len;
2760
2761 tlen += PFKEY_ALIGN8(len);
2762 }
2763 }
2764
2765 return tlen;
2766 }
2767
2768 /*
2769 * SADB_SPDEXPIRE processing
2770 * send
2771 * <base, address(SD), lifetime(CH), policy>
2772 * to KMD by PF_KEY.
2773 *
2774 * OUT: 0 : succeed
2775 * others : error number
2776 */
2777 static int
2778 key_spdexpire(struct secpolicy *sp)
2779 {
2780 int s;
2781 struct mbuf *result = NULL, *m;
2782 int len;
2783 int error = -1;
2784 struct sadb_lifetime *lt;
2785
2786 /* XXX: Why do we lock ? */
2787 s = splsoftnet(); /*called from softclock()*/
2788
2789 KASSERT(sp != NULL);
2790
2791 /* set msg header */
2792 m = key_setsadbmsg(SADB_X_SPDEXPIRE, 0, 0, 0, 0, 0);
2793 if (!m) {
2794 error = ENOBUFS;
2795 goto fail;
2796 }
2797 result = m;
2798
2799 /* create lifetime extension (current and hard) */
2800 len = PFKEY_ALIGN8(sizeof(*lt)) * 2;
2801 m = key_alloc_mbuf(len);
2802 if (!m || m->m_next) { /*XXX*/
2803 if (m)
2804 m_freem(m);
2805 error = ENOBUFS;
2806 goto fail;
2807 }
2808 memset(mtod(m, void *), 0, len);
2809 lt = mtod(m, struct sadb_lifetime *);
2810 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
2811 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
2812 lt->sadb_lifetime_allocations = 0;
2813 lt->sadb_lifetime_bytes = 0;
2814 lt->sadb_lifetime_addtime = sp->created + time_second - time_uptime;
2815 lt->sadb_lifetime_usetime = sp->lastused + time_second - time_uptime;
2816 lt = (struct sadb_lifetime *)(mtod(m, char *) + len / 2);
2817 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
2818 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
2819 lt->sadb_lifetime_allocations = 0;
2820 lt->sadb_lifetime_bytes = 0;
2821 lt->sadb_lifetime_addtime = sp->lifetime;
2822 lt->sadb_lifetime_usetime = sp->validtime;
2823 m_cat(result, m);
2824
2825 /* set sadb_address for source */
2826 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC, &sp->spidx.src.sa,
2827 sp->spidx.prefs, sp->spidx.ul_proto);
2828 if (!m) {
2829 error = ENOBUFS;
2830 goto fail;
2831 }
2832 m_cat(result, m);
2833
2834 /* set sadb_address for destination */
2835 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST, &sp->spidx.dst.sa,
2836 sp->spidx.prefd, sp->spidx.ul_proto);
2837 if (!m) {
2838 error = ENOBUFS;
2839 goto fail;
2840 }
2841 m_cat(result, m);
2842
2843 /* set secpolicy */
2844 m = key_sp2msg(sp);
2845 if (!m) {
2846 error = ENOBUFS;
2847 goto fail;
2848 }
2849 m_cat(result, m);
2850
2851 if ((result->m_flags & M_PKTHDR) == 0) {
2852 error = EINVAL;
2853 goto fail;
2854 }
2855
2856 if (result->m_len < sizeof(struct sadb_msg)) {
2857 result = m_pullup(result, sizeof(struct sadb_msg));
2858 if (result == NULL) {
2859 error = ENOBUFS;
2860 goto fail;
2861 }
2862 }
2863
2864 result->m_pkthdr.len = 0;
2865 for (m = result; m; m = m->m_next)
2866 result->m_pkthdr.len += m->m_len;
2867
2868 mtod(result, struct sadb_msg *)->sadb_msg_len =
2869 PFKEY_UNIT64(result->m_pkthdr.len);
2870
2871 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
2872
2873 fail:
2874 if (result)
2875 m_freem(result);
2876 splx(s);
2877 return error;
2878 }
2879
2880 /* %%% SAD management */
2881 /*
2882 * allocating a memory for new SA head, and copy from the values of mhp.
2883 * OUT: NULL : failure due to the lack of memory.
2884 * others : pointer to new SA head.
2885 */
2886 static struct secashead *
2887 key_newsah(const struct secasindex *saidx)
2888 {
2889 struct secashead *newsah;
2890 int i;
2891
2892 KASSERT(saidx != NULL);
2893
2894 newsah = kmem_zalloc(sizeof(struct secashead), KM_SLEEP);
2895 for (i = 0; i < __arraycount(newsah->savtree); i++)
2896 LIST_INIT(&newsah->savtree[i]);
2897 newsah->saidx = *saidx;
2898
2899 /* add to saidxtree */
2900 newsah->state = SADB_SASTATE_MATURE;
2901 LIST_INSERT_HEAD(&sahtree, newsah, chain);
2902
2903 return newsah;
2904 }
2905
2906 /*
2907 * delete SA index and all SA registerd.
2908 */
2909 static void
2910 key_delsah(struct secashead *sah)
2911 {
2912 struct secasvar *sav, *nextsav;
2913 u_int state;
2914 int s;
2915 int zombie = 0;
2916
2917 KASSERT(!cpu_softintr_p());
2918 KASSERT(sah != NULL);
2919
2920 s = splsoftnet();
2921
2922 /* searching all SA registerd in the secindex. */
2923 SASTATE_ANY_FOREACH(state) {
2924 LIST_FOREACH_SAFE(sav, &sah->savtree[state], chain, nextsav) {
2925 if (sav->refcnt == 0) {
2926 /* sanity check */
2927 KEY_CHKSASTATE(state, sav->state);
2928 KEY_FREESAV(&sav);
2929 } else {
2930 /* give up to delete this sa */
2931 zombie++;
2932 }
2933 }
2934 }
2935
2936 /* don't delete sah only if there are savs. */
2937 if (zombie) {
2938 splx(s);
2939 return;
2940 }
2941
2942 rtcache_free(&sah->sa_route);
2943
2944 /* remove from tree of SA index */
2945 KASSERT(__LIST_CHAINED(sah));
2946 LIST_REMOVE(sah, chain);
2947
2948 if (sah->idents != NULL)
2949 kmem_free(sah->idents, sah->idents_len);
2950 if (sah->identd != NULL)
2951 kmem_free(sah->identd, sah->identd_len);
2952
2953 kmem_free(sah, sizeof(*sah));
2954
2955 splx(s);
2956 return;
2957 }
2958
2959 /*
2960 * allocating a new SA with LARVAL state. key_add() and key_getspi() call,
2961 * and copy the values of mhp into new buffer.
2962 * When SAD message type is GETSPI:
2963 * to set sequence number from acq_seq++,
2964 * to set zero to SPI.
2965 * not to call key_setsava().
2966 * OUT: NULL : fail
2967 * others : pointer to new secasvar.
2968 *
2969 * does not modify mbuf. does not free mbuf on error.
2970 */
2971 static struct secasvar *
2972 key_newsav(struct mbuf *m, const struct sadb_msghdr *mhp,
2973 struct secashead *sah, int *errp,
2974 const char* where, int tag)
2975 {
2976 struct secasvar *newsav;
2977 const struct sadb_sa *xsa;
2978
2979 KASSERT(!cpu_softintr_p());
2980 KASSERT(m != NULL);
2981 KASSERT(mhp != NULL);
2982 KASSERT(mhp->msg != NULL);
2983 KASSERT(sah != NULL);
2984
2985 newsav = kmem_zalloc(sizeof(struct secasvar), KM_SLEEP);
2986
2987 switch (mhp->msg->sadb_msg_type) {
2988 case SADB_GETSPI:
2989 newsav->spi = 0;
2990
2991 #ifdef IPSEC_DOSEQCHECK
2992 /* sync sequence number */
2993 if (mhp->msg->sadb_msg_seq == 0)
2994 newsav->seq =
2995 (acq_seq = (acq_seq == ~0 ? 1 : ++acq_seq));
2996 else
2997 #endif
2998 newsav->seq = mhp->msg->sadb_msg_seq;
2999 break;
3000
3001 case SADB_ADD:
3002 /* sanity check */
3003 if (mhp->ext[SADB_EXT_SA] == NULL) {
3004 IPSECLOG(LOG_DEBUG, "invalid message is passed.\n");
3005 *errp = EINVAL;
3006 goto error;
3007 }
3008 xsa = (const struct sadb_sa *)mhp->ext[SADB_EXT_SA];
3009 newsav->spi = xsa->sadb_sa_spi;
3010 newsav->seq = mhp->msg->sadb_msg_seq;
3011 break;
3012 default:
3013 *errp = EINVAL;
3014 goto error;
3015 }
3016
3017 /* copy sav values */
3018 if (mhp->msg->sadb_msg_type != SADB_GETSPI) {
3019 *errp = key_setsaval(newsav, m, mhp);
3020 if (*errp)
3021 goto error;
3022 }
3023
3024 /* reset created */
3025 newsav->created = time_uptime;
3026 newsav->pid = mhp->msg->sadb_msg_pid;
3027
3028 /* add to satree */
3029 newsav->sah = sah;
3030 newsav->refcnt = 1;
3031 newsav->state = SADB_SASTATE_LARVAL;
3032 LIST_INSERT_TAIL(&sah->savtree[SADB_SASTATE_LARVAL], newsav,
3033 secasvar, chain);
3034 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP,
3035 "DP from %s:%u return SA:%p\n", where, tag, newsav);
3036 return newsav;
3037
3038 error:
3039 KASSERT(*errp != 0);
3040 kmem_free(newsav, sizeof(*newsav));
3041 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP,
3042 "DP from %s:%u return SA:NULL\n", where, tag);
3043 return NULL;
3044 }
3045
3046 /*
3047 * free() SA variable entry.
3048 */
3049 static void
3050 key_delsav(struct secasvar *sav)
3051 {
3052
3053 KASSERT(sav != NULL);
3054 KASSERTMSG(sav->refcnt == 0, "reference count %u > 0", sav->refcnt);
3055
3056 /* remove from SA header */
3057 KASSERT(__LIST_CHAINED(sav));
3058 LIST_REMOVE(sav, chain);
3059
3060 /*
3061 * Cleanup xform state. Note that zeroize'ing causes the
3062 * keys to be cleared; otherwise we must do it ourself.
3063 */
3064 if (sav->tdb_xform != NULL) {
3065 sav->tdb_xform->xf_zeroize(sav);
3066 sav->tdb_xform = NULL;
3067 } else {
3068 if (sav->key_auth != NULL)
3069 explicit_memset(_KEYBUF(sav->key_auth), 0,
3070 _KEYLEN(sav->key_auth));
3071 if (sav->key_enc != NULL)
3072 explicit_memset(_KEYBUF(sav->key_enc), 0,
3073 _KEYLEN(sav->key_enc));
3074 }
3075
3076 key_freesaval(sav);
3077 kmem_intr_free(sav, sizeof(*sav));
3078
3079 return;
3080 }
3081
3082 /*
3083 * search SAD.
3084 * OUT:
3085 * NULL : not found
3086 * others : found, pointer to a SA.
3087 */
3088 static struct secashead *
3089 key_getsah(const struct secasindex *saidx)
3090 {
3091 struct secashead *sah;
3092
3093 LIST_FOREACH(sah, &sahtree, chain) {
3094 if (sah->state == SADB_SASTATE_DEAD)
3095 continue;
3096 if (key_saidx_match(&sah->saidx, saidx, CMP_REQID))
3097 return sah;
3098 }
3099
3100 return NULL;
3101 }
3102
3103 /*
3104 * check not to be duplicated SPI.
3105 * NOTE: this function is too slow due to searching all SAD.
3106 * OUT:
3107 * NULL : not found
3108 * others : found, pointer to a SA.
3109 */
3110 static struct secasvar *
3111 key_checkspidup(const struct secasindex *saidx, u_int32_t spi)
3112 {
3113 struct secashead *sah;
3114 struct secasvar *sav;
3115
3116 /* check address family */
3117 if (saidx->src.sa.sa_family != saidx->dst.sa.sa_family) {
3118 IPSECLOG(LOG_DEBUG, "address family mismatched.\n");
3119 return NULL;
3120 }
3121
3122 /* check all SAD */
3123 LIST_FOREACH(sah, &sahtree, chain) {
3124 if (!key_ismyaddr((struct sockaddr *)&sah->saidx.dst))
3125 continue;
3126 sav = key_getsavbyspi(sah, spi);
3127 if (sav != NULL)
3128 return sav;
3129 }
3130
3131 return NULL;
3132 }
3133
3134 /*
3135 * search SAD litmited alive SA, protocol, SPI.
3136 * OUT:
3137 * NULL : not found
3138 * others : found, pointer to a SA.
3139 */
3140 static struct secasvar *
3141 key_getsavbyspi(struct secashead *sah, u_int32_t spi)
3142 {
3143 struct secasvar *sav;
3144 u_int state;
3145
3146 /* search all status */
3147 SASTATE_ALIVE_FOREACH(state) {
3148 LIST_FOREACH(sav, &sah->savtree[state], chain) {
3149
3150 /* sanity check */
3151 if (sav->state != state) {
3152 IPSECLOG(LOG_DEBUG,
3153 "invalid sav->state (queue: %d SA: %d)\n",
3154 state, sav->state);
3155 continue;
3156 }
3157
3158 if (sav->spi == spi)
3159 return sav;
3160 }
3161 }
3162
3163 return NULL;
3164 }
3165
3166 /*
3167 * Free allocated data to member variables of sav:
3168 * sav->replay, sav->key_* and sav->lft_*.
3169 */
3170 static void
3171 key_freesaval(struct secasvar *sav)
3172 {
3173
3174 if (sav->replay != NULL) {
3175 kmem_intr_free(sav->replay, sav->replay_len);
3176 sav->replay = NULL;
3177 sav->replay_len = 0;
3178 }
3179 if (sav->key_auth != NULL) {
3180 kmem_intr_free(sav->key_auth, sav->key_auth_len);
3181 sav->key_auth = NULL;
3182 sav->key_auth_len = 0;
3183 }
3184 if (sav->key_enc != NULL) {
3185 kmem_intr_free(sav->key_enc, sav->key_enc_len);
3186 sav->key_enc = NULL;
3187 sav->key_enc_len = 0;
3188 }
3189 if (sav->lft_c != NULL) {
3190 kmem_intr_free(sav->lft_c, sizeof(*(sav->lft_c)));
3191 sav->lft_c = NULL;
3192 }
3193 if (sav->lft_h != NULL) {
3194 kmem_intr_free(sav->lft_h, sizeof(*(sav->lft_h)));
3195 sav->lft_h = NULL;
3196 }
3197 if (sav->lft_s != NULL) {
3198 kmem_intr_free(sav->lft_s, sizeof(*(sav->lft_s)));
3199 sav->lft_s = NULL;
3200 }
3201 }
3202
3203 /*
3204 * copy SA values from PF_KEY message except *SPI, SEQ, PID, STATE and TYPE*.
3205 * You must update these if need.
3206 * OUT: 0: success.
3207 * !0: failure.
3208 *
3209 * does not modify mbuf. does not free mbuf on error.
3210 */
3211 static int
3212 key_setsaval(struct secasvar *sav, struct mbuf *m,
3213 const struct sadb_msghdr *mhp)
3214 {
3215 int error = 0;
3216
3217 KASSERT(!cpu_softintr_p());
3218 KASSERT(m != NULL);
3219 KASSERT(mhp != NULL);
3220 KASSERT(mhp->msg != NULL);
3221
3222 /* initialization */
3223 key_freesaval(sav);
3224 sav->tdb_xform = NULL; /* transform */
3225 sav->tdb_encalgxform = NULL; /* encoding algorithm */
3226 sav->tdb_authalgxform = NULL; /* authentication algorithm */
3227 sav->tdb_compalgxform = NULL; /* compression algorithm */
3228 sav->natt_type = 0;
3229 sav->esp_frag = 0;
3230
3231 /* SA */
3232 if (mhp->ext[SADB_EXT_SA] != NULL) {
3233 const struct sadb_sa *sa0;
3234
3235 sa0 = (const struct sadb_sa *)mhp->ext[SADB_EXT_SA];
3236 if (mhp->extlen[SADB_EXT_SA] < sizeof(*sa0)) {
3237 error = EINVAL;
3238 goto fail;
3239 }
3240
3241 sav->alg_auth = sa0->sadb_sa_auth;
3242 sav->alg_enc = sa0->sadb_sa_encrypt;
3243 sav->flags = sa0->sadb_sa_flags;
3244
3245 /* replay window */
3246 if ((sa0->sadb_sa_flags & SADB_X_EXT_OLD) == 0) {
3247 size_t len = sizeof(struct secreplay) +
3248 sa0->sadb_sa_replay;
3249 sav->replay = kmem_zalloc(len, KM_SLEEP);
3250 sav->replay_len = len;
3251 if (sa0->sadb_sa_replay != 0)
3252 sav->replay->bitmap = (char*)(sav->replay+1);
3253 sav->replay->wsize = sa0->sadb_sa_replay;
3254 }
3255 }
3256
3257 /* Authentication keys */
3258 if (mhp->ext[SADB_EXT_KEY_AUTH] != NULL) {
3259 const struct sadb_key *key0;
3260 int len;
3261
3262 key0 = (const struct sadb_key *)mhp->ext[SADB_EXT_KEY_AUTH];
3263 len = mhp->extlen[SADB_EXT_KEY_AUTH];
3264
3265 error = 0;
3266 if (len < sizeof(*key0)) {
3267 error = EINVAL;
3268 goto fail;
3269 }
3270 switch (mhp->msg->sadb_msg_satype) {
3271 case SADB_SATYPE_AH:
3272 case SADB_SATYPE_ESP:
3273 case SADB_X_SATYPE_TCPSIGNATURE:
3274 if (len == PFKEY_ALIGN8(sizeof(struct sadb_key)) &&
3275 sav->alg_auth != SADB_X_AALG_NULL)
3276 error = EINVAL;
3277 break;
3278 case SADB_X_SATYPE_IPCOMP:
3279 default:
3280 error = EINVAL;
3281 break;
3282 }
3283 if (error) {
3284 IPSECLOG(LOG_DEBUG, "invalid key_auth values.\n");
3285 goto fail;
3286 }
3287
3288 sav->key_auth = key_newbuf(key0, len);
3289 sav->key_auth_len = len;
3290 }
3291
3292 /* Encryption key */
3293 if (mhp->ext[SADB_EXT_KEY_ENCRYPT] != NULL) {
3294 const struct sadb_key *key0;
3295 int len;
3296
3297 key0 = (const struct sadb_key *)mhp->ext[SADB_EXT_KEY_ENCRYPT];
3298 len = mhp->extlen[SADB_EXT_KEY_ENCRYPT];
3299
3300 error = 0;
3301 if (len < sizeof(*key0)) {
3302 error = EINVAL;
3303 goto fail;
3304 }
3305 switch (mhp->msg->sadb_msg_satype) {
3306 case SADB_SATYPE_ESP:
3307 if (len == PFKEY_ALIGN8(sizeof(struct sadb_key)) &&
3308 sav->alg_enc != SADB_EALG_NULL) {
3309 error = EINVAL;
3310 break;
3311 }
3312 sav->key_enc = key_newbuf(key0, len);
3313 sav->key_enc_len = len;
3314 break;
3315 case SADB_X_SATYPE_IPCOMP:
3316 if (len != PFKEY_ALIGN8(sizeof(struct sadb_key)))
3317 error = EINVAL;
3318 sav->key_enc = NULL; /*just in case*/
3319 break;
3320 case SADB_SATYPE_AH:
3321 case SADB_X_SATYPE_TCPSIGNATURE:
3322 default:
3323 error = EINVAL;
3324 break;
3325 }
3326 if (error) {
3327 IPSECLOG(LOG_DEBUG, "invalid key_enc value.\n");
3328 goto fail;
3329 }
3330 }
3331
3332 /* set iv */
3333 sav->ivlen = 0;
3334
3335 switch (mhp->msg->sadb_msg_satype) {
3336 case SADB_SATYPE_AH:
3337 error = xform_init(sav, XF_AH);
3338 break;
3339 case SADB_SATYPE_ESP:
3340 error = xform_init(sav, XF_ESP);
3341 break;
3342 case SADB_X_SATYPE_IPCOMP:
3343 error = xform_init(sav, XF_IPCOMP);
3344 break;
3345 case SADB_X_SATYPE_TCPSIGNATURE:
3346 error = xform_init(sav, XF_TCPSIGNATURE);
3347 break;
3348 }
3349 if (error) {
3350 IPSECLOG(LOG_DEBUG, "unable to initialize SA type %u.\n",
3351 mhp->msg->sadb_msg_satype);
3352 goto fail;
3353 }
3354
3355 /* reset created */
3356 sav->created = time_uptime;
3357
3358 /* make lifetime for CURRENT */
3359 sav->lft_c = kmem_alloc(sizeof(struct sadb_lifetime), KM_SLEEP);
3360
3361 sav->lft_c->sadb_lifetime_len =
3362 PFKEY_UNIT64(sizeof(struct sadb_lifetime));
3363 sav->lft_c->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
3364 sav->lft_c->sadb_lifetime_allocations = 0;
3365 sav->lft_c->sadb_lifetime_bytes = 0;
3366 sav->lft_c->sadb_lifetime_addtime = time_uptime;
3367 sav->lft_c->sadb_lifetime_usetime = 0;
3368
3369 /* lifetimes for HARD and SOFT */
3370 {
3371 const struct sadb_lifetime *lft0;
3372
3373 lft0 = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_HARD];
3374 if (lft0 != NULL) {
3375 if (mhp->extlen[SADB_EXT_LIFETIME_HARD] < sizeof(*lft0)) {
3376 error = EINVAL;
3377 goto fail;
3378 }
3379 sav->lft_h = key_newbuf(lft0, sizeof(*lft0));
3380 }
3381
3382 lft0 = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_SOFT];
3383 if (lft0 != NULL) {
3384 if (mhp->extlen[SADB_EXT_LIFETIME_SOFT] < sizeof(*lft0)) {
3385 error = EINVAL;
3386 goto fail;
3387 }
3388 sav->lft_s = key_newbuf(lft0, sizeof(*lft0));
3389 /* to be initialize ? */
3390 }
3391 }
3392
3393 return 0;
3394
3395 fail:
3396 /* initialization */
3397 key_freesaval(sav);
3398
3399 return error;
3400 }
3401
3402 /*
3403 * validation with a secasvar entry, and set SADB_SATYPE_MATURE.
3404 * OUT: 0: valid
3405 * other: errno
3406 */
3407 static int
3408 key_mature(struct secasvar *sav)
3409 {
3410 int error;
3411
3412 /* check SPI value */
3413 switch (sav->sah->saidx.proto) {
3414 case IPPROTO_ESP:
3415 case IPPROTO_AH:
3416 if (ntohl(sav->spi) <= 255) {
3417 IPSECLOG(LOG_DEBUG, "illegal range of SPI %u.\n",
3418 (u_int32_t)ntohl(sav->spi));
3419 return EINVAL;
3420 }
3421 break;
3422 }
3423
3424 /* check satype */
3425 switch (sav->sah->saidx.proto) {
3426 case IPPROTO_ESP:
3427 /* check flags */
3428 if ((sav->flags & (SADB_X_EXT_OLD|SADB_X_EXT_DERIV)) ==
3429 (SADB_X_EXT_OLD|SADB_X_EXT_DERIV)) {
3430 IPSECLOG(LOG_DEBUG,
3431 "invalid flag (derived) given to old-esp.\n");
3432 return EINVAL;
3433 }
3434 error = xform_init(sav, XF_ESP);
3435 break;
3436 case IPPROTO_AH:
3437 /* check flags */
3438 if (sav->flags & SADB_X_EXT_DERIV) {
3439 IPSECLOG(LOG_DEBUG,
3440 "invalid flag (derived) given to AH SA.\n");
3441 return EINVAL;
3442 }
3443 if (sav->alg_enc != SADB_EALG_NONE) {
3444 IPSECLOG(LOG_DEBUG,
3445 "protocol and algorithm mismated.\n");
3446 return(EINVAL);
3447 }
3448 error = xform_init(sav, XF_AH);
3449 break;
3450 case IPPROTO_IPCOMP:
3451 if (sav->alg_auth != SADB_AALG_NONE) {
3452 IPSECLOG(LOG_DEBUG,
3453 "protocol and algorithm mismated.\n");
3454 return(EINVAL);
3455 }
3456 if ((sav->flags & SADB_X_EXT_RAWCPI) == 0
3457 && ntohl(sav->spi) >= 0x10000) {
3458 IPSECLOG(LOG_DEBUG, "invalid cpi for IPComp.\n");
3459 return(EINVAL);
3460 }
3461 error = xform_init(sav, XF_IPCOMP);
3462 break;
3463 case IPPROTO_TCP:
3464 if (sav->alg_enc != SADB_EALG_NONE) {
3465 IPSECLOG(LOG_DEBUG,
3466 "protocol and algorithm mismated.\n");
3467 return(EINVAL);
3468 }
3469 error = xform_init(sav, XF_TCPSIGNATURE);
3470 break;
3471 default:
3472 IPSECLOG(LOG_DEBUG, "Invalid satype.\n");
3473 error = EPROTONOSUPPORT;
3474 break;
3475 }
3476 if (error == 0)
3477 key_sa_chgstate(sav, SADB_SASTATE_MATURE);
3478 return (error);
3479 }
3480
3481 /*
3482 * subroutine for SADB_GET and SADB_DUMP.
3483 */
3484 static struct mbuf *
3485 key_setdumpsa(struct secasvar *sav, u_int8_t type, u_int8_t satype,
3486 u_int32_t seq, u_int32_t pid)
3487 {
3488 struct mbuf *result = NULL, *tres = NULL, *m;
3489 int l = 0;
3490 int i;
3491 void *p;
3492 struct sadb_lifetime lt;
3493 int dumporder[] = {
3494 SADB_EXT_SA, SADB_X_EXT_SA2,
3495 SADB_EXT_LIFETIME_HARD, SADB_EXT_LIFETIME_SOFT,
3496 SADB_EXT_LIFETIME_CURRENT, SADB_EXT_ADDRESS_SRC,
3497 SADB_EXT_ADDRESS_DST, SADB_EXT_ADDRESS_PROXY, SADB_EXT_KEY_AUTH,
3498 SADB_EXT_KEY_ENCRYPT, SADB_EXT_IDENTITY_SRC,
3499 SADB_EXT_IDENTITY_DST, SADB_EXT_SENSITIVITY,
3500 SADB_X_EXT_NAT_T_TYPE,
3501 SADB_X_EXT_NAT_T_SPORT, SADB_X_EXT_NAT_T_DPORT,
3502 SADB_X_EXT_NAT_T_OAI, SADB_X_EXT_NAT_T_OAR,
3503 SADB_X_EXT_NAT_T_FRAG,
3504
3505 };
3506
3507 m = key_setsadbmsg(type, 0, satype, seq, pid, sav->refcnt);
3508 if (m == NULL)
3509 goto fail;
3510 result = m;
3511
3512 for (i = __arraycount(dumporder) - 1; i >= 0; i--) {
3513 m = NULL;
3514 p = NULL;
3515 switch (dumporder[i]) {
3516 case SADB_EXT_SA:
3517 m = key_setsadbsa(sav);
3518 break;
3519
3520 case SADB_X_EXT_SA2:
3521 m = key_setsadbxsa2(sav->sah->saidx.mode,
3522 sav->replay ? sav->replay->count : 0,
3523 sav->sah->saidx.reqid);
3524 break;
3525
3526 case SADB_EXT_ADDRESS_SRC:
3527 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
3528 &sav->sah->saidx.src.sa,
3529 FULLMASK, IPSEC_ULPROTO_ANY);
3530 break;
3531
3532 case SADB_EXT_ADDRESS_DST:
3533 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
3534 &sav->sah->saidx.dst.sa,
3535 FULLMASK, IPSEC_ULPROTO_ANY);
3536 break;
3537
3538 case SADB_EXT_KEY_AUTH:
3539 if (!sav->key_auth)
3540 continue;
3541 l = PFKEY_UNUNIT64(sav->key_auth->sadb_key_len);
3542 p = sav->key_auth;
3543 break;
3544
3545 case SADB_EXT_KEY_ENCRYPT:
3546 if (!sav->key_enc)
3547 continue;
3548 l = PFKEY_UNUNIT64(sav->key_enc->sadb_key_len);
3549 p = sav->key_enc;
3550 break;
3551
3552 case SADB_EXT_LIFETIME_CURRENT:
3553 if (!sav->lft_c)
3554 continue;
3555 l = PFKEY_UNUNIT64(((struct sadb_ext *)sav->lft_c)->sadb_ext_len);
3556 memcpy(<, sav->lft_c, sizeof(struct sadb_lifetime));
3557 lt.sadb_lifetime_addtime += time_second - time_uptime;
3558 lt.sadb_lifetime_usetime += time_second - time_uptime;
3559 p = <
3560 break;
3561
3562 case SADB_EXT_LIFETIME_HARD:
3563 if (!sav->lft_h)
3564 continue;
3565 l = PFKEY_UNUNIT64(((struct sadb_ext *)sav->lft_h)->sadb_ext_len);
3566 p = sav->lft_h;
3567 break;
3568
3569 case SADB_EXT_LIFETIME_SOFT:
3570 if (!sav->lft_s)
3571 continue;
3572 l = PFKEY_UNUNIT64(((struct sadb_ext *)sav->lft_s)->sadb_ext_len);
3573 p = sav->lft_s;
3574 break;
3575
3576 case SADB_X_EXT_NAT_T_TYPE:
3577 m = key_setsadbxtype(sav->natt_type);
3578 break;
3579
3580 case SADB_X_EXT_NAT_T_DPORT:
3581 if (sav->natt_type == 0)
3582 continue;
3583 m = key_setsadbxport(
3584 key_portfromsaddr(&sav->sah->saidx.dst),
3585 SADB_X_EXT_NAT_T_DPORT);
3586 break;
3587
3588 case SADB_X_EXT_NAT_T_SPORT:
3589 if (sav->natt_type == 0)
3590 continue;
3591 m = key_setsadbxport(
3592 key_portfromsaddr(&sav->sah->saidx.src),
3593 SADB_X_EXT_NAT_T_SPORT);
3594 break;
3595
3596 case SADB_X_EXT_NAT_T_FRAG:
3597 /* don't send frag info if not set */
3598 if (sav->natt_type == 0 || sav->esp_frag == IP_MAXPACKET)
3599 continue;
3600 m = key_setsadbxfrag(sav->esp_frag);
3601 break;
3602
3603 case SADB_X_EXT_NAT_T_OAI:
3604 case SADB_X_EXT_NAT_T_OAR:
3605 continue;
3606
3607 case SADB_EXT_ADDRESS_PROXY:
3608 case SADB_EXT_IDENTITY_SRC:
3609 case SADB_EXT_IDENTITY_DST:
3610 /* XXX: should we brought from SPD ? */
3611 case SADB_EXT_SENSITIVITY:
3612 default:
3613 continue;
3614 }
3615
3616 KASSERT(!(m && p));
3617 if (!m && !p)
3618 goto fail;
3619 if (p && tres) {
3620 M_PREPEND(tres, l, M_DONTWAIT);
3621 if (!tres)
3622 goto fail;
3623 memcpy(mtod(tres, void *), p, l);
3624 continue;
3625 }
3626 if (p) {
3627 m = key_alloc_mbuf(l);
3628 if (!m)
3629 goto fail;
3630 m_copyback(m, 0, l, p);
3631 }
3632
3633 if (tres)
3634 m_cat(m, tres);
3635 tres = m;
3636 }
3637
3638 m_cat(result, tres);
3639 tres = NULL; /* avoid free on error below */
3640
3641 if (result->m_len < sizeof(struct sadb_msg)) {
3642 result = m_pullup(result, sizeof(struct sadb_msg));
3643 if (result == NULL)
3644 goto fail;
3645 }
3646
3647 result->m_pkthdr.len = 0;
3648 for (m = result; m; m = m->m_next)
3649 result->m_pkthdr.len += m->m_len;
3650
3651 mtod(result, struct sadb_msg *)->sadb_msg_len =
3652 PFKEY_UNIT64(result->m_pkthdr.len);
3653
3654 return result;
3655
3656 fail:
3657 m_freem(result);
3658 m_freem(tres);
3659 return NULL;
3660 }
3661
3662
3663 /*
3664 * set a type in sadb_x_nat_t_type
3665 */
3666 static struct mbuf *
3667 key_setsadbxtype(u_int16_t type)
3668 {
3669 struct mbuf *m;
3670 size_t len;
3671 struct sadb_x_nat_t_type *p;
3672
3673 len = PFKEY_ALIGN8(sizeof(struct sadb_x_nat_t_type));
3674
3675 m = key_alloc_mbuf(len);
3676 if (!m || m->m_next) { /*XXX*/
3677 if (m)
3678 m_freem(m);
3679 return NULL;
3680 }
3681
3682 p = mtod(m, struct sadb_x_nat_t_type *);
3683
3684 memset(p, 0, len);
3685 p->sadb_x_nat_t_type_len = PFKEY_UNIT64(len);
3686 p->sadb_x_nat_t_type_exttype = SADB_X_EXT_NAT_T_TYPE;
3687 p->sadb_x_nat_t_type_type = type;
3688
3689 return m;
3690 }
3691 /*
3692 * set a port in sadb_x_nat_t_port. port is in network order
3693 */
3694 static struct mbuf *
3695 key_setsadbxport(u_int16_t port, u_int16_t type)
3696 {
3697 struct mbuf *m;
3698 size_t len;
3699 struct sadb_x_nat_t_port *p;
3700
3701 len = PFKEY_ALIGN8(sizeof(struct sadb_x_nat_t_port));
3702
3703 m = key_alloc_mbuf(len);
3704 if (!m || m->m_next) { /*XXX*/
3705 if (m)
3706 m_freem(m);
3707 return NULL;
3708 }
3709
3710 p = mtod(m, struct sadb_x_nat_t_port *);
3711
3712 memset(p, 0, len);
3713 p->sadb_x_nat_t_port_len = PFKEY_UNIT64(len);
3714 p->sadb_x_nat_t_port_exttype = type;
3715 p->sadb_x_nat_t_port_port = port;
3716
3717 return m;
3718 }
3719
3720 /*
3721 * set fragmentation info in sadb_x_nat_t_frag
3722 */
3723 static struct mbuf *
3724 key_setsadbxfrag(u_int16_t flen)
3725 {
3726 struct mbuf *m;
3727 size_t len;
3728 struct sadb_x_nat_t_frag *p;
3729
3730 len = PFKEY_ALIGN8(sizeof(struct sadb_x_nat_t_frag));
3731
3732 m = key_alloc_mbuf(len);
3733 if (!m || m->m_next) { /*XXX*/
3734 if (m)
3735 m_freem(m);
3736 return NULL;
3737 }
3738
3739 p = mtod(m, struct sadb_x_nat_t_frag *);
3740
3741 memset(p, 0, len);
3742 p->sadb_x_nat_t_frag_len = PFKEY_UNIT64(len);
3743 p->sadb_x_nat_t_frag_exttype = SADB_X_EXT_NAT_T_FRAG;
3744 p->sadb_x_nat_t_frag_fraglen = flen;
3745
3746 return m;
3747 }
3748
3749 /*
3750 * Get port from sockaddr, port is in network order
3751 */
3752 u_int16_t
3753 key_portfromsaddr(const union sockaddr_union *saddr)
3754 {
3755 u_int16_t port;
3756
3757 switch (saddr->sa.sa_family) {
3758 case AF_INET: {
3759 port = saddr->sin.sin_port;
3760 break;
3761 }
3762 #ifdef INET6
3763 case AF_INET6: {
3764 port = saddr->sin6.sin6_port;
3765 break;
3766 }
3767 #endif
3768 default:
3769 printf("%s: unexpected address family\n", __func__);
3770 port = 0;
3771 break;
3772 }
3773
3774 return port;
3775 }
3776
3777
3778 /*
3779 * Set port is struct sockaddr. port is in network order
3780 */
3781 static void
3782 key_porttosaddr(union sockaddr_union *saddr, u_int16_t port)
3783 {
3784 switch (saddr->sa.sa_family) {
3785 case AF_INET: {
3786 saddr->sin.sin_port = port;
3787 break;
3788 }
3789 #ifdef INET6
3790 case AF_INET6: {
3791 saddr->sin6.sin6_port = port;
3792 break;
3793 }
3794 #endif
3795 default:
3796 printf("%s: unexpected address family %d\n", __func__,
3797 saddr->sa.sa_family);
3798 break;
3799 }
3800
3801 return;
3802 }
3803
3804 /*
3805 * Safety check sa_len
3806 */
3807 static int
3808 key_checksalen(const union sockaddr_union *saddr)
3809 {
3810 switch (saddr->sa.sa_family) {
3811 case AF_INET:
3812 if (saddr->sa.sa_len != sizeof(struct sockaddr_in))
3813 return -1;
3814 break;
3815 #ifdef INET6
3816 case AF_INET6:
3817 if (saddr->sa.sa_len != sizeof(struct sockaddr_in6))
3818 return -1;
3819 break;
3820 #endif
3821 default:
3822 printf("%s: unexpected sa_family %d\n", __func__,
3823 saddr->sa.sa_family);
3824 return -1;
3825 break;
3826 }
3827 return 0;
3828 }
3829
3830
3831 /*
3832 * set data into sadb_msg.
3833 */
3834 static struct mbuf *
3835 key_setsadbmsg(u_int8_t type, u_int16_t tlen, u_int8_t satype,
3836 u_int32_t seq, pid_t pid, u_int16_t reserved)
3837 {
3838 struct mbuf *m;
3839 struct sadb_msg *p;
3840 int len;
3841
3842 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
3843 if (len > MCLBYTES)
3844 return NULL;
3845 MGETHDR(m, M_DONTWAIT, MT_DATA);
3846 if (m && len > MHLEN) {
3847 MCLGET(m, M_DONTWAIT);
3848 if ((m->m_flags & M_EXT) == 0) {
3849 m_freem(m);
3850 m = NULL;
3851 }
3852 }
3853 if (!m)
3854 return NULL;
3855 m->m_pkthdr.len = m->m_len = len;
3856 m->m_next = NULL;
3857
3858 p = mtod(m, struct sadb_msg *);
3859
3860 memset(p, 0, len);
3861 p->sadb_msg_version = PF_KEY_V2;
3862 p->sadb_msg_type = type;
3863 p->sadb_msg_errno = 0;
3864 p->sadb_msg_satype = satype;
3865 p->sadb_msg_len = PFKEY_UNIT64(tlen);
3866 p->sadb_msg_reserved = reserved;
3867 p->sadb_msg_seq = seq;
3868 p->sadb_msg_pid = (u_int32_t)pid;
3869
3870 return m;
3871 }
3872
3873 /*
3874 * copy secasvar data into sadb_address.
3875 */
3876 static struct mbuf *
3877 key_setsadbsa(struct secasvar *sav)
3878 {
3879 struct mbuf *m;
3880 struct sadb_sa *p;
3881 int len;
3882
3883 len = PFKEY_ALIGN8(sizeof(struct sadb_sa));
3884 m = key_alloc_mbuf(len);
3885 if (!m || m->m_next) { /*XXX*/
3886 if (m)
3887 m_freem(m);
3888 return NULL;
3889 }
3890
3891 p = mtod(m, struct sadb_sa *);
3892
3893 memset(p, 0, len);
3894 p->sadb_sa_len = PFKEY_UNIT64(len);
3895 p->sadb_sa_exttype = SADB_EXT_SA;
3896 p->sadb_sa_spi = sav->spi;
3897 p->sadb_sa_replay = (sav->replay != NULL ? sav->replay->wsize : 0);
3898 p->sadb_sa_state = sav->state;
3899 p->sadb_sa_auth = sav->alg_auth;
3900 p->sadb_sa_encrypt = sav->alg_enc;
3901 p->sadb_sa_flags = sav->flags;
3902
3903 return m;
3904 }
3905
3906 /*
3907 * set data into sadb_address.
3908 */
3909 static struct mbuf *
3910 key_setsadbaddr(u_int16_t exttype, const struct sockaddr *saddr,
3911 u_int8_t prefixlen, u_int16_t ul_proto)
3912 {
3913 struct mbuf *m;
3914 struct sadb_address *p;
3915 size_t len;
3916
3917 len = PFKEY_ALIGN8(sizeof(struct sadb_address)) +
3918 PFKEY_ALIGN8(saddr->sa_len);
3919 m = key_alloc_mbuf(len);
3920 if (!m || m->m_next) { /*XXX*/
3921 if (m)
3922 m_freem(m);
3923 return NULL;
3924 }
3925
3926 p = mtod(m, struct sadb_address *);
3927
3928 memset(p, 0, len);
3929 p->sadb_address_len = PFKEY_UNIT64(len);
3930 p->sadb_address_exttype = exttype;
3931 p->sadb_address_proto = ul_proto;
3932 if (prefixlen == FULLMASK) {
3933 switch (saddr->sa_family) {
3934 case AF_INET:
3935 prefixlen = sizeof(struct in_addr) << 3;
3936 break;
3937 case AF_INET6:
3938 prefixlen = sizeof(struct in6_addr) << 3;
3939 break;
3940 default:
3941 ; /*XXX*/
3942 }
3943 }
3944 p->sadb_address_prefixlen = prefixlen;
3945 p->sadb_address_reserved = 0;
3946
3947 memcpy(mtod(m, char *) + PFKEY_ALIGN8(sizeof(struct sadb_address)),
3948 saddr, saddr->sa_len);
3949
3950 return m;
3951 }
3952
3953 #if 0
3954 /*
3955 * set data into sadb_ident.
3956 */
3957 static struct mbuf *
3958 key_setsadbident(u_int16_t exttype, u_int16_t idtype,
3959 void *string, int stringlen, u_int64_t id)
3960 {
3961 struct mbuf *m;
3962 struct sadb_ident *p;
3963 size_t len;
3964
3965 len = PFKEY_ALIGN8(sizeof(struct sadb_ident)) + PFKEY_ALIGN8(stringlen);
3966 m = key_alloc_mbuf(len);
3967 if (!m || m->m_next) { /*XXX*/
3968 if (m)
3969 m_freem(m);
3970 return NULL;
3971 }
3972
3973 p = mtod(m, struct sadb_ident *);
3974
3975 memset(p, 0, len);
3976 p->sadb_ident_len = PFKEY_UNIT64(len);
3977 p->sadb_ident_exttype = exttype;
3978 p->sadb_ident_type = idtype;
3979 p->sadb_ident_reserved = 0;
3980 p->sadb_ident_id = id;
3981
3982 memcpy(mtod(m, void *) + PFKEY_ALIGN8(sizeof(struct sadb_ident)),
3983 string, stringlen);
3984
3985 return m;
3986 }
3987 #endif
3988
3989 /*
3990 * set data into sadb_x_sa2.
3991 */
3992 static struct mbuf *
3993 key_setsadbxsa2(u_int8_t mode, u_int32_t seq, u_int16_t reqid)
3994 {
3995 struct mbuf *m;
3996 struct sadb_x_sa2 *p;
3997 size_t len;
3998
3999 len = PFKEY_ALIGN8(sizeof(struct sadb_x_sa2));
4000 m = key_alloc_mbuf(len);
4001 if (!m || m->m_next) { /*XXX*/
4002 if (m)
4003 m_freem(m);
4004 return NULL;
4005 }
4006
4007 p = mtod(m, struct sadb_x_sa2 *);
4008
4009 memset(p, 0, len);
4010 p->sadb_x_sa2_len = PFKEY_UNIT64(len);
4011 p->sadb_x_sa2_exttype = SADB_X_EXT_SA2;
4012 p->sadb_x_sa2_mode = mode;
4013 p->sadb_x_sa2_reserved1 = 0;
4014 p->sadb_x_sa2_reserved2 = 0;
4015 p->sadb_x_sa2_sequence = seq;
4016 p->sadb_x_sa2_reqid = reqid;
4017
4018 return m;
4019 }
4020
4021 /*
4022 * set data into sadb_x_policy
4023 */
4024 static struct mbuf *
4025 key_setsadbxpolicy(u_int16_t type, u_int8_t dir, u_int32_t id)
4026 {
4027 struct mbuf *m;
4028 struct sadb_x_policy *p;
4029 size_t len;
4030
4031 len = PFKEY_ALIGN8(sizeof(struct sadb_x_policy));
4032 m = key_alloc_mbuf(len);
4033 if (!m || m->m_next) { /*XXX*/
4034 if (m)
4035 m_freem(m);
4036 return NULL;
4037 }
4038
4039 p = mtod(m, struct sadb_x_policy *);
4040
4041 memset(p, 0, len);
4042 p->sadb_x_policy_len = PFKEY_UNIT64(len);
4043 p->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
4044 p->sadb_x_policy_type = type;
4045 p->sadb_x_policy_dir = dir;
4046 p->sadb_x_policy_id = id;
4047
4048 return m;
4049 }
4050
4051 /* %%% utilities */
4052 /*
4053 * copy a buffer into the new buffer allocated.
4054 */
4055 static void *
4056 key_newbuf(const void *src, u_int len)
4057 {
4058 void *new;
4059
4060 new = kmem_alloc(len, KM_SLEEP);
4061 memcpy(new, src, len);
4062
4063 return new;
4064 }
4065
4066 /* compare my own address
4067 * OUT: 1: true, i.e. my address.
4068 * 0: false
4069 */
4070 int
4071 key_ismyaddr(const struct sockaddr *sa)
4072 {
4073 #ifdef INET
4074 const struct sockaddr_in *sin;
4075 const struct in_ifaddr *ia;
4076 int s;
4077 #endif
4078
4079 KASSERT(sa != NULL);
4080
4081 switch (sa->sa_family) {
4082 #ifdef INET
4083 case AF_INET:
4084 sin = (const struct sockaddr_in *)sa;
4085 s = pserialize_read_enter();
4086 IN_ADDRLIST_READER_FOREACH(ia) {
4087 if (sin->sin_family == ia->ia_addr.sin_family &&
4088 sin->sin_len == ia->ia_addr.sin_len &&
4089 sin->sin_addr.s_addr == ia->ia_addr.sin_addr.s_addr)
4090 {
4091 pserialize_read_exit(s);
4092 return 1;
4093 }
4094 }
4095 pserialize_read_exit(s);
4096 break;
4097 #endif
4098 #ifdef INET6
4099 case AF_INET6:
4100 return key_ismyaddr6((const struct sockaddr_in6 *)sa);
4101 #endif
4102 }
4103
4104 return 0;
4105 }
4106
4107 #ifdef INET6
4108 /*
4109 * compare my own address for IPv6.
4110 * 1: ours
4111 * 0: other
4112 * NOTE: derived ip6_input() in KAME. This is necessary to modify more.
4113 */
4114 #include <netinet6/in6_var.h>
4115
4116 static int
4117 key_ismyaddr6(const struct sockaddr_in6 *sin6)
4118 {
4119 struct in6_ifaddr *ia;
4120 int s;
4121 struct psref psref;
4122 int bound;
4123 int ours = 1;
4124
4125 bound = curlwp_bind();
4126 s = pserialize_read_enter();
4127 IN6_ADDRLIST_READER_FOREACH(ia) {
4128 bool ingroup;
4129
4130 if (key_sockaddr_match((const struct sockaddr *)&sin6,
4131 (const struct sockaddr *)&ia->ia_addr, 0)) {
4132 pserialize_read_exit(s);
4133 goto ours;
4134 }
4135 ia6_acquire(ia, &psref);
4136 pserialize_read_exit(s);
4137
4138 /*
4139 * XXX Multicast
4140 * XXX why do we care about multlicast here while we don't care
4141 * about IPv4 multicast??
4142 * XXX scope
4143 */
4144 ingroup = in6_multi_group(&sin6->sin6_addr, ia->ia_ifp);
4145 if (ingroup) {
4146 ia6_release(ia, &psref);
4147 goto ours;
4148 }
4149
4150 s = pserialize_read_enter();
4151 ia6_release(ia, &psref);
4152 }
4153 pserialize_read_exit(s);
4154
4155 /* loopback, just for safety */
4156 if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
4157 goto ours;
4158
4159 ours = 0;
4160 ours:
4161 curlwp_bindx(bound);
4162
4163 return ours;
4164 }
4165 #endif /*INET6*/
4166
4167 /*
4168 * compare two secasindex structure.
4169 * flag can specify to compare 2 saidxes.
4170 * compare two secasindex structure without both mode and reqid.
4171 * don't compare port.
4172 * IN:
4173 * saidx0: source, it can be in SAD.
4174 * saidx1: object.
4175 * OUT:
4176 * 1 : equal
4177 * 0 : not equal
4178 */
4179 static int
4180 key_saidx_match(
4181 const struct secasindex *saidx0,
4182 const struct secasindex *saidx1,
4183 int flag)
4184 {
4185 int chkport;
4186 const struct sockaddr *sa0src, *sa0dst, *sa1src, *sa1dst;
4187
4188 /* sanity */
4189 if (saidx0 == NULL && saidx1 == NULL)
4190 return 1;
4191
4192 if (saidx0 == NULL || saidx1 == NULL)
4193 return 0;
4194
4195 if (saidx0->proto != saidx1->proto)
4196 return 0;
4197
4198 if (flag == CMP_EXACTLY) {
4199 if (saidx0->mode != saidx1->mode)
4200 return 0;
4201 if (saidx0->reqid != saidx1->reqid)
4202 return 0;
4203 if (memcmp(&saidx0->src, &saidx1->src, saidx0->src.sa.sa_len) != 0 ||
4204 memcmp(&saidx0->dst, &saidx1->dst, saidx0->dst.sa.sa_len) != 0)
4205 return 0;
4206 } else {
4207
4208 /* CMP_MODE_REQID, CMP_REQID, CMP_HEAD */
4209 if (flag == CMP_MODE_REQID ||flag == CMP_REQID) {
4210 /*
4211 * If reqid of SPD is non-zero, unique SA is required.
4212 * The result must be of same reqid in this case.
4213 */
4214 if (saidx1->reqid != 0 && saidx0->reqid != saidx1->reqid)
4215 return 0;
4216 }
4217
4218 if (flag == CMP_MODE_REQID) {
4219 if (saidx0->mode != IPSEC_MODE_ANY &&
4220 saidx0->mode != saidx1->mode)
4221 return 0;
4222 }
4223
4224
4225 sa0src = &saidx0->src.sa;
4226 sa0dst = &saidx0->dst.sa;
4227 sa1src = &saidx1->src.sa;
4228 sa1dst = &saidx1->dst.sa;
4229 /*
4230 * If NAT-T is enabled, check ports for tunnel mode.
4231 * Don't do it for transport mode, as there is no
4232 * port information available in the SP.
4233 * Also don't check ports if they are set to zero
4234 * in the SPD: This means we have a non-generated
4235 * SPD which can't know UDP ports.
4236 */
4237 if (saidx1->mode == IPSEC_MODE_TUNNEL)
4238 chkport = PORT_LOOSE;
4239 else
4240 chkport = PORT_NONE;
4241
4242 if (!key_sockaddr_match(sa0src, sa1src, chkport)) {
4243 return 0;
4244 }
4245 if (!key_sockaddr_match(sa0dst, sa1dst, chkport)) {
4246 return 0;
4247 }
4248 }
4249
4250 return 1;
4251 }
4252
4253 /*
4254 * compare two secindex structure exactly.
4255 * IN:
4256 * spidx0: source, it is often in SPD.
4257 * spidx1: object, it is often from PFKEY message.
4258 * OUT:
4259 * 1 : equal
4260 * 0 : not equal
4261 */
4262 static int
4263 key_spidx_match_exactly(
4264 const struct secpolicyindex *spidx0,
4265 const struct secpolicyindex *spidx1)
4266 {
4267 /* sanity */
4268 if (spidx0 == NULL && spidx1 == NULL)
4269 return 1;
4270
4271 if (spidx0 == NULL || spidx1 == NULL)
4272 return 0;
4273
4274 if (spidx0->prefs != spidx1->prefs ||
4275 spidx0->prefd != spidx1->prefd ||
4276 spidx0->ul_proto != spidx1->ul_proto)
4277 return 0;
4278
4279 return key_sockaddr_match(&spidx0->src.sa, &spidx1->src.sa, PORT_STRICT) &&
4280 key_sockaddr_match(&spidx0->dst.sa, &spidx1->dst.sa, PORT_STRICT);
4281 }
4282
4283 /*
4284 * compare two secindex structure with mask.
4285 * IN:
4286 * spidx0: source, it is often in SPD.
4287 * spidx1: object, it is often from IP header.
4288 * OUT:
4289 * 1 : equal
4290 * 0 : not equal
4291 */
4292 static int
4293 key_spidx_match_withmask(
4294 const struct secpolicyindex *spidx0,
4295 const struct secpolicyindex *spidx1)
4296 {
4297
4298 KASSERT(spidx0 != NULL);
4299 KASSERT(spidx1 != NULL);
4300
4301 if (spidx0->src.sa.sa_family != spidx1->src.sa.sa_family ||
4302 spidx0->dst.sa.sa_family != spidx1->dst.sa.sa_family ||
4303 spidx0->src.sa.sa_len != spidx1->src.sa.sa_len ||
4304 spidx0->dst.sa.sa_len != spidx1->dst.sa.sa_len)
4305 return 0;
4306
4307 /* if spidx.ul_proto == IPSEC_ULPROTO_ANY, ignore. */
4308 if (spidx0->ul_proto != (u_int16_t)IPSEC_ULPROTO_ANY &&
4309 spidx0->ul_proto != spidx1->ul_proto)
4310 return 0;
4311
4312 switch (spidx0->src.sa.sa_family) {
4313 case AF_INET:
4314 if (spidx0->src.sin.sin_port != IPSEC_PORT_ANY &&
4315 spidx0->src.sin.sin_port != spidx1->src.sin.sin_port)
4316 return 0;
4317 if (!key_bb_match_withmask(&spidx0->src.sin.sin_addr,
4318 &spidx1->src.sin.sin_addr, spidx0->prefs))
4319 return 0;
4320 break;
4321 case AF_INET6:
4322 if (spidx0->src.sin6.sin6_port != IPSEC_PORT_ANY &&
4323 spidx0->src.sin6.sin6_port != spidx1->src.sin6.sin6_port)
4324 return 0;
4325 /*
4326 * scope_id check. if sin6_scope_id is 0, we regard it
4327 * as a wildcard scope, which matches any scope zone ID.
4328 */
4329 if (spidx0->src.sin6.sin6_scope_id &&
4330 spidx1->src.sin6.sin6_scope_id &&
4331 spidx0->src.sin6.sin6_scope_id != spidx1->src.sin6.sin6_scope_id)
4332 return 0;
4333 if (!key_bb_match_withmask(&spidx0->src.sin6.sin6_addr,
4334 &spidx1->src.sin6.sin6_addr, spidx0->prefs))
4335 return 0;
4336 break;
4337 default:
4338 /* XXX */
4339 if (memcmp(&spidx0->src, &spidx1->src, spidx0->src.sa.sa_len) != 0)
4340 return 0;
4341 break;
4342 }
4343
4344 switch (spidx0->dst.sa.sa_family) {
4345 case AF_INET:
4346 if (spidx0->dst.sin.sin_port != IPSEC_PORT_ANY &&
4347 spidx0->dst.sin.sin_port != spidx1->dst.sin.sin_port)
4348 return 0;
4349 if (!key_bb_match_withmask(&spidx0->dst.sin.sin_addr,
4350 &spidx1->dst.sin.sin_addr, spidx0->prefd))
4351 return 0;
4352 break;
4353 case AF_INET6:
4354 if (spidx0->dst.sin6.sin6_port != IPSEC_PORT_ANY &&
4355 spidx0->dst.sin6.sin6_port != spidx1->dst.sin6.sin6_port)
4356 return 0;
4357 /*
4358 * scope_id check. if sin6_scope_id is 0, we regard it
4359 * as a wildcard scope, which matches any scope zone ID.
4360 */
4361 if (spidx0->src.sin6.sin6_scope_id &&
4362 spidx1->src.sin6.sin6_scope_id &&
4363 spidx0->dst.sin6.sin6_scope_id != spidx1->dst.sin6.sin6_scope_id)
4364 return 0;
4365 if (!key_bb_match_withmask(&spidx0->dst.sin6.sin6_addr,
4366 &spidx1->dst.sin6.sin6_addr, spidx0->prefd))
4367 return 0;
4368 break;
4369 default:
4370 /* XXX */
4371 if (memcmp(&spidx0->dst, &spidx1->dst, spidx0->dst.sa.sa_len) != 0)
4372 return 0;
4373 break;
4374 }
4375
4376 /* XXX Do we check other field ? e.g. flowinfo */
4377
4378 return 1;
4379 }
4380
4381 /* returns 0 on match */
4382 static int
4383 key_portcomp(in_port_t port1, in_port_t port2, int howport)
4384 {
4385 switch (howport) {
4386 case PORT_NONE:
4387 return 0;
4388 case PORT_LOOSE:
4389 if (port1 == 0 || port2 == 0)
4390 return 0;
4391 /*FALLTHROUGH*/
4392 case PORT_STRICT:
4393 if (port1 != port2) {
4394 KEYDEBUG_PRINTF(KEYDEBUG_MATCH,
4395 "port fail %d != %d\n", port1, port2);
4396 return 1;
4397 }
4398 return 0;
4399 default:
4400 KASSERT(0);
4401 return 1;
4402 }
4403 }
4404
4405 /* returns 1 on match */
4406 static int
4407 key_sockaddr_match(
4408 const struct sockaddr *sa1,
4409 const struct sockaddr *sa2,
4410 int howport)
4411 {
4412 const struct sockaddr_in *sin1, *sin2;
4413 const struct sockaddr_in6 *sin61, *sin62;
4414
4415 if (sa1->sa_family != sa2->sa_family || sa1->sa_len != sa2->sa_len) {
4416 KEYDEBUG_PRINTF(KEYDEBUG_MATCH,
4417 "fam/len fail %d != %d || %d != %d\n",
4418 sa1->sa_family, sa2->sa_family, sa1->sa_len,
4419 sa2->sa_len);
4420 return 0;
4421 }
4422
4423 switch (sa1->sa_family) {
4424 case AF_INET:
4425 if (sa1->sa_len != sizeof(struct sockaddr_in)) {
4426 KEYDEBUG_PRINTF(KEYDEBUG_MATCH,
4427 "len fail %d != %zu\n",
4428 sa1->sa_len, sizeof(struct sockaddr_in));
4429 return 0;
4430 }
4431 sin1 = (const struct sockaddr_in *)sa1;
4432 sin2 = (const struct sockaddr_in *)sa2;
4433 if (sin1->sin_addr.s_addr != sin2->sin_addr.s_addr) {
4434 KEYDEBUG_PRINTF(KEYDEBUG_MATCH,
4435 "addr fail %#x != %#x\n",
4436 sin1->sin_addr.s_addr, sin2->sin_addr.s_addr);
4437 return 0;
4438 }
4439 if (key_portcomp(sin1->sin_port, sin2->sin_port, howport)) {
4440 return 0;
4441 }
4442 KEYDEBUG_PRINTF(KEYDEBUG_MATCH,
4443 "addr success %#x[%d] == %#x[%d]\n",
4444 sin1->sin_addr.s_addr, sin1->sin_port,
4445 sin2->sin_addr.s_addr, sin2->sin_port);
4446 break;
4447 case AF_INET6:
4448 sin61 = (const struct sockaddr_in6 *)sa1;
4449 sin62 = (const struct sockaddr_in6 *)sa2;
4450 if (sa1->sa_len != sizeof(struct sockaddr_in6))
4451 return 0; /*EINVAL*/
4452
4453 if (sin61->sin6_scope_id != sin62->sin6_scope_id) {
4454 return 0;
4455 }
4456 if (!IN6_ARE_ADDR_EQUAL(&sin61->sin6_addr, &sin62->sin6_addr)) {
4457 return 0;
4458 }
4459 if (key_portcomp(sin61->sin6_port, sin62->sin6_port, howport)) {
4460 return 0;
4461 }
4462 break;
4463 default:
4464 if (memcmp(sa1, sa2, sa1->sa_len) != 0)
4465 return 0;
4466 break;
4467 }
4468
4469 return 1;
4470 }
4471
4472 /*
4473 * compare two buffers with mask.
4474 * IN:
4475 * addr1: source
4476 * addr2: object
4477 * bits: Number of bits to compare
4478 * OUT:
4479 * 1 : equal
4480 * 0 : not equal
4481 */
4482 static int
4483 key_bb_match_withmask(const void *a1, const void *a2, u_int bits)
4484 {
4485 const unsigned char *p1 = a1;
4486 const unsigned char *p2 = a2;
4487
4488 /* XXX: This could be considerably faster if we compare a word
4489 * at a time, but it is complicated on LSB Endian machines */
4490
4491 /* Handle null pointers */
4492 if (p1 == NULL || p2 == NULL)
4493 return (p1 == p2);
4494
4495 while (bits >= 8) {
4496 if (*p1++ != *p2++)
4497 return 0;
4498 bits -= 8;
4499 }
4500
4501 if (bits > 0) {
4502 u_int8_t mask = ~((1<<(8-bits))-1);
4503 if ((*p1 & mask) != (*p2 & mask))
4504 return 0;
4505 }
4506 return 1; /* Match! */
4507 }
4508
4509 /*
4510 * time handler.
4511 * scanning SPD and SAD to check status for each entries,
4512 * and do to remove or to expire.
4513 */
4514 static void
4515 key_timehandler_work(struct work *wk, void *arg)
4516 {
4517 u_int dir;
4518 int s;
4519 time_t now = time_uptime;
4520
4521 s = splsoftnet();
4522 mutex_enter(softnet_lock);
4523
4524 /* SPD */
4525 {
4526 struct secpolicy *sp, *nextsp;
4527
4528 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
4529 LIST_FOREACH_SAFE(sp, &sptree[dir], chain, nextsp) {
4530 if (sp->state == IPSEC_SPSTATE_DEAD) {
4531 key_sp_unlink(sp); /*XXX*/
4532
4533 /* 'sp' dead; continue transfers to
4534 * 'sp = nextsp'
4535 */
4536 continue;
4537 }
4538
4539 if (sp->lifetime == 0 && sp->validtime == 0)
4540 continue;
4541
4542 /* the deletion will occur next time */
4543 if ((sp->lifetime && now - sp->created > sp->lifetime) ||
4544 (sp->validtime && now - sp->lastused > sp->validtime)) {
4545 key_sp_dead(sp);
4546 key_spdexpire(sp);
4547 continue;
4548 }
4549 }
4550 }
4551 }
4552
4553 /* SAD */
4554 {
4555 struct secashead *sah, *nextsah;
4556 struct secasvar *sav, *nextsav;
4557
4558 LIST_FOREACH_SAFE(sah, &sahtree, chain, nextsah) {
4559 /* if sah has been dead, then delete it and process next sah. */
4560 if (sah->state == SADB_SASTATE_DEAD) {
4561 key_delsah(sah);
4562 continue;
4563 }
4564
4565 /* if LARVAL entry doesn't become MATURE, delete it. */
4566 LIST_FOREACH_SAFE(sav, &sah->savtree[SADB_SASTATE_LARVAL],
4567 chain, nextsav) {
4568 if (now - sav->created > key_larval_lifetime) {
4569 KEY_FREESAV(&sav);
4570 }
4571 }
4572
4573 /*
4574 * check MATURE entry to start to send expire message
4575 * whether or not.
4576 */
4577 LIST_FOREACH_SAFE(sav, &sah->savtree[SADB_SASTATE_MATURE],
4578 chain, nextsav) {
4579 /* we don't need to check. */
4580 if (sav->lft_s == NULL)
4581 continue;
4582
4583 /* sanity check */
4584 if (sav->lft_c == NULL) {
4585 IPSECLOG(LOG_DEBUG,
4586 "There is no CURRENT time, why?\n");
4587 continue;
4588 }
4589
4590 /* check SOFT lifetime */
4591 if (sav->lft_s->sadb_lifetime_addtime != 0 &&
4592 now - sav->created > sav->lft_s->sadb_lifetime_addtime) {
4593 /*
4594 * check SA to be used whether or not.
4595 * when SA hasn't been used, delete it.
4596 */
4597 if (sav->lft_c->sadb_lifetime_usetime == 0) {
4598 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
4599 KEY_FREESAV(&sav);
4600 } else {
4601 key_sa_chgstate(sav, SADB_SASTATE_DYING);
4602 /*
4603 * XXX If we keep to send expire
4604 * message in the status of
4605 * DYING. Do remove below code.
4606 */
4607 key_expire(sav);
4608 }
4609 }
4610 /* check SOFT lifetime by bytes */
4611 /*
4612 * XXX I don't know the way to delete this SA
4613 * when new SA is installed. Caution when it's
4614 * installed too big lifetime by time.
4615 */
4616 else if (sav->lft_s->sadb_lifetime_bytes != 0 &&
4617 sav->lft_s->sadb_lifetime_bytes <
4618 sav->lft_c->sadb_lifetime_bytes) {
4619
4620 key_sa_chgstate(sav, SADB_SASTATE_DYING);
4621 /*
4622 * XXX If we keep to send expire
4623 * message in the status of
4624 * DYING. Do remove below code.
4625 */
4626 key_expire(sav);
4627 }
4628 }
4629
4630 /* check DYING entry to change status to DEAD. */
4631 LIST_FOREACH_SAFE(sav, &sah->savtree[SADB_SASTATE_DYING],
4632 chain, nextsav) {
4633 /* we don't need to check. */
4634 if (sav->lft_h == NULL)
4635 continue;
4636
4637 /* sanity check */
4638 if (sav->lft_c == NULL) {
4639 IPSECLOG(LOG_DEBUG,
4640 "There is no CURRENT time, why?\n");
4641 continue;
4642 }
4643
4644 if (sav->lft_h->sadb_lifetime_addtime != 0 &&
4645 now - sav->created > sav->lft_h->sadb_lifetime_addtime) {
4646 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
4647 KEY_FREESAV(&sav);
4648 }
4649 #if 0 /* XXX Should we keep to send expire message until HARD lifetime ? */
4650 else if (sav->lft_s != NULL
4651 && sav->lft_s->sadb_lifetime_addtime != 0
4652 && now - sav->created > sav->lft_s->sadb_lifetime_addtime) {
4653 /*
4654 * XXX: should be checked to be
4655 * installed the valid SA.
4656 */
4657
4658 /*
4659 * If there is no SA then sending
4660 * expire message.
4661 */
4662 key_expire(sav);
4663 }
4664 #endif
4665 /* check HARD lifetime by bytes */
4666 else if (sav->lft_h->sadb_lifetime_bytes != 0 &&
4667 sav->lft_h->sadb_lifetime_bytes <
4668 sav->lft_c->sadb_lifetime_bytes) {
4669 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
4670 KEY_FREESAV(&sav);
4671 }
4672 }
4673
4674 /* delete entry in DEAD */
4675 LIST_FOREACH_SAFE(sav, &sah->savtree[SADB_SASTATE_DEAD],
4676 chain, nextsav) {
4677 /* sanity check */
4678 if (sav->state != SADB_SASTATE_DEAD) {
4679 IPSECLOG(LOG_DEBUG,
4680 "invalid sav->state (queue: %d SA: %d): "
4681 "kill it anyway\n",
4682 SADB_SASTATE_DEAD, sav->state);
4683 }
4684
4685 /*
4686 * do not call key_freesav() here.
4687 * sav should already be freed, and sav->refcnt
4688 * shows other references to sav
4689 * (such as from SPD).
4690 */
4691 }
4692 }
4693 }
4694
4695 #ifndef IPSEC_NONBLOCK_ACQUIRE
4696 /* ACQ tree */
4697 {
4698 struct secacq *acq, *nextacq;
4699
4700 restart:
4701 mutex_enter(&key_mtx);
4702 LIST_FOREACH_SAFE(acq, &acqtree, chain, nextacq) {
4703 if (now - acq->created > key_blockacq_lifetime) {
4704 LIST_REMOVE(acq, chain);
4705 mutex_exit(&key_mtx);
4706 kmem_free(acq, sizeof(*acq));
4707 goto restart;
4708 }
4709 }
4710 mutex_exit(&key_mtx);
4711 }
4712 #endif
4713
4714 #ifdef notyet
4715 /* SP ACQ tree */
4716 {
4717 struct secspacq *acq, *nextacq;
4718
4719 LIST_FOREACH_SAFE(acq, &spacqtree, chain, nextacq) {
4720 if (now - acq->created > key_blockacq_lifetime) {
4721 KASSERT(__LIST_CHAINED(acq));
4722 LIST_REMOVE(acq, chain);
4723 kmem_free(acq, sizeof(*acq));
4724 }
4725 }
4726 }
4727 #endif
4728
4729 /* do exchange to tick time !! */
4730 callout_reset(&key_timehandler_ch, hz, key_timehandler, NULL);
4731
4732 mutex_exit(softnet_lock);
4733 splx(s);
4734 return;
4735 }
4736
4737 static void
4738 key_timehandler(void *arg)
4739 {
4740
4741 workqueue_enqueue(key_timehandler_wq, &key_timehandler_wk, NULL);
4742 }
4743
4744 u_long
4745 key_random(void)
4746 {
4747 u_long value;
4748
4749 key_randomfill(&value, sizeof(value));
4750 return value;
4751 }
4752
4753 void
4754 key_randomfill(void *p, size_t l)
4755 {
4756
4757 cprng_fast(p, l);
4758 }
4759
4760 /*
4761 * map SADB_SATYPE_* to IPPROTO_*.
4762 * if satype == SADB_SATYPE then satype is mapped to ~0.
4763 * OUT:
4764 * 0: invalid satype.
4765 */
4766 static u_int16_t
4767 key_satype2proto(u_int8_t satype)
4768 {
4769 switch (satype) {
4770 case SADB_SATYPE_UNSPEC:
4771 return IPSEC_PROTO_ANY;
4772 case SADB_SATYPE_AH:
4773 return IPPROTO_AH;
4774 case SADB_SATYPE_ESP:
4775 return IPPROTO_ESP;
4776 case SADB_X_SATYPE_IPCOMP:
4777 return IPPROTO_IPCOMP;
4778 case SADB_X_SATYPE_TCPSIGNATURE:
4779 return IPPROTO_TCP;
4780 default:
4781 return 0;
4782 }
4783 /* NOTREACHED */
4784 }
4785
4786 /*
4787 * map IPPROTO_* to SADB_SATYPE_*
4788 * OUT:
4789 * 0: invalid protocol type.
4790 */
4791 static u_int8_t
4792 key_proto2satype(u_int16_t proto)
4793 {
4794 switch (proto) {
4795 case IPPROTO_AH:
4796 return SADB_SATYPE_AH;
4797 case IPPROTO_ESP:
4798 return SADB_SATYPE_ESP;
4799 case IPPROTO_IPCOMP:
4800 return SADB_X_SATYPE_IPCOMP;
4801 case IPPROTO_TCP:
4802 return SADB_X_SATYPE_TCPSIGNATURE;
4803 default:
4804 return 0;
4805 }
4806 /* NOTREACHED */
4807 }
4808
4809 static int
4810 key_setsecasidx(int proto, int mode, int reqid,
4811 const struct sadb_address * src,
4812 const struct sadb_address * dst,
4813 struct secasindex * saidx)
4814 {
4815 const union sockaddr_union *src_u = (const union sockaddr_union *)src;
4816 const union sockaddr_union *dst_u = (const union sockaddr_union *)dst;
4817
4818 /* sa len safety check */
4819 if (key_checksalen(src_u) != 0)
4820 return -1;
4821 if (key_checksalen(dst_u) != 0)
4822 return -1;
4823
4824 memset(saidx, 0, sizeof(*saidx));
4825 saidx->proto = proto;
4826 saidx->mode = mode;
4827 saidx->reqid = reqid;
4828 memcpy(&saidx->src, src_u, src_u->sa.sa_len);
4829 memcpy(&saidx->dst, dst_u, dst_u->sa.sa_len);
4830
4831 key_porttosaddr(&((saidx)->src), 0);
4832 key_porttosaddr(&((saidx)->dst), 0);
4833 return 0;
4834 }
4835
4836 /* %%% PF_KEY */
4837 /*
4838 * SADB_GETSPI processing is to receive
4839 * <base, (SA2), src address, dst address, (SPI range)>
4840 * from the IKMPd, to assign a unique spi value, to hang on the INBOUND
4841 * tree with the status of LARVAL, and send
4842 * <base, SA(*), address(SD)>
4843 * to the IKMPd.
4844 *
4845 * IN: mhp: pointer to the pointer to each header.
4846 * OUT: NULL if fail.
4847 * other if success, return pointer to the message to send.
4848 */
4849 static int
4850 key_getspi(struct socket *so, struct mbuf *m,
4851 const struct sadb_msghdr *mhp)
4852 {
4853 struct sadb_address *src0, *dst0;
4854 struct secasindex saidx;
4855 struct secashead *newsah;
4856 struct secasvar *newsav;
4857 u_int8_t proto;
4858 u_int32_t spi;
4859 u_int8_t mode;
4860 u_int16_t reqid;
4861 int error;
4862
4863 KASSERT(!cpu_softintr_p());
4864 KASSERT(so != NULL);
4865 KASSERT(m != NULL);
4866 KASSERT(mhp != NULL);
4867 KASSERT(mhp->msg != NULL);
4868
4869 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
4870 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
4871 IPSECLOG(LOG_DEBUG, "invalid message is passed.\n");
4872 return key_senderror(so, m, EINVAL);
4873 }
4874 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
4875 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
4876 IPSECLOG(LOG_DEBUG, "invalid message is passed.\n");
4877 return key_senderror(so, m, EINVAL);
4878 }
4879 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
4880 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
4881 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
4882 } else {
4883 mode = IPSEC_MODE_ANY;
4884 reqid = 0;
4885 }
4886
4887 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
4888 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
4889
4890 /* map satype to proto */
4891 proto = key_satype2proto(mhp->msg->sadb_msg_satype);
4892 if (proto == 0) {
4893 IPSECLOG(LOG_DEBUG, "invalid satype is passed.\n");
4894 return key_senderror(so, m, EINVAL);
4895 }
4896
4897
4898 error = key_setsecasidx(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
4899 if (error != 0)
4900 return key_senderror(so, m, EINVAL);
4901
4902 error = key_set_natt_ports(&saidx.src, &saidx.dst, mhp);
4903 if (error != 0)
4904 return key_senderror(so, m, EINVAL);
4905
4906 /* SPI allocation */
4907 spi = key_do_getnewspi((struct sadb_spirange *)mhp->ext[SADB_EXT_SPIRANGE],
4908 &saidx);
4909 if (spi == 0)
4910 return key_senderror(so, m, EINVAL);
4911
4912 /* get a SA index */
4913 newsah = key_getsah(&saidx);
4914 if (newsah == NULL) {
4915 /* create a new SA index */
4916 newsah = key_newsah(&saidx);
4917 if (newsah == NULL) {
4918 IPSECLOG(LOG_DEBUG, "No more memory.\n");
4919 return key_senderror(so, m, ENOBUFS);
4920 }
4921 }
4922
4923 /* get a new SA */
4924 /* XXX rewrite */
4925 newsav = KEY_NEWSAV(m, mhp, newsah, &error);
4926 if (newsav == NULL) {
4927 /* XXX don't free new SA index allocated in above. */
4928 return key_senderror(so, m, error);
4929 }
4930
4931 /* set spi */
4932 newsav->spi = htonl(spi);
4933
4934 #ifndef IPSEC_NONBLOCK_ACQUIRE
4935 /* delete the entry in acqtree */
4936 if (mhp->msg->sadb_msg_seq != 0) {
4937 struct secacq *acq;
4938 mutex_enter(&key_mtx);
4939 acq = key_getacqbyseq(mhp->msg->sadb_msg_seq);
4940 if (acq != NULL) {
4941 /* reset counter in order to deletion by timehandler. */
4942 acq->created = time_uptime;
4943 acq->count = 0;
4944 }
4945 mutex_exit(&key_mtx);
4946 }
4947 #endif
4948
4949 {
4950 struct mbuf *n, *nn;
4951 struct sadb_sa *m_sa;
4952 struct sadb_msg *newmsg;
4953 int off, len;
4954
4955 /* create new sadb_msg to reply. */
4956 len = PFKEY_ALIGN8(sizeof(struct sadb_msg)) +
4957 PFKEY_ALIGN8(sizeof(struct sadb_sa));
4958 if (len > MCLBYTES)
4959 return key_senderror(so, m, ENOBUFS);
4960
4961 MGETHDR(n, M_DONTWAIT, MT_DATA);
4962 if (len > MHLEN) {
4963 MCLGET(n, M_DONTWAIT);
4964 if ((n->m_flags & M_EXT) == 0) {
4965 m_freem(n);
4966 n = NULL;
4967 }
4968 }
4969 if (!n)
4970 return key_senderror(so, m, ENOBUFS);
4971
4972 n->m_len = len;
4973 n->m_next = NULL;
4974 off = 0;
4975
4976 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, char *) + off);
4977 off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
4978
4979 m_sa = (struct sadb_sa *)(mtod(n, char *) + off);
4980 m_sa->sadb_sa_len = PFKEY_UNIT64(sizeof(struct sadb_sa));
4981 m_sa->sadb_sa_exttype = SADB_EXT_SA;
4982 m_sa->sadb_sa_spi = htonl(spi);
4983 off += PFKEY_ALIGN8(sizeof(struct sadb_sa));
4984
4985 KASSERTMSG(off == len, "length inconsistency");
4986
4987 n->m_next = key_gather_mbuf(m, mhp, 0, 2, SADB_EXT_ADDRESS_SRC,
4988 SADB_EXT_ADDRESS_DST);
4989 if (!n->m_next) {
4990 m_freem(n);
4991 return key_senderror(so, m, ENOBUFS);
4992 }
4993
4994 if (n->m_len < sizeof(struct sadb_msg)) {
4995 n = m_pullup(n, sizeof(struct sadb_msg));
4996 if (n == NULL)
4997 return key_sendup_mbuf(so, m, KEY_SENDUP_ONE);
4998 }
4999
5000 n->m_pkthdr.len = 0;
5001 for (nn = n; nn; nn = nn->m_next)
5002 n->m_pkthdr.len += nn->m_len;
5003
5004 newmsg = mtod(n, struct sadb_msg *);
5005 newmsg->sadb_msg_seq = newsav->seq;
5006 newmsg->sadb_msg_errno = 0;
5007 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
5008
5009 m_freem(m);
5010 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
5011 }
5012 }
5013
5014 /*
5015 * allocating new SPI
5016 * called by key_getspi().
5017 * OUT:
5018 * 0: failure.
5019 * others: success.
5020 */
5021 static u_int32_t
5022 key_do_getnewspi(const struct sadb_spirange *spirange,
5023 const struct secasindex *saidx)
5024 {
5025 u_int32_t newspi;
5026 u_int32_t spmin, spmax;
5027 int count = key_spi_trycnt;
5028
5029 /* set spi range to allocate */
5030 if (spirange != NULL) {
5031 spmin = spirange->sadb_spirange_min;
5032 spmax = spirange->sadb_spirange_max;
5033 } else {
5034 spmin = key_spi_minval;
5035 spmax = key_spi_maxval;
5036 }
5037 /* IPCOMP needs 2-byte SPI */
5038 if (saidx->proto == IPPROTO_IPCOMP) {
5039 u_int32_t t;
5040 if (spmin >= 0x10000)
5041 spmin = 0xffff;
5042 if (spmax >= 0x10000)
5043 spmax = 0xffff;
5044 if (spmin > spmax) {
5045 t = spmin; spmin = spmax; spmax = t;
5046 }
5047 }
5048
5049 if (spmin == spmax) {
5050 if (key_checkspidup(saidx, htonl(spmin)) != NULL) {
5051 IPSECLOG(LOG_DEBUG, "SPI %u exists already.\n", spmin);
5052 return 0;
5053 }
5054
5055 count--; /* taking one cost. */
5056 newspi = spmin;
5057
5058 } else {
5059
5060 /* init SPI */
5061 newspi = 0;
5062
5063 /* when requesting to allocate spi ranged */
5064 while (count--) {
5065 /* generate pseudo-random SPI value ranged. */
5066 newspi = spmin + (key_random() % (spmax - spmin + 1));
5067
5068 if (key_checkspidup(saidx, htonl(newspi)) == NULL)
5069 break;
5070 }
5071
5072 if (count == 0 || newspi == 0) {
5073 IPSECLOG(LOG_DEBUG, "to allocate spi is failed.\n");
5074 return 0;
5075 }
5076 }
5077
5078 /* statistics */
5079 keystat.getspi_count =
5080 (keystat.getspi_count + key_spi_trycnt - count) / 2;
5081
5082 return newspi;
5083 }
5084
5085 static int
5086 key_handle_natt_info(struct secasvar *sav,
5087 const struct sadb_msghdr *mhp)
5088 {
5089 const char *msg = "?" ;
5090 struct sadb_x_nat_t_type *type;
5091 struct sadb_x_nat_t_port *sport, *dport;
5092 struct sadb_address *iaddr, *raddr;
5093 struct sadb_x_nat_t_frag *frag;
5094
5095 if (mhp->ext[SADB_X_EXT_NAT_T_TYPE] == NULL ||
5096 mhp->ext[SADB_X_EXT_NAT_T_SPORT] == NULL ||
5097 mhp->ext[SADB_X_EXT_NAT_T_DPORT] == NULL)
5098 return 0;
5099
5100 if (mhp->extlen[SADB_X_EXT_NAT_T_TYPE] < sizeof(*type)) {
5101 msg = "TYPE";
5102 goto bad;
5103 }
5104
5105 if (mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport)) {
5106 msg = "SPORT";
5107 goto bad;
5108 }
5109
5110 if (mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport)) {
5111 msg = "DPORT";
5112 goto bad;
5113 }
5114
5115 if (mhp->ext[SADB_X_EXT_NAT_T_OAI] != NULL) {
5116 IPSECLOG(LOG_DEBUG, "NAT-T OAi present\n");
5117 if (mhp->extlen[SADB_X_EXT_NAT_T_OAI] < sizeof(*iaddr)) {
5118 msg = "OAI";
5119 goto bad;
5120 }
5121 }
5122
5123 if (mhp->ext[SADB_X_EXT_NAT_T_OAR] != NULL) {
5124 IPSECLOG(LOG_DEBUG, "NAT-T OAr present\n");
5125 if (mhp->extlen[SADB_X_EXT_NAT_T_OAR] < sizeof(*raddr)) {
5126 msg = "OAR";
5127 goto bad;
5128 }
5129 }
5130
5131 if (mhp->ext[SADB_X_EXT_NAT_T_FRAG] != NULL) {
5132 if (mhp->extlen[SADB_X_EXT_NAT_T_FRAG] < sizeof(*frag)) {
5133 msg = "FRAG";
5134 goto bad;
5135 }
5136 }
5137
5138 type = (struct sadb_x_nat_t_type *)mhp->ext[SADB_X_EXT_NAT_T_TYPE];
5139 sport = (struct sadb_x_nat_t_port *)mhp->ext[SADB_X_EXT_NAT_T_SPORT];
5140 dport = (struct sadb_x_nat_t_port *)mhp->ext[SADB_X_EXT_NAT_T_DPORT];
5141 iaddr = (struct sadb_address *)mhp->ext[SADB_X_EXT_NAT_T_OAI];
5142 raddr = (struct sadb_address *)mhp->ext[SADB_X_EXT_NAT_T_OAR];
5143 frag = (struct sadb_x_nat_t_frag *)mhp->ext[SADB_X_EXT_NAT_T_FRAG];
5144
5145 IPSECLOG(LOG_DEBUG, "type %d, sport = %d, dport = %d\n",
5146 type->sadb_x_nat_t_type_type,
5147 ntohs(sport->sadb_x_nat_t_port_port),
5148 ntohs(dport->sadb_x_nat_t_port_port));
5149
5150 sav->natt_type = type->sadb_x_nat_t_type_type;
5151 key_porttosaddr(&sav->sah->saidx.src, sport->sadb_x_nat_t_port_port);
5152 key_porttosaddr(&sav->sah->saidx.dst, dport->sadb_x_nat_t_port_port);
5153 if (frag)
5154 sav->esp_frag = frag->sadb_x_nat_t_frag_fraglen;
5155 else
5156 sav->esp_frag = IP_MAXPACKET;
5157
5158 return 0;
5159 bad:
5160 IPSECLOG(LOG_DEBUG, "invalid message %s\n", msg);
5161 __USE(msg);
5162 return -1;
5163 }
5164
5165 /* Just update the IPSEC_NAT_T ports if present */
5166 static int
5167 key_set_natt_ports(union sockaddr_union *src, union sockaddr_union *dst,
5168 const struct sadb_msghdr *mhp)
5169 {
5170 if (mhp->ext[SADB_X_EXT_NAT_T_OAI] != NULL)
5171 IPSECLOG(LOG_DEBUG, "NAT-T OAi present\n");
5172 if (mhp->ext[SADB_X_EXT_NAT_T_OAR] != NULL)
5173 IPSECLOG(LOG_DEBUG, "NAT-T OAr present\n");
5174
5175 if ((mhp->ext[SADB_X_EXT_NAT_T_TYPE] != NULL) &&
5176 (mhp->ext[SADB_X_EXT_NAT_T_SPORT] != NULL) &&
5177 (mhp->ext[SADB_X_EXT_NAT_T_DPORT] != NULL)) {
5178 struct sadb_x_nat_t_type *type;
5179 struct sadb_x_nat_t_port *sport;
5180 struct sadb_x_nat_t_port *dport;
5181
5182 if ((mhp->extlen[SADB_X_EXT_NAT_T_TYPE] < sizeof(*type)) ||
5183 (mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport)) ||
5184 (mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport))) {
5185 IPSECLOG(LOG_DEBUG, "invalid message\n");
5186 return -1;
5187 }
5188
5189 type = (struct sadb_x_nat_t_type *)
5190 mhp->ext[SADB_X_EXT_NAT_T_TYPE];
5191 sport = (struct sadb_x_nat_t_port *)
5192 mhp->ext[SADB_X_EXT_NAT_T_SPORT];
5193 dport = (struct sadb_x_nat_t_port *)
5194 mhp->ext[SADB_X_EXT_NAT_T_DPORT];
5195
5196 key_porttosaddr(src, sport->sadb_x_nat_t_port_port);
5197 key_porttosaddr(dst, dport->sadb_x_nat_t_port_port);
5198
5199 IPSECLOG(LOG_DEBUG, "type %d, sport = %d, dport = %d\n",
5200 type->sadb_x_nat_t_type_type,
5201 ntohs(sport->sadb_x_nat_t_port_port),
5202 ntohs(dport->sadb_x_nat_t_port_port));
5203 }
5204
5205 return 0;
5206 }
5207
5208
5209 /*
5210 * SADB_UPDATE processing
5211 * receive
5212 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
5213 * key(AE), (identity(SD),) (sensitivity)>
5214 * from the ikmpd, and update a secasvar entry whose status is SADB_SASTATE_LARVAL.
5215 * and send
5216 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
5217 * (identity(SD),) (sensitivity)>
5218 * to the ikmpd.
5219 *
5220 * m will always be freed.
5221 */
5222 static int
5223 key_update(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
5224 {
5225 struct sadb_sa *sa0;
5226 struct sadb_address *src0, *dst0;
5227 struct secasindex saidx;
5228 struct secashead *sah;
5229 struct secasvar *sav;
5230 u_int16_t proto;
5231 u_int8_t mode;
5232 u_int16_t reqid;
5233 int error;
5234
5235 KASSERT(!cpu_softintr_p());
5236 KASSERT(so != NULL);
5237 KASSERT(m != NULL);
5238 KASSERT(mhp != NULL);
5239 KASSERT(mhp->msg != NULL);
5240
5241 /* map satype to proto */
5242 proto = key_satype2proto(mhp->msg->sadb_msg_satype);
5243 if (proto == 0) {
5244 IPSECLOG(LOG_DEBUG, "invalid satype is passed.\n");
5245 return key_senderror(so, m, EINVAL);
5246 }
5247
5248 if (mhp->ext[SADB_EXT_SA] == NULL ||
5249 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5250 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
5251 (mhp->msg->sadb_msg_satype == SADB_SATYPE_ESP &&
5252 mhp->ext[SADB_EXT_KEY_ENCRYPT] == NULL) ||
5253 (mhp->msg->sadb_msg_satype == SADB_SATYPE_AH &&
5254 mhp->ext[SADB_EXT_KEY_AUTH] == NULL) ||
5255 (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL &&
5256 mhp->ext[SADB_EXT_LIFETIME_SOFT] == NULL) ||
5257 (mhp->ext[SADB_EXT_LIFETIME_HARD] == NULL &&
5258 mhp->ext[SADB_EXT_LIFETIME_SOFT] != NULL)) {
5259 IPSECLOG(LOG_DEBUG, "invalid message is passed.\n");
5260 return key_senderror(so, m, EINVAL);
5261 }
5262 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
5263 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5264 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
5265 IPSECLOG(LOG_DEBUG, "invalid message is passed.\n");
5266 return key_senderror(so, m, EINVAL);
5267 }
5268 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
5269 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
5270 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
5271 } else {
5272 mode = IPSEC_MODE_ANY;
5273 reqid = 0;
5274 }
5275 /* XXX boundary checking for other extensions */
5276
5277 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5278 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
5279 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
5280
5281 error = key_setsecasidx(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
5282 if (error != 0)
5283 return key_senderror(so, m, EINVAL);
5284
5285 error = key_set_natt_ports(&saidx.src, &saidx.dst, mhp);
5286 if (error != 0)
5287 return key_senderror(so, m, EINVAL);
5288
5289 /* get a SA header */
5290 sah = key_getsah(&saidx);
5291 if (sah == NULL) {
5292 IPSECLOG(LOG_DEBUG, "no SA index found.\n");
5293 return key_senderror(so, m, ENOENT);
5294 }
5295
5296 /* set spidx if there */
5297 /* XXX rewrite */
5298 error = key_setident(sah, m, mhp);
5299 if (error)
5300 return key_senderror(so, m, error);
5301
5302 /* find a SA with sequence number. */
5303 #ifdef IPSEC_DOSEQCHECK
5304 if (mhp->msg->sadb_msg_seq != 0) {
5305 sav = key_getsavbyseq(sah, mhp->msg->sadb_msg_seq);
5306 if (sav == NULL) {
5307 IPSECLOG(LOG_DEBUG,
5308 "no larval SA with sequence %u exists.\n",
5309 mhp->msg->sadb_msg_seq);
5310 return key_senderror(so, m, ENOENT);
5311 }
5312 }
5313 #else
5314 sav = key_getsavbyspi(sah, sa0->sadb_sa_spi);
5315 if (sav == NULL) {
5316 IPSECLOG(LOG_DEBUG, "no such a SA found (spi:%u)\n",
5317 (u_int32_t)ntohl(sa0->sadb_sa_spi));
5318 return key_senderror(so, m, EINVAL);
5319 }
5320 #endif
5321
5322 /* validity check */
5323 if (sav->sah->saidx.proto != proto) {
5324 IPSECLOG(LOG_DEBUG, "protocol mismatched (DB=%u param=%u)\n",
5325 sav->sah->saidx.proto, proto);
5326 return key_senderror(so, m, EINVAL);
5327 }
5328 #ifdef IPSEC_DOSEQCHECK
5329 if (sav->spi != sa0->sadb_sa_spi) {
5330 IPSECLOG(LOG_DEBUG, "SPI mismatched (DB:%u param:%u)\n",
5331 (u_int32_t)ntohl(sav->spi),
5332 (u_int32_t)ntohl(sa0->sadb_sa_spi));
5333 return key_senderror(so, m, EINVAL);
5334 }
5335 #endif
5336 if (sav->pid != mhp->msg->sadb_msg_pid) {
5337 IPSECLOG(LOG_DEBUG, "pid mismatched (DB:%u param:%u)\n",
5338 sav->pid, mhp->msg->sadb_msg_pid);
5339 return key_senderror(so, m, EINVAL);
5340 }
5341
5342 /* copy sav values */
5343 error = key_setsaval(sav, m, mhp);
5344 if (error) {
5345 KEY_FREESAV(&sav);
5346 return key_senderror(so, m, error);
5347 }
5348
5349 error = key_handle_natt_info(sav,mhp);
5350 if (error != 0)
5351 return key_senderror(so, m, EINVAL);
5352
5353 /* check SA values to be mature. */
5354 mhp->msg->sadb_msg_errno = key_mature(sav);
5355 if (mhp->msg->sadb_msg_errno != 0) {
5356 KEY_FREESAV(&sav);
5357 return key_senderror(so, m, 0);
5358 }
5359
5360 {
5361 struct mbuf *n;
5362
5363 /* set msg buf from mhp */
5364 n = key_getmsgbuf_x1(m, mhp);
5365 if (n == NULL) {
5366 IPSECLOG(LOG_DEBUG, "No more memory.\n");
5367 return key_senderror(so, m, ENOBUFS);
5368 }
5369
5370 m_freem(m);
5371 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5372 }
5373 }
5374
5375 /*
5376 * search SAD with sequence for a SA which state is SADB_SASTATE_LARVAL.
5377 * only called by key_update().
5378 * OUT:
5379 * NULL : not found
5380 * others : found, pointer to a SA.
5381 */
5382 #ifdef IPSEC_DOSEQCHECK
5383 static struct secasvar *
5384 key_getsavbyseq(struct secashead *sah, u_int32_t seq)
5385 {
5386 struct secasvar *sav;
5387 u_int state;
5388
5389 state = SADB_SASTATE_LARVAL;
5390
5391 /* search SAD with sequence number ? */
5392 LIST_FOREACH(sav, &sah->savtree[state], chain) {
5393
5394 KEY_CHKSASTATE(state, sav->state);
5395
5396 if (sav->seq == seq) {
5397 SA_ADDREF(sav);
5398 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP,
5399 "DP cause refcnt++:%d SA:%p\n",
5400 sav->refcnt, sav);
5401 return sav;
5402 }
5403 }
5404
5405 return NULL;
5406 }
5407 #endif
5408
5409 /*
5410 * SADB_ADD processing
5411 * add an entry to SA database, when received
5412 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
5413 * key(AE), (identity(SD),) (sensitivity)>
5414 * from the ikmpd,
5415 * and send
5416 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
5417 * (identity(SD),) (sensitivity)>
5418 * to the ikmpd.
5419 *
5420 * IGNORE identity and sensitivity messages.
5421 *
5422 * m will always be freed.
5423 */
5424 static int
5425 key_add(struct socket *so, struct mbuf *m,
5426 const struct sadb_msghdr *mhp)
5427 {
5428 struct sadb_sa *sa0;
5429 struct sadb_address *src0, *dst0;
5430 struct secasindex saidx;
5431 struct secashead *newsah;
5432 struct secasvar *newsav;
5433 u_int16_t proto;
5434 u_int8_t mode;
5435 u_int16_t reqid;
5436 int error;
5437
5438 KASSERT(so != NULL);
5439 KASSERT(m != NULL);
5440 KASSERT(mhp != NULL);
5441 KASSERT(mhp->msg != NULL);
5442
5443 /* map satype to proto */
5444 proto = key_satype2proto(mhp->msg->sadb_msg_satype);
5445 if (proto == 0) {
5446 IPSECLOG(LOG_DEBUG, "invalid satype is passed.\n");
5447 return key_senderror(so, m, EINVAL);
5448 }
5449
5450 if (mhp->ext[SADB_EXT_SA] == NULL ||
5451 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5452 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
5453 (mhp->msg->sadb_msg_satype == SADB_SATYPE_ESP &&
5454 mhp->ext[SADB_EXT_KEY_ENCRYPT] == NULL) ||
5455 (mhp->msg->sadb_msg_satype == SADB_SATYPE_AH &&
5456 mhp->ext[SADB_EXT_KEY_AUTH] == NULL) ||
5457 (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL &&
5458 mhp->ext[SADB_EXT_LIFETIME_SOFT] == NULL) ||
5459 (mhp->ext[SADB_EXT_LIFETIME_HARD] == NULL &&
5460 mhp->ext[SADB_EXT_LIFETIME_SOFT] != NULL)) {
5461 IPSECLOG(LOG_DEBUG, "invalid message is passed.\n");
5462 return key_senderror(so, m, EINVAL);
5463 }
5464 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
5465 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5466 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
5467 /* XXX need more */
5468 IPSECLOG(LOG_DEBUG, "invalid message is passed.\n");
5469 return key_senderror(so, m, EINVAL);
5470 }
5471 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
5472 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
5473 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
5474 } else {
5475 mode = IPSEC_MODE_ANY;
5476 reqid = 0;
5477 }
5478
5479 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5480 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
5481 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
5482
5483 error = key_setsecasidx(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
5484 if (error != 0)
5485 return key_senderror(so, m, EINVAL);
5486
5487 error = key_set_natt_ports(&saidx.src, &saidx.dst, mhp);
5488 if (error != 0)
5489 return key_senderror(so, m, EINVAL);
5490
5491 /* get a SA header */
5492 newsah = key_getsah(&saidx);
5493 if (newsah == NULL) {
5494 /* create a new SA header */
5495 newsah = key_newsah(&saidx);
5496 if (newsah == NULL) {
5497 IPSECLOG(LOG_DEBUG, "No more memory.\n");
5498 return key_senderror(so, m, ENOBUFS);
5499 }
5500 }
5501
5502 /* set spidx if there */
5503 /* XXX rewrite */
5504 error = key_setident(newsah, m, mhp);
5505 if (error) {
5506 return key_senderror(so, m, error);
5507 }
5508
5509 /* create new SA entry. */
5510 /* We can create new SA only if SPI is differenct. */
5511 if (key_getsavbyspi(newsah, sa0->sadb_sa_spi)) {
5512 IPSECLOG(LOG_DEBUG, "SA already exists.\n");
5513 return key_senderror(so, m, EEXIST);
5514 }
5515 newsav = KEY_NEWSAV(m, mhp, newsah, &error);
5516 if (newsav == NULL) {
5517 return key_senderror(so, m, error);
5518 }
5519
5520 error = key_handle_natt_info(newsav, mhp);
5521 if (error != 0)
5522 return key_senderror(so, m, EINVAL);
5523
5524 /* check SA values to be mature. */
5525 error = key_mature(newsav);
5526 if (error != 0) {
5527 KEY_FREESAV(&newsav);
5528 return key_senderror(so, m, error);
5529 }
5530
5531 /*
5532 * don't call key_freesav() here, as we would like to keep the SA
5533 * in the database on success.
5534 */
5535
5536 {
5537 struct mbuf *n;
5538
5539 /* set msg buf from mhp */
5540 n = key_getmsgbuf_x1(m, mhp);
5541 if (n == NULL) {
5542 IPSECLOG(LOG_DEBUG, "No more memory.\n");
5543 return key_senderror(so, m, ENOBUFS);
5544 }
5545
5546 m_freem(m);
5547 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5548 }
5549 }
5550
5551 /* m is retained */
5552 static int
5553 key_setident(struct secashead *sah, struct mbuf *m,
5554 const struct sadb_msghdr *mhp)
5555 {
5556 const struct sadb_ident *idsrc, *iddst;
5557 int idsrclen, iddstlen;
5558
5559 KASSERT(!cpu_softintr_p());
5560 KASSERT(sah != NULL);
5561 KASSERT(m != NULL);
5562 KASSERT(mhp != NULL);
5563 KASSERT(mhp->msg != NULL);
5564
5565 /*
5566 * Can be called with an existing sah from key_update().
5567 */
5568 if (sah->idents != NULL) {
5569 kmem_free(sah->idents, sah->idents_len);
5570 sah->idents = NULL;
5571 sah->idents_len = 0;
5572 }
5573 if (sah->identd != NULL) {
5574 kmem_free(sah->identd, sah->identd_len);
5575 sah->identd = NULL;
5576 sah->identd_len = 0;
5577 }
5578
5579 /* don't make buffer if not there */
5580 if (mhp->ext[SADB_EXT_IDENTITY_SRC] == NULL &&
5581 mhp->ext[SADB_EXT_IDENTITY_DST] == NULL) {
5582 sah->idents = NULL;
5583 sah->identd = NULL;
5584 return 0;
5585 }
5586
5587 if (mhp->ext[SADB_EXT_IDENTITY_SRC] == NULL ||
5588 mhp->ext[SADB_EXT_IDENTITY_DST] == NULL) {
5589 IPSECLOG(LOG_DEBUG, "invalid identity.\n");
5590 return EINVAL;
5591 }
5592
5593 idsrc = (const struct sadb_ident *)mhp->ext[SADB_EXT_IDENTITY_SRC];
5594 iddst = (const struct sadb_ident *)mhp->ext[SADB_EXT_IDENTITY_DST];
5595 idsrclen = mhp->extlen[SADB_EXT_IDENTITY_SRC];
5596 iddstlen = mhp->extlen[SADB_EXT_IDENTITY_DST];
5597
5598 /* validity check */
5599 if (idsrc->sadb_ident_type != iddst->sadb_ident_type) {
5600 IPSECLOG(LOG_DEBUG, "ident type mismatch.\n");
5601 return EINVAL;
5602 }
5603
5604 switch (idsrc->sadb_ident_type) {
5605 case SADB_IDENTTYPE_PREFIX:
5606 case SADB_IDENTTYPE_FQDN:
5607 case SADB_IDENTTYPE_USERFQDN:
5608 default:
5609 /* XXX do nothing */
5610 sah->idents = NULL;
5611 sah->identd = NULL;
5612 return 0;
5613 }
5614
5615 /* make structure */
5616 sah->idents = kmem_alloc(idsrclen, KM_SLEEP);
5617 sah->idents_len = idsrclen;
5618 sah->identd = kmem_alloc(iddstlen, KM_SLEEP);
5619 sah->identd_len = iddstlen;
5620 memcpy(sah->idents, idsrc, idsrclen);
5621 memcpy(sah->identd, iddst, iddstlen);
5622
5623 return 0;
5624 }
5625
5626 /*
5627 * m will not be freed on return.
5628 * it is caller's responsibility to free the result.
5629 */
5630 static struct mbuf *
5631 key_getmsgbuf_x1(struct mbuf *m, const struct sadb_msghdr *mhp)
5632 {
5633 struct mbuf *n;
5634
5635 KASSERT(m != NULL);
5636 KASSERT(mhp != NULL);
5637 KASSERT(mhp->msg != NULL);
5638
5639 /* create new sadb_msg to reply. */
5640 n = key_gather_mbuf(m, mhp, 1, 15, SADB_EXT_RESERVED,
5641 SADB_EXT_SA, SADB_X_EXT_SA2,
5642 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST,
5643 SADB_EXT_LIFETIME_HARD, SADB_EXT_LIFETIME_SOFT,
5644 SADB_EXT_IDENTITY_SRC, SADB_EXT_IDENTITY_DST,
5645 SADB_X_EXT_NAT_T_TYPE, SADB_X_EXT_NAT_T_SPORT,
5646 SADB_X_EXT_NAT_T_DPORT, SADB_X_EXT_NAT_T_OAI,
5647 SADB_X_EXT_NAT_T_OAR, SADB_X_EXT_NAT_T_FRAG);
5648 if (!n)
5649 return NULL;
5650
5651 if (n->m_len < sizeof(struct sadb_msg)) {
5652 n = m_pullup(n, sizeof(struct sadb_msg));
5653 if (n == NULL)
5654 return NULL;
5655 }
5656 mtod(n, struct sadb_msg *)->sadb_msg_errno = 0;
5657 mtod(n, struct sadb_msg *)->sadb_msg_len =
5658 PFKEY_UNIT64(n->m_pkthdr.len);
5659
5660 return n;
5661 }
5662
5663 static int key_delete_all (struct socket *, struct mbuf *,
5664 const struct sadb_msghdr *, u_int16_t);
5665
5666 /*
5667 * SADB_DELETE processing
5668 * receive
5669 * <base, SA(*), address(SD)>
5670 * from the ikmpd, and set SADB_SASTATE_DEAD,
5671 * and send,
5672 * <base, SA(*), address(SD)>
5673 * to the ikmpd.
5674 *
5675 * m will always be freed.
5676 */
5677 static int
5678 key_delete(struct socket *so, struct mbuf *m,
5679 const struct sadb_msghdr *mhp)
5680 {
5681 struct sadb_sa *sa0;
5682 struct sadb_address *src0, *dst0;
5683 struct secasindex saidx;
5684 struct secashead *sah;
5685 struct secasvar *sav = NULL;
5686 u_int16_t proto;
5687 int error;
5688
5689 KASSERT(so != NULL);
5690 KASSERT(m != NULL);
5691 KASSERT(mhp != NULL);
5692 KASSERT(mhp->msg != NULL);
5693
5694 /* map satype to proto */
5695 proto = key_satype2proto(mhp->msg->sadb_msg_satype);
5696 if (proto == 0) {
5697 IPSECLOG(LOG_DEBUG, "invalid satype is passed.\n");
5698 return key_senderror(so, m, EINVAL);
5699 }
5700
5701 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5702 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
5703 IPSECLOG(LOG_DEBUG, "invalid message is passed.\n");
5704 return key_senderror(so, m, EINVAL);
5705 }
5706
5707 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5708 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
5709 IPSECLOG(LOG_DEBUG, "invalid message is passed.\n");
5710 return key_senderror(so, m, EINVAL);
5711 }
5712
5713 if (mhp->ext[SADB_EXT_SA] == NULL) {
5714 /*
5715 * Caller wants us to delete all non-LARVAL SAs
5716 * that match the src/dst. This is used during
5717 * IKE INITIAL-CONTACT.
5718 */
5719 IPSECLOG(LOG_DEBUG, "doing delete all.\n");
5720 return key_delete_all(so, m, mhp, proto);
5721 } else if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa)) {
5722 IPSECLOG(LOG_DEBUG, "invalid message is passed.\n");
5723 return key_senderror(so, m, EINVAL);
5724 }
5725
5726 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5727 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
5728 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
5729
5730 error = key_setsecasidx(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1,
5731 &saidx);
5732 if (error != 0)
5733 return key_senderror(so, m, EINVAL);
5734
5735 error = key_set_natt_ports(&saidx.src, &saidx.dst, mhp);
5736 if (error != 0)
5737 return key_senderror(so, m, EINVAL);
5738
5739 /* get a SA header */
5740 LIST_FOREACH(sah, &sahtree, chain) {
5741 if (sah->state == SADB_SASTATE_DEAD)
5742 continue;
5743 if (!key_saidx_match(&sah->saidx, &saidx, CMP_HEAD))
5744 continue;
5745
5746 /* get a SA with SPI. */
5747 sav = key_getsavbyspi(sah, sa0->sadb_sa_spi);
5748 if (sav)
5749 break;
5750 }
5751 if (sah == NULL) {
5752 IPSECLOG(LOG_DEBUG, "no SA found.\n");
5753 return key_senderror(so, m, ENOENT);
5754 }
5755
5756 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
5757 KEY_FREESAV(&sav);
5758
5759 {
5760 struct mbuf *n;
5761 struct sadb_msg *newmsg;
5762
5763 /* create new sadb_msg to reply. */
5764 n = key_gather_mbuf(m, mhp, 1, 4, SADB_EXT_RESERVED,
5765 SADB_EXT_SA, SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
5766 if (!n)
5767 return key_senderror(so, m, ENOBUFS);
5768
5769 if (n->m_len < sizeof(struct sadb_msg)) {
5770 n = m_pullup(n, sizeof(struct sadb_msg));
5771 if (n == NULL)
5772 return key_senderror(so, m, ENOBUFS);
5773 }
5774 newmsg = mtod(n, struct sadb_msg *);
5775 newmsg->sadb_msg_errno = 0;
5776 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
5777
5778 m_freem(m);
5779 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5780 }
5781 }
5782
5783 /*
5784 * delete all SAs for src/dst. Called from key_delete().
5785 */
5786 static int
5787 key_delete_all(struct socket *so, struct mbuf *m,
5788 const struct sadb_msghdr *mhp, u_int16_t proto)
5789 {
5790 struct sadb_address *src0, *dst0;
5791 struct secasindex saidx;
5792 struct secashead *sah;
5793 struct secasvar *sav, *nextsav;
5794 u_int state;
5795 int error;
5796
5797 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
5798 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
5799
5800 error = key_setsecasidx(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1,
5801 &saidx);
5802 if (error != 0)
5803 return key_senderror(so, m, EINVAL);
5804
5805 error = key_set_natt_ports(&saidx.src, &saidx.dst, mhp);
5806 if (error != 0)
5807 return key_senderror(so, m, EINVAL);
5808
5809 LIST_FOREACH(sah, &sahtree, chain) {
5810 if (sah->state == SADB_SASTATE_DEAD)
5811 continue;
5812 if (!key_saidx_match(&sah->saidx, &saidx, CMP_HEAD))
5813 continue;
5814
5815 /* Delete all non-LARVAL SAs. */
5816 SASTATE_ALIVE_FOREACH(state) {
5817 if (state == SADB_SASTATE_LARVAL)
5818 continue;
5819 LIST_FOREACH_SAFE(sav, &sah->savtree[state], chain,
5820 nextsav) {
5821 /* sanity check */
5822 if (sav->state != state) {
5823 IPSECLOG(LOG_DEBUG,
5824 "invalid sav->state "
5825 "(queue: %d SA: %d)\n",
5826 state, sav->state);
5827 continue;
5828 }
5829
5830 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
5831 KEY_FREESAV(&sav);
5832 }
5833 }
5834 }
5835 {
5836 struct mbuf *n;
5837 struct sadb_msg *newmsg;
5838
5839 /* create new sadb_msg to reply. */
5840 n = key_gather_mbuf(m, mhp, 1, 3, SADB_EXT_RESERVED,
5841 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
5842 if (!n)
5843 return key_senderror(so, m, ENOBUFS);
5844
5845 if (n->m_len < sizeof(struct sadb_msg)) {
5846 n = m_pullup(n, sizeof(struct sadb_msg));
5847 if (n == NULL)
5848 return key_senderror(so, m, ENOBUFS);
5849 }
5850 newmsg = mtod(n, struct sadb_msg *);
5851 newmsg->sadb_msg_errno = 0;
5852 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
5853
5854 m_freem(m);
5855 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5856 }
5857 }
5858
5859 /*
5860 * SADB_GET processing
5861 * receive
5862 * <base, SA(*), address(SD)>
5863 * from the ikmpd, and get a SP and a SA to respond,
5864 * and send,
5865 * <base, SA, (lifetime(HSC),) address(SD), (address(P),) key(AE),
5866 * (identity(SD),) (sensitivity)>
5867 * to the ikmpd.
5868 *
5869 * m will always be freed.
5870 */
5871 static int
5872 key_get(struct socket *so, struct mbuf *m,
5873 const struct sadb_msghdr *mhp)
5874 {
5875 struct sadb_sa *sa0;
5876 struct sadb_address *src0, *dst0;
5877 struct secasindex saidx;
5878 struct secashead *sah;
5879 struct secasvar *sav = NULL;
5880 u_int16_t proto;
5881 int error;
5882
5883 KASSERT(so != NULL);
5884 KASSERT(m != NULL);
5885 KASSERT(mhp != NULL);
5886 KASSERT(mhp->msg != NULL);
5887
5888 /* map satype to proto */
5889 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5890 IPSECLOG(LOG_DEBUG, "invalid satype is passed.\n");
5891 return key_senderror(so, m, EINVAL);
5892 }
5893
5894 if (mhp->ext[SADB_EXT_SA] == NULL ||
5895 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5896 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
5897 IPSECLOG(LOG_DEBUG, "invalid message is passed.\n");
5898 return key_senderror(so, m, EINVAL);
5899 }
5900 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
5901 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5902 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
5903 IPSECLOG(LOG_DEBUG, "invalid message is passed.\n");
5904 return key_senderror(so, m, EINVAL);
5905 }
5906
5907 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5908 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
5909 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
5910
5911 error = key_setsecasidx(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1,
5912 &saidx);
5913 if (error != 0)
5914 return key_senderror(so, m, EINVAL);
5915
5916 error = key_set_natt_ports(&saidx.src, &saidx.dst, mhp);
5917 if (error != 0)
5918 return key_senderror(so, m, EINVAL);
5919
5920 /* get a SA header */
5921 LIST_FOREACH(sah, &sahtree, chain) {
5922 if (sah->state == SADB_SASTATE_DEAD)
5923 continue;
5924 if (!key_saidx_match(&sah->saidx, &saidx, CMP_HEAD))
5925 continue;
5926
5927 /* get a SA with SPI. */
5928 sav = key_getsavbyspi(sah, sa0->sadb_sa_spi);
5929 if (sav)
5930 break;
5931 }
5932 if (sah == NULL) {
5933 IPSECLOG(LOG_DEBUG, "no SA found.\n");
5934 return key_senderror(so, m, ENOENT);
5935 }
5936
5937 {
5938 struct mbuf *n;
5939 u_int8_t satype;
5940
5941 /* map proto to satype */
5942 satype = key_proto2satype(sah->saidx.proto);
5943 if (satype == 0) {
5944 IPSECLOG(LOG_DEBUG, "there was invalid proto in SAD.\n");
5945 return key_senderror(so, m, EINVAL);
5946 }
5947
5948 /* create new sadb_msg to reply. */
5949 n = key_setdumpsa(sav, SADB_GET, satype, mhp->msg->sadb_msg_seq,
5950 mhp->msg->sadb_msg_pid);
5951 if (!n)
5952 return key_senderror(so, m, ENOBUFS);
5953
5954 m_freem(m);
5955 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
5956 }
5957 }
5958
5959 /* XXX make it sysctl-configurable? */
5960 static void
5961 key_getcomb_setlifetime(struct sadb_comb *comb)
5962 {
5963
5964 comb->sadb_comb_soft_allocations = 1;
5965 comb->sadb_comb_hard_allocations = 1;
5966 comb->sadb_comb_soft_bytes = 0;
5967 comb->sadb_comb_hard_bytes = 0;
5968 comb->sadb_comb_hard_addtime = 86400; /* 1 day */
5969 comb->sadb_comb_soft_addtime = comb->sadb_comb_soft_addtime * 80 / 100;
5970 comb->sadb_comb_soft_usetime = 28800; /* 8 hours */
5971 comb->sadb_comb_hard_usetime = comb->sadb_comb_hard_usetime * 80 / 100;
5972 }
5973
5974 /*
5975 * XXX reorder combinations by preference
5976 * XXX no idea if the user wants ESP authentication or not
5977 */
5978 static struct mbuf *
5979 key_getcomb_esp(void)
5980 {
5981 struct sadb_comb *comb;
5982 const struct enc_xform *algo;
5983 struct mbuf *result = NULL, *m, *n;
5984 int encmin;
5985 int i, off, o;
5986 int totlen;
5987 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
5988
5989 m = NULL;
5990 for (i = 1; i <= SADB_EALG_MAX; i++) {
5991 algo = esp_algorithm_lookup(i);
5992 if (algo == NULL)
5993 continue;
5994
5995 /* discard algorithms with key size smaller than system min */
5996 if (_BITS(algo->maxkey) < ipsec_esp_keymin)
5997 continue;
5998 if (_BITS(algo->minkey) < ipsec_esp_keymin)
5999 encmin = ipsec_esp_keymin;
6000 else
6001 encmin = _BITS(algo->minkey);
6002
6003 if (ipsec_esp_auth)
6004 m = key_getcomb_ah();
6005 else {
6006 KASSERTMSG(l <= MLEN,
6007 "l=%u > MLEN=%lu", l, (u_long) MLEN);
6008 MGET(m, M_DONTWAIT, MT_DATA);
6009 if (m) {
6010 M_ALIGN(m, l);
6011 m->m_len = l;
6012 m->m_next = NULL;
6013 memset(mtod(m, void *), 0, m->m_len);
6014 }
6015 }
6016 if (!m)
6017 goto fail;
6018
6019 totlen = 0;
6020 for (n = m; n; n = n->m_next)
6021 totlen += n->m_len;
6022 KASSERTMSG((totlen % l) == 0, "totlen=%u, l=%u", totlen, l);
6023
6024 for (off = 0; off < totlen; off += l) {
6025 n = m_pulldown(m, off, l, &o);
6026 if (!n) {
6027 /* m is already freed */
6028 goto fail;
6029 }
6030 comb = (struct sadb_comb *)(mtod(n, char *) + o);
6031 memset(comb, 0, sizeof(*comb));
6032 key_getcomb_setlifetime(comb);
6033 comb->sadb_comb_encrypt = i;
6034 comb->sadb_comb_encrypt_minbits = encmin;
6035 comb->sadb_comb_encrypt_maxbits = _BITS(algo->maxkey);
6036 }
6037
6038 if (!result)
6039 result = m;
6040 else
6041 m_cat(result, m);
6042 }
6043
6044 return result;
6045
6046 fail:
6047 if (result)
6048 m_freem(result);
6049 return NULL;
6050 }
6051
6052 static void
6053 key_getsizes_ah(const struct auth_hash *ah, int alg,
6054 u_int16_t* ksmin, u_int16_t* ksmax)
6055 {
6056 *ksmin = *ksmax = ah->keysize;
6057 if (ah->keysize == 0) {
6058 /*
6059 * Transform takes arbitrary key size but algorithm
6060 * key size is restricted. Enforce this here.
6061 */
6062 switch (alg) {
6063 case SADB_X_AALG_MD5: *ksmin = *ksmax = 16; break;
6064 case SADB_X_AALG_SHA: *ksmin = *ksmax = 20; break;
6065 case SADB_X_AALG_NULL: *ksmin = 0; *ksmax = 256; break;
6066 default:
6067 IPSECLOG(LOG_DEBUG, "unknown AH algorithm %u\n", alg);
6068 break;
6069 }
6070 }
6071 }
6072
6073 /*
6074 * XXX reorder combinations by preference
6075 */
6076 static struct mbuf *
6077 key_getcomb_ah(void)
6078 {
6079 struct sadb_comb *comb;
6080 const struct auth_hash *algo;
6081 struct mbuf *m;
6082 u_int16_t minkeysize, maxkeysize;
6083 int i;
6084 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
6085
6086 m = NULL;
6087 for (i = 1; i <= SADB_AALG_MAX; i++) {
6088 #if 1
6089 /* we prefer HMAC algorithms, not old algorithms */
6090 if (i != SADB_AALG_SHA1HMAC &&
6091 i != SADB_AALG_MD5HMAC &&
6092 i != SADB_X_AALG_SHA2_256 &&
6093 i != SADB_X_AALG_SHA2_384 &&
6094 i != SADB_X_AALG_SHA2_512)
6095 continue;
6096 #endif
6097 algo = ah_algorithm_lookup(i);
6098 if (!algo)
6099 continue;
6100 key_getsizes_ah(algo, i, &minkeysize, &maxkeysize);
6101 /* discard algorithms with key size smaller than system min */
6102 if (_BITS(minkeysize) < ipsec_ah_keymin)
6103 continue;
6104
6105 if (!m) {
6106 KASSERTMSG(l <= MLEN,
6107 "l=%u > MLEN=%lu", l, (u_long) MLEN);
6108 MGET(m, M_DONTWAIT, MT_DATA);
6109 if (m) {
6110 M_ALIGN(m, l);
6111 m->m_len = l;
6112 m->m_next = NULL;
6113 }
6114 } else
6115 M_PREPEND(m, l, M_DONTWAIT);
6116 if (!m)
6117 return NULL;
6118
6119 comb = mtod(m, struct sadb_comb *);
6120 memset(comb, 0, sizeof(*comb));
6121 key_getcomb_setlifetime(comb);
6122 comb->sadb_comb_auth = i;
6123 comb->sadb_comb_auth_minbits = _BITS(minkeysize);
6124 comb->sadb_comb_auth_maxbits = _BITS(maxkeysize);
6125 }
6126
6127 return m;
6128 }
6129
6130 /*
6131 * not really an official behavior. discussed in pf_key (at) inner.net in Sep2000.
6132 * XXX reorder combinations by preference
6133 */
6134 static struct mbuf *
6135 key_getcomb_ipcomp(void)
6136 {
6137 struct sadb_comb *comb;
6138 const struct comp_algo *algo;
6139 struct mbuf *m;
6140 int i;
6141 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
6142
6143 m = NULL;
6144 for (i = 1; i <= SADB_X_CALG_MAX; i++) {
6145 algo = ipcomp_algorithm_lookup(i);
6146 if (!algo)
6147 continue;
6148
6149 if (!m) {
6150 KASSERTMSG(l <= MLEN,
6151 "l=%u > MLEN=%lu", l, (u_long) MLEN);
6152 MGET(m, M_DONTWAIT, MT_DATA);
6153 if (m) {
6154 M_ALIGN(m, l);
6155 m->m_len = l;
6156 m->m_next = NULL;
6157 }
6158 } else
6159 M_PREPEND(m, l, M_DONTWAIT);
6160 if (!m)
6161 return NULL;
6162
6163 comb = mtod(m, struct sadb_comb *);
6164 memset(comb, 0, sizeof(*comb));
6165 key_getcomb_setlifetime(comb);
6166 comb->sadb_comb_encrypt = i;
6167 /* what should we set into sadb_comb_*_{min,max}bits? */
6168 }
6169
6170 return m;
6171 }
6172
6173 /*
6174 * XXX no way to pass mode (transport/tunnel) to userland
6175 * XXX replay checking?
6176 * XXX sysctl interface to ipsec_{ah,esp}_keymin
6177 */
6178 static struct mbuf *
6179 key_getprop(const struct secasindex *saidx)
6180 {
6181 struct sadb_prop *prop;
6182 struct mbuf *m, *n;
6183 const int l = PFKEY_ALIGN8(sizeof(struct sadb_prop));
6184 int totlen;
6185
6186 switch (saidx->proto) {
6187 case IPPROTO_ESP:
6188 m = key_getcomb_esp();
6189 break;
6190 case IPPROTO_AH:
6191 m = key_getcomb_ah();
6192 break;
6193 case IPPROTO_IPCOMP:
6194 m = key_getcomb_ipcomp();
6195 break;
6196 default:
6197 return NULL;
6198 }
6199
6200 if (!m)
6201 return NULL;
6202 M_PREPEND(m, l, M_DONTWAIT);
6203 if (!m)
6204 return NULL;
6205
6206 totlen = 0;
6207 for (n = m; n; n = n->m_next)
6208 totlen += n->m_len;
6209
6210 prop = mtod(m, struct sadb_prop *);
6211 memset(prop, 0, sizeof(*prop));
6212 prop->sadb_prop_len = PFKEY_UNIT64(totlen);
6213 prop->sadb_prop_exttype = SADB_EXT_PROPOSAL;
6214 prop->sadb_prop_replay = 32; /* XXX */
6215
6216 return m;
6217 }
6218
6219 /*
6220 * SADB_ACQUIRE processing called by key_checkrequest() and key_acquire2().
6221 * send
6222 * <base, SA, address(SD), (address(P)), x_policy,
6223 * (identity(SD),) (sensitivity,) proposal>
6224 * to KMD, and expect to receive
6225 * <base> with SADB_ACQUIRE if error occurred,
6226 * or
6227 * <base, src address, dst address, (SPI range)> with SADB_GETSPI
6228 * from KMD by PF_KEY.
6229 *
6230 * XXX x_policy is outside of RFC2367 (KAME extension).
6231 * XXX sensitivity is not supported.
6232 * XXX for ipcomp, RFC2367 does not define how to fill in proposal.
6233 * see comment for key_getcomb_ipcomp().
6234 *
6235 * OUT:
6236 * 0 : succeed
6237 * others: error number
6238 */
6239 static int
6240 key_acquire(const struct secasindex *saidx, struct secpolicy *sp)
6241 {
6242 struct mbuf *result = NULL, *m;
6243 #ifndef IPSEC_NONBLOCK_ACQUIRE
6244 struct secacq *newacq;
6245 #endif
6246 u_int8_t satype;
6247 int error = -1;
6248 u_int32_t seq;
6249
6250 /* sanity check */
6251 KASSERT(saidx != NULL);
6252 satype = key_proto2satype(saidx->proto);
6253 KASSERTMSG(satype != 0, "null satype, protocol %u", saidx->proto);
6254
6255 #ifndef IPSEC_NONBLOCK_ACQUIRE
6256 /*
6257 * We never do anything about acquirng SA. There is anather
6258 * solution that kernel blocks to send SADB_ACQUIRE message until
6259 * getting something message from IKEd. In later case, to be
6260 * managed with ACQUIRING list.
6261 */
6262 /* Get an entry to check whether sending message or not. */
6263 mutex_enter(&key_mtx);
6264 newacq = key_getacq(saidx);
6265 if (newacq != NULL) {
6266 if (key_blockacq_count < newacq->count) {
6267 /* reset counter and do send message. */
6268 newacq->count = 0;
6269 } else {
6270 /* increment counter and do nothing. */
6271 newacq->count++;
6272 mutex_exit(&key_mtx);
6273 return 0;
6274 }
6275 } else {
6276 /* make new entry for blocking to send SADB_ACQUIRE. */
6277 newacq = key_newacq(saidx);
6278 if (newacq == NULL)
6279 return ENOBUFS;
6280
6281 /* add to acqtree */
6282 LIST_INSERT_HEAD(&acqtree, newacq, chain);
6283 }
6284
6285 seq = newacq->seq;
6286 mutex_exit(&key_mtx);
6287 #else
6288 seq = (acq_seq = (acq_seq == ~0 ? 1 : ++acq_seq));
6289 #endif
6290 m = key_setsadbmsg(SADB_ACQUIRE, 0, satype, seq, 0, 0);
6291 if (!m) {
6292 error = ENOBUFS;
6293 goto fail;
6294 }
6295 result = m;
6296
6297 /* set sadb_address for saidx's. */
6298 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC, &saidx->src.sa, FULLMASK,
6299 IPSEC_ULPROTO_ANY);
6300 if (!m) {
6301 error = ENOBUFS;
6302 goto fail;
6303 }
6304 m_cat(result, m);
6305
6306 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST, &saidx->dst.sa, FULLMASK,
6307 IPSEC_ULPROTO_ANY);
6308 if (!m) {
6309 error = ENOBUFS;
6310 goto fail;
6311 }
6312 m_cat(result, m);
6313
6314 /* XXX proxy address (optional) */
6315
6316 /* set sadb_x_policy */
6317 if (sp) {
6318 m = key_setsadbxpolicy(sp->policy, sp->spidx.dir, sp->id);
6319 if (!m) {
6320 error = ENOBUFS;
6321 goto fail;
6322 }
6323 m_cat(result, m);
6324 }
6325
6326 /* XXX identity (optional) */
6327 #if 0
6328 if (idexttype && fqdn) {
6329 /* create identity extension (FQDN) */
6330 struct sadb_ident *id;
6331 int fqdnlen;
6332
6333 fqdnlen = strlen(fqdn) + 1; /* +1 for terminating-NUL */
6334 id = (struct sadb_ident *)p;
6335 memset(id, 0, sizeof(*id) + PFKEY_ALIGN8(fqdnlen));
6336 id->sadb_ident_len = PFKEY_UNIT64(sizeof(*id) + PFKEY_ALIGN8(fqdnlen));
6337 id->sadb_ident_exttype = idexttype;
6338 id->sadb_ident_type = SADB_IDENTTYPE_FQDN;
6339 memcpy(id + 1, fqdn, fqdnlen);
6340 p += sizeof(struct sadb_ident) + PFKEY_ALIGN8(fqdnlen);
6341 }
6342
6343 if (idexttype) {
6344 /* create identity extension (USERFQDN) */
6345 struct sadb_ident *id;
6346 int userfqdnlen;
6347
6348 if (userfqdn) {
6349 /* +1 for terminating-NUL */
6350 userfqdnlen = strlen(userfqdn) + 1;
6351 } else
6352 userfqdnlen = 0;
6353 id = (struct sadb_ident *)p;
6354 memset(id, 0, sizeof(*id) + PFKEY_ALIGN8(userfqdnlen));
6355 id->sadb_ident_len = PFKEY_UNIT64(sizeof(*id) + PFKEY_ALIGN8(userfqdnlen));
6356 id->sadb_ident_exttype = idexttype;
6357 id->sadb_ident_type = SADB_IDENTTYPE_USERFQDN;
6358 /* XXX is it correct? */
6359 if (curlwp)
6360 id->sadb_ident_id = kauth_cred_getuid(curlwp->l_cred);
6361 if (userfqdn && userfqdnlen)
6362 memcpy(id + 1, userfqdn, userfqdnlen);
6363 p += sizeof(struct sadb_ident) + PFKEY_ALIGN8(userfqdnlen);
6364 }
6365 #endif
6366
6367 /* XXX sensitivity (optional) */
6368
6369 /* create proposal/combination extension */
6370 m = key_getprop(saidx);
6371 #if 0
6372 /*
6373 * spec conformant: always attach proposal/combination extension,
6374 * the problem is that we have no way to attach it for ipcomp,
6375 * due to the way sadb_comb is declared in RFC2367.
6376 */
6377 if (!m) {
6378 error = ENOBUFS;
6379 goto fail;
6380 }
6381 m_cat(result, m);
6382 #else
6383 /*
6384 * outside of spec; make proposal/combination extension optional.
6385 */
6386 if (m)
6387 m_cat(result, m);
6388 #endif
6389
6390 if ((result->m_flags & M_PKTHDR) == 0) {
6391 error = EINVAL;
6392 goto fail;
6393 }
6394
6395 if (result->m_len < sizeof(struct sadb_msg)) {
6396 result = m_pullup(result, sizeof(struct sadb_msg));
6397 if (result == NULL) {
6398 error = ENOBUFS;
6399 goto fail;
6400 }
6401 }
6402
6403 result->m_pkthdr.len = 0;
6404 for (m = result; m; m = m->m_next)
6405 result->m_pkthdr.len += m->m_len;
6406
6407 mtod(result, struct sadb_msg *)->sadb_msg_len =
6408 PFKEY_UNIT64(result->m_pkthdr.len);
6409
6410 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
6411
6412 fail:
6413 if (result)
6414 m_freem(result);
6415 return error;
6416 }
6417
6418 #ifndef IPSEC_NONBLOCK_ACQUIRE
6419 static struct secacq *
6420 key_newacq(const struct secasindex *saidx)
6421 {
6422 struct secacq *newacq;
6423
6424 /* get new entry */
6425 newacq = kmem_intr_zalloc(sizeof(struct secacq), KM_NOSLEEP);
6426 if (newacq == NULL) {
6427 IPSECLOG(LOG_DEBUG, "No more memory.\n");
6428 return NULL;
6429 }
6430
6431 /* copy secindex */
6432 memcpy(&newacq->saidx, saidx, sizeof(newacq->saidx));
6433 newacq->seq = (acq_seq == ~0 ? 1 : ++acq_seq);
6434 newacq->created = time_uptime;
6435 newacq->count = 0;
6436
6437 return newacq;
6438 }
6439
6440 static struct secacq *
6441 key_getacq(const struct secasindex *saidx)
6442 {
6443 struct secacq *acq;
6444
6445 KASSERT(mutex_owned(&key_mtx));
6446
6447 LIST_FOREACH(acq, &acqtree, chain) {
6448 if (key_saidx_match(saidx, &acq->saidx, CMP_EXACTLY))
6449 return acq;
6450 }
6451
6452 return NULL;
6453 }
6454
6455 static struct secacq *
6456 key_getacqbyseq(u_int32_t seq)
6457 {
6458 struct secacq *acq;
6459
6460 KASSERT(mutex_owned(&key_mtx));
6461
6462 LIST_FOREACH(acq, &acqtree, chain) {
6463 if (acq->seq == seq)
6464 return acq;
6465 }
6466
6467 return NULL;
6468 }
6469 #endif
6470
6471 #ifdef notyet
6472 static struct secspacq *
6473 key_newspacq(const struct secpolicyindex *spidx)
6474 {
6475 struct secspacq *acq;
6476
6477 /* get new entry */
6478 acq = kmem_intr_zalloc(sizeof(struct secspacq), KM_NOSLEEP);
6479 if (acq == NULL) {
6480 IPSECLOG(LOG_DEBUG, "No more memory.\n");
6481 return NULL;
6482 }
6483
6484 /* copy secindex */
6485 memcpy(&acq->spidx, spidx, sizeof(acq->spidx));
6486 acq->created = time_uptime;
6487 acq->count = 0;
6488
6489 return acq;
6490 }
6491
6492 static struct secspacq *
6493 key_getspacq(const struct secpolicyindex *spidx)
6494 {
6495 struct secspacq *acq;
6496
6497 LIST_FOREACH(acq, &spacqtree, chain) {
6498 if (key_spidx_match_exactly(spidx, &acq->spidx))
6499 return acq;
6500 }
6501
6502 return NULL;
6503 }
6504 #endif /* notyet */
6505
6506 /*
6507 * SADB_ACQUIRE processing,
6508 * in first situation, is receiving
6509 * <base>
6510 * from the ikmpd, and clear sequence of its secasvar entry.
6511 *
6512 * In second situation, is receiving
6513 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
6514 * from a user land process, and return
6515 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
6516 * to the socket.
6517 *
6518 * m will always be freed.
6519 */
6520 static int
6521 key_acquire2(struct socket *so, struct mbuf *m,
6522 const struct sadb_msghdr *mhp)
6523 {
6524 const struct sadb_address *src0, *dst0;
6525 struct secasindex saidx;
6526 struct secashead *sah;
6527 u_int16_t proto;
6528 int error;
6529
6530 KASSERT(so != NULL);
6531 KASSERT(m != NULL);
6532 KASSERT(mhp != NULL);
6533 KASSERT(mhp->msg != NULL);
6534
6535 /*
6536 * Error message from KMd.
6537 * We assume that if error was occurred in IKEd, the length of PFKEY
6538 * message is equal to the size of sadb_msg structure.
6539 * We do not raise error even if error occurred in this function.
6540 */
6541 if (mhp->msg->sadb_msg_len == PFKEY_UNIT64(sizeof(struct sadb_msg))) {
6542 #ifndef IPSEC_NONBLOCK_ACQUIRE
6543 struct secacq *acq;
6544
6545 /* check sequence number */
6546 if (mhp->msg->sadb_msg_seq == 0) {
6547 IPSECLOG(LOG_DEBUG, "must specify sequence number.\n");
6548 m_freem(m);
6549 return 0;
6550 }
6551
6552 mutex_enter(&key_mtx);
6553 acq = key_getacqbyseq(mhp->msg->sadb_msg_seq);
6554 if (acq == NULL) {
6555 mutex_exit(&key_mtx);
6556 /*
6557 * the specified larval SA is already gone, or we got
6558 * a bogus sequence number. we can silently ignore it.
6559 */
6560 m_freem(m);
6561 return 0;
6562 }
6563
6564 /* reset acq counter in order to deletion by timehander. */
6565 acq->created = time_uptime;
6566 acq->count = 0;
6567 mutex_exit(&key_mtx);
6568 #endif
6569 m_freem(m);
6570 return 0;
6571 }
6572
6573 /*
6574 * This message is from user land.
6575 */
6576
6577 /* map satype to proto */
6578 proto = key_satype2proto(mhp->msg->sadb_msg_satype);
6579 if (proto == 0) {
6580 IPSECLOG(LOG_DEBUG, "invalid satype is passed.\n");
6581 return key_senderror(so, m, EINVAL);
6582 }
6583
6584 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
6585 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
6586 mhp->ext[SADB_EXT_PROPOSAL] == NULL) {
6587 /* error */
6588 IPSECLOG(LOG_DEBUG, "invalid message is passed.\n");
6589 return key_senderror(so, m, EINVAL);
6590 }
6591 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
6592 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
6593 mhp->extlen[SADB_EXT_PROPOSAL] < sizeof(struct sadb_prop)) {
6594 /* error */
6595 IPSECLOG(LOG_DEBUG, "invalid message is passed.\n");
6596 return key_senderror(so, m, EINVAL);
6597 }
6598
6599 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
6600 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
6601
6602 error = key_setsecasidx(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1,
6603 &saidx);
6604 if (error != 0)
6605 return key_senderror(so, m, EINVAL);
6606
6607 error = key_set_natt_ports(&saidx.src, &saidx.dst, mhp);
6608 if (error != 0)
6609 return key_senderror(so, m, EINVAL);
6610
6611 /* get a SA index */
6612 LIST_FOREACH(sah, &sahtree, chain) {
6613 if (sah->state == SADB_SASTATE_DEAD)
6614 continue;
6615 if (key_saidx_match(&sah->saidx, &saidx, CMP_MODE_REQID))
6616 break;
6617 }
6618 if (sah != NULL) {
6619 IPSECLOG(LOG_DEBUG, "a SA exists already.\n");
6620 return key_senderror(so, m, EEXIST);
6621 }
6622
6623 error = key_acquire(&saidx, NULL);
6624 if (error != 0) {
6625 IPSECLOG(LOG_DEBUG, "error %d returned from key_acquire.\n",
6626 mhp->msg->sadb_msg_errno);
6627 return key_senderror(so, m, error);
6628 }
6629
6630 return key_sendup_mbuf(so, m, KEY_SENDUP_REGISTERED);
6631 }
6632
6633 /*
6634 * SADB_REGISTER processing.
6635 * If SATYPE_UNSPEC has been passed as satype, only return sabd_supported.
6636 * receive
6637 * <base>
6638 * from the ikmpd, and register a socket to send PF_KEY messages,
6639 * and send
6640 * <base, supported>
6641 * to KMD by PF_KEY.
6642 * If socket is detached, must free from regnode.
6643 *
6644 * m will always be freed.
6645 */
6646 static int
6647 key_register(struct socket *so, struct mbuf *m,
6648 const struct sadb_msghdr *mhp)
6649 {
6650 struct secreg *reg, *newreg = 0;
6651
6652 KASSERT(!cpu_softintr_p());
6653 KASSERT(so != NULL);
6654 KASSERT(m != NULL);
6655 KASSERT(mhp != NULL);
6656 KASSERT(mhp->msg != NULL);
6657
6658 /* check for invalid register message */
6659 if (mhp->msg->sadb_msg_satype >= __arraycount(regtree))
6660 return key_senderror(so, m, EINVAL);
6661
6662 /* When SATYPE_UNSPEC is specified, only return sabd_supported. */
6663 if (mhp->msg->sadb_msg_satype == SADB_SATYPE_UNSPEC)
6664 goto setmsg;
6665
6666 /* Allocate regnode in advance, out of mutex */
6667 newreg = kmem_zalloc(sizeof(*newreg), KM_SLEEP);
6668
6669 /* check whether existing or not */
6670 mutex_enter(&key_mtx);
6671 LIST_FOREACH(reg, ®tree[mhp->msg->sadb_msg_satype], chain) {
6672 if (reg->so == so) {
6673 IPSECLOG(LOG_DEBUG, "socket exists already.\n");
6674 mutex_exit(&key_mtx);
6675 kmem_free(newreg, sizeof(*newreg));
6676 return key_senderror(so, m, EEXIST);
6677 }
6678 }
6679
6680 newreg->so = so;
6681 ((struct keycb *)sotorawcb(so))->kp_registered++;
6682
6683 /* add regnode to regtree. */
6684 LIST_INSERT_HEAD(®tree[mhp->msg->sadb_msg_satype], newreg, chain);
6685 mutex_exit(&key_mtx);
6686
6687 setmsg:
6688 {
6689 struct mbuf *n;
6690 struct sadb_msg *newmsg;
6691 struct sadb_supported *sup;
6692 u_int len, alen, elen;
6693 int off;
6694 int i;
6695 struct sadb_alg *alg;
6696
6697 /* create new sadb_msg to reply. */
6698 alen = 0;
6699 for (i = 1; i <= SADB_AALG_MAX; i++) {
6700 if (ah_algorithm_lookup(i))
6701 alen += sizeof(struct sadb_alg);
6702 }
6703 if (alen)
6704 alen += sizeof(struct sadb_supported);
6705 elen = 0;
6706 for (i = 1; i <= SADB_EALG_MAX; i++) {
6707 if (esp_algorithm_lookup(i))
6708 elen += sizeof(struct sadb_alg);
6709 }
6710 if (elen)
6711 elen += sizeof(struct sadb_supported);
6712
6713 len = sizeof(struct sadb_msg) + alen + elen;
6714
6715 if (len > MCLBYTES)
6716 return key_senderror(so, m, ENOBUFS);
6717
6718 MGETHDR(n, M_DONTWAIT, MT_DATA);
6719 if (len > MHLEN) {
6720 MCLGET(n, M_DONTWAIT);
6721 if ((n->m_flags & M_EXT) == 0) {
6722 m_freem(n);
6723 n = NULL;
6724 }
6725 }
6726 if (!n)
6727 return key_senderror(so, m, ENOBUFS);
6728
6729 n->m_pkthdr.len = n->m_len = len;
6730 n->m_next = NULL;
6731 off = 0;
6732
6733 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, char *) + off);
6734 newmsg = mtod(n, struct sadb_msg *);
6735 newmsg->sadb_msg_errno = 0;
6736 newmsg->sadb_msg_len = PFKEY_UNIT64(len);
6737 off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
6738
6739 /* for authentication algorithm */
6740 if (alen) {
6741 sup = (struct sadb_supported *)(mtod(n, char *) + off);
6742 sup->sadb_supported_len = PFKEY_UNIT64(alen);
6743 sup->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH;
6744 off += PFKEY_ALIGN8(sizeof(*sup));
6745
6746 for (i = 1; i <= SADB_AALG_MAX; i++) {
6747 const struct auth_hash *aalgo;
6748 u_int16_t minkeysize, maxkeysize;
6749
6750 aalgo = ah_algorithm_lookup(i);
6751 if (!aalgo)
6752 continue;
6753 alg = (struct sadb_alg *)(mtod(n, char *) + off);
6754 alg->sadb_alg_id = i;
6755 alg->sadb_alg_ivlen = 0;
6756 key_getsizes_ah(aalgo, i, &minkeysize, &maxkeysize);
6757 alg->sadb_alg_minbits = _BITS(minkeysize);
6758 alg->sadb_alg_maxbits = _BITS(maxkeysize);
6759 off += PFKEY_ALIGN8(sizeof(*alg));
6760 }
6761 }
6762
6763 /* for encryption algorithm */
6764 if (elen) {
6765 sup = (struct sadb_supported *)(mtod(n, char *) + off);
6766 sup->sadb_supported_len = PFKEY_UNIT64(elen);
6767 sup->sadb_supported_exttype = SADB_EXT_SUPPORTED_ENCRYPT;
6768 off += PFKEY_ALIGN8(sizeof(*sup));
6769
6770 for (i = 1; i <= SADB_EALG_MAX; i++) {
6771 const struct enc_xform *ealgo;
6772
6773 ealgo = esp_algorithm_lookup(i);
6774 if (!ealgo)
6775 continue;
6776 alg = (struct sadb_alg *)(mtod(n, char *) + off);
6777 alg->sadb_alg_id = i;
6778 alg->sadb_alg_ivlen = ealgo->blocksize;
6779 alg->sadb_alg_minbits = _BITS(ealgo->minkey);
6780 alg->sadb_alg_maxbits = _BITS(ealgo->maxkey);
6781 off += PFKEY_ALIGN8(sizeof(struct sadb_alg));
6782 }
6783 }
6784
6785 KASSERTMSG(off == len, "length inconsistency");
6786
6787 m_freem(m);
6788 return key_sendup_mbuf(so, n, KEY_SENDUP_REGISTERED);
6789 }
6790 }
6791
6792 /*
6793 * free secreg entry registered.
6794 * XXX: I want to do free a socket marked done SADB_RESIGER to socket.
6795 */
6796 void
6797 key_freereg(struct socket *so)
6798 {
6799 struct secreg *reg;
6800 int i;
6801
6802 KASSERT(!cpu_softintr_p());
6803 KASSERT(so != NULL);
6804
6805 /*
6806 * check whether existing or not.
6807 * check all type of SA, because there is a potential that
6808 * one socket is registered to multiple type of SA.
6809 */
6810 for (i = 0; i <= SADB_SATYPE_MAX; i++) {
6811 mutex_enter(&key_mtx);
6812 LIST_FOREACH(reg, ®tree[i], chain) {
6813 if (reg->so == so) {
6814 LIST_REMOVE(reg, chain);
6815 break;
6816 }
6817 }
6818 mutex_exit(&key_mtx);
6819 if (reg != NULL)
6820 kmem_free(reg, sizeof(*reg));
6821 }
6822
6823 return;
6824 }
6825
6826 /*
6827 * SADB_EXPIRE processing
6828 * send
6829 * <base, SA, SA2, lifetime(C and one of HS), address(SD)>
6830 * to KMD by PF_KEY.
6831 * NOTE: We send only soft lifetime extension.
6832 *
6833 * OUT: 0 : succeed
6834 * others : error number
6835 */
6836 static int
6837 key_expire(struct secasvar *sav)
6838 {
6839 int s;
6840 int satype;
6841 struct mbuf *result = NULL, *m;
6842 int len;
6843 int error = -1;
6844 struct sadb_lifetime *lt;
6845
6846 /* XXX: Why do we lock ? */
6847 s = splsoftnet(); /*called from softclock()*/
6848
6849 KASSERT(sav != NULL);
6850 KASSERT(sav->sah != NULL);
6851
6852 satype = key_proto2satype(sav->sah->saidx.proto);
6853 KASSERTMSG(satype != 0, "invalid proto is passed");
6854
6855 /* set msg header */
6856 m = key_setsadbmsg(SADB_EXPIRE, 0, satype, sav->seq, 0, sav->refcnt);
6857 if (!m) {
6858 error = ENOBUFS;
6859 goto fail;
6860 }
6861 result = m;
6862
6863 /* create SA extension */
6864 m = key_setsadbsa(sav);
6865 if (!m) {
6866 error = ENOBUFS;
6867 goto fail;
6868 }
6869 m_cat(result, m);
6870
6871 /* create SA extension */
6872 m = key_setsadbxsa2(sav->sah->saidx.mode,
6873 sav->replay ? sav->replay->count : 0, sav->sah->saidx.reqid);
6874 if (!m) {
6875 error = ENOBUFS;
6876 goto fail;
6877 }
6878 m_cat(result, m);
6879
6880 /* create lifetime extension (current and soft) */
6881 len = PFKEY_ALIGN8(sizeof(*lt)) * 2;
6882 m = key_alloc_mbuf(len);
6883 if (!m || m->m_next) { /*XXX*/
6884 if (m)
6885 m_freem(m);
6886 error = ENOBUFS;
6887 goto fail;
6888 }
6889 memset(mtod(m, void *), 0, len);
6890 lt = mtod(m, struct sadb_lifetime *);
6891 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
6892 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
6893 lt->sadb_lifetime_allocations = sav->lft_c->sadb_lifetime_allocations;
6894 lt->sadb_lifetime_bytes = sav->lft_c->sadb_lifetime_bytes;
6895 lt->sadb_lifetime_addtime =
6896 time_mono_to_wall(sav->lft_c->sadb_lifetime_addtime);
6897 lt->sadb_lifetime_usetime =
6898 time_mono_to_wall(sav->lft_c->sadb_lifetime_usetime);
6899 lt = (struct sadb_lifetime *)(mtod(m, char *) + len / 2);
6900 memcpy(lt, sav->lft_s, sizeof(*lt));
6901 m_cat(result, m);
6902
6903 /* set sadb_address for source */
6904 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC, &sav->sah->saidx.src.sa,
6905 FULLMASK, IPSEC_ULPROTO_ANY);
6906 if (!m) {
6907 error = ENOBUFS;
6908 goto fail;
6909 }
6910 m_cat(result, m);
6911
6912 /* set sadb_address for destination */
6913 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST, &sav->sah->saidx.dst.sa,
6914 FULLMASK, IPSEC_ULPROTO_ANY);
6915 if (!m) {
6916 error = ENOBUFS;
6917 goto fail;
6918 }
6919 m_cat(result, m);
6920
6921 if ((result->m_flags & M_PKTHDR) == 0) {
6922 error = EINVAL;
6923 goto fail;
6924 }
6925
6926 if (result->m_len < sizeof(struct sadb_msg)) {
6927 result = m_pullup(result, sizeof(struct sadb_msg));
6928 if (result == NULL) {
6929 error = ENOBUFS;
6930 goto fail;
6931 }
6932 }
6933
6934 result->m_pkthdr.len = 0;
6935 for (m = result; m; m = m->m_next)
6936 result->m_pkthdr.len += m->m_len;
6937
6938 mtod(result, struct sadb_msg *)->sadb_msg_len =
6939 PFKEY_UNIT64(result->m_pkthdr.len);
6940
6941 splx(s);
6942 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
6943
6944 fail:
6945 if (result)
6946 m_freem(result);
6947 splx(s);
6948 return error;
6949 }
6950
6951 /*
6952 * SADB_FLUSH processing
6953 * receive
6954 * <base>
6955 * from the ikmpd, and free all entries in secastree.
6956 * and send,
6957 * <base>
6958 * to the ikmpd.
6959 * NOTE: to do is only marking SADB_SASTATE_DEAD.
6960 *
6961 * m will always be freed.
6962 */
6963 static int
6964 key_flush(struct socket *so, struct mbuf *m,
6965 const struct sadb_msghdr *mhp)
6966 {
6967 struct sadb_msg *newmsg;
6968 struct secashead *sah;
6969 struct secasvar *sav, *nextsav;
6970 u_int16_t proto;
6971 u_int8_t state;
6972
6973 KASSERT(so != NULL);
6974 KASSERT(mhp != NULL);
6975 KASSERT(mhp->msg != NULL);
6976
6977 /* map satype to proto */
6978 proto = key_satype2proto(mhp->msg->sadb_msg_satype);
6979 if (proto == 0) {
6980 IPSECLOG(LOG_DEBUG, "invalid satype is passed.\n");
6981 return key_senderror(so, m, EINVAL);
6982 }
6983
6984 /* no SATYPE specified, i.e. flushing all SA. */
6985 LIST_FOREACH(sah, &sahtree, chain) {
6986 if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC &&
6987 proto != sah->saidx.proto)
6988 continue;
6989
6990 SASTATE_ALIVE_FOREACH(state) {
6991 LIST_FOREACH_SAFE(sav, &sah->savtree[state], chain,
6992 nextsav) {
6993 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
6994 KEY_FREESAV(&sav);
6995 }
6996 }
6997
6998 sah->state = SADB_SASTATE_DEAD;
6999 }
7000
7001 if (m->m_len < sizeof(struct sadb_msg) ||
7002 sizeof(struct sadb_msg) > m->m_len + M_TRAILINGSPACE(m)) {
7003 IPSECLOG(LOG_DEBUG, "No more memory.\n");
7004 return key_senderror(so, m, ENOBUFS);
7005 }
7006
7007 if (m->m_next)
7008 m_freem(m->m_next);
7009 m->m_next = NULL;
7010 m->m_pkthdr.len = m->m_len = sizeof(struct sadb_msg);
7011 newmsg = mtod(m, struct sadb_msg *);
7012 newmsg->sadb_msg_errno = 0;
7013 newmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
7014
7015 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
7016 }
7017
7018
7019 static struct mbuf *
7020 key_setdump_chain(u_int8_t req_satype, int *errorp, int *lenp, pid_t pid)
7021 {
7022 struct secashead *sah;
7023 struct secasvar *sav;
7024 u_int16_t proto;
7025 u_int8_t satype;
7026 u_int8_t state;
7027 int cnt;
7028 struct mbuf *m, *n, *prev;
7029
7030 *lenp = 0;
7031
7032 /* map satype to proto */
7033 proto = key_satype2proto(req_satype);
7034 if (proto == 0) {
7035 *errorp = EINVAL;
7036 return (NULL);
7037 }
7038
7039 /* count sav entries to be sent to userland. */
7040 cnt = 0;
7041 LIST_FOREACH(sah, &sahtree, chain) {
7042 if (req_satype != SADB_SATYPE_UNSPEC &&
7043 proto != sah->saidx.proto)
7044 continue;
7045
7046 SASTATE_ANY_FOREACH(state) {
7047 LIST_FOREACH(sav, &sah->savtree[state], chain) {
7048 cnt++;
7049 }
7050 }
7051 }
7052
7053 if (cnt == 0) {
7054 *errorp = ENOENT;
7055 return (NULL);
7056 }
7057
7058 /* send this to the userland, one at a time. */
7059 m = NULL;
7060 prev = m;
7061 LIST_FOREACH(sah, &sahtree, chain) {
7062 if (req_satype != SADB_SATYPE_UNSPEC &&
7063 proto != sah->saidx.proto)
7064 continue;
7065
7066 /* map proto to satype */
7067 satype = key_proto2satype(sah->saidx.proto);
7068 if (satype == 0) {
7069 m_freem(m);
7070 *errorp = EINVAL;
7071 return (NULL);
7072 }
7073
7074 SASTATE_ANY_FOREACH(state) {
7075 LIST_FOREACH(sav, &sah->savtree[state], chain) {
7076 n = key_setdumpsa(sav, SADB_DUMP, satype,
7077 --cnt, pid);
7078 if (!n) {
7079 m_freem(m);
7080 *errorp = ENOBUFS;
7081 return (NULL);
7082 }
7083
7084 if (!m)
7085 m = n;
7086 else
7087 prev->m_nextpkt = n;
7088 prev = n;
7089 }
7090 }
7091 }
7092
7093 if (!m) {
7094 *errorp = EINVAL;
7095 return (NULL);
7096 }
7097
7098 if ((m->m_flags & M_PKTHDR) != 0) {
7099 m->m_pkthdr.len = 0;
7100 for (n = m; n; n = n->m_next)
7101 m->m_pkthdr.len += n->m_len;
7102 }
7103
7104 *errorp = 0;
7105 return (m);
7106 }
7107
7108 /*
7109 * SADB_DUMP processing
7110 * dump all entries including status of DEAD in SAD.
7111 * receive
7112 * <base>
7113 * from the ikmpd, and dump all secasvar leaves
7114 * and send,
7115 * <base> .....
7116 * to the ikmpd.
7117 *
7118 * m will always be freed.
7119 */
7120 static int
7121 key_dump(struct socket *so, struct mbuf *m0,
7122 const struct sadb_msghdr *mhp)
7123 {
7124 u_int16_t proto;
7125 u_int8_t satype;
7126 struct mbuf *n;
7127 int s;
7128 int error, len, ok;
7129
7130 KASSERT(so != NULL);
7131 KASSERT(m0 != NULL);
7132 KASSERT(mhp != NULL);
7133 KASSERT(mhp->msg != NULL);
7134
7135 /* map satype to proto */
7136 satype = mhp->msg->sadb_msg_satype;
7137 proto = key_satype2proto(satype);
7138 if (proto == 0) {
7139 IPSECLOG(LOG_DEBUG, "invalid satype is passed.\n");
7140 return key_senderror(so, m0, EINVAL);
7141 }
7142
7143 /*
7144 * If the requestor has insufficient socket-buffer space
7145 * for the entire chain, nobody gets any response to the DUMP.
7146 * XXX For now, only the requestor ever gets anything.
7147 * Moreover, if the requestor has any space at all, they receive
7148 * the entire chain, otherwise the request is refused with ENOBUFS.
7149 */
7150 if (sbspace(&so->so_rcv) <= 0) {
7151 return key_senderror(so, m0, ENOBUFS);
7152 }
7153
7154 s = splsoftnet();
7155 n = key_setdump_chain(satype, &error, &len, mhp->msg->sadb_msg_pid);
7156 splx(s);
7157
7158 if (n == NULL) {
7159 return key_senderror(so, m0, ENOENT);
7160 }
7161 {
7162 uint64_t *ps = PFKEY_STAT_GETREF();
7163 ps[PFKEY_STAT_IN_TOTAL]++;
7164 ps[PFKEY_STAT_IN_BYTES] += len;
7165 PFKEY_STAT_PUTREF();
7166 }
7167
7168 /*
7169 * PF_KEY DUMP responses are no longer broadcast to all PF_KEY sockets.
7170 * The requestor receives either the entire chain, or an
7171 * error message with ENOBUFS.
7172 *
7173 * sbappendaddrchain() takes the chain of entries, one
7174 * packet-record per SPD entry, prepends the key_src sockaddr
7175 * to each packet-record, links the sockaddr mbufs into a new
7176 * list of records, then appends the entire resulting
7177 * list to the requesting socket.
7178 */
7179 ok = sbappendaddrchain(&so->so_rcv, (struct sockaddr *)&key_src, n,
7180 SB_PRIO_ONESHOT_OVERFLOW);
7181
7182 if (!ok) {
7183 PFKEY_STATINC(PFKEY_STAT_IN_NOMEM);
7184 m_freem(n);
7185 return key_senderror(so, m0, ENOBUFS);
7186 }
7187
7188 m_freem(m0);
7189 return 0;
7190 }
7191
7192 /*
7193 * SADB_X_PROMISC processing
7194 *
7195 * m will always be freed.
7196 */
7197 static int
7198 key_promisc(struct socket *so, struct mbuf *m,
7199 const struct sadb_msghdr *mhp)
7200 {
7201 int olen;
7202
7203 KASSERT(so != NULL);
7204 KASSERT(m != NULL);
7205 KASSERT(mhp != NULL);
7206 KASSERT(mhp->msg != NULL);
7207
7208 olen = PFKEY_UNUNIT64(mhp->msg->sadb_msg_len);
7209
7210 if (olen < sizeof(struct sadb_msg)) {
7211 #if 1
7212 return key_senderror(so, m, EINVAL);
7213 #else
7214 m_freem(m);
7215 return 0;
7216 #endif
7217 } else if (olen == sizeof(struct sadb_msg)) {
7218 /* enable/disable promisc mode */
7219 struct keycb *kp = (struct keycb *)sotorawcb(so);
7220 if (kp == NULL)
7221 return key_senderror(so, m, EINVAL);
7222 mhp->msg->sadb_msg_errno = 0;
7223 switch (mhp->msg->sadb_msg_satype) {
7224 case 0:
7225 case 1:
7226 kp->kp_promisc = mhp->msg->sadb_msg_satype;
7227 break;
7228 default:
7229 return key_senderror(so, m, EINVAL);
7230 }
7231
7232 /* send the original message back to everyone */
7233 mhp->msg->sadb_msg_errno = 0;
7234 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
7235 } else {
7236 /* send packet as is */
7237
7238 m_adj(m, PFKEY_ALIGN8(sizeof(struct sadb_msg)));
7239
7240 /* TODO: if sadb_msg_seq is specified, send to specific pid */
7241 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
7242 }
7243 }
7244
7245 static int (*key_typesw[]) (struct socket *, struct mbuf *,
7246 const struct sadb_msghdr *) = {
7247 NULL, /* SADB_RESERVED */
7248 key_getspi, /* SADB_GETSPI */
7249 key_update, /* SADB_UPDATE */
7250 key_add, /* SADB_ADD */
7251 key_delete, /* SADB_DELETE */
7252 key_get, /* SADB_GET */
7253 key_acquire2, /* SADB_ACQUIRE */
7254 key_register, /* SADB_REGISTER */
7255 NULL, /* SADB_EXPIRE */
7256 key_flush, /* SADB_FLUSH */
7257 key_dump, /* SADB_DUMP */
7258 key_promisc, /* SADB_X_PROMISC */
7259 NULL, /* SADB_X_PCHANGE */
7260 key_spdadd, /* SADB_X_SPDUPDATE */
7261 key_spdadd, /* SADB_X_SPDADD */
7262 key_spddelete, /* SADB_X_SPDDELETE */
7263 key_spdget, /* SADB_X_SPDGET */
7264 NULL, /* SADB_X_SPDACQUIRE */
7265 key_spddump, /* SADB_X_SPDDUMP */
7266 key_spdflush, /* SADB_X_SPDFLUSH */
7267 key_spdadd, /* SADB_X_SPDSETIDX */
7268 NULL, /* SADB_X_SPDEXPIRE */
7269 key_spddelete2, /* SADB_X_SPDDELETE2 */
7270 key_nat_map, /* SADB_X_NAT_T_NEW_MAPPING */
7271 };
7272
7273 /*
7274 * parse sadb_msg buffer to process PFKEYv2,
7275 * and create a data to response if needed.
7276 * I think to be dealed with mbuf directly.
7277 * IN:
7278 * msgp : pointer to pointer to a received buffer pulluped.
7279 * This is rewrited to response.
7280 * so : pointer to socket.
7281 * OUT:
7282 * length for buffer to send to user process.
7283 */
7284 int
7285 key_parse(struct mbuf *m, struct socket *so)
7286 {
7287 struct sadb_msg *msg;
7288 struct sadb_msghdr mh;
7289 u_int orglen;
7290 int error;
7291 int target;
7292
7293 KASSERT(m != NULL);
7294 KASSERT(so != NULL);
7295
7296 #if 0 /*kdebug_sadb assumes msg in linear buffer*/
7297 if (KEYDEBUG_ON(KEYDEBUG_KEY_DUMP)) {
7298 IPSECLOG(LOG_DEBUG, "passed sadb_msg\n");
7299 kdebug_sadb(msg);
7300 }
7301 #endif
7302
7303 if (m->m_len < sizeof(struct sadb_msg)) {
7304 m = m_pullup(m, sizeof(struct sadb_msg));
7305 if (!m)
7306 return ENOBUFS;
7307 }
7308 msg = mtod(m, struct sadb_msg *);
7309 orglen = PFKEY_UNUNIT64(msg->sadb_msg_len);
7310 target = KEY_SENDUP_ONE;
7311
7312 if ((m->m_flags & M_PKTHDR) == 0 ||
7313 m->m_pkthdr.len != orglen) {
7314 IPSECLOG(LOG_DEBUG, "invalid message length.\n");
7315 PFKEY_STATINC(PFKEY_STAT_OUT_INVLEN);
7316 error = EINVAL;
7317 goto senderror;
7318 }
7319
7320 if (msg->sadb_msg_version != PF_KEY_V2) {
7321 IPSECLOG(LOG_DEBUG, "PF_KEY version %u is mismatched.\n",
7322 msg->sadb_msg_version);
7323 PFKEY_STATINC(PFKEY_STAT_OUT_INVVER);
7324 error = EINVAL;
7325 goto senderror;
7326 }
7327
7328 if (msg->sadb_msg_type > SADB_MAX) {
7329 IPSECLOG(LOG_DEBUG, "invalid type %u is passed.\n",
7330 msg->sadb_msg_type);
7331 PFKEY_STATINC(PFKEY_STAT_OUT_INVMSGTYPE);
7332 error = EINVAL;
7333 goto senderror;
7334 }
7335
7336 /* for old-fashioned code - should be nuked */
7337 if (m->m_pkthdr.len > MCLBYTES) {
7338 m_freem(m);
7339 return ENOBUFS;
7340 }
7341 if (m->m_next) {
7342 struct mbuf *n;
7343
7344 MGETHDR(n, M_DONTWAIT, MT_DATA);
7345 if (n && m->m_pkthdr.len > MHLEN) {
7346 MCLGET(n, M_DONTWAIT);
7347 if ((n->m_flags & M_EXT) == 0) {
7348 m_free(n);
7349 n = NULL;
7350 }
7351 }
7352 if (!n) {
7353 m_freem(m);
7354 return ENOBUFS;
7355 }
7356 m_copydata(m, 0, m->m_pkthdr.len, mtod(n, void *));
7357 n->m_pkthdr.len = n->m_len = m->m_pkthdr.len;
7358 n->m_next = NULL;
7359 m_freem(m);
7360 m = n;
7361 }
7362
7363 /* align the mbuf chain so that extensions are in contiguous region. */
7364 error = key_align(m, &mh);
7365 if (error)
7366 return error;
7367
7368 if (m->m_next) { /*XXX*/
7369 m_freem(m);
7370 return ENOBUFS;
7371 }
7372
7373 msg = mh.msg;
7374
7375 /* check SA type */
7376 switch (msg->sadb_msg_satype) {
7377 case SADB_SATYPE_UNSPEC:
7378 switch (msg->sadb_msg_type) {
7379 case SADB_GETSPI:
7380 case SADB_UPDATE:
7381 case SADB_ADD:
7382 case SADB_DELETE:
7383 case SADB_GET:
7384 case SADB_ACQUIRE:
7385 case SADB_EXPIRE:
7386 IPSECLOG(LOG_DEBUG,
7387 "must specify satype when msg type=%u.\n",
7388 msg->sadb_msg_type);
7389 PFKEY_STATINC(PFKEY_STAT_OUT_INVSATYPE);
7390 error = EINVAL;
7391 goto senderror;
7392 }
7393 break;
7394 case SADB_SATYPE_AH:
7395 case SADB_SATYPE_ESP:
7396 case SADB_X_SATYPE_IPCOMP:
7397 case SADB_X_SATYPE_TCPSIGNATURE:
7398 switch (msg->sadb_msg_type) {
7399 case SADB_X_SPDADD:
7400 case SADB_X_SPDDELETE:
7401 case SADB_X_SPDGET:
7402 case SADB_X_SPDDUMP:
7403 case SADB_X_SPDFLUSH:
7404 case SADB_X_SPDSETIDX:
7405 case SADB_X_SPDUPDATE:
7406 case SADB_X_SPDDELETE2:
7407 IPSECLOG(LOG_DEBUG, "illegal satype=%u\n",
7408 msg->sadb_msg_type);
7409 PFKEY_STATINC(PFKEY_STAT_OUT_INVSATYPE);
7410 error = EINVAL;
7411 goto senderror;
7412 }
7413 break;
7414 case SADB_SATYPE_RSVP:
7415 case SADB_SATYPE_OSPFV2:
7416 case SADB_SATYPE_RIPV2:
7417 case SADB_SATYPE_MIP:
7418 IPSECLOG(LOG_DEBUG, "type %u isn't supported.\n",
7419 msg->sadb_msg_satype);
7420 PFKEY_STATINC(PFKEY_STAT_OUT_INVSATYPE);
7421 error = EOPNOTSUPP;
7422 goto senderror;
7423 case 1: /* XXX: What does it do? */
7424 if (msg->sadb_msg_type == SADB_X_PROMISC)
7425 break;
7426 /*FALLTHROUGH*/
7427 default:
7428 IPSECLOG(LOG_DEBUG, "invalid type %u is passed.\n",
7429 msg->sadb_msg_satype);
7430 PFKEY_STATINC(PFKEY_STAT_OUT_INVSATYPE);
7431 error = EINVAL;
7432 goto senderror;
7433 }
7434
7435 /* check field of upper layer protocol and address family */
7436 if (mh.ext[SADB_EXT_ADDRESS_SRC] != NULL &&
7437 mh.ext[SADB_EXT_ADDRESS_DST] != NULL) {
7438 struct sadb_address *src0, *dst0;
7439 u_int plen;
7440
7441 src0 = (struct sadb_address *)(mh.ext[SADB_EXT_ADDRESS_SRC]);
7442 dst0 = (struct sadb_address *)(mh.ext[SADB_EXT_ADDRESS_DST]);
7443
7444 /* check upper layer protocol */
7445 if (src0->sadb_address_proto != dst0->sadb_address_proto) {
7446 IPSECLOG(LOG_DEBUG, "upper layer protocol mismatched.\n");
7447 PFKEY_STATINC(PFKEY_STAT_OUT_INVADDR);
7448 error = EINVAL;
7449 goto senderror;
7450 }
7451
7452 /* check family */
7453 if (PFKEY_ADDR_SADDR(src0)->sa_family !=
7454 PFKEY_ADDR_SADDR(dst0)->sa_family) {
7455 IPSECLOG(LOG_DEBUG, "address family mismatched.\n");
7456 PFKEY_STATINC(PFKEY_STAT_OUT_INVADDR);
7457 error = EINVAL;
7458 goto senderror;
7459 }
7460 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
7461 PFKEY_ADDR_SADDR(dst0)->sa_len) {
7462 IPSECLOG(LOG_DEBUG,
7463 "address struct size mismatched.\n");
7464 PFKEY_STATINC(PFKEY_STAT_OUT_INVADDR);
7465 error = EINVAL;
7466 goto senderror;
7467 }
7468
7469 switch (PFKEY_ADDR_SADDR(src0)->sa_family) {
7470 case AF_INET:
7471 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
7472 sizeof(struct sockaddr_in)) {
7473 PFKEY_STATINC(PFKEY_STAT_OUT_INVADDR);
7474 error = EINVAL;
7475 goto senderror;
7476 }
7477 break;
7478 case AF_INET6:
7479 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
7480 sizeof(struct sockaddr_in6)) {
7481 PFKEY_STATINC(PFKEY_STAT_OUT_INVADDR);
7482 error = EINVAL;
7483 goto senderror;
7484 }
7485 break;
7486 default:
7487 IPSECLOG(LOG_DEBUG, "unsupported address family.\n");
7488 PFKEY_STATINC(PFKEY_STAT_OUT_INVADDR);
7489 error = EAFNOSUPPORT;
7490 goto senderror;
7491 }
7492
7493 switch (PFKEY_ADDR_SADDR(src0)->sa_family) {
7494 case AF_INET:
7495 plen = sizeof(struct in_addr) << 3;
7496 break;
7497 case AF_INET6:
7498 plen = sizeof(struct in6_addr) << 3;
7499 break;
7500 default:
7501 plen = 0; /*fool gcc*/
7502 break;
7503 }
7504
7505 /* check max prefix length */
7506 if (src0->sadb_address_prefixlen > plen ||
7507 dst0->sadb_address_prefixlen > plen) {
7508 IPSECLOG(LOG_DEBUG, "illegal prefixlen.\n");
7509 PFKEY_STATINC(PFKEY_STAT_OUT_INVADDR);
7510 error = EINVAL;
7511 goto senderror;
7512 }
7513
7514 /*
7515 * prefixlen == 0 is valid because there can be a case when
7516 * all addresses are matched.
7517 */
7518 }
7519
7520 if (msg->sadb_msg_type >= __arraycount(key_typesw) ||
7521 key_typesw[msg->sadb_msg_type] == NULL) {
7522 PFKEY_STATINC(PFKEY_STAT_OUT_INVMSGTYPE);
7523 error = EINVAL;
7524 goto senderror;
7525 }
7526
7527 return (*key_typesw[msg->sadb_msg_type])(so, m, &mh);
7528
7529 senderror:
7530 msg->sadb_msg_errno = error;
7531 return key_sendup_mbuf(so, m, target);
7532 }
7533
7534 static int
7535 key_senderror(struct socket *so, struct mbuf *m, int code)
7536 {
7537 struct sadb_msg *msg;
7538
7539 KASSERT(m->m_len >= sizeof(struct sadb_msg));
7540
7541 msg = mtod(m, struct sadb_msg *);
7542 msg->sadb_msg_errno = code;
7543 return key_sendup_mbuf(so, m, KEY_SENDUP_ONE);
7544 }
7545
7546 /*
7547 * set the pointer to each header into message buffer.
7548 * m will be freed on error.
7549 * XXX larger-than-MCLBYTES extension?
7550 */
7551 static int
7552 key_align(struct mbuf *m, struct sadb_msghdr *mhp)
7553 {
7554 struct mbuf *n;
7555 struct sadb_ext *ext;
7556 size_t off, end;
7557 int extlen;
7558 int toff;
7559
7560 KASSERT(m != NULL);
7561 KASSERT(mhp != NULL);
7562 KASSERT(m->m_len >= sizeof(struct sadb_msg));
7563
7564 /* initialize */
7565 memset(mhp, 0, sizeof(*mhp));
7566
7567 mhp->msg = mtod(m, struct sadb_msg *);
7568 mhp->ext[0] = (struct sadb_ext *)mhp->msg; /*XXX backward compat */
7569
7570 end = PFKEY_UNUNIT64(mhp->msg->sadb_msg_len);
7571 extlen = end; /*just in case extlen is not updated*/
7572 for (off = sizeof(struct sadb_msg); off < end; off += extlen) {
7573 n = m_pulldown(m, off, sizeof(struct sadb_ext), &toff);
7574 if (!n) {
7575 /* m is already freed */
7576 return ENOBUFS;
7577 }
7578 ext = (struct sadb_ext *)(mtod(n, char *) + toff);
7579
7580 /* set pointer */
7581 switch (ext->sadb_ext_type) {
7582 case SADB_EXT_SA:
7583 case SADB_EXT_ADDRESS_SRC:
7584 case SADB_EXT_ADDRESS_DST:
7585 case SADB_EXT_ADDRESS_PROXY:
7586 case SADB_EXT_LIFETIME_CURRENT:
7587 case SADB_EXT_LIFETIME_HARD:
7588 case SADB_EXT_LIFETIME_SOFT:
7589 case SADB_EXT_KEY_AUTH:
7590 case SADB_EXT_KEY_ENCRYPT:
7591 case SADB_EXT_IDENTITY_SRC:
7592 case SADB_EXT_IDENTITY_DST:
7593 case SADB_EXT_SENSITIVITY:
7594 case SADB_EXT_PROPOSAL:
7595 case SADB_EXT_SUPPORTED_AUTH:
7596 case SADB_EXT_SUPPORTED_ENCRYPT:
7597 case SADB_EXT_SPIRANGE:
7598 case SADB_X_EXT_POLICY:
7599 case SADB_X_EXT_SA2:
7600 case SADB_X_EXT_NAT_T_TYPE:
7601 case SADB_X_EXT_NAT_T_SPORT:
7602 case SADB_X_EXT_NAT_T_DPORT:
7603 case SADB_X_EXT_NAT_T_OAI:
7604 case SADB_X_EXT_NAT_T_OAR:
7605 case SADB_X_EXT_NAT_T_FRAG:
7606 /* duplicate check */
7607 /*
7608 * XXX Are there duplication payloads of either
7609 * KEY_AUTH or KEY_ENCRYPT ?
7610 */
7611 if (mhp->ext[ext->sadb_ext_type] != NULL) {
7612 IPSECLOG(LOG_DEBUG,
7613 "duplicate ext_type %u is passed.\n",
7614 ext->sadb_ext_type);
7615 m_freem(m);
7616 PFKEY_STATINC(PFKEY_STAT_OUT_DUPEXT);
7617 return EINVAL;
7618 }
7619 break;
7620 default:
7621 IPSECLOG(LOG_DEBUG, "invalid ext_type %u is passed.\n",
7622 ext->sadb_ext_type);
7623 m_freem(m);
7624 PFKEY_STATINC(PFKEY_STAT_OUT_INVEXTTYPE);
7625 return EINVAL;
7626 }
7627
7628 extlen = PFKEY_UNUNIT64(ext->sadb_ext_len);
7629
7630 if (key_validate_ext(ext, extlen)) {
7631 m_freem(m);
7632 PFKEY_STATINC(PFKEY_STAT_OUT_INVLEN);
7633 return EINVAL;
7634 }
7635
7636 n = m_pulldown(m, off, extlen, &toff);
7637 if (!n) {
7638 /* m is already freed */
7639 return ENOBUFS;
7640 }
7641 ext = (struct sadb_ext *)(mtod(n, char *) + toff);
7642
7643 mhp->ext[ext->sadb_ext_type] = ext;
7644 mhp->extoff[ext->sadb_ext_type] = off;
7645 mhp->extlen[ext->sadb_ext_type] = extlen;
7646 }
7647
7648 if (off != end) {
7649 m_freem(m);
7650 PFKEY_STATINC(PFKEY_STAT_OUT_INVLEN);
7651 return EINVAL;
7652 }
7653
7654 return 0;
7655 }
7656
7657 static int
7658 key_validate_ext(const struct sadb_ext *ext, int len)
7659 {
7660 const struct sockaddr *sa;
7661 enum { NONE, ADDR } checktype = NONE;
7662 int baselen = 0;
7663 const int sal = offsetof(struct sockaddr, sa_len) + sizeof(sa->sa_len);
7664
7665 if (len != PFKEY_UNUNIT64(ext->sadb_ext_len))
7666 return EINVAL;
7667
7668 /* if it does not match minimum/maximum length, bail */
7669 if (ext->sadb_ext_type >= __arraycount(minsize) ||
7670 ext->sadb_ext_type >= __arraycount(maxsize))
7671 return EINVAL;
7672 if (!minsize[ext->sadb_ext_type] || len < minsize[ext->sadb_ext_type])
7673 return EINVAL;
7674 if (maxsize[ext->sadb_ext_type] && len > maxsize[ext->sadb_ext_type])
7675 return EINVAL;
7676
7677 /* more checks based on sadb_ext_type XXX need more */
7678 switch (ext->sadb_ext_type) {
7679 case SADB_EXT_ADDRESS_SRC:
7680 case SADB_EXT_ADDRESS_DST:
7681 case SADB_EXT_ADDRESS_PROXY:
7682 baselen = PFKEY_ALIGN8(sizeof(struct sadb_address));
7683 checktype = ADDR;
7684 break;
7685 case SADB_EXT_IDENTITY_SRC:
7686 case SADB_EXT_IDENTITY_DST:
7687 if (((const struct sadb_ident *)ext)->sadb_ident_type ==
7688 SADB_X_IDENTTYPE_ADDR) {
7689 baselen = PFKEY_ALIGN8(sizeof(struct sadb_ident));
7690 checktype = ADDR;
7691 } else
7692 checktype = NONE;
7693 break;
7694 default:
7695 checktype = NONE;
7696 break;
7697 }
7698
7699 switch (checktype) {
7700 case NONE:
7701 break;
7702 case ADDR:
7703 sa = (const struct sockaddr *)(((const u_int8_t*)ext)+baselen);
7704 if (len < baselen + sal)
7705 return EINVAL;
7706 if (baselen + PFKEY_ALIGN8(sa->sa_len) != len)
7707 return EINVAL;
7708 break;
7709 }
7710
7711 return 0;
7712 }
7713
7714 static int
7715 key_do_init(void)
7716 {
7717 int i, error;
7718
7719 mutex_init(&key_mtx, MUTEX_DEFAULT, IPL_NONE);
7720
7721 pfkeystat_percpu = percpu_alloc(sizeof(uint64_t) * PFKEY_NSTATS);
7722
7723 callout_init(&key_timehandler_ch, 0);
7724 error = workqueue_create(&key_timehandler_wq, "key_timehandler",
7725 key_timehandler_work, NULL, PRI_SOFTNET, IPL_SOFTNET, WQ_MPSAFE);
7726 if (error != 0)
7727 panic("%s: workqueue_create failed (%d)\n", __func__, error);
7728
7729 for (i = 0; i < IPSEC_DIR_MAX; i++) {
7730 LIST_INIT(&sptree[i]);
7731 }
7732
7733 LIST_INIT(&sahtree);
7734
7735 for (i = 0; i <= SADB_SATYPE_MAX; i++) {
7736 LIST_INIT(®tree[i]);
7737 }
7738
7739 #ifndef IPSEC_NONBLOCK_ACQUIRE
7740 LIST_INIT(&acqtree);
7741 #endif
7742 #ifdef notyet
7743 LIST_INIT(&spacqtree);
7744 #endif
7745
7746 /* system default */
7747 ip4_def_policy.policy = IPSEC_POLICY_NONE;
7748 ip4_def_policy.refcnt++; /*never reclaim this*/
7749
7750 #ifdef INET6
7751 ip6_def_policy.policy = IPSEC_POLICY_NONE;
7752 ip6_def_policy.refcnt++; /*never reclaim this*/
7753 #endif
7754
7755 callout_reset(&key_timehandler_ch, hz, key_timehandler, NULL);
7756
7757 /* initialize key statistics */
7758 keystat.getspi_count = 1;
7759
7760 aprint_verbose("IPsec: Initialized Security Association Processing.\n");
7761
7762 return (0);
7763 }
7764
7765 void
7766 key_init(void)
7767 {
7768 static ONCE_DECL(key_init_once);
7769
7770 sysctl_net_keyv2_setup(NULL);
7771 sysctl_net_key_compat_setup(NULL);
7772
7773 RUN_ONCE(&key_init_once, key_do_init);
7774 }
7775
7776 /*
7777 * XXX: maybe This function is called after INBOUND IPsec processing.
7778 *
7779 * Special check for tunnel-mode packets.
7780 * We must make some checks for consistency between inner and outer IP header.
7781 *
7782 * xxx more checks to be provided
7783 */
7784 int
7785 key_checktunnelsanity(
7786 struct secasvar *sav,
7787 u_int family,
7788 void *src,
7789 void *dst
7790 )
7791 {
7792
7793 KASSERT(sav->sah != NULL);
7794
7795 /* XXX: check inner IP header */
7796
7797 return 1;
7798 }
7799
7800 #if 0
7801 #define hostnamelen strlen(hostname)
7802
7803 /*
7804 * Get FQDN for the host.
7805 * If the administrator configured hostname (by hostname(1)) without
7806 * domain name, returns nothing.
7807 */
7808 static const char *
7809 key_getfqdn(void)
7810 {
7811 int i;
7812 int hasdot;
7813 static char fqdn[MAXHOSTNAMELEN + 1];
7814
7815 if (!hostnamelen)
7816 return NULL;
7817
7818 /* check if it comes with domain name. */
7819 hasdot = 0;
7820 for (i = 0; i < hostnamelen; i++) {
7821 if (hostname[i] == '.')
7822 hasdot++;
7823 }
7824 if (!hasdot)
7825 return NULL;
7826
7827 /* NOTE: hostname may not be NUL-terminated. */
7828 memset(fqdn, 0, sizeof(fqdn));
7829 memcpy(fqdn, hostname, hostnamelen);
7830 fqdn[hostnamelen] = '\0';
7831 return fqdn;
7832 }
7833
7834 /*
7835 * get username@FQDN for the host/user.
7836 */
7837 static const char *
7838 key_getuserfqdn(void)
7839 {
7840 const char *host;
7841 static char userfqdn[MAXHOSTNAMELEN + MAXLOGNAME + 2];
7842 struct proc *p = curproc;
7843 char *q;
7844
7845 if (!p || !p->p_pgrp || !p->p_pgrp->pg_session)
7846 return NULL;
7847 if (!(host = key_getfqdn()))
7848 return NULL;
7849
7850 /* NOTE: s_login may not be-NUL terminated. */
7851 memset(userfqdn, 0, sizeof(userfqdn));
7852 memcpy(userfqdn, Mp->p_pgrp->pg_session->s_login, AXLOGNAME);
7853 userfqdn[MAXLOGNAME] = '\0'; /* safeguard */
7854 q = userfqdn + strlen(userfqdn);
7855 *q++ = '@';
7856 memcpy(q, host, strlen(host));
7857 q += strlen(host);
7858 *q++ = '\0';
7859
7860 return userfqdn;
7861 }
7862 #endif
7863
7864 /* record data transfer on SA, and update timestamps */
7865 void
7866 key_sa_recordxfer(struct secasvar *sav, struct mbuf *m)
7867 {
7868
7869 KASSERT(sav != NULL);
7870 KASSERT(m != NULL);
7871 if (!sav->lft_c)
7872 return;
7873
7874 /*
7875 * XXX Currently, there is a difference of bytes size
7876 * between inbound and outbound processing.
7877 */
7878 sav->lft_c->sadb_lifetime_bytes += m->m_pkthdr.len;
7879 /* to check bytes lifetime is done in key_timehandler(). */
7880
7881 /*
7882 * We use the number of packets as the unit of
7883 * sadb_lifetime_allocations. We increment the variable
7884 * whenever {esp,ah}_{in,out}put is called.
7885 */
7886 sav->lft_c->sadb_lifetime_allocations++;
7887 /* XXX check for expires? */
7888
7889 /*
7890 * NOTE: We record CURRENT sadb_lifetime_usetime by using wall clock,
7891 * in seconds. HARD and SOFT lifetime are measured by the time
7892 * difference (again in seconds) from sadb_lifetime_usetime.
7893 *
7894 * usetime
7895 * v expire expire
7896 * -----+-----+--------+---> t
7897 * <--------------> HARD
7898 * <-----> SOFT
7899 */
7900 sav->lft_c->sadb_lifetime_usetime = time_uptime;
7901 /* XXX check for expires? */
7902
7903 return;
7904 }
7905
7906 /* dumb version */
7907 void
7908 key_sa_routechange(struct sockaddr *dst)
7909 {
7910 struct secashead *sah;
7911 struct route *ro;
7912 const struct sockaddr *sa;
7913
7914 LIST_FOREACH(sah, &sahtree, chain) {
7915 ro = &sah->sa_route;
7916 sa = rtcache_getdst(ro);
7917 if (sa != NULL && dst->sa_len == sa->sa_len &&
7918 memcmp(dst, sa, dst->sa_len) == 0)
7919 rtcache_free(ro);
7920 }
7921
7922 return;
7923 }
7924
7925 static void
7926 key_sa_chgstate(struct secasvar *sav, u_int8_t state)
7927 {
7928
7929 KASSERT(sav != NULL);
7930
7931 if (sav->state == state)
7932 return;
7933
7934 KASSERT(__LIST_CHAINED(sav));
7935 LIST_REMOVE(sav, chain);
7936
7937 sav->state = state;
7938 LIST_INSERT_HEAD(&sav->sah->savtree[state], sav, chain);
7939 }
7940
7941 /* XXX too much? */
7942 static struct mbuf *
7943 key_alloc_mbuf(int l)
7944 {
7945 struct mbuf *m = NULL, *n;
7946 int len, t;
7947
7948 len = l;
7949 while (len > 0) {
7950 MGET(n, M_DONTWAIT, MT_DATA);
7951 if (n && len > MLEN)
7952 MCLGET(n, M_DONTWAIT);
7953 if (!n) {
7954 m_freem(m);
7955 return NULL;
7956 }
7957
7958 n->m_next = NULL;
7959 n->m_len = 0;
7960 n->m_len = M_TRAILINGSPACE(n);
7961 /* use the bottom of mbuf, hoping we can prepend afterwards */
7962 if (n->m_len > len) {
7963 t = (n->m_len - len) & ~(sizeof(long) - 1);
7964 n->m_data += t;
7965 n->m_len = len;
7966 }
7967
7968 len -= n->m_len;
7969
7970 if (m)
7971 m_cat(m, n);
7972 else
7973 m = n;
7974 }
7975
7976 return m;
7977 }
7978
7979 static struct mbuf *
7980 key_setdump(u_int8_t req_satype, int *errorp, uint32_t pid)
7981 {
7982 struct secashead *sah;
7983 struct secasvar *sav;
7984 u_int16_t proto;
7985 u_int8_t satype;
7986 u_int8_t state;
7987 int cnt;
7988 struct mbuf *m, *n;
7989
7990 /* map satype to proto */
7991 proto = key_satype2proto(req_satype);
7992 if (proto == 0) {
7993 *errorp = EINVAL;
7994 return (NULL);
7995 }
7996
7997 /* count sav entries to be sent to the userland. */
7998 cnt = 0;
7999 LIST_FOREACH(sah, &sahtree, chain) {
8000 if (req_satype != SADB_SATYPE_UNSPEC &&
8001 proto != sah->saidx.proto)
8002 continue;
8003
8004 SASTATE_ANY_FOREACH(state) {
8005 LIST_FOREACH(sav, &sah->savtree[state], chain) {
8006 cnt++;
8007 }
8008 }
8009 }
8010
8011 if (cnt == 0) {
8012 *errorp = ENOENT;
8013 return (NULL);
8014 }
8015
8016 /* send this to the userland, one at a time. */
8017 m = NULL;
8018 LIST_FOREACH(sah, &sahtree, chain) {
8019 if (req_satype != SADB_SATYPE_UNSPEC &&
8020 proto != sah->saidx.proto)
8021 continue;
8022
8023 /* map proto to satype */
8024 satype = key_proto2satype(sah->saidx.proto);
8025 if (satype == 0) {
8026 m_freem(m);
8027 *errorp = EINVAL;
8028 return (NULL);
8029 }
8030
8031 SASTATE_ANY_FOREACH(state) {
8032 LIST_FOREACH(sav, &sah->savtree[state], chain) {
8033 n = key_setdumpsa(sav, SADB_DUMP, satype,
8034 --cnt, pid);
8035 if (!n) {
8036 m_freem(m);
8037 *errorp = ENOBUFS;
8038 return (NULL);
8039 }
8040
8041 if (!m)
8042 m = n;
8043 else
8044 m_cat(m, n);
8045 }
8046 }
8047 }
8048
8049 if (!m) {
8050 *errorp = EINVAL;
8051 return (NULL);
8052 }
8053
8054 if ((m->m_flags & M_PKTHDR) != 0) {
8055 m->m_pkthdr.len = 0;
8056 for (n = m; n; n = n->m_next)
8057 m->m_pkthdr.len += n->m_len;
8058 }
8059
8060 *errorp = 0;
8061 return (m);
8062 }
8063
8064 static struct mbuf *
8065 key_setspddump(int *errorp, pid_t pid)
8066 {
8067 struct secpolicy *sp;
8068 int cnt;
8069 u_int dir;
8070 struct mbuf *m, *n;
8071
8072 /* search SPD entry and get buffer size. */
8073 cnt = 0;
8074 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
8075 LIST_FOREACH(sp, &sptree[dir], chain) {
8076 cnt++;
8077 }
8078 }
8079
8080 if (cnt == 0) {
8081 *errorp = ENOENT;
8082 return (NULL);
8083 }
8084
8085 m = NULL;
8086 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
8087 LIST_FOREACH(sp, &sptree[dir], chain) {
8088 --cnt;
8089 n = key_setdumpsp(sp, SADB_X_SPDDUMP, cnt, pid);
8090
8091 if (!n) {
8092 *errorp = ENOBUFS;
8093 m_freem(m);
8094 return (NULL);
8095 }
8096 if (!m)
8097 m = n;
8098 else {
8099 m->m_pkthdr.len += n->m_pkthdr.len;
8100 m_cat(m, n);
8101 }
8102 }
8103 }
8104
8105 *errorp = 0;
8106 return (m);
8107 }
8108
8109 int
8110 key_get_used(void) {
8111 return !LIST_EMPTY(&sptree[IPSEC_DIR_INBOUND]) ||
8112 !LIST_EMPTY(&sptree[IPSEC_DIR_OUTBOUND]);
8113 }
8114
8115 void
8116 key_update_used(void)
8117 {
8118 switch (ipsec_enabled) {
8119 default:
8120 case 0:
8121 #ifdef notyet
8122 /* XXX: racy */
8123 ipsec_used = 0;
8124 #endif
8125 break;
8126 case 1:
8127 #ifndef notyet
8128 /* XXX: racy */
8129 if (!ipsec_used)
8130 #endif
8131 ipsec_used = key_get_used();
8132 break;
8133 case 2:
8134 ipsec_used = 1;
8135 break;
8136 }
8137 }
8138
8139 static int
8140 sysctl_net_key_dumpsa(SYSCTLFN_ARGS)
8141 {
8142 struct mbuf *m, *n;
8143 int err2 = 0;
8144 char *p, *ep;
8145 size_t len;
8146 int s, error;
8147
8148 if (newp)
8149 return (EPERM);
8150 if (namelen != 1)
8151 return (EINVAL);
8152
8153 s = splsoftnet();
8154 m = key_setdump(name[0], &error, l->l_proc->p_pid);
8155 splx(s);
8156 if (!m)
8157 return (error);
8158 if (!oldp)
8159 *oldlenp = m->m_pkthdr.len;
8160 else {
8161 p = oldp;
8162 if (*oldlenp < m->m_pkthdr.len) {
8163 err2 = ENOMEM;
8164 ep = p + *oldlenp;
8165 } else {
8166 *oldlenp = m->m_pkthdr.len;
8167 ep = p + m->m_pkthdr.len;
8168 }
8169 for (n = m; n; n = n->m_next) {
8170 len = (ep - p < n->m_len) ?
8171 ep - p : n->m_len;
8172 error = copyout(mtod(n, const void *), p, len);
8173 p += len;
8174 if (error)
8175 break;
8176 }
8177 if (error == 0)
8178 error = err2;
8179 }
8180 m_freem(m);
8181
8182 return (error);
8183 }
8184
8185 static int
8186 sysctl_net_key_dumpsp(SYSCTLFN_ARGS)
8187 {
8188 struct mbuf *m, *n;
8189 int err2 = 0;
8190 char *p, *ep;
8191 size_t len;
8192 int s, error;
8193
8194 if (newp)
8195 return (EPERM);
8196 if (namelen != 0)
8197 return (EINVAL);
8198
8199 s = splsoftnet();
8200 m = key_setspddump(&error, l->l_proc->p_pid);
8201 splx(s);
8202 if (!m)
8203 return (error);
8204 if (!oldp)
8205 *oldlenp = m->m_pkthdr.len;
8206 else {
8207 p = oldp;
8208 if (*oldlenp < m->m_pkthdr.len) {
8209 err2 = ENOMEM;
8210 ep = p + *oldlenp;
8211 } else {
8212 *oldlenp = m->m_pkthdr.len;
8213 ep = p + m->m_pkthdr.len;
8214 }
8215 for (n = m; n; n = n->m_next) {
8216 len = (ep - p < n->m_len) ? ep - p : n->m_len;
8217 error = copyout(mtod(n, const void *), p, len);
8218 p += len;
8219 if (error)
8220 break;
8221 }
8222 if (error == 0)
8223 error = err2;
8224 }
8225 m_freem(m);
8226
8227 return (error);
8228 }
8229
8230 /*
8231 * Create sysctl tree for native IPSEC key knobs, originally
8232 * under name "net.keyv2" * with MIB number { CTL_NET, PF_KEY_V2. }.
8233 * However, sysctl(8) never checked for nodes under { CTL_NET, PF_KEY_V2 };
8234 * and in any case the part of our sysctl namespace used for dumping the
8235 * SPD and SA database *HAS* to be compatible with the KAME sysctl
8236 * namespace, for API reasons.
8237 *
8238 * Pending a consensus on the right way to fix this, add a level of
8239 * indirection in how we number the `native' IPSEC key nodes;
8240 * and (as requested by Andrew Brown) move registration of the
8241 * KAME-compatible names to a separate function.
8242 */
8243 #if 0
8244 # define IPSEC_PFKEY PF_KEY_V2
8245 # define IPSEC_PFKEY_NAME "keyv2"
8246 #else
8247 # define IPSEC_PFKEY PF_KEY
8248 # define IPSEC_PFKEY_NAME "key"
8249 #endif
8250
8251 static int
8252 sysctl_net_key_stats(SYSCTLFN_ARGS)
8253 {
8254
8255 return (NETSTAT_SYSCTL(pfkeystat_percpu, PFKEY_NSTATS));
8256 }
8257
8258 static void
8259 sysctl_net_keyv2_setup(struct sysctllog **clog)
8260 {
8261
8262 sysctl_createv(clog, 0, NULL, NULL,
8263 CTLFLAG_PERMANENT,
8264 CTLTYPE_NODE, IPSEC_PFKEY_NAME, NULL,
8265 NULL, 0, NULL, 0,
8266 CTL_NET, IPSEC_PFKEY, CTL_EOL);
8267
8268 sysctl_createv(clog, 0, NULL, NULL,
8269 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
8270 CTLTYPE_INT, "debug", NULL,
8271 NULL, 0, &key_debug_level, 0,
8272 CTL_NET, IPSEC_PFKEY, KEYCTL_DEBUG_LEVEL, CTL_EOL);
8273 sysctl_createv(clog, 0, NULL, NULL,
8274 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
8275 CTLTYPE_INT, "spi_try", NULL,
8276 NULL, 0, &key_spi_trycnt, 0,
8277 CTL_NET, IPSEC_PFKEY, KEYCTL_SPI_TRY, CTL_EOL);
8278 sysctl_createv(clog, 0, NULL, NULL,
8279 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
8280 CTLTYPE_INT, "spi_min_value", NULL,
8281 NULL, 0, &key_spi_minval, 0,
8282 CTL_NET, IPSEC_PFKEY, KEYCTL_SPI_MIN_VALUE, CTL_EOL);
8283 sysctl_createv(clog, 0, NULL, NULL,
8284 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
8285 CTLTYPE_INT, "spi_max_value", NULL,
8286 NULL, 0, &key_spi_maxval, 0,
8287 CTL_NET, IPSEC_PFKEY, KEYCTL_SPI_MAX_VALUE, CTL_EOL);
8288 sysctl_createv(clog, 0, NULL, NULL,
8289 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
8290 CTLTYPE_INT, "random_int", NULL,
8291 NULL, 0, &key_int_random, 0,
8292 CTL_NET, IPSEC_PFKEY, KEYCTL_RANDOM_INT, CTL_EOL);
8293 sysctl_createv(clog, 0, NULL, NULL,
8294 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
8295 CTLTYPE_INT, "larval_lifetime", NULL,
8296 NULL, 0, &key_larval_lifetime, 0,
8297 CTL_NET, IPSEC_PFKEY, KEYCTL_LARVAL_LIFETIME, CTL_EOL);
8298 sysctl_createv(clog, 0, NULL, NULL,
8299 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
8300 CTLTYPE_INT, "blockacq_count", NULL,
8301 NULL, 0, &key_blockacq_count, 0,
8302 CTL_NET, IPSEC_PFKEY, KEYCTL_BLOCKACQ_COUNT, CTL_EOL);
8303 sysctl_createv(clog, 0, NULL, NULL,
8304 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
8305 CTLTYPE_INT, "blockacq_lifetime", NULL,
8306 NULL, 0, &key_blockacq_lifetime, 0,
8307 CTL_NET, IPSEC_PFKEY, KEYCTL_BLOCKACQ_LIFETIME, CTL_EOL);
8308 sysctl_createv(clog, 0, NULL, NULL,
8309 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
8310 CTLTYPE_INT, "esp_keymin", NULL,
8311 NULL, 0, &ipsec_esp_keymin, 0,
8312 CTL_NET, IPSEC_PFKEY, KEYCTL_ESP_KEYMIN, CTL_EOL);
8313 sysctl_createv(clog, 0, NULL, NULL,
8314 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
8315 CTLTYPE_INT, "prefered_oldsa", NULL,
8316 NULL, 0, &key_prefered_oldsa, 0,
8317 CTL_NET, PF_KEY, KEYCTL_PREFERED_OLDSA, CTL_EOL);
8318 sysctl_createv(clog, 0, NULL, NULL,
8319 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
8320 CTLTYPE_INT, "esp_auth", NULL,
8321 NULL, 0, &ipsec_esp_auth, 0,
8322 CTL_NET, IPSEC_PFKEY, KEYCTL_ESP_AUTH, CTL_EOL);
8323 sysctl_createv(clog, 0, NULL, NULL,
8324 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
8325 CTLTYPE_INT, "ah_keymin", NULL,
8326 NULL, 0, &ipsec_ah_keymin, 0,
8327 CTL_NET, IPSEC_PFKEY, KEYCTL_AH_KEYMIN, CTL_EOL);
8328 sysctl_createv(clog, 0, NULL, NULL,
8329 CTLFLAG_PERMANENT,
8330 CTLTYPE_STRUCT, "stats",
8331 SYSCTL_DESCR("PF_KEY statistics"),
8332 sysctl_net_key_stats, 0, NULL, 0,
8333 CTL_NET, IPSEC_PFKEY, CTL_CREATE, CTL_EOL);
8334 }
8335
8336 /*
8337 * Register sysctl names used by setkey(8). For historical reasons,
8338 * and to share a single API, these names appear under { CTL_NET, PF_KEY }
8339 * for both IPSEC and KAME IPSEC.
8340 */
8341 static void
8342 sysctl_net_key_compat_setup(struct sysctllog **clog)
8343 {
8344
8345 sysctl_createv(clog, 0, NULL, NULL,
8346 CTLFLAG_PERMANENT,
8347 CTLTYPE_NODE, "key", NULL,
8348 NULL, 0, NULL, 0,
8349 CTL_NET, PF_KEY, CTL_EOL);
8350
8351 /* Register the net.key.dump{sa,sp} nodes used by setkey(8). */
8352 sysctl_createv(clog, 0, NULL, NULL,
8353 CTLFLAG_PERMANENT,
8354 CTLTYPE_STRUCT, "dumpsa", NULL,
8355 sysctl_net_key_dumpsa, 0, NULL, 0,
8356 CTL_NET, PF_KEY, KEYCTL_DUMPSA, CTL_EOL);
8357 sysctl_createv(clog, 0, NULL, NULL,
8358 CTLFLAG_PERMANENT,
8359 CTLTYPE_STRUCT, "dumpsp", NULL,
8360 sysctl_net_key_dumpsp, 0, NULL, 0,
8361 CTL_NET, PF_KEY, KEYCTL_DUMPSP, CTL_EOL);
8362 }
8363