#include <sys/types.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/sysctl.h>
#include <sys/mbuf.h>
#include <sys/malloc.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/domain.h>
#include <sys/protosw.h>
#include <sys/errno.h>
#include <kern/locks.h>
#include <net/raw_cb.h>
#include <net/route.h>
#include <net/pfkeyv2.h>
#include <netkey/keydb.h>
#include <netkey/key.h>
#include <netkey/keysock.h>
#include <netkey/key_debug.h>
extern lck_mtx_t *raw_mtx;
extern lck_mtx_t *sadb_mutex;
extern void key_init(void);
struct sockaddr key_dst = { 2, PF_KEY, };
struct sockaddr key_src = { 2, PF_KEY, };
static int key_sendup0(struct rawcb *, struct mbuf *, int);
struct pfkeystat pfkeystat;
int
#ifdef __APPLE__
key_output(struct mbuf *m, struct socket *so)
#else
#if __STDC__
key_output(struct mbuf *m, ...)
#else
key_output(m, va_alist)
struct mbuf *m;
va_dcl
#endif
#endif
{
struct sadb_msg *msg;
int len, error = 0;
#ifndef __APPLE__
struct socket *so;
va_list ap;
va_start(ap, m);
so = va_arg(ap, struct socket *);
va_end(ap);
#endif
if (m == 0)
panic("key_output: NULL pointer was passed.\n");
socket_unlock(so, 0);
lck_mtx_lock(sadb_mutex);
pfkeystat.out_total++;
pfkeystat.out_bytes += m->m_pkthdr.len;
len = m->m_pkthdr.len;
if (len < sizeof(struct sadb_msg)) {
#if IPSEC_DEBUG
printf("key_output: Invalid message length.\n");
#endif
pfkeystat.out_tooshort++;
error = EINVAL;
goto end;
}
if (m->m_len < sizeof(struct sadb_msg)) {
if ((m = m_pullup(m, sizeof(struct sadb_msg))) == 0) {
#if IPSEC_DEBUG
printf("key_output: can't pullup mbuf\n");
#endif
pfkeystat.out_nomem++;
error = ENOBUFS;
goto end;
}
}
if ((m->m_flags & M_PKTHDR) == 0)
panic("key_output: not M_PKTHDR ??");
#if IPSEC_DEBUG
KEYDEBUG(KEYDEBUG_KEY_DUMP, kdebug_mbuf(m));
#endif
msg = mtod(m, struct sadb_msg *);
pfkeystat.out_msgtype[msg->sadb_msg_type]++;
if (len != PFKEY_UNUNIT64(msg->sadb_msg_len)) {
#if IPSEC_DEBUG
printf("key_output: Invalid message length.\n");
#endif
pfkeystat.out_invlen++;
error = EINVAL;
goto end;
}
error = key_parse(m, so);
m = NULL;
end:
if (m)
m_freem(m);
lck_mtx_unlock(sadb_mutex);
socket_lock(so, 0);
return error;
}
static int
key_sendup0(rp, m, promisc)
struct rawcb *rp;
struct mbuf *m;
int promisc;
{
int error;
lck_mtx_assert(sadb_mutex, LCK_MTX_ASSERT_OWNED);
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) {
#if IPSEC_DEBUG
printf("key_sendup0: cannot pullup\n");
#endif
pfkeystat.in_nomem++;
m_freem(m);
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);
pfkeystat.in_msgtype[pmsg->sadb_msg_type]++;
}
if (!sbappendaddr(&rp->rcb_socket->so_rcv, (struct sockaddr *)&key_src,
m, NULL, &error)) {
#if IPSEC_DEBUG
printf("key_sendup0: sbappendaddr failed\n");
#endif
pfkeystat.in_nomem++;
}
else {
sorwakeup(rp->rcb_socket);
}
return error;
}
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;
int error = 0;
lck_mtx_assert(sadb_mutex, LCK_MTX_ASSERT_OWNED);
if (m == NULL)
panic("key_sendup_mbuf: NULL pointer was passed.\n");
if (so == NULL && target == KEY_SENDUP_ONE)
panic("key_sendup_mbuf: NULL pointer was passed.\n");
pfkeystat.in_total++;
pfkeystat.in_bytes += m->m_pkthdr.len;
if (m->m_len < sizeof(struct sadb_msg)) {
#if 1
m = m_pullup(m, sizeof(struct sadb_msg));
if (m == NULL) {
pfkeystat.in_nomem++;
return ENOBUFS;
}
#else
#endif
}
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]++;
}
lck_mtx_lock(raw_mtx);
LIST_FOREACH(rp, &rawcb_list, list)
{
if (rp->rcb_proto.sp_family != PF_KEY)
continue;
if (rp->rcb_proto.sp_protocol
&& rp->rcb_proto.sp_protocol != PF_KEY_V2) {
continue;
}
kp = (struct keycb *)rp;
socket_lock(rp->rcb_socket, 1);
if (((struct keycb *)rp)->kp_promisc) {
if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
(void)key_sendup0(rp, n, 1);
n = NULL;
}
}
if (so && sotorawcb(so) == rp) {
socket_unlock(rp->rcb_socket, 1);
continue;
}
sendup = 0;
switch (target) {
case KEY_SENDUP_ONE:
break;
case KEY_SENDUP_ALL:
sendup++;
break;
case KEY_SENDUP_REGISTERED:
if (kp->kp_registered)
sendup++;
break;
}
pfkeystat.in_msgtarget[target]++;
if (!sendup) {
socket_unlock(rp->rcb_socket, 1);
continue;
}
else
sendup = 0;
if ((n = m_copy(m, 0, (int)M_COPYALL)) == NULL) {
#if IPSEC_DEBUG
printf("key_sendup: m_copy fail\n");
#endif
m_freem(m);
pfkeystat.in_nomem++;
socket_unlock(rp->rcb_socket, 1);
lck_mtx_unlock(raw_mtx);
return ENOBUFS;
}
key_sendup0(rp, n, 0);
socket_unlock(rp->rcb_socket, 1);
n = NULL;
}
lck_mtx_unlock(raw_mtx);
if (so) {
socket_lock(so, 1);
error = key_sendup0(sotorawcb(so), m, 0);
socket_unlock(so, 1);
m = NULL;
} else {
error = 0;
m_freem(m);
}
return error;
}
static int
key_abort(struct socket *so)
{
int error;
error = raw_usrreqs.pru_abort(so);
return error;
}
static int
key_attach(struct socket *so, int proto, struct proc *p)
{
struct keycb *kp;
int error;
if (sotorawcb(so) != 0)
return EISCONN;
kp = (struct keycb *)_MALLOC(sizeof *kp, M_PCB, M_WAITOK);
if (kp == 0)
return ENOBUFS;
bzero(kp, sizeof *kp);
so->so_pcb = (caddr_t)kp;
kp->kp_promisc = kp->kp_registered = 0;
kp->kp_raw.rcb_laddr = &key_src;
kp->kp_raw.rcb_faddr = &key_dst;
error = raw_usrreqs.pru_attach(so, proto, p);
kp = (struct keycb *)sotorawcb(so);
if (error) {
_FREE(kp, M_PCB);
so->so_pcb = (caddr_t) 0;
so->so_flags |= SOF_PCBCLEARING;
printf("key_usrreq: key_usrreq results %d\n", error);
return error;
}
if (kp->kp_raw.rcb_proto.sp_protocol == PF_KEY)
key_cb.key_count++;
key_cb.any_count++;
soisconnected(so);
so->so_options |= SO_USELOOPBACK;
return 0;
}
static int
key_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
{
int error;
error = raw_usrreqs.pru_bind(so, nam, p);
return error;
}
static int
key_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
{
int error;
error = raw_usrreqs.pru_connect(so, nam, p);
return error;
}
static int
key_detach(struct socket *so)
{
struct keycb *kp = (struct keycb *)sotorawcb(so);
int error;
if (kp != 0) {
if (kp->kp_raw.rcb_proto.sp_protocol == PF_KEY)
key_cb.key_count--;
key_cb.any_count--;
socket_unlock(so, 0);
lck_mtx_lock(sadb_mutex);
key_freereg(so);
lck_mtx_unlock(sadb_mutex);
socket_lock(so, 0);
}
error = raw_usrreqs.pru_detach(so);
return error;
}
static int
key_disconnect(struct socket *so)
{
int error;
error = raw_usrreqs.pru_disconnect(so);
return error;
}
static int
key_peeraddr(struct socket *so, struct sockaddr **nam)
{
int error;
error = raw_usrreqs.pru_peeraddr(so, nam);
return error;
}
static int
key_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
struct mbuf *control, struct proc *p)
{
int error;
error = raw_usrreqs.pru_send(so, flags, m, nam, control, p);
return error;
}
static int
key_shutdown(struct socket *so)
{
int error;
error = raw_usrreqs.pru_shutdown(so);
return error;
}
static int
key_sockaddr(struct socket *so, struct sockaddr **nam)
{
int error;
error = raw_usrreqs.pru_sockaddr(so, nam);
return error;
}
struct pr_usrreqs key_usrreqs = {
key_abort, pru_accept_notsupp, key_attach, key_bind,
key_connect,
pru_connect2_notsupp, pru_control_notsupp, key_detach,
key_disconnect, pru_listen_notsupp, key_peeraddr,
pru_rcvd_notsupp,
pru_rcvoob_notsupp, key_send, pru_sense_null, key_shutdown,
key_sockaddr, sosend, soreceive, pru_sopoll_notsupp
};
SYSCTL_NODE(_net, PF_KEY, key, CTLFLAG_RW, 0, "Key Family");
extern struct domain keydomain;
struct protosw keysw[] = {
{ SOCK_RAW, &keydomain, PF_KEY_V2, PR_ATOMIC|PR_ADDR,
0, key_output, raw_ctlinput, 0,
0,
key_init, 0, 0, 0,
0,
&key_usrreqs,
0, 0, 0,
}
};
struct domain keydomain =
{ PF_KEY, "key", key_domain_init, 0, 0,
keysw, 0,
0,0,
sizeof(struct key_cb), 0
};
DOMAIN_SET(key);