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 #include <sys/vimage.h> 54 55 #include <net/if.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 struct key_cb key_cb; 74 75 static struct sockaddr key_src = { 2, PF_KEY, }; 76 77 static int key_sendup0 __P((struct rawcb *, struct mbuf *, int)); 78 79 struct pfkeystat pfkeystat; 80 81 /* 82 * key_output() 83 */ 84 int 85 key_output(struct mbuf *m, struct socket *so) 86 { 87 INIT_VNET_IPSEC(curvnet); 88 struct sadb_msg *msg; 89 int len, error = 0; 90 91 if (m == 0) 92 panic("%s: NULL pointer was passed.\n", __func__); 93 94 V_pfkeystat.out_total++; 95 V_pfkeystat.out_bytes += m->m_pkthdr.len; 96 97 len = m->m_pkthdr.len; 98 if (len < sizeof(struct sadb_msg)) { 99 V_pfkeystat.out_tooshort++; 100 error = EINVAL; 101 goto end; 102 } 103 104 if (m->m_len < sizeof(struct sadb_msg)) { 105 if ((m = m_pullup(m, sizeof(struct sadb_msg))) == 0) { 106 V_pfkeystat.out_nomem++; 107 error = ENOBUFS; 108 goto end; 109 } 110 } 111 112 M_ASSERTPKTHDR(m); 113 114 KEYDEBUG(KEYDEBUG_KEY_DUMP, kdebug_mbuf(m)); 115 116 msg = mtod(m, struct sadb_msg *); 117 V_pfkeystat.out_msgtype[msg->sadb_msg_type]++; 118 if (len != PFKEY_UNUNIT64(msg->sadb_msg_len)) { 119 V_pfkeystat.out_invlen++; 120 error = EINVAL; 121 goto end; 122 } 123 124 error = key_parse(m, so); 125 m = NULL; 126 end: 127 if (m) 128 m_freem(m); 129 return error; 130 } 131 132 /* 133 * send message to the socket. 134 */ 135 static int 136 key_sendup0(rp, m, promisc) 137 struct rawcb *rp; 138 struct mbuf *m; 139 int promisc; 140 { 141 INIT_VNET_IPSEC(curvnet); 142 int error; 143 144 if (promisc) { 145 struct sadb_msg *pmsg; 146 147 M_PREPEND(m, sizeof(struct sadb_msg), M_DONTWAIT); 148 if (m && m->m_len < sizeof(struct sadb_msg)) 149 m = m_pullup(m, sizeof(struct sadb_msg)); 150 if (!m) { 151 V_pfkeystat.in_nomem++; 152 m_freem(m); 153 return ENOBUFS; 154 } 155 m->m_pkthdr.len += sizeof(*pmsg); 156 157 pmsg = mtod(m, struct sadb_msg *); 158 bzero(pmsg, sizeof(*pmsg)); 159 pmsg->sadb_msg_version = PF_KEY_V2; 160 pmsg->sadb_msg_type = SADB_X_PROMISC; 161 pmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len); 162 /* pid and seq? */ 163 164 V_pfkeystat.in_msgtype[pmsg->sadb_msg_type]++; 165 } 166 167 if (!sbappendaddr(&rp->rcb_socket->so_rcv, (struct sockaddr *)&V_key_src, 168 m, NULL)) { 169 V_pfkeystat.in_nomem++; 170 m_freem(m); 171 error = ENOBUFS; 172 } else 173 error = 0; 174 sorwakeup(rp->rcb_socket); 175 return error; 176 } 177 178 /* XXX this interface should be obsoleted. */ 179 int 180 key_sendup(so, msg, len, target) 181 struct socket *so; 182 struct sadb_msg *msg; 183 u_int len; 184 int target; /*target of the resulting message*/ 185 { 186 INIT_VNET_IPSEC(curvnet); 187 struct mbuf *m, *n, *mprev; 188 int tlen; 189 190 /* sanity check */ 191 if (so == 0 || msg == 0) 192 panic("%s: NULL pointer was passed.\n", __func__); 193 194 KEYDEBUG(KEYDEBUG_KEY_DUMP, 195 printf("%s: \n", __func__); 196 kdebug_sadb(msg)); 197 198 /* 199 * we increment statistics here, just in case we have ENOBUFS 200 * in this function. 201 */ 202 V_pfkeystat.in_total++; 203 V_pfkeystat.in_bytes += len; 204 V_pfkeystat.in_msgtype[msg->sadb_msg_type]++; 205 206 /* 207 * Get mbuf chain whenever possible (not clusters), 208 * to save socket buffer. We'll be generating many SADB_ACQUIRE 209 * messages to listening key sockets. If we simply allocate clusters, 210 * sbappendaddr() will raise ENOBUFS due to too little sbspace(). 211 * sbspace() computes # of actual data bytes AND mbuf region. 212 * 213 * TODO: SADB_ACQUIRE filters should be implemented. 214 */ 215 tlen = len; 216 m = mprev = NULL; 217 while (tlen > 0) { 218 if (tlen == len) { 219 MGETHDR(n, M_DONTWAIT, MT_DATA); 220 if (n == NULL) { 221 V_pfkeystat.in_nomem++; 222 return ENOBUFS; 223 } 224 n->m_len = MHLEN; 225 } else { 226 MGET(n, M_DONTWAIT, MT_DATA); 227 if (n == NULL) { 228 V_pfkeystat.in_nomem++; 229 return ENOBUFS; 230 } 231 n->m_len = MLEN; 232 } 233 if (tlen >= MCLBYTES) { /*XXX better threshold? */ 234 MCLGET(n, M_DONTWAIT); 235 if ((n->m_flags & M_EXT) == 0) { 236 m_free(n); 237 m_freem(m); 238 V_pfkeystat.in_nomem++; 239 return ENOBUFS; 240 } 241 n->m_len = MCLBYTES; 242 } 243 244 if (tlen < n->m_len) 245 n->m_len = tlen; 246 n->m_next = NULL; 247 if (m == NULL) 248 m = mprev = n; 249 else { 250 mprev->m_next = n; 251 mprev = n; 252 } 253 tlen -= n->m_len; 254 n = NULL; 255 } 256 m->m_pkthdr.len = len; 257 m->m_pkthdr.rcvif = NULL; 258 m_copyback(m, 0, len, (caddr_t)msg); 259 260 /* avoid duplicated statistics */ 261 V_pfkeystat.in_total--; 262 V_pfkeystat.in_bytes -= len; 263 V_pfkeystat.in_msgtype[msg->sadb_msg_type]--; 264 265 return key_sendup_mbuf(so, m, target); 266 } 267 268 /* so can be NULL if target != KEY_SENDUP_ONE */ 269 int 270 key_sendup_mbuf(so, m, target) 271 struct socket *so; 272 struct mbuf *m; 273 int target; 274 { 275 INIT_VNET_NET(curvnet); 276 INIT_VNET_IPSEC(curvnet); 277 struct mbuf *n; 278 struct keycb *kp; 279 int sendup; 280 struct rawcb *rp; 281 int error = 0; 282 283 if (m == NULL) 284 panic("key_sendup_mbuf: NULL pointer was passed.\n"); 285 if (so == NULL && target == KEY_SENDUP_ONE) 286 panic("%s: NULL pointer was passed.\n", __func__); 287 288 V_pfkeystat.in_total++; 289 V_pfkeystat.in_bytes += m->m_pkthdr.len; 290 if (m->m_len < sizeof(struct sadb_msg)) { 291 m = m_pullup(m, sizeof(struct sadb_msg)); 292 if (m == NULL) { 293 V_pfkeystat.in_nomem++; 294 return ENOBUFS; 295 } 296 } 297 if (m->m_len >= sizeof(struct sadb_msg)) { 298 struct sadb_msg *msg; 299 msg = mtod(m, struct sadb_msg *); 300 V_pfkeystat.in_msgtype[msg->sadb_msg_type]++; 301 } 302 mtx_lock(&rawcb_mtx); 303 LIST_FOREACH(rp, &V_rawcb_list, list) 304 { 305 if (rp->rcb_proto.sp_family != PF_KEY) 306 continue; 307 if (rp->rcb_proto.sp_protocol 308 && rp->rcb_proto.sp_protocol != PF_KEY_V2) { 309 continue; 310 } 311 312 kp = (struct keycb *)rp; 313 314 /* 315 * If you are in promiscuous mode, and when you get broadcasted 316 * reply, you'll get two PF_KEY messages. 317 * (based on pf_key@inner.net message on 14 Oct 1998) 318 */ 319 if (((struct keycb *)rp)->kp_promisc) { 320 if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) { 321 (void)key_sendup0(rp, n, 1); 322 n = NULL; 323 } 324 } 325 326 /* the exact target will be processed later */ 327 if (so && sotorawcb(so) == rp) 328 continue; 329 330 sendup = 0; 331 switch (target) { 332 case KEY_SENDUP_ONE: 333 /* the statement has no effect */ 334 if (so && sotorawcb(so) == rp) 335 sendup++; 336 break; 337 case KEY_SENDUP_ALL: 338 sendup++; 339 break; 340 case KEY_SENDUP_REGISTERED: 341 if (kp->kp_registered) 342 sendup++; 343 break; 344 } 345 V_pfkeystat.in_msgtarget[target]++; 346 347 if (!sendup) 348 continue; 349 350 if ((n = m_copy(m, 0, (int)M_COPYALL)) == NULL) { 351 m_freem(m); 352 V_pfkeystat.in_nomem++; 353 mtx_unlock(&rawcb_mtx); 354 return ENOBUFS; 355 } 356 357 if ((error = key_sendup0(rp, n, 0)) != 0) { 358 m_freem(m); 359 mtx_unlock(&rawcb_mtx); 360 return error; 361 } 362 363 n = NULL; 364 } 365 366 if (so) { 367 error = key_sendup0(sotorawcb(so), m, 0); 368 m = NULL; 369 } else { 370 error = 0; 371 m_freem(m); 372 } 373 mtx_unlock(&rawcb_mtx); 374 return error; 375 } 376 377 /* 378 * key_abort() 379 * derived from net/rtsock.c:rts_abort() 380 */ 381 static void 382 key_abort(struct socket *so) 383 { 384 raw_usrreqs.pru_abort(so); 385 } 386 387 /* 388 * key_attach() 389 * derived from net/rtsock.c:rts_attach() 390 */ 391 static int 392 key_attach(struct socket *so, int proto, struct thread *td) 393 { 394 INIT_VNET_IPSEC(curvnet); 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 INIT_VNET_IPSEC(curvnet); 470 struct keycb *kp = (struct keycb *)sotorawcb(so); 471 472 KASSERT(kp != NULL, ("key_detach: kp == NULL")); 473 if (kp->kp_raw.rcb_proto.sp_protocol 474 == PF_KEY) /* XXX: AF_KEY */ 475 V_key_cb.key_count--; 476 V_key_cb.any_count--; 477 478 key_freereg(so); 479 raw_usrreqs.pru_detach(so); 480 } 481 482 /* 483 * key_disconnect() 484 * derived from net/rtsock.c:key_disconnect() 485 */ 486 static int 487 key_disconnect(struct socket *so) 488 { 489 return(raw_usrreqs.pru_disconnect(so)); 490 } 491 492 /* 493 * key_peeraddr() 494 * derived from net/rtsock.c:rts_peeraddr() 495 */ 496 static int 497 key_peeraddr(struct socket *so, struct sockaddr **nam) 498 { 499 return(raw_usrreqs.pru_peeraddr(so, nam)); 500 } 501 502 /* 503 * key_send() 504 * derived from net/rtsock.c:rts_send() 505 */ 506 static int 507 key_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 508 struct mbuf *control, struct thread *td) 509 { 510 return(raw_usrreqs.pru_send(so, flags, m, nam, control, td)); 511 } 512 513 /* 514 * key_shutdown() 515 * derived from net/rtsock.c:rts_shutdown() 516 */ 517 static int 518 key_shutdown(struct socket *so) 519 { 520 return(raw_usrreqs.pru_shutdown(so)); 521 } 522 523 /* 524 * key_sockaddr() 525 * derived from net/rtsock.c:rts_sockaddr() 526 */ 527 static int 528 key_sockaddr(struct socket *so, struct sockaddr **nam) 529 { 530 return(raw_usrreqs.pru_sockaddr(so, nam)); 531 } 532 533 struct pr_usrreqs key_usrreqs = { 534 .pru_abort = key_abort, 535 .pru_attach = key_attach, 536 .pru_bind = key_bind, 537 .pru_connect = key_connect, 538 .pru_detach = key_detach, 539 .pru_disconnect = key_disconnect, 540 .pru_peeraddr = key_peeraddr, 541 .pru_send = key_send, 542 .pru_shutdown = key_shutdown, 543 .pru_sockaddr = key_sockaddr, 544 .pru_close = key_close, 545 }; 546 547 /* sysctl */ 548 SYSCTL_NODE(_net, PF_KEY, key, CTLFLAG_RW, 0, "Key Family"); 549 550 /* 551 * Definitions of protocols supported in the KEY domain. 552 */ 553 554 extern struct domain keydomain; 555 556 struct protosw keysw[] = { 557 { 558 .pr_type = SOCK_RAW, 559 .pr_domain = &keydomain, 560 .pr_protocol = PF_KEY_V2, 561 .pr_flags = PR_ATOMIC|PR_ADDR, 562 .pr_output = key_output, 563 .pr_ctlinput = raw_ctlinput, 564 .pr_init = raw_init, 565 .pr_usrreqs = &key_usrreqs 566 } 567 }; 568 569 static void 570 key_init0(void) 571 { 572 INIT_VNET_IPSEC(curvnet); 573 bzero((caddr_t)&V_key_cb, sizeof(V_key_cb)); 574 key_init(); 575 } 576 577 struct domain keydomain = { 578 .dom_family = PF_KEY, 579 .dom_name = "key", 580 .dom_init = key_init0, 581 .dom_protosw = keysw, 582 .dom_protoswNPROTOSW = &keysw[sizeof(keysw)/sizeof(keysw[0])] 583 }; 584 585 DOMAIN_SET(key); 586