Dynamic sysctl.
Gone are the old kern_sysctl(), cpu_sysctl(), hw_sysctl(),
vfs_sysctl(), etc, routines, along with sysctl_int() et al. Now all
nodes are registered with the tree, and nodes can be added (or
removed) easily, and I/O to and from the tree is handled generically.
Since the nodes are registered with the tree, the mapping from name to
number (and back again) can now be discovered, instead of having to be
hard coded. Adding new nodes to the tree is likewise much simpler --
the new infrastructure handles almost all the work for simple types,
and just about anything else can be done with a small helper function.
All existing nodes are where they were before (numerically speaking),
so all existing consumers of sysctl information should notice no
difference.
PS - I'm sorry, but there's a distinct lack of documentation at the
moment. I'm working on sysctl(3/8/9) right now, and I promise to
watch out for buses.
2003-12-04 22:38:21 +03:00
|
|
|
/* $NetBSD: keysock.c,v 1.26 2003/12/04 19:38:25 atatat Exp $ */
|
2003-08-22 09:46:37 +04:00
|
|
|
/* $KAME: keysock.c,v 1.32 2003/08/22 05:45:08 itojun Exp $ */
|
1999-07-04 01:24:45 +04:00
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
|
|
|
|
* All rights reserved.
|
2000-06-12 14:40:37 +04:00
|
|
|
*
|
1999-06-28 10:36:47 +04:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the project nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
2000-06-12 14:40:37 +04:00
|
|
|
*
|
1999-06-28 10:36:47 +04:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2001-11-13 03:56:55 +03:00
|
|
|
#include <sys/cdefs.h>
|
Dynamic sysctl.
Gone are the old kern_sysctl(), cpu_sysctl(), hw_sysctl(),
vfs_sysctl(), etc, routines, along with sysctl_int() et al. Now all
nodes are registered with the tree, and nodes can be added (or
removed) easily, and I/O to and from the tree is handled generically.
Since the nodes are registered with the tree, the mapping from name to
number (and back again) can now be discovered, instead of having to be
hard coded. Adding new nodes to the tree is likewise much simpler --
the new infrastructure handles almost all the work for simple types,
and just about anything else can be done with a small helper function.
All existing nodes are where they were before (numerically speaking),
so all existing consumers of sysctl information should notice no
difference.
PS - I'm sorry, but there's a distinct lack of documentation at the
moment. I'm working on sysctl(3/8/9) right now, and I promise to
watch out for buses.
2003-12-04 22:38:21 +03:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: keysock.c,v 1.26 2003/12/04 19:38:25 atatat Exp $");
|
2001-11-13 03:56:55 +03:00
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
#include "opt_inet.h"
|
|
|
|
|
|
|
|
/* This code has derived from sys/net/rtsock.c on FreeBSD2.2.5 */
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/mbuf.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/socketvar.h>
|
|
|
|
#include <sys/domain.h>
|
|
|
|
#include <sys/protosw.h>
|
|
|
|
#include <sys/errno.h>
|
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/queue.h>
|
|
|
|
|
|
|
|
#include <net/raw_cb.h>
|
|
|
|
#include <net/route.h>
|
2003-01-08 08:46:49 +03:00
|
|
|
#include <netinet/in.h>
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-02-06 15:49:37 +03:00
|
|
|
#include <net/pfkeyv2.h>
|
1999-06-28 10:36:47 +04:00
|
|
|
#include <netkey/keydb.h>
|
|
|
|
#include <netkey/key.h>
|
|
|
|
#include <netkey/keysock.h>
|
|
|
|
#include <netkey/key_debug.h>
|
|
|
|
|
|
|
|
#include <machine/stdarg.h>
|
|
|
|
|
|
|
|
struct sockaddr key_dst = { 2, PF_KEY, };
|
|
|
|
struct sockaddr key_src = { 2, PF_KEY, };
|
|
|
|
|
|
|
|
static int key_sendup0 __P((struct rawcb *, struct mbuf *, int));
|
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
struct pfkeystat pfkeystat;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* key_usrreq()
|
|
|
|
* derived from net/rtsock.c:route_usrreq()
|
|
|
|
*/
|
|
|
|
int
|
2003-06-30 02:28:00 +04:00
|
|
|
key_usrreq(so, req, m, nam, control, p)
|
2001-08-02 15:32:14 +04:00
|
|
|
struct socket *so;
|
1999-06-28 10:36:47 +04:00
|
|
|
int req;
|
|
|
|
struct mbuf *m, *nam, *control;
|
2003-06-30 02:28:00 +04:00
|
|
|
struct proc *p;
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
2001-08-02 15:32:14 +04:00
|
|
|
int error = 0;
|
|
|
|
struct keycb *kp = (struct keycb *)sotorawcb(so);
|
1999-06-28 10:36:47 +04:00
|
|
|
int s;
|
|
|
|
|
1999-07-04 06:01:15 +04:00
|
|
|
s = splsoftnet();
|
1999-06-28 10:36:47 +04:00
|
|
|
if (req == PRU_ATTACH) {
|
2000-01-31 17:18:52 +03:00
|
|
|
kp = (struct keycb *)malloc(sizeof(*kp), M_PCB, M_WAITOK);
|
1999-06-28 10:36:47 +04:00
|
|
|
so->so_pcb = (caddr_t)kp;
|
|
|
|
if (so->so_pcb)
|
|
|
|
bzero(so->so_pcb, sizeof(*kp));
|
|
|
|
}
|
|
|
|
if (req == PRU_DETACH && kp) {
|
|
|
|
int af = kp->kp_raw.rcb_proto.sp_protocol;
|
|
|
|
if (af == PF_KEY) /* XXX: AF_KEY */
|
|
|
|
key_cb.key_count--;
|
|
|
|
key_cb.any_count--;
|
|
|
|
|
|
|
|
key_freereg(so);
|
|
|
|
}
|
|
|
|
|
2003-06-30 02:28:00 +04:00
|
|
|
error = raw_usrreq(so, req, m, nam, control, p);
|
1999-06-28 10:36:47 +04:00
|
|
|
m = control = NULL; /* reclaimed in raw_usrreq */
|
|
|
|
kp = (struct keycb *)sotorawcb(so);
|
|
|
|
if (req == PRU_ATTACH && kp) {
|
|
|
|
int af = kp->kp_raw.rcb_proto.sp_protocol;
|
|
|
|
if (error) {
|
2000-01-31 17:18:52 +03:00
|
|
|
pfkeystat.sockerr++;
|
1999-06-28 10:36:47 +04:00
|
|
|
free((caddr_t)kp, M_PCB);
|
|
|
|
so->so_pcb = (caddr_t) 0;
|
|
|
|
splx(s);
|
2002-09-11 06:46:42 +04:00
|
|
|
return (error);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
kp->kp_promisc = kp->kp_registered = 0;
|
|
|
|
|
|
|
|
if (af == PF_KEY) /* XXX: AF_KEY */
|
|
|
|
key_cb.key_count++;
|
|
|
|
key_cb.any_count++;
|
|
|
|
kp->kp_raw.rcb_laddr = &key_src;
|
|
|
|
kp->kp_raw.rcb_faddr = &key_dst;
|
|
|
|
soisconnected(so);
|
|
|
|
so->so_options |= SO_USELOOPBACK;
|
|
|
|
}
|
|
|
|
splx(s);
|
2002-09-11 06:46:42 +04:00
|
|
|
return (error);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* key_output()
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
#if __STDC__
|
|
|
|
key_output(struct mbuf *m, ...)
|
|
|
|
#else
|
|
|
|
key_output(m, va_alist)
|
|
|
|
struct mbuf *m;
|
|
|
|
va_dcl
|
|
|
|
#endif
|
|
|
|
{
|
2000-06-12 14:40:37 +04:00
|
|
|
struct sadb_msg *msg;
|
1999-06-28 10:36:47 +04:00
|
|
|
int len, error = 0;
|
|
|
|
int s;
|
|
|
|
struct socket *so;
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, m);
|
|
|
|
so = va_arg(ap, struct socket *);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
if (m == 0)
|
2002-09-27 19:35:29 +04:00
|
|
|
panic("key_output: NULL pointer was passed.");
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
pfkeystat.out_total++;
|
|
|
|
pfkeystat.out_bytes += m->m_pkthdr.len;
|
|
|
|
|
|
|
|
len = m->m_pkthdr.len;
|
|
|
|
if (len < sizeof(struct sadb_msg)) {
|
|
|
|
pfkeystat.out_tooshort++;
|
|
|
|
error = EINVAL;
|
1999-06-28 10:36:47 +04:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
if (m->m_len < sizeof(struct sadb_msg)) {
|
|
|
|
if ((m = m_pullup(m, sizeof(struct sadb_msg))) == 0) {
|
|
|
|
pfkeystat.out_nomem++;
|
|
|
|
error = ENOBUFS;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
if ((m->m_flags & M_PKTHDR) == 0)
|
|
|
|
panic("key_output: not M_PKTHDR ??");
|
|
|
|
|
|
|
|
KEYDEBUG(KEYDEBUG_KEY_DUMP, kdebug_mbuf(m));
|
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
msg = mtod(m, struct sadb_msg *);
|
|
|
|
pfkeystat.out_msgtype[msg->sadb_msg_type]++;
|
|
|
|
if (len != PFKEY_UNUNIT64(msg->sadb_msg_len)) {
|
|
|
|
pfkeystat.out_invlen++;
|
1999-06-28 10:36:47 +04:00
|
|
|
error = EINVAL;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
1999-07-31 22:41:15 +04:00
|
|
|
/*XXX giant lock*/
|
|
|
|
s = splsoftnet();
|
2000-06-12 14:40:37 +04:00
|
|
|
error = key_parse(m, so);
|
|
|
|
m = NULL;
|
1999-06-28 10:36:47 +04:00
|
|
|
splx(s);
|
|
|
|
end:
|
2000-06-12 14:40:37 +04:00
|
|
|
if (m)
|
|
|
|
m_freem(m);
|
|
|
|
return error;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* send message to the socket.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
key_sendup0(rp, m, promisc)
|
|
|
|
struct rawcb *rp;
|
|
|
|
struct mbuf *m;
|
|
|
|
int promisc;
|
|
|
|
{
|
2000-09-22 12:28:56 +04:00
|
|
|
int error;
|
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
if (promisc) {
|
|
|
|
struct sadb_msg *pmsg;
|
|
|
|
|
|
|
|
M_PREPEND(m, sizeof(struct sadb_msg), M_NOWAIT);
|
|
|
|
if (m && m->m_len < sizeof(struct sadb_msg))
|
|
|
|
m = m_pullup(m, sizeof(struct sadb_msg));
|
|
|
|
if (!m) {
|
2000-06-12 14:40:37 +04:00
|
|
|
pfkeystat.in_nomem++;
|
1999-06-28 10:36:47 +04:00
|
|
|
return ENOBUFS;
|
|
|
|
}
|
|
|
|
m->m_pkthdr.len += sizeof(*pmsg);
|
|
|
|
|
|
|
|
pmsg = mtod(m, struct sadb_msg *);
|
|
|
|
bzero(pmsg, sizeof(*pmsg));
|
|
|
|
pmsg->sadb_msg_version = PF_KEY_V2;
|
|
|
|
pmsg->sadb_msg_type = SADB_X_PROMISC;
|
|
|
|
pmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
|
|
|
|
/* pid and seq? */
|
2000-01-31 17:18:52 +03:00
|
|
|
|
|
|
|
pfkeystat.in_msgtype[pmsg->sadb_msg_type]++;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
2000-09-22 12:28:56 +04:00
|
|
|
if (!sbappendaddr(&rp->rcb_socket->so_rcv, (struct sockaddr *)&key_src,
|
|
|
|
m, NULL)) {
|
2000-01-31 17:18:52 +03:00
|
|
|
pfkeystat.in_nomem++;
|
1999-06-28 10:36:47 +04:00
|
|
|
m_freem(m);
|
2000-09-22 12:28:56 +04:00
|
|
|
error = ENOBUFS;
|
|
|
|
} else
|
|
|
|
error = 0;
|
1999-06-28 10:36:47 +04:00
|
|
|
sorwakeup(rp->rcb_socket);
|
2000-09-22 12:28:56 +04:00
|
|
|
return error;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
/* so can be NULL if target != KEY_SENDUP_ONE */
|
2000-01-31 17:18:52 +03:00
|
|
|
int
|
|
|
|
key_sendup_mbuf(so, m, target)
|
|
|
|
struct socket *so;
|
|
|
|
struct mbuf *m;
|
|
|
|
int target;
|
|
|
|
{
|
|
|
|
struct mbuf *n;
|
|
|
|
struct keycb *kp;
|
|
|
|
int sendup;
|
|
|
|
struct rawcb *rp;
|
2000-06-12 14:40:37 +04:00
|
|
|
int error = 0;
|
2000-01-31 17:18:52 +03:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
if (m == NULL)
|
2002-09-27 19:35:29 +04:00
|
|
|
panic("key_sendup_mbuf: NULL pointer was passed.");
|
2000-06-12 14:40:37 +04:00
|
|
|
if (so == NULL && target == KEY_SENDUP_ONE)
|
2002-09-27 19:35:29 +04:00
|
|
|
panic("key_sendup_mbuf: NULL pointer was passed.");
|
2000-01-31 17:18:52 +03:00
|
|
|
|
|
|
|
pfkeystat.in_total++;
|
|
|
|
pfkeystat.in_bytes += m->m_pkthdr.len;
|
|
|
|
if (m->m_len < sizeof(struct sadb_msg)) {
|
|
|
|
m = m_pullup(m, sizeof(struct sadb_msg));
|
|
|
|
if (m == NULL) {
|
|
|
|
pfkeystat.in_nomem++;
|
|
|
|
return ENOBUFS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (m->m_len >= sizeof(struct sadb_msg)) {
|
|
|
|
struct sadb_msg *msg;
|
|
|
|
msg = mtod(m, struct sadb_msg *);
|
|
|
|
pfkeystat.in_msgtype[msg->sadb_msg_type]++;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (rp = rawcb.lh_first; rp; rp = rp->rcb_list.le_next)
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
|
|
|
if (rp->rcb_proto.sp_family != PF_KEY)
|
|
|
|
continue;
|
2003-08-22 10:21:09 +04:00
|
|
|
if (rp->rcb_proto.sp_protocol &&
|
|
|
|
rp->rcb_proto.sp_protocol != PF_KEY_V2) {
|
1999-06-28 10:36:47 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
kp = (struct keycb *)rp;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If you are in promiscuous mode, and when you get broadcasted
|
|
|
|
* reply, you'll get two PF_KEY messages.
|
|
|
|
* (based on pf_key@inner.net message on 14 Oct 1998)
|
|
|
|
*/
|
|
|
|
if (((struct keycb *)rp)->kp_promisc) {
|
|
|
|
if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
|
|
|
|
(void)key_sendup0(rp, n, 1);
|
|
|
|
n = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* the exact target will be processed later */
|
2000-06-12 14:40:37 +04:00
|
|
|
if (so && sotorawcb(so) == rp)
|
1999-06-28 10:36:47 +04:00
|
|
|
continue;
|
|
|
|
|
|
|
|
sendup = 0;
|
|
|
|
switch (target) {
|
|
|
|
case KEY_SENDUP_ONE:
|
|
|
|
/* the statement has no effect */
|
2000-06-12 14:40:37 +04:00
|
|
|
if (so && sotorawcb(so) == rp)
|
1999-06-28 10:36:47 +04:00
|
|
|
sendup++;
|
|
|
|
break;
|
|
|
|
case KEY_SENDUP_ALL:
|
|
|
|
sendup++;
|
|
|
|
break;
|
|
|
|
case KEY_SENDUP_REGISTERED:
|
|
|
|
if (kp->kp_registered)
|
|
|
|
sendup++;
|
|
|
|
break;
|
|
|
|
}
|
2000-01-31 17:18:52 +03:00
|
|
|
pfkeystat.in_msgtarget[target]++;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
if (!sendup)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if ((n = m_copy(m, 0, (int)M_COPYALL)) == NULL) {
|
|
|
|
m_freem(m);
|
2000-01-31 17:18:52 +03:00
|
|
|
pfkeystat.in_nomem++;
|
1999-06-28 10:36:47 +04:00
|
|
|
return ENOBUFS;
|
|
|
|
}
|
|
|
|
|
2003-08-22 09:46:37 +04:00
|
|
|
/*
|
|
|
|
* ignore error even if queue is full. PF_KEY does not
|
|
|
|
* guarantee the delivery of the message.
|
|
|
|
* this is important when target == KEY_SENDUP_ALL.
|
|
|
|
*/
|
|
|
|
key_sendup0(rp, n, 0);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
n = NULL;
|
|
|
|
}
|
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
if (so) {
|
|
|
|
error = key_sendup0(sotorawcb(so), m, 0);
|
|
|
|
m = NULL;
|
|
|
|
} else {
|
|
|
|
error = 0;
|
|
|
|
m_freem(m);
|
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Definitions of protocols supported in the KEY domain.
|
|
|
|
*/
|
|
|
|
|
|
|
|
extern struct domain keydomain;
|
|
|
|
|
|
|
|
struct protosw keysw[] = {
|
|
|
|
{ SOCK_RAW, &keydomain, PF_KEY_V2, PR_ATOMIC|PR_ADDR,
|
|
|
|
0, key_output, raw_ctlinput, 0,
|
|
|
|
key_usrreq,
|
|
|
|
raw_init, 0, 0, 0,
|
Dynamic sysctl.
Gone are the old kern_sysctl(), cpu_sysctl(), hw_sysctl(),
vfs_sysctl(), etc, routines, along with sysctl_int() et al. Now all
nodes are registered with the tree, and nodes can be added (or
removed) easily, and I/O to and from the tree is handled generically.
Since the nodes are registered with the tree, the mapping from name to
number (and back again) can now be discovered, instead of having to be
hard coded. Adding new nodes to the tree is likewise much simpler --
the new infrastructure handles almost all the work for simple types,
and just about anything else can be done with a small helper function.
All existing nodes are where they were before (numerically speaking),
so all existing consumers of sysctl information should notice no
difference.
PS - I'm sorry, but there's a distinct lack of documentation at the
moment. I'm working on sysctl(3/8/9) right now, and I promise to
watch out for buses.
2003-12-04 22:38:21 +03:00
|
|
|
NULL,
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct domain keydomain =
|
|
|
|
{ PF_KEY, "key", key_init, 0, 0,
|
|
|
|
keysw, &keysw[sizeof(keysw)/sizeof(keysw[0])] };
|
|
|
|
|