1 /* $NetBSD: key.c,v 1.174 2009/04/18 14:58:06 tsutsui Exp $ */
2 /* $KAME: key.c,v 1.310 2003/09/08 02:23:44 itojun Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
34 * This code is referred to RFC 2367
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: key.c,v 1.174 2009/04/18 14:58:06 tsutsui Exp $");
41 #include "opt_ipsec.h"
42 #include "fs_kernfs.h"
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/callout.h>
47 #include <sys/kernel.h>
49 #include <sys/domain.h>
50 #include <sys/protosw.h>
51 #include <sys/malloc.h>
52 #include <sys/socket.h>
53 #include <sys/socketvar.h>
54 #include <sys/errno.h>
56 #include <sys/queue.h>
57 #include <sys/sysctl.h>
58 #include <sys/syslog.h>
62 #include <net/route.h>
63 #include <net/raw_cb.h>
65 #include <netinet/in.h>
66 #include <netinet/in_systm.h>
67 #include <netinet/ip.h>
68 #include <netinet/in_var.h>
71 #include <netinet/ip6.h>
72 #include <netinet6/in6_var.h>
73 #include <netinet6/ip6_var.h>
74 #include <netinet6/scope6_var.h>
78 #include <netinet/in_pcb.h>
81 #include <netinet6/in6_pcb.h>
84 #include <net/pfkeyv2.h>
85 #include <netkey/keydb.h>
86 #include <netkey/key.h>
87 #include <netkey/keysock.h>
88 #include <netkey/key_debug.h>
89 #include <netkey/key_private.h>
91 #include <netinet6/ipsec.h>
92 #include <netinet6/ah.h>
94 #include <netinet6/esp.h>
96 #include <netinet6/ipcomp.h>
99 #include <miscfs/kernfs/kernfs.h>
102 #include <machine/stdarg.h>
109 #include <net/net_osdep.h>
112 #define offsetof(type, member) ((size_t)(&((type *)0)->member))
115 #define satosin(s) ((struct sockaddr_in *)s)
118 #define FULLMASK 0xff
120 percpu_t
*pfkeystat_percpu
;
123 * Note on SA reference counting:
124 * - SAs that are not in DEAD state will have (total external reference + 1)
125 * following value in reference count field. they cannot be freed and are
126 * referenced from SA header.
127 * - SAs that are in DEAD state will have (total external reference)
128 * in reference count field. they are ready to be freed. reference from
129 * SA header will be removed in keydb_delsecasvar(), when the reference count
130 * field hits 0 (= no external reference other than from SA header.
133 u_int32_t key_debug_level
= 0;
134 static u_int key_spi_trycnt
= 1000;
135 static u_int32_t key_spi_minval
= 0x100;
136 static u_int32_t key_spi_maxval
= 0x0fffffff; /* XXX */
137 static u_int key_larval_lifetime
= 30; /* interval to expire acquiring, 30(s)*/
138 static int key_blockacq_count
= 10; /* counter for blocking SADB_ACQUIRE.*/
139 static int key_blockacq_lifetime
= 20; /* lifetime for blocking SADB_ACQUIRE.*/
141 static u_int32_t acq_seq
= 0;
143 struct _satailq satailq
; /* list of all SAD entry */
144 struct _sptailq sptailq
; /* SPD table + pcb */
145 static LIST_HEAD(_sptree
, secpolicy
) sptree
[IPSEC_DIR_MAX
]; /* SPD table */
146 static LIST_HEAD(_sahtree
, secashead
) sahtree
; /* SAD */
147 static LIST_HEAD(_regtree
, secreg
) regtree
[SADB_SATYPE_MAX
+ 1];
150 #define SPIHASHSIZE 128
151 #define SPIHASH(x) (((x) ^ ((x) >> 16)) % SPIHASHSIZE)
152 static LIST_HEAD(_spihash
, secasvar
) spihash
[SPIHASHSIZE
];
154 #ifndef IPSEC_NONBLOCK_ACQUIRE
155 static LIST_HEAD(_acqtree
, secacq
) acqtree
; /* acquiring list */
157 static LIST_HEAD(_spacqtree
, secspacq
) spacqtree
; /* SP acquiring list */
159 struct key_cb key_cb
;
161 /* search order for SAs */
162 static const u_int saorder_state_valid
[] = {
163 SADB_SASTATE_DYING
, SADB_SASTATE_MATURE
,
165 * This order is important because we must select a oldest SA
166 * for outbound processing. For inbound, This is not important.
169 static const u_int saorder_state_alive
[] = {
171 SADB_SASTATE_MATURE
, SADB_SASTATE_DYING
, SADB_SASTATE_LARVAL
173 static const u_int saorder_state_any
[] = {
174 SADB_SASTATE_MATURE
, SADB_SASTATE_DYING
,
175 SADB_SASTATE_LARVAL
, SADB_SASTATE_DEAD
178 static const int minsize
[] = {
179 sizeof(struct sadb_msg
), /* SADB_EXT_RESERVED */
180 sizeof(struct sadb_sa
), /* SADB_EXT_SA */
181 sizeof(struct sadb_lifetime
), /* SADB_EXT_LIFETIME_CURRENT */
182 sizeof(struct sadb_lifetime
), /* SADB_EXT_LIFETIME_HARD */
183 sizeof(struct sadb_lifetime
), /* SADB_EXT_LIFETIME_SOFT */
184 sizeof(struct sadb_address
), /* SADB_EXT_ADDRESS_SRC */
185 sizeof(struct sadb_address
), /* SADB_EXT_ADDRESS_DST */
186 sizeof(struct sadb_address
), /* SADB_EXT_ADDRESS_PROXY */
187 sizeof(struct sadb_key
), /* SADB_EXT_KEY_AUTH */
188 sizeof(struct sadb_key
), /* SADB_EXT_KEY_ENCRYPT */
189 sizeof(struct sadb_ident
), /* SADB_EXT_IDENTITY_SRC */
190 sizeof(struct sadb_ident
), /* SADB_EXT_IDENTITY_DST */
191 sizeof(struct sadb_sens
), /* SADB_EXT_SENSITIVITY */
192 sizeof(struct sadb_prop
), /* SADB_EXT_PROPOSAL */
193 sizeof(struct sadb_supported
), /* SADB_EXT_SUPPORTED_AUTH */
194 sizeof(struct sadb_supported
), /* SADB_EXT_SUPPORTED_ENCRYPT */
195 sizeof(struct sadb_spirange
), /* SADB_EXT_SPIRANGE */
196 0, /* SADB_X_EXT_KMPRIVATE */
197 sizeof(struct sadb_x_policy
), /* SADB_X_EXT_POLICY */
198 sizeof(struct sadb_x_sa2
), /* SADB_X_SA2 */
199 sizeof(struct sadb_x_nat_t_type
), /* SADB_X_EXT_NAT_T_TYPE */
200 sizeof(struct sadb_x_nat_t_port
), /* SADB_X_EXT_NAT_T_SPORT */
201 sizeof(struct sadb_x_nat_t_port
), /* SADB_X_EXT_NAT_T_DPORT */
202 sizeof(struct sadb_address
), /* SADB_X_EXT_NAT_T_OA */
203 sizeof(struct sadb_x_nat_t_frag
),/* SADB_X_EXT_NAT_T_FRAG */
204 #ifdef SADB_X_EXT_TAG
205 sizeof(struct sadb_x_tag
), /* SADB_X_TAG */
208 static const int maxsize
[] = {
209 sizeof(struct sadb_msg
), /* SADB_EXT_RESERVED */
210 sizeof(struct sadb_sa
), /* SADB_EXT_SA */
211 sizeof(struct sadb_lifetime
), /* SADB_EXT_LIFETIME_CURRENT */
212 sizeof(struct sadb_lifetime
), /* SADB_EXT_LIFETIME_HARD */
213 sizeof(struct sadb_lifetime
), /* SADB_EXT_LIFETIME_SOFT */
214 0, /* SADB_EXT_ADDRESS_SRC */
215 0, /* SADB_EXT_ADDRESS_DST */
216 0, /* SADB_EXT_ADDRESS_PROXY */
217 0, /* SADB_EXT_KEY_AUTH */
218 0, /* SADB_EXT_KEY_ENCRYPT */
219 0, /* SADB_EXT_IDENTITY_SRC */
220 0, /* SADB_EXT_IDENTITY_DST */
221 0, /* SADB_EXT_SENSITIVITY */
222 0, /* SADB_EXT_PROPOSAL */
223 0, /* SADB_EXT_SUPPORTED_AUTH */
224 0, /* SADB_EXT_SUPPORTED_ENCRYPT */
225 sizeof(struct sadb_spirange
), /* SADB_EXT_SPIRANGE */
226 0, /* SADB_X_EXT_KMPRIVATE */
227 0, /* SADB_X_EXT_POLICY */
228 sizeof(struct sadb_x_sa2
), /* SADB_X_SA2 */
229 sizeof(struct sadb_x_nat_t_type
), /* SADB_X_EXT_NAT_T_TYPE */
230 sizeof(struct sadb_x_nat_t_port
), /* SADB_X_EXT_NAT_T_SPORT */
231 sizeof(struct sadb_x_nat_t_port
), /* SADB_X_EXT_NAT_T_DPORT */
232 0, /* SADB_X_EXT_NAT_T_OA */
233 sizeof(struct sadb_x_nat_t_frag
), /* SADB_X_EXT_NAT_T_FRAG */
234 #ifdef SADB_X_EXT_TAG
235 sizeof(struct sadb_x_tag
), /* SADB_X_TAG */
239 static int ipsec_esp_keymin
= 256;
240 static int ipsec_esp_auth
= 0;
241 static int ipsec_ah_keymin
= 128;
243 #define __LIST_CHAINED(elm) \
244 (!((elm)->chain.le_next == NULL && (elm)->chain.le_prev == NULL))
245 #define LIST_INSERT_TAIL(head, elm, type, field) \
247 struct type *curelm = LIST_FIRST(head); \
248 if (curelm == NULL) {\
249 LIST_INSERT_HEAD(head, elm, field); \
251 while (LIST_NEXT(curelm, field)) \
252 curelm = LIST_NEXT(curelm, field);\
253 LIST_INSERT_AFTER(curelm, elm, field);\
255 } while (/*CONSTCOND*/ 0)
257 #define KEY_CHKSASTATE(head, sav, name) \
259 if ((head) != (sav)) { \
260 ipseclog((LOG_DEBUG, "%s: state mismatched (TREE=%u SA=%u)\n", \
261 (name), (head), (sav))); \
264 } // while (/*CONSTCOND*/ 0)
266 #define KEY_CHKSPDIR(head, sp, name) \
268 if ((head) != (sp)) { \
269 ipseclog((LOG_DEBUG, "%s: direction mismatched (TREE=%u SP=%u), " \
270 "anyway continue.\n", \
271 (name), (head), (sp))); \
273 } while (/*CONSTCOND*/ 0)
276 #define KMALLOC(p, t, n) \
277 ((p) = (t) malloc((unsigned long)(n), M_SECA, M_NOWAIT))
279 free((void *)(p), M_SECA)
281 #define KMALLOC(p, t, n) \
283 ((p) = (t)malloc((unsigned long)(n), M_SECA, M_NOWAIT)); \
284 printf("%s %d: %p <- KMALLOC(%s, %d)\n", \
285 __FILE__, __LINE__, (p), #t, n); \
286 } while (/*CONSTCOND*/ 0)
290 printf("%s %d: %p -> KFREE()\n", __FILE__, __LINE__, (p)); \
291 free((void *)(p), M_SECA); \
292 } while (/*CONSTCOND*/ 0)
296 * set parameters into secpolicyindex buffer.
297 * Must allocate secpolicyindex buffer passed to this function.
299 #define KEY_SETSECSPIDX(s, d, ps, pd, ulp, idx) \
301 (void)memset((idx), 0, sizeof(struct secpolicyindex)); \
302 (idx)->prefs = (ps); \
303 (idx)->prefd = (pd); \
304 (idx)->ul_proto = (ulp); \
305 (void)memcpy(&(idx)->src, (s), ((const struct sockaddr *)(s))->sa_len);\
306 (void)memcpy(&(idx)->dst, (d), ((const struct sockaddr *)(d))->sa_len);\
307 } while (/*CONSTCOND*/ 0)
310 * set parameters into secasindex buffer.
311 * Must allocate secasindex buffer before calling this function.
313 #define KEY_SETSECASIDX(p, m, r, s, d, idx) \
315 (void)memset((idx), 0, sizeof(struct secasindex)); \
316 (idx)->proto = (p); \
318 (idx)->reqid = (r); \
319 (void)memcpy(&(idx)->src, (s), ((const struct sockaddr *)(s))->sa_len);\
320 (void)memcpy(&(idx)->dst, (d), ((const struct sockaddr *)(d))->sa_len);\
321 } while (/*CONSTCOND*/ 0)
325 u_long getspi_count
; /* the avarage of count to try to get new SPI */
329 struct sadb_msg
*msg
;
330 struct sadb_ext
*ext
[SADB_EXT_MAX
+ 1];
331 int extoff
[SADB_EXT_MAX
+ 1];
332 int extlen
[SADB_EXT_MAX
+ 1];
335 static struct secasvar
*key_allocsa_policy(struct secasindex
*);
336 static struct secasvar
*key_do_allocsa_policy(struct secashead
*, u_int
);
337 static void key_delsav(struct secasvar
*);
338 static void key_delsp(struct secpolicy
*);
339 static struct secpolicy
*key_getsp(struct secpolicyindex
*, int);
340 #ifdef SADB_X_EXT_TAG
341 static struct secpolicy
*key_getspbytag(u_int16_t
, int);
343 static u_int16_t
key_newreqid(void);
344 static struct mbuf
*key_gather_mbuf(struct mbuf
*,
345 const struct sadb_msghdr
*, int, int, ...);
346 static int key_spdadd(struct socket
*, struct mbuf
*,
347 const struct sadb_msghdr
*);
348 static int key_spddelete(struct socket
*, struct mbuf
*,
349 const struct sadb_msghdr
*);
350 static int key_spddelete2(struct socket
*, struct mbuf
*,
351 const struct sadb_msghdr
*);
352 static int key_spdget(struct socket
*, struct mbuf
*,
353 const struct sadb_msghdr
*);
354 static int key_spdflush(struct socket
*, struct mbuf
*,
355 const struct sadb_msghdr
*);
356 static int key_spddump(struct socket
*, struct mbuf
*,
357 const struct sadb_msghdr
*);
359 static int key_nat_map(struct socket
*, struct mbuf
*,
360 const struct sadb_msghdr
*);
362 static struct mbuf
*key_setspddump(int *);
363 static u_int
key_getspreqmsglen(struct secpolicy
*);
364 static int key_spdexpire(struct secpolicy
*);
365 static struct secashead
*key_newsah(struct secasindex
*);
366 static void key_delsah(struct secashead
*);
367 static struct secasvar
*key_newsav(struct mbuf
*,
368 const struct sadb_msghdr
*, struct secashead
*, int *);
369 static struct secashead
*key_getsah(struct secasindex
*);
370 static struct secasvar
*key_checkspidup(struct secasindex
*, u_int32_t
);
371 static void key_setspi(struct secasvar
*, u_int32_t
);
372 static struct secasvar
*key_getsavbyspi(struct secashead
*, u_int32_t
);
373 static int key_setsaval(struct secasvar
*, struct mbuf
*,
374 const struct sadb_msghdr
*);
375 static int key_mature(struct secasvar
*);
376 static struct mbuf
*key_setdumpsa(struct secasvar
*, u_int8_t
,
377 u_int8_t
, u_int32_t
, u_int32_t
);
378 static struct mbuf
*key_setsadbmsg(u_int8_t
, u_int16_t
, u_int8_t
,
379 u_int32_t
, pid_t
, u_int16_t
);
380 static struct mbuf
*key_setsadbsa(struct secasvar
*);
381 static struct mbuf
*key_setsadbaddr(u_int16_t
,
382 struct sockaddr
*, u_int8_t
, u_int16_t
);
384 static struct mbuf
*key_setsadbident(u_int16_t
, u_int16_t
, void *,
387 static struct mbuf
*key_setsadbxsa2(u_int8_t
, u_int32_t
, u_int16_t
);
388 #ifdef SADB_X_EXT_TAG
389 static struct mbuf
*key_setsadbxtag(u_int16_t
);
392 static struct mbuf
*key_setsadbxport(u_int16_t
, u_int16_t
);
393 static struct mbuf
*key_setsadbxtype(u_int16_t
);
395 static void key_porttosaddr(struct sockaddr
*, u_int16_t
);
396 #define KEY_PORTTOSADDR(saddr, port) \
397 key_porttosaddr((struct sockaddr *)(saddr), (port))
398 static int key_checksalen(const struct sockaddr
*);
399 #define KEY_CHECKSALEN(saddr) key_checksalen((const struct sockaddr *)(saddr))
400 static struct mbuf
*key_setsadblifetime(u_int16_t
, u_int32_t
,
401 u_int64_t
, u_int64_t
, u_int64_t
);
402 static struct mbuf
*key_setsadbxpolicy(u_int16_t
, u_int8_t
,
404 static void *key_newbuf(const void *, u_int
);
405 static int key_ismyaddr(struct sockaddr
*);
407 static int key_ismyaddr6(struct sockaddr_in6
*);
409 static int key_cmpsaidx_exactly
410 (struct secasindex
*, struct secasindex
*);
411 static int key_cmpsaidx_withmode
412 (struct secasindex
*, struct secasindex
*);
413 static int key_cmpsaidx_withoutmode
414 (struct secasindex
*, struct secasindex
*);
415 static int key_sockaddrcmp(struct sockaddr
*, struct sockaddr
*, int);
416 static int key_bbcmp(void *, void *, u_int
);
417 static u_long
key_random(void);
418 static u_int16_t
key_satype2proto(u_int8_t
);
419 static u_int8_t
key_proto2satype(u_int16_t
);
421 static int key_getspi(struct socket
*, struct mbuf
*,
422 const struct sadb_msghdr
*);
423 static u_int32_t
key_do_getnewspi(struct sadb_spirange
*,
424 struct secasindex
*);
425 static int key_update(struct socket
*, struct mbuf
*,
426 const struct sadb_msghdr
*);
427 #ifdef IPSEC_DOSEQCHECK
428 static struct secasvar
*key_getsavbyseq(struct secashead
*, u_int32_t
);
430 static int key_add(struct socket
*, struct mbuf
*,
431 const struct sadb_msghdr
*);
432 static int key_setident(struct secashead
*, struct mbuf
*,
433 const struct sadb_msghdr
*);
434 static struct mbuf
*key_getmsgbuf_x1(struct mbuf
*,
435 const struct sadb_msghdr
*);
436 static int key_delete(struct socket
*, struct mbuf
*,
437 const struct sadb_msghdr
*);
438 static int key_get(struct socket
*, struct mbuf
*,
439 const struct sadb_msghdr
*);
441 static void key_getcomb_setlifetime(struct sadb_comb
*);
443 static struct mbuf
*key_getcomb_esp(void);
445 static struct mbuf
*key_getcomb_ah(void);
446 static struct mbuf
*key_getcomb_ipcomp(void);
447 static struct mbuf
*key_getprop(const struct secasindex
*);
449 static int key_acquire(struct secasindex
*, struct secpolicy
*);
450 #ifndef IPSEC_NONBLOCK_ACQUIRE
451 static struct secacq
*key_newacq(struct secasindex
*);
452 static struct secacq
*key_getacq(struct secasindex
*);
453 static struct secacq
*key_getacqbyseq(u_int32_t
);
455 static struct secspacq
*key_newspacq(struct secpolicyindex
*);
456 static struct secspacq
*key_getspacq(struct secpolicyindex
*);
457 static int key_acquire2(struct socket
*, struct mbuf
*,
458 const struct sadb_msghdr
*);
459 static int key_register(struct socket
*, struct mbuf
*,
460 const struct sadb_msghdr
*);
461 static int key_expire(struct secasvar
*);
462 static int key_flush(struct socket
*, struct mbuf
*,
463 const struct sadb_msghdr
*);
464 static int key_dump(struct socket
*, struct mbuf
*,
465 const struct sadb_msghdr
*);
466 static struct mbuf
*key_setdump(u_int8_t
, int *);
467 static int key_promisc(struct socket
*, struct mbuf
*,
468 const struct sadb_msghdr
*);
469 static int key_senderror(struct socket
*, struct mbuf
*, int);
470 static int key_validate_ext(const struct sadb_ext
*, int);
471 static int key_align(struct mbuf
*, struct sadb_msghdr
*);
473 static const char *key_getfqdn(void);
474 static const char *key_getuserfqdn(void);
476 static void key_sa_chgstate(struct secasvar
*, u_int8_t
);
477 static void key_sp_dead(struct secpolicy
*);
478 static void key_sp_unlink(struct secpolicy
*);
479 static struct mbuf
*key_alloc_mbuf(int);
480 struct callout key_timehandler_ch
;
482 /* %%% IPsec policy management */
484 * allocating a SP for OUTBOUND or INBOUND packet.
485 * Must call key_freesp() later.
486 * OUT: NULL: not found
487 * others: found and return the pointer.
490 key_allocsp(u_int16_t tag
, struct secpolicyindex
*spidx
, u_int dir
)
492 struct secpolicy
*sp
;
495 /* check direction */
497 case IPSEC_DIR_INBOUND
:
498 case IPSEC_DIR_OUTBOUND
:
501 panic("key_allocsp: Invalid direction is passed.");
505 s
= splsoftnet(); /*called from softclock()*/
507 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
508 printf("*** objects\n");
509 kdebug_secpolicyindex(spidx
));
512 LIST_FOREACH(sp
, &sptree
[dir
], chain
) {
513 if (sp
->state
== IPSEC_SPSTATE_DEAD
)
524 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
525 printf("*** in SPD\n");
526 kdebug_secpolicyindex(sp
->spidx
));
528 if (key_cmpspidx_withmask(sp
->spidx
, spidx
))
538 KEY_CHKSPDIR(sp
->dir
, dir
, "key_allocsp");
540 /* found a SPD entry */
541 sp
->lastused
= time_second
;
544 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
545 printf("DP key_allocsp cause refcnt++:%d SP:%p\n",
552 * allocating an SA entry for an *OUTBOUND* packet.
553 * checking each request entries in SP, and acquire an SA if need.
554 * OUT: 0: there are valid requests.
555 * ENOENT: policy may be valid, but SA with REQUIRE is on acquiring.
558 key_checkrequest(struct ipsecrequest
*isr
, struct secasindex
*saidx
)
564 if (isr
== NULL
|| saidx
== NULL
)
565 panic("key_checkrequest: NULL pointer is passed.");
568 switch (saidx
->mode
) {
569 case IPSEC_MODE_TRANSPORT
:
570 case IPSEC_MODE_TUNNEL
:
574 panic("key_checkrequest: Invalid policy defined.");
577 /* get current level */
578 level
= ipsec_get_reqlevel(isr
, saidx
->src
.ss_family
);
582 * We do allocate new SA only if the state of SA in the holder is
583 * SADB_SASTATE_DEAD. The SA for outbound must be the oldest.
585 if (isr
->sav
!= NULL
) {
586 if (isr
->sav
->sah
== NULL
)
587 panic("key_checkrequest: sah is null.");
589 LIST_FIRST(&isr
->sav
->sah
->savtree
[SADB_SASTATE_DEAD
])) {
590 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
591 printf("DP checkrequest calls free SA:%p\n",
593 key_freesav(isr
->sav
);
599 * we free any SA stashed in the IPsec request because a different
600 * SA may be involved each time this request is checked, either
601 * because new SAs are being configured, or this request is
602 * associated with an unconnected datagram socket, or this request
603 * is associated with a system default policy.
605 * The operation may have negative impact to performance. We may
606 * want to check cached SA carefully, rather than picking new SA
609 if (isr
->sav
!= NULL
) {
610 key_freesav(isr
->sav
);
616 * new SA allocation if no SA found.
617 * key_allocsa_policy should allocate the oldest SA available.
618 * See key_do_allocsa_policy(), and draft-jenkins-ipsec-rekeying-03.txt.
620 if (isr
->sav
== NULL
)
621 isr
->sav
= key_allocsa_policy(saidx
);
623 /* When there is SA. */
624 if (isr
->sav
!= NULL
)
628 if ((error
= key_acquire(saidx
, isr
->sp
)) != 0) {
629 /* XXX What should I do ? */
630 ipseclog((LOG_DEBUG
, "key_checkrequest: error %d returned "
631 "from key_acquire.\n", error
));
635 return level
== IPSEC_LEVEL_REQUIRE
? ENOENT
: 0;
639 * allocating a SA for policy entry from SAD.
640 * NOTE: searching SAD of aliving state.
641 * OUT: NULL: not found.
642 * others: found and return the pointer.
644 static struct secasvar
*
645 key_allocsa_policy(struct secasindex
*saidx
)
647 struct secashead
*sah
;
648 struct secasvar
*sav
;
649 u_int stateidx
, state
;
651 LIST_FOREACH(sah
, &sahtree
, chain
) {
652 if (sah
->state
== SADB_SASTATE_DEAD
)
654 if (key_cmpsaidx_withmode(&sah
->saidx
, saidx
))
662 /* search valid state */
664 stateidx
< _ARRAYLEN(saorder_state_valid
);
667 state
= saorder_state_valid
[stateidx
];
669 sav
= key_do_allocsa_policy(sah
, state
);
678 * searching SAD with direction, protocol, mode and state.
679 * called by key_allocsa_policy().
682 * others : found, pointer to a SA.
684 static struct secasvar
*
685 key_do_allocsa_policy(struct secashead
*sah
, u_int state
)
687 struct secasvar
*sav
, *candidate
;
692 LIST_FOREACH(sav
, &sah
->savtree
[state
], chain
) {
695 KEY_CHKSASTATE(sav
->state
, state
, "key_do_allocsa_policy");
698 if (candidate
== NULL
) {
703 /* Which SA is the better ? */
706 if (candidate
->lft_c
== NULL
|| sav
->lft_c
== NULL
)
707 panic("key_do_allocsa_policy: "
708 "lifetime_current is NULL.");
710 /* XXX What the best method is to compare ? */
711 if (candidate
->lft_c
->sadb_lifetime_addtime
>
712 sav
->lft_c
->sadb_lifetime_addtime
) {
720 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
721 printf("DP allocsa_policy cause "
722 "refcnt++:%d SA:%p\n",
723 candidate
->refcnt
, candidate
));
729 * allocating a SA entry for a *INBOUND* packet.
730 * Must call key_freesav() later.
731 * OUT: positive: pointer to a sav.
732 * NULL: not found, or error occurred.
734 * In the comparison, source address will be ignored for RFC2401 conformance.
735 * To quote, from section 4.1:
736 * A security association is uniquely identified by a triple consisting
737 * of a Security Parameter Index (SPI), an IP Destination Address, and a
738 * security protocol (AH or ESP) identifier.
739 * Note that, however, we do need to keep source address in IPsec SA.
740 * IKE specification and PF_KEY specification do assume that we
741 * keep source address in IPsec SA. We see a tricky situation here.
743 * sport and dport are used for NAT-T. network order is always used.
756 struct secasvar
*sav
, *match
;
757 u_int stateidx
, state
, tmpidx
, matchidx
;
758 struct sockaddr_in sin
;
760 struct sockaddr_in6 sin6
;
766 if ((sport
!= 0) && (dport
!= 0))
770 if (src
== NULL
|| dst
== NULL
)
771 panic("key_allocsa: NULL pointer is passed.");
775 * XXX: to be checked internal IP header somewhere. Also when
776 * IPsec tunnel packet is received. But ESP tunnel mode is
777 * encrypted so we can't check internal IP header.
779 s
= splsoftnet(); /*called from softclock()*/
780 /* search valid state */
782 matchidx
= _ARRAYLEN(saorder_state_valid
);
783 LIST_FOREACH(sav
, &spihash
[SPIHASH(spi
)], spihash
) {
786 if (proto
!= sav
->sah
->saidx
.proto
)
788 if (family
!= sav
->sah
->saidx
.src
.ss_family
||
789 family
!= sav
->sah
->saidx
.dst
.ss_family
)
791 tmpidx
= _ARRAYLEN(saorder_state_valid
);
792 for (stateidx
= 0; stateidx
< matchidx
; stateidx
++) {
793 state
= saorder_state_valid
[stateidx
];
794 if (sav
->state
== state
) {
799 if (tmpidx
>= matchidx
)
802 #if 0 /* src address check ignored for RFC 2401 conformance */
803 /* check src address */
806 memset(&sin
, 0, sizeof(sin
));
807 sin
.sin_family
= AF_INET
;
808 sin
.sin_len
= sizeof(sin
);
809 memcpy(&sin
.sin_addr
, src
,
810 sizeof(sin
.sin_addr
));
812 sin
.sin_port
= sport
;
814 if (key_sockaddrcmp((struct sockaddr
*)&sin
,
815 (struct sockaddr
*)&sav
->sah
->saidx
.src
,
822 memset(&sin6
, 0, sizeof(sin6
));
823 sin6
.sin6_family
= AF_INET6
;
824 sin6
.sin6_len
= sizeof(sin6
);
825 memcpy(&sin6
.sin6_addr
, src
,
826 sizeof(sin6
.sin6_addr
));
828 sin6
.sin6_port
= sport
;
830 if (sa6_recoverscope(&sin6
))
832 if (key_sockaddrcmp((struct sockaddr
*)&sin6
,
833 (struct sockaddr
*)&sav
->sah
->saidx
.src
,
839 ipseclog((LOG_DEBUG
, "key_allocsa: "
840 "unknown address family=%d.\n",
846 /* check dst address */
849 memset(&sin
, 0, sizeof(sin
));
850 sin
.sin_family
= AF_INET
;
851 sin
.sin_len
= sizeof(sin
);
852 memcpy(&sin
.sin_addr
, dst
,
853 sizeof(sin
.sin_addr
));
855 sin
.sin_port
= dport
;
857 if (key_sockaddrcmp((struct sockaddr
*)&sin
,
858 (struct sockaddr
*)&sav
->sah
->saidx
.dst
,
865 memset(&sin6
, 0, sizeof(sin6
));
866 sin6
.sin6_family
= AF_INET6
;
867 sin6
.sin6_len
= sizeof(sin6
);
868 memcpy(&sin6
.sin6_addr
, dst
,
869 sizeof(sin6
.sin6_addr
));
871 sin6
.sin6_port
= dport
;
873 if (sa6_recoverscope(&sin6
))
875 if (key_sockaddrcmp((struct sockaddr
*)&sin6
,
876 (struct sockaddr
*)&sav
->sah
->saidx
.dst
,
882 ipseclog((LOG_DEBUG
, "key_allocsa: "
883 "unknown address family=%d.\n", family
));
901 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
902 printf("DP allocsa cause refcnt++:%d SA:%p\n",
903 match
->refcnt
, match
));
908 * Must be called after calling key_allocsp().
909 * For both the packet without socket and key_freeso().
912 key_freesp(struct secpolicy
*sp
)
916 panic("key_freesp: NULL pointer is passed.");
919 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
920 printf("DP freesp cause refcnt--:%d SP:%p\n",
930 * Must be called after calling key_allocsa().
931 * This function is called by key_freesp() to free some SA allocated
935 key_freesav(struct secasvar
*sav
)
939 panic("key_freesav: NULL pointer is passed.");
942 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
943 printf("DP freesav cause refcnt--:%d SA:%p SPI %u\n",
944 sav
->refcnt
, sav
, (u_int32_t
)ntohl(sav
->spi
)));
953 key_delsav(struct secasvar
*sav
)
959 panic("key_delsav: NULL pointer is passed.");
962 panic("key_delsav: called with positive refcnt");
967 kernfs_revoke_sa(sav
);
970 if (__LIST_CHAINED(sav
))
971 LIST_REMOVE(sav
, chain
);
973 if (sav
->spihash
.le_prev
|| sav
->spihash
.le_next
)
974 LIST_REMOVE(sav
, spihash
);
976 if (sav
->key_auth
!= NULL
) {
977 memset(_KEYBUF(sav
->key_auth
), 0, _KEYLEN(sav
->key_auth
));
978 KFREE(sav
->key_auth
);
979 sav
->key_auth
= NULL
;
981 if (sav
->key_enc
!= NULL
) {
982 memset(_KEYBUF(sav
->key_enc
), 0, _KEYLEN(sav
->key_enc
));
987 memset(sav
->sched
, 0, sav
->schedlen
);
991 if (sav
->replay
!= NULL
) {
992 keydb_delsecreplay(sav
->replay
);
995 if (sav
->lft_c
!= NULL
) {
999 if (sav
->lft_h
!= NULL
) {
1003 if (sav
->lft_s
!= NULL
) {
1007 if (sav
->iv
!= NULL
) {
1012 keydb_delsecasvar(sav
);
1017 /* %%% SPD management */
1019 * free security policy entry.
1022 key_delsp(struct secpolicy
*sp
)
1028 panic("key_delsp: NULL pointer is passed.");
1031 panic("key_delsp: called with positive refcnt");
1033 s
= splsoftnet(); /*called from softclock()*/
1036 kernfs_revoke_sp(sp
);
1040 struct ipsecrequest
*isr
= sp
->req
, *nextisr
;
1042 while (isr
!= NULL
) {
1043 if (isr
->sav
!= NULL
) {
1044 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1045 printf("DP delsp calls free SA:%p\n",
1047 key_freesav(isr
->sav
);
1051 nextisr
= isr
->next
;
1057 keydb_delsecpolicy(sp
);
1066 * OUT: NULL : not found
1067 * others : found, pointer to a SP.
1069 static struct secpolicy
*
1070 key_getsp(struct secpolicyindex
*spidx
, int dir
)
1072 struct secpolicy
*sp
;
1076 panic("key_getsp: NULL pointer is passed.");
1078 LIST_FOREACH(sp
, &sptree
[dir
], chain
) {
1079 if (sp
->state
== IPSEC_SPSTATE_DEAD
)
1083 if (key_cmpspidx_exactly(spidx
, sp
->spidx
)) {
1092 #ifdef SADB_X_EXT_TAG
1093 static struct secpolicy
*
1094 key_getspbytag(u_int16_t tag
, int dir
)
1096 struct secpolicy
*sp
;
1098 LIST_FOREACH(sp
, &sptree
[dir
], chain
) {
1099 if (sp
->state
== IPSEC_SPSTATE_DEAD
)
1103 if (sp
->tag
== tag
) {
1115 * OUT: NULL : not found
1116 * others : found, pointer to a SP.
1119 key_getspbyid(u_int32_t id
)
1121 struct secpolicy
*sp
;
1123 TAILQ_FOREACH(sp
, &sptailq
, tailq
) {
1134 key_newsp(u_int32_t id
)
1136 struct secpolicy
*newsp
= NULL
, *sp
;
1139 if (id
> IPSEC_MANUAL_POLICYID_MAX
) {
1140 ipseclog((LOG_DEBUG
,
1141 "key_newsp: policy_id=%u range "
1142 "violation, updated by kernel.\n", id
));
1147 if ((newid
= keydb_newspid()) == 0) {
1148 ipseclog((LOG_DEBUG
,
1149 "key_newsp: new policy_id allocation failed."));
1153 sp
= key_getspbyid(id
);
1155 ipseclog((LOG_DEBUG
,
1156 "key_newsp: policy_id(%u) has been used.\n", id
));
1163 newsp
= keydb_newsecpolicy();
1175 * create secpolicy structure from sadb_x_policy structure.
1176 * NOTE: `state', `secpolicyindex' in secpolicy structure are not set,
1177 * so must be set properly later.
1180 key_msg2sp(struct sadb_x_policy
*xpl0
, size_t len
, int *error
)
1182 struct secpolicy
*newsp
;
1186 panic("key_msg2sp: NULL pointer was passed.");
1187 if (len
< sizeof(*xpl0
))
1188 panic("key_msg2sp: invalid length.");
1189 if (len
!= PFKEY_EXTLEN(xpl0
)) {
1190 ipseclog((LOG_DEBUG
, "key_msg2sp: Invalid msg length.\n"));
1195 if ((newsp
= key_newsp(xpl0
->sadb_x_policy_id
)) == NULL
) {
1200 newsp
->dir
= xpl0
->sadb_x_policy_dir
;
1201 newsp
->policy
= xpl0
->sadb_x_policy_type
;
1204 switch (xpl0
->sadb_x_policy_type
) {
1205 case IPSEC_POLICY_DISCARD
:
1206 case IPSEC_POLICY_NONE
:
1207 case IPSEC_POLICY_ENTRUST
:
1208 case IPSEC_POLICY_BYPASS
:
1212 case IPSEC_POLICY_IPSEC
:
1215 struct sadb_x_ipsecrequest
*xisr
;
1216 struct ipsecrequest
**p_isr
= &newsp
->req
;
1218 /* validity check */
1219 if (PFKEY_EXTLEN(xpl0
) < sizeof(*xpl0
)) {
1220 ipseclog((LOG_DEBUG
,
1221 "key_msg2sp: Invalid msg length.\n"));
1227 tlen
= PFKEY_EXTLEN(xpl0
) - sizeof(*xpl0
);
1228 xisr
= (struct sadb_x_ipsecrequest
*)(xpl0
+ 1);
1233 if (xisr
->sadb_x_ipsecrequest_len
< sizeof(*xisr
)) {
1234 ipseclog((LOG_DEBUG
, "key_msg2sp: "
1235 "invalid ipsecrequest length.\n"));
1241 /* allocate request buffer */
1242 KMALLOC(*p_isr
, struct ipsecrequest
*, sizeof(**p_isr
));
1243 if ((*p_isr
) == NULL
) {
1244 ipseclog((LOG_DEBUG
,
1245 "key_msg2sp: No more memory.\n"));
1250 memset(*p_isr
, 0, sizeof(**p_isr
));
1253 (*p_isr
)->next
= NULL
;
1255 switch (xisr
->sadb_x_ipsecrequest_proto
) {
1258 case IPPROTO_IPCOMP
:
1263 ipseclog((LOG_DEBUG
,
1264 "key_msg2sp: invalid proto type=%u\n",
1265 xisr
->sadb_x_ipsecrequest_proto
));
1267 *error
= EPROTONOSUPPORT
;
1270 (*p_isr
)->saidx
.proto
= xisr
->sadb_x_ipsecrequest_proto
;
1272 switch (xisr
->sadb_x_ipsecrequest_mode
) {
1273 case IPSEC_MODE_TRANSPORT
:
1274 case IPSEC_MODE_TUNNEL
:
1276 case IPSEC_MODE_ANY
:
1278 ipseclog((LOG_DEBUG
,
1279 "key_msg2sp: invalid mode=%u\n",
1280 xisr
->sadb_x_ipsecrequest_mode
));
1285 (*p_isr
)->saidx
.mode
= xisr
->sadb_x_ipsecrequest_mode
;
1287 switch (xisr
->sadb_x_ipsecrequest_level
) {
1288 case IPSEC_LEVEL_DEFAULT
:
1289 case IPSEC_LEVEL_USE
:
1290 case IPSEC_LEVEL_REQUIRE
:
1292 case IPSEC_LEVEL_UNIQUE
:
1293 /* validity check */
1295 * If range violation of reqid, kernel will
1296 * update it, don't refuse it.
1298 if (xisr
->sadb_x_ipsecrequest_reqid
1299 > IPSEC_MANUAL_REQID_MAX
) {
1300 ipseclog((LOG_DEBUG
,
1301 "key_msg2sp: reqid=%u range "
1302 "violation, updated by kernel.\n",
1303 xisr
->sadb_x_ipsecrequest_reqid
));
1304 xisr
->sadb_x_ipsecrequest_reqid
= 0;
1307 /* allocate new reqid id if reqid is zero. */
1308 if (xisr
->sadb_x_ipsecrequest_reqid
== 0) {
1310 if ((reqid
= key_newreqid()) == 0) {
1315 (*p_isr
)->saidx
.reqid
= reqid
;
1316 xisr
->sadb_x_ipsecrequest_reqid
= reqid
;
1318 /* set it for manual keying. */
1319 (*p_isr
)->saidx
.reqid
=
1320 xisr
->sadb_x_ipsecrequest_reqid
;
1325 ipseclog((LOG_DEBUG
, "key_msg2sp: invalid level=%u\n",
1326 xisr
->sadb_x_ipsecrequest_level
));
1331 (*p_isr
)->level
= xisr
->sadb_x_ipsecrequest_level
;
1333 /* set IP addresses if there */
1334 if (xisr
->sadb_x_ipsecrequest_len
> sizeof(*xisr
)) {
1335 struct sockaddr
*paddr
;
1337 paddr
= (struct sockaddr
*)(xisr
+ 1);
1339 /* validity check */
1341 > sizeof((*p_isr
)->saidx
.src
)) {
1342 ipseclog((LOG_DEBUG
, "key_msg2sp: invalid request "
1343 "address length.\n"));
1348 memcpy(&(*p_isr
)->saidx
.src
, paddr
,
1351 paddr
= (struct sockaddr
*)((char *)paddr
1354 /* validity check */
1356 > sizeof((*p_isr
)->saidx
.dst
)) {
1357 ipseclog((LOG_DEBUG
, "key_msg2sp: invalid request "
1358 "address length.\n"));
1363 memcpy(&(*p_isr
)->saidx
.dst
, paddr
,
1367 (*p_isr
)->sav
= NULL
;
1368 (*p_isr
)->sp
= newsp
;
1370 /* initialization for the next. */
1371 p_isr
= &(*p_isr
)->next
;
1372 tlen
-= xisr
->sadb_x_ipsecrequest_len
;
1374 /* validity check */
1376 ipseclog((LOG_DEBUG
, "key_msg2sp: becoming tlen < 0.\n"));
1382 xisr
= (struct sadb_x_ipsecrequest
*)((char *)xisr
1383 + xisr
->sadb_x_ipsecrequest_len
);
1388 ipseclog((LOG_DEBUG
, "key_msg2sp: invalid policy type.\n"));
1401 static u_int16_t auto_reqid
= IPSEC_MANUAL_REQID_MAX
+ 1;
1403 auto_reqid
= (auto_reqid
== 0xffff
1404 ? IPSEC_MANUAL_REQID_MAX
+ 1 : auto_reqid
+ 1);
1406 /* XXX should be unique check */
1412 * copy secpolicy struct to sadb_x_policy structure indicated.
1415 key_sp2msg(struct secpolicy
*sp
)
1417 struct sadb_x_policy
*xpl
;
1424 panic("key_sp2msg: NULL pointer was passed.");
1426 tlen
= key_getspreqmsglen(sp
);
1428 m
= key_alloc_mbuf(tlen
);
1429 if (!m
|| m
->m_next
) { /*XXX*/
1437 xpl
= mtod(m
, struct sadb_x_policy
*);
1438 memset(xpl
, 0, tlen
);
1440 xpl
->sadb_x_policy_len
= PFKEY_UNIT64(tlen
);
1441 xpl
->sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
1442 xpl
->sadb_x_policy_type
= sp
->policy
;
1443 xpl
->sadb_x_policy_dir
= sp
->dir
;
1444 xpl
->sadb_x_policy_id
= sp
->id
;
1445 p
= (char *)xpl
+ sizeof(*xpl
);
1447 /* if is the policy for ipsec ? */
1448 if (sp
->policy
== IPSEC_POLICY_IPSEC
) {
1449 struct sadb_x_ipsecrequest
*xisr
;
1450 struct ipsecrequest
*isr
;
1452 for (isr
= sp
->req
; isr
!= NULL
; isr
= isr
->next
) {
1454 xisr
= (struct sadb_x_ipsecrequest
*)p
;
1456 xisr
->sadb_x_ipsecrequest_proto
= isr
->saidx
.proto
;
1457 xisr
->sadb_x_ipsecrequest_mode
= isr
->saidx
.mode
;
1458 xisr
->sadb_x_ipsecrequest_level
= isr
->level
;
1459 xisr
->sadb_x_ipsecrequest_reqid
= isr
->saidx
.reqid
;
1462 memcpy(p
, &isr
->saidx
.src
, isr
->saidx
.src
.ss_len
);
1463 p
+= isr
->saidx
.src
.ss_len
;
1464 memcpy(p
, &isr
->saidx
.dst
, isr
->saidx
.dst
.ss_len
);
1465 p
+= isr
->saidx
.src
.ss_len
;
1467 xisr
->sadb_x_ipsecrequest_len
=
1468 PFKEY_ALIGN8(sizeof(*xisr
)
1469 + isr
->saidx
.src
.ss_len
1470 + isr
->saidx
.dst
.ss_len
);
1477 /* m will not be freed nor modified */
1478 static struct mbuf
*
1479 key_gather_mbuf(struct mbuf
*m
, const struct sadb_msghdr
*mhp
,
1480 int ndeep
, int nitem
, ...)
1485 struct mbuf
*result
= NULL
, *n
;
1488 if (m
== NULL
|| mhp
== NULL
)
1489 panic("null pointer passed to key_gather");
1491 va_start(ap
, nitem
);
1492 for (i
= 0; i
< nitem
; i
++) {
1493 idx
= va_arg(ap
, int);
1494 if (idx
< 0 || idx
> SADB_EXT_MAX
)
1496 /* don't attempt to pull empty extension */
1497 if (idx
== SADB_EXT_RESERVED
&& mhp
->msg
== NULL
)
1499 if (idx
!= SADB_EXT_RESERVED
&&
1500 (mhp
->ext
[idx
] == NULL
|| mhp
->extlen
[idx
] == 0))
1503 if (idx
== SADB_EXT_RESERVED
) {
1504 len
= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
1507 panic("assumption failed");
1509 MGETHDR(n
, M_DONTWAIT
, MT_DATA
);
1514 m_copydata(m
, 0, sizeof(struct sadb_msg
),
1516 } else if (i
< ndeep
) {
1517 len
= mhp
->extlen
[idx
];
1518 n
= key_alloc_mbuf(len
);
1519 if (!n
|| n
->m_next
) { /*XXX*/
1524 m_copydata(m
, mhp
->extoff
[idx
], mhp
->extlen
[idx
],
1527 n
= m_copym(m
, mhp
->extoff
[idx
], mhp
->extlen
[idx
],
1540 if ((result
->m_flags
& M_PKTHDR
) != 0) {
1541 result
->m_pkthdr
.len
= 0;
1542 for (n
= result
; n
; n
= n
->m_next
)
1543 result
->m_pkthdr
.len
+= n
->m_len
;
1555 * SADB_X_SPDADD, SADB_X_SPDSETIDX or SADB_X_SPDUPDATE processing
1556 * add an entry to SP database, when received
1557 * <base, address(SD), (lifetime(H),) policy>
1559 * Adding to SP database,
1561 * <base, address(SD), (lifetime(H),) policy>
1562 * to the socket which was send.
1564 * SPDADD set a unique policy entry.
1565 * SPDSETIDX like SPDADD without a part of policy requests.
1566 * SPDUPDATE replace a unique policy entry.
1568 * m will always be freed.
1571 key_spdadd(struct socket
*so
, struct mbuf
*m
, const struct sadb_msghdr
*mhp
)
1573 struct sadb_address
*src0
= NULL
, *dst0
= NULL
;
1574 struct sadb_x_policy
*xpl0
, *xpl
;
1575 struct sadb_lifetime
*lft
= NULL
;
1576 #ifdef SADB_X_EXT_TAG
1577 struct sadb_x_tag
*tag
= NULL
;
1579 struct secpolicyindex spidx
;
1580 struct secpolicy
*newsp
;
1581 struct ipsecrequest
*isr
;
1583 #ifdef SADB_X_EXT_TAG
1584 u_int16_t tagvalue
= 0;
1589 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
1590 panic("key_spdadd: NULL pointer is passed.");
1592 #ifdef SADB_X_EXT_TAG
1593 if ((mhp
->ext
[SADB_EXT_ADDRESS_SRC
] != NULL
&&
1594 mhp
->ext
[SADB_EXT_ADDRESS_DST
] != NULL
) ||
1595 mhp
->ext
[SADB_X_EXT_TAG
] != NULL
)
1597 if (mhp
->ext
[SADB_EXT_ADDRESS_SRC
] != NULL
&&
1598 mhp
->ext
[SADB_EXT_ADDRESS_DST
] != NULL
)
1603 ipseclog((LOG_DEBUG
, "key_spdadd: invalid message is passed.\n"));
1604 return key_senderror(so
, m
, EINVAL
);
1606 #ifdef SADB_X_EXT_TAG
1607 if (mhp
->ext
[SADB_X_EXT_TAG
] != NULL
) {
1608 ipseclog((LOG_DEBUG
, "key_spdadd: tag not supported.\n"));
1609 return key_senderror(so
, m
, EOPNOTSUPP
);
1612 if (mhp
->ext
[SADB_X_EXT_POLICY
] == NULL
) {
1613 ipseclog((LOG_DEBUG
, "key_spdadd: invalid message is passed.\n"));
1614 return key_senderror(so
, m
, EINVAL
);
1616 if ((mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] &&
1617 mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
)) ||
1618 (mhp
->extlen
[SADB_EXT_ADDRESS_DST
] &&
1619 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) ||
1620 #ifdef SADB_X_EXT_TAG
1621 (mhp
->extlen
[SADB_X_EXT_TAG
] &&
1622 mhp
->extlen
[SADB_X_EXT_TAG
] < sizeof(struct sadb_x_tag
)) ||
1624 mhp
->extlen
[SADB_X_EXT_POLICY
] < sizeof(struct sadb_x_policy
)) {
1625 ipseclog((LOG_DEBUG
, "key_spdadd: invalid message is passed.\n"));
1626 return key_senderror(so
, m
, EINVAL
);
1628 if (mhp
->ext
[SADB_EXT_LIFETIME_HARD
] != NULL
) {
1629 if (mhp
->extlen
[SADB_EXT_LIFETIME_HARD
]
1630 < sizeof(struct sadb_lifetime
)) {
1631 ipseclog((LOG_DEBUG
, "key_spdadd: invalid message is passed.\n"));
1632 return key_senderror(so
, m
, EINVAL
);
1634 lft
= (struct sadb_lifetime
*)mhp
->ext
[SADB_EXT_LIFETIME_HARD
];
1637 /* spidx mode, or tag mode */
1638 spidxmode
= (mhp
->ext
[SADB_EXT_ADDRESS_SRC
] != NULL
);
1639 #ifndef SADB_X_EXT_TAG
1641 return key_senderror(so
, m
, EINVAL
);
1645 src0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_SRC
];
1646 dst0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_DST
];
1648 /* XXX boundary check against sa_len */
1649 KEY_SETSECSPIDX(src0
+ 1, dst0
+ 1,
1650 src0
->sadb_address_prefixlen
, dst0
->sadb_address_prefixlen
,
1651 src0
->sadb_address_proto
, &spidx
);
1653 #ifdef SADB_X_EXT_TAG
1655 tag
= (struct sadb_x_tag
*)mhp
->ext
[SADB_X_EXT_TAG
];
1657 xpl0
= (struct sadb_x_policy
*)mhp
->ext
[SADB_X_EXT_POLICY
];
1659 /* checking the direciton. */
1660 switch (xpl0
->sadb_x_policy_dir
) {
1661 case IPSEC_DIR_INBOUND
:
1662 case IPSEC_DIR_OUTBOUND
:
1665 ipseclog((LOG_DEBUG
, "key_spdadd: Invalid SP direction.\n"));
1666 mhp
->msg
->sadb_msg_errno
= EINVAL
;
1671 /* key_spdadd() accepts DISCARD, NONE and IPSEC. */
1672 if (xpl0
->sadb_x_policy_type
== IPSEC_POLICY_ENTRUST
||
1673 xpl0
->sadb_x_policy_type
== IPSEC_POLICY_BYPASS
) {
1674 ipseclog((LOG_DEBUG
, "key_spdadd: Invalid policy type.\n"));
1675 return key_senderror(so
, m
, EINVAL
);
1678 /* policy requests are mandatory when action is ipsec. */
1679 if (mhp
->msg
->sadb_msg_type
!= SADB_X_SPDSETIDX
&&
1680 xpl0
->sadb_x_policy_type
== IPSEC_POLICY_IPSEC
&&
1681 mhp
->extlen
[SADB_X_EXT_POLICY
] <= sizeof(*xpl0
)) {
1682 ipseclog((LOG_DEBUG
, "key_spdadd: some policy requests part required.\n"));
1683 return key_senderror(so
, m
, EINVAL
);
1687 * checking there is SP already or not.
1688 * SPDUPDATE doesn't depend on whether there is a SP or not.
1689 * If the type is either SPDADD or SPDSETIDX AND a SP is found,
1692 if (xpl0
->sadb_x_policy_id
!= 0)
1693 newsp
= key_getspbyid(xpl0
->sadb_x_policy_id
);
1695 newsp
= key_getsp(&spidx
, xpl0
->sadb_x_policy_dir
);
1696 #ifdef SADB_X_EXT_TAG
1698 tagvalue
= m_nametag_tagname2tag(tag
->sadb_x_tag_name
);
1700 newsp
= key_getspbytag(tagvalue
, xpl0
->sadb_x_policy_dir
);
1707 if (newsp
&& (newsp
->readonly
|| newsp
->persist
)) {
1708 ipseclog((LOG_DEBUG
,
1709 "key_spdadd: tried to alter readonly/persistent SP.\n"));
1710 return key_senderror(so
, m
, EPERM
);
1713 if (mhp
->msg
->sadb_msg_type
== SADB_X_SPDUPDATE
) {
1716 key_freesp(newsp
); /* ref gained by key_getsp */
1717 key_sp_unlink(newsp
);
1721 if (newsp
!= NULL
) {
1723 ipseclog((LOG_DEBUG
, "key_spdadd: a SP entry exists already.\n"));
1724 #ifdef SADB_X_EXT_TAG
1725 if (!mhp
->ext
[SADB_EXT_ADDRESS_SRC
])
1726 m_nametag_unref(tagvalue
);
1728 return key_senderror(so
, m
, EEXIST
);
1732 /* allocation new SP entry */
1733 if ((newsp
= key_msg2sp(xpl0
, PFKEY_EXTLEN(xpl0
), &error
)) == NULL
) {
1734 #ifdef SADB_X_EXT_TAG
1736 m_nametag_unref(tagvalue
);
1738 return key_senderror(so
, m
, error
);
1742 error
= keydb_setsecpolicyindex(newsp
, &spidx
);
1744 keydb_delsecpolicy(newsp
);
1745 return key_senderror(so
, m
, error
);
1748 /* sanity check on addr pair */
1749 if (((struct sockaddr
*)(src0
+ 1))->sa_family
!=
1750 ((struct sockaddr
*)(dst0
+ 1))->sa_family
) {
1751 keydb_delsecpolicy(newsp
);
1752 return key_senderror(so
, m
, EINVAL
);
1754 if (((struct sockaddr
*)(src0
+ 1))->sa_len
!=
1755 ((struct sockaddr
*)(dst0
+ 1))->sa_len
) {
1756 keydb_delsecpolicy(newsp
);
1757 return key_senderror(so
, m
, EINVAL
);
1760 #ifdef SADB_X_EXT_TAG
1762 newsp
->tag
= tagvalue
;
1766 for (isr
= newsp
->req
; isr
; isr
= isr
->next
) {
1768 struct sockaddr
*sa
;
1771 * port spec is not permitted for tunnel mode
1773 if (isr
->saidx
.mode
== IPSEC_MODE_TUNNEL
&& src0
&& dst0
) {
1774 sa
= (struct sockaddr
*)(src0
+ 1);
1775 switch (sa
->sa_family
) {
1777 if (((struct sockaddr_in
*)sa
)->sin_port
) {
1778 keydb_delsecpolicy(newsp
);
1779 return key_senderror(so
, m
, EINVAL
);
1783 if (((struct sockaddr_in6
*)sa
)->sin6_port
) {
1784 keydb_delsecpolicy(newsp
);
1785 return key_senderror(so
, m
, EINVAL
);
1791 sa
= (struct sockaddr
*)(dst0
+ 1);
1792 switch (sa
->sa_family
) {
1794 if (((struct sockaddr_in
*)sa
)->sin_port
) {
1795 keydb_delsecpolicy(newsp
);
1796 return key_senderror(so
, m
, EINVAL
);
1800 if (((struct sockaddr_in6
*)sa
)->sin6_port
) {
1801 keydb_delsecpolicy(newsp
);
1802 return key_senderror(so
, m
, EINVAL
);
1809 #endif /* !IPSEC_NAT_T */
1813 * bark if we have different address family on tunnel address
1814 * specification. applies only if we decapsulate in RFC2401
1815 * IPsec (implementation limitation).
1817 for (isr
= newsp
->req
; isr
; isr
= isr
->next
) {
1818 struct sockaddr
*sa
;
1820 if (isr
->saidx
.src
.ss_family
&& src0
) {
1821 sa
= (struct sockaddr
*)(src0
+ 1);
1822 if (sa
->sa_family
!= isr
->saidx
.src
.ss_family
) {
1823 keydb_delsecpolicy(newsp
);
1824 return key_senderror(so
, m
, EINVAL
);
1827 if (isr
->saidx
.dst
.ss_family
&& dst0
) {
1828 sa
= (struct sockaddr
*)(dst0
+ 1);
1829 if (sa
->sa_family
!= isr
->saidx
.dst
.ss_family
) {
1830 keydb_delsecpolicy(newsp
);
1831 return key_senderror(so
, m
, EINVAL
);
1836 newsp
->created
= time_second
;
1837 newsp
->lastused
= time_second
;
1838 newsp
->lifetime
= lft
? lft
->sadb_lifetime_addtime
: 0;
1839 newsp
->validtime
= lft
? lft
->sadb_lifetime_usetime
: 0;
1841 newsp
->refcnt
= 1; /* do not reclaim until I say I do */
1842 newsp
->state
= IPSEC_SPSTATE_ALIVE
;
1843 LIST_INSERT_TAIL(&sptree
[newsp
->dir
], newsp
, secpolicy
, chain
);
1845 /* delete the entry in spacqtree */
1846 if (mhp
->msg
->sadb_msg_type
== SADB_X_SPDUPDATE
&&
1847 mhp
->ext
[SADB_EXT_ADDRESS_SRC
]) {
1848 struct secspacq
*spacq
;
1849 if ((spacq
= key_getspacq(&spidx
)) != NULL
) {
1850 /* reset counter in order to deletion by timehandler. */
1851 spacq
->created
= time_second
;
1856 /* invalidate all cached SPD pointers on pcb */
1857 ipsec_invalpcbcacheall();
1860 struct mbuf
*n
, *mpolicy
;
1861 struct sadb_msg
*newmsg
;
1864 /* create new sadb_msg to reply. */
1866 n
= key_gather_mbuf(m
, mhp
, 2, 5, SADB_EXT_RESERVED
,
1867 SADB_X_EXT_POLICY
, SADB_EXT_LIFETIME_HARD
,
1868 SADB_EXT_ADDRESS_SRC
, SADB_EXT_ADDRESS_DST
);
1870 n
= key_gather_mbuf(m
, mhp
, 2, 4, SADB_EXT_RESERVED
,
1872 SADB_EXT_ADDRESS_SRC
, SADB_EXT_ADDRESS_DST
);
1875 return key_senderror(so
, m
, ENOBUFS
);
1877 if (n
->m_len
< sizeof(*newmsg
)) {
1878 n
= m_pullup(n
, sizeof(*newmsg
));
1880 return key_senderror(so
, m
, ENOBUFS
);
1882 newmsg
= mtod(n
, struct sadb_msg
*);
1883 newmsg
->sadb_msg_errno
= 0;
1884 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
1887 mpolicy
= m_pulldown(n
, PFKEY_ALIGN8(sizeof(struct sadb_msg
)),
1888 sizeof(*xpl
), &off
);
1889 if (mpolicy
== NULL
) {
1890 /* n is already freed */
1891 return key_senderror(so
, m
, ENOBUFS
);
1893 xpl
= (struct sadb_x_policy
*)(mtod(mpolicy
, char *) + off
);
1894 if (xpl
->sadb_x_policy_exttype
!= SADB_X_EXT_POLICY
) {
1896 return key_senderror(so
, m
, EINVAL
);
1898 xpl
->sadb_x_policy_id
= newsp
->id
;
1901 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
1906 * SADB_SPDDELETE processing
1908 * <base, address(SD), policy(*)>
1909 * from the user(?), and set SADB_SASTATE_DEAD,
1911 * <base, address(SD), policy(*)>
1913 * policy(*) including the direction of the policy.
1915 * m will always be freed.
1918 key_spddelete(struct socket
*so
, struct mbuf
*m
, const struct sadb_msghdr
*mhp
)
1920 struct sadb_address
*src0
, *dst0
;
1921 struct sadb_x_policy
*xpl0
;
1922 struct secpolicyindex spidx
;
1923 struct secpolicy
*sp
;
1926 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
1927 panic("key_spddelete: NULL pointer is passed.");
1929 if (mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
1930 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
||
1931 mhp
->ext
[SADB_X_EXT_POLICY
] == NULL
) {
1932 ipseclog((LOG_DEBUG
, "key_spddelete: invalid message is passed.\n"));
1933 return key_senderror(so
, m
, EINVAL
);
1935 if (mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
1936 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
) ||
1937 mhp
->extlen
[SADB_X_EXT_POLICY
] < sizeof(struct sadb_x_policy
)) {
1938 ipseclog((LOG_DEBUG
, "key_spddelete: invalid message is passed.\n"));
1939 return key_senderror(so
, m
, EINVAL
);
1942 src0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_SRC
];
1943 dst0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_DST
];
1944 xpl0
= (struct sadb_x_policy
*)mhp
->ext
[SADB_X_EXT_POLICY
];
1947 /* XXX boundary check against sa_len */
1948 KEY_SETSECSPIDX(src0
+ 1,
1950 src0
->sadb_address_prefixlen
,
1951 dst0
->sadb_address_prefixlen
,
1952 src0
->sadb_address_proto
,
1955 /* checking the direciton. */
1956 switch (xpl0
->sadb_x_policy_dir
) {
1957 case IPSEC_DIR_INBOUND
:
1958 case IPSEC_DIR_OUTBOUND
:
1961 ipseclog((LOG_DEBUG
, "key_spddelete: Invalid SP direction.\n"));
1962 return key_senderror(so
, m
, EINVAL
);
1965 /* Is there SP in SPD ? */
1966 if ((sp
= key_getsp(&spidx
, xpl0
->sadb_x_policy_dir
)) == NULL
) {
1967 ipseclog((LOG_DEBUG
, "key_spddelete: no SP found.\n"));
1968 return key_senderror(so
, m
, EINVAL
);
1972 ipseclog((LOG_DEBUG
,
1973 "key_spddelete2: attempt to remove persistent SP:%u.\n",
1975 key_freesp(sp
); /* ref gained by key_getsp */
1976 return key_senderror(so
, m
, EPERM
);
1979 /* save policy id to be returned. */
1980 xpl0
->sadb_x_policy_id
= sp
->id
;
1983 key_freesp(sp
); /* ref gained by key_getsp */
1987 /* invalidate all cached SPD pointers on pcb */
1988 ipsec_invalpcbcacheall();
1992 struct sadb_msg
*newmsg
;
1994 /* create new sadb_msg to reply. */
1995 n
= key_gather_mbuf(m
, mhp
, 1, 4, SADB_EXT_RESERVED
,
1996 SADB_X_EXT_POLICY
, SADB_EXT_ADDRESS_SRC
, SADB_EXT_ADDRESS_DST
);
1998 return key_senderror(so
, m
, ENOBUFS
);
2000 newmsg
= mtod(n
, struct sadb_msg
*);
2001 newmsg
->sadb_msg_errno
= 0;
2002 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
2005 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
2010 * SADB_SPDDELETE2 processing
2013 * from the user(?), and set SADB_SASTATE_DEAD,
2017 * policy(*) including the policy id.
2019 * m will always be freed.
2022 key_spddelete2(struct socket
*so
, struct mbuf
*m
, const struct sadb_msghdr
*mhp
)
2025 struct secpolicy
*sp
;
2028 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
2029 panic("key_spddelete2: NULL pointer is passed.");
2031 if (mhp
->ext
[SADB_X_EXT_POLICY
] == NULL
||
2032 mhp
->extlen
[SADB_X_EXT_POLICY
] < sizeof(struct sadb_x_policy
)) {
2033 ipseclog((LOG_DEBUG
, "key_spddelete2: invalid message is passed.\n"));
2034 return key_senderror(so
, m
, EINVAL
);
2037 id
= ((struct sadb_x_policy
*)mhp
->ext
[SADB_X_EXT_POLICY
])->sadb_x_policy_id
;
2039 /* Is there SP in SPD ? */
2040 if ((sp
= key_getspbyid(id
)) == NULL
) {
2041 ipseclog((LOG_DEBUG
, "key_spddelete2: no SP found id:%u.\n",
2043 return key_senderror(so
, m
, EINVAL
);
2047 ipseclog((LOG_DEBUG
,
2048 "key_spddelete2: attempt to remove persistent SP:%u.\n",
2050 key_freesp(sp
); /* ref gained by key_getspbyid */
2051 return key_senderror(so
, m
, EPERM
);
2055 key_freesp(sp
); /* ref gained by key_getspbyid */
2059 /* invalidate all cached SPD pointers on pcb */
2060 ipsec_invalpcbcacheall();
2063 struct mbuf
*n
, *nn
;
2064 struct sadb_msg
*newmsg
;
2067 /* create new sadb_msg to reply. */
2068 len
= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
2071 return key_senderror(so
, m
, ENOBUFS
);
2072 MGETHDR(n
, M_DONTWAIT
, MT_DATA
);
2073 if (n
&& len
> MHLEN
) {
2074 MCLGET(n
, M_DONTWAIT
);
2075 if ((n
->m_flags
& M_EXT
) == 0) {
2081 return key_senderror(so
, m
, ENOBUFS
);
2087 m_copydata(m
, 0, sizeof(struct sadb_msg
), mtod(n
, char *) + off
);
2088 off
+= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
2092 panic("length inconsistency in key_spddelete2");
2095 n
->m_next
= m_copym(m
, mhp
->extoff
[SADB_X_EXT_POLICY
],
2096 mhp
->extlen
[SADB_X_EXT_POLICY
], M_DONTWAIT
);
2099 return key_senderror(so
, m
, ENOBUFS
);
2102 n
->m_pkthdr
.len
= 0;
2103 for (nn
= n
; nn
; nn
= nn
->m_next
)
2104 n
->m_pkthdr
.len
+= nn
->m_len
;
2106 newmsg
= mtod(n
, struct sadb_msg
*);
2107 newmsg
->sadb_msg_errno
= 0;
2108 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
2111 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
2116 * SADB_X_SPDGET processing
2121 * <base, address(SD), policy>
2123 * policy(*) including direction of policy.
2125 * m will always be freed.
2128 key_spdget(struct socket
*so
, struct mbuf
*m
, const struct sadb_msghdr
*mhp
)
2131 struct secpolicy
*sp
;
2135 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
2136 panic("key_spdget: NULL pointer is passed.");
2138 if (mhp
->ext
[SADB_X_EXT_POLICY
] == NULL
||
2139 mhp
->extlen
[SADB_X_EXT_POLICY
] < sizeof(struct sadb_x_policy
)) {
2140 ipseclog((LOG_DEBUG
, "key_spdget: invalid message is passed.\n"));
2141 return key_senderror(so
, m
, EINVAL
);
2144 id
= ((struct sadb_x_policy
*)mhp
->ext
[SADB_X_EXT_POLICY
])->sadb_x_policy_id
;
2146 /* Is there SP in SPD ? */
2147 if ((sp
= key_getspbyid(id
)) == NULL
) {
2148 ipseclog((LOG_DEBUG
, "key_spdget: no SP found id:%u.\n", id
));
2149 return key_senderror(so
, m
, ENOENT
);
2152 n
= key_setdumpsp(sp
, SADB_X_SPDGET
, mhp
->msg
->sadb_msg_seq
,
2153 mhp
->msg
->sadb_msg_pid
);
2154 key_freesp(sp
); /* ref gained by key_getspbyid */
2157 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ONE
);
2159 return key_senderror(so
, m
, ENOBUFS
);
2163 * SADB_X_SPDACQUIRE processing.
2164 * Acquire policy and SA(s) for a *OUTBOUND* packet.
2167 * to KMD, and expect to receive
2168 * <base> with SADB_X_SPDACQUIRE if error occurred,
2171 * with SADB_X_SPDUPDATE from KMD by PF_KEY.
2172 * policy(*) is without policy requests.
2175 * others: error number
2178 key_spdacquire(struct secpolicy
*sp
)
2180 struct mbuf
*result
= NULL
, *m
;
2181 #ifndef IPSEC_NONBLOCK_ACQUIRE
2182 struct secspacq
*newspacq
;
2188 panic("key_spdacquire: NULL pointer is passed.");
2189 if (sp
->req
!= NULL
)
2190 panic("key_spdacquire: called but there is request.");
2191 if (sp
->policy
!= IPSEC_POLICY_IPSEC
)
2192 panic("key_spdacquire: policy mismathed. IPsec is expected.");
2198 #ifndef IPSEC_NONBLOCK_ACQUIRE
2199 /* get an entry to check whether sent message or not. */
2200 if ((newspacq
= key_getspacq(sp
->spidx
)) != NULL
) {
2201 if (key_blockacq_count
< newspacq
->count
) {
2202 /* reset counter and do send message. */
2203 newspacq
->count
= 0;
2205 /* increment counter and do nothing. */
2210 /* make new entry for blocking to send SADB_ACQUIRE. */
2211 if ((newspacq
= key_newspacq(sp
->spidx
)) == NULL
)
2214 /* add to acqtree */
2215 LIST_INSERT_HEAD(&spacqtree
, newspacq
, chain
);
2219 /* create new sadb_msg to reply. */
2220 m
= key_setsadbmsg(SADB_X_SPDACQUIRE
, 0, 0, 0, 0, 0);
2227 /* set sadb_x_policy */
2229 m
= key_setsadbxpolicy(sp
->policy
, sp
->dir
, sp
->id
);
2237 result
->m_pkthdr
.len
= 0;
2238 for (m
= result
; m
; m
= m
->m_next
)
2239 result
->m_pkthdr
.len
+= m
->m_len
;
2241 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
2242 PFKEY_UNIT64(result
->m_pkthdr
.len
);
2244 return key_sendup_mbuf(NULL
, result
, KEY_SENDUP_REGISTERED
);
2253 * SADB_SPDFLUSH processing
2256 * from the user, and free all entries in secpctree.
2260 * NOTE: what to do is only marking SADB_SASTATE_DEAD.
2262 * m will always be freed.
2265 key_spdflush(struct socket
*so
, struct mbuf
*m
, const struct sadb_msghdr
*mhp
)
2267 struct sadb_msg
*newmsg
;
2268 struct secpolicy
*sp
, *nextsp
;
2271 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
2272 panic("key_spdflush: NULL pointer is passed.");
2274 if (m
->m_len
!= PFKEY_ALIGN8(sizeof(struct sadb_msg
)))
2275 return key_senderror(so
, m
, EINVAL
);
2277 for (sp
= TAILQ_FIRST(&sptailq
); sp
; sp
= nextsp
) {
2278 nextsp
= TAILQ_NEXT(sp
, tailq
);
2281 if (sp
->state
== IPSEC_SPSTATE_DEAD
)
2288 /* invalidate all cached SPD pointers on pcb */
2289 ipsec_invalpcbcacheall();
2291 if (sizeof(struct sadb_msg
) > m
->m_len
+ M_TRAILINGSPACE(m
)) {
2292 ipseclog((LOG_DEBUG
, "key_spdflush: No more memory.\n"));
2293 return key_senderror(so
, m
, ENOBUFS
);
2299 m
->m_pkthdr
.len
= m
->m_len
= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
2300 newmsg
= mtod(m
, struct sadb_msg
*);
2301 newmsg
->sadb_msg_errno
= 0;
2302 newmsg
->sadb_msg_len
= PFKEY_UNIT64(m
->m_pkthdr
.len
);
2304 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ALL
);
2308 * SADB_SPDDUMP processing
2311 * from the user, and dump all SP leaves
2316 * m will always be freed.
2319 key_spddump(struct socket
*so
, struct mbuf
*m
, const struct sadb_msghdr
*mhp
)
2321 struct secpolicy
*sp
;
2326 int error
= 0, needwait
= 0;
2329 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
2330 panic("key_spddump: NULL pointer is passed.");
2332 /* search SPD entry and get buffer size. */
2334 for (dir
= 0; dir
< IPSEC_DIR_MAX
; dir
++) {
2335 LIST_FOREACH(sp
, &sptree
[dir
], chain
) {
2341 return key_senderror(so
, m
, ENOENT
);
2343 for (dir
= 0; dir
< IPSEC_DIR_MAX
; dir
++) {
2344 LIST_FOREACH(sp
, &sptree
[dir
], chain
) {
2346 n
= key_setdumpsp(sp
, SADB_X_SPDDUMP
, cnt
,
2347 mhp
->msg
->sadb_msg_pid
);
2350 error
= key_sendup_mbuf(so
, n
,
2351 KEY_SENDUP_ONE
| KEY_SENDUP_CANWAIT
);
2352 if (error
== EAGAIN
)
2358 kp
= (struct keycb
*)sotorawcb(so
);
2359 while (needwait
&& kp
->kp_queue
)
2360 sbwait(&so
->so_rcv
);
2368 * SADB_X_NAT_T_NEW_MAPPING. Unused by racoon as of 2005/04/23
2371 key_nat_map(struct socket
*so
, struct mbuf
*m
, const struct sadb_msghdr
*mhp
)
2373 struct sadb_x_nat_t_type
*type
;
2374 struct sadb_x_nat_t_port
*sport
;
2375 struct sadb_x_nat_t_port
*dport
;
2376 struct sadb_address
*addr
;
2377 struct sadb_x_nat_t_frag
*frag
;
2380 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
2381 panic("key_nat_map: NULL pointer is passed.");
2383 if (mhp
->ext
[SADB_X_EXT_NAT_T_TYPE
] == NULL
||
2384 mhp
->ext
[SADB_X_EXT_NAT_T_SPORT
] == NULL
||
2385 mhp
->ext
[SADB_X_EXT_NAT_T_DPORT
] == NULL
) {
2386 ipseclog((LOG_DEBUG
, "key_nat_map: invalid message.\n"));
2387 return key_senderror(so
, m
, EINVAL
);
2389 if ((mhp
->extlen
[SADB_X_EXT_NAT_T_TYPE
] < sizeof(*type
)) ||
2390 (mhp
->extlen
[SADB_X_EXT_NAT_T_SPORT
] < sizeof(*sport
)) ||
2391 (mhp
->extlen
[SADB_X_EXT_NAT_T_DPORT
] < sizeof(*dport
))) {
2392 ipseclog((LOG_DEBUG
, "key_nat_map: invalid message.\n"));
2393 return key_senderror(so
, m
, EINVAL
);
2396 if ((mhp
->ext
[SADB_X_EXT_NAT_T_OA
] != NULL
) &&
2397 (mhp
->extlen
[SADB_X_EXT_NAT_T_OA
] < sizeof(*addr
))) {
2398 ipseclog((LOG_DEBUG
, "key_nat_map: invalid message\n"));
2399 return key_senderror(so
, m
, EINVAL
);
2402 if ((mhp
->ext
[SADB_X_EXT_NAT_T_FRAG
] != NULL
) &&
2403 (mhp
->extlen
[SADB_X_EXT_NAT_T_FRAG
] < sizeof(*frag
))) {
2404 ipseclog((LOG_DEBUG
, "key_nat_map: invalid message\n"));
2405 return key_senderror(so
, m
, EINVAL
);
2408 type
= (struct sadb_x_nat_t_type
*)mhp
->ext
[SADB_X_EXT_NAT_T_TYPE
];
2409 sport
= (struct sadb_x_nat_t_port
*)mhp
->ext
[SADB_X_EXT_NAT_T_SPORT
];
2410 dport
= (struct sadb_x_nat_t_port
*)mhp
->ext
[SADB_X_EXT_NAT_T_DPORT
];
2411 addr
= (struct sadb_address
*)mhp
->ext
[SADB_X_EXT_NAT_T_OA
];
2412 frag
= (struct sadb_x_nat_t_frag
*) mhp
->ext
[SADB_X_EXT_NAT_T_FRAG
];
2414 printf("sadb_nat_map called\n");
2417 * XXX handle that, it should also contain a SA, or anything
2418 * that enable to update the SA information.
2423 #endif /* IPSEC_NAT_T */
2425 static struct mbuf
*
2426 key_setspddump(int *errorp
)
2428 struct secpolicy
*sp
;
2433 /* search SPD entry and get buffer size. */
2435 for (dir
= 0; dir
< IPSEC_DIR_MAX
; dir
++) {
2436 LIST_FOREACH(sp
, &sptree
[dir
], chain
) {
2447 for (dir
= 0; dir
< IPSEC_DIR_MAX
; dir
++) {
2448 LIST_FOREACH(sp
, &sptree
[dir
], chain
) {
2450 n
= key_setdumpsp(sp
, SADB_X_SPDDUMP
, cnt
, 0);
2460 m
->m_pkthdr
.len
+= n
->m_pkthdr
.len
;
2471 key_setdumpsp(struct secpolicy
*sp
, u_int8_t type
, u_int32_t seq
, u_int32_t pid
)
2473 struct mbuf
*result
= NULL
, *m
;
2475 m
= key_setsadbmsg(type
, 0, SADB_SATYPE_UNSPEC
, seq
, pid
, sp
->refcnt
);
2481 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
2482 (struct sockaddr
*)&sp
->spidx
->src
, sp
->spidx
->prefs
,
2483 sp
->spidx
->ul_proto
);
2488 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
2489 (struct sockaddr
*)&sp
->spidx
->dst
, sp
->spidx
->prefd
,
2490 sp
->spidx
->ul_proto
);
2495 #ifdef SADB_X_EXT_TAG
2497 m
= key_setsadbxtag(sp
->tag
);
2509 m
= key_setsadblifetime(SADB_EXT_LIFETIME_CURRENT
,
2510 0, 0, (u_int64_t
)sp
->created
, (u_int64_t
)sp
->lastused
);
2515 m
= key_setsadblifetime(SADB_EXT_LIFETIME_HARD
,
2516 0, 0, (u_int64_t
)sp
->lifetime
, (u_int64_t
)sp
->validtime
);
2521 if ((result
->m_flags
& M_PKTHDR
) == 0)
2524 if (result
->m_len
< sizeof(struct sadb_msg
)) {
2525 result
= m_pullup(result
, sizeof(struct sadb_msg
));
2530 result
->m_pkthdr
.len
= 0;
2531 for (m
= result
; m
; m
= m
->m_next
)
2532 result
->m_pkthdr
.len
+= m
->m_len
;
2534 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
2535 PFKEY_UNIT64(result
->m_pkthdr
.len
);
2545 * get PFKEY message length for security policy and request.
2548 key_getspreqmsglen(struct secpolicy
*sp
)
2552 tlen
= sizeof(struct sadb_x_policy
);
2554 /* if is the policy for ipsec ? */
2555 if (sp
->policy
!= IPSEC_POLICY_IPSEC
)
2558 /* get length of ipsec requests */
2560 struct ipsecrequest
*isr
;
2563 for (isr
= sp
->req
; isr
!= NULL
; isr
= isr
->next
) {
2564 len
= sizeof(struct sadb_x_ipsecrequest
)
2565 + isr
->saidx
.src
.ss_len
2566 + isr
->saidx
.dst
.ss_len
;
2568 tlen
+= PFKEY_ALIGN8(len
);
2576 * SADB_X_SPDEXPIRE processing
2578 * <base, address(SD), lifetime(CH), policy>
2582 * others : error number
2585 key_spdexpire(struct secpolicy
*sp
)
2588 struct mbuf
*result
= NULL
, *m
;
2591 struct sadb_lifetime
*lt
;
2593 /* XXX: Why do we lock ? */
2594 s
= splsoftnet(); /*called from softclock()*/
2598 panic("key_spdexpire: NULL pointer is passed.");
2600 /* set msg header */
2601 m
= key_setsadbmsg(SADB_X_SPDEXPIRE
, 0, 0, 0, 0, 0);
2608 /* create lifetime extension (current and hard) */
2609 len
= PFKEY_ALIGN8(sizeof(*lt
)) * 2;
2610 m
= key_alloc_mbuf(len
);
2611 if (!m
|| m
->m_next
) { /*XXX*/
2617 memset(mtod(m
, void *), 0, len
);
2618 lt
= mtod(m
, struct sadb_lifetime
*);
2619 lt
->sadb_lifetime_len
= PFKEY_UNIT64(sizeof(struct sadb_lifetime
));
2620 lt
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_CURRENT
;
2621 lt
->sadb_lifetime_allocations
= 0;
2622 lt
->sadb_lifetime_bytes
= 0;
2623 lt
->sadb_lifetime_addtime
= sp
->created
;
2624 lt
->sadb_lifetime_usetime
= sp
->lastused
;
2625 lt
= (struct sadb_lifetime
*)(mtod(m
, char *) + len
/ 2);
2626 lt
->sadb_lifetime_len
= PFKEY_UNIT64(sizeof(struct sadb_lifetime
));
2627 lt
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_HARD
;
2628 lt
->sadb_lifetime_allocations
= 0;
2629 lt
->sadb_lifetime_bytes
= 0;
2630 lt
->sadb_lifetime_addtime
= sp
->lifetime
;
2631 lt
->sadb_lifetime_usetime
= sp
->validtime
;
2634 /* set sadb_address for source */
2636 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
2637 (struct sockaddr
*)&sp
->spidx
->src
,
2638 sp
->spidx
->prefs
, sp
->spidx
->ul_proto
);
2645 /* set sadb_address for destination */
2646 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
2647 (struct sockaddr
*)&sp
->spidx
->dst
,
2648 sp
->spidx
->prefd
, sp
->spidx
->ul_proto
);
2655 #ifdef SADB_X_EXT_TAG
2657 m
= key_setsadbxtag(sp
->tag
);
2674 if ((result
->m_flags
& M_PKTHDR
) == 0) {
2679 if (result
->m_len
< sizeof(struct sadb_msg
)) {
2680 result
= m_pullup(result
, sizeof(struct sadb_msg
));
2681 if (result
== NULL
) {
2687 result
->m_pkthdr
.len
= 0;
2688 for (m
= result
; m
; m
= m
->m_next
)
2689 result
->m_pkthdr
.len
+= m
->m_len
;
2691 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
2692 PFKEY_UNIT64(result
->m_pkthdr
.len
);
2695 return key_sendup_mbuf(NULL
, result
, KEY_SENDUP_REGISTERED
);
2704 /* %%% SAD management */
2706 * allocating a memory for new SA head, and copy from the values of mhp.
2707 * OUT: NULL : failure due to the lack of memory.
2708 * others : pointer to new SA head.
2710 static struct secashead
*
2711 key_newsah(struct secasindex
*saidx
)
2713 struct secashead
*newsah
;
2717 panic("key_newsaidx: NULL pointer is passed.");
2719 newsah
= keydb_newsecashead();
2723 memcpy(&newsah
->saidx
, saidx
, sizeof(newsah
->saidx
));
2725 /* add to saidxtree */
2726 newsah
->state
= SADB_SASTATE_MATURE
;
2727 LIST_INSERT_HEAD(&sahtree
, newsah
, chain
);
2733 * delete SA index and all SA registerd.
2736 key_delsah(struct secashead
*sah
)
2738 struct secasvar
*sav
, *nextsav
;
2739 u_int stateidx
, state
;
2745 panic("key_delsah: NULL pointer is passed.");
2747 s
= splsoftnet(); /*called from softclock()*/
2749 /* searching all SA registerd in the secindex. */
2751 stateidx
< _ARRAYLEN(saorder_state_any
);
2754 state
= saorder_state_any
[stateidx
];
2755 for (sav
= LIST_FIRST(&sah
->savtree
[state
]);
2759 nextsav
= LIST_NEXT(sav
, chain
);
2761 if (sav
->refcnt
> 0) {
2762 /* give up to delete this sa */
2768 KEY_CHKSASTATE(state
, sav
->state
, "key_delsah");
2770 /* remove back pointer */
2779 /* delete sah only if there's no sav. */
2785 rtcache_free(&sah
->sa_route
);
2787 /* remove from tree of SA index */
2788 if (__LIST_CHAINED(sah
))
2789 LIST_REMOVE(sah
, chain
);
2798 * allocating a new SA with LARVAL state. key_add() and key_getspi() call,
2799 * and copy the values of mhp into new buffer.
2800 * When SAD message type is GETSPI:
2801 * to set sequence number from acq_seq++,
2802 * to set zero to SPI.
2803 * not to call key_setsava().
2805 * others : pointer to new secasvar.
2807 * does not modify mbuf. does not free mbuf on error.
2809 static struct secasvar
*
2810 key_newsav(struct mbuf
*m
, const struct sadb_msghdr
*mhp
, struct secashead
*sah
, int *errp
)
2812 struct secasvar
*newsav
;
2813 const struct sadb_sa
*xsa
;
2816 if (m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
|| sah
== NULL
)
2817 panic("key_newsa: NULL pointer is passed.");
2819 newsav
= keydb_newsecasvar();
2820 if (newsav
== NULL
) {
2821 ipseclog((LOG_DEBUG
, "key_newsa: No more memory.\n"));
2826 switch (mhp
->msg
->sadb_msg_type
) {
2828 key_setspi(newsav
, 0);
2830 #ifdef IPSEC_DOSEQCHECK
2831 /* sync sequence number */
2832 if (mhp
->msg
->sadb_msg_seq
== 0)
2834 (acq_seq
= (acq_seq
== ~0 ? 1 : ++acq_seq
));
2837 newsav
->seq
= mhp
->msg
->sadb_msg_seq
;
2842 if (mhp
->ext
[SADB_EXT_SA
] == NULL
) {
2844 ipseclog((LOG_DEBUG
, "key_newsa: invalid message is passed.\n"));
2848 xsa
= (const struct sadb_sa
*)mhp
->ext
[SADB_EXT_SA
];
2849 key_setspi(newsav
, xsa
->sadb_sa_spi
);
2850 newsav
->seq
= mhp
->msg
->sadb_msg_seq
;
2858 /* copy sav values */
2859 if (mhp
->msg
->sadb_msg_type
!= SADB_GETSPI
) {
2860 *errp
= key_setsaval(newsav
, m
, mhp
);
2868 newsav
->created
= time_second
;
2870 newsav
->pid
= mhp
->msg
->sadb_msg_pid
;
2875 newsav
->state
= SADB_SASTATE_LARVAL
;
2876 LIST_INSERT_TAIL(&sah
->savtree
[SADB_SASTATE_LARVAL
], newsav
,
2886 * others : found, pointer to a SA.
2888 static struct secashead
*
2889 key_getsah(struct secasindex
*saidx
)
2891 struct secashead
*sah
;
2893 LIST_FOREACH(sah
, &sahtree
, chain
) {
2894 if (sah
->state
== SADB_SASTATE_DEAD
)
2896 if (key_cmpsaidx_exactly(&sah
->saidx
, saidx
))
2904 * check not to be duplicated SPI.
2905 * NOTE: this function is too slow due to searching all SAD.
2908 * others : found, pointer to a SA.
2910 static struct secasvar
*
2911 key_checkspidup(struct secasindex
*saidx
, u_int32_t spi
)
2913 struct secasvar
*sav
;
2914 u_int stateidx
, state
;
2916 /* check address family */
2917 if (saidx
->src
.ss_family
!= saidx
->dst
.ss_family
) {
2918 ipseclog((LOG_DEBUG
, "key_checkspidup: address family mismatched.\n"));
2923 LIST_FOREACH(sav
, &spihash
[SPIHASH(spi
)], spihash
) {
2924 if (sav
->spi
!= spi
)
2927 stateidx
< _ARRAYLEN(saorder_state_alive
);
2929 state
= saorder_state_alive
[stateidx
];
2930 if (sav
->state
== state
&&
2931 key_ismyaddr((struct sockaddr
*)&sav
->sah
->saidx
.dst
))
2940 key_setspi(struct secasvar
*sav
, u_int32_t spi
)
2946 if (sav
->spihash
.le_prev
|| sav
->spihash
.le_next
)
2947 LIST_REMOVE(sav
, spihash
);
2948 LIST_INSERT_HEAD(&spihash
[SPIHASH(spi
)], sav
, spihash
);
2953 * search SAD litmited alive SA, protocol, SPI.
2956 * others : found, pointer to a SA.
2958 static struct secasvar
*
2959 key_getsavbyspi(struct secashead
*sah
, u_int32_t spi
)
2961 struct secasvar
*sav
, *match
;
2962 u_int stateidx
, state
, matchidx
;
2965 matchidx
= _ARRAYLEN(saorder_state_alive
);
2966 LIST_FOREACH(sav
, &spihash
[SPIHASH(spi
)], spihash
) {
2967 if (sav
->spi
!= spi
)
2969 if (sav
->sah
!= sah
)
2971 for (stateidx
= 0; stateidx
< matchidx
; stateidx
++) {
2972 state
= saorder_state_alive
[stateidx
];
2973 if (sav
->state
== state
) {
2975 matchidx
= stateidx
;
2985 * copy SA values from PF_KEY message except *SPI, SEQ, PID, STATE and TYPE*.
2986 * You must update these if need.
2990 * does not modify mbuf. does not free mbuf on error.
2993 key_setsaval(struct secasvar
*sav
, struct mbuf
*m
, const struct sadb_msghdr
*mhp
)
2996 const struct esp_algorithm
*algo
;
3001 if (m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
3002 panic("key_setsaval: NULL pointer is passed.");
3004 /* initialization */
3006 sav
->key_auth
= NULL
;
3007 sav
->key_enc
= NULL
;
3020 if (mhp
->ext
[SADB_EXT_SA
] != NULL
) {
3021 const struct sadb_sa
*sa0
;
3023 sa0
= (const struct sadb_sa
*)mhp
->ext
[SADB_EXT_SA
];
3024 if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(*sa0
)) {
3029 sav
->alg_auth
= sa0
->sadb_sa_auth
;
3030 sav
->alg_enc
= sa0
->sadb_sa_encrypt
;
3031 sav
->flags
= sa0
->sadb_sa_flags
;
3034 if ((sa0
->sadb_sa_flags
& SADB_X_EXT_OLD
) == 0) {
3035 sav
->replay
= keydb_newsecreplay(sa0
->sadb_sa_replay
);
3036 if (sav
->replay
== NULL
) {
3037 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
3044 /* Authentication keys */
3045 if (mhp
->ext
[SADB_EXT_KEY_AUTH
] != NULL
) {
3046 const struct sadb_key
*key0
;
3049 key0
= (const struct sadb_key
*)mhp
->ext
[SADB_EXT_KEY_AUTH
];
3050 len
= mhp
->extlen
[SADB_EXT_KEY_AUTH
];
3053 if (len
< sizeof(*key0
)) {
3057 switch (mhp
->msg
->sadb_msg_satype
) {
3058 case SADB_SATYPE_AH
:
3059 case SADB_SATYPE_ESP
:
3060 case SADB_X_SATYPE_TCPSIGNATURE
:
3061 if (len
== PFKEY_ALIGN8(sizeof(struct sadb_key
)) &&
3062 sav
->alg_auth
!= SADB_X_AALG_NULL
)
3065 case SADB_X_SATYPE_IPCOMP
:
3071 ipseclog((LOG_DEBUG
, "key_setsaval: invalid key_auth values.\n"));
3075 sav
->key_auth
= (struct sadb_key
*)key_newbuf(key0
, len
);
3076 if (sav
->key_auth
== NULL
) {
3077 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
3083 /* Encryption key */
3084 if (mhp
->ext
[SADB_EXT_KEY_ENCRYPT
] != NULL
) {
3085 const struct sadb_key
*key0
;
3088 key0
= (const struct sadb_key
*)mhp
->ext
[SADB_EXT_KEY_ENCRYPT
];
3089 len
= mhp
->extlen
[SADB_EXT_KEY_ENCRYPT
];
3092 if (len
< sizeof(*key0
)) {
3096 switch (mhp
->msg
->sadb_msg_satype
) {
3097 case SADB_SATYPE_ESP
:
3098 if (len
== PFKEY_ALIGN8(sizeof(struct sadb_key
)) &&
3099 sav
->alg_enc
!= SADB_EALG_NULL
) {
3103 sav
->key_enc
= (struct sadb_key
*)key_newbuf(key0
, len
);
3104 if (sav
->key_enc
== NULL
) {
3105 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
3110 case SADB_X_SATYPE_IPCOMP
:
3111 if (len
!= PFKEY_ALIGN8(sizeof(struct sadb_key
)))
3113 sav
->key_enc
= NULL
; /*just in case*/
3115 case SADB_SATYPE_AH
:
3116 case SADB_X_SATYPE_TCPSIGNATURE
:
3122 ipseclog((LOG_DEBUG
, "key_setsatval: invalid key_enc value.\n"));
3130 switch (mhp
->msg
->sadb_msg_satype
) {
3131 case SADB_SATYPE_ESP
:
3133 algo
= esp_algorithm_lookup(sav
->alg_enc
);
3134 if (algo
&& algo
->ivlen
)
3135 sav
->ivlen
= (*algo
->ivlen
)(algo
, sav
);
3136 if (sav
->ivlen
== 0)
3138 KMALLOC(sav
->iv
, void *, sav
->ivlen
);
3140 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
3146 key_randomfill(sav
->iv
, sav
->ivlen
);
3149 case SADB_SATYPE_AH
:
3151 case SADB_X_SATYPE_IPCOMP
:
3153 case SADB_X_SATYPE_TCPSIGNATURE
:
3154 if (sav
->alg_enc
!= SADB_EALG_NONE
) {
3155 ipseclog((LOG_DEBUG
, "key_setsaval: protocol and "
3156 "algorithm mismatched.\n"));
3162 ipseclog((LOG_DEBUG
, "key_setsaval: invalid SA type.\n"));
3168 sav
->created
= time_second
;
3170 /* make lifetime for CURRENT */
3171 KMALLOC(sav
->lft_c
, struct sadb_lifetime
*,
3172 sizeof(struct sadb_lifetime
));
3173 if (sav
->lft_c
== NULL
) {
3174 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
3179 sav
->lft_c
->sadb_lifetime_len
=
3180 PFKEY_UNIT64(sizeof(struct sadb_lifetime
));
3181 sav
->lft_c
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_CURRENT
;
3182 sav
->lft_c
->sadb_lifetime_allocations
= 0;
3183 sav
->lft_c
->sadb_lifetime_bytes
= 0;
3184 sav
->lft_c
->sadb_lifetime_addtime
= time_second
;
3185 sav
->lft_c
->sadb_lifetime_usetime
= 0;
3187 /* lifetimes for HARD and SOFT */
3189 const struct sadb_lifetime
*lft0
;
3191 lft0
= (struct sadb_lifetime
*)mhp
->ext
[SADB_EXT_LIFETIME_HARD
];
3193 if (mhp
->extlen
[SADB_EXT_LIFETIME_HARD
] < sizeof(*lft0
)) {
3197 sav
->lft_h
= (struct sadb_lifetime
*)key_newbuf(lft0
,
3199 if (sav
->lft_h
== NULL
) {
3200 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
3204 /* we no longer support byte lifetime */
3205 if (sav
->lft_h
->sadb_lifetime_bytes
) {
3212 lft0
= (struct sadb_lifetime
*)mhp
->ext
[SADB_EXT_LIFETIME_SOFT
];
3214 if (mhp
->extlen
[SADB_EXT_LIFETIME_SOFT
] < sizeof(*lft0
)) {
3218 sav
->lft_s
= (struct sadb_lifetime
*)key_newbuf(lft0
,
3220 if (sav
->lft_s
== NULL
) {
3221 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
3225 /* we no longer support byte lifetime */
3226 if (sav
->lft_s
->sadb_lifetime_bytes
) {
3237 /* initialization */
3238 if (sav
->replay
!= NULL
) {
3239 keydb_delsecreplay(sav
->replay
);
3242 if (sav
->key_auth
!= NULL
) {
3243 memset(_KEYBUF(sav
->key_auth
), 0, _KEYLEN(sav
->key_auth
));
3244 KFREE(sav
->key_auth
);
3245 sav
->key_auth
= NULL
;
3247 if (sav
->key_enc
!= NULL
) {
3248 memset(_KEYBUF(sav
->key_enc
), 0, _KEYLEN(sav
->key_enc
));
3249 KFREE(sav
->key_enc
);
3250 sav
->key_enc
= NULL
;
3253 memset(sav
->sched
, 0, sav
->schedlen
);
3257 if (sav
->iv
!= NULL
) {
3261 if (sav
->lft_c
!= NULL
) {
3265 if (sav
->lft_h
!= NULL
) {
3269 if (sav
->lft_s
!= NULL
) {
3278 * validation with a secasvar entry, and set SADB_SATYPE_MATURE.
3283 key_mature(struct secasvar
*sav
)
3286 int checkmask
= 0; /* 2^0: ealg 2^1: aalg 2^2: calg */
3287 int mustmask
= 0; /* 2^0: ealg 2^1: aalg 2^2: calg */
3291 /* check SPI value */
3292 switch (sav
->sah
->saidx
.proto
) {
3295 if (ntohl(sav
->spi
) <= 255) {
3296 ipseclog((LOG_DEBUG
,
3297 "key_mature: illegal range of SPI %u.\n",
3298 (u_int32_t
)ntohl(sav
->spi
)));
3303 if (ntohl(sav
->spi
) != 0x1000) { /*TCP_SIG_SPI*/
3304 ipseclog((LOG_DEBUG
,
3305 "key_mature: SPI must be 0x1000 for TCPMD5.\n"));
3315 switch (sav
->sah
->saidx
.proto
) {
3319 if ((sav
->flags
& SADB_X_EXT_OLD
) &&
3320 (sav
->flags
& SADB_X_EXT_DERIV
)) {
3321 ipseclog((LOG_DEBUG
, "key_mature: "
3322 "invalid flag (derived) given to old-esp.\n"));
3325 if (sav
->alg_auth
== SADB_AALG_NONE
)
3334 if (sav
->flags
& SADB_X_EXT_DERIV
) {
3335 ipseclog((LOG_DEBUG
, "key_mature: "
3336 "invalid flag (derived) given to AH SA.\n"));
3339 if (sav
->alg_enc
!= SADB_EALG_NONE
) {
3340 ipseclog((LOG_DEBUG
, "key_mature: "
3341 "protocol and algorithm mismated.\n"));
3347 case IPPROTO_IPCOMP
:
3348 if (sav
->alg_auth
!= SADB_AALG_NONE
) {
3349 ipseclog((LOG_DEBUG
, "key_mature: "
3350 "protocol and algorithm mismated.\n"));
3353 if ((sav
->flags
& SADB_X_EXT_RAWCPI
) == 0 &&
3354 ntohl(sav
->spi
) >= 0x10000) {
3355 ipseclog((LOG_DEBUG
,
3356 "key_mature: invalid cpi for IPComp.\n"));
3363 if (sav
->alg_enc
!= SADB_EALG_NONE
) {
3364 ipseclog((LOG_DEBUG
, "key_mature: "
3365 "encryption algorithm must be null for TCPMD5.\n"));
3368 if (sav
->alg_auth
!= SADB_X_AALG_TCP_MD5
) {
3369 ipseclog((LOG_DEBUG
, "key_mature: "
3370 "auth algorithm must be tcp-md5 for TCPMD5.\n"));
3379 ipseclog((LOG_DEBUG
, "key_mature: Invalid satype.\n"));
3380 return EPROTONOSUPPORT
;
3383 /* check authentication algorithm */
3384 if ((checkmask
& 2) != 0) {
3385 const struct ah_algorithm
*algo
;
3388 algo
= ah_algorithm_lookup(sav
->alg_auth
);
3390 ipseclog((LOG_DEBUG
,"key_mature: "
3391 "unknown authentication algorithm.\n"));
3395 /* algorithm-dependent check */
3397 keylen
= sav
->key_auth
->sadb_key_bits
;
3400 if (keylen
< algo
->keymin
|| algo
->keymax
< keylen
) {
3401 ipseclog((LOG_DEBUG
,
3402 "key_mature: invalid AH key length %d "
3403 "(%d-%d allowed)\n",
3404 keylen
, algo
->keymin
, algo
->keymax
));
3409 if ((*algo
->mature
)(sav
)) {
3410 /* message generated in per-algorithm function*/
3413 mature
= SADB_SATYPE_AH
;
3416 if ((mustmask
& 2) != 0 && mature
!= SADB_SATYPE_AH
) {
3417 ipseclog((LOG_DEBUG
, "key_mature: no satisfy algorithm for AH\n"));
3422 /* check encryption algorithm */
3423 if ((checkmask
& 1) != 0) {
3425 const struct esp_algorithm
*algo
;
3428 algo
= esp_algorithm_lookup(sav
->alg_enc
);
3430 ipseclog((LOG_DEBUG
, "key_mature: unknown encryption algorithm.\n"));
3434 /* algorithm-dependent check */
3436 keylen
= sav
->key_enc
->sadb_key_bits
;
3439 if (keylen
< algo
->keymin
|| algo
->keymax
< keylen
) {
3440 ipseclog((LOG_DEBUG
,
3441 "key_mature: invalid ESP key length %d "
3442 "(%d-%d allowed)\n",
3443 keylen
, algo
->keymin
, algo
->keymax
));
3448 if ((*algo
->mature
)(sav
)) {
3449 /* message generated in per-algorithm function*/
3452 mature
= SADB_SATYPE_ESP
;
3455 if ((mustmask
& 1) != 0 && mature
!= SADB_SATYPE_ESP
) {
3456 ipseclog((LOG_DEBUG
, "key_mature: no satisfy algorithm for ESP\n"));
3460 ipseclog((LOG_DEBUG
, "key_mature: ESP not supported in this configuration\n"));
3465 /* check compression algorithm */
3466 if ((checkmask
& 4) != 0) {
3467 const struct ipcomp_algorithm
*algo
;
3469 /* algorithm-dependent check */
3470 algo
= ipcomp_algorithm_lookup(sav
->alg_enc
);
3472 ipseclog((LOG_DEBUG
, "key_mature: unknown compression algorithm.\n"));
3477 key_sa_chgstate(sav
, SADB_SASTATE_MATURE
);
3482 * subroutine for SADB_GET and SADB_DUMP.
3484 static struct mbuf
*
3485 key_setdumpsa(struct secasvar
*sav
, u_int8_t type
, u_int8_t satype
, u_int32_t seq
, u_int32_t pid
)
3487 struct mbuf
*result
= NULL
, *tres
= NULL
, *m
;
3492 SADB_EXT_SA
, SADB_X_EXT_SA2
,
3493 SADB_EXT_LIFETIME_HARD
, SADB_EXT_LIFETIME_SOFT
,
3494 SADB_EXT_LIFETIME_CURRENT
, SADB_EXT_ADDRESS_SRC
,
3495 SADB_EXT_ADDRESS_DST
, SADB_EXT_ADDRESS_PROXY
, SADB_EXT_KEY_AUTH
,
3496 SADB_EXT_KEY_ENCRYPT
, SADB_EXT_IDENTITY_SRC
,
3497 SADB_EXT_IDENTITY_DST
, SADB_EXT_SENSITIVITY
,
3499 SADB_X_EXT_NAT_T_TYPE
, SADB_X_EXT_NAT_T_SPORT
,
3500 SADB_X_EXT_NAT_T_DPORT
, SADB_X_EXT_NAT_T_OA
,
3501 SADB_X_EXT_NAT_T_FRAG
,
3505 m
= key_setsadbmsg(type
, 0, satype
, seq
, pid
, sav
->refcnt
);
3510 for (i
= sizeof(dumporder
)/sizeof(dumporder
[0]) - 1; i
>= 0; i
--) {
3513 switch (dumporder
[i
]) {
3515 m
= key_setsadbsa(sav
);
3520 case SADB_X_EXT_SA2
:
3521 m
= key_setsadbxsa2(sav
->sah
->saidx
.mode
,
3522 sav
->replay
? (sav
->replay
->count
& 0xffffffff) : 0,
3523 sav
->sah
->saidx
.reqid
);
3528 case SADB_EXT_ADDRESS_SRC
:
3529 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
3530 (struct sockaddr
*)&sav
->sah
->saidx
.src
,
3531 FULLMASK
, IPSEC_ULPROTO_ANY
);
3536 case SADB_EXT_ADDRESS_DST
:
3537 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
3538 (struct sockaddr
*)&sav
->sah
->saidx
.dst
,
3539 FULLMASK
, IPSEC_ULPROTO_ANY
);
3544 case SADB_EXT_KEY_AUTH
:
3547 l
= PFKEY_UNUNIT64(sav
->key_auth
->sadb_key_len
);
3551 case SADB_EXT_KEY_ENCRYPT
:
3554 l
= PFKEY_UNUNIT64(sav
->key_enc
->sadb_key_len
);
3558 case SADB_EXT_LIFETIME_CURRENT
:
3561 l
= PFKEY_UNUNIT64(((struct sadb_ext
*)sav
->lft_c
)->sadb_ext_len
);
3565 case SADB_EXT_LIFETIME_HARD
:
3568 l
= PFKEY_UNUNIT64(((struct sadb_ext
*)sav
->lft_h
)->sadb_ext_len
);
3572 case SADB_EXT_LIFETIME_SOFT
:
3575 l
= PFKEY_UNUNIT64(((struct sadb_ext
*)sav
->lft_s
)->sadb_ext_len
);
3580 case SADB_X_EXT_NAT_T_TYPE
:
3581 if ((m
= key_setsadbxtype(sav
->natt_type
)) == NULL
)
3585 case SADB_X_EXT_NAT_T_DPORT
:
3586 if ((m
= key_setsadbxport(KEY_PORTFROMSADDR
3587 (&sav
->sah
->saidx
.dst
),
3588 SADB_X_EXT_NAT_T_DPORT
)) == NULL
)
3592 case SADB_X_EXT_NAT_T_SPORT
:
3593 if ((m
= key_setsadbxport(KEY_PORTFROMSADDR
3594 (&sav
->sah
->saidx
.src
),
3595 SADB_X_EXT_NAT_T_SPORT
)) == NULL
)
3599 case SADB_X_EXT_NAT_T_OA
:
3600 case SADB_X_EXT_NAT_T_FRAG
:
3603 case SADB_EXT_ADDRESS_PROXY
:
3604 case SADB_EXT_IDENTITY_SRC
:
3605 case SADB_EXT_IDENTITY_DST
:
3606 /* XXX: should we brought from SPD ? */
3607 case SADB_EXT_SENSITIVITY
:
3612 if ((!m
&& !p
) || (m
&& p
))
3615 M_PREPEND(tres
, l
, M_DONTWAIT
);
3618 memcpy(mtod(tres
, void *), p
, l
);
3622 m
= key_alloc_mbuf(l
);
3625 m_copyback(m
, 0, l
, p
);
3633 m_cat(result
, tres
);
3635 if (result
->m_len
< sizeof(struct sadb_msg
)) {
3636 result
= m_pullup(result
, sizeof(struct sadb_msg
));
3641 result
->m_pkthdr
.len
= 0;
3642 for (m
= result
; m
; m
= m
->m_next
)
3643 result
->m_pkthdr
.len
+= m
->m_len
;
3645 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
3646 PFKEY_UNIT64(result
->m_pkthdr
.len
);
3657 * set data into sadb_msg.
3659 static struct mbuf
*
3660 key_setsadbmsg(u_int8_t type
, u_int16_t tlen
, u_int8_t satype
, u_int32_t seq
, pid_t pid
, u_int16_t reserved
)
3666 len
= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
3669 MGETHDR(m
, M_DONTWAIT
, MT_DATA
);
3670 if (m
&& len
> MHLEN
) {
3671 MCLGET(m
, M_DONTWAIT
);
3672 if ((m
->m_flags
& M_EXT
) == 0) {
3679 m
->m_pkthdr
.len
= m
->m_len
= len
;
3682 p
= mtod(m
, struct sadb_msg
*);
3685 p
->sadb_msg_version
= PF_KEY_V2
;
3686 p
->sadb_msg_type
= type
;
3687 p
->sadb_msg_errno
= 0;
3688 p
->sadb_msg_satype
= satype
;
3689 p
->sadb_msg_len
= PFKEY_UNIT64(tlen
);
3690 p
->sadb_msg_reserved
= reserved
;
3691 p
->sadb_msg_seq
= seq
;
3692 p
->sadb_msg_pid
= (u_int32_t
)pid
;
3698 * copy secasvar data into sadb_address.
3700 static struct mbuf
*
3701 key_setsadbsa(struct secasvar
*sav
)
3707 len
= PFKEY_ALIGN8(sizeof(struct sadb_sa
));
3708 m
= key_alloc_mbuf(len
);
3709 if (!m
|| m
->m_next
) { /*XXX*/
3715 p
= mtod(m
, struct sadb_sa
*);
3718 p
->sadb_sa_len
= PFKEY_UNIT64(len
);
3719 p
->sadb_sa_exttype
= SADB_EXT_SA
;
3720 p
->sadb_sa_spi
= sav
->spi
;
3721 p
->sadb_sa_replay
= (sav
->replay
!= NULL
? sav
->replay
->wsize
: 0);
3722 p
->sadb_sa_state
= sav
->state
;
3723 p
->sadb_sa_auth
= sav
->alg_auth
;
3724 p
->sadb_sa_encrypt
= sav
->alg_enc
;
3725 p
->sadb_sa_flags
= sav
->flags
;
3731 * set data into sadb_address.
3733 static struct mbuf
*
3734 key_setsadbaddr(u_int16_t exttype
, struct sockaddr
*saddr
, u_int8_t prefixlen
, u_int16_t ul_proto
)
3737 struct sadb_address
*p
;
3740 len
= PFKEY_ALIGN8(sizeof(struct sadb_address
)) +
3741 PFKEY_ALIGN8(saddr
->sa_len
);
3742 m
= key_alloc_mbuf(len
);
3743 if (!m
|| m
->m_next
) { /*XXX*/
3749 p
= mtod(m
, struct sadb_address
*);
3752 p
->sadb_address_len
= PFKEY_UNIT64(len
);
3753 p
->sadb_address_exttype
= exttype
;
3754 p
->sadb_address_proto
= ul_proto
;
3755 if (prefixlen
== FULLMASK
) {
3756 switch (saddr
->sa_family
) {
3758 prefixlen
= sizeof(struct in_addr
) << 3;
3761 prefixlen
= sizeof(struct in6_addr
) << 3;
3767 p
->sadb_address_prefixlen
= prefixlen
;
3768 p
->sadb_address_reserved
= 0;
3770 memcpy(mtod(m
, char *) + PFKEY_ALIGN8(sizeof(struct sadb_address
)),
3771 saddr
, saddr
->sa_len
);
3778 * set data into sadb_ident.
3780 static struct mbuf
*
3781 key_setsadbident(u_int16_t exttype
, u_int16_t idtype
, void *string
, int stringlen
, u_int64_t id
)
3784 struct sadb_ident
*p
;
3787 len
= PFKEY_ALIGN8(sizeof(struct sadb_ident
)) + PFKEY_ALIGN8(stringlen
);
3788 m
= key_alloc_mbuf(len
);
3789 if (!m
|| m
->m_next
) { /*XXX*/
3795 p
= mtod(m
, struct sadb_ident
*);
3798 p
->sadb_ident_len
= PFKEY_UNIT64(len
);
3799 p
->sadb_ident_exttype
= exttype
;
3800 p
->sadb_ident_type
= idtype
;
3801 p
->sadb_ident_reserved
= 0;
3802 p
->sadb_ident_id
= id
;
3805 mtod(m
, void *) + PFKEY_ALIGN8(sizeof(struct sadb_ident
)),
3813 * set data into sadb_x_sa2.
3815 static struct mbuf
*
3816 key_setsadbxsa2(u_int8_t mode
, u_int32_t seq
, u_int16_t reqid
)
3819 struct sadb_x_sa2
*p
;
3822 len
= PFKEY_ALIGN8(sizeof(struct sadb_x_sa2
));
3823 m
= key_alloc_mbuf(len
);
3824 if (!m
|| m
->m_next
) { /*XXX*/
3830 p
= mtod(m
, struct sadb_x_sa2
*);
3833 p
->sadb_x_sa2_len
= PFKEY_UNIT64(len
);
3834 p
->sadb_x_sa2_exttype
= SADB_X_EXT_SA2
;
3835 p
->sadb_x_sa2_mode
= mode
;
3836 p
->sadb_x_sa2_reserved1
= 0;
3837 p
->sadb_x_sa2_reserved2
= 0;
3838 p
->sadb_x_sa2_sequence
= seq
;
3839 p
->sadb_x_sa2_reqid
= reqid
;
3844 #ifdef SADB_X_EXT_TAG
3846 * set data into sadb_x_tag.
3848 static struct mbuf
*
3849 key_setsadbxtag(u_int16_t tag
)
3852 struct sadb_x_tag
*p
;
3855 len
= PFKEY_ALIGN8(sizeof(struct sadb_x_tag
));
3856 m
= key_alloc_mbuf(len
);
3857 if (!m
|| m
->m_next
) { /*XXX*/
3863 p
= mtod(m
, struct sadb_x_tag
*);
3866 p
->sadb_x_tag_len
= PFKEY_UNIT64(len
);
3867 p
->sadb_x_tag_exttype
= SADB_X_EXT_TAG
;
3868 m_nametag_tag2tagname(tag
, p
->sadb_x_tag_name
);
3876 * set a type in sadb_x_nat_t_type
3878 static struct mbuf
*
3879 key_setsadbxtype(u_int16_t type
)
3883 struct sadb_x_nat_t_type
*p
;
3885 len
= PFKEY_ALIGN8(sizeof(struct sadb_x_nat_t_type
));
3887 m
= key_alloc_mbuf(len
);
3888 if (!m
|| m
->m_next
) { /*XXX*/
3894 p
= mtod(m
, struct sadb_x_nat_t_type
*);
3897 p
->sadb_x_nat_t_type_len
= PFKEY_UNIT64(len
);
3898 p
->sadb_x_nat_t_type_exttype
= SADB_X_EXT_NAT_T_TYPE
;
3899 p
->sadb_x_nat_t_type_type
= type
;
3904 * set a port in sadb_x_nat_t_port. port is in network order
3906 static struct mbuf
*
3907 key_setsadbxport(u_int16_t port
, u_int16_t type
)
3911 struct sadb_x_nat_t_port
*p
;
3913 len
= PFKEY_ALIGN8(sizeof(struct sadb_x_nat_t_port
));
3915 m
= key_alloc_mbuf(len
);
3916 if (!m
|| m
->m_next
) { /*XXX*/
3922 p
= mtod(m
, struct sadb_x_nat_t_port
*);
3925 p
->sadb_x_nat_t_port_len
= PFKEY_UNIT64(len
);
3926 p
->sadb_x_nat_t_port_exttype
= type
;
3927 p
->sadb_x_nat_t_port_port
= port
;
3933 * Get port from sockaddr, port is in network order
3936 key_portfromsaddr(struct sockaddr
*saddr
)
3940 switch (saddr
->sa_family
) {
3942 struct sockaddr_in
*sin
= (struct sockaddr_in
*)saddr
;
3944 port
= sin
->sin_port
;
3949 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)saddr
;
3951 port
= sin6
->sin6_port
;
3956 printf("key_portfromsaddr: unexpected address family\n");
3963 #endif /* IPSEC_NAT_T */
3966 * Set port is struct sockaddr. port is in network order
3969 key_porttosaddr(struct sockaddr
*saddr
, u_int16_t port
)
3971 switch (saddr
->sa_family
) {
3973 struct sockaddr_in
*sin
= (struct sockaddr_in
*)saddr
;
3975 sin
->sin_port
= port
;
3980 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)saddr
;
3982 sin6
->sin6_port
= port
;
3987 printf("key_porttosaddr: unexpected address family %d\n",
3996 * Safety check sa_len
3999 key_checksalen(const struct sockaddr
*saddr
)
4001 switch (saddr
->sa_family
) {
4003 if (saddr
->sa_len
!= sizeof(struct sockaddr_in
))
4008 if (saddr
->sa_len
!= sizeof(struct sockaddr_in6
))
4013 printf("key_checksalen: unexpected sa_family %d\n",
4023 * set data into sadb_lifetime
4025 static struct mbuf
*
4026 key_setsadblifetime(u_int16_t type
, u_int32_t alloc
, u_int64_t bytes
, u_int64_t addtime
, u_int64_t usetime
)
4029 struct sadb_lifetime
*p
;
4032 len
= PFKEY_ALIGN8(sizeof(struct sadb_lifetime
));
4033 m
= key_alloc_mbuf(len
);
4034 if (!m
|| m
->m_next
) { /*XXX*/
4040 p
= mtod(m
, struct sadb_lifetime
*);
4043 p
->sadb_lifetime_len
= PFKEY_UNIT64(len
);
4044 p
->sadb_lifetime_exttype
= type
;
4045 p
->sadb_lifetime_allocations
= alloc
;
4046 p
->sadb_lifetime_bytes
= bytes
;
4047 p
->sadb_lifetime_addtime
= addtime
;
4048 p
->sadb_lifetime_usetime
= usetime
;
4054 * set data into sadb_x_policy
4056 static struct mbuf
*
4057 key_setsadbxpolicy(u_int16_t type
, u_int8_t dir
, u_int32_t id
)
4060 struct sadb_x_policy
*p
;
4063 len
= PFKEY_ALIGN8(sizeof(struct sadb_x_policy
));
4064 m
= key_alloc_mbuf(len
);
4065 if (!m
|| m
->m_next
) { /*XXX*/
4071 p
= mtod(m
, struct sadb_x_policy
*);
4074 p
->sadb_x_policy_len
= PFKEY_UNIT64(len
);
4075 p
->sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
4076 p
->sadb_x_policy_type
= type
;
4077 p
->sadb_x_policy_dir
= dir
;
4078 p
->sadb_x_policy_id
= id
;
4085 * copy a buffer into the new buffer allocated.
4088 key_newbuf(const void *src
, u_int len
)
4092 KMALLOC(new, void *, len
);
4094 ipseclog((LOG_DEBUG
, "key_newbuf: No more memory.\n"));
4097 memcpy(new, src
, len
);
4102 /* compare my own address
4103 * OUT: 1: true, i.e. my address.
4107 key_ismyaddr(struct sockaddr
*sa
)
4110 struct sockaddr_in
*sin
;
4111 struct in_ifaddr
*ia
;
4116 panic("key_ismyaddr: NULL pointer is passed.");
4118 switch (sa
->sa_family
) {
4121 sin
= (struct sockaddr_in
*)sa
;
4122 TAILQ_FOREACH(ia
, &in_ifaddrhead
, ia_list
) {
4123 if (sin
->sin_family
== ia
->ia_addr
.sin_family
&&
4124 sin
->sin_len
== ia
->ia_addr
.sin_len
&&
4125 sin
->sin_addr
.s_addr
== ia
->ia_addr
.sin_addr
.s_addr
)
4134 return key_ismyaddr6((struct sockaddr_in6
*)sa
);
4143 * compare my own address for IPv6.
4146 * NOTE: derived ip6_input() in KAME. This is necessary to modify more.
4150 key_ismyaddr6(struct sockaddr_in6
*sin6
)
4152 struct in6_ifaddr
*ia
;
4153 struct in6_multi
*in6m
;
4155 if (sa6_embedscope(sin6
, 0) != 0)
4158 for (ia
= in6_ifaddr
; ia
; ia
= ia
->ia_next
) {
4159 if (key_sockaddrcmp((struct sockaddr
*)&sin6
,
4160 (struct sockaddr
*)&ia
->ia_addr
, 0) == 0)
4165 * XXX why do we care about multlicast here while we don't care
4166 * about IPv4 multicast??
4170 for ((in6m
) = ia
->ia6_multiaddrs
.lh_first
;
4172 !IN6_ARE_ADDR_EQUAL(&(in6m
)->in6m_addr
, &sin6
->sin6_addr
);
4173 (in6m
) = in6m
->in6m_entry
.le_next
)
4179 /* loopback, just for safety */
4180 if (IN6_IS_ADDR_LOOPBACK(&sin6
->sin6_addr
))
4188 * compare two secasindex structure exactly.
4190 * saidx0: source, it can be in SAD.
4197 key_cmpsaidx_exactly(struct secasindex
*saidx0
, struct secasindex
*saidx1
)
4200 if (saidx0
== NULL
&& saidx1
== NULL
)
4203 if (saidx0
== NULL
|| saidx1
== NULL
)
4206 if (saidx0
->proto
!= saidx1
->proto
|| saidx0
->mode
!= saidx1
->mode
||
4207 saidx0
->reqid
!= saidx1
->reqid
)
4210 if (memcmp(&saidx0
->src
, &saidx1
->src
, saidx0
->src
.ss_len
) != 0 ||
4211 memcmp(&saidx0
->dst
, &saidx1
->dst
, saidx0
->dst
.ss_len
) != 0)
4218 * compare two secasindex structure with consideration mode.
4219 * don't compare port.
4221 * saidx0: source, it is often in SAD.
4222 * saidx1: object, it is often from SPD.
4228 key_cmpsaidx_withmode(struct secasindex
*saidx0
, struct secasindex
*saidx1
)
4233 if (saidx0
== NULL
&& saidx1
== NULL
)
4236 if (saidx0
== NULL
|| saidx1
== NULL
)
4239 if (saidx0
->proto
!= saidx1
->proto
)
4243 * If NAT-T is enabled, check ports for tunnel mode.
4244 * Don't do it for transport mode, as there is no
4245 * port information available in the SP.
4248 if (saidx1
->mode
== IPSEC_MODE_TUNNEL
)
4252 * If reqid of SPD is non-zero, unique SA is required.
4253 * The result must be of same reqid in this case.
4255 if (saidx1
->reqid
!= 0 && saidx0
->reqid
!= saidx1
->reqid
)
4258 if (saidx0
->mode
!= IPSEC_MODE_ANY
&& saidx0
->mode
!= saidx1
->mode
)
4261 if (key_sockaddrcmp((struct sockaddr
*)&saidx0
->src
,
4262 (struct sockaddr
*)&saidx1
->src
, chkport
) != 0) {
4265 if (key_sockaddrcmp((struct sockaddr
*)&saidx0
->dst
,
4266 (struct sockaddr
*)&saidx1
->dst
, chkport
) != 0) {
4274 * compare two secasindex structure without mode.
4275 * don't compare port.
4277 * saidx0: source, it is often in SAD.
4278 * saidx1: object, it is often from user.
4284 key_cmpsaidx_withoutmode(struct secasindex
*saidx0
, struct secasindex
*saidx1
)
4293 if (saidx0
== NULL
&& saidx1
== NULL
)
4296 if (saidx0
== NULL
|| saidx1
== NULL
)
4299 if (saidx0
->proto
!= saidx1
->proto
)
4302 if (key_sockaddrcmp((struct sockaddr
*)&saidx0
->src
,
4303 (struct sockaddr
*)&saidx1
->src
, chkport
) != 0) {
4306 if (key_sockaddrcmp((struct sockaddr
*)&saidx0
->dst
,
4307 (struct sockaddr
*)&saidx1
->dst
, chkport
) != 0) {
4315 * compare two secindex structure exactly.
4317 * spidx0: source, it is often in SPD.
4318 * spidx1: object, it is often from PFKEY message.
4324 key_cmpspidx_exactly(struct secpolicyindex
*spidx0
, struct secpolicyindex
*spidx1
)
4327 if (spidx0
== NULL
&& spidx1
== NULL
)
4330 if (spidx0
== NULL
|| spidx1
== NULL
)
4333 if (spidx0
->prefs
!= spidx1
->prefs
|| spidx0
->prefd
!= spidx1
->prefd
||
4334 spidx0
->ul_proto
!= spidx1
->ul_proto
)
4337 if (key_sockaddrcmp((struct sockaddr
*)&spidx0
->src
,
4338 (struct sockaddr
*)&spidx1
->src
, 1) != 0) {
4341 if (key_sockaddrcmp((struct sockaddr
*)&spidx0
->dst
,
4342 (struct sockaddr
*)&spidx1
->dst
, 1) != 0) {
4350 * compare two secindex structure with mask.
4352 * spidx0: source, it is often in SPD.
4353 * spidx1: object, it is often from IP header.
4359 key_cmpspidx_withmask(struct secpolicyindex
*spidx0
, struct secpolicyindex
*spidx1
)
4362 if (spidx0
== NULL
&& spidx1
== NULL
)
4365 if (spidx0
== NULL
|| spidx1
== NULL
)
4368 if (spidx0
->src
.ss_family
!= spidx1
->src
.ss_family
||
4369 spidx0
->dst
.ss_family
!= spidx1
->dst
.ss_family
||
4370 spidx0
->src
.ss_len
!= spidx1
->src
.ss_len
||
4371 spidx0
->dst
.ss_len
!= spidx1
->dst
.ss_len
)
4374 /* if spidx.ul_proto == IPSEC_ULPROTO_ANY, ignore. */
4375 if (spidx0
->ul_proto
!= (u_int16_t
)IPSEC_ULPROTO_ANY
&&
4376 spidx0
->ul_proto
!= spidx1
->ul_proto
)
4379 switch (spidx0
->src
.ss_family
) {
4381 if (satosin(&spidx0
->src
)->sin_port
!= IPSEC_PORT_ANY
&&
4382 satosin(&spidx0
->src
)->sin_port
!=
4383 satosin(&spidx1
->src
)->sin_port
)
4385 if (!key_bbcmp((void *)&satosin(&spidx0
->src
)->sin_addr
,
4386 (void *)&satosin(&spidx1
->src
)->sin_addr
, spidx0
->prefs
))
4390 if (satosin6(&spidx0
->src
)->sin6_port
!= IPSEC_PORT_ANY
&&
4391 satosin6(&spidx0
->src
)->sin6_port
!=
4392 satosin6(&spidx1
->src
)->sin6_port
)
4395 * scope_id check. if sin6_scope_id is 0, we regard it
4396 * as a wildcard scope, which matches any scope zone ID.
4398 if (satosin6(&spidx0
->src
)->sin6_scope_id
&&
4399 satosin6(&spidx1
->src
)->sin6_scope_id
&&
4400 satosin6(&spidx0
->src
)->sin6_scope_id
!=
4401 satosin6(&spidx1
->src
)->sin6_scope_id
)
4403 if (!key_bbcmp((void *)&satosin6(&spidx0
->src
)->sin6_addr
,
4404 (void *)&satosin6(&spidx1
->src
)->sin6_addr
, spidx0
->prefs
))
4409 if (memcmp(&spidx0
->src
, &spidx1
->src
, spidx0
->src
.ss_len
) != 0)
4414 switch (spidx0
->dst
.ss_family
) {
4416 if (satosin(&spidx0
->dst
)->sin_port
!= IPSEC_PORT_ANY
&&
4417 satosin(&spidx0
->dst
)->sin_port
!=
4418 satosin(&spidx1
->dst
)->sin_port
)
4420 if (!key_bbcmp((void *)&satosin(&spidx0
->dst
)->sin_addr
,
4421 (void *)&satosin(&spidx1
->dst
)->sin_addr
, spidx0
->prefd
))
4425 if (satosin6(&spidx0
->dst
)->sin6_port
!= IPSEC_PORT_ANY
&&
4426 satosin6(&spidx0
->dst
)->sin6_port
!=
4427 satosin6(&spidx1
->dst
)->sin6_port
)
4430 * scope_id check. if sin6_scope_id is 0, we regard it
4431 * as a wildcard scope, which matches any scope zone ID.
4433 if (satosin6(&spidx0
->src
)->sin6_scope_id
&&
4434 satosin6(&spidx1
->src
)->sin6_scope_id
&&
4435 satosin6(&spidx0
->dst
)->sin6_scope_id
!=
4436 satosin6(&spidx1
->dst
)->sin6_scope_id
)
4438 if (!key_bbcmp((void *)&satosin6(&spidx0
->dst
)->sin6_addr
,
4439 (void *)&satosin6(&spidx1
->dst
)->sin6_addr
, spidx0
->prefd
))
4444 if (memcmp(&spidx0
->dst
, &spidx1
->dst
, spidx0
->dst
.ss_len
) != 0)
4449 /* XXX Do we check other field ? e.g. flowinfo */
4454 /* returns 0 on match */
4456 key_sockaddrcmp(struct sockaddr
*sa1
, struct sockaddr
*sa2
, int port
)
4458 if (sa1
->sa_family
!= sa2
->sa_family
|| sa1
->sa_len
!= sa2
->sa_len
)
4461 switch (sa1
->sa_family
) {
4463 if (sa1
->sa_len
!= sizeof(struct sockaddr_in
))
4465 if (satosin(sa1
)->sin_addr
.s_addr
!=
4466 satosin(sa2
)->sin_addr
.s_addr
) {
4469 if (port
&& satosin(sa1
)->sin_port
!= satosin(sa2
)->sin_port
)
4473 if (sa1
->sa_len
!= sizeof(struct sockaddr_in6
))
4474 return 1; /*EINVAL*/
4475 if (satosin6(sa1
)->sin6_scope_id
!=
4476 satosin6(sa2
)->sin6_scope_id
) {
4479 if (!IN6_ARE_ADDR_EQUAL(&satosin6(sa1
)->sin6_addr
,
4480 &satosin6(sa2
)->sin6_addr
)) {
4484 satosin6(sa1
)->sin6_port
!= satosin6(sa2
)->sin6_port
) {
4489 if (memcmp(sa1
, sa2
, sa1
->sa_len
) != 0)
4498 * compare two buffers with mask.
4502 * bits: Number of bits to compare
4508 key_bbcmp(void *p1v
, void *p2v
, u_int bits
)
4514 /* XXX: This could be considerably faster if we compare a word
4515 * at a time, but it is complicated on LSB Endian machines */
4517 /* Handle null pointers */
4518 if (p1
== NULL
|| p2
== NULL
)
4528 mask
= ~((1<<(8-bits
))-1);
4529 if ((*p1
& mask
) != (*p2
& mask
))
4532 return 1; /* Match! */
4537 * scanning SPD and SAD to check status for each entries,
4538 * and do to remove or to expire.
4541 key_timehandler(void *arg
)
4549 s
= splsoftnet(); /*called from softclock()*/
4550 mutex_enter(softnet_lock
);
4554 struct secpolicy
*sp
, *nextsp
;
4556 for (dir
= 0; dir
< IPSEC_DIR_MAX
; dir
++) {
4557 for (sp
= LIST_FIRST(&sptree
[dir
]);
4560 nextsp
= LIST_NEXT(sp
, chain
);
4562 if (sp
->state
== IPSEC_SPSTATE_DEAD
) {
4563 key_sp_unlink(sp
); /*XXX*/
4568 if (sp
->lifetime
== 0 && sp
->validtime
== 0)
4571 /* the deletion will occur next time */
4572 if ((sp
->lifetime
&&
4573 tv
.tv_sec
- sp
->created
> sp
->lifetime
) ||
4575 tv
.tv_sec
- sp
->lastused
> sp
->validtime
)) {
4583 /* invalidate all cached SPD pointers on pcb */
4584 ipsec_invalpcbcacheall();
4589 struct secashead
*sah
, *nextsah
;
4590 struct secasvar
*sav
, *nextsav
;
4592 u_int stateidx
, state
;
4594 for (sah
= LIST_FIRST(&sahtree
);
4598 nextsah
= LIST_NEXT(sah
, chain
);
4600 /* if sah has been dead, then delete it and process next sah. */
4601 if (sah
->state
== SADB_SASTATE_DEAD
) {
4606 /* if LARVAL entry doesn't become MATURE, delete it. */
4607 for (sav
= LIST_FIRST(&sah
->savtree
[SADB_SASTATE_LARVAL
]);
4611 nextsav
= LIST_NEXT(sav
, chain
);
4613 if (tv
.tv_sec
- sav
->created
> key_larval_lifetime
) {
4619 * check MATURE entry to start to send expire message
4622 for (sav
= LIST_FIRST(&sah
->savtree
[SADB_SASTATE_MATURE
]);
4626 nextsav
= LIST_NEXT(sav
, chain
);
4628 /* we don't need to check. */
4629 if (sav
->lft_s
== NULL
)
4633 if (sav
->lft_c
== NULL
) {
4634 ipseclog((LOG_DEBUG
, "key_timehandler: "
4635 "There is no CURRENT time, why?\n"));
4639 /* check SOFT lifetime */
4640 if (sav
->lft_s
->sadb_lifetime_addtime
!= 0 &&
4641 tv
.tv_sec
- sav
->created
> sav
->lft_s
->sadb_lifetime_addtime
) {
4643 * check the SA if it has been used.
4644 * when it hasn't been used, delete it.
4645 * i don't think such SA will be used.
4647 if (sav
->lft_c
->sadb_lifetime_usetime
== 0) {
4648 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
4652 key_sa_chgstate(sav
, SADB_SASTATE_DYING
);
4654 * XXX If we keep to send expire
4655 * message in the status of
4656 * DYING. Do remove below code.
4662 /* check SOFT lifetime by bytes */
4664 * XXX I don't know the way to delete this SA
4665 * when new SA is installed. Caution when it's
4666 * installed too big lifetime by time.
4668 else if (sav
->lft_s
->sadb_lifetime_bytes
!= 0
4669 && sav
->lft_s
->sadb_lifetime_bytes
< sav
->lft_c
->sadb_lifetime_bytes
) {
4671 key_sa_chgstate(sav
, SADB_SASTATE_DYING
);
4673 * XXX If we keep to send expire
4674 * message in the status of
4675 * DYING. Do remove below code.
4681 /* check DYING entry to change status to DEAD. */
4682 for (sav
= LIST_FIRST(&sah
->savtree
[SADB_SASTATE_DYING
]);
4686 nextsav
= LIST_NEXT(sav
, chain
);
4688 /* we don't need to check. */
4689 if (sav
->lft_h
== NULL
)
4693 if (sav
->lft_c
== NULL
) {
4694 ipseclog((LOG_DEBUG
,"key_timehandler: "
4695 "There is no CURRENT time, why?\n"));
4699 if (sav
->lft_h
->sadb_lifetime_addtime
!= 0 &&
4700 tv
.tv_sec
- sav
->created
> sav
->lft_h
->sadb_lifetime_addtime
) {
4701 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
4705 #if 0 /* XXX Should we keep to send expire message until HARD lifetime ? */
4706 else if (sav
->lft_s
!= NULL
4707 && sav
->lft_s
->sadb_lifetime_addtime
!= 0
4708 && tv
.tv_sec
- sav
->created
> sav
->lft_s
->sadb_lifetime_addtime
) {
4710 * XXX: should be checked to be
4711 * installed the valid SA.
4715 * If there is no SA then sending
4721 /* check HARD lifetime by bytes */
4722 else if (sav
->lft_h
->sadb_lifetime_bytes
!= 0
4723 && sav
->lft_h
->sadb_lifetime_bytes
< sav
->lft_c
->sadb_lifetime_bytes
) {
4724 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
4730 /* delete entry in DEAD */
4731 for (sav
= LIST_FIRST(&sah
->savtree
[SADB_SASTATE_DEAD
]);
4735 nextsav
= LIST_NEXT(sav
, chain
);
4738 if (sav
->state
!= SADB_SASTATE_DEAD
) {
4739 ipseclog((LOG_DEBUG
, "key_timehandler: "
4740 "invalid sav->state "
4741 "(queue: %u SA: %u): "
4743 SADB_SASTATE_DEAD
, sav
->state
));
4747 * do not call key_freesav() here.
4748 * sav should already be freed, and sav->refcnt
4749 * shows other references to sav
4750 * (such as from SPD).
4754 /* move SA header to DEAD if there's no SA */
4757 stateidx
< _ARRAYLEN(saorder_state_alive
);
4759 state
= saorder_state_alive
[stateidx
];
4760 if (LIST_FIRST(&sah
->savtree
[state
])) {
4766 ipseclog((LOG_DEBUG
, "key_timehandler: "
4767 "move sah %p to DEAD (no more SAs)\n", sah
));
4768 sah
->state
= SADB_SASTATE_DEAD
;
4773 #ifndef IPSEC_NONBLOCK_ACQUIRE
4776 struct secacq
*acq
, *nextacq
;
4778 for (acq
= LIST_FIRST(&acqtree
);
4782 nextacq
= LIST_NEXT(acq
, chain
);
4784 if (tv
.tv_sec
- acq
->created
> key_blockacq_lifetime
&&
4785 __LIST_CHAINED(acq
)) {
4786 LIST_REMOVE(acq
, chain
);
4795 struct secspacq
*acq
, *nextacq
;
4797 for (acq
= LIST_FIRST(&spacqtree
);
4801 nextacq
= LIST_NEXT(acq
, chain
);
4803 if (tv
.tv_sec
- acq
->created
> key_blockacq_lifetime
&&
4804 __LIST_CHAINED(acq
)) {
4805 LIST_REMOVE(acq
, chain
);
4811 callout_reset(&key_timehandler_ch
, hz
, key_timehandler
, (void *)0);
4813 mutex_exit(softnet_lock
);
4819 * to initialize a seed for random()
4826 key_randomfill(&value
, sizeof(value
));
4831 key_randomfill(void *p
, size_t l
)
4834 static int warn
= 1;
4837 arc4randbytes(p
, l
);
4840 /* the arc4 generator is keyed with junk. */
4842 printf("WARNING: pseudo-random number generator "
4843 "used for IPsec processing\n");
4850 * map SADB_SATYPE_* to IPPROTO_*.
4851 * if satype == SADB_SATYPE then satype is mapped to ~0.
4853 * 0: invalid satype.
4856 key_satype2proto(u_int8_t satype
)
4859 case SADB_SATYPE_UNSPEC
:
4860 return IPSEC_PROTO_ANY
;
4861 case SADB_SATYPE_AH
:
4863 case SADB_SATYPE_ESP
:
4865 case SADB_X_SATYPE_IPCOMP
:
4866 return IPPROTO_IPCOMP
;
4867 case SADB_X_SATYPE_TCPSIGNATURE
:
4876 * map IPPROTO_* to SADB_SATYPE_*
4878 * 0: invalid protocol type.
4881 key_proto2satype(u_int16_t proto
)
4885 return SADB_SATYPE_AH
;
4887 return SADB_SATYPE_ESP
;
4888 case IPPROTO_IPCOMP
:
4889 return SADB_X_SATYPE_IPCOMP
;
4891 return SADB_X_SATYPE_TCPSIGNATURE
;
4900 * SADB_GETSPI processing is to receive
4901 * <base, (SA2), src address, dst address, (SPI range)>
4902 * from the IKMPd, to assign a unique spi value, to hang on the INBOUND
4903 * tree with the status of LARVAL, and send
4904 * <base, SA(*), address(SD)>
4907 * IN: mhp: pointer to the pointer to each header.
4908 * OUT: NULL if fail.
4909 * other if success, return pointer to the message to send.
4912 key_getspi(struct socket
*so
, struct mbuf
*m
, const struct sadb_msghdr
*mhp
)
4914 struct sadb_address
*src0
, *dst0
;
4915 struct secasindex saidx
;
4916 struct secashead
*newsah
;
4917 struct secasvar
*newsav
;
4925 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
4926 panic("key_getspi: NULL pointer is passed.");
4928 if (mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
4929 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
) {
4930 ipseclog((LOG_DEBUG
, "key_getspi: invalid message is passed.\n"));
4931 return key_senderror(so
, m
, EINVAL
);
4933 if (mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
4934 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) {
4935 ipseclog((LOG_DEBUG
, "key_getspi: invalid message is passed.\n"));
4936 return key_senderror(so
, m
, EINVAL
);
4938 if (mhp
->ext
[SADB_X_EXT_SA2
] != NULL
) {
4939 mode
= ((struct sadb_x_sa2
*)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_mode
;
4940 reqid
= ((struct sadb_x_sa2
*)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_reqid
;
4942 mode
= IPSEC_MODE_ANY
;
4946 src0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_SRC
]);
4947 dst0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_DST
]);
4949 /* map satype to proto */
4950 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
4951 ipseclog((LOG_DEBUG
, "key_getspi: invalid satype is passed.\n"));
4952 return key_senderror(so
, m
, EINVAL
);
4955 /* sa_len safety check */
4956 if (KEY_CHECKSALEN(src0
+ 1) != 0)
4957 return key_senderror(so
, m
, EINVAL
);
4958 if (KEY_CHECKSALEN(dst0
+ 1) != 0)
4959 return key_senderror(so
, m
, EINVAL
);
4961 KEY_SETSECASIDX(proto
, mode
, reqid
, src0
+ 1, dst0
+ 1, &saidx
);
4963 /* If not using NAT-T, make sure port numbers are set to zero. */
4965 KEY_PORTTOSADDR(&saidx
.src
, 0);
4966 KEY_PORTTOSADDR(&saidx
.dst
, 0);
4969 /* SPI allocation */
4970 spi
= key_do_getnewspi((struct sadb_spirange
*)mhp
->ext
[SADB_EXT_SPIRANGE
],
4973 return key_senderror(so
, m
, EINVAL
);
4975 /* get a SA index */
4976 if ((newsah
= key_getsah(&saidx
)) == NULL
) {
4977 /* create a new SA index */
4978 if ((newsah
= key_newsah(&saidx
)) == NULL
) {
4979 ipseclog((LOG_DEBUG
, "key_getspi: No more memory.\n"));
4980 return key_senderror(so
, m
, ENOBUFS
);
4986 newsav
= key_newsav(m
, mhp
, newsah
, &error
);
4987 if (newsav
== NULL
) {
4988 /* XXX don't free new SA index allocated in above. */
4989 return key_senderror(so
, m
, error
);
4993 key_setspi(newsav
, htonl(spi
));
4995 #ifndef IPSEC_NONBLOCK_ACQUIRE
4996 /* delete the entry in acqtree */
4997 if (mhp
->msg
->sadb_msg_seq
!= 0) {
4999 if ((acq
= key_getacqbyseq(mhp
->msg
->sadb_msg_seq
)) != NULL
) {
5000 /* reset counter in order to deletion by timehandler. */
5001 acq
->created
= time_second
;
5008 struct mbuf
*n
, *nn
;
5009 struct sadb_sa
*m_sa
;
5010 struct sadb_msg
*newmsg
;
5013 /* create new sadb_msg to reply. */
5014 len
= PFKEY_ALIGN8(sizeof(struct sadb_msg
)) +
5015 PFKEY_ALIGN8(sizeof(struct sadb_sa
));
5017 return key_senderror(so
, m
, ENOBUFS
);
5019 MGETHDR(n
, M_DONTWAIT
, MT_DATA
);
5021 MCLGET(n
, M_DONTWAIT
);
5022 if ((n
->m_flags
& M_EXT
) == 0) {
5028 return key_senderror(so
, m
, ENOBUFS
);
5034 m_copydata(m
, 0, sizeof(struct sadb_msg
), mtod(n
, char *) + off
);
5035 off
+= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
5037 m_sa
= (struct sadb_sa
*)(mtod(n
, char *) + off
);
5038 m_sa
->sadb_sa_len
= PFKEY_UNIT64(sizeof(struct sadb_sa
));
5039 m_sa
->sadb_sa_exttype
= SADB_EXT_SA
;
5040 m_sa
->sadb_sa_spi
= htonl(spi
);
5041 off
+= PFKEY_ALIGN8(sizeof(struct sadb_sa
));
5045 panic("length inconsistency in key_getspi");
5048 n
->m_next
= key_gather_mbuf(m
, mhp
, 0, 2, SADB_EXT_ADDRESS_SRC
,
5049 SADB_EXT_ADDRESS_DST
);
5052 return key_senderror(so
, m
, ENOBUFS
);
5055 if (n
->m_len
< sizeof(struct sadb_msg
)) {
5056 n
= m_pullup(n
, sizeof(struct sadb_msg
));
5058 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ONE
);
5061 n
->m_pkthdr
.len
= 0;
5062 for (nn
= n
; nn
; nn
= nn
->m_next
)
5063 n
->m_pkthdr
.len
+= nn
->m_len
;
5065 newmsg
= mtod(n
, struct sadb_msg
*);
5066 newmsg
->sadb_msg_seq
= newsav
->seq
;
5067 newmsg
->sadb_msg_errno
= 0;
5068 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
5071 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ONE
);
5076 * allocating new SPI
5077 * called by key_getspi().
5083 key_do_getnewspi(struct sadb_spirange
*spirange
, struct secasindex
*saidx
)
5086 u_int32_t xmin
, xmax
;
5087 int count
= key_spi_trycnt
;
5089 /* set spi range to allocate */
5090 if (spirange
!= NULL
) {
5091 xmin
= spirange
->sadb_spirange_min
;
5092 xmax
= spirange
->sadb_spirange_max
;
5094 xmin
= key_spi_minval
;
5095 xmax
= key_spi_maxval
;
5097 /* IPCOMP needs 2-byte SPI */
5098 if (saidx
->proto
== IPPROTO_IPCOMP
) {
5100 if (xmin
>= 0x10000)
5102 if (xmax
>= 0x10000)
5105 t
= xmin
; xmin
= xmax
; xmax
= t
;
5110 if (key_checkspidup(saidx
, htonl(xmin
)) != NULL
) {
5111 ipseclog((LOG_DEBUG
, "key_do_getnewspi: SPI %u exists already.\n", xmin
));
5115 count
--; /* taking one cost. */
5123 /* when requesting to allocate spi ranged */
5125 /* generate pseudo-random SPI value ranged. */
5126 newspi
= xmin
+ (key_random() % (xmax
- xmin
+ 1));
5128 if (key_checkspidup(saidx
, htonl(newspi
)) == NULL
)
5132 if (count
== 0 || newspi
== 0) {
5133 ipseclog((LOG_DEBUG
, "key_do_getnewspi: to allocate spi is failed.\n"));
5139 keystat
.getspi_count
=
5140 (keystat
.getspi_count
+ key_spi_trycnt
- count
) / 2;
5146 * SADB_UPDATE processing
5148 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
5149 * key(AE), (identity(SD),) (sensitivity)>
5150 * from the ikmpd, and update a secasvar entry whose status is SADB_SASTATE_LARVAL.
5152 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
5153 * (identity(SD),) (sensitivity)>
5156 * m will always be freed.
5159 key_update(struct socket
*so
, struct mbuf
*m
, const struct sadb_msghdr
*mhp
)
5161 struct sadb_sa
*sa0
;
5162 struct sadb_address
*src0
, *dst0
;
5163 struct secasindex saidx
;
5164 struct secashead
*sah
;
5165 struct secasvar
*sav
;
5172 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
5173 panic("key_update: NULL pointer is passed.");
5175 /* map satype to proto */
5176 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
5177 ipseclog((LOG_DEBUG
, "key_update: invalid satype is passed.\n"));
5178 return key_senderror(so
, m
, EINVAL
);
5181 if (mhp
->ext
[SADB_EXT_SA
] == NULL
||
5182 mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
5183 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
||
5184 (mhp
->msg
->sadb_msg_satype
== SADB_SATYPE_ESP
&&
5185 mhp
->ext
[SADB_EXT_KEY_ENCRYPT
] == NULL
) ||
5186 (mhp
->msg
->sadb_msg_satype
== SADB_SATYPE_AH
&&
5187 mhp
->ext
[SADB_EXT_KEY_AUTH
] == NULL
) ||
5188 (mhp
->ext
[SADB_EXT_LIFETIME_HARD
] != NULL
&&
5189 mhp
->ext
[SADB_EXT_LIFETIME_SOFT
] == NULL
) ||
5190 (mhp
->ext
[SADB_EXT_LIFETIME_HARD
] == NULL
&&
5191 mhp
->ext
[SADB_EXT_LIFETIME_SOFT
] != NULL
)) {
5192 ipseclog((LOG_DEBUG
, "key_update: invalid message is passed.\n"));
5193 return key_senderror(so
, m
, EINVAL
);
5195 if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(struct sadb_sa
) ||
5196 mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
5197 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) {
5198 ipseclog((LOG_DEBUG
, "key_update: invalid message is passed.\n"));
5199 return key_senderror(so
, m
, EINVAL
);
5201 if (mhp
->ext
[SADB_X_EXT_SA2
] != NULL
) {
5202 mode
= ((struct sadb_x_sa2
*)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_mode
;
5203 reqid
= ((struct sadb_x_sa2
*)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_reqid
;
5205 mode
= IPSEC_MODE_ANY
;
5208 /* XXX boundary checking for other extensions */
5210 sa0
= (struct sadb_sa
*)mhp
->ext
[SADB_EXT_SA
];
5211 src0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_SRC
]);
5212 dst0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_DST
]);
5214 /* sa_len safety check */
5215 if (KEY_CHECKSALEN(src0
+ 1) != 0)
5216 return key_senderror(so
, m
, EINVAL
);
5217 if (KEY_CHECKSALEN(dst0
+ 1) != 0)
5218 return key_senderror(so
, m
, EINVAL
);
5220 KEY_SETSECASIDX(proto
, mode
, reqid
, src0
+ 1, dst0
+ 1, &saidx
);
5222 /* If not using NAT-T, make sure if port number is zero. */
5224 KEY_PORTTOSADDR(&saidx
.src
, 0);
5225 KEY_PORTTOSADDR(&saidx
.dst
, 0);
5228 /* get a SA header */
5229 if ((sah
= key_getsah(&saidx
)) == NULL
) {
5230 ipseclog((LOG_DEBUG
, "key_update: no SA index found.\n"));
5231 return key_senderror(so
, m
, ENOENT
);
5234 /* set spidx if there */
5236 error
= key_setident(sah
, m
, mhp
);
5238 return key_senderror(so
, m
, error
);
5240 /* find a SA with sequence number. */
5241 #ifdef IPSEC_DOSEQCHECK
5242 if (mhp
->msg
->sadb_msg_seq
!= 0 &&
5243 (sav
= key_getsavbyseq(sah
, mhp
->msg
->sadb_msg_seq
)) == NULL
) {
5244 ipseclog((LOG_DEBUG
,
5245 "key_update: no larval SA with sequence %u exists.\n",
5246 mhp
->msg
->sadb_msg_seq
));
5247 return key_senderror(so
, m
, ENOENT
);
5250 if ((sav
= key_getsavbyspi(sah
, sa0
->sadb_sa_spi
)) == NULL
) {
5251 ipseclog((LOG_DEBUG
,
5252 "key_update: no such a SA found (spi:%u)\n",
5253 (u_int32_t
)ntohl(sa0
->sadb_sa_spi
)));
5254 return key_senderror(so
, m
, EINVAL
);
5258 /* validity check */
5259 if (sav
->sah
->saidx
.proto
!= proto
) {
5260 ipseclog((LOG_DEBUG
,
5261 "key_update: protocol mismatched (DB=%u param=%u)\n",
5262 sav
->sah
->saidx
.proto
, proto
));
5263 return key_senderror(so
, m
, EINVAL
);
5265 #ifdef IPSEC_DOSEQCHECK
5266 if (sav
->spi
!= sa0
->sadb_sa_spi
) {
5267 ipseclog((LOG_DEBUG
,
5268 "key_update: SPI mismatched (DB:%u param:%u)\n",
5269 (u_int32_t
)ntohl(sav
->spi
),
5270 (u_int32_t
)ntohl(sa0
->sadb_sa_spi
)));
5271 return key_senderror(so
, m
, EINVAL
);
5274 if (sav
->pid
!= mhp
->msg
->sadb_msg_pid
) {
5275 ipseclog((LOG_DEBUG
,
5276 "key_update: pid mismatched (DB:%u param:%u)\n",
5277 sav
->pid
, mhp
->msg
->sadb_msg_pid
));
5278 return key_senderror(so
, m
, EINVAL
);
5281 /* copy sav values */
5282 error
= key_setsaval(sav
, m
, mhp
);
5285 return key_senderror(so
, m
, error
);
5288 /* check SA values to be mature. */
5289 if ((error
= key_mature(sav
)) != 0) {
5291 return key_senderror(so
, m
, error
);
5296 * Handle NAT-T info if present
5298 if (mhp
->ext
[SADB_X_EXT_NAT_T_OA
] != NULL
)
5299 printf("update: NAT-T OA present\n");
5301 if ((mhp
->ext
[SADB_X_EXT_NAT_T_TYPE
] != NULL
) &&
5302 (mhp
->ext
[SADB_X_EXT_NAT_T_SPORT
] != NULL
) &&
5303 (mhp
->ext
[SADB_X_EXT_NAT_T_DPORT
] != NULL
)) {
5304 struct sadb_x_nat_t_type
*type
;
5305 struct sadb_x_nat_t_port
*sport
;
5306 struct sadb_x_nat_t_port
*dport
;
5307 struct sadb_address
*addr
;
5308 struct sadb_x_nat_t_frag
*frag
;
5310 if ((mhp
->extlen
[SADB_X_EXT_NAT_T_TYPE
] < sizeof(*type
)) ||
5311 (mhp
->extlen
[SADB_X_EXT_NAT_T_SPORT
] < sizeof(*sport
)) ||
5312 (mhp
->extlen
[SADB_X_EXT_NAT_T_DPORT
] < sizeof(*dport
))) {
5313 ipseclog((LOG_DEBUG
, "key_update: "
5314 "invalid message.\n"));
5315 return key_senderror(so
, m
, EINVAL
);
5318 if ((mhp
->ext
[SADB_X_EXT_NAT_T_OA
] != NULL
) &&
5319 (mhp
->extlen
[SADB_X_EXT_NAT_T_OA
] < sizeof(*addr
))) {
5320 ipseclog((LOG_DEBUG
, "key_update: invalid message\n"));
5321 return key_senderror(so
, m
, EINVAL
);
5324 if ((mhp
->ext
[SADB_X_EXT_NAT_T_FRAG
] != NULL
) &&
5325 (mhp
->extlen
[SADB_X_EXT_NAT_T_FRAG
] < sizeof(*frag
))) {
5326 ipseclog((LOG_DEBUG
, "key_update: invalid message\n"));
5327 return key_senderror(so
, m
, EINVAL
);
5330 type
= (struct sadb_x_nat_t_type
*)
5331 mhp
->ext
[SADB_X_EXT_NAT_T_TYPE
];
5332 sport
= (struct sadb_x_nat_t_port
*)
5333 mhp
->ext
[SADB_X_EXT_NAT_T_SPORT
];
5334 dport
= (struct sadb_x_nat_t_port
*)
5335 mhp
->ext
[SADB_X_EXT_NAT_T_DPORT
];
5336 addr
= (struct sadb_address
*)
5337 mhp
->ext
[SADB_X_EXT_NAT_T_OA
];
5338 frag
= (struct sadb_x_nat_t_frag
*)
5339 mhp
->ext
[SADB_X_EXT_NAT_T_FRAG
];
5342 sav
->natt_type
= type
->sadb_x_nat_t_type_type
;
5344 KEY_PORTTOSADDR(&sav
->sah
->saidx
.src
,
5345 sport
->sadb_x_nat_t_port_port
);
5347 KEY_PORTTOSADDR(&sav
->sah
->saidx
.dst
,
5348 dport
->sadb_x_nat_t_port_port
);
5350 sav
->esp_frag
= frag
->sadb_x_nat_t_frag_fraglen
;
5352 sav
->esp_frag
= IP_MAXPACKET
;
5354 #endif /* IPSEC_NAT_T */
5359 /* set msg buf from mhp */
5360 n
= key_getmsgbuf_x1(m
, mhp
);
5362 ipseclog((LOG_DEBUG
, "key_update: No more memory.\n"));
5363 return key_senderror(so
, m
, ENOBUFS
);
5367 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
5372 * search SAD with sequence for a SA which state is SADB_SASTATE_LARVAL.
5373 * only called by key_update().
5376 * others : found, pointer to a SA.
5378 #ifdef IPSEC_DOSEQCHECK
5379 static struct secasvar
*
5380 key_getsavbyseq(struct secashead
*sah
, u_int32_t seq
)
5382 struct secasvar
*sav
;
5385 state
= SADB_SASTATE_LARVAL
;
5387 /* search SAD with sequence number ? */
5388 LIST_FOREACH(sav
, &sah
->savtree
[state
], chain
) {
5390 KEY_CHKSASTATE(state
, sav
->state
, "key_getsabyseq");
5392 if (sav
->seq
== seq
)
5400 * SADB_ADD processing
5401 * add an entry to SA database, when received
5402 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
5403 * key(AE), (identity(SD),) (sensitivity)>
5406 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
5407 * (identity(SD),) (sensitivity)>
5410 * IGNORE identity and sensitivity messages.
5412 * m will always be freed.
5415 key_add(struct socket
*so
, struct mbuf
*m
, const struct sadb_msghdr
*mhp
)
5417 struct sadb_sa
*sa0
;
5418 struct sadb_address
*src0
, *dst0
;
5419 struct secasindex saidx
;
5420 struct secashead
*newsah
;
5421 struct secasvar
*newsav
;
5428 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
5429 panic("key_add: NULL pointer is passed.");
5431 /* map satype to proto */
5432 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
5433 ipseclog((LOG_DEBUG
, "key_add: invalid satype is passed.\n"));
5434 return key_senderror(so
, m
, EINVAL
);
5437 if (mhp
->ext
[SADB_EXT_SA
] == NULL
||
5438 mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
5439 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
||
5440 (mhp
->msg
->sadb_msg_satype
== SADB_SATYPE_ESP
&&
5441 mhp
->ext
[SADB_EXT_KEY_ENCRYPT
] == NULL
) ||
5442 (mhp
->msg
->sadb_msg_satype
== SADB_SATYPE_AH
&&
5443 mhp
->ext
[SADB_EXT_KEY_AUTH
] == NULL
) ||
5444 (mhp
->ext
[SADB_EXT_LIFETIME_HARD
] != NULL
&&
5445 mhp
->ext
[SADB_EXT_LIFETIME_SOFT
] == NULL
) ||
5446 (mhp
->ext
[SADB_EXT_LIFETIME_HARD
] == NULL
&&
5447 mhp
->ext
[SADB_EXT_LIFETIME_SOFT
] != NULL
)) {
5448 ipseclog((LOG_DEBUG
, "key_add: invalid message is passed.\n"));
5449 return key_senderror(so
, m
, EINVAL
);
5451 if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(struct sadb_sa
) ||
5452 mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
5453 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) {
5455 ipseclog((LOG_DEBUG
, "key_add: invalid message is passed.\n"));
5456 return key_senderror(so
, m
, EINVAL
);
5458 if (mhp
->ext
[SADB_X_EXT_SA2
] != NULL
) {
5459 mode
= ((struct sadb_x_sa2
*)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_mode
;
5460 reqid
= ((struct sadb_x_sa2
*)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_reqid
;
5462 mode
= IPSEC_MODE_ANY
;
5466 sa0
= (struct sadb_sa
*)mhp
->ext
[SADB_EXT_SA
];
5467 src0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_SRC
];
5468 dst0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_DST
];
5470 /* sa_len safety check */
5471 if (KEY_CHECKSALEN(src0
+ 1) != 0)
5472 return key_senderror(so
, m
, EINVAL
);
5473 if (KEY_CHECKSALEN(dst0
+ 1) != 0)
5474 return key_senderror(so
, m
, EINVAL
);
5476 KEY_SETSECASIDX(proto
, mode
, reqid
, src0
+ 1, dst0
+ 1, &saidx
);
5478 /* If not using NAT-T, make sure if port number is zero. */
5480 KEY_PORTTOSADDR(&saidx
.src
, 0);
5481 KEY_PORTTOSADDR(&saidx
.dst
, 0);
5484 /* get a SA header */
5485 if ((newsah
= key_getsah(&saidx
)) == NULL
) {
5486 /* create a new SA header */
5487 if ((newsah
= key_newsah(&saidx
)) == NULL
) {
5488 ipseclog((LOG_DEBUG
, "key_add: No more memory.\n"));
5489 return key_senderror(so
, m
, ENOBUFS
);
5493 /* set spidx if there */
5495 error
= key_setident(newsah
, m
, mhp
);
5497 return key_senderror(so
, m
, error
);
5500 /* create new SA entry. */
5501 /* We can create new SA only if SPI is differenct. */
5502 if (key_getsavbyspi(newsah
, sa0
->sadb_sa_spi
)) {
5503 ipseclog((LOG_DEBUG
, "key_add: SA already exists.\n"));
5504 return key_senderror(so
, m
, EEXIST
);
5506 newsav
= key_newsav(m
, mhp
, newsah
, &error
);
5507 if (newsav
== NULL
) {
5508 return key_senderror(so
, m
, error
);
5511 /* check SA values to be mature. */
5512 if ((error
= key_mature(newsav
)) != 0) {
5513 key_freesav(newsav
);
5514 return key_senderror(so
, m
, error
);
5519 * Handle NAT-T info if present
5521 if (mhp
->ext
[SADB_X_EXT_NAT_T_OA
] != NULL
)
5522 printf("add: NAT-T OA present\n");
5524 if ((mhp
->ext
[SADB_X_EXT_NAT_T_TYPE
] != NULL
) &&
5525 (mhp
->ext
[SADB_X_EXT_NAT_T_SPORT
] != NULL
) &&
5526 (mhp
->ext
[SADB_X_EXT_NAT_T_DPORT
] != NULL
)) {
5527 struct sadb_x_nat_t_type
*type
;
5528 struct sadb_x_nat_t_port
*sport
;
5529 struct sadb_x_nat_t_port
*dport
;
5530 struct sadb_address
*addr
;
5531 struct sadb_x_nat_t_frag
*frag
;
5533 if ((mhp
->extlen
[SADB_X_EXT_NAT_T_TYPE
] < sizeof(*type
)) ||
5534 (mhp
->extlen
[SADB_X_EXT_NAT_T_SPORT
] < sizeof(*sport
)) ||
5535 (mhp
->extlen
[SADB_X_EXT_NAT_T_DPORT
] < sizeof(*dport
))) {
5536 ipseclog((LOG_DEBUG
, "key_add: "
5537 "invalid message.\n"));
5538 return key_senderror(so
, m
, EINVAL
);
5541 if ((mhp
->ext
[SADB_X_EXT_NAT_T_OA
] != NULL
) &&
5542 (mhp
->extlen
[SADB_X_EXT_NAT_T_OA
] < sizeof(*addr
))) {
5543 ipseclog((LOG_DEBUG
, "key_add: invalid message\n"));
5544 return key_senderror(so
, m
, EINVAL
);
5547 if ((mhp
->ext
[SADB_X_EXT_NAT_T_FRAG
] != NULL
) &&
5548 (mhp
->extlen
[SADB_X_EXT_NAT_T_FRAG
] < sizeof(*frag
))) {
5549 ipseclog((LOG_DEBUG
, "key_update: invalid message\n"));
5550 return key_senderror(so
, m
, EINVAL
);
5553 type
= (struct sadb_x_nat_t_type
*)
5554 mhp
->ext
[SADB_X_EXT_NAT_T_TYPE
];
5555 sport
= (struct sadb_x_nat_t_port
*)
5556 mhp
->ext
[SADB_X_EXT_NAT_T_SPORT
];
5557 dport
= (struct sadb_x_nat_t_port
*)
5558 mhp
->ext
[SADB_X_EXT_NAT_T_DPORT
];
5559 addr
= (struct sadb_address
*)
5560 mhp
->ext
[SADB_X_EXT_NAT_T_OA
];
5561 frag
= (struct sadb_x_nat_t_frag
*)
5562 mhp
->ext
[SADB_X_EXT_NAT_T_FRAG
];
5565 newsav
->natt_type
= type
->sadb_x_nat_t_type_type
;
5567 KEY_PORTTOSADDR(&newsav
->sah
->saidx
.src
,
5568 sport
->sadb_x_nat_t_port_port
);
5570 KEY_PORTTOSADDR(&newsav
->sah
->saidx
.dst
,
5571 dport
->sadb_x_nat_t_port_port
);
5573 newsav
->esp_frag
= frag
->sadb_x_nat_t_frag_fraglen
;
5575 newsav
->esp_frag
= IP_MAXPACKET
;
5577 #endif /* IPSEC_NAT_T */
5580 * don't call key_freesav() here, as we would like to keep the SA
5581 * in the database on success.
5587 /* set msg buf from mhp */
5588 n
= key_getmsgbuf_x1(m
, mhp
);
5590 ipseclog((LOG_DEBUG
, "key_update: No more memory.\n"));
5591 return key_senderror(so
, m
, ENOBUFS
);
5595 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
5601 key_setident(struct secashead
*sah
, struct mbuf
*m
, const struct sadb_msghdr
*mhp
)
5603 const struct sadb_ident
*idsrc
, *iddst
;
5604 int idsrclen
, iddstlen
;
5607 if (sah
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
5608 panic("key_setident: NULL pointer is passed.");
5610 /* don't make buffer if not there */
5611 if (mhp
->ext
[SADB_EXT_IDENTITY_SRC
] == NULL
&&
5612 mhp
->ext
[SADB_EXT_IDENTITY_DST
] == NULL
) {
5618 if (mhp
->ext
[SADB_EXT_IDENTITY_SRC
] == NULL
||
5619 mhp
->ext
[SADB_EXT_IDENTITY_DST
] == NULL
) {
5620 ipseclog((LOG_DEBUG
, "key_setident: invalid identity.\n"));
5624 idsrc
= (const struct sadb_ident
*)mhp
->ext
[SADB_EXT_IDENTITY_SRC
];
5625 iddst
= (const struct sadb_ident
*)mhp
->ext
[SADB_EXT_IDENTITY_DST
];
5626 idsrclen
= mhp
->extlen
[SADB_EXT_IDENTITY_SRC
];
5627 iddstlen
= mhp
->extlen
[SADB_EXT_IDENTITY_DST
];
5629 /* validity check */
5630 if (idsrc
->sadb_ident_type
!= iddst
->sadb_ident_type
) {
5631 ipseclog((LOG_DEBUG
, "key_setident: ident type mismatch.\n"));
5635 switch (idsrc
->sadb_ident_type
) {
5636 case SADB_IDENTTYPE_PREFIX
:
5637 case SADB_IDENTTYPE_FQDN
:
5638 case SADB_IDENTTYPE_USERFQDN
:
5640 /* XXX do nothing */
5646 /* make structure */
5647 KMALLOC(sah
->idents
, struct sadb_ident
*, idsrclen
);
5648 if (sah
->idents
== NULL
) {
5649 ipseclog((LOG_DEBUG
, "key_setident: No more memory.\n"));
5652 KMALLOC(sah
->identd
, struct sadb_ident
*, iddstlen
);
5653 if (sah
->identd
== NULL
) {
5656 ipseclog((LOG_DEBUG
, "key_setident: No more memory.\n"));
5659 memcpy(sah
->idents
, idsrc
, idsrclen
);
5660 memcpy(sah
->identd
, iddst
, iddstlen
);
5666 * m will not be freed on return.
5667 * it is caller's responsibility to free the result.
5669 static struct mbuf
*
5670 key_getmsgbuf_x1(struct mbuf
*m
, const struct sadb_msghdr
*mhp
)
5675 if (m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
5676 panic("key_getmsgbuf_x1: NULL pointer is passed.");
5678 /* create new sadb_msg to reply. */
5679 n
= key_gather_mbuf(m
, mhp
, 1, 9, SADB_EXT_RESERVED
,
5680 SADB_EXT_SA
, SADB_X_EXT_SA2
,
5681 SADB_EXT_ADDRESS_SRC
, SADB_EXT_ADDRESS_DST
,
5682 SADB_EXT_LIFETIME_HARD
, SADB_EXT_LIFETIME_SOFT
,
5683 SADB_EXT_IDENTITY_SRC
, SADB_EXT_IDENTITY_DST
);
5687 if (n
->m_len
< sizeof(struct sadb_msg
)) {
5688 n
= m_pullup(n
, sizeof(struct sadb_msg
));
5692 mtod(n
, struct sadb_msg
*)->sadb_msg_errno
= 0;
5693 mtod(n
, struct sadb_msg
*)->sadb_msg_len
=
5694 PFKEY_UNIT64(n
->m_pkthdr
.len
);
5699 static int key_delete_all(struct socket
*, struct mbuf
*,
5700 const struct sadb_msghdr
*, u_int16_t
);
5703 * SADB_DELETE processing
5705 * <base, SA(*), address(SD)>
5706 * from the ikmpd, and set SADB_SASTATE_DEAD,
5708 * <base, SA(*), address(SD)>
5711 * m will always be freed.
5714 key_delete(struct socket
*so
, struct mbuf
*m
, const struct sadb_msghdr
*mhp
)
5716 struct sadb_sa
*sa0
;
5717 struct sadb_address
*src0
, *dst0
;
5718 struct secasindex saidx
;
5719 struct secashead
*sah
;
5720 struct secasvar
*sav
= NULL
;
5724 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
5725 panic("key_delete: NULL pointer is passed.");
5727 /* map satype to proto */
5728 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
5729 ipseclog((LOG_DEBUG
, "key_delete: invalid satype is passed.\n"));
5730 return key_senderror(so
, m
, EINVAL
);
5733 if (mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
5734 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
) {
5735 ipseclog((LOG_DEBUG
, "key_delete: invalid message is passed.\n"));
5736 return key_senderror(so
, m
, EINVAL
);
5739 if (mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
5740 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) {
5741 ipseclog((LOG_DEBUG
, "key_delete: invalid message is passed.\n"));
5742 return key_senderror(so
, m
, EINVAL
);
5745 if (mhp
->ext
[SADB_EXT_SA
] == NULL
) {
5747 * Caller wants us to delete all non-LARVAL SAs
5748 * that match the src/dst. This is used during
5749 * IKE INITIAL-CONTACT.
5751 ipseclog((LOG_DEBUG
, "key_delete: doing delete all.\n"));
5752 return key_delete_all(so
, m
, mhp
, proto
);
5753 } else if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(struct sadb_sa
)) {
5754 ipseclog((LOG_DEBUG
, "key_delete: invalid message is passed.\n"));
5755 return key_senderror(so
, m
, EINVAL
);
5758 sa0
= (struct sadb_sa
*)mhp
->ext
[SADB_EXT_SA
];
5759 src0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_SRC
]);
5760 dst0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_DST
]);
5762 /* sa_len safety check */
5763 if (KEY_CHECKSALEN(src0
+ 1) != 0)
5764 return key_senderror(so
, m
, EINVAL
);
5765 if (KEY_CHECKSALEN(dst0
+ 1) != 0)
5766 return key_senderror(so
, m
, EINVAL
);
5768 KEY_SETSECASIDX(proto
, IPSEC_MODE_ANY
, 0, src0
+ 1, dst0
+ 1, &saidx
);
5770 /* If not using NAT-T, make sure if port number is zero. */
5772 KEY_PORTTOSADDR(&saidx
.src
, 0);
5773 KEY_PORTTOSADDR(&saidx
.dst
, 0);
5776 /* get a SA header */
5777 LIST_FOREACH(sah
, &sahtree
, chain
) {
5778 if (sah
->state
== SADB_SASTATE_DEAD
)
5780 if (key_cmpsaidx_withoutmode(&sah
->saidx
, &saidx
) == 0)
5783 /* get a SA with SPI. */
5784 sav
= key_getsavbyspi(sah
, sa0
->sadb_sa_spi
);
5789 ipseclog((LOG_DEBUG
, "key_delete: no SA found.\n"));
5790 return key_senderror(so
, m
, ENOENT
);
5793 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
5799 struct sadb_msg
*newmsg
;
5801 /* create new sadb_msg to reply. */
5802 n
= key_gather_mbuf(m
, mhp
, 1, 4, SADB_EXT_RESERVED
,
5803 SADB_EXT_SA
, SADB_EXT_ADDRESS_SRC
, SADB_EXT_ADDRESS_DST
);
5805 return key_senderror(so
, m
, ENOBUFS
);
5807 if (n
->m_len
< sizeof(struct sadb_msg
)) {
5808 n
= m_pullup(n
, sizeof(struct sadb_msg
));
5810 return key_senderror(so
, m
, ENOBUFS
);
5812 newmsg
= mtod(n
, struct sadb_msg
*);
5813 newmsg
->sadb_msg_errno
= 0;
5814 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
5817 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
5822 * delete all SAs for src/dst. Called from key_delete().
5825 key_delete_all(struct socket
*so
, struct mbuf
*m
, const struct sadb_msghdr
*mhp
, u_int16_t proto
)
5827 struct sadb_address
*src0
, *dst0
;
5828 struct secasindex saidx
;
5829 struct secashead
*sah
;
5830 struct secasvar
*sav
, *nextsav
;
5831 u_int stateidx
, state
;
5833 src0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_SRC
]);
5834 dst0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_DST
]);
5836 /* sa_len safety check */
5837 if (KEY_CHECKSALEN(src0
+ 1) != 0)
5838 return key_senderror(so
, m
, EINVAL
);
5839 if (KEY_CHECKSALEN(dst0
+ 1) != 0)
5840 return key_senderror(so
, m
, EINVAL
);
5842 KEY_SETSECASIDX(proto
, IPSEC_MODE_ANY
, 0, src0
+ 1, dst0
+ 1, &saidx
);
5844 /* If not using NAT-T, make sure if port number is zero. */
5846 KEY_PORTTOSADDR(&saidx
.src
, 0);
5847 KEY_PORTTOSADDR(&saidx
.dst
, 0);
5850 LIST_FOREACH(sah
, &sahtree
, chain
) {
5851 if (sah
->state
== SADB_SASTATE_DEAD
)
5853 if (key_cmpsaidx_withoutmode(&sah
->saidx
, &saidx
) == 0)
5856 /* Delete all non-LARVAL SAs. */
5858 stateidx
< _ARRAYLEN(saorder_state_alive
);
5860 state
= saorder_state_alive
[stateidx
];
5861 if (state
== SADB_SASTATE_LARVAL
)
5863 for (sav
= LIST_FIRST(&sah
->savtree
[state
]);
5864 sav
!= NULL
; sav
= nextsav
) {
5865 nextsav
= LIST_NEXT(sav
, chain
);
5867 if (sav
->state
!= state
) {
5868 ipseclog((LOG_DEBUG
, "key_delete_all: "
5869 "invalid sav->state "
5870 "(queue: %u SA: %u)\n",
5871 state
, sav
->state
));
5875 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
5882 struct sadb_msg
*newmsg
;
5884 /* create new sadb_msg to reply. */
5885 n
= key_gather_mbuf(m
, mhp
, 1, 3, SADB_EXT_RESERVED
,
5886 SADB_EXT_ADDRESS_SRC
, SADB_EXT_ADDRESS_DST
);
5888 return key_senderror(so
, m
, ENOBUFS
);
5890 if (n
->m_len
< sizeof(struct sadb_msg
)) {
5891 n
= m_pullup(n
, sizeof(struct sadb_msg
));
5893 return key_senderror(so
, m
, ENOBUFS
);
5895 newmsg
= mtod(n
, struct sadb_msg
*);
5896 newmsg
->sadb_msg_errno
= 0;
5897 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
5900 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
5905 * SADB_GET processing
5907 * <base, SA(*), address(SD)>
5908 * from the ikmpd, and get a SP and a SA to respond,
5910 * <base, SA, (lifetime(HSC),) address(SD), (address(P),) key(AE),
5911 * (identity(SD),) (sensitivity)>
5914 * m will always be freed.
5917 key_get(struct socket
*so
, struct mbuf
*m
, const struct sadb_msghdr
*mhp
)
5919 struct sadb_sa
*sa0
;
5920 struct sadb_address
*src0
, *dst0
;
5921 struct secasindex saidx
;
5922 struct secashead
*sah
;
5923 struct secasvar
*sav
= NULL
;
5927 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
5928 panic("key_get: NULL pointer is passed.");
5930 /* map satype to proto */
5931 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
5932 ipseclog((LOG_DEBUG
, "key_get: invalid satype is passed.\n"));
5933 return key_senderror(so
, m
, EINVAL
);
5936 if (mhp
->ext
[SADB_EXT_SA
] == NULL
||
5937 mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
5938 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
) {
5939 ipseclog((LOG_DEBUG
, "key_get: invalid message is passed.\n"));
5940 return key_senderror(so
, m
, EINVAL
);
5942 if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(struct sadb_sa
) ||
5943 mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
5944 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) {
5945 ipseclog((LOG_DEBUG
, "key_get: invalid message is passed.\n"));
5946 return key_senderror(so
, m
, EINVAL
);
5949 sa0
= (struct sadb_sa
*)mhp
->ext
[SADB_EXT_SA
];
5950 src0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_SRC
];
5951 dst0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_DST
];
5953 /* sa_len safety check */
5954 if (KEY_CHECKSALEN(src0
+ 1) != 0)
5955 return key_senderror(so
, m
, EINVAL
);
5956 if (KEY_CHECKSALEN(dst0
+ 1) != 0)
5957 return key_senderror(so
, m
, EINVAL
);
5959 KEY_SETSECASIDX(proto
, IPSEC_MODE_ANY
, 0, src0
+ 1, dst0
+ 1, &saidx
);
5961 /* If not using NAT-T, make sure if port number is zero. */
5963 KEY_PORTTOSADDR(&saidx
.src
, 0);
5964 KEY_PORTTOSADDR(&saidx
.dst
, 0);
5967 /* get a SA header */
5968 LIST_FOREACH(sah
, &sahtree
, chain
) {
5969 if (sah
->state
== SADB_SASTATE_DEAD
)
5971 if (key_cmpsaidx_withoutmode(&sah
->saidx
, &saidx
) == 0)
5974 /* get a SA with SPI. */
5975 sav
= key_getsavbyspi(sah
, sa0
->sadb_sa_spi
);
5980 ipseclog((LOG_DEBUG
, "key_get: no SA found.\n"));
5981 return key_senderror(so
, m
, ENOENT
);
5988 /* map proto to satype */
5989 if ((satype
= key_proto2satype(sah
->saidx
.proto
)) == 0) {
5990 ipseclog((LOG_DEBUG
, "key_get: there was invalid proto in SAD.\n"));
5991 return key_senderror(so
, m
, EINVAL
);
5994 /* create new sadb_msg to reply. */
5995 n
= key_setdumpsa(sav
, SADB_GET
, satype
, mhp
->msg
->sadb_msg_seq
,
5996 mhp
->msg
->sadb_msg_pid
);
5998 return key_senderror(so
, m
, ENOBUFS
);
6001 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ONE
);
6005 /* XXX make it sysctl-configurable? */
6007 key_getcomb_setlifetime(struct sadb_comb
*comb
)
6010 comb
->sadb_comb_soft_allocations
= 1;
6011 comb
->sadb_comb_hard_allocations
= 1;
6012 comb
->sadb_comb_soft_bytes
= 0;
6013 comb
->sadb_comb_hard_bytes
= 0;
6014 comb
->sadb_comb_hard_addtime
= 86400; /* 1 day */
6015 comb
->sadb_comb_soft_addtime
= comb
->sadb_comb_hard_addtime
* 80 / 100;
6016 comb
->sadb_comb_hard_usetime
= 28800; /* 8 hours */
6017 comb
->sadb_comb_soft_usetime
= comb
->sadb_comb_hard_usetime
* 80 / 100;
6022 * XXX reorder combinations by preference
6023 * XXX no idea if the user wants ESP authentication or not
6025 static struct mbuf
*
6026 key_getcomb_esp(void)
6028 struct sadb_comb
*comb
;
6029 const struct esp_algorithm
*algo
;
6030 struct mbuf
*result
= NULL
, *m
, *n
;
6034 const int l
= PFKEY_ALIGN8(sizeof(struct sadb_comb
));
6037 for (i
= 1; i
<= SADB_EALG_MAX
; i
++) {
6038 algo
= esp_algorithm_lookup(i
);
6042 if (algo
->keymax
< ipsec_esp_keymin
)
6044 if (algo
->keymin
< ipsec_esp_keymin
)
6045 encmin
= ipsec_esp_keymin
;
6047 encmin
= algo
->keymin
;
6050 m
= key_getcomb_ah();
6054 panic("assumption failed in key_getcomb_esp");
6056 MGET(m
, M_DONTWAIT
, MT_DATA
);
6061 memset(mtod(m
, void *), 0, m
->m_len
);
6068 for (n
= m
; n
; n
= n
->m_next
)
6072 panic("assumption failed in key_getcomb_esp");
6075 for (off
= 0; off
< totlen
; off
+= l
) {
6076 n
= m_pulldown(m
, off
, l
, &o
);
6078 /* m is already freed */
6081 comb
= (struct sadb_comb
*)(mtod(n
, char *) + o
);
6082 memset(comb
, 0, sizeof(*comb
));
6083 key_getcomb_setlifetime(comb
);
6084 comb
->sadb_comb_encrypt
= i
;
6085 comb
->sadb_comb_encrypt_minbits
= encmin
;
6086 comb
->sadb_comb_encrypt_maxbits
= algo
->keymax
;
6105 * XXX reorder combinations by preference
6107 static struct mbuf
*
6108 key_getcomb_ah(void)
6110 struct sadb_comb
*comb
;
6111 const struct ah_algorithm
*algo
;
6115 const int l
= PFKEY_ALIGN8(sizeof(struct sadb_comb
));
6118 for (i
= 1; i
<= SADB_AALG_MAX
; i
++) {
6120 /* we prefer HMAC algorithms, not old algorithms */
6121 if (i
!= SADB_AALG_SHA1HMAC
&& i
!= SADB_AALG_MD5HMAC
)
6124 algo
= ah_algorithm_lookup(i
);
6128 if (algo
->keymax
< ipsec_ah_keymin
)
6130 if (algo
->keymin
< ipsec_ah_keymin
)
6131 xmin
= ipsec_ah_keymin
;
6133 xmin
= algo
->keymin
;
6138 panic("assumption failed in key_getcomb_ah");
6140 MGET(m
, M_DONTWAIT
, MT_DATA
);
6147 M_PREPEND(m
, l
, M_DONTWAIT
);
6151 comb
= mtod(m
, struct sadb_comb
*);
6152 memset(comb
, 0, sizeof(*comb
));
6153 key_getcomb_setlifetime(comb
);
6154 comb
->sadb_comb_auth
= i
;
6155 comb
->sadb_comb_auth_minbits
= xmin
;
6156 comb
->sadb_comb_auth_maxbits
= algo
->keymax
;
6163 * not really an official behavior. discussed in pf_key@inner.net in Sep2000.
6164 * XXX reorder combinations by preference
6166 static struct mbuf
*
6167 key_getcomb_ipcomp(void)
6169 struct sadb_comb
*comb
;
6170 const struct ipcomp_algorithm
*algo
;
6173 const int l
= PFKEY_ALIGN8(sizeof(struct sadb_comb
));
6176 for (i
= 1; i
<= SADB_X_CALG_MAX
; i
++) {
6177 algo
= ipcomp_algorithm_lookup(i
);
6184 panic("assumption failed in key_getcomb_ipcomp");
6186 MGET(m
, M_DONTWAIT
, MT_DATA
);
6193 M_PREPEND(m
, l
, M_DONTWAIT
);
6197 comb
= mtod(m
, struct sadb_comb
*);
6198 memset(comb
, 0, sizeof(*comb
));
6199 key_getcomb_setlifetime(comb
);
6200 comb
->sadb_comb_encrypt
= i
;
6201 /* what should we set into sadb_comb_*_{min,max}bits? */
6208 * XXX no way to pass mode (transport/tunnel) to userland
6209 * XXX replay checking?
6210 * XXX sysctl interface to ipsec_{ah,esp}_keymin
6212 static struct mbuf
*
6213 key_getprop(const struct secasindex
*saidx
)
6215 struct sadb_prop
*prop
;
6217 const int l
= PFKEY_ALIGN8(sizeof(struct sadb_prop
));
6220 switch (saidx
->proto
) {
6223 m
= key_getcomb_esp();
6227 m
= key_getcomb_ah();
6229 case IPPROTO_IPCOMP
:
6230 m
= key_getcomb_ipcomp();
6238 M_PREPEND(m
, l
, M_DONTWAIT
);
6243 for (n
= m
; n
; n
= n
->m_next
)
6246 prop
= mtod(m
, struct sadb_prop
*);
6247 memset(prop
, 0, sizeof(*prop
));
6248 prop
->sadb_prop_len
= PFKEY_UNIT64(totlen
);
6249 prop
->sadb_prop_exttype
= SADB_EXT_PROPOSAL
;
6250 prop
->sadb_prop_replay
= 32; /* XXX */
6256 * SADB_ACQUIRE processing called by key_checkrequest() and key_acquire2().
6258 * <base, SA, address(SD), (address(P)), x_policy,
6259 * (identity(SD),) (sensitivity,) proposal>
6260 * to KMD, and expect to receive
6261 * <base> with SADB_ACQUIRE if error occurred,
6263 * <base, src address, dst address, (SPI range)> with SADB_GETSPI
6264 * from KMD by PF_KEY.
6266 * XXX x_policy is outside of RFC2367 (KAME extension).
6267 * XXX sensitivity is not supported.
6268 * XXX for ipcomp, RFC2367 does not define how to fill in proposal.
6269 * see comment for key_getcomb_ipcomp().
6273 * others: error number
6276 key_acquire(struct secasindex
*saidx
, struct secpolicy
*sp
)
6278 struct mbuf
*result
= NULL
, *m
;
6279 #ifndef IPSEC_NONBLOCK_ACQUIRE
6280 struct secacq
*newacq
;
6288 panic("key_acquire: NULL pointer is passed.");
6289 if ((satype
= key_proto2satype(saidx
->proto
)) == 0)
6290 panic("key_acquire: invalid proto is passed.");
6292 #ifndef IPSEC_NONBLOCK_ACQUIRE
6294 * We never do anything about acquirng SA. There is anather
6295 * solution that kernel blocks to send SADB_ACQUIRE message until
6296 * getting something message from IKEd. In later case, to be
6297 * managed with ACQUIRING list.
6299 /* get an entry to check whether sending message or not. */
6300 if ((newacq
= key_getacq(saidx
)) != NULL
) {
6301 if (key_blockacq_count
< newacq
->count
) {
6302 /* reset counter and do send message. */
6305 /* increment counter and do nothing. */
6310 /* make new entry for blocking to send SADB_ACQUIRE. */
6311 if ((newacq
= key_newacq(saidx
)) == NULL
)
6314 /* add to acqtree */
6315 LIST_INSERT_HEAD(&acqtree
, newacq
, chain
);
6319 #ifndef IPSEC_NONBLOCK_ACQUIRE
6322 seq
= (acq_seq
= (acq_seq
== ~0 ? 1 : ++acq_seq
));
6324 m
= key_setsadbmsg(SADB_ACQUIRE
, 0, satype
, seq
, 0, 0);
6331 /* set sadb_address for saidx's. */
6332 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
6333 (struct sockaddr
*)&saidx
->src
, FULLMASK
, IPSEC_ULPROTO_ANY
);
6340 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
6341 (struct sockaddr
*)&saidx
->dst
, FULLMASK
, IPSEC_ULPROTO_ANY
);
6348 /* XXX proxy address (optional) */
6350 /* set sadb_x_policy */
6352 m
= key_setsadbxpolicy(sp
->policy
, sp
->dir
, sp
->id
);
6360 #ifdef SADB_X_EXT_TAG
6361 /* set sadb_x_tag */
6362 if (sp
&& sp
->tag
) {
6363 m
= key_setsadbxtag(sp
->tag
);
6372 /* XXX identity (optional) */
6374 if (idexttype
&& fqdn
) {
6375 /* create identity extension (FQDN) */
6376 struct sadb_ident
*id
;
6379 fqdnlen
= strlen(fqdn
) + 1; /* +1 for terminating-NUL */
6380 id
= (struct sadb_ident
*)p
;
6381 memset(id
, 0, sizeof(*id
) + PFKEY_ALIGN8(fqdnlen
));
6382 id
->sadb_ident_len
= PFKEY_UNIT64(sizeof(*id
) + PFKEY_ALIGN8(fqdnlen
));
6383 id
->sadb_ident_exttype
= idexttype
;
6384 id
->sadb_ident_type
= SADB_IDENTTYPE_FQDN
;
6385 memcpy(id
+ 1, fqdn
, fqdnlen
);
6386 p
+= sizeof(struct sadb_ident
) + PFKEY_ALIGN8(fqdnlen
);
6390 /* create identity extension (USERFQDN) */
6391 struct sadb_ident
*id
;
6395 /* +1 for terminating-NUL */
6396 userfqdnlen
= strlen(userfqdn
) + 1;
6399 id
= (struct sadb_ident
*)p
;
6400 memset(id
, 0, sizeof(*id
) + PFKEY_ALIGN8(userfqdnlen
));
6401 id
->sadb_ident_len
= PFKEY_UNIT64(sizeof(*id
) + PFKEY_ALIGN8(userfqdnlen
));
6402 id
->sadb_ident_exttype
= idexttype
;
6403 id
->sadb_ident_type
= SADB_IDENTTYPE_USERFQDN
;
6404 /* XXX is it correct? */
6406 id
->sadb_ident_id
= kauth_cred_getuid(curlwp
->l_cred
);
6407 if (userfqdn
&& userfqdnlen
)
6408 memcpy(id
+ 1, userfqdn
, userfqdnlen
);
6409 p
+= sizeof(struct sadb_ident
) + PFKEY_ALIGN8(userfqdnlen
);
6413 /* XXX sensitivity (optional) */
6415 /* create proposal/combination extension */
6416 m
= key_getprop(saidx
);
6419 * spec conformant: always attach proposal/combination extension,
6420 * the problem is that we have no way to attach it for ipcomp,
6421 * due to the way sadb_comb is declared in RFC2367.
6430 * outside of spec; make proposal/combination extension optional.
6436 if ((result
->m_flags
& M_PKTHDR
) == 0) {
6441 if (result
->m_len
< sizeof(struct sadb_msg
)) {
6442 result
= m_pullup(result
, sizeof(struct sadb_msg
));
6443 if (result
== NULL
) {
6449 result
->m_pkthdr
.len
= 0;
6450 for (m
= result
; m
; m
= m
->m_next
)
6451 result
->m_pkthdr
.len
+= m
->m_len
;
6453 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
6454 PFKEY_UNIT64(result
->m_pkthdr
.len
);
6456 return key_sendup_mbuf(NULL
, result
, KEY_SENDUP_REGISTERED
);
6464 #ifndef IPSEC_NONBLOCK_ACQUIRE
6465 static struct secacq
*
6466 key_newacq(struct secasindex
*saidx
)
6468 struct secacq
*newacq
;
6471 KMALLOC(newacq
, struct secacq
*, sizeof(struct secacq
));
6472 if (newacq
== NULL
) {
6473 ipseclog((LOG_DEBUG
, "key_newacq: No more memory.\n"));
6476 memset(newacq
, 0, sizeof(*newacq
));
6479 memcpy(&newacq
->saidx
, saidx
, sizeof(newacq
->saidx
));
6480 newacq
->seq
= (acq_seq
== ~0 ? 1 : ++acq_seq
);
6481 newacq
->created
= time_second
;
6487 static struct secacq
*
6488 key_getacq(struct secasindex
*saidx
)
6492 LIST_FOREACH(acq
, &acqtree
, chain
) {
6493 if (key_cmpsaidx_exactly(saidx
, &acq
->saidx
))
6500 static struct secacq
*
6501 key_getacqbyseq(u_int32_t seq
)
6505 LIST_FOREACH(acq
, &acqtree
, chain
) {
6506 if (acq
->seq
== seq
)
6514 static struct secspacq
*
6515 key_newspacq(struct secpolicyindex
*spidx
)
6517 struct secspacq
*acq
;
6523 KMALLOC(acq
, struct secspacq
*, sizeof(struct secspacq
));
6525 ipseclog((LOG_DEBUG
, "key_newspacq: No more memory.\n"));
6528 memset(acq
, 0, sizeof(*acq
));
6531 memcpy(&acq
->spidx
, spidx
, sizeof(acq
->spidx
));
6532 acq
->created
= time_second
;
6538 static struct secspacq
*
6539 key_getspacq(struct secpolicyindex
*spidx
)
6541 struct secspacq
*acq
;
6546 LIST_FOREACH(acq
, &spacqtree
, chain
) {
6547 if (key_cmpspidx_exactly(spidx
, &acq
->spidx
))
6555 * SADB_ACQUIRE processing,
6556 * in first situation, is receiving
6558 * from the ikmpd, and clear sequence of its secasvar entry.
6560 * In second situation, is receiving
6561 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
6562 * from a user land process, and return
6563 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
6566 * m will always be freed.
6569 key_acquire2(struct socket
*so
, struct mbuf
*m
, const struct sadb_msghdr
*mhp
)
6571 const struct sadb_address
*src0
, *dst0
;
6572 struct secasindex saidx
;
6573 struct secashead
*sah
;
6578 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
6579 panic("key_acquire2: NULL pointer is passed.");
6582 * Error message from KMd.
6583 * We assume that if error was occurred in IKEd, the length of PFKEY
6584 * message is equal to the size of sadb_msg structure.
6585 * We do not raise error even if error occurred in this function.
6587 if (mhp
->msg
->sadb_msg_len
== PFKEY_UNIT64(sizeof(struct sadb_msg
))) {
6588 #ifndef IPSEC_NONBLOCK_ACQUIRE
6591 /* check sequence number */
6592 if (mhp
->msg
->sadb_msg_seq
== 0) {
6593 ipseclog((LOG_DEBUG
, "key_acquire2: must specify sequence number.\n"));
6598 if ((acq
= key_getacqbyseq(mhp
->msg
->sadb_msg_seq
)) == NULL
) {
6600 * the specified larval SA is already gone, or we got
6601 * a bogus sequence number. we can silently ignore it.
6607 /* reset acq counter in order to deletion by timehander. */
6608 acq
->created
= time_second
;
6616 * This message is from user land.
6619 /* map satype to proto */
6620 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
6621 ipseclog((LOG_DEBUG
, "key_acquire2: invalid satype is passed.\n"));
6622 return key_senderror(so
, m
, EINVAL
);
6625 if (mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
6626 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
||
6627 mhp
->ext
[SADB_EXT_PROPOSAL
] == NULL
) {
6629 ipseclog((LOG_DEBUG
, "key_acquire2: invalid message is passed.\n"));
6630 return key_senderror(so
, m
, EINVAL
);
6632 if (mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
6633 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
) ||
6634 mhp
->extlen
[SADB_EXT_PROPOSAL
] < sizeof(struct sadb_prop
)) {
6636 ipseclog((LOG_DEBUG
, "key_acquire2: invalid message is passed.\n"));
6637 return key_senderror(so
, m
, EINVAL
);
6640 src0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_SRC
];
6641 dst0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_DST
];
6643 /* sa_len safety check */
6644 if (KEY_CHECKSALEN(src0
+ 1) != 0)
6645 return key_senderror(so
, m
, EINVAL
);
6646 if (KEY_CHECKSALEN(dst0
+ 1) != 0)
6647 return key_senderror(so
, m
, EINVAL
);
6649 KEY_SETSECASIDX(proto
, IPSEC_MODE_ANY
, 0, src0
+ 1, dst0
+ 1, &saidx
);
6651 /* If not using NAT-T, make sure if port number is zero. */
6653 KEY_PORTTOSADDR(&saidx
.src
, 0);
6654 KEY_PORTTOSADDR(&saidx
.dst
, 0);
6657 /* get a SA index */
6658 LIST_FOREACH(sah
, &sahtree
, chain
) {
6659 if (sah
->state
== SADB_SASTATE_DEAD
)
6661 if (key_cmpsaidx_withmode(&sah
->saidx
, &saidx
))
6665 ipseclog((LOG_DEBUG
, "key_acquire2: a SA exists already.\n"));
6666 return key_senderror(so
, m
, EEXIST
);
6669 error
= key_acquire(&saidx
, NULL
);
6671 ipseclog((LOG_DEBUG
, "key_acquire2: error %d returned "
6672 "from key_acquire.\n", mhp
->msg
->sadb_msg_errno
));
6673 return key_senderror(so
, m
, error
);
6676 return key_sendup_mbuf(so
, m
, KEY_SENDUP_REGISTERED
);
6680 * SADB_REGISTER processing.
6681 * If SATYPE_UNSPEC has been passed as satype, only return sabd_supported.
6684 * from the ikmpd, and register a socket to send PF_KEY messages,
6688 * If socket is detached, must free from regnode.
6690 * m will always be freed.
6693 key_register(struct socket
*so
, struct mbuf
*m
, const struct sadb_msghdr
*mhp
)
6695 struct secreg
*reg
, *newreg
= 0;
6698 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
6699 panic("key_register: NULL pointer is passed.");
6701 /* check for invalid register message */
6702 if (mhp
->msg
->sadb_msg_satype
>= sizeof(regtree
)/sizeof(regtree
[0]))
6703 return key_senderror(so
, m
, EINVAL
);
6705 /* When SATYPE_UNSPEC is specified, only return sabd_supported. */
6706 if (mhp
->msg
->sadb_msg_satype
== SADB_SATYPE_UNSPEC
)
6709 /* check whether existing or not */
6710 LIST_FOREACH(reg
, ®tree
[mhp
->msg
->sadb_msg_satype
], chain
) {
6711 if (reg
->so
== so
) {
6712 ipseclog((LOG_DEBUG
, "key_register: socket exists already.\n"));
6713 return key_senderror(so
, m
, EEXIST
);
6717 /* create regnode */
6718 KMALLOC(newreg
, struct secreg
*, sizeof(*newreg
));
6719 if (newreg
== NULL
) {
6720 ipseclog((LOG_DEBUG
, "key_register: No more memory.\n"));
6721 return key_senderror(so
, m
, ENOBUFS
);
6723 memset((void *)newreg
, 0, sizeof(*newreg
));
6726 ((struct keycb
*)sotorawcb(so
))->kp_registered
++;
6728 /* add regnode to regtree. */
6729 LIST_INSERT_HEAD(®tree
[mhp
->msg
->sadb_msg_satype
], newreg
, chain
);
6734 struct sadb_msg
*newmsg
;
6735 struct sadb_supported
*sup
;
6736 u_int len
, alen
, elen
;
6739 struct sadb_alg
*alg
;
6741 /* create new sadb_msg to reply. */
6743 for (i
= 1; i
<= SADB_AALG_MAX
; i
++) {
6744 if (ah_algorithm_lookup(i
))
6745 alen
+= sizeof(struct sadb_alg
);
6748 alen
+= sizeof(struct sadb_supported
);
6751 for (i
= 1; i
<= SADB_EALG_MAX
; i
++) {
6752 if (esp_algorithm_lookup(i
))
6753 elen
+= sizeof(struct sadb_alg
);
6756 elen
+= sizeof(struct sadb_supported
);
6759 len
= sizeof(struct sadb_msg
) + alen
+ elen
;
6762 return key_senderror(so
, m
, ENOBUFS
);
6764 MGETHDR(n
, M_DONTWAIT
, MT_DATA
);
6766 MCLGET(n
, M_DONTWAIT
);
6767 if ((n
->m_flags
& M_EXT
) == 0) {
6773 return key_senderror(so
, m
, ENOBUFS
);
6775 n
->m_pkthdr
.len
= n
->m_len
= len
;
6779 m_copydata(m
, 0, sizeof(struct sadb_msg
), mtod(n
, char *) + off
);
6780 newmsg
= mtod(n
, struct sadb_msg
*);
6781 newmsg
->sadb_msg_errno
= 0;
6782 newmsg
->sadb_msg_len
= PFKEY_UNIT64(len
);
6783 off
+= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
6785 /* for authentication algorithm */
6787 sup
= (struct sadb_supported
*)(mtod(n
, char *) + off
);
6788 sup
->sadb_supported_len
= PFKEY_UNIT64(alen
);
6789 sup
->sadb_supported_exttype
= SADB_EXT_SUPPORTED_AUTH
;
6790 off
+= PFKEY_ALIGN8(sizeof(*sup
));
6792 for (i
= 1; i
<= SADB_AALG_MAX
; i
++) {
6793 const struct ah_algorithm
*aalgo
;
6795 aalgo
= ah_algorithm_lookup(i
);
6798 alg
= (struct sadb_alg
*)(mtod(n
, char *) + off
);
6799 alg
->sadb_alg_id
= i
;
6800 alg
->sadb_alg_ivlen
= 0;
6801 alg
->sadb_alg_minbits
= aalgo
->keymin
;
6802 alg
->sadb_alg_maxbits
= aalgo
->keymax
;
6803 off
+= PFKEY_ALIGN8(sizeof(*alg
));
6808 /* for encryption algorithm */
6810 sup
= (struct sadb_supported
*)(mtod(n
, char *) + off
);
6811 sup
->sadb_supported_len
= PFKEY_UNIT64(elen
);
6812 sup
->sadb_supported_exttype
= SADB_EXT_SUPPORTED_ENCRYPT
;
6813 off
+= PFKEY_ALIGN8(sizeof(*sup
));
6815 for (i
= 1; i
<= SADB_EALG_MAX
; i
++) {
6816 const struct esp_algorithm
*ealgo
;
6818 ealgo
= esp_algorithm_lookup(i
);
6821 alg
= (struct sadb_alg
*)(mtod(n
, char *) + off
);
6822 alg
->sadb_alg_id
= i
;
6823 if (ealgo
&& ealgo
->ivlen
) {
6825 * give NULL to get the value preferred by
6826 * algorithm XXX SADB_X_EXT_DERIV ?
6828 alg
->sadb_alg_ivlen
=
6829 (*ealgo
->ivlen
)(ealgo
, NULL
);
6831 alg
->sadb_alg_ivlen
= 0;
6832 alg
->sadb_alg_minbits
= ealgo
->keymin
;
6833 alg
->sadb_alg_maxbits
= ealgo
->keymax
;
6834 off
+= PFKEY_ALIGN8(sizeof(struct sadb_alg
));
6841 panic("length assumption failed in key_register");
6845 return key_sendup_mbuf(so
, n
, KEY_SENDUP_REGISTERED
);
6850 * free secreg entry registered.
6851 * XXX: I want to do free a socket marked done SADB_RESIGER to socket.
6854 key_freereg(struct socket
*so
)
6861 panic("key_freereg: NULL pointer is passed.");
6864 * check whether existing or not.
6865 * check all type of SA, because there is a potential that
6866 * one socket is registered to multiple type of SA.
6868 for (i
= 0; i
<= SADB_SATYPE_MAX
; i
++) {
6869 LIST_FOREACH(reg
, ®tree
[i
], chain
) {
6870 if (reg
->so
== so
&& __LIST_CHAINED(reg
)) {
6871 LIST_REMOVE(reg
, chain
);
6882 * SADB_EXPIRE processing
6884 * <base, SA, SA2, lifetime(C and one of HS), address(SD)>
6886 * NOTE: We send only soft lifetime extension.
6889 * others : error number
6892 key_expire(struct secasvar
*sav
)
6896 struct mbuf
*result
= NULL
, *m
;
6899 struct sadb_lifetime
*lt
;
6901 /* XXX: Why do we lock ? */
6902 s
= splsoftnet(); /*called from softclock()*/
6906 panic("key_expire: NULL pointer is passed.");
6907 if (sav
->sah
== NULL
)
6908 panic("key_expire: Why was SA index in SA NULL.");
6909 if ((satype
= key_proto2satype(sav
->sah
->saidx
.proto
)) == 0)
6910 panic("key_expire: invalid proto is passed.");
6912 /* set msg header */
6913 m
= key_setsadbmsg(SADB_EXPIRE
, 0, satype
, sav
->seq
, 0, sav
->refcnt
);
6920 /* create SA extension */
6921 m
= key_setsadbsa(sav
);
6928 /* create SA extension */
6929 m
= key_setsadbxsa2(sav
->sah
->saidx
.mode
,
6930 sav
->replay
? (sav
->replay
->count
& 0xffffffff) : 0,
6931 sav
->sah
->saidx
.reqid
);
6938 /* create lifetime extension (current and soft) */
6939 len
= PFKEY_ALIGN8(sizeof(*lt
)) * 2;
6940 m
= key_alloc_mbuf(len
);
6941 if (!m
|| m
->m_next
) { /*XXX*/
6947 memset(mtod(m
, void *), 0, len
);
6948 lt
= mtod(m
, struct sadb_lifetime
*);
6949 lt
->sadb_lifetime_len
= PFKEY_UNIT64(sizeof(struct sadb_lifetime
));
6950 lt
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_CURRENT
;
6951 lt
->sadb_lifetime_allocations
= sav
->lft_c
->sadb_lifetime_allocations
;
6952 lt
->sadb_lifetime_bytes
= sav
->lft_c
->sadb_lifetime_bytes
;
6953 lt
->sadb_lifetime_addtime
= sav
->lft_c
->sadb_lifetime_addtime
;
6954 lt
->sadb_lifetime_usetime
= sav
->lft_c
->sadb_lifetime_usetime
;
6955 lt
= (struct sadb_lifetime
*)(mtod(m
, char *) + len
/ 2);
6956 memcpy(lt
, sav
->lft_s
, sizeof(*lt
));
6959 /* set sadb_address for source */
6960 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
6961 (struct sockaddr
*)&sav
->sah
->saidx
.src
,
6962 FULLMASK
, IPSEC_ULPROTO_ANY
);
6969 /* set sadb_address for destination */
6970 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
6971 (struct sockaddr
*)&sav
->sah
->saidx
.dst
,
6972 FULLMASK
, IPSEC_ULPROTO_ANY
);
6979 if ((result
->m_flags
& M_PKTHDR
) == 0) {
6984 if (result
->m_len
< sizeof(struct sadb_msg
)) {
6985 result
= m_pullup(result
, sizeof(struct sadb_msg
));
6986 if (result
== NULL
) {
6992 result
->m_pkthdr
.len
= 0;
6993 for (m
= result
; m
; m
= m
->m_next
)
6994 result
->m_pkthdr
.len
+= m
->m_len
;
6996 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
6997 PFKEY_UNIT64(result
->m_pkthdr
.len
);
7000 return key_sendup_mbuf(NULL
, result
, KEY_SENDUP_REGISTERED
);
7010 * SADB_FLUSH processing
7013 * from the ikmpd, and free all entries in secastree.
7017 * NOTE: to do is only marking SADB_SASTATE_DEAD.
7019 * m will always be freed.
7022 key_flush(struct socket
*so
, struct mbuf
*m
, const struct sadb_msghdr
*mhp
)
7024 struct sadb_msg
*newmsg
;
7025 struct secashead
*sah
, *nextsah
;
7026 struct secasvar
*sav
, *nextsav
;
7032 if (so
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
7033 panic("key_flush: NULL pointer is passed.");
7035 /* map satype to proto */
7036 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
7037 ipseclog((LOG_DEBUG
, "key_flush: invalid satype is passed.\n"));
7038 return key_senderror(so
, m
, EINVAL
);
7041 /* no SATYPE specified, i.e. flushing all SA. */
7042 for (sah
= LIST_FIRST(&sahtree
); sah
!= NULL
; sah
= nextsah
) {
7043 nextsah
= LIST_NEXT(sah
, chain
);
7045 if (mhp
->msg
->sadb_msg_satype
!= SADB_SATYPE_UNSPEC
&&
7046 proto
!= sah
->saidx
.proto
)
7050 stateidx
< _ARRAYLEN(saorder_state_alive
);
7052 state
= saorder_state_any
[stateidx
];
7053 for (sav
= LIST_FIRST(&sah
->savtree
[state
]);
7057 nextsav
= LIST_NEXT(sav
, chain
);
7059 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
7064 sah
->state
= SADB_SASTATE_DEAD
;
7067 if (m
->m_len
< sizeof(struct sadb_msg
) ||
7068 sizeof(struct sadb_msg
) > m
->m_len
+ M_TRAILINGSPACE(m
)) {
7069 ipseclog((LOG_DEBUG
, "key_flush: No more memory.\n"));
7070 return key_senderror(so
, m
, ENOBUFS
);
7076 m
->m_pkthdr
.len
= m
->m_len
= sizeof(struct sadb_msg
);
7077 newmsg
= mtod(m
, struct sadb_msg
*);
7078 newmsg
->sadb_msg_errno
= 0;
7079 newmsg
->sadb_msg_len
= PFKEY_UNIT64(m
->m_pkthdr
.len
);
7081 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ALL
);
7085 * SADB_DUMP processing
7086 * dump all entries including status of DEAD in SAD.
7089 * from the ikmpd, and dump all secasvar leaves
7094 * m will always be freed.
7097 key_dump(struct socket
*so
, struct mbuf
*m
, const struct sadb_msghdr
*mhp
)
7099 struct secashead
*sah
;
7100 struct secasvar
*sav
;
7105 int cnt
, error
= 0, needwait
= 0;
7110 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
7111 panic("key_dump: NULL pointer is passed.");
7113 /* map satype to proto */
7114 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
7115 ipseclog((LOG_DEBUG
, "key_dump: invalid satype is passed.\n"));
7116 return key_senderror(so
, m
, EINVAL
);
7119 /* count sav entries to be sent to the userland. */
7121 LIST_FOREACH(sah
, &sahtree
, chain
) {
7122 if (mhp
->msg
->sadb_msg_satype
!= SADB_SATYPE_UNSPEC
&&
7123 proto
!= sah
->saidx
.proto
)
7127 stateidx
< _ARRAYLEN(saorder_state_any
);
7129 state
= saorder_state_any
[stateidx
];
7130 LIST_FOREACH(sav
, &sah
->savtree
[state
], chain
) {
7137 return key_senderror(so
, m
, ENOENT
);
7139 /* send this to the userland, one at a time. */
7140 LIST_FOREACH(sah
, &sahtree
, chain
) {
7141 if (mhp
->msg
->sadb_msg_satype
!= SADB_SATYPE_UNSPEC
&&
7142 proto
!= sah
->saidx
.proto
)
7145 /* map proto to satype */
7146 if ((satype
= key_proto2satype(sah
->saidx
.proto
)) == 0) {
7147 ipseclog((LOG_DEBUG
, "key_dump: there was invalid proto in SAD.\n"));
7148 return key_senderror(so
, m
, EINVAL
);
7152 stateidx
< _ARRAYLEN(saorder_state_any
);
7154 state
= saorder_state_any
[stateidx
];
7155 LIST_FOREACH(sav
, &sah
->savtree
[state
], chain
) {
7156 n
= key_setdumpsa(sav
, SADB_DUMP
, satype
,
7157 --cnt
, mhp
->msg
->sadb_msg_pid
);
7159 return key_senderror(so
, m
, ENOBUFS
);
7161 error
= key_sendup_mbuf(so
, n
,
7162 KEY_SENDUP_ONE
| KEY_SENDUP_CANWAIT
);
7163 if (error
== EAGAIN
)
7169 kp
= (struct keycb
*)sotorawcb(so
);
7170 while (needwait
&& kp
->kp_queue
)
7171 sbwait(&so
->so_rcv
);
7177 static struct mbuf
*
7178 key_setdump(u_int8_t req_satype
, int *errorp
)
7180 struct secashead
*sah
;
7181 struct secasvar
*sav
;
7189 /* map satype to proto */
7190 if ((proto
= key_satype2proto(req_satype
)) == 0) {
7195 /* count sav entries to be sent to the userland. */
7197 LIST_FOREACH(sah
, &sahtree
, chain
) {
7198 if (req_satype
!= SADB_SATYPE_UNSPEC
&&
7199 proto
!= sah
->saidx
.proto
)
7203 stateidx
< _ARRAYLEN(saorder_state_any
);
7205 state
= saorder_state_any
[stateidx
];
7206 LIST_FOREACH(sav
, &sah
->savtree
[state
], chain
) {
7217 /* send this to the userland, one at a time. */
7219 LIST_FOREACH(sah
, &sahtree
, chain
) {
7220 if (req_satype
!= SADB_SATYPE_UNSPEC
&&
7221 proto
!= sah
->saidx
.proto
)
7224 /* map proto to satype */
7225 if ((satype
= key_proto2satype(sah
->saidx
.proto
)) == 0) {
7232 stateidx
< _ARRAYLEN(saorder_state_any
);
7234 state
= saorder_state_any
[stateidx
];
7235 LIST_FOREACH(sav
, &sah
->savtree
[state
], chain
) {
7236 n
= key_setdumpsa(sav
, SADB_DUMP
, satype
,
7257 if ((m
->m_flags
& M_PKTHDR
) != 0) {
7258 m
->m_pkthdr
.len
= 0;
7259 for (n
= m
; n
; n
= n
->m_next
)
7260 m
->m_pkthdr
.len
+= n
->m_len
;
7268 key_setdumpsa_spi(u_int32_t spi
)
7271 struct secasvar
*sav
;
7276 LIST_FOREACH(sav
, &spihash
[SPIHASH(spi
)], spihash
) {
7277 if (sav
->spi
!= spi
)
7286 LIST_FOREACH(sav
, &spihash
[SPIHASH(spi
)], spihash
) {
7287 if (sav
->spi
!= spi
)
7289 satype
= key_proto2satype(sav
->sah
->saidx
.proto
);
7290 n
= key_setdumpsa(sav
, SADB_DUMP
, satype
, --cnt
, 0);
7300 if ((m
->m_flags
& M_PKTHDR
) != 0) {
7301 m
->m_pkthdr
.len
= 0;
7302 for (n
= m
; n
; n
= n
->m_next
)
7303 m
->m_pkthdr
.len
+= n
->m_len
;
7310 * SADB_X_PROMISC processing
7312 * m will always be freed.
7315 key_promisc(struct socket
*so
, struct mbuf
*m
, const struct sadb_msghdr
*mhp
)
7320 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
7321 panic("key_promisc: NULL pointer is passed.");
7323 olen
= PFKEY_UNUNIT64(mhp
->msg
->sadb_msg_len
);
7325 if (olen
< sizeof(struct sadb_msg
)) {
7327 return key_senderror(so
, m
, EINVAL
);
7332 } else if (olen
== sizeof(struct sadb_msg
)) {
7333 /* enable/disable promisc mode */
7336 if ((kp
= (struct keycb
*)sotorawcb(so
)) == NULL
)
7337 return key_senderror(so
, m
, EINVAL
);
7338 mhp
->msg
->sadb_msg_errno
= 0;
7339 switch (mhp
->msg
->sadb_msg_satype
) {
7342 kp
->kp_promisc
= mhp
->msg
->sadb_msg_satype
;
7345 return key_senderror(so
, m
, EINVAL
);
7348 /* send the original message back to everyone */
7349 mhp
->msg
->sadb_msg_errno
= 0;
7350 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ALL
);
7352 /* send packet as is */
7354 m_adj(m
, PFKEY_ALIGN8(sizeof(struct sadb_msg
)));
7356 /* TODO: if sadb_msg_seq is specified, send to specific pid */
7357 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ALL
);
7361 static int (*key_typesw
[])(struct socket
*, struct mbuf
*,
7362 const struct sadb_msghdr
*) = {
7363 NULL
, /* SADB_RESERVED */
7364 key_getspi
, /* SADB_GETSPI */
7365 key_update
, /* SADB_UPDATE */
7366 key_add
, /* SADB_ADD */
7367 key_delete
, /* SADB_DELETE */
7368 key_get
, /* SADB_GET */
7369 key_acquire2
, /* SADB_ACQUIRE */
7370 key_register
, /* SADB_REGISTER */
7371 NULL
, /* SADB_EXPIRE */
7372 key_flush
, /* SADB_FLUSH */
7373 key_dump
, /* SADB_DUMP */
7374 key_promisc
, /* SADB_X_PROMISC */
7375 NULL
, /* SADB_X_PCHANGE */
7376 key_spdadd
, /* SADB_X_SPDUPDATE */
7377 key_spdadd
, /* SADB_X_SPDADD */
7378 key_spddelete
, /* SADB_X_SPDDELETE */
7379 key_spdget
, /* SADB_X_SPDGET */
7380 NULL
, /* SADB_X_SPDACQUIRE */
7381 key_spddump
, /* SADB_X_SPDDUMP */
7382 key_spdflush
, /* SADB_X_SPDFLUSH */
7383 key_spdadd
, /* SADB_X_SPDSETIDX */
7384 NULL
, /* SADB_X_SPDEXPIRE */
7385 key_spddelete2
, /* SADB_X_SPDDELETE2 */
7387 key_nat_map
, /* SADB_X_NAT_T_NEW_MAPPING */
7394 * parse sadb_msg buffer to process PFKEYv2,
7395 * and create a data to response if needed.
7396 * I think to be dealed with mbuf directly.
7398 * msgp : pointer to pointer to a received buffer pulluped.
7399 * This is rewrited to response.
7400 * so : pointer to socket.
7402 * length for buffer to send to user process.
7405 key_parse(struct mbuf
*m
, struct socket
*so
)
7407 struct sadb_msg
*msg
;
7408 struct sadb_msghdr mh
;
7413 struct sockaddr_in6
*sin6
;
7417 if (m
== NULL
|| so
== NULL
)
7418 panic("key_parse: NULL pointer is passed.");
7420 #if 0 /*kdebug_sadb assumes msg in linear buffer*/
7421 KEYDEBUG(KEYDEBUG_KEY_DUMP
,
7422 ipseclog((LOG_DEBUG
, "key_parse: passed sadb_msg\n"));
7426 if (m
->m_len
< sizeof(struct sadb_msg
)) {
7427 m
= m_pullup(m
, sizeof(struct sadb_msg
));
7431 msg
= mtod(m
, struct sadb_msg
*);
7432 orglen
= PFKEY_UNUNIT64(msg
->sadb_msg_len
);
7433 target
= KEY_SENDUP_ONE
;
7435 if ((m
->m_flags
& M_PKTHDR
) == 0 ||
7436 m
->m_pkthdr
.len
!= m
->m_pkthdr
.len
) {
7437 ipseclog((LOG_DEBUG
, "key_parse: invalid message length.\n"));
7438 PFKEY_STATINC(PFKEY_STAT_OUT_INVLEN
);
7443 if (msg
->sadb_msg_version
!= PF_KEY_V2
) {
7444 ipseclog((LOG_DEBUG
,
7445 "key_parse: PF_KEY version %u is mismatched.\n",
7446 msg
->sadb_msg_version
));
7447 PFKEY_STATINC(PFKEY_STAT_OUT_INVVER
);
7452 if (msg
->sadb_msg_type
> SADB_MAX
) {
7453 ipseclog((LOG_DEBUG
, "key_parse: invalid type %u is passed.\n",
7454 msg
->sadb_msg_type
));
7455 PFKEY_STATINC(PFKEY_STAT_OUT_INVMSGTYPE
);
7460 /* for old-fashioned code - should be nuked */
7461 if (m
->m_pkthdr
.len
> MCLBYTES
) {
7468 MGETHDR(n
, M_DONTWAIT
, MT_DATA
);
7469 if (n
&& m
->m_pkthdr
.len
> MHLEN
) {
7470 MCLGET(n
, M_DONTWAIT
);
7471 if ((n
->m_flags
& M_EXT
) == 0) {
7480 m_copydata(m
, 0, m
->m_pkthdr
.len
, mtod(n
, void *));
7481 n
->m_pkthdr
.len
= n
->m_len
= m
->m_pkthdr
.len
;
7487 /* align the mbuf chain so that extensions are in contiguous region. */
7488 error
= key_align(m
, &mh
);
7495 switch (msg
->sadb_msg_satype
) {
7496 case SADB_SATYPE_UNSPEC
:
7497 switch (msg
->sadb_msg_type
) {
7505 ipseclog((LOG_DEBUG
, "key_parse: must specify satype "
7506 "when msg type=%u.\n", msg
->sadb_msg_type
));
7507 PFKEY_STATINC(PFKEY_STAT_OUT_INVSATYPE
);
7512 case SADB_SATYPE_AH
:
7513 case SADB_SATYPE_ESP
:
7514 case SADB_X_SATYPE_IPCOMP
:
7515 case SADB_X_SATYPE_TCPSIGNATURE
:
7516 switch (msg
->sadb_msg_type
) {
7518 case SADB_X_SPDDELETE
:
7520 case SADB_X_SPDDUMP
:
7521 case SADB_X_SPDFLUSH
:
7522 case SADB_X_SPDSETIDX
:
7523 case SADB_X_SPDUPDATE
:
7524 case SADB_X_SPDDELETE2
:
7525 ipseclog((LOG_DEBUG
, "key_parse: illegal satype=%u\n",
7526 msg
->sadb_msg_type
));
7527 PFKEY_STATINC(PFKEY_STAT_OUT_INVSATYPE
);
7532 case SADB_SATYPE_RSVP
:
7533 case SADB_SATYPE_OSPFV2
:
7534 case SADB_SATYPE_RIPV2
:
7535 case SADB_SATYPE_MIP
:
7536 ipseclog((LOG_DEBUG
, "key_parse: type %u isn't supported.\n",
7537 msg
->sadb_msg_satype
));
7538 PFKEY_STATINC(PFKEY_STAT_OUT_INVSATYPE
);
7541 case 1: /* XXX: What does it do? */
7542 if (msg
->sadb_msg_type
== SADB_X_PROMISC
)
7546 ipseclog((LOG_DEBUG
, "key_parse: invalid type %u is passed.\n",
7547 msg
->sadb_msg_satype
));
7548 PFKEY_STATINC(PFKEY_STAT_OUT_INVSATYPE
);
7553 /* check field of upper layer protocol and address family */
7554 if (mh
.ext
[SADB_EXT_ADDRESS_SRC
] != NULL
&&
7555 mh
.ext
[SADB_EXT_ADDRESS_DST
] != NULL
) {
7556 struct sadb_address
*src0
, *dst0
;
7559 src0
= (struct sadb_address
*)(mh
.ext
[SADB_EXT_ADDRESS_SRC
]);
7560 dst0
= (struct sadb_address
*)(mh
.ext
[SADB_EXT_ADDRESS_DST
]);
7562 /* check upper layer protocol */
7563 if (src0
->sadb_address_proto
!= dst0
->sadb_address_proto
) {
7564 ipseclog((LOG_DEBUG
, "key_parse: upper layer protocol mismatched.\n"));
7565 PFKEY_STATINC(PFKEY_STAT_OUT_INVADDR
);
7571 if (PFKEY_ADDR_SADDR(src0
)->sa_family
!=
7572 PFKEY_ADDR_SADDR(dst0
)->sa_family
) {
7573 ipseclog((LOG_DEBUG
, "key_parse: address family mismatched.\n"));
7574 PFKEY_STATINC(PFKEY_STAT_OUT_INVADDR
);
7578 if (PFKEY_ADDR_SADDR(src0
)->sa_len
!=
7579 PFKEY_ADDR_SADDR(dst0
)->sa_len
) {
7580 ipseclog((LOG_DEBUG
,
7581 "key_parse: address struct size mismatched.\n"));
7582 PFKEY_STATINC(PFKEY_STAT_OUT_INVADDR
);
7587 switch (PFKEY_ADDR_SADDR(src0
)->sa_family
) {
7589 if (PFKEY_ADDR_SADDR(src0
)->sa_len
!=
7590 sizeof(struct sockaddr_in
)) {
7591 PFKEY_STATINC(PFKEY_STAT_OUT_INVADDR
);
7597 if (PFKEY_ADDR_SADDR(src0
)->sa_len
!=
7598 sizeof(struct sockaddr_in6
)) {
7599 PFKEY_STATINC(PFKEY_STAT_OUT_INVADDR
);
7605 * Check validity of the scope zone ID of the
7606 * addresses, and embed the zone ID into the address
7609 sin6
= (struct sockaddr_in6
*)PFKEY_ADDR_SADDR(src0
);
7610 if ((error
= sa6_embedscope(sin6
, 0)) != 0)
7612 sin6
= (struct sockaddr_in6
*)PFKEY_ADDR_SADDR(dst0
);
7613 if ((error
= sa6_embedscope(sin6
, 0)) != 0)
7618 ipseclog((LOG_DEBUG
,
7619 "key_parse: unsupported address family.\n"));
7620 PFKEY_STATINC(PFKEY_STAT_OUT_INVADDR
);
7621 error
= EAFNOSUPPORT
;
7625 switch (PFKEY_ADDR_SADDR(src0
)->sa_family
) {
7627 plen
= sizeof(struct in_addr
) << 3;
7630 plen
= sizeof(struct in6_addr
) << 3;
7633 plen
= 0; /*fool gcc*/
7637 /* check max prefix length */
7638 if (src0
->sadb_address_prefixlen
> plen
||
7639 dst0
->sadb_address_prefixlen
> plen
) {
7640 ipseclog((LOG_DEBUG
,
7641 "key_parse: illegal prefixlen.\n"));
7642 PFKEY_STATINC(PFKEY_STAT_OUT_INVADDR
);
7648 * prefixlen == 0 is valid because there can be a case when
7649 * all addresses are matched.
7653 if (msg
->sadb_msg_type
>= sizeof(key_typesw
)/sizeof(key_typesw
[0]) ||
7654 key_typesw
[msg
->sadb_msg_type
] == NULL
) {
7655 PFKEY_STATINC(PFKEY_STAT_OUT_INVMSGTYPE
);
7660 return (*key_typesw
[msg
->sadb_msg_type
])(so
, m
, &mh
);
7663 msg
->sadb_msg_errno
= error
;
7664 return key_sendup_mbuf(so
, m
, target
);
7668 key_senderror(struct socket
*so
, struct mbuf
*m
, int code
)
7670 struct sadb_msg
*msg
;
7672 if (m
->m_len
< sizeof(struct sadb_msg
))
7673 panic("invalid mbuf passed to key_senderror");
7675 msg
= mtod(m
, struct sadb_msg
*);
7676 msg
->sadb_msg_errno
= code
;
7677 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ONE
);
7681 * set the pointer to each header into message buffer.
7682 * m will be freed on error.
7683 * XXX larger-than-MCLBYTES extension?
7686 key_align(struct mbuf
*m
, struct sadb_msghdr
*mhp
)
7689 struct sadb_ext
*ext
;
7695 if (m
== NULL
|| mhp
== NULL
)
7696 panic("key_align: NULL pointer is passed.");
7697 if (m
->m_len
< sizeof(struct sadb_msg
))
7698 panic("invalid mbuf passed to key_align");
7701 memset(mhp
, 0, sizeof(*mhp
));
7703 mhp
->msg
= mtod(m
, struct sadb_msg
*);
7704 mhp
->ext
[0] = (struct sadb_ext
*)mhp
->msg
; /*XXX backward compat */
7706 end
= PFKEY_UNUNIT64(mhp
->msg
->sadb_msg_len
);
7707 extlen
= end
; /*just in case extlen is not updated*/
7708 for (off
= sizeof(struct sadb_msg
); off
< end
; off
+= extlen
) {
7709 n
= m_pulldown(m
, off
, sizeof(struct sadb_ext
), &toff
);
7711 /* m is already freed */
7714 ext
= (struct sadb_ext
*)(mtod(n
, char *) + toff
);
7717 switch (ext
->sadb_ext_type
) {
7719 case SADB_EXT_ADDRESS_SRC
:
7720 case SADB_EXT_ADDRESS_DST
:
7721 case SADB_EXT_ADDRESS_PROXY
:
7722 case SADB_EXT_LIFETIME_CURRENT
:
7723 case SADB_EXT_LIFETIME_HARD
:
7724 case SADB_EXT_LIFETIME_SOFT
:
7725 case SADB_EXT_KEY_AUTH
:
7726 case SADB_EXT_KEY_ENCRYPT
:
7727 case SADB_EXT_IDENTITY_SRC
:
7728 case SADB_EXT_IDENTITY_DST
:
7729 case SADB_EXT_SENSITIVITY
:
7730 case SADB_EXT_PROPOSAL
:
7731 case SADB_EXT_SUPPORTED_AUTH
:
7732 case SADB_EXT_SUPPORTED_ENCRYPT
:
7733 case SADB_EXT_SPIRANGE
:
7734 case SADB_X_EXT_POLICY
:
7735 case SADB_X_EXT_SA2
:
7737 case SADB_X_EXT_NAT_T_TYPE
:
7738 case SADB_X_EXT_NAT_T_SPORT
:
7739 case SADB_X_EXT_NAT_T_DPORT
:
7740 case SADB_X_EXT_NAT_T_OA
:
7741 case SADB_X_EXT_NAT_T_FRAG
:
7743 #ifdef SADB_X_EXT_TAG
7744 case SADB_X_EXT_TAG
:
7746 /* duplicate check */
7748 * XXX Are there duplication payloads of either
7749 * KEY_AUTH or KEY_ENCRYPT ?
7751 if (mhp
->ext
[ext
->sadb_ext_type
] != NULL
) {
7752 ipseclog((LOG_DEBUG
,
7753 "key_align: duplicate ext_type %u "
7754 "is passed.\n", ext
->sadb_ext_type
));
7756 PFKEY_STATINC(PFKEY_STAT_OUT_DUPEXT
);
7761 ipseclog((LOG_DEBUG
,
7762 "key_align: invalid ext_type %u is passed.\n",
7763 ext
->sadb_ext_type
));
7765 PFKEY_STATINC(PFKEY_STAT_OUT_INVEXTTYPE
);
7769 extlen
= PFKEY_UNUNIT64(ext
->sadb_ext_len
);
7771 if (key_validate_ext(ext
, extlen
)) {
7773 PFKEY_STATINC(PFKEY_STAT_OUT_INVLEN
);
7777 n
= m_pulldown(m
, off
, extlen
, &toff
);
7779 /* m is already freed */
7782 ext
= (struct sadb_ext
*)(mtod(n
, char *) + toff
);
7784 mhp
->ext
[ext
->sadb_ext_type
] = ext
;
7785 mhp
->extoff
[ext
->sadb_ext_type
] = off
;
7786 mhp
->extlen
[ext
->sadb_ext_type
] = extlen
;
7791 PFKEY_STATINC(PFKEY_STAT_OUT_INVLEN
);
7799 key_validate_ext(const struct sadb_ext
*ext
, int len
)
7801 const struct sockaddr
*sa
;
7802 enum { NONE
, ADDR
} checktype
= NONE
;
7804 const int sal
= offsetof(struct sockaddr
, sa_len
) + sizeof(sa
->sa_len
);
7806 if (len
!= PFKEY_UNUNIT64(ext
->sadb_ext_len
))
7809 /* if it does not match minimum/maximum length, bail */
7810 if (ext
->sadb_ext_type
>= sizeof(minsize
) / sizeof(minsize
[0]) ||
7811 ext
->sadb_ext_type
>= sizeof(maxsize
) / sizeof(maxsize
[0]))
7813 if (!minsize
[ext
->sadb_ext_type
] || len
< minsize
[ext
->sadb_ext_type
])
7815 if (maxsize
[ext
->sadb_ext_type
] && len
> maxsize
[ext
->sadb_ext_type
])
7818 /* more checks based on sadb_ext_type XXX need more */
7819 switch (ext
->sadb_ext_type
) {
7820 case SADB_EXT_ADDRESS_SRC
:
7821 case SADB_EXT_ADDRESS_DST
:
7822 case SADB_EXT_ADDRESS_PROXY
:
7823 baselen
= PFKEY_ALIGN8(sizeof(struct sadb_address
));
7826 case SADB_EXT_IDENTITY_SRC
:
7827 case SADB_EXT_IDENTITY_DST
:
7828 if (((const struct sadb_ident
*)ext
)->sadb_ident_type
==
7829 SADB_X_IDENTTYPE_ADDR
) {
7830 baselen
= PFKEY_ALIGN8(sizeof(struct sadb_ident
));
7840 switch (checktype
) {
7844 sa
= (const struct sockaddr
*)((const char *)ext
+ baselen
);
7845 if (len
< baselen
+ sal
)
7847 if (baselen
+ PFKEY_ALIGN8(sa
->sa_len
) != len
)
7860 pfkeystat_percpu
= percpu_alloc(sizeof(uint64_t) * PFKEY_NSTATS
);
7862 memset((void *)&key_cb
, 0, sizeof(key_cb
));
7864 callout_init(&key_timehandler_ch
, 0);
7866 for (i
= 0; i
< IPSEC_DIR_MAX
; i
++)
7867 LIST_INIT(&sptree
[i
]);
7869 LIST_INIT(&sahtree
);
7871 for (i
= 0; i
<= SADB_SATYPE_MAX
; i
++)
7872 LIST_INIT(®tree
[i
]);
7874 for (i
= 0; i
< SPIHASHSIZE
; i
++)
7875 LIST_INIT(&spihash
[i
]);
7877 #ifndef IPSEC_NONBLOCK_ACQUIRE
7878 LIST_INIT(&acqtree
);
7880 LIST_INIT(&spacqtree
);
7882 TAILQ_INIT(&satailq
);
7883 TAILQ_INIT(&sptailq
);
7885 /* system default */
7887 ip4_def_policy
= key_newsp(0);
7888 if (!ip4_def_policy
)
7889 panic("could not initialize IPv4 default security policy");
7890 ip4_def_policy
->state
= IPSEC_SPSTATE_ALIVE
;
7891 ip4_def_policy
->policy
= IPSEC_POLICY_NONE
;
7892 ip4_def_policy
->dir
= IPSEC_DIR_ANY
;
7893 ip4_def_policy
->readonly
= 1;
7894 ip4_def_policy
->persist
= 1;
7897 ip6_def_policy
= key_newsp(0);
7898 if (!ip6_def_policy
)
7899 panic("could not initialize IPv6 default security policy");
7900 ip6_def_policy
->state
= IPSEC_SPSTATE_ALIVE
;
7901 ip6_def_policy
->policy
= IPSEC_POLICY_NONE
;
7902 ip6_def_policy
->dir
= IPSEC_DIR_ANY
;
7903 ip6_def_policy
->readonly
= 1;
7904 ip6_def_policy
->persist
= 1;
7907 callout_reset(&key_timehandler_ch
, hz
, key_timehandler
, (void *)0);
7909 /* initialize key statistics */
7910 keystat
.getspi_count
= 1;
7912 printf("IPsec: Initialized Security Association Processing.\n");
7920 static ONCE_DECL(key_init_once
);
7922 RUN_ONCE(&key_init_once
, key_do_init
);
7926 * XXX: maybe This function is called after INBOUND IPsec processing.
7928 * Special check for tunnel-mode packets.
7929 * We must make some checks for consistency between inner and outer IP header.
7931 * xxx more checks to be provided
7934 key_checktunnelsanity(struct secasvar
*sav
, u_int family
,
7935 void *src
, void *dst
)
7938 if (sav
->sah
== NULL
)
7939 panic("sav->sah == NULL at key_checktunnelsanity");
7941 /* XXX: check inner IP header */
7948 * Get FQDN for the host.
7949 * If the administrator configured hostname (by hostname(1)) without
7950 * domain name, returns nothing.
7957 static char fqdn
[MAXHOSTNAMELEN
+ 1];
7962 /* check if it comes with domain name. */
7964 for (i
= 0; i
< hostnamelen
; i
++) {
7965 if (hostname
[i
] == '.')
7971 /* NOTE: hostname may not be NUL-terminated. */
7972 memset(fqdn
, 0, sizeof(fqdn
));
7973 memcpy(fqdn
, hostname
, hostnamelen
);
7974 fqdn
[hostnamelen
] = '\0';
7979 * get username@FQDN for the host/user.
7982 key_getuserfqdn(void)
7985 static char userfqdn
[MAXHOSTNAMELEN
+ MAXLOGNAME
+ 2];
7986 struct proc
*p
= curproc
;
7989 if (!p
|| !p
->p_pgrp
|| !p
->p_pgrp
->pg_session
)
7991 if (!(host
= key_getfqdn()))
7994 /* NOTE: s_login may not be-NUL terminated. */
7995 memset(userfqdn
, 0, sizeof(userfqdn
));
7996 memcpy(userfqdn
, p
->p_pgrp
->pg_session
->s_login
, MAXLOGNAME
);
7997 userfqdn
[MAXLOGNAME
] = '\0'; /* safeguard */
7998 q
= userfqdn
+ strlen(userfqdn
);
8000 memcpy(q
, host
, strlen(host
));
8008 /* record data transfer on SA, and update timestamps */
8010 key_sa_recordxfer(struct secasvar
*sav
, struct mbuf
*m
)
8013 panic("key_sa_recordxfer called with sav == NULL");
8015 panic("key_sa_recordxfer called with m == NULL");
8020 * XXX Currently, there is a difference of bytes size
8021 * between inbound and outbound processing.
8023 sav
->lft_c
->sadb_lifetime_bytes
+= m
->m_pkthdr
.len
;
8024 /* to check bytes lifetime is done in key_timehandler(). */
8027 * We use the number of packets as the unit of
8028 * sadb_lifetime_allocations. We increment the variable
8029 * whenever {esp,ah}_{in,out}put is called.
8031 sav
->lft_c
->sadb_lifetime_allocations
++;
8032 /* XXX check for expires? */
8035 * NOTE: We record CURRENT sadb_lifetime_usetime by using wall clock,
8036 * in seconds. HARD and SOFT lifetime are measured by the time
8037 * difference (again in seconds) from sadb_lifetime_usetime.
8041 * -----+-----+--------+---> t
8042 * <--------------> HARD
8046 sav
->lft_c
->sadb_lifetime_usetime
= time_second
;
8047 /* XXX check for expires? */
8055 key_sa_routechange(struct sockaddr
*dst
)
8057 struct secashead
*sah
;
8059 const struct sockaddr
*sa
;
8061 LIST_FOREACH(sah
, &sahtree
, chain
) {
8062 ro
= &sah
->sa_route
;
8063 sa
= rtcache_getdst(ro
);
8064 if (sa
!= NULL
&& dst
->sa_len
== sa
->sa_len
&&
8065 memcmp(dst
, sa
, dst
->sa_len
) == 0)
8073 key_sa_chgstate(struct secasvar
*sav
, u_int8_t state
)
8076 panic("key_sa_chgstate called with sav == NULL");
8078 if (sav
->state
== state
)
8081 if (__LIST_CHAINED(sav
))
8082 LIST_REMOVE(sav
, chain
);
8085 LIST_INSERT_HEAD(&sav
->sah
->savtree
[state
], sav
, chain
);
8089 key_sa_stir_iv(struct secasvar
*sav
)
8093 panic("key_sa_stir_iv called with sav == NULL");
8094 key_randomfill(sav
->iv
, sav
->ivlen
);
8098 key_sp_dead(struct secpolicy
*sp
)
8101 /* mark the SP dead */
8102 sp
->state
= IPSEC_SPSTATE_DEAD
;
8106 key_sp_unlink(struct secpolicy
*sp
)
8109 /* remove from SP index */
8110 if (__LIST_CHAINED(sp
)) {
8111 LIST_REMOVE(sp
, chain
);
8117 static struct mbuf
*
8118 key_alloc_mbuf(int l
)
8120 struct mbuf
*m
= NULL
, *n
;
8125 MGET(n
, M_DONTWAIT
, MT_DATA
);
8126 if (n
&& len
> MLEN
)
8127 MCLGET(n
, M_DONTWAIT
);
8135 n
->m_len
= M_TRAILINGSPACE(n
);
8136 /* use the bottom of mbuf, hoping we can prepend afterwards */
8137 if (n
->m_len
> len
) {
8138 t
= (n
->m_len
- len
) & ~(sizeof(long) - 1);
8155 sysctl_net_key_dumpsa(SYSCTLFN_ARGS
)
8169 m
= key_setdump(name
[0], &error
);
8174 *oldlenp
= m
->m_pkthdr
.len
;
8177 if (*oldlenp
< m
->m_pkthdr
.len
) {
8181 *oldlenp
= m
->m_pkthdr
.len
;
8182 ep
= p
+ m
->m_pkthdr
.len
;
8184 for (n
= m
; n
; n
= n
->m_next
) {
8185 len
= (ep
- p
< n
->m_len
) ?
8187 error
= copyout(mtod(n
, const void *), p
, len
);
8201 sysctl_net_key_dumpsp(SYSCTLFN_ARGS
)
8215 m
= key_setspddump(&error
);
8220 *oldlenp
= m
->m_pkthdr
.len
;
8223 if (*oldlenp
< m
->m_pkthdr
.len
) {
8227 *oldlenp
= m
->m_pkthdr
.len
;
8228 ep
= p
+ m
->m_pkthdr
.len
;
8230 for (n
= m
; n
; n
= n
->m_next
) {
8231 len
= (ep
- p
< n
->m_len
) ?
8233 error
= copyout(mtod(n
, const void *), p
, len
);
8247 sysctl_net_key_stats(SYSCTLFN_ARGS
)
8250 return (NETSTAT_SYSCTL(pfkeystat_percpu
, PFKEY_NSTATS
));
8253 SYSCTL_SETUP(sysctl_net_key_setup
, "sysctl net.key subtree setup")
8256 sysctl_createv(clog
, 0, NULL
, NULL
,
8258 CTLTYPE_NODE
, "net", NULL
,
8261 sysctl_createv(clog
, 0, NULL
, NULL
,
8263 CTLTYPE_NODE
, "key", NULL
,
8265 CTL_NET
, PF_KEY
, CTL_EOL
);
8267 sysctl_createv(clog
, 0, NULL
, NULL
,
8268 CTLFLAG_PERMANENT
|CTLFLAG_READWRITE
,
8269 CTLTYPE_INT
, "debug", NULL
,
8270 NULL
, 0, &key_debug_level
, 0,
8271 CTL_NET
, PF_KEY
, KEYCTL_DEBUG_LEVEL
, CTL_EOL
);
8272 sysctl_createv(clog
, 0, NULL
, NULL
,
8273 CTLFLAG_PERMANENT
|CTLFLAG_READWRITE
,
8274 CTLTYPE_INT
, "spi_try", NULL
,
8275 NULL
, 0, &key_spi_trycnt
, 0,
8276 CTL_NET
, PF_KEY
, KEYCTL_SPI_TRY
, CTL_EOL
);
8277 sysctl_createv(clog
, 0, NULL
, NULL
,
8278 CTLFLAG_PERMANENT
|CTLFLAG_READWRITE
,
8279 CTLTYPE_INT
, "spi_min_value", NULL
,
8280 NULL
, 0, &key_spi_minval
, 0,
8281 CTL_NET
, PF_KEY
, KEYCTL_SPI_MIN_VALUE
, CTL_EOL
);
8282 sysctl_createv(clog
, 0, NULL
, NULL
,
8283 CTLFLAG_PERMANENT
|CTLFLAG_READWRITE
,
8284 CTLTYPE_INT
, "spi_max_value", NULL
,
8285 NULL
, 0, &key_spi_maxval
, 0,
8286 CTL_NET
, PF_KEY
, KEYCTL_SPI_MAX_VALUE
, CTL_EOL
);
8287 sysctl_createv(clog
, 0, NULL
, NULL
,
8288 CTLFLAG_PERMANENT
|CTLFLAG_READWRITE
,
8289 CTLTYPE_INT
, "larval_lifetime", NULL
,
8290 NULL
, 0, &key_larval_lifetime
, 0,
8291 CTL_NET
, PF_KEY
, KEYCTL_LARVAL_LIFETIME
, CTL_EOL
);
8292 sysctl_createv(clog
, 0, NULL
, NULL
,
8293 CTLFLAG_PERMANENT
|CTLFLAG_READWRITE
,
8294 CTLTYPE_INT
, "blockacq_count", NULL
,
8295 NULL
, 0, &key_blockacq_count
, 0,
8296 CTL_NET
, PF_KEY
, KEYCTL_BLOCKACQ_COUNT
, CTL_EOL
);
8297 sysctl_createv(clog
, 0, NULL
, NULL
,
8298 CTLFLAG_PERMANENT
|CTLFLAG_READWRITE
,
8299 CTLTYPE_INT
, "blockacq_lifetime", NULL
,
8300 NULL
, 0, &key_blockacq_lifetime
, 0,
8301 CTL_NET
, PF_KEY
, KEYCTL_BLOCKACQ_LIFETIME
, CTL_EOL
);
8302 sysctl_createv(clog
, 0, NULL
, NULL
,
8303 CTLFLAG_PERMANENT
|CTLFLAG_READWRITE
,
8304 CTLTYPE_INT
, "esp_keymin", NULL
,
8305 NULL
, 0, &ipsec_esp_keymin
, 0,
8306 CTL_NET
, PF_KEY
, KEYCTL_ESP_KEYMIN
, CTL_EOL
);
8307 sysctl_createv(clog
, 0, NULL
, NULL
,
8308 CTLFLAG_PERMANENT
|CTLFLAG_READWRITE
,
8309 CTLTYPE_INT
, "esp_auth", NULL
,
8310 NULL
, 0, &ipsec_esp_auth
, 0,
8311 CTL_NET
, PF_KEY
, KEYCTL_ESP_AUTH
, CTL_EOL
);
8312 sysctl_createv(clog
, 0, NULL
, NULL
,
8313 CTLFLAG_PERMANENT
|CTLFLAG_READWRITE
,
8314 CTLTYPE_INT
, "ah_keymin", NULL
,
8315 NULL
, 0, &ipsec_ah_keymin
, 0,
8316 CTL_NET
, PF_KEY
, KEYCTL_AH_KEYMIN
, CTL_EOL
);
8317 sysctl_createv(clog
, 0, NULL
, NULL
,
8319 CTLTYPE_STRUCT
, "dumpsa", NULL
,
8320 sysctl_net_key_dumpsa
, 0, NULL
, 0,
8321 CTL_NET
, PF_KEY
, KEYCTL_DUMPSA
, CTL_EOL
);
8322 sysctl_createv(clog
, 0, NULL
, NULL
,
8324 CTLTYPE_STRUCT
, "dumpsp", NULL
,
8325 sysctl_net_key_dumpsp
, 0, NULL
, 0,
8326 CTL_NET
, PF_KEY
, KEYCTL_DUMPSP
, CTL_EOL
);
8327 sysctl_createv(clog
, 0, NULL
, NULL
,
8329 CTLTYPE_STRUCT
, "stats",
8330 SYSCTL_DESCR("PF_KEY statistics"),
8331 sysctl_net_key_stats
, 0, NULL
, 0,
8332 CTL_NET
, PF_KEY
, CTL_CREATE
, CTL_EOL
);