1 /* $FreeBSD$ */ 2 /* $KAME: keysock.c,v 1.25 2001/08/13 20:07:41 itojun Exp $ */ 3 4 /*- 5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the project nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #include "opt_ipsec.h" 34 35 /* This code has derived from sys/net/rtsock.c on FreeBSD2.2.5 */ 36 37 #include <sys/types.h> 38 #include <sys/param.h> 39 #include <sys/domain.h> 40 #include <sys/errno.h> 41 #include <sys/kernel.h> 42 #include <sys/lock.h> 43 #include <sys/malloc.h> 44 #include <sys/mbuf.h> 45 #include <sys/mutex.h> 46 #include <sys/priv.h> 47 #include <sys/protosw.h> 48 #include <sys/signalvar.h> 49 #include <sys/socket.h> 50 #include <sys/socketvar.h> 51 #include <sys/sysctl.h> 52 #include <sys/systm.h> 53 54 #include <net/if.h> 55 #include <net/vnet.h> 56 #include <net/raw_cb.h> 57 #include <net/route.h> 58 59 #include <netinet/in.h> 60 61 #include <net/pfkeyv2.h> 62 #include <netipsec/key.h> 63 #include <netipsec/keysock.h> 64 #include <netipsec/key_debug.h> 65 #include <netipsec/ipsec.h> 66 67 #include <machine/stdarg.h> 68 69 struct key_cb { 70 int key_count; 71 int any_count; 72 }; 73 static VNET_DEFINE(struct key_cb, key_cb); 74 #define V_key_cb VNET(key_cb) 75 76 static struct sockaddr key_src = { 2, PF_KEY, }; 77 78 static int key_sendup0 __P((struct rawcb *, struct mbuf *, int)); 79 80 VNET_PCPUSTAT_DEFINE(struct pfkeystat, pfkeystat); 81 VNET_PCPUSTAT_SYSINIT(pfkeystat); 82 83 #ifdef VIMAGE 84 VNET_PCPUSTAT_SYSUNINIT(pfkeystat); 85 #endif /* VIMAGE */ 86 87 /* 88 * key_output() 89 */ 90 int 91 key_output(struct mbuf *m, struct socket *so) 92 { 93 struct sadb_msg *msg; 94 int len, error = 0; 95 96 if (m == 0) 97 panic("%s: NULL pointer was passed.\n", __func__); 98 99 PFKEYSTAT_INC(out_total); 100 PFKEYSTAT_ADD(out_bytes, m->m_pkthdr.len); 101 102 len = m->m_pkthdr.len; 103 if (len < sizeof(struct sadb_msg)) { 104 PFKEYSTAT_INC(out_tooshort); 105 error = EINVAL; 106 goto end; 107 } 108 109 if (m->m_len < sizeof(struct sadb_msg)) { 110 if ((m = m_pullup(m, sizeof(struct sadb_msg))) == 0) { 111 PFKEYSTAT_INC(out_nomem); 112 error = ENOBUFS; 113 goto end; 114 } 115 } 116 117 M_ASSERTPKTHDR(m); 118 119 KEYDEBUG(KEYDEBUG_KEY_DUMP, kdebug_mbuf(m)); 120 121 msg = mtod(m, struct sadb_msg *); 122 PFKEYSTAT_INC(out_msgtype[msg->sadb_msg_type]); 123 if (len != PFKEY_UNUNIT64(msg->sadb_msg_len)) { 124 PFKEYSTAT_INC(out_invlen); 125 error = EINVAL; 126 goto end; 127 } 128 129 error = key_parse(m, so); 130 m = NULL; 131 end: 132 if (m) 133 m_freem(m); 134 return error; 135 } 136 137 /* 138 * send message to the socket. 139 */ 140 static int 141 key_sendup0(rp, m, promisc) 142 struct rawcb *rp; 143 struct mbuf *m; 144 int promisc; 145 { 146 int error; 147 148 if (promisc) { 149 struct sadb_msg *pmsg; 150 151 M_PREPEND(m, sizeof(struct sadb_msg), M_NOWAIT); 152 if (m && m->m_len < sizeof(struct sadb_msg)) 153 m = m_pullup(m, sizeof(struct sadb_msg)); 154 if (!m) { 155 PFKEYSTAT_INC(in_nomem); 156 m_freem(m); 157 return ENOBUFS; 158 } 159 m->m_pkthdr.len += sizeof(*pmsg); 160 161 pmsg = mtod(m, struct sadb_msg *); 162 bzero(pmsg, sizeof(*pmsg)); 163 pmsg->sadb_msg_version = PF_KEY_V2; 164 pmsg->sadb_msg_type = SADB_X_PROMISC; 165 pmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len); 166 /* pid and seq? */ 167 168 PFKEYSTAT_INC(in_msgtype[pmsg->sadb_msg_type]); 169 } 170 171 if (!sbappendaddr(&rp->rcb_socket->so_rcv, (struct sockaddr *)&key_src, 172 m, NULL)) { 173 PFKEYSTAT_INC(in_nomem); 174 m_freem(m); 175 error = ENOBUFS; 176 } else 177 error = 0; 178 sorwakeup(rp->rcb_socket); 179 return error; 180 } 181 182 /* XXX this interface should be obsoleted. */ 183 int 184 key_sendup(so, msg, len, target) 185 struct socket *so; 186 struct sadb_msg *msg; 187 u_int len; 188 int target; /*target of the resulting message*/ 189 { 190 struct mbuf *m, *n, *mprev; 191 int tlen; 192 193 /* sanity check */ 194 if (so == 0 || msg == 0) 195 panic("%s: NULL pointer was passed.\n", __func__); 196 197 KEYDEBUG(KEYDEBUG_KEY_DUMP, 198 printf("%s: \n", __func__); 199 kdebug_sadb(msg)); 200 201 /* 202 * we increment statistics here, just in case we have ENOBUFS 203 * in this function. 204 */ 205 PFKEYSTAT_INC(in_total); 206 PFKEYSTAT_ADD(in_bytes, len); 207 PFKEYSTAT_INC(in_msgtype[msg->sadb_msg_type]); 208 209 /* 210 * Get mbuf chain whenever possible (not clusters), 211 * to save socket buffer. We'll be generating many SADB_ACQUIRE 212 * messages to listening key sockets. If we simply allocate clusters, 213 * sbappendaddr() will raise ENOBUFS due to too little sbspace(). 214 * sbspace() computes # of actual data bytes AND mbuf region. 215 * 216 * TODO: SADB_ACQUIRE filters should be implemented. 217 */ 218 tlen = len; 219 m = mprev = NULL; 220 while (tlen > 0) { 221 if (tlen == len) { 222 MGETHDR(n, M_NOWAIT, MT_DATA); 223 if (n == NULL) { 224 PFKEYSTAT_INC(in_nomem); 225 return ENOBUFS; 226 } 227 n->m_len = MHLEN; 228 } else { 229 MGET(n, M_NOWAIT, MT_DATA); 230 if (n == NULL) { 231 PFKEYSTAT_INC(in_nomem); 232 return ENOBUFS; 233 } 234 n->m_len = MLEN; 235 } 236 if (tlen >= MCLBYTES) { /*XXX better threshold? */ 237 MCLGET(n, M_NOWAIT); 238 if ((n->m_flags & M_EXT) == 0) { 239 m_free(n); 240 m_freem(m); 241 PFKEYSTAT_INC(in_nomem); 242 return ENOBUFS; 243 } 244 n->m_len = MCLBYTES; 245 } 246 247 if (tlen < n->m_len) 248 n->m_len = tlen; 249 n->m_next = NULL; 250 if (m == NULL) 251 m = mprev = n; 252 else { 253 mprev->m_next = n; 254 mprev = n; 255 } 256 tlen -= n->m_len; 257 n = NULL; 258 } 259 m->m_pkthdr.len = len; 260 m->m_pkthdr.rcvif = NULL; 261 m_copyback(m, 0, len, (caddr_t)msg); 262 263 /* avoid duplicated statistics */ 264 PFKEYSTAT_ADD(in_total, -1); 265 PFKEYSTAT_ADD(in_bytes, -len); 266 PFKEYSTAT_ADD(in_msgtype[msg->sadb_msg_type], -1); 267 268 return key_sendup_mbuf(so, m, target); 269 } 270 271 /* so can be NULL if target != KEY_SENDUP_ONE */ 272 int 273 key_sendup_mbuf(so, m, target) 274 struct socket *so; 275 struct mbuf *m; 276 int target; 277 { 278 struct mbuf *n; 279 struct keycb *kp; 280 int sendup; 281 struct rawcb *rp; 282 int error = 0; 283 284 if (m == NULL) 285 panic("key_sendup_mbuf: NULL pointer was passed.\n"); 286 if (so == NULL && target == KEY_SENDUP_ONE) 287 panic("%s: NULL pointer was passed.\n", __func__); 288 289 PFKEYSTAT_INC(in_total); 290 PFKEYSTAT_ADD(in_bytes, m->m_pkthdr.len); 291 if (m->m_len < sizeof(struct sadb_msg)) { 292 m = m_pullup(m, sizeof(struct sadb_msg)); 293 if (m == NULL) { 294 PFKEYSTAT_INC(in_nomem); 295 return ENOBUFS; 296 } 297 } 298 if (m->m_len >= sizeof(struct sadb_msg)) { 299 struct sadb_msg *msg; 300 msg = mtod(m, struct sadb_msg *); 301 PFKEYSTAT_INC(in_msgtype[msg->sadb_msg_type]); 302 } 303 mtx_lock(&rawcb_mtx); 304 LIST_FOREACH(rp, &V_rawcb_list, list) 305 { 306 if (rp->rcb_proto.sp_family != PF_KEY) 307 continue; 308 if (rp->rcb_proto.sp_protocol 309 && rp->rcb_proto.sp_protocol != PF_KEY_V2) { 310 continue; 311 } 312 313 kp = (struct keycb *)rp; 314 315 /* 316 * If you are in promiscuous mode, and when you get broadcasted 317 * reply, you'll get two PF_KEY messages. 318 * (based on pf_key@inner.net message on 14 Oct 1998) 319 */ 320 if (((struct keycb *)rp)->kp_promisc) { 321 if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) { 322 (void)key_sendup0(rp, n, 1); 323 n = NULL; 324 } 325 } 326 327 /* the exact target will be processed later */ 328 if (so && sotorawcb(so) == rp) 329 continue; 330 331 sendup = 0; 332 switch (target) { 333 case KEY_SENDUP_ONE: 334 /* the statement has no effect */ 335 if (so && sotorawcb(so) == rp) 336 sendup++; 337 break; 338 case KEY_SENDUP_ALL: 339 sendup++; 340 break; 341 case KEY_SENDUP_REGISTERED: 342 if (kp->kp_registered) 343 sendup++; 344 break; 345 } 346 PFKEYSTAT_INC(in_msgtarget[target]); 347 348 if (!sendup) 349 continue; 350 351 if ((n = m_copy(m, 0, (int)M_COPYALL)) == NULL) { 352 m_freem(m); 353 PFKEYSTAT_INC(in_nomem); 354 mtx_unlock(&rawcb_mtx); 355 return ENOBUFS; 356 } 357 358 if ((error = key_sendup0(rp, n, 0)) != 0) { 359 m_freem(m); 360 mtx_unlock(&rawcb_mtx); 361 return error; 362 } 363 364 n = NULL; 365 } 366 367 if (so) { 368 error = key_sendup0(sotorawcb(so), m, 0); 369 m = NULL; 370 } else { 371 error = 0; 372 m_freem(m); 373 } 374 mtx_unlock(&rawcb_mtx); 375 return error; 376 } 377 378 /* 379 * key_abort() 380 * derived from net/rtsock.c:rts_abort() 381 */ 382 static void 383 key_abort(struct socket *so) 384 { 385 raw_usrreqs.pru_abort(so); 386 } 387 388 /* 389 * key_attach() 390 * derived from net/rtsock.c:rts_attach() 391 */ 392 static int 393 key_attach(struct socket *so, int proto, struct thread *td) 394 { 395 struct keycb *kp; 396 int error; 397 398 KASSERT(so->so_pcb == NULL, ("key_attach: so_pcb != NULL")); 399 400 if (td != NULL) { 401 error = priv_check(td, PRIV_NET_RAW); 402 if (error) 403 return error; 404 } 405 406 /* XXX */ 407 kp = malloc(sizeof *kp, M_PCB, M_WAITOK | M_ZERO); 408 if (kp == 0) 409 return ENOBUFS; 410 411 so->so_pcb = (caddr_t)kp; 412 error = raw_attach(so, proto); 413 kp = (struct keycb *)sotorawcb(so); 414 if (error) { 415 free(kp, M_PCB); 416 so->so_pcb = (caddr_t) 0; 417 return error; 418 } 419 420 kp->kp_promisc = kp->kp_registered = 0; 421 422 if (kp->kp_raw.rcb_proto.sp_protocol == PF_KEY) /* XXX: AF_KEY */ 423 V_key_cb.key_count++; 424 V_key_cb.any_count++; 425 soisconnected(so); 426 so->so_options |= SO_USELOOPBACK; 427 428 return 0; 429 } 430 431 /* 432 * key_bind() 433 * derived from net/rtsock.c:rts_bind() 434 */ 435 static int 436 key_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 437 { 438 return EINVAL; 439 } 440 441 /* 442 * key_close() 443 * derived from net/rtsock.c:rts_close(). 444 */ 445 static void 446 key_close(struct socket *so) 447 { 448 449 raw_usrreqs.pru_close(so); 450 } 451 452 /* 453 * key_connect() 454 * derived from net/rtsock.c:rts_connect() 455 */ 456 static int 457 key_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 458 { 459 return EINVAL; 460 } 461 462 /* 463 * key_detach() 464 * derived from net/rtsock.c:rts_detach() 465 */ 466 static void 467 key_detach(struct socket *so) 468 { 469 struct keycb *kp = (struct keycb *)sotorawcb(so); 470 471 KASSERT(kp != NULL, ("key_detach: kp == NULL")); 472 if (kp->kp_raw.rcb_proto.sp_protocol 473 == PF_KEY) /* XXX: AF_KEY */ 474 V_key_cb.key_count--; 475 V_key_cb.any_count--; 476 477 key_freereg(so); 478 raw_usrreqs.pru_detach(so); 479 } 480 481 /* 482 * key_disconnect() 483 * derived from net/rtsock.c:key_disconnect() 484 */ 485 static int 486 key_disconnect(struct socket *so) 487 { 488 return(raw_usrreqs.pru_disconnect(so)); 489 } 490 491 /* 492 * key_peeraddr() 493 * derived from net/rtsock.c:rts_peeraddr() 494 */ 495 static int 496 key_peeraddr(struct socket *so, struct sockaddr **nam) 497 { 498 return(raw_usrreqs.pru_peeraddr(so, nam)); 499 } 500 501 /* 502 * key_send() 503 * derived from net/rtsock.c:rts_send() 504 */ 505 static int 506 key_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 507 struct mbuf *control, struct thread *td) 508 { 509 return(raw_usrreqs.pru_send(so, flags, m, nam, control, td)); 510 } 511 512 /* 513 * key_shutdown() 514 * derived from net/rtsock.c:rts_shutdown() 515 */ 516 static int 517 key_shutdown(struct socket *so) 518 { 519 return(raw_usrreqs.pru_shutdown(so)); 520 } 521 522 /* 523 * key_sockaddr() 524 * derived from net/rtsock.c:rts_sockaddr() 525 */ 526 static int 527 key_sockaddr(struct socket *so, struct sockaddr **nam) 528 { 529 return(raw_usrreqs.pru_sockaddr(so, nam)); 530 } 531 532 struct pr_usrreqs key_usrreqs = { 533 .pru_abort = key_abort, 534 .pru_attach = key_attach, 535 .pru_bind = key_bind, 536 .pru_connect = key_connect, 537 .pru_detach = key_detach, 538 .pru_disconnect = key_disconnect, 539 .pru_peeraddr = key_peeraddr, 540 .pru_send = key_send, 541 .pru_shutdown = key_shutdown, 542 .pru_sockaddr = key_sockaddr, 543 .pru_close = key_close, 544 }; 545 546 /* sysctl */ 547 SYSCTL_NODE(_net, PF_KEY, key, CTLFLAG_RW, 0, "Key Family"); 548 549 /* 550 * Definitions of protocols supported in the KEY domain. 551 */ 552 553 extern struct domain keydomain; 554 555 struct protosw keysw[] = { 556 { 557 .pr_type = SOCK_RAW, 558 .pr_domain = &keydomain, 559 .pr_protocol = PF_KEY_V2, 560 .pr_flags = PR_ATOMIC|PR_ADDR, 561 .pr_output = key_output, 562 .pr_ctlinput = raw_ctlinput, 563 .pr_init = raw_init, 564 .pr_usrreqs = &key_usrreqs 565 } 566 }; 567 568 static void 569 key_init0(void) 570 { 571 572 bzero((caddr_t)&V_key_cb, sizeof(V_key_cb)); 573 key_init(); 574 } 575 576 struct domain keydomain = { 577 .dom_family = PF_KEY, 578 .dom_name = "key", 579 .dom_init = key_init0, 580 #ifdef VIMAGE 581 .dom_destroy = key_destroy, 582 #endif 583 .dom_protosw = keysw, 584 .dom_protoswNPROTOSW = &keysw[sizeof(keysw)/sizeof(keysw[0])] 585 }; 586 587 VNET_DOMAIN_SET(key); 588