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