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