2 /* $KAME: keysock.c,v 1.25 2001/08/13 20:07:41 itojun Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
33 #include "opt_ipsec.h"
35 /* This code has derived from sys/net/rtsock.c on FreeBSD2.2.5 */
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>
43 #include <sys/malloc.h>
45 #include <sys/mutex.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>
55 #include <net/raw_cb.h>
58 #include <netinet/in.h>
60 #include <net/pfkeyv2.h>
61 #include <netipsec/key.h>
62 #include <netipsec/keysock.h>
63 #include <netipsec/key_debug.h>
64 #include <netipsec/ipsec.h>
66 #include <machine/stdarg.h>
72 static VNET_DEFINE(struct key_cb, key_cb);
73 #define V_key_cb VNET(key_cb)
75 static struct sockaddr key_src = { 2, PF_KEY, };
77 static int key_sendup0 __P((struct rawcb *, struct mbuf *, int));
79 VNET_PCPUSTAT_DEFINE(struct pfkeystat, pfkeystat);
80 VNET_PCPUSTAT_SYSINIT(pfkeystat);
83 VNET_PCPUSTAT_SYSUNINIT(pfkeystat);
90 key_output(struct mbuf *m, struct socket *so)
96 panic("%s: NULL pointer was passed.\n", __func__);
98 PFKEYSTAT_INC(out_total);
99 PFKEYSTAT_ADD(out_bytes, m->m_pkthdr.len);
101 len = m->m_pkthdr.len;
102 if (len < sizeof(struct sadb_msg)) {
103 PFKEYSTAT_INC(out_tooshort);
108 if (m->m_len < sizeof(struct sadb_msg)) {
109 if ((m = m_pullup(m, sizeof(struct sadb_msg))) == 0) {
110 PFKEYSTAT_INC(out_nomem);
118 KEYDEBUG(KEYDEBUG_KEY_DUMP, kdebug_mbuf(m));
120 msg = mtod(m, struct sadb_msg *);
121 PFKEYSTAT_INC(out_msgtype[msg->sadb_msg_type]);
122 if (len != PFKEY_UNUNIT64(msg->sadb_msg_len)) {
123 PFKEYSTAT_INC(out_invlen);
128 error = key_parse(m, so);
137 * send message to the socket.
140 key_sendup0(rp, m, promisc)
148 struct sadb_msg *pmsg;
150 M_PREPEND(m, sizeof(struct sadb_msg), M_NOWAIT);
152 PFKEYSTAT_INC(in_nomem);
155 pmsg = mtod(m, struct sadb_msg *);
156 bzero(pmsg, sizeof(*pmsg));
157 pmsg->sadb_msg_version = PF_KEY_V2;
158 pmsg->sadb_msg_type = SADB_X_PROMISC;
159 pmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
162 PFKEYSTAT_INC(in_msgtype[pmsg->sadb_msg_type]);
165 if (!sbappendaddr(&rp->rcb_socket->so_rcv, (struct sockaddr *)&key_src,
167 PFKEYSTAT_INC(in_nomem);
172 sorwakeup(rp->rcb_socket);
176 /* XXX this interface should be obsoleted. */
178 key_sendup(so, msg, len, target)
180 struct sadb_msg *msg;
182 int target; /*target of the resulting message*/
184 struct mbuf *m, *n, *mprev;
188 if (so == 0 || msg == 0)
189 panic("%s: NULL pointer was passed.\n", __func__);
191 KEYDEBUG(KEYDEBUG_KEY_DUMP,
192 printf("%s: \n", __func__);
196 * we increment statistics here, just in case we have ENOBUFS
199 PFKEYSTAT_INC(in_total);
200 PFKEYSTAT_ADD(in_bytes, len);
201 PFKEYSTAT_INC(in_msgtype[msg->sadb_msg_type]);
204 * Get mbuf chain whenever possible (not clusters),
205 * to save socket buffer. We'll be generating many SADB_ACQUIRE
206 * messages to listening key sockets. If we simply allocate clusters,
207 * sbappendaddr() will raise ENOBUFS due to too little sbspace().
208 * sbspace() computes # of actual data bytes AND mbuf region.
210 * TODO: SADB_ACQUIRE filters should be implemented.
216 MGETHDR(n, M_NOWAIT, MT_DATA);
218 PFKEYSTAT_INC(in_nomem);
223 MGET(n, M_NOWAIT, MT_DATA);
225 PFKEYSTAT_INC(in_nomem);
230 if (tlen >= MCLBYTES) { /*XXX better threshold? */
232 if ((n->m_flags & M_EXT) == 0) {
235 PFKEYSTAT_INC(in_nomem);
253 m->m_pkthdr.len = len;
254 m->m_pkthdr.rcvif = NULL;
255 m_copyback(m, 0, len, (caddr_t)msg);
257 /* avoid duplicated statistics */
258 PFKEYSTAT_ADD(in_total, -1);
259 PFKEYSTAT_ADD(in_bytes, -len);
260 PFKEYSTAT_ADD(in_msgtype[msg->sadb_msg_type], -1);
262 return key_sendup_mbuf(so, m, target);
265 /* so can be NULL if target != KEY_SENDUP_ONE */
267 key_sendup_mbuf(so, m, target)
279 panic("key_sendup_mbuf: NULL pointer was passed.\n");
280 if (so == NULL && target == KEY_SENDUP_ONE)
281 panic("%s: NULL pointer was passed.\n", __func__);
283 PFKEYSTAT_INC(in_total);
284 PFKEYSTAT_ADD(in_bytes, m->m_pkthdr.len);
285 if (m->m_len < sizeof(struct sadb_msg)) {
286 m = m_pullup(m, sizeof(struct sadb_msg));
288 PFKEYSTAT_INC(in_nomem);
292 if (m->m_len >= sizeof(struct sadb_msg)) {
293 struct sadb_msg *msg;
294 msg = mtod(m, struct sadb_msg *);
295 PFKEYSTAT_INC(in_msgtype[msg->sadb_msg_type]);
297 mtx_lock(&rawcb_mtx);
298 LIST_FOREACH(rp, &V_rawcb_list, list)
300 if (rp->rcb_proto.sp_family != PF_KEY)
302 if (rp->rcb_proto.sp_protocol
303 && rp->rcb_proto.sp_protocol != PF_KEY_V2) {
307 kp = (struct keycb *)rp;
310 * If you are in promiscuous mode, and when you get broadcasted
311 * reply, you'll get two PF_KEY messages.
312 * (based on pf_key@inner.net message on 14 Oct 1998)
314 if (((struct keycb *)rp)->kp_promisc) {
315 if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
316 (void)key_sendup0(rp, n, 1);
321 /* the exact target will be processed later */
322 if (so && sotorawcb(so) == rp)
328 /* the statement has no effect */
329 if (so && sotorawcb(so) == rp)
335 case KEY_SENDUP_REGISTERED:
336 if (kp->kp_registered)
340 PFKEYSTAT_INC(in_msgtarget[target]);
345 if ((n = m_copy(m, 0, (int)M_COPYALL)) == NULL) {
347 PFKEYSTAT_INC(in_nomem);
348 mtx_unlock(&rawcb_mtx);
352 if ((error = key_sendup0(rp, n, 0)) != 0) {
354 mtx_unlock(&rawcb_mtx);
362 error = key_sendup0(sotorawcb(so), m, 0);
368 mtx_unlock(&rawcb_mtx);
374 * derived from net/rtsock.c:rts_abort()
377 key_abort(struct socket *so)
379 raw_usrreqs.pru_abort(so);
384 * derived from net/rtsock.c:rts_attach()
387 key_attach(struct socket *so, int proto, struct thread *td)
392 KASSERT(so->so_pcb == NULL, ("key_attach: so_pcb != NULL"));
395 error = priv_check(td, PRIV_NET_RAW);
401 kp = malloc(sizeof *kp, M_PCB, M_WAITOK | M_ZERO);
405 so->so_pcb = (caddr_t)kp;
406 error = raw_attach(so, proto);
407 kp = (struct keycb *)sotorawcb(so);
410 so->so_pcb = (caddr_t) 0;
414 kp->kp_promisc = kp->kp_registered = 0;
416 if (kp->kp_raw.rcb_proto.sp_protocol == PF_KEY) /* XXX: AF_KEY */
417 V_key_cb.key_count++;
418 V_key_cb.any_count++;
420 so->so_options |= SO_USELOOPBACK;
427 * derived from net/rtsock.c:rts_bind()
430 key_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
437 * derived from net/rtsock.c:rts_close().
440 key_close(struct socket *so)
443 raw_usrreqs.pru_close(so);
448 * derived from net/rtsock.c:rts_connect()
451 key_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
458 * derived from net/rtsock.c:rts_detach()
461 key_detach(struct socket *so)
463 struct keycb *kp = (struct keycb *)sotorawcb(so);
465 KASSERT(kp != NULL, ("key_detach: kp == NULL"));
466 if (kp->kp_raw.rcb_proto.sp_protocol
467 == PF_KEY) /* XXX: AF_KEY */
468 V_key_cb.key_count--;
469 V_key_cb.any_count--;
472 raw_usrreqs.pru_detach(so);
477 * derived from net/rtsock.c:key_disconnect()
480 key_disconnect(struct socket *so)
482 return(raw_usrreqs.pru_disconnect(so));
487 * derived from net/rtsock.c:rts_peeraddr()
490 key_peeraddr(struct socket *so, struct sockaddr **nam)
492 return(raw_usrreqs.pru_peeraddr(so, nam));
497 * derived from net/rtsock.c:rts_send()
500 key_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
501 struct mbuf *control, struct thread *td)
503 return(raw_usrreqs.pru_send(so, flags, m, nam, control, td));
508 * derived from net/rtsock.c:rts_shutdown()
511 key_shutdown(struct socket *so)
513 return(raw_usrreqs.pru_shutdown(so));
518 * derived from net/rtsock.c:rts_sockaddr()
521 key_sockaddr(struct socket *so, struct sockaddr **nam)
523 return(raw_usrreqs.pru_sockaddr(so, nam));
526 struct pr_usrreqs key_usrreqs = {
527 .pru_abort = key_abort,
528 .pru_attach = key_attach,
529 .pru_bind = key_bind,
530 .pru_connect = key_connect,
531 .pru_detach = key_detach,
532 .pru_disconnect = key_disconnect,
533 .pru_peeraddr = key_peeraddr,
534 .pru_send = key_send,
535 .pru_shutdown = key_shutdown,
536 .pru_sockaddr = key_sockaddr,
537 .pru_close = key_close,
541 SYSCTL_NODE(_net, PF_KEY, key, CTLFLAG_RW, 0, "Key Family");
544 * Definitions of protocols supported in the KEY domain.
547 extern struct domain keydomain;
549 struct protosw keysw[] = {
552 .pr_domain = &keydomain,
553 .pr_protocol = PF_KEY_V2,
554 .pr_flags = PR_ATOMIC|PR_ADDR,
555 .pr_output = key_output,
556 .pr_ctlinput = raw_ctlinput,
558 .pr_usrreqs = &key_usrreqs
566 bzero((caddr_t)&V_key_cb, sizeof(V_key_cb));
570 struct domain keydomain = {
571 .dom_family = PF_KEY,
573 .dom_init = key_init0,
575 .dom_destroy = key_destroy,
577 .dom_protosw = keysw,
578 .dom_protoswNPROTOSW = &keysw[sizeof(keysw)/sizeof(keysw[0])]
581 VNET_DOMAIN_SET(key);