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