Home | History | Annotate | Line # | Download | only in netipsec
keysock.c revision 1.16.10.1
      1  1.16.10.1     skrll /*	$NetBSD: keysock.c,v 1.16.10.1 2009/04/28 07:37:34 skrll 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.16.10.1     skrll __KERNEL_RCSID(0, "$NetBSD: keysock.c,v 1.16.10.1 2009/04/28 07:37:34 skrll 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.16.10.1     skrll static int key_sendup0(struct rawcb *, struct mbuf *, int, int);
     87        1.1  jonathan 
     88        1.5  jonathan int key_registered_sb_max = (NMBCLUSTERS * 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 			m_freem(m);
    184        1.1  jonathan 			return ENOBUFS;
    185        1.1  jonathan 		}
    186        1.1  jonathan 		m->m_pkthdr.len += sizeof(*pmsg);
    187        1.1  jonathan 
    188        1.1  jonathan 		pmsg = mtod(m, struct sadb_msg *);
    189  1.16.10.1     skrll 		memset(pmsg, 0, sizeof(*pmsg));
    190        1.1  jonathan 		pmsg->sadb_msg_version = PF_KEY_V2;
    191        1.1  jonathan 		pmsg->sadb_msg_type = SADB_X_PROMISC;
    192        1.1  jonathan 		pmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
    193        1.1  jonathan 		/* pid and seq? */
    194        1.1  jonathan 
    195       1.15   thorpej 		PFKEY_STATINC(PFKEY_STAT_IN_MSGTYPE + pmsg->sadb_msg_type);
    196        1.1  jonathan 	}
    197        1.1  jonathan 
    198        1.5  jonathan 	if (sbprio == 0)
    199        1.5  jonathan 		ok = sbappendaddr(&rp->rcb_socket->so_rcv,
    200        1.5  jonathan 			       (struct sockaddr *)&key_src, m, NULL);
    201        1.5  jonathan 	else
    202        1.5  jonathan 		ok = sbappendaddrchain(&rp->rcb_socket->so_rcv,
    203        1.5  jonathan 			       (struct sockaddr *)&key_src, m, sbprio);
    204        1.5  jonathan 
    205        1.5  jonathan 	  if (!ok) {
    206       1.15   thorpej 		PFKEY_STATINC(PFKEY_STAT_IN_NOMEM);
    207        1.1  jonathan 		m_freem(m);
    208        1.1  jonathan 		error = ENOBUFS;
    209        1.1  jonathan 	} else
    210        1.1  jonathan 		error = 0;
    211        1.1  jonathan 	sorwakeup(rp->rcb_socket);
    212        1.1  jonathan 	return error;
    213        1.1  jonathan }
    214        1.1  jonathan 
    215        1.1  jonathan /* XXX this interface should be obsoleted. */
    216        1.1  jonathan int
    217       1.14  degroote key_sendup(struct socket *so, struct sadb_msg *msg, u_int len,
    218       1.14  degroote 	   int target)	/*target of the resulting message*/
    219        1.1  jonathan {
    220        1.1  jonathan 	struct mbuf *m, *n, *mprev;
    221        1.1  jonathan 	int tlen;
    222        1.1  jonathan 
    223        1.1  jonathan 	/* sanity check */
    224        1.1  jonathan 	if (so == 0 || msg == 0)
    225        1.8  christos 		panic("key_sendup: NULL pointer was passed");
    226        1.1  jonathan 
    227        1.1  jonathan 	KEYDEBUG(KEYDEBUG_KEY_DUMP,
    228        1.1  jonathan 		printf("key_sendup: \n");
    229        1.1  jonathan 		kdebug_sadb(msg));
    230        1.1  jonathan 
    231        1.1  jonathan 	/*
    232        1.1  jonathan 	 * we increment statistics here, just in case we have ENOBUFS
    233        1.1  jonathan 	 * in this function.
    234        1.1  jonathan 	 */
    235       1.15   thorpej 	{
    236       1.15   thorpej 		uint64_t *ps = PFKEY_STAT_GETREF();
    237       1.15   thorpej 		ps[PFKEY_STAT_IN_TOTAL]++;
    238       1.15   thorpej 		ps[PFKEY_STAT_IN_BYTES] += len;
    239       1.15   thorpej 		ps[PFKEY_STAT_IN_MSGTYPE + msg->sadb_msg_type]++;
    240       1.15   thorpej 		PFKEY_STAT_PUTREF();
    241       1.15   thorpej 	}
    242        1.1  jonathan 
    243        1.1  jonathan 	/*
    244        1.1  jonathan 	 * Get mbuf chain whenever possible (not clusters),
    245        1.1  jonathan 	 * to save socket buffer.  We'll be generating many SADB_ACQUIRE
    246        1.1  jonathan 	 * messages to listening key sockets.  If we simply allocate clusters,
    247        1.1  jonathan 	 * sbappendaddr() will raise ENOBUFS due to too little sbspace().
    248        1.1  jonathan 	 * sbspace() computes # of actual data bytes AND mbuf region.
    249        1.1  jonathan 	 *
    250        1.1  jonathan 	 * TODO: SADB_ACQUIRE filters should be implemented.
    251        1.1  jonathan 	 */
    252        1.1  jonathan 	tlen = len;
    253        1.1  jonathan 	m = mprev = NULL;
    254        1.1  jonathan 	while (tlen > 0) {
    255        1.1  jonathan 		if (tlen == len) {
    256        1.1  jonathan 			MGETHDR(n, M_DONTWAIT, MT_DATA);
    257        1.1  jonathan 			n->m_len = MHLEN;
    258        1.1  jonathan 		} else {
    259        1.1  jonathan 			MGET(n, M_DONTWAIT, MT_DATA);
    260        1.1  jonathan 			n->m_len = MLEN;
    261        1.1  jonathan 		}
    262        1.1  jonathan 		if (!n) {
    263       1.15   thorpej 			PFKEY_STATINC(PFKEY_STAT_IN_NOMEM);
    264        1.1  jonathan 			return ENOBUFS;
    265        1.1  jonathan 		}
    266        1.1  jonathan 		if (tlen >= MCLBYTES) {	/*XXX better threshold? */
    267        1.1  jonathan 			MCLGET(n, M_DONTWAIT);
    268        1.1  jonathan 			if ((n->m_flags & M_EXT) == 0) {
    269        1.1  jonathan 				m_free(n);
    270        1.1  jonathan 				m_freem(m);
    271       1.15   thorpej 				PFKEY_STATINC(PFKEY_STAT_IN_NOMEM);
    272        1.1  jonathan 				return ENOBUFS;
    273        1.1  jonathan 			}
    274        1.1  jonathan 			n->m_len = MCLBYTES;
    275        1.1  jonathan 		}
    276        1.1  jonathan 
    277        1.1  jonathan 		if (tlen < n->m_len)
    278        1.1  jonathan 			n->m_len = tlen;
    279        1.1  jonathan 		n->m_next = NULL;
    280        1.1  jonathan 		if (m == NULL)
    281        1.1  jonathan 			m = mprev = n;
    282        1.1  jonathan 		else {
    283        1.1  jonathan 			mprev->m_next = n;
    284        1.1  jonathan 			mprev = n;
    285        1.1  jonathan 		}
    286        1.1  jonathan 		tlen -= n->m_len;
    287        1.1  jonathan 		n = NULL;
    288        1.1  jonathan 	}
    289        1.1  jonathan 	m->m_pkthdr.len = len;
    290        1.1  jonathan 	m->m_pkthdr.rcvif = NULL;
    291       1.13  degroote 	m_copyback(m, 0, len, msg);
    292        1.1  jonathan 
    293        1.1  jonathan 	/* avoid duplicated statistics */
    294       1.15   thorpej 	{
    295       1.15   thorpej 		uint64_t *ps = PFKEY_STAT_GETREF();
    296       1.15   thorpej 		ps[PFKEY_STAT_IN_TOTAL]--;
    297       1.15   thorpej 		ps[PFKEY_STAT_IN_BYTES] -= len;
    298       1.15   thorpej 		ps[PFKEY_STAT_IN_MSGTYPE + msg->sadb_msg_type]--;
    299       1.15   thorpej 		PFKEY_STAT_PUTREF();
    300       1.15   thorpej 	}
    301        1.1  jonathan 
    302        1.1  jonathan 	return key_sendup_mbuf(so, m, target);
    303        1.1  jonathan }
    304        1.1  jonathan 
    305        1.1  jonathan /* so can be NULL if target != KEY_SENDUP_ONE */
    306        1.1  jonathan int
    307       1.14  degroote key_sendup_mbuf(struct socket *so, struct mbuf *m,
    308       1.14  degroote 		int target/*, sbprio */)
    309        1.1  jonathan {
    310        1.1  jonathan 	struct mbuf *n;
    311        1.1  jonathan 	struct keycb *kp;
    312        1.1  jonathan 	int sendup;
    313        1.1  jonathan 	struct rawcb *rp;
    314        1.1  jonathan 	int error = 0;
    315        1.5  jonathan 	int sbprio = 0; /* XXX should be a parameter */
    316        1.1  jonathan 
    317        1.1  jonathan 	if (m == NULL)
    318        1.8  christos 		panic("key_sendup_mbuf: NULL pointer was passed");
    319        1.1  jonathan 	if (so == NULL && target == KEY_SENDUP_ONE)
    320        1.8  christos 		panic("key_sendup_mbuf: NULL pointer was passed");
    321        1.7     perry 
    322        1.5  jonathan 	/*
    323        1.5  jonathan 	 * RFC 2367 says ACQUIRE and other kernel-generated messages
    324        1.5  jonathan 	 * are special. We treat all KEY_SENDUP_REGISTERED messages
    325        1.5  jonathan 	 * as special, delivering them to all registered sockets
    326        1.5  jonathan 	 * even if the socket is at or above its so->so_rcv.sb_max limits.
    327        1.5  jonathan 	 * The only constraint is that the  so_rcv data fall below
    328        1.5  jonathan 	 * key_registered_sb_max.
    329        1.5  jonathan 	 * Doing that check here avoids reworking every key_sendup_mbuf()
    330        1.5  jonathan 	 * in the short term. . The rework will be done after a technical
    331        1.5  jonathan 	 * conensus that this approach is appropriate.
    332        1.5  jonathan  	 */
    333        1.5  jonathan 	if (target == KEY_SENDUP_REGISTERED) {
    334        1.5  jonathan 		sbprio = SB_PRIO_BESTEFFORT;
    335        1.5  jonathan 	}
    336        1.1  jonathan 
    337       1.15   thorpej 	{
    338       1.15   thorpej 		uint64_t *ps = PFKEY_STAT_GETREF();
    339       1.15   thorpej 		ps[PFKEY_STAT_IN_TOTAL]++;
    340       1.15   thorpej 		ps[PFKEY_STAT_IN_BYTES] += m->m_pkthdr.len;
    341       1.15   thorpej 		PFKEY_STAT_PUTREF();
    342       1.15   thorpej 	}
    343        1.1  jonathan 	if (m->m_len < sizeof(struct sadb_msg)) {
    344        1.1  jonathan #if 1
    345        1.1  jonathan 		m = m_pullup(m, sizeof(struct sadb_msg));
    346        1.1  jonathan 		if (m == NULL) {
    347       1.15   thorpej 			PFKEY_STATINC(PFKEY_STAT_IN_NOMEM);
    348        1.1  jonathan 			return ENOBUFS;
    349        1.1  jonathan 		}
    350        1.1  jonathan #else
    351        1.1  jonathan 		/* don't bother pulling it up just for stats */
    352        1.1  jonathan #endif
    353        1.1  jonathan 	}
    354        1.1  jonathan 	if (m->m_len >= sizeof(struct sadb_msg)) {
    355        1.1  jonathan 		struct sadb_msg *msg;
    356        1.1  jonathan 		msg = mtod(m, struct sadb_msg *);
    357       1.15   thorpej 		PFKEY_STATINC(PFKEY_STAT_IN_MSGTYPE + msg->sadb_msg_type);
    358        1.1  jonathan 	}
    359        1.1  jonathan 
    360        1.1  jonathan 	LIST_FOREACH(rp, &rawcb_list, rcb_list)
    361        1.1  jonathan 	{
    362        1.5  jonathan 		struct socket * kso = rp->rcb_socket;
    363        1.1  jonathan 		if (rp->rcb_proto.sp_family != PF_KEY)
    364        1.1  jonathan 			continue;
    365        1.1  jonathan 		if (rp->rcb_proto.sp_protocol
    366        1.1  jonathan 		 && rp->rcb_proto.sp_protocol != PF_KEY_V2) {
    367        1.1  jonathan 			continue;
    368        1.1  jonathan 		}
    369        1.1  jonathan 
    370        1.1  jonathan 		kp = (struct keycb *)rp;
    371        1.1  jonathan 
    372        1.1  jonathan 		/*
    373        1.1  jonathan 		 * If you are in promiscuous mode, and when you get broadcasted
    374        1.1  jonathan 		 * reply, you'll get two PF_KEY messages.
    375        1.1  jonathan 		 * (based on pf_key (at) inner.net message on 14 Oct 1998)
    376        1.1  jonathan 		 */
    377        1.1  jonathan 		if (((struct keycb *)rp)->kp_promisc) {
    378        1.1  jonathan 			if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
    379        1.5  jonathan 				(void)key_sendup0(rp, n, 1, 0);
    380        1.1  jonathan 				n = NULL;
    381        1.1  jonathan 			}
    382        1.1  jonathan 		}
    383        1.1  jonathan 
    384        1.1  jonathan 		/* the exact target will be processed later */
    385        1.1  jonathan 		if (so && sotorawcb(so) == rp)
    386        1.1  jonathan 			continue;
    387        1.1  jonathan 
    388        1.1  jonathan 		sendup = 0;
    389        1.1  jonathan 		switch (target) {
    390        1.1  jonathan 		case KEY_SENDUP_ONE:
    391        1.1  jonathan 			/* the statement has no effect */
    392        1.1  jonathan 			if (so && sotorawcb(so) == rp)
    393        1.1  jonathan 				sendup++;
    394        1.1  jonathan 			break;
    395        1.1  jonathan 		case KEY_SENDUP_ALL:
    396        1.1  jonathan 			sendup++;
    397        1.1  jonathan 			break;
    398        1.1  jonathan 		case KEY_SENDUP_REGISTERED:
    399        1.5  jonathan 			if (kp->kp_registered) {
    400        1.5  jonathan 				if (kso->so_rcv.sb_cc <= key_registered_sb_max)
    401        1.5  jonathan 					sendup++;
    402        1.5  jonathan 			  	else
    403        1.5  jonathan 			  		printf("keysock: "
    404        1.5  jonathan 					       "registered sendup dropped, "
    405        1.5  jonathan 					       "sb_cc %ld max %d\n",
    406        1.5  jonathan 					       kso->so_rcv.sb_cc,
    407        1.5  jonathan 					       key_registered_sb_max);
    408        1.5  jonathan 			}
    409        1.1  jonathan 			break;
    410        1.1  jonathan 		}
    411       1.15   thorpej 		PFKEY_STATINC(PFKEY_STAT_IN_MSGTARGET + target);
    412        1.1  jonathan 
    413        1.1  jonathan 		if (!sendup)
    414        1.1  jonathan 			continue;
    415        1.1  jonathan 
    416        1.1  jonathan 		if ((n = m_copy(m, 0, (int)M_COPYALL)) == NULL) {
    417        1.1  jonathan 			m_freem(m);
    418       1.15   thorpej 			PFKEY_STATINC(PFKEY_STAT_IN_NOMEM);
    419        1.1  jonathan 			return ENOBUFS;
    420        1.1  jonathan 		}
    421        1.1  jonathan 
    422        1.5  jonathan 		if ((error = key_sendup0(rp, n, 0, 0)) != 0) {
    423        1.1  jonathan 			m_freem(m);
    424        1.1  jonathan 			return error;
    425        1.1  jonathan 		}
    426        1.1  jonathan 
    427        1.1  jonathan 		n = NULL;
    428        1.1  jonathan 	}
    429        1.1  jonathan 
    430        1.5  jonathan 	/* The 'later' time for processing the exact target has arrived */
    431        1.1  jonathan 	if (so) {
    432        1.5  jonathan 		error = key_sendup0(sotorawcb(so), m, 0, sbprio);
    433        1.1  jonathan 		m = NULL;
    434        1.1  jonathan 	} else {
    435        1.1  jonathan 		error = 0;
    436        1.1  jonathan 		m_freem(m);
    437        1.1  jonathan 	}
    438        1.1  jonathan 	return error;
    439        1.1  jonathan }
    440        1.1  jonathan 
    441        1.1  jonathan #ifdef __FreeBSD__
    442        1.1  jonathan 
    443        1.1  jonathan /*
    444        1.1  jonathan  * key_abort()
    445        1.1  jonathan  * derived from net/rtsock.c:rts_abort()
    446        1.1  jonathan  */
    447        1.1  jonathan static int
    448        1.1  jonathan key_abort(struct socket *so)
    449        1.1  jonathan {
    450        1.1  jonathan 	int s, error;
    451        1.1  jonathan 	s = splnet(); 	/* FreeBSD */
    452        1.1  jonathan 	error = raw_usrreqs.pru_abort(so);
    453        1.1  jonathan 	splx(s);
    454        1.1  jonathan 	return error;
    455        1.1  jonathan }
    456        1.1  jonathan 
    457        1.1  jonathan /*
    458        1.1  jonathan  * key_attach()
    459        1.1  jonathan  * derived from net/rtsock.c:rts_attach()
    460        1.1  jonathan  */
    461        1.1  jonathan static int
    462        1.1  jonathan key_attach(struct socket *so, int proto, struct proc *td)
    463        1.1  jonathan {
    464        1.1  jonathan 	struct keycb *kp;
    465        1.1  jonathan 	int s, error;
    466        1.1  jonathan 
    467        1.1  jonathan 	if (sotorawcb(so) != 0)
    468        1.1  jonathan 		return EISCONN;	/* XXX panic? */
    469        1.1  jonathan 	kp = (struct keycb *)malloc(sizeof *kp, M_PCB, M_WAITOK|M_ZERO); /* XXX */
    470        1.1  jonathan 	if (kp == 0)
    471        1.1  jonathan 		return ENOBUFS;
    472        1.1  jonathan 
    473        1.1  jonathan 	/*
    474        1.1  jonathan 	 * The spl[soft]net() is necessary to block protocols from sending
    475        1.1  jonathan 	 * error notifications (like RTM_REDIRECT or RTM_LOSING) while
    476        1.1  jonathan 	 * this PCB is extant but incompletely initialized.
    477        1.1  jonathan 	 * Probably we should try to do more of this work beforehand and
    478        1.1  jonathan 	 * eliminate the spl.
    479        1.1  jonathan 	 */
    480        1.1  jonathan 	s = splnet();	/* FreeBSD */
    481       1.13  degroote 	so->so_pcb = kp;
    482        1.1  jonathan 	error = raw_usrreqs.pru_attach(so, proto, td);
    483        1.1  jonathan 	kp = (struct keycb *)sotorawcb(so);
    484        1.1  jonathan 	if (error) {
    485        1.1  jonathan 		free(kp, M_PCB);
    486       1.13  degroote 		so->so_pcb = NULL;
    487        1.1  jonathan 		splx(s);
    488        1.1  jonathan 		return error;
    489        1.1  jonathan 	}
    490        1.1  jonathan 
    491        1.1  jonathan 	kp->kp_promisc = kp->kp_registered = 0;
    492        1.1  jonathan 
    493        1.1  jonathan 	if (kp->kp_raw.rcb_proto.sp_protocol == PF_KEY) /* XXX: AF_KEY */
    494        1.1  jonathan 		key_cb.key_count++;
    495        1.1  jonathan 	key_cb.any_count++;
    496        1.1  jonathan 	kp->kp_raw.rcb_laddr = &key_src;
    497        1.1  jonathan 	kp->kp_raw.rcb_faddr = &key_dst;
    498        1.1  jonathan 	soisconnected(so);
    499        1.1  jonathan 	so->so_options |= SO_USELOOPBACK;
    500        1.1  jonathan 
    501        1.1  jonathan 	splx(s);
    502        1.1  jonathan 	return 0;
    503        1.1  jonathan }
    504        1.1  jonathan 
    505        1.1  jonathan /*
    506        1.1  jonathan  * key_bind()
    507        1.1  jonathan  * derived from net/rtsock.c:rts_bind()
    508        1.1  jonathan  */
    509        1.1  jonathan static int
    510        1.1  jonathan key_bind(struct socket *so, struct sockaddr *nam, struct proc *td)
    511        1.1  jonathan {
    512        1.1  jonathan 	int s, error;
    513        1.1  jonathan 	s = splnet();	/* FreeBSD */
    514        1.1  jonathan 	error = raw_usrreqs.pru_bind(so, nam, td); /* xxx just EINVAL */
    515        1.1  jonathan 	splx(s);
    516        1.1  jonathan 	return error;
    517        1.1  jonathan }
    518        1.1  jonathan 
    519        1.1  jonathan /*
    520        1.1  jonathan  * key_connect()
    521        1.1  jonathan  * derived from net/rtsock.c:rts_connect()
    522        1.1  jonathan  */
    523        1.1  jonathan static int
    524        1.1  jonathan key_connect(struct socket *so, struct sockaddr *nam, struct proc *td)
    525        1.1  jonathan {
    526        1.1  jonathan 	int s, error;
    527        1.1  jonathan 	s = splnet();	/* FreeBSD */
    528        1.1  jonathan 	error = raw_usrreqs.pru_connect(so, nam, td); /* XXX just EINVAL */
    529        1.1  jonathan 	splx(s);
    530        1.1  jonathan 	return error;
    531        1.1  jonathan }
    532        1.1  jonathan 
    533        1.1  jonathan /*
    534        1.1  jonathan  * key_detach()
    535        1.1  jonathan  * derived from net/rtsock.c:rts_detach()
    536        1.1  jonathan  */
    537        1.1  jonathan static int
    538        1.1  jonathan key_detach(struct socket *so)
    539        1.1  jonathan {
    540        1.1  jonathan 	struct keycb *kp = (struct keycb *)sotorawcb(so);
    541        1.1  jonathan 	int s, error;
    542        1.1  jonathan 
    543        1.1  jonathan 	s = splnet();	/* FreeBSD */
    544        1.1  jonathan 	if (kp != 0) {
    545        1.1  jonathan 		if (kp->kp_raw.rcb_proto.sp_protocol
    546        1.1  jonathan 		    == PF_KEY) /* XXX: AF_KEY */
    547        1.1  jonathan 			key_cb.key_count--;
    548        1.1  jonathan 		key_cb.any_count--;
    549        1.1  jonathan 
    550        1.1  jonathan 		key_freereg(so);
    551        1.1  jonathan 	}
    552        1.1  jonathan 	error = raw_usrreqs.pru_detach(so);
    553        1.1  jonathan 	splx(s);
    554        1.1  jonathan 	return error;
    555        1.1  jonathan }
    556        1.1  jonathan 
    557        1.1  jonathan /*
    558        1.1  jonathan  * key_disconnect()
    559        1.1  jonathan  * derived from net/rtsock.c:key_disconnect()
    560        1.1  jonathan  */
    561        1.1  jonathan static int
    562        1.1  jonathan key_disconnect(struct socket *so)
    563        1.1  jonathan {
    564        1.1  jonathan 	int s, error;
    565        1.1  jonathan 	s = splnet();	/* FreeBSD */
    566        1.1  jonathan 	error = raw_usrreqs.pru_disconnect(so);
    567        1.1  jonathan 	splx(s);
    568        1.1  jonathan 	return error;
    569        1.1  jonathan }
    570        1.1  jonathan 
    571        1.1  jonathan /*
    572        1.1  jonathan  * key_peeraddr()
    573        1.1  jonathan  * derived from net/rtsock.c:rts_peeraddr()
    574        1.1  jonathan  */
    575        1.1  jonathan static int
    576        1.1  jonathan key_peeraddr(struct socket *so, struct sockaddr **nam)
    577        1.1  jonathan {
    578        1.1  jonathan 	int s, error;
    579        1.1  jonathan 	s = splnet();	/* FreeBSD */
    580        1.1  jonathan 	error = raw_usrreqs.pru_peeraddr(so, nam);
    581        1.1  jonathan 	splx(s);
    582        1.1  jonathan 	return error;
    583        1.1  jonathan }
    584        1.1  jonathan 
    585        1.1  jonathan /*
    586        1.1  jonathan  * key_send()
    587        1.1  jonathan  * derived from net/rtsock.c:rts_send()
    588        1.1  jonathan  */
    589        1.1  jonathan static int
    590        1.1  jonathan key_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
    591        1.1  jonathan 	 struct mbuf *control, struct proc *td)
    592        1.1  jonathan {
    593        1.1  jonathan 	int s, error;
    594        1.1  jonathan 	s = splnet();	/* FreeBSD */
    595        1.1  jonathan 	error = raw_usrreqs.pru_send(so, flags, m, nam, control, td);
    596        1.1  jonathan 	splx(s);
    597        1.1  jonathan 	return error;
    598        1.1  jonathan }
    599        1.1  jonathan 
    600        1.1  jonathan /*
    601        1.1  jonathan  * key_shutdown()
    602        1.1  jonathan  * derived from net/rtsock.c:rts_shutdown()
    603        1.1  jonathan  */
    604        1.1  jonathan static int
    605        1.1  jonathan key_shutdown(struct socket *so)
    606        1.1  jonathan {
    607        1.1  jonathan 	int s, error;
    608        1.1  jonathan 	s = splnet();	/* FreeBSD */
    609        1.1  jonathan 	error = raw_usrreqs.pru_shutdown(so);
    610        1.1  jonathan 	splx(s);
    611        1.1  jonathan 	return error;
    612        1.1  jonathan }
    613        1.1  jonathan 
    614        1.1  jonathan /*
    615        1.1  jonathan  * key_sockaddr()
    616        1.1  jonathan  * derived from net/rtsock.c:rts_sockaddr()
    617        1.1  jonathan  */
    618        1.1  jonathan static int
    619        1.1  jonathan key_sockaddr(struct socket *so, struct sockaddr **nam)
    620        1.1  jonathan {
    621        1.1  jonathan 	int s, error;
    622        1.1  jonathan 	s = splnet();	/* FreeBSD */
    623        1.1  jonathan 	error = raw_usrreqs.pru_sockaddr(so, nam);
    624        1.1  jonathan 	splx(s);
    625        1.1  jonathan 	return error;
    626        1.1  jonathan }
    627        1.1  jonathan #else /*!__FreeBSD__ -- traditional proto_usrreq() switch */
    628        1.1  jonathan 
    629        1.1  jonathan /*
    630        1.1  jonathan  * key_usrreq()
    631        1.1  jonathan  * derived from net/rtsock.c:route_usrreq()
    632        1.1  jonathan  */
    633        1.1  jonathan int
    634       1.14  degroote key_usrreq(struct socket *so, int req,struct mbuf *m, struct mbuf *nam,
    635       1.14  degroote 	   struct mbuf *control, struct lwp *l)
    636        1.1  jonathan {
    637        1.1  jonathan 	int error = 0;
    638        1.1  jonathan 	struct keycb *kp = (struct keycb *)sotorawcb(so);
    639        1.1  jonathan 	int s;
    640        1.1  jonathan 
    641        1.1  jonathan 	s = splsoftnet();
    642        1.1  jonathan 	if (req == PRU_ATTACH) {
    643        1.1  jonathan 		kp = (struct keycb *)malloc(sizeof(*kp), M_PCB, M_WAITOK);
    644       1.16        ad 		sosetlock(so);
    645       1.13  degroote 		so->so_pcb = kp;
    646        1.1  jonathan 		if (so->so_pcb)
    647  1.16.10.1     skrll 			memset(so->so_pcb, 0, sizeof(*kp));
    648        1.1  jonathan 	}
    649        1.1  jonathan 	if (req == PRU_DETACH && kp) {
    650        1.1  jonathan 		int af = kp->kp_raw.rcb_proto.sp_protocol;
    651        1.1  jonathan 		if (af == PF_KEY) /* XXX: AF_KEY */
    652        1.1  jonathan 			key_cb.key_count--;
    653        1.1  jonathan 		key_cb.any_count--;
    654        1.1  jonathan 
    655        1.1  jonathan 		key_freereg(so);
    656        1.1  jonathan 	}
    657        1.1  jonathan 
    658        1.9  christos 	error = raw_usrreq(so, req, m, nam, control, l);
    659        1.1  jonathan 	m = control = NULL;	/* reclaimed in raw_usrreq */
    660        1.1  jonathan 	kp = (struct keycb *)sotorawcb(so);
    661        1.1  jonathan 	if (req == PRU_ATTACH && kp) {
    662        1.1  jonathan 		int af = kp->kp_raw.rcb_proto.sp_protocol;
    663        1.1  jonathan 		if (error) {
    664       1.15   thorpej 			PFKEY_STATINC(PFKEY_STAT_SOCKERR);
    665       1.13  degroote 			free(kp, M_PCB);
    666       1.13  degroote 			so->so_pcb = NULL;
    667        1.1  jonathan 			splx(s);
    668        1.1  jonathan 			return (error);
    669        1.1  jonathan 		}
    670        1.1  jonathan 
    671        1.1  jonathan 		kp->kp_promisc = kp->kp_registered = 0;
    672        1.1  jonathan 
    673        1.1  jonathan 		if (af == PF_KEY) /* XXX: AF_KEY */
    674        1.1  jonathan 			key_cb.key_count++;
    675        1.1  jonathan 		key_cb.any_count++;
    676        1.1  jonathan 		kp->kp_raw.rcb_laddr = &key_src;
    677        1.1  jonathan 		kp->kp_raw.rcb_faddr = &key_dst;
    678        1.1  jonathan 		soisconnected(so);
    679        1.1  jonathan 		so->so_options |= SO_USELOOPBACK;
    680        1.1  jonathan 	}
    681        1.1  jonathan 	splx(s);
    682        1.1  jonathan 	return (error);
    683        1.1  jonathan }
    684        1.1  jonathan #endif /*!__FreeBSD__*/
    685        1.1  jonathan 
    686        1.1  jonathan /* sysctl */
    687        1.1  jonathan #ifdef SYSCTL_NODE
    688        1.1  jonathan SYSCTL_NODE(_net, PF_KEY, key, CTLFLAG_RW, 0, "Key Family");
    689        1.2       tls #endif /* SYSCTL_NODE */
    690        1.1  jonathan 
    691        1.1  jonathan /*
    692        1.1  jonathan  * Definitions of protocols supported in the KEY domain.
    693        1.1  jonathan  */
    694        1.1  jonathan 
    695        1.6      matt #ifdef __FreeBSD__
    696        1.1  jonathan extern struct domain keydomain;
    697        1.1  jonathan 
    698        1.1  jonathan struct pr_usrreqs key_usrreqs = {
    699        1.1  jonathan 	key_abort, pru_accept_notsupp, key_attach, key_bind,
    700        1.1  jonathan 	key_connect,
    701        1.1  jonathan 	pru_connect2_notsupp, pru_control_notsupp, key_detach,
    702        1.1  jonathan 	key_disconnect, pru_listen_notsupp, key_peeraddr,
    703        1.1  jonathan 	pru_rcvd_notsupp,
    704        1.1  jonathan 	pru_rcvoob_notsupp, key_send, pru_sense_null, key_shutdown,
    705        1.1  jonathan 	key_sockaddr, sosend, soreceive, sopoll
    706        1.1  jonathan };
    707        1.1  jonathan 
    708        1.1  jonathan struct protosw keysw[] = {
    709        1.1  jonathan { SOCK_RAW,	&keydomain,	PF_KEY_V2,	PR_ATOMIC|PR_ADDR,
    710        1.1  jonathan   0,		(pr_output_t *)key_output,	raw_ctlinput, 0,
    711        1.1  jonathan   0,
    712        1.1  jonathan   raw_init,	0,		0,		0,
    713        1.1  jonathan   &key_usrreqs
    714        1.1  jonathan }
    715        1.1  jonathan };
    716        1.1  jonathan 
    717        1.1  jonathan static void
    718        1.1  jonathan key_init0(void)
    719        1.1  jonathan {
    720  1.16.10.1     skrll 	memset(&key_cb, 0, sizeof(key_cb));
    721        1.1  jonathan 	key_init();
    722        1.1  jonathan }
    723        1.1  jonathan 
    724        1.1  jonathan struct domain keydomain =
    725        1.1  jonathan     { PF_KEY, "key", key_init0, 0, 0,
    726        1.1  jonathan       keysw, &keysw[sizeof(keysw)/sizeof(keysw[0])] };
    727        1.1  jonathan 
    728        1.1  jonathan DOMAIN_SET(key);
    729        1.1  jonathan 
    730        1.1  jonathan #else /* !__FreeBSD__ */
    731        1.1  jonathan 
    732        1.6      matt DOMAIN_DEFINE(keydomain);
    733        1.1  jonathan 
    734       1.10      matt const struct protosw keysw[] = {
    735       1.10      matt     {
    736       1.10      matt 	.pr_type = SOCK_RAW,
    737       1.10      matt 	.pr_domain = &keydomain,
    738       1.10      matt 	.pr_protocol = PF_KEY_V2,
    739       1.10      matt 	.pr_flags = PR_ATOMIC|PR_ADDR,
    740       1.10      matt 	.pr_output = key_output,
    741       1.10      matt 	.pr_ctlinput = raw_ctlinput,
    742       1.10      matt 	.pr_usrreq = key_usrreq,
    743       1.10      matt 	.pr_init = raw_init,
    744       1.10      matt     }
    745        1.1  jonathan };
    746        1.1  jonathan 
    747       1.10      matt struct domain keydomain = {
    748       1.10      matt     .dom_family = PF_KEY,
    749       1.10      matt     .dom_name = "key",
    750       1.10      matt     .dom_init = key_init,
    751       1.10      matt     .dom_protosw = keysw,
    752       1.10      matt     .dom_protoswNPROTOSW = &keysw[__arraycount(keysw)],
    753       1.10      matt };
    754        1.1  jonathan 
    755        1.1  jonathan #endif
    756