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