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