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>
56 #include <net/route.h>
59 #include <netinet/in.h>
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>
67 #include <machine/stdarg.h>
73 static VNET_DEFINE(struct key_cb, key_cb);
74 #define V_key_cb VNET(key_cb)
76 static struct sockaddr key_src = { 2, PF_KEY, };
78 static int key_sendup0 __P((struct rawcb *, struct mbuf *, int));
80 VNET_DEFINE(struct pfkeystat, pfkeystat);
86 key_output(struct mbuf *m, struct socket *so)
92 panic("%s: NULL pointer was passed.\n", __func__);
94 PFKEYSTAT_INC(out_total);
95 PFKEYSTAT_ADD(out_bytes, m->m_pkthdr.len);
97 len = m->m_pkthdr.len;
98 if (len < sizeof(struct sadb_msg)) {
99 PFKEYSTAT_INC(out_tooshort);
104 if (m->m_len < sizeof(struct sadb_msg)) {
105 if ((m = m_pullup(m, sizeof(struct sadb_msg))) == 0) {
106 PFKEYSTAT_INC(out_nomem);
114 KEYDEBUG(KEYDEBUG_KEY_DUMP, kdebug_mbuf(m));
116 msg = mtod(m, struct sadb_msg *);
117 PFKEYSTAT_INC(out_msgtype[msg->sadb_msg_type]);
118 if (len != PFKEY_UNUNIT64(msg->sadb_msg_len)) {
119 PFKEYSTAT_INC(out_invlen);
124 error = key_parse(m, so);
133 * send message to the socket.
136 key_sendup0(rp, m, promisc)
144 struct sadb_msg *pmsg;
146 M_PREPEND(m, sizeof(struct sadb_msg), M_DONTWAIT);
148 PFKEYSTAT_INC(in_nomem);
151 m->m_pkthdr.len += sizeof(*pmsg); /* XXX: is this correct? */
153 pmsg = mtod(m, struct sadb_msg *);
154 bzero(pmsg, sizeof(*pmsg));
155 pmsg->sadb_msg_version = PF_KEY_V2;
156 pmsg->sadb_msg_type = SADB_X_PROMISC;
157 pmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
160 PFKEYSTAT_INC(in_msgtype[pmsg->sadb_msg_type]);
163 if (!sbappendaddr(&rp->rcb_socket->so_rcv, (struct sockaddr *)&key_src,
165 PFKEYSTAT_INC(in_nomem);
170 sorwakeup(rp->rcb_socket);
174 /* XXX this interface should be obsoleted. */
176 key_sendup(so, msg, len, target)
178 struct sadb_msg *msg;
180 int target; /*target of the resulting message*/
182 struct mbuf *m, *n, *mprev;
186 if (so == 0 || msg == 0)
187 panic("%s: NULL pointer was passed.\n", __func__);
189 KEYDEBUG(KEYDEBUG_KEY_DUMP,
190 printf("%s: \n", __func__);
194 * we increment statistics here, just in case we have ENOBUFS
197 PFKEYSTAT_INC(in_total);
198 PFKEYSTAT_ADD(in_bytes, len);
199 PFKEYSTAT_INC(in_msgtype[msg->sadb_msg_type]);
202 * Get mbuf chain whenever possible (not clusters),
203 * to save socket buffer. We'll be generating many SADB_ACQUIRE
204 * messages to listening key sockets. If we simply allocate clusters,
205 * sbappendaddr() will raise ENOBUFS due to too little sbspace().
206 * sbspace() computes # of actual data bytes AND mbuf region.
208 * TODO: SADB_ACQUIRE filters should be implemented.
214 MGETHDR(n, M_DONTWAIT, MT_DATA);
216 PFKEYSTAT_INC(in_nomem);
221 MGET(n, M_DONTWAIT, MT_DATA);
223 PFKEYSTAT_INC(in_nomem);
228 if (tlen >= MCLBYTES) { /*XXX better threshold? */
229 MCLGET(n, M_DONTWAIT);
230 if ((n->m_flags & M_EXT) == 0) {
233 PFKEYSTAT_INC(in_nomem);
251 m->m_pkthdr.len = len;
252 m->m_pkthdr.rcvif = NULL;
253 m_copyback(m, 0, len, (caddr_t)msg);
255 /* avoid duplicated statistics */
256 PFKEYSTAT_ADD(in_total, -1);
257 PFKEYSTAT_ADD(in_bytes, -len);
258 PFKEYSTAT_ADD(in_msgtype[msg->sadb_msg_type], -1);
260 return key_sendup_mbuf(so, m, target);
263 /* so can be NULL if target != KEY_SENDUP_ONE */
265 key_sendup_mbuf(so, m, target)
277 panic("key_sendup_mbuf: NULL pointer was passed.\n");
278 if (so == NULL && target == KEY_SENDUP_ONE)
279 panic("%s: NULL pointer was passed.\n", __func__);
281 PFKEYSTAT_INC(in_total);
282 PFKEYSTAT_ADD(in_bytes, m->m_pkthdr.len);
283 if (m->m_len < sizeof(struct sadb_msg)) {
284 m = m_pullup(m, sizeof(struct sadb_msg));
286 PFKEYSTAT_INC(in_nomem);
290 if (m->m_len >= sizeof(struct sadb_msg)) {
291 struct sadb_msg *msg;
292 msg = mtod(m, struct sadb_msg *);
293 PFKEYSTAT_INC(in_msgtype[msg->sadb_msg_type]);
295 mtx_lock(&rawcb_mtx);
296 LIST_FOREACH(rp, &V_rawcb_list, list)
298 if (rp->rcb_proto.sp_family != PF_KEY)
300 if (rp->rcb_proto.sp_protocol
301 && rp->rcb_proto.sp_protocol != PF_KEY_V2) {
305 kp = (struct keycb *)rp;
308 * If you are in promiscuous mode, and when you get broadcasted
309 * reply, you'll get two PF_KEY messages.
310 * (based on pf_key@inner.net message on 14 Oct 1998)
312 if (((struct keycb *)rp)->kp_promisc) {
313 if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
314 (void)key_sendup0(rp, n, 1);
319 /* the exact target will be processed later */
320 if (so && sotorawcb(so) == rp)
326 /* the statement has no effect */
327 if (so && sotorawcb(so) == rp)
333 case KEY_SENDUP_REGISTERED:
334 if (kp->kp_registered)
338 PFKEYSTAT_INC(in_msgtarget[target]);
343 if ((n = m_copy(m, 0, (int)M_COPYALL)) == NULL) {
345 PFKEYSTAT_INC(in_nomem);
346 mtx_unlock(&rawcb_mtx);
350 if ((error = key_sendup0(rp, n, 0)) != 0) {
352 mtx_unlock(&rawcb_mtx);
360 error = key_sendup0(sotorawcb(so), m, 0);
366 mtx_unlock(&rawcb_mtx);
372 * derived from net/rtsock.c:rts_abort()
375 key_abort(struct socket *so)
377 raw_usrreqs.pru_abort(so);
382 * derived from net/rtsock.c:rts_attach()
385 key_attach(struct socket *so, int proto, struct thread *td)
390 KASSERT(so->so_pcb == NULL, ("key_attach: so_pcb != NULL"));
393 error = priv_check(td, PRIV_NET_RAW);
399 kp = malloc(sizeof *kp, M_PCB, M_WAITOK | M_ZERO);
403 so->so_pcb = (caddr_t)kp;
404 error = raw_attach(so, proto);
405 kp = (struct keycb *)sotorawcb(so);
408 so->so_pcb = (caddr_t) 0;
412 kp->kp_promisc = kp->kp_registered = 0;
414 if (kp->kp_raw.rcb_proto.sp_protocol == PF_KEY) /* XXX: AF_KEY */
415 V_key_cb.key_count++;
416 V_key_cb.any_count++;
418 so->so_options |= SO_USELOOPBACK;
425 * derived from net/rtsock.c:rts_bind()
428 key_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
435 * derived from net/rtsock.c:rts_close().
438 key_close(struct socket *so)
441 raw_usrreqs.pru_close(so);
446 * derived from net/rtsock.c:rts_connect()
449 key_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
456 * derived from net/rtsock.c:rts_detach()
459 key_detach(struct socket *so)
461 struct keycb *kp = (struct keycb *)sotorawcb(so);
463 KASSERT(kp != NULL, ("key_detach: kp == NULL"));
464 if (kp->kp_raw.rcb_proto.sp_protocol
465 == PF_KEY) /* XXX: AF_KEY */
466 V_key_cb.key_count--;
467 V_key_cb.any_count--;
470 raw_usrreqs.pru_detach(so);
475 * derived from net/rtsock.c:key_disconnect()
478 key_disconnect(struct socket *so)
480 return(raw_usrreqs.pru_disconnect(so));
485 * derived from net/rtsock.c:rts_peeraddr()
488 key_peeraddr(struct socket *so, struct sockaddr **nam)
490 return(raw_usrreqs.pru_peeraddr(so, nam));
495 * derived from net/rtsock.c:rts_send()
498 key_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
499 struct mbuf *control, struct thread *td)
501 return(raw_usrreqs.pru_send(so, flags, m, nam, control, td));
506 * derived from net/rtsock.c:rts_shutdown()
509 key_shutdown(struct socket *so)
511 return(raw_usrreqs.pru_shutdown(so));
516 * derived from net/rtsock.c:rts_sockaddr()
519 key_sockaddr(struct socket *so, struct sockaddr **nam)
521 return(raw_usrreqs.pru_sockaddr(so, nam));
524 struct pr_usrreqs key_usrreqs = {
525 .pru_abort = key_abort,
526 .pru_attach = key_attach,
527 .pru_bind = key_bind,
528 .pru_connect = key_connect,
529 .pru_detach = key_detach,
530 .pru_disconnect = key_disconnect,
531 .pru_peeraddr = key_peeraddr,
532 .pru_send = key_send,
533 .pru_shutdown = key_shutdown,
534 .pru_sockaddr = key_sockaddr,
535 .pru_close = key_close,
539 SYSCTL_NODE(_net, PF_KEY, key, CTLFLAG_RW, 0, "Key Family");
542 * Definitions of protocols supported in the KEY domain.
545 extern struct domain keydomain;
547 struct protosw keysw[] = {
550 .pr_domain = &keydomain,
551 .pr_protocol = PF_KEY_V2,
552 .pr_flags = PR_ATOMIC|PR_ADDR,
553 .pr_output = key_output,
554 .pr_ctlinput = raw_ctlinput,
556 .pr_usrreqs = &key_usrreqs
564 bzero((caddr_t)&V_key_cb, sizeof(V_key_cb));
568 struct domain keydomain = {
569 .dom_family = PF_KEY,
571 .dom_init = key_init0,
573 .dom_destroy = key_destroy,
575 .dom_protosw = keysw,
576 .dom_protoswNPROTOSW = &keysw[sizeof(keysw)/sizeof(keysw[0])]
579 VNET_DOMAIN_SET(key);