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