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 KEYDBG(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 /* so can be NULL if target != KEY_SENDUP_ONE */
175 key_sendup_mbuf(struct socket *so, struct mbuf *m, int target)
182 KASSERT(m != NULL, ("NULL mbuf pointer was passed."));
183 KASSERT(so != NULL || target != KEY_SENDUP_ONE,
184 ("NULL socket pointer was passed."));
185 KASSERT(target == KEY_SENDUP_ONE || target == KEY_SENDUP_ALL ||
186 target == KEY_SENDUP_REGISTERED, ("Wrong target %d", target));
188 PFKEYSTAT_INC(in_total);
189 PFKEYSTAT_ADD(in_bytes, m->m_pkthdr.len);
190 if (m->m_len < sizeof(struct sadb_msg)) {
191 m = m_pullup(m, sizeof(struct sadb_msg));
193 PFKEYSTAT_INC(in_nomem);
197 if (m->m_len >= sizeof(struct sadb_msg)) {
198 struct sadb_msg *msg;
199 msg = mtod(m, struct sadb_msg *);
200 PFKEYSTAT_INC(in_msgtype[msg->sadb_msg_type]);
202 mtx_lock(&rawcb_mtx);
203 if (V_key_cb.any_count == 0) {
204 mtx_unlock(&rawcb_mtx);
208 LIST_FOREACH(rp, &V_rawcb_list, list)
210 if (rp->rcb_proto.sp_family != PF_KEY)
212 if (rp->rcb_proto.sp_protocol
213 && rp->rcb_proto.sp_protocol != PF_KEY_V2) {
218 * If you are in promiscuous mode, and when you get broadcasted
219 * reply, you'll get two PF_KEY messages.
220 * (based on pf_key@inner.net message on 14 Oct 1998)
222 kp = (struct keycb *)rp;
223 if (kp->kp_promisc) {
224 n = m_copym(m, 0, M_COPYALL, M_NOWAIT);
226 key_sendup0(rp, n, 1);
228 PFKEYSTAT_INC(in_nomem);
231 /* the exact target will be processed later */
232 if (so && sotorawcb(so) == rp)
235 if (target == KEY_SENDUP_ONE || (
236 target == KEY_SENDUP_REGISTERED && kp->kp_registered == 0))
239 /* KEY_SENDUP_ALL + KEY_SENDUP_REGISTERED */
240 n = m_copym(m, 0, M_COPYALL, M_NOWAIT);
242 PFKEYSTAT_INC(in_nomem);
243 /* Try send to another socket */
247 if (key_sendup0(rp, n, 0) == 0)
248 PFKEYSTAT_INC(in_msgtarget[target]);
251 if (so) { /* KEY_SENDUP_ONE */
252 error = key_sendup0(sotorawcb(so), m, 0);
254 PFKEYSTAT_INC(in_msgtarget[KEY_SENDUP_ONE]);
259 mtx_unlock(&rawcb_mtx);
265 * derived from net/rtsock.c:rts_abort()
268 key_abort(struct socket *so)
270 raw_usrreqs.pru_abort(so);
275 * derived from net/rtsock.c:rts_attach()
278 key_attach(struct socket *so, int proto, struct thread *td)
283 KASSERT(so->so_pcb == NULL, ("key_attach: so_pcb != NULL"));
286 error = priv_check(td, PRIV_NET_RAW);
292 kp = malloc(sizeof *kp, M_PCB, M_WAITOK | M_ZERO);
296 so->so_pcb = (caddr_t)kp;
297 error = raw_attach(so, proto);
298 kp = (struct keycb *)sotorawcb(so);
301 so->so_pcb = (caddr_t) 0;
305 kp->kp_promisc = kp->kp_registered = 0;
307 if (kp->kp_raw.rcb_proto.sp_protocol == PF_KEY) /* XXX: AF_KEY */
308 V_key_cb.key_count++;
309 V_key_cb.any_count++;
311 so->so_options |= SO_USELOOPBACK;
318 * derived from net/rtsock.c:rts_bind()
321 key_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
328 * derived from net/rtsock.c:rts_close().
331 key_close(struct socket *so)
334 raw_usrreqs.pru_close(so);
339 * derived from net/rtsock.c:rts_connect()
342 key_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
349 * derived from net/rtsock.c:rts_detach()
352 key_detach(struct socket *so)
354 struct keycb *kp = (struct keycb *)sotorawcb(so);
356 KASSERT(kp != NULL, ("key_detach: kp == NULL"));
357 if (kp->kp_raw.rcb_proto.sp_protocol
358 == PF_KEY) /* XXX: AF_KEY */
359 V_key_cb.key_count--;
360 V_key_cb.any_count--;
363 raw_usrreqs.pru_detach(so);
368 * derived from net/rtsock.c:key_disconnect()
371 key_disconnect(struct socket *so)
373 return(raw_usrreqs.pru_disconnect(so));
378 * derived from net/rtsock.c:rts_peeraddr()
381 key_peeraddr(struct socket *so, struct sockaddr **nam)
383 return(raw_usrreqs.pru_peeraddr(so, nam));
388 * derived from net/rtsock.c:rts_send()
391 key_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
392 struct mbuf *control, struct thread *td)
394 return(raw_usrreqs.pru_send(so, flags, m, nam, control, td));
399 * derived from net/rtsock.c:rts_shutdown()
402 key_shutdown(struct socket *so)
404 return(raw_usrreqs.pru_shutdown(so));
409 * derived from net/rtsock.c:rts_sockaddr()
412 key_sockaddr(struct socket *so, struct sockaddr **nam)
414 return(raw_usrreqs.pru_sockaddr(so, nam));
417 struct pr_usrreqs key_usrreqs = {
418 .pru_abort = key_abort,
419 .pru_attach = key_attach,
420 .pru_bind = key_bind,
421 .pru_connect = key_connect,
422 .pru_detach = key_detach,
423 .pru_disconnect = key_disconnect,
424 .pru_peeraddr = key_peeraddr,
425 .pru_send = key_send,
426 .pru_shutdown = key_shutdown,
427 .pru_sockaddr = key_sockaddr,
428 .pru_close = key_close,
432 SYSCTL_NODE(_net, PF_KEY, key, CTLFLAG_RW, 0, "Key Family");
435 * Definitions of protocols supported in the KEY domain.
438 extern struct domain keydomain;
440 struct protosw keysw[] = {
443 .pr_domain = &keydomain,
444 .pr_protocol = PF_KEY_V2,
445 .pr_flags = PR_ATOMIC|PR_ADDR,
446 .pr_output = key_output,
447 .pr_ctlinput = raw_ctlinput,
449 .pr_usrreqs = &key_usrreqs
457 bzero((caddr_t)&V_key_cb, sizeof(V_key_cb));
461 struct domain keydomain = {
462 .dom_family = PF_KEY,
464 .dom_init = key_init0,
466 .dom_destroy = key_destroy,
468 .dom_protosw = keysw,
469 .dom_protoswNPROTOSW = &keysw[nitems(keysw)]
472 VNET_DOMAIN_SET(key);