1 /* $OpenBSD: pfkeyv2.c,v 1.263 2024/12/27 10:15:09 mvs Exp $ */ 2 3 /* 4 * @(#)COPYRIGHT 1.1 (NRL) 17 January 1995 5 * 6 * NRL grants permission for redistribution and use in source and binary 7 * forms, with or without modification, of the software and documentation 8 * created at NRL provided that the following conditions are met: 9 * 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgements: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * This product includes software developed at the Information 20 * Technology Division, US Naval Research Laboratory. 21 * 4. Neither the name of the NRL nor the names of its contributors 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THE SOFTWARE PROVIDED BY NRL IS PROVIDED BY NRL AND CONTRIBUTORS ``AS 26 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 28 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NRL OR 29 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 30 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 31 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 32 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 36 * 37 * The views and conclusions contained in the software and documentation 38 * are those of the authors and should not be interpreted as representing 39 * official policies, either expressed or implied, of the US Naval 40 * Research Laboratory (NRL). 41 */ 42 43 /* 44 * Copyright (c) 1995, 1996, 1997, 1998, 1999 Craig Metz. All rights reserved. 45 * 46 * Redistribution and use in source and binary forms, with or without 47 * modification, are permitted provided that the following conditions 48 * are met: 49 * 1. Redistributions of source code must retain the above copyright 50 * notice, this list of conditions and the following disclaimer. 51 * 2. Redistributions in binary form must reproduce the above copyright 52 * notice, this list of conditions and the following disclaimer in the 53 * documentation and/or other materials provided with the distribution. 54 * 3. Neither the name of the author nor the names of any contributors 55 * may be used to endorse or promote products derived from this software 56 * without specific prior written permission. 57 * 58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 68 * SUCH DAMAGE. 69 */ 70 71 #include "pf.h" 72 73 #include <sys/param.h> 74 #include <sys/socket.h> 75 #include <sys/socketvar.h> 76 #include <sys/protosw.h> 77 #include <sys/domain.h> 78 #include <sys/systm.h> 79 #include <sys/mbuf.h> 80 #include <sys/kernel.h> 81 #include <sys/proc.h> 82 #include <sys/pool.h> 83 #include <sys/mutex.h> 84 85 #include <net/route.h> 86 #include <netinet/ip_ipsp.h> 87 #include <net/pfkeyv2.h> 88 #include <net/radix.h> 89 #include <netinet/ip_ah.h> 90 #include <netinet/ip_esp.h> 91 #include <netinet/ip_ipcomp.h> 92 #include <crypto/blf.h> 93 94 #if NPF > 0 95 #include <net/if.h> 96 #include <net/pfvar.h> 97 #endif 98 99 #define PFKEYSNDQ 8192 100 #define PFKEYRCVQ 8192 101 102 static const struct sadb_alg ealgs[] = { 103 { SADB_EALG_NULL, 0, 0, 0 }, 104 { SADB_EALG_3DESCBC, 64, 192, 192 }, 105 { SADB_X_EALG_BLF, 64, 40, BLF_MAXKEYLEN * 8}, 106 { SADB_X_EALG_CAST, 64, 40, 128}, 107 { SADB_X_EALG_AES, 128, 128, 256}, 108 { SADB_X_EALG_AESCTR, 128, 128 + 32, 256 + 32} 109 }; 110 111 static const struct sadb_alg aalgs[] = { 112 { SADB_AALG_SHA1HMAC, 0, 160, 160 }, 113 { SADB_AALG_MD5HMAC, 0, 128, 128 }, 114 { SADB_X_AALG_RIPEMD160HMAC, 0, 160, 160 }, 115 { SADB_X_AALG_SHA2_256, 0, 256, 256 }, 116 { SADB_X_AALG_SHA2_384, 0, 384, 384 }, 117 { SADB_X_AALG_SHA2_512, 0, 512, 512 } 118 }; 119 120 static const struct sadb_alg calgs[] = { 121 { SADB_X_CALG_DEFLATE, 0, 0, 0} 122 }; 123 124 struct pool pkpcb_pool; 125 #define PFKEY_MSG_MAXSZ 4096 126 const struct sockaddr pfkey_addr = { 2, PF_KEY, }; 127 const struct domain pfkeydomain; 128 129 /* 130 * pfkey PCB 131 * 132 * Locks used to protect struct members in this file: 133 * I immutable after creation 134 * a atomic operations 135 * l pkptable's lock 136 * s socket lock 137 */ 138 struct pkpcb { 139 struct socket *kcb_socket; /* [I] associated socket */ 140 141 SRPL_ENTRY(pkpcb) kcb_list; /* [l] */ 142 struct refcnt kcb_refcnt; /* [a] */ 143 int kcb_flags; /* [s] */ 144 uint32_t kcb_reg; /* [s] Inc if SATYPE_MAX > 31 */ 145 uint32_t kcb_pid; /* [I] */ 146 unsigned int kcb_rdomain; /* [I] routing domain */ 147 }; 148 #define sotokeycb(so) ((struct pkpcb *)(so)->so_pcb) 149 #define keylock(kp) solock((kp)->kcb_socket) 150 #define keyunlock(kp) sounlock((kp)->kcb_socket) 151 152 153 struct dump_state { 154 struct sadb_msg *sadb_msg; 155 struct socket *socket; 156 }; 157 158 struct pkptable { 159 SRPL_HEAD(, pkpcb) pkp_list; 160 struct srpl_rc pkp_rc; 161 struct rwlock pkp_lk; 162 }; 163 164 struct pkptable pkptable; 165 struct mutex pfkeyv2_mtx = MUTEX_INITIALIZER(IPL_MPFLOOR); 166 static uint32_t pfkeyv2_seq = 1; 167 static int nregistered = 0; 168 static int npromisc = 0; 169 170 void pfkey_init(void); 171 172 int pfkeyv2_attach(struct socket *, int, int); 173 int pfkeyv2_detach(struct socket *); 174 int pfkeyv2_disconnect(struct socket *); 175 int pfkeyv2_shutdown(struct socket *); 176 int pfkeyv2_send(struct socket *, struct mbuf *, struct mbuf *, 177 struct mbuf *); 178 int pfkeyv2_sockaddr(struct socket *, struct mbuf *); 179 int pfkeyv2_peeraddr(struct socket *, struct mbuf *); 180 int pfkeyv2_output(struct mbuf *, struct socket *); 181 int pfkey_sendup(struct pkpcb *, struct mbuf *, int); 182 int pfkeyv2_sa_flush(struct tdb *, void *, int); 183 int pfkeyv2_policy_flush(struct ipsec_policy *, void *, unsigned int); 184 int pfkeyv2_sysctl_policydumper(struct ipsec_policy *, void *, unsigned int); 185 186 void keycb_ref(void *, void *); 187 void keycb_unref(void *, void *); 188 189 /* 190 * Wrapper around m_devget(); copy data from contiguous buffer to mbuf 191 * chain. 192 */ 193 int 194 pfdatatopacket(void *data, int len, struct mbuf **packet) 195 { 196 if (!(*packet = m_devget(data, len, 0))) 197 return (ENOMEM); 198 199 /* Make sure, all data gets zeroized on free */ 200 (*packet)->m_flags |= M_ZEROIZE; 201 202 return (0); 203 } 204 205 const struct pr_usrreqs pfkeyv2_usrreqs = { 206 .pru_attach = pfkeyv2_attach, 207 .pru_detach = pfkeyv2_detach, 208 .pru_disconnect = pfkeyv2_disconnect, 209 .pru_shutdown = pfkeyv2_shutdown, 210 .pru_send = pfkeyv2_send, 211 .pru_sockaddr = pfkeyv2_sockaddr, 212 .pru_peeraddr = pfkeyv2_peeraddr, 213 }; 214 215 const struct protosw pfkeysw[] = { 216 { 217 .pr_type = SOCK_RAW, 218 .pr_domain = &pfkeydomain, 219 .pr_protocol = PF_KEY_V2, 220 .pr_flags = PR_ATOMIC | PR_ADDR, 221 .pr_usrreqs = &pfkeyv2_usrreqs, 222 .pr_sysctl = pfkeyv2_sysctl, 223 } 224 }; 225 226 const struct domain pfkeydomain = { 227 .dom_family = PF_KEY, 228 .dom_name = "pfkey", 229 .dom_init = pfkey_init, 230 .dom_protosw = pfkeysw, 231 .dom_protoswNPROTOSW = &pfkeysw[nitems(pfkeysw)], 232 }; 233 234 void 235 keycb_ref(void *null, void *v) 236 { 237 struct pkpcb *kp = v; 238 239 refcnt_take(&kp->kcb_refcnt); 240 } 241 242 void 243 keycb_unref(void *null, void *v) 244 { 245 struct pkpcb *kp = v; 246 247 refcnt_rele_wake(&kp->kcb_refcnt); 248 } 249 250 void 251 pfkey_init(void) 252 { 253 rn_init(sizeof(struct sockaddr_encap)); 254 srpl_rc_init(&pkptable.pkp_rc, keycb_ref, keycb_unref, NULL); 255 rw_init(&pkptable.pkp_lk, "pfkey"); 256 SRPL_INIT(&pkptable.pkp_list); 257 pool_init(&pkpcb_pool, sizeof(struct pkpcb), 0, 258 IPL_SOFTNET, PR_WAITOK, "pkpcb", NULL); 259 pool_init(&ipsec_policy_pool, sizeof(struct ipsec_policy), 0, 260 IPL_SOFTNET, 0, "ipsec policy", NULL); 261 pool_init(&ipsec_acquire_pool, sizeof(struct ipsec_acquire), 0, 262 IPL_SOFTNET, 0, "ipsec acquire", NULL); 263 } 264 265 266 /* 267 * Attach a new PF_KEYv2 socket. 268 */ 269 int 270 pfkeyv2_attach(struct socket *so, int proto, int wait) 271 { 272 struct pkpcb *kp; 273 int error; 274 275 if ((so->so_state & SS_PRIV) == 0) 276 return EACCES; 277 278 error = soreserve(so, PFKEYSNDQ, PFKEYRCVQ); 279 if (error) 280 return (error); 281 282 kp = pool_get(&pkpcb_pool, (wait == M_WAIT ? PR_WAITOK : PR_NOWAIT) | 283 PR_ZERO); 284 if (kp == NULL) 285 return (ENOBUFS); 286 so->so_pcb = kp; 287 refcnt_init(&kp->kcb_refcnt); 288 kp->kcb_socket = so; 289 kp->kcb_pid = curproc->p_p->ps_pid; 290 kp->kcb_rdomain = rtable_l2(curproc->p_p->ps_rtableid); 291 292 so->so_options |= SO_USELOOPBACK; 293 soisconnected(so); 294 295 rw_enter(&pkptable.pkp_lk, RW_WRITE); 296 SRPL_INSERT_HEAD_LOCKED(&pkptable.pkp_rc, &pkptable.pkp_list, kp, kcb_list); 297 rw_exit(&pkptable.pkp_lk); 298 299 return (0); 300 } 301 302 /* 303 * Close a PF_KEYv2 socket. 304 */ 305 int 306 pfkeyv2_detach(struct socket *so) 307 { 308 struct pkpcb *kp; 309 310 soassertlocked(so); 311 312 kp = sotokeycb(so); 313 if (kp == NULL) 314 return ENOTCONN; 315 316 if (kp->kcb_flags & 317 (PFKEYV2_SOCKETFLAGS_REGISTERED|PFKEYV2_SOCKETFLAGS_PROMISC)) { 318 mtx_enter(&pfkeyv2_mtx); 319 if (kp->kcb_flags & PFKEYV2_SOCKETFLAGS_REGISTERED) 320 nregistered--; 321 322 if (kp->kcb_flags & PFKEYV2_SOCKETFLAGS_PROMISC) 323 npromisc--; 324 mtx_leave(&pfkeyv2_mtx); 325 } 326 327 rw_enter(&pkptable.pkp_lk, RW_WRITE); 328 SRPL_REMOVE_LOCKED(&pkptable.pkp_rc, &pkptable.pkp_list, kp, pkpcb, 329 kcb_list); 330 rw_exit(&pkptable.pkp_lk); 331 332 sounlock(so); 333 /* wait for all references to drop */ 334 refcnt_finalize(&kp->kcb_refcnt, "pfkeyrefs"); 335 solock(so); 336 337 so->so_pcb = NULL; 338 KASSERT((so->so_state & SS_NOFDREF) == 0); 339 pool_put(&pkpcb_pool, kp); 340 341 return (0); 342 } 343 344 int 345 pfkeyv2_disconnect(struct socket *so) 346 { 347 soisdisconnected(so); 348 return (0); 349 } 350 351 int 352 pfkeyv2_shutdown(struct socket *so) 353 { 354 socantsendmore(so); 355 return (0); 356 } 357 358 int 359 pfkeyv2_send(struct socket *so, struct mbuf *m, struct mbuf *nam, 360 struct mbuf *control) 361 { 362 int error; 363 364 soassertlocked(so); 365 366 if (control && control->m_len) { 367 error = EOPNOTSUPP; 368 goto out; 369 } 370 371 if (nam) { 372 error = EISCONN; 373 goto out; 374 } 375 376 error = pfkeyv2_output(m, so); 377 m = NULL; 378 379 out: 380 m_freem(control); 381 m_freem(m); 382 383 return (error); 384 } 385 386 int 387 pfkeyv2_sockaddr(struct socket *so, struct mbuf *nam) 388 { 389 return (EINVAL); 390 } 391 392 int 393 pfkeyv2_peeraddr(struct socket *so, struct mbuf *nam) 394 { 395 /* minimal support, just implement a fake peer address */ 396 bcopy(&pfkey_addr, mtod(nam, caddr_t), pfkey_addr.sa_len); 397 nam->m_len = pfkey_addr.sa_len; 398 return (0); 399 } 400 401 int 402 pfkeyv2_output(struct mbuf *mbuf, struct socket *so) 403 { 404 void *message; 405 int error = 0; 406 407 #ifdef DIAGNOSTIC 408 if (!mbuf || !(mbuf->m_flags & M_PKTHDR)) { 409 error = EINVAL; 410 goto ret; 411 } 412 #endif /* DIAGNOSTIC */ 413 414 if (mbuf->m_pkthdr.len > PFKEY_MSG_MAXSZ) { 415 error = EMSGSIZE; 416 goto ret; 417 } 418 419 if (!(message = malloc((unsigned long) mbuf->m_pkthdr.len, 420 M_PFKEY, M_DONTWAIT))) { 421 error = ENOMEM; 422 goto ret; 423 } 424 425 m_copydata(mbuf, 0, mbuf->m_pkthdr.len, message); 426 427 /* 428 * The socket can't be closed concurrently because the file 429 * descriptor reference is still held. 430 */ 431 432 sounlock(so); 433 error = pfkeyv2_dosend(so, message, mbuf->m_pkthdr.len); 434 solock(so); 435 436 ret: 437 m_freem(mbuf); 438 return (error); 439 } 440 441 int 442 pfkey_sendup(struct pkpcb *kp, struct mbuf *m0, int more) 443 { 444 struct socket *so = kp->kcb_socket; 445 struct mbuf *m; 446 int ret; 447 448 if (more) { 449 if (!(m = m_dup_pkt(m0, 0, M_DONTWAIT))) 450 return (ENOMEM); 451 } else 452 m = m0; 453 454 mtx_enter(&so->so_rcv.sb_mtx); 455 ret = sbappendaddr(so, &so->so_rcv, &pfkey_addr, m, NULL); 456 mtx_leave(&so->so_rcv.sb_mtx); 457 458 if (ret == 0) { 459 m_freem(m); 460 return (ENOBUFS); 461 } 462 463 sorwakeup(so); 464 return (0); 465 } 466 467 /* 468 * Send a PFKEYv2 message, possibly to many receivers, based on the 469 * satype of the socket (which is set by the REGISTER message), and the 470 * third argument. 471 */ 472 int 473 pfkeyv2_sendmessage(void **headers, int mode, struct socket *so, 474 u_int8_t satype, int count, u_int rdomain) 475 { 476 int i, j, rval; 477 void *p, *buffer = NULL; 478 struct mbuf *packet; 479 struct pkpcb *kp; 480 struct sadb_msg *smsg; 481 struct srp_ref sr; 482 483 /* Find out how much space we'll need... */ 484 j = sizeof(struct sadb_msg); 485 486 for (i = 1; i <= SADB_EXT_MAX; i++) 487 if (headers[i]) 488 j += ((struct sadb_ext *)headers[i])->sadb_ext_len * 489 sizeof(uint64_t); 490 491 /* ...and allocate it */ 492 if (!(buffer = malloc(j + sizeof(struct sadb_msg), M_PFKEY, 493 M_NOWAIT))) { 494 rval = ENOMEM; 495 goto ret; 496 } 497 498 p = buffer + sizeof(struct sadb_msg); 499 bcopy(headers[0], p, sizeof(struct sadb_msg)); 500 ((struct sadb_msg *) p)->sadb_msg_len = j / sizeof(uint64_t); 501 p += sizeof(struct sadb_msg); 502 503 /* Copy payloads in the packet */ 504 for (i = 1; i <= SADB_EXT_MAX; i++) 505 if (headers[i]) { 506 ((struct sadb_ext *) headers[i])->sadb_ext_type = i; 507 bcopy(headers[i], p, EXTLEN(headers[i])); 508 p += EXTLEN(headers[i]); 509 } 510 511 if ((rval = pfdatatopacket(buffer + sizeof(struct sadb_msg), 512 j, &packet)) != 0) 513 goto ret; 514 515 switch (mode) { 516 case PFKEYV2_SENDMESSAGE_UNICAST: 517 /* 518 * Send message to the specified socket, plus all 519 * promiscuous listeners. 520 */ 521 pfkey_sendup(sotokeycb(so), packet, 0); 522 523 /* 524 * Promiscuous messages contain the original message 525 * encapsulated in another sadb_msg header. 526 */ 527 bzero(buffer, sizeof(struct sadb_msg)); 528 smsg = (struct sadb_msg *) buffer; 529 smsg->sadb_msg_version = PF_KEY_V2; 530 smsg->sadb_msg_type = SADB_X_PROMISC; 531 smsg->sadb_msg_len = (sizeof(struct sadb_msg) + j) / 532 sizeof(uint64_t); 533 smsg->sadb_msg_seq = 0; 534 535 /* Copy to mbuf chain */ 536 if ((rval = pfdatatopacket(buffer, sizeof(struct sadb_msg) + j, 537 &packet)) != 0) 538 goto ret; 539 540 /* 541 * Search for promiscuous listeners, skipping the 542 * original destination. 543 */ 544 SRPL_FOREACH(kp, &sr, &pkptable.pkp_list, kcb_list) { 545 if (kp->kcb_socket == so || kp->kcb_rdomain != rdomain) 546 continue; 547 548 if (kp->kcb_flags & PFKEYV2_SOCKETFLAGS_PROMISC) 549 pfkey_sendup(kp, packet, 1); 550 } 551 SRPL_LEAVE(&sr); 552 m_freem(packet); 553 break; 554 555 case PFKEYV2_SENDMESSAGE_REGISTERED: 556 /* 557 * Send the message to all registered sockets that match 558 * the specified satype (e.g., all IPSEC-ESP negotiators) 559 */ 560 SRPL_FOREACH(kp, &sr, &pkptable.pkp_list, kcb_list) { 561 if (kp->kcb_rdomain != rdomain) 562 continue; 563 564 if (kp->kcb_flags & PFKEYV2_SOCKETFLAGS_REGISTERED) { 565 if (!satype) { 566 /* Just send to everyone registered */ 567 pfkey_sendup(kp, packet, 1); 568 } else { 569 keylock(kp); 570 /* Check for specified satype */ 571 if ((1 << satype) & kp->kcb_reg) 572 pfkey_sendup(kp, packet, 1); 573 keyunlock(kp); 574 } 575 } 576 } 577 SRPL_LEAVE(&sr); 578 /* Free last/original copy of the packet */ 579 m_freem(packet); 580 581 /* Encapsulate the original message "inside" an sadb_msg header */ 582 bzero(buffer, sizeof(struct sadb_msg)); 583 smsg = (struct sadb_msg *) buffer; 584 smsg->sadb_msg_version = PF_KEY_V2; 585 smsg->sadb_msg_type = SADB_X_PROMISC; 586 smsg->sadb_msg_len = (sizeof(struct sadb_msg) + j) / 587 sizeof(uint64_t); 588 smsg->sadb_msg_seq = 0; 589 590 /* Convert to mbuf chain */ 591 if ((rval = pfdatatopacket(buffer, sizeof(struct sadb_msg) + j, 592 &packet)) != 0) 593 goto ret; 594 595 /* Send to all registered promiscuous listeners */ 596 SRPL_FOREACH(kp, &sr, &pkptable.pkp_list, kcb_list) { 597 int flags = READ_ONCE(kp->kcb_flags); 598 599 if (kp->kcb_rdomain != rdomain) 600 continue; 601 602 if ((flags & PFKEYV2_SOCKETFLAGS_PROMISC) && 603 !(flags & PFKEYV2_SOCKETFLAGS_REGISTERED)) 604 pfkey_sendup(kp, packet, 1); 605 } 606 SRPL_LEAVE(&sr); 607 m_freem(packet); 608 break; 609 610 case PFKEYV2_SENDMESSAGE_BROADCAST: 611 /* Send message to all sockets */ 612 SRPL_FOREACH(kp, &sr, &pkptable.pkp_list, kcb_list) { 613 if (kp->kcb_rdomain != rdomain) 614 continue; 615 616 pfkey_sendup(kp, packet, 1); 617 } 618 SRPL_LEAVE(&sr); 619 m_freem(packet); 620 break; 621 } 622 623 ret: 624 if (buffer != NULL) { 625 explicit_bzero(buffer, j + sizeof(struct sadb_msg)); 626 free(buffer, M_PFKEY, j + sizeof(struct sadb_msg)); 627 } 628 629 return (rval); 630 } 631 632 /* 633 * Get SPD information for an ACQUIRE. We setup the message such that 634 * the SRC/DST payloads are relative to us (regardless of whether the 635 * SPD rule was for incoming or outgoing packets). 636 */ 637 int 638 pfkeyv2_policy(struct ipsec_acquire *ipa, void **headers, void **buffer, 639 int *bufferlen) 640 { 641 union sockaddr_union sunion; 642 struct sadb_protocol *sp; 643 int rval, i, dir; 644 void *p; 645 646 /* Find out how big a buffer we need */ 647 i = 4 * sizeof(struct sadb_address) + sizeof(struct sadb_protocol); 648 bzero(&sunion, sizeof(union sockaddr_union)); 649 650 switch (ipa->ipa_info.sen_type) { 651 case SENT_IP4: 652 i += 4 * PADUP(sizeof(struct sockaddr_in)); 653 sunion.sa.sa_family = AF_INET; 654 sunion.sa.sa_len = sizeof(struct sockaddr_in); 655 dir = ipa->ipa_info.sen_direction; 656 break; 657 658 #ifdef INET6 659 case SENT_IP6: 660 i += 4 * PADUP(sizeof(struct sockaddr_in6)); 661 sunion.sa.sa_family = AF_INET6; 662 sunion.sa.sa_len = sizeof(struct sockaddr_in6); 663 dir = ipa->ipa_info.sen_ip6_direction; 664 break; 665 #endif /* INET6 */ 666 667 default: 668 return (EINVAL); 669 } 670 671 if (!(p = malloc(i, M_PFKEY, M_NOWAIT | M_ZERO))) { 672 rval = ENOMEM; 673 goto ret; 674 } else { 675 *buffer = p; 676 *bufferlen = i; 677 } 678 679 if (dir == IPSP_DIRECTION_OUT) 680 headers[SADB_X_EXT_SRC_FLOW] = p; 681 else 682 headers[SADB_X_EXT_DST_FLOW] = p; 683 switch (sunion.sa.sa_family) { 684 case AF_INET: 685 sunion.sin.sin_addr = ipa->ipa_info.sen_ip_src; 686 sunion.sin.sin_port = ipa->ipa_info.sen_sport; 687 break; 688 689 #ifdef INET6 690 case AF_INET6: 691 sunion.sin6.sin6_addr = ipa->ipa_info.sen_ip6_src; 692 sunion.sin6.sin6_port = ipa->ipa_info.sen_ip6_sport; 693 break; 694 #endif /* INET6 */ 695 } 696 export_address(&p, &sunion.sa); 697 698 if (dir == IPSP_DIRECTION_OUT) 699 headers[SADB_X_EXT_SRC_MASK] = p; 700 else 701 headers[SADB_X_EXT_DST_MASK] = p; 702 switch (sunion.sa.sa_family) { 703 case AF_INET: 704 sunion.sin.sin_addr = ipa->ipa_mask.sen_ip_src; 705 sunion.sin.sin_port = ipa->ipa_mask.sen_sport; 706 break; 707 708 #ifdef INET6 709 case AF_INET6: 710 sunion.sin6.sin6_addr = ipa->ipa_mask.sen_ip6_src; 711 sunion.sin6.sin6_port = ipa->ipa_mask.sen_ip6_sport; 712 break; 713 #endif /* INET6 */ 714 } 715 export_address(&p, &sunion.sa); 716 717 if (dir == IPSP_DIRECTION_OUT) 718 headers[SADB_X_EXT_DST_FLOW] = p; 719 else 720 headers[SADB_X_EXT_SRC_FLOW] = p; 721 switch (sunion.sa.sa_family) { 722 case AF_INET: 723 sunion.sin.sin_addr = ipa->ipa_info.sen_ip_dst; 724 sunion.sin.sin_port = ipa->ipa_info.sen_dport; 725 break; 726 727 #ifdef INET6 728 case AF_INET6: 729 sunion.sin6.sin6_addr = ipa->ipa_info.sen_ip6_dst; 730 sunion.sin6.sin6_port = ipa->ipa_info.sen_ip6_dport; 731 break; 732 #endif /* INET6 */ 733 } 734 export_address(&p, &sunion.sa); 735 736 if (dir == IPSP_DIRECTION_OUT) 737 headers[SADB_X_EXT_DST_MASK] = p; 738 else 739 headers[SADB_X_EXT_SRC_MASK] = p; 740 switch (sunion.sa.sa_family) { 741 case AF_INET: 742 sunion.sin.sin_addr = ipa->ipa_mask.sen_ip_dst; 743 sunion.sin.sin_port = ipa->ipa_mask.sen_dport; 744 break; 745 746 #ifdef INET6 747 case AF_INET6: 748 sunion.sin6.sin6_addr = ipa->ipa_mask.sen_ip6_dst; 749 sunion.sin6.sin6_port = ipa->ipa_mask.sen_ip6_dport; 750 break; 751 #endif /* INET6 */ 752 } 753 export_address(&p, &sunion.sa); 754 755 headers[SADB_X_EXT_FLOW_TYPE] = p; 756 sp = p; 757 sp->sadb_protocol_len = sizeof(struct sadb_protocol) / 758 sizeof(u_int64_t); 759 switch (sunion.sa.sa_family) { 760 case AF_INET: 761 if (ipa->ipa_mask.sen_proto) 762 sp->sadb_protocol_proto = ipa->ipa_info.sen_proto; 763 sp->sadb_protocol_direction = ipa->ipa_info.sen_direction; 764 break; 765 766 #ifdef INET6 767 case AF_INET6: 768 if (ipa->ipa_mask.sen_ip6_proto) 769 sp->sadb_protocol_proto = ipa->ipa_info.sen_ip6_proto; 770 sp->sadb_protocol_direction = ipa->ipa_info.sen_ip6_direction; 771 break; 772 #endif /* INET6 */ 773 } 774 775 rval = 0; 776 777 ret: 778 return (rval); 779 } 780 781 /* 782 * Get all the information contained in an SA to a PFKEYV2 message. 783 */ 784 int 785 pfkeyv2_get(struct tdb *tdb, void **headers, void **buffer, int *lenp, 786 int *lenused) 787 { 788 int rval, i; 789 void *p; 790 791 NET_ASSERT_LOCKED(); 792 793 /* Find how much space we need */ 794 i = sizeof(struct sadb_sa) + sizeof(struct sadb_lifetime) + 795 sizeof(struct sadb_x_counter); 796 797 if (tdb->tdb_soft_allocations || tdb->tdb_soft_bytes || 798 tdb->tdb_soft_timeout || tdb->tdb_soft_first_use) 799 i += sizeof(struct sadb_lifetime); 800 801 if (tdb->tdb_exp_allocations || tdb->tdb_exp_bytes || 802 tdb->tdb_exp_timeout || tdb->tdb_exp_first_use) 803 i += sizeof(struct sadb_lifetime); 804 805 if (tdb->tdb_last_used) 806 i += sizeof(struct sadb_lifetime); 807 808 i += sizeof(struct sadb_address) + PADUP(tdb->tdb_src.sa.sa_len); 809 i += sizeof(struct sadb_address) + PADUP(tdb->tdb_dst.sa.sa_len); 810 811 if (tdb->tdb_ids) { 812 i += sizeof(struct sadb_ident) + PADUP(tdb->tdb_ids->id_local->len); 813 i += sizeof(struct sadb_ident) + PADUP(tdb->tdb_ids->id_remote->len); 814 } 815 816 if (tdb->tdb_amxkey) 817 i += sizeof(struct sadb_key) + PADUP(tdb->tdb_amxkeylen); 818 819 if (tdb->tdb_emxkey) 820 i += sizeof(struct sadb_key) + PADUP(tdb->tdb_emxkeylen); 821 822 if (tdb->tdb_filter.sen_type) { 823 i += 2 * sizeof(struct sadb_protocol); 824 825 /* We'll need four of them: src, src mask, dst, dst mask. */ 826 switch (tdb->tdb_filter.sen_type) { 827 case SENT_IP4: 828 i += 4 * PADUP(sizeof(struct sockaddr_in)); 829 i += 4 * sizeof(struct sadb_address); 830 break; 831 #ifdef INET6 832 case SENT_IP6: 833 i += 4 * PADUP(sizeof(struct sockaddr_in6)); 834 i += 4 * sizeof(struct sadb_address); 835 break; 836 #endif /* INET6 */ 837 default: 838 rval = EINVAL; 839 goto ret; 840 } 841 } 842 843 if (tdb->tdb_onext) { 844 i += sizeof(struct sadb_sa); 845 i += sizeof(struct sadb_address) + 846 PADUP(tdb->tdb_onext->tdb_dst.sa.sa_len); 847 i += sizeof(struct sadb_protocol); 848 } 849 850 if (tdb->tdb_udpencap_port) 851 i += sizeof(struct sadb_x_udpencap); 852 853 i += sizeof(struct sadb_x_replay); 854 855 if (tdb->tdb_mtu > 0) 856 i+= sizeof(struct sadb_x_mtu); 857 858 if (tdb->tdb_rdomain != tdb->tdb_rdomain_post) 859 i += sizeof(struct sadb_x_rdomain); 860 861 #if NPF > 0 862 if (tdb->tdb_tag) 863 i += sizeof(struct sadb_x_tag) + PADUP(PF_TAG_NAME_SIZE); 864 if (tdb->tdb_tap) 865 i += sizeof(struct sadb_x_tap); 866 #endif 867 868 if (ISSET(tdb->tdb_flags, TDBF_IFACE)) 869 i += sizeof(struct sadb_x_iface); 870 871 if (lenp) 872 *lenp = i; 873 874 if (buffer == NULL) { 875 rval = 0; 876 goto ret; 877 } 878 879 if (!(p = malloc(i, M_PFKEY, M_NOWAIT | M_ZERO))) { 880 rval = ENOMEM; 881 goto ret; 882 } else 883 *buffer = p; 884 885 headers[SADB_EXT_SA] = p; 886 887 export_sa(&p, tdb); /* Export SA information (mostly flags) */ 888 889 /* Export lifetimes where applicable */ 890 headers[SADB_EXT_LIFETIME_CURRENT] = p; 891 export_lifetime(&p, tdb, PFKEYV2_LIFETIME_CURRENT); 892 893 if (tdb->tdb_soft_allocations || tdb->tdb_soft_bytes || 894 tdb->tdb_soft_first_use || tdb->tdb_soft_timeout) { 895 headers[SADB_EXT_LIFETIME_SOFT] = p; 896 export_lifetime(&p, tdb, PFKEYV2_LIFETIME_SOFT); 897 } 898 899 if (tdb->tdb_exp_allocations || tdb->tdb_exp_bytes || 900 tdb->tdb_exp_first_use || tdb->tdb_exp_timeout) { 901 headers[SADB_EXT_LIFETIME_HARD] = p; 902 export_lifetime(&p, tdb, PFKEYV2_LIFETIME_HARD); 903 } 904 905 if (tdb->tdb_last_used) { 906 headers[SADB_X_EXT_LIFETIME_LASTUSE] = p; 907 export_lifetime(&p, tdb, PFKEYV2_LIFETIME_LASTUSE); 908 } 909 910 /* Export TDB source address */ 911 headers[SADB_EXT_ADDRESS_SRC] = p; 912 export_address(&p, &tdb->tdb_src.sa); 913 914 /* Export TDB destination address */ 915 headers[SADB_EXT_ADDRESS_DST] = p; 916 export_address(&p, &tdb->tdb_dst.sa); 917 918 /* Export source/destination identities, if present */ 919 if (tdb->tdb_ids) 920 export_identities(&p, tdb->tdb_ids, tdb->tdb_ids_swapped, headers); 921 922 /* Export authentication key, if present */ 923 if (tdb->tdb_amxkey) { 924 headers[SADB_EXT_KEY_AUTH] = p; 925 export_key(&p, tdb, PFKEYV2_AUTHENTICATION_KEY); 926 } 927 928 /* Export encryption key, if present */ 929 if (tdb->tdb_emxkey) { 930 headers[SADB_EXT_KEY_ENCRYPT] = p; 931 export_key(&p, tdb, PFKEYV2_ENCRYPTION_KEY); 932 } 933 934 /* Export flow/filter, if present */ 935 if (tdb->tdb_filter.sen_type) 936 export_flow(&p, IPSP_IPSEC_USE, &tdb->tdb_filter, 937 &tdb->tdb_filtermask, headers); 938 939 if (tdb->tdb_onext) { 940 headers[SADB_X_EXT_SA2] = p; 941 export_sa(&p, tdb->tdb_onext); 942 headers[SADB_X_EXT_DST2] = p; 943 export_address(&p, &tdb->tdb_onext->tdb_dst.sa); 944 headers[SADB_X_EXT_SATYPE2] = p; 945 export_satype(&p, tdb->tdb_onext); 946 } 947 948 /* Export UDP encapsulation port, if present */ 949 if (tdb->tdb_udpencap_port) { 950 headers[SADB_X_EXT_UDPENCAP] = p; 951 export_udpencap(&p, tdb); 952 } 953 954 headers[SADB_X_EXT_REPLAY] = p; 955 export_replay(&p, tdb); 956 957 if (tdb->tdb_mtu > 0) { 958 headers[SADB_X_EXT_MTU] = p; 959 export_mtu(&p, tdb); 960 } 961 962 /* Export rdomain switch, if present */ 963 if (tdb->tdb_rdomain != tdb->tdb_rdomain_post) { 964 headers[SADB_X_EXT_RDOMAIN] = p; 965 export_rdomain(&p, tdb); 966 } 967 968 #if NPF > 0 969 /* Export tag information, if present */ 970 if (tdb->tdb_tag) { 971 headers[SADB_X_EXT_TAG] = p; 972 export_tag(&p, tdb); 973 } 974 975 /* Export tap enc(4) device information, if present */ 976 if (tdb->tdb_tap) { 977 headers[SADB_X_EXT_TAP] = p; 978 export_tap(&p, tdb); 979 } 980 #endif 981 982 /* Export sec(4) interface information, if present */ 983 if (ISSET(tdb->tdb_flags, TDBF_IFACE)) { 984 headers[SADB_X_EXT_IFACE] = p; 985 export_iface(&p, tdb); 986 } 987 988 headers[SADB_X_EXT_COUNTER] = p; 989 export_counter(&p, tdb); 990 991 if (lenused) 992 *lenused = p - *buffer; 993 rval = 0; 994 995 ret: 996 return (rval); 997 } 998 999 /* 1000 * Dump a TDB. 1001 */ 1002 int 1003 pfkeyv2_dump_walker(struct tdb *tdb, void *state, int last) 1004 { 1005 struct dump_state *dump_state = (struct dump_state *) state; 1006 void *headers[SADB_EXT_MAX+1], *buffer; 1007 int buflen; 1008 int rval; 1009 1010 /* If not satype was specified, dump all TDBs */ 1011 if (!dump_state->sadb_msg->sadb_msg_satype || 1012 (tdb->tdb_satype == dump_state->sadb_msg->sadb_msg_satype)) { 1013 bzero(headers, sizeof(headers)); 1014 headers[0] = (void *) dump_state->sadb_msg; 1015 1016 /* Get the information from the TDB to a PFKEYv2 message */ 1017 if ((rval = pfkeyv2_get(tdb, headers, &buffer, &buflen, NULL)) != 0) 1018 return (rval); 1019 1020 if (last) 1021 ((struct sadb_msg *)headers[0])->sadb_msg_seq = 0; 1022 1023 /* Send the message to the specified socket */ 1024 rval = pfkeyv2_sendmessage(headers, 1025 PFKEYV2_SENDMESSAGE_UNICAST, dump_state->socket, 0, 0, 1026 tdb->tdb_rdomain); 1027 1028 explicit_bzero(buffer, buflen); 1029 free(buffer, M_PFKEY, buflen); 1030 if (rval) 1031 return (rval); 1032 } 1033 1034 return (0); 1035 } 1036 1037 /* 1038 * Delete an SA. 1039 */ 1040 int 1041 pfkeyv2_sa_flush(struct tdb *tdb, void *satype_vp, int last) 1042 { 1043 if (!(*((u_int8_t *) satype_vp)) || 1044 tdb->tdb_satype == *((u_int8_t *) satype_vp)) 1045 tdb_delete(tdb); 1046 return (0); 1047 } 1048 1049 /* 1050 * Convert between SATYPEs and IPsec protocols, taking into consideration 1051 * sysctl variables enabling/disabling ESP/AH and the presence of the old 1052 * IPsec transforms. 1053 */ 1054 int 1055 pfkeyv2_get_proto_alg(u_int8_t satype, u_int8_t *sproto, int *alg) 1056 { 1057 switch (satype) { 1058 #ifdef IPSEC 1059 case SADB_SATYPE_AH: 1060 if (!atomic_load_int(&ah_enable)) 1061 return (EOPNOTSUPP); 1062 1063 *sproto = IPPROTO_AH; 1064 1065 if(alg != NULL) 1066 *alg = satype = XF_AH; 1067 1068 break; 1069 1070 case SADB_SATYPE_ESP: 1071 if (!esp_enable) 1072 return (EOPNOTSUPP); 1073 1074 *sproto = IPPROTO_ESP; 1075 1076 if(alg != NULL) 1077 *alg = satype = XF_ESP; 1078 1079 break; 1080 1081 case SADB_X_SATYPE_IPIP: 1082 *sproto = IPPROTO_IPIP; 1083 1084 if (alg != NULL) 1085 *alg = XF_IP4; 1086 1087 break; 1088 1089 case SADB_X_SATYPE_IPCOMP: 1090 if (!atomic_load_int(&ipcomp_enable)) 1091 return (EOPNOTSUPP); 1092 1093 *sproto = IPPROTO_IPCOMP; 1094 1095 if(alg != NULL) 1096 *alg = satype = XF_IPCOMP; 1097 1098 break; 1099 #endif /* IPSEC */ 1100 #ifdef TCP_SIGNATURE 1101 case SADB_X_SATYPE_TCPSIGNATURE: 1102 *sproto = IPPROTO_TCP; 1103 1104 if (alg != NULL) 1105 *alg = XF_TCPSIGNATURE; 1106 1107 break; 1108 #endif /* TCP_SIGNATURE */ 1109 1110 default: /* Nothing else supported */ 1111 return (EOPNOTSUPP); 1112 } 1113 1114 return (0); 1115 } 1116 1117 /* 1118 * Handle all messages from userland to kernel. 1119 */ 1120 int 1121 pfkeyv2_dosend(struct socket *so, void *message, int len) 1122 { 1123 int i, j, rval = 0, mode = PFKEYV2_SENDMESSAGE_BROADCAST; 1124 int delflag = 0; 1125 struct sockaddr_encap encapdst, encapnetmask; 1126 struct ipsec_policy *ipo; 1127 struct ipsec_acquire *ipa; 1128 struct radix_node_head *rnh; 1129 struct radix_node *rn = NULL; 1130 struct pkpcb *kp, *bkp; 1131 void *freeme = NULL, *freeme2 = NULL, *freeme3 = NULL; 1132 int freeme_sz = 0, freeme2_sz = 0, freeme3_sz = 0; 1133 void *bckptr = NULL; 1134 void *headers[SADB_EXT_MAX + 1]; 1135 union sockaddr_union *sunionp; 1136 struct tdb *sa1 = NULL, *sa2 = NULL; 1137 struct sadb_msg *smsg; 1138 struct sadb_spirange *sprng; 1139 struct sadb_sa *ssa; 1140 struct sadb_supported *ssup; 1141 struct sadb_ident *sid, *did; 1142 struct srp_ref sr; 1143 struct sadb_x_rdomain *srdomain; 1144 u_int rdomain = 0; 1145 int promisc; 1146 1147 mtx_enter(&pfkeyv2_mtx); 1148 promisc = npromisc; 1149 mtx_leave(&pfkeyv2_mtx); 1150 1151 /* Verify that we received this over a legitimate pfkeyv2 socket */ 1152 bzero(headers, sizeof(headers)); 1153 1154 kp = sotokeycb(so); 1155 if (!kp) { 1156 rval = EINVAL; 1157 goto ret; 1158 } 1159 1160 rdomain = kp->kcb_rdomain; 1161 1162 /* Validate message format */ 1163 if ((rval = pfkeyv2_parsemessage(message, len, headers)) != 0) 1164 goto ret; 1165 1166 /* If we have any promiscuous listeners, send them a copy of the message */ 1167 if (promisc) { 1168 struct mbuf *packet; 1169 1170 freeme_sz = sizeof(struct sadb_msg) + len; 1171 if (!(freeme = malloc(freeme_sz, M_PFKEY, M_NOWAIT))) { 1172 rval = ENOMEM; 1173 goto ret; 1174 } 1175 1176 /* Initialize encapsulating header */ 1177 bzero(freeme, sizeof(struct sadb_msg)); 1178 smsg = (struct sadb_msg *) freeme; 1179 smsg->sadb_msg_version = PF_KEY_V2; 1180 smsg->sadb_msg_type = SADB_X_PROMISC; 1181 smsg->sadb_msg_len = (sizeof(struct sadb_msg) + len) / 1182 sizeof(uint64_t); 1183 smsg->sadb_msg_seq = curproc->p_p->ps_pid; 1184 1185 bcopy(message, freeme + sizeof(struct sadb_msg), len); 1186 1187 /* Convert to mbuf chain */ 1188 if ((rval = pfdatatopacket(freeme, freeme_sz, &packet)) != 0) 1189 goto ret; 1190 1191 /* Send to all promiscuous listeners */ 1192 SRPL_FOREACH(bkp, &sr, &pkptable.pkp_list, kcb_list) { 1193 if (bkp->kcb_rdomain != kp->kcb_rdomain) 1194 continue; 1195 1196 if (bkp->kcb_flags & PFKEYV2_SOCKETFLAGS_PROMISC) 1197 pfkey_sendup(bkp, packet, 1); 1198 } 1199 SRPL_LEAVE(&sr); 1200 1201 m_freem(packet); 1202 1203 /* Paranoid */ 1204 explicit_bzero(freeme, freeme_sz); 1205 free(freeme, M_PFKEY, freeme_sz); 1206 freeme = NULL; 1207 freeme_sz = 0; 1208 } 1209 1210 /* use specified rdomain */ 1211 srdomain = (struct sadb_x_rdomain *) headers[SADB_X_EXT_RDOMAIN]; 1212 if (srdomain) { 1213 if (!rtable_exists(srdomain->sadb_x_rdomain_dom1) || 1214 !rtable_exists(srdomain->sadb_x_rdomain_dom2)) { 1215 rval = EINVAL; 1216 goto ret; 1217 } 1218 rdomain = srdomain->sadb_x_rdomain_dom1; 1219 } 1220 1221 smsg = (struct sadb_msg *) headers[0]; 1222 switch (smsg->sadb_msg_type) { 1223 case SADB_GETSPI: /* Reserve an SPI */ 1224 sa1 = malloc(sizeof (*sa1), M_PFKEY, M_NOWAIT | M_ZERO); 1225 if (sa1 == NULL) { 1226 rval = ENOMEM; 1227 goto ret; 1228 } 1229 1230 sa1->tdb_satype = smsg->sadb_msg_satype; 1231 if ((rval = pfkeyv2_get_proto_alg(sa1->tdb_satype, 1232 &sa1->tdb_sproto, 0))) 1233 goto ret; 1234 1235 import_address(&sa1->tdb_src.sa, headers[SADB_EXT_ADDRESS_SRC]); 1236 import_address(&sa1->tdb_dst.sa, headers[SADB_EXT_ADDRESS_DST]); 1237 1238 /* Find an unused SA identifier */ 1239 sprng = (struct sadb_spirange *) headers[SADB_EXT_SPIRANGE]; 1240 NET_LOCK(); 1241 sa1->tdb_spi = reserve_spi(rdomain, 1242 sprng->sadb_spirange_min, sprng->sadb_spirange_max, 1243 &sa1->tdb_src, &sa1->tdb_dst, sa1->tdb_sproto, &rval); 1244 if (sa1->tdb_spi == 0) { 1245 NET_UNLOCK(); 1246 goto ret; 1247 } 1248 1249 /* Send a message back telling what the SA (the SPI really) is */ 1250 freeme_sz = sizeof(struct sadb_sa); 1251 if (!(freeme = malloc(freeme_sz, M_PFKEY, M_NOWAIT | M_ZERO))) { 1252 rval = ENOMEM; 1253 NET_UNLOCK(); 1254 goto ret; 1255 } 1256 1257 headers[SADB_EXT_SPIRANGE] = NULL; 1258 headers[SADB_EXT_SA] = freeme; 1259 bckptr = freeme; 1260 1261 /* We really only care about the SPI, but we'll export the SA */ 1262 export_sa((void **) &bckptr, sa1); 1263 NET_UNLOCK(); 1264 break; 1265 1266 case SADB_UPDATE: 1267 ssa = (struct sadb_sa *) headers[SADB_EXT_SA]; 1268 sunionp = (union sockaddr_union *) (headers[SADB_EXT_ADDRESS_DST] + 1269 sizeof(struct sadb_address)); 1270 1271 /* Either all or none of the flow must be included */ 1272 if ((headers[SADB_X_EXT_SRC_FLOW] || 1273 headers[SADB_X_EXT_PROTOCOL] || 1274 headers[SADB_X_EXT_FLOW_TYPE] || 1275 headers[SADB_X_EXT_DST_FLOW] || 1276 headers[SADB_X_EXT_SRC_MASK] || 1277 headers[SADB_X_EXT_DST_MASK]) && 1278 !(headers[SADB_X_EXT_SRC_FLOW] && 1279 headers[SADB_X_EXT_PROTOCOL] && 1280 headers[SADB_X_EXT_FLOW_TYPE] && 1281 headers[SADB_X_EXT_DST_FLOW] && 1282 headers[SADB_X_EXT_SRC_MASK] && 1283 headers[SADB_X_EXT_DST_MASK])) { 1284 rval = EINVAL; 1285 goto ret; 1286 } 1287 #ifdef IPSEC 1288 /* UDP encap has to be enabled and is only supported for ESP */ 1289 if (headers[SADB_X_EXT_UDPENCAP] && 1290 (!udpencap_enable || 1291 smsg->sadb_msg_satype != SADB_SATYPE_ESP)) { 1292 rval = EINVAL; 1293 goto ret; 1294 } 1295 #endif /* IPSEC */ 1296 1297 /* Find TDB */ 1298 NET_LOCK(); 1299 sa2 = gettdb(rdomain, ssa->sadb_sa_spi, sunionp, 1300 SADB_X_GETSPROTO(smsg->sadb_msg_satype)); 1301 1302 /* If there's no such SA, we're done */ 1303 if (sa2 == NULL) { 1304 rval = ESRCH; 1305 NET_UNLOCK(); 1306 goto ret; 1307 } 1308 1309 /* If this is a reserved SA */ 1310 if (sa2->tdb_flags & TDBF_INVALID) { 1311 struct tdb *newsa; 1312 struct ipsecinit ii; 1313 int alg; 1314 1315 /* Create new TDB */ 1316 newsa = tdb_alloc(rdomain); 1317 newsa->tdb_satype = smsg->sadb_msg_satype; 1318 1319 if ((rval = pfkeyv2_get_proto_alg(newsa->tdb_satype, 1320 &newsa->tdb_sproto, &alg))) { 1321 tdb_unref(newsa); 1322 NET_UNLOCK(); 1323 goto ret; 1324 } 1325 1326 /* Initialize SA */ 1327 bzero(&ii, sizeof(struct ipsecinit)); 1328 import_sa(newsa, headers[SADB_EXT_SA], &ii); 1329 import_address(&newsa->tdb_src.sa, 1330 headers[SADB_EXT_ADDRESS_SRC]); 1331 import_address(&newsa->tdb_dst.sa, 1332 headers[SADB_EXT_ADDRESS_DST]); 1333 import_lifetime(newsa, 1334 headers[SADB_EXT_LIFETIME_CURRENT], 1335 PFKEYV2_LIFETIME_CURRENT); 1336 import_lifetime(newsa, headers[SADB_EXT_LIFETIME_SOFT], 1337 PFKEYV2_LIFETIME_SOFT); 1338 import_lifetime(newsa, headers[SADB_EXT_LIFETIME_HARD], 1339 PFKEYV2_LIFETIME_HARD); 1340 import_key(&ii, headers[SADB_EXT_KEY_AUTH], 1341 PFKEYV2_AUTHENTICATION_KEY); 1342 import_key(&ii, headers[SADB_EXT_KEY_ENCRYPT], 1343 PFKEYV2_ENCRYPTION_KEY); 1344 newsa->tdb_ids_swapped = 1; /* only on TDB_UPDATE */ 1345 import_identities(&newsa->tdb_ids, 1346 newsa->tdb_ids_swapped, 1347 headers[SADB_EXT_IDENTITY_SRC], 1348 headers[SADB_EXT_IDENTITY_DST]); 1349 if ((rval = import_flow(&newsa->tdb_filter, 1350 &newsa->tdb_filtermask, 1351 headers[SADB_X_EXT_SRC_FLOW], 1352 headers[SADB_X_EXT_SRC_MASK], 1353 headers[SADB_X_EXT_DST_FLOW], 1354 headers[SADB_X_EXT_DST_MASK], 1355 headers[SADB_X_EXT_PROTOCOL], 1356 headers[SADB_X_EXT_FLOW_TYPE]))) { 1357 tdb_unref(newsa); 1358 NET_UNLOCK(); 1359 goto ret; 1360 } 1361 import_udpencap(newsa, headers[SADB_X_EXT_UDPENCAP]); 1362 import_rdomain(newsa, headers[SADB_X_EXT_RDOMAIN]); 1363 #if NPF > 0 1364 import_tag(newsa, headers[SADB_X_EXT_TAG]); 1365 import_tap(newsa, headers[SADB_X_EXT_TAP]); 1366 #endif 1367 import_iface(newsa, headers[SADB_X_EXT_IFACE]); 1368 1369 /* Exclude sensitive data from reply message. */ 1370 headers[SADB_EXT_KEY_AUTH] = NULL; 1371 headers[SADB_EXT_KEY_ENCRYPT] = NULL; 1372 headers[SADB_X_EXT_LOCAL_AUTH] = NULL; 1373 headers[SADB_X_EXT_REMOTE_AUTH] = NULL; 1374 1375 newsa->tdb_seq = smsg->sadb_msg_seq; 1376 1377 rval = tdb_init(newsa, alg, &ii); 1378 if (rval) { 1379 rval = EINVAL; 1380 tdb_unref(newsa); 1381 NET_UNLOCK(); 1382 goto ret; 1383 } 1384 1385 newsa->tdb_cur_allocations = sa2->tdb_cur_allocations; 1386 1387 /* Delete old version of the SA, insert new one */ 1388 tdb_delete(sa2); 1389 1390 tdb_addtimeouts(newsa); 1391 1392 puttdb(newsa); 1393 } else { 1394 /* 1395 * The SA is already initialized, so we're only allowed to 1396 * change lifetimes and some other information; we're 1397 * not allowed to change keys, addresses or identities. 1398 */ 1399 if (headers[SADB_EXT_KEY_AUTH] || 1400 headers[SADB_EXT_KEY_ENCRYPT] || 1401 headers[SADB_EXT_IDENTITY_SRC] || 1402 headers[SADB_EXT_IDENTITY_DST] || 1403 headers[SADB_EXT_SENSITIVITY]) { 1404 rval = EINVAL; 1405 NET_UNLOCK(); 1406 goto ret; 1407 } 1408 1409 import_sa(sa2, headers[SADB_EXT_SA], NULL); 1410 import_lifetime(sa2, 1411 headers[SADB_EXT_LIFETIME_CURRENT], 1412 PFKEYV2_LIFETIME_CURRENT); 1413 import_lifetime(sa2, headers[SADB_EXT_LIFETIME_SOFT], 1414 PFKEYV2_LIFETIME_SOFT); 1415 import_lifetime(sa2, headers[SADB_EXT_LIFETIME_HARD], 1416 PFKEYV2_LIFETIME_HARD); 1417 import_udpencap(sa2, headers[SADB_X_EXT_UDPENCAP]); 1418 #if NPF > 0 1419 import_tag(sa2, headers[SADB_X_EXT_TAG]); 1420 import_tap(sa2, headers[SADB_X_EXT_TAP]); 1421 #endif 1422 import_iface(sa2, headers[SADB_X_EXT_IFACE]); 1423 1424 tdb_addtimeouts(sa2); 1425 1426 if (headers[SADB_EXT_ADDRESS_SRC] || 1427 headers[SADB_EXT_ADDRESS_PROXY]) { 1428 mtx_enter(&tdb_sadb_mtx); 1429 tdb_unlink_locked(sa2); 1430 import_address((struct sockaddr *)&sa2->tdb_src, 1431 headers[SADB_EXT_ADDRESS_SRC]); 1432 import_address((struct sockaddr *)&sa2->tdb_dst, 1433 headers[SADB_EXT_ADDRESS_PROXY]); 1434 puttdb_locked(sa2); 1435 mtx_leave(&tdb_sadb_mtx); 1436 } 1437 } 1438 NET_UNLOCK(); 1439 1440 break; 1441 case SADB_ADD: 1442 ssa = (struct sadb_sa *) headers[SADB_EXT_SA]; 1443 sunionp = (union sockaddr_union *) (headers[SADB_EXT_ADDRESS_DST] + 1444 sizeof(struct sadb_address)); 1445 1446 /* Either all or none of the flow must be included */ 1447 if ((headers[SADB_X_EXT_SRC_FLOW] || 1448 headers[SADB_X_EXT_PROTOCOL] || 1449 headers[SADB_X_EXT_FLOW_TYPE] || 1450 headers[SADB_X_EXT_DST_FLOW] || 1451 headers[SADB_X_EXT_SRC_MASK] || 1452 headers[SADB_X_EXT_DST_MASK]) && 1453 !(headers[SADB_X_EXT_SRC_FLOW] && 1454 headers[SADB_X_EXT_PROTOCOL] && 1455 headers[SADB_X_EXT_FLOW_TYPE] && 1456 headers[SADB_X_EXT_DST_FLOW] && 1457 headers[SADB_X_EXT_SRC_MASK] && 1458 headers[SADB_X_EXT_DST_MASK])) { 1459 rval = EINVAL; 1460 goto ret; 1461 } 1462 #ifdef IPSEC 1463 /* UDP encap has to be enabled and is only supported for ESP */ 1464 if (headers[SADB_X_EXT_UDPENCAP] && 1465 (!udpencap_enable || 1466 smsg->sadb_msg_satype != SADB_SATYPE_ESP)) { 1467 rval = EINVAL; 1468 goto ret; 1469 } 1470 #endif /* IPSEC */ 1471 1472 NET_LOCK(); 1473 sa2 = gettdb(rdomain, ssa->sadb_sa_spi, sunionp, 1474 SADB_X_GETSPROTO(smsg->sadb_msg_satype)); 1475 1476 /* We can't add an existing SA! */ 1477 if (sa2 != NULL) { 1478 rval = EEXIST; 1479 NET_UNLOCK(); 1480 goto ret; 1481 } 1482 1483 /* We can only add "mature" SAs */ 1484 if (ssa->sadb_sa_state != SADB_SASTATE_MATURE) { 1485 rval = EINVAL; 1486 NET_UNLOCK(); 1487 goto ret; 1488 } 1489 1490 { 1491 struct tdb *newsa; 1492 struct ipsecinit ii; 1493 int alg; 1494 1495 /* Create new TDB */ 1496 newsa = tdb_alloc(rdomain); 1497 newsa->tdb_satype = smsg->sadb_msg_satype; 1498 1499 if ((rval = pfkeyv2_get_proto_alg(newsa->tdb_satype, 1500 &newsa->tdb_sproto, &alg))) { 1501 tdb_unref(newsa); 1502 NET_UNLOCK(); 1503 goto ret; 1504 } 1505 1506 /* Initialize SA */ 1507 bzero(&ii, sizeof(struct ipsecinit)); 1508 import_sa(newsa, headers[SADB_EXT_SA], &ii); 1509 import_address(&newsa->tdb_src.sa, 1510 headers[SADB_EXT_ADDRESS_SRC]); 1511 import_address(&newsa->tdb_dst.sa, 1512 headers[SADB_EXT_ADDRESS_DST]); 1513 1514 import_lifetime(newsa, 1515 headers[SADB_EXT_LIFETIME_CURRENT], 1516 PFKEYV2_LIFETIME_CURRENT); 1517 import_lifetime(newsa, headers[SADB_EXT_LIFETIME_SOFT], 1518 PFKEYV2_LIFETIME_SOFT); 1519 import_lifetime(newsa, headers[SADB_EXT_LIFETIME_HARD], 1520 PFKEYV2_LIFETIME_HARD); 1521 1522 import_key(&ii, headers[SADB_EXT_KEY_AUTH], 1523 PFKEYV2_AUTHENTICATION_KEY); 1524 import_key(&ii, headers[SADB_EXT_KEY_ENCRYPT], 1525 PFKEYV2_ENCRYPTION_KEY); 1526 1527 import_identities(&newsa->tdb_ids, 1528 newsa->tdb_ids_swapped, 1529 headers[SADB_EXT_IDENTITY_SRC], 1530 headers[SADB_EXT_IDENTITY_DST]); 1531 1532 if ((rval = import_flow(&newsa->tdb_filter, 1533 &newsa->tdb_filtermask, 1534 headers[SADB_X_EXT_SRC_FLOW], 1535 headers[SADB_X_EXT_SRC_MASK], 1536 headers[SADB_X_EXT_DST_FLOW], 1537 headers[SADB_X_EXT_DST_MASK], 1538 headers[SADB_X_EXT_PROTOCOL], 1539 headers[SADB_X_EXT_FLOW_TYPE]))) { 1540 tdb_unref(newsa); 1541 NET_UNLOCK(); 1542 goto ret; 1543 } 1544 import_udpencap(newsa, headers[SADB_X_EXT_UDPENCAP]); 1545 import_rdomain(newsa, headers[SADB_X_EXT_RDOMAIN]); 1546 #if NPF > 0 1547 import_tag(newsa, headers[SADB_X_EXT_TAG]); 1548 import_tap(newsa, headers[SADB_X_EXT_TAP]); 1549 #endif 1550 import_iface(newsa, headers[SADB_X_EXT_IFACE]); 1551 1552 /* Exclude sensitive data from reply message. */ 1553 headers[SADB_EXT_KEY_AUTH] = NULL; 1554 headers[SADB_EXT_KEY_ENCRYPT] = NULL; 1555 headers[SADB_X_EXT_LOCAL_AUTH] = NULL; 1556 headers[SADB_X_EXT_REMOTE_AUTH] = NULL; 1557 1558 newsa->tdb_seq = smsg->sadb_msg_seq; 1559 1560 rval = tdb_init(newsa, alg, &ii); 1561 if (rval) { 1562 rval = EINVAL; 1563 tdb_unref(newsa); 1564 NET_UNLOCK(); 1565 goto ret; 1566 } 1567 1568 tdb_addtimeouts(newsa); 1569 1570 /* Add TDB in table */ 1571 puttdb(newsa); 1572 } 1573 NET_UNLOCK(); 1574 1575 break; 1576 1577 case SADB_DELETE: 1578 ssa = (struct sadb_sa *) headers[SADB_EXT_SA]; 1579 sunionp = 1580 (union sockaddr_union *)(headers[SADB_EXT_ADDRESS_DST] + 1581 sizeof(struct sadb_address)); 1582 1583 NET_LOCK(); 1584 sa2 = gettdb(rdomain, ssa->sadb_sa_spi, sunionp, 1585 SADB_X_GETSPROTO(smsg->sadb_msg_satype)); 1586 if (sa2 == NULL) { 1587 rval = ESRCH; 1588 NET_UNLOCK(); 1589 goto ret; 1590 } 1591 1592 tdb_delete(sa2); 1593 NET_UNLOCK(); 1594 1595 break; 1596 1597 case SADB_X_ASKPOLICY: 1598 /* Get the relevant policy */ 1599 NET_LOCK(); 1600 ipa = ipsec_get_acquire(((struct sadb_x_policy *) 1601 headers[SADB_X_EXT_POLICY])->sadb_x_policy_seq); 1602 if (ipa == NULL) { 1603 rval = ESRCH; 1604 NET_UNLOCK(); 1605 goto ret; 1606 } 1607 1608 rval = pfkeyv2_policy(ipa, headers, &freeme, &freeme_sz); 1609 NET_UNLOCK(); 1610 ipsec_unref_acquire(ipa); 1611 if (rval) 1612 mode = PFKEYV2_SENDMESSAGE_UNICAST; 1613 1614 break; 1615 1616 case SADB_GET: 1617 ssa = (struct sadb_sa *) headers[SADB_EXT_SA]; 1618 sunionp = 1619 (union sockaddr_union *)(headers[SADB_EXT_ADDRESS_DST] + 1620 sizeof(struct sadb_address)); 1621 1622 NET_LOCK(); 1623 sa2 = gettdb(rdomain, ssa->sadb_sa_spi, sunionp, 1624 SADB_X_GETSPROTO(smsg->sadb_msg_satype)); 1625 if (sa2 == NULL) { 1626 rval = ESRCH; 1627 NET_UNLOCK(); 1628 goto ret; 1629 } 1630 1631 rval = pfkeyv2_get(sa2, headers, &freeme, &freeme_sz, NULL); 1632 NET_UNLOCK(); 1633 if (rval) 1634 mode = PFKEYV2_SENDMESSAGE_UNICAST; 1635 1636 break; 1637 1638 case SADB_REGISTER: 1639 keylock(kp); 1640 if (!(kp->kcb_flags & PFKEYV2_SOCKETFLAGS_REGISTERED)) { 1641 kp->kcb_flags |= PFKEYV2_SOCKETFLAGS_REGISTERED; 1642 mtx_enter(&pfkeyv2_mtx); 1643 nregistered++; 1644 mtx_leave(&pfkeyv2_mtx); 1645 } 1646 keyunlock(kp); 1647 1648 freeme_sz = sizeof(struct sadb_supported) + sizeof(ealgs); 1649 if (!(freeme = malloc(freeme_sz, M_PFKEY, M_NOWAIT | M_ZERO))) { 1650 rval = ENOMEM; 1651 goto ret; 1652 } 1653 1654 ssup = (struct sadb_supported *) freeme; 1655 ssup->sadb_supported_len = freeme_sz / sizeof(uint64_t); 1656 1657 { 1658 void *p = freeme + sizeof(struct sadb_supported); 1659 1660 bcopy(&ealgs[0], p, sizeof(ealgs)); 1661 } 1662 1663 headers[SADB_EXT_SUPPORTED_ENCRYPT] = freeme; 1664 1665 freeme2_sz = sizeof(struct sadb_supported) + sizeof(aalgs); 1666 if (!(freeme2 = malloc(freeme2_sz, M_PFKEY, 1667 M_NOWAIT | M_ZERO))) { 1668 rval = ENOMEM; 1669 goto ret; 1670 } 1671 1672 /* Keep track what this socket has registered for */ 1673 keylock(kp); 1674 kp->kcb_reg |= 1675 (1 << ((struct sadb_msg *)message)->sadb_msg_satype); 1676 keyunlock(kp); 1677 1678 ssup = (struct sadb_supported *) freeme2; 1679 ssup->sadb_supported_len = freeme2_sz / sizeof(uint64_t); 1680 1681 { 1682 void *p = freeme2 + sizeof(struct sadb_supported); 1683 1684 bcopy(&aalgs[0], p, sizeof(aalgs)); 1685 } 1686 1687 headers[SADB_EXT_SUPPORTED_AUTH] = freeme2; 1688 1689 freeme3_sz = sizeof(struct sadb_supported) + sizeof(calgs); 1690 if (!(freeme3 = malloc(freeme3_sz, M_PFKEY, 1691 M_NOWAIT | M_ZERO))) { 1692 rval = ENOMEM; 1693 goto ret; 1694 } 1695 1696 ssup = (struct sadb_supported *) freeme3; 1697 ssup->sadb_supported_len = freeme3_sz / sizeof(uint64_t); 1698 1699 { 1700 void *p = freeme3 + sizeof(struct sadb_supported); 1701 1702 bcopy(&calgs[0], p, sizeof(calgs)); 1703 } 1704 1705 headers[SADB_X_EXT_SUPPORTED_COMP] = freeme3; 1706 1707 break; 1708 1709 case SADB_ACQUIRE: 1710 case SADB_EXPIRE: 1711 /* Nothing to handle */ 1712 rval = 0; 1713 break; 1714 1715 case SADB_FLUSH: 1716 rval = 0; 1717 1718 NET_LOCK(); 1719 switch (smsg->sadb_msg_satype) { 1720 case SADB_SATYPE_UNSPEC: 1721 spd_table_walk(rdomain, pfkeyv2_policy_flush, NULL); 1722 /* FALLTHROUGH */ 1723 case SADB_SATYPE_AH: 1724 case SADB_SATYPE_ESP: 1725 case SADB_X_SATYPE_IPIP: 1726 case SADB_X_SATYPE_IPCOMP: 1727 #ifdef TCP_SIGNATURE 1728 case SADB_X_SATYPE_TCPSIGNATURE: 1729 #endif /* TCP_SIGNATURE */ 1730 tdb_walk(rdomain, pfkeyv2_sa_flush, 1731 (u_int8_t *) &(smsg->sadb_msg_satype)); 1732 1733 break; 1734 1735 default: 1736 rval = EINVAL; /* Unknown/unsupported type */ 1737 } 1738 NET_UNLOCK(); 1739 1740 break; 1741 1742 case SADB_DUMP: 1743 { 1744 struct dump_state dump_state; 1745 dump_state.sadb_msg = (struct sadb_msg *) headers[0]; 1746 dump_state.socket = so; 1747 1748 NET_LOCK(); 1749 rval = tdb_walk(rdomain, pfkeyv2_dump_walker, &dump_state); 1750 NET_UNLOCK(); 1751 if (!rval) 1752 goto realret; 1753 if ((rval == ENOMEM) || (rval == ENOBUFS)) 1754 rval = 0; 1755 } 1756 break; 1757 1758 case SADB_X_GRPSPIS: 1759 { 1760 struct tdb *tdb1, *tdb2, *tdb3; 1761 struct sadb_protocol *sa_proto; 1762 1763 ssa = (struct sadb_sa *) headers[SADB_EXT_SA]; 1764 sunionp = (union sockaddr_union *) (headers[SADB_EXT_ADDRESS_DST] + 1765 sizeof(struct sadb_address)); 1766 1767 NET_LOCK(); 1768 tdb1 = gettdb(rdomain, ssa->sadb_sa_spi, sunionp, 1769 SADB_X_GETSPROTO(smsg->sadb_msg_satype)); 1770 if (tdb1 == NULL) { 1771 rval = ESRCH; 1772 NET_UNLOCK(); 1773 goto ret; 1774 } 1775 1776 ssa = (struct sadb_sa *) headers[SADB_X_EXT_SA2]; 1777 sunionp = (union sockaddr_union *) (headers[SADB_X_EXT_DST2] + 1778 sizeof(struct sadb_address)); 1779 sa_proto = (struct sadb_protocol *) headers[SADB_X_EXT_SATYPE2]; 1780 1781 /* optionally fetch tdb2 from rdomain2 */ 1782 tdb2 = gettdb(srdomain ? srdomain->sadb_x_rdomain_dom2 : rdomain, 1783 ssa->sadb_sa_spi, sunionp, 1784 SADB_X_GETSPROTO(sa_proto->sadb_protocol_proto)); 1785 if (tdb2 == NULL) { 1786 tdb_unref(tdb1); 1787 rval = ESRCH; 1788 NET_UNLOCK(); 1789 goto ret; 1790 } 1791 1792 /* Detect cycles */ 1793 for (tdb3 = tdb2; tdb3; tdb3 = tdb3->tdb_onext) 1794 if (tdb3 == tdb1) { 1795 tdb_unref(tdb1); 1796 tdb_unref(tdb2); 1797 rval = ESRCH; 1798 NET_UNLOCK(); 1799 goto ret; 1800 } 1801 1802 /* Maintenance */ 1803 if ((tdb1->tdb_onext) && 1804 (tdb1->tdb_onext->tdb_inext == tdb1)) { 1805 tdb_unref(tdb1->tdb_onext->tdb_inext); 1806 tdb1->tdb_onext->tdb_inext = NULL; 1807 } 1808 1809 if ((tdb2->tdb_inext) && 1810 (tdb2->tdb_inext->tdb_onext == tdb2)) { 1811 tdb_unref(tdb2->tdb_inext->tdb_onext); 1812 tdb2->tdb_inext->tdb_onext = NULL; 1813 } 1814 1815 /* Link them */ 1816 tdb1->tdb_onext = tdb2; 1817 tdb2->tdb_inext = tdb1; 1818 NET_UNLOCK(); 1819 } 1820 break; 1821 1822 case SADB_X_DELFLOW: 1823 delflag = 1; 1824 /*FALLTHROUGH*/ 1825 case SADB_X_ADDFLOW: 1826 { 1827 struct sadb_protocol *sab; 1828 union sockaddr_union *ssrc; 1829 int exists = 0; 1830 1831 NET_LOCK(); 1832 if ((rnh = spd_table_add(rdomain)) == NULL) { 1833 rval = ENOMEM; 1834 NET_UNLOCK(); 1835 goto ret; 1836 } 1837 1838 sab = (struct sadb_protocol *) headers[SADB_X_EXT_FLOW_TYPE]; 1839 1840 if ((sab->sadb_protocol_direction != IPSP_DIRECTION_IN) && 1841 (sab->sadb_protocol_direction != IPSP_DIRECTION_OUT)) { 1842 rval = EINVAL; 1843 NET_UNLOCK(); 1844 goto ret; 1845 } 1846 1847 /* If the security protocol wasn't specified, pretend it was ESP */ 1848 if (smsg->sadb_msg_satype == 0) 1849 smsg->sadb_msg_satype = SADB_SATYPE_ESP; 1850 1851 if (headers[SADB_EXT_ADDRESS_DST]) 1852 sunionp = (union sockaddr_union *) 1853 (headers[SADB_EXT_ADDRESS_DST] + 1854 sizeof(struct sadb_address)); 1855 else 1856 sunionp = NULL; 1857 1858 if (headers[SADB_EXT_ADDRESS_SRC]) 1859 ssrc = (union sockaddr_union *) 1860 (headers[SADB_EXT_ADDRESS_SRC] + 1861 sizeof(struct sadb_address)); 1862 else 1863 ssrc = NULL; 1864 1865 if ((rval = import_flow(&encapdst, &encapnetmask, 1866 headers[SADB_X_EXT_SRC_FLOW], headers[SADB_X_EXT_SRC_MASK], 1867 headers[SADB_X_EXT_DST_FLOW], headers[SADB_X_EXT_DST_MASK], 1868 headers[SADB_X_EXT_PROTOCOL], 1869 headers[SADB_X_EXT_FLOW_TYPE]))) { 1870 NET_UNLOCK(); 1871 goto ret; 1872 } 1873 1874 /* Determine whether the exact same SPD entry already exists. */ 1875 if ((rn = rn_match(&encapdst, rnh)) != NULL) { 1876 ipo = (struct ipsec_policy *)rn; 1877 1878 /* Verify that the entry is identical */ 1879 if (bcmp(&ipo->ipo_addr, &encapdst, 1880 sizeof(struct sockaddr_encap)) || 1881 bcmp(&ipo->ipo_mask, &encapnetmask, 1882 sizeof(struct sockaddr_encap))) 1883 ipo = NULL; /* Fall through */ 1884 else 1885 exists = 1; 1886 } else 1887 ipo = NULL; 1888 1889 /* 1890 * If the existing policy is static, only delete or update 1891 * it if the new one is also static. 1892 */ 1893 if (exists && (ipo->ipo_flags & IPSP_POLICY_STATIC)) { 1894 if (!(sab->sadb_protocol_flags & 1895 SADB_X_POLICYFLAGS_POLICY)) { 1896 NET_UNLOCK(); 1897 goto ret; 1898 } 1899 } 1900 1901 /* Delete ? */ 1902 if (delflag) { 1903 if (exists) { 1904 rval = ipsec_delete_policy(ipo); 1905 NET_UNLOCK(); 1906 goto ret; 1907 } 1908 1909 /* If we were asked to delete something non-existent, error. */ 1910 rval = ESRCH; 1911 NET_UNLOCK(); 1912 break; 1913 } 1914 1915 if (!exists) { 1916 /* Allocate policy entry */ 1917 ipo = pool_get(&ipsec_policy_pool, PR_NOWAIT|PR_ZERO); 1918 if (ipo == NULL) { 1919 rval = ENOMEM; 1920 NET_UNLOCK(); 1921 goto ret; 1922 } 1923 } 1924 1925 switch (sab->sadb_protocol_proto) { 1926 case SADB_X_FLOW_TYPE_USE: 1927 ipo->ipo_type = IPSP_IPSEC_USE; 1928 break; 1929 1930 case SADB_X_FLOW_TYPE_ACQUIRE: 1931 ipo->ipo_type = IPSP_IPSEC_ACQUIRE; 1932 break; 1933 1934 case SADB_X_FLOW_TYPE_REQUIRE: 1935 ipo->ipo_type = IPSP_IPSEC_REQUIRE; 1936 break; 1937 1938 case SADB_X_FLOW_TYPE_DENY: 1939 ipo->ipo_type = IPSP_DENY; 1940 break; 1941 1942 case SADB_X_FLOW_TYPE_BYPASS: 1943 ipo->ipo_type = IPSP_PERMIT; 1944 break; 1945 1946 case SADB_X_FLOW_TYPE_DONTACQ: 1947 ipo->ipo_type = IPSP_IPSEC_DONTACQ; 1948 break; 1949 1950 default: 1951 if (!exists) 1952 pool_put(&ipsec_policy_pool, ipo); 1953 else 1954 ipsec_delete_policy(ipo); 1955 1956 rval = EINVAL; 1957 NET_UNLOCK(); 1958 goto ret; 1959 } 1960 1961 if (sab->sadb_protocol_flags & SADB_X_POLICYFLAGS_POLICY) 1962 ipo->ipo_flags |= IPSP_POLICY_STATIC; 1963 1964 if (sunionp) 1965 bcopy(sunionp, &ipo->ipo_dst, 1966 sizeof(union sockaddr_union)); 1967 else 1968 bzero(&ipo->ipo_dst, sizeof(union sockaddr_union)); 1969 1970 if (ssrc) 1971 bcopy(ssrc, &ipo->ipo_src, 1972 sizeof(union sockaddr_union)); 1973 else 1974 bzero(&ipo->ipo_src, sizeof(union sockaddr_union)); 1975 1976 ipo->ipo_sproto = SADB_X_GETSPROTO(smsg->sadb_msg_satype); 1977 1978 if (ipo->ipo_ids) { 1979 ipsp_ids_free(ipo->ipo_ids); 1980 ipo->ipo_ids = NULL; 1981 } 1982 1983 if ((sid = headers[SADB_EXT_IDENTITY_SRC]) != NULL && 1984 (did = headers[SADB_EXT_IDENTITY_DST]) != NULL) { 1985 import_identities(&ipo->ipo_ids, 0, sid, did); 1986 if (ipo->ipo_ids == NULL) { 1987 if (exists) 1988 ipsec_delete_policy(ipo); 1989 else 1990 pool_put(&ipsec_policy_pool, ipo); 1991 rval = ENOBUFS; 1992 NET_UNLOCK(); 1993 goto ret; 1994 } 1995 } 1996 1997 /* Flow type */ 1998 if (!exists) { 1999 /* Initialize policy entry */ 2000 bcopy(&encapdst, &ipo->ipo_addr, 2001 sizeof(struct sockaddr_encap)); 2002 bcopy(&encapnetmask, &ipo->ipo_mask, 2003 sizeof(struct sockaddr_encap)); 2004 2005 TAILQ_INIT(&ipo->ipo_acquires); 2006 ipo->ipo_rdomain = rdomain; 2007 refcnt_init(&ipo->ipo_refcnt); 2008 2009 /* Add SPD entry */ 2010 if ((rnh = spd_table_get(rdomain)) == NULL || 2011 (rn = rn_addroute((caddr_t)&ipo->ipo_addr, 2012 (caddr_t)&ipo->ipo_mask, rnh, 2013 ipo->ipo_nodes, 0)) == NULL) { 2014 /* Remove from linked list of policies on TDB */ 2015 mtx_enter(&ipo_tdb_mtx); 2016 if (ipo->ipo_tdb != NULL) { 2017 TAILQ_REMOVE( 2018 &ipo->ipo_tdb->tdb_policy_head, 2019 ipo, ipo_tdb_next); 2020 tdb_unref(ipo->ipo_tdb); 2021 ipo->ipo_tdb = NULL; 2022 } 2023 mtx_leave(&ipo_tdb_mtx); 2024 if (ipo->ipo_ids) 2025 ipsp_ids_free(ipo->ipo_ids); 2026 pool_put(&ipsec_policy_pool, ipo); 2027 NET_UNLOCK(); 2028 goto ret; 2029 } 2030 TAILQ_INSERT_HEAD(&ipsec_policy_head, ipo, ipo_list); 2031 ipsec_in_use++; 2032 } else { 2033 ipo->ipo_last_searched = ipo->ipo_flags = 0; 2034 } 2035 NET_UNLOCK(); 2036 } 2037 break; 2038 2039 case SADB_X_PROMISC: 2040 if (len >= 2 * sizeof(struct sadb_msg)) { 2041 struct mbuf *packet; 2042 2043 if ((rval = pfdatatopacket(message, len, &packet)) != 0) 2044 goto ret; 2045 2046 SRPL_FOREACH(bkp, &sr, &pkptable.pkp_list, kcb_list) { 2047 if (bkp == kp || 2048 bkp->kcb_rdomain != kp->kcb_rdomain) 2049 continue; 2050 2051 if (!smsg->sadb_msg_seq || 2052 (smsg->sadb_msg_seq == kp->kcb_pid)) { 2053 pfkey_sendup(bkp, packet, 1); 2054 } 2055 } 2056 SRPL_LEAVE(&sr); 2057 2058 m_freem(packet); 2059 } else { 2060 if (len != sizeof(struct sadb_msg)) { 2061 rval = EINVAL; 2062 goto ret; 2063 } 2064 2065 keylock(kp); 2066 i = (kp->kcb_flags & 2067 PFKEYV2_SOCKETFLAGS_PROMISC) ? 1 : 0; 2068 j = smsg->sadb_msg_satype ? 1 : 0; 2069 2070 if (i ^ j) { 2071 if (j) { 2072 kp->kcb_flags |= 2073 PFKEYV2_SOCKETFLAGS_PROMISC; 2074 mtx_enter(&pfkeyv2_mtx); 2075 npromisc++; 2076 mtx_leave(&pfkeyv2_mtx); 2077 } else { 2078 kp->kcb_flags &= 2079 ~PFKEYV2_SOCKETFLAGS_PROMISC; 2080 mtx_enter(&pfkeyv2_mtx); 2081 npromisc--; 2082 mtx_leave(&pfkeyv2_mtx); 2083 } 2084 } 2085 keyunlock(kp); 2086 } 2087 2088 break; 2089 2090 default: 2091 rval = EINVAL; 2092 goto ret; 2093 } 2094 2095 ret: 2096 if (rval) { 2097 if ((rval == EINVAL) || (rval == ENOMEM) || (rval == ENOBUFS)) 2098 goto realret; 2099 2100 for (i = 1; i <= SADB_EXT_MAX; i++) 2101 headers[i] = NULL; 2102 2103 smsg->sadb_msg_errno = abs(rval); 2104 } else { 2105 uint64_t seen = 0LL; 2106 2107 for (i = 1; i <= SADB_EXT_MAX; i++) 2108 if (headers[i]) 2109 seen |= (1LL << i); 2110 2111 if ((seen & sadb_exts_allowed_out[smsg->sadb_msg_type]) 2112 != seen) { 2113 rval = EPERM; 2114 goto realret; 2115 } 2116 2117 if ((seen & sadb_exts_required_out[smsg->sadb_msg_type]) != 2118 sadb_exts_required_out[smsg->sadb_msg_type]) { 2119 rval = EPERM; 2120 goto realret; 2121 } 2122 } 2123 2124 rval = pfkeyv2_sendmessage(headers, mode, so, 0, 0, kp->kcb_rdomain); 2125 2126 realret: 2127 2128 if (freeme != NULL) 2129 explicit_bzero(freeme, freeme_sz); 2130 free(freeme, M_PFKEY, freeme_sz); 2131 free(freeme2, M_PFKEY, freeme2_sz); 2132 free(freeme3, M_PFKEY, freeme3_sz); 2133 2134 explicit_bzero(message, len); 2135 free(message, M_PFKEY, len); 2136 2137 free(sa1, M_PFKEY, sizeof(*sa1)); 2138 2139 NET_LOCK(); 2140 tdb_unref(sa2); 2141 NET_UNLOCK(); 2142 2143 return (rval); 2144 } 2145 2146 /* 2147 * Send an ACQUIRE message to key management, to get a new SA. 2148 */ 2149 int 2150 pfkeyv2_acquire(struct ipsec_policy *ipo, union sockaddr_union *gw, 2151 union sockaddr_union *laddr, u_int32_t *seq, struct sockaddr_encap *ddst) 2152 { 2153 void *p, *headers[SADB_EXT_MAX + 1], *buffer = NULL; 2154 struct sadb_comb *sadb_comb; 2155 struct sadb_address *sadd; 2156 struct sadb_prop *sa_prop; 2157 struct sadb_msg *smsg; 2158 int rval = 0; 2159 int i, j, registered; 2160 2161 mtx_enter(&pfkeyv2_mtx); 2162 *seq = pfkeyv2_seq++; 2163 2164 registered = nregistered; 2165 mtx_leave(&pfkeyv2_mtx); 2166 2167 if (!registered) { 2168 rval = ESRCH; 2169 goto ret; 2170 } 2171 2172 /* How large a buffer do we need... XXX we only do one proposal for now */ 2173 i = sizeof(struct sadb_msg) + 2174 (laddr == NULL ? 0 : sizeof(struct sadb_address) + 2175 PADUP(ipo->ipo_src.sa.sa_len)) + 2176 sizeof(struct sadb_address) + PADUP(gw->sa.sa_len) + 2177 sizeof(struct sadb_prop) + 1 * sizeof(struct sadb_comb); 2178 2179 if (ipo->ipo_ids) { 2180 i += sizeof(struct sadb_ident) + PADUP(ipo->ipo_ids->id_local->len); 2181 i += sizeof(struct sadb_ident) + PADUP(ipo->ipo_ids->id_remote->len); 2182 } 2183 2184 /* Allocate */ 2185 if (!(p = malloc(i, M_PFKEY, M_NOWAIT | M_ZERO))) { 2186 rval = ENOMEM; 2187 goto ret; 2188 } 2189 2190 bzero(headers, sizeof(headers)); 2191 2192 buffer = p; 2193 2194 headers[0] = p; 2195 p += sizeof(struct sadb_msg); 2196 2197 smsg = (struct sadb_msg *) headers[0]; 2198 smsg->sadb_msg_version = PF_KEY_V2; 2199 smsg->sadb_msg_type = SADB_ACQUIRE; 2200 smsg->sadb_msg_len = i / sizeof(uint64_t); 2201 smsg->sadb_msg_seq = *seq; 2202 2203 if (ipo->ipo_sproto == IPPROTO_ESP) 2204 smsg->sadb_msg_satype = SADB_SATYPE_ESP; 2205 else if (ipo->ipo_sproto == IPPROTO_AH) 2206 smsg->sadb_msg_satype = SADB_SATYPE_AH; 2207 else if (ipo->ipo_sproto == IPPROTO_IPCOMP) 2208 smsg->sadb_msg_satype = SADB_X_SATYPE_IPCOMP; 2209 2210 if (laddr) { 2211 headers[SADB_EXT_ADDRESS_SRC] = p; 2212 p += sizeof(struct sadb_address) + PADUP(laddr->sa.sa_len); 2213 sadd = (struct sadb_address *) headers[SADB_EXT_ADDRESS_SRC]; 2214 sadd->sadb_address_len = (sizeof(struct sadb_address) + 2215 laddr->sa.sa_len + sizeof(uint64_t) - 1) / 2216 sizeof(uint64_t); 2217 bcopy(laddr, headers[SADB_EXT_ADDRESS_SRC] + 2218 sizeof(struct sadb_address), laddr->sa.sa_len); 2219 } 2220 2221 headers[SADB_EXT_ADDRESS_DST] = p; 2222 p += sizeof(struct sadb_address) + PADUP(gw->sa.sa_len); 2223 sadd = (struct sadb_address *) headers[SADB_EXT_ADDRESS_DST]; 2224 sadd->sadb_address_len = (sizeof(struct sadb_address) + 2225 gw->sa.sa_len + sizeof(uint64_t) - 1) / sizeof(uint64_t); 2226 bcopy(gw, headers[SADB_EXT_ADDRESS_DST] + sizeof(struct sadb_address), 2227 gw->sa.sa_len); 2228 2229 if (ipo->ipo_ids) 2230 export_identities(&p, ipo->ipo_ids, 0, headers); 2231 2232 headers[SADB_EXT_PROPOSAL] = p; 2233 p += sizeof(struct sadb_prop); 2234 sa_prop = (struct sadb_prop *) headers[SADB_EXT_PROPOSAL]; 2235 sa_prop->sadb_prop_num = 1; /* XXX One proposal only */ 2236 sa_prop->sadb_prop_len = (sizeof(struct sadb_prop) + 2237 (sizeof(struct sadb_comb) * sa_prop->sadb_prop_num)) / 2238 sizeof(uint64_t); 2239 2240 sadb_comb = p; 2241 2242 /* XXX Should actually ask the crypto layer what's supported */ 2243 for (j = 0; j < sa_prop->sadb_prop_num; j++) { 2244 sadb_comb->sadb_comb_flags = 0; 2245 #ifdef IPSEC 2246 if (ipsec_require_pfs) 2247 sadb_comb->sadb_comb_flags |= SADB_SAFLAGS_PFS; 2248 2249 /* Set the encryption algorithm */ 2250 if (ipo->ipo_sproto == IPPROTO_ESP) { 2251 if (!strncasecmp(ipsec_def_enc, "aes", 2252 sizeof("aes"))) { 2253 sadb_comb->sadb_comb_encrypt = SADB_X_EALG_AES; 2254 sadb_comb->sadb_comb_encrypt_minbits = 128; 2255 sadb_comb->sadb_comb_encrypt_maxbits = 256; 2256 } else if (!strncasecmp(ipsec_def_enc, "aesctr", 2257 sizeof("aesctr"))) { 2258 sadb_comb->sadb_comb_encrypt = SADB_X_EALG_AESCTR; 2259 sadb_comb->sadb_comb_encrypt_minbits = 128+32; 2260 sadb_comb->sadb_comb_encrypt_maxbits = 256+32; 2261 } else if (!strncasecmp(ipsec_def_enc, "3des", 2262 sizeof("3des"))) { 2263 sadb_comb->sadb_comb_encrypt = SADB_EALG_3DESCBC; 2264 sadb_comb->sadb_comb_encrypt_minbits = 192; 2265 sadb_comb->sadb_comb_encrypt_maxbits = 192; 2266 } else if (!strncasecmp(ipsec_def_enc, "blowfish", 2267 sizeof("blowfish"))) { 2268 sadb_comb->sadb_comb_encrypt = SADB_X_EALG_BLF; 2269 sadb_comb->sadb_comb_encrypt_minbits = 40; 2270 sadb_comb->sadb_comb_encrypt_maxbits = BLF_MAXKEYLEN * 8; 2271 } else if (!strncasecmp(ipsec_def_enc, "cast128", 2272 sizeof("cast128"))) { 2273 sadb_comb->sadb_comb_encrypt = SADB_X_EALG_CAST; 2274 sadb_comb->sadb_comb_encrypt_minbits = 40; 2275 sadb_comb->sadb_comb_encrypt_maxbits = 128; 2276 } 2277 } else if (ipo->ipo_sproto == IPPROTO_IPCOMP) { 2278 /* Set the compression algorithm */ 2279 if (!strncasecmp(ipsec_def_comp, "deflate", 2280 sizeof("deflate"))) { 2281 sadb_comb->sadb_comb_encrypt = SADB_X_CALG_DEFLATE; 2282 sadb_comb->sadb_comb_encrypt_minbits = 0; 2283 sadb_comb->sadb_comb_encrypt_maxbits = 0; 2284 } 2285 } 2286 2287 /* Set the authentication algorithm */ 2288 if (!strncasecmp(ipsec_def_auth, "hmac-sha1", 2289 sizeof("hmac-sha1"))) { 2290 sadb_comb->sadb_comb_auth = SADB_AALG_SHA1HMAC; 2291 sadb_comb->sadb_comb_auth_minbits = 160; 2292 sadb_comb->sadb_comb_auth_maxbits = 160; 2293 } else if (!strncasecmp(ipsec_def_auth, "hmac-ripemd160", 2294 sizeof("hmac_ripemd160"))) { 2295 sadb_comb->sadb_comb_auth = SADB_X_AALG_RIPEMD160HMAC; 2296 sadb_comb->sadb_comb_auth_minbits = 160; 2297 sadb_comb->sadb_comb_auth_maxbits = 160; 2298 } else if (!strncasecmp(ipsec_def_auth, "hmac-md5", 2299 sizeof("hmac-md5"))) { 2300 sadb_comb->sadb_comb_auth = SADB_AALG_MD5HMAC; 2301 sadb_comb->sadb_comb_auth_minbits = 128; 2302 sadb_comb->sadb_comb_auth_maxbits = 128; 2303 } else if (!strncasecmp(ipsec_def_auth, "hmac-sha2-256", 2304 sizeof("hmac-sha2-256"))) { 2305 sadb_comb->sadb_comb_auth = SADB_X_AALG_SHA2_256; 2306 sadb_comb->sadb_comb_auth_minbits = 256; 2307 sadb_comb->sadb_comb_auth_maxbits = 256; 2308 } else if (!strncasecmp(ipsec_def_auth, "hmac-sha2-384", 2309 sizeof("hmac-sha2-384"))) { 2310 sadb_comb->sadb_comb_auth = SADB_X_AALG_SHA2_384; 2311 sadb_comb->sadb_comb_auth_minbits = 384; 2312 sadb_comb->sadb_comb_auth_maxbits = 384; 2313 } else if (!strncasecmp(ipsec_def_auth, "hmac-sha2-512", 2314 sizeof("hmac-sha2-512"))) { 2315 sadb_comb->sadb_comb_auth = SADB_X_AALG_SHA2_512; 2316 sadb_comb->sadb_comb_auth_minbits = 512; 2317 sadb_comb->sadb_comb_auth_maxbits = 512; 2318 } 2319 2320 sadb_comb->sadb_comb_soft_allocations = ipsec_soft_allocations; 2321 sadb_comb->sadb_comb_hard_allocations = ipsec_exp_allocations; 2322 2323 sadb_comb->sadb_comb_soft_bytes = ipsec_soft_bytes; 2324 sadb_comb->sadb_comb_hard_bytes = ipsec_exp_bytes; 2325 2326 sadb_comb->sadb_comb_soft_addtime = ipsec_soft_timeout; 2327 sadb_comb->sadb_comb_hard_addtime = ipsec_exp_timeout; 2328 2329 sadb_comb->sadb_comb_soft_usetime = ipsec_soft_first_use; 2330 sadb_comb->sadb_comb_hard_usetime = ipsec_exp_first_use; 2331 #endif 2332 sadb_comb++; 2333 } 2334 2335 /* Send the ACQUIRE message to all compliant registered listeners. */ 2336 if ((rval = pfkeyv2_sendmessage(headers, 2337 PFKEYV2_SENDMESSAGE_REGISTERED, NULL, smsg->sadb_msg_satype, 0, 2338 ipo->ipo_rdomain)) != 0) 2339 goto ret; 2340 2341 rval = 0; 2342 ret: 2343 if (buffer != NULL) { 2344 explicit_bzero(buffer, i); 2345 free(buffer, M_PFKEY, i); 2346 } 2347 2348 return (rval); 2349 } 2350 2351 /* 2352 * Notify key management that an expiration went off. The second argument 2353 * specifies the type of expiration (soft or hard). 2354 */ 2355 int 2356 pfkeyv2_expire(struct tdb *tdb, u_int16_t type) 2357 { 2358 void *p, *headers[SADB_EXT_MAX+1], *buffer = NULL; 2359 struct sadb_msg *smsg; 2360 int rval = 0; 2361 int i; 2362 2363 NET_ASSERT_LOCKED(); 2364 2365 switch (tdb->tdb_sproto) { 2366 case IPPROTO_AH: 2367 case IPPROTO_ESP: 2368 case IPPROTO_IPIP: 2369 case IPPROTO_IPCOMP: 2370 #ifdef TCP_SIGNATURE 2371 case IPPROTO_TCP: 2372 #endif /* TCP_SIGNATURE */ 2373 break; 2374 2375 default: 2376 rval = EOPNOTSUPP; 2377 goto ret; 2378 } 2379 2380 i = sizeof(struct sadb_msg) + sizeof(struct sadb_sa) + 2381 2 * sizeof(struct sadb_lifetime) + 2382 sizeof(struct sadb_address) + PADUP(tdb->tdb_src.sa.sa_len) + 2383 sizeof(struct sadb_address) + PADUP(tdb->tdb_dst.sa.sa_len); 2384 2385 if (!(p = malloc(i, M_PFKEY, M_NOWAIT | M_ZERO))) { 2386 rval = ENOMEM; 2387 goto ret; 2388 } 2389 2390 bzero(headers, sizeof(headers)); 2391 2392 buffer = p; 2393 2394 headers[0] = p; 2395 p += sizeof(struct sadb_msg); 2396 2397 smsg = (struct sadb_msg *) headers[0]; 2398 smsg->sadb_msg_version = PF_KEY_V2; 2399 smsg->sadb_msg_type = SADB_EXPIRE; 2400 smsg->sadb_msg_satype = tdb->tdb_satype; 2401 smsg->sadb_msg_len = i / sizeof(uint64_t); 2402 2403 mtx_enter(&pfkeyv2_mtx); 2404 smsg->sadb_msg_seq = pfkeyv2_seq++; 2405 mtx_leave(&pfkeyv2_mtx); 2406 2407 headers[SADB_EXT_SA] = p; 2408 export_sa(&p, tdb); 2409 2410 headers[SADB_EXT_LIFETIME_CURRENT] = p; 2411 export_lifetime(&p, tdb, PFKEYV2_LIFETIME_CURRENT); 2412 2413 headers[type] = p; 2414 export_lifetime(&p, tdb, type == SADB_EXT_LIFETIME_SOFT ? 2415 PFKEYV2_LIFETIME_SOFT : PFKEYV2_LIFETIME_HARD); 2416 2417 headers[SADB_EXT_ADDRESS_SRC] = p; 2418 export_address(&p, &tdb->tdb_src.sa); 2419 2420 headers[SADB_EXT_ADDRESS_DST] = p; 2421 export_address(&p, &tdb->tdb_dst.sa); 2422 2423 if ((rval = pfkeyv2_sendmessage(headers, PFKEYV2_SENDMESSAGE_BROADCAST, 2424 NULL, 0, 0, tdb->tdb_rdomain)) != 0) 2425 goto ret; 2426 /* XXX */ 2427 if (tdb->tdb_rdomain != tdb->tdb_rdomain_post) 2428 if ((rval = pfkeyv2_sendmessage(headers, 2429 PFKEYV2_SENDMESSAGE_BROADCAST, NULL, 0, 0, 2430 tdb->tdb_rdomain_post)) != 0) 2431 goto ret; 2432 2433 rval = 0; 2434 2435 ret: 2436 if (buffer != NULL) { 2437 explicit_bzero(buffer, i); 2438 free(buffer, M_PFKEY, i); 2439 } 2440 2441 return (rval); 2442 } 2443 2444 struct pfkeyv2_sysctl_walk { 2445 void *w_where; 2446 size_t w_len; 2447 int w_op; 2448 u_int8_t w_satype; 2449 }; 2450 2451 int 2452 pfkeyv2_sysctl_walker(struct tdb *tdb, void *arg, int last) 2453 { 2454 struct pfkeyv2_sysctl_walk *w = (struct pfkeyv2_sysctl_walk *)arg; 2455 void *buffer = NULL; 2456 int error = 0; 2457 int usedlen, buflen, i; 2458 2459 if (w->w_satype != SADB_SATYPE_UNSPEC && 2460 w->w_satype != tdb->tdb_satype) 2461 return (0); 2462 2463 if (w->w_where) { 2464 void *headers[SADB_EXT_MAX+1]; 2465 struct sadb_msg msg; 2466 2467 bzero(headers, sizeof(headers)); 2468 if ((error = pfkeyv2_get(tdb, headers, &buffer, &buflen, 2469 &usedlen)) != 0) 2470 goto done; 2471 if (w->w_len < sizeof(msg) + usedlen) { 2472 error = ENOMEM; 2473 goto done; 2474 } 2475 /* prepend header */ 2476 bzero(&msg, sizeof(msg)); 2477 msg.sadb_msg_version = PF_KEY_V2; 2478 msg.sadb_msg_satype = tdb->tdb_satype; 2479 msg.sadb_msg_type = SADB_DUMP; 2480 msg.sadb_msg_len = (sizeof(msg) + usedlen) / sizeof(uint64_t); 2481 if ((error = copyout(&msg, w->w_where, sizeof(msg))) != 0) 2482 goto done; 2483 w->w_where += sizeof(msg); 2484 w->w_len -= sizeof(msg); 2485 /* set extension type */ 2486 for (i = 1; i <= SADB_EXT_MAX; i++) 2487 if (headers[i]) 2488 ((struct sadb_ext *) 2489 headers[i])->sadb_ext_type = i; 2490 if ((error = copyout(buffer, w->w_where, usedlen)) != 0) 2491 goto done; 2492 w->w_where += usedlen; 2493 w->w_len -= usedlen; 2494 } else { 2495 if ((error = pfkeyv2_get(tdb, NULL, NULL, &buflen, NULL)) != 0) 2496 return (error); 2497 w->w_len += buflen; 2498 w->w_len += sizeof(struct sadb_msg); 2499 } 2500 2501 done: 2502 if (buffer != NULL) { 2503 explicit_bzero(buffer, buflen); 2504 free(buffer, M_PFKEY, buflen); 2505 } 2506 return (error); 2507 } 2508 2509 int 2510 pfkeyv2_dump_policy(struct ipsec_policy *ipo, void **headers, void **buffer, 2511 int *lenp) 2512 { 2513 int i, rval, perm; 2514 void *p; 2515 2516 /* Find how much space we need. */ 2517 i = 2 * sizeof(struct sadb_protocol); 2518 2519 /* We'll need four of them: src, src mask, dst, dst mask. */ 2520 switch (ipo->ipo_addr.sen_type) { 2521 case SENT_IP4: 2522 i += 4 * PADUP(sizeof(struct sockaddr_in)); 2523 i += 4 * sizeof(struct sadb_address); 2524 break; 2525 #ifdef INET6 2526 case SENT_IP6: 2527 i += 4 * PADUP(sizeof(struct sockaddr_in6)); 2528 i += 4 * sizeof(struct sadb_address); 2529 break; 2530 #endif /* INET6 */ 2531 default: 2532 return (EINVAL); 2533 } 2534 2535 /* Local address, might be zeroed. */ 2536 switch (ipo->ipo_src.sa.sa_family) { 2537 case 0: 2538 break; 2539 case AF_INET: 2540 i += PADUP(sizeof(struct sockaddr_in)); 2541 i += sizeof(struct sadb_address); 2542 break; 2543 #ifdef INET6 2544 case AF_INET6: 2545 i += PADUP(sizeof(struct sockaddr_in6)); 2546 i += sizeof(struct sadb_address); 2547 break; 2548 #endif /* INET6 */ 2549 default: 2550 return (EINVAL); 2551 } 2552 2553 /* Remote address, might be zeroed. XXX ??? */ 2554 switch (ipo->ipo_dst.sa.sa_family) { 2555 case 0: 2556 break; 2557 case AF_INET: 2558 i += PADUP(sizeof(struct sockaddr_in)); 2559 i += sizeof(struct sadb_address); 2560 break; 2561 #ifdef INET6 2562 case AF_INET6: 2563 i += PADUP(sizeof(struct sockaddr_in6)); 2564 i += sizeof(struct sadb_address); 2565 break; 2566 #endif /* INET6 */ 2567 default: 2568 return (EINVAL); 2569 } 2570 2571 if (ipo->ipo_ids) { 2572 i += sizeof(struct sadb_ident) + PADUP(ipo->ipo_ids->id_local->len); 2573 i += sizeof(struct sadb_ident) + PADUP(ipo->ipo_ids->id_remote->len); 2574 } 2575 2576 if (lenp) 2577 *lenp = i; 2578 2579 if (buffer == NULL) { 2580 rval = 0; 2581 goto ret; 2582 } 2583 2584 if (!(p = malloc(i, M_PFKEY, M_NOWAIT | M_ZERO))) { 2585 rval = ENOMEM; 2586 goto ret; 2587 } else 2588 *buffer = p; 2589 2590 /* Local address. */ 2591 if (ipo->ipo_src.sa.sa_family) { 2592 headers[SADB_EXT_ADDRESS_SRC] = p; 2593 export_address(&p, &ipo->ipo_src.sa); 2594 } 2595 2596 /* Remote address. */ 2597 if (ipo->ipo_dst.sa.sa_family) { 2598 headers[SADB_EXT_ADDRESS_DST] = p; 2599 export_address(&p, &ipo->ipo_dst.sa); 2600 } 2601 2602 /* Get actual flow. */ 2603 export_flow(&p, ipo->ipo_type, &ipo->ipo_addr, &ipo->ipo_mask, 2604 headers); 2605 2606 /* Add ids only when we are root. */ 2607 perm = suser(curproc); 2608 if (perm == 0 && ipo->ipo_ids) 2609 export_identities(&p, ipo->ipo_ids, 0, headers); 2610 2611 rval = 0; 2612 ret: 2613 return (rval); 2614 } 2615 2616 int 2617 pfkeyv2_sysctl_policydumper(struct ipsec_policy *ipo, void *arg, 2618 unsigned int tableid) 2619 { 2620 struct pfkeyv2_sysctl_walk *w = (struct pfkeyv2_sysctl_walk *)arg; 2621 void *buffer = NULL; 2622 int i, buflen, error = 0; 2623 2624 if (w->w_where) { 2625 void *headers[SADB_EXT_MAX + 1]; 2626 struct sadb_msg msg; 2627 2628 bzero(headers, sizeof(headers)); 2629 if ((error = pfkeyv2_dump_policy(ipo, headers, &buffer, 2630 &buflen)) != 0) 2631 goto done; 2632 if (w->w_len < buflen) { 2633 error = ENOMEM; 2634 goto done; 2635 } 2636 /* prepend header */ 2637 bzero(&msg, sizeof(msg)); 2638 msg.sadb_msg_version = PF_KEY_V2; 2639 if (ipo->ipo_sproto == IPPROTO_ESP) 2640 msg.sadb_msg_satype = SADB_SATYPE_ESP; 2641 else if (ipo->ipo_sproto == IPPROTO_AH) 2642 msg.sadb_msg_satype = SADB_SATYPE_AH; 2643 else if (ipo->ipo_sproto == IPPROTO_IPCOMP) 2644 msg.sadb_msg_satype = SADB_X_SATYPE_IPCOMP; 2645 else if (ipo->ipo_sproto == IPPROTO_IPIP) 2646 msg.sadb_msg_satype = SADB_X_SATYPE_IPIP; 2647 msg.sadb_msg_type = SADB_X_SPDDUMP; 2648 msg.sadb_msg_len = (sizeof(msg) + buflen) / sizeof(uint64_t); 2649 if ((error = copyout(&msg, w->w_where, sizeof(msg))) != 0) 2650 goto done; 2651 w->w_where += sizeof(msg); 2652 w->w_len -= sizeof(msg); 2653 /* set extension type */ 2654 for (i = 1; i <= SADB_EXT_MAX; i++) 2655 if (headers[i]) 2656 ((struct sadb_ext *) 2657 headers[i])->sadb_ext_type = i; 2658 if ((error = copyout(buffer, w->w_where, buflen)) != 0) 2659 goto done; 2660 w->w_where += buflen; 2661 w->w_len -= buflen; 2662 } else { 2663 if ((error = pfkeyv2_dump_policy(ipo, NULL, NULL, 2664 &buflen)) != 0) 2665 goto done; 2666 w->w_len += buflen; 2667 w->w_len += sizeof(struct sadb_msg); 2668 } 2669 2670 done: 2671 if (buffer) 2672 free(buffer, M_PFKEY, buflen); 2673 return (error); 2674 } 2675 2676 int 2677 pfkeyv2_policy_flush(struct ipsec_policy *ipo, void *arg, unsigned int tableid) 2678 { 2679 int error; 2680 2681 error = ipsec_delete_policy(ipo); 2682 if (error == 0) 2683 error = EAGAIN; 2684 2685 return (error); 2686 } 2687 2688 int 2689 pfkeyv2_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, 2690 void *new, size_t newlen) 2691 { 2692 struct pfkeyv2_sysctl_walk w; 2693 int error = EINVAL; 2694 u_int rdomain; 2695 u_int tableid; 2696 2697 if (new) 2698 return (EPERM); 2699 if (namelen < 1) 2700 return (EINVAL); 2701 w.w_op = name[0]; 2702 if (namelen >= 2) 2703 w.w_satype = name[1]; 2704 else 2705 w.w_satype = SADB_SATYPE_UNSPEC; 2706 w.w_where = oldp; 2707 w.w_len = oldp ? *oldlenp : 0; 2708 2709 if (namelen == 3) { 2710 tableid = name[2]; 2711 if (!rtable_exists(tableid)) 2712 return (ENOENT); 2713 } else 2714 tableid = curproc->p_p->ps_rtableid; 2715 rdomain = rtable_l2(tableid); 2716 2717 switch(w.w_op) { 2718 case NET_KEY_SADB_DUMP: 2719 if ((error = suser(curproc)) != 0) 2720 return (error); 2721 NET_LOCK(); 2722 error = tdb_walk(rdomain, pfkeyv2_sysctl_walker, &w); 2723 NET_UNLOCK(); 2724 if (oldp) 2725 *oldlenp = w.w_where - oldp; 2726 else 2727 *oldlenp = w.w_len; 2728 break; 2729 2730 case NET_KEY_SPD_DUMP: 2731 NET_LOCK_SHARED(); 2732 error = spd_table_walk(rdomain, 2733 pfkeyv2_sysctl_policydumper, &w); 2734 NET_UNLOCK_SHARED(); 2735 if (oldp) 2736 *oldlenp = w.w_where - oldp; 2737 else 2738 *oldlenp = w.w_len; 2739 break; 2740 } 2741 2742 return (error); 2743 } 2744