keysock.c revision 1.70
1/*	$NetBSD: keysock.c,v 1.70 2019/06/12 22:23:50 christos 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.70 2019/06/12 22:23:50 christos 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
65struct key_cb {
66	int key_count;
67	int any_count;
68};
69static struct key_cb key_cb;
70
71static struct sockaddr key_dst = {
72    .sa_len = 2,
73    .sa_family = PF_KEY,
74};
75static struct sockaddr key_src = {
76    .sa_len = 2,
77    .sa_family = PF_KEY,
78};
79
80static const struct protosw keysw[];
81
82static int key_sendup0(struct rawcb *, struct mbuf *, int, int);
83
84int key_registered_sb_max = (2048 * MHLEN); /* XXX arbitrary */
85
86static kmutex_t *key_so_mtx;
87static struct rawcbhead key_rawcb;
88
89void
90key_init_so(void)
91{
92
93	key_so_mtx = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
94}
95
96static void
97key_pr_init(void)
98{
99
100	LIST_INIT(&key_rawcb);
101}
102
103/*
104 * key_output()
105 */
106static int
107key_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);
155end:
156	if (m)
157		m_freem(m);
158	return error;
159}
160
161/*
162 * send message to the socket.
163 */
164static int
165key_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/* so can be NULL if target != KEY_SENDUP_ONE */
222static int
223_key_sendup_mbuf(struct socket *so, struct mbuf *m,
224		int target/*, sbprio */)
225{
226	struct mbuf *n;
227	struct keycb *kp;
228	int sendup;
229	struct rawcb *rp;
230	int error = 0;
231	int sbprio = 0; /* XXX should be a parameter */
232
233	KASSERT(m != NULL);
234	KASSERT(so != NULL || target != KEY_SENDUP_ONE);
235
236	/*
237	 * RFC 2367 says ACQUIRE and other kernel-generated messages
238	 * are special. We treat all KEY_SENDUP_REGISTERED messages
239	 * as special, delivering them to all registered sockets
240	 * even if the socket is at or above its so->so_rcv.sb_max limits.
241	 * The only constraint is that the  so_rcv data fall below
242	 * key_registered_sb_max.
243	 * Doing that check here avoids reworking every key_sendup_mbuf()
244	 * in the short term. . The rework will be done after a technical
245	 * conensus that this approach is appropriate.
246 	 */
247	if (target == KEY_SENDUP_REGISTERED) {
248		sbprio = SB_PRIO_BESTEFFORT;
249	}
250
251	{
252		uint64_t *ps = PFKEY_STAT_GETREF();
253		ps[PFKEY_STAT_IN_TOTAL]++;
254		ps[PFKEY_STAT_IN_BYTES] += m->m_pkthdr.len;
255		PFKEY_STAT_PUTREF();
256	}
257	if (m->m_len < sizeof(struct sadb_msg)) {
258#if 1
259		m = m_pullup(m, sizeof(struct sadb_msg));
260		if (m == NULL) {
261			PFKEY_STATINC(PFKEY_STAT_IN_NOMEM);
262			return ENOBUFS;
263		}
264#else
265		/* don't bother pulling it up just for stats */
266#endif
267	}
268	if (m->m_len >= sizeof(struct sadb_msg)) {
269		struct sadb_msg *msg;
270		msg = mtod(m, struct sadb_msg *);
271		PFKEY_STATINC(PFKEY_STAT_IN_MSGTYPE + msg->sadb_msg_type);
272	}
273
274	LIST_FOREACH(rp, &key_rawcb, rcb_list)
275	{
276		struct socket * kso = rp->rcb_socket;
277		if (rp->rcb_proto.sp_family != PF_KEY)
278			continue;
279		if (rp->rcb_proto.sp_protocol
280		 && rp->rcb_proto.sp_protocol != PF_KEY_V2) {
281			continue;
282		}
283
284		kp = (struct keycb *)rp;
285
286		/*
287		 * If you are in promiscuous mode, and when you get broadcasted
288		 * reply, you'll get two PF_KEY messages.
289		 * (based on pf_key@inner.net message on 14 Oct 1998)
290		 */
291		if (((struct keycb *)rp)->kp_promisc) {
292			if ((n = m_copym(m, 0, (int)M_COPYALL, M_DONTWAIT)) != NULL) {
293				(void)key_sendup0(rp, n, 1, 0);
294				n = NULL;
295			}
296		}
297
298		/* the exact target will be processed later */
299		if (so && sotorawcb(so) == rp)
300			continue;
301
302		sendup = 0;
303		switch (target) {
304		case KEY_SENDUP_ONE:
305			/* the statement has no effect */
306			if (so && sotorawcb(so) == rp)
307				sendup++;
308			break;
309		case KEY_SENDUP_ALL:
310			sendup++;
311			break;
312		case KEY_SENDUP_REGISTERED:
313			if (kp->kp_registered) {
314				if (kso->so_rcv.sb_cc <= key_registered_sb_max)
315					sendup++;
316			  	else
317			  		printf("keysock: "
318					       "registered sendup dropped, "
319					       "sb_cc %ld max %d\n",
320					       kso->so_rcv.sb_cc,
321					       key_registered_sb_max);
322			}
323			break;
324		}
325		PFKEY_STATINC(PFKEY_STAT_IN_MSGTARGET + target);
326
327		if (!sendup)
328			continue;
329
330		if ((n = m_copym(m, 0, (int)M_COPYALL, M_DONTWAIT)) == NULL) {
331			m_freem(m);
332			PFKEY_STATINC(PFKEY_STAT_IN_NOMEM);
333			return ENOBUFS;
334		}
335
336		if ((error = key_sendup0(rp, n, 0, 0)) != 0) {
337			m_freem(m);
338			return error;
339		}
340
341		n = NULL;
342	}
343
344	/* The 'later' time for processing the exact target has arrived */
345	if (so) {
346		error = key_sendup0(sotorawcb(so), m, 0, sbprio);
347		m = NULL;
348	} else {
349		error = 0;
350		m_freem(m);
351	}
352	return error;
353}
354
355int
356key_sendup_mbuf(struct socket *so, struct mbuf *m,
357		int target/*, sbprio */)
358{
359	int error;
360
361	if (so == NULL)
362		mutex_enter(key_so_mtx);
363	else
364		KASSERT(solocked(so));
365
366	error = _key_sendup_mbuf(so, m, target);
367
368	if (so == NULL)
369		mutex_exit(key_so_mtx);
370	return error;
371}
372
373static int
374key_attach(struct socket *so, int proto)
375{
376	struct keycb *kp;
377	int s, error;
378
379	KASSERT(sotorawcb(so) == NULL);
380	kp = kmem_zalloc(sizeof(*kp), KM_SLEEP);
381	kp->kp_raw.rcb_len = sizeof(*kp);
382	so->so_pcb = kp;
383
384	s = splsoftnet();
385
386	if (so->so_lock != key_so_mtx) {
387		KASSERT(so->so_lock == NULL);
388		mutex_obj_hold(key_so_mtx);
389		so->so_lock = key_so_mtx;
390		solock(so);
391	}
392
393	error = raw_attach(so, proto, &key_rawcb);
394	if (error) {
395		PFKEY_STATINC(PFKEY_STAT_SOCKERR);
396		kmem_free(kp, sizeof(*kp));
397		so->so_pcb = NULL;
398		goto out;
399	}
400
401	kp->kp_promisc = kp->kp_registered = 0;
402
403	if (kp->kp_raw.rcb_proto.sp_protocol == PF_KEY) /* XXX: AF_KEY */
404		key_cb.key_count++;
405	key_cb.any_count++;
406	kp->kp_raw.rcb_laddr = &key_src;
407	kp->kp_raw.rcb_faddr = &key_dst;
408	soisconnected(so);
409	so->so_options |= SO_USELOOPBACK;
410out:
411	KASSERT(solocked(so));
412	splx(s);
413	return error;
414}
415
416static void
417key_detach(struct socket *so)
418{
419	struct keycb *kp = (struct keycb *)sotorawcb(so);
420	int s;
421
422	KASSERT(!cpu_softintr_p());
423	KASSERT(solocked(so));
424	KASSERT(kp != NULL);
425
426	s = splsoftnet();
427	if (kp->kp_raw.rcb_proto.sp_protocol == PF_KEY) /* XXX: AF_KEY */
428		key_cb.key_count--;
429	key_cb.any_count--;
430	key_freereg(so);
431	raw_detach(so);
432	splx(s);
433}
434
435static int
436key_accept(struct socket *so, struct sockaddr *nam)
437{
438	KASSERT(solocked(so));
439
440	panic("%s: unsupported", __func__);
441
442	return EOPNOTSUPP;
443}
444
445static int
446key_bind(struct socket *so, struct sockaddr *nam, struct lwp *l)
447{
448	KASSERT(solocked(so));
449
450	return EOPNOTSUPP;
451}
452
453static int
454key_listen(struct socket *so, struct lwp *l)
455{
456	KASSERT(solocked(so));
457
458	return EOPNOTSUPP;
459}
460
461static int
462key_connect(struct socket *so, struct sockaddr *nam, struct lwp *l)
463{
464	KASSERT(solocked(so));
465
466	return EOPNOTSUPP;
467}
468
469static int
470key_connect2(struct socket *so, struct socket *so2)
471{
472	KASSERT(solocked(so));
473
474	return EOPNOTSUPP;
475}
476
477static int
478key_disconnect(struct socket *so)
479{
480	struct rawcb *rp = sotorawcb(so);
481	int s;
482
483	KASSERT(solocked(so));
484	KASSERT(rp != NULL);
485
486	s = splsoftnet();
487	soisdisconnected(so);
488	raw_disconnect(rp);
489	splx(s);
490
491	return 0;
492}
493
494static int
495key_shutdown(struct socket *so)
496{
497	int s;
498
499	KASSERT(solocked(so));
500
501	/*
502	 * Mark the connection as being incapable of further input.
503	 */
504	s = splsoftnet();
505	socantsendmore(so);
506	splx(s);
507
508	return 0;
509}
510
511static int
512key_abort(struct socket *so)
513{
514	KASSERT(solocked(so));
515
516	panic("%s: unsupported", __func__);
517
518	return EOPNOTSUPP;
519}
520
521static int
522key_ioctl(struct socket *so, u_long cmd, void *nam, struct ifnet *ifp)
523{
524	return EOPNOTSUPP;
525}
526
527static int
528key_stat(struct socket *so, struct stat *ub)
529{
530	KASSERT(solocked(so));
531
532	return 0;
533}
534
535static int
536key_peeraddr(struct socket *so, struct sockaddr *nam)
537{
538	struct rawcb *rp = sotorawcb(so);
539
540	KASSERT(solocked(so));
541	KASSERT(rp != NULL);
542	KASSERT(nam != NULL);
543
544	if (rp->rcb_faddr == NULL)
545		return ENOTCONN;
546
547	raw_setpeeraddr(rp, nam);
548	return 0;
549}
550
551static int
552key_sockaddr(struct socket *so, struct sockaddr *nam)
553{
554	struct rawcb *rp = sotorawcb(so);
555
556	KASSERT(solocked(so));
557	KASSERT(rp != NULL);
558	KASSERT(nam != NULL);
559
560	if (rp->rcb_faddr == NULL)
561		return ENOTCONN;
562
563	raw_setsockaddr(rp, nam);
564	return 0;
565}
566
567static int
568key_rcvd(struct socket *so, int flags, struct lwp *l)
569{
570	KASSERT(solocked(so));
571
572	return EOPNOTSUPP;
573}
574
575static int
576key_recvoob(struct socket *so, struct mbuf *m, int flags)
577{
578	KASSERT(solocked(so));
579
580	return EOPNOTSUPP;
581}
582
583static int
584key_send(struct socket *so, struct mbuf *m, struct sockaddr *nam,
585    struct mbuf *control, struct lwp *l)
586{
587	int error = 0;
588	int s;
589
590	KASSERT(solocked(so));
591	KASSERT(so->so_proto == &keysw[0]);
592
593	s = splsoftnet();
594	error = raw_send(so, m, nam, control, l, &key_output);
595	splx(s);
596
597	return error;
598}
599
600static int
601key_sendoob(struct socket *so, struct mbuf *m, struct mbuf *control)
602{
603	KASSERT(solocked(so));
604
605	m_freem(m);
606	m_freem(control);
607
608	return EOPNOTSUPP;
609}
610
611static int
612key_purgeif(struct socket *so, struct ifnet *ifa)
613{
614
615	panic("%s: unsupported", __func__);
616
617	return EOPNOTSUPP;
618}
619
620/*
621 * Definitions of protocols supported in the KEY domain.
622 */
623
624DOMAIN_DEFINE(keydomain);
625
626PR_WRAP_USRREQS(key)
627#define	key_attach	key_attach_wrapper
628#define	key_detach	key_detach_wrapper
629#define	key_accept	key_accept_wrapper
630#define	key_bind	key_bind_wrapper
631#define	key_listen	key_listen_wrapper
632#define	key_connect	key_connect_wrapper
633#define	key_connect2	key_connect2_wrapper
634#define	key_disconnect	key_disconnect_wrapper
635#define	key_shutdown	key_shutdown_wrapper
636#define	key_abort	key_abort_wrapper
637#define	key_ioctl	key_ioctl_wrapper
638#define	key_stat	key_stat_wrapper
639#define	key_peeraddr	key_peeraddr_wrapper
640#define	key_sockaddr	key_sockaddr_wrapper
641#define	key_rcvd	key_rcvd_wrapper
642#define	key_recvoob	key_recvoob_wrapper
643#define	key_send	key_send_wrapper
644#define	key_sendoob	key_sendoob_wrapper
645#define	key_purgeif	key_purgeif_wrapper
646
647static const struct pr_usrreqs key_usrreqs = {
648	.pr_attach	= key_attach,
649	.pr_detach	= key_detach,
650	.pr_accept	= key_accept,
651	.pr_bind	= key_bind,
652	.pr_listen	= key_listen,
653	.pr_connect	= key_connect,
654	.pr_connect2	= key_connect2,
655	.pr_disconnect	= key_disconnect,
656	.pr_shutdown	= key_shutdown,
657	.pr_abort	= key_abort,
658	.pr_ioctl	= key_ioctl,
659	.pr_stat	= key_stat,
660	.pr_peeraddr	= key_peeraddr,
661	.pr_sockaddr	= key_sockaddr,
662	.pr_rcvd	= key_rcvd,
663	.pr_recvoob	= key_recvoob,
664	.pr_send	= key_send,
665	.pr_sendoob	= key_sendoob,
666	.pr_purgeif	= key_purgeif,
667};
668
669static const struct protosw keysw[] = {
670    {
671	.pr_type = SOCK_RAW,
672	.pr_domain = &keydomain,
673	.pr_protocol = PF_KEY_V2,
674	.pr_flags = PR_ATOMIC|PR_ADDR,
675	.pr_ctlinput = raw_ctlinput,
676	.pr_usrreqs = &key_usrreqs,
677	.pr_init = key_pr_init,
678    }
679};
680
681struct domain keydomain = {
682    .dom_family = PF_KEY,
683    .dom_name = "key",
684    .dom_init = key_init,
685    .dom_protosw = keysw,
686    .dom_protoswNPROTOSW = &keysw[__arraycount(keysw)],
687};
688