1 /* $NetBSD: keysock.c,v 1.72 2024/07/05 04:31:54 rin Exp $ */ 2 /* $FreeBSD: keysock.c,v 1.3.2.1 2003/01/24 05:11:36 sam Exp $ */ 3 /* $KAME: keysock.c,v 1.25 2001/08/13 20:07:41 itojun Exp $ */ 4 5 /* 6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. Neither the name of the project nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #include <sys/cdefs.h> 35 __KERNEL_RCSID(0, "$NetBSD: keysock.c,v 1.72 2024/07/05 04:31:54 rin Exp $"); 36 37 /* This code has derived from sys/net/rtsock.c on FreeBSD2.2.5 */ 38 39 #include <sys/types.h> 40 #include <sys/param.h> 41 #include <sys/domain.h> 42 #include <sys/errno.h> 43 #include <sys/kernel.h> 44 #include <sys/kmem.h> 45 #include <sys/mbuf.h> 46 #include <sys/protosw.h> 47 #include <sys/signalvar.h> 48 #include <sys/socket.h> 49 #include <sys/socketvar.h> 50 #include <sys/sysctl.h> 51 #include <sys/systm.h> 52 #include <sys/cpu.h> 53 #include <sys/syslog.h> 54 55 #include <net/raw_cb.h> 56 #include <net/route.h> 57 58 #include <net/pfkeyv2.h> 59 #include <netipsec/key.h> 60 #include <netipsec/keysock.h> 61 #include <netipsec/key_debug.h> 62 63 #include <netipsec/ipsec_private.h> 64 65 struct key_cb { 66 int key_count; 67 int any_count; 68 }; 69 static struct key_cb key_cb; 70 71 static struct sockaddr key_dst = { 72 .sa_len = 2, 73 .sa_family = PF_KEY, 74 }; 75 static struct sockaddr key_src = { 76 .sa_len = 2, 77 .sa_family = PF_KEY, 78 }; 79 80 static const struct protosw keysw[]; 81 82 static int key_sendup0(struct rawcb *, struct mbuf *, int, int); 83 84 int key_registered_sb_max = (2048 * MHLEN); /* XXX arbitrary */ 85 86 static kmutex_t *key_so_mtx; 87 static struct rawcbhead key_rawcb; 88 89 void 90 key_init_so(void) 91 { 92 93 key_so_mtx = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE); 94 } 95 96 static void 97 key_pr_init(void) 98 { 99 100 LIST_INIT(&key_rawcb); 101 } 102 103 /* 104 * key_output() 105 */ 106 static int 107 key_output(struct mbuf *m, struct socket *so) 108 { 109 struct sadb_msg *msg; 110 int len, error = 0; 111 int s; 112 113 KASSERT(m != NULL); 114 115 { 116 net_stat_ref_t ps = PFKEY_STAT_GETREF(); 117 _NET_STATINC_REF(ps, PFKEY_STAT_OUT_TOTAL); 118 _NET_STATADD_REF(ps, PFKEY_STAT_OUT_BYTES, m->m_pkthdr.len); 119 PFKEY_STAT_PUTREF(); 120 } 121 122 len = m->m_pkthdr.len; 123 if (len < sizeof(struct sadb_msg)) { 124 PFKEY_STATINC(PFKEY_STAT_OUT_TOOSHORT); 125 error = EINVAL; 126 goto end; 127 } 128 129 if (m->m_len < sizeof(struct sadb_msg)) { 130 if ((m = m_pullup(m, sizeof(struct sadb_msg))) == 0) { 131 PFKEY_STATINC(PFKEY_STAT_OUT_NOMEM); 132 error = ENOBUFS; 133 goto end; 134 } 135 } 136 137 KASSERT((m->m_flags & M_PKTHDR) != 0); 138 139 if (KEYDEBUG_ON(KEYDEBUG_KEY_DUMP)) 140 kdebug_mbuf(__func__, m); 141 142 msg = mtod(m, struct sadb_msg *); 143 PFKEY_STATINC(PFKEY_STAT_OUT_MSGTYPE + msg->sadb_msg_type); 144 if (len != PFKEY_UNUNIT64(msg->sadb_msg_len)) { 145 PFKEY_STATINC(PFKEY_STAT_OUT_INVLEN); 146 error = EINVAL; 147 goto end; 148 } 149 150 /*XXX giant lock*/ 151 s = splsoftnet(); 152 error = key_parse(m, so); 153 m = NULL; 154 splx(s); 155 end: 156 m_freem(m); 157 return error; 158 } 159 160 /* 161 * send message to the socket. 162 */ 163 static int 164 key_sendup0( 165 struct rawcb *rp, 166 struct mbuf *m, 167 int promisc, 168 int sbprio 169 ) 170 { 171 int error; 172 int ok; 173 174 if (promisc) { 175 struct sadb_msg *pmsg; 176 177 M_PREPEND(m, sizeof(struct sadb_msg), M_DONTWAIT); 178 if (m && m->m_len < sizeof(struct sadb_msg)) 179 m = m_pullup(m, sizeof(struct sadb_msg)); 180 if (!m) { 181 PFKEY_STATINC(PFKEY_STAT_IN_NOMEM); 182 return ENOBUFS; 183 } 184 m->m_pkthdr.len += sizeof(*pmsg); 185 186 pmsg = mtod(m, struct sadb_msg *); 187 memset(pmsg, 0, sizeof(*pmsg)); 188 pmsg->sadb_msg_version = PF_KEY_V2; 189 pmsg->sadb_msg_type = SADB_X_PROMISC; 190 pmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len); 191 /* pid and seq? */ 192 193 PFKEY_STATINC(PFKEY_STAT_IN_MSGTYPE + pmsg->sadb_msg_type); 194 } 195 196 if (sbprio == 0) 197 ok = sbappendaddr(&rp->rcb_socket->so_rcv, 198 (struct sockaddr *)&key_src, m, NULL); 199 else 200 ok = sbappendaddrchain(&rp->rcb_socket->so_rcv, 201 (struct sockaddr *)&key_src, m, sbprio); 202 203 if (!ok) { 204 log(LOG_WARNING, 205 "%s: couldn't send PF_KEY message to the socket\n", 206 __func__); 207 PFKEY_STATINC(PFKEY_STAT_IN_NOMEM); 208 m_freem(m); 209 /* Don't call soroverflow because we're returning this 210 * error directly to the sender. */ 211 rp->rcb_socket->so_rcv.sb_overflowed++; 212 error = ENOBUFS; 213 } else { 214 sorwakeup(rp->rcb_socket); 215 error = 0; 216 } 217 return error; 218 } 219 220 /* so can be NULL if target != KEY_SENDUP_ONE */ 221 static int 222 _key_sendup_mbuf(struct socket *so, struct mbuf *m, 223 int target/*, sbprio */) 224 { 225 struct mbuf *n; 226 struct keycb *kp; 227 int sendup; 228 struct rawcb *rp; 229 int error = 0; 230 int sbprio = 0; /* XXX should be a parameter */ 231 232 KASSERT(m != NULL); 233 KASSERT(so != NULL || target != KEY_SENDUP_ONE); 234 235 /* 236 * RFC 2367 says ACQUIRE and other kernel-generated messages 237 * are special. We treat all KEY_SENDUP_REGISTERED messages 238 * as special, delivering them to all registered sockets 239 * even if the socket is at or above its so->so_rcv.sb_max limits. 240 * The only constraint is that the so_rcv data fall below 241 * key_registered_sb_max. 242 * Doing that check here avoids reworking every key_sendup_mbuf() 243 * in the short term. . The rework will be done after a technical 244 * conensus that this approach is appropriate. 245 */ 246 if (target == KEY_SENDUP_REGISTERED) { 247 sbprio = SB_PRIO_BESTEFFORT; 248 } 249 250 { 251 net_stat_ref_t ps = PFKEY_STAT_GETREF(); 252 _NET_STATINC_REF(ps, PFKEY_STAT_IN_TOTAL); 253 _NET_STATADD_REF(ps, PFKEY_STAT_IN_BYTES, m->m_pkthdr.len); 254 PFKEY_STAT_PUTREF(); 255 } 256 if (m->m_len < sizeof(struct sadb_msg)) { 257 #if 1 258 m = m_pullup(m, sizeof(struct sadb_msg)); 259 if (m == NULL) { 260 PFKEY_STATINC(PFKEY_STAT_IN_NOMEM); 261 return ENOBUFS; 262 } 263 #else 264 /* don't bother pulling it up just for stats */ 265 #endif 266 } 267 if (m->m_len >= sizeof(struct sadb_msg)) { 268 struct sadb_msg *msg; 269 msg = mtod(m, struct sadb_msg *); 270 PFKEY_STATINC(PFKEY_STAT_IN_MSGTYPE + msg->sadb_msg_type); 271 } 272 273 LIST_FOREACH(rp, &key_rawcb, rcb_list) 274 { 275 struct socket * kso = rp->rcb_socket; 276 if (rp->rcb_proto.sp_family != PF_KEY) 277 continue; 278 if (rp->rcb_proto.sp_protocol 279 && rp->rcb_proto.sp_protocol != PF_KEY_V2) { 280 continue; 281 } 282 283 kp = (struct keycb *)rp; 284 285 /* 286 * If you are in promiscuous mode, and when you get broadcasted 287 * reply, you'll get two PF_KEY messages. 288 * (based on pf_key@inner.net message on 14 Oct 1998) 289 */ 290 if (((struct keycb *)rp)->kp_promisc) { 291 if ((n = m_copym(m, 0, (int)M_COPYALL, M_DONTWAIT)) != NULL) { 292 (void)key_sendup0(rp, n, 1, 0); 293 n = NULL; 294 } 295 } 296 297 /* the exact target will be processed later */ 298 if (so && sotorawcb(so) == rp) 299 continue; 300 301 sendup = 0; 302 switch (target) { 303 case KEY_SENDUP_ONE: 304 /* the statement has no effect */ 305 if (so && sotorawcb(so) == rp) 306 sendup++; 307 break; 308 case KEY_SENDUP_ALL: 309 sendup++; 310 break; 311 case KEY_SENDUP_REGISTERED: 312 if (kp->kp_registered) { 313 if (kso->so_rcv.sb_cc <= key_registered_sb_max) 314 sendup++; 315 else 316 printf("keysock: " 317 "registered sendup dropped, " 318 "sb_cc %ld max %d\n", 319 kso->so_rcv.sb_cc, 320 key_registered_sb_max); 321 } 322 break; 323 } 324 PFKEY_STATINC(PFKEY_STAT_IN_MSGTARGET + target); 325 326 if (!sendup) 327 continue; 328 329 if ((n = m_copym(m, 0, (int)M_COPYALL, M_DONTWAIT)) == NULL) { 330 m_freem(m); 331 PFKEY_STATINC(PFKEY_STAT_IN_NOMEM); 332 return ENOBUFS; 333 } 334 335 if ((error = key_sendup0(rp, n, 0, 0)) != 0) { 336 m_freem(m); 337 return error; 338 } 339 340 n = NULL; 341 } 342 343 /* The 'later' time for processing the exact target has arrived */ 344 if (so) { 345 error = key_sendup0(sotorawcb(so), m, 0, sbprio); 346 m = NULL; 347 } else { 348 error = 0; 349 m_freem(m); 350 } 351 return error; 352 } 353 354 int 355 key_sendup_mbuf(struct socket *so, struct mbuf *m, 356 int target/*, sbprio */) 357 { 358 int error; 359 360 if (so == NULL) 361 mutex_enter(key_so_mtx); 362 else 363 KASSERT(solocked(so)); 364 365 error = _key_sendup_mbuf(so, m, target); 366 367 if (so == NULL) 368 mutex_exit(key_so_mtx); 369 return error; 370 } 371 372 static int 373 key_attach(struct socket *so, int proto) 374 { 375 struct keycb *kp; 376 int s, error; 377 378 KASSERT(sotorawcb(so) == NULL); 379 kp = kmem_zalloc(sizeof(*kp), KM_SLEEP); 380 kp->kp_raw.rcb_len = sizeof(*kp); 381 so->so_pcb = kp; 382 383 s = splsoftnet(); 384 385 if (so->so_lock != key_so_mtx) { 386 KASSERT(so->so_lock == NULL); 387 mutex_obj_hold(key_so_mtx); 388 so->so_lock = key_so_mtx; 389 solock(so); 390 } 391 392 error = raw_attach(so, proto, &key_rawcb); 393 if (error) { 394 PFKEY_STATINC(PFKEY_STAT_SOCKERR); 395 kmem_free(kp, sizeof(*kp)); 396 so->so_pcb = NULL; 397 goto out; 398 } 399 400 kp->kp_promisc = kp->kp_registered = 0; 401 402 if (kp->kp_raw.rcb_proto.sp_protocol == PF_KEY) /* XXX: AF_KEY */ 403 key_cb.key_count++; 404 key_cb.any_count++; 405 kp->kp_raw.rcb_laddr = &key_src; 406 kp->kp_raw.rcb_faddr = &key_dst; 407 soisconnected(so); 408 so->so_options |= SO_USELOOPBACK; 409 out: 410 KASSERT(solocked(so)); 411 splx(s); 412 return error; 413 } 414 415 static void 416 key_detach(struct socket *so) 417 { 418 struct keycb *kp = (struct keycb *)sotorawcb(so); 419 int s; 420 421 KASSERT(!cpu_softintr_p()); 422 KASSERT(solocked(so)); 423 KASSERT(kp != NULL); 424 425 s = splsoftnet(); 426 if (kp->kp_raw.rcb_proto.sp_protocol == PF_KEY) /* XXX: AF_KEY */ 427 key_cb.key_count--; 428 key_cb.any_count--; 429 key_freereg(so); 430 raw_detach(so); 431 splx(s); 432 } 433 434 static int 435 key_accept(struct socket *so, struct sockaddr *nam) 436 { 437 KASSERT(solocked(so)); 438 439 panic("%s: unsupported", __func__); 440 441 return EOPNOTSUPP; 442 } 443 444 static int 445 key_bind(struct socket *so, struct sockaddr *nam, struct lwp *l) 446 { 447 KASSERT(solocked(so)); 448 449 return EOPNOTSUPP; 450 } 451 452 static int 453 key_listen(struct socket *so, struct lwp *l) 454 { 455 KASSERT(solocked(so)); 456 457 return EOPNOTSUPP; 458 } 459 460 static int 461 key_connect(struct socket *so, struct sockaddr *nam, struct lwp *l) 462 { 463 KASSERT(solocked(so)); 464 465 return EOPNOTSUPP; 466 } 467 468 static int 469 key_connect2(struct socket *so, struct socket *so2) 470 { 471 KASSERT(solocked(so)); 472 473 return EOPNOTSUPP; 474 } 475 476 static int 477 key_disconnect(struct socket *so) 478 { 479 struct rawcb *rp = sotorawcb(so); 480 int s; 481 482 KASSERT(solocked(so)); 483 KASSERT(rp != NULL); 484 485 s = splsoftnet(); 486 soisdisconnected(so); 487 raw_disconnect(rp); 488 splx(s); 489 490 return 0; 491 } 492 493 static int 494 key_shutdown(struct socket *so) 495 { 496 int s; 497 498 KASSERT(solocked(so)); 499 500 /* 501 * Mark the connection as being incapable of further input. 502 */ 503 s = splsoftnet(); 504 socantsendmore(so); 505 splx(s); 506 507 return 0; 508 } 509 510 static int 511 key_abort(struct socket *so) 512 { 513 KASSERT(solocked(so)); 514 515 panic("%s: unsupported", __func__); 516 517 return EOPNOTSUPP; 518 } 519 520 static int 521 key_ioctl(struct socket *so, u_long cmd, void *nam, struct ifnet *ifp) 522 { 523 return EOPNOTSUPP; 524 } 525 526 static int 527 key_stat(struct socket *so, struct stat *ub) 528 { 529 KASSERT(solocked(so)); 530 531 return 0; 532 } 533 534 static int 535 key_peeraddr(struct socket *so, struct sockaddr *nam) 536 { 537 struct rawcb *rp = sotorawcb(so); 538 539 KASSERT(solocked(so)); 540 KASSERT(rp != NULL); 541 KASSERT(nam != NULL); 542 543 if (rp->rcb_faddr == NULL) 544 return ENOTCONN; 545 546 raw_setpeeraddr(rp, nam); 547 return 0; 548 } 549 550 static int 551 key_sockaddr(struct socket *so, struct sockaddr *nam) 552 { 553 struct rawcb *rp = sotorawcb(so); 554 555 KASSERT(solocked(so)); 556 KASSERT(rp != NULL); 557 KASSERT(nam != NULL); 558 559 if (rp->rcb_faddr == NULL) 560 return ENOTCONN; 561 562 raw_setsockaddr(rp, nam); 563 return 0; 564 } 565 566 static int 567 key_rcvd(struct socket *so, int flags, struct lwp *l) 568 { 569 KASSERT(solocked(so)); 570 571 return EOPNOTSUPP; 572 } 573 574 static int 575 key_recvoob(struct socket *so, struct mbuf *m, int flags) 576 { 577 KASSERT(solocked(so)); 578 579 return EOPNOTSUPP; 580 } 581 582 static int 583 key_send(struct socket *so, struct mbuf *m, struct sockaddr *nam, 584 struct mbuf *control, struct lwp *l) 585 { 586 int error = 0; 587 int s; 588 589 KASSERT(solocked(so)); 590 KASSERT(so->so_proto == &keysw[0]); 591 592 s = splsoftnet(); 593 error = raw_send(so, m, nam, control, l, &key_output); 594 splx(s); 595 596 return error; 597 } 598 599 static int 600 key_sendoob(struct socket *so, struct mbuf *m, struct mbuf *control) 601 { 602 KASSERT(solocked(so)); 603 604 m_freem(m); 605 m_freem(control); 606 607 return EOPNOTSUPP; 608 } 609 610 static int 611 key_purgeif(struct socket *so, struct ifnet *ifa) 612 { 613 614 panic("%s: unsupported", __func__); 615 616 return EOPNOTSUPP; 617 } 618 619 /* 620 * Definitions of protocols supported in the KEY domain. 621 */ 622 623 DOMAIN_DEFINE(keydomain); 624 625 PR_WRAP_USRREQS(key) 626 #define key_attach key_attach_wrapper 627 #define key_detach key_detach_wrapper 628 #define key_accept key_accept_wrapper 629 #define key_bind key_bind_wrapper 630 #define key_listen key_listen_wrapper 631 #define key_connect key_connect_wrapper 632 #define key_connect2 key_connect2_wrapper 633 #define key_disconnect key_disconnect_wrapper 634 #define key_shutdown key_shutdown_wrapper 635 #define key_abort key_abort_wrapper 636 #define key_ioctl key_ioctl_wrapper 637 #define key_stat key_stat_wrapper 638 #define key_peeraddr key_peeraddr_wrapper 639 #define key_sockaddr key_sockaddr_wrapper 640 #define key_rcvd key_rcvd_wrapper 641 #define key_recvoob key_recvoob_wrapper 642 #define key_send key_send_wrapper 643 #define key_sendoob key_sendoob_wrapper 644 #define key_purgeif key_purgeif_wrapper 645 646 static const struct pr_usrreqs key_usrreqs = { 647 .pr_attach = key_attach, 648 .pr_detach = key_detach, 649 .pr_accept = key_accept, 650 .pr_bind = key_bind, 651 .pr_listen = key_listen, 652 .pr_connect = key_connect, 653 .pr_connect2 = key_connect2, 654 .pr_disconnect = key_disconnect, 655 .pr_shutdown = key_shutdown, 656 .pr_abort = key_abort, 657 .pr_ioctl = key_ioctl, 658 .pr_stat = key_stat, 659 .pr_peeraddr = key_peeraddr, 660 .pr_sockaddr = key_sockaddr, 661 .pr_rcvd = key_rcvd, 662 .pr_recvoob = key_recvoob, 663 .pr_send = key_send, 664 .pr_sendoob = key_sendoob, 665 .pr_purgeif = key_purgeif, 666 }; 667 668 static const struct protosw keysw[] = { 669 { 670 .pr_type = SOCK_RAW, 671 .pr_domain = &keydomain, 672 .pr_protocol = PF_KEY_V2, 673 .pr_flags = PR_ATOMIC|PR_ADDR, 674 .pr_ctlinput = raw_ctlinput, 675 .pr_usrreqs = &key_usrreqs, 676 .pr_init = key_pr_init, 677 } 678 }; 679 680 struct domain keydomain = { 681 .dom_family = PF_KEY, 682 .dom_name = "key", 683 .dom_init = key_init, 684 .dom_protosw = keysw, 685 .dom_protoswNPROTOSW = &keysw[__arraycount(keysw)], 686 }; 687