xref: /freebsd/sys/netipsec/keysock.c (revision fcc7aabdcabbce82b7281607fa3a51c52de6c1b4)
188768458SSam Leffler /*	$FreeBSD$	*/
288768458SSam Leffler /*	$KAME: keysock.c,v 1.25 2001/08/13 20:07:41 itojun Exp $	*/
388768458SSam Leffler 
4c398230bSWarner Losh /*-
551369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
651369649SPedro F. Giffuni  *
788768458SSam Leffler  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
888768458SSam Leffler  * All rights reserved.
988768458SSam Leffler  *
1088768458SSam Leffler  * Redistribution and use in source and binary forms, with or without
1188768458SSam Leffler  * modification, are permitted provided that the following conditions
1288768458SSam Leffler  * are met:
1388768458SSam Leffler  * 1. Redistributions of source code must retain the above copyright
1488768458SSam Leffler  *    notice, this list of conditions and the following disclaimer.
1588768458SSam Leffler  * 2. Redistributions in binary form must reproduce the above copyright
1688768458SSam Leffler  *    notice, this list of conditions and the following disclaimer in the
1788768458SSam Leffler  *    documentation and/or other materials provided with the distribution.
1888768458SSam Leffler  * 3. Neither the name of the project nor the names of its contributors
1988768458SSam Leffler  *    may be used to endorse or promote products derived from this software
2088768458SSam Leffler  *    without specific prior written permission.
2188768458SSam Leffler  *
2288768458SSam Leffler  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
2388768458SSam Leffler  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2488768458SSam Leffler  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2588768458SSam Leffler  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
2688768458SSam Leffler  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2788768458SSam Leffler  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2888768458SSam Leffler  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2988768458SSam Leffler  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3088768458SSam Leffler  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3188768458SSam Leffler  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3288768458SSam Leffler  * SUCH DAMAGE.
3388768458SSam Leffler  */
3488768458SSam Leffler 
3588768458SSam Leffler #include "opt_ipsec.h"
3688768458SSam Leffler 
3788768458SSam Leffler /* This code has derived from sys/net/rtsock.c on FreeBSD2.2.5 */
3888768458SSam Leffler 
3988768458SSam Leffler #include <sys/types.h>
4088768458SSam Leffler #include <sys/param.h>
4188768458SSam Leffler #include <sys/domain.h>
4288768458SSam Leffler #include <sys/errno.h>
4388768458SSam Leffler #include <sys/kernel.h>
448eafa045SBruce M Simpson #include <sys/lock.h>
4588768458SSam Leffler #include <sys/malloc.h>
4688768458SSam Leffler #include <sys/mbuf.h>
478eafa045SBruce M Simpson #include <sys/mutex.h>
48190320e2SBjoern A. Zeeb #include <sys/priv.h>
4988768458SSam Leffler #include <sys/protosw.h>
5088768458SSam Leffler #include <sys/signalvar.h>
5188768458SSam Leffler #include <sys/socket.h>
5288768458SSam Leffler #include <sys/socketvar.h>
5388768458SSam Leffler #include <sys/sysctl.h>
5488768458SSam Leffler #include <sys/systm.h>
5588768458SSam Leffler 
568b615593SMarko Zec #include <net/if.h>
57eedc7fd9SGleb Smirnoff #include <net/vnet.h>
5888768458SSam Leffler 
598b615593SMarko Zec #include <netinet/in.h>
608b615593SMarko Zec 
6188768458SSam Leffler #include <net/pfkeyv2.h>
6288768458SSam Leffler #include <netipsec/key.h>
6388768458SSam Leffler #include <netipsec/keysock.h>
6488768458SSam Leffler #include <netipsec/key_debug.h>
658b615593SMarko Zec #include <netipsec/ipsec.h>
6688768458SSam Leffler 
6788768458SSam Leffler #include <machine/stdarg.h>
6888768458SSam Leffler 
69ea7be129SGleb Smirnoff static struct mtx keysock_mtx;
70ea7be129SGleb Smirnoff MTX_SYSINIT(keysock, &keysock_mtx, "key socket pcb list", MTX_DEF);
71ea7be129SGleb Smirnoff 
72ea7be129SGleb Smirnoff #define	KEYSOCK_LOCK()		mtx_lock(&keysock_mtx)
73ea7be129SGleb Smirnoff #define	KEYSOCK_UNLOCK()	mtx_unlock(&keysock_mtx)
74ea7be129SGleb Smirnoff 
75b7bf3cb0SGleb Smirnoff VNET_DEFINE_STATIC(LIST_HEAD(, keycb), keycb_list) =
76b7bf3cb0SGleb Smirnoff     LIST_HEAD_INITIALIZER(keycb_list);
77ea7be129SGleb Smirnoff #define	V_keycb_list		VNET(keycb_list)
7888768458SSam Leffler 
79eddfbb76SRobert Watson static struct sockaddr key_src = { 2, PF_KEY, };
8088768458SSam Leffler 
81ea7be129SGleb Smirnoff static int key_sendup0(struct keycb *, struct mbuf *, int);
8288768458SSam Leffler 
83db8c0879SAndrey V. Elsukov VNET_PCPUSTAT_DEFINE(struct pfkeystat, pfkeystat);
84db8c0879SAndrey V. Elsukov VNET_PCPUSTAT_SYSINIT(pfkeystat);
85db8c0879SAndrey V. Elsukov 
86db8c0879SAndrey V. Elsukov #ifdef VIMAGE
87db8c0879SAndrey V. Elsukov VNET_PCPUSTAT_SYSUNINIT(pfkeystat);
88db8c0879SAndrey V. Elsukov #endif /* VIMAGE */
89eddfbb76SRobert Watson 
90ea7be129SGleb Smirnoff static int
91ea7be129SGleb Smirnoff key_send(struct socket *so, int flags, struct mbuf *m,
92ea7be129SGleb Smirnoff     struct sockaddr *nam, struct mbuf *control, struct thread *td)
9388768458SSam Leffler {
9488768458SSam Leffler 	struct sadb_msg *msg;
9588768458SSam Leffler 	int len, error = 0;
9688768458SSam Leffler 
97ea7be129SGleb Smirnoff 	if ((flags & PRUS_OOB) || control != NULL) {
98ea7be129SGleb Smirnoff 		m_freem(m);
99ea7be129SGleb Smirnoff 		if (control != NULL)
100ea7be129SGleb Smirnoff 			m_freem(control);
101ea7be129SGleb Smirnoff 		return (EOPNOTSUPP);
102ea7be129SGleb Smirnoff 	}
10388768458SSam Leffler 
104a04d64d8SAndrey V. Elsukov 	PFKEYSTAT_INC(out_total);
105a04d64d8SAndrey V. Elsukov 	PFKEYSTAT_ADD(out_bytes, m->m_pkthdr.len);
10688768458SSam Leffler 
10788768458SSam Leffler 	len = m->m_pkthdr.len;
10888768458SSam Leffler 	if (len < sizeof(struct sadb_msg)) {
109a04d64d8SAndrey V. Elsukov 		PFKEYSTAT_INC(out_tooshort);
11088768458SSam Leffler 		error = EINVAL;
11188768458SSam Leffler 		goto end;
11288768458SSam Leffler 	}
11388768458SSam Leffler 
11488768458SSam Leffler 	if (m->m_len < sizeof(struct sadb_msg)) {
115155d72c4SPedro F. Giffuni 		if ((m = m_pullup(m, sizeof(struct sadb_msg))) == NULL) {
116a04d64d8SAndrey V. Elsukov 			PFKEYSTAT_INC(out_nomem);
11788768458SSam Leffler 			error = ENOBUFS;
11888768458SSam Leffler 			goto end;
11988768458SSam Leffler 		}
12088768458SSam Leffler 	}
12188768458SSam Leffler 
122fe584538SDag-Erling Smørgrav 	M_ASSERTPKTHDR(m);
12388768458SSam Leffler 
124fcf59617SAndrey V. Elsukov 	KEYDBG(KEY_DUMP, kdebug_mbuf(m));
12588768458SSam Leffler 
12688768458SSam Leffler 	msg = mtod(m, struct sadb_msg *);
127a04d64d8SAndrey V. Elsukov 	PFKEYSTAT_INC(out_msgtype[msg->sadb_msg_type]);
12888768458SSam Leffler 	if (len != PFKEY_UNUNIT64(msg->sadb_msg_len)) {
129a04d64d8SAndrey V. Elsukov 		PFKEYSTAT_INC(out_invlen);
13088768458SSam Leffler 		error = EINVAL;
13188768458SSam Leffler 		goto end;
13288768458SSam Leffler 	}
13388768458SSam Leffler 
13488768458SSam Leffler 	error = key_parse(m, so);
13588768458SSam Leffler 	m = NULL;
13688768458SSam Leffler end:
137*fcc7aabdSKonstantin Belousov 	if (m != NULL)
13888768458SSam Leffler 		m_freem(m);
139*fcc7aabdSKonstantin Belousov 	return (error);
14088768458SSam Leffler }
14188768458SSam Leffler 
14288768458SSam Leffler /*
14388768458SSam Leffler  * send message to the socket.
14488768458SSam Leffler  */
14588768458SSam Leffler static int
146ea7be129SGleb Smirnoff key_sendup0(struct keycb *kp, struct mbuf *m, int promisc)
14788768458SSam Leffler {
14888768458SSam Leffler 
14988768458SSam Leffler 	if (promisc) {
15088768458SSam Leffler 		struct sadb_msg *pmsg;
15188768458SSam Leffler 
152eb1b1807SGleb Smirnoff 		M_PREPEND(m, sizeof(struct sadb_msg), M_NOWAIT);
1533d6aff56SAndrey V. Elsukov 		if (m == NULL) {
154a04d64d8SAndrey V. Elsukov 			PFKEYSTAT_INC(in_nomem);
1553d6aff56SAndrey V. Elsukov 			return (ENOBUFS);
15688768458SSam Leffler 		}
15788768458SSam Leffler 		pmsg = mtod(m, struct sadb_msg *);
15888768458SSam Leffler 		bzero(pmsg, sizeof(*pmsg));
15988768458SSam Leffler 		pmsg->sadb_msg_version = PF_KEY_V2;
16088768458SSam Leffler 		pmsg->sadb_msg_type = SADB_X_PROMISC;
16188768458SSam Leffler 		pmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
16288768458SSam Leffler 		/* pid and seq? */
16388768458SSam Leffler 
164a04d64d8SAndrey V. Elsukov 		PFKEYSTAT_INC(in_msgtype[pmsg->sadb_msg_type]);
16588768458SSam Leffler 	}
16688768458SSam Leffler 
167ea7be129SGleb Smirnoff 	if (!sbappendaddr(&kp->kp_socket->so_rcv, &key_src, m, NULL)) {
168a04d64d8SAndrey V. Elsukov 		PFKEYSTAT_INC(in_nomem);
16988768458SSam Leffler 		m_freem(m);
170ea7be129SGleb Smirnoff 		soroverflow(kp->kp_socket);
171*fcc7aabdSKonstantin Belousov 		return (ENOBUFS);
1727045b160SRoy Marples 	}
1737045b160SRoy Marples 
174ea7be129SGleb Smirnoff 	sorwakeup(kp->kp_socket);
175*fcc7aabdSKonstantin Belousov 	return (0);
17688768458SSam Leffler }
17788768458SSam Leffler 
17888768458SSam Leffler /* so can be NULL if target != KEY_SENDUP_ONE */
17988768458SSam Leffler int
1802e84e6eaSAndrey V. Elsukov key_sendup_mbuf(struct socket *so, struct mbuf *m, int target)
18188768458SSam Leffler {
18288768458SSam Leffler 	struct mbuf *n;
18388768458SSam Leffler 	struct keycb *kp;
18488768458SSam Leffler 	int error = 0;
18588768458SSam Leffler 
186e3004d24SAndrey V. Elsukov 	KASSERT(m != NULL, ("NULL mbuf pointer was passed."));
187e3004d24SAndrey V. Elsukov 	KASSERT(so != NULL || target != KEY_SENDUP_ONE,
188e3004d24SAndrey V. Elsukov 	    ("NULL socket pointer was passed."));
189d158b221SAndrey V. Elsukov 	KASSERT(target == KEY_SENDUP_ONE || target == KEY_SENDUP_ALL ||
190d158b221SAndrey V. Elsukov 	    target == KEY_SENDUP_REGISTERED, ("Wrong target %d", target));
19188768458SSam Leffler 
192a04d64d8SAndrey V. Elsukov 	PFKEYSTAT_INC(in_total);
193a04d64d8SAndrey V. Elsukov 	PFKEYSTAT_ADD(in_bytes, m->m_pkthdr.len);
19488768458SSam Leffler 	if (m->m_len < sizeof(struct sadb_msg)) {
19588768458SSam Leffler 		m = m_pullup(m, sizeof(struct sadb_msg));
19688768458SSam Leffler 		if (m == NULL) {
197a04d64d8SAndrey V. Elsukov 			PFKEYSTAT_INC(in_nomem);
198*fcc7aabdSKonstantin Belousov 			return (ENOBUFS);
19988768458SSam Leffler 		}
20088768458SSam Leffler 	}
20188768458SSam Leffler 	if (m->m_len >= sizeof(struct sadb_msg)) {
20288768458SSam Leffler 		struct sadb_msg *msg;
20388768458SSam Leffler 		msg = mtod(m, struct sadb_msg *);
204a04d64d8SAndrey V. Elsukov 		PFKEYSTAT_INC(in_msgtype[msg->sadb_msg_type]);
20588768458SSam Leffler 	}
206ea7be129SGleb Smirnoff 	KEYSOCK_LOCK();
207ea7be129SGleb Smirnoff 	LIST_FOREACH(kp, &V_keycb_list, kp_next) {
20888768458SSam Leffler 		/*
20988768458SSam Leffler 		 * If you are in promiscuous mode, and when you get broadcasted
21088768458SSam Leffler 		 * reply, you'll get two PF_KEY messages.
21188768458SSam Leffler 		 * (based on pf_key@inner.net message on 14 Oct 1998)
21288768458SSam Leffler 		 */
213017a5e58SAndrey V. Elsukov 		if (kp->kp_promisc) {
214017a5e58SAndrey V. Elsukov 			n = m_copym(m, 0, M_COPYALL, M_NOWAIT);
215017a5e58SAndrey V. Elsukov 			if (n != NULL)
216ea7be129SGleb Smirnoff 				key_sendup0(kp, n, 1);
217017a5e58SAndrey V. Elsukov 			else
218017a5e58SAndrey V. Elsukov 				PFKEYSTAT_INC(in_nomem);
21988768458SSam Leffler 		}
22088768458SSam Leffler 
22188768458SSam Leffler 		/* the exact target will be processed later */
222ea7be129SGleb Smirnoff 		if (so != NULL && so->so_pcb == kp)
22388768458SSam Leffler 			continue;
22488768458SSam Leffler 
225*fcc7aabdSKonstantin Belousov 		if (target == KEY_SENDUP_ONE || (target ==
226*fcc7aabdSKonstantin Belousov 		    KEY_SENDUP_REGISTERED && kp->kp_registered == 0))
22788768458SSam Leffler 			continue;
22888768458SSam Leffler 
229017a5e58SAndrey V. Elsukov 		/* KEY_SENDUP_ALL + KEY_SENDUP_REGISTERED */
230017a5e58SAndrey V. Elsukov 		n = m_copym(m, 0, M_COPYALL, M_NOWAIT);
231017a5e58SAndrey V. Elsukov 		if (n == NULL) {
232a04d64d8SAndrey V. Elsukov 			PFKEYSTAT_INC(in_nomem);
233017a5e58SAndrey V. Elsukov 			/* Try send to another socket */
234017a5e58SAndrey V. Elsukov 			continue;
23588768458SSam Leffler 		}
23688768458SSam Leffler 
237ea7be129SGleb Smirnoff 		if (key_sendup0(kp, n, 0) == 0)
238017a5e58SAndrey V. Elsukov 			PFKEYSTAT_INC(in_msgtarget[target]);
23988768458SSam Leffler 	}
24088768458SSam Leffler 
241017a5e58SAndrey V. Elsukov 	if (so)	{ /* KEY_SENDUP_ONE */
242ea7be129SGleb Smirnoff 		error = key_sendup0(so->so_pcb, m, 0);
243017a5e58SAndrey V. Elsukov 		if (error == 0)
244017a5e58SAndrey V. Elsukov 			PFKEYSTAT_INC(in_msgtarget[KEY_SENDUP_ONE]);
24588768458SSam Leffler 	} else {
24688768458SSam Leffler 		error = 0;
24788768458SSam Leffler 		m_freem(m);
24888768458SSam Leffler 	}
249ea7be129SGleb Smirnoff 	KEYSOCK_UNLOCK();
250017a5e58SAndrey V. Elsukov 	return (error);
25188768458SSam Leffler }
25288768458SSam Leffler 
253ea7be129SGleb Smirnoff static u_long key_sendspace = 8192;
254ea7be129SGleb Smirnoff SYSCTL_ULONG(_net_key, OID_AUTO, sendspace, CTLFLAG_RW, &key_sendspace, 0,
255ea7be129SGleb Smirnoff     "Default key socket send space");
256ea7be129SGleb Smirnoff static u_long key_recvspace = 8192;
257ea7be129SGleb Smirnoff SYSCTL_ULONG(_net_key, OID_AUTO, recvspace, CTLFLAG_RW, &key_recvspace, 0,
258ea7be129SGleb Smirnoff     "Default key socket receive space");
25988768458SSam Leffler 
26088768458SSam Leffler static int
26188768458SSam Leffler key_attach(struct socket *so, int proto, struct thread *td)
26288768458SSam Leffler {
26388768458SSam Leffler 	struct keycb *kp;
2642cb64cb2SGeorge V. Neville-Neil 	int error;
26588768458SSam Leffler 
2662cb64cb2SGeorge V. Neville-Neil 	KASSERT(so->so_pcb == NULL, ("key_attach: so_pcb != NULL"));
2672cb64cb2SGeorge V. Neville-Neil 
268cf94a6a9SBjoern A. Zeeb 	if (td != NULL) {
269cf94a6a9SBjoern A. Zeeb 		error = priv_check(td, PRIV_NET_RAW);
270*fcc7aabdSKonstantin Belousov 		if (error != 0)
271*fcc7aabdSKonstantin Belousov 			return (error);
272cf94a6a9SBjoern A. Zeeb 	}
273cf94a6a9SBjoern A. Zeeb 
274ea7be129SGleb Smirnoff 	error = soreserve(so, key_sendspace, key_recvspace);
275*fcc7aabdSKonstantin Belousov 	if (error != 0)
276ea7be129SGleb Smirnoff 		return (error);
27788768458SSam Leffler 
278ea7be129SGleb Smirnoff 	kp = malloc(sizeof(*kp), M_PCB, M_WAITOK);
279ea7be129SGleb Smirnoff 	kp->kp_socket = so;
28088768458SSam Leffler 	kp->kp_promisc = kp->kp_registered = 0;
28188768458SSam Leffler 
282ea7be129SGleb Smirnoff 	so->so_pcb = kp;
28388768458SSam Leffler 	so->so_options |= SO_USELOOPBACK;
28488768458SSam Leffler 
285ea7be129SGleb Smirnoff 	KEYSOCK_LOCK();
286ea7be129SGleb Smirnoff 	LIST_INSERT_HEAD(&V_keycb_list, kp, kp_next);
287ea7be129SGleb Smirnoff 	KEYSOCK_UNLOCK();
288ea7be129SGleb Smirnoff 	soisconnected(so);
289ea7be129SGleb Smirnoff 
290ea7be129SGleb Smirnoff 	return (0);
29188768458SSam Leffler }
29288768458SSam Leffler 
293a152f8a3SRobert Watson static void
29487b4dfd5SGeorge V. Neville-Neil key_close(struct socket *so)
295a152f8a3SRobert Watson {
296a152f8a3SRobert Watson 
297ea7be129SGleb Smirnoff 	soisdisconnected(so);
298a152f8a3SRobert Watson }
299a152f8a3SRobert Watson 
300bc725eafSRobert Watson static void
30188768458SSam Leffler key_detach(struct socket *so)
30288768458SSam Leffler {
303ea7be129SGleb Smirnoff 	struct keycb *kp = so->so_pcb;
30488768458SSam Leffler 
30588768458SSam Leffler 	key_freereg(so);
306ea7be129SGleb Smirnoff 	KEYSOCK_LOCK();
307ea7be129SGleb Smirnoff 	LIST_REMOVE(kp, kp_next);
308ea7be129SGleb Smirnoff 	KEYSOCK_UNLOCK();
309ea7be129SGleb Smirnoff 	free(kp, M_PCB);
310ea7be129SGleb Smirnoff 	so->so_pcb = NULL;
31188768458SSam Leffler }
31288768458SSam Leffler 
31388768458SSam Leffler static int
31488768458SSam Leffler key_shutdown(struct socket *so)
31588768458SSam Leffler {
316ea7be129SGleb Smirnoff 	socantsendmore(so);
317ea7be129SGleb Smirnoff 	return (0);
31888768458SSam Leffler }
31988768458SSam Leffler 
3207029da5cSPawel Biernacki SYSCTL_NODE(_net, PF_KEY, key, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
3217029da5cSPawel Biernacki     "Key Family");
32288768458SSam Leffler 
323e7d02be1SGleb Smirnoff static struct protosw keysw = {
324303989a2SRuslan Ermilov 	.pr_type =		SOCK_RAW,
325303989a2SRuslan Ermilov 	.pr_protocol =		PF_KEY_V2,
326303989a2SRuslan Ermilov 	.pr_flags =		PR_ATOMIC | PR_ADDR,
327e7d02be1SGleb Smirnoff 	.pr_abort =		key_close,
328e7d02be1SGleb Smirnoff 	.pr_attach =		key_attach,
329e7d02be1SGleb Smirnoff 	.pr_detach =		key_detach,
330e7d02be1SGleb Smirnoff 	.pr_send =		key_send,
331e7d02be1SGleb Smirnoff 	.pr_shutdown =		key_shutdown,
332e7d02be1SGleb Smirnoff 	.pr_close =		key_close,
33388768458SSam Leffler };
33488768458SSam Leffler 
335e7d02be1SGleb Smirnoff static struct domain keydomain = {
336303989a2SRuslan Ermilov 	.dom_family =		PF_KEY,
337303989a2SRuslan Ermilov 	.dom_name =		"key",
338e7d02be1SGleb Smirnoff 	.dom_nprotosw =		1,
339e7d02be1SGleb Smirnoff 	.dom_protosw =		{ &keysw },
340303989a2SRuslan Ermilov };
3419880323aSGleb Smirnoff DOMAIN_SET(key);
342