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>
56 #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(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))) == NULL) {
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(struct rawcb *rp, struct mbuf *m, int promisc)
145 struct sadb_msg *pmsg;
147 M_PREPEND(m, sizeof(struct sadb_msg), M_NOWAIT);
149 PFKEYSTAT_INC(in_nomem);
152 pmsg = mtod(m, struct sadb_msg *);
153 bzero(pmsg, sizeof(*pmsg));
154 pmsg->sadb_msg_version = PF_KEY_V2;
155 pmsg->sadb_msg_type = SADB_X_PROMISC;
156 pmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
159 PFKEYSTAT_INC(in_msgtype[pmsg->sadb_msg_type]);
162 if (!sbappendaddr(&rp->rcb_socket->so_rcv, (struct sockaddr *)&key_src,
164 PFKEYSTAT_INC(in_nomem);
169 sorwakeup(rp->rcb_socket);
173 /* XXX this interface should be obsoleted. */
175 key_sendup(struct socket *so, struct sadb_msg *msg, u_int len, int target)
177 struct mbuf *m, *n, *mprev;
181 if (so == NULL || msg == NULL)
182 panic("%s: NULL pointer was passed.\n", __func__);
184 KEYDEBUG(KEYDEBUG_KEY_DUMP,
185 printf("%s: \n", __func__);
189 * we increment statistics here, just in case we have ENOBUFS
192 PFKEYSTAT_INC(in_total);
193 PFKEYSTAT_ADD(in_bytes, len);
194 PFKEYSTAT_INC(in_msgtype[msg->sadb_msg_type]);
197 * Get mbuf chain whenever possible (not clusters),
198 * to save socket buffer. We'll be generating many SADB_ACQUIRE
199 * messages to listening key sockets. If we simply allocate clusters,
200 * sbappendaddr() will raise ENOBUFS due to too little sbspace().
201 * sbspace() computes # of actual data bytes AND mbuf region.
203 * TODO: SADB_ACQUIRE filters should be implemented.
209 MGETHDR(n, M_NOWAIT, MT_DATA);
211 PFKEYSTAT_INC(in_nomem);
216 MGET(n, M_NOWAIT, MT_DATA);
218 PFKEYSTAT_INC(in_nomem);
223 if (tlen >= MCLBYTES) { /*XXX better threshold? */
224 if (!(MCLGET(n, M_NOWAIT))) {
227 PFKEYSTAT_INC(in_nomem);
245 m->m_pkthdr.len = len;
246 m->m_pkthdr.rcvif = NULL;
247 m_copyback(m, 0, len, (caddr_t)msg);
249 /* avoid duplicated statistics */
250 PFKEYSTAT_ADD(in_total, -1);
251 PFKEYSTAT_ADD(in_bytes, -len);
252 PFKEYSTAT_ADD(in_msgtype[msg->sadb_msg_type], -1);
254 return key_sendup_mbuf(so, m, target);
257 /* so can be NULL if target != KEY_SENDUP_ONE */
259 key_sendup_mbuf(struct socket *so, struct mbuf *m, int target)
268 panic("key_sendup_mbuf: NULL pointer was passed.\n");
269 if (so == NULL && target == KEY_SENDUP_ONE)
270 panic("%s: NULL pointer was passed.\n", __func__);
272 PFKEYSTAT_INC(in_total);
273 PFKEYSTAT_ADD(in_bytes, m->m_pkthdr.len);
274 if (m->m_len < sizeof(struct sadb_msg)) {
275 m = m_pullup(m, sizeof(struct sadb_msg));
277 PFKEYSTAT_INC(in_nomem);
281 if (m->m_len >= sizeof(struct sadb_msg)) {
282 struct sadb_msg *msg;
283 msg = mtod(m, struct sadb_msg *);
284 PFKEYSTAT_INC(in_msgtype[msg->sadb_msg_type]);
286 mtx_lock(&rawcb_mtx);
287 LIST_FOREACH(rp, &V_rawcb_list, list)
289 if (rp->rcb_proto.sp_family != PF_KEY)
291 if (rp->rcb_proto.sp_protocol
292 && rp->rcb_proto.sp_protocol != PF_KEY_V2) {
296 kp = (struct keycb *)rp;
299 * If you are in promiscuous mode, and when you get broadcasted
300 * reply, you'll get two PF_KEY messages.
301 * (based on pf_key@inner.net message on 14 Oct 1998)
303 if (((struct keycb *)rp)->kp_promisc) {
304 if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
305 (void)key_sendup0(rp, n, 1);
310 /* the exact target will be processed later */
311 if (so && sotorawcb(so) == rp)
317 /* the statement has no effect */
318 if (so && sotorawcb(so) == rp)
324 case KEY_SENDUP_REGISTERED:
325 if (kp->kp_registered)
329 PFKEYSTAT_INC(in_msgtarget[target]);
334 if ((n = m_copy(m, 0, (int)M_COPYALL)) == NULL) {
336 PFKEYSTAT_INC(in_nomem);
337 mtx_unlock(&rawcb_mtx);
341 if ((error = key_sendup0(rp, n, 0)) != 0) {
343 mtx_unlock(&rawcb_mtx);
351 error = key_sendup0(sotorawcb(so), m, 0);
357 mtx_unlock(&rawcb_mtx);
363 * derived from net/rtsock.c:rts_abort()
366 key_abort(struct socket *so)
368 raw_usrreqs.pru_abort(so);
373 * derived from net/rtsock.c:rts_attach()
376 key_attach(struct socket *so, int proto, struct thread *td)
381 KASSERT(so->so_pcb == NULL, ("key_attach: so_pcb != NULL"));
384 error = priv_check(td, PRIV_NET_RAW);
390 kp = malloc(sizeof *kp, M_PCB, M_WAITOK | M_ZERO);
394 so->so_pcb = (caddr_t)kp;
395 error = raw_attach(so, proto);
396 kp = (struct keycb *)sotorawcb(so);
399 so->so_pcb = (caddr_t) 0;
403 kp->kp_promisc = kp->kp_registered = 0;
405 if (kp->kp_raw.rcb_proto.sp_protocol == PF_KEY) /* XXX: AF_KEY */
406 V_key_cb.key_count++;
407 V_key_cb.any_count++;
409 so->so_options |= SO_USELOOPBACK;
416 * derived from net/rtsock.c:rts_bind()
419 key_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
426 * derived from net/rtsock.c:rts_close().
429 key_close(struct socket *so)
432 raw_usrreqs.pru_close(so);
437 * derived from net/rtsock.c:rts_connect()
440 key_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
447 * derived from net/rtsock.c:rts_detach()
450 key_detach(struct socket *so)
452 struct keycb *kp = (struct keycb *)sotorawcb(so);
454 KASSERT(kp != NULL, ("key_detach: kp == NULL"));
455 if (kp->kp_raw.rcb_proto.sp_protocol
456 == PF_KEY) /* XXX: AF_KEY */
457 V_key_cb.key_count--;
458 V_key_cb.any_count--;
461 raw_usrreqs.pru_detach(so);
466 * derived from net/rtsock.c:key_disconnect()
469 key_disconnect(struct socket *so)
471 return(raw_usrreqs.pru_disconnect(so));
476 * derived from net/rtsock.c:rts_peeraddr()
479 key_peeraddr(struct socket *so, struct sockaddr **nam)
481 return(raw_usrreqs.pru_peeraddr(so, nam));
486 * derived from net/rtsock.c:rts_send()
489 key_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
490 struct mbuf *control, struct thread *td)
492 return(raw_usrreqs.pru_send(so, flags, m, nam, control, td));
497 * derived from net/rtsock.c:rts_shutdown()
500 key_shutdown(struct socket *so)
502 return(raw_usrreqs.pru_shutdown(so));
507 * derived from net/rtsock.c:rts_sockaddr()
510 key_sockaddr(struct socket *so, struct sockaddr **nam)
512 return(raw_usrreqs.pru_sockaddr(so, nam));
515 struct pr_usrreqs key_usrreqs = {
516 .pru_abort = key_abort,
517 .pru_attach = key_attach,
518 .pru_bind = key_bind,
519 .pru_connect = key_connect,
520 .pru_detach = key_detach,
521 .pru_disconnect = key_disconnect,
522 .pru_peeraddr = key_peeraddr,
523 .pru_send = key_send,
524 .pru_shutdown = key_shutdown,
525 .pru_sockaddr = key_sockaddr,
526 .pru_close = key_close,
530 SYSCTL_NODE(_net, PF_KEY, key, CTLFLAG_RW, 0, "Key Family");
533 * Definitions of protocols supported in the KEY domain.
536 extern struct domain keydomain;
538 struct protosw keysw[] = {
541 .pr_domain = &keydomain,
542 .pr_protocol = PF_KEY_V2,
543 .pr_flags = PR_ATOMIC|PR_ADDR,
544 .pr_output = key_output,
545 .pr_ctlinput = raw_ctlinput,
547 .pr_usrreqs = &key_usrreqs
555 bzero((caddr_t)&V_key_cb, sizeof(V_key_cb));
559 struct domain keydomain = {
560 .dom_family = PF_KEY,
562 .dom_init = key_init0,
564 .dom_destroy = key_destroy,
566 .dom_protosw = keysw,
567 .dom_protoswNPROTOSW = &keysw[nitems(keysw)]
570 VNET_DOMAIN_SET(key);