1 /* $OpenBSD: pfkeyv2_convert.c,v 1.50 2015/04/17 10:04:37 mikeb 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/types.h> 97 #include <sys/param.h> 98 #include <sys/systm.h> 99 #include <sys/mbuf.h> 100 #include <sys/kernel.h> 101 #include <sys/socket.h> 102 #include <sys/timeout.h> 103 #include <net/route.h> 104 #include <net/if.h> 105 106 #include <netinet/in.h> 107 #include <netinet/ip_ipsp.h> 108 #include <net/pfkeyv2.h> 109 #include <crypto/cryptodev.h> 110 #include <crypto/xform.h> 111 112 #if NPF > 0 113 #include <net/pfvar.h> 114 #endif 115 116 /* 117 * (Partly) Initialize a TDB based on an SADB_SA payload. Other parts 118 * of the TDB will be initialized by other import routines, and tdb_init(). 119 */ 120 void 121 import_sa(struct tdb *tdb, struct sadb_sa *sadb_sa, struct ipsecinit *ii) 122 { 123 if (!sadb_sa) 124 return; 125 126 if (ii) { 127 ii->ii_encalg = sadb_sa->sadb_sa_encrypt; 128 ii->ii_authalg = sadb_sa->sadb_sa_auth; 129 ii->ii_compalg = sadb_sa->sadb_sa_encrypt; /* Yeurk! */ 130 131 tdb->tdb_spi = sadb_sa->sadb_sa_spi; 132 tdb->tdb_wnd = sadb_sa->sadb_sa_replay; 133 134 if (sadb_sa->sadb_sa_flags & SADB_SAFLAGS_PFS) 135 tdb->tdb_flags |= TDBF_PFS; 136 137 if (sadb_sa->sadb_sa_flags & SADB_X_SAFLAGS_TUNNEL) 138 tdb->tdb_flags |= TDBF_TUNNELING; 139 140 if (sadb_sa->sadb_sa_flags & SADB_X_SAFLAGS_UDPENCAP) 141 tdb->tdb_flags |= TDBF_UDPENCAP; 142 143 if (sadb_sa->sadb_sa_flags & SADB_X_SAFLAGS_ESN) 144 tdb->tdb_flags |= TDBF_ESN; 145 } 146 147 if (sadb_sa->sadb_sa_state != SADB_SASTATE_MATURE) 148 tdb->tdb_flags |= TDBF_INVALID; 149 } 150 151 /* 152 * Export some of the information on a TDB. 153 */ 154 void 155 export_sa(void **p, struct tdb *tdb) 156 { 157 struct sadb_sa *sadb_sa = (struct sadb_sa *) *p; 158 159 sadb_sa->sadb_sa_len = sizeof(struct sadb_sa) / sizeof(uint64_t); 160 161 sadb_sa->sadb_sa_spi = tdb->tdb_spi; 162 sadb_sa->sadb_sa_replay = tdb->tdb_wnd; 163 164 if (tdb->tdb_flags & TDBF_INVALID) 165 sadb_sa->sadb_sa_state = SADB_SASTATE_LARVAL; 166 else 167 sadb_sa->sadb_sa_state = SADB_SASTATE_MATURE; 168 169 if (tdb->tdb_sproto == IPPROTO_IPCOMP && 170 tdb->tdb_compalgxform != NULL) { 171 switch (tdb->tdb_compalgxform->type) { 172 case CRYPTO_DEFLATE_COMP: 173 sadb_sa->sadb_sa_encrypt = SADB_X_CALG_DEFLATE; 174 break; 175 case CRYPTO_LZS_COMP: 176 sadb_sa->sadb_sa_encrypt = SADB_X_CALG_LZS; 177 break; 178 } 179 } 180 181 if (tdb->tdb_authalgxform) { 182 switch (tdb->tdb_authalgxform->type) { 183 case CRYPTO_MD5_HMAC: 184 sadb_sa->sadb_sa_auth = SADB_AALG_MD5HMAC; 185 break; 186 187 case CRYPTO_SHA1_HMAC: 188 sadb_sa->sadb_sa_auth = SADB_AALG_SHA1HMAC; 189 break; 190 191 case CRYPTO_RIPEMD160_HMAC: 192 sadb_sa->sadb_sa_auth = SADB_X_AALG_RIPEMD160HMAC; 193 break; 194 195 case CRYPTO_SHA2_256_HMAC: 196 sadb_sa->sadb_sa_auth = SADB_X_AALG_SHA2_256; 197 break; 198 199 case CRYPTO_SHA2_384_HMAC: 200 sadb_sa->sadb_sa_auth = SADB_X_AALG_SHA2_384; 201 break; 202 203 case CRYPTO_SHA2_512_HMAC: 204 sadb_sa->sadb_sa_auth = SADB_X_AALG_SHA2_512; 205 break; 206 207 case CRYPTO_AES_128_GMAC: 208 sadb_sa->sadb_sa_auth = SADB_X_AALG_AES128GMAC; 209 break; 210 211 case CRYPTO_AES_192_GMAC: 212 sadb_sa->sadb_sa_auth = SADB_X_AALG_AES192GMAC; 213 break; 214 215 case CRYPTO_AES_256_GMAC: 216 sadb_sa->sadb_sa_auth = SADB_X_AALG_AES256GMAC; 217 break; 218 } 219 } 220 221 if (tdb->tdb_encalgxform) { 222 switch (tdb->tdb_encalgxform->type) { 223 case CRYPTO_NULL: 224 sadb_sa->sadb_sa_encrypt = SADB_EALG_NULL; 225 break; 226 227 case CRYPTO_DES_CBC: 228 sadb_sa->sadb_sa_encrypt = SADB_EALG_DESCBC; 229 break; 230 231 case CRYPTO_3DES_CBC: 232 sadb_sa->sadb_sa_encrypt = SADB_EALG_3DESCBC; 233 break; 234 235 case CRYPTO_AES_CBC: 236 sadb_sa->sadb_sa_encrypt = SADB_X_EALG_AES; 237 break; 238 239 case CRYPTO_AES_CTR: 240 sadb_sa->sadb_sa_encrypt = SADB_X_EALG_AESCTR; 241 break; 242 243 case CRYPTO_AES_GCM_16: 244 sadb_sa->sadb_sa_encrypt = SADB_X_EALG_AESGCM16; 245 break; 246 247 case CRYPTO_AES_GMAC: 248 sadb_sa->sadb_sa_encrypt = SADB_X_EALG_AESGMAC; 249 break; 250 251 case CRYPTO_CAST_CBC: 252 sadb_sa->sadb_sa_encrypt = SADB_X_EALG_CAST; 253 break; 254 255 case CRYPTO_BLF_CBC: 256 sadb_sa->sadb_sa_encrypt = SADB_X_EALG_BLF; 257 break; 258 } 259 } 260 261 if (tdb->tdb_flags & TDBF_PFS) 262 sadb_sa->sadb_sa_flags |= SADB_SAFLAGS_PFS; 263 264 if (tdb->tdb_flags & TDBF_TUNNELING) 265 sadb_sa->sadb_sa_flags |= SADB_X_SAFLAGS_TUNNEL; 266 267 if (tdb->tdb_flags & TDBF_UDPENCAP) 268 sadb_sa->sadb_sa_flags |= SADB_X_SAFLAGS_UDPENCAP; 269 270 if (tdb->tdb_flags & TDBF_ESN) 271 sadb_sa->sadb_sa_flags |= SADB_X_SAFLAGS_ESN; 272 273 *p += sizeof(struct sadb_sa); 274 } 275 276 /* 277 * Initialize expirations and counters based on lifetime payload. 278 */ 279 void 280 import_lifetime(struct tdb *tdb, struct sadb_lifetime *sadb_lifetime, int type) 281 { 282 struct timeval tv; 283 284 if (!sadb_lifetime) 285 return; 286 287 getmicrotime(&tv); 288 289 switch (type) { 290 case PFKEYV2_LIFETIME_HARD: 291 if ((tdb->tdb_exp_allocations = 292 sadb_lifetime->sadb_lifetime_allocations) != 0) 293 tdb->tdb_flags |= TDBF_ALLOCATIONS; 294 else 295 tdb->tdb_flags &= ~TDBF_ALLOCATIONS; 296 297 if ((tdb->tdb_exp_bytes = 298 sadb_lifetime->sadb_lifetime_bytes) != 0) 299 tdb->tdb_flags |= TDBF_BYTES; 300 else 301 tdb->tdb_flags &= ~TDBF_BYTES; 302 303 if ((tdb->tdb_exp_timeout = 304 sadb_lifetime->sadb_lifetime_addtime) != 0) { 305 tdb->tdb_flags |= TDBF_TIMER; 306 if (tv.tv_sec + tdb->tdb_exp_timeout < tv.tv_sec) 307 tv.tv_sec = ((unsigned long) -1) / 2; /* XXX */ 308 else 309 tv.tv_sec += tdb->tdb_exp_timeout; 310 timeout_add(&tdb->tdb_timer_tmo, hzto(&tv)); 311 } else 312 tdb->tdb_flags &= ~TDBF_TIMER; 313 314 if ((tdb->tdb_exp_first_use = 315 sadb_lifetime->sadb_lifetime_usetime) != 0) 316 tdb->tdb_flags |= TDBF_FIRSTUSE; 317 else 318 tdb->tdb_flags &= ~TDBF_FIRSTUSE; 319 break; 320 321 case PFKEYV2_LIFETIME_SOFT: 322 if ((tdb->tdb_soft_allocations = 323 sadb_lifetime->sadb_lifetime_allocations) != 0) 324 tdb->tdb_flags |= TDBF_SOFT_ALLOCATIONS; 325 else 326 tdb->tdb_flags &= ~TDBF_SOFT_ALLOCATIONS; 327 328 if ((tdb->tdb_soft_bytes = 329 sadb_lifetime->sadb_lifetime_bytes) != 0) 330 tdb->tdb_flags |= TDBF_SOFT_BYTES; 331 else 332 tdb->tdb_flags &= ~TDBF_SOFT_BYTES; 333 334 if ((tdb->tdb_soft_timeout = 335 sadb_lifetime->sadb_lifetime_addtime) != 0) { 336 tdb->tdb_flags |= TDBF_SOFT_TIMER; 337 if (tv.tv_sec + tdb->tdb_soft_timeout < tv.tv_sec) 338 tv.tv_sec = ((unsigned long) -1) / 2; /* XXX */ 339 else 340 tv.tv_sec += tdb->tdb_soft_timeout; 341 timeout_add(&tdb->tdb_stimer_tmo, hzto(&tv)); 342 } else 343 tdb->tdb_flags &= ~TDBF_SOFT_TIMER; 344 345 if ((tdb->tdb_soft_first_use = 346 sadb_lifetime->sadb_lifetime_usetime) != 0) 347 tdb->tdb_flags |= TDBF_SOFT_FIRSTUSE; 348 else 349 tdb->tdb_flags &= ~TDBF_SOFT_FIRSTUSE; 350 break; 351 352 case PFKEYV2_LIFETIME_CURRENT: /* Nothing fancy here. */ 353 tdb->tdb_cur_allocations = 354 sadb_lifetime->sadb_lifetime_allocations; 355 tdb->tdb_cur_bytes = sadb_lifetime->sadb_lifetime_bytes; 356 tdb->tdb_established = sadb_lifetime->sadb_lifetime_addtime; 357 tdb->tdb_first_use = sadb_lifetime->sadb_lifetime_usetime; 358 } 359 } 360 361 /* 362 * Export TDB expiration information. 363 */ 364 void 365 export_lifetime(void **p, struct tdb *tdb, int type) 366 { 367 struct sadb_lifetime *sadb_lifetime = (struct sadb_lifetime *) *p; 368 369 sadb_lifetime->sadb_lifetime_len = sizeof(struct sadb_lifetime) / 370 sizeof(uint64_t); 371 372 switch (type) { 373 case PFKEYV2_LIFETIME_HARD: 374 if (tdb->tdb_flags & TDBF_ALLOCATIONS) 375 sadb_lifetime->sadb_lifetime_allocations = 376 tdb->tdb_exp_allocations; 377 378 if (tdb->tdb_flags & TDBF_BYTES) 379 sadb_lifetime->sadb_lifetime_bytes = 380 tdb->tdb_exp_bytes; 381 382 if (tdb->tdb_flags & TDBF_TIMER) 383 sadb_lifetime->sadb_lifetime_addtime = 384 tdb->tdb_exp_timeout; 385 386 if (tdb->tdb_flags & TDBF_FIRSTUSE) 387 sadb_lifetime->sadb_lifetime_usetime = 388 tdb->tdb_exp_first_use; 389 break; 390 391 case PFKEYV2_LIFETIME_SOFT: 392 if (tdb->tdb_flags & TDBF_SOFT_ALLOCATIONS) 393 sadb_lifetime->sadb_lifetime_allocations = 394 tdb->tdb_soft_allocations; 395 396 if (tdb->tdb_flags & TDBF_SOFT_BYTES) 397 sadb_lifetime->sadb_lifetime_bytes = 398 tdb->tdb_soft_bytes; 399 400 if (tdb->tdb_flags & TDBF_SOFT_TIMER) 401 sadb_lifetime->sadb_lifetime_addtime = 402 tdb->tdb_soft_timeout; 403 404 if (tdb->tdb_flags & TDBF_SOFT_FIRSTUSE) 405 sadb_lifetime->sadb_lifetime_usetime = 406 tdb->tdb_soft_first_use; 407 break; 408 409 case PFKEYV2_LIFETIME_CURRENT: 410 sadb_lifetime->sadb_lifetime_allocations = 411 tdb->tdb_cur_allocations; 412 sadb_lifetime->sadb_lifetime_bytes = tdb->tdb_cur_bytes; 413 sadb_lifetime->sadb_lifetime_addtime = tdb->tdb_established; 414 sadb_lifetime->sadb_lifetime_usetime = tdb->tdb_first_use; 415 break; 416 417 case PFKEYV2_LIFETIME_LASTUSE: 418 sadb_lifetime->sadb_lifetime_allocations = 0; 419 sadb_lifetime->sadb_lifetime_bytes = 0; 420 sadb_lifetime->sadb_lifetime_addtime = 0; 421 sadb_lifetime->sadb_lifetime_usetime = tdb->tdb_last_used; 422 break; 423 } 424 425 *p += sizeof(struct sadb_lifetime); 426 } 427 428 /* 429 * Import flow information to two struct sockaddr_encap's. Either 430 * all or none of the address arguments are NULL. 431 */ 432 void 433 import_flow(struct sockaddr_encap *flow, struct sockaddr_encap *flowmask, 434 struct sadb_address *ssrc, struct sadb_address *ssrcmask, 435 struct sadb_address *ddst, struct sadb_address *ddstmask, 436 struct sadb_protocol *sab, struct sadb_protocol *ftype) 437 { 438 u_int8_t transproto = 0; 439 union sockaddr_union *src = (union sockaddr_union *)(ssrc + 1); 440 union sockaddr_union *dst = (union sockaddr_union *)(ddst + 1); 441 union sockaddr_union *srcmask = (union sockaddr_union *)(ssrcmask + 1); 442 union sockaddr_union *dstmask = (union sockaddr_union *)(ddstmask + 1); 443 444 if (ssrc == NULL) 445 return; /* There wasn't any information to begin with. */ 446 447 bzero(flow, sizeof(*flow)); 448 bzero(flowmask, sizeof(*flowmask)); 449 450 if (sab != NULL) 451 transproto = sab->sadb_protocol_proto; 452 453 /* 454 * Check that all the address families match. We know they are 455 * valid and supported because pfkeyv2_parsemessage() checked that. 456 */ 457 if ((src->sa.sa_family != dst->sa.sa_family) || 458 (src->sa.sa_family != srcmask->sa.sa_family) || 459 (src->sa.sa_family != dstmask->sa.sa_family)) 460 return; 461 462 /* 463 * We set these as an indication that tdb_filter/tdb_filtermask are 464 * in fact initialized. 465 */ 466 flow->sen_family = flowmask->sen_family = PF_KEY; 467 flow->sen_len = flowmask->sen_len = SENT_LEN; 468 469 switch (src->sa.sa_family) { 470 case AF_INET: 471 /* netmask handling */ 472 rt_maskedcopy(&src->sa, &src->sa, &srcmask->sa); 473 rt_maskedcopy(&dst->sa, &dst->sa, &dstmask->sa); 474 475 flow->sen_type = SENT_IP4; 476 flow->sen_direction = ftype->sadb_protocol_direction; 477 flow->sen_ip_src = src->sin.sin_addr; 478 flow->sen_ip_dst = dst->sin.sin_addr; 479 flow->sen_proto = transproto; 480 flow->sen_sport = src->sin.sin_port; 481 flow->sen_dport = dst->sin.sin_port; 482 483 flowmask->sen_type = SENT_IP4; 484 flowmask->sen_direction = 0xff; 485 flowmask->sen_ip_src = srcmask->sin.sin_addr; 486 flowmask->sen_ip_dst = dstmask->sin.sin_addr; 487 flowmask->sen_sport = srcmask->sin.sin_port; 488 flowmask->sen_dport = dstmask->sin.sin_port; 489 if (transproto) 490 flowmask->sen_proto = 0xff; 491 break; 492 493 #ifdef INET6 494 case AF_INET6: 495 in6_embedscope(&src->sin6.sin6_addr, &src->sin6, 496 NULL, NULL); 497 in6_embedscope(&dst->sin6.sin6_addr, &dst->sin6, 498 NULL, NULL); 499 500 /* netmask handling */ 501 rt_maskedcopy(&src->sa, &src->sa, &srcmask->sa); 502 rt_maskedcopy(&dst->sa, &dst->sa, &dstmask->sa); 503 504 flow->sen_type = SENT_IP6; 505 flow->sen_ip6_direction = ftype->sadb_protocol_direction; 506 flow->sen_ip6_src = src->sin6.sin6_addr; 507 flow->sen_ip6_dst = dst->sin6.sin6_addr; 508 flow->sen_ip6_proto = transproto; 509 flow->sen_ip6_sport = src->sin6.sin6_port; 510 flow->sen_ip6_dport = dst->sin6.sin6_port; 511 512 flowmask->sen_type = SENT_IP6; 513 flowmask->sen_ip6_direction = 0xff; 514 flowmask->sen_ip6_src = srcmask->sin6.sin6_addr; 515 flowmask->sen_ip6_dst = dstmask->sin6.sin6_addr; 516 flowmask->sen_ip6_sport = srcmask->sin6.sin6_port; 517 flowmask->sen_ip6_dport = dstmask->sin6.sin6_port; 518 if (transproto) 519 flowmask->sen_ip6_proto = 0xff; 520 break; 521 #endif /* INET6 */ 522 } 523 } 524 525 /* 526 * Helper to export addresses from an struct sockaddr_encap. 527 */ 528 static void 529 export_encap(void **p, struct sockaddr_encap *encap, int type) 530 { 531 struct sadb_address *saddr = (struct sadb_address *)*p; 532 union sockaddr_union *sunion; 533 534 *p += sizeof(struct sadb_address); 535 sunion = (union sockaddr_union *)*p; 536 537 switch (encap->sen_type) { 538 case SENT_IP4: 539 saddr->sadb_address_len = (sizeof(struct sadb_address) + 540 PADUP(sizeof(struct sockaddr_in))) / sizeof(uint64_t); 541 sunion->sa.sa_len = sizeof(struct sockaddr_in); 542 sunion->sa.sa_family = AF_INET; 543 if (type == SADB_X_EXT_SRC_FLOW || 544 type == SADB_X_EXT_SRC_MASK) { 545 sunion->sin.sin_addr = encap->sen_ip_src; 546 sunion->sin.sin_port = encap->sen_sport; 547 } else { 548 sunion->sin.sin_addr = encap->sen_ip_dst; 549 sunion->sin.sin_port = encap->sen_dport; 550 } 551 *p += PADUP(sizeof(struct sockaddr_in)); 552 break; 553 case SENT_IP6: 554 saddr->sadb_address_len = (sizeof(struct sadb_address) 555 + PADUP(sizeof(struct sockaddr_in6))) / sizeof(uint64_t); 556 sunion->sa.sa_len = sizeof(struct sockaddr_in6); 557 sunion->sa.sa_family = AF_INET6; 558 if (type == SADB_X_EXT_SRC_FLOW || 559 type == SADB_X_EXT_SRC_MASK) { 560 sunion->sin6.sin6_addr = encap->sen_ip6_src; 561 sunion->sin6.sin6_port = encap->sen_ip6_sport; 562 } else { 563 sunion->sin6.sin6_addr = encap->sen_ip6_dst; 564 sunion->sin6.sin6_port = encap->sen_ip6_dport; 565 } 566 *p += PADUP(sizeof(struct sockaddr_in6)); 567 break; 568 } 569 } 570 571 /* 572 * Export flow information from two struct sockaddr_encap's. 573 */ 574 void 575 export_flow(void **p, u_int8_t ftype, struct sockaddr_encap *flow, 576 struct sockaddr_encap *flowmask, void **headers) 577 { 578 struct sadb_protocol *sab; 579 580 headers[SADB_X_EXT_FLOW_TYPE] = *p; 581 sab = (struct sadb_protocol *)*p; 582 sab->sadb_protocol_len = sizeof(struct sadb_protocol) / 583 sizeof(uint64_t); 584 585 switch (ftype) { 586 case IPSP_IPSEC_USE: 587 sab->sadb_protocol_proto = SADB_X_FLOW_TYPE_USE; 588 break; 589 case IPSP_IPSEC_ACQUIRE: 590 sab->sadb_protocol_proto = SADB_X_FLOW_TYPE_ACQUIRE; 591 break; 592 case IPSP_IPSEC_REQUIRE: 593 sab->sadb_protocol_proto = SADB_X_FLOW_TYPE_REQUIRE; 594 break; 595 case IPSP_DENY: 596 sab->sadb_protocol_proto = SADB_X_FLOW_TYPE_DENY; 597 break; 598 case IPSP_PERMIT: 599 sab->sadb_protocol_proto = SADB_X_FLOW_TYPE_BYPASS; 600 break; 601 case IPSP_IPSEC_DONTACQ: 602 sab->sadb_protocol_proto = SADB_X_FLOW_TYPE_DONTACQ; 603 break; 604 default: 605 sab->sadb_protocol_proto = 0; 606 break; 607 } 608 609 switch (flow->sen_type) { 610 case SENT_IP4: 611 sab->sadb_protocol_direction = flow->sen_direction; 612 break; 613 #ifdef INET6 614 case SENT_IP6: 615 sab->sadb_protocol_direction = flow->sen_ip6_direction; 616 break; 617 #endif /* INET6 */ 618 } 619 *p += sizeof(struct sadb_protocol); 620 621 headers[SADB_X_EXT_PROTOCOL] = *p; 622 sab = (struct sadb_protocol *)*p; 623 sab->sadb_protocol_len = sizeof(struct sadb_protocol) / 624 sizeof(uint64_t); 625 switch (flow->sen_type) { 626 case SENT_IP4: 627 sab->sadb_protocol_proto = flow->sen_proto; 628 break; 629 #ifdef INET6 630 case SENT_IP6: 631 sab->sadb_protocol_proto = flow->sen_ip6_proto; 632 break; 633 #endif /* INET6 */ 634 } 635 *p += sizeof(struct sadb_protocol); 636 637 headers[SADB_X_EXT_SRC_FLOW] = *p; 638 export_encap(p, flow, SADB_X_EXT_SRC_FLOW); 639 640 headers[SADB_X_EXT_SRC_MASK] = *p; 641 export_encap(p, flowmask, SADB_X_EXT_SRC_MASK); 642 643 headers[SADB_X_EXT_DST_FLOW] = *p; 644 export_encap(p, flow, SADB_X_EXT_DST_FLOW); 645 646 headers[SADB_X_EXT_DST_MASK] = *p; 647 export_encap(p, flowmask, SADB_X_EXT_DST_MASK); 648 } 649 650 /* 651 * Copy an SADB_ADDRESS payload to a struct sockaddr. 652 */ 653 void 654 import_address(struct sockaddr *sa, struct sadb_address *sadb_address) 655 { 656 int salen; 657 struct sockaddr *ssa = (struct sockaddr *)((void *) sadb_address + 658 sizeof(struct sadb_address)); 659 660 if (!sadb_address) 661 return; 662 663 if (ssa->sa_len) 664 salen = ssa->sa_len; 665 else 666 switch (ssa->sa_family) { 667 case AF_INET: 668 salen = sizeof(struct sockaddr_in); 669 break; 670 671 #ifdef INET6 672 case AF_INET6: 673 salen = sizeof(struct sockaddr_in6); 674 break; 675 #endif /* INET6 */ 676 677 default: 678 return; 679 } 680 681 bcopy(ssa, sa, salen); 682 sa->sa_len = salen; 683 } 684 685 /* 686 * Export a struct sockaddr as an SADB_ADDRESS payload. 687 */ 688 void 689 export_address(void **p, struct sockaddr *sa) 690 { 691 struct sadb_address *sadb_address = (struct sadb_address *) *p; 692 693 sadb_address->sadb_address_len = (sizeof(struct sadb_address) + 694 PADUP(SA_LEN(sa))) / sizeof(uint64_t); 695 696 *p += sizeof(struct sadb_address); 697 bcopy(sa, *p, SA_LEN(sa)); 698 ((struct sockaddr *) *p)->sa_family = sa->sa_family; 699 *p += PADUP(SA_LEN(sa)); 700 } 701 702 /* 703 * Import an identity payload into the TDB. 704 */ 705 void 706 import_identity(struct ipsec_ref **ipr, struct sadb_ident *sadb_ident) 707 { 708 if (!sadb_ident) 709 return; 710 711 *ipr = malloc(EXTLEN(sadb_ident) - sizeof(struct sadb_ident) + 712 sizeof(struct ipsec_ref), M_CREDENTIALS, M_WAITOK); 713 (*ipr)->ref_len = EXTLEN(sadb_ident) - sizeof(struct sadb_ident); 714 715 switch (sadb_ident->sadb_ident_type) { 716 case SADB_IDENTTYPE_PREFIX: 717 (*ipr)->ref_type = IPSP_IDENTITY_PREFIX; 718 break; 719 case SADB_IDENTTYPE_FQDN: 720 (*ipr)->ref_type = IPSP_IDENTITY_FQDN; 721 break; 722 case SADB_IDENTTYPE_USERFQDN: 723 (*ipr)->ref_type = IPSP_IDENTITY_USERFQDN; 724 break; 725 default: 726 free(*ipr, M_CREDENTIALS, 0); 727 *ipr = NULL; 728 return; 729 } 730 (*ipr)->ref_count = 1; 731 (*ipr)->ref_malloctype = M_CREDENTIALS; 732 bcopy((void *) sadb_ident + sizeof(struct sadb_ident), (*ipr) + 1, 733 (*ipr)->ref_len); 734 } 735 736 void 737 export_identity(void **p, struct ipsec_ref **ipr) 738 { 739 struct sadb_ident *sadb_ident = (struct sadb_ident *) *p; 740 741 sadb_ident->sadb_ident_len = (sizeof(struct sadb_ident) + 742 PADUP((*ipr)->ref_len)) / sizeof(uint64_t); 743 744 switch ((*ipr)->ref_type) { 745 case IPSP_IDENTITY_PREFIX: 746 sadb_ident->sadb_ident_type = SADB_IDENTTYPE_PREFIX; 747 break; 748 case IPSP_IDENTITY_FQDN: 749 sadb_ident->sadb_ident_type = SADB_IDENTTYPE_FQDN; 750 break; 751 case IPSP_IDENTITY_USERFQDN: 752 sadb_ident->sadb_ident_type = SADB_IDENTTYPE_USERFQDN; 753 break; 754 } 755 *p += sizeof(struct sadb_ident); 756 bcopy((*ipr) + 1, *p, (*ipr)->ref_len); 757 *p += PADUP((*ipr)->ref_len); 758 } 759 760 /* ... */ 761 void 762 import_key(struct ipsecinit *ii, struct sadb_key *sadb_key, int type) 763 { 764 if (!sadb_key) 765 return; 766 767 if (type == PFKEYV2_ENCRYPTION_KEY) { /* Encryption key */ 768 ii->ii_enckeylen = sadb_key->sadb_key_bits / 8; 769 ii->ii_enckey = (void *)sadb_key + sizeof(struct sadb_key); 770 } else { 771 ii->ii_authkeylen = sadb_key->sadb_key_bits / 8; 772 ii->ii_authkey = (void *)sadb_key + sizeof(struct sadb_key); 773 } 774 } 775 776 void 777 export_key(void **p, struct tdb *tdb, int type) 778 { 779 struct sadb_key *sadb_key = (struct sadb_key *) *p; 780 781 if (type == PFKEYV2_ENCRYPTION_KEY) { 782 sadb_key->sadb_key_len = (sizeof(struct sadb_key) + 783 PADUP(tdb->tdb_emxkeylen)) / 784 sizeof(uint64_t); 785 sadb_key->sadb_key_bits = tdb->tdb_emxkeylen * 8; 786 *p += sizeof(struct sadb_key); 787 bcopy(tdb->tdb_emxkey, *p, tdb->tdb_emxkeylen); 788 *p += PADUP(tdb->tdb_emxkeylen); 789 } else { 790 sadb_key->sadb_key_len = (sizeof(struct sadb_key) + 791 PADUP(tdb->tdb_amxkeylen)) / 792 sizeof(uint64_t); 793 sadb_key->sadb_key_bits = tdb->tdb_amxkeylen * 8; 794 *p += sizeof(struct sadb_key); 795 bcopy(tdb->tdb_amxkey, *p, tdb->tdb_amxkeylen); 796 *p += PADUP(tdb->tdb_amxkeylen); 797 } 798 } 799 800 /* Import/Export remote port for UDP Encapsulation */ 801 void 802 import_udpencap(struct tdb *tdb, struct sadb_x_udpencap *sadb_udpencap) 803 { 804 if (sadb_udpencap) 805 tdb->tdb_udpencap_port = sadb_udpencap->sadb_x_udpencap_port; 806 } 807 808 void 809 export_udpencap(void **p, struct tdb *tdb) 810 { 811 struct sadb_x_udpencap *sadb_udpencap = (struct sadb_x_udpencap *) *p; 812 813 sadb_udpencap->sadb_x_udpencap_port = tdb->tdb_udpencap_port; 814 sadb_udpencap->sadb_x_udpencap_reserved = 0; 815 sadb_udpencap->sadb_x_udpencap_len = 816 sizeof(struct sadb_x_udpencap) / sizeof(uint64_t); 817 *p += sizeof(struct sadb_x_udpencap); 818 } 819 820 #if NPF > 0 821 /* Import PF tag information for SA */ 822 void 823 import_tag(struct tdb *tdb, struct sadb_x_tag *stag) 824 { 825 char *s; 826 827 if (stag) { 828 s = (char *)(stag + 1); 829 tdb->tdb_tag = pf_tagname2tag(s, 1); 830 } 831 } 832 833 /* Export PF tag information for SA */ 834 void 835 export_tag(void **p, struct tdb *tdb) 836 { 837 struct sadb_x_tag *stag = (struct sadb_x_tag *)*p; 838 char *s = (char *)(stag + 1); 839 840 pf_tag2tagname(tdb->tdb_tag, s); 841 842 stag->sadb_x_tag_taglen = strlen(s) + 1; 843 stag->sadb_x_tag_len = (sizeof(struct sadb_x_tag) + 844 PADUP(stag->sadb_x_tag_taglen)) / sizeof(uint64_t); 845 *p += sizeof(struct sadb_x_tag) + PADUP(stag->sadb_x_tag_taglen); 846 } 847 848 /* Import enc(4) tap device information for SA */ 849 void 850 import_tap(struct tdb *tdb, struct sadb_x_tap *stap) 851 { 852 if (stap) 853 tdb->tdb_tap = stap->sadb_x_tap_unit; 854 } 855 856 /* Export enc(4) tap device information for SA */ 857 void 858 export_tap(void **p, struct tdb *tdb) 859 { 860 struct sadb_x_tap *stag = (struct sadb_x_tap *)*p; 861 862 stag->sadb_x_tap_unit = tdb->tdb_tap; 863 stag->sadb_x_tap_len = sizeof(struct sadb_x_tap) / sizeof(uint64_t); 864 *p += sizeof(struct sadb_x_tap); 865 } 866 #endif 867