1 /* $OpenBSD: pfkeyv2_convert.c,v 1.68 2020/07/18 15:10:03 kn Exp $ */ 2 /* 3 * The author of this code is Angelos D. Keromytis (angelos@keromytis.org) 4 * 5 * Part of this code is based on code written by Craig Metz (cmetz@inner.net) 6 * for NRL. Those licenses follow this one. 7 * 8 * Copyright (c) 2001 Angelos D. Keromytis. 9 * 10 * Permission to use, copy, and modify this software with or without fee 11 * is hereby granted, provided that this entire notice is included in 12 * all copies of any software which is or includes a copy or 13 * modification of this software. 14 * You may use this code under the GNU public license if you so wish. Please 15 * contribute changes back to the authors under this freer than GPL license 16 * so that we may further the use of strong encryption without limitations to 17 * all. 18 * 19 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR 20 * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY 21 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE 22 * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR 23 * PURPOSE. 24 */ 25 26 /* 27 * @(#)COPYRIGHT 1.1 (NRL) 17 January 1995 28 * 29 * NRL grants permission for redistribution and use in source and binary 30 * forms, with or without modification, of the software and documentation 31 * created at NRL provided that the following conditions are met: 32 * 33 * 1. Redistributions of source code must retain the above copyright 34 * notice, this list of conditions and the following disclaimer. 35 * 2. Redistributions in binary form must reproduce the above copyright 36 * notice, this list of conditions and the following disclaimer in the 37 * documentation and/or other materials provided with the distribution. 38 * 3. All advertising materials mentioning features or use of this software 39 * must display the following acknowledgements: 40 * This product includes software developed by the University of 41 * California, Berkeley and its contributors. 42 * This product includes software developed at the Information 43 * Technology Division, US Naval Research Laboratory. 44 * 4. Neither the name of the NRL nor the names of its contributors 45 * may be used to endorse or promote products derived from this software 46 * without specific prior written permission. 47 * 48 * THE SOFTWARE PROVIDED BY NRL IS PROVIDED BY NRL AND CONTRIBUTORS ``AS 49 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 50 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 51 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NRL OR 52 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 53 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 54 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 55 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 56 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 57 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 58 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 59 * 60 * The views and conclusions contained in the software and documentation 61 * are those of the authors and should not be interpreted as representing 62 * official policies, either expressed or implied, of the US Naval 63 * Research Laboratory (NRL). 64 */ 65 66 /* 67 * Copyright (c) 1995, 1996, 1997, 1998, 1999 Craig Metz. All rights reserved. 68 * 69 * Redistribution and use in source and binary forms, with or without 70 * modification, are permitted provided that the following conditions 71 * are met: 72 * 1. Redistributions of source code must retain the above copyright 73 * notice, this list of conditions and the following disclaimer. 74 * 2. Redistributions in binary form must reproduce the above copyright 75 * notice, this list of conditions and the following disclaimer in the 76 * documentation and/or other materials provided with the distribution. 77 * 3. Neither the name of the author nor the names of any contributors 78 * may be used to endorse or promote products derived from this software 79 * without specific prior written permission. 80 * 81 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 82 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 83 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 84 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 85 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 86 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 87 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 88 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 89 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 90 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 91 * SUCH DAMAGE. 92 */ 93 94 #include "pf.h" 95 96 #include <sys/param.h> 97 #include <sys/systm.h> 98 #include <sys/mbuf.h> 99 #include <sys/kernel.h> 100 #include <sys/socket.h> 101 #include <sys/timeout.h> 102 #include <net/route.h> 103 #include <net/if.h> 104 105 #include <netinet/in.h> 106 #include <netinet/ip_ipsp.h> 107 #include <net/pfkeyv2.h> 108 #include <crypto/cryptodev.h> 109 #include <crypto/xform.h> 110 111 #if NPF > 0 112 #include <net/pfvar.h> 113 #endif 114 115 /* 116 * (Partly) Initialize a TDB based on an SADB_SA payload. Other parts 117 * of the TDB will be initialized by other import routines, and tdb_init(). 118 */ 119 void 120 import_sa(struct tdb *tdb, struct sadb_sa *sadb_sa, struct ipsecinit *ii) 121 { 122 if (!sadb_sa) 123 return; 124 125 if (ii) { 126 ii->ii_encalg = sadb_sa->sadb_sa_encrypt; 127 ii->ii_authalg = sadb_sa->sadb_sa_auth; 128 ii->ii_compalg = sadb_sa->sadb_sa_encrypt; /* Yeurk! */ 129 130 tdb->tdb_spi = sadb_sa->sadb_sa_spi; 131 tdb->tdb_wnd = sadb_sa->sadb_sa_replay; 132 133 if (sadb_sa->sadb_sa_flags & SADB_SAFLAGS_PFS) 134 tdb->tdb_flags |= TDBF_PFS; 135 136 if (sadb_sa->sadb_sa_flags & SADB_X_SAFLAGS_TUNNEL) 137 tdb->tdb_flags |= TDBF_TUNNELING; 138 139 if (sadb_sa->sadb_sa_flags & SADB_X_SAFLAGS_UDPENCAP) 140 tdb->tdb_flags |= TDBF_UDPENCAP; 141 142 if (sadb_sa->sadb_sa_flags & SADB_X_SAFLAGS_ESN) 143 tdb->tdb_flags |= TDBF_ESN; 144 } 145 146 if (sadb_sa->sadb_sa_state != SADB_SASTATE_MATURE) 147 tdb->tdb_flags |= TDBF_INVALID; 148 } 149 150 /* 151 * Export some of the information on a TDB. 152 */ 153 void 154 export_sa(void **p, struct tdb *tdb) 155 { 156 struct sadb_sa *sadb_sa = (struct sadb_sa *) *p; 157 158 sadb_sa->sadb_sa_len = sizeof(struct sadb_sa) / sizeof(uint64_t); 159 160 sadb_sa->sadb_sa_spi = tdb->tdb_spi; 161 sadb_sa->sadb_sa_replay = tdb->tdb_wnd; 162 163 if (tdb->tdb_flags & TDBF_INVALID) 164 sadb_sa->sadb_sa_state = SADB_SASTATE_LARVAL; 165 else 166 sadb_sa->sadb_sa_state = SADB_SASTATE_MATURE; 167 168 if (tdb->tdb_sproto == IPPROTO_IPCOMP && 169 tdb->tdb_compalgxform != NULL) { 170 switch (tdb->tdb_compalgxform->type) { 171 case CRYPTO_DEFLATE_COMP: 172 sadb_sa->sadb_sa_encrypt = SADB_X_CALG_DEFLATE; 173 break; 174 case CRYPTO_LZS_COMP: 175 sadb_sa->sadb_sa_encrypt = SADB_X_CALG_LZS; 176 break; 177 } 178 } 179 180 if (tdb->tdb_authalgxform) { 181 switch (tdb->tdb_authalgxform->type) { 182 case CRYPTO_MD5_HMAC: 183 sadb_sa->sadb_sa_auth = SADB_AALG_MD5HMAC; 184 break; 185 186 case CRYPTO_SHA1_HMAC: 187 sadb_sa->sadb_sa_auth = SADB_AALG_SHA1HMAC; 188 break; 189 190 case CRYPTO_RIPEMD160_HMAC: 191 sadb_sa->sadb_sa_auth = SADB_X_AALG_RIPEMD160HMAC; 192 break; 193 194 case CRYPTO_SHA2_256_HMAC: 195 sadb_sa->sadb_sa_auth = SADB_X_AALG_SHA2_256; 196 break; 197 198 case CRYPTO_SHA2_384_HMAC: 199 sadb_sa->sadb_sa_auth = SADB_X_AALG_SHA2_384; 200 break; 201 202 case CRYPTO_SHA2_512_HMAC: 203 sadb_sa->sadb_sa_auth = SADB_X_AALG_SHA2_512; 204 break; 205 206 case CRYPTO_AES_128_GMAC: 207 sadb_sa->sadb_sa_auth = SADB_X_AALG_AES128GMAC; 208 break; 209 210 case CRYPTO_AES_192_GMAC: 211 sadb_sa->sadb_sa_auth = SADB_X_AALG_AES192GMAC; 212 break; 213 214 case CRYPTO_AES_256_GMAC: 215 sadb_sa->sadb_sa_auth = SADB_X_AALG_AES256GMAC; 216 break; 217 218 case CRYPTO_CHACHA20_POLY1305_MAC: 219 sadb_sa->sadb_sa_auth = SADB_X_AALG_CHACHA20POLY1305; 220 break; 221 } 222 } 223 224 if (tdb->tdb_encalgxform) { 225 switch (tdb->tdb_encalgxform->type) { 226 case CRYPTO_NULL: 227 sadb_sa->sadb_sa_encrypt = SADB_EALG_NULL; 228 break; 229 230 case CRYPTO_3DES_CBC: 231 sadb_sa->sadb_sa_encrypt = SADB_EALG_3DESCBC; 232 break; 233 234 case CRYPTO_AES_CBC: 235 sadb_sa->sadb_sa_encrypt = SADB_X_EALG_AES; 236 break; 237 238 case CRYPTO_AES_CTR: 239 sadb_sa->sadb_sa_encrypt = SADB_X_EALG_AESCTR; 240 break; 241 242 case CRYPTO_AES_GCM_16: 243 sadb_sa->sadb_sa_encrypt = SADB_X_EALG_AESGCM16; 244 break; 245 246 case CRYPTO_AES_GMAC: 247 sadb_sa->sadb_sa_encrypt = SADB_X_EALG_AESGMAC; 248 break; 249 250 case CRYPTO_CAST_CBC: 251 sadb_sa->sadb_sa_encrypt = SADB_X_EALG_CAST; 252 break; 253 254 case CRYPTO_BLF_CBC: 255 sadb_sa->sadb_sa_encrypt = SADB_X_EALG_BLF; 256 break; 257 258 case CRYPTO_CHACHA20_POLY1305: 259 sadb_sa->sadb_sa_encrypt = SADB_X_EALG_CHACHA20POLY1305; 260 break; 261 } 262 } 263 264 if (tdb->tdb_flags & TDBF_PFS) 265 sadb_sa->sadb_sa_flags |= SADB_SAFLAGS_PFS; 266 267 if (tdb->tdb_flags & TDBF_TUNNELING) 268 sadb_sa->sadb_sa_flags |= SADB_X_SAFLAGS_TUNNEL; 269 270 if (tdb->tdb_flags & TDBF_UDPENCAP) 271 sadb_sa->sadb_sa_flags |= SADB_X_SAFLAGS_UDPENCAP; 272 273 if (tdb->tdb_flags & TDBF_ESN) 274 sadb_sa->sadb_sa_flags |= SADB_X_SAFLAGS_ESN; 275 276 *p += sizeof(struct sadb_sa); 277 } 278 279 /* 280 * Initialize expirations and counters based on lifetime payload. 281 */ 282 void 283 import_lifetime(struct tdb *tdb, struct sadb_lifetime *sadb_lifetime, int type) 284 { 285 if (!sadb_lifetime) 286 return; 287 288 switch (type) { 289 case PFKEYV2_LIFETIME_HARD: 290 if ((tdb->tdb_exp_allocations = 291 sadb_lifetime->sadb_lifetime_allocations) != 0) 292 tdb->tdb_flags |= TDBF_ALLOCATIONS; 293 else 294 tdb->tdb_flags &= ~TDBF_ALLOCATIONS; 295 296 if ((tdb->tdb_exp_bytes = 297 sadb_lifetime->sadb_lifetime_bytes) != 0) 298 tdb->tdb_flags |= TDBF_BYTES; 299 else 300 tdb->tdb_flags &= ~TDBF_BYTES; 301 302 if ((tdb->tdb_exp_timeout = 303 sadb_lifetime->sadb_lifetime_addtime) != 0) { 304 tdb->tdb_flags |= TDBF_TIMER; 305 timeout_add_sec(&tdb->tdb_timer_tmo, 306 tdb->tdb_exp_timeout); 307 } else 308 tdb->tdb_flags &= ~TDBF_TIMER; 309 310 if ((tdb->tdb_exp_first_use = 311 sadb_lifetime->sadb_lifetime_usetime) != 0) 312 tdb->tdb_flags |= TDBF_FIRSTUSE; 313 else 314 tdb->tdb_flags &= ~TDBF_FIRSTUSE; 315 break; 316 317 case PFKEYV2_LIFETIME_SOFT: 318 if ((tdb->tdb_soft_allocations = 319 sadb_lifetime->sadb_lifetime_allocations) != 0) 320 tdb->tdb_flags |= TDBF_SOFT_ALLOCATIONS; 321 else 322 tdb->tdb_flags &= ~TDBF_SOFT_ALLOCATIONS; 323 324 if ((tdb->tdb_soft_bytes = 325 sadb_lifetime->sadb_lifetime_bytes) != 0) 326 tdb->tdb_flags |= TDBF_SOFT_BYTES; 327 else 328 tdb->tdb_flags &= ~TDBF_SOFT_BYTES; 329 330 if ((tdb->tdb_soft_timeout = 331 sadb_lifetime->sadb_lifetime_addtime) != 0) { 332 tdb->tdb_flags |= TDBF_SOFT_TIMER; 333 timeout_add_sec(&tdb->tdb_stimer_tmo, 334 tdb->tdb_soft_timeout); 335 } else 336 tdb->tdb_flags &= ~TDBF_SOFT_TIMER; 337 338 if ((tdb->tdb_soft_first_use = 339 sadb_lifetime->sadb_lifetime_usetime) != 0) 340 tdb->tdb_flags |= TDBF_SOFT_FIRSTUSE; 341 else 342 tdb->tdb_flags &= ~TDBF_SOFT_FIRSTUSE; 343 break; 344 345 case PFKEYV2_LIFETIME_CURRENT: /* Nothing fancy here. */ 346 tdb->tdb_cur_allocations = 347 sadb_lifetime->sadb_lifetime_allocations; 348 tdb->tdb_cur_bytes = sadb_lifetime->sadb_lifetime_bytes; 349 tdb->tdb_established = sadb_lifetime->sadb_lifetime_addtime; 350 tdb->tdb_first_use = sadb_lifetime->sadb_lifetime_usetime; 351 } 352 } 353 354 /* 355 * Export TDB expiration information. 356 */ 357 void 358 export_lifetime(void **p, struct tdb *tdb, int type) 359 { 360 struct sadb_lifetime *sadb_lifetime = (struct sadb_lifetime *) *p; 361 362 sadb_lifetime->sadb_lifetime_len = sizeof(struct sadb_lifetime) / 363 sizeof(uint64_t); 364 365 switch (type) { 366 case PFKEYV2_LIFETIME_HARD: 367 if (tdb->tdb_flags & TDBF_ALLOCATIONS) 368 sadb_lifetime->sadb_lifetime_allocations = 369 tdb->tdb_exp_allocations; 370 371 if (tdb->tdb_flags & TDBF_BYTES) 372 sadb_lifetime->sadb_lifetime_bytes = 373 tdb->tdb_exp_bytes; 374 375 if (tdb->tdb_flags & TDBF_TIMER) 376 sadb_lifetime->sadb_lifetime_addtime = 377 tdb->tdb_exp_timeout; 378 379 if (tdb->tdb_flags & TDBF_FIRSTUSE) 380 sadb_lifetime->sadb_lifetime_usetime = 381 tdb->tdb_exp_first_use; 382 break; 383 384 case PFKEYV2_LIFETIME_SOFT: 385 if (tdb->tdb_flags & TDBF_SOFT_ALLOCATIONS) 386 sadb_lifetime->sadb_lifetime_allocations = 387 tdb->tdb_soft_allocations; 388 389 if (tdb->tdb_flags & TDBF_SOFT_BYTES) 390 sadb_lifetime->sadb_lifetime_bytes = 391 tdb->tdb_soft_bytes; 392 393 if (tdb->tdb_flags & TDBF_SOFT_TIMER) 394 sadb_lifetime->sadb_lifetime_addtime = 395 tdb->tdb_soft_timeout; 396 397 if (tdb->tdb_flags & TDBF_SOFT_FIRSTUSE) 398 sadb_lifetime->sadb_lifetime_usetime = 399 tdb->tdb_soft_first_use; 400 break; 401 402 case PFKEYV2_LIFETIME_CURRENT: 403 sadb_lifetime->sadb_lifetime_allocations = 404 tdb->tdb_cur_allocations; 405 sadb_lifetime->sadb_lifetime_bytes = tdb->tdb_cur_bytes; 406 sadb_lifetime->sadb_lifetime_addtime = tdb->tdb_established; 407 sadb_lifetime->sadb_lifetime_usetime = tdb->tdb_first_use; 408 break; 409 410 case PFKEYV2_LIFETIME_LASTUSE: 411 sadb_lifetime->sadb_lifetime_allocations = 0; 412 sadb_lifetime->sadb_lifetime_bytes = 0; 413 sadb_lifetime->sadb_lifetime_addtime = 0; 414 sadb_lifetime->sadb_lifetime_usetime = tdb->tdb_last_used; 415 break; 416 } 417 418 *p += sizeof(struct sadb_lifetime); 419 } 420 421 /* 422 * Import flow information to two struct sockaddr_encap's. Either 423 * all or none of the address arguments are NULL. 424 */ 425 void 426 import_flow(struct sockaddr_encap *flow, struct sockaddr_encap *flowmask, 427 struct sadb_address *ssrc, struct sadb_address *ssrcmask, 428 struct sadb_address *ddst, struct sadb_address *ddstmask, 429 struct sadb_protocol *sab, struct sadb_protocol *ftype) 430 { 431 u_int8_t transproto = 0; 432 union sockaddr_union *src = (union sockaddr_union *)(ssrc + 1); 433 union sockaddr_union *dst = (union sockaddr_union *)(ddst + 1); 434 union sockaddr_union *srcmask = (union sockaddr_union *)(ssrcmask + 1); 435 union sockaddr_union *dstmask = (union sockaddr_union *)(ddstmask + 1); 436 437 if (ssrc == NULL) 438 return; /* There wasn't any information to begin with. */ 439 440 bzero(flow, sizeof(*flow)); 441 bzero(flowmask, sizeof(*flowmask)); 442 443 if (sab != NULL) 444 transproto = sab->sadb_protocol_proto; 445 446 /* 447 * Check that all the address families match. We know they are 448 * valid and supported because pfkeyv2_parsemessage() checked that. 449 */ 450 if ((src->sa.sa_family != dst->sa.sa_family) || 451 (src->sa.sa_family != srcmask->sa.sa_family) || 452 (src->sa.sa_family != dstmask->sa.sa_family)) 453 return; 454 455 /* 456 * We set these as an indication that tdb_filter/tdb_filtermask are 457 * in fact initialized. 458 */ 459 flow->sen_family = flowmask->sen_family = PF_KEY; 460 flow->sen_len = flowmask->sen_len = SENT_LEN; 461 462 switch (src->sa.sa_family) { 463 case AF_INET: 464 /* netmask handling */ 465 rt_maskedcopy(&src->sa, &src->sa, &srcmask->sa); 466 rt_maskedcopy(&dst->sa, &dst->sa, &dstmask->sa); 467 468 flow->sen_type = SENT_IP4; 469 flow->sen_direction = ftype->sadb_protocol_direction; 470 flow->sen_ip_src = src->sin.sin_addr; 471 flow->sen_ip_dst = dst->sin.sin_addr; 472 flow->sen_proto = transproto; 473 flow->sen_sport = src->sin.sin_port; 474 flow->sen_dport = dst->sin.sin_port; 475 476 flowmask->sen_type = SENT_IP4; 477 flowmask->sen_direction = 0xff; 478 flowmask->sen_ip_src = srcmask->sin.sin_addr; 479 flowmask->sen_ip_dst = dstmask->sin.sin_addr; 480 flowmask->sen_sport = srcmask->sin.sin_port; 481 flowmask->sen_dport = dstmask->sin.sin_port; 482 if (transproto) 483 flowmask->sen_proto = 0xff; 484 break; 485 486 #ifdef INET6 487 case AF_INET6: 488 in6_embedscope(&src->sin6.sin6_addr, &src->sin6, 489 NULL); 490 in6_embedscope(&dst->sin6.sin6_addr, &dst->sin6, 491 NULL); 492 493 /* netmask handling */ 494 rt_maskedcopy(&src->sa, &src->sa, &srcmask->sa); 495 rt_maskedcopy(&dst->sa, &dst->sa, &dstmask->sa); 496 497 flow->sen_type = SENT_IP6; 498 flow->sen_ip6_direction = ftype->sadb_protocol_direction; 499 flow->sen_ip6_src = src->sin6.sin6_addr; 500 flow->sen_ip6_dst = dst->sin6.sin6_addr; 501 flow->sen_ip6_proto = transproto; 502 flow->sen_ip6_sport = src->sin6.sin6_port; 503 flow->sen_ip6_dport = dst->sin6.sin6_port; 504 505 flowmask->sen_type = SENT_IP6; 506 flowmask->sen_ip6_direction = 0xff; 507 flowmask->sen_ip6_src = srcmask->sin6.sin6_addr; 508 flowmask->sen_ip6_dst = dstmask->sin6.sin6_addr; 509 flowmask->sen_ip6_sport = srcmask->sin6.sin6_port; 510 flowmask->sen_ip6_dport = dstmask->sin6.sin6_port; 511 if (transproto) 512 flowmask->sen_ip6_proto = 0xff; 513 break; 514 #endif /* INET6 */ 515 } 516 } 517 518 /* 519 * Helper to export addresses from an struct sockaddr_encap. 520 */ 521 static void 522 export_encap(void **p, struct sockaddr_encap *encap, int type) 523 { 524 struct sadb_address *saddr = (struct sadb_address *)*p; 525 union sockaddr_union *sunion; 526 527 *p += sizeof(struct sadb_address); 528 sunion = (union sockaddr_union *)*p; 529 530 switch (encap->sen_type) { 531 case SENT_IP4: 532 saddr->sadb_address_len = (sizeof(struct sadb_address) + 533 PADUP(sizeof(struct sockaddr_in))) / sizeof(uint64_t); 534 sunion->sa.sa_len = sizeof(struct sockaddr_in); 535 sunion->sa.sa_family = AF_INET; 536 if (type == SADB_X_EXT_SRC_FLOW || 537 type == SADB_X_EXT_SRC_MASK) { 538 sunion->sin.sin_addr = encap->sen_ip_src; 539 sunion->sin.sin_port = encap->sen_sport; 540 } else { 541 sunion->sin.sin_addr = encap->sen_ip_dst; 542 sunion->sin.sin_port = encap->sen_dport; 543 } 544 *p += PADUP(sizeof(struct sockaddr_in)); 545 break; 546 case SENT_IP6: 547 saddr->sadb_address_len = (sizeof(struct sadb_address) 548 + PADUP(sizeof(struct sockaddr_in6))) / sizeof(uint64_t); 549 sunion->sa.sa_len = sizeof(struct sockaddr_in6); 550 sunion->sa.sa_family = AF_INET6; 551 if (type == SADB_X_EXT_SRC_FLOW || 552 type == SADB_X_EXT_SRC_MASK) { 553 sunion->sin6.sin6_addr = encap->sen_ip6_src; 554 sunion->sin6.sin6_port = encap->sen_ip6_sport; 555 } else { 556 sunion->sin6.sin6_addr = encap->sen_ip6_dst; 557 sunion->sin6.sin6_port = encap->sen_ip6_dport; 558 } 559 *p += PADUP(sizeof(struct sockaddr_in6)); 560 break; 561 } 562 } 563 564 /* 565 * Export flow information from two struct sockaddr_encap's. 566 */ 567 void 568 export_flow(void **p, u_int8_t ftype, struct sockaddr_encap *flow, 569 struct sockaddr_encap *flowmask, void **headers) 570 { 571 struct sadb_protocol *sab; 572 573 headers[SADB_X_EXT_FLOW_TYPE] = *p; 574 sab = (struct sadb_protocol *)*p; 575 sab->sadb_protocol_len = sizeof(struct sadb_protocol) / 576 sizeof(uint64_t); 577 578 switch (ftype) { 579 case IPSP_IPSEC_USE: 580 sab->sadb_protocol_proto = SADB_X_FLOW_TYPE_USE; 581 break; 582 case IPSP_IPSEC_ACQUIRE: 583 sab->sadb_protocol_proto = SADB_X_FLOW_TYPE_ACQUIRE; 584 break; 585 case IPSP_IPSEC_REQUIRE: 586 sab->sadb_protocol_proto = SADB_X_FLOW_TYPE_REQUIRE; 587 break; 588 case IPSP_DENY: 589 sab->sadb_protocol_proto = SADB_X_FLOW_TYPE_DENY; 590 break; 591 case IPSP_PERMIT: 592 sab->sadb_protocol_proto = SADB_X_FLOW_TYPE_BYPASS; 593 break; 594 case IPSP_IPSEC_DONTACQ: 595 sab->sadb_protocol_proto = SADB_X_FLOW_TYPE_DONTACQ; 596 break; 597 default: 598 sab->sadb_protocol_proto = 0; 599 break; 600 } 601 602 switch (flow->sen_type) { 603 case SENT_IP4: 604 sab->sadb_protocol_direction = flow->sen_direction; 605 break; 606 #ifdef INET6 607 case SENT_IP6: 608 sab->sadb_protocol_direction = flow->sen_ip6_direction; 609 break; 610 #endif /* INET6 */ 611 } 612 *p += sizeof(struct sadb_protocol); 613 614 headers[SADB_X_EXT_PROTOCOL] = *p; 615 sab = (struct sadb_protocol *)*p; 616 sab->sadb_protocol_len = sizeof(struct sadb_protocol) / 617 sizeof(uint64_t); 618 switch (flow->sen_type) { 619 case SENT_IP4: 620 sab->sadb_protocol_proto = flow->sen_proto; 621 break; 622 #ifdef INET6 623 case SENT_IP6: 624 sab->sadb_protocol_proto = flow->sen_ip6_proto; 625 break; 626 #endif /* INET6 */ 627 } 628 *p += sizeof(struct sadb_protocol); 629 630 headers[SADB_X_EXT_SRC_FLOW] = *p; 631 export_encap(p, flow, SADB_X_EXT_SRC_FLOW); 632 633 headers[SADB_X_EXT_SRC_MASK] = *p; 634 export_encap(p, flowmask, SADB_X_EXT_SRC_MASK); 635 636 headers[SADB_X_EXT_DST_FLOW] = *p; 637 export_encap(p, flow, SADB_X_EXT_DST_FLOW); 638 639 headers[SADB_X_EXT_DST_MASK] = *p; 640 export_encap(p, flowmask, SADB_X_EXT_DST_MASK); 641 } 642 643 /* 644 * Copy an SADB_ADDRESS payload to a struct sockaddr. 645 */ 646 void 647 import_address(struct sockaddr *sa, struct sadb_address *sadb_address) 648 { 649 int salen; 650 struct sockaddr *ssa = (struct sockaddr *)((void *) sadb_address + 651 sizeof(struct sadb_address)); 652 653 if (!sadb_address) 654 return; 655 656 if (ssa->sa_len) 657 salen = ssa->sa_len; 658 else 659 switch (ssa->sa_family) { 660 case AF_INET: 661 salen = sizeof(struct sockaddr_in); 662 break; 663 664 #ifdef INET6 665 case AF_INET6: 666 salen = sizeof(struct sockaddr_in6); 667 break; 668 #endif /* INET6 */ 669 670 default: 671 return; 672 } 673 674 bcopy(ssa, sa, salen); 675 sa->sa_len = salen; 676 } 677 678 /* 679 * Export a struct sockaddr as an SADB_ADDRESS payload. 680 */ 681 void 682 export_address(void **p, struct sockaddr *sa) 683 { 684 struct sadb_address *sadb_address = (struct sadb_address *) *p; 685 686 sadb_address->sadb_address_len = (sizeof(struct sadb_address) + 687 PADUP(sa->sa_len)) / sizeof(uint64_t); 688 689 *p += sizeof(struct sadb_address); 690 bcopy(sa, *p, sa->sa_len); 691 ((struct sockaddr *) *p)->sa_family = sa->sa_family; 692 *p += PADUP(sa->sa_len); 693 } 694 695 /* 696 * Import an identity payload into the TDB. 697 */ 698 static void 699 import_identity(struct ipsec_id **id, struct sadb_ident *sadb_ident, 700 size_t *id_sz) 701 { 702 size_t id_len; 703 704 if (!sadb_ident) { 705 *id = NULL; 706 return; 707 } 708 709 id_len = EXTLEN(sadb_ident) - sizeof(struct sadb_ident); 710 *id_sz = sizeof(struct ipsec_id) + id_len; 711 *id = malloc(*id_sz, M_CREDENTIALS, M_WAITOK); 712 (*id)->len = id_len; 713 714 switch (sadb_ident->sadb_ident_type) { 715 case SADB_IDENTTYPE_PREFIX: 716 (*id)->type = IPSP_IDENTITY_PREFIX; 717 break; 718 case SADB_IDENTTYPE_FQDN: 719 (*id)->type = IPSP_IDENTITY_FQDN; 720 break; 721 case SADB_IDENTTYPE_USERFQDN: 722 (*id)->type = IPSP_IDENTITY_USERFQDN; 723 break; 724 default: 725 free(*id, M_CREDENTIALS, *id_sz); 726 *id = NULL; 727 return; 728 } 729 bcopy((void *) sadb_ident + sizeof(struct sadb_ident), (*id) + 1, 730 (*id)->len); 731 } 732 733 void 734 import_identities(struct ipsec_ids **ids, int swapped, 735 struct sadb_ident *srcid, struct sadb_ident *dstid) 736 { 737 struct ipsec_ids *tmp; 738 size_t id_local_sz, id_remote_sz; 739 740 *ids = NULL; 741 tmp = malloc(sizeof(struct ipsec_ids), M_CREDENTIALS, M_WAITOK); 742 import_identity(&tmp->id_local, swapped ? dstid: srcid, &id_local_sz); 743 import_identity(&tmp->id_remote, swapped ? srcid: dstid, &id_remote_sz); 744 if (tmp->id_local != NULL && tmp->id_remote != NULL) { 745 *ids = ipsp_ids_insert(tmp); 746 if (*ids == tmp) 747 return; 748 } 749 free(tmp->id_local, M_CREDENTIALS, id_local_sz); 750 free(tmp->id_remote, M_CREDENTIALS, id_remote_sz); 751 free(tmp, M_CREDENTIALS, sizeof(*tmp)); 752 } 753 754 static void 755 export_identity(void **p, struct ipsec_id *id) 756 { 757 struct sadb_ident *sadb_ident = (struct sadb_ident *) *p; 758 759 sadb_ident->sadb_ident_len = (sizeof(struct sadb_ident) + 760 PADUP(id->len)) / sizeof(uint64_t); 761 762 switch (id->type) { 763 case IPSP_IDENTITY_PREFIX: 764 sadb_ident->sadb_ident_type = SADB_IDENTTYPE_PREFIX; 765 break; 766 case IPSP_IDENTITY_FQDN: 767 sadb_ident->sadb_ident_type = SADB_IDENTTYPE_FQDN; 768 break; 769 case IPSP_IDENTITY_USERFQDN: 770 sadb_ident->sadb_ident_type = SADB_IDENTTYPE_USERFQDN; 771 break; 772 } 773 *p += sizeof(struct sadb_ident); 774 bcopy(id + 1, *p, id->len); 775 *p += PADUP(id->len); 776 } 777 778 void 779 export_identities(void **p, struct ipsec_ids *ids, int swapped, 780 void **headers) 781 { 782 headers[SADB_EXT_IDENTITY_SRC] = *p; 783 export_identity(p, swapped ? ids->id_remote : ids->id_local); 784 headers[SADB_EXT_IDENTITY_DST] = *p; 785 export_identity(p, swapped ? ids->id_local : ids->id_remote); 786 } 787 788 /* ... */ 789 void 790 import_key(struct ipsecinit *ii, struct sadb_key *sadb_key, int type) 791 { 792 if (!sadb_key) 793 return; 794 795 if (type == PFKEYV2_ENCRYPTION_KEY) { /* Encryption key */ 796 ii->ii_enckeylen = sadb_key->sadb_key_bits / 8; 797 ii->ii_enckey = (void *)sadb_key + sizeof(struct sadb_key); 798 } else { 799 ii->ii_authkeylen = sadb_key->sadb_key_bits / 8; 800 ii->ii_authkey = (void *)sadb_key + sizeof(struct sadb_key); 801 } 802 } 803 804 void 805 export_key(void **p, struct tdb *tdb, int type) 806 { 807 struct sadb_key *sadb_key = (struct sadb_key *) *p; 808 809 if (type == PFKEYV2_ENCRYPTION_KEY) { 810 sadb_key->sadb_key_len = (sizeof(struct sadb_key) + 811 PADUP(tdb->tdb_emxkeylen)) / 812 sizeof(uint64_t); 813 sadb_key->sadb_key_bits = tdb->tdb_emxkeylen * 8; 814 *p += sizeof(struct sadb_key); 815 bcopy(tdb->tdb_emxkey, *p, tdb->tdb_emxkeylen); 816 *p += PADUP(tdb->tdb_emxkeylen); 817 } else { 818 sadb_key->sadb_key_len = (sizeof(struct sadb_key) + 819 PADUP(tdb->tdb_amxkeylen)) / 820 sizeof(uint64_t); 821 sadb_key->sadb_key_bits = tdb->tdb_amxkeylen * 8; 822 *p += sizeof(struct sadb_key); 823 bcopy(tdb->tdb_amxkey, *p, tdb->tdb_amxkeylen); 824 *p += PADUP(tdb->tdb_amxkeylen); 825 } 826 } 827 828 /* Import/Export remote port for UDP Encapsulation */ 829 void 830 import_udpencap(struct tdb *tdb, struct sadb_x_udpencap *sadb_udpencap) 831 { 832 if (sadb_udpencap) 833 tdb->tdb_udpencap_port = sadb_udpencap->sadb_x_udpencap_port; 834 } 835 836 void 837 export_udpencap(void **p, struct tdb *tdb) 838 { 839 struct sadb_x_udpencap *sadb_udpencap = (struct sadb_x_udpencap *) *p; 840 841 sadb_udpencap->sadb_x_udpencap_port = tdb->tdb_udpencap_port; 842 sadb_udpencap->sadb_x_udpencap_reserved = 0; 843 sadb_udpencap->sadb_x_udpencap_len = 844 sizeof(struct sadb_x_udpencap) / sizeof(uint64_t); 845 *p += sizeof(struct sadb_x_udpencap); 846 } 847 848 /* Import rdomain switch for SA */ 849 void 850 import_rdomain(struct tdb *tdb, struct sadb_x_rdomain *srdomain) 851 { 852 if (srdomain) 853 tdb->tdb_rdomain_post = srdomain->sadb_x_rdomain_dom2; 854 } 855 856 /* Export rdomain switch for SA */ 857 void 858 export_rdomain(void **p, struct tdb *tdb) 859 { 860 struct sadb_x_rdomain *srdomain = (struct sadb_x_rdomain *)*p; 861 862 srdomain->sadb_x_rdomain_dom1 = tdb->tdb_rdomain; 863 srdomain->sadb_x_rdomain_dom2 = tdb->tdb_rdomain_post; 864 srdomain->sadb_x_rdomain_len = 865 sizeof(struct sadb_x_rdomain) / sizeof(uint64_t); 866 *p += sizeof(struct sadb_x_rdomain); 867 } 868 869 #if NPF > 0 870 /* Import PF tag information for SA */ 871 void 872 import_tag(struct tdb *tdb, struct sadb_x_tag *stag) 873 { 874 char *s; 875 876 if (stag) { 877 s = (char *)(stag + 1); 878 tdb->tdb_tag = pf_tagname2tag(s, 1); 879 } 880 } 881 882 /* Export PF tag information for SA */ 883 void 884 export_tag(void **p, struct tdb *tdb) 885 { 886 struct sadb_x_tag *stag = (struct sadb_x_tag *)*p; 887 char *s = (char *)(stag + 1); 888 889 pf_tag2tagname(tdb->tdb_tag, s); 890 891 stag->sadb_x_tag_taglen = strlen(s) + 1; 892 stag->sadb_x_tag_len = (sizeof(struct sadb_x_tag) + 893 PADUP(stag->sadb_x_tag_taglen)) / sizeof(uint64_t); 894 *p += sizeof(struct sadb_x_tag) + PADUP(stag->sadb_x_tag_taglen); 895 } 896 897 /* Import enc(4) tap device information for SA */ 898 void 899 import_tap(struct tdb *tdb, struct sadb_x_tap *stap) 900 { 901 if (stap) 902 tdb->tdb_tap = stap->sadb_x_tap_unit; 903 } 904 905 /* Export enc(4) tap device information for SA */ 906 void 907 export_tap(void **p, struct tdb *tdb) 908 { 909 struct sadb_x_tap *stag = (struct sadb_x_tap *)*p; 910 911 stag->sadb_x_tap_unit = tdb->tdb_tap; 912 stag->sadb_x_tap_len = sizeof(struct sadb_x_tap) / sizeof(uint64_t); 913 *p += sizeof(struct sadb_x_tap); 914 } 915 #endif 916 917 void 918 export_satype(void **p, struct tdb *tdb) 919 { 920 struct sadb_protocol *sab = *p; 921 922 sab->sadb_protocol_len = sizeof(struct sadb_protocol) / 923 sizeof(uint64_t); 924 sab->sadb_protocol_proto = tdb->tdb_satype; 925 *p += sizeof(struct sadb_protocol); 926 } 927 928 void 929 export_counter(void **p, struct tdb *tdb) 930 { 931 struct sadb_x_counter *scnt = (struct sadb_x_counter *)*p; 932 933 scnt->sadb_x_counter_len = sizeof(struct sadb_x_counter) / 934 sizeof(uint64_t); 935 scnt->sadb_x_counter_pad = 0; 936 scnt->sadb_x_counter_ipackets = tdb->tdb_ipackets; 937 scnt->sadb_x_counter_opackets = tdb->tdb_opackets; 938 scnt->sadb_x_counter_ibytes = tdb->tdb_ibytes; 939 scnt->sadb_x_counter_obytes = tdb->tdb_obytes; 940 scnt->sadb_x_counter_idrops = tdb->tdb_idrops; 941 scnt->sadb_x_counter_odrops = tdb->tdb_odrops; 942 scnt->sadb_x_counter_idecompbytes = tdb->tdb_idecompbytes; 943 scnt->sadb_x_counter_ouncompbytes = tdb->tdb_ouncompbytes; 944 *p += sizeof(struct sadb_x_counter); 945 } 946