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