2 /* $KAME: keysock.c,v 1.25 2001/08/13 20:07:41 itojun Exp $ */
5 * SPDX-License-Identifier: BSD-3-Clause
7 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the project nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include "opt_ipsec.h"
37 /* This code has derived from sys/net/rtsock.c on FreeBSD2.2.5 */
39 #include <sys/types.h>
40 #include <sys/param.h>
41 #include <sys/domain.h>
42 #include <sys/errno.h>
43 #include <sys/kernel.h>
45 #include <sys/malloc.h>
47 #include <sys/mutex.h>
49 #include <sys/protosw.h>
50 #include <sys/signalvar.h>
51 #include <sys/socket.h>
52 #include <sys/socketvar.h>
53 #include <sys/sysctl.h>
54 #include <sys/systm.h>
58 #include <net/raw_cb.h>
60 #include <netinet/in.h>
62 #include <net/pfkeyv2.h>
63 #include <netipsec/key.h>
64 #include <netipsec/keysock.h>
65 #include <netipsec/key_debug.h>
66 #include <netipsec/ipsec.h>
68 #include <machine/stdarg.h>
74 static VNET_DEFINE(struct key_cb, key_cb);
75 #define V_key_cb VNET(key_cb)
77 static struct sockaddr key_src = { 2, PF_KEY, };
79 static int key_sendup0(struct rawcb *, struct mbuf *, int);
81 VNET_PCPUSTAT_DEFINE(struct pfkeystat, pfkeystat);
82 VNET_PCPUSTAT_SYSINIT(pfkeystat);
85 VNET_PCPUSTAT_SYSUNINIT(pfkeystat);
92 key_output(struct mbuf *m, struct socket *so, ...)
98 panic("%s: NULL pointer was passed.\n", __func__);
100 PFKEYSTAT_INC(out_total);
101 PFKEYSTAT_ADD(out_bytes, m->m_pkthdr.len);
103 len = m->m_pkthdr.len;
104 if (len < sizeof(struct sadb_msg)) {
105 PFKEYSTAT_INC(out_tooshort);
110 if (m->m_len < sizeof(struct sadb_msg)) {
111 if ((m = m_pullup(m, sizeof(struct sadb_msg))) == NULL) {
112 PFKEYSTAT_INC(out_nomem);
120 KEYDBG(KEY_DUMP, kdebug_mbuf(m));
122 msg = mtod(m, struct sadb_msg *);
123 PFKEYSTAT_INC(out_msgtype[msg->sadb_msg_type]);
124 if (len != PFKEY_UNUNIT64(msg->sadb_msg_len)) {
125 PFKEYSTAT_INC(out_invlen);
130 error = key_parse(m, so);
139 * send message to the socket.
142 key_sendup0(struct rawcb *rp, struct mbuf *m, int promisc)
147 struct sadb_msg *pmsg;
149 M_PREPEND(m, sizeof(struct sadb_msg), M_NOWAIT);
151 PFKEYSTAT_INC(in_nomem);
154 pmsg = mtod(m, struct sadb_msg *);
155 bzero(pmsg, sizeof(*pmsg));
156 pmsg->sadb_msg_version = PF_KEY_V2;
157 pmsg->sadb_msg_type = SADB_X_PROMISC;
158 pmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
161 PFKEYSTAT_INC(in_msgtype[pmsg->sadb_msg_type]);
164 if (!sbappendaddr(&rp->rcb_socket->so_rcv, (struct sockaddr *)&key_src,
166 PFKEYSTAT_INC(in_nomem);
171 sorwakeup(rp->rcb_socket);
175 /* XXX this interface should be obsoleted. */
177 key_sendup(struct socket *so, struct sadb_msg *msg, u_int len, int target)
179 struct mbuf *m, *n, *mprev;
183 if (so == NULL || msg == NULL)
184 panic("%s: NULL pointer was passed.\n", __func__);
187 printf("%s: \n", __func__);
191 * we increment statistics here, just in case we have ENOBUFS
194 PFKEYSTAT_INC(in_total);
195 PFKEYSTAT_ADD(in_bytes, len);
196 PFKEYSTAT_INC(in_msgtype[msg->sadb_msg_type]);
199 * Get mbuf chain whenever possible (not clusters),
200 * to save socket buffer. We'll be generating many SADB_ACQUIRE
201 * messages to listening key sockets. If we simply allocate clusters,
202 * sbappendaddr() will raise ENOBUFS due to too little sbspace().
203 * sbspace() computes # of actual data bytes AND mbuf region.
205 * TODO: SADB_ACQUIRE filters should be implemented.
211 MGETHDR(n, M_NOWAIT, MT_DATA);
213 PFKEYSTAT_INC(in_nomem);
218 MGET(n, M_NOWAIT, MT_DATA);
220 PFKEYSTAT_INC(in_nomem);
225 if (tlen >= MCLBYTES) { /*XXX better threshold? */
226 if (!(MCLGET(n, M_NOWAIT))) {
229 PFKEYSTAT_INC(in_nomem);
247 m->m_pkthdr.len = len;
248 m->m_pkthdr.rcvif = NULL;
249 m_copyback(m, 0, len, (caddr_t)msg);
251 /* avoid duplicated statistics */
252 PFKEYSTAT_ADD(in_total, -1);
253 PFKEYSTAT_ADD(in_bytes, -len);
254 PFKEYSTAT_ADD(in_msgtype[msg->sadb_msg_type], -1);
256 return key_sendup_mbuf(so, m, target);
259 /* so can be NULL if target != KEY_SENDUP_ONE */
261 key_sendup_mbuf(struct socket *so, struct mbuf *m, int target)
270 panic("key_sendup_mbuf: NULL pointer was passed.\n");
271 if (so == NULL && target == KEY_SENDUP_ONE)
272 panic("%s: NULL pointer was passed.\n", __func__);
274 PFKEYSTAT_INC(in_total);
275 PFKEYSTAT_ADD(in_bytes, m->m_pkthdr.len);
276 if (m->m_len < sizeof(struct sadb_msg)) {
277 m = m_pullup(m, sizeof(struct sadb_msg));
279 PFKEYSTAT_INC(in_nomem);
283 if (m->m_len >= sizeof(struct sadb_msg)) {
284 struct sadb_msg *msg;
285 msg = mtod(m, struct sadb_msg *);
286 PFKEYSTAT_INC(in_msgtype[msg->sadb_msg_type]);
288 mtx_lock(&rawcb_mtx);
289 LIST_FOREACH(rp, &V_rawcb_list, list)
291 if (rp->rcb_proto.sp_family != PF_KEY)
293 if (rp->rcb_proto.sp_protocol
294 && rp->rcb_proto.sp_protocol != PF_KEY_V2) {
298 kp = (struct keycb *)rp;
301 * If you are in promiscuous mode, and when you get broadcasted
302 * reply, you'll get two PF_KEY messages.
303 * (based on pf_key@inner.net message on 14 Oct 1998)
305 if (((struct keycb *)rp)->kp_promisc) {
306 if ((n = m_copym(m, 0, M_COPYALL, M_NOWAIT)) != NULL) {
307 (void)key_sendup0(rp, n, 1);
312 /* the exact target will be processed later */
313 if (so && sotorawcb(so) == rp)
319 /* the statement has no effect */
320 if (so && sotorawcb(so) == rp)
326 case KEY_SENDUP_REGISTERED:
327 if (kp->kp_registered)
331 PFKEYSTAT_INC(in_msgtarget[target]);
336 if ((n = m_copym(m, 0, M_COPYALL, M_NOWAIT)) == NULL) {
338 PFKEYSTAT_INC(in_nomem);
339 mtx_unlock(&rawcb_mtx);
343 if ((error = key_sendup0(rp, n, 0)) != 0) {
345 mtx_unlock(&rawcb_mtx);
353 error = key_sendup0(sotorawcb(so), m, 0);
359 mtx_unlock(&rawcb_mtx);
365 * derived from net/rtsock.c:rts_abort()
368 key_abort(struct socket *so)
370 raw_usrreqs.pru_abort(so);
375 * derived from net/rtsock.c:rts_attach()
378 key_attach(struct socket *so, int proto, struct thread *td)
383 KASSERT(so->so_pcb == NULL, ("key_attach: so_pcb != NULL"));
386 error = priv_check(td, PRIV_NET_RAW);
392 kp = malloc(sizeof *kp, M_PCB, M_WAITOK | M_ZERO);
396 so->so_pcb = (caddr_t)kp;
397 error = raw_attach(so, proto);
398 kp = (struct keycb *)sotorawcb(so);
401 so->so_pcb = (caddr_t) 0;
405 kp->kp_promisc = kp->kp_registered = 0;
407 if (kp->kp_raw.rcb_proto.sp_protocol == PF_KEY) /* XXX: AF_KEY */
408 V_key_cb.key_count++;
409 V_key_cb.any_count++;
411 so->so_options |= SO_USELOOPBACK;
418 * derived from net/rtsock.c:rts_bind()
421 key_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
428 * derived from net/rtsock.c:rts_close().
431 key_close(struct socket *so)
434 raw_usrreqs.pru_close(so);
439 * derived from net/rtsock.c:rts_connect()
442 key_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
449 * derived from net/rtsock.c:rts_detach()
452 key_detach(struct socket *so)
454 struct keycb *kp = (struct keycb *)sotorawcb(so);
456 KASSERT(kp != NULL, ("key_detach: kp == NULL"));
457 if (kp->kp_raw.rcb_proto.sp_protocol
458 == PF_KEY) /* XXX: AF_KEY */
459 V_key_cb.key_count--;
460 V_key_cb.any_count--;
463 raw_usrreqs.pru_detach(so);
468 * derived from net/rtsock.c:key_disconnect()
471 key_disconnect(struct socket *so)
473 return(raw_usrreqs.pru_disconnect(so));
478 * derived from net/rtsock.c:rts_peeraddr()
481 key_peeraddr(struct socket *so, struct sockaddr **nam)
483 return(raw_usrreqs.pru_peeraddr(so, nam));
488 * derived from net/rtsock.c:rts_send()
491 key_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
492 struct mbuf *control, struct thread *td)
494 return(raw_usrreqs.pru_send(so, flags, m, nam, control, td));
499 * derived from net/rtsock.c:rts_shutdown()
502 key_shutdown(struct socket *so)
504 return(raw_usrreqs.pru_shutdown(so));
509 * derived from net/rtsock.c:rts_sockaddr()
512 key_sockaddr(struct socket *so, struct sockaddr **nam)
514 return(raw_usrreqs.pru_sockaddr(so, nam));
517 struct pr_usrreqs key_usrreqs = {
518 .pru_abort = key_abort,
519 .pru_attach = key_attach,
520 .pru_bind = key_bind,
521 .pru_connect = key_connect,
522 .pru_detach = key_detach,
523 .pru_disconnect = key_disconnect,
524 .pru_peeraddr = key_peeraddr,
525 .pru_send = key_send,
526 .pru_shutdown = key_shutdown,
527 .pru_sockaddr = key_sockaddr,
528 .pru_close = key_close,
532 SYSCTL_NODE(_net, PF_KEY, key, CTLFLAG_RW, 0, "Key Family");
535 * Definitions of protocols supported in the KEY domain.
538 extern struct domain keydomain;
540 struct protosw keysw[] = {
543 .pr_domain = &keydomain,
544 .pr_protocol = PF_KEY_V2,
545 .pr_flags = PR_ATOMIC|PR_ADDR,
546 .pr_output = key_output,
547 .pr_ctlinput = raw_ctlinput,
549 .pr_usrreqs = &key_usrreqs
557 bzero((caddr_t)&V_key_cb, sizeof(V_key_cb));
561 struct domain keydomain = {
562 .dom_family = PF_KEY,
564 .dom_init = key_init0,
566 .dom_destroy = key_destroy,
568 .dom_protosw = keysw,
569 .dom_protoswNPROTOSW = &keysw[nitems(keysw)]
572 VNET_DOMAIN_SET(key);