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