Home | History | Annotate | Line # | Download | only in netipsec
keysock.c revision 1.19.2.1
      1  1.19.2.1     rmind /*	$NetBSD: keysock.c,v 1.19.2.1 2011/05/31 03:05:10 rmind Exp $	*/
      2       1.1  jonathan /*	$FreeBSD: src/sys/netipsec/keysock.c,v 1.3.2.1 2003/01/24 05:11:36 sam Exp $	*/
      3       1.1  jonathan /*	$KAME: keysock.c,v 1.25 2001/08/13 20:07:41 itojun Exp $	*/
      4       1.1  jonathan 
      5       1.1  jonathan /*
      6       1.1  jonathan  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      7       1.1  jonathan  * All rights reserved.
      8       1.1  jonathan  *
      9       1.1  jonathan  * Redistribution and use in source and binary forms, with or without
     10       1.1  jonathan  * modification, are permitted provided that the following conditions
     11       1.1  jonathan  * are met:
     12       1.1  jonathan  * 1. Redistributions of source code must retain the above copyright
     13       1.1  jonathan  *    notice, this list of conditions and the following disclaimer.
     14       1.1  jonathan  * 2. Redistributions in binary form must reproduce the above copyright
     15       1.1  jonathan  *    notice, this list of conditions and the following disclaimer in the
     16       1.1  jonathan  *    documentation and/or other materials provided with the distribution.
     17       1.1  jonathan  * 3. Neither the name of the project nor the names of its contributors
     18       1.1  jonathan  *    may be used to endorse or promote products derived from this software
     19       1.1  jonathan  *    without specific prior written permission.
     20       1.1  jonathan  *
     21       1.1  jonathan  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     22       1.1  jonathan  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23       1.1  jonathan  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24       1.1  jonathan  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     25       1.1  jonathan  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26       1.1  jonathan  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27       1.1  jonathan  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28       1.1  jonathan  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29       1.1  jonathan  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30       1.1  jonathan  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31       1.1  jonathan  * SUCH DAMAGE.
     32       1.1  jonathan  */
     33       1.1  jonathan 
     34       1.1  jonathan #include <sys/cdefs.h>
     35  1.19.2.1     rmind __KERNEL_RCSID(0, "$NetBSD: keysock.c,v 1.19.2.1 2011/05/31 03:05:10 rmind Exp $");
     36       1.1  jonathan 
     37       1.1  jonathan #include "opt_ipsec.h"
     38       1.1  jonathan 
     39       1.1  jonathan /* This code has derived from sys/net/rtsock.c on FreeBSD2.2.5 */
     40       1.1  jonathan 
     41       1.1  jonathan #include <sys/types.h>
     42       1.1  jonathan #include <sys/param.h>
     43       1.1  jonathan #include <sys/domain.h>
     44       1.1  jonathan #include <sys/errno.h>
     45       1.1  jonathan #include <sys/kernel.h>
     46       1.1  jonathan #include <sys/malloc.h>
     47       1.1  jonathan #include <sys/mbuf.h>
     48       1.1  jonathan #include <sys/protosw.h>
     49       1.1  jonathan #include <sys/signalvar.h>
     50       1.1  jonathan #include <sys/socket.h>
     51       1.1  jonathan #include <sys/socketvar.h>
     52       1.1  jonathan #include <sys/sysctl.h>
     53       1.1  jonathan #include <sys/systm.h>
     54       1.1  jonathan 
     55       1.1  jonathan #include <net/raw_cb.h>
     56       1.1  jonathan #include <net/route.h>
     57       1.1  jonathan 
     58       1.1  jonathan #include <net/pfkeyv2.h>
     59       1.1  jonathan #include <netipsec/key.h>
     60       1.1  jonathan #include <netipsec/keysock.h>
     61       1.1  jonathan #include <netipsec/key_debug.h>
     62       1.1  jonathan 
     63       1.1  jonathan #include <netipsec/ipsec_osdep.h>
     64      1.15   thorpej #include <netipsec/ipsec_private.h>
     65       1.1  jonathan 
     66       1.1  jonathan #include <machine/stdarg.h>
     67       1.1  jonathan 
     68       1.1  jonathan typedef int	pr_output_t (struct mbuf *, struct socket *);
     69       1.1  jonathan 
     70       1.1  jonathan struct key_cb {
     71       1.1  jonathan 	int key_count;
     72       1.1  jonathan 	int any_count;
     73       1.1  jonathan };
     74       1.1  jonathan static struct key_cb key_cb;
     75       1.1  jonathan 
     76      1.11  christos static struct sockaddr key_dst = {
     77      1.11  christos     .sa_len = 2,
     78      1.11  christos     .sa_family = PF_KEY,
     79      1.11  christos };
     80      1.11  christos static struct sockaddr key_src = {
     81      1.11  christos     .sa_len = 2,
     82      1.11  christos     .sa_family = PF_KEY,
     83      1.11  christos };
     84       1.1  jonathan 
     85       1.5  jonathan 
     86      1.17       dsl static int key_sendup0(struct rawcb *, struct mbuf *, int, int);
     87       1.1  jonathan 
     88      1.19     joerg int key_registered_sb_max = (2048 * MHLEN); /* XXX arbitrary */
     89       1.5  jonathan 
     90       1.5  jonathan /* XXX sysctl */
     91       1.5  jonathan #ifdef __FreeBSD__
     92       1.7     perry SYSCTL_INT(_net_key, OID_AUTO, registered_sbmax, CTLFLAG_RD,
     93       1.5  jonathan     &key_registered_sb_max , 0, "Maximum kernel-to-user PFKEY datagram size");
     94       1.5  jonathan #endif
     95       1.5  jonathan 
     96       1.1  jonathan /*
     97       1.1  jonathan  * key_output()
     98       1.1  jonathan  */
     99       1.1  jonathan int
    100       1.1  jonathan key_output(struct mbuf *m, ...)
    101       1.1  jonathan {
    102       1.1  jonathan 	struct sadb_msg *msg;
    103       1.1  jonathan 	int len, error = 0;
    104       1.1  jonathan 	int s;
    105       1.1  jonathan 	struct socket *so;
    106       1.1  jonathan 	va_list ap;
    107       1.1  jonathan 
    108       1.1  jonathan 	va_start(ap, m);
    109       1.1  jonathan 	so = va_arg(ap, struct socket *);
    110       1.1  jonathan 	va_end(ap);
    111       1.1  jonathan 
    112       1.1  jonathan 	if (m == 0)
    113       1.8  christos 		panic("key_output: NULL pointer was passed");
    114       1.1  jonathan 
    115      1.15   thorpej 	{
    116      1.15   thorpej 		uint64_t *ps = PFKEY_STAT_GETREF();
    117      1.15   thorpej 		ps[PFKEY_STAT_OUT_TOTAL]++;
    118      1.15   thorpej 		ps[PFKEY_STAT_OUT_BYTES] += m->m_pkthdr.len;
    119      1.15   thorpej 		PFKEY_STAT_PUTREF();
    120      1.15   thorpej 	}
    121       1.1  jonathan 
    122       1.1  jonathan 	len = m->m_pkthdr.len;
    123       1.1  jonathan 	if (len < sizeof(struct sadb_msg)) {
    124      1.15   thorpej 		PFKEY_STATINC(PFKEY_STAT_OUT_TOOSHORT);
    125       1.1  jonathan 		error = EINVAL;
    126       1.1  jonathan 		goto end;
    127       1.1  jonathan 	}
    128       1.1  jonathan 
    129       1.1  jonathan 	if (m->m_len < sizeof(struct sadb_msg)) {
    130       1.1  jonathan 		if ((m = m_pullup(m, sizeof(struct sadb_msg))) == 0) {
    131      1.15   thorpej 			PFKEY_STATINC(PFKEY_STAT_OUT_NOMEM);
    132       1.1  jonathan 			error = ENOBUFS;
    133       1.1  jonathan 			goto end;
    134       1.1  jonathan 		}
    135       1.1  jonathan 	}
    136       1.1  jonathan 
    137       1.1  jonathan 	if ((m->m_flags & M_PKTHDR) == 0)
    138       1.1  jonathan 		panic("key_output: not M_PKTHDR ??");
    139       1.1  jonathan 
    140       1.1  jonathan 	KEYDEBUG(KEYDEBUG_KEY_DUMP, kdebug_mbuf(m));
    141       1.1  jonathan 
    142       1.1  jonathan 	msg = mtod(m, struct sadb_msg *);
    143      1.15   thorpej 	PFKEY_STATINC(PFKEY_STAT_OUT_MSGTYPE + msg->sadb_msg_type);
    144       1.1  jonathan 	if (len != PFKEY_UNUNIT64(msg->sadb_msg_len)) {
    145      1.15   thorpej 		PFKEY_STATINC(PFKEY_STAT_OUT_INVLEN);
    146       1.1  jonathan 		error = EINVAL;
    147       1.1  jonathan 		goto end;
    148       1.1  jonathan 	}
    149       1.1  jonathan 
    150       1.1  jonathan 	/*XXX giant lock*/
    151       1.1  jonathan 	s = splsoftnet();
    152       1.1  jonathan 	error = key_parse(m, so);
    153       1.1  jonathan 	m = NULL;
    154       1.1  jonathan 	splx(s);
    155       1.1  jonathan end:
    156       1.1  jonathan 	if (m)
    157       1.1  jonathan 		m_freem(m);
    158       1.1  jonathan 	return error;
    159       1.1  jonathan }
    160       1.1  jonathan 
    161       1.1  jonathan /*
    162       1.1  jonathan  * send message to the socket.
    163       1.1  jonathan  */
    164       1.1  jonathan static int
    165      1.11  christos key_sendup0(
    166      1.11  christos     struct rawcb *rp,
    167      1.11  christos     struct mbuf *m,
    168      1.11  christos     int promisc,
    169      1.11  christos     int sbprio
    170      1.11  christos )
    171       1.1  jonathan {
    172       1.1  jonathan 	int error;
    173       1.5  jonathan 	int ok;
    174       1.1  jonathan 
    175       1.1  jonathan 	if (promisc) {
    176       1.1  jonathan 		struct sadb_msg *pmsg;
    177       1.1  jonathan 
    178       1.1  jonathan 		M_PREPEND(m, sizeof(struct sadb_msg), M_DONTWAIT);
    179       1.1  jonathan 		if (m && m->m_len < sizeof(struct sadb_msg))
    180       1.1  jonathan 			m = m_pullup(m, sizeof(struct sadb_msg));
    181       1.1  jonathan 		if (!m) {
    182      1.15   thorpej 			PFKEY_STATINC(PFKEY_STAT_IN_NOMEM);
    183       1.1  jonathan 			return ENOBUFS;
    184       1.1  jonathan 		}
    185       1.1  jonathan 		m->m_pkthdr.len += sizeof(*pmsg);
    186       1.1  jonathan 
    187       1.1  jonathan 		pmsg = mtod(m, struct sadb_msg *);
    188      1.18    cegger 		memset(pmsg, 0, sizeof(*pmsg));
    189       1.1  jonathan 		pmsg->sadb_msg_version = PF_KEY_V2;
    190       1.1  jonathan 		pmsg->sadb_msg_type = SADB_X_PROMISC;
    191       1.1  jonathan 		pmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
    192       1.1  jonathan 		/* pid and seq? */
    193       1.1  jonathan 
    194      1.15   thorpej 		PFKEY_STATINC(PFKEY_STAT_IN_MSGTYPE + pmsg->sadb_msg_type);
    195       1.1  jonathan 	}
    196       1.1  jonathan 
    197       1.5  jonathan 	if (sbprio == 0)
    198       1.5  jonathan 		ok = sbappendaddr(&rp->rcb_socket->so_rcv,
    199       1.5  jonathan 			       (struct sockaddr *)&key_src, m, NULL);
    200       1.5  jonathan 	else
    201       1.5  jonathan 		ok = sbappendaddrchain(&rp->rcb_socket->so_rcv,
    202       1.5  jonathan 			       (struct sockaddr *)&key_src, m, sbprio);
    203       1.5  jonathan 
    204       1.5  jonathan 	  if (!ok) {
    205      1.15   thorpej 		PFKEY_STATINC(PFKEY_STAT_IN_NOMEM);
    206       1.1  jonathan 		m_freem(m);
    207       1.1  jonathan 		error = ENOBUFS;
    208       1.1  jonathan 	} else
    209       1.1  jonathan 		error = 0;
    210       1.1  jonathan 	sorwakeup(rp->rcb_socket);
    211       1.1  jonathan 	return error;
    212       1.1  jonathan }
    213       1.1  jonathan 
    214       1.1  jonathan /* XXX this interface should be obsoleted. */
    215       1.1  jonathan int
    216      1.14  degroote key_sendup(struct socket *so, struct sadb_msg *msg, u_int len,
    217      1.14  degroote 	   int target)	/*target of the resulting message*/
    218       1.1  jonathan {
    219       1.1  jonathan 	struct mbuf *m, *n, *mprev;
    220       1.1  jonathan 	int tlen;
    221       1.1  jonathan 
    222       1.1  jonathan 	/* sanity check */
    223       1.1  jonathan 	if (so == 0 || msg == 0)
    224       1.8  christos 		panic("key_sendup: NULL pointer was passed");
    225       1.1  jonathan 
    226       1.1  jonathan 	KEYDEBUG(KEYDEBUG_KEY_DUMP,
    227       1.1  jonathan 		printf("key_sendup: \n");
    228       1.1  jonathan 		kdebug_sadb(msg));
    229       1.1  jonathan 
    230       1.1  jonathan 	/*
    231       1.1  jonathan 	 * we increment statistics here, just in case we have ENOBUFS
    232       1.1  jonathan 	 * in this function.
    233       1.1  jonathan 	 */
    234      1.15   thorpej 	{
    235      1.15   thorpej 		uint64_t *ps = PFKEY_STAT_GETREF();
    236      1.15   thorpej 		ps[PFKEY_STAT_IN_TOTAL]++;
    237      1.15   thorpej 		ps[PFKEY_STAT_IN_BYTES] += len;
    238      1.15   thorpej 		ps[PFKEY_STAT_IN_MSGTYPE + msg->sadb_msg_type]++;
    239      1.15   thorpej 		PFKEY_STAT_PUTREF();
    240      1.15   thorpej 	}
    241       1.1  jonathan 
    242       1.1  jonathan 	/*
    243       1.1  jonathan 	 * Get mbuf chain whenever possible (not clusters),
    244       1.1  jonathan 	 * to save socket buffer.  We'll be generating many SADB_ACQUIRE
    245       1.1  jonathan 	 * messages to listening key sockets.  If we simply allocate clusters,
    246       1.1  jonathan 	 * sbappendaddr() will raise ENOBUFS due to too little sbspace().
    247       1.1  jonathan 	 * sbspace() computes # of actual data bytes AND mbuf region.
    248       1.1  jonathan 	 *
    249       1.1  jonathan 	 * TODO: SADB_ACQUIRE filters should be implemented.
    250       1.1  jonathan 	 */
    251       1.1  jonathan 	tlen = len;
    252       1.1  jonathan 	m = mprev = NULL;
    253       1.1  jonathan 	while (tlen > 0) {
    254       1.1  jonathan 		if (tlen == len) {
    255       1.1  jonathan 			MGETHDR(n, M_DONTWAIT, MT_DATA);
    256       1.1  jonathan 			n->m_len = MHLEN;
    257       1.1  jonathan 		} else {
    258       1.1  jonathan 			MGET(n, M_DONTWAIT, MT_DATA);
    259       1.1  jonathan 			n->m_len = MLEN;
    260       1.1  jonathan 		}
    261       1.1  jonathan 		if (!n) {
    262      1.15   thorpej 			PFKEY_STATINC(PFKEY_STAT_IN_NOMEM);
    263       1.1  jonathan 			return ENOBUFS;
    264       1.1  jonathan 		}
    265       1.1  jonathan 		if (tlen >= MCLBYTES) {	/*XXX better threshold? */
    266       1.1  jonathan 			MCLGET(n, M_DONTWAIT);
    267       1.1  jonathan 			if ((n->m_flags & M_EXT) == 0) {
    268       1.1  jonathan 				m_free(n);
    269       1.1  jonathan 				m_freem(m);
    270      1.15   thorpej 				PFKEY_STATINC(PFKEY_STAT_IN_NOMEM);
    271       1.1  jonathan 				return ENOBUFS;
    272       1.1  jonathan 			}
    273       1.1  jonathan 			n->m_len = MCLBYTES;
    274       1.1  jonathan 		}
    275       1.1  jonathan 
    276       1.1  jonathan 		if (tlen < n->m_len)
    277       1.1  jonathan 			n->m_len = tlen;
    278       1.1  jonathan 		n->m_next = NULL;
    279       1.1  jonathan 		if (m == NULL)
    280       1.1  jonathan 			m = mprev = n;
    281       1.1  jonathan 		else {
    282       1.1  jonathan 			mprev->m_next = n;
    283       1.1  jonathan 			mprev = n;
    284       1.1  jonathan 		}
    285       1.1  jonathan 		tlen -= n->m_len;
    286       1.1  jonathan 		n = NULL;
    287       1.1  jonathan 	}
    288       1.1  jonathan 	m->m_pkthdr.len = len;
    289       1.1  jonathan 	m->m_pkthdr.rcvif = NULL;
    290      1.13  degroote 	m_copyback(m, 0, len, msg);
    291       1.1  jonathan 
    292       1.1  jonathan 	/* avoid duplicated statistics */
    293      1.15   thorpej 	{
    294      1.15   thorpej 		uint64_t *ps = PFKEY_STAT_GETREF();
    295      1.15   thorpej 		ps[PFKEY_STAT_IN_TOTAL]--;
    296      1.15   thorpej 		ps[PFKEY_STAT_IN_BYTES] -= len;
    297      1.15   thorpej 		ps[PFKEY_STAT_IN_MSGTYPE + msg->sadb_msg_type]--;
    298      1.15   thorpej 		PFKEY_STAT_PUTREF();
    299      1.15   thorpej 	}
    300       1.1  jonathan 
    301       1.1  jonathan 	return key_sendup_mbuf(so, m, target);
    302       1.1  jonathan }
    303       1.1  jonathan 
    304       1.1  jonathan /* so can be NULL if target != KEY_SENDUP_ONE */
    305       1.1  jonathan int
    306      1.14  degroote key_sendup_mbuf(struct socket *so, struct mbuf *m,
    307      1.14  degroote 		int target/*, sbprio */)
    308       1.1  jonathan {
    309       1.1  jonathan 	struct mbuf *n;
    310       1.1  jonathan 	struct keycb *kp;
    311       1.1  jonathan 	int sendup;
    312       1.1  jonathan 	struct rawcb *rp;
    313       1.1  jonathan 	int error = 0;
    314       1.5  jonathan 	int sbprio = 0; /* XXX should be a parameter */
    315       1.1  jonathan 
    316       1.1  jonathan 	if (m == NULL)
    317       1.8  christos 		panic("key_sendup_mbuf: NULL pointer was passed");
    318       1.1  jonathan 	if (so == NULL && target == KEY_SENDUP_ONE)
    319       1.8  christos 		panic("key_sendup_mbuf: NULL pointer was passed");
    320       1.7     perry 
    321       1.5  jonathan 	/*
    322       1.5  jonathan 	 * RFC 2367 says ACQUIRE and other kernel-generated messages
    323       1.5  jonathan 	 * are special. We treat all KEY_SENDUP_REGISTERED messages
    324       1.5  jonathan 	 * as special, delivering them to all registered sockets
    325       1.5  jonathan 	 * even if the socket is at or above its so->so_rcv.sb_max limits.
    326       1.5  jonathan 	 * The only constraint is that the  so_rcv data fall below
    327       1.5  jonathan 	 * key_registered_sb_max.
    328       1.5  jonathan 	 * Doing that check here avoids reworking every key_sendup_mbuf()
    329       1.5  jonathan 	 * in the short term. . The rework will be done after a technical
    330       1.5  jonathan 	 * conensus that this approach is appropriate.
    331       1.5  jonathan  	 */
    332       1.5  jonathan 	if (target == KEY_SENDUP_REGISTERED) {
    333       1.5  jonathan 		sbprio = SB_PRIO_BESTEFFORT;
    334       1.5  jonathan 	}
    335       1.1  jonathan 
    336      1.15   thorpej 	{
    337      1.15   thorpej 		uint64_t *ps = PFKEY_STAT_GETREF();
    338      1.15   thorpej 		ps[PFKEY_STAT_IN_TOTAL]++;
    339      1.15   thorpej 		ps[PFKEY_STAT_IN_BYTES] += m->m_pkthdr.len;
    340      1.15   thorpej 		PFKEY_STAT_PUTREF();
    341      1.15   thorpej 	}
    342       1.1  jonathan 	if (m->m_len < sizeof(struct sadb_msg)) {
    343       1.1  jonathan #if 1
    344       1.1  jonathan 		m = m_pullup(m, sizeof(struct sadb_msg));
    345       1.1  jonathan 		if (m == NULL) {
    346      1.15   thorpej 			PFKEY_STATINC(PFKEY_STAT_IN_NOMEM);
    347       1.1  jonathan 			return ENOBUFS;
    348       1.1  jonathan 		}
    349       1.1  jonathan #else
    350       1.1  jonathan 		/* don't bother pulling it up just for stats */
    351       1.1  jonathan #endif
    352       1.1  jonathan 	}
    353       1.1  jonathan 	if (m->m_len >= sizeof(struct sadb_msg)) {
    354       1.1  jonathan 		struct sadb_msg *msg;
    355       1.1  jonathan 		msg = mtod(m, struct sadb_msg *);
    356      1.15   thorpej 		PFKEY_STATINC(PFKEY_STAT_IN_MSGTYPE + msg->sadb_msg_type);
    357       1.1  jonathan 	}
    358       1.1  jonathan 
    359       1.1  jonathan 	LIST_FOREACH(rp, &rawcb_list, rcb_list)
    360       1.1  jonathan 	{
    361       1.5  jonathan 		struct socket * kso = rp->rcb_socket;
    362       1.1  jonathan 		if (rp->rcb_proto.sp_family != PF_KEY)
    363       1.1  jonathan 			continue;
    364       1.1  jonathan 		if (rp->rcb_proto.sp_protocol
    365       1.1  jonathan 		 && rp->rcb_proto.sp_protocol != PF_KEY_V2) {
    366       1.1  jonathan 			continue;
    367       1.1  jonathan 		}
    368       1.1  jonathan 
    369       1.1  jonathan 		kp = (struct keycb *)rp;
    370       1.1  jonathan 
    371       1.1  jonathan 		/*
    372       1.1  jonathan 		 * If you are in promiscuous mode, and when you get broadcasted
    373       1.1  jonathan 		 * reply, you'll get two PF_KEY messages.
    374       1.1  jonathan 		 * (based on pf_key (at) inner.net message on 14 Oct 1998)
    375       1.1  jonathan 		 */
    376       1.1  jonathan 		if (((struct keycb *)rp)->kp_promisc) {
    377       1.1  jonathan 			if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
    378       1.5  jonathan 				(void)key_sendup0(rp, n, 1, 0);
    379       1.1  jonathan 				n = NULL;
    380       1.1  jonathan 			}
    381       1.1  jonathan 		}
    382       1.1  jonathan 
    383       1.1  jonathan 		/* the exact target will be processed later */
    384       1.1  jonathan 		if (so && sotorawcb(so) == rp)
    385       1.1  jonathan 			continue;
    386       1.1  jonathan 
    387       1.1  jonathan 		sendup = 0;
    388       1.1  jonathan 		switch (target) {
    389       1.1  jonathan 		case KEY_SENDUP_ONE:
    390       1.1  jonathan 			/* the statement has no effect */
    391       1.1  jonathan 			if (so && sotorawcb(so) == rp)
    392       1.1  jonathan 				sendup++;
    393       1.1  jonathan 			break;
    394       1.1  jonathan 		case KEY_SENDUP_ALL:
    395       1.1  jonathan 			sendup++;
    396       1.1  jonathan 			break;
    397       1.1  jonathan 		case KEY_SENDUP_REGISTERED:
    398       1.5  jonathan 			if (kp->kp_registered) {
    399       1.5  jonathan 				if (kso->so_rcv.sb_cc <= key_registered_sb_max)
    400       1.5  jonathan 					sendup++;
    401       1.5  jonathan 			  	else
    402       1.5  jonathan 			  		printf("keysock: "
    403       1.5  jonathan 					       "registered sendup dropped, "
    404       1.5  jonathan 					       "sb_cc %ld max %d\n",
    405       1.5  jonathan 					       kso->so_rcv.sb_cc,
    406       1.5  jonathan 					       key_registered_sb_max);
    407       1.5  jonathan 			}
    408       1.1  jonathan 			break;
    409       1.1  jonathan 		}
    410      1.15   thorpej 		PFKEY_STATINC(PFKEY_STAT_IN_MSGTARGET + target);
    411       1.1  jonathan 
    412       1.1  jonathan 		if (!sendup)
    413       1.1  jonathan 			continue;
    414       1.1  jonathan 
    415       1.1  jonathan 		if ((n = m_copy(m, 0, (int)M_COPYALL)) == NULL) {
    416       1.1  jonathan 			m_freem(m);
    417      1.15   thorpej 			PFKEY_STATINC(PFKEY_STAT_IN_NOMEM);
    418       1.1  jonathan 			return ENOBUFS;
    419       1.1  jonathan 		}
    420       1.1  jonathan 
    421       1.5  jonathan 		if ((error = key_sendup0(rp, n, 0, 0)) != 0) {
    422       1.1  jonathan 			m_freem(m);
    423       1.1  jonathan 			return error;
    424       1.1  jonathan 		}
    425       1.1  jonathan 
    426       1.1  jonathan 		n = NULL;
    427       1.1  jonathan 	}
    428       1.1  jonathan 
    429       1.5  jonathan 	/* The 'later' time for processing the exact target has arrived */
    430       1.1  jonathan 	if (so) {
    431       1.5  jonathan 		error = key_sendup0(sotorawcb(so), m, 0, sbprio);
    432       1.1  jonathan 		m = NULL;
    433       1.1  jonathan 	} else {
    434       1.1  jonathan 		error = 0;
    435       1.1  jonathan 		m_freem(m);
    436       1.1  jonathan 	}
    437       1.1  jonathan 	return error;
    438       1.1  jonathan }
    439       1.1  jonathan 
    440       1.1  jonathan #ifdef __FreeBSD__
    441       1.1  jonathan 
    442       1.1  jonathan /*
    443       1.1  jonathan  * key_abort()
    444       1.1  jonathan  * derived from net/rtsock.c:rts_abort()
    445       1.1  jonathan  */
    446       1.1  jonathan static int
    447       1.1  jonathan key_abort(struct socket *so)
    448       1.1  jonathan {
    449       1.1  jonathan 	int s, error;
    450       1.1  jonathan 	s = splnet(); 	/* FreeBSD */
    451       1.1  jonathan 	error = raw_usrreqs.pru_abort(so);
    452       1.1  jonathan 	splx(s);
    453       1.1  jonathan 	return error;
    454       1.1  jonathan }
    455       1.1  jonathan 
    456       1.1  jonathan /*
    457       1.1  jonathan  * key_attach()
    458       1.1  jonathan  * derived from net/rtsock.c:rts_attach()
    459       1.1  jonathan  */
    460       1.1  jonathan static int
    461       1.1  jonathan key_attach(struct socket *so, int proto, struct proc *td)
    462       1.1  jonathan {
    463       1.1  jonathan 	struct keycb *kp;
    464       1.1  jonathan 	int s, error;
    465       1.1  jonathan 
    466       1.1  jonathan 	if (sotorawcb(so) != 0)
    467       1.1  jonathan 		return EISCONN;	/* XXX panic? */
    468       1.1  jonathan 	kp = (struct keycb *)malloc(sizeof *kp, M_PCB, M_WAITOK|M_ZERO); /* XXX */
    469       1.1  jonathan 	if (kp == 0)
    470       1.1  jonathan 		return ENOBUFS;
    471       1.1  jonathan 
    472       1.1  jonathan 	/*
    473       1.1  jonathan 	 * The spl[soft]net() is necessary to block protocols from sending
    474       1.1  jonathan 	 * error notifications (like RTM_REDIRECT or RTM_LOSING) while
    475       1.1  jonathan 	 * this PCB is extant but incompletely initialized.
    476       1.1  jonathan 	 * Probably we should try to do more of this work beforehand and
    477       1.1  jonathan 	 * eliminate the spl.
    478       1.1  jonathan 	 */
    479       1.1  jonathan 	s = splnet();	/* FreeBSD */
    480      1.13  degroote 	so->so_pcb = kp;
    481       1.1  jonathan 	error = raw_usrreqs.pru_attach(so, proto, td);
    482       1.1  jonathan 	kp = (struct keycb *)sotorawcb(so);
    483       1.1  jonathan 	if (error) {
    484       1.1  jonathan 		free(kp, M_PCB);
    485      1.13  degroote 		so->so_pcb = NULL;
    486       1.1  jonathan 		splx(s);
    487       1.1  jonathan 		return error;
    488       1.1  jonathan 	}
    489       1.1  jonathan 
    490       1.1  jonathan 	kp->kp_promisc = kp->kp_registered = 0;
    491       1.1  jonathan 
    492       1.1  jonathan 	if (kp->kp_raw.rcb_proto.sp_protocol == PF_KEY) /* XXX: AF_KEY */
    493       1.1  jonathan 		key_cb.key_count++;
    494       1.1  jonathan 	key_cb.any_count++;
    495       1.1  jonathan 	kp->kp_raw.rcb_laddr = &key_src;
    496       1.1  jonathan 	kp->kp_raw.rcb_faddr = &key_dst;
    497       1.1  jonathan 	soisconnected(so);
    498       1.1  jonathan 	so->so_options |= SO_USELOOPBACK;
    499       1.1  jonathan 
    500       1.1  jonathan 	splx(s);
    501       1.1  jonathan 	return 0;
    502       1.1  jonathan }
    503       1.1  jonathan 
    504       1.1  jonathan /*
    505       1.1  jonathan  * key_bind()
    506       1.1  jonathan  * derived from net/rtsock.c:rts_bind()
    507       1.1  jonathan  */
    508       1.1  jonathan static int
    509       1.1  jonathan key_bind(struct socket *so, struct sockaddr *nam, struct proc *td)
    510       1.1  jonathan {
    511       1.1  jonathan 	int s, error;
    512       1.1  jonathan 	s = splnet();	/* FreeBSD */
    513       1.1  jonathan 	error = raw_usrreqs.pru_bind(so, nam, td); /* xxx just EINVAL */
    514       1.1  jonathan 	splx(s);
    515       1.1  jonathan 	return error;
    516       1.1  jonathan }
    517       1.1  jonathan 
    518       1.1  jonathan /*
    519       1.1  jonathan  * key_connect()
    520       1.1  jonathan  * derived from net/rtsock.c:rts_connect()
    521       1.1  jonathan  */
    522       1.1  jonathan static int
    523       1.1  jonathan key_connect(struct socket *so, struct sockaddr *nam, struct proc *td)
    524       1.1  jonathan {
    525       1.1  jonathan 	int s, error;
    526       1.1  jonathan 	s = splnet();	/* FreeBSD */
    527       1.1  jonathan 	error = raw_usrreqs.pru_connect(so, nam, td); /* XXX just EINVAL */
    528       1.1  jonathan 	splx(s);
    529       1.1  jonathan 	return error;
    530       1.1  jonathan }
    531       1.1  jonathan 
    532       1.1  jonathan /*
    533       1.1  jonathan  * key_detach()
    534       1.1  jonathan  * derived from net/rtsock.c:rts_detach()
    535       1.1  jonathan  */
    536       1.1  jonathan static int
    537       1.1  jonathan key_detach(struct socket *so)
    538       1.1  jonathan {
    539       1.1  jonathan 	struct keycb *kp = (struct keycb *)sotorawcb(so);
    540       1.1  jonathan 	int s, error;
    541       1.1  jonathan 
    542       1.1  jonathan 	s = splnet();	/* FreeBSD */
    543       1.1  jonathan 	if (kp != 0) {
    544       1.1  jonathan 		if (kp->kp_raw.rcb_proto.sp_protocol
    545       1.1  jonathan 		    == PF_KEY) /* XXX: AF_KEY */
    546       1.1  jonathan 			key_cb.key_count--;
    547       1.1  jonathan 		key_cb.any_count--;
    548       1.1  jonathan 
    549       1.1  jonathan 		key_freereg(so);
    550       1.1  jonathan 	}
    551       1.1  jonathan 	error = raw_usrreqs.pru_detach(so);
    552       1.1  jonathan 	splx(s);
    553       1.1  jonathan 	return error;
    554       1.1  jonathan }
    555       1.1  jonathan 
    556       1.1  jonathan /*
    557       1.1  jonathan  * key_disconnect()
    558       1.1  jonathan  * derived from net/rtsock.c:key_disconnect()
    559       1.1  jonathan  */
    560       1.1  jonathan static int
    561       1.1  jonathan key_disconnect(struct socket *so)
    562       1.1  jonathan {
    563       1.1  jonathan 	int s, error;
    564       1.1  jonathan 	s = splnet();	/* FreeBSD */
    565       1.1  jonathan 	error = raw_usrreqs.pru_disconnect(so);
    566       1.1  jonathan 	splx(s);
    567       1.1  jonathan 	return error;
    568       1.1  jonathan }
    569       1.1  jonathan 
    570       1.1  jonathan /*
    571       1.1  jonathan  * key_peeraddr()
    572       1.1  jonathan  * derived from net/rtsock.c:rts_peeraddr()
    573       1.1  jonathan  */
    574       1.1  jonathan static int
    575       1.1  jonathan key_peeraddr(struct socket *so, struct sockaddr **nam)
    576       1.1  jonathan {
    577       1.1  jonathan 	int s, error;
    578       1.1  jonathan 	s = splnet();	/* FreeBSD */
    579       1.1  jonathan 	error = raw_usrreqs.pru_peeraddr(so, nam);
    580       1.1  jonathan 	splx(s);
    581       1.1  jonathan 	return error;
    582       1.1  jonathan }
    583       1.1  jonathan 
    584       1.1  jonathan /*
    585       1.1  jonathan  * key_send()
    586       1.1  jonathan  * derived from net/rtsock.c:rts_send()
    587       1.1  jonathan  */
    588       1.1  jonathan static int
    589       1.1  jonathan key_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
    590       1.1  jonathan 	 struct mbuf *control, struct proc *td)
    591       1.1  jonathan {
    592       1.1  jonathan 	int s, error;
    593       1.1  jonathan 	s = splnet();	/* FreeBSD */
    594       1.1  jonathan 	error = raw_usrreqs.pru_send(so, flags, m, nam, control, td);
    595       1.1  jonathan 	splx(s);
    596       1.1  jonathan 	return error;
    597       1.1  jonathan }
    598       1.1  jonathan 
    599       1.1  jonathan /*
    600       1.1  jonathan  * key_shutdown()
    601       1.1  jonathan  * derived from net/rtsock.c:rts_shutdown()
    602       1.1  jonathan  */
    603       1.1  jonathan static int
    604       1.1  jonathan key_shutdown(struct socket *so)
    605       1.1  jonathan {
    606       1.1  jonathan 	int s, error;
    607       1.1  jonathan 	s = splnet();	/* FreeBSD */
    608       1.1  jonathan 	error = raw_usrreqs.pru_shutdown(so);
    609       1.1  jonathan 	splx(s);
    610       1.1  jonathan 	return error;
    611       1.1  jonathan }
    612       1.1  jonathan 
    613       1.1  jonathan /*
    614       1.1  jonathan  * key_sockaddr()
    615       1.1  jonathan  * derived from net/rtsock.c:rts_sockaddr()
    616       1.1  jonathan  */
    617       1.1  jonathan static int
    618       1.1  jonathan key_sockaddr(struct socket *so, struct sockaddr **nam)
    619       1.1  jonathan {
    620       1.1  jonathan 	int s, error;
    621       1.1  jonathan 	s = splnet();	/* FreeBSD */
    622       1.1  jonathan 	error = raw_usrreqs.pru_sockaddr(so, nam);
    623       1.1  jonathan 	splx(s);
    624       1.1  jonathan 	return error;
    625       1.1  jonathan }
    626       1.1  jonathan #else /*!__FreeBSD__ -- traditional proto_usrreq() switch */
    627       1.1  jonathan 
    628       1.1  jonathan /*
    629       1.1  jonathan  * key_usrreq()
    630       1.1  jonathan  * derived from net/rtsock.c:route_usrreq()
    631       1.1  jonathan  */
    632       1.1  jonathan int
    633      1.14  degroote key_usrreq(struct socket *so, int req,struct mbuf *m, struct mbuf *nam,
    634      1.14  degroote 	   struct mbuf *control, struct lwp *l)
    635       1.1  jonathan {
    636       1.1  jonathan 	int error = 0;
    637       1.1  jonathan 	struct keycb *kp = (struct keycb *)sotorawcb(so);
    638       1.1  jonathan 	int s;
    639       1.1  jonathan 
    640       1.1  jonathan 	s = splsoftnet();
    641       1.1  jonathan 	if (req == PRU_ATTACH) {
    642       1.1  jonathan 		kp = (struct keycb *)malloc(sizeof(*kp), M_PCB, M_WAITOK);
    643      1.16        ad 		sosetlock(so);
    644      1.13  degroote 		so->so_pcb = kp;
    645       1.1  jonathan 		if (so->so_pcb)
    646      1.18    cegger 			memset(so->so_pcb, 0, sizeof(*kp));
    647       1.1  jonathan 	}
    648       1.1  jonathan 	if (req == PRU_DETACH && kp) {
    649       1.1  jonathan 		int af = kp->kp_raw.rcb_proto.sp_protocol;
    650       1.1  jonathan 		if (af == PF_KEY) /* XXX: AF_KEY */
    651       1.1  jonathan 			key_cb.key_count--;
    652       1.1  jonathan 		key_cb.any_count--;
    653       1.1  jonathan 
    654       1.1  jonathan 		key_freereg(so);
    655       1.1  jonathan 	}
    656       1.1  jonathan 
    657       1.9  christos 	error = raw_usrreq(so, req, m, nam, control, l);
    658       1.1  jonathan 	m = control = NULL;	/* reclaimed in raw_usrreq */
    659       1.1  jonathan 	kp = (struct keycb *)sotorawcb(so);
    660       1.1  jonathan 	if (req == PRU_ATTACH && kp) {
    661       1.1  jonathan 		int af = kp->kp_raw.rcb_proto.sp_protocol;
    662       1.1  jonathan 		if (error) {
    663      1.15   thorpej 			PFKEY_STATINC(PFKEY_STAT_SOCKERR);
    664      1.13  degroote 			free(kp, M_PCB);
    665      1.13  degroote 			so->so_pcb = NULL;
    666       1.1  jonathan 			splx(s);
    667       1.1  jonathan 			return (error);
    668       1.1  jonathan 		}
    669       1.1  jonathan 
    670       1.1  jonathan 		kp->kp_promisc = kp->kp_registered = 0;
    671       1.1  jonathan 
    672       1.1  jonathan 		if (af == PF_KEY) /* XXX: AF_KEY */
    673       1.1  jonathan 			key_cb.key_count++;
    674       1.1  jonathan 		key_cb.any_count++;
    675       1.1  jonathan 		kp->kp_raw.rcb_laddr = &key_src;
    676       1.1  jonathan 		kp->kp_raw.rcb_faddr = &key_dst;
    677       1.1  jonathan 		soisconnected(so);
    678       1.1  jonathan 		so->so_options |= SO_USELOOPBACK;
    679       1.1  jonathan 	}
    680       1.1  jonathan 	splx(s);
    681       1.1  jonathan 	return (error);
    682       1.1  jonathan }
    683       1.1  jonathan #endif /*!__FreeBSD__*/
    684       1.1  jonathan 
    685       1.1  jonathan /* sysctl */
    686       1.1  jonathan #ifdef SYSCTL_NODE
    687       1.1  jonathan SYSCTL_NODE(_net, PF_KEY, key, CTLFLAG_RW, 0, "Key Family");
    688       1.2       tls #endif /* SYSCTL_NODE */
    689       1.1  jonathan 
    690       1.1  jonathan /*
    691       1.1  jonathan  * Definitions of protocols supported in the KEY domain.
    692       1.1  jonathan  */
    693       1.1  jonathan 
    694       1.6      matt #ifdef __FreeBSD__
    695       1.1  jonathan extern struct domain keydomain;
    696       1.1  jonathan 
    697       1.1  jonathan struct pr_usrreqs key_usrreqs = {
    698       1.1  jonathan 	key_abort, pru_accept_notsupp, key_attach, key_bind,
    699       1.1  jonathan 	key_connect,
    700       1.1  jonathan 	pru_connect2_notsupp, pru_control_notsupp, key_detach,
    701       1.1  jonathan 	key_disconnect, pru_listen_notsupp, key_peeraddr,
    702       1.1  jonathan 	pru_rcvd_notsupp,
    703       1.1  jonathan 	pru_rcvoob_notsupp, key_send, pru_sense_null, key_shutdown,
    704       1.1  jonathan 	key_sockaddr, sosend, soreceive, sopoll
    705       1.1  jonathan };
    706       1.1  jonathan 
    707       1.1  jonathan struct protosw keysw[] = {
    708       1.1  jonathan { SOCK_RAW,	&keydomain,	PF_KEY_V2,	PR_ATOMIC|PR_ADDR,
    709       1.1  jonathan   0,		(pr_output_t *)key_output,	raw_ctlinput, 0,
    710       1.1  jonathan   0,
    711       1.1  jonathan   raw_init,	0,		0,		0,
    712       1.1  jonathan   &key_usrreqs
    713       1.1  jonathan }
    714       1.1  jonathan };
    715       1.1  jonathan 
    716       1.1  jonathan static void
    717       1.1  jonathan key_init0(void)
    718       1.1  jonathan {
    719      1.18    cegger 	memset(&key_cb, 0, sizeof(key_cb));
    720       1.1  jonathan 	key_init();
    721       1.1  jonathan }
    722       1.1  jonathan 
    723       1.1  jonathan struct domain keydomain =
    724       1.1  jonathan     { PF_KEY, "key", key_init0, 0, 0,
    725       1.1  jonathan       keysw, &keysw[sizeof(keysw)/sizeof(keysw[0])] };
    726       1.1  jonathan 
    727       1.1  jonathan DOMAIN_SET(key);
    728       1.1  jonathan 
    729       1.1  jonathan #else /* !__FreeBSD__ */
    730       1.1  jonathan 
    731       1.6      matt DOMAIN_DEFINE(keydomain);
    732       1.1  jonathan 
    733      1.10      matt const struct protosw keysw[] = {
    734      1.10      matt     {
    735      1.10      matt 	.pr_type = SOCK_RAW,
    736      1.10      matt 	.pr_domain = &keydomain,
    737      1.10      matt 	.pr_protocol = PF_KEY_V2,
    738      1.10      matt 	.pr_flags = PR_ATOMIC|PR_ADDR,
    739      1.10      matt 	.pr_output = key_output,
    740      1.10      matt 	.pr_ctlinput = raw_ctlinput,
    741      1.10      matt 	.pr_usrreq = key_usrreq,
    742      1.10      matt 	.pr_init = raw_init,
    743      1.10      matt     }
    744       1.1  jonathan };
    745       1.1  jonathan 
    746      1.10      matt struct domain keydomain = {
    747      1.10      matt     .dom_family = PF_KEY,
    748      1.10      matt     .dom_name = "key",
    749      1.10      matt     .dom_init = key_init,
    750      1.10      matt     .dom_protosw = keysw,
    751      1.10      matt     .dom_protoswNPROTOSW = &keysw[__arraycount(keysw)],
    752      1.10      matt };
    753       1.1  jonathan 
    754       1.1  jonathan #endif
    755