Home | History | Annotate | Line # | Download | only in netipsec
keysock.c revision 1.66
      1 /*	$NetBSD: keysock.c,v 1.66 2018/11/08 04:30:38 roy Exp $	*/
      2 /*	$FreeBSD: 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.66 2018/11/08 04:30:38 roy 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 #include <sys/cpu.h>
     53 #include <sys/syslog.h>
     54 
     55 #include <net/raw_cb.h>
     56 #include <net/route.h>
     57 
     58 #include <net/pfkeyv2.h>
     59 #include <netipsec/key.h>
     60 #include <netipsec/keysock.h>
     61 #include <netipsec/key_debug.h>
     62 
     63 #include <netipsec/ipsec_private.h>
     64 
     65 struct key_cb {
     66 	int key_count;
     67 	int any_count;
     68 };
     69 static struct key_cb key_cb;
     70 
     71 static struct sockaddr key_dst = {
     72     .sa_len = 2,
     73     .sa_family = PF_KEY,
     74 };
     75 static struct sockaddr key_src = {
     76     .sa_len = 2,
     77     .sa_family = PF_KEY,
     78 };
     79 
     80 static const struct protosw keysw[];
     81 
     82 static int key_sendup0(struct rawcb *, struct mbuf *, int, int);
     83 
     84 int key_registered_sb_max = (2048 * MHLEN); /* XXX arbitrary */
     85 
     86 static kmutex_t *key_so_mtx;
     87 static struct rawcbhead key_rawcb;
     88 
     89 void
     90 key_init_so(void)
     91 {
     92 
     93 	key_so_mtx = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
     94 }
     95 
     96 static void
     97 key_pr_init(void)
     98 {
     99 
    100 	LIST_INIT(&key_rawcb);
    101 }
    102 
    103 /*
    104  * key_output()
    105  */
    106 static int
    107 key_output(struct mbuf *m, struct socket *so)
    108 {
    109 	struct sadb_msg *msg;
    110 	int len, error = 0;
    111 	int s;
    112 
    113 	KASSERT(m != NULL);
    114 
    115 	{
    116 		uint64_t *ps = PFKEY_STAT_GETREF();
    117 		ps[PFKEY_STAT_OUT_TOTAL]++;
    118 		ps[PFKEY_STAT_OUT_BYTES] += m->m_pkthdr.len;
    119 		PFKEY_STAT_PUTREF();
    120 	}
    121 
    122 	len = m->m_pkthdr.len;
    123 	if (len < sizeof(struct sadb_msg)) {
    124 		PFKEY_STATINC(PFKEY_STAT_OUT_TOOSHORT);
    125 		error = EINVAL;
    126 		goto end;
    127 	}
    128 
    129 	if (m->m_len < sizeof(struct sadb_msg)) {
    130 		if ((m = m_pullup(m, sizeof(struct sadb_msg))) == 0) {
    131 			PFKEY_STATINC(PFKEY_STAT_OUT_NOMEM);
    132 			error = ENOBUFS;
    133 			goto end;
    134 		}
    135 	}
    136 
    137 	KASSERT((m->m_flags & M_PKTHDR) != 0);
    138 
    139 	if (KEYDEBUG_ON(KEYDEBUG_KEY_DUMP))
    140 		kdebug_mbuf(__func__, m);
    141 
    142 	msg = mtod(m, struct sadb_msg *);
    143 	PFKEY_STATINC(PFKEY_STAT_OUT_MSGTYPE + msg->sadb_msg_type);
    144 	if (len != PFKEY_UNUNIT64(msg->sadb_msg_len)) {
    145 		PFKEY_STATINC(PFKEY_STAT_OUT_INVLEN);
    146 		error = EINVAL;
    147 		goto end;
    148 	}
    149 
    150 	/*XXX giant lock*/
    151 	s = splsoftnet();
    152 	error = key_parse(m, so);
    153 	m = NULL;
    154 	splx(s);
    155 end:
    156 	if (m)
    157 		m_freem(m);
    158 	return error;
    159 }
    160 
    161 /*
    162  * send message to the socket.
    163  */
    164 static int
    165 key_sendup0(
    166     struct rawcb *rp,
    167     struct mbuf *m,
    168     int promisc,
    169     int sbprio
    170 )
    171 {
    172 	int error;
    173 	int ok;
    174 
    175 	if (promisc) {
    176 		struct sadb_msg *pmsg;
    177 
    178 		M_PREPEND(m, sizeof(struct sadb_msg), M_DONTWAIT);
    179 		if (m && m->m_len < sizeof(struct sadb_msg))
    180 			m = m_pullup(m, sizeof(struct sadb_msg));
    181 		if (!m) {
    182 			PFKEY_STATINC(PFKEY_STAT_IN_NOMEM);
    183 			return ENOBUFS;
    184 		}
    185 		m->m_pkthdr.len += sizeof(*pmsg);
    186 
    187 		pmsg = mtod(m, struct sadb_msg *);
    188 		memset(pmsg, 0, sizeof(*pmsg));
    189 		pmsg->sadb_msg_version = PF_KEY_V2;
    190 		pmsg->sadb_msg_type = SADB_X_PROMISC;
    191 		pmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
    192 		/* pid and seq? */
    193 
    194 		PFKEY_STATINC(PFKEY_STAT_IN_MSGTYPE + pmsg->sadb_msg_type);
    195 	}
    196 
    197 	if (sbprio == 0)
    198 		ok = sbappendaddr(&rp->rcb_socket->so_rcv,
    199 			       (struct sockaddr *)&key_src, m, NULL);
    200 	else
    201 		ok = sbappendaddrchain(&rp->rcb_socket->so_rcv,
    202 			       (struct sockaddr *)&key_src, m, sbprio);
    203 
    204 	if (!ok) {
    205 		log(LOG_WARNING,
    206 		    "%s: couldn't send PF_KEY message to the socket\n",
    207 		    __func__);
    208 		PFKEY_STATINC(PFKEY_STAT_IN_NOMEM);
    209 		m_freem(m);
    210 		/* Don't call soroverflow because we're returning this
    211 		 * error directly to the sender. */
    212 		rp->rcb_socket->so_rcv.sb_overflowed++;
    213 		error = ENOBUFS;
    214 	} else {
    215 		sorwakeup(rp->rcb_socket);
    216 		error = 0;
    217 	}
    218 	return error;
    219 }
    220 
    221 /* XXX this interface should be obsoleted. */
    222 int
    223 key_sendup(struct socket *so, struct sadb_msg *msg, u_int len,
    224 	   int target)	/*target of the resulting message*/
    225 {
    226 	struct mbuf *m, *n, *mprev;
    227 	int tlen;
    228 
    229 	KASSERT(so != NULL);
    230 	KASSERT(msg != NULL);
    231 
    232 	if (KEYDEBUG_ON(KEYDEBUG_KEY_DUMP)) {
    233 		printf("key_sendup: \n");
    234 		kdebug_sadb(msg);
    235 	}
    236 
    237 	/*
    238 	 * we increment statistics here, just in case we have ENOBUFS
    239 	 * in this function.
    240 	 */
    241 	{
    242 		uint64_t *ps = PFKEY_STAT_GETREF();
    243 		ps[PFKEY_STAT_IN_TOTAL]++;
    244 		ps[PFKEY_STAT_IN_BYTES] += len;
    245 		ps[PFKEY_STAT_IN_MSGTYPE + msg->sadb_msg_type]++;
    246 		PFKEY_STAT_PUTREF();
    247 	}
    248 
    249 	/*
    250 	 * Get mbuf chain whenever possible (not clusters),
    251 	 * to save socket buffer.  We'll be generating many SADB_ACQUIRE
    252 	 * messages to listening key sockets.  If we simply allocate clusters,
    253 	 * sbappendaddr() will raise ENOBUFS due to too little sbspace().
    254 	 * sbspace() computes # of actual data bytes AND mbuf region.
    255 	 *
    256 	 * TODO: SADB_ACQUIRE filters should be implemented.
    257 	 */
    258 	tlen = len;
    259 	m = mprev = NULL;
    260 	while (tlen > 0) {
    261 		int mlen;
    262 		if (tlen == len) {
    263 			MGETHDR(n, M_DONTWAIT, MT_DATA);
    264 			mlen = MHLEN;
    265 		} else {
    266 			MGET(n, M_DONTWAIT, MT_DATA);
    267 			mlen = MLEN;
    268 		}
    269 		if (!n) {
    270 			PFKEY_STATINC(PFKEY_STAT_IN_NOMEM);
    271 			return ENOBUFS;
    272 		}
    273 		n->m_len = mlen;
    274 		if (tlen >= MCLBYTES) {	/*XXX better threshold? */
    275 			MCLGET(n, M_DONTWAIT);
    276 			if ((n->m_flags & M_EXT) == 0) {
    277 				m_free(n);
    278 				m_freem(m);
    279 				PFKEY_STATINC(PFKEY_STAT_IN_NOMEM);
    280 				return ENOBUFS;
    281 			}
    282 			n->m_len = MCLBYTES;
    283 		}
    284 
    285 		if (tlen < n->m_len)
    286 			n->m_len = tlen;
    287 		n->m_next = NULL;
    288 		if (m == NULL)
    289 			m = mprev = n;
    290 		else {
    291 			mprev->m_next = n;
    292 			mprev = n;
    293 		}
    294 		tlen -= n->m_len;
    295 		n = NULL;
    296 	}
    297 	m->m_pkthdr.len = len;
    298 	m_reset_rcvif(m);
    299 	m_copyback(m, 0, len, msg);
    300 
    301 	/* avoid duplicated statistics */
    302 	{
    303 		uint64_t *ps = PFKEY_STAT_GETREF();
    304 		ps[PFKEY_STAT_IN_TOTAL]--;
    305 		ps[PFKEY_STAT_IN_BYTES] -= len;
    306 		ps[PFKEY_STAT_IN_MSGTYPE + msg->sadb_msg_type]--;
    307 		PFKEY_STAT_PUTREF();
    308 	}
    309 
    310 	return key_sendup_mbuf(so, m, target);
    311 }
    312 
    313 /* so can be NULL if target != KEY_SENDUP_ONE */
    314 static int
    315 _key_sendup_mbuf(struct socket *so, struct mbuf *m,
    316 		int target/*, sbprio */)
    317 {
    318 	struct mbuf *n;
    319 	struct keycb *kp;
    320 	int sendup;
    321 	struct rawcb *rp;
    322 	int error = 0;
    323 	int sbprio = 0; /* XXX should be a parameter */
    324 
    325 	KASSERT(m != NULL);
    326 	KASSERT(so != NULL || target != KEY_SENDUP_ONE);
    327 
    328 	/*
    329 	 * RFC 2367 says ACQUIRE and other kernel-generated messages
    330 	 * are special. We treat all KEY_SENDUP_REGISTERED messages
    331 	 * as special, delivering them to all registered sockets
    332 	 * even if the socket is at or above its so->so_rcv.sb_max limits.
    333 	 * The only constraint is that the  so_rcv data fall below
    334 	 * key_registered_sb_max.
    335 	 * Doing that check here avoids reworking every key_sendup_mbuf()
    336 	 * in the short term. . The rework will be done after a technical
    337 	 * conensus that this approach is appropriate.
    338  	 */
    339 	if (target == KEY_SENDUP_REGISTERED) {
    340 		sbprio = SB_PRIO_BESTEFFORT;
    341 	}
    342 
    343 	{
    344 		uint64_t *ps = PFKEY_STAT_GETREF();
    345 		ps[PFKEY_STAT_IN_TOTAL]++;
    346 		ps[PFKEY_STAT_IN_BYTES] += m->m_pkthdr.len;
    347 		PFKEY_STAT_PUTREF();
    348 	}
    349 	if (m->m_len < sizeof(struct sadb_msg)) {
    350 #if 1
    351 		m = m_pullup(m, sizeof(struct sadb_msg));
    352 		if (m == NULL) {
    353 			PFKEY_STATINC(PFKEY_STAT_IN_NOMEM);
    354 			return ENOBUFS;
    355 		}
    356 #else
    357 		/* don't bother pulling it up just for stats */
    358 #endif
    359 	}
    360 	if (m->m_len >= sizeof(struct sadb_msg)) {
    361 		struct sadb_msg *msg;
    362 		msg = mtod(m, struct sadb_msg *);
    363 		PFKEY_STATINC(PFKEY_STAT_IN_MSGTYPE + msg->sadb_msg_type);
    364 	}
    365 
    366 	LIST_FOREACH(rp, &key_rawcb, rcb_list)
    367 	{
    368 		struct socket * kso = rp->rcb_socket;
    369 		if (rp->rcb_proto.sp_family != PF_KEY)
    370 			continue;
    371 		if (rp->rcb_proto.sp_protocol
    372 		 && rp->rcb_proto.sp_protocol != PF_KEY_V2) {
    373 			continue;
    374 		}
    375 
    376 		kp = (struct keycb *)rp;
    377 
    378 		/*
    379 		 * If you are in promiscuous mode, and when you get broadcasted
    380 		 * reply, you'll get two PF_KEY messages.
    381 		 * (based on pf_key (at) inner.net message on 14 Oct 1998)
    382 		 */
    383 		if (((struct keycb *)rp)->kp_promisc) {
    384 			if ((n = m_copym(m, 0, (int)M_COPYALL, M_DONTWAIT)) != NULL) {
    385 				(void)key_sendup0(rp, n, 1, 0);
    386 				n = NULL;
    387 			}
    388 		}
    389 
    390 		/* the exact target will be processed later */
    391 		if (so && sotorawcb(so) == rp)
    392 			continue;
    393 
    394 		sendup = 0;
    395 		switch (target) {
    396 		case KEY_SENDUP_ONE:
    397 			/* the statement has no effect */
    398 			if (so && sotorawcb(so) == rp)
    399 				sendup++;
    400 			break;
    401 		case KEY_SENDUP_ALL:
    402 			sendup++;
    403 			break;
    404 		case KEY_SENDUP_REGISTERED:
    405 			if (kp->kp_registered) {
    406 				if (kso->so_rcv.sb_cc <= key_registered_sb_max)
    407 					sendup++;
    408 			  	else
    409 			  		printf("keysock: "
    410 					       "registered sendup dropped, "
    411 					       "sb_cc %ld max %d\n",
    412 					       kso->so_rcv.sb_cc,
    413 					       key_registered_sb_max);
    414 			}
    415 			break;
    416 		}
    417 		PFKEY_STATINC(PFKEY_STAT_IN_MSGTARGET + target);
    418 
    419 		if (!sendup)
    420 			continue;
    421 
    422 		if ((n = m_copym(m, 0, (int)M_COPYALL, M_DONTWAIT)) == NULL) {
    423 			m_freem(m);
    424 			PFKEY_STATINC(PFKEY_STAT_IN_NOMEM);
    425 			return ENOBUFS;
    426 		}
    427 
    428 		if ((error = key_sendup0(rp, n, 0, 0)) != 0) {
    429 			m_freem(m);
    430 			return error;
    431 		}
    432 
    433 		n = NULL;
    434 	}
    435 
    436 	/* The 'later' time for processing the exact target has arrived */
    437 	if (so) {
    438 		error = key_sendup0(sotorawcb(so), m, 0, sbprio);
    439 		m = NULL;
    440 	} else {
    441 		error = 0;
    442 		m_freem(m);
    443 	}
    444 	return error;
    445 }
    446 
    447 int
    448 key_sendup_mbuf(struct socket *so, struct mbuf *m,
    449 		int target/*, sbprio */)
    450 {
    451 	int error;
    452 
    453 	if (so == NULL)
    454 		mutex_enter(key_so_mtx);
    455 	else
    456 		KASSERT(solocked(so));
    457 
    458 	error = _key_sendup_mbuf(so, m, target);
    459 
    460 	if (so == NULL)
    461 		mutex_exit(key_so_mtx);
    462 	return error;
    463 }
    464 
    465 static int
    466 key_attach(struct socket *so, int proto)
    467 {
    468 	struct keycb *kp;
    469 	int s, error;
    470 
    471 	KASSERT(sotorawcb(so) == NULL);
    472 	kp = kmem_zalloc(sizeof(*kp), KM_SLEEP);
    473 	kp->kp_raw.rcb_len = sizeof(*kp);
    474 	so->so_pcb = kp;
    475 
    476 	s = splsoftnet();
    477 
    478 	KASSERT(so->so_lock == NULL);
    479 	mutex_obj_hold(key_so_mtx);
    480 	so->so_lock = key_so_mtx;
    481 	solock(so);
    482 
    483 	error = raw_attach(so, proto, &key_rawcb);
    484 	if (error) {
    485 		PFKEY_STATINC(PFKEY_STAT_SOCKERR);
    486 		kmem_free(kp, sizeof(*kp));
    487 		so->so_pcb = NULL;
    488 		goto out;
    489 	}
    490 
    491 	kp->kp_promisc = kp->kp_registered = 0;
    492 
    493 	if (kp->kp_raw.rcb_proto.sp_protocol == PF_KEY) /* XXX: AF_KEY */
    494 		key_cb.key_count++;
    495 	key_cb.any_count++;
    496 	kp->kp_raw.rcb_laddr = &key_src;
    497 	kp->kp_raw.rcb_faddr = &key_dst;
    498 	soisconnected(so);
    499 	so->so_options |= SO_USELOOPBACK;
    500 out:
    501 	KASSERT(solocked(so));
    502 	splx(s);
    503 	return error;
    504 }
    505 
    506 static void
    507 key_detach(struct socket *so)
    508 {
    509 	struct keycb *kp = (struct keycb *)sotorawcb(so);
    510 	int s;
    511 
    512 	KASSERT(!cpu_softintr_p());
    513 	KASSERT(solocked(so));
    514 	KASSERT(kp != NULL);
    515 
    516 	s = splsoftnet();
    517 	if (kp->kp_raw.rcb_proto.sp_protocol == PF_KEY) /* XXX: AF_KEY */
    518 		key_cb.key_count--;
    519 	key_cb.any_count--;
    520 	key_freereg(so);
    521 	raw_detach(so);
    522 	splx(s);
    523 }
    524 
    525 static int
    526 key_accept(struct socket *so, struct sockaddr *nam)
    527 {
    528 	KASSERT(solocked(so));
    529 
    530 	panic("key_accept");
    531 
    532 	return EOPNOTSUPP;
    533 }
    534 
    535 static int
    536 key_bind(struct socket *so, struct sockaddr *nam, struct lwp *l)
    537 {
    538 	KASSERT(solocked(so));
    539 
    540 	return EOPNOTSUPP;
    541 }
    542 
    543 static int
    544 key_listen(struct socket *so, struct lwp *l)
    545 {
    546 	KASSERT(solocked(so));
    547 
    548 	return EOPNOTSUPP;
    549 }
    550 
    551 static int
    552 key_connect(struct socket *so, struct sockaddr *nam, struct lwp *l)
    553 {
    554 	KASSERT(solocked(so));
    555 
    556 	return EOPNOTSUPP;
    557 }
    558 
    559 static int
    560 key_connect2(struct socket *so, struct socket *so2)
    561 {
    562 	KASSERT(solocked(so));
    563 
    564 	return EOPNOTSUPP;
    565 }
    566 
    567 static int
    568 key_disconnect(struct socket *so)
    569 {
    570 	struct rawcb *rp = sotorawcb(so);
    571 	int s;
    572 
    573 	KASSERT(solocked(so));
    574 	KASSERT(rp != NULL);
    575 
    576 	s = splsoftnet();
    577 	soisdisconnected(so);
    578 	raw_disconnect(rp);
    579 	splx(s);
    580 
    581 	return 0;
    582 }
    583 
    584 static int
    585 key_shutdown(struct socket *so)
    586 {
    587 	int s;
    588 
    589 	KASSERT(solocked(so));
    590 
    591 	/*
    592 	 * Mark the connection as being incapable of further input.
    593 	 */
    594 	s = splsoftnet();
    595 	socantsendmore(so);
    596 	splx(s);
    597 
    598 	return 0;
    599 }
    600 
    601 static int
    602 key_abort(struct socket *so)
    603 {
    604 	KASSERT(solocked(so));
    605 
    606 	panic("key_abort");
    607 
    608 	return EOPNOTSUPP;
    609 }
    610 
    611 static int
    612 key_ioctl(struct socket *so, u_long cmd, void *nam, struct ifnet *ifp)
    613 {
    614 	return EOPNOTSUPP;
    615 }
    616 
    617 static int
    618 key_stat(struct socket *so, struct stat *ub)
    619 {
    620 	KASSERT(solocked(so));
    621 
    622 	return 0;
    623 }
    624 
    625 static int
    626 key_peeraddr(struct socket *so, struct sockaddr *nam)
    627 {
    628 	struct rawcb *rp = sotorawcb(so);
    629 
    630 	KASSERT(solocked(so));
    631 	KASSERT(rp != NULL);
    632 	KASSERT(nam != NULL);
    633 
    634 	if (rp->rcb_faddr == NULL)
    635 		return ENOTCONN;
    636 
    637 	raw_setpeeraddr(rp, nam);
    638 	return 0;
    639 }
    640 
    641 static int
    642 key_sockaddr(struct socket *so, struct sockaddr *nam)
    643 {
    644 	struct rawcb *rp = sotorawcb(so);
    645 
    646 	KASSERT(solocked(so));
    647 	KASSERT(rp != NULL);
    648 	KASSERT(nam != NULL);
    649 
    650 	if (rp->rcb_faddr == NULL)
    651 		return ENOTCONN;
    652 
    653 	raw_setsockaddr(rp, nam);
    654 	return 0;
    655 }
    656 
    657 static int
    658 key_rcvd(struct socket *so, int flags, struct lwp *l)
    659 {
    660 	KASSERT(solocked(so));
    661 
    662 	return EOPNOTSUPP;
    663 }
    664 
    665 static int
    666 key_recvoob(struct socket *so, struct mbuf *m, int flags)
    667 {
    668 	KASSERT(solocked(so));
    669 
    670 	return EOPNOTSUPP;
    671 }
    672 
    673 static int
    674 key_send(struct socket *so, struct mbuf *m, struct sockaddr *nam,
    675     struct mbuf *control, struct lwp *l)
    676 {
    677 	int error = 0;
    678 	int s;
    679 
    680 	KASSERT(solocked(so));
    681 	KASSERT(so->so_proto == &keysw[0]);
    682 
    683 	s = splsoftnet();
    684 	error = raw_send(so, m, nam, control, l, &key_output);
    685 	splx(s);
    686 
    687 	return error;
    688 }
    689 
    690 static int
    691 key_sendoob(struct socket *so, struct mbuf *m, struct mbuf *control)
    692 {
    693 	KASSERT(solocked(so));
    694 
    695 	m_freem(m);
    696 	m_freem(control);
    697 
    698 	return EOPNOTSUPP;
    699 }
    700 
    701 static int
    702 key_purgeif(struct socket *so, struct ifnet *ifa)
    703 {
    704 
    705 	panic("key_purgeif");
    706 
    707 	return EOPNOTSUPP;
    708 }
    709 
    710 /*
    711  * Definitions of protocols supported in the KEY domain.
    712  */
    713 
    714 DOMAIN_DEFINE(keydomain);
    715 
    716 PR_WRAP_USRREQS(key)
    717 #define	key_attach	key_attach_wrapper
    718 #define	key_detach	key_detach_wrapper
    719 #define	key_accept	key_accept_wrapper
    720 #define	key_bind	key_bind_wrapper
    721 #define	key_listen	key_listen_wrapper
    722 #define	key_connect	key_connect_wrapper
    723 #define	key_connect2	key_connect2_wrapper
    724 #define	key_disconnect	key_disconnect_wrapper
    725 #define	key_shutdown	key_shutdown_wrapper
    726 #define	key_abort	key_abort_wrapper
    727 #define	key_ioctl	key_ioctl_wrapper
    728 #define	key_stat	key_stat_wrapper
    729 #define	key_peeraddr	key_peeraddr_wrapper
    730 #define	key_sockaddr	key_sockaddr_wrapper
    731 #define	key_rcvd	key_rcvd_wrapper
    732 #define	key_recvoob	key_recvoob_wrapper
    733 #define	key_send	key_send_wrapper
    734 #define	key_sendoob	key_sendoob_wrapper
    735 #define	key_purgeif	key_purgeif_wrapper
    736 
    737 static const struct pr_usrreqs key_usrreqs = {
    738 	.pr_attach	= key_attach,
    739 	.pr_detach	= key_detach,
    740 	.pr_accept	= key_accept,
    741 	.pr_bind	= key_bind,
    742 	.pr_listen	= key_listen,
    743 	.pr_connect	= key_connect,
    744 	.pr_connect2	= key_connect2,
    745 	.pr_disconnect	= key_disconnect,
    746 	.pr_shutdown	= key_shutdown,
    747 	.pr_abort	= key_abort,
    748 	.pr_ioctl	= key_ioctl,
    749 	.pr_stat	= key_stat,
    750 	.pr_peeraddr	= key_peeraddr,
    751 	.pr_sockaddr	= key_sockaddr,
    752 	.pr_rcvd	= key_rcvd,
    753 	.pr_recvoob	= key_recvoob,
    754 	.pr_send	= key_send,
    755 	.pr_sendoob	= key_sendoob,
    756 	.pr_purgeif	= key_purgeif,
    757 };
    758 
    759 static const struct protosw keysw[] = {
    760     {
    761 	.pr_type = SOCK_RAW,
    762 	.pr_domain = &keydomain,
    763 	.pr_protocol = PF_KEY_V2,
    764 	.pr_flags = PR_ATOMIC|PR_ADDR,
    765 	.pr_ctlinput = raw_ctlinput,
    766 	.pr_usrreqs = &key_usrreqs,
    767 	.pr_init = key_pr_init,
    768     }
    769 };
    770 
    771 struct domain keydomain = {
    772     .dom_family = PF_KEY,
    773     .dom_name = "key",
    774     .dom_init = key_init,
    775     .dom_protosw = keysw,
    776     .dom_protoswNPROTOSW = &keysw[__arraycount(keysw)],
    777 };
    778