key.c revision 177553
1105197Ssam/* $FreeBSD: head/sys/netipsec/key.c 177553 2008-03-24 14:55:09Z bz $ */ 2105197Ssam/* $KAME: key.c,v 1.191 2001/06/27 10:46:49 sakane Exp $ */ 3105197Ssam 4139823Simp/*- 5105197Ssam * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 6105197Ssam * All rights reserved. 7105197Ssam * 8105197Ssam * Redistribution and use in source and binary forms, with or without 9105197Ssam * modification, are permitted provided that the following conditions 10105197Ssam * are met: 11105197Ssam * 1. Redistributions of source code must retain the above copyright 12105197Ssam * notice, this list of conditions and the following disclaimer. 13105197Ssam * 2. Redistributions in binary form must reproduce the above copyright 14105197Ssam * notice, this list of conditions and the following disclaimer in the 15105197Ssam * documentation and/or other materials provided with the distribution. 16105197Ssam * 3. Neither the name of the project nor the names of its contributors 17105197Ssam * may be used to endorse or promote products derived from this software 18105197Ssam * without specific prior written permission. 19105197Ssam * 20105197Ssam * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 21105197Ssam * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22105197Ssam * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23105197Ssam * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 24105197Ssam * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25105197Ssam * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26105197Ssam * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27105197Ssam * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28105197Ssam * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29105197Ssam * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30105197Ssam * SUCH DAMAGE. 31105197Ssam */ 32105197Ssam 33105197Ssam/* 34105197Ssam * This code is referd to RFC 2367 35105197Ssam */ 36105197Ssam 37105197Ssam#include "opt_inet.h" 38105197Ssam#include "opt_inet6.h" 39105197Ssam#include "opt_ipsec.h" 40105197Ssam 41105197Ssam#include <sys/types.h> 42105197Ssam#include <sys/param.h> 43105197Ssam#include <sys/systm.h> 44105197Ssam#include <sys/kernel.h> 45119643Ssam#include <sys/lock.h> 46119643Ssam#include <sys/mutex.h> 47105197Ssam#include <sys/mbuf.h> 48105197Ssam#include <sys/domain.h> 49105197Ssam#include <sys/protosw.h> 50105197Ssam#include <sys/malloc.h> 51105197Ssam#include <sys/socket.h> 52105197Ssam#include <sys/socketvar.h> 53105197Ssam#include <sys/sysctl.h> 54105197Ssam#include <sys/errno.h> 55105197Ssam#include <sys/proc.h> 56105197Ssam#include <sys/queue.h> 57158767Spjd#include <sys/refcount.h> 58105197Ssam#include <sys/syslog.h> 59105197Ssam 60105197Ssam#include <net/if.h> 61105197Ssam#include <net/route.h> 62105197Ssam#include <net/raw_cb.h> 63105197Ssam 64105197Ssam#include <netinet/in.h> 65105197Ssam#include <netinet/in_systm.h> 66105197Ssam#include <netinet/ip.h> 67105197Ssam#include <netinet/in_var.h> 68105197Ssam 69105197Ssam#ifdef INET6 70105197Ssam#include <netinet/ip6.h> 71105197Ssam#include <netinet6/in6_var.h> 72105197Ssam#include <netinet6/ip6_var.h> 73105197Ssam#endif /* INET6 */ 74105197Ssam 75105197Ssam#ifdef INET 76105197Ssam#include <netinet/in_pcb.h> 77105197Ssam#endif 78105197Ssam#ifdef INET6 79105197Ssam#include <netinet6/in6_pcb.h> 80105197Ssam#endif /* INET6 */ 81105197Ssam 82105197Ssam#include <net/pfkeyv2.h> 83105197Ssam#include <netipsec/keydb.h> 84105197Ssam#include <netipsec/key.h> 85105197Ssam#include <netipsec/keysock.h> 86105197Ssam#include <netipsec/key_debug.h> 87105197Ssam 88105197Ssam#include <netipsec/ipsec.h> 89105197Ssam#ifdef INET6 90105197Ssam#include <netipsec/ipsec6.h> 91105197Ssam#endif 92105197Ssam 93105197Ssam#include <netipsec/xform.h> 94105197Ssam 95105197Ssam#include <machine/stdarg.h> 96105197Ssam 97105197Ssam/* randomness */ 98105197Ssam#include <sys/random.h> 99105197Ssam 100105197Ssam#define FULLMASK 0xff 101105197Ssam#define _BITS(bytes) ((bytes) << 3) 102105197Ssam 103105197Ssam/* 104105197Ssam * Note on SA reference counting: 105105197Ssam * - SAs that are not in DEAD state will have (total external reference + 1) 106105197Ssam * following value in reference count field. they cannot be freed and are 107105197Ssam * referenced from SA header. 108105197Ssam * - SAs that are in DEAD state will have (total external reference) 109105197Ssam * in reference count field. they are ready to be freed. reference from 110105197Ssam * SA header will be removed in key_delsav(), when the reference count 111105197Ssam * field hits 0 (= no external reference other than from SA header. 112105197Ssam */ 113105197Ssam 114105197Ssamu_int32_t key_debug_level = 0; 115105197Ssamstatic u_int key_spi_trycnt = 1000; 116105197Ssamstatic u_int32_t key_spi_minval = 0x100; 117105197Ssamstatic u_int32_t key_spi_maxval = 0x0fffffff; /* XXX */ 118105197Ssamstatic u_int32_t policy_id = 0; 119105197Ssamstatic u_int key_int_random = 60; /*interval to initialize randseed,1(m)*/ 120105197Ssamstatic u_int key_larval_lifetime = 30; /* interval to expire acquiring, 30(s)*/ 121105197Ssamstatic int key_blockacq_count = 10; /* counter for blocking SADB_ACQUIRE.*/ 122105197Ssamstatic int key_blockacq_lifetime = 20; /* lifetime for blocking SADB_ACQUIRE.*/ 123125876Sguidostatic int key_preferred_oldsa = 1; /* preferred old sa rather than new sa.*/ 124105197Ssam 125105197Ssamstatic u_int32_t acq_seq = 0; 126105197Ssam 127105197Ssamstatic LIST_HEAD(_sptree, secpolicy) sptree[IPSEC_DIR_MAX]; /* SPD */ 128119643Ssamstatic struct mtx sptree_lock; 129120585Ssam#define SPTREE_LOCK_INIT() \ 130120585Ssam mtx_init(&sptree_lock, "sptree", \ 131120585Ssam "fast ipsec security policy database", MTX_DEF) 132120585Ssam#define SPTREE_LOCK_DESTROY() mtx_destroy(&sptree_lock) 133120585Ssam#define SPTREE_LOCK() mtx_lock(&sptree_lock) 134120585Ssam#define SPTREE_UNLOCK() mtx_unlock(&sptree_lock) 135120585Ssam#define SPTREE_LOCK_ASSERT() mtx_assert(&sptree_lock, MA_OWNED) 136120585Ssam 137105197Ssamstatic LIST_HEAD(_sahtree, secashead) sahtree; /* SAD */ 138119643Ssamstatic struct mtx sahtree_lock; 139120585Ssam#define SAHTREE_LOCK_INIT() \ 140120585Ssam mtx_init(&sahtree_lock, "sahtree", \ 141120585Ssam "fast ipsec security association database", MTX_DEF) 142120585Ssam#define SAHTREE_LOCK_DESTROY() mtx_destroy(&sahtree_lock) 143120585Ssam#define SAHTREE_LOCK() mtx_lock(&sahtree_lock) 144120585Ssam#define SAHTREE_UNLOCK() mtx_unlock(&sahtree_lock) 145120585Ssam#define SAHTREE_LOCK_ASSERT() mtx_assert(&sahtree_lock, MA_OWNED) 146120585Ssam 147119643Ssam /* registed list */ 148105197Ssamstatic LIST_HEAD(_regtree, secreg) regtree[SADB_SATYPE_MAX + 1]; 149119643Ssamstatic struct mtx regtree_lock; 150120585Ssam#define REGTREE_LOCK_INIT() \ 151120585Ssam mtx_init(®tree_lock, "regtree", "fast ipsec regtree", MTX_DEF) 152120585Ssam#define REGTREE_LOCK_DESTROY() mtx_destroy(®tree_lock) 153120585Ssam#define REGTREE_LOCK() mtx_lock(®tree_lock) 154120585Ssam#define REGTREE_UNLOCK() mtx_unlock(®tree_lock) 155120585Ssam#define REGTREE_LOCK_ASSERT() mtx_assert(®tree_lock, MA_OWNED) 156120585Ssam 157105197Ssamstatic LIST_HEAD(_acqtree, secacq) acqtree; /* acquiring list */ 158119643Ssamstatic struct mtx acq_lock; 159120585Ssam#define ACQ_LOCK_INIT() \ 160120585Ssam mtx_init(&acq_lock, "acqtree", "fast ipsec acquire list", MTX_DEF) 161120585Ssam#define ACQ_LOCK_DESTROY() mtx_destroy(&acq_lock) 162120585Ssam#define ACQ_LOCK() mtx_lock(&acq_lock) 163120585Ssam#define ACQ_UNLOCK() mtx_unlock(&acq_lock) 164120585Ssam#define ACQ_LOCK_ASSERT() mtx_assert(&acq_lock, MA_OWNED) 165120585Ssam 166105197Ssamstatic LIST_HEAD(_spacqtree, secspacq) spacqtree; /* SP acquiring list */ 167119643Ssamstatic struct mtx spacq_lock; 168120585Ssam#define SPACQ_LOCK_INIT() \ 169120585Ssam mtx_init(&spacq_lock, "spacqtree", \ 170120585Ssam "fast ipsec security policy acquire list", MTX_DEF) 171120585Ssam#define SPACQ_LOCK_DESTROY() mtx_destroy(&spacq_lock) 172120585Ssam#define SPACQ_LOCK() mtx_lock(&spacq_lock) 173120585Ssam#define SPACQ_UNLOCK() mtx_unlock(&spacq_lock) 174120585Ssam#define SPACQ_LOCK_ASSERT() mtx_assert(&spacq_lock, MA_OWNED) 175105197Ssam 176105197Ssam/* search order for SAs */ 177128856Ssamstatic const u_int saorder_state_valid_prefer_old[] = { 178105197Ssam SADB_SASTATE_DYING, SADB_SASTATE_MATURE, 179105197Ssam}; 180128856Ssamstatic const u_int saorder_state_valid_prefer_new[] = { 181128856Ssam SADB_SASTATE_MATURE, SADB_SASTATE_DYING, 182128856Ssam}; 183105197Ssamstatic u_int saorder_state_alive[] = { 184105197Ssam /* except DEAD */ 185105197Ssam SADB_SASTATE_MATURE, SADB_SASTATE_DYING, SADB_SASTATE_LARVAL 186105197Ssam}; 187105197Ssamstatic u_int saorder_state_any[] = { 188105197Ssam SADB_SASTATE_MATURE, SADB_SASTATE_DYING, 189105197Ssam SADB_SASTATE_LARVAL, SADB_SASTATE_DEAD 190105197Ssam}; 191105197Ssam 192105197Ssamstatic const int minsize[] = { 193105197Ssam sizeof(struct sadb_msg), /* SADB_EXT_RESERVED */ 194105197Ssam sizeof(struct sadb_sa), /* SADB_EXT_SA */ 195105197Ssam sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_CURRENT */ 196105197Ssam sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_HARD */ 197105197Ssam sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_SOFT */ 198105197Ssam sizeof(struct sadb_address), /* SADB_EXT_ADDRESS_SRC */ 199105197Ssam sizeof(struct sadb_address), /* SADB_EXT_ADDRESS_DST */ 200105197Ssam sizeof(struct sadb_address), /* SADB_EXT_ADDRESS_PROXY */ 201105197Ssam sizeof(struct sadb_key), /* SADB_EXT_KEY_AUTH */ 202105197Ssam sizeof(struct sadb_key), /* SADB_EXT_KEY_ENCRYPT */ 203105197Ssam sizeof(struct sadb_ident), /* SADB_EXT_IDENTITY_SRC */ 204105197Ssam sizeof(struct sadb_ident), /* SADB_EXT_IDENTITY_DST */ 205105197Ssam sizeof(struct sadb_sens), /* SADB_EXT_SENSITIVITY */ 206105197Ssam sizeof(struct sadb_prop), /* SADB_EXT_PROPOSAL */ 207105197Ssam sizeof(struct sadb_supported), /* SADB_EXT_SUPPORTED_AUTH */ 208105197Ssam sizeof(struct sadb_supported), /* SADB_EXT_SUPPORTED_ENCRYPT */ 209105197Ssam sizeof(struct sadb_spirange), /* SADB_EXT_SPIRANGE */ 210105197Ssam 0, /* SADB_X_EXT_KMPRIVATE */ 211105197Ssam sizeof(struct sadb_x_policy), /* SADB_X_EXT_POLICY */ 212105197Ssam sizeof(struct sadb_x_sa2), /* SADB_X_SA2 */ 213105197Ssam}; 214105197Ssamstatic const int maxsize[] = { 215105197Ssam sizeof(struct sadb_msg), /* SADB_EXT_RESERVED */ 216105197Ssam sizeof(struct sadb_sa), /* SADB_EXT_SA */ 217105197Ssam sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_CURRENT */ 218105197Ssam sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_HARD */ 219105197Ssam sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_SOFT */ 220105197Ssam 0, /* SADB_EXT_ADDRESS_SRC */ 221105197Ssam 0, /* SADB_EXT_ADDRESS_DST */ 222105197Ssam 0, /* SADB_EXT_ADDRESS_PROXY */ 223105197Ssam 0, /* SADB_EXT_KEY_AUTH */ 224105197Ssam 0, /* SADB_EXT_KEY_ENCRYPT */ 225105197Ssam 0, /* SADB_EXT_IDENTITY_SRC */ 226105197Ssam 0, /* SADB_EXT_IDENTITY_DST */ 227105197Ssam 0, /* SADB_EXT_SENSITIVITY */ 228105197Ssam 0, /* SADB_EXT_PROPOSAL */ 229105197Ssam 0, /* SADB_EXT_SUPPORTED_AUTH */ 230105197Ssam 0, /* SADB_EXT_SUPPORTED_ENCRYPT */ 231105197Ssam sizeof(struct sadb_spirange), /* SADB_EXT_SPIRANGE */ 232105197Ssam 0, /* SADB_X_EXT_KMPRIVATE */ 233105197Ssam 0, /* SADB_X_EXT_POLICY */ 234105197Ssam sizeof(struct sadb_x_sa2), /* SADB_X_SA2 */ 235105197Ssam}; 236105197Ssam 237105197Ssamstatic int ipsec_esp_keymin = 256; 238105197Ssamstatic int ipsec_esp_auth = 0; 239105197Ssamstatic int ipsec_ah_keymin = 128; 240105197Ssam 241105197Ssam#ifdef SYSCTL_DECL 242105197SsamSYSCTL_DECL(_net_key); 243105197Ssam#endif 244105197Ssam 245105197SsamSYSCTL_INT(_net_key, KEYCTL_DEBUG_LEVEL, debug, CTLFLAG_RW, \ 246105197Ssam &key_debug_level, 0, ""); 247105197Ssam 248105197Ssam/* max count of trial for the decision of spi value */ 249105197SsamSYSCTL_INT(_net_key, KEYCTL_SPI_TRY, spi_trycnt, CTLFLAG_RW, \ 250105197Ssam &key_spi_trycnt, 0, ""); 251105197Ssam 252105197Ssam/* minimum spi value to allocate automatically. */ 253105197SsamSYSCTL_INT(_net_key, KEYCTL_SPI_MIN_VALUE, spi_minval, CTLFLAG_RW, \ 254105197Ssam &key_spi_minval, 0, ""); 255105197Ssam 256105197Ssam/* maximun spi value to allocate automatically. */ 257105197SsamSYSCTL_INT(_net_key, KEYCTL_SPI_MAX_VALUE, spi_maxval, CTLFLAG_RW, \ 258105197Ssam &key_spi_maxval, 0, ""); 259105197Ssam 260105197Ssam/* interval to initialize randseed */ 261105197SsamSYSCTL_INT(_net_key, KEYCTL_RANDOM_INT, int_random, CTLFLAG_RW, \ 262105197Ssam &key_int_random, 0, ""); 263105197Ssam 264105197Ssam/* lifetime for larval SA */ 265105197SsamSYSCTL_INT(_net_key, KEYCTL_LARVAL_LIFETIME, larval_lifetime, CTLFLAG_RW, \ 266105197Ssam &key_larval_lifetime, 0, ""); 267105197Ssam 268105197Ssam/* counter for blocking to send SADB_ACQUIRE to IKEd */ 269105197SsamSYSCTL_INT(_net_key, KEYCTL_BLOCKACQ_COUNT, blockacq_count, CTLFLAG_RW, \ 270105197Ssam &key_blockacq_count, 0, ""); 271105197Ssam 272105197Ssam/* lifetime for blocking to send SADB_ACQUIRE to IKEd */ 273105197SsamSYSCTL_INT(_net_key, KEYCTL_BLOCKACQ_LIFETIME, blockacq_lifetime, CTLFLAG_RW, \ 274105197Ssam &key_blockacq_lifetime, 0, ""); 275105197Ssam 276105197Ssam/* ESP auth */ 277105197SsamSYSCTL_INT(_net_key, KEYCTL_ESP_AUTH, esp_auth, CTLFLAG_RW, \ 278105197Ssam &ipsec_esp_auth, 0, ""); 279105197Ssam 280105197Ssam/* minimum ESP key length */ 281105197SsamSYSCTL_INT(_net_key, KEYCTL_ESP_KEYMIN, esp_keymin, CTLFLAG_RW, \ 282105197Ssam &ipsec_esp_keymin, 0, ""); 283105197Ssam 284105197Ssam/* minimum AH key length */ 285105197SsamSYSCTL_INT(_net_key, KEYCTL_AH_KEYMIN, ah_keymin, CTLFLAG_RW, \ 286105197Ssam &ipsec_ah_keymin, 0, ""); 287105197Ssam 288105197Ssam/* perfered old SA rather than new SA */ 289125876SguidoSYSCTL_INT(_net_key, KEYCTL_PREFERED_OLDSA, preferred_oldsa, CTLFLAG_RW,\ 290125876Sguido &key_preferred_oldsa, 0, ""); 291105197Ssam 292105197Ssam#define __LIST_CHAINED(elm) \ 293105197Ssam (!((elm)->chain.le_next == NULL && (elm)->chain.le_prev == NULL)) 294105197Ssam#define LIST_INSERT_TAIL(head, elm, type, field) \ 295105197Ssamdo {\ 296105197Ssam struct type *curelm = LIST_FIRST(head); \ 297105197Ssam if (curelm == NULL) {\ 298105197Ssam LIST_INSERT_HEAD(head, elm, field); \ 299105197Ssam } else { \ 300105197Ssam while (LIST_NEXT(curelm, field)) \ 301105197Ssam curelm = LIST_NEXT(curelm, field);\ 302105197Ssam LIST_INSERT_AFTER(curelm, elm, field);\ 303105197Ssam }\ 304105197Ssam} while (0) 305105197Ssam 306105197Ssam#define KEY_CHKSASTATE(head, sav, name) \ 307105197Ssamdo { \ 308105197Ssam if ((head) != (sav)) { \ 309105197Ssam ipseclog((LOG_DEBUG, "%s: state mismatched (TREE=%d SA=%d)\n", \ 310105197Ssam (name), (head), (sav))); \ 311105197Ssam continue; \ 312105197Ssam } \ 313105197Ssam} while (0) 314105197Ssam 315105197Ssam#define KEY_CHKSPDIR(head, sp, name) \ 316105197Ssamdo { \ 317105197Ssam if ((head) != (sp)) { \ 318105197Ssam ipseclog((LOG_DEBUG, "%s: direction mismatched (TREE=%d SP=%d), " \ 319105197Ssam "anyway continue.\n", \ 320105197Ssam (name), (head), (sp))); \ 321105197Ssam } \ 322105197Ssam} while (0) 323105197Ssam 324119643SsamMALLOC_DEFINE(M_IPSEC_SA, "secasvar", "ipsec security association"); 325119643SsamMALLOC_DEFINE(M_IPSEC_SAH, "sahead", "ipsec sa head"); 326119643SsamMALLOC_DEFINE(M_IPSEC_SP, "ipsecpolicy", "ipsec security policy"); 327119643SsamMALLOC_DEFINE(M_IPSEC_SR, "ipsecrequest", "ipsec security request"); 328119643SsamMALLOC_DEFINE(M_IPSEC_MISC, "ipsec-misc", "ipsec miscellaneous"); 329119643SsamMALLOC_DEFINE(M_IPSEC_SAQ, "ipsec-saq", "ipsec sa acquire"); 330119643SsamMALLOC_DEFINE(M_IPSEC_SAR, "ipsec-reg", "ipsec sa acquire"); 331105197Ssam 332105197Ssam/* 333105197Ssam * set parameters into secpolicyindex buffer. 334105197Ssam * Must allocate secpolicyindex buffer passed to this function. 335105197Ssam */ 336105197Ssam#define KEY_SETSECSPIDX(_dir, s, d, ps, pd, ulp, idx) \ 337105197Ssamdo { \ 338105197Ssam bzero((idx), sizeof(struct secpolicyindex)); \ 339105197Ssam (idx)->dir = (_dir); \ 340105197Ssam (idx)->prefs = (ps); \ 341105197Ssam (idx)->prefd = (pd); \ 342105197Ssam (idx)->ul_proto = (ulp); \ 343105197Ssam bcopy((s), &(idx)->src, ((const struct sockaddr *)(s))->sa_len); \ 344105197Ssam bcopy((d), &(idx)->dst, ((const struct sockaddr *)(d))->sa_len); \ 345105197Ssam} while (0) 346105197Ssam 347105197Ssam/* 348105197Ssam * set parameters into secasindex buffer. 349105197Ssam * Must allocate secasindex buffer before calling this function. 350105197Ssam */ 351105197Ssam#define KEY_SETSECASIDX(p, m, r, s, d, idx) \ 352105197Ssamdo { \ 353105197Ssam bzero((idx), sizeof(struct secasindex)); \ 354105197Ssam (idx)->proto = (p); \ 355105197Ssam (idx)->mode = (m); \ 356105197Ssam (idx)->reqid = (r); \ 357105197Ssam bcopy((s), &(idx)->src, ((const struct sockaddr *)(s))->sa_len); \ 358105197Ssam bcopy((d), &(idx)->dst, ((const struct sockaddr *)(d))->sa_len); \ 359105197Ssam} while (0) 360105197Ssam 361105197Ssam/* key statistics */ 362105197Ssamstruct _keystat { 363105197Ssam u_long getspi_count; /* the avarage of count to try to get new SPI */ 364105197Ssam} keystat; 365105197Ssam 366105197Ssamstruct sadb_msghdr { 367105197Ssam struct sadb_msg *msg; 368105197Ssam struct sadb_ext *ext[SADB_EXT_MAX + 1]; 369105197Ssam int extoff[SADB_EXT_MAX + 1]; 370105197Ssam int extlen[SADB_EXT_MAX + 1]; 371105197Ssam}; 372105197Ssam 373105197Ssamstatic struct secasvar *key_allocsa_policy __P((const struct secasindex *)); 374105197Ssamstatic void key_freesp_so __P((struct secpolicy **)); 375105197Ssamstatic struct secasvar *key_do_allocsa_policy __P((struct secashead *, u_int)); 376105197Ssamstatic void key_delsp __P((struct secpolicy *)); 377105197Ssamstatic struct secpolicy *key_getsp __P((struct secpolicyindex *)); 378119643Ssamstatic void _key_delsp(struct secpolicy *sp); 379105197Ssamstatic struct secpolicy *key_getspbyid __P((u_int32_t)); 380105197Ssamstatic u_int32_t key_newreqid __P((void)); 381105197Ssamstatic struct mbuf *key_gather_mbuf __P((struct mbuf *, 382105197Ssam const struct sadb_msghdr *, int, int, ...)); 383105197Ssamstatic int key_spdadd __P((struct socket *, struct mbuf *, 384105197Ssam const struct sadb_msghdr *)); 385105197Ssamstatic u_int32_t key_getnewspid __P((void)); 386105197Ssamstatic int key_spddelete __P((struct socket *, struct mbuf *, 387105197Ssam const struct sadb_msghdr *)); 388105197Ssamstatic int key_spddelete2 __P((struct socket *, struct mbuf *, 389105197Ssam const struct sadb_msghdr *)); 390105197Ssamstatic int key_spdget __P((struct socket *, struct mbuf *, 391105197Ssam const struct sadb_msghdr *)); 392105197Ssamstatic int key_spdflush __P((struct socket *, struct mbuf *, 393105197Ssam const struct sadb_msghdr *)); 394105197Ssamstatic int key_spddump __P((struct socket *, struct mbuf *, 395105197Ssam const struct sadb_msghdr *)); 396105197Ssamstatic struct mbuf *key_setdumpsp __P((struct secpolicy *, 397105197Ssam u_int8_t, u_int32_t, u_int32_t)); 398105197Ssamstatic u_int key_getspreqmsglen __P((struct secpolicy *)); 399105197Ssamstatic int key_spdexpire __P((struct secpolicy *)); 400105197Ssamstatic struct secashead *key_newsah __P((struct secasindex *)); 401105197Ssamstatic void key_delsah __P((struct secashead *)); 402105197Ssamstatic struct secasvar *key_newsav __P((struct mbuf *, 403105197Ssam const struct sadb_msghdr *, struct secashead *, int *, 404105197Ssam const char*, int)); 405105197Ssam#define KEY_NEWSAV(m, sadb, sah, e) \ 406105197Ssam key_newsav(m, sadb, sah, e, __FILE__, __LINE__) 407105197Ssamstatic void key_delsav __P((struct secasvar *)); 408105197Ssamstatic struct secashead *key_getsah __P((struct secasindex *)); 409105197Ssamstatic struct secasvar *key_checkspidup __P((struct secasindex *, u_int32_t)); 410105197Ssamstatic struct secasvar *key_getsavbyspi __P((struct secashead *, u_int32_t)); 411105197Ssamstatic int key_setsaval __P((struct secasvar *, struct mbuf *, 412105197Ssam const struct sadb_msghdr *)); 413105197Ssamstatic int key_mature __P((struct secasvar *)); 414105197Ssamstatic struct mbuf *key_setdumpsa __P((struct secasvar *, u_int8_t, 415105197Ssam u_int8_t, u_int32_t, u_int32_t)); 416105197Ssamstatic struct mbuf *key_setsadbmsg __P((u_int8_t, u_int16_t, u_int8_t, 417105197Ssam u_int32_t, pid_t, u_int16_t)); 418105197Ssamstatic struct mbuf *key_setsadbsa __P((struct secasvar *)); 419105197Ssamstatic struct mbuf *key_setsadbaddr __P((u_int16_t, 420105197Ssam const struct sockaddr *, u_int8_t, u_int16_t)); 421105197Ssamstatic struct mbuf *key_setsadbxsa2 __P((u_int8_t, u_int32_t, u_int32_t)); 422105197Ssamstatic struct mbuf *key_setsadbxpolicy __P((u_int16_t, u_int8_t, 423105197Ssam u_int32_t)); 424157123Sgnnstatic struct seckey *key_dup_keymsg(const struct sadb_key *, u_int, 425157123Sgnn struct malloc_type *); 426157123Sgnnstatic struct seclifetime *key_dup_lifemsg(const struct sadb_lifetime *src, 427157123Sgnn struct malloc_type *type); 428105197Ssam#ifdef INET6 429105197Ssamstatic int key_ismyaddr6 __P((struct sockaddr_in6 *)); 430105197Ssam#endif 431105197Ssam 432105197Ssam/* flags for key_cmpsaidx() */ 433105197Ssam#define CMP_HEAD 1 /* protocol, addresses. */ 434105197Ssam#define CMP_MODE_REQID 2 /* additionally HEAD, reqid, mode. */ 435105197Ssam#define CMP_REQID 3 /* additionally HEAD, reaid. */ 436105197Ssam#define CMP_EXACTLY 4 /* all elements. */ 437105197Ssamstatic int key_cmpsaidx 438105197Ssam __P((const struct secasindex *, const struct secasindex *, int)); 439105197Ssam 440105197Ssamstatic int key_cmpspidx_exactly 441105197Ssam __P((struct secpolicyindex *, struct secpolicyindex *)); 442105197Ssamstatic int key_cmpspidx_withmask 443105197Ssam __P((struct secpolicyindex *, struct secpolicyindex *)); 444105197Ssamstatic int key_sockaddrcmp __P((const struct sockaddr *, const struct sockaddr *, int)); 445105197Ssamstatic int key_bbcmp __P((const void *, const void *, u_int)); 446105197Ssamstatic u_int16_t key_satype2proto __P((u_int8_t)); 447105197Ssamstatic u_int8_t key_proto2satype __P((u_int16_t)); 448105197Ssam 449105197Ssamstatic int key_getspi __P((struct socket *, struct mbuf *, 450105197Ssam const struct sadb_msghdr *)); 451105197Ssamstatic u_int32_t key_do_getnewspi __P((struct sadb_spirange *, 452105197Ssam struct secasindex *)); 453105197Ssamstatic int key_update __P((struct socket *, struct mbuf *, 454105197Ssam const struct sadb_msghdr *)); 455105197Ssam#ifdef IPSEC_DOSEQCHECK 456105197Ssamstatic struct secasvar *key_getsavbyseq __P((struct secashead *, u_int32_t)); 457105197Ssam#endif 458105197Ssamstatic int key_add __P((struct socket *, struct mbuf *, 459105197Ssam const struct sadb_msghdr *)); 460105197Ssamstatic int key_setident __P((struct secashead *, struct mbuf *, 461105197Ssam const struct sadb_msghdr *)); 462105197Ssamstatic struct mbuf *key_getmsgbuf_x1 __P((struct mbuf *, 463105197Ssam const struct sadb_msghdr *)); 464105197Ssamstatic int key_delete __P((struct socket *, struct mbuf *, 465105197Ssam const struct sadb_msghdr *)); 466105197Ssamstatic int key_get __P((struct socket *, struct mbuf *, 467105197Ssam const struct sadb_msghdr *)); 468105197Ssam 469105197Ssamstatic void key_getcomb_setlifetime __P((struct sadb_comb *)); 470105197Ssamstatic struct mbuf *key_getcomb_esp __P((void)); 471105197Ssamstatic struct mbuf *key_getcomb_ah __P((void)); 472105197Ssamstatic struct mbuf *key_getcomb_ipcomp __P((void)); 473105197Ssamstatic struct mbuf *key_getprop __P((const struct secasindex *)); 474105197Ssam 475105197Ssamstatic int key_acquire __P((const struct secasindex *, struct secpolicy *)); 476105197Ssamstatic struct secacq *key_newacq __P((const struct secasindex *)); 477105197Ssamstatic struct secacq *key_getacq __P((const struct secasindex *)); 478105197Ssamstatic struct secacq *key_getacqbyseq __P((u_int32_t)); 479105197Ssamstatic struct secspacq *key_newspacq __P((struct secpolicyindex *)); 480105197Ssamstatic struct secspacq *key_getspacq __P((struct secpolicyindex *)); 481105197Ssamstatic int key_acquire2 __P((struct socket *, struct mbuf *, 482105197Ssam const struct sadb_msghdr *)); 483105197Ssamstatic int key_register __P((struct socket *, struct mbuf *, 484105197Ssam const struct sadb_msghdr *)); 485105197Ssamstatic int key_expire __P((struct secasvar *)); 486105197Ssamstatic int key_flush __P((struct socket *, struct mbuf *, 487105197Ssam const struct sadb_msghdr *)); 488105197Ssamstatic int key_dump __P((struct socket *, struct mbuf *, 489105197Ssam const struct sadb_msghdr *)); 490105197Ssamstatic int key_promisc __P((struct socket *, struct mbuf *, 491105197Ssam const struct sadb_msghdr *)); 492105197Ssamstatic int key_senderror __P((struct socket *, struct mbuf *, int)); 493105197Ssamstatic int key_validate_ext __P((const struct sadb_ext *, int)); 494105197Ssamstatic int key_align __P((struct mbuf *, struct sadb_msghdr *)); 495157123Sgnnstatic struct mbuf *key_setlifetime(struct seclifetime *src, 496157123Sgnn u_int16_t exttype); 497157123Sgnnstatic struct mbuf *key_setkey(struct seckey *src, u_int16_t exttype); 498157123Sgnn 499105197Ssam#if 0 500105197Ssamstatic const char *key_getfqdn __P((void)); 501105197Ssamstatic const char *key_getuserfqdn __P((void)); 502105197Ssam#endif 503105197Ssamstatic void key_sa_chgstate __P((struct secasvar *, u_int8_t)); 504105197Ssamstatic struct mbuf *key_alloc_mbuf __P((int)); 505105197Ssam 506158767Spjdstatic __inline void 507158767Spjdsa_initref(struct secasvar *sav) 508158767Spjd{ 509105197Ssam 510158767Spjd refcount_init(&sav->refcnt, 1); 511158767Spjd} 512158767Spjdstatic __inline void 513158767Spjdsa_addref(struct secasvar *sav) 514158767Spjd{ 515158767Spjd 516158767Spjd refcount_acquire(&sav->refcnt); 517158767Spjd IPSEC_ASSERT(sav->refcnt != 0, ("SA refcnt overflow")); 518158767Spjd} 519158767Spjdstatic __inline int 520158767Spjdsa_delref(struct secasvar *sav) 521158767Spjd{ 522158767Spjd 523158767Spjd IPSEC_ASSERT(sav->refcnt > 0, ("SA refcnt underflow")); 524158767Spjd return (refcount_release(&sav->refcnt)); 525158767Spjd} 526158767Spjd 527105197Ssam#define SP_ADDREF(p) do { \ 528105197Ssam (p)->refcnt++; \ 529120585Ssam IPSEC_ASSERT((p)->refcnt != 0, ("SP refcnt overflow")); \ 530105197Ssam} while (0) 531105197Ssam#define SP_DELREF(p) do { \ 532120585Ssam IPSEC_ASSERT((p)->refcnt > 0, ("SP refcnt underflow")); \ 533105197Ssam (p)->refcnt--; \ 534105197Ssam} while (0) 535135947Ssam 536105197Ssam 537105197Ssam/* 538135947Ssam * Update the refcnt while holding the SPTREE lock. 539135947Ssam */ 540135947Ssamvoid 541135947Ssamkey_addref(struct secpolicy *sp) 542135947Ssam{ 543135947Ssam SPTREE_LOCK(); 544135947Ssam SP_ADDREF(sp); 545135947Ssam SPTREE_UNLOCK(); 546135947Ssam} 547135947Ssam 548135947Ssam/* 549105197Ssam * Return 0 when there are known to be no SP's for the specified 550105197Ssam * direction. Otherwise return 1. This is used by IPsec code 551105197Ssam * to optimize performance. 552105197Ssam */ 553105197Ssamint 554105197Ssamkey_havesp(u_int dir) 555105197Ssam{ 556105197Ssam return (dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND ? 557105197Ssam LIST_FIRST(&sptree[dir]) != NULL : 1); 558105197Ssam} 559105197Ssam 560105197Ssam/* %%% IPsec policy management */ 561105197Ssam/* 562105197Ssam * allocating a SP for OUTBOUND or INBOUND packet. 563105197Ssam * Must call key_freesp() later. 564105197Ssam * OUT: NULL: not found 565105197Ssam * others: found and return the pointer. 566105197Ssam */ 567105197Ssamstruct secpolicy * 568105197Ssamkey_allocsp(struct secpolicyindex *spidx, u_int dir, const char* where, int tag) 569105197Ssam{ 570105197Ssam struct secpolicy *sp; 571105197Ssam 572120585Ssam IPSEC_ASSERT(spidx != NULL, ("null spidx")); 573120585Ssam IPSEC_ASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND, 574120585Ssam ("invalid direction %u", dir)); 575105197Ssam 576105197Ssam KEYDEBUG(KEYDEBUG_IPSEC_STAMP, 577120585Ssam printf("DP %s from %s:%u\n", __func__, where, tag)); 578105197Ssam 579105197Ssam /* get a SP entry */ 580105197Ssam KEYDEBUG(KEYDEBUG_IPSEC_DATA, 581105197Ssam printf("*** objects\n"); 582105197Ssam kdebug_secpolicyindex(spidx)); 583105197Ssam 584120585Ssam SPTREE_LOCK(); 585105197Ssam LIST_FOREACH(sp, &sptree[dir], chain) { 586105197Ssam KEYDEBUG(KEYDEBUG_IPSEC_DATA, 587105197Ssam printf("*** in SPD\n"); 588105197Ssam kdebug_secpolicyindex(&sp->spidx)); 589105197Ssam 590105197Ssam if (sp->state == IPSEC_SPSTATE_DEAD) 591105197Ssam continue; 592105197Ssam if (key_cmpspidx_withmask(&sp->spidx, spidx)) 593105197Ssam goto found; 594105197Ssam } 595105197Ssam sp = NULL; 596105197Ssamfound: 597105197Ssam if (sp) { 598105197Ssam /* sanity check */ 599120585Ssam KEY_CHKSPDIR(sp->spidx.dir, dir, __func__); 600105197Ssam 601105197Ssam /* found a SPD entry */ 602105197Ssam sp->lastused = time_second; 603105197Ssam SP_ADDREF(sp); 604105197Ssam } 605120585Ssam SPTREE_UNLOCK(); 606105197Ssam 607105197Ssam KEYDEBUG(KEYDEBUG_IPSEC_STAMP, 608120585Ssam printf("DP %s return SP:%p (ID=%u) refcnt %u\n", __func__, 609105197Ssam sp, sp ? sp->id : 0, sp ? sp->refcnt : 0)); 610105197Ssam return sp; 611105197Ssam} 612105197Ssam 613105197Ssam/* 614105197Ssam * allocating a SP for OUTBOUND or INBOUND packet. 615105197Ssam * Must call key_freesp() later. 616105197Ssam * OUT: NULL: not found 617105197Ssam * others: found and return the pointer. 618105197Ssam */ 619105197Ssamstruct secpolicy * 620105197Ssamkey_allocsp2(u_int32_t spi, 621105197Ssam union sockaddr_union *dst, 622105197Ssam u_int8_t proto, 623105197Ssam u_int dir, 624105197Ssam const char* where, int tag) 625105197Ssam{ 626105197Ssam struct secpolicy *sp; 627105197Ssam 628120585Ssam IPSEC_ASSERT(dst != NULL, ("null dst")); 629120585Ssam IPSEC_ASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND, 630120585Ssam ("invalid direction %u", dir)); 631105197Ssam 632105197Ssam KEYDEBUG(KEYDEBUG_IPSEC_STAMP, 633120585Ssam printf("DP %s from %s:%u\n", __func__, where, tag)); 634105197Ssam 635105197Ssam /* get a SP entry */ 636105197Ssam KEYDEBUG(KEYDEBUG_IPSEC_DATA, 637105197Ssam printf("*** objects\n"); 638105197Ssam printf("spi %u proto %u dir %u\n", spi, proto, dir); 639105197Ssam kdebug_sockaddr(&dst->sa)); 640105197Ssam 641120585Ssam SPTREE_LOCK(); 642105197Ssam LIST_FOREACH(sp, &sptree[dir], chain) { 643105197Ssam KEYDEBUG(KEYDEBUG_IPSEC_DATA, 644105197Ssam printf("*** in SPD\n"); 645105197Ssam kdebug_secpolicyindex(&sp->spidx)); 646105197Ssam 647105197Ssam if (sp->state == IPSEC_SPSTATE_DEAD) 648105197Ssam continue; 649105197Ssam /* compare simple values, then dst address */ 650105197Ssam if (sp->spidx.ul_proto != proto) 651105197Ssam continue; 652105197Ssam /* NB: spi's must exist and match */ 653105197Ssam if (!sp->req || !sp->req->sav || sp->req->sav->spi != spi) 654105197Ssam continue; 655105197Ssam if (key_sockaddrcmp(&sp->spidx.dst.sa, &dst->sa, 1) == 0) 656105197Ssam goto found; 657105197Ssam } 658105197Ssam sp = NULL; 659105197Ssamfound: 660105197Ssam if (sp) { 661105197Ssam /* sanity check */ 662120585Ssam KEY_CHKSPDIR(sp->spidx.dir, dir, __func__); 663105197Ssam 664105197Ssam /* found a SPD entry */ 665105197Ssam sp->lastused = time_second; 666105197Ssam SP_ADDREF(sp); 667105197Ssam } 668120585Ssam SPTREE_UNLOCK(); 669105197Ssam 670105197Ssam KEYDEBUG(KEYDEBUG_IPSEC_STAMP, 671120585Ssam printf("DP %s return SP:%p (ID=%u) refcnt %u\n", __func__, 672105197Ssam sp, sp ? sp->id : 0, sp ? sp->refcnt : 0)); 673105197Ssam return sp; 674105197Ssam} 675105197Ssam 676105197Ssam/* 677105197Ssam * return a policy that matches this particular inbound packet. 678105197Ssam * XXX slow 679105197Ssam */ 680105197Ssamstruct secpolicy * 681105197Ssamkey_gettunnel(const struct sockaddr *osrc, 682105197Ssam const struct sockaddr *odst, 683105197Ssam const struct sockaddr *isrc, 684105197Ssam const struct sockaddr *idst, 685105197Ssam const char* where, int tag) 686105197Ssam{ 687105197Ssam struct secpolicy *sp; 688105197Ssam const int dir = IPSEC_DIR_INBOUND; 689105197Ssam struct ipsecrequest *r1, *r2, *p; 690105197Ssam struct secpolicyindex spidx; 691105197Ssam 692105197Ssam KEYDEBUG(KEYDEBUG_IPSEC_STAMP, 693120585Ssam printf("DP %s from %s:%u\n", __func__, where, tag)); 694105197Ssam 695105197Ssam if (isrc->sa_family != idst->sa_family) { 696120585Ssam ipseclog((LOG_ERR, "%s: protocol family mismatched %d != %d\n.", 697120585Ssam __func__, isrc->sa_family, idst->sa_family)); 698105197Ssam sp = NULL; 699105197Ssam goto done; 700105197Ssam } 701105197Ssam 702120585Ssam SPTREE_LOCK(); 703105197Ssam LIST_FOREACH(sp, &sptree[dir], chain) { 704105197Ssam if (sp->state == IPSEC_SPSTATE_DEAD) 705105197Ssam continue; 706105197Ssam 707105197Ssam r1 = r2 = NULL; 708105197Ssam for (p = sp->req; p; p = p->next) { 709105197Ssam if (p->saidx.mode != IPSEC_MODE_TUNNEL) 710105197Ssam continue; 711105197Ssam 712105197Ssam r1 = r2; 713105197Ssam r2 = p; 714105197Ssam 715105197Ssam if (!r1) { 716105197Ssam /* here we look at address matches only */ 717105197Ssam spidx = sp->spidx; 718105197Ssam if (isrc->sa_len > sizeof(spidx.src) || 719105197Ssam idst->sa_len > sizeof(spidx.dst)) 720105197Ssam continue; 721105197Ssam bcopy(isrc, &spidx.src, isrc->sa_len); 722105197Ssam bcopy(idst, &spidx.dst, idst->sa_len); 723105197Ssam if (!key_cmpspidx_withmask(&sp->spidx, &spidx)) 724105197Ssam continue; 725105197Ssam } else { 726105197Ssam if (key_sockaddrcmp(&r1->saidx.src.sa, isrc, 0) || 727105197Ssam key_sockaddrcmp(&r1->saidx.dst.sa, idst, 0)) 728105197Ssam continue; 729105197Ssam } 730105197Ssam 731105197Ssam if (key_sockaddrcmp(&r2->saidx.src.sa, osrc, 0) || 732105197Ssam key_sockaddrcmp(&r2->saidx.dst.sa, odst, 0)) 733105197Ssam continue; 734105197Ssam 735105197Ssam goto found; 736105197Ssam } 737105197Ssam } 738105197Ssam sp = NULL; 739105197Ssamfound: 740105197Ssam if (sp) { 741105197Ssam sp->lastused = time_second; 742105197Ssam SP_ADDREF(sp); 743105197Ssam } 744120585Ssam SPTREE_UNLOCK(); 745105197Ssamdone: 746105197Ssam KEYDEBUG(KEYDEBUG_IPSEC_STAMP, 747120585Ssam printf("DP %s return SP:%p (ID=%u) refcnt %u\n", __func__, 748105197Ssam sp, sp ? sp->id : 0, sp ? sp->refcnt : 0)); 749105197Ssam return sp; 750105197Ssam} 751105197Ssam 752105197Ssam/* 753105197Ssam * allocating an SA entry for an *OUTBOUND* packet. 754105197Ssam * checking each request entries in SP, and acquire an SA if need. 755105197Ssam * OUT: 0: there are valid requests. 756105197Ssam * ENOENT: policy may be valid, but SA with REQUIRE is on acquiring. 757105197Ssam */ 758105197Ssamint 759105197Ssamkey_checkrequest(struct ipsecrequest *isr, const struct secasindex *saidx) 760105197Ssam{ 761105197Ssam u_int level; 762105197Ssam int error; 763105197Ssam 764120585Ssam IPSEC_ASSERT(isr != NULL, ("null isr")); 765120585Ssam IPSEC_ASSERT(saidx != NULL, ("null saidx")); 766120585Ssam IPSEC_ASSERT(saidx->mode == IPSEC_MODE_TRANSPORT || 767105197Ssam saidx->mode == IPSEC_MODE_TUNNEL, 768120585Ssam ("unexpected policy %u", saidx->mode)); 769105197Ssam 770105197Ssam /* 771105197Ssam * XXX guard against protocol callbacks from the crypto 772105197Ssam * thread as they reference ipsecrequest.sav which we 773105197Ssam * temporarily null out below. Need to rethink how we 774105197Ssam * handle bundled SA's in the callback thread. 775105197Ssam */ 776120585Ssam IPSECREQUEST_LOCK_ASSERT(isr); 777119643Ssam 778119643Ssam /* get current level */ 779119643Ssam level = ipsec_get_reqlevel(isr); 780105197Ssam#if 0 781105197Ssam /* 782105197Ssam * We do allocate new SA only if the state of SA in the holder is 783105197Ssam * SADB_SASTATE_DEAD. The SA for outbound must be the oldest. 784105197Ssam */ 785105197Ssam if (isr->sav != NULL) { 786105197Ssam if (isr->sav->sah == NULL) 787120585Ssam panic("%s: sah is null.\n", __func__); 788105197Ssam if (isr->sav == (struct secasvar *)LIST_FIRST( 789105197Ssam &isr->sav->sah->savtree[SADB_SASTATE_DEAD])) { 790105197Ssam KEY_FREESAV(&isr->sav); 791105197Ssam isr->sav = NULL; 792105197Ssam } 793105197Ssam } 794105197Ssam#else 795105197Ssam /* 796105197Ssam * we free any SA stashed in the IPsec request because a different 797105197Ssam * SA may be involved each time this request is checked, either 798105197Ssam * because new SAs are being configured, or this request is 799105197Ssam * associated with an unconnected datagram socket, or this request 800105197Ssam * is associated with a system default policy. 801105197Ssam * 802105197Ssam * The operation may have negative impact to performance. We may 803105197Ssam * want to check cached SA carefully, rather than picking new SA 804105197Ssam * every time. 805105197Ssam */ 806105197Ssam if (isr->sav != NULL) { 807105197Ssam KEY_FREESAV(&isr->sav); 808105197Ssam isr->sav = NULL; 809105197Ssam } 810105197Ssam#endif 811105197Ssam 812105197Ssam /* 813105197Ssam * new SA allocation if no SA found. 814105197Ssam * key_allocsa_policy should allocate the oldest SA available. 815105197Ssam * See key_do_allocsa_policy(), and draft-jenkins-ipsec-rekeying-03.txt. 816105197Ssam */ 817105197Ssam if (isr->sav == NULL) 818105197Ssam isr->sav = key_allocsa_policy(saidx); 819105197Ssam 820105197Ssam /* When there is SA. */ 821105197Ssam if (isr->sav != NULL) { 822105197Ssam if (isr->sav->state != SADB_SASTATE_MATURE && 823105197Ssam isr->sav->state != SADB_SASTATE_DYING) 824105197Ssam return EINVAL; 825105197Ssam return 0; 826105197Ssam } 827105197Ssam 828105197Ssam /* there is no SA */ 829105197Ssam error = key_acquire(saidx, isr->sp); 830105197Ssam if (error != 0) { 831105197Ssam /* XXX What should I do ? */ 832120585Ssam ipseclog((LOG_DEBUG, "%s: error %d returned from key_acquire\n", 833120585Ssam __func__, error)); 834105197Ssam return error; 835105197Ssam } 836105197Ssam 837105197Ssam if (level != IPSEC_LEVEL_REQUIRE) { 838105197Ssam /* XXX sigh, the interface to this routine is botched */ 839120585Ssam IPSEC_ASSERT(isr->sav == NULL, ("unexpected SA")); 840105197Ssam return 0; 841105197Ssam } else { 842105197Ssam return ENOENT; 843105197Ssam } 844105197Ssam} 845105197Ssam 846105197Ssam/* 847105197Ssam * allocating a SA for policy entry from SAD. 848105197Ssam * NOTE: searching SAD of aliving state. 849105197Ssam * OUT: NULL: not found. 850105197Ssam * others: found and return the pointer. 851105197Ssam */ 852105197Ssamstatic struct secasvar * 853105197Ssamkey_allocsa_policy(const struct secasindex *saidx) 854105197Ssam{ 855128856Ssam#define N(a) _ARRAYLEN(a) 856105197Ssam struct secashead *sah; 857105197Ssam struct secasvar *sav; 858128856Ssam u_int stateidx, arraysize; 859128856Ssam const u_int *state_valid; 860105197Ssam 861120585Ssam SAHTREE_LOCK(); 862105197Ssam LIST_FOREACH(sah, &sahtree, chain) { 863105197Ssam if (sah->state == SADB_SASTATE_DEAD) 864105197Ssam continue; 865119643Ssam if (key_cmpsaidx(&sah->saidx, saidx, CMP_MODE_REQID)) { 866128856Ssam if (key_preferred_oldsa) { 867128856Ssam state_valid = saorder_state_valid_prefer_old; 868128856Ssam arraysize = N(saorder_state_valid_prefer_old); 869128856Ssam } else { 870128856Ssam state_valid = saorder_state_valid_prefer_new; 871128856Ssam arraysize = N(saorder_state_valid_prefer_new); 872128856Ssam } 873120585Ssam SAHTREE_UNLOCK(); 874105197Ssam goto found; 875119643Ssam } 876105197Ssam } 877120585Ssam SAHTREE_UNLOCK(); 878105197Ssam 879105197Ssam return NULL; 880105197Ssam 881105197Ssam found: 882105197Ssam /* search valid state */ 883128856Ssam for (stateidx = 0; stateidx < arraysize; stateidx++) { 884128856Ssam sav = key_do_allocsa_policy(sah, state_valid[stateidx]); 885105197Ssam if (sav != NULL) 886105197Ssam return sav; 887105197Ssam } 888105197Ssam 889105197Ssam return NULL; 890128856Ssam#undef N 891105197Ssam} 892105197Ssam 893105197Ssam/* 894105197Ssam * searching SAD with direction, protocol, mode and state. 895105197Ssam * called by key_allocsa_policy(). 896105197Ssam * OUT: 897105197Ssam * NULL : not found 898105197Ssam * others : found, pointer to a SA. 899105197Ssam */ 900105197Ssamstatic struct secasvar * 901105197Ssamkey_do_allocsa_policy(struct secashead *sah, u_int state) 902105197Ssam{ 903105197Ssam struct secasvar *sav, *nextsav, *candidate, *d; 904105197Ssam 905105197Ssam /* initilize */ 906105197Ssam candidate = NULL; 907105197Ssam 908120585Ssam SAHTREE_LOCK(); 909105197Ssam for (sav = LIST_FIRST(&sah->savtree[state]); 910105197Ssam sav != NULL; 911105197Ssam sav = nextsav) { 912105197Ssam 913105197Ssam nextsav = LIST_NEXT(sav, chain); 914105197Ssam 915105197Ssam /* sanity check */ 916120585Ssam KEY_CHKSASTATE(sav->state, state, __func__); 917105197Ssam 918105197Ssam /* initialize */ 919105197Ssam if (candidate == NULL) { 920105197Ssam candidate = sav; 921105197Ssam continue; 922105197Ssam } 923105197Ssam 924105197Ssam /* Which SA is the better ? */ 925105197Ssam 926120585Ssam IPSEC_ASSERT(candidate->lft_c != NULL, 927120585Ssam ("null candidate lifetime")); 928120585Ssam IPSEC_ASSERT(sav->lft_c != NULL, ("null sav lifetime")); 929105197Ssam 930105197Ssam /* What the best method is to compare ? */ 931125876Sguido if (key_preferred_oldsa) { 932157123Sgnn if (candidate->lft_c->addtime > 933157123Sgnn sav->lft_c->addtime) { 934105197Ssam candidate = sav; 935105197Ssam } 936105197Ssam continue; 937105197Ssam /*NOTREACHED*/ 938105197Ssam } 939105197Ssam 940125876Sguido /* preferred new sa rather than old sa */ 941157123Sgnn if (candidate->lft_c->addtime < 942157123Sgnn sav->lft_c->addtime) { 943105197Ssam d = candidate; 944105197Ssam candidate = sav; 945105197Ssam } else 946105197Ssam d = sav; 947105197Ssam 948105197Ssam /* 949105197Ssam * prepared to delete the SA when there is more 950105197Ssam * suitable candidate and the lifetime of the SA is not 951105197Ssam * permanent. 952105197Ssam */ 953177553Sbz if (d->lft_h->addtime != 0) { 954105197Ssam struct mbuf *m, *result; 955125508Ssam u_int8_t satype; 956105197Ssam 957105197Ssam key_sa_chgstate(d, SADB_SASTATE_DEAD); 958105197Ssam 959120585Ssam IPSEC_ASSERT(d->refcnt > 0, ("bogus ref count")); 960125508Ssam 961125508Ssam satype = key_proto2satype(d->sah->saidx.proto); 962125508Ssam if (satype == 0) 963125508Ssam goto msgfail; 964125508Ssam 965105197Ssam m = key_setsadbmsg(SADB_DELETE, 0, 966125508Ssam satype, 0, 0, d->refcnt - 1); 967105197Ssam if (!m) 968105197Ssam goto msgfail; 969105197Ssam result = m; 970105197Ssam 971105197Ssam /* set sadb_address for saidx's. */ 972105197Ssam m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC, 973105197Ssam &d->sah->saidx.src.sa, 974105197Ssam d->sah->saidx.src.sa.sa_len << 3, 975105197Ssam IPSEC_ULPROTO_ANY); 976105197Ssam if (!m) 977105197Ssam goto msgfail; 978105197Ssam m_cat(result, m); 979105197Ssam 980105197Ssam /* set sadb_address for saidx's. */ 981105197Ssam m = key_setsadbaddr(SADB_EXT_ADDRESS_DST, 982128860Ssam &d->sah->saidx.dst.sa, 983128860Ssam d->sah->saidx.dst.sa.sa_len << 3, 984105197Ssam IPSEC_ULPROTO_ANY); 985105197Ssam if (!m) 986105197Ssam goto msgfail; 987105197Ssam m_cat(result, m); 988105197Ssam 989105197Ssam /* create SA extension */ 990105197Ssam m = key_setsadbsa(d); 991105197Ssam if (!m) 992105197Ssam goto msgfail; 993105197Ssam m_cat(result, m); 994105197Ssam 995105197Ssam if (result->m_len < sizeof(struct sadb_msg)) { 996105197Ssam result = m_pullup(result, 997105197Ssam sizeof(struct sadb_msg)); 998105197Ssam if (result == NULL) 999105197Ssam goto msgfail; 1000105197Ssam } 1001105197Ssam 1002105197Ssam result->m_pkthdr.len = 0; 1003105197Ssam for (m = result; m; m = m->m_next) 1004105197Ssam result->m_pkthdr.len += m->m_len; 1005105197Ssam mtod(result, struct sadb_msg *)->sadb_msg_len = 1006105197Ssam PFKEY_UNIT64(result->m_pkthdr.len); 1007105197Ssam 1008105197Ssam if (key_sendup_mbuf(NULL, result, 1009105197Ssam KEY_SENDUP_REGISTERED)) 1010105197Ssam goto msgfail; 1011105197Ssam msgfail: 1012105197Ssam KEY_FREESAV(&d); 1013105197Ssam } 1014105197Ssam } 1015105197Ssam if (candidate) { 1016158767Spjd sa_addref(candidate); 1017105197Ssam KEYDEBUG(KEYDEBUG_IPSEC_STAMP, 1018120585Ssam printf("DP %s cause refcnt++:%d SA:%p\n", 1019120585Ssam __func__, candidate->refcnt, candidate)); 1020105197Ssam } 1021120585Ssam SAHTREE_UNLOCK(); 1022119643Ssam 1023105197Ssam return candidate; 1024105197Ssam} 1025105197Ssam 1026105197Ssam/* 1027105197Ssam * allocating a usable SA entry for a *INBOUND* packet. 1028105197Ssam * Must call key_freesav() later. 1029105197Ssam * OUT: positive: pointer to a usable sav (i.e. MATURE or DYING state). 1030105197Ssam * NULL: not found, or error occured. 1031105197Ssam * 1032105197Ssam * In the comparison, no source address is used--for RFC2401 conformance. 1033105197Ssam * To quote, from section 4.1: 1034105197Ssam * A security association is uniquely identified by a triple consisting 1035105197Ssam * of a Security Parameter Index (SPI), an IP Destination Address, and a 1036105197Ssam * security protocol (AH or ESP) identifier. 1037105197Ssam * Note that, however, we do need to keep source address in IPsec SA. 1038105197Ssam * IKE specification and PF_KEY specification do assume that we 1039105197Ssam * keep source address in IPsec SA. We see a tricky situation here. 1040105197Ssam */ 1041105197Ssamstruct secasvar * 1042105197Ssamkey_allocsa( 1043105197Ssam union sockaddr_union *dst, 1044105197Ssam u_int proto, 1045105197Ssam u_int32_t spi, 1046105197Ssam const char* where, int tag) 1047105197Ssam{ 1048105197Ssam struct secashead *sah; 1049105197Ssam struct secasvar *sav; 1050128856Ssam u_int stateidx, arraysize, state; 1051128856Ssam const u_int *saorder_state_valid; 1052105197Ssam 1053120585Ssam IPSEC_ASSERT(dst != NULL, ("null dst address")); 1054105197Ssam 1055105197Ssam KEYDEBUG(KEYDEBUG_IPSEC_STAMP, 1056120585Ssam printf("DP %s from %s:%u\n", __func__, where, tag)); 1057105197Ssam 1058105197Ssam /* 1059105197Ssam * searching SAD. 1060105197Ssam * XXX: to be checked internal IP header somewhere. Also when 1061105197Ssam * IPsec tunnel packet is received. But ESP tunnel mode is 1062105197Ssam * encrypted so we can't check internal IP header. 1063105197Ssam */ 1064120585Ssam SAHTREE_LOCK(); 1065128856Ssam if (key_preferred_oldsa) { 1066128856Ssam saorder_state_valid = saorder_state_valid_prefer_old; 1067128856Ssam arraysize = _ARRAYLEN(saorder_state_valid_prefer_old); 1068128856Ssam } else { 1069128856Ssam saorder_state_valid = saorder_state_valid_prefer_new; 1070128856Ssam arraysize = _ARRAYLEN(saorder_state_valid_prefer_new); 1071128856Ssam } 1072105197Ssam LIST_FOREACH(sah, &sahtree, chain) { 1073105197Ssam /* search valid state */ 1074128856Ssam for (stateidx = 0; stateidx < arraysize; stateidx++) { 1075105197Ssam state = saorder_state_valid[stateidx]; 1076105197Ssam LIST_FOREACH(sav, &sah->savtree[state], chain) { 1077105197Ssam /* sanity check */ 1078120585Ssam KEY_CHKSASTATE(sav->state, state, __func__); 1079105197Ssam /* do not return entries w/ unusable state */ 1080105197Ssam if (sav->state != SADB_SASTATE_MATURE && 1081105197Ssam sav->state != SADB_SASTATE_DYING) 1082105197Ssam continue; 1083105197Ssam if (proto != sav->sah->saidx.proto) 1084105197Ssam continue; 1085105197Ssam if (spi != sav->spi) 1086105197Ssam continue; 1087105197Ssam#if 0 /* don't check src */ 1088105197Ssam /* check src address */ 1089105197Ssam if (key_sockaddrcmp(&src->sa, &sav->sah->saidx.src.sa, 0) != 0) 1090105197Ssam continue; 1091105197Ssam#endif 1092105197Ssam /* check dst address */ 1093105197Ssam if (key_sockaddrcmp(&dst->sa, &sav->sah->saidx.dst.sa, 0) != 0) 1094105197Ssam continue; 1095158767Spjd sa_addref(sav); 1096105197Ssam goto done; 1097105197Ssam } 1098105197Ssam } 1099105197Ssam } 1100105197Ssam sav = NULL; 1101105197Ssamdone: 1102120585Ssam SAHTREE_UNLOCK(); 1103105197Ssam 1104105197Ssam KEYDEBUG(KEYDEBUG_IPSEC_STAMP, 1105120585Ssam printf("DP %s return SA:%p; refcnt %u\n", __func__, 1106105197Ssam sav, sav ? sav->refcnt : 0)); 1107105197Ssam return sav; 1108105197Ssam} 1109105197Ssam 1110105197Ssam/* 1111105197Ssam * Must be called after calling key_allocsp(). 1112105197Ssam * For both the packet without socket and key_freeso(). 1113105197Ssam */ 1114105197Ssamvoid 1115105197Ssam_key_freesp(struct secpolicy **spp, const char* where, int tag) 1116105197Ssam{ 1117105197Ssam struct secpolicy *sp = *spp; 1118105197Ssam 1119120585Ssam IPSEC_ASSERT(sp != NULL, ("null sp")); 1120105197Ssam 1121120585Ssam SPTREE_LOCK(); 1122105197Ssam SP_DELREF(sp); 1123105197Ssam 1124105197Ssam KEYDEBUG(KEYDEBUG_IPSEC_STAMP, 1125120585Ssam printf("DP %s SP:%p (ID=%u) from %s:%u; refcnt now %u\n", 1126120585Ssam __func__, sp, sp->id, where, tag, sp->refcnt)); 1127105197Ssam 1128105197Ssam if (sp->refcnt == 0) { 1129105197Ssam *spp = NULL; 1130105197Ssam key_delsp(sp); 1131105197Ssam } 1132120585Ssam SPTREE_UNLOCK(); 1133105197Ssam} 1134105197Ssam 1135105197Ssam/* 1136105197Ssam * Must be called after calling key_allocsp(). 1137105197Ssam * For the packet with socket. 1138105197Ssam */ 1139105197Ssamvoid 1140105197Ssamkey_freeso(struct socket *so) 1141105197Ssam{ 1142120585Ssam IPSEC_ASSERT(so != NULL, ("null so")); 1143105197Ssam 1144105197Ssam switch (so->so_proto->pr_domain->dom_family) { 1145105197Ssam#ifdef INET 1146105197Ssam case PF_INET: 1147105197Ssam { 1148105197Ssam struct inpcb *pcb = sotoinpcb(so); 1149105197Ssam 1150105197Ssam /* Does it have a PCB ? */ 1151105197Ssam if (pcb == NULL) 1152105197Ssam return; 1153105197Ssam key_freesp_so(&pcb->inp_sp->sp_in); 1154105197Ssam key_freesp_so(&pcb->inp_sp->sp_out); 1155105197Ssam } 1156105197Ssam break; 1157105197Ssam#endif 1158105197Ssam#ifdef INET6 1159105197Ssam case PF_INET6: 1160105197Ssam { 1161105197Ssam#ifdef HAVE_NRL_INPCB 1162105197Ssam struct inpcb *pcb = sotoinpcb(so); 1163105197Ssam 1164105197Ssam /* Does it have a PCB ? */ 1165105197Ssam if (pcb == NULL) 1166105197Ssam return; 1167105197Ssam key_freesp_so(&pcb->inp_sp->sp_in); 1168105197Ssam key_freesp_so(&pcb->inp_sp->sp_out); 1169105197Ssam#else 1170105197Ssam struct in6pcb *pcb = sotoin6pcb(so); 1171105197Ssam 1172105197Ssam /* Does it have a PCB ? */ 1173105197Ssam if (pcb == NULL) 1174105197Ssam return; 1175105197Ssam key_freesp_so(&pcb->in6p_sp->sp_in); 1176105197Ssam key_freesp_so(&pcb->in6p_sp->sp_out); 1177105197Ssam#endif 1178105197Ssam } 1179105197Ssam break; 1180105197Ssam#endif /* INET6 */ 1181105197Ssam default: 1182120585Ssam ipseclog((LOG_DEBUG, "%s: unknown address family=%d.\n", 1183120585Ssam __func__, so->so_proto->pr_domain->dom_family)); 1184105197Ssam return; 1185105197Ssam } 1186105197Ssam} 1187105197Ssam 1188105197Ssamstatic void 1189105197Ssamkey_freesp_so(struct secpolicy **sp) 1190105197Ssam{ 1191120585Ssam IPSEC_ASSERT(sp != NULL && *sp != NULL, ("null sp")); 1192105197Ssam 1193105197Ssam if ((*sp)->policy == IPSEC_POLICY_ENTRUST || 1194105197Ssam (*sp)->policy == IPSEC_POLICY_BYPASS) 1195105197Ssam return; 1196105197Ssam 1197120585Ssam IPSEC_ASSERT((*sp)->policy == IPSEC_POLICY_IPSEC, 1198120585Ssam ("invalid policy %u", (*sp)->policy)); 1199105197Ssam KEY_FREESP(sp); 1200105197Ssam} 1201105197Ssam 1202105197Ssam/* 1203105197Ssam * Must be called after calling key_allocsa(). 1204105197Ssam * This function is called by key_freesp() to free some SA allocated 1205105197Ssam * for a policy. 1206105197Ssam */ 1207105197Ssamvoid 1208105197Ssamkey_freesav(struct secasvar **psav, const char* where, int tag) 1209105197Ssam{ 1210105197Ssam struct secasvar *sav = *psav; 1211105197Ssam 1212120585Ssam IPSEC_ASSERT(sav != NULL, ("null sav")); 1213105197Ssam 1214158767Spjd if (sa_delref(sav)) { 1215158767Spjd KEYDEBUG(KEYDEBUG_IPSEC_STAMP, 1216158767Spjd printf("DP %s SA:%p (SPI %u) from %s:%u; refcnt now %u\n", 1217158767Spjd __func__, sav, ntohl(sav->spi), where, tag, sav->refcnt)); 1218105197Ssam *psav = NULL; 1219105197Ssam key_delsav(sav); 1220158767Spjd } else { 1221158767Spjd KEYDEBUG(KEYDEBUG_IPSEC_STAMP, 1222158767Spjd printf("DP %s SA:%p (SPI %u) from %s:%u; refcnt now %u\n", 1223158767Spjd __func__, sav, ntohl(sav->spi), where, tag, sav->refcnt)); 1224105197Ssam } 1225105197Ssam} 1226105197Ssam 1227105197Ssam/* %%% SPD management */ 1228105197Ssam/* 1229105197Ssam * free security policy entry. 1230105197Ssam */ 1231105197Ssamstatic void 1232105197Ssamkey_delsp(struct secpolicy *sp) 1233105197Ssam{ 1234119643Ssam struct ipsecrequest *isr, *nextisr; 1235105197Ssam 1236120585Ssam IPSEC_ASSERT(sp != NULL, ("null sp")); 1237120585Ssam SPTREE_LOCK_ASSERT(); 1238105197Ssam 1239105197Ssam sp->state = IPSEC_SPSTATE_DEAD; 1240105197Ssam 1241120585Ssam IPSEC_ASSERT(sp->refcnt == 0, 1242120585Ssam ("SP with references deleted (refcnt %u)", sp->refcnt)); 1243105197Ssam 1244105197Ssam /* remove from SP index */ 1245105197Ssam if (__LIST_CHAINED(sp)) 1246105197Ssam LIST_REMOVE(sp, chain); 1247105197Ssam 1248119643Ssam for (isr = sp->req; isr != NULL; isr = nextisr) { 1249105197Ssam if (isr->sav != NULL) { 1250105197Ssam KEY_FREESAV(&isr->sav); 1251105197Ssam isr->sav = NULL; 1252105197Ssam } 1253105197Ssam 1254105197Ssam nextisr = isr->next; 1255119643Ssam ipsec_delisr(isr); 1256105197Ssam } 1257119643Ssam _key_delsp(sp); 1258105197Ssam} 1259105197Ssam 1260105197Ssam/* 1261105197Ssam * search SPD 1262105197Ssam * OUT: NULL : not found 1263105197Ssam * others : found, pointer to a SP. 1264105197Ssam */ 1265105197Ssamstatic struct secpolicy * 1266105197Ssamkey_getsp(struct secpolicyindex *spidx) 1267105197Ssam{ 1268105197Ssam struct secpolicy *sp; 1269105197Ssam 1270120585Ssam IPSEC_ASSERT(spidx != NULL, ("null spidx")); 1271105197Ssam 1272120585Ssam SPTREE_LOCK(); 1273105197Ssam LIST_FOREACH(sp, &sptree[spidx->dir], chain) { 1274105197Ssam if (sp->state == IPSEC_SPSTATE_DEAD) 1275105197Ssam continue; 1276105197Ssam if (key_cmpspidx_exactly(spidx, &sp->spidx)) { 1277105197Ssam SP_ADDREF(sp); 1278119643Ssam break; 1279105197Ssam } 1280105197Ssam } 1281120585Ssam SPTREE_UNLOCK(); 1282105197Ssam 1283119643Ssam return sp; 1284105197Ssam} 1285105197Ssam 1286105197Ssam/* 1287105197Ssam * get SP by index. 1288105197Ssam * OUT: NULL : not found 1289105197Ssam * others : found, pointer to a SP. 1290105197Ssam */ 1291105197Ssamstatic struct secpolicy * 1292105197Ssamkey_getspbyid(u_int32_t id) 1293105197Ssam{ 1294105197Ssam struct secpolicy *sp; 1295105197Ssam 1296120585Ssam SPTREE_LOCK(); 1297105197Ssam LIST_FOREACH(sp, &sptree[IPSEC_DIR_INBOUND], chain) { 1298105197Ssam if (sp->state == IPSEC_SPSTATE_DEAD) 1299105197Ssam continue; 1300105197Ssam if (sp->id == id) { 1301105197Ssam SP_ADDREF(sp); 1302119643Ssam goto done; 1303105197Ssam } 1304105197Ssam } 1305105197Ssam 1306105197Ssam LIST_FOREACH(sp, &sptree[IPSEC_DIR_OUTBOUND], chain) { 1307105197Ssam if (sp->state == IPSEC_SPSTATE_DEAD) 1308105197Ssam continue; 1309105197Ssam if (sp->id == id) { 1310105197Ssam SP_ADDREF(sp); 1311119643Ssam goto done; 1312105197Ssam } 1313105197Ssam } 1314119643Ssamdone: 1315120585Ssam SPTREE_UNLOCK(); 1316105197Ssam 1317119643Ssam return sp; 1318105197Ssam} 1319105197Ssam 1320105197Ssamstruct secpolicy * 1321105197Ssamkey_newsp(const char* where, int tag) 1322105197Ssam{ 1323105197Ssam struct secpolicy *newsp = NULL; 1324105197Ssam 1325105197Ssam newsp = (struct secpolicy *) 1326119643Ssam malloc(sizeof(struct secpolicy), M_IPSEC_SP, M_NOWAIT|M_ZERO); 1327105197Ssam if (newsp) { 1328120585Ssam SECPOLICY_LOCK_INIT(newsp); 1329105197Ssam newsp->refcnt = 1; 1330105197Ssam newsp->req = NULL; 1331105197Ssam } 1332105197Ssam 1333105197Ssam KEYDEBUG(KEYDEBUG_IPSEC_STAMP, 1334120585Ssam printf("DP %s from %s:%u return SP:%p\n", __func__, 1335105197Ssam where, tag, newsp)); 1336105197Ssam return newsp; 1337105197Ssam} 1338105197Ssam 1339119643Ssamstatic void 1340119643Ssam_key_delsp(struct secpolicy *sp) 1341119643Ssam{ 1342120585Ssam SECPOLICY_LOCK_DESTROY(sp); 1343119643Ssam free(sp, M_IPSEC_SP); 1344119643Ssam} 1345119643Ssam 1346105197Ssam/* 1347105197Ssam * create secpolicy structure from sadb_x_policy structure. 1348105197Ssam * NOTE: `state', `secpolicyindex' in secpolicy structure are not set, 1349105197Ssam * so must be set properly later. 1350105197Ssam */ 1351105197Ssamstruct secpolicy * 1352105197Ssamkey_msg2sp(xpl0, len, error) 1353105197Ssam struct sadb_x_policy *xpl0; 1354105197Ssam size_t len; 1355105197Ssam int *error; 1356105197Ssam{ 1357105197Ssam struct secpolicy *newsp; 1358105197Ssam 1359120585Ssam IPSEC_ASSERT(xpl0 != NULL, ("null xpl0")); 1360127972Spjd IPSEC_ASSERT(len >= sizeof(*xpl0), ("policy too short: %zu", len)); 1361120585Ssam 1362105197Ssam if (len != PFKEY_EXTLEN(xpl0)) { 1363120585Ssam ipseclog((LOG_DEBUG, "%s: Invalid msg length.\n", __func__)); 1364105197Ssam *error = EINVAL; 1365105197Ssam return NULL; 1366105197Ssam } 1367105197Ssam 1368105197Ssam if ((newsp = KEY_NEWSP()) == NULL) { 1369105197Ssam *error = ENOBUFS; 1370105197Ssam return NULL; 1371105197Ssam } 1372105197Ssam 1373105197Ssam newsp->spidx.dir = xpl0->sadb_x_policy_dir; 1374105197Ssam newsp->policy = xpl0->sadb_x_policy_type; 1375105197Ssam 1376105197Ssam /* check policy */ 1377105197Ssam switch (xpl0->sadb_x_policy_type) { 1378105197Ssam case IPSEC_POLICY_DISCARD: 1379105197Ssam case IPSEC_POLICY_NONE: 1380105197Ssam case IPSEC_POLICY_ENTRUST: 1381105197Ssam case IPSEC_POLICY_BYPASS: 1382105197Ssam newsp->req = NULL; 1383105197Ssam break; 1384105197Ssam 1385105197Ssam case IPSEC_POLICY_IPSEC: 1386105197Ssam { 1387105197Ssam int tlen; 1388105197Ssam struct sadb_x_ipsecrequest *xisr; 1389105197Ssam struct ipsecrequest **p_isr = &newsp->req; 1390105197Ssam 1391105197Ssam /* validity check */ 1392105197Ssam if (PFKEY_EXTLEN(xpl0) < sizeof(*xpl0)) { 1393120585Ssam ipseclog((LOG_DEBUG, "%s: Invalid msg length.\n", 1394120585Ssam __func__)); 1395105197Ssam KEY_FREESP(&newsp); 1396105197Ssam *error = EINVAL; 1397105197Ssam return NULL; 1398105197Ssam } 1399105197Ssam 1400105197Ssam tlen = PFKEY_EXTLEN(xpl0) - sizeof(*xpl0); 1401105197Ssam xisr = (struct sadb_x_ipsecrequest *)(xpl0 + 1); 1402105197Ssam 1403105197Ssam while (tlen > 0) { 1404105197Ssam /* length check */ 1405105197Ssam if (xisr->sadb_x_ipsecrequest_len < sizeof(*xisr)) { 1406120585Ssam ipseclog((LOG_DEBUG, "%s: invalid ipsecrequest " 1407120585Ssam "length.\n", __func__)); 1408105197Ssam KEY_FREESP(&newsp); 1409105197Ssam *error = EINVAL; 1410105197Ssam return NULL; 1411105197Ssam } 1412105197Ssam 1413105197Ssam /* allocate request buffer */ 1414119643Ssam /* NB: data structure is zero'd */ 1415119643Ssam *p_isr = ipsec_newisr(); 1416105197Ssam if ((*p_isr) == NULL) { 1417105197Ssam ipseclog((LOG_DEBUG, 1418120585Ssam "%s: No more memory.\n", __func__)); 1419105197Ssam KEY_FREESP(&newsp); 1420105197Ssam *error = ENOBUFS; 1421105197Ssam return NULL; 1422105197Ssam } 1423105197Ssam 1424105197Ssam /* set values */ 1425105197Ssam switch (xisr->sadb_x_ipsecrequest_proto) { 1426105197Ssam case IPPROTO_ESP: 1427105197Ssam case IPPROTO_AH: 1428105197Ssam case IPPROTO_IPCOMP: 1429105197Ssam break; 1430105197Ssam default: 1431105197Ssam ipseclog((LOG_DEBUG, 1432120585Ssam "%s: invalid proto type=%u\n", __func__, 1433105197Ssam xisr->sadb_x_ipsecrequest_proto)); 1434105197Ssam KEY_FREESP(&newsp); 1435105197Ssam *error = EPROTONOSUPPORT; 1436105197Ssam return NULL; 1437105197Ssam } 1438105197Ssam (*p_isr)->saidx.proto = xisr->sadb_x_ipsecrequest_proto; 1439105197Ssam 1440105197Ssam switch (xisr->sadb_x_ipsecrequest_mode) { 1441105197Ssam case IPSEC_MODE_TRANSPORT: 1442105197Ssam case IPSEC_MODE_TUNNEL: 1443105197Ssam break; 1444105197Ssam case IPSEC_MODE_ANY: 1445105197Ssam default: 1446105197Ssam ipseclog((LOG_DEBUG, 1447120585Ssam "%s: invalid mode=%u\n", __func__, 1448105197Ssam xisr->sadb_x_ipsecrequest_mode)); 1449105197Ssam KEY_FREESP(&newsp); 1450105197Ssam *error = EINVAL; 1451105197Ssam return NULL; 1452105197Ssam } 1453105197Ssam (*p_isr)->saidx.mode = xisr->sadb_x_ipsecrequest_mode; 1454105197Ssam 1455105197Ssam switch (xisr->sadb_x_ipsecrequest_level) { 1456105197Ssam case IPSEC_LEVEL_DEFAULT: 1457105197Ssam case IPSEC_LEVEL_USE: 1458105197Ssam case IPSEC_LEVEL_REQUIRE: 1459105197Ssam break; 1460105197Ssam case IPSEC_LEVEL_UNIQUE: 1461105197Ssam /* validity check */ 1462105197Ssam /* 1463105197Ssam * If range violation of reqid, kernel will 1464105197Ssam * update it, don't refuse it. 1465105197Ssam */ 1466105197Ssam if (xisr->sadb_x_ipsecrequest_reqid 1467105197Ssam > IPSEC_MANUAL_REQID_MAX) { 1468105197Ssam ipseclog((LOG_DEBUG, 1469120585Ssam "%s: reqid=%d range " 1470105197Ssam "violation, updated by kernel.\n", 1471120585Ssam __func__, 1472105197Ssam xisr->sadb_x_ipsecrequest_reqid)); 1473105197Ssam xisr->sadb_x_ipsecrequest_reqid = 0; 1474105197Ssam } 1475105197Ssam 1476105197Ssam /* allocate new reqid id if reqid is zero. */ 1477105197Ssam if (xisr->sadb_x_ipsecrequest_reqid == 0) { 1478105197Ssam u_int32_t reqid; 1479105197Ssam if ((reqid = key_newreqid()) == 0) { 1480105197Ssam KEY_FREESP(&newsp); 1481105197Ssam *error = ENOBUFS; 1482105197Ssam return NULL; 1483105197Ssam } 1484105197Ssam (*p_isr)->saidx.reqid = reqid; 1485105197Ssam xisr->sadb_x_ipsecrequest_reqid = reqid; 1486105197Ssam } else { 1487105197Ssam /* set it for manual keying. */ 1488105197Ssam (*p_isr)->saidx.reqid = 1489105197Ssam xisr->sadb_x_ipsecrequest_reqid; 1490105197Ssam } 1491105197Ssam break; 1492105197Ssam 1493105197Ssam default: 1494120585Ssam ipseclog((LOG_DEBUG, "%s: invalid level=%u\n", 1495120585Ssam __func__, 1496105197Ssam xisr->sadb_x_ipsecrequest_level)); 1497105197Ssam KEY_FREESP(&newsp); 1498105197Ssam *error = EINVAL; 1499105197Ssam return NULL; 1500105197Ssam } 1501105197Ssam (*p_isr)->level = xisr->sadb_x_ipsecrequest_level; 1502105197Ssam 1503105197Ssam /* set IP addresses if there */ 1504105197Ssam if (xisr->sadb_x_ipsecrequest_len > sizeof(*xisr)) { 1505105197Ssam struct sockaddr *paddr; 1506105197Ssam 1507105197Ssam paddr = (struct sockaddr *)(xisr + 1); 1508105197Ssam 1509105197Ssam /* validity check */ 1510105197Ssam if (paddr->sa_len 1511105197Ssam > sizeof((*p_isr)->saidx.src)) { 1512120585Ssam ipseclog((LOG_DEBUG, "%s: invalid " 1513120585Ssam "request address length.\n", 1514120585Ssam __func__)); 1515105197Ssam KEY_FREESP(&newsp); 1516105197Ssam *error = EINVAL; 1517105197Ssam return NULL; 1518105197Ssam } 1519105197Ssam bcopy(paddr, &(*p_isr)->saidx.src, 1520105197Ssam paddr->sa_len); 1521105197Ssam 1522105197Ssam paddr = (struct sockaddr *)((caddr_t)paddr 1523105197Ssam + paddr->sa_len); 1524105197Ssam 1525105197Ssam /* validity check */ 1526105197Ssam if (paddr->sa_len 1527105197Ssam > sizeof((*p_isr)->saidx.dst)) { 1528120585Ssam ipseclog((LOG_DEBUG, "%s: invalid " 1529120585Ssam "request address length.\n", 1530120585Ssam __func__)); 1531105197Ssam KEY_FREESP(&newsp); 1532105197Ssam *error = EINVAL; 1533105197Ssam return NULL; 1534105197Ssam } 1535105197Ssam bcopy(paddr, &(*p_isr)->saidx.dst, 1536105197Ssam paddr->sa_len); 1537105197Ssam } 1538105197Ssam 1539105197Ssam (*p_isr)->sp = newsp; 1540105197Ssam 1541105197Ssam /* initialization for the next. */ 1542105197Ssam p_isr = &(*p_isr)->next; 1543105197Ssam tlen -= xisr->sadb_x_ipsecrequest_len; 1544105197Ssam 1545105197Ssam /* validity check */ 1546105197Ssam if (tlen < 0) { 1547120585Ssam ipseclog((LOG_DEBUG, "%s: becoming tlen < 0.\n", 1548120585Ssam __func__)); 1549105197Ssam KEY_FREESP(&newsp); 1550105197Ssam *error = EINVAL; 1551105197Ssam return NULL; 1552105197Ssam } 1553105197Ssam 1554105197Ssam xisr = (struct sadb_x_ipsecrequest *)((caddr_t)xisr 1555105197Ssam + xisr->sadb_x_ipsecrequest_len); 1556105197Ssam } 1557105197Ssam } 1558105197Ssam break; 1559105197Ssam default: 1560120585Ssam ipseclog((LOG_DEBUG, "%s: invalid policy type.\n", __func__)); 1561105197Ssam KEY_FREESP(&newsp); 1562105197Ssam *error = EINVAL; 1563105197Ssam return NULL; 1564105197Ssam } 1565105197Ssam 1566105197Ssam *error = 0; 1567105197Ssam return newsp; 1568105197Ssam} 1569105197Ssam 1570105197Ssamstatic u_int32_t 1571105197Ssamkey_newreqid() 1572105197Ssam{ 1573105197Ssam static u_int32_t auto_reqid = IPSEC_MANUAL_REQID_MAX + 1; 1574105197Ssam 1575105197Ssam auto_reqid = (auto_reqid == ~0 1576105197Ssam ? IPSEC_MANUAL_REQID_MAX + 1 : auto_reqid + 1); 1577105197Ssam 1578105197Ssam /* XXX should be unique check */ 1579105197Ssam 1580105197Ssam return auto_reqid; 1581105197Ssam} 1582105197Ssam 1583105197Ssam/* 1584105197Ssam * copy secpolicy struct to sadb_x_policy structure indicated. 1585105197Ssam */ 1586105197Ssamstruct mbuf * 1587105197Ssamkey_sp2msg(sp) 1588105197Ssam struct secpolicy *sp; 1589105197Ssam{ 1590105197Ssam struct sadb_x_policy *xpl; 1591105197Ssam int tlen; 1592105197Ssam caddr_t p; 1593105197Ssam struct mbuf *m; 1594105197Ssam 1595120585Ssam IPSEC_ASSERT(sp != NULL, ("null policy")); 1596105197Ssam 1597105197Ssam tlen = key_getspreqmsglen(sp); 1598105197Ssam 1599105197Ssam m = key_alloc_mbuf(tlen); 1600105197Ssam if (!m || m->m_next) { /*XXX*/ 1601105197Ssam if (m) 1602105197Ssam m_freem(m); 1603105197Ssam return NULL; 1604105197Ssam } 1605105197Ssam 1606105197Ssam m->m_len = tlen; 1607105197Ssam m->m_next = NULL; 1608105197Ssam xpl = mtod(m, struct sadb_x_policy *); 1609105197Ssam bzero(xpl, tlen); 1610105197Ssam 1611105197Ssam xpl->sadb_x_policy_len = PFKEY_UNIT64(tlen); 1612105197Ssam xpl->sadb_x_policy_exttype = SADB_X_EXT_POLICY; 1613105197Ssam xpl->sadb_x_policy_type = sp->policy; 1614105197Ssam xpl->sadb_x_policy_dir = sp->spidx.dir; 1615105197Ssam xpl->sadb_x_policy_id = sp->id; 1616105197Ssam p = (caddr_t)xpl + sizeof(*xpl); 1617105197Ssam 1618105197Ssam /* if is the policy for ipsec ? */ 1619105197Ssam if (sp->policy == IPSEC_POLICY_IPSEC) { 1620105197Ssam struct sadb_x_ipsecrequest *xisr; 1621105197Ssam struct ipsecrequest *isr; 1622105197Ssam 1623105197Ssam for (isr = sp->req; isr != NULL; isr = isr->next) { 1624105197Ssam 1625105197Ssam xisr = (struct sadb_x_ipsecrequest *)p; 1626105197Ssam 1627105197Ssam xisr->sadb_x_ipsecrequest_proto = isr->saidx.proto; 1628105197Ssam xisr->sadb_x_ipsecrequest_mode = isr->saidx.mode; 1629105197Ssam xisr->sadb_x_ipsecrequest_level = isr->level; 1630105197Ssam xisr->sadb_x_ipsecrequest_reqid = isr->saidx.reqid; 1631105197Ssam 1632105197Ssam p += sizeof(*xisr); 1633105197Ssam bcopy(&isr->saidx.src, p, isr->saidx.src.sa.sa_len); 1634105197Ssam p += isr->saidx.src.sa.sa_len; 1635105197Ssam bcopy(&isr->saidx.dst, p, isr->saidx.dst.sa.sa_len); 1636105197Ssam p += isr->saidx.src.sa.sa_len; 1637105197Ssam 1638105197Ssam xisr->sadb_x_ipsecrequest_len = 1639105197Ssam PFKEY_ALIGN8(sizeof(*xisr) 1640105197Ssam + isr->saidx.src.sa.sa_len 1641105197Ssam + isr->saidx.dst.sa.sa_len); 1642105197Ssam } 1643105197Ssam } 1644105197Ssam 1645105197Ssam return m; 1646105197Ssam} 1647105197Ssam 1648105197Ssam/* m will not be freed nor modified */ 1649105197Ssamstatic struct mbuf * 1650105197Ssam#ifdef __STDC__ 1651105197Ssamkey_gather_mbuf(struct mbuf *m, const struct sadb_msghdr *mhp, 1652105197Ssam int ndeep, int nitem, ...) 1653105197Ssam#else 1654105197Ssamkey_gather_mbuf(m, mhp, ndeep, nitem, va_alist) 1655105197Ssam struct mbuf *m; 1656105197Ssam const struct sadb_msghdr *mhp; 1657105197Ssam int ndeep; 1658105197Ssam int nitem; 1659105197Ssam va_dcl 1660105197Ssam#endif 1661105197Ssam{ 1662105197Ssam va_list ap; 1663105197Ssam int idx; 1664105197Ssam int i; 1665105197Ssam struct mbuf *result = NULL, *n; 1666105197Ssam int len; 1667105197Ssam 1668120585Ssam IPSEC_ASSERT(m != NULL, ("null mbuf")); 1669120585Ssam IPSEC_ASSERT(mhp != NULL, ("null msghdr")); 1670105197Ssam 1671105197Ssam va_start(ap, nitem); 1672105197Ssam for (i = 0; i < nitem; i++) { 1673105197Ssam idx = va_arg(ap, int); 1674105197Ssam if (idx < 0 || idx > SADB_EXT_MAX) 1675105197Ssam goto fail; 1676105197Ssam /* don't attempt to pull empty extension */ 1677105197Ssam if (idx == SADB_EXT_RESERVED && mhp->msg == NULL) 1678105197Ssam continue; 1679105197Ssam if (idx != SADB_EXT_RESERVED && 1680105197Ssam (mhp->ext[idx] == NULL || mhp->extlen[idx] == 0)) 1681105197Ssam continue; 1682105197Ssam 1683105197Ssam if (idx == SADB_EXT_RESERVED) { 1684105197Ssam len = PFKEY_ALIGN8(sizeof(struct sadb_msg)); 1685120585Ssam 1686120585Ssam IPSEC_ASSERT(len <= MHLEN, ("header too big %u", len)); 1687120585Ssam 1688111119Simp MGETHDR(n, M_DONTWAIT, MT_DATA); 1689105197Ssam if (!n) 1690105197Ssam goto fail; 1691105197Ssam n->m_len = len; 1692105197Ssam n->m_next = NULL; 1693105197Ssam m_copydata(m, 0, sizeof(struct sadb_msg), 1694105197Ssam mtod(n, caddr_t)); 1695105197Ssam } else if (i < ndeep) { 1696105197Ssam len = mhp->extlen[idx]; 1697105197Ssam n = key_alloc_mbuf(len); 1698105197Ssam if (!n || n->m_next) { /*XXX*/ 1699105197Ssam if (n) 1700105197Ssam m_freem(n); 1701105197Ssam goto fail; 1702105197Ssam } 1703105197Ssam m_copydata(m, mhp->extoff[idx], mhp->extlen[idx], 1704105197Ssam mtod(n, caddr_t)); 1705105197Ssam } else { 1706105197Ssam n = m_copym(m, mhp->extoff[idx], mhp->extlen[idx], 1707111119Simp M_DONTWAIT); 1708105197Ssam } 1709105197Ssam if (n == NULL) 1710105197Ssam goto fail; 1711105197Ssam 1712105197Ssam if (result) 1713105197Ssam m_cat(result, n); 1714105197Ssam else 1715105197Ssam result = n; 1716105197Ssam } 1717105197Ssam va_end(ap); 1718105197Ssam 1719105197Ssam if ((result->m_flags & M_PKTHDR) != 0) { 1720105197Ssam result->m_pkthdr.len = 0; 1721105197Ssam for (n = result; n; n = n->m_next) 1722105197Ssam result->m_pkthdr.len += n->m_len; 1723105197Ssam } 1724105197Ssam 1725105197Ssam return result; 1726105197Ssam 1727105197Ssamfail: 1728105197Ssam m_freem(result); 1729105197Ssam return NULL; 1730105197Ssam} 1731105197Ssam 1732105197Ssam/* 1733105197Ssam * SADB_X_SPDADD, SADB_X_SPDSETIDX or SADB_X_SPDUPDATE processing 1734108533Sschweikh * add an entry to SP database, when received 1735105197Ssam * <base, address(SD), (lifetime(H),) policy> 1736105197Ssam * from the user(?). 1737105197Ssam * Adding to SP database, 1738105197Ssam * and send 1739105197Ssam * <base, address(SD), (lifetime(H),) policy> 1740105197Ssam * to the socket which was send. 1741105197Ssam * 1742105197Ssam * SPDADD set a unique policy entry. 1743105197Ssam * SPDSETIDX like SPDADD without a part of policy requests. 1744105197Ssam * SPDUPDATE replace a unique policy entry. 1745105197Ssam * 1746105197Ssam * m will always be freed. 1747105197Ssam */ 1748105197Ssamstatic int 1749105197Ssamkey_spdadd(so, m, mhp) 1750105197Ssam struct socket *so; 1751105197Ssam struct mbuf *m; 1752105197Ssam const struct sadb_msghdr *mhp; 1753105197Ssam{ 1754105197Ssam struct sadb_address *src0, *dst0; 1755105197Ssam struct sadb_x_policy *xpl0, *xpl; 1756105197Ssam struct sadb_lifetime *lft = NULL; 1757105197Ssam struct secpolicyindex spidx; 1758105197Ssam struct secpolicy *newsp; 1759105197Ssam int error; 1760105197Ssam 1761120585Ssam IPSEC_ASSERT(so != NULL, ("null socket")); 1762120585Ssam IPSEC_ASSERT(m != NULL, ("null mbuf")); 1763120585Ssam IPSEC_ASSERT(mhp != NULL, ("null msghdr")); 1764120585Ssam IPSEC_ASSERT(mhp->msg != NULL, ("null msg")); 1765105197Ssam 1766105197Ssam if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL || 1767105197Ssam mhp->ext[SADB_EXT_ADDRESS_DST] == NULL || 1768105197Ssam mhp->ext[SADB_X_EXT_POLICY] == NULL) { 1769105197Ssam ipseclog((LOG_DEBUG, "key_spdadd: invalid message is passed.\n")); 1770105197Ssam return key_senderror(so, m, EINVAL); 1771105197Ssam } 1772105197Ssam if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) || 1773105197Ssam mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) || 1774105197Ssam mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) { 1775120585Ssam ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n", 1776120585Ssam __func__)); 1777105197Ssam return key_senderror(so, m, EINVAL); 1778105197Ssam } 1779105197Ssam if (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL) { 1780105197Ssam if (mhp->extlen[SADB_EXT_LIFETIME_HARD] 1781105197Ssam < sizeof(struct sadb_lifetime)) { 1782120585Ssam ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n", 1783120585Ssam __func__)); 1784105197Ssam return key_senderror(so, m, EINVAL); 1785105197Ssam } 1786105197Ssam lft = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_HARD]; 1787105197Ssam } 1788105197Ssam 1789105197Ssam src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC]; 1790105197Ssam dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST]; 1791105197Ssam xpl0 = (struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY]; 1792105197Ssam 1793105197Ssam /* make secindex */ 1794105197Ssam /* XXX boundary check against sa_len */ 1795105197Ssam KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir, 1796105197Ssam src0 + 1, 1797105197Ssam dst0 + 1, 1798105197Ssam src0->sadb_address_prefixlen, 1799105197Ssam dst0->sadb_address_prefixlen, 1800105197Ssam src0->sadb_address_proto, 1801105197Ssam &spidx); 1802105197Ssam 1803105197Ssam /* checking the direciton. */ 1804105197Ssam switch (xpl0->sadb_x_policy_dir) { 1805105197Ssam case IPSEC_DIR_INBOUND: 1806105197Ssam case IPSEC_DIR_OUTBOUND: 1807105197Ssam break; 1808105197Ssam default: 1809120585Ssam ipseclog((LOG_DEBUG, "%s: Invalid SP direction.\n", __func__)); 1810105197Ssam mhp->msg->sadb_msg_errno = EINVAL; 1811105197Ssam return 0; 1812105197Ssam } 1813105197Ssam 1814105197Ssam /* check policy */ 1815105197Ssam /* key_spdadd() accepts DISCARD, NONE and IPSEC. */ 1816105197Ssam if (xpl0->sadb_x_policy_type == IPSEC_POLICY_ENTRUST 1817105197Ssam || xpl0->sadb_x_policy_type == IPSEC_POLICY_BYPASS) { 1818120585Ssam ipseclog((LOG_DEBUG, "%s: Invalid policy type.\n", __func__)); 1819105197Ssam return key_senderror(so, m, EINVAL); 1820105197Ssam } 1821105197Ssam 1822105197Ssam /* policy requests are mandatory when action is ipsec. */ 1823105197Ssam if (mhp->msg->sadb_msg_type != SADB_X_SPDSETIDX 1824105197Ssam && xpl0->sadb_x_policy_type == IPSEC_POLICY_IPSEC 1825105197Ssam && mhp->extlen[SADB_X_EXT_POLICY] <= sizeof(*xpl0)) { 1826120585Ssam ipseclog((LOG_DEBUG, "%s: some policy requests part required\n", 1827120585Ssam __func__)); 1828105197Ssam return key_senderror(so, m, EINVAL); 1829105197Ssam } 1830105197Ssam 1831105197Ssam /* 1832105197Ssam * checking there is SP already or not. 1833105197Ssam * SPDUPDATE doesn't depend on whether there is a SP or not. 1834105197Ssam * If the type is either SPDADD or SPDSETIDX AND a SP is found, 1835105197Ssam * then error. 1836105197Ssam */ 1837105197Ssam newsp = key_getsp(&spidx); 1838105197Ssam if (mhp->msg->sadb_msg_type == SADB_X_SPDUPDATE) { 1839105197Ssam if (newsp) { 1840105197Ssam newsp->state = IPSEC_SPSTATE_DEAD; 1841105197Ssam KEY_FREESP(&newsp); 1842105197Ssam } 1843105197Ssam } else { 1844105197Ssam if (newsp != NULL) { 1845105197Ssam KEY_FREESP(&newsp); 1846120585Ssam ipseclog((LOG_DEBUG, "%s: a SP entry exists already.\n", 1847120585Ssam __func__)); 1848105197Ssam return key_senderror(so, m, EEXIST); 1849105197Ssam } 1850105197Ssam } 1851105197Ssam 1852105197Ssam /* allocation new SP entry */ 1853105197Ssam if ((newsp = key_msg2sp(xpl0, PFKEY_EXTLEN(xpl0), &error)) == NULL) { 1854105197Ssam return key_senderror(so, m, error); 1855105197Ssam } 1856105197Ssam 1857105197Ssam if ((newsp->id = key_getnewspid()) == 0) { 1858119643Ssam _key_delsp(newsp); 1859105197Ssam return key_senderror(so, m, ENOBUFS); 1860105197Ssam } 1861105197Ssam 1862105197Ssam /* XXX boundary check against sa_len */ 1863105197Ssam KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir, 1864105197Ssam src0 + 1, 1865105197Ssam dst0 + 1, 1866105197Ssam src0->sadb_address_prefixlen, 1867105197Ssam dst0->sadb_address_prefixlen, 1868105197Ssam src0->sadb_address_proto, 1869105197Ssam &newsp->spidx); 1870105197Ssam 1871105197Ssam /* sanity check on addr pair */ 1872105197Ssam if (((struct sockaddr *)(src0 + 1))->sa_family != 1873105197Ssam ((struct sockaddr *)(dst0+ 1))->sa_family) { 1874119643Ssam _key_delsp(newsp); 1875105197Ssam return key_senderror(so, m, EINVAL); 1876105197Ssam } 1877105197Ssam if (((struct sockaddr *)(src0 + 1))->sa_len != 1878105197Ssam ((struct sockaddr *)(dst0+ 1))->sa_len) { 1879119643Ssam _key_delsp(newsp); 1880105197Ssam return key_senderror(so, m, EINVAL); 1881105197Ssam } 1882105197Ssam#if 1 1883105197Ssam if (newsp->req && newsp->req->saidx.src.sa.sa_family) { 1884105197Ssam struct sockaddr *sa; 1885105197Ssam sa = (struct sockaddr *)(src0 + 1); 1886105197Ssam if (sa->sa_family != newsp->req->saidx.src.sa.sa_family) { 1887119643Ssam _key_delsp(newsp); 1888105197Ssam return key_senderror(so, m, EINVAL); 1889105197Ssam } 1890105197Ssam } 1891105197Ssam if (newsp->req && newsp->req->saidx.dst.sa.sa_family) { 1892105197Ssam struct sockaddr *sa; 1893105197Ssam sa = (struct sockaddr *)(dst0 + 1); 1894105197Ssam if (sa->sa_family != newsp->req->saidx.dst.sa.sa_family) { 1895119643Ssam _key_delsp(newsp); 1896105197Ssam return key_senderror(so, m, EINVAL); 1897105197Ssam } 1898105197Ssam } 1899105197Ssam#endif 1900105197Ssam 1901105197Ssam newsp->created = time_second; 1902105197Ssam newsp->lastused = newsp->created; 1903105197Ssam newsp->lifetime = lft ? lft->sadb_lifetime_addtime : 0; 1904105197Ssam newsp->validtime = lft ? lft->sadb_lifetime_usetime : 0; 1905105197Ssam 1906105197Ssam newsp->refcnt = 1; /* do not reclaim until I say I do */ 1907105197Ssam newsp->state = IPSEC_SPSTATE_ALIVE; 1908105197Ssam LIST_INSERT_TAIL(&sptree[newsp->spidx.dir], newsp, secpolicy, chain); 1909105197Ssam 1910105197Ssam /* delete the entry in spacqtree */ 1911105197Ssam if (mhp->msg->sadb_msg_type == SADB_X_SPDUPDATE) { 1912119643Ssam struct secspacq *spacq = key_getspacq(&spidx); 1913119643Ssam if (spacq != NULL) { 1914105197Ssam /* reset counter in order to deletion by timehandler. */ 1915105197Ssam spacq->created = time_second; 1916105197Ssam spacq->count = 0; 1917120585Ssam SPACQ_UNLOCK(); 1918105197Ssam } 1919105197Ssam } 1920105197Ssam 1921105197Ssam { 1922105197Ssam struct mbuf *n, *mpolicy; 1923105197Ssam struct sadb_msg *newmsg; 1924105197Ssam int off; 1925105197Ssam 1926105197Ssam /* create new sadb_msg to reply. */ 1927105197Ssam if (lft) { 1928105197Ssam n = key_gather_mbuf(m, mhp, 2, 5, SADB_EXT_RESERVED, 1929105197Ssam SADB_X_EXT_POLICY, SADB_EXT_LIFETIME_HARD, 1930105197Ssam SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST); 1931105197Ssam } else { 1932105197Ssam n = key_gather_mbuf(m, mhp, 2, 4, SADB_EXT_RESERVED, 1933105197Ssam SADB_X_EXT_POLICY, 1934105197Ssam SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST); 1935105197Ssam } 1936105197Ssam if (!n) 1937105197Ssam return key_senderror(so, m, ENOBUFS); 1938105197Ssam 1939105197Ssam if (n->m_len < sizeof(*newmsg)) { 1940105197Ssam n = m_pullup(n, sizeof(*newmsg)); 1941105197Ssam if (!n) 1942105197Ssam return key_senderror(so, m, ENOBUFS); 1943105197Ssam } 1944105197Ssam newmsg = mtod(n, struct sadb_msg *); 1945105197Ssam newmsg->sadb_msg_errno = 0; 1946105197Ssam newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len); 1947105197Ssam 1948105197Ssam off = 0; 1949105197Ssam mpolicy = m_pulldown(n, PFKEY_ALIGN8(sizeof(struct sadb_msg)), 1950105197Ssam sizeof(*xpl), &off); 1951105197Ssam if (mpolicy == NULL) { 1952105197Ssam /* n is already freed */ 1953105197Ssam return key_senderror(so, m, ENOBUFS); 1954105197Ssam } 1955105197Ssam xpl = (struct sadb_x_policy *)(mtod(mpolicy, caddr_t) + off); 1956105197Ssam if (xpl->sadb_x_policy_exttype != SADB_X_EXT_POLICY) { 1957105197Ssam m_freem(n); 1958105197Ssam return key_senderror(so, m, EINVAL); 1959105197Ssam } 1960105197Ssam xpl->sadb_x_policy_id = newsp->id; 1961105197Ssam 1962105197Ssam m_freem(m); 1963105197Ssam return key_sendup_mbuf(so, n, KEY_SENDUP_ALL); 1964105197Ssam } 1965105197Ssam} 1966105197Ssam 1967105197Ssam/* 1968105197Ssam * get new policy id. 1969105197Ssam * OUT: 1970105197Ssam * 0: failure. 1971105197Ssam * others: success. 1972105197Ssam */ 1973105197Ssamstatic u_int32_t 1974105197Ssamkey_getnewspid() 1975105197Ssam{ 1976105197Ssam u_int32_t newid = 0; 1977105197Ssam int count = key_spi_trycnt; /* XXX */ 1978105197Ssam struct secpolicy *sp; 1979105197Ssam 1980105197Ssam /* when requesting to allocate spi ranged */ 1981105197Ssam while (count--) { 1982105197Ssam newid = (policy_id = (policy_id == ~0 ? 1 : policy_id + 1)); 1983105197Ssam 1984105197Ssam if ((sp = key_getspbyid(newid)) == NULL) 1985105197Ssam break; 1986105197Ssam 1987105197Ssam KEY_FREESP(&sp); 1988105197Ssam } 1989105197Ssam 1990105197Ssam if (count == 0 || newid == 0) { 1991120585Ssam ipseclog((LOG_DEBUG, "%s: to allocate policy id is failed.\n", 1992120585Ssam __func__)); 1993105197Ssam return 0; 1994105197Ssam } 1995105197Ssam 1996105197Ssam return newid; 1997105197Ssam} 1998105197Ssam 1999105197Ssam/* 2000105197Ssam * SADB_SPDDELETE processing 2001105197Ssam * receive 2002105197Ssam * <base, address(SD), policy(*)> 2003105197Ssam * from the user(?), and set SADB_SASTATE_DEAD, 2004105197Ssam * and send, 2005105197Ssam * <base, address(SD), policy(*)> 2006105197Ssam * to the ikmpd. 2007105197Ssam * policy(*) including direction of policy. 2008105197Ssam * 2009105197Ssam * m will always be freed. 2010105197Ssam */ 2011105197Ssamstatic int 2012105197Ssamkey_spddelete(so, m, mhp) 2013105197Ssam struct socket *so; 2014105197Ssam struct mbuf *m; 2015105197Ssam const struct sadb_msghdr *mhp; 2016105197Ssam{ 2017105197Ssam struct sadb_address *src0, *dst0; 2018105197Ssam struct sadb_x_policy *xpl0; 2019105197Ssam struct secpolicyindex spidx; 2020105197Ssam struct secpolicy *sp; 2021105197Ssam 2022120585Ssam IPSEC_ASSERT(so != NULL, ("null so")); 2023120585Ssam IPSEC_ASSERT(m != NULL, ("null mbuf")); 2024120585Ssam IPSEC_ASSERT(mhp != NULL, ("null msghdr")); 2025120585Ssam IPSEC_ASSERT(mhp->msg != NULL, ("null msg")); 2026105197Ssam 2027105197Ssam if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL || 2028105197Ssam mhp->ext[SADB_EXT_ADDRESS_DST] == NULL || 2029105197Ssam mhp->ext[SADB_X_EXT_POLICY] == NULL) { 2030120585Ssam ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n", 2031120585Ssam __func__)); 2032105197Ssam return key_senderror(so, m, EINVAL); 2033105197Ssam } 2034105197Ssam if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) || 2035105197Ssam mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) || 2036105197Ssam mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) { 2037120585Ssam ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n", 2038120585Ssam __func__)); 2039105197Ssam return key_senderror(so, m, EINVAL); 2040105197Ssam } 2041105197Ssam 2042105197Ssam src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC]; 2043105197Ssam dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST]; 2044105197Ssam xpl0 = (struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY]; 2045105197Ssam 2046105197Ssam /* make secindex */ 2047105197Ssam /* XXX boundary check against sa_len */ 2048105197Ssam KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir, 2049105197Ssam src0 + 1, 2050105197Ssam dst0 + 1, 2051105197Ssam src0->sadb_address_prefixlen, 2052105197Ssam dst0->sadb_address_prefixlen, 2053105197Ssam src0->sadb_address_proto, 2054105197Ssam &spidx); 2055105197Ssam 2056105197Ssam /* checking the direciton. */ 2057105197Ssam switch (xpl0->sadb_x_policy_dir) { 2058105197Ssam case IPSEC_DIR_INBOUND: 2059105197Ssam case IPSEC_DIR_OUTBOUND: 2060105197Ssam break; 2061105197Ssam default: 2062120585Ssam ipseclog((LOG_DEBUG, "%s: Invalid SP direction.\n", __func__)); 2063105197Ssam return key_senderror(so, m, EINVAL); 2064105197Ssam } 2065105197Ssam 2066105197Ssam /* Is there SP in SPD ? */ 2067105197Ssam if ((sp = key_getsp(&spidx)) == NULL) { 2068120585Ssam ipseclog((LOG_DEBUG, "%s: no SP found.\n", __func__)); 2069105197Ssam return key_senderror(so, m, EINVAL); 2070105197Ssam } 2071105197Ssam 2072105197Ssam /* save policy id to buffer to be returned. */ 2073105197Ssam xpl0->sadb_x_policy_id = sp->id; 2074105197Ssam 2075105197Ssam sp->state = IPSEC_SPSTATE_DEAD; 2076105197Ssam KEY_FREESP(&sp); 2077105197Ssam 2078105197Ssam { 2079105197Ssam struct mbuf *n; 2080105197Ssam struct sadb_msg *newmsg; 2081105197Ssam 2082105197Ssam /* create new sadb_msg to reply. */ 2083105197Ssam n = key_gather_mbuf(m, mhp, 1, 4, SADB_EXT_RESERVED, 2084105197Ssam SADB_X_EXT_POLICY, SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST); 2085105197Ssam if (!n) 2086105197Ssam return key_senderror(so, m, ENOBUFS); 2087105197Ssam 2088105197Ssam newmsg = mtod(n, struct sadb_msg *); 2089105197Ssam newmsg->sadb_msg_errno = 0; 2090105197Ssam newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len); 2091105197Ssam 2092105197Ssam m_freem(m); 2093105197Ssam return key_sendup_mbuf(so, n, KEY_SENDUP_ALL); 2094105197Ssam } 2095105197Ssam} 2096105197Ssam 2097105197Ssam/* 2098105197Ssam * SADB_SPDDELETE2 processing 2099105197Ssam * receive 2100105197Ssam * <base, policy(*)> 2101105197Ssam * from the user(?), and set SADB_SASTATE_DEAD, 2102105197Ssam * and send, 2103105197Ssam * <base, policy(*)> 2104105197Ssam * to the ikmpd. 2105105197Ssam * policy(*) including direction of policy. 2106105197Ssam * 2107105197Ssam * m will always be freed. 2108105197Ssam */ 2109105197Ssamstatic int 2110105197Ssamkey_spddelete2(so, m, mhp) 2111105197Ssam struct socket *so; 2112105197Ssam struct mbuf *m; 2113105197Ssam const struct sadb_msghdr *mhp; 2114105197Ssam{ 2115105197Ssam u_int32_t id; 2116105197Ssam struct secpolicy *sp; 2117105197Ssam 2118120585Ssam IPSEC_ASSERT(so != NULL, ("null socket")); 2119120585Ssam IPSEC_ASSERT(m != NULL, ("null mbuf")); 2120120585Ssam IPSEC_ASSERT(mhp != NULL, ("null msghdr")); 2121120585Ssam IPSEC_ASSERT(mhp->msg != NULL, ("null msg")); 2122105197Ssam 2123105197Ssam if (mhp->ext[SADB_X_EXT_POLICY] == NULL || 2124105197Ssam mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) { 2125120585Ssam ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n", __func__)); 2126170803Sbz return key_senderror(so, m, EINVAL); 2127105197Ssam } 2128105197Ssam 2129105197Ssam id = ((struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY])->sadb_x_policy_id; 2130105197Ssam 2131105197Ssam /* Is there SP in SPD ? */ 2132105197Ssam if ((sp = key_getspbyid(id)) == NULL) { 2133120585Ssam ipseclog((LOG_DEBUG, "%s: no SP found id:%u.\n", __func__, id)); 2134170803Sbz return key_senderror(so, m, EINVAL); 2135105197Ssam } 2136105197Ssam 2137105197Ssam sp->state = IPSEC_SPSTATE_DEAD; 2138105197Ssam KEY_FREESP(&sp); 2139105197Ssam 2140105197Ssam { 2141105197Ssam struct mbuf *n, *nn; 2142105197Ssam struct sadb_msg *newmsg; 2143105197Ssam int off, len; 2144105197Ssam 2145105197Ssam /* create new sadb_msg to reply. */ 2146105197Ssam len = PFKEY_ALIGN8(sizeof(struct sadb_msg)); 2147105197Ssam 2148111119Simp MGETHDR(n, M_DONTWAIT, MT_DATA); 2149105197Ssam if (n && len > MHLEN) { 2150111119Simp MCLGET(n, M_DONTWAIT); 2151105197Ssam if ((n->m_flags & M_EXT) == 0) { 2152105197Ssam m_freem(n); 2153105197Ssam n = NULL; 2154105197Ssam } 2155105197Ssam } 2156105197Ssam if (!n) 2157105197Ssam return key_senderror(so, m, ENOBUFS); 2158105197Ssam 2159105197Ssam n->m_len = len; 2160105197Ssam n->m_next = NULL; 2161105197Ssam off = 0; 2162105197Ssam 2163105197Ssam m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off); 2164105197Ssam off += PFKEY_ALIGN8(sizeof(struct sadb_msg)); 2165105197Ssam 2166120585Ssam IPSEC_ASSERT(off == len, ("length inconsistency (off %u len %u)", 2167120585Ssam off, len)); 2168105197Ssam 2169105197Ssam n->m_next = m_copym(m, mhp->extoff[SADB_X_EXT_POLICY], 2170111119Simp mhp->extlen[SADB_X_EXT_POLICY], M_DONTWAIT); 2171105197Ssam if (!n->m_next) { 2172105197Ssam m_freem(n); 2173105197Ssam return key_senderror(so, m, ENOBUFS); 2174105197Ssam } 2175105197Ssam 2176105197Ssam n->m_pkthdr.len = 0; 2177105197Ssam for (nn = n; nn; nn = nn->m_next) 2178105197Ssam n->m_pkthdr.len += nn->m_len; 2179105197Ssam 2180105197Ssam newmsg = mtod(n, struct sadb_msg *); 2181105197Ssam newmsg->sadb_msg_errno = 0; 2182105197Ssam newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len); 2183105197Ssam 2184105197Ssam m_freem(m); 2185105197Ssam return key_sendup_mbuf(so, n, KEY_SENDUP_ALL); 2186105197Ssam } 2187105197Ssam} 2188105197Ssam 2189105197Ssam/* 2190105197Ssam * SADB_X_GET processing 2191105197Ssam * receive 2192105197Ssam * <base, policy(*)> 2193105197Ssam * from the user(?), 2194105197Ssam * and send, 2195105197Ssam * <base, address(SD), policy> 2196105197Ssam * to the ikmpd. 2197105197Ssam * policy(*) including direction of policy. 2198105197Ssam * 2199105197Ssam * m will always be freed. 2200105197Ssam */ 2201105197Ssamstatic int 2202105197Ssamkey_spdget(so, m, mhp) 2203105197Ssam struct socket *so; 2204105197Ssam struct mbuf *m; 2205105197Ssam const struct sadb_msghdr *mhp; 2206105197Ssam{ 2207105197Ssam u_int32_t id; 2208105197Ssam struct secpolicy *sp; 2209105197Ssam struct mbuf *n; 2210105197Ssam 2211120585Ssam IPSEC_ASSERT(so != NULL, ("null socket")); 2212120585Ssam IPSEC_ASSERT(m != NULL, ("null mbuf")); 2213120585Ssam IPSEC_ASSERT(mhp != NULL, ("null msghdr")); 2214120585Ssam IPSEC_ASSERT(mhp->msg != NULL, ("null msg")); 2215105197Ssam 2216105197Ssam if (mhp->ext[SADB_X_EXT_POLICY] == NULL || 2217105197Ssam mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) { 2218120585Ssam ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n", 2219120585Ssam __func__)); 2220105197Ssam return key_senderror(so, m, EINVAL); 2221105197Ssam } 2222105197Ssam 2223105197Ssam id = ((struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY])->sadb_x_policy_id; 2224105197Ssam 2225105197Ssam /* Is there SP in SPD ? */ 2226105197Ssam if ((sp = key_getspbyid(id)) == NULL) { 2227120585Ssam ipseclog((LOG_DEBUG, "%s: no SP found id:%u.\n", __func__, id)); 2228105197Ssam return key_senderror(so, m, ENOENT); 2229105197Ssam } 2230105197Ssam 2231105197Ssam n = key_setdumpsp(sp, SADB_X_SPDGET, 0, mhp->msg->sadb_msg_pid); 2232105197Ssam if (n != NULL) { 2233105197Ssam m_freem(m); 2234105197Ssam return key_sendup_mbuf(so, n, KEY_SENDUP_ONE); 2235105197Ssam } else 2236105197Ssam return key_senderror(so, m, ENOBUFS); 2237105197Ssam} 2238105197Ssam 2239105197Ssam/* 2240105197Ssam * SADB_X_SPDACQUIRE processing. 2241105197Ssam * Acquire policy and SA(s) for a *OUTBOUND* packet. 2242105197Ssam * send 2243105197Ssam * <base, policy(*)> 2244105197Ssam * to KMD, and expect to receive 2245105197Ssam * <base> with SADB_X_SPDACQUIRE if error occured, 2246105197Ssam * or 2247105197Ssam * <base, policy> 2248105197Ssam * with SADB_X_SPDUPDATE from KMD by PF_KEY. 2249105197Ssam * policy(*) is without policy requests. 2250105197Ssam * 2251105197Ssam * 0 : succeed 2252105197Ssam * others: error number 2253105197Ssam */ 2254105197Ssamint 2255105197Ssamkey_spdacquire(sp) 2256105197Ssam struct secpolicy *sp; 2257105197Ssam{ 2258105197Ssam struct mbuf *result = NULL, *m; 2259105197Ssam struct secspacq *newspacq; 2260105197Ssam 2261120585Ssam IPSEC_ASSERT(sp != NULL, ("null secpolicy")); 2262120585Ssam IPSEC_ASSERT(sp->req == NULL, ("policy exists")); 2263120585Ssam IPSEC_ASSERT(sp->policy == IPSEC_POLICY_IPSEC, 2264120585Ssam ("policy not IPSEC %u", sp->policy)); 2265105197Ssam 2266108533Sschweikh /* Get an entry to check whether sent message or not. */ 2267119643Ssam newspacq = key_getspacq(&sp->spidx); 2268119643Ssam if (newspacq != NULL) { 2269105197Ssam if (key_blockacq_count < newspacq->count) { 2270105197Ssam /* reset counter and do send message. */ 2271105197Ssam newspacq->count = 0; 2272105197Ssam } else { 2273105197Ssam /* increment counter and do nothing. */ 2274105197Ssam newspacq->count++; 2275105197Ssam return 0; 2276105197Ssam } 2277120585Ssam SPACQ_UNLOCK(); 2278105197Ssam } else { 2279105197Ssam /* make new entry for blocking to send SADB_ACQUIRE. */ 2280119643Ssam newspacq = key_newspacq(&sp->spidx); 2281119643Ssam if (newspacq == NULL) 2282105197Ssam return ENOBUFS; 2283105197Ssam } 2284105197Ssam 2285105197Ssam /* create new sadb_msg to reply. */ 2286105197Ssam m = key_setsadbmsg(SADB_X_SPDACQUIRE, 0, 0, 0, 0, 0); 2287170805Sbz if (!m) 2288170805Sbz return ENOBUFS; 2289170805Sbz 2290105197Ssam result = m; 2291105197Ssam 2292105197Ssam result->m_pkthdr.len = 0; 2293105197Ssam for (m = result; m; m = m->m_next) 2294105197Ssam result->m_pkthdr.len += m->m_len; 2295105197Ssam 2296105197Ssam mtod(result, struct sadb_msg *)->sadb_msg_len = 2297105197Ssam PFKEY_UNIT64(result->m_pkthdr.len); 2298105197Ssam 2299105197Ssam return key_sendup_mbuf(NULL, m, KEY_SENDUP_REGISTERED); 2300105197Ssam} 2301105197Ssam 2302105197Ssam/* 2303105197Ssam * SADB_SPDFLUSH processing 2304105197Ssam * receive 2305105197Ssam * <base> 2306105197Ssam * from the user, and free all entries in secpctree. 2307105197Ssam * and send, 2308105197Ssam * <base> 2309105197Ssam * to the user. 2310105197Ssam * NOTE: what to do is only marking SADB_SASTATE_DEAD. 2311105197Ssam * 2312105197Ssam * m will always be freed. 2313105197Ssam */ 2314105197Ssamstatic int 2315105197Ssamkey_spdflush(so, m, mhp) 2316105197Ssam struct socket *so; 2317105197Ssam struct mbuf *m; 2318105197Ssam const struct sadb_msghdr *mhp; 2319105197Ssam{ 2320105197Ssam struct sadb_msg *newmsg; 2321105197Ssam struct secpolicy *sp; 2322105197Ssam u_int dir; 2323105197Ssam 2324120585Ssam IPSEC_ASSERT(so != NULL, ("null socket")); 2325120585Ssam IPSEC_ASSERT(m != NULL, ("null mbuf")); 2326120585Ssam IPSEC_ASSERT(mhp != NULL, ("null msghdr")); 2327120585Ssam IPSEC_ASSERT(mhp->msg != NULL, ("null msg")); 2328105197Ssam 2329105197Ssam if (m->m_len != PFKEY_ALIGN8(sizeof(struct sadb_msg))) 2330105197Ssam return key_senderror(so, m, EINVAL); 2331105197Ssam 2332105197Ssam for (dir = 0; dir < IPSEC_DIR_MAX; dir++) { 2333120585Ssam SPTREE_LOCK(); 2334120585Ssam LIST_FOREACH(sp, &sptree[dir], chain) 2335105197Ssam sp->state = IPSEC_SPSTATE_DEAD; 2336120585Ssam SPTREE_UNLOCK(); 2337105197Ssam } 2338105197Ssam 2339105197Ssam if (sizeof(struct sadb_msg) > m->m_len + M_TRAILINGSPACE(m)) { 2340120585Ssam ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__)); 2341105197Ssam return key_senderror(so, m, ENOBUFS); 2342105197Ssam } 2343105197Ssam 2344105197Ssam if (m->m_next) 2345105197Ssam m_freem(m->m_next); 2346105197Ssam m->m_next = NULL; 2347105197Ssam m->m_pkthdr.len = m->m_len = PFKEY_ALIGN8(sizeof(struct sadb_msg)); 2348105197Ssam newmsg = mtod(m, struct sadb_msg *); 2349105197Ssam newmsg->sadb_msg_errno = 0; 2350105197Ssam newmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len); 2351105197Ssam 2352105197Ssam return key_sendup_mbuf(so, m, KEY_SENDUP_ALL); 2353105197Ssam} 2354105197Ssam 2355105197Ssam/* 2356105197Ssam * SADB_SPDDUMP processing 2357105197Ssam * receive 2358105197Ssam * <base> 2359105197Ssam * from the user, and dump all SP leaves 2360105197Ssam * and send, 2361105197Ssam * <base> ..... 2362105197Ssam * to the ikmpd. 2363105197Ssam * 2364105197Ssam * m will always be freed. 2365105197Ssam */ 2366105197Ssamstatic int 2367105197Ssamkey_spddump(so, m, mhp) 2368105197Ssam struct socket *so; 2369105197Ssam struct mbuf *m; 2370105197Ssam const struct sadb_msghdr *mhp; 2371105197Ssam{ 2372105197Ssam struct secpolicy *sp; 2373105197Ssam int cnt; 2374105197Ssam u_int dir; 2375105197Ssam struct mbuf *n; 2376105197Ssam 2377120585Ssam IPSEC_ASSERT(so != NULL, ("null socket")); 2378120585Ssam IPSEC_ASSERT(m != NULL, ("null mbuf")); 2379120585Ssam IPSEC_ASSERT(mhp != NULL, ("null msghdr")); 2380120585Ssam IPSEC_ASSERT(mhp->msg != NULL, ("null msg")); 2381105197Ssam 2382105197Ssam /* search SPD entry and get buffer size. */ 2383105197Ssam cnt = 0; 2384105197Ssam for (dir = 0; dir < IPSEC_DIR_MAX; dir++) { 2385105197Ssam LIST_FOREACH(sp, &sptree[dir], chain) { 2386105197Ssam cnt++; 2387105197Ssam } 2388105197Ssam } 2389105197Ssam 2390105197Ssam if (cnt == 0) 2391105197Ssam return key_senderror(so, m, ENOENT); 2392105197Ssam 2393105197Ssam for (dir = 0; dir < IPSEC_DIR_MAX; dir++) { 2394105197Ssam LIST_FOREACH(sp, &sptree[dir], chain) { 2395105197Ssam --cnt; 2396105197Ssam n = key_setdumpsp(sp, SADB_X_SPDDUMP, cnt, 2397105197Ssam mhp->msg->sadb_msg_pid); 2398105197Ssam 2399105197Ssam if (n) 2400105197Ssam key_sendup_mbuf(so, n, KEY_SENDUP_ONE); 2401105197Ssam } 2402105197Ssam } 2403105197Ssam 2404105197Ssam m_freem(m); 2405105197Ssam return 0; 2406105197Ssam} 2407105197Ssam 2408105197Ssamstatic struct mbuf * 2409105197Ssamkey_setdumpsp(sp, type, seq, pid) 2410105197Ssam struct secpolicy *sp; 2411105197Ssam u_int8_t type; 2412105197Ssam u_int32_t seq, pid; 2413105197Ssam{ 2414105197Ssam struct mbuf *result = NULL, *m; 2415105197Ssam 2416105197Ssam m = key_setsadbmsg(type, 0, SADB_SATYPE_UNSPEC, seq, pid, sp->refcnt); 2417105197Ssam if (!m) 2418105197Ssam goto fail; 2419105197Ssam result = m; 2420105197Ssam 2421105197Ssam m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC, 2422105197Ssam &sp->spidx.src.sa, sp->spidx.prefs, 2423105197Ssam sp->spidx.ul_proto); 2424105197Ssam if (!m) 2425105197Ssam goto fail; 2426105197Ssam m_cat(result, m); 2427105197Ssam 2428105197Ssam m = key_setsadbaddr(SADB_EXT_ADDRESS_DST, 2429105197Ssam &sp->spidx.dst.sa, sp->spidx.prefd, 2430105197Ssam sp->spidx.ul_proto); 2431105197Ssam if (!m) 2432105197Ssam goto fail; 2433105197Ssam m_cat(result, m); 2434105197Ssam 2435105197Ssam m = key_sp2msg(sp); 2436105197Ssam if (!m) 2437105197Ssam goto fail; 2438105197Ssam m_cat(result, m); 2439105197Ssam 2440105197Ssam if ((result->m_flags & M_PKTHDR) == 0) 2441105197Ssam goto fail; 2442105197Ssam 2443105197Ssam if (result->m_len < sizeof(struct sadb_msg)) { 2444105197Ssam result = m_pullup(result, sizeof(struct sadb_msg)); 2445105197Ssam if (result == NULL) 2446105197Ssam goto fail; 2447105197Ssam } 2448105197Ssam 2449105197Ssam result->m_pkthdr.len = 0; 2450105197Ssam for (m = result; m; m = m->m_next) 2451105197Ssam result->m_pkthdr.len += m->m_len; 2452105197Ssam 2453105197Ssam mtod(result, struct sadb_msg *)->sadb_msg_len = 2454105197Ssam PFKEY_UNIT64(result->m_pkthdr.len); 2455105197Ssam 2456105197Ssam return result; 2457105197Ssam 2458105197Ssamfail: 2459105197Ssam m_freem(result); 2460105197Ssam return NULL; 2461105197Ssam} 2462105197Ssam 2463105197Ssam/* 2464105197Ssam * get PFKEY message length for security policy and request. 2465105197Ssam */ 2466105197Ssamstatic u_int 2467105197Ssamkey_getspreqmsglen(sp) 2468105197Ssam struct secpolicy *sp; 2469105197Ssam{ 2470105197Ssam u_int tlen; 2471105197Ssam 2472105197Ssam tlen = sizeof(struct sadb_x_policy); 2473105197Ssam 2474105197Ssam /* if is the policy for ipsec ? */ 2475105197Ssam if (sp->policy != IPSEC_POLICY_IPSEC) 2476105197Ssam return tlen; 2477105197Ssam 2478105197Ssam /* get length of ipsec requests */ 2479105197Ssam { 2480105197Ssam struct ipsecrequest *isr; 2481105197Ssam int len; 2482105197Ssam 2483105197Ssam for (isr = sp->req; isr != NULL; isr = isr->next) { 2484105197Ssam len = sizeof(struct sadb_x_ipsecrequest) 2485105197Ssam + isr->saidx.src.sa.sa_len 2486105197Ssam + isr->saidx.dst.sa.sa_len; 2487105197Ssam 2488105197Ssam tlen += PFKEY_ALIGN8(len); 2489105197Ssam } 2490105197Ssam } 2491105197Ssam 2492105197Ssam return tlen; 2493105197Ssam} 2494105197Ssam 2495105197Ssam/* 2496105197Ssam * SADB_SPDEXPIRE processing 2497105197Ssam * send 2498105197Ssam * <base, address(SD), lifetime(CH), policy> 2499105197Ssam * to KMD by PF_KEY. 2500105197Ssam * 2501105197Ssam * OUT: 0 : succeed 2502105197Ssam * others : error number 2503105197Ssam */ 2504105197Ssamstatic int 2505105197Ssamkey_spdexpire(sp) 2506105197Ssam struct secpolicy *sp; 2507105197Ssam{ 2508105197Ssam struct mbuf *result = NULL, *m; 2509105197Ssam int len; 2510105197Ssam int error = -1; 2511105197Ssam struct sadb_lifetime *lt; 2512105197Ssam 2513105197Ssam /* XXX: Why do we lock ? */ 2514105197Ssam 2515120585Ssam IPSEC_ASSERT(sp != NULL, ("null secpolicy")); 2516105197Ssam 2517105197Ssam /* set msg header */ 2518105197Ssam m = key_setsadbmsg(SADB_X_SPDEXPIRE, 0, 0, 0, 0, 0); 2519105197Ssam if (!m) { 2520105197Ssam error = ENOBUFS; 2521105197Ssam goto fail; 2522105197Ssam } 2523105197Ssam result = m; 2524105197Ssam 2525105197Ssam /* create lifetime extension (current and hard) */ 2526105197Ssam len = PFKEY_ALIGN8(sizeof(*lt)) * 2; 2527105197Ssam m = key_alloc_mbuf(len); 2528105197Ssam if (!m || m->m_next) { /*XXX*/ 2529105197Ssam if (m) 2530105197Ssam m_freem(m); 2531105197Ssam error = ENOBUFS; 2532105197Ssam goto fail; 2533105197Ssam } 2534105197Ssam bzero(mtod(m, caddr_t), len); 2535105197Ssam lt = mtod(m, struct sadb_lifetime *); 2536105197Ssam lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime)); 2537105197Ssam lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT; 2538105197Ssam lt->sadb_lifetime_allocations = 0; 2539105197Ssam lt->sadb_lifetime_bytes = 0; 2540105197Ssam lt->sadb_lifetime_addtime = sp->created; 2541105197Ssam lt->sadb_lifetime_usetime = sp->lastused; 2542105197Ssam lt = (struct sadb_lifetime *)(mtod(m, caddr_t) + len / 2); 2543105197Ssam lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime)); 2544105197Ssam lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD; 2545105197Ssam lt->sadb_lifetime_allocations = 0; 2546105197Ssam lt->sadb_lifetime_bytes = 0; 2547105197Ssam lt->sadb_lifetime_addtime = sp->lifetime; 2548105197Ssam lt->sadb_lifetime_usetime = sp->validtime; 2549105197Ssam m_cat(result, m); 2550105197Ssam 2551105197Ssam /* set sadb_address for source */ 2552105197Ssam m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC, 2553105197Ssam &sp->spidx.src.sa, 2554105197Ssam sp->spidx.prefs, sp->spidx.ul_proto); 2555105197Ssam if (!m) { 2556105197Ssam error = ENOBUFS; 2557105197Ssam goto fail; 2558105197Ssam } 2559105197Ssam m_cat(result, m); 2560105197Ssam 2561105197Ssam /* set sadb_address for destination */ 2562105197Ssam m = key_setsadbaddr(SADB_EXT_ADDRESS_DST, 2563105197Ssam &sp->spidx.dst.sa, 2564105197Ssam sp->spidx.prefd, sp->spidx.ul_proto); 2565105197Ssam if (!m) { 2566105197Ssam error = ENOBUFS; 2567105197Ssam goto fail; 2568105197Ssam } 2569105197Ssam m_cat(result, m); 2570105197Ssam 2571105197Ssam /* set secpolicy */ 2572105197Ssam m = key_sp2msg(sp); 2573105197Ssam if (!m) { 2574105197Ssam error = ENOBUFS; 2575105197Ssam goto fail; 2576105197Ssam } 2577105197Ssam m_cat(result, m); 2578105197Ssam 2579105197Ssam if ((result->m_flags & M_PKTHDR) == 0) { 2580105197Ssam error = EINVAL; 2581105197Ssam goto fail; 2582105197Ssam } 2583105197Ssam 2584105197Ssam if (result->m_len < sizeof(struct sadb_msg)) { 2585105197Ssam result = m_pullup(result, sizeof(struct sadb_msg)); 2586105197Ssam if (result == NULL) { 2587105197Ssam error = ENOBUFS; 2588105197Ssam goto fail; 2589105197Ssam } 2590105197Ssam } 2591105197Ssam 2592105197Ssam result->m_pkthdr.len = 0; 2593105197Ssam for (m = result; m; m = m->m_next) 2594105197Ssam result->m_pkthdr.len += m->m_len; 2595105197Ssam 2596105197Ssam mtod(result, struct sadb_msg *)->sadb_msg_len = 2597105197Ssam PFKEY_UNIT64(result->m_pkthdr.len); 2598105197Ssam 2599105197Ssam return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED); 2600105197Ssam 2601105197Ssam fail: 2602105197Ssam if (result) 2603105197Ssam m_freem(result); 2604105197Ssam return error; 2605105197Ssam} 2606105197Ssam 2607105197Ssam/* %%% SAD management */ 2608105197Ssam/* 2609105197Ssam * allocating a memory for new SA head, and copy from the values of mhp. 2610105197Ssam * OUT: NULL : failure due to the lack of memory. 2611105197Ssam * others : pointer to new SA head. 2612105197Ssam */ 2613105197Ssamstatic struct secashead * 2614105197Ssamkey_newsah(saidx) 2615105197Ssam struct secasindex *saidx; 2616105197Ssam{ 2617105197Ssam struct secashead *newsah; 2618105197Ssam 2619120585Ssam IPSEC_ASSERT(saidx != NULL, ("null saidx")); 2620105197Ssam 2621119643Ssam newsah = malloc(sizeof(struct secashead), M_IPSEC_SAH, M_NOWAIT|M_ZERO); 2622105197Ssam if (newsah != NULL) { 2623105197Ssam int i; 2624105197Ssam for (i = 0; i < sizeof(newsah->savtree)/sizeof(newsah->savtree[0]); i++) 2625105197Ssam LIST_INIT(&newsah->savtree[i]); 2626105197Ssam newsah->saidx = *saidx; 2627105197Ssam 2628105197Ssam /* add to saidxtree */ 2629105197Ssam newsah->state = SADB_SASTATE_MATURE; 2630119643Ssam 2631120585Ssam SAHTREE_LOCK(); 2632105197Ssam LIST_INSERT_HEAD(&sahtree, newsah, chain); 2633120585Ssam SAHTREE_UNLOCK(); 2634105197Ssam } 2635105197Ssam return(newsah); 2636105197Ssam} 2637105197Ssam 2638105197Ssam/* 2639105197Ssam * delete SA index and all SA registerd. 2640105197Ssam */ 2641105197Ssamstatic void 2642105197Ssamkey_delsah(sah) 2643105197Ssam struct secashead *sah; 2644105197Ssam{ 2645105197Ssam struct secasvar *sav, *nextsav; 2646120585Ssam u_int stateidx; 2647105197Ssam int zombie = 0; 2648105197Ssam 2649120585Ssam IPSEC_ASSERT(sah != NULL, ("NULL sah")); 2650120585Ssam SAHTREE_LOCK_ASSERT(); 2651105197Ssam 2652105197Ssam /* searching all SA registerd in the secindex. */ 2653105197Ssam for (stateidx = 0; 2654105197Ssam stateidx < _ARRAYLEN(saorder_state_any); 2655105197Ssam stateidx++) { 2656120585Ssam u_int state = saorder_state_any[stateidx]; 2657120585Ssam LIST_FOREACH_SAFE(sav, &sah->savtree[state], chain, nextsav) { 2658105197Ssam if (sav->refcnt == 0) { 2659105197Ssam /* sanity check */ 2660120585Ssam KEY_CHKSASTATE(state, sav->state, __func__); 2661105197Ssam KEY_FREESAV(&sav); 2662105197Ssam } else { 2663105197Ssam /* give up to delete this sa */ 2664105197Ssam zombie++; 2665105197Ssam } 2666105197Ssam } 2667105197Ssam } 2668120585Ssam if (!zombie) { /* delete only if there are savs */ 2669120585Ssam /* remove from tree of SA index */ 2670120585Ssam if (__LIST_CHAINED(sah)) 2671120585Ssam LIST_REMOVE(sah, chain); 2672120585Ssam if (sah->sa_route.ro_rt) { 2673120585Ssam RTFREE(sah->sa_route.ro_rt); 2674120585Ssam sah->sa_route.ro_rt = (struct rtentry *)NULL; 2675120585Ssam } 2676120585Ssam free(sah, M_IPSEC_SAH); 2677105197Ssam } 2678105197Ssam} 2679105197Ssam 2680105197Ssam/* 2681105197Ssam * allocating a new SA with LARVAL state. key_add() and key_getspi() call, 2682105197Ssam * and copy the values of mhp into new buffer. 2683105197Ssam * When SAD message type is GETSPI: 2684105197Ssam * to set sequence number from acq_seq++, 2685105197Ssam * to set zero to SPI. 2686105197Ssam * not to call key_setsava(). 2687105197Ssam * OUT: NULL : fail 2688105197Ssam * others : pointer to new secasvar. 2689105197Ssam * 2690105197Ssam * does not modify mbuf. does not free mbuf on error. 2691105197Ssam */ 2692105197Ssamstatic struct secasvar * 2693105197Ssamkey_newsav(m, mhp, sah, errp, where, tag) 2694105197Ssam struct mbuf *m; 2695105197Ssam const struct sadb_msghdr *mhp; 2696105197Ssam struct secashead *sah; 2697105197Ssam int *errp; 2698105197Ssam const char* where; 2699105197Ssam int tag; 2700105197Ssam{ 2701105197Ssam struct secasvar *newsav; 2702105197Ssam const struct sadb_sa *xsa; 2703105197Ssam 2704120585Ssam IPSEC_ASSERT(m != NULL, ("null mbuf")); 2705120585Ssam IPSEC_ASSERT(mhp != NULL, ("null msghdr")); 2706120585Ssam IPSEC_ASSERT(mhp->msg != NULL, ("null msg")); 2707120585Ssam IPSEC_ASSERT(sah != NULL, ("null secashead")); 2708105197Ssam 2709119643Ssam newsav = malloc(sizeof(struct secasvar), M_IPSEC_SA, M_NOWAIT|M_ZERO); 2710105197Ssam if (newsav == NULL) { 2711120585Ssam ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__)); 2712105197Ssam *errp = ENOBUFS; 2713105197Ssam goto done; 2714105197Ssam } 2715105197Ssam 2716105197Ssam switch (mhp->msg->sadb_msg_type) { 2717105197Ssam case SADB_GETSPI: 2718105197Ssam newsav->spi = 0; 2719105197Ssam 2720105197Ssam#ifdef IPSEC_DOSEQCHECK 2721105197Ssam /* sync sequence number */ 2722105197Ssam if (mhp->msg->sadb_msg_seq == 0) 2723105197Ssam newsav->seq = 2724105197Ssam (acq_seq = (acq_seq == ~0 ? 1 : ++acq_seq)); 2725105197Ssam else 2726105197Ssam#endif 2727105197Ssam newsav->seq = mhp->msg->sadb_msg_seq; 2728105197Ssam break; 2729105197Ssam 2730105197Ssam case SADB_ADD: 2731105197Ssam /* sanity check */ 2732105197Ssam if (mhp->ext[SADB_EXT_SA] == NULL) { 2733119643Ssam free(newsav, M_IPSEC_SA); 2734119643Ssam newsav = NULL; 2735120585Ssam ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n", 2736120585Ssam __func__)); 2737105197Ssam *errp = EINVAL; 2738105197Ssam goto done; 2739105197Ssam } 2740105197Ssam xsa = (const struct sadb_sa *)mhp->ext[SADB_EXT_SA]; 2741105197Ssam newsav->spi = xsa->sadb_sa_spi; 2742105197Ssam newsav->seq = mhp->msg->sadb_msg_seq; 2743105197Ssam break; 2744105197Ssam default: 2745119643Ssam free(newsav, M_IPSEC_SA); 2746119643Ssam newsav = NULL; 2747105197Ssam *errp = EINVAL; 2748105197Ssam goto done; 2749105197Ssam } 2750105197Ssam 2751119643Ssam 2752105197Ssam /* copy sav values */ 2753105197Ssam if (mhp->msg->sadb_msg_type != SADB_GETSPI) { 2754105197Ssam *errp = key_setsaval(newsav, m, mhp); 2755105197Ssam if (*errp) { 2756119643Ssam free(newsav, M_IPSEC_SA); 2757119643Ssam newsav = NULL; 2758105197Ssam goto done; 2759105197Ssam } 2760105197Ssam } 2761105197Ssam 2762120585Ssam SECASVAR_LOCK_INIT(newsav); 2763119643Ssam 2764105197Ssam /* reset created */ 2765105197Ssam newsav->created = time_second; 2766105197Ssam newsav->pid = mhp->msg->sadb_msg_pid; 2767105197Ssam 2768105197Ssam /* add to satree */ 2769105197Ssam newsav->sah = sah; 2770158767Spjd sa_initref(newsav); 2771105197Ssam newsav->state = SADB_SASTATE_LARVAL; 2772119643Ssam 2773119643Ssam /* XXX locking??? */ 2774105197Ssam LIST_INSERT_TAIL(&sah->savtree[SADB_SASTATE_LARVAL], newsav, 2775105197Ssam secasvar, chain); 2776105197Ssamdone: 2777105197Ssam KEYDEBUG(KEYDEBUG_IPSEC_STAMP, 2778120585Ssam printf("DP %s from %s:%u return SP:%p\n", __func__, 2779105197Ssam where, tag, newsav)); 2780105197Ssam 2781105197Ssam return newsav; 2782105197Ssam} 2783105197Ssam 2784105197Ssam/* 2785105197Ssam * free() SA variable entry. 2786105197Ssam */ 2787105197Ssamstatic void 2788119643Ssamkey_cleansav(struct secasvar *sav) 2789105197Ssam{ 2790117051Ssam /* 2791117051Ssam * Cleanup xform state. Note that zeroize'ing causes the 2792117051Ssam * keys to be cleared; otherwise we must do it ourself. 2793117051Ssam */ 2794117051Ssam if (sav->tdb_xform != NULL) { 2795117051Ssam sav->tdb_xform->xf_zeroize(sav); 2796117051Ssam sav->tdb_xform = NULL; 2797117051Ssam } else { 2798120585Ssam KASSERT(sav->iv == NULL, ("iv but no xform")); 2799117051Ssam if (sav->key_auth != NULL) 2800157123Sgnn bzero(sav->key_auth->key_data, _KEYLEN(sav->key_auth)); 2801117051Ssam if (sav->key_enc != NULL) 2802157123Sgnn bzero(sav->key_enc->key_data, _KEYLEN(sav->key_enc)); 2803117051Ssam } 2804105197Ssam if (sav->key_auth != NULL) { 2805157123Sgnn if (sav->key_auth->key_data != NULL) 2806157123Sgnn free(sav->key_auth->key_data, M_IPSEC_MISC); 2807119643Ssam free(sav->key_auth, M_IPSEC_MISC); 2808105197Ssam sav->key_auth = NULL; 2809105197Ssam } 2810105197Ssam if (sav->key_enc != NULL) { 2811157123Sgnn if (sav->key_enc->key_data != NULL) 2812157123Sgnn free(sav->key_enc->key_data, M_IPSEC_MISC); 2813119643Ssam free(sav->key_enc, M_IPSEC_MISC); 2814105197Ssam sav->key_enc = NULL; 2815105197Ssam } 2816105197Ssam if (sav->sched) { 2817105197Ssam bzero(sav->sched, sav->schedlen); 2818119643Ssam free(sav->sched, M_IPSEC_MISC); 2819105197Ssam sav->sched = NULL; 2820105197Ssam } 2821105197Ssam if (sav->replay != NULL) { 2822119643Ssam free(sav->replay, M_IPSEC_MISC); 2823105197Ssam sav->replay = NULL; 2824105197Ssam } 2825105197Ssam if (sav->lft_c != NULL) { 2826119643Ssam free(sav->lft_c, M_IPSEC_MISC); 2827105197Ssam sav->lft_c = NULL; 2828105197Ssam } 2829105197Ssam if (sav->lft_h != NULL) { 2830119643Ssam free(sav->lft_h, M_IPSEC_MISC); 2831105197Ssam sav->lft_h = NULL; 2832105197Ssam } 2833105197Ssam if (sav->lft_s != NULL) { 2834119643Ssam free(sav->lft_s, M_IPSEC_MISC); 2835105197Ssam sav->lft_s = NULL; 2836105197Ssam } 2837119643Ssam} 2838105197Ssam 2839119643Ssam/* 2840119643Ssam * free() SA variable entry. 2841119643Ssam */ 2842119643Ssamstatic void 2843119643Ssamkey_delsav(sav) 2844119643Ssam struct secasvar *sav; 2845119643Ssam{ 2846120585Ssam IPSEC_ASSERT(sav != NULL, ("null sav")); 2847120585Ssam IPSEC_ASSERT(sav->refcnt == 0, ("reference count %u > 0", sav->refcnt)); 2848105197Ssam 2849119643Ssam /* remove from SA header */ 2850119643Ssam if (__LIST_CHAINED(sav)) 2851119643Ssam LIST_REMOVE(sav, chain); 2852119643Ssam key_cleansav(sav); 2853120585Ssam SECASVAR_LOCK_DESTROY(sav); 2854119643Ssam free(sav, M_IPSEC_SA); 2855105197Ssam} 2856105197Ssam 2857105197Ssam/* 2858105197Ssam * search SAD. 2859105197Ssam * OUT: 2860105197Ssam * NULL : not found 2861105197Ssam * others : found, pointer to a SA. 2862105197Ssam */ 2863105197Ssamstatic struct secashead * 2864105197Ssamkey_getsah(saidx) 2865105197Ssam struct secasindex *saidx; 2866105197Ssam{ 2867105197Ssam struct secashead *sah; 2868105197Ssam 2869120585Ssam SAHTREE_LOCK(); 2870105197Ssam LIST_FOREACH(sah, &sahtree, chain) { 2871105197Ssam if (sah->state == SADB_SASTATE_DEAD) 2872105197Ssam continue; 2873105197Ssam if (key_cmpsaidx(&sah->saidx, saidx, CMP_REQID)) 2874119643Ssam break; 2875105197Ssam } 2876120585Ssam SAHTREE_UNLOCK(); 2877105197Ssam 2878119643Ssam return sah; 2879105197Ssam} 2880105197Ssam 2881105197Ssam/* 2882105197Ssam * check not to be duplicated SPI. 2883105197Ssam * NOTE: this function is too slow due to searching all SAD. 2884105197Ssam * OUT: 2885105197Ssam * NULL : not found 2886105197Ssam * others : found, pointer to a SA. 2887105197Ssam */ 2888105197Ssamstatic struct secasvar * 2889105197Ssamkey_checkspidup(saidx, spi) 2890105197Ssam struct secasindex *saidx; 2891105197Ssam u_int32_t spi; 2892105197Ssam{ 2893105197Ssam struct secashead *sah; 2894105197Ssam struct secasvar *sav; 2895105197Ssam 2896105197Ssam /* check address family */ 2897105197Ssam if (saidx->src.sa.sa_family != saidx->dst.sa.sa_family) { 2898120585Ssam ipseclog((LOG_DEBUG, "%s: address family mismatched.\n", 2899120585Ssam __func__)); 2900105197Ssam return NULL; 2901105197Ssam } 2902105197Ssam 2903119643Ssam sav = NULL; 2904105197Ssam /* check all SAD */ 2905120585Ssam SAHTREE_LOCK(); 2906105197Ssam LIST_FOREACH(sah, &sahtree, chain) { 2907105197Ssam if (!key_ismyaddr((struct sockaddr *)&sah->saidx.dst)) 2908105197Ssam continue; 2909105197Ssam sav = key_getsavbyspi(sah, spi); 2910105197Ssam if (sav != NULL) 2911119643Ssam break; 2912105197Ssam } 2913120585Ssam SAHTREE_UNLOCK(); 2914105197Ssam 2915119643Ssam return sav; 2916105197Ssam} 2917105197Ssam 2918105197Ssam/* 2919105197Ssam * search SAD litmited alive SA, protocol, SPI. 2920105197Ssam * OUT: 2921105197Ssam * NULL : not found 2922105197Ssam * others : found, pointer to a SA. 2923105197Ssam */ 2924105197Ssamstatic struct secasvar * 2925105197Ssamkey_getsavbyspi(sah, spi) 2926105197Ssam struct secashead *sah; 2927105197Ssam u_int32_t spi; 2928105197Ssam{ 2929105197Ssam struct secasvar *sav; 2930105197Ssam u_int stateidx, state; 2931105197Ssam 2932119643Ssam sav = NULL; 2933120585Ssam SAHTREE_LOCK_ASSERT(); 2934105197Ssam /* search all status */ 2935105197Ssam for (stateidx = 0; 2936105197Ssam stateidx < _ARRAYLEN(saorder_state_alive); 2937105197Ssam stateidx++) { 2938105197Ssam 2939105197Ssam state = saorder_state_alive[stateidx]; 2940105197Ssam LIST_FOREACH(sav, &sah->savtree[state], chain) { 2941105197Ssam 2942105197Ssam /* sanity check */ 2943105197Ssam if (sav->state != state) { 2944120585Ssam ipseclog((LOG_DEBUG, "%s: " 2945105197Ssam "invalid sav->state (queue: %d SA: %d)\n", 2946120585Ssam __func__, state, sav->state)); 2947105197Ssam continue; 2948105197Ssam } 2949105197Ssam 2950105197Ssam if (sav->spi == spi) 2951128859Ssam return sav; 2952105197Ssam } 2953105197Ssam } 2954105197Ssam 2955128859Ssam return NULL; 2956105197Ssam} 2957105197Ssam 2958105197Ssam/* 2959105197Ssam * copy SA values from PF_KEY message except *SPI, SEQ, PID, STATE and TYPE*. 2960105197Ssam * You must update these if need. 2961105197Ssam * OUT: 0: success. 2962105197Ssam * !0: failure. 2963105197Ssam * 2964105197Ssam * does not modify mbuf. does not free mbuf on error. 2965105197Ssam */ 2966105197Ssamstatic int 2967105197Ssamkey_setsaval(sav, m, mhp) 2968105197Ssam struct secasvar *sav; 2969105197Ssam struct mbuf *m; 2970105197Ssam const struct sadb_msghdr *mhp; 2971105197Ssam{ 2972105197Ssam int error = 0; 2973105197Ssam 2974120585Ssam IPSEC_ASSERT(m != NULL, ("null mbuf")); 2975120585Ssam IPSEC_ASSERT(mhp != NULL, ("null msghdr")); 2976120585Ssam IPSEC_ASSERT(mhp->msg != NULL, ("null msg")); 2977105197Ssam 2978105197Ssam /* initialization */ 2979105197Ssam sav->replay = NULL; 2980105197Ssam sav->key_auth = NULL; 2981105197Ssam sav->key_enc = NULL; 2982105197Ssam sav->sched = NULL; 2983105197Ssam sav->schedlen = 0; 2984105197Ssam sav->iv = NULL; 2985105197Ssam sav->lft_c = NULL; 2986105197Ssam sav->lft_h = NULL; 2987105197Ssam sav->lft_s = NULL; 2988105197Ssam sav->tdb_xform = NULL; /* transform */ 2989105197Ssam sav->tdb_encalgxform = NULL; /* encoding algorithm */ 2990105197Ssam sav->tdb_authalgxform = NULL; /* authentication algorithm */ 2991105197Ssam sav->tdb_compalgxform = NULL; /* compression algorithm */ 2992105197Ssam 2993105197Ssam /* SA */ 2994105197Ssam if (mhp->ext[SADB_EXT_SA] != NULL) { 2995105197Ssam const struct sadb_sa *sa0; 2996105197Ssam 2997105197Ssam sa0 = (const struct sadb_sa *)mhp->ext[SADB_EXT_SA]; 2998105197Ssam if (mhp->extlen[SADB_EXT_SA] < sizeof(*sa0)) { 2999105197Ssam error = EINVAL; 3000105197Ssam goto fail; 3001105197Ssam } 3002105197Ssam 3003105197Ssam sav->alg_auth = sa0->sadb_sa_auth; 3004105197Ssam sav->alg_enc = sa0->sadb_sa_encrypt; 3005105197Ssam sav->flags = sa0->sadb_sa_flags; 3006105197Ssam 3007105197Ssam /* replay window */ 3008105197Ssam if ((sa0->sadb_sa_flags & SADB_X_EXT_OLD) == 0) { 3009105197Ssam sav->replay = (struct secreplay *) 3010119643Ssam malloc(sizeof(struct secreplay)+sa0->sadb_sa_replay, M_IPSEC_MISC, M_NOWAIT|M_ZERO); 3011105197Ssam if (sav->replay == NULL) { 3012120585Ssam ipseclog((LOG_DEBUG, "%s: No more memory.\n", 3013120585Ssam __func__)); 3014105197Ssam error = ENOBUFS; 3015105197Ssam goto fail; 3016105197Ssam } 3017105197Ssam if (sa0->sadb_sa_replay != 0) 3018105197Ssam sav->replay->bitmap = (caddr_t)(sav->replay+1); 3019105197Ssam sav->replay->wsize = sa0->sadb_sa_replay; 3020105197Ssam } 3021105197Ssam } 3022105197Ssam 3023105197Ssam /* Authentication keys */ 3024105197Ssam if (mhp->ext[SADB_EXT_KEY_AUTH] != NULL) { 3025105197Ssam const struct sadb_key *key0; 3026105197Ssam int len; 3027105197Ssam 3028105197Ssam key0 = (const struct sadb_key *)mhp->ext[SADB_EXT_KEY_AUTH]; 3029105197Ssam len = mhp->extlen[SADB_EXT_KEY_AUTH]; 3030105197Ssam 3031105197Ssam error = 0; 3032105197Ssam if (len < sizeof(*key0)) { 3033105197Ssam error = EINVAL; 3034105197Ssam goto fail; 3035105197Ssam } 3036105197Ssam switch (mhp->msg->sadb_msg_satype) { 3037105197Ssam case SADB_SATYPE_AH: 3038105197Ssam case SADB_SATYPE_ESP: 3039125680Sbms case SADB_X_SATYPE_TCPSIGNATURE: 3040105197Ssam if (len == PFKEY_ALIGN8(sizeof(struct sadb_key)) && 3041105197Ssam sav->alg_auth != SADB_X_AALG_NULL) 3042105197Ssam error = EINVAL; 3043105197Ssam break; 3044105197Ssam case SADB_X_SATYPE_IPCOMP: 3045105197Ssam default: 3046105197Ssam error = EINVAL; 3047105197Ssam break; 3048105197Ssam } 3049105197Ssam if (error) { 3050120585Ssam ipseclog((LOG_DEBUG, "%s: invalid key_auth values.\n", 3051120585Ssam __func__)); 3052105197Ssam goto fail; 3053105197Ssam } 3054105197Ssam 3055157123Sgnn sav->key_auth = (struct seckey *)key_dup_keymsg(key0, len, 3056157123Sgnn M_IPSEC_MISC); 3057157123Sgnn if (sav->key_auth == NULL ) { 3058157123Sgnn ipseclog((LOG_DEBUG, "%s: No more memory.\n", 3059157123Sgnn __func__)); 3060105197Ssam error = ENOBUFS; 3061105197Ssam goto fail; 3062105197Ssam } 3063105197Ssam } 3064105197Ssam 3065105197Ssam /* Encryption key */ 3066105197Ssam if (mhp->ext[SADB_EXT_KEY_ENCRYPT] != NULL) { 3067105197Ssam const struct sadb_key *key0; 3068105197Ssam int len; 3069105197Ssam 3070105197Ssam key0 = (const struct sadb_key *)mhp->ext[SADB_EXT_KEY_ENCRYPT]; 3071105197Ssam len = mhp->extlen[SADB_EXT_KEY_ENCRYPT]; 3072105197Ssam 3073105197Ssam error = 0; 3074105197Ssam if (len < sizeof(*key0)) { 3075105197Ssam error = EINVAL; 3076105197Ssam goto fail; 3077105197Ssam } 3078105197Ssam switch (mhp->msg->sadb_msg_satype) { 3079105197Ssam case SADB_SATYPE_ESP: 3080105197Ssam if (len == PFKEY_ALIGN8(sizeof(struct sadb_key)) && 3081105197Ssam sav->alg_enc != SADB_EALG_NULL) { 3082105197Ssam error = EINVAL; 3083105197Ssam break; 3084105197Ssam } 3085157123Sgnn sav->key_enc = (struct seckey *)key_dup_keymsg(key0, 3086157123Sgnn len, 3087157123Sgnn M_IPSEC_MISC); 3088105197Ssam if (sav->key_enc == NULL) { 3089120585Ssam ipseclog((LOG_DEBUG, "%s: No more memory.\n", 3090120585Ssam __func__)); 3091105197Ssam error = ENOBUFS; 3092105197Ssam goto fail; 3093105197Ssam } 3094105197Ssam break; 3095105197Ssam case SADB_X_SATYPE_IPCOMP: 3096105197Ssam if (len != PFKEY_ALIGN8(sizeof(struct sadb_key))) 3097105197Ssam error = EINVAL; 3098105197Ssam sav->key_enc = NULL; /*just in case*/ 3099105197Ssam break; 3100105197Ssam case SADB_SATYPE_AH: 3101125680Sbms case SADB_X_SATYPE_TCPSIGNATURE: 3102105197Ssam default: 3103105197Ssam error = EINVAL; 3104105197Ssam break; 3105105197Ssam } 3106105197Ssam if (error) { 3107120585Ssam ipseclog((LOG_DEBUG, "%s: invalid key_enc value.\n", 3108120585Ssam __func__)); 3109105197Ssam goto fail; 3110105197Ssam } 3111105197Ssam } 3112105197Ssam 3113105197Ssam /* set iv */ 3114105197Ssam sav->ivlen = 0; 3115105197Ssam 3116105197Ssam switch (mhp->msg->sadb_msg_satype) { 3117105197Ssam case SADB_SATYPE_AH: 3118105197Ssam error = xform_init(sav, XF_AH); 3119105197Ssam break; 3120105197Ssam case SADB_SATYPE_ESP: 3121105197Ssam error = xform_init(sav, XF_ESP); 3122105197Ssam break; 3123105197Ssam case SADB_X_SATYPE_IPCOMP: 3124105197Ssam error = xform_init(sav, XF_IPCOMP); 3125105197Ssam break; 3126125680Sbms case SADB_X_SATYPE_TCPSIGNATURE: 3127125680Sbms error = xform_init(sav, XF_TCPSIGNATURE); 3128125680Sbms break; 3129105197Ssam } 3130105197Ssam if (error) { 3131120585Ssam ipseclog((LOG_DEBUG, "%s: unable to initialize SA type %u.\n", 3132120585Ssam __func__, mhp->msg->sadb_msg_satype)); 3133105197Ssam goto fail; 3134105197Ssam } 3135105197Ssam 3136105197Ssam /* reset created */ 3137105197Ssam sav->created = time_second; 3138105197Ssam 3139105197Ssam /* make lifetime for CURRENT */ 3140176743Sbz sav->lft_c = malloc(sizeof(struct seclifetime), M_IPSEC_MISC, M_NOWAIT); 3141105197Ssam if (sav->lft_c == NULL) { 3142120585Ssam ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__)); 3143105197Ssam error = ENOBUFS; 3144105197Ssam goto fail; 3145105197Ssam } 3146105197Ssam 3147157123Sgnn sav->lft_c->allocations = 0; 3148157123Sgnn sav->lft_c->bytes = 0; 3149157123Sgnn sav->lft_c->addtime = time_second; 3150157123Sgnn sav->lft_c->usetime = 0; 3151105197Ssam 3152105197Ssam /* lifetimes for HARD and SOFT */ 3153105197Ssam { 3154105197Ssam const struct sadb_lifetime *lft0; 3155105197Ssam 3156105197Ssam lft0 = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_HARD]; 3157105197Ssam if (lft0 != NULL) { 3158105197Ssam if (mhp->extlen[SADB_EXT_LIFETIME_HARD] < sizeof(*lft0)) { 3159105197Ssam error = EINVAL; 3160105197Ssam goto fail; 3161105197Ssam } 3162157123Sgnn sav->lft_h = key_dup_lifemsg(lft0, M_IPSEC_MISC); 3163105197Ssam if (sav->lft_h == NULL) { 3164120585Ssam ipseclog((LOG_DEBUG, "%s: No more memory.\n",__func__)); 3165105197Ssam error = ENOBUFS; 3166105197Ssam goto fail; 3167105197Ssam } 3168105197Ssam /* to be initialize ? */ 3169105197Ssam } 3170105197Ssam 3171105197Ssam lft0 = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_SOFT]; 3172105197Ssam if (lft0 != NULL) { 3173105197Ssam if (mhp->extlen[SADB_EXT_LIFETIME_SOFT] < sizeof(*lft0)) { 3174105197Ssam error = EINVAL; 3175105197Ssam goto fail; 3176105197Ssam } 3177157123Sgnn sav->lft_s = key_dup_lifemsg(lft0, M_IPSEC_MISC); 3178105197Ssam if (sav->lft_s == NULL) { 3179120585Ssam ipseclog((LOG_DEBUG, "%s: No more memory.\n",__func__)); 3180105197Ssam error = ENOBUFS; 3181105197Ssam goto fail; 3182105197Ssam } 3183105197Ssam /* to be initialize ? */ 3184105197Ssam } 3185105197Ssam } 3186105197Ssam 3187105197Ssam return 0; 3188105197Ssam 3189105197Ssam fail: 3190105197Ssam /* initialization */ 3191119643Ssam key_cleansav(sav); 3192105197Ssam 3193105197Ssam return error; 3194105197Ssam} 3195105197Ssam 3196105197Ssam/* 3197105197Ssam * validation with a secasvar entry, and set SADB_SATYPE_MATURE. 3198105197Ssam * OUT: 0: valid 3199105197Ssam * other: errno 3200105197Ssam */ 3201105197Ssamstatic int 3202119643Ssamkey_mature(struct secasvar *sav) 3203105197Ssam{ 3204105197Ssam int error; 3205105197Ssam 3206105197Ssam /* check SPI value */ 3207105197Ssam switch (sav->sah->saidx.proto) { 3208105197Ssam case IPPROTO_ESP: 3209105197Ssam case IPPROTO_AH: 3210170823Sbz /* 3211170823Sbz * RFC 4302, 2.4. Security Parameters Index (SPI), SPI values 3212170823Sbz * 1-255 reserved by IANA for future use, 3213170823Sbz * 0 for implementation specific, local use. 3214170823Sbz */ 3215170823Sbz if (ntohl(sav->spi) <= 255) { 3216120585Ssam ipseclog((LOG_DEBUG, "%s: illegal range of SPI %u.\n", 3217120585Ssam __func__, (u_int32_t)ntohl(sav->spi))); 3218105197Ssam return EINVAL; 3219105197Ssam } 3220105197Ssam break; 3221105197Ssam } 3222105197Ssam 3223105197Ssam /* check satype */ 3224105197Ssam switch (sav->sah->saidx.proto) { 3225105197Ssam case IPPROTO_ESP: 3226105197Ssam /* check flags */ 3227105197Ssam if ((sav->flags & (SADB_X_EXT_OLD|SADB_X_EXT_DERIV)) == 3228105197Ssam (SADB_X_EXT_OLD|SADB_X_EXT_DERIV)) { 3229120585Ssam ipseclog((LOG_DEBUG, "%s: invalid flag (derived) " 3230120585Ssam "given to old-esp.\n", __func__)); 3231105197Ssam return EINVAL; 3232105197Ssam } 3233105197Ssam error = xform_init(sav, XF_ESP); 3234105197Ssam break; 3235105197Ssam case IPPROTO_AH: 3236105197Ssam /* check flags */ 3237105197Ssam if (sav->flags & SADB_X_EXT_DERIV) { 3238120585Ssam ipseclog((LOG_DEBUG, "%s: invalid flag (derived) " 3239120585Ssam "given to AH SA.\n", __func__)); 3240105197Ssam return EINVAL; 3241105197Ssam } 3242105197Ssam if (sav->alg_enc != SADB_EALG_NONE) { 3243120585Ssam ipseclog((LOG_DEBUG, "%s: protocol and algorithm " 3244120585Ssam "mismated.\n", __func__)); 3245105197Ssam return(EINVAL); 3246105197Ssam } 3247105197Ssam error = xform_init(sav, XF_AH); 3248105197Ssam break; 3249105197Ssam case IPPROTO_IPCOMP: 3250105197Ssam if (sav->alg_auth != SADB_AALG_NONE) { 3251120585Ssam ipseclog((LOG_DEBUG, "%s: protocol and algorithm " 3252120585Ssam "mismated.\n", __func__)); 3253105197Ssam return(EINVAL); 3254105197Ssam } 3255105197Ssam if ((sav->flags & SADB_X_EXT_RAWCPI) == 0 3256105197Ssam && ntohl(sav->spi) >= 0x10000) { 3257120585Ssam ipseclog((LOG_DEBUG, "%s: invalid cpi for IPComp.\n", 3258120585Ssam __func__)); 3259105197Ssam return(EINVAL); 3260105197Ssam } 3261105197Ssam error = xform_init(sav, XF_IPCOMP); 3262105197Ssam break; 3263125680Sbms case IPPROTO_TCP: 3264125680Sbms if (sav->alg_enc != SADB_EALG_NONE) { 3265125680Sbms ipseclog((LOG_DEBUG, "%s: protocol and algorithm " 3266125680Sbms "mismated.\n", __func__)); 3267125680Sbms return(EINVAL); 3268125680Sbms } 3269125680Sbms error = xform_init(sav, XF_TCPSIGNATURE); 3270125680Sbms break; 3271105197Ssam default: 3272120585Ssam ipseclog((LOG_DEBUG, "%s: Invalid satype.\n", __func__)); 3273105197Ssam error = EPROTONOSUPPORT; 3274105197Ssam break; 3275105197Ssam } 3276119643Ssam if (error == 0) { 3277120585Ssam SAHTREE_LOCK(); 3278105197Ssam key_sa_chgstate(sav, SADB_SASTATE_MATURE); 3279120585Ssam SAHTREE_UNLOCK(); 3280119643Ssam } 3281105197Ssam return (error); 3282105197Ssam} 3283105197Ssam 3284105197Ssam/* 3285105197Ssam * subroutine for SADB_GET and SADB_DUMP. 3286105197Ssam */ 3287105197Ssamstatic struct mbuf * 3288105197Ssamkey_setdumpsa(sav, type, satype, seq, pid) 3289105197Ssam struct secasvar *sav; 3290105197Ssam u_int8_t type, satype; 3291105197Ssam u_int32_t seq, pid; 3292105197Ssam{ 3293105197Ssam struct mbuf *result = NULL, *tres = NULL, *m; 3294105197Ssam int i; 3295105197Ssam int dumporder[] = { 3296105197Ssam SADB_EXT_SA, SADB_X_EXT_SA2, 3297105197Ssam SADB_EXT_LIFETIME_HARD, SADB_EXT_LIFETIME_SOFT, 3298105197Ssam SADB_EXT_LIFETIME_CURRENT, SADB_EXT_ADDRESS_SRC, 3299105197Ssam SADB_EXT_ADDRESS_DST, SADB_EXT_ADDRESS_PROXY, SADB_EXT_KEY_AUTH, 3300105197Ssam SADB_EXT_KEY_ENCRYPT, SADB_EXT_IDENTITY_SRC, 3301105197Ssam SADB_EXT_IDENTITY_DST, SADB_EXT_SENSITIVITY, 3302105197Ssam }; 3303105197Ssam 3304105197Ssam m = key_setsadbmsg(type, 0, satype, seq, pid, sav->refcnt); 3305105197Ssam if (m == NULL) 3306105197Ssam goto fail; 3307105197Ssam result = m; 3308105197Ssam 3309105197Ssam for (i = sizeof(dumporder)/sizeof(dumporder[0]) - 1; i >= 0; i--) { 3310105197Ssam m = NULL; 3311105197Ssam switch (dumporder[i]) { 3312105197Ssam case SADB_EXT_SA: 3313105197Ssam m = key_setsadbsa(sav); 3314105197Ssam if (!m) 3315105197Ssam goto fail; 3316105197Ssam break; 3317105197Ssam 3318105197Ssam case SADB_X_EXT_SA2: 3319105197Ssam m = key_setsadbxsa2(sav->sah->saidx.mode, 3320105197Ssam sav->replay ? sav->replay->count : 0, 3321105197Ssam sav->sah->saidx.reqid); 3322105197Ssam if (!m) 3323105197Ssam goto fail; 3324105197Ssam break; 3325105197Ssam 3326105197Ssam case SADB_EXT_ADDRESS_SRC: 3327105197Ssam m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC, 3328105197Ssam &sav->sah->saidx.src.sa, 3329105197Ssam FULLMASK, IPSEC_ULPROTO_ANY); 3330105197Ssam if (!m) 3331105197Ssam goto fail; 3332105197Ssam break; 3333105197Ssam 3334105197Ssam case SADB_EXT_ADDRESS_DST: 3335105197Ssam m = key_setsadbaddr(SADB_EXT_ADDRESS_DST, 3336105197Ssam &sav->sah->saidx.dst.sa, 3337105197Ssam FULLMASK, IPSEC_ULPROTO_ANY); 3338105197Ssam if (!m) 3339105197Ssam goto fail; 3340105197Ssam break; 3341105197Ssam 3342105197Ssam case SADB_EXT_KEY_AUTH: 3343105197Ssam if (!sav->key_auth) 3344105197Ssam continue; 3345157123Sgnn m = key_setkey(sav->key_auth, SADB_EXT_KEY_AUTH); 3346157123Sgnn if (!m) 3347157123Sgnn goto fail; 3348105197Ssam break; 3349105197Ssam 3350105197Ssam case SADB_EXT_KEY_ENCRYPT: 3351105197Ssam if (!sav->key_enc) 3352105197Ssam continue; 3353157123Sgnn m = key_setkey(sav->key_enc, SADB_EXT_KEY_ENCRYPT); 3354157123Sgnn if (!m) 3355157123Sgnn goto fail; 3356105197Ssam break; 3357105197Ssam 3358105197Ssam case SADB_EXT_LIFETIME_CURRENT: 3359105197Ssam if (!sav->lft_c) 3360105197Ssam continue; 3361157123Sgnn m = key_setlifetime(sav->lft_c, 3362157123Sgnn SADB_EXT_LIFETIME_CURRENT); 3363157123Sgnn if (!m) 3364157123Sgnn goto fail; 3365105197Ssam break; 3366105197Ssam 3367105197Ssam case SADB_EXT_LIFETIME_HARD: 3368105197Ssam if (!sav->lft_h) 3369105197Ssam continue; 3370157123Sgnn m = key_setlifetime(sav->lft_h, 3371157123Sgnn SADB_EXT_LIFETIME_HARD); 3372157123Sgnn if (!m) 3373157123Sgnn goto fail; 3374105197Ssam break; 3375105197Ssam 3376105197Ssam case SADB_EXT_LIFETIME_SOFT: 3377105197Ssam if (!sav->lft_s) 3378105197Ssam continue; 3379157123Sgnn m = key_setlifetime(sav->lft_h, 3380157123Sgnn SADB_EXT_LIFETIME_SOFT); 3381157123Sgnn 3382157123Sgnn if (!m) 3383157123Sgnn goto fail; 3384105197Ssam break; 3385105197Ssam 3386105197Ssam case SADB_EXT_ADDRESS_PROXY: 3387105197Ssam case SADB_EXT_IDENTITY_SRC: 3388105197Ssam case SADB_EXT_IDENTITY_DST: 3389105197Ssam /* XXX: should we brought from SPD ? */ 3390105197Ssam case SADB_EXT_SENSITIVITY: 3391105197Ssam default: 3392105197Ssam continue; 3393105197Ssam } 3394105197Ssam 3395157123Sgnn if (!m) 3396105197Ssam goto fail; 3397105197Ssam if (tres) 3398105197Ssam m_cat(m, tres); 3399105197Ssam tres = m; 3400157123Sgnn 3401105197Ssam } 3402105197Ssam 3403105197Ssam m_cat(result, tres); 3404105197Ssam if (result->m_len < sizeof(struct sadb_msg)) { 3405105197Ssam result = m_pullup(result, sizeof(struct sadb_msg)); 3406105197Ssam if (result == NULL) 3407105197Ssam goto fail; 3408105197Ssam } 3409105197Ssam 3410105197Ssam result->m_pkthdr.len = 0; 3411105197Ssam for (m = result; m; m = m->m_next) 3412105197Ssam result->m_pkthdr.len += m->m_len; 3413105197Ssam 3414105197Ssam mtod(result, struct sadb_msg *)->sadb_msg_len = 3415105197Ssam PFKEY_UNIT64(result->m_pkthdr.len); 3416105197Ssam 3417105197Ssam return result; 3418105197Ssam 3419105197Ssamfail: 3420105197Ssam m_freem(result); 3421105197Ssam m_freem(tres); 3422105197Ssam return NULL; 3423105197Ssam} 3424105197Ssam 3425105197Ssam/* 3426105197Ssam * set data into sadb_msg. 3427105197Ssam */ 3428105197Ssamstatic struct mbuf * 3429105197Ssamkey_setsadbmsg(type, tlen, satype, seq, pid, reserved) 3430105197Ssam u_int8_t type, satype; 3431105197Ssam u_int16_t tlen; 3432105197Ssam u_int32_t seq; 3433105197Ssam pid_t pid; 3434105197Ssam u_int16_t reserved; 3435105197Ssam{ 3436105197Ssam struct mbuf *m; 3437105197Ssam struct sadb_msg *p; 3438105197Ssam int len; 3439105197Ssam 3440105197Ssam len = PFKEY_ALIGN8(sizeof(struct sadb_msg)); 3441105197Ssam if (len > MCLBYTES) 3442105197Ssam return NULL; 3443111119Simp MGETHDR(m, M_DONTWAIT, MT_DATA); 3444105197Ssam if (m && len > MHLEN) { 3445111119Simp MCLGET(m, M_DONTWAIT); 3446105197Ssam if ((m->m_flags & M_EXT) == 0) { 3447105197Ssam m_freem(m); 3448105197Ssam m = NULL; 3449105197Ssam } 3450105197Ssam } 3451105197Ssam if (!m) 3452105197Ssam return NULL; 3453105197Ssam m->m_pkthdr.len = m->m_len = len; 3454105197Ssam m->m_next = NULL; 3455105197Ssam 3456105197Ssam p = mtod(m, struct sadb_msg *); 3457105197Ssam 3458105197Ssam bzero(p, len); 3459105197Ssam p->sadb_msg_version = PF_KEY_V2; 3460105197Ssam p->sadb_msg_type = type; 3461105197Ssam p->sadb_msg_errno = 0; 3462105197Ssam p->sadb_msg_satype = satype; 3463105197Ssam p->sadb_msg_len = PFKEY_UNIT64(tlen); 3464105197Ssam p->sadb_msg_reserved = reserved; 3465105197Ssam p->sadb_msg_seq = seq; 3466105197Ssam p->sadb_msg_pid = (u_int32_t)pid; 3467105197Ssam 3468105197Ssam return m; 3469105197Ssam} 3470105197Ssam 3471105197Ssam/* 3472105197Ssam * copy secasvar data into sadb_address. 3473105197Ssam */ 3474105197Ssamstatic struct mbuf * 3475105197Ssamkey_setsadbsa(sav) 3476105197Ssam struct secasvar *sav; 3477105197Ssam{ 3478105197Ssam struct mbuf *m; 3479105197Ssam struct sadb_sa *p; 3480105197Ssam int len; 3481105197Ssam 3482105197Ssam len = PFKEY_ALIGN8(sizeof(struct sadb_sa)); 3483105197Ssam m = key_alloc_mbuf(len); 3484105197Ssam if (!m || m->m_next) { /*XXX*/ 3485105197Ssam if (m) 3486105197Ssam m_freem(m); 3487105197Ssam return NULL; 3488105197Ssam } 3489105197Ssam 3490105197Ssam p = mtod(m, struct sadb_sa *); 3491105197Ssam 3492105197Ssam bzero(p, len); 3493105197Ssam p->sadb_sa_len = PFKEY_UNIT64(len); 3494105197Ssam p->sadb_sa_exttype = SADB_EXT_SA; 3495105197Ssam p->sadb_sa_spi = sav->spi; 3496105197Ssam p->sadb_sa_replay = (sav->replay != NULL ? sav->replay->wsize : 0); 3497105197Ssam p->sadb_sa_state = sav->state; 3498105197Ssam p->sadb_sa_auth = sav->alg_auth; 3499105197Ssam p->sadb_sa_encrypt = sav->alg_enc; 3500105197Ssam p->sadb_sa_flags = sav->flags; 3501105197Ssam 3502105197Ssam return m; 3503105197Ssam} 3504105197Ssam 3505105197Ssam/* 3506105197Ssam * set data into sadb_address. 3507105197Ssam */ 3508105197Ssamstatic struct mbuf * 3509105197Ssamkey_setsadbaddr(exttype, saddr, prefixlen, ul_proto) 3510105197Ssam u_int16_t exttype; 3511105197Ssam const struct sockaddr *saddr; 3512105197Ssam u_int8_t prefixlen; 3513105197Ssam u_int16_t ul_proto; 3514105197Ssam{ 3515105197Ssam struct mbuf *m; 3516105197Ssam struct sadb_address *p; 3517105197Ssam size_t len; 3518105197Ssam 3519105197Ssam len = PFKEY_ALIGN8(sizeof(struct sadb_address)) + 3520105197Ssam PFKEY_ALIGN8(saddr->sa_len); 3521105197Ssam m = key_alloc_mbuf(len); 3522105197Ssam if (!m || m->m_next) { /*XXX*/ 3523105197Ssam if (m) 3524105197Ssam m_freem(m); 3525105197Ssam return NULL; 3526105197Ssam } 3527105197Ssam 3528105197Ssam p = mtod(m, struct sadb_address *); 3529105197Ssam 3530105197Ssam bzero(p, len); 3531105197Ssam p->sadb_address_len = PFKEY_UNIT64(len); 3532105197Ssam p->sadb_address_exttype = exttype; 3533105197Ssam p->sadb_address_proto = ul_proto; 3534105197Ssam if (prefixlen == FULLMASK) { 3535105197Ssam switch (saddr->sa_family) { 3536105197Ssam case AF_INET: 3537105197Ssam prefixlen = sizeof(struct in_addr) << 3; 3538105197Ssam break; 3539105197Ssam case AF_INET6: 3540105197Ssam prefixlen = sizeof(struct in6_addr) << 3; 3541105197Ssam break; 3542105197Ssam default: 3543105197Ssam ; /*XXX*/ 3544105197Ssam } 3545105197Ssam } 3546105197Ssam p->sadb_address_prefixlen = prefixlen; 3547105197Ssam p->sadb_address_reserved = 0; 3548105197Ssam 3549105197Ssam bcopy(saddr, 3550105197Ssam mtod(m, caddr_t) + PFKEY_ALIGN8(sizeof(struct sadb_address)), 3551105197Ssam saddr->sa_len); 3552105197Ssam 3553105197Ssam return m; 3554105197Ssam} 3555105197Ssam 3556105197Ssam/* 3557105197Ssam * set data into sadb_x_sa2. 3558105197Ssam */ 3559105197Ssamstatic struct mbuf * 3560105197Ssamkey_setsadbxsa2(mode, seq, reqid) 3561105197Ssam u_int8_t mode; 3562105197Ssam u_int32_t seq, reqid; 3563105197Ssam{ 3564105197Ssam struct mbuf *m; 3565105197Ssam struct sadb_x_sa2 *p; 3566105197Ssam size_t len; 3567105197Ssam 3568105197Ssam len = PFKEY_ALIGN8(sizeof(struct sadb_x_sa2)); 3569105197Ssam m = key_alloc_mbuf(len); 3570105197Ssam if (!m || m->m_next) { /*XXX*/ 3571105197Ssam if (m) 3572105197Ssam m_freem(m); 3573105197Ssam return NULL; 3574105197Ssam } 3575105197Ssam 3576105197Ssam p = mtod(m, struct sadb_x_sa2 *); 3577105197Ssam 3578105197Ssam bzero(p, len); 3579105197Ssam p->sadb_x_sa2_len = PFKEY_UNIT64(len); 3580105197Ssam p->sadb_x_sa2_exttype = SADB_X_EXT_SA2; 3581105197Ssam p->sadb_x_sa2_mode = mode; 3582105197Ssam p->sadb_x_sa2_reserved1 = 0; 3583105197Ssam p->sadb_x_sa2_reserved2 = 0; 3584105197Ssam p->sadb_x_sa2_sequence = seq; 3585105197Ssam p->sadb_x_sa2_reqid = reqid; 3586105197Ssam 3587105197Ssam return m; 3588105197Ssam} 3589105197Ssam 3590105197Ssam/* 3591105197Ssam * set data into sadb_x_policy 3592105197Ssam */ 3593105197Ssamstatic struct mbuf * 3594105197Ssamkey_setsadbxpolicy(type, dir, id) 3595105197Ssam u_int16_t type; 3596105197Ssam u_int8_t dir; 3597105197Ssam u_int32_t id; 3598105197Ssam{ 3599105197Ssam struct mbuf *m; 3600105197Ssam struct sadb_x_policy *p; 3601105197Ssam size_t len; 3602105197Ssam 3603105197Ssam len = PFKEY_ALIGN8(sizeof(struct sadb_x_policy)); 3604105197Ssam m = key_alloc_mbuf(len); 3605105197Ssam if (!m || m->m_next) { /*XXX*/ 3606105197Ssam if (m) 3607105197Ssam m_freem(m); 3608105197Ssam return NULL; 3609105197Ssam } 3610105197Ssam 3611105197Ssam p = mtod(m, struct sadb_x_policy *); 3612105197Ssam 3613105197Ssam bzero(p, len); 3614105197Ssam p->sadb_x_policy_len = PFKEY_UNIT64(len); 3615105197Ssam p->sadb_x_policy_exttype = SADB_X_EXT_POLICY; 3616105197Ssam p->sadb_x_policy_type = type; 3617105197Ssam p->sadb_x_policy_dir = dir; 3618105197Ssam p->sadb_x_policy_id = id; 3619105197Ssam 3620105197Ssam return m; 3621105197Ssam} 3622105197Ssam 3623105197Ssam/* %%% utilities */ 3624157123Sgnn/* Take a key message (sadb_key) from the socket and turn it into one 3625157123Sgnn * of the kernel's key structures (seckey). 3626157123Sgnn * 3627157123Sgnn * IN: pointer to the src 3628157123Sgnn * OUT: NULL no more memory 3629105197Ssam */ 3630157123Sgnnstruct seckey * 3631157123Sgnnkey_dup_keymsg(const struct sadb_key *src, u_int len, 3632157123Sgnn struct malloc_type *type) 3633105197Ssam{ 3634157123Sgnn struct seckey *dst; 3635157123Sgnn dst = (struct seckey *)malloc(sizeof(struct seckey), type, M_NOWAIT); 3636157123Sgnn if (dst != NULL) { 3637157123Sgnn dst->bits = src->sadb_key_bits; 3638157123Sgnn dst->key_data = (char *)malloc(len, type, M_NOWAIT); 3639157123Sgnn if (dst->key_data != NULL) { 3640157123Sgnn bcopy((const char *)src + sizeof(struct sadb_key), 3641157123Sgnn dst->key_data, len); 3642157123Sgnn } else { 3643157123Sgnn ipseclog((LOG_DEBUG, "%s: No more memory.\n", 3644157123Sgnn __func__)); 3645157123Sgnn free(dst, type); 3646157123Sgnn dst = NULL; 3647157123Sgnn } 3648157123Sgnn } else { 3649157123Sgnn ipseclog((LOG_DEBUG, "%s: No more memory.\n", 3650157123Sgnn __func__)); 3651105197Ssam 3652157123Sgnn } 3653157123Sgnn return dst; 3654157123Sgnn} 3655157123Sgnn 3656157123Sgnn/* Take a lifetime message (sadb_lifetime) passed in on a socket and 3657157123Sgnn * turn it into one of the kernel's lifetime structures (seclifetime). 3658157123Sgnn * 3659157123Sgnn * IN: pointer to the destination, source and malloc type 3660157123Sgnn * OUT: NULL, no more memory 3661157123Sgnn */ 3662157123Sgnn 3663157123Sgnnstatic struct seclifetime * 3664157123Sgnnkey_dup_lifemsg(const struct sadb_lifetime *src, 3665157123Sgnn struct malloc_type *type) 3666157123Sgnn{ 3667157123Sgnn struct seclifetime *dst = NULL; 3668157123Sgnn 3669157123Sgnn dst = (struct seclifetime *)malloc(sizeof(struct seclifetime), 3670157123Sgnn type, M_NOWAIT); 3671157123Sgnn if (dst == NULL) { 3672119643Ssam /* XXX counter */ 3673120585Ssam ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__)); 3674157123Sgnn } else { 3675157123Sgnn dst->allocations = src->sadb_lifetime_allocations; 3676157123Sgnn dst->bytes = src->sadb_lifetime_bytes; 3677157123Sgnn dst->addtime = src->sadb_lifetime_addtime; 3678157123Sgnn dst->usetime = src->sadb_lifetime_usetime; 3679157123Sgnn } 3680157123Sgnn return dst; 3681105197Ssam} 3682105197Ssam 3683105197Ssam/* compare my own address 3684105197Ssam * OUT: 1: true, i.e. my address. 3685105197Ssam * 0: false 3686105197Ssam */ 3687105197Ssamint 3688105197Ssamkey_ismyaddr(sa) 3689105197Ssam struct sockaddr *sa; 3690105197Ssam{ 3691105197Ssam#ifdef INET 3692105197Ssam struct sockaddr_in *sin; 3693105197Ssam struct in_ifaddr *ia; 3694105197Ssam#endif 3695105197Ssam 3696120585Ssam IPSEC_ASSERT(sa != NULL, ("null sockaddr")); 3697105197Ssam 3698105197Ssam switch (sa->sa_family) { 3699105197Ssam#ifdef INET 3700105197Ssam case AF_INET: 3701105197Ssam sin = (struct sockaddr_in *)sa; 3702105197Ssam for (ia = in_ifaddrhead.tqh_first; ia; 3703105197Ssam ia = ia->ia_link.tqe_next) 3704105197Ssam { 3705105197Ssam if (sin->sin_family == ia->ia_addr.sin_family && 3706105197Ssam sin->sin_len == ia->ia_addr.sin_len && 3707105197Ssam sin->sin_addr.s_addr == ia->ia_addr.sin_addr.s_addr) 3708105197Ssam { 3709105197Ssam return 1; 3710105197Ssam } 3711105197Ssam } 3712105197Ssam break; 3713105197Ssam#endif 3714105197Ssam#ifdef INET6 3715105197Ssam case AF_INET6: 3716105197Ssam return key_ismyaddr6((struct sockaddr_in6 *)sa); 3717105197Ssam#endif 3718105197Ssam } 3719105197Ssam 3720105197Ssam return 0; 3721105197Ssam} 3722105197Ssam 3723105197Ssam#ifdef INET6 3724105197Ssam/* 3725105197Ssam * compare my own address for IPv6. 3726105197Ssam * 1: ours 3727105197Ssam * 0: other 3728105197Ssam * NOTE: derived ip6_input() in KAME. This is necessary to modify more. 3729105197Ssam */ 3730105197Ssam#include <netinet6/in6_var.h> 3731105197Ssam 3732105197Ssamstatic int 3733105197Ssamkey_ismyaddr6(sin6) 3734105197Ssam struct sockaddr_in6 *sin6; 3735105197Ssam{ 3736105197Ssam struct in6_ifaddr *ia; 3737105197Ssam struct in6_multi *in6m; 3738105197Ssam 3739105197Ssam for (ia = in6_ifaddr; ia; ia = ia->ia_next) { 3740105197Ssam if (key_sockaddrcmp((struct sockaddr *)&sin6, 3741105197Ssam (struct sockaddr *)&ia->ia_addr, 0) == 0) 3742105197Ssam return 1; 3743105197Ssam 3744105197Ssam /* 3745105197Ssam * XXX Multicast 3746105197Ssam * XXX why do we care about multlicast here while we don't care 3747105197Ssam * about IPv4 multicast?? 3748105197Ssam * XXX scope 3749105197Ssam */ 3750105197Ssam in6m = NULL; 3751105197Ssam IN6_LOOKUP_MULTI(sin6->sin6_addr, ia->ia_ifp, in6m); 3752105197Ssam if (in6m) 3753105197Ssam return 1; 3754105197Ssam } 3755105197Ssam 3756105197Ssam /* loopback, just for safety */ 3757105197Ssam if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr)) 3758105197Ssam return 1; 3759105197Ssam 3760105197Ssam return 0; 3761105197Ssam} 3762105197Ssam#endif /*INET6*/ 3763105197Ssam 3764105197Ssam/* 3765105197Ssam * compare two secasindex structure. 3766105197Ssam * flag can specify to compare 2 saidxes. 3767105197Ssam * compare two secasindex structure without both mode and reqid. 3768105197Ssam * don't compare port. 3769105197Ssam * IN: 3770105197Ssam * saidx0: source, it can be in SAD. 3771105197Ssam * saidx1: object. 3772105197Ssam * OUT: 3773105197Ssam * 1 : equal 3774105197Ssam * 0 : not equal 3775105197Ssam */ 3776105197Ssamstatic int 3777105197Ssamkey_cmpsaidx( 3778105197Ssam const struct secasindex *saidx0, 3779105197Ssam const struct secasindex *saidx1, 3780105197Ssam int flag) 3781105197Ssam{ 3782105197Ssam /* sanity */ 3783105197Ssam if (saidx0 == NULL && saidx1 == NULL) 3784105197Ssam return 1; 3785105197Ssam 3786105197Ssam if (saidx0 == NULL || saidx1 == NULL) 3787105197Ssam return 0; 3788105197Ssam 3789105197Ssam if (saidx0->proto != saidx1->proto) 3790105197Ssam return 0; 3791105197Ssam 3792105197Ssam if (flag == CMP_EXACTLY) { 3793105197Ssam if (saidx0->mode != saidx1->mode) 3794105197Ssam return 0; 3795105197Ssam if (saidx0->reqid != saidx1->reqid) 3796105197Ssam return 0; 3797105197Ssam if (bcmp(&saidx0->src, &saidx1->src, saidx0->src.sa.sa_len) != 0 || 3798105197Ssam bcmp(&saidx0->dst, &saidx1->dst, saidx0->dst.sa.sa_len) != 0) 3799105197Ssam return 0; 3800105197Ssam } else { 3801105197Ssam 3802105197Ssam /* CMP_MODE_REQID, CMP_REQID, CMP_HEAD */ 3803105197Ssam if (flag == CMP_MODE_REQID 3804105197Ssam ||flag == CMP_REQID) { 3805105197Ssam /* 3806105197Ssam * If reqid of SPD is non-zero, unique SA is required. 3807105197Ssam * The result must be of same reqid in this case. 3808105197Ssam */ 3809105197Ssam if (saidx1->reqid != 0 && saidx0->reqid != saidx1->reqid) 3810105197Ssam return 0; 3811105197Ssam } 3812105197Ssam 3813105197Ssam if (flag == CMP_MODE_REQID) { 3814105197Ssam if (saidx0->mode != IPSEC_MODE_ANY 3815105197Ssam && saidx0->mode != saidx1->mode) 3816105197Ssam return 0; 3817105197Ssam } 3818105197Ssam 3819105197Ssam if (key_sockaddrcmp(&saidx0->src.sa, &saidx1->src.sa, 0) != 0) { 3820105197Ssam return 0; 3821105197Ssam } 3822105197Ssam if (key_sockaddrcmp(&saidx0->dst.sa, &saidx1->dst.sa, 0) != 0) { 3823105197Ssam return 0; 3824105197Ssam } 3825105197Ssam } 3826105197Ssam 3827105197Ssam return 1; 3828105197Ssam} 3829105197Ssam 3830105197Ssam/* 3831105197Ssam * compare two secindex structure exactly. 3832105197Ssam * IN: 3833105197Ssam * spidx0: source, it is often in SPD. 3834105197Ssam * spidx1: object, it is often from PFKEY message. 3835105197Ssam * OUT: 3836105197Ssam * 1 : equal 3837105197Ssam * 0 : not equal 3838105197Ssam */ 3839105197Ssamstatic int 3840105197Ssamkey_cmpspidx_exactly( 3841105197Ssam struct secpolicyindex *spidx0, 3842105197Ssam struct secpolicyindex *spidx1) 3843105197Ssam{ 3844105197Ssam /* sanity */ 3845105197Ssam if (spidx0 == NULL && spidx1 == NULL) 3846105197Ssam return 1; 3847105197Ssam 3848105197Ssam if (spidx0 == NULL || spidx1 == NULL) 3849105197Ssam return 0; 3850105197Ssam 3851105197Ssam if (spidx0->prefs != spidx1->prefs 3852105197Ssam || spidx0->prefd != spidx1->prefd 3853105197Ssam || spidx0->ul_proto != spidx1->ul_proto) 3854105197Ssam return 0; 3855105197Ssam 3856105197Ssam return key_sockaddrcmp(&spidx0->src.sa, &spidx1->src.sa, 1) == 0 && 3857105197Ssam key_sockaddrcmp(&spidx0->dst.sa, &spidx1->dst.sa, 1) == 0; 3858105197Ssam} 3859105197Ssam 3860105197Ssam/* 3861105197Ssam * compare two secindex structure with mask. 3862105197Ssam * IN: 3863105197Ssam * spidx0: source, it is often in SPD. 3864105197Ssam * spidx1: object, it is often from IP header. 3865105197Ssam * OUT: 3866105197Ssam * 1 : equal 3867105197Ssam * 0 : not equal 3868105197Ssam */ 3869105197Ssamstatic int 3870105197Ssamkey_cmpspidx_withmask( 3871105197Ssam struct secpolicyindex *spidx0, 3872105197Ssam struct secpolicyindex *spidx1) 3873105197Ssam{ 3874105197Ssam /* sanity */ 3875105197Ssam if (spidx0 == NULL && spidx1 == NULL) 3876105197Ssam return 1; 3877105197Ssam 3878105197Ssam if (spidx0 == NULL || spidx1 == NULL) 3879105197Ssam return 0; 3880105197Ssam 3881105197Ssam if (spidx0->src.sa.sa_family != spidx1->src.sa.sa_family || 3882105197Ssam spidx0->dst.sa.sa_family != spidx1->dst.sa.sa_family || 3883105197Ssam spidx0->src.sa.sa_len != spidx1->src.sa.sa_len || 3884105197Ssam spidx0->dst.sa.sa_len != spidx1->dst.sa.sa_len) 3885105197Ssam return 0; 3886105197Ssam 3887105197Ssam /* if spidx.ul_proto == IPSEC_ULPROTO_ANY, ignore. */ 3888105197Ssam if (spidx0->ul_proto != (u_int16_t)IPSEC_ULPROTO_ANY 3889105197Ssam && spidx0->ul_proto != spidx1->ul_proto) 3890105197Ssam return 0; 3891105197Ssam 3892105197Ssam switch (spidx0->src.sa.sa_family) { 3893105197Ssam case AF_INET: 3894105197Ssam if (spidx0->src.sin.sin_port != IPSEC_PORT_ANY 3895105197Ssam && spidx0->src.sin.sin_port != spidx1->src.sin.sin_port) 3896105197Ssam return 0; 3897105197Ssam if (!key_bbcmp(&spidx0->src.sin.sin_addr, 3898105197Ssam &spidx1->src.sin.sin_addr, spidx0->prefs)) 3899105197Ssam return 0; 3900105197Ssam break; 3901105197Ssam case AF_INET6: 3902105197Ssam if (spidx0->src.sin6.sin6_port != IPSEC_PORT_ANY 3903105197Ssam && spidx0->src.sin6.sin6_port != spidx1->src.sin6.sin6_port) 3904105197Ssam return 0; 3905105197Ssam /* 3906105197Ssam * scope_id check. if sin6_scope_id is 0, we regard it 3907105197Ssam * as a wildcard scope, which matches any scope zone ID. 3908105197Ssam */ 3909105197Ssam if (spidx0->src.sin6.sin6_scope_id && 3910105197Ssam spidx1->src.sin6.sin6_scope_id && 3911105197Ssam spidx0->src.sin6.sin6_scope_id != spidx1->src.sin6.sin6_scope_id) 3912105197Ssam return 0; 3913105197Ssam if (!key_bbcmp(&spidx0->src.sin6.sin6_addr, 3914105197Ssam &spidx1->src.sin6.sin6_addr, spidx0->prefs)) 3915105197Ssam return 0; 3916105197Ssam break; 3917105197Ssam default: 3918105197Ssam /* XXX */ 3919105197Ssam if (bcmp(&spidx0->src, &spidx1->src, spidx0->src.sa.sa_len) != 0) 3920105197Ssam return 0; 3921105197Ssam break; 3922105197Ssam } 3923105197Ssam 3924105197Ssam switch (spidx0->dst.sa.sa_family) { 3925105197Ssam case AF_INET: 3926105197Ssam if (spidx0->dst.sin.sin_port != IPSEC_PORT_ANY 3927105197Ssam && spidx0->dst.sin.sin_port != spidx1->dst.sin.sin_port) 3928105197Ssam return 0; 3929105197Ssam if (!key_bbcmp(&spidx0->dst.sin.sin_addr, 3930105197Ssam &spidx1->dst.sin.sin_addr, spidx0->prefd)) 3931105197Ssam return 0; 3932105197Ssam break; 3933105197Ssam case AF_INET6: 3934105197Ssam if (spidx0->dst.sin6.sin6_port != IPSEC_PORT_ANY 3935105197Ssam && spidx0->dst.sin6.sin6_port != spidx1->dst.sin6.sin6_port) 3936105197Ssam return 0; 3937105197Ssam /* 3938105197Ssam * scope_id check. if sin6_scope_id is 0, we regard it 3939105197Ssam * as a wildcard scope, which matches any scope zone ID. 3940105197Ssam */ 3941130928Sbms if (spidx0->dst.sin6.sin6_scope_id && 3942130928Sbms spidx1->dst.sin6.sin6_scope_id && 3943105197Ssam spidx0->dst.sin6.sin6_scope_id != spidx1->dst.sin6.sin6_scope_id) 3944105197Ssam return 0; 3945105197Ssam if (!key_bbcmp(&spidx0->dst.sin6.sin6_addr, 3946105197Ssam &spidx1->dst.sin6.sin6_addr, spidx0->prefd)) 3947105197Ssam return 0; 3948105197Ssam break; 3949105197Ssam default: 3950105197Ssam /* XXX */ 3951105197Ssam if (bcmp(&spidx0->dst, &spidx1->dst, spidx0->dst.sa.sa_len) != 0) 3952105197Ssam return 0; 3953105197Ssam break; 3954105197Ssam } 3955105197Ssam 3956105197Ssam /* XXX Do we check other field ? e.g. flowinfo */ 3957105197Ssam 3958105197Ssam return 1; 3959105197Ssam} 3960105197Ssam 3961105197Ssam/* returns 0 on match */ 3962105197Ssamstatic int 3963105197Ssamkey_sockaddrcmp( 3964105197Ssam const struct sockaddr *sa1, 3965105197Ssam const struct sockaddr *sa2, 3966105197Ssam int port) 3967105197Ssam{ 3968105197Ssam#ifdef satosin 3969105197Ssam#undef satosin 3970105197Ssam#endif 3971105197Ssam#define satosin(s) ((const struct sockaddr_in *)s) 3972105197Ssam#ifdef satosin6 3973105197Ssam#undef satosin6 3974105197Ssam#endif 3975105197Ssam#define satosin6(s) ((const struct sockaddr_in6 *)s) 3976105197Ssam if (sa1->sa_family != sa2->sa_family || sa1->sa_len != sa2->sa_len) 3977105197Ssam return 1; 3978105197Ssam 3979105197Ssam switch (sa1->sa_family) { 3980105197Ssam case AF_INET: 3981105197Ssam if (sa1->sa_len != sizeof(struct sockaddr_in)) 3982105197Ssam return 1; 3983105197Ssam if (satosin(sa1)->sin_addr.s_addr != 3984105197Ssam satosin(sa2)->sin_addr.s_addr) { 3985105197Ssam return 1; 3986105197Ssam } 3987105197Ssam if (port && satosin(sa1)->sin_port != satosin(sa2)->sin_port) 3988105197Ssam return 1; 3989105197Ssam break; 3990105197Ssam case AF_INET6: 3991105197Ssam if (sa1->sa_len != sizeof(struct sockaddr_in6)) 3992105197Ssam return 1; /*EINVAL*/ 3993105197Ssam if (satosin6(sa1)->sin6_scope_id != 3994105197Ssam satosin6(sa2)->sin6_scope_id) { 3995105197Ssam return 1; 3996105197Ssam } 3997105197Ssam if (!IN6_ARE_ADDR_EQUAL(&satosin6(sa1)->sin6_addr, 3998105197Ssam &satosin6(sa2)->sin6_addr)) { 3999105197Ssam return 1; 4000105197Ssam } 4001105197Ssam if (port && 4002105197Ssam satosin6(sa1)->sin6_port != satosin6(sa2)->sin6_port) { 4003105197Ssam return 1; 4004105197Ssam } 4005170120Sbz break; 4006105197Ssam default: 4007105197Ssam if (bcmp(sa1, sa2, sa1->sa_len) != 0) 4008105197Ssam return 1; 4009105197Ssam break; 4010105197Ssam } 4011105197Ssam 4012105197Ssam return 0; 4013105197Ssam#undef satosin 4014105197Ssam#undef satosin6 4015105197Ssam} 4016105197Ssam 4017105197Ssam/* 4018105197Ssam * compare two buffers with mask. 4019105197Ssam * IN: 4020105197Ssam * addr1: source 4021105197Ssam * addr2: object 4022105197Ssam * bits: Number of bits to compare 4023105197Ssam * OUT: 4024105197Ssam * 1 : equal 4025105197Ssam * 0 : not equal 4026105197Ssam */ 4027105197Ssamstatic int 4028105197Ssamkey_bbcmp(const void *a1, const void *a2, u_int bits) 4029105197Ssam{ 4030105197Ssam const unsigned char *p1 = a1; 4031105197Ssam const unsigned char *p2 = a2; 4032105197Ssam 4033105197Ssam /* XXX: This could be considerably faster if we compare a word 4034105197Ssam * at a time, but it is complicated on LSB Endian machines */ 4035105197Ssam 4036105197Ssam /* Handle null pointers */ 4037105197Ssam if (p1 == NULL || p2 == NULL) 4038105197Ssam return (p1 == p2); 4039105197Ssam 4040105197Ssam while (bits >= 8) { 4041105197Ssam if (*p1++ != *p2++) 4042105197Ssam return 0; 4043105197Ssam bits -= 8; 4044105197Ssam } 4045105197Ssam 4046105197Ssam if (bits > 0) { 4047105197Ssam u_int8_t mask = ~((1<<(8-bits))-1); 4048105197Ssam if ((*p1 & mask) != (*p2 & mask)) 4049105197Ssam return 0; 4050105197Ssam } 4051105197Ssam return 1; /* Match! */ 4052105197Ssam} 4053105197Ssam 4054119643Ssamstatic void 4055119643Ssamkey_flush_spd(time_t now) 4056105197Ssam{ 4057120585Ssam static u_int16_t sptree_scangen = 0; 4058120585Ssam u_int16_t gen = sptree_scangen++; 4059120585Ssam struct secpolicy *sp; 4060105197Ssam u_int dir; 4061105197Ssam 4062105197Ssam /* SPD */ 4063105197Ssam for (dir = 0; dir < IPSEC_DIR_MAX; dir++) { 4064120585Ssamrestart: 4065120585Ssam SPTREE_LOCK(); 4066120585Ssam LIST_FOREACH(sp, &sptree[dir], chain) { 4067120585Ssam if (sp->scangen == gen) /* previously handled */ 4068120585Ssam continue; 4069120585Ssam sp->scangen = gen; 4070105197Ssam if (sp->state == IPSEC_SPSTATE_DEAD) { 4071120585Ssam /* NB: clean entries created by key_spdflush */ 4072120585Ssam SPTREE_UNLOCK(); 4073105197Ssam KEY_FREESP(&sp); 4074120585Ssam goto restart; 4075105197Ssam } 4076105197Ssam if (sp->lifetime == 0 && sp->validtime == 0) 4077105197Ssam continue; 4078105197Ssam if ((sp->lifetime && now - sp->created > sp->lifetime) 4079105197Ssam || (sp->validtime && now - sp->lastused > sp->validtime)) { 4080105197Ssam sp->state = IPSEC_SPSTATE_DEAD; 4081120585Ssam SPTREE_UNLOCK(); 4082105197Ssam key_spdexpire(sp); 4083120585Ssam KEY_FREESP(&sp); 4084120585Ssam goto restart; 4085105197Ssam } 4086105197Ssam } 4087120585Ssam SPTREE_UNLOCK(); 4088105197Ssam } 4089119643Ssam} 4090105197Ssam 4091119643Ssamstatic void 4092119643Ssamkey_flush_sad(time_t now) 4093119643Ssam{ 4094105197Ssam struct secashead *sah, *nextsah; 4095105197Ssam struct secasvar *sav, *nextsav; 4096105197Ssam 4097119643Ssam /* SAD */ 4098120585Ssam SAHTREE_LOCK(); 4099120585Ssam LIST_FOREACH_SAFE(sah, &sahtree, chain, nextsah) { 4100105197Ssam /* if sah has been dead, then delete it and process next sah. */ 4101105197Ssam if (sah->state == SADB_SASTATE_DEAD) { 4102105197Ssam key_delsah(sah); 4103105197Ssam continue; 4104105197Ssam } 4105105197Ssam 4106105197Ssam /* if LARVAL entry doesn't become MATURE, delete it. */ 4107120585Ssam LIST_FOREACH_SAFE(sav, &sah->savtree[SADB_SASTATE_LARVAL], chain, nextsav) { 4108120585Ssam if (now - sav->created > key_larval_lifetime) 4109105197Ssam KEY_FREESAV(&sav); 4110105197Ssam } 4111105197Ssam 4112105197Ssam /* 4113105197Ssam * check MATURE entry to start to send expire message 4114105197Ssam * whether or not. 4115105197Ssam */ 4116120585Ssam LIST_FOREACH_SAFE(sav, &sah->savtree[SADB_SASTATE_MATURE], chain, nextsav) { 4117105197Ssam /* we don't need to check. */ 4118105197Ssam if (sav->lft_s == NULL) 4119105197Ssam continue; 4120105197Ssam 4121105197Ssam /* sanity check */ 4122105197Ssam if (sav->lft_c == NULL) { 4123120585Ssam ipseclog((LOG_DEBUG,"%s: there is no CURRENT " 4124120585Ssam "time, why?\n", __func__)); 4125105197Ssam continue; 4126105197Ssam } 4127105197Ssam 4128105197Ssam /* check SOFT lifetime */ 4129157123Sgnn if (sav->lft_s->addtime != 0 && 4130157123Sgnn now - sav->created > sav->lft_s->addtime) { 4131105197Ssam /* 4132105197Ssam * check SA to be used whether or not. 4133105197Ssam * when SA hasn't been used, delete it. 4134105197Ssam */ 4135157123Sgnn if (sav->lft_c->usetime == 0) { 4136105197Ssam key_sa_chgstate(sav, SADB_SASTATE_DEAD); 4137105197Ssam KEY_FREESAV(&sav); 4138105197Ssam } else { 4139105197Ssam key_sa_chgstate(sav, SADB_SASTATE_DYING); 4140105197Ssam /* 4141105197Ssam * XXX If we keep to send expire 4142105197Ssam * message in the status of 4143105197Ssam * DYING. Do remove below code. 4144105197Ssam */ 4145105197Ssam key_expire(sav); 4146105197Ssam } 4147105197Ssam } 4148105197Ssam /* check SOFT lifetime by bytes */ 4149105197Ssam /* 4150105197Ssam * XXX I don't know the way to delete this SA 4151105197Ssam * when new SA is installed. Caution when it's 4152105197Ssam * installed too big lifetime by time. 4153105197Ssam */ 4154157123Sgnn else if (sav->lft_s->bytes != 0 && 4155157123Sgnn sav->lft_s->bytes < sav->lft_c->bytes) { 4156105197Ssam 4157105197Ssam key_sa_chgstate(sav, SADB_SASTATE_DYING); 4158105197Ssam /* 4159105197Ssam * XXX If we keep to send expire 4160105197Ssam * message in the status of 4161105197Ssam * DYING. Do remove below code. 4162105197Ssam */ 4163105197Ssam key_expire(sav); 4164105197Ssam } 4165105197Ssam } 4166105197Ssam 4167105197Ssam /* check DYING entry to change status to DEAD. */ 4168120585Ssam LIST_FOREACH_SAFE(sav, &sah->savtree[SADB_SASTATE_DYING], chain, nextsav) { 4169105197Ssam /* we don't need to check. */ 4170105197Ssam if (sav->lft_h == NULL) 4171105197Ssam continue; 4172105197Ssam 4173105197Ssam /* sanity check */ 4174105197Ssam if (sav->lft_c == NULL) { 4175120585Ssam ipseclog((LOG_DEBUG, "%s: there is no CURRENT " 4176120585Ssam "time, why?\n", __func__)); 4177105197Ssam continue; 4178105197Ssam } 4179105197Ssam 4180157123Sgnn if (sav->lft_h->addtime != 0 && 4181157123Sgnn now - sav->created > sav->lft_h->addtime) { 4182105197Ssam key_sa_chgstate(sav, SADB_SASTATE_DEAD); 4183105197Ssam KEY_FREESAV(&sav); 4184105197Ssam } 4185105197Ssam#if 0 /* XXX Should we keep to send expire message until HARD lifetime ? */ 4186105197Ssam else if (sav->lft_s != NULL 4187157123Sgnn && sav->lft_s->addtime != 0 4188157123Sgnn && now - sav->created > sav->lft_s->addtime) { 4189105197Ssam /* 4190105197Ssam * XXX: should be checked to be 4191105197Ssam * installed the valid SA. 4192105197Ssam */ 4193105197Ssam 4194105197Ssam /* 4195105197Ssam * If there is no SA then sending 4196105197Ssam * expire message. 4197105197Ssam */ 4198105197Ssam key_expire(sav); 4199105197Ssam } 4200105197Ssam#endif 4201105197Ssam /* check HARD lifetime by bytes */ 4202157123Sgnn else if (sav->lft_h->bytes != 0 && 4203157123Sgnn sav->lft_h->bytes < sav->lft_c->bytes) { 4204105197Ssam key_sa_chgstate(sav, SADB_SASTATE_DEAD); 4205105197Ssam KEY_FREESAV(&sav); 4206105197Ssam } 4207105197Ssam } 4208105197Ssam 4209105197Ssam /* delete entry in DEAD */ 4210120585Ssam LIST_FOREACH_SAFE(sav, &sah->savtree[SADB_SASTATE_DEAD], chain, nextsav) { 4211105197Ssam /* sanity check */ 4212105197Ssam if (sav->state != SADB_SASTATE_DEAD) { 4213120585Ssam ipseclog((LOG_DEBUG, "%s: invalid sav->state " 4214120585Ssam "(queue: %d SA: %d): kill it anyway\n", 4215120585Ssam __func__, 4216105197Ssam SADB_SASTATE_DEAD, sav->state)); 4217105197Ssam } 4218105197Ssam /* 4219105197Ssam * do not call key_freesav() here. 4220105197Ssam * sav should already be freed, and sav->refcnt 4221105197Ssam * shows other references to sav 4222105197Ssam * (such as from SPD). 4223105197Ssam */ 4224105197Ssam } 4225105197Ssam } 4226120585Ssam SAHTREE_UNLOCK(); 4227119643Ssam} 4228105197Ssam 4229119643Ssamstatic void 4230119643Ssamkey_flush_acq(time_t now) 4231119643Ssam{ 4232105197Ssam struct secacq *acq, *nextacq; 4233105197Ssam 4234119643Ssam /* ACQ tree */ 4235120585Ssam ACQ_LOCK(); 4236119643Ssam for (acq = LIST_FIRST(&acqtree); acq != NULL; acq = nextacq) { 4237105197Ssam nextacq = LIST_NEXT(acq, chain); 4238105197Ssam if (now - acq->created > key_blockacq_lifetime 4239105197Ssam && __LIST_CHAINED(acq)) { 4240105197Ssam LIST_REMOVE(acq, chain); 4241119643Ssam free(acq, M_IPSEC_SAQ); 4242105197Ssam } 4243105197Ssam } 4244120585Ssam ACQ_UNLOCK(); 4245119643Ssam} 4246105197Ssam 4247119643Ssamstatic void 4248119643Ssamkey_flush_spacq(time_t now) 4249119643Ssam{ 4250105197Ssam struct secspacq *acq, *nextacq; 4251105197Ssam 4252119643Ssam /* SP ACQ tree */ 4253120585Ssam SPACQ_LOCK(); 4254119643Ssam for (acq = LIST_FIRST(&spacqtree); acq != NULL; acq = nextacq) { 4255105197Ssam nextacq = LIST_NEXT(acq, chain); 4256105197Ssam if (now - acq->created > key_blockacq_lifetime 4257105197Ssam && __LIST_CHAINED(acq)) { 4258105197Ssam LIST_REMOVE(acq, chain); 4259119643Ssam free(acq, M_IPSEC_SAQ); 4260105197Ssam } 4261105197Ssam } 4262120585Ssam SPACQ_UNLOCK(); 4263119643Ssam} 4264105197Ssam 4265119643Ssam/* 4266119643Ssam * time handler. 4267119643Ssam * scanning SPD and SAD to check status for each entries, 4268119643Ssam * and do to remove or to expire. 4269119643Ssam * XXX: year 2038 problem may remain. 4270119643Ssam */ 4271119643Ssamvoid 4272119643Ssamkey_timehandler(void) 4273119643Ssam{ 4274119643Ssam time_t now = time_second; 4275105197Ssam 4276119643Ssam key_flush_spd(now); 4277119643Ssam key_flush_sad(now); 4278119643Ssam key_flush_acq(now); 4279119643Ssam key_flush_spacq(now); 4280119643Ssam 4281105197Ssam#ifndef IPSEC_DEBUG2 4282105197Ssam /* do exchange to tick time !! */ 4283105197Ssam (void)timeout((void *)key_timehandler, (void *)0, hz); 4284105197Ssam#endif /* IPSEC_DEBUG2 */ 4285105197Ssam} 4286105197Ssam 4287105197Ssamu_long 4288105197Ssamkey_random() 4289105197Ssam{ 4290105197Ssam u_long value; 4291105197Ssam 4292105197Ssam key_randomfill(&value, sizeof(value)); 4293105197Ssam return value; 4294105197Ssam} 4295105197Ssam 4296105197Ssamvoid 4297105197Ssamkey_randomfill(p, l) 4298105197Ssam void *p; 4299105197Ssam size_t l; 4300105197Ssam{ 4301105197Ssam size_t n; 4302105197Ssam u_long v; 4303105197Ssam static int warn = 1; 4304105197Ssam 4305105197Ssam n = 0; 4306105197Ssam n = (size_t)read_random(p, (u_int)l); 4307105197Ssam /* last resort */ 4308105197Ssam while (n < l) { 4309105197Ssam v = random(); 4310105197Ssam bcopy(&v, (u_int8_t *)p + n, 4311105197Ssam l - n < sizeof(v) ? l - n : sizeof(v)); 4312105197Ssam n += sizeof(v); 4313105197Ssam 4314105197Ssam if (warn) { 4315105197Ssam printf("WARNING: pseudo-random number generator " 4316105197Ssam "used for IPsec processing\n"); 4317105197Ssam warn = 0; 4318105197Ssam } 4319105197Ssam } 4320105197Ssam} 4321105197Ssam 4322105197Ssam/* 4323105197Ssam * map SADB_SATYPE_* to IPPROTO_*. 4324105197Ssam * if satype == SADB_SATYPE then satype is mapped to ~0. 4325105197Ssam * OUT: 4326105197Ssam * 0: invalid satype. 4327105197Ssam */ 4328105197Ssamstatic u_int16_t 4329105197Ssamkey_satype2proto(satype) 4330105197Ssam u_int8_t satype; 4331105197Ssam{ 4332105197Ssam switch (satype) { 4333105197Ssam case SADB_SATYPE_UNSPEC: 4334105197Ssam return IPSEC_PROTO_ANY; 4335105197Ssam case SADB_SATYPE_AH: 4336105197Ssam return IPPROTO_AH; 4337105197Ssam case SADB_SATYPE_ESP: 4338105197Ssam return IPPROTO_ESP; 4339105197Ssam case SADB_X_SATYPE_IPCOMP: 4340105197Ssam return IPPROTO_IPCOMP; 4341125680Sbms case SADB_X_SATYPE_TCPSIGNATURE: 4342125680Sbms return IPPROTO_TCP; 4343105197Ssam default: 4344105197Ssam return 0; 4345105197Ssam } 4346105197Ssam /* NOTREACHED */ 4347105197Ssam} 4348105197Ssam 4349105197Ssam/* 4350105197Ssam * map IPPROTO_* to SADB_SATYPE_* 4351105197Ssam * OUT: 4352105197Ssam * 0: invalid protocol type. 4353105197Ssam */ 4354105197Ssamstatic u_int8_t 4355105197Ssamkey_proto2satype(proto) 4356105197Ssam u_int16_t proto; 4357105197Ssam{ 4358105197Ssam switch (proto) { 4359105197Ssam case IPPROTO_AH: 4360105197Ssam return SADB_SATYPE_AH; 4361105197Ssam case IPPROTO_ESP: 4362105197Ssam return SADB_SATYPE_ESP; 4363105197Ssam case IPPROTO_IPCOMP: 4364105197Ssam return SADB_X_SATYPE_IPCOMP; 4365125680Sbms case IPPROTO_TCP: 4366125680Sbms return SADB_X_SATYPE_TCPSIGNATURE; 4367105197Ssam default: 4368105197Ssam return 0; 4369105197Ssam } 4370105197Ssam /* NOTREACHED */ 4371105197Ssam} 4372105197Ssam 4373105197Ssam/* %%% PF_KEY */ 4374105197Ssam/* 4375105197Ssam * SADB_GETSPI processing is to receive 4376105197Ssam * <base, (SA2), src address, dst address, (SPI range)> 4377105197Ssam * from the IKMPd, to assign a unique spi value, to hang on the INBOUND 4378105197Ssam * tree with the status of LARVAL, and send 4379105197Ssam * <base, SA(*), address(SD)> 4380105197Ssam * to the IKMPd. 4381105197Ssam * 4382105197Ssam * IN: mhp: pointer to the pointer to each header. 4383105197Ssam * OUT: NULL if fail. 4384105197Ssam * other if success, return pointer to the message to send. 4385105197Ssam */ 4386105197Ssamstatic int 4387105197Ssamkey_getspi(so, m, mhp) 4388105197Ssam struct socket *so; 4389105197Ssam struct mbuf *m; 4390105197Ssam const struct sadb_msghdr *mhp; 4391105197Ssam{ 4392105197Ssam struct sadb_address *src0, *dst0; 4393105197Ssam struct secasindex saidx; 4394105197Ssam struct secashead *newsah; 4395105197Ssam struct secasvar *newsav; 4396105197Ssam u_int8_t proto; 4397105197Ssam u_int32_t spi; 4398105197Ssam u_int8_t mode; 4399105197Ssam u_int32_t reqid; 4400105197Ssam int error; 4401105197Ssam 4402120585Ssam IPSEC_ASSERT(so != NULL, ("null socket")); 4403120585Ssam IPSEC_ASSERT(m != NULL, ("null mbuf")); 4404120585Ssam IPSEC_ASSERT(mhp != NULL, ("null msghdr")); 4405120585Ssam IPSEC_ASSERT(mhp->msg != NULL, ("null msg")); 4406105197Ssam 4407105197Ssam if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL || 4408105197Ssam mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) { 4409120585Ssam ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n", 4410120585Ssam __func__)); 4411105197Ssam return key_senderror(so, m, EINVAL); 4412105197Ssam } 4413105197Ssam if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) || 4414105197Ssam mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) { 4415120585Ssam ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n", 4416120585Ssam __func__)); 4417105197Ssam return key_senderror(so, m, EINVAL); 4418105197Ssam } 4419105197Ssam if (mhp->ext[SADB_X_EXT_SA2] != NULL) { 4420105197Ssam mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode; 4421105197Ssam reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid; 4422105197Ssam } else { 4423105197Ssam mode = IPSEC_MODE_ANY; 4424105197Ssam reqid = 0; 4425105197Ssam } 4426105197Ssam 4427105197Ssam src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]); 4428105197Ssam dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]); 4429105197Ssam 4430105197Ssam /* map satype to proto */ 4431105197Ssam if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) { 4432120585Ssam ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n", 4433120585Ssam __func__)); 4434105197Ssam return key_senderror(so, m, EINVAL); 4435105197Ssam } 4436105197Ssam 4437105197Ssam /* make sure if port number is zero. */ 4438105197Ssam switch (((struct sockaddr *)(src0 + 1))->sa_family) { 4439105197Ssam case AF_INET: 4440105197Ssam if (((struct sockaddr *)(src0 + 1))->sa_len != 4441105197Ssam sizeof(struct sockaddr_in)) 4442105197Ssam return key_senderror(so, m, EINVAL); 4443105197Ssam ((struct sockaddr_in *)(src0 + 1))->sin_port = 0; 4444105197Ssam break; 4445105197Ssam case AF_INET6: 4446105197Ssam if (((struct sockaddr *)(src0 + 1))->sa_len != 4447105197Ssam sizeof(struct sockaddr_in6)) 4448105197Ssam return key_senderror(so, m, EINVAL); 4449105197Ssam ((struct sockaddr_in6 *)(src0 + 1))->sin6_port = 0; 4450105197Ssam break; 4451105197Ssam default: 4452105197Ssam ; /*???*/ 4453105197Ssam } 4454105197Ssam switch (((struct sockaddr *)(dst0 + 1))->sa_family) { 4455105197Ssam case AF_INET: 4456105197Ssam if (((struct sockaddr *)(dst0 + 1))->sa_len != 4457105197Ssam sizeof(struct sockaddr_in)) 4458105197Ssam return key_senderror(so, m, EINVAL); 4459105197Ssam ((struct sockaddr_in *)(dst0 + 1))->sin_port = 0; 4460105197Ssam break; 4461105197Ssam case AF_INET6: 4462105197Ssam if (((struct sockaddr *)(dst0 + 1))->sa_len != 4463105197Ssam sizeof(struct sockaddr_in6)) 4464105197Ssam return key_senderror(so, m, EINVAL); 4465105197Ssam ((struct sockaddr_in6 *)(dst0 + 1))->sin6_port = 0; 4466105197Ssam break; 4467105197Ssam default: 4468105197Ssam ; /*???*/ 4469105197Ssam } 4470105197Ssam 4471105197Ssam /* XXX boundary check against sa_len */ 4472105197Ssam KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx); 4473105197Ssam 4474105197Ssam /* SPI allocation */ 4475105197Ssam spi = key_do_getnewspi((struct sadb_spirange *)mhp->ext[SADB_EXT_SPIRANGE], 4476105197Ssam &saidx); 4477105197Ssam if (spi == 0) 4478105197Ssam return key_senderror(so, m, EINVAL); 4479105197Ssam 4480105197Ssam /* get a SA index */ 4481105197Ssam if ((newsah = key_getsah(&saidx)) == NULL) { 4482105197Ssam /* create a new SA index */ 4483105197Ssam if ((newsah = key_newsah(&saidx)) == NULL) { 4484120585Ssam ipseclog((LOG_DEBUG, "%s: No more memory.\n",__func__)); 4485105197Ssam return key_senderror(so, m, ENOBUFS); 4486105197Ssam } 4487105197Ssam } 4488105197Ssam 4489105197Ssam /* get a new SA */ 4490105197Ssam /* XXX rewrite */ 4491105197Ssam newsav = KEY_NEWSAV(m, mhp, newsah, &error); 4492105197Ssam if (newsav == NULL) { 4493105197Ssam /* XXX don't free new SA index allocated in above. */ 4494105197Ssam return key_senderror(so, m, error); 4495105197Ssam } 4496105197Ssam 4497105197Ssam /* set spi */ 4498105197Ssam newsav->spi = htonl(spi); 4499105197Ssam 4500105197Ssam /* delete the entry in acqtree */ 4501105197Ssam if (mhp->msg->sadb_msg_seq != 0) { 4502105197Ssam struct secacq *acq; 4503105197Ssam if ((acq = key_getacqbyseq(mhp->msg->sadb_msg_seq)) != NULL) { 4504105197Ssam /* reset counter in order to deletion by timehandler. */ 4505105197Ssam acq->created = time_second; 4506105197Ssam acq->count = 0; 4507105197Ssam } 4508105197Ssam } 4509105197Ssam 4510105197Ssam { 4511105197Ssam struct mbuf *n, *nn; 4512105197Ssam struct sadb_sa *m_sa; 4513105197Ssam struct sadb_msg *newmsg; 4514105197Ssam int off, len; 4515105197Ssam 4516105197Ssam /* create new sadb_msg to reply. */ 4517105197Ssam len = PFKEY_ALIGN8(sizeof(struct sadb_msg)) + 4518105197Ssam PFKEY_ALIGN8(sizeof(struct sadb_sa)); 4519105197Ssam 4520111119Simp MGETHDR(n, M_DONTWAIT, MT_DATA); 4521105197Ssam if (len > MHLEN) { 4522111119Simp MCLGET(n, M_DONTWAIT); 4523105197Ssam if ((n->m_flags & M_EXT) == 0) { 4524105197Ssam m_freem(n); 4525105197Ssam n = NULL; 4526105197Ssam } 4527105197Ssam } 4528105197Ssam if (!n) 4529105197Ssam return key_senderror(so, m, ENOBUFS); 4530105197Ssam 4531105197Ssam n->m_len = len; 4532105197Ssam n->m_next = NULL; 4533105197Ssam off = 0; 4534105197Ssam 4535105197Ssam m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off); 4536105197Ssam off += PFKEY_ALIGN8(sizeof(struct sadb_msg)); 4537105197Ssam 4538105197Ssam m_sa = (struct sadb_sa *)(mtod(n, caddr_t) + off); 4539105197Ssam m_sa->sadb_sa_len = PFKEY_UNIT64(sizeof(struct sadb_sa)); 4540105197Ssam m_sa->sadb_sa_exttype = SADB_EXT_SA; 4541105197Ssam m_sa->sadb_sa_spi = htonl(spi); 4542105197Ssam off += PFKEY_ALIGN8(sizeof(struct sadb_sa)); 4543105197Ssam 4544120585Ssam IPSEC_ASSERT(off == len, 4545120585Ssam ("length inconsistency (off %u len %u)", off, len)); 4546105197Ssam 4547105197Ssam n->m_next = key_gather_mbuf(m, mhp, 0, 2, SADB_EXT_ADDRESS_SRC, 4548105197Ssam SADB_EXT_ADDRESS_DST); 4549105197Ssam if (!n->m_next) { 4550105197Ssam m_freem(n); 4551105197Ssam return key_senderror(so, m, ENOBUFS); 4552105197Ssam } 4553105197Ssam 4554105197Ssam if (n->m_len < sizeof(struct sadb_msg)) { 4555105197Ssam n = m_pullup(n, sizeof(struct sadb_msg)); 4556105197Ssam if (n == NULL) 4557105197Ssam return key_sendup_mbuf(so, m, KEY_SENDUP_ONE); 4558105197Ssam } 4559105197Ssam 4560105197Ssam n->m_pkthdr.len = 0; 4561105197Ssam for (nn = n; nn; nn = nn->m_next) 4562105197Ssam n->m_pkthdr.len += nn->m_len; 4563105197Ssam 4564105197Ssam newmsg = mtod(n, struct sadb_msg *); 4565105197Ssam newmsg->sadb_msg_seq = newsav->seq; 4566105197Ssam newmsg->sadb_msg_errno = 0; 4567105197Ssam newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len); 4568105197Ssam 4569105197Ssam m_freem(m); 4570105197Ssam return key_sendup_mbuf(so, n, KEY_SENDUP_ONE); 4571105197Ssam } 4572105197Ssam} 4573105197Ssam 4574105197Ssam/* 4575105197Ssam * allocating new SPI 4576105197Ssam * called by key_getspi(). 4577105197Ssam * OUT: 4578105197Ssam * 0: failure. 4579105197Ssam * others: success. 4580105197Ssam */ 4581105197Ssamstatic u_int32_t 4582105197Ssamkey_do_getnewspi(spirange, saidx) 4583105197Ssam struct sadb_spirange *spirange; 4584105197Ssam struct secasindex *saidx; 4585105197Ssam{ 4586105197Ssam u_int32_t newspi; 4587105197Ssam u_int32_t min, max; 4588105197Ssam int count = key_spi_trycnt; 4589105197Ssam 4590105197Ssam /* set spi range to allocate */ 4591105197Ssam if (spirange != NULL) { 4592105197Ssam min = spirange->sadb_spirange_min; 4593105197Ssam max = spirange->sadb_spirange_max; 4594105197Ssam } else { 4595105197Ssam min = key_spi_minval; 4596105197Ssam max = key_spi_maxval; 4597105197Ssam } 4598105197Ssam /* IPCOMP needs 2-byte SPI */ 4599105197Ssam if (saidx->proto == IPPROTO_IPCOMP) { 4600105197Ssam u_int32_t t; 4601105197Ssam if (min >= 0x10000) 4602105197Ssam min = 0xffff; 4603105197Ssam if (max >= 0x10000) 4604105197Ssam max = 0xffff; 4605105197Ssam if (min > max) { 4606105197Ssam t = min; min = max; max = t; 4607105197Ssam } 4608105197Ssam } 4609105197Ssam 4610105197Ssam if (min == max) { 4611105197Ssam if (key_checkspidup(saidx, min) != NULL) { 4612120585Ssam ipseclog((LOG_DEBUG, "%s: SPI %u exists already.\n", 4613120585Ssam __func__, min)); 4614105197Ssam return 0; 4615105197Ssam } 4616105197Ssam 4617105197Ssam count--; /* taking one cost. */ 4618105197Ssam newspi = min; 4619105197Ssam 4620105197Ssam } else { 4621105197Ssam 4622105197Ssam /* init SPI */ 4623105197Ssam newspi = 0; 4624105197Ssam 4625105197Ssam /* when requesting to allocate spi ranged */ 4626105197Ssam while (count--) { 4627105197Ssam /* generate pseudo-random SPI value ranged. */ 4628105197Ssam newspi = min + (key_random() % (max - min + 1)); 4629105197Ssam 4630105197Ssam if (key_checkspidup(saidx, newspi) == NULL) 4631105197Ssam break; 4632105197Ssam } 4633105197Ssam 4634105197Ssam if (count == 0 || newspi == 0) { 4635120585Ssam ipseclog((LOG_DEBUG, "%s: to allocate spi is failed.\n", 4636120585Ssam __func__)); 4637105197Ssam return 0; 4638105197Ssam } 4639105197Ssam } 4640105197Ssam 4641105197Ssam /* statistics */ 4642105197Ssam keystat.getspi_count = 4643105197Ssam (keystat.getspi_count + key_spi_trycnt - count) / 2; 4644105197Ssam 4645105197Ssam return newspi; 4646105197Ssam} 4647105197Ssam 4648105197Ssam/* 4649105197Ssam * SADB_UPDATE processing 4650105197Ssam * receive 4651105197Ssam * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),) 4652105197Ssam * key(AE), (identity(SD),) (sensitivity)> 4653105197Ssam * from the ikmpd, and update a secasvar entry whose status is SADB_SASTATE_LARVAL. 4654105197Ssam * and send 4655105197Ssam * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),) 4656105197Ssam * (identity(SD),) (sensitivity)> 4657105197Ssam * to the ikmpd. 4658105197Ssam * 4659105197Ssam * m will always be freed. 4660105197Ssam */ 4661105197Ssamstatic int 4662105197Ssamkey_update(so, m, mhp) 4663105197Ssam struct socket *so; 4664105197Ssam struct mbuf *m; 4665105197Ssam const struct sadb_msghdr *mhp; 4666105197Ssam{ 4667105197Ssam struct sadb_sa *sa0; 4668105197Ssam struct sadb_address *src0, *dst0; 4669105197Ssam struct secasindex saidx; 4670105197Ssam struct secashead *sah; 4671105197Ssam struct secasvar *sav; 4672105197Ssam u_int16_t proto; 4673105197Ssam u_int8_t mode; 4674105197Ssam u_int32_t reqid; 4675105197Ssam int error; 4676105197Ssam 4677120585Ssam IPSEC_ASSERT(so != NULL, ("null socket")); 4678120585Ssam IPSEC_ASSERT(m != NULL, ("null mbuf")); 4679120585Ssam IPSEC_ASSERT(mhp != NULL, ("null msghdr")); 4680120585Ssam IPSEC_ASSERT(mhp->msg != NULL, ("null msg")); 4681105197Ssam 4682105197Ssam /* map satype to proto */ 4683105197Ssam if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) { 4684120585Ssam ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n", 4685120585Ssam __func__)); 4686105197Ssam return key_senderror(so, m, EINVAL); 4687105197Ssam } 4688105197Ssam 4689105197Ssam if (mhp->ext[SADB_EXT_SA] == NULL || 4690105197Ssam mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL || 4691105197Ssam mhp->ext[SADB_EXT_ADDRESS_DST] == NULL || 4692105197Ssam (mhp->msg->sadb_msg_satype == SADB_SATYPE_ESP && 4693105197Ssam mhp->ext[SADB_EXT_KEY_ENCRYPT] == NULL) || 4694105197Ssam (mhp->msg->sadb_msg_satype == SADB_SATYPE_AH && 4695105197Ssam mhp->ext[SADB_EXT_KEY_AUTH] == NULL) || 4696105197Ssam (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL && 4697105197Ssam mhp->ext[SADB_EXT_LIFETIME_SOFT] == NULL) || 4698105197Ssam (mhp->ext[SADB_EXT_LIFETIME_HARD] == NULL && 4699105197Ssam mhp->ext[SADB_EXT_LIFETIME_SOFT] != NULL)) { 4700120585Ssam ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n", 4701120585Ssam __func__)); 4702105197Ssam return key_senderror(so, m, EINVAL); 4703105197Ssam } 4704105197Ssam if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) || 4705105197Ssam mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) || 4706105197Ssam mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) { 4707120585Ssam ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n", 4708120585Ssam __func__)); 4709105197Ssam return key_senderror(so, m, EINVAL); 4710105197Ssam } 4711105197Ssam if (mhp->ext[SADB_X_EXT_SA2] != NULL) { 4712105197Ssam mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode; 4713105197Ssam reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid; 4714105197Ssam } else { 4715105197Ssam mode = IPSEC_MODE_ANY; 4716105197Ssam reqid = 0; 4717105197Ssam } 4718105197Ssam /* XXX boundary checking for other extensions */ 4719105197Ssam 4720105197Ssam sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA]; 4721105197Ssam src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]); 4722105197Ssam dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]); 4723105197Ssam 4724105197Ssam /* XXX boundary check against sa_len */ 4725105197Ssam KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx); 4726105197Ssam 4727105197Ssam /* get a SA header */ 4728105197Ssam if ((sah = key_getsah(&saidx)) == NULL) { 4729120585Ssam ipseclog((LOG_DEBUG, "%s: no SA index found.\n", __func__)); 4730105197Ssam return key_senderror(so, m, ENOENT); 4731105197Ssam } 4732105197Ssam 4733105197Ssam /* set spidx if there */ 4734105197Ssam /* XXX rewrite */ 4735105197Ssam error = key_setident(sah, m, mhp); 4736105197Ssam if (error) 4737105197Ssam return key_senderror(so, m, error); 4738105197Ssam 4739105197Ssam /* find a SA with sequence number. */ 4740105197Ssam#ifdef IPSEC_DOSEQCHECK 4741105197Ssam if (mhp->msg->sadb_msg_seq != 0 4742105197Ssam && (sav = key_getsavbyseq(sah, mhp->msg->sadb_msg_seq)) == NULL) { 4743120585Ssam ipseclog((LOG_DEBUG, "%s: no larval SA with sequence %u " 4744120585Ssam "exists.\n", __func__, mhp->msg->sadb_msg_seq)); 4745105197Ssam return key_senderror(so, m, ENOENT); 4746105197Ssam } 4747105197Ssam#else 4748120585Ssam SAHTREE_LOCK(); 4749120585Ssam sav = key_getsavbyspi(sah, sa0->sadb_sa_spi); 4750120585Ssam SAHTREE_UNLOCK(); 4751120585Ssam if (sav == NULL) { 4752120585Ssam ipseclog((LOG_DEBUG, "%s: no such a SA found (spi:%u)\n", 4753120585Ssam __func__, (u_int32_t)ntohl(sa0->sadb_sa_spi))); 4754105197Ssam return key_senderror(so, m, EINVAL); 4755105197Ssam } 4756105197Ssam#endif 4757105197Ssam 4758105197Ssam /* validity check */ 4759105197Ssam if (sav->sah->saidx.proto != proto) { 4760120585Ssam ipseclog((LOG_DEBUG, "%s: protocol mismatched " 4761120585Ssam "(DB=%u param=%u)\n", __func__, 4762120585Ssam sav->sah->saidx.proto, proto)); 4763105197Ssam return key_senderror(so, m, EINVAL); 4764105197Ssam } 4765105197Ssam#ifdef IPSEC_DOSEQCHECK 4766105197Ssam if (sav->spi != sa0->sadb_sa_spi) { 4767120585Ssam ipseclog((LOG_DEBUG, "%s: SPI mismatched (DB:%u param:%u)\n", 4768120585Ssam __func__, 4769105197Ssam (u_int32_t)ntohl(sav->spi), 4770105197Ssam (u_int32_t)ntohl(sa0->sadb_sa_spi))); 4771105197Ssam return key_senderror(so, m, EINVAL); 4772105197Ssam } 4773105197Ssam#endif 4774105197Ssam if (sav->pid != mhp->msg->sadb_msg_pid) { 4775120585Ssam ipseclog((LOG_DEBUG, "%s: pid mismatched (DB:%u param:%u)\n", 4776120585Ssam __func__, sav->pid, mhp->msg->sadb_msg_pid)); 4777105197Ssam return key_senderror(so, m, EINVAL); 4778105197Ssam } 4779105197Ssam 4780105197Ssam /* copy sav values */ 4781105197Ssam error = key_setsaval(sav, m, mhp); 4782105197Ssam if (error) { 4783105197Ssam KEY_FREESAV(&sav); 4784105197Ssam return key_senderror(so, m, error); 4785105197Ssam } 4786105197Ssam 4787105197Ssam /* check SA values to be mature. */ 4788105197Ssam if ((mhp->msg->sadb_msg_errno = key_mature(sav)) != 0) { 4789105197Ssam KEY_FREESAV(&sav); 4790105197Ssam return key_senderror(so, m, 0); 4791105197Ssam } 4792105197Ssam 4793105197Ssam { 4794105197Ssam struct mbuf *n; 4795105197Ssam 4796105197Ssam /* set msg buf from mhp */ 4797105197Ssam n = key_getmsgbuf_x1(m, mhp); 4798105197Ssam if (n == NULL) { 4799120585Ssam ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__)); 4800105197Ssam return key_senderror(so, m, ENOBUFS); 4801105197Ssam } 4802105197Ssam 4803105197Ssam m_freem(m); 4804105197Ssam return key_sendup_mbuf(so, n, KEY_SENDUP_ALL); 4805105197Ssam } 4806105197Ssam} 4807105197Ssam 4808105197Ssam/* 4809105197Ssam * search SAD with sequence for a SA which state is SADB_SASTATE_LARVAL. 4810105197Ssam * only called by key_update(). 4811105197Ssam * OUT: 4812105197Ssam * NULL : not found 4813105197Ssam * others : found, pointer to a SA. 4814105197Ssam */ 4815105197Ssam#ifdef IPSEC_DOSEQCHECK 4816105197Ssamstatic struct secasvar * 4817105197Ssamkey_getsavbyseq(sah, seq) 4818105197Ssam struct secashead *sah; 4819105197Ssam u_int32_t seq; 4820105197Ssam{ 4821105197Ssam struct secasvar *sav; 4822105197Ssam u_int state; 4823105197Ssam 4824105197Ssam state = SADB_SASTATE_LARVAL; 4825105197Ssam 4826105197Ssam /* search SAD with sequence number ? */ 4827105197Ssam LIST_FOREACH(sav, &sah->savtree[state], chain) { 4828105197Ssam 4829120585Ssam KEY_CHKSASTATE(state, sav->state, __func__); 4830105197Ssam 4831105197Ssam if (sav->seq == seq) { 4832158767Spjd sa_addref(sav); 4833105197Ssam KEYDEBUG(KEYDEBUG_IPSEC_STAMP, 4834120585Ssam printf("DP %s cause refcnt++:%d SA:%p\n", 4835120585Ssam __func__, sav->refcnt, sav)); 4836105197Ssam return sav; 4837105197Ssam } 4838105197Ssam } 4839105197Ssam 4840105197Ssam return NULL; 4841105197Ssam} 4842105197Ssam#endif 4843105197Ssam 4844105197Ssam/* 4845105197Ssam * SADB_ADD processing 4846108533Sschweikh * add an entry to SA database, when received 4847105197Ssam * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),) 4848105197Ssam * key(AE), (identity(SD),) (sensitivity)> 4849105197Ssam * from the ikmpd, 4850105197Ssam * and send 4851105197Ssam * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),) 4852105197Ssam * (identity(SD),) (sensitivity)> 4853105197Ssam * to the ikmpd. 4854105197Ssam * 4855105197Ssam * IGNORE identity and sensitivity messages. 4856105197Ssam * 4857105197Ssam * m will always be freed. 4858105197Ssam */ 4859105197Ssamstatic int 4860105197Ssamkey_add(so, m, mhp) 4861105197Ssam struct socket *so; 4862105197Ssam struct mbuf *m; 4863105197Ssam const struct sadb_msghdr *mhp; 4864105197Ssam{ 4865105197Ssam struct sadb_sa *sa0; 4866105197Ssam struct sadb_address *src0, *dst0; 4867105197Ssam struct secasindex saidx; 4868105197Ssam struct secashead *newsah; 4869105197Ssam struct secasvar *newsav; 4870105197Ssam u_int16_t proto; 4871105197Ssam u_int8_t mode; 4872105197Ssam u_int32_t reqid; 4873105197Ssam int error; 4874105197Ssam 4875120585Ssam IPSEC_ASSERT(so != NULL, ("null socket")); 4876120585Ssam IPSEC_ASSERT(m != NULL, ("null mbuf")); 4877120585Ssam IPSEC_ASSERT(mhp != NULL, ("null msghdr")); 4878120585Ssam IPSEC_ASSERT(mhp->msg != NULL, ("null msg")); 4879105197Ssam 4880105197Ssam /* map satype to proto */ 4881105197Ssam if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) { 4882120585Ssam ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n", 4883120585Ssam __func__)); 4884105197Ssam return key_senderror(so, m, EINVAL); 4885105197Ssam } 4886105197Ssam 4887105197Ssam if (mhp->ext[SADB_EXT_SA] == NULL || 4888105197Ssam mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL || 4889105197Ssam mhp->ext[SADB_EXT_ADDRESS_DST] == NULL || 4890105197Ssam (mhp->msg->sadb_msg_satype == SADB_SATYPE_ESP && 4891105197Ssam mhp->ext[SADB_EXT_KEY_ENCRYPT] == NULL) || 4892105197Ssam (mhp->msg->sadb_msg_satype == SADB_SATYPE_AH && 4893105197Ssam mhp->ext[SADB_EXT_KEY_AUTH] == NULL) || 4894105197Ssam (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL && 4895105197Ssam mhp->ext[SADB_EXT_LIFETIME_SOFT] == NULL) || 4896105197Ssam (mhp->ext[SADB_EXT_LIFETIME_HARD] == NULL && 4897105197Ssam mhp->ext[SADB_EXT_LIFETIME_SOFT] != NULL)) { 4898120585Ssam ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n", 4899120585Ssam __func__)); 4900105197Ssam return key_senderror(so, m, EINVAL); 4901105197Ssam } 4902105197Ssam if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) || 4903105197Ssam mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) || 4904105197Ssam mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) { 4905105197Ssam /* XXX need more */ 4906120585Ssam ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n", 4907120585Ssam __func__)); 4908105197Ssam return key_senderror(so, m, EINVAL); 4909105197Ssam } 4910105197Ssam if (mhp->ext[SADB_X_EXT_SA2] != NULL) { 4911105197Ssam mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode; 4912105197Ssam reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid; 4913105197Ssam } else { 4914105197Ssam mode = IPSEC_MODE_ANY; 4915105197Ssam reqid = 0; 4916105197Ssam } 4917105197Ssam 4918105197Ssam sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA]; 4919105197Ssam src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC]; 4920105197Ssam dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST]; 4921105197Ssam 4922105197Ssam /* XXX boundary check against sa_len */ 4923105197Ssam KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx); 4924105197Ssam 4925105197Ssam /* get a SA header */ 4926105197Ssam if ((newsah = key_getsah(&saidx)) == NULL) { 4927105197Ssam /* create a new SA header */ 4928105197Ssam if ((newsah = key_newsah(&saidx)) == NULL) { 4929120585Ssam ipseclog((LOG_DEBUG, "%s: No more memory.\n",__func__)); 4930105197Ssam return key_senderror(so, m, ENOBUFS); 4931105197Ssam } 4932105197Ssam } 4933105197Ssam 4934105197Ssam /* set spidx if there */ 4935105197Ssam /* XXX rewrite */ 4936105197Ssam error = key_setident(newsah, m, mhp); 4937105197Ssam if (error) { 4938105197Ssam return key_senderror(so, m, error); 4939105197Ssam } 4940105197Ssam 4941105197Ssam /* create new SA entry. */ 4942105197Ssam /* We can create new SA only if SPI is differenct. */ 4943120585Ssam SAHTREE_LOCK(); 4944120585Ssam newsav = key_getsavbyspi(newsah, sa0->sadb_sa_spi); 4945120585Ssam SAHTREE_UNLOCK(); 4946120585Ssam if (newsav != NULL) { 4947120585Ssam ipseclog((LOG_DEBUG, "%s: SA already exists.\n", __func__)); 4948105197Ssam return key_senderror(so, m, EEXIST); 4949105197Ssam } 4950105197Ssam newsav = KEY_NEWSAV(m, mhp, newsah, &error); 4951105197Ssam if (newsav == NULL) { 4952105197Ssam return key_senderror(so, m, error); 4953105197Ssam } 4954105197Ssam 4955105197Ssam /* check SA values to be mature. */ 4956105197Ssam if ((error = key_mature(newsav)) != 0) { 4957105197Ssam KEY_FREESAV(&newsav); 4958105197Ssam return key_senderror(so, m, error); 4959105197Ssam } 4960105197Ssam 4961105197Ssam /* 4962105197Ssam * don't call key_freesav() here, as we would like to keep the SA 4963105197Ssam * in the database on success. 4964105197Ssam */ 4965105197Ssam 4966105197Ssam { 4967105197Ssam struct mbuf *n; 4968105197Ssam 4969105197Ssam /* set msg buf from mhp */ 4970105197Ssam n = key_getmsgbuf_x1(m, mhp); 4971105197Ssam if (n == NULL) { 4972120585Ssam ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__)); 4973105197Ssam return key_senderror(so, m, ENOBUFS); 4974105197Ssam } 4975105197Ssam 4976105197Ssam m_freem(m); 4977105197Ssam return key_sendup_mbuf(so, n, KEY_SENDUP_ALL); 4978105197Ssam } 4979105197Ssam} 4980105197Ssam 4981105197Ssam/* m is retained */ 4982105197Ssamstatic int 4983105197Ssamkey_setident(sah, m, mhp) 4984105197Ssam struct secashead *sah; 4985105197Ssam struct mbuf *m; 4986105197Ssam const struct sadb_msghdr *mhp; 4987105197Ssam{ 4988105197Ssam const struct sadb_ident *idsrc, *iddst; 4989105197Ssam int idsrclen, iddstlen; 4990105197Ssam 4991120585Ssam IPSEC_ASSERT(sah != NULL, ("null secashead")); 4992120585Ssam IPSEC_ASSERT(m != NULL, ("null mbuf")); 4993120585Ssam IPSEC_ASSERT(mhp != NULL, ("null msghdr")); 4994120585Ssam IPSEC_ASSERT(mhp->msg != NULL, ("null msg")); 4995105197Ssam 4996105197Ssam /* don't make buffer if not there */ 4997105197Ssam if (mhp->ext[SADB_EXT_IDENTITY_SRC] == NULL && 4998105197Ssam mhp->ext[SADB_EXT_IDENTITY_DST] == NULL) { 4999105197Ssam sah->idents = NULL; 5000105197Ssam sah->identd = NULL; 5001105197Ssam return 0; 5002105197Ssam } 5003105197Ssam 5004105197Ssam if (mhp->ext[SADB_EXT_IDENTITY_SRC] == NULL || 5005105197Ssam mhp->ext[SADB_EXT_IDENTITY_DST] == NULL) { 5006120585Ssam ipseclog((LOG_DEBUG, "%s: invalid identity.\n", __func__)); 5007105197Ssam return EINVAL; 5008105197Ssam } 5009105197Ssam 5010105197Ssam idsrc = (const struct sadb_ident *)mhp->ext[SADB_EXT_IDENTITY_SRC]; 5011105197Ssam iddst = (const struct sadb_ident *)mhp->ext[SADB_EXT_IDENTITY_DST]; 5012105197Ssam idsrclen = mhp->extlen[SADB_EXT_IDENTITY_SRC]; 5013105197Ssam iddstlen = mhp->extlen[SADB_EXT_IDENTITY_DST]; 5014105197Ssam 5015105197Ssam /* validity check */ 5016105197Ssam if (idsrc->sadb_ident_type != iddst->sadb_ident_type) { 5017120585Ssam ipseclog((LOG_DEBUG, "%s: ident type mismatch.\n", __func__)); 5018105197Ssam return EINVAL; 5019105197Ssam } 5020105197Ssam 5021105197Ssam switch (idsrc->sadb_ident_type) { 5022105197Ssam case SADB_IDENTTYPE_PREFIX: 5023105197Ssam case SADB_IDENTTYPE_FQDN: 5024105197Ssam case SADB_IDENTTYPE_USERFQDN: 5025105197Ssam default: 5026105197Ssam /* XXX do nothing */ 5027105197Ssam sah->idents = NULL; 5028105197Ssam sah->identd = NULL; 5029105197Ssam return 0; 5030105197Ssam } 5031105197Ssam 5032105197Ssam /* make structure */ 5033157123Sgnn sah->idents = malloc(sizeof(struct secident), M_IPSEC_MISC, M_NOWAIT); 5034105197Ssam if (sah->idents == NULL) { 5035120585Ssam ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__)); 5036105197Ssam return ENOBUFS; 5037105197Ssam } 5038157123Sgnn sah->identd = malloc(sizeof(struct secident), M_IPSEC_MISC, M_NOWAIT); 5039105197Ssam if (sah->identd == NULL) { 5040119643Ssam free(sah->idents, M_IPSEC_MISC); 5041105197Ssam sah->idents = NULL; 5042120585Ssam ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__)); 5043105197Ssam return ENOBUFS; 5044105197Ssam } 5045157123Sgnn sah->idents->type = idsrc->sadb_ident_type; 5046157123Sgnn sah->idents->id = idsrc->sadb_ident_id; 5047105197Ssam 5048157123Sgnn sah->identd->type = iddst->sadb_ident_type; 5049157123Sgnn sah->identd->id = iddst->sadb_ident_id; 5050157123Sgnn 5051105197Ssam return 0; 5052105197Ssam} 5053105197Ssam 5054105197Ssam/* 5055105197Ssam * m will not be freed on return. 5056105197Ssam * it is caller's responsibility to free the result. 5057105197Ssam */ 5058105197Ssamstatic struct mbuf * 5059105197Ssamkey_getmsgbuf_x1(m, mhp) 5060105197Ssam struct mbuf *m; 5061105197Ssam const struct sadb_msghdr *mhp; 5062105197Ssam{ 5063105197Ssam struct mbuf *n; 5064105197Ssam 5065120585Ssam IPSEC_ASSERT(m != NULL, ("null mbuf")); 5066120585Ssam IPSEC_ASSERT(mhp != NULL, ("null msghdr")); 5067120585Ssam IPSEC_ASSERT(mhp->msg != NULL, ("null msg")); 5068105197Ssam 5069105197Ssam /* create new sadb_msg to reply. */ 5070105197Ssam n = key_gather_mbuf(m, mhp, 1, 9, SADB_EXT_RESERVED, 5071105197Ssam SADB_EXT_SA, SADB_X_EXT_SA2, 5072105197Ssam SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST, 5073105197Ssam SADB_EXT_LIFETIME_HARD, SADB_EXT_LIFETIME_SOFT, 5074105197Ssam SADB_EXT_IDENTITY_SRC, SADB_EXT_IDENTITY_DST); 5075105197Ssam if (!n) 5076105197Ssam return NULL; 5077105197Ssam 5078105197Ssam if (n->m_len < sizeof(struct sadb_msg)) { 5079105197Ssam n = m_pullup(n, sizeof(struct sadb_msg)); 5080105197Ssam if (n == NULL) 5081105197Ssam return NULL; 5082105197Ssam } 5083105197Ssam mtod(n, struct sadb_msg *)->sadb_msg_errno = 0; 5084105197Ssam mtod(n, struct sadb_msg *)->sadb_msg_len = 5085105197Ssam PFKEY_UNIT64(n->m_pkthdr.len); 5086105197Ssam 5087105197Ssam return n; 5088105197Ssam} 5089105197Ssam 5090105197Ssamstatic int key_delete_all __P((struct socket *, struct mbuf *, 5091105197Ssam const struct sadb_msghdr *, u_int16_t)); 5092105197Ssam 5093105197Ssam/* 5094105197Ssam * SADB_DELETE processing 5095105197Ssam * receive 5096105197Ssam * <base, SA(*), address(SD)> 5097105197Ssam * from the ikmpd, and set SADB_SASTATE_DEAD, 5098105197Ssam * and send, 5099105197Ssam * <base, SA(*), address(SD)> 5100105197Ssam * to the ikmpd. 5101105197Ssam * 5102105197Ssam * m will always be freed. 5103105197Ssam */ 5104105197Ssamstatic int 5105105197Ssamkey_delete(so, m, mhp) 5106105197Ssam struct socket *so; 5107105197Ssam struct mbuf *m; 5108105197Ssam const struct sadb_msghdr *mhp; 5109105197Ssam{ 5110105197Ssam struct sadb_sa *sa0; 5111105197Ssam struct sadb_address *src0, *dst0; 5112105197Ssam struct secasindex saidx; 5113105197Ssam struct secashead *sah; 5114105197Ssam struct secasvar *sav = NULL; 5115105197Ssam u_int16_t proto; 5116105197Ssam 5117120585Ssam IPSEC_ASSERT(so != NULL, ("null socket")); 5118120585Ssam IPSEC_ASSERT(m != NULL, ("null mbuf")); 5119120585Ssam IPSEC_ASSERT(mhp != NULL, ("null msghdr")); 5120120585Ssam IPSEC_ASSERT(mhp->msg != NULL, ("null msg")); 5121105197Ssam 5122105197Ssam /* map satype to proto */ 5123105197Ssam if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) { 5124120585Ssam ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n", 5125120585Ssam __func__)); 5126105197Ssam return key_senderror(so, m, EINVAL); 5127105197Ssam } 5128105197Ssam 5129105197Ssam if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL || 5130105197Ssam mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) { 5131120585Ssam ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n", 5132120585Ssam __func__)); 5133105197Ssam return key_senderror(so, m, EINVAL); 5134105197Ssam } 5135105197Ssam 5136105197Ssam if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) || 5137105197Ssam mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) { 5138120585Ssam ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n", 5139120585Ssam __func__)); 5140105197Ssam return key_senderror(so, m, EINVAL); 5141105197Ssam } 5142105197Ssam 5143105197Ssam if (mhp->ext[SADB_EXT_SA] == NULL) { 5144105197Ssam /* 5145105197Ssam * Caller wants us to delete all non-LARVAL SAs 5146105197Ssam * that match the src/dst. This is used during 5147105197Ssam * IKE INITIAL-CONTACT. 5148105197Ssam */ 5149120585Ssam ipseclog((LOG_DEBUG, "%s: doing delete all.\n", __func__)); 5150105197Ssam return key_delete_all(so, m, mhp, proto); 5151105197Ssam } else if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa)) { 5152120585Ssam ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n", 5153120585Ssam __func__)); 5154105197Ssam return key_senderror(so, m, EINVAL); 5155105197Ssam } 5156105197Ssam 5157105197Ssam sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA]; 5158105197Ssam src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]); 5159105197Ssam dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]); 5160105197Ssam 5161105197Ssam /* XXX boundary check against sa_len */ 5162105197Ssam KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx); 5163105197Ssam 5164105197Ssam /* get a SA header */ 5165120585Ssam SAHTREE_LOCK(); 5166105197Ssam LIST_FOREACH(sah, &sahtree, chain) { 5167105197Ssam if (sah->state == SADB_SASTATE_DEAD) 5168105197Ssam continue; 5169105197Ssam if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0) 5170105197Ssam continue; 5171105197Ssam 5172105197Ssam /* get a SA with SPI. */ 5173105197Ssam sav = key_getsavbyspi(sah, sa0->sadb_sa_spi); 5174105197Ssam if (sav) 5175105197Ssam break; 5176105197Ssam } 5177105197Ssam if (sah == NULL) { 5178120585Ssam SAHTREE_UNLOCK(); 5179120585Ssam ipseclog((LOG_DEBUG, "%s: no SA found.\n", __func__)); 5180105197Ssam return key_senderror(so, m, ENOENT); 5181105197Ssam } 5182105197Ssam 5183105197Ssam key_sa_chgstate(sav, SADB_SASTATE_DEAD); 5184120585Ssam SAHTREE_UNLOCK(); 5185105197Ssam KEY_FREESAV(&sav); 5186105197Ssam 5187105197Ssam { 5188105197Ssam struct mbuf *n; 5189105197Ssam struct sadb_msg *newmsg; 5190105197Ssam 5191105197Ssam /* create new sadb_msg to reply. */ 5192105197Ssam n = key_gather_mbuf(m, mhp, 1, 4, SADB_EXT_RESERVED, 5193105197Ssam SADB_EXT_SA, SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST); 5194105197Ssam if (!n) 5195105197Ssam return key_senderror(so, m, ENOBUFS); 5196105197Ssam 5197105197Ssam if (n->m_len < sizeof(struct sadb_msg)) { 5198105197Ssam n = m_pullup(n, sizeof(struct sadb_msg)); 5199105197Ssam if (n == NULL) 5200105197Ssam return key_senderror(so, m, ENOBUFS); 5201105197Ssam } 5202105197Ssam newmsg = mtod(n, struct sadb_msg *); 5203105197Ssam newmsg->sadb_msg_errno = 0; 5204105197Ssam newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len); 5205105197Ssam 5206105197Ssam m_freem(m); 5207105197Ssam return key_sendup_mbuf(so, n, KEY_SENDUP_ALL); 5208105197Ssam } 5209105197Ssam} 5210105197Ssam 5211105197Ssam/* 5212105197Ssam * delete all SAs for src/dst. Called from key_delete(). 5213105197Ssam */ 5214105197Ssamstatic int 5215105197Ssamkey_delete_all(so, m, mhp, proto) 5216105197Ssam struct socket *so; 5217105197Ssam struct mbuf *m; 5218105197Ssam const struct sadb_msghdr *mhp; 5219105197Ssam u_int16_t proto; 5220105197Ssam{ 5221105197Ssam struct sadb_address *src0, *dst0; 5222105197Ssam struct secasindex saidx; 5223105197Ssam struct secashead *sah; 5224105197Ssam struct secasvar *sav, *nextsav; 5225105197Ssam u_int stateidx, state; 5226105197Ssam 5227105197Ssam src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]); 5228105197Ssam dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]); 5229105197Ssam 5230105197Ssam /* XXX boundary check against sa_len */ 5231105197Ssam KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx); 5232105197Ssam 5233120585Ssam SAHTREE_LOCK(); 5234105197Ssam LIST_FOREACH(sah, &sahtree, chain) { 5235105197Ssam if (sah->state == SADB_SASTATE_DEAD) 5236105197Ssam continue; 5237105197Ssam if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0) 5238105197Ssam continue; 5239105197Ssam 5240105197Ssam /* Delete all non-LARVAL SAs. */ 5241105197Ssam for (stateidx = 0; 5242105197Ssam stateidx < _ARRAYLEN(saorder_state_alive); 5243105197Ssam stateidx++) { 5244105197Ssam state = saorder_state_alive[stateidx]; 5245105197Ssam if (state == SADB_SASTATE_LARVAL) 5246105197Ssam continue; 5247105197Ssam for (sav = LIST_FIRST(&sah->savtree[state]); 5248105197Ssam sav != NULL; sav = nextsav) { 5249105197Ssam nextsav = LIST_NEXT(sav, chain); 5250105197Ssam /* sanity check */ 5251105197Ssam if (sav->state != state) { 5252120585Ssam ipseclog((LOG_DEBUG, "%s: invalid " 5253120585Ssam "sav->state (queue %d SA %d)\n", 5254120585Ssam __func__, state, sav->state)); 5255105197Ssam continue; 5256105197Ssam } 5257105197Ssam 5258105197Ssam key_sa_chgstate(sav, SADB_SASTATE_DEAD); 5259105197Ssam KEY_FREESAV(&sav); 5260105197Ssam } 5261105197Ssam } 5262105197Ssam } 5263120585Ssam SAHTREE_UNLOCK(); 5264105197Ssam { 5265105197Ssam struct mbuf *n; 5266105197Ssam struct sadb_msg *newmsg; 5267105197Ssam 5268105197Ssam /* create new sadb_msg to reply. */ 5269105197Ssam n = key_gather_mbuf(m, mhp, 1, 3, SADB_EXT_RESERVED, 5270105197Ssam SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST); 5271105197Ssam if (!n) 5272105197Ssam return key_senderror(so, m, ENOBUFS); 5273105197Ssam 5274105197Ssam if (n->m_len < sizeof(struct sadb_msg)) { 5275105197Ssam n = m_pullup(n, sizeof(struct sadb_msg)); 5276105197Ssam if (n == NULL) 5277105197Ssam return key_senderror(so, m, ENOBUFS); 5278105197Ssam } 5279105197Ssam newmsg = mtod(n, struct sadb_msg *); 5280105197Ssam newmsg->sadb_msg_errno = 0; 5281105197Ssam newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len); 5282105197Ssam 5283105197Ssam m_freem(m); 5284105197Ssam return key_sendup_mbuf(so, n, KEY_SENDUP_ALL); 5285105197Ssam } 5286105197Ssam} 5287105197Ssam 5288105197Ssam/* 5289105197Ssam * SADB_GET processing 5290105197Ssam * receive 5291105197Ssam * <base, SA(*), address(SD)> 5292105197Ssam * from the ikmpd, and get a SP and a SA to respond, 5293105197Ssam * and send, 5294105197Ssam * <base, SA, (lifetime(HSC),) address(SD), (address(P),) key(AE), 5295105197Ssam * (identity(SD),) (sensitivity)> 5296105197Ssam * to the ikmpd. 5297105197Ssam * 5298105197Ssam * m will always be freed. 5299105197Ssam */ 5300105197Ssamstatic int 5301105197Ssamkey_get(so, m, mhp) 5302105197Ssam struct socket *so; 5303105197Ssam struct mbuf *m; 5304105197Ssam const struct sadb_msghdr *mhp; 5305105197Ssam{ 5306105197Ssam struct sadb_sa *sa0; 5307105197Ssam struct sadb_address *src0, *dst0; 5308105197Ssam struct secasindex saidx; 5309105197Ssam struct secashead *sah; 5310105197Ssam struct secasvar *sav = NULL; 5311105197Ssam u_int16_t proto; 5312105197Ssam 5313120585Ssam IPSEC_ASSERT(so != NULL, ("null socket")); 5314120585Ssam IPSEC_ASSERT(m != NULL, ("null mbuf")); 5315120585Ssam IPSEC_ASSERT(mhp != NULL, ("null msghdr")); 5316120585Ssam IPSEC_ASSERT(mhp->msg != NULL, ("null msg")); 5317105197Ssam 5318105197Ssam /* map satype to proto */ 5319105197Ssam if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) { 5320120585Ssam ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n", 5321120585Ssam __func__)); 5322105197Ssam return key_senderror(so, m, EINVAL); 5323105197Ssam } 5324105197Ssam 5325105197Ssam if (mhp->ext[SADB_EXT_SA] == NULL || 5326105197Ssam mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL || 5327105197Ssam mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) { 5328120585Ssam ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n", 5329120585Ssam __func__)); 5330105197Ssam return key_senderror(so, m, EINVAL); 5331105197Ssam } 5332105197Ssam if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) || 5333105197Ssam mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) || 5334105197Ssam mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) { 5335120585Ssam ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n", 5336120585Ssam __func__)); 5337105197Ssam return key_senderror(so, m, EINVAL); 5338105197Ssam } 5339105197Ssam 5340105197Ssam sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA]; 5341105197Ssam src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC]; 5342105197Ssam dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST]; 5343105197Ssam 5344105197Ssam /* XXX boundary check against sa_len */ 5345105197Ssam KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx); 5346105197Ssam 5347105197Ssam /* get a SA header */ 5348120585Ssam SAHTREE_LOCK(); 5349105197Ssam LIST_FOREACH(sah, &sahtree, chain) { 5350105197Ssam if (sah->state == SADB_SASTATE_DEAD) 5351105197Ssam continue; 5352105197Ssam if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0) 5353105197Ssam continue; 5354105197Ssam 5355105197Ssam /* get a SA with SPI. */ 5356105197Ssam sav = key_getsavbyspi(sah, sa0->sadb_sa_spi); 5357105197Ssam if (sav) 5358105197Ssam break; 5359105197Ssam } 5360120585Ssam SAHTREE_UNLOCK(); 5361105197Ssam if (sah == NULL) { 5362120585Ssam ipseclog((LOG_DEBUG, "%s: no SA found.\n", __func__)); 5363105197Ssam return key_senderror(so, m, ENOENT); 5364105197Ssam } 5365105197Ssam 5366105197Ssam { 5367105197Ssam struct mbuf *n; 5368105197Ssam u_int8_t satype; 5369105197Ssam 5370105197Ssam /* map proto to satype */ 5371105197Ssam if ((satype = key_proto2satype(sah->saidx.proto)) == 0) { 5372120585Ssam ipseclog((LOG_DEBUG, "%s: there was invalid proto in SAD.\n", 5373120585Ssam __func__)); 5374105197Ssam return key_senderror(so, m, EINVAL); 5375105197Ssam } 5376105197Ssam 5377105197Ssam /* create new sadb_msg to reply. */ 5378105197Ssam n = key_setdumpsa(sav, SADB_GET, satype, mhp->msg->sadb_msg_seq, 5379105197Ssam mhp->msg->sadb_msg_pid); 5380105197Ssam if (!n) 5381105197Ssam return key_senderror(so, m, ENOBUFS); 5382105197Ssam 5383105197Ssam m_freem(m); 5384105197Ssam return key_sendup_mbuf(so, n, KEY_SENDUP_ONE); 5385105197Ssam } 5386105197Ssam} 5387105197Ssam 5388105197Ssam/* XXX make it sysctl-configurable? */ 5389105197Ssamstatic void 5390105197Ssamkey_getcomb_setlifetime(comb) 5391105197Ssam struct sadb_comb *comb; 5392105197Ssam{ 5393105197Ssam 5394105197Ssam comb->sadb_comb_soft_allocations = 1; 5395105197Ssam comb->sadb_comb_hard_allocations = 1; 5396105197Ssam comb->sadb_comb_soft_bytes = 0; 5397105197Ssam comb->sadb_comb_hard_bytes = 0; 5398105197Ssam comb->sadb_comb_hard_addtime = 86400; /* 1 day */ 5399105197Ssam comb->sadb_comb_soft_addtime = comb->sadb_comb_soft_addtime * 80 / 100; 5400105197Ssam comb->sadb_comb_soft_usetime = 28800; /* 8 hours */ 5401105197Ssam comb->sadb_comb_hard_usetime = comb->sadb_comb_hard_usetime * 80 / 100; 5402105197Ssam} 5403105197Ssam 5404105197Ssam/* 5405105197Ssam * XXX reorder combinations by preference 5406105197Ssam * XXX no idea if the user wants ESP authentication or not 5407105197Ssam */ 5408105197Ssamstatic struct mbuf * 5409105197Ssamkey_getcomb_esp() 5410105197Ssam{ 5411105197Ssam struct sadb_comb *comb; 5412105197Ssam struct enc_xform *algo; 5413105197Ssam struct mbuf *result = NULL, *m, *n; 5414105197Ssam int encmin; 5415105197Ssam int i, off, o; 5416105197Ssam int totlen; 5417105197Ssam const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb)); 5418105197Ssam 5419105197Ssam m = NULL; 5420105197Ssam for (i = 1; i <= SADB_EALG_MAX; i++) { 5421105197Ssam algo = esp_algorithm_lookup(i); 5422105197Ssam if (algo == NULL) 5423105197Ssam continue; 5424105197Ssam 5425105197Ssam /* discard algorithms with key size smaller than system min */ 5426105197Ssam if (_BITS(algo->maxkey) < ipsec_esp_keymin) 5427105197Ssam continue; 5428105197Ssam if (_BITS(algo->minkey) < ipsec_esp_keymin) 5429105197Ssam encmin = ipsec_esp_keymin; 5430105197Ssam else 5431105197Ssam encmin = _BITS(algo->minkey); 5432105197Ssam 5433105197Ssam if (ipsec_esp_auth) 5434105197Ssam m = key_getcomb_ah(); 5435105197Ssam else { 5436120585Ssam IPSEC_ASSERT(l <= MLEN, 5437120585Ssam ("l=%u > MLEN=%lu", l, (u_long) MLEN)); 5438111119Simp MGET(m, M_DONTWAIT, MT_DATA); 5439105197Ssam if (m) { 5440105197Ssam M_ALIGN(m, l); 5441105197Ssam m->m_len = l; 5442105197Ssam m->m_next = NULL; 5443105197Ssam bzero(mtod(m, caddr_t), m->m_len); 5444105197Ssam } 5445105197Ssam } 5446105197Ssam if (!m) 5447105197Ssam goto fail; 5448105197Ssam 5449105197Ssam totlen = 0; 5450105197Ssam for (n = m; n; n = n->m_next) 5451105197Ssam totlen += n->m_len; 5452120585Ssam IPSEC_ASSERT((totlen % l) == 0, ("totlen=%u, l=%u", totlen, l)); 5453105197Ssam 5454105197Ssam for (off = 0; off < totlen; off += l) { 5455105197Ssam n = m_pulldown(m, off, l, &o); 5456105197Ssam if (!n) { 5457105197Ssam /* m is already freed */ 5458105197Ssam goto fail; 5459105197Ssam } 5460105197Ssam comb = (struct sadb_comb *)(mtod(n, caddr_t) + o); 5461105197Ssam bzero(comb, sizeof(*comb)); 5462105197Ssam key_getcomb_setlifetime(comb); 5463105197Ssam comb->sadb_comb_encrypt = i; 5464105197Ssam comb->sadb_comb_encrypt_minbits = encmin; 5465105197Ssam comb->sadb_comb_encrypt_maxbits = _BITS(algo->maxkey); 5466105197Ssam } 5467105197Ssam 5468105197Ssam if (!result) 5469105197Ssam result = m; 5470105197Ssam else 5471105197Ssam m_cat(result, m); 5472105197Ssam } 5473105197Ssam 5474105197Ssam return result; 5475105197Ssam 5476105197Ssam fail: 5477105197Ssam if (result) 5478105197Ssam m_freem(result); 5479105197Ssam return NULL; 5480105197Ssam} 5481105197Ssam 5482105197Ssamstatic void 5483105197Ssamkey_getsizes_ah( 5484105197Ssam const struct auth_hash *ah, 5485105197Ssam int alg, 5486105197Ssam u_int16_t* min, 5487105197Ssam u_int16_t* max) 5488105197Ssam{ 5489105197Ssam *min = *max = ah->keysize; 5490105197Ssam if (ah->keysize == 0) { 5491105197Ssam /* 5492105197Ssam * Transform takes arbitrary key size but algorithm 5493105197Ssam * key size is restricted. Enforce this here. 5494105197Ssam */ 5495105197Ssam switch (alg) { 5496105197Ssam case SADB_X_AALG_MD5: *min = *max = 16; break; 5497105197Ssam case SADB_X_AALG_SHA: *min = *max = 20; break; 5498105197Ssam case SADB_X_AALG_NULL: *min = 1; *max = 256; break; 5499105197Ssam default: 5500120585Ssam DPRINTF(("%s: unknown AH algorithm %u\n", 5501120585Ssam __func__, alg)); 5502105197Ssam break; 5503105197Ssam } 5504105197Ssam } 5505105197Ssam} 5506105197Ssam 5507105197Ssam/* 5508105197Ssam * XXX reorder combinations by preference 5509105197Ssam */ 5510105197Ssamstatic struct mbuf * 5511105197Ssamkey_getcomb_ah() 5512105197Ssam{ 5513105197Ssam struct sadb_comb *comb; 5514105197Ssam struct auth_hash *algo; 5515105197Ssam struct mbuf *m; 5516105197Ssam u_int16_t minkeysize, maxkeysize; 5517105197Ssam int i; 5518105197Ssam const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb)); 5519105197Ssam 5520105197Ssam m = NULL; 5521105197Ssam for (i = 1; i <= SADB_AALG_MAX; i++) { 5522105197Ssam#if 1 5523105197Ssam /* we prefer HMAC algorithms, not old algorithms */ 5524105197Ssam if (i != SADB_AALG_SHA1HMAC && i != SADB_AALG_MD5HMAC) 5525105197Ssam continue; 5526105197Ssam#endif 5527105197Ssam algo = ah_algorithm_lookup(i); 5528105197Ssam if (!algo) 5529105197Ssam continue; 5530105197Ssam key_getsizes_ah(algo, i, &minkeysize, &maxkeysize); 5531105197Ssam /* discard algorithms with key size smaller than system min */ 5532105197Ssam if (_BITS(minkeysize) < ipsec_ah_keymin) 5533105197Ssam continue; 5534105197Ssam 5535105197Ssam if (!m) { 5536120585Ssam IPSEC_ASSERT(l <= MLEN, 5537120585Ssam ("l=%u > MLEN=%lu", l, (u_long) MLEN)); 5538111119Simp MGET(m, M_DONTWAIT, MT_DATA); 5539105197Ssam if (m) { 5540105197Ssam M_ALIGN(m, l); 5541105197Ssam m->m_len = l; 5542105197Ssam m->m_next = NULL; 5543105197Ssam } 5544105197Ssam } else 5545111119Simp M_PREPEND(m, l, M_DONTWAIT); 5546105197Ssam if (!m) 5547105197Ssam return NULL; 5548105197Ssam 5549105197Ssam comb = mtod(m, struct sadb_comb *); 5550105197Ssam bzero(comb, sizeof(*comb)); 5551105197Ssam key_getcomb_setlifetime(comb); 5552105197Ssam comb->sadb_comb_auth = i; 5553105197Ssam comb->sadb_comb_auth_minbits = _BITS(minkeysize); 5554105197Ssam comb->sadb_comb_auth_maxbits = _BITS(maxkeysize); 5555105197Ssam } 5556105197Ssam 5557105197Ssam return m; 5558105197Ssam} 5559105197Ssam 5560105197Ssam/* 5561105197Ssam * not really an official behavior. discussed in pf_key@inner.net in Sep2000. 5562105197Ssam * XXX reorder combinations by preference 5563105197Ssam */ 5564105197Ssamstatic struct mbuf * 5565105197Ssamkey_getcomb_ipcomp() 5566105197Ssam{ 5567105197Ssam struct sadb_comb *comb; 5568105197Ssam struct comp_algo *algo; 5569105197Ssam struct mbuf *m; 5570105197Ssam int i; 5571105197Ssam const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb)); 5572105197Ssam 5573105197Ssam m = NULL; 5574105197Ssam for (i = 1; i <= SADB_X_CALG_MAX; i++) { 5575105197Ssam algo = ipcomp_algorithm_lookup(i); 5576105197Ssam if (!algo) 5577105197Ssam continue; 5578105197Ssam 5579105197Ssam if (!m) { 5580120585Ssam IPSEC_ASSERT(l <= MLEN, 5581120585Ssam ("l=%u > MLEN=%lu", l, (u_long) MLEN)); 5582111119Simp MGET(m, M_DONTWAIT, MT_DATA); 5583105197Ssam if (m) { 5584105197Ssam M_ALIGN(m, l); 5585105197Ssam m->m_len = l; 5586105197Ssam m->m_next = NULL; 5587105197Ssam } 5588105197Ssam } else 5589111119Simp M_PREPEND(m, l, M_DONTWAIT); 5590105197Ssam if (!m) 5591105197Ssam return NULL; 5592105197Ssam 5593105197Ssam comb = mtod(m, struct sadb_comb *); 5594105197Ssam bzero(comb, sizeof(*comb)); 5595105197Ssam key_getcomb_setlifetime(comb); 5596105197Ssam comb->sadb_comb_encrypt = i; 5597105197Ssam /* what should we set into sadb_comb_*_{min,max}bits? */ 5598105197Ssam } 5599105197Ssam 5600105197Ssam return m; 5601105197Ssam} 5602105197Ssam 5603105197Ssam/* 5604105197Ssam * XXX no way to pass mode (transport/tunnel) to userland 5605105197Ssam * XXX replay checking? 5606105197Ssam * XXX sysctl interface to ipsec_{ah,esp}_keymin 5607105197Ssam */ 5608105197Ssamstatic struct mbuf * 5609105197Ssamkey_getprop(saidx) 5610105197Ssam const struct secasindex *saidx; 5611105197Ssam{ 5612105197Ssam struct sadb_prop *prop; 5613105197Ssam struct mbuf *m, *n; 5614105197Ssam const int l = PFKEY_ALIGN8(sizeof(struct sadb_prop)); 5615105197Ssam int totlen; 5616105197Ssam 5617105197Ssam switch (saidx->proto) { 5618105197Ssam case IPPROTO_ESP: 5619105197Ssam m = key_getcomb_esp(); 5620105197Ssam break; 5621105197Ssam case IPPROTO_AH: 5622105197Ssam m = key_getcomb_ah(); 5623105197Ssam break; 5624105197Ssam case IPPROTO_IPCOMP: 5625105197Ssam m = key_getcomb_ipcomp(); 5626105197Ssam break; 5627105197Ssam default: 5628105197Ssam return NULL; 5629105197Ssam } 5630105197Ssam 5631105197Ssam if (!m) 5632105197Ssam return NULL; 5633111119Simp M_PREPEND(m, l, M_DONTWAIT); 5634105197Ssam if (!m) 5635105197Ssam return NULL; 5636105197Ssam 5637105197Ssam totlen = 0; 5638105197Ssam for (n = m; n; n = n->m_next) 5639105197Ssam totlen += n->m_len; 5640105197Ssam 5641105197Ssam prop = mtod(m, struct sadb_prop *); 5642105197Ssam bzero(prop, sizeof(*prop)); 5643105197Ssam prop->sadb_prop_len = PFKEY_UNIT64(totlen); 5644105197Ssam prop->sadb_prop_exttype = SADB_EXT_PROPOSAL; 5645105197Ssam prop->sadb_prop_replay = 32; /* XXX */ 5646105197Ssam 5647105197Ssam return m; 5648105197Ssam} 5649105197Ssam 5650105197Ssam/* 5651105197Ssam * SADB_ACQUIRE processing called by key_checkrequest() and key_acquire2(). 5652105197Ssam * send 5653105197Ssam * <base, SA, address(SD), (address(P)), x_policy, 5654105197Ssam * (identity(SD),) (sensitivity,) proposal> 5655105197Ssam * to KMD, and expect to receive 5656105197Ssam * <base> with SADB_ACQUIRE if error occured, 5657105197Ssam * or 5658105197Ssam * <base, src address, dst address, (SPI range)> with SADB_GETSPI 5659105197Ssam * from KMD by PF_KEY. 5660105197Ssam * 5661105197Ssam * XXX x_policy is outside of RFC2367 (KAME extension). 5662105197Ssam * XXX sensitivity is not supported. 5663105197Ssam * XXX for ipcomp, RFC2367 does not define how to fill in proposal. 5664105197Ssam * see comment for key_getcomb_ipcomp(). 5665105197Ssam * 5666105197Ssam * OUT: 5667105197Ssam * 0 : succeed 5668105197Ssam * others: error number 5669105197Ssam */ 5670105197Ssamstatic int 5671105197Ssamkey_acquire(const struct secasindex *saidx, struct secpolicy *sp) 5672105197Ssam{ 5673105197Ssam struct mbuf *result = NULL, *m; 5674105197Ssam struct secacq *newacq; 5675105197Ssam u_int8_t satype; 5676105197Ssam int error = -1; 5677105197Ssam u_int32_t seq; 5678105197Ssam 5679120585Ssam IPSEC_ASSERT(saidx != NULL, ("null saidx")); 5680105197Ssam satype = key_proto2satype(saidx->proto); 5681120585Ssam IPSEC_ASSERT(satype != 0, ("null satype, protocol %u", saidx->proto)); 5682105197Ssam 5683105197Ssam /* 5684105197Ssam * We never do anything about acquirng SA. There is anather 5685105197Ssam * solution that kernel blocks to send SADB_ACQUIRE message until 5686105197Ssam * getting something message from IKEd. In later case, to be 5687105197Ssam * managed with ACQUIRING list. 5688105197Ssam */ 5689108533Sschweikh /* Get an entry to check whether sending message or not. */ 5690105197Ssam if ((newacq = key_getacq(saidx)) != NULL) { 5691105197Ssam if (key_blockacq_count < newacq->count) { 5692105197Ssam /* reset counter and do send message. */ 5693105197Ssam newacq->count = 0; 5694105197Ssam } else { 5695105197Ssam /* increment counter and do nothing. */ 5696105197Ssam newacq->count++; 5697105197Ssam return 0; 5698105197Ssam } 5699105197Ssam } else { 5700105197Ssam /* make new entry for blocking to send SADB_ACQUIRE. */ 5701105197Ssam if ((newacq = key_newacq(saidx)) == NULL) 5702105197Ssam return ENOBUFS; 5703105197Ssam } 5704105197Ssam 5705105197Ssam 5706105197Ssam seq = newacq->seq; 5707105197Ssam m = key_setsadbmsg(SADB_ACQUIRE, 0, satype, seq, 0, 0); 5708105197Ssam if (!m) { 5709105197Ssam error = ENOBUFS; 5710105197Ssam goto fail; 5711105197Ssam } 5712105197Ssam result = m; 5713105197Ssam 5714105197Ssam /* set sadb_address for saidx's. */ 5715105197Ssam m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC, 5716105197Ssam &saidx->src.sa, FULLMASK, IPSEC_ULPROTO_ANY); 5717105197Ssam if (!m) { 5718105197Ssam error = ENOBUFS; 5719105197Ssam goto fail; 5720105197Ssam } 5721105197Ssam m_cat(result, m); 5722105197Ssam 5723105197Ssam m = key_setsadbaddr(SADB_EXT_ADDRESS_DST, 5724105197Ssam &saidx->dst.sa, FULLMASK, IPSEC_ULPROTO_ANY); 5725105197Ssam if (!m) { 5726105197Ssam error = ENOBUFS; 5727105197Ssam goto fail; 5728105197Ssam } 5729105197Ssam m_cat(result, m); 5730105197Ssam 5731105197Ssam /* XXX proxy address (optional) */ 5732105197Ssam 5733105197Ssam /* set sadb_x_policy */ 5734105197Ssam if (sp) { 5735105197Ssam m = key_setsadbxpolicy(sp->policy, sp->spidx.dir, sp->id); 5736105197Ssam if (!m) { 5737105197Ssam error = ENOBUFS; 5738105197Ssam goto fail; 5739105197Ssam } 5740105197Ssam m_cat(result, m); 5741105197Ssam } 5742105197Ssam 5743105197Ssam /* XXX identity (optional) */ 5744105197Ssam#if 0 5745105197Ssam if (idexttype && fqdn) { 5746105197Ssam /* create identity extension (FQDN) */ 5747105197Ssam struct sadb_ident *id; 5748105197Ssam int fqdnlen; 5749105197Ssam 5750105197Ssam fqdnlen = strlen(fqdn) + 1; /* +1 for terminating-NUL */ 5751105197Ssam id = (struct sadb_ident *)p; 5752105197Ssam bzero(id, sizeof(*id) + PFKEY_ALIGN8(fqdnlen)); 5753105197Ssam id->sadb_ident_len = PFKEY_UNIT64(sizeof(*id) + PFKEY_ALIGN8(fqdnlen)); 5754105197Ssam id->sadb_ident_exttype = idexttype; 5755105197Ssam id->sadb_ident_type = SADB_IDENTTYPE_FQDN; 5756105197Ssam bcopy(fqdn, id + 1, fqdnlen); 5757105197Ssam p += sizeof(struct sadb_ident) + PFKEY_ALIGN8(fqdnlen); 5758105197Ssam } 5759105197Ssam 5760105197Ssam if (idexttype) { 5761105197Ssam /* create identity extension (USERFQDN) */ 5762105197Ssam struct sadb_ident *id; 5763105197Ssam int userfqdnlen; 5764105197Ssam 5765105197Ssam if (userfqdn) { 5766105197Ssam /* +1 for terminating-NUL */ 5767105197Ssam userfqdnlen = strlen(userfqdn) + 1; 5768105197Ssam } else 5769105197Ssam userfqdnlen = 0; 5770105197Ssam id = (struct sadb_ident *)p; 5771105197Ssam bzero(id, sizeof(*id) + PFKEY_ALIGN8(userfqdnlen)); 5772105197Ssam id->sadb_ident_len = PFKEY_UNIT64(sizeof(*id) + PFKEY_ALIGN8(userfqdnlen)); 5773105197Ssam id->sadb_ident_exttype = idexttype; 5774105197Ssam id->sadb_ident_type = SADB_IDENTTYPE_USERFQDN; 5775105197Ssam /* XXX is it correct? */ 5776105197Ssam if (curproc && curproc->p_cred) 5777105197Ssam id->sadb_ident_id = curproc->p_cred->p_ruid; 5778105197Ssam if (userfqdn && userfqdnlen) 5779105197Ssam bcopy(userfqdn, id + 1, userfqdnlen); 5780105197Ssam p += sizeof(struct sadb_ident) + PFKEY_ALIGN8(userfqdnlen); 5781105197Ssam } 5782105197Ssam#endif 5783105197Ssam 5784105197Ssam /* XXX sensitivity (optional) */ 5785105197Ssam 5786105197Ssam /* create proposal/combination extension */ 5787105197Ssam m = key_getprop(saidx); 5788105197Ssam#if 0 5789105197Ssam /* 5790105197Ssam * spec conformant: always attach proposal/combination extension, 5791105197Ssam * the problem is that we have no way to attach it for ipcomp, 5792105197Ssam * due to the way sadb_comb is declared in RFC2367. 5793105197Ssam */ 5794105197Ssam if (!m) { 5795105197Ssam error = ENOBUFS; 5796105197Ssam goto fail; 5797105197Ssam } 5798105197Ssam m_cat(result, m); 5799105197Ssam#else 5800105197Ssam /* 5801105197Ssam * outside of spec; make proposal/combination extension optional. 5802105197Ssam */ 5803105197Ssam if (m) 5804105197Ssam m_cat(result, m); 5805105197Ssam#endif 5806105197Ssam 5807105197Ssam if ((result->m_flags & M_PKTHDR) == 0) { 5808105197Ssam error = EINVAL; 5809105197Ssam goto fail; 5810105197Ssam } 5811105197Ssam 5812105197Ssam if (result->m_len < sizeof(struct sadb_msg)) { 5813105197Ssam result = m_pullup(result, sizeof(struct sadb_msg)); 5814105197Ssam if (result == NULL) { 5815105197Ssam error = ENOBUFS; 5816105197Ssam goto fail; 5817105197Ssam } 5818105197Ssam } 5819105197Ssam 5820105197Ssam result->m_pkthdr.len = 0; 5821105197Ssam for (m = result; m; m = m->m_next) 5822105197Ssam result->m_pkthdr.len += m->m_len; 5823105197Ssam 5824105197Ssam mtod(result, struct sadb_msg *)->sadb_msg_len = 5825105197Ssam PFKEY_UNIT64(result->m_pkthdr.len); 5826105197Ssam 5827105197Ssam return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED); 5828105197Ssam 5829105197Ssam fail: 5830105197Ssam if (result) 5831105197Ssam m_freem(result); 5832105197Ssam return error; 5833105197Ssam} 5834105197Ssam 5835105197Ssamstatic struct secacq * 5836105197Ssamkey_newacq(const struct secasindex *saidx) 5837105197Ssam{ 5838105197Ssam struct secacq *newacq; 5839105197Ssam 5840105197Ssam /* get new entry */ 5841119643Ssam newacq = malloc(sizeof(struct secacq), M_IPSEC_SAQ, M_NOWAIT|M_ZERO); 5842105197Ssam if (newacq == NULL) { 5843120585Ssam ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__)); 5844105197Ssam return NULL; 5845105197Ssam } 5846105197Ssam 5847105197Ssam /* copy secindex */ 5848105197Ssam bcopy(saidx, &newacq->saidx, sizeof(newacq->saidx)); 5849105197Ssam newacq->seq = (acq_seq == ~0 ? 1 : ++acq_seq); 5850105197Ssam newacq->created = time_second; 5851105197Ssam newacq->count = 0; 5852105197Ssam 5853119643Ssam /* add to acqtree */ 5854120585Ssam ACQ_LOCK(); 5855119643Ssam LIST_INSERT_HEAD(&acqtree, newacq, chain); 5856120585Ssam ACQ_UNLOCK(); 5857119643Ssam 5858105197Ssam return newacq; 5859105197Ssam} 5860105197Ssam 5861105197Ssamstatic struct secacq * 5862105197Ssamkey_getacq(const struct secasindex *saidx) 5863105197Ssam{ 5864105197Ssam struct secacq *acq; 5865105197Ssam 5866120585Ssam ACQ_LOCK(); 5867105197Ssam LIST_FOREACH(acq, &acqtree, chain) { 5868105197Ssam if (key_cmpsaidx(saidx, &acq->saidx, CMP_EXACTLY)) 5869119643Ssam break; 5870105197Ssam } 5871120585Ssam ACQ_UNLOCK(); 5872105197Ssam 5873119643Ssam return acq; 5874105197Ssam} 5875105197Ssam 5876105197Ssamstatic struct secacq * 5877105197Ssamkey_getacqbyseq(seq) 5878105197Ssam u_int32_t seq; 5879105197Ssam{ 5880105197Ssam struct secacq *acq; 5881105197Ssam 5882120585Ssam ACQ_LOCK(); 5883105197Ssam LIST_FOREACH(acq, &acqtree, chain) { 5884105197Ssam if (acq->seq == seq) 5885119643Ssam break; 5886105197Ssam } 5887120585Ssam ACQ_UNLOCK(); 5888105197Ssam 5889119643Ssam return acq; 5890105197Ssam} 5891105197Ssam 5892105197Ssamstatic struct secspacq * 5893105197Ssamkey_newspacq(spidx) 5894105197Ssam struct secpolicyindex *spidx; 5895105197Ssam{ 5896105197Ssam struct secspacq *acq; 5897105197Ssam 5898105197Ssam /* get new entry */ 5899119643Ssam acq = malloc(sizeof(struct secspacq), M_IPSEC_SAQ, M_NOWAIT|M_ZERO); 5900105197Ssam if (acq == NULL) { 5901120585Ssam ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__)); 5902105197Ssam return NULL; 5903105197Ssam } 5904105197Ssam 5905105197Ssam /* copy secindex */ 5906105197Ssam bcopy(spidx, &acq->spidx, sizeof(acq->spidx)); 5907105197Ssam acq->created = time_second; 5908105197Ssam acq->count = 0; 5909105197Ssam 5910119643Ssam /* add to spacqtree */ 5911120585Ssam SPACQ_LOCK(); 5912119643Ssam LIST_INSERT_HEAD(&spacqtree, acq, chain); 5913120585Ssam SPACQ_UNLOCK(); 5914119643Ssam 5915105197Ssam return acq; 5916105197Ssam} 5917105197Ssam 5918105197Ssamstatic struct secspacq * 5919105197Ssamkey_getspacq(spidx) 5920105197Ssam struct secpolicyindex *spidx; 5921105197Ssam{ 5922105197Ssam struct secspacq *acq; 5923105197Ssam 5924120585Ssam SPACQ_LOCK(); 5925105197Ssam LIST_FOREACH(acq, &spacqtree, chain) { 5926119643Ssam if (key_cmpspidx_exactly(spidx, &acq->spidx)) { 5927119643Ssam /* NB: return holding spacq_lock */ 5928105197Ssam return acq; 5929119643Ssam } 5930105197Ssam } 5931120585Ssam SPACQ_UNLOCK(); 5932105197Ssam 5933105197Ssam return NULL; 5934105197Ssam} 5935105197Ssam 5936105197Ssam/* 5937105197Ssam * SADB_ACQUIRE processing, 5938105197Ssam * in first situation, is receiving 5939105197Ssam * <base> 5940105197Ssam * from the ikmpd, and clear sequence of its secasvar entry. 5941105197Ssam * 5942105197Ssam * In second situation, is receiving 5943105197Ssam * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal> 5944105197Ssam * from a user land process, and return 5945105197Ssam * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal> 5946105197Ssam * to the socket. 5947105197Ssam * 5948105197Ssam * m will always be freed. 5949105197Ssam */ 5950105197Ssamstatic int 5951105197Ssamkey_acquire2(so, m, mhp) 5952105197Ssam struct socket *so; 5953105197Ssam struct mbuf *m; 5954105197Ssam const struct sadb_msghdr *mhp; 5955105197Ssam{ 5956105197Ssam const struct sadb_address *src0, *dst0; 5957105197Ssam struct secasindex saidx; 5958105197Ssam struct secashead *sah; 5959105197Ssam u_int16_t proto; 5960105197Ssam int error; 5961105197Ssam 5962120585Ssam IPSEC_ASSERT(so != NULL, ("null socket")); 5963120585Ssam IPSEC_ASSERT(m != NULL, ("null mbuf")); 5964120585Ssam IPSEC_ASSERT(mhp != NULL, ("null msghdr")); 5965120585Ssam IPSEC_ASSERT(mhp->msg != NULL, ("null msg")); 5966105197Ssam 5967105197Ssam /* 5968105197Ssam * Error message from KMd. 5969105197Ssam * We assume that if error was occured in IKEd, the length of PFKEY 5970105197Ssam * message is equal to the size of sadb_msg structure. 5971105197Ssam * We do not raise error even if error occured in this function. 5972105197Ssam */ 5973105197Ssam if (mhp->msg->sadb_msg_len == PFKEY_UNIT64(sizeof(struct sadb_msg))) { 5974105197Ssam struct secacq *acq; 5975105197Ssam 5976105197Ssam /* check sequence number */ 5977105197Ssam if (mhp->msg->sadb_msg_seq == 0) { 5978120585Ssam ipseclog((LOG_DEBUG, "%s: must specify sequence " 5979120585Ssam "number.\n", __func__)); 5980105197Ssam m_freem(m); 5981105197Ssam return 0; 5982105197Ssam } 5983105197Ssam 5984105197Ssam if ((acq = key_getacqbyseq(mhp->msg->sadb_msg_seq)) == NULL) { 5985105197Ssam /* 5986105197Ssam * the specified larval SA is already gone, or we got 5987105197Ssam * a bogus sequence number. we can silently ignore it. 5988105197Ssam */ 5989105197Ssam m_freem(m); 5990105197Ssam return 0; 5991105197Ssam } 5992105197Ssam 5993105197Ssam /* reset acq counter in order to deletion by timehander. */ 5994105197Ssam acq->created = time_second; 5995105197Ssam acq->count = 0; 5996105197Ssam m_freem(m); 5997105197Ssam return 0; 5998105197Ssam } 5999105197Ssam 6000105197Ssam /* 6001105197Ssam * This message is from user land. 6002105197Ssam */ 6003105197Ssam 6004105197Ssam /* map satype to proto */ 6005105197Ssam if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) { 6006120585Ssam ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n", 6007120585Ssam __func__)); 6008105197Ssam return key_senderror(so, m, EINVAL); 6009105197Ssam } 6010105197Ssam 6011105197Ssam if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL || 6012105197Ssam mhp->ext[SADB_EXT_ADDRESS_DST] == NULL || 6013105197Ssam mhp->ext[SADB_EXT_PROPOSAL] == NULL) { 6014105197Ssam /* error */ 6015120585Ssam ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n", 6016120585Ssam __func__)); 6017105197Ssam return key_senderror(so, m, EINVAL); 6018105197Ssam } 6019105197Ssam if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) || 6020105197Ssam mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) || 6021105197Ssam mhp->extlen[SADB_EXT_PROPOSAL] < sizeof(struct sadb_prop)) { 6022105197Ssam /* error */ 6023120585Ssam ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n", 6024120585Ssam __func__)); 6025105197Ssam return key_senderror(so, m, EINVAL); 6026105197Ssam } 6027105197Ssam 6028105197Ssam src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC]; 6029105197Ssam dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST]; 6030105197Ssam 6031105197Ssam /* XXX boundary check against sa_len */ 6032105197Ssam KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx); 6033105197Ssam 6034105197Ssam /* get a SA index */ 6035120585Ssam SAHTREE_LOCK(); 6036105197Ssam LIST_FOREACH(sah, &sahtree, chain) { 6037105197Ssam if (sah->state == SADB_SASTATE_DEAD) 6038105197Ssam continue; 6039105197Ssam if (key_cmpsaidx(&sah->saidx, &saidx, CMP_MODE_REQID)) 6040105197Ssam break; 6041105197Ssam } 6042120585Ssam SAHTREE_UNLOCK(); 6043105197Ssam if (sah != NULL) { 6044120585Ssam ipseclog((LOG_DEBUG, "%s: a SA exists already.\n", __func__)); 6045105197Ssam return key_senderror(so, m, EEXIST); 6046105197Ssam } 6047105197Ssam 6048105197Ssam error = key_acquire(&saidx, NULL); 6049105197Ssam if (error != 0) { 6050120585Ssam ipseclog((LOG_DEBUG, "%s: error %d returned from key_acquire\n", 6051120585Ssam __func__, mhp->msg->sadb_msg_errno)); 6052105197Ssam return key_senderror(so, m, error); 6053105197Ssam } 6054105197Ssam 6055105197Ssam return key_sendup_mbuf(so, m, KEY_SENDUP_REGISTERED); 6056105197Ssam} 6057105197Ssam 6058105197Ssam/* 6059105197Ssam * SADB_REGISTER processing. 6060105197Ssam * If SATYPE_UNSPEC has been passed as satype, only return sabd_supported. 6061105197Ssam * receive 6062105197Ssam * <base> 6063105197Ssam * from the ikmpd, and register a socket to send PF_KEY messages, 6064105197Ssam * and send 6065105197Ssam * <base, supported> 6066105197Ssam * to KMD by PF_KEY. 6067105197Ssam * If socket is detached, must free from regnode. 6068105197Ssam * 6069105197Ssam * m will always be freed. 6070105197Ssam */ 6071105197Ssamstatic int 6072105197Ssamkey_register(so, m, mhp) 6073105197Ssam struct socket *so; 6074105197Ssam struct mbuf *m; 6075105197Ssam const struct sadb_msghdr *mhp; 6076105197Ssam{ 6077105197Ssam struct secreg *reg, *newreg = 0; 6078105197Ssam 6079120585Ssam IPSEC_ASSERT(so != NULL, ("null socket")); 6080120585Ssam IPSEC_ASSERT(m != NULL, ("null mbuf")); 6081120585Ssam IPSEC_ASSERT(mhp != NULL, ("null msghdr")); 6082120585Ssam IPSEC_ASSERT(mhp->msg != NULL, ("null msg")); 6083105197Ssam 6084105197Ssam /* check for invalid register message */ 6085105197Ssam if (mhp->msg->sadb_msg_satype >= sizeof(regtree)/sizeof(regtree[0])) 6086105197Ssam return key_senderror(so, m, EINVAL); 6087105197Ssam 6088105197Ssam /* When SATYPE_UNSPEC is specified, only return sabd_supported. */ 6089105197Ssam if (mhp->msg->sadb_msg_satype == SADB_SATYPE_UNSPEC) 6090105197Ssam goto setmsg; 6091105197Ssam 6092105197Ssam /* check whether existing or not */ 6093120585Ssam REGTREE_LOCK(); 6094105197Ssam LIST_FOREACH(reg, ®tree[mhp->msg->sadb_msg_satype], chain) { 6095105197Ssam if (reg->so == so) { 6096120585Ssam REGTREE_UNLOCK(); 6097120585Ssam ipseclog((LOG_DEBUG, "%s: socket exists already.\n", 6098120585Ssam __func__)); 6099105197Ssam return key_senderror(so, m, EEXIST); 6100105197Ssam } 6101105197Ssam } 6102105197Ssam 6103105197Ssam /* create regnode */ 6104119643Ssam newreg = malloc(sizeof(struct secreg), M_IPSEC_SAR, M_NOWAIT|M_ZERO); 6105105197Ssam if (newreg == NULL) { 6106120585Ssam REGTREE_UNLOCK(); 6107120585Ssam ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__)); 6108105197Ssam return key_senderror(so, m, ENOBUFS); 6109105197Ssam } 6110105197Ssam 6111105197Ssam newreg->so = so; 6112105197Ssam ((struct keycb *)sotorawcb(so))->kp_registered++; 6113105197Ssam 6114105197Ssam /* add regnode to regtree. */ 6115105197Ssam LIST_INSERT_HEAD(®tree[mhp->msg->sadb_msg_satype], newreg, chain); 6116120585Ssam REGTREE_UNLOCK(); 6117105197Ssam 6118105197Ssam setmsg: 6119105197Ssam { 6120105197Ssam struct mbuf *n; 6121105197Ssam struct sadb_msg *newmsg; 6122105197Ssam struct sadb_supported *sup; 6123105197Ssam u_int len, alen, elen; 6124105197Ssam int off; 6125105197Ssam int i; 6126105197Ssam struct sadb_alg *alg; 6127105197Ssam 6128105197Ssam /* create new sadb_msg to reply. */ 6129105197Ssam alen = 0; 6130105197Ssam for (i = 1; i <= SADB_AALG_MAX; i++) { 6131105197Ssam if (ah_algorithm_lookup(i)) 6132105197Ssam alen += sizeof(struct sadb_alg); 6133105197Ssam } 6134105197Ssam if (alen) 6135105197Ssam alen += sizeof(struct sadb_supported); 6136105197Ssam elen = 0; 6137105197Ssam for (i = 1; i <= SADB_EALG_MAX; i++) { 6138105197Ssam if (esp_algorithm_lookup(i)) 6139105197Ssam elen += sizeof(struct sadb_alg); 6140105197Ssam } 6141105197Ssam if (elen) 6142105197Ssam elen += sizeof(struct sadb_supported); 6143105197Ssam 6144105197Ssam len = sizeof(struct sadb_msg) + alen + elen; 6145105197Ssam 6146105197Ssam if (len > MCLBYTES) 6147105197Ssam return key_senderror(so, m, ENOBUFS); 6148105197Ssam 6149111119Simp MGETHDR(n, M_DONTWAIT, MT_DATA); 6150105197Ssam if (len > MHLEN) { 6151111119Simp MCLGET(n, M_DONTWAIT); 6152105197Ssam if ((n->m_flags & M_EXT) == 0) { 6153105197Ssam m_freem(n); 6154105197Ssam n = NULL; 6155105197Ssam } 6156105197Ssam } 6157105197Ssam if (!n) 6158105197Ssam return key_senderror(so, m, ENOBUFS); 6159105197Ssam 6160105197Ssam n->m_pkthdr.len = n->m_len = len; 6161105197Ssam n->m_next = NULL; 6162105197Ssam off = 0; 6163105197Ssam 6164105197Ssam m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off); 6165105197Ssam newmsg = mtod(n, struct sadb_msg *); 6166105197Ssam newmsg->sadb_msg_errno = 0; 6167105197Ssam newmsg->sadb_msg_len = PFKEY_UNIT64(len); 6168105197Ssam off += PFKEY_ALIGN8(sizeof(struct sadb_msg)); 6169105197Ssam 6170105197Ssam /* for authentication algorithm */ 6171105197Ssam if (alen) { 6172105197Ssam sup = (struct sadb_supported *)(mtod(n, caddr_t) + off); 6173105197Ssam sup->sadb_supported_len = PFKEY_UNIT64(alen); 6174105197Ssam sup->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH; 6175105197Ssam off += PFKEY_ALIGN8(sizeof(*sup)); 6176105197Ssam 6177105197Ssam for (i = 1; i <= SADB_AALG_MAX; i++) { 6178105197Ssam struct auth_hash *aalgo; 6179105197Ssam u_int16_t minkeysize, maxkeysize; 6180105197Ssam 6181105197Ssam aalgo = ah_algorithm_lookup(i); 6182105197Ssam if (!aalgo) 6183105197Ssam continue; 6184105197Ssam alg = (struct sadb_alg *)(mtod(n, caddr_t) + off); 6185105197Ssam alg->sadb_alg_id = i; 6186105197Ssam alg->sadb_alg_ivlen = 0; 6187105197Ssam key_getsizes_ah(aalgo, i, &minkeysize, &maxkeysize); 6188105197Ssam alg->sadb_alg_minbits = _BITS(minkeysize); 6189105197Ssam alg->sadb_alg_maxbits = _BITS(maxkeysize); 6190105197Ssam off += PFKEY_ALIGN8(sizeof(*alg)); 6191105197Ssam } 6192105197Ssam } 6193105197Ssam 6194105197Ssam /* for encryption algorithm */ 6195105197Ssam if (elen) { 6196105197Ssam sup = (struct sadb_supported *)(mtod(n, caddr_t) + off); 6197105197Ssam sup->sadb_supported_len = PFKEY_UNIT64(elen); 6198105197Ssam sup->sadb_supported_exttype = SADB_EXT_SUPPORTED_ENCRYPT; 6199105197Ssam off += PFKEY_ALIGN8(sizeof(*sup)); 6200105197Ssam 6201105197Ssam for (i = 1; i <= SADB_EALG_MAX; i++) { 6202105197Ssam struct enc_xform *ealgo; 6203105197Ssam 6204105197Ssam ealgo = esp_algorithm_lookup(i); 6205105197Ssam if (!ealgo) 6206105197Ssam continue; 6207105197Ssam alg = (struct sadb_alg *)(mtod(n, caddr_t) + off); 6208105197Ssam alg->sadb_alg_id = i; 6209105197Ssam alg->sadb_alg_ivlen = ealgo->blocksize; 6210105197Ssam alg->sadb_alg_minbits = _BITS(ealgo->minkey); 6211105197Ssam alg->sadb_alg_maxbits = _BITS(ealgo->maxkey); 6212105197Ssam off += PFKEY_ALIGN8(sizeof(struct sadb_alg)); 6213105197Ssam } 6214105197Ssam } 6215105197Ssam 6216120585Ssam IPSEC_ASSERT(off == len, 6217120585Ssam ("length assumption failed (off %u len %u)", off, len)); 6218105197Ssam 6219105197Ssam m_freem(m); 6220105197Ssam return key_sendup_mbuf(so, n, KEY_SENDUP_REGISTERED); 6221105197Ssam } 6222105197Ssam} 6223105197Ssam 6224105197Ssam/* 6225105197Ssam * free secreg entry registered. 6226105197Ssam * XXX: I want to do free a socket marked done SADB_RESIGER to socket. 6227105197Ssam */ 6228105197Ssamvoid 6229119643Ssamkey_freereg(struct socket *so) 6230105197Ssam{ 6231105197Ssam struct secreg *reg; 6232105197Ssam int i; 6233105197Ssam 6234120585Ssam IPSEC_ASSERT(so != NULL, ("NULL so")); 6235105197Ssam 6236105197Ssam /* 6237105197Ssam * check whether existing or not. 6238105197Ssam * check all type of SA, because there is a potential that 6239105197Ssam * one socket is registered to multiple type of SA. 6240105197Ssam */ 6241120585Ssam REGTREE_LOCK(); 6242105197Ssam for (i = 0; i <= SADB_SATYPE_MAX; i++) { 6243105197Ssam LIST_FOREACH(reg, ®tree[i], chain) { 6244119643Ssam if (reg->so == so && __LIST_CHAINED(reg)) { 6245105197Ssam LIST_REMOVE(reg, chain); 6246119643Ssam free(reg, M_IPSEC_SAR); 6247105197Ssam break; 6248105197Ssam } 6249105197Ssam } 6250105197Ssam } 6251120585Ssam REGTREE_UNLOCK(); 6252105197Ssam} 6253105197Ssam 6254105197Ssam/* 6255105197Ssam * SADB_EXPIRE processing 6256105197Ssam * send 6257105197Ssam * <base, SA, SA2, lifetime(C and one of HS), address(SD)> 6258105197Ssam * to KMD by PF_KEY. 6259105197Ssam * NOTE: We send only soft lifetime extension. 6260105197Ssam * 6261105197Ssam * OUT: 0 : succeed 6262105197Ssam * others : error number 6263105197Ssam */ 6264105197Ssamstatic int 6265119643Ssamkey_expire(struct secasvar *sav) 6266105197Ssam{ 6267105197Ssam int s; 6268105197Ssam int satype; 6269105197Ssam struct mbuf *result = NULL, *m; 6270105197Ssam int len; 6271105197Ssam int error = -1; 6272105197Ssam struct sadb_lifetime *lt; 6273105197Ssam 6274105197Ssam /* XXX: Why do we lock ? */ 6275105197Ssam s = splnet(); /*called from softclock()*/ 6276105197Ssam 6277120585Ssam IPSEC_ASSERT (sav != NULL, ("null sav")); 6278120585Ssam IPSEC_ASSERT (sav->sah != NULL, ("null sa header")); 6279105197Ssam 6280105197Ssam /* set msg header */ 6281120585Ssam satype = key_proto2satype(sav->sah->saidx.proto); 6282120585Ssam IPSEC_ASSERT(satype != 0, ("invalid proto, satype %u", satype)); 6283105197Ssam m = key_setsadbmsg(SADB_EXPIRE, 0, satype, sav->seq, 0, sav->refcnt); 6284105197Ssam if (!m) { 6285105197Ssam error = ENOBUFS; 6286105197Ssam goto fail; 6287105197Ssam } 6288105197Ssam result = m; 6289105197Ssam 6290105197Ssam /* create SA extension */ 6291105197Ssam m = key_setsadbsa(sav); 6292105197Ssam if (!m) { 6293105197Ssam error = ENOBUFS; 6294105197Ssam goto fail; 6295105197Ssam } 6296105197Ssam m_cat(result, m); 6297105197Ssam 6298105197Ssam /* create SA extension */ 6299105197Ssam m = key_setsadbxsa2(sav->sah->saidx.mode, 6300105197Ssam sav->replay ? sav->replay->count : 0, 6301105197Ssam sav->sah->saidx.reqid); 6302105197Ssam if (!m) { 6303105197Ssam error = ENOBUFS; 6304105197Ssam goto fail; 6305105197Ssam } 6306105197Ssam m_cat(result, m); 6307105197Ssam 6308105197Ssam /* create lifetime extension (current and soft) */ 6309105197Ssam len = PFKEY_ALIGN8(sizeof(*lt)) * 2; 6310105197Ssam m = key_alloc_mbuf(len); 6311105197Ssam if (!m || m->m_next) { /*XXX*/ 6312105197Ssam if (m) 6313105197Ssam m_freem(m); 6314105197Ssam error = ENOBUFS; 6315105197Ssam goto fail; 6316105197Ssam } 6317105197Ssam bzero(mtod(m, caddr_t), len); 6318105197Ssam lt = mtod(m, struct sadb_lifetime *); 6319105197Ssam lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime)); 6320105197Ssam lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT; 6321157123Sgnn lt->sadb_lifetime_allocations = sav->lft_c->allocations; 6322157123Sgnn lt->sadb_lifetime_bytes = sav->lft_c->bytes; 6323157123Sgnn lt->sadb_lifetime_addtime = sav->lft_c->addtime; 6324157123Sgnn lt->sadb_lifetime_usetime = sav->lft_c->usetime; 6325105197Ssam lt = (struct sadb_lifetime *)(mtod(m, caddr_t) + len / 2); 6326176743Sbz lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime)); 6327176743Sbz lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT; 6328176743Sbz lt->sadb_lifetime_allocations = sav->lft_s->allocations; 6329176743Sbz lt->sadb_lifetime_bytes = sav->lft_s->bytes; 6330176743Sbz lt->sadb_lifetime_addtime = sav->lft_s->addtime; 6331176743Sbz lt->sadb_lifetime_usetime = sav->lft_s->usetime; 6332105197Ssam m_cat(result, m); 6333105197Ssam 6334105197Ssam /* set sadb_address for source */ 6335105197Ssam m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC, 6336105197Ssam &sav->sah->saidx.src.sa, 6337105197Ssam FULLMASK, IPSEC_ULPROTO_ANY); 6338105197Ssam if (!m) { 6339105197Ssam error = ENOBUFS; 6340105197Ssam goto fail; 6341105197Ssam } 6342105197Ssam m_cat(result, m); 6343105197Ssam 6344105197Ssam /* set sadb_address for destination */ 6345105197Ssam m = key_setsadbaddr(SADB_EXT_ADDRESS_DST, 6346105197Ssam &sav->sah->saidx.dst.sa, 6347105197Ssam FULLMASK, IPSEC_ULPROTO_ANY); 6348105197Ssam if (!m) { 6349105197Ssam error = ENOBUFS; 6350105197Ssam goto fail; 6351105197Ssam } 6352105197Ssam m_cat(result, m); 6353105197Ssam 6354105197Ssam if ((result->m_flags & M_PKTHDR) == 0) { 6355105197Ssam error = EINVAL; 6356105197Ssam goto fail; 6357105197Ssam } 6358105197Ssam 6359105197Ssam if (result->m_len < sizeof(struct sadb_msg)) { 6360105197Ssam result = m_pullup(result, sizeof(struct sadb_msg)); 6361105197Ssam if (result == NULL) { 6362105197Ssam error = ENOBUFS; 6363105197Ssam goto fail; 6364105197Ssam } 6365105197Ssam } 6366105197Ssam 6367105197Ssam result->m_pkthdr.len = 0; 6368105197Ssam for (m = result; m; m = m->m_next) 6369105197Ssam result->m_pkthdr.len += m->m_len; 6370105197Ssam 6371105197Ssam mtod(result, struct sadb_msg *)->sadb_msg_len = 6372105197Ssam PFKEY_UNIT64(result->m_pkthdr.len); 6373105197Ssam 6374105197Ssam splx(s); 6375105197Ssam return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED); 6376105197Ssam 6377105197Ssam fail: 6378105197Ssam if (result) 6379105197Ssam m_freem(result); 6380105197Ssam splx(s); 6381105197Ssam return error; 6382105197Ssam} 6383105197Ssam 6384105197Ssam/* 6385105197Ssam * SADB_FLUSH processing 6386105197Ssam * receive 6387105197Ssam * <base> 6388105197Ssam * from the ikmpd, and free all entries in secastree. 6389105197Ssam * and send, 6390105197Ssam * <base> 6391105197Ssam * to the ikmpd. 6392105197Ssam * NOTE: to do is only marking SADB_SASTATE_DEAD. 6393105197Ssam * 6394105197Ssam * m will always be freed. 6395105197Ssam */ 6396105197Ssamstatic int 6397105197Ssamkey_flush(so, m, mhp) 6398105197Ssam struct socket *so; 6399105197Ssam struct mbuf *m; 6400105197Ssam const struct sadb_msghdr *mhp; 6401105197Ssam{ 6402105197Ssam struct sadb_msg *newmsg; 6403105197Ssam struct secashead *sah, *nextsah; 6404105197Ssam struct secasvar *sav, *nextsav; 6405105197Ssam u_int16_t proto; 6406105197Ssam u_int8_t state; 6407105197Ssam u_int stateidx; 6408105197Ssam 6409120585Ssam IPSEC_ASSERT(so != NULL, ("null socket")); 6410120585Ssam IPSEC_ASSERT(mhp != NULL, ("null msghdr")); 6411120585Ssam IPSEC_ASSERT(mhp->msg != NULL, ("null msg")); 6412105197Ssam 6413105197Ssam /* map satype to proto */ 6414105197Ssam if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) { 6415120585Ssam ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n", 6416120585Ssam __func__)); 6417105197Ssam return key_senderror(so, m, EINVAL); 6418105197Ssam } 6419105197Ssam 6420105197Ssam /* no SATYPE specified, i.e. flushing all SA. */ 6421120585Ssam SAHTREE_LOCK(); 6422105197Ssam for (sah = LIST_FIRST(&sahtree); 6423105197Ssam sah != NULL; 6424105197Ssam sah = nextsah) { 6425105197Ssam nextsah = LIST_NEXT(sah, chain); 6426105197Ssam 6427105197Ssam if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC 6428105197Ssam && proto != sah->saidx.proto) 6429105197Ssam continue; 6430105197Ssam 6431105197Ssam for (stateidx = 0; 6432105197Ssam stateidx < _ARRAYLEN(saorder_state_alive); 6433105197Ssam stateidx++) { 6434105197Ssam state = saorder_state_any[stateidx]; 6435105197Ssam for (sav = LIST_FIRST(&sah->savtree[state]); 6436105197Ssam sav != NULL; 6437105197Ssam sav = nextsav) { 6438105197Ssam 6439105197Ssam nextsav = LIST_NEXT(sav, chain); 6440105197Ssam 6441105197Ssam key_sa_chgstate(sav, SADB_SASTATE_DEAD); 6442105197Ssam KEY_FREESAV(&sav); 6443105197Ssam } 6444105197Ssam } 6445105197Ssam 6446105197Ssam sah->state = SADB_SASTATE_DEAD; 6447105197Ssam } 6448120585Ssam SAHTREE_UNLOCK(); 6449105197Ssam 6450105197Ssam if (m->m_len < sizeof(struct sadb_msg) || 6451105197Ssam sizeof(struct sadb_msg) > m->m_len + M_TRAILINGSPACE(m)) { 6452120585Ssam ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__)); 6453105197Ssam return key_senderror(so, m, ENOBUFS); 6454105197Ssam } 6455105197Ssam 6456105197Ssam if (m->m_next) 6457105197Ssam m_freem(m->m_next); 6458105197Ssam m->m_next = NULL; 6459105197Ssam m->m_pkthdr.len = m->m_len = sizeof(struct sadb_msg); 6460105197Ssam newmsg = mtod(m, struct sadb_msg *); 6461105197Ssam newmsg->sadb_msg_errno = 0; 6462105197Ssam newmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len); 6463105197Ssam 6464105197Ssam return key_sendup_mbuf(so, m, KEY_SENDUP_ALL); 6465105197Ssam} 6466105197Ssam 6467105197Ssam/* 6468105197Ssam * SADB_DUMP processing 6469105197Ssam * dump all entries including status of DEAD in SAD. 6470105197Ssam * receive 6471105197Ssam * <base> 6472105197Ssam * from the ikmpd, and dump all secasvar leaves 6473105197Ssam * and send, 6474105197Ssam * <base> ..... 6475105197Ssam * to the ikmpd. 6476105197Ssam * 6477105197Ssam * m will always be freed. 6478105197Ssam */ 6479105197Ssamstatic int 6480105197Ssamkey_dump(so, m, mhp) 6481105197Ssam struct socket *so; 6482105197Ssam struct mbuf *m; 6483105197Ssam const struct sadb_msghdr *mhp; 6484105197Ssam{ 6485105197Ssam struct secashead *sah; 6486105197Ssam struct secasvar *sav; 6487105197Ssam u_int16_t proto; 6488105197Ssam u_int stateidx; 6489105197Ssam u_int8_t satype; 6490105197Ssam u_int8_t state; 6491105197Ssam int cnt; 6492105197Ssam struct sadb_msg *newmsg; 6493105197Ssam struct mbuf *n; 6494105197Ssam 6495120585Ssam IPSEC_ASSERT(so != NULL, ("null socket")); 6496120585Ssam IPSEC_ASSERT(m != NULL, ("null mbuf")); 6497120585Ssam IPSEC_ASSERT(mhp != NULL, ("null msghdr")); 6498120585Ssam IPSEC_ASSERT(mhp->msg != NULL, ("null msg")); 6499105197Ssam 6500105197Ssam /* map satype to proto */ 6501105197Ssam if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) { 6502120585Ssam ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n", 6503120585Ssam __func__)); 6504105197Ssam return key_senderror(so, m, EINVAL); 6505105197Ssam } 6506105197Ssam 6507105197Ssam /* count sav entries to be sent to the userland. */ 6508105197Ssam cnt = 0; 6509120585Ssam SAHTREE_LOCK(); 6510105197Ssam LIST_FOREACH(sah, &sahtree, chain) { 6511105197Ssam if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC 6512105197Ssam && proto != sah->saidx.proto) 6513105197Ssam continue; 6514105197Ssam 6515105197Ssam for (stateidx = 0; 6516105197Ssam stateidx < _ARRAYLEN(saorder_state_any); 6517105197Ssam stateidx++) { 6518105197Ssam state = saorder_state_any[stateidx]; 6519105197Ssam LIST_FOREACH(sav, &sah->savtree[state], chain) { 6520105197Ssam cnt++; 6521105197Ssam } 6522105197Ssam } 6523105197Ssam } 6524105197Ssam 6525119643Ssam if (cnt == 0) { 6526120585Ssam SAHTREE_UNLOCK(); 6527105197Ssam return key_senderror(so, m, ENOENT); 6528119643Ssam } 6529105197Ssam 6530105197Ssam /* send this to the userland, one at a time. */ 6531105197Ssam newmsg = NULL; 6532105197Ssam LIST_FOREACH(sah, &sahtree, chain) { 6533105197Ssam if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC 6534105197Ssam && proto != sah->saidx.proto) 6535105197Ssam continue; 6536105197Ssam 6537105197Ssam /* map proto to satype */ 6538105197Ssam if ((satype = key_proto2satype(sah->saidx.proto)) == 0) { 6539120585Ssam SAHTREE_UNLOCK(); 6540120585Ssam ipseclog((LOG_DEBUG, "%s: there was invalid proto in " 6541120585Ssam "SAD.\n", __func__)); 6542105197Ssam return key_senderror(so, m, EINVAL); 6543105197Ssam } 6544105197Ssam 6545105197Ssam for (stateidx = 0; 6546105197Ssam stateidx < _ARRAYLEN(saorder_state_any); 6547105197Ssam stateidx++) { 6548105197Ssam state = saorder_state_any[stateidx]; 6549105197Ssam LIST_FOREACH(sav, &sah->savtree[state], chain) { 6550105197Ssam n = key_setdumpsa(sav, SADB_DUMP, satype, 6551105197Ssam --cnt, mhp->msg->sadb_msg_pid); 6552119643Ssam if (!n) { 6553120585Ssam SAHTREE_UNLOCK(); 6554105197Ssam return key_senderror(so, m, ENOBUFS); 6555119643Ssam } 6556105197Ssam key_sendup_mbuf(so, n, KEY_SENDUP_ONE); 6557105197Ssam } 6558105197Ssam } 6559105197Ssam } 6560120585Ssam SAHTREE_UNLOCK(); 6561105197Ssam 6562105197Ssam m_freem(m); 6563105197Ssam return 0; 6564105197Ssam} 6565105197Ssam 6566105197Ssam/* 6567105197Ssam * SADB_X_PROMISC processing 6568105197Ssam * 6569105197Ssam * m will always be freed. 6570105197Ssam */ 6571105197Ssamstatic int 6572105197Ssamkey_promisc(so, m, mhp) 6573105197Ssam struct socket *so; 6574105197Ssam struct mbuf *m; 6575105197Ssam const struct sadb_msghdr *mhp; 6576105197Ssam{ 6577105197Ssam int olen; 6578105197Ssam 6579120585Ssam IPSEC_ASSERT(so != NULL, ("null socket")); 6580120585Ssam IPSEC_ASSERT(m != NULL, ("null mbuf")); 6581120585Ssam IPSEC_ASSERT(mhp != NULL, ("null msghdr")); 6582120585Ssam IPSEC_ASSERT(mhp->msg != NULL, ("null msg")); 6583105197Ssam 6584105197Ssam olen = PFKEY_UNUNIT64(mhp->msg->sadb_msg_len); 6585105197Ssam 6586105197Ssam if (olen < sizeof(struct sadb_msg)) { 6587105197Ssam#if 1 6588105197Ssam return key_senderror(so, m, EINVAL); 6589105197Ssam#else 6590105197Ssam m_freem(m); 6591105197Ssam return 0; 6592105197Ssam#endif 6593105197Ssam } else if (olen == sizeof(struct sadb_msg)) { 6594105197Ssam /* enable/disable promisc mode */ 6595105197Ssam struct keycb *kp; 6596105197Ssam 6597105197Ssam if ((kp = (struct keycb *)sotorawcb(so)) == NULL) 6598105197Ssam return key_senderror(so, m, EINVAL); 6599105197Ssam mhp->msg->sadb_msg_errno = 0; 6600105197Ssam switch (mhp->msg->sadb_msg_satype) { 6601105197Ssam case 0: 6602105197Ssam case 1: 6603105197Ssam kp->kp_promisc = mhp->msg->sadb_msg_satype; 6604105197Ssam break; 6605105197Ssam default: 6606105197Ssam return key_senderror(so, m, EINVAL); 6607105197Ssam } 6608105197Ssam 6609105197Ssam /* send the original message back to everyone */ 6610105197Ssam mhp->msg->sadb_msg_errno = 0; 6611105197Ssam return key_sendup_mbuf(so, m, KEY_SENDUP_ALL); 6612105197Ssam } else { 6613105197Ssam /* send packet as is */ 6614105197Ssam 6615105197Ssam m_adj(m, PFKEY_ALIGN8(sizeof(struct sadb_msg))); 6616105197Ssam 6617105197Ssam /* TODO: if sadb_msg_seq is specified, send to specific pid */ 6618105197Ssam return key_sendup_mbuf(so, m, KEY_SENDUP_ALL); 6619105197Ssam } 6620105197Ssam} 6621105197Ssam 6622105197Ssamstatic int (*key_typesw[]) __P((struct socket *, struct mbuf *, 6623105197Ssam const struct sadb_msghdr *)) = { 6624105197Ssam NULL, /* SADB_RESERVED */ 6625105197Ssam key_getspi, /* SADB_GETSPI */ 6626105197Ssam key_update, /* SADB_UPDATE */ 6627105197Ssam key_add, /* SADB_ADD */ 6628105197Ssam key_delete, /* SADB_DELETE */ 6629105197Ssam key_get, /* SADB_GET */ 6630105197Ssam key_acquire2, /* SADB_ACQUIRE */ 6631105197Ssam key_register, /* SADB_REGISTER */ 6632105197Ssam NULL, /* SADB_EXPIRE */ 6633105197Ssam key_flush, /* SADB_FLUSH */ 6634105197Ssam key_dump, /* SADB_DUMP */ 6635105197Ssam key_promisc, /* SADB_X_PROMISC */ 6636105197Ssam NULL, /* SADB_X_PCHANGE */ 6637105197Ssam key_spdadd, /* SADB_X_SPDUPDATE */ 6638105197Ssam key_spdadd, /* SADB_X_SPDADD */ 6639105197Ssam key_spddelete, /* SADB_X_SPDDELETE */ 6640105197Ssam key_spdget, /* SADB_X_SPDGET */ 6641105197Ssam NULL, /* SADB_X_SPDACQUIRE */ 6642105197Ssam key_spddump, /* SADB_X_SPDDUMP */ 6643105197Ssam key_spdflush, /* SADB_X_SPDFLUSH */ 6644105197Ssam key_spdadd, /* SADB_X_SPDSETIDX */ 6645105197Ssam NULL, /* SADB_X_SPDEXPIRE */ 6646105197Ssam key_spddelete2, /* SADB_X_SPDDELETE2 */ 6647105197Ssam}; 6648105197Ssam 6649105197Ssam/* 6650105197Ssam * parse sadb_msg buffer to process PFKEYv2, 6651105197Ssam * and create a data to response if needed. 6652105197Ssam * I think to be dealed with mbuf directly. 6653105197Ssam * IN: 6654105197Ssam * msgp : pointer to pointer to a received buffer pulluped. 6655105197Ssam * This is rewrited to response. 6656105197Ssam * so : pointer to socket. 6657105197Ssam * OUT: 6658105197Ssam * length for buffer to send to user process. 6659105197Ssam */ 6660105197Ssamint 6661105197Ssamkey_parse(m, so) 6662105197Ssam struct mbuf *m; 6663105197Ssam struct socket *so; 6664105197Ssam{ 6665105197Ssam struct sadb_msg *msg; 6666105197Ssam struct sadb_msghdr mh; 6667105197Ssam u_int orglen; 6668105197Ssam int error; 6669105197Ssam int target; 6670105197Ssam 6671120585Ssam IPSEC_ASSERT(so != NULL, ("null socket")); 6672120585Ssam IPSEC_ASSERT(m != NULL, ("null mbuf")); 6673105197Ssam 6674105197Ssam#if 0 /*kdebug_sadb assumes msg in linear buffer*/ 6675105197Ssam KEYDEBUG(KEYDEBUG_KEY_DUMP, 6676120585Ssam ipseclog((LOG_DEBUG, "%s: passed sadb_msg\n", __func__)); 6677105197Ssam kdebug_sadb(msg)); 6678105197Ssam#endif 6679105197Ssam 6680105197Ssam if (m->m_len < sizeof(struct sadb_msg)) { 6681105197Ssam m = m_pullup(m, sizeof(struct sadb_msg)); 6682105197Ssam if (!m) 6683105197Ssam return ENOBUFS; 6684105197Ssam } 6685105197Ssam msg = mtod(m, struct sadb_msg *); 6686105197Ssam orglen = PFKEY_UNUNIT64(msg->sadb_msg_len); 6687105197Ssam target = KEY_SENDUP_ONE; 6688105197Ssam 6689105197Ssam if ((m->m_flags & M_PKTHDR) == 0 || 6690105197Ssam m->m_pkthdr.len != m->m_pkthdr.len) { 6691120585Ssam ipseclog((LOG_DEBUG, "%s: invalid message length.\n",__func__)); 6692105197Ssam pfkeystat.out_invlen++; 6693105197Ssam error = EINVAL; 6694105197Ssam goto senderror; 6695105197Ssam } 6696105197Ssam 6697105197Ssam if (msg->sadb_msg_version != PF_KEY_V2) { 6698120585Ssam ipseclog((LOG_DEBUG, "%s: PF_KEY version %u is mismatched.\n", 6699120585Ssam __func__, msg->sadb_msg_version)); 6700105197Ssam pfkeystat.out_invver++; 6701105197Ssam error = EINVAL; 6702105197Ssam goto senderror; 6703105197Ssam } 6704105197Ssam 6705105197Ssam if (msg->sadb_msg_type > SADB_MAX) { 6706120585Ssam ipseclog((LOG_DEBUG, "%s: invalid type %u is passed.\n", 6707120585Ssam __func__, msg->sadb_msg_type)); 6708105197Ssam pfkeystat.out_invmsgtype++; 6709105197Ssam error = EINVAL; 6710105197Ssam goto senderror; 6711105197Ssam } 6712105197Ssam 6713105197Ssam /* for old-fashioned code - should be nuked */ 6714105197Ssam if (m->m_pkthdr.len > MCLBYTES) { 6715105197Ssam m_freem(m); 6716105197Ssam return ENOBUFS; 6717105197Ssam } 6718105197Ssam if (m->m_next) { 6719105197Ssam struct mbuf *n; 6720105197Ssam 6721111119Simp MGETHDR(n, M_DONTWAIT, MT_DATA); 6722105197Ssam if (n && m->m_pkthdr.len > MHLEN) { 6723111119Simp MCLGET(n, M_DONTWAIT); 6724105197Ssam if ((n->m_flags & M_EXT) == 0) { 6725105197Ssam m_free(n); 6726105197Ssam n = NULL; 6727105197Ssam } 6728105197Ssam } 6729105197Ssam if (!n) { 6730105197Ssam m_freem(m); 6731105197Ssam return ENOBUFS; 6732105197Ssam } 6733105197Ssam m_copydata(m, 0, m->m_pkthdr.len, mtod(n, caddr_t)); 6734105197Ssam n->m_pkthdr.len = n->m_len = m->m_pkthdr.len; 6735105197Ssam n->m_next = NULL; 6736105197Ssam m_freem(m); 6737105197Ssam m = n; 6738105197Ssam } 6739105197Ssam 6740105197Ssam /* align the mbuf chain so that extensions are in contiguous region. */ 6741105197Ssam error = key_align(m, &mh); 6742105197Ssam if (error) 6743105197Ssam return error; 6744105197Ssam 6745105197Ssam msg = mh.msg; 6746105197Ssam 6747105197Ssam /* check SA type */ 6748105197Ssam switch (msg->sadb_msg_satype) { 6749105197Ssam case SADB_SATYPE_UNSPEC: 6750105197Ssam switch (msg->sadb_msg_type) { 6751105197Ssam case SADB_GETSPI: 6752105197Ssam case SADB_UPDATE: 6753105197Ssam case SADB_ADD: 6754105197Ssam case SADB_DELETE: 6755105197Ssam case SADB_GET: 6756105197Ssam case SADB_ACQUIRE: 6757105197Ssam case SADB_EXPIRE: 6758120585Ssam ipseclog((LOG_DEBUG, "%s: must specify satype " 6759120585Ssam "when msg type=%u.\n", __func__, 6760120585Ssam msg->sadb_msg_type)); 6761105197Ssam pfkeystat.out_invsatype++; 6762105197Ssam error = EINVAL; 6763105197Ssam goto senderror; 6764105197Ssam } 6765105197Ssam break; 6766105197Ssam case SADB_SATYPE_AH: 6767105197Ssam case SADB_SATYPE_ESP: 6768105197Ssam case SADB_X_SATYPE_IPCOMP: 6769125680Sbms case SADB_X_SATYPE_TCPSIGNATURE: 6770105197Ssam switch (msg->sadb_msg_type) { 6771105197Ssam case SADB_X_SPDADD: 6772105197Ssam case SADB_X_SPDDELETE: 6773105197Ssam case SADB_X_SPDGET: 6774105197Ssam case SADB_X_SPDDUMP: 6775105197Ssam case SADB_X_SPDFLUSH: 6776105197Ssam case SADB_X_SPDSETIDX: 6777105197Ssam case SADB_X_SPDUPDATE: 6778105197Ssam case SADB_X_SPDDELETE2: 6779120585Ssam ipseclog((LOG_DEBUG, "%s: illegal satype=%u\n", 6780120585Ssam __func__, msg->sadb_msg_type)); 6781105197Ssam pfkeystat.out_invsatype++; 6782105197Ssam error = EINVAL; 6783105197Ssam goto senderror; 6784105197Ssam } 6785105197Ssam break; 6786105197Ssam case SADB_SATYPE_RSVP: 6787105197Ssam case SADB_SATYPE_OSPFV2: 6788105197Ssam case SADB_SATYPE_RIPV2: 6789105197Ssam case SADB_SATYPE_MIP: 6790120585Ssam ipseclog((LOG_DEBUG, "%s: type %u isn't supported.\n", 6791120585Ssam __func__, msg->sadb_msg_satype)); 6792105197Ssam pfkeystat.out_invsatype++; 6793105197Ssam error = EOPNOTSUPP; 6794105197Ssam goto senderror; 6795105197Ssam case 1: /* XXX: What does it do? */ 6796105197Ssam if (msg->sadb_msg_type == SADB_X_PROMISC) 6797105197Ssam break; 6798105197Ssam /*FALLTHROUGH*/ 6799105197Ssam default: 6800120585Ssam ipseclog((LOG_DEBUG, "%s: invalid type %u is passed.\n", 6801120585Ssam __func__, msg->sadb_msg_satype)); 6802105197Ssam pfkeystat.out_invsatype++; 6803105197Ssam error = EINVAL; 6804105197Ssam goto senderror; 6805105197Ssam } 6806105197Ssam 6807105197Ssam /* check field of upper layer protocol and address family */ 6808105197Ssam if (mh.ext[SADB_EXT_ADDRESS_SRC] != NULL 6809105197Ssam && mh.ext[SADB_EXT_ADDRESS_DST] != NULL) { 6810105197Ssam struct sadb_address *src0, *dst0; 6811105197Ssam u_int plen; 6812105197Ssam 6813105197Ssam src0 = (struct sadb_address *)(mh.ext[SADB_EXT_ADDRESS_SRC]); 6814105197Ssam dst0 = (struct sadb_address *)(mh.ext[SADB_EXT_ADDRESS_DST]); 6815105197Ssam 6816105197Ssam /* check upper layer protocol */ 6817105197Ssam if (src0->sadb_address_proto != dst0->sadb_address_proto) { 6818120585Ssam ipseclog((LOG_DEBUG, "%s: upper layer protocol " 6819120585Ssam "mismatched.\n", __func__)); 6820105197Ssam pfkeystat.out_invaddr++; 6821105197Ssam error = EINVAL; 6822105197Ssam goto senderror; 6823105197Ssam } 6824105197Ssam 6825105197Ssam /* check family */ 6826105197Ssam if (PFKEY_ADDR_SADDR(src0)->sa_family != 6827105197Ssam PFKEY_ADDR_SADDR(dst0)->sa_family) { 6828120585Ssam ipseclog((LOG_DEBUG, "%s: address family mismatched.\n", 6829120585Ssam __func__)); 6830105197Ssam pfkeystat.out_invaddr++; 6831105197Ssam error = EINVAL; 6832105197Ssam goto senderror; 6833105197Ssam } 6834105197Ssam if (PFKEY_ADDR_SADDR(src0)->sa_len != 6835105197Ssam PFKEY_ADDR_SADDR(dst0)->sa_len) { 6836120585Ssam ipseclog((LOG_DEBUG, "%s: address struct size " 6837120585Ssam "mismatched.\n", __func__)); 6838105197Ssam pfkeystat.out_invaddr++; 6839105197Ssam error = EINVAL; 6840105197Ssam goto senderror; 6841105197Ssam } 6842105197Ssam 6843105197Ssam switch (PFKEY_ADDR_SADDR(src0)->sa_family) { 6844105197Ssam case AF_INET: 6845105197Ssam if (PFKEY_ADDR_SADDR(src0)->sa_len != 6846105197Ssam sizeof(struct sockaddr_in)) { 6847105197Ssam pfkeystat.out_invaddr++; 6848105197Ssam error = EINVAL; 6849105197Ssam goto senderror; 6850105197Ssam } 6851105197Ssam break; 6852105197Ssam case AF_INET6: 6853105197Ssam if (PFKEY_ADDR_SADDR(src0)->sa_len != 6854105197Ssam sizeof(struct sockaddr_in6)) { 6855105197Ssam pfkeystat.out_invaddr++; 6856105197Ssam error = EINVAL; 6857105197Ssam goto senderror; 6858105197Ssam } 6859105197Ssam break; 6860105197Ssam default: 6861120585Ssam ipseclog((LOG_DEBUG, "%s: unsupported address family\n", 6862120585Ssam __func__)); 6863105197Ssam pfkeystat.out_invaddr++; 6864105197Ssam error = EAFNOSUPPORT; 6865105197Ssam goto senderror; 6866105197Ssam } 6867105197Ssam 6868105197Ssam switch (PFKEY_ADDR_SADDR(src0)->sa_family) { 6869105197Ssam case AF_INET: 6870105197Ssam plen = sizeof(struct in_addr) << 3; 6871105197Ssam break; 6872105197Ssam case AF_INET6: 6873105197Ssam plen = sizeof(struct in6_addr) << 3; 6874105197Ssam break; 6875105197Ssam default: 6876105197Ssam plen = 0; /*fool gcc*/ 6877105197Ssam break; 6878105197Ssam } 6879105197Ssam 6880105197Ssam /* check max prefix length */ 6881105197Ssam if (src0->sadb_address_prefixlen > plen || 6882105197Ssam dst0->sadb_address_prefixlen > plen) { 6883120585Ssam ipseclog((LOG_DEBUG, "%s: illegal prefixlen.\n", 6884120585Ssam __func__)); 6885105197Ssam pfkeystat.out_invaddr++; 6886105197Ssam error = EINVAL; 6887105197Ssam goto senderror; 6888105197Ssam } 6889105197Ssam 6890105197Ssam /* 6891105197Ssam * prefixlen == 0 is valid because there can be a case when 6892105197Ssam * all addresses are matched. 6893105197Ssam */ 6894105197Ssam } 6895105197Ssam 6896105197Ssam if (msg->sadb_msg_type >= sizeof(key_typesw)/sizeof(key_typesw[0]) || 6897105197Ssam key_typesw[msg->sadb_msg_type] == NULL) { 6898105197Ssam pfkeystat.out_invmsgtype++; 6899105197Ssam error = EINVAL; 6900105197Ssam goto senderror; 6901105197Ssam } 6902105197Ssam 6903105197Ssam return (*key_typesw[msg->sadb_msg_type])(so, m, &mh); 6904105197Ssam 6905105197Ssamsenderror: 6906105197Ssam msg->sadb_msg_errno = error; 6907105197Ssam return key_sendup_mbuf(so, m, target); 6908105197Ssam} 6909105197Ssam 6910105197Ssamstatic int 6911105197Ssamkey_senderror(so, m, code) 6912105197Ssam struct socket *so; 6913105197Ssam struct mbuf *m; 6914105197Ssam int code; 6915105197Ssam{ 6916105197Ssam struct sadb_msg *msg; 6917105197Ssam 6918120585Ssam IPSEC_ASSERT(m->m_len >= sizeof(struct sadb_msg), 6919120585Ssam ("mbuf too small, len %u", m->m_len)); 6920105197Ssam 6921105197Ssam msg = mtod(m, struct sadb_msg *); 6922105197Ssam msg->sadb_msg_errno = code; 6923105197Ssam return key_sendup_mbuf(so, m, KEY_SENDUP_ONE); 6924105197Ssam} 6925105197Ssam 6926105197Ssam/* 6927105197Ssam * set the pointer to each header into message buffer. 6928105197Ssam * m will be freed on error. 6929105197Ssam * XXX larger-than-MCLBYTES extension? 6930105197Ssam */ 6931105197Ssamstatic int 6932105197Ssamkey_align(m, mhp) 6933105197Ssam struct mbuf *m; 6934105197Ssam struct sadb_msghdr *mhp; 6935105197Ssam{ 6936105197Ssam struct mbuf *n; 6937105197Ssam struct sadb_ext *ext; 6938105197Ssam size_t off, end; 6939105197Ssam int extlen; 6940105197Ssam int toff; 6941105197Ssam 6942120585Ssam IPSEC_ASSERT(m != NULL, ("null mbuf")); 6943120585Ssam IPSEC_ASSERT(mhp != NULL, ("null msghdr")); 6944120585Ssam IPSEC_ASSERT(m->m_len >= sizeof(struct sadb_msg), 6945120585Ssam ("mbuf too small, len %u", m->m_len)); 6946105197Ssam 6947105197Ssam /* initialize */ 6948105197Ssam bzero(mhp, sizeof(*mhp)); 6949105197Ssam 6950105197Ssam mhp->msg = mtod(m, struct sadb_msg *); 6951105197Ssam mhp->ext[0] = (struct sadb_ext *)mhp->msg; /*XXX backward compat */ 6952105197Ssam 6953105197Ssam end = PFKEY_UNUNIT64(mhp->msg->sadb_msg_len); 6954105197Ssam extlen = end; /*just in case extlen is not updated*/ 6955105197Ssam for (off = sizeof(struct sadb_msg); off < end; off += extlen) { 6956105197Ssam n = m_pulldown(m, off, sizeof(struct sadb_ext), &toff); 6957105197Ssam if (!n) { 6958105197Ssam /* m is already freed */ 6959105197Ssam return ENOBUFS; 6960105197Ssam } 6961105197Ssam ext = (struct sadb_ext *)(mtod(n, caddr_t) + toff); 6962105197Ssam 6963105197Ssam /* set pointer */ 6964105197Ssam switch (ext->sadb_ext_type) { 6965105197Ssam case SADB_EXT_SA: 6966105197Ssam case SADB_EXT_ADDRESS_SRC: 6967105197Ssam case SADB_EXT_ADDRESS_DST: 6968105197Ssam case SADB_EXT_ADDRESS_PROXY: 6969105197Ssam case SADB_EXT_LIFETIME_CURRENT: 6970105197Ssam case SADB_EXT_LIFETIME_HARD: 6971105197Ssam case SADB_EXT_LIFETIME_SOFT: 6972105197Ssam case SADB_EXT_KEY_AUTH: 6973105197Ssam case SADB_EXT_KEY_ENCRYPT: 6974105197Ssam case SADB_EXT_IDENTITY_SRC: 6975105197Ssam case SADB_EXT_IDENTITY_DST: 6976105197Ssam case SADB_EXT_SENSITIVITY: 6977105197Ssam case SADB_EXT_PROPOSAL: 6978105197Ssam case SADB_EXT_SUPPORTED_AUTH: 6979105197Ssam case SADB_EXT_SUPPORTED_ENCRYPT: 6980105197Ssam case SADB_EXT_SPIRANGE: 6981105197Ssam case SADB_X_EXT_POLICY: 6982105197Ssam case SADB_X_EXT_SA2: 6983105197Ssam /* duplicate check */ 6984105197Ssam /* 6985105197Ssam * XXX Are there duplication payloads of either 6986105197Ssam * KEY_AUTH or KEY_ENCRYPT ? 6987105197Ssam */ 6988105197Ssam if (mhp->ext[ext->sadb_ext_type] != NULL) { 6989120585Ssam ipseclog((LOG_DEBUG, "%s: duplicate ext_type " 6990120585Ssam "%u\n", __func__, ext->sadb_ext_type)); 6991105197Ssam m_freem(m); 6992105197Ssam pfkeystat.out_dupext++; 6993105197Ssam return EINVAL; 6994105197Ssam } 6995105197Ssam break; 6996105197Ssam default: 6997120585Ssam ipseclog((LOG_DEBUG, "%s: invalid ext_type %u\n", 6998120585Ssam __func__, ext->sadb_ext_type)); 6999105197Ssam m_freem(m); 7000105197Ssam pfkeystat.out_invexttype++; 7001105197Ssam return EINVAL; 7002105197Ssam } 7003105197Ssam 7004105197Ssam extlen = PFKEY_UNUNIT64(ext->sadb_ext_len); 7005105197Ssam 7006105197Ssam if (key_validate_ext(ext, extlen)) { 7007105197Ssam m_freem(m); 7008105197Ssam pfkeystat.out_invlen++; 7009105197Ssam return EINVAL; 7010105197Ssam } 7011105197Ssam 7012105197Ssam n = m_pulldown(m, off, extlen, &toff); 7013105197Ssam if (!n) { 7014105197Ssam /* m is already freed */ 7015105197Ssam return ENOBUFS; 7016105197Ssam } 7017105197Ssam ext = (struct sadb_ext *)(mtod(n, caddr_t) + toff); 7018105197Ssam 7019105197Ssam mhp->ext[ext->sadb_ext_type] = ext; 7020105197Ssam mhp->extoff[ext->sadb_ext_type] = off; 7021105197Ssam mhp->extlen[ext->sadb_ext_type] = extlen; 7022105197Ssam } 7023105197Ssam 7024105197Ssam if (off != end) { 7025105197Ssam m_freem(m); 7026105197Ssam pfkeystat.out_invlen++; 7027105197Ssam return EINVAL; 7028105197Ssam } 7029105197Ssam 7030105197Ssam return 0; 7031105197Ssam} 7032105197Ssam 7033105197Ssamstatic int 7034105197Ssamkey_validate_ext(ext, len) 7035105197Ssam const struct sadb_ext *ext; 7036105197Ssam int len; 7037105197Ssam{ 7038105197Ssam const struct sockaddr *sa; 7039105197Ssam enum { NONE, ADDR } checktype = NONE; 7040105197Ssam int baselen = 0; 7041105197Ssam const int sal = offsetof(struct sockaddr, sa_len) + sizeof(sa->sa_len); 7042105197Ssam 7043105197Ssam if (len != PFKEY_UNUNIT64(ext->sadb_ext_len)) 7044105197Ssam return EINVAL; 7045105197Ssam 7046105197Ssam /* if it does not match minimum/maximum length, bail */ 7047105197Ssam if (ext->sadb_ext_type >= sizeof(minsize) / sizeof(minsize[0]) || 7048105197Ssam ext->sadb_ext_type >= sizeof(maxsize) / sizeof(maxsize[0])) 7049105197Ssam return EINVAL; 7050105197Ssam if (!minsize[ext->sadb_ext_type] || len < minsize[ext->sadb_ext_type]) 7051105197Ssam return EINVAL; 7052105197Ssam if (maxsize[ext->sadb_ext_type] && len > maxsize[ext->sadb_ext_type]) 7053105197Ssam return EINVAL; 7054105197Ssam 7055105197Ssam /* more checks based on sadb_ext_type XXX need more */ 7056105197Ssam switch (ext->sadb_ext_type) { 7057105197Ssam case SADB_EXT_ADDRESS_SRC: 7058105197Ssam case SADB_EXT_ADDRESS_DST: 7059105197Ssam case SADB_EXT_ADDRESS_PROXY: 7060105197Ssam baselen = PFKEY_ALIGN8(sizeof(struct sadb_address)); 7061105197Ssam checktype = ADDR; 7062105197Ssam break; 7063105197Ssam case SADB_EXT_IDENTITY_SRC: 7064105197Ssam case SADB_EXT_IDENTITY_DST: 7065105197Ssam if (((const struct sadb_ident *)ext)->sadb_ident_type == 7066105197Ssam SADB_X_IDENTTYPE_ADDR) { 7067105197Ssam baselen = PFKEY_ALIGN8(sizeof(struct sadb_ident)); 7068105197Ssam checktype = ADDR; 7069105197Ssam } else 7070105197Ssam checktype = NONE; 7071105197Ssam break; 7072105197Ssam default: 7073105197Ssam checktype = NONE; 7074105197Ssam break; 7075105197Ssam } 7076105197Ssam 7077105197Ssam switch (checktype) { 7078105197Ssam case NONE: 7079105197Ssam break; 7080105197Ssam case ADDR: 7081105197Ssam sa = (const struct sockaddr *)(((const u_int8_t*)ext)+baselen); 7082105197Ssam if (len < baselen + sal) 7083105197Ssam return EINVAL; 7084105197Ssam if (baselen + PFKEY_ALIGN8(sa->sa_len) != len) 7085105197Ssam return EINVAL; 7086105197Ssam break; 7087105197Ssam } 7088105197Ssam 7089105197Ssam return 0; 7090105197Ssam} 7091105197Ssam 7092105197Ssamvoid 7093105197Ssamkey_init() 7094105197Ssam{ 7095105197Ssam int i; 7096105197Ssam 7097120585Ssam SPTREE_LOCK_INIT(); 7098120585Ssam REGTREE_LOCK_INIT(); 7099120585Ssam SAHTREE_LOCK_INIT(); 7100120585Ssam ACQ_LOCK_INIT(); 7101120585Ssam SPACQ_LOCK_INIT(); 7102119643Ssam 7103119643Ssam for (i = 0; i < IPSEC_DIR_MAX; i++) 7104105197Ssam LIST_INIT(&sptree[i]); 7105105197Ssam 7106105197Ssam LIST_INIT(&sahtree); 7107105197Ssam 7108119643Ssam for (i = 0; i <= SADB_SATYPE_MAX; i++) 7109105197Ssam LIST_INIT(®tree[i]); 7110105197Ssam 7111105197Ssam LIST_INIT(&acqtree); 7112105197Ssam LIST_INIT(&spacqtree); 7113105197Ssam 7114105197Ssam /* system default */ 7115105197Ssam ip4_def_policy.policy = IPSEC_POLICY_NONE; 7116105197Ssam ip4_def_policy.refcnt++; /*never reclaim this*/ 7117105197Ssam 7118105197Ssam#ifndef IPSEC_DEBUG2 7119105197Ssam timeout((void *)key_timehandler, (void *)0, hz); 7120105197Ssam#endif /*IPSEC_DEBUG2*/ 7121105197Ssam 7122105197Ssam /* initialize key statistics */ 7123105197Ssam keystat.getspi_count = 1; 7124105197Ssam 7125177173Sbz printf("IPsec: Initialized Security Association Processing.\n"); 7126105197Ssam 7127105197Ssam return; 7128105197Ssam} 7129105197Ssam 7130105197Ssam/* 7131105197Ssam * XXX: maybe This function is called after INBOUND IPsec processing. 7132105197Ssam * 7133105197Ssam * Special check for tunnel-mode packets. 7134105197Ssam * We must make some checks for consistency between inner and outer IP header. 7135105197Ssam * 7136105197Ssam * xxx more checks to be provided 7137105197Ssam */ 7138105197Ssamint 7139105197Ssamkey_checktunnelsanity(sav, family, src, dst) 7140105197Ssam struct secasvar *sav; 7141105197Ssam u_int family; 7142105197Ssam caddr_t src; 7143105197Ssam caddr_t dst; 7144105197Ssam{ 7145120585Ssam IPSEC_ASSERT(sav->sah != NULL, ("null SA header")); 7146105197Ssam 7147105197Ssam /* XXX: check inner IP header */ 7148105197Ssam 7149105197Ssam return 1; 7150105197Ssam} 7151105197Ssam 7152105197Ssam/* record data transfer on SA, and update timestamps */ 7153105197Ssamvoid 7154105197Ssamkey_sa_recordxfer(sav, m) 7155105197Ssam struct secasvar *sav; 7156105197Ssam struct mbuf *m; 7157105197Ssam{ 7158120585Ssam IPSEC_ASSERT(sav != NULL, ("Null secasvar")); 7159120585Ssam IPSEC_ASSERT(m != NULL, ("Null mbuf")); 7160105197Ssam if (!sav->lft_c) 7161105197Ssam return; 7162105197Ssam 7163105197Ssam /* 7164105197Ssam * XXX Currently, there is a difference of bytes size 7165105197Ssam * between inbound and outbound processing. 7166105197Ssam */ 7167157123Sgnn sav->lft_c->bytes += m->m_pkthdr.len; 7168105197Ssam /* to check bytes lifetime is done in key_timehandler(). */ 7169105197Ssam 7170105197Ssam /* 7171105197Ssam * We use the number of packets as the unit of 7172157123Sgnn * allocations. We increment the variable 7173105197Ssam * whenever {esp,ah}_{in,out}put is called. 7174105197Ssam */ 7175157123Sgnn sav->lft_c->allocations++; 7176105197Ssam /* XXX check for expires? */ 7177105197Ssam 7178105197Ssam /* 7179157123Sgnn * NOTE: We record CURRENT usetime by using wall clock, 7180105197Ssam * in seconds. HARD and SOFT lifetime are measured by the time 7181157123Sgnn * difference (again in seconds) from usetime. 7182105197Ssam * 7183105197Ssam * usetime 7184105197Ssam * v expire expire 7185105197Ssam * -----+-----+--------+---> t 7186105197Ssam * <--------------> HARD 7187105197Ssam * <-----> SOFT 7188105197Ssam */ 7189157123Sgnn sav->lft_c->usetime = time_second; 7190105197Ssam /* XXX check for expires? */ 7191105197Ssam 7192105197Ssam return; 7193105197Ssam} 7194105197Ssam 7195105197Ssam/* dumb version */ 7196105197Ssamvoid 7197105197Ssamkey_sa_routechange(dst) 7198105197Ssam struct sockaddr *dst; 7199105197Ssam{ 7200105197Ssam struct secashead *sah; 7201105197Ssam struct route *ro; 7202105197Ssam 7203120585Ssam SAHTREE_LOCK(); 7204105197Ssam LIST_FOREACH(sah, &sahtree, chain) { 7205105197Ssam ro = &sah->sa_route; 7206105197Ssam if (ro->ro_rt && dst->sa_len == ro->ro_dst.sa_len 7207105197Ssam && bcmp(dst, &ro->ro_dst, dst->sa_len) == 0) { 7208105197Ssam RTFREE(ro->ro_rt); 7209105197Ssam ro->ro_rt = (struct rtentry *)NULL; 7210105197Ssam } 7211105197Ssam } 7212120585Ssam SAHTREE_UNLOCK(); 7213105197Ssam} 7214105197Ssam 7215105197Ssamstatic void 7216105197Ssamkey_sa_chgstate(sav, state) 7217105197Ssam struct secasvar *sav; 7218105197Ssam u_int8_t state; 7219105197Ssam{ 7220120585Ssam IPSEC_ASSERT(sav != NULL, ("NULL sav")); 7221120585Ssam SAHTREE_LOCK_ASSERT(); 7222105197Ssam 7223119643Ssam if (sav->state != state) { 7224119643Ssam if (__LIST_CHAINED(sav)) 7225119643Ssam LIST_REMOVE(sav, chain); 7226119643Ssam sav->state = state; 7227119643Ssam LIST_INSERT_HEAD(&sav->sah->savtree[state], sav, chain); 7228119643Ssam } 7229105197Ssam} 7230105197Ssam 7231105197Ssamvoid 7232105197Ssamkey_sa_stir_iv(sav) 7233105197Ssam struct secasvar *sav; 7234105197Ssam{ 7235105197Ssam 7236120585Ssam IPSEC_ASSERT(sav->iv != NULL, ("null IV")); 7237105197Ssam key_randomfill(sav->iv, sav->ivlen); 7238105197Ssam} 7239105197Ssam 7240105197Ssam/* XXX too much? */ 7241105197Ssamstatic struct mbuf * 7242105197Ssamkey_alloc_mbuf(l) 7243105197Ssam int l; 7244105197Ssam{ 7245105197Ssam struct mbuf *m = NULL, *n; 7246105197Ssam int len, t; 7247105197Ssam 7248105197Ssam len = l; 7249105197Ssam while (len > 0) { 7250111119Simp MGET(n, M_DONTWAIT, MT_DATA); 7251105197Ssam if (n && len > MLEN) 7252111119Simp MCLGET(n, M_DONTWAIT); 7253105197Ssam if (!n) { 7254105197Ssam m_freem(m); 7255105197Ssam return NULL; 7256105197Ssam } 7257105197Ssam 7258105197Ssam n->m_next = NULL; 7259105197Ssam n->m_len = 0; 7260105197Ssam n->m_len = M_TRAILINGSPACE(n); 7261105197Ssam /* use the bottom of mbuf, hoping we can prepend afterwards */ 7262105197Ssam if (n->m_len > len) { 7263105197Ssam t = (n->m_len - len) & ~(sizeof(long) - 1); 7264105197Ssam n->m_data += t; 7265105197Ssam n->m_len = len; 7266105197Ssam } 7267105197Ssam 7268105197Ssam len -= n->m_len; 7269105197Ssam 7270105197Ssam if (m) 7271105197Ssam m_cat(m, n); 7272105197Ssam else 7273105197Ssam m = n; 7274105197Ssam } 7275105197Ssam 7276105197Ssam return m; 7277105197Ssam} 7278157123Sgnn 7279157123Sgnn/* 7280157123Sgnn * Take one of the kernel's security keys and convert it into a PF_KEY 7281157123Sgnn * structure within an mbuf, suitable for sending up to a waiting 7282157123Sgnn * application in user land. 7283157123Sgnn * 7284157123Sgnn * IN: 7285157123Sgnn * src: A pointer to a kernel security key. 7286157123Sgnn * exttype: Which type of key this is. Refer to the PF_KEY data structures. 7287157123Sgnn * OUT: 7288157123Sgnn * a valid mbuf or NULL indicating an error 7289157123Sgnn * 7290157123Sgnn */ 7291157123Sgnn 7292157123Sgnnstatic struct mbuf * 7293157123Sgnnkey_setkey(struct seckey *src, u_int16_t exttype) 7294157123Sgnn{ 7295157123Sgnn struct mbuf *m; 7296157123Sgnn struct sadb_key *p; 7297170799Sbz int len; 7298157123Sgnn 7299157123Sgnn if (src == NULL) 7300157123Sgnn return NULL; 7301157123Sgnn 7302170799Sbz len = PFKEY_ALIGN8(sizeof(struct sadb_key) + _KEYLEN(src)); 7303157123Sgnn m = key_alloc_mbuf(len); 7304157123Sgnn if (m == NULL) 7305157123Sgnn return NULL; 7306157123Sgnn p = mtod(m, struct sadb_key *); 7307157123Sgnn bzero(p, len); 7308157123Sgnn p->sadb_key_len = PFKEY_UNIT64(len); 7309157123Sgnn p->sadb_key_exttype = exttype; 7310157123Sgnn p->sadb_key_bits = src->bits; 7311157123Sgnn bcopy(src->key_data, _KEYBUF(p), _KEYLEN(src)); 7312157123Sgnn 7313157123Sgnn return m; 7314157123Sgnn} 7315157123Sgnn 7316157123Sgnn/* 7317157123Sgnn * Take one of the kernel's lifetime data structures and convert it 7318157123Sgnn * into a PF_KEY structure within an mbuf, suitable for sending up to 7319157123Sgnn * a waiting application in user land. 7320157123Sgnn * 7321157123Sgnn * IN: 7322157123Sgnn * src: A pointer to a kernel lifetime structure. 7323157123Sgnn * exttype: Which type of lifetime this is. Refer to the PF_KEY 7324157123Sgnn * data structures for more information. 7325157123Sgnn * OUT: 7326157123Sgnn * a valid mbuf or NULL indicating an error 7327157123Sgnn * 7328157123Sgnn */ 7329157123Sgnn 7330157123Sgnnstatic struct mbuf * 7331157123Sgnnkey_setlifetime(struct seclifetime *src, u_int16_t exttype) 7332157123Sgnn{ 7333157123Sgnn struct mbuf *m = NULL; 7334157123Sgnn struct sadb_lifetime *p; 7335157123Sgnn int len = PFKEY_ALIGN8(sizeof(struct sadb_lifetime)); 7336157123Sgnn 7337157123Sgnn if (src == NULL) 7338157123Sgnn return NULL; 7339157123Sgnn 7340157123Sgnn m = key_alloc_mbuf(len); 7341157123Sgnn if (m == NULL) 7342157123Sgnn return m; 7343157123Sgnn p = mtod(m, struct sadb_lifetime *); 7344157123Sgnn 7345157123Sgnn bzero(p, len); 7346157123Sgnn p->sadb_lifetime_len = PFKEY_UNIT64(len); 7347157123Sgnn p->sadb_lifetime_exttype = exttype; 7348157123Sgnn p->sadb_lifetime_allocations = src->allocations; 7349157123Sgnn p->sadb_lifetime_bytes = src->bytes; 7350157123Sgnn p->sadb_lifetime_addtime = src->addtime; 7351157123Sgnn p->sadb_lifetime_usetime = src->usetime; 7352157123Sgnn 7353157123Sgnn return m; 7354157123Sgnn 7355157123Sgnn} 7356