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