xref: /freebsd-13-stable/sys/netipsec/keysock.c (revision 4fbf14e22d7b83de7080a8e491ba14a5785a0ff4)
1 /*	$KAME: keysock.c,v 1.25 2001/08/13 20:07:41 itojun Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-3-Clause
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 "opt_ipsec.h"
35 
36 /* This code has derived from sys/net/rtsock.c on FreeBSD2.2.5 */
37 
38 #include <sys/types.h>
39 #include <sys/param.h>
40 #include <sys/domain.h>
41 #include <sys/errno.h>
42 #include <sys/kernel.h>
43 #include <sys/lock.h>
44 #include <sys/malloc.h>
45 #include <sys/mbuf.h>
46 #include <sys/mutex.h>
47 #include <sys/priv.h>
48 #include <sys/protosw.h>
49 #include <sys/signalvar.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
52 #include <sys/sysctl.h>
53 #include <sys/systm.h>
54 
55 #include <net/if.h>
56 #include <net/vnet.h>
57 #include <net/raw_cb.h>
58 
59 #include <netinet/in.h>
60 
61 #include <net/pfkeyv2.h>
62 #include <netipsec/key.h>
63 #include <netipsec/keysock.h>
64 #include <netipsec/key_debug.h>
65 #include <netipsec/ipsec.h>
66 
67 #include <machine/stdarg.h>
68 
69 struct key_cb {
70 	int key_count;
71 	int any_count;
72 };
73 VNET_DEFINE_STATIC(struct key_cb, key_cb);
74 #define	V_key_cb		VNET(key_cb)
75 
76 static struct sockaddr key_src = { 2, PF_KEY, };
77 
78 static int key_sendup0(struct rawcb *, struct mbuf *, int);
79 
80 VNET_PCPUSTAT_DEFINE(struct pfkeystat, pfkeystat);
81 VNET_PCPUSTAT_SYSINIT(pfkeystat);
82 
83 #ifdef VIMAGE
84 VNET_PCPUSTAT_SYSUNINIT(pfkeystat);
85 #endif /* VIMAGE */
86 
87 /*
88  * key_output()
89  */
90 int
key_output(struct mbuf * m,struct socket * so,...)91 key_output(struct mbuf *m, struct socket *so, ...)
92 {
93 	struct sadb_msg *msg;
94 	int len, error = 0;
95 
96 	if (m == NULL)
97 		panic("%s: NULL pointer was passed.\n", __func__);
98 
99 	PFKEYSTAT_INC(out_total);
100 	PFKEYSTAT_ADD(out_bytes, m->m_pkthdr.len);
101 
102 	len = m->m_pkthdr.len;
103 	if (len < sizeof(struct sadb_msg)) {
104 		PFKEYSTAT_INC(out_tooshort);
105 		error = EINVAL;
106 		goto end;
107 	}
108 
109 	if (m->m_len < sizeof(struct sadb_msg)) {
110 		if ((m = m_pullup(m, sizeof(struct sadb_msg))) == NULL) {
111 			PFKEYSTAT_INC(out_nomem);
112 			error = ENOBUFS;
113 			goto end;
114 		}
115 	}
116 
117 	M_ASSERTPKTHDR(m);
118 
119 	KEYDBG(KEY_DUMP, kdebug_mbuf(m));
120 
121 	msg = mtod(m, struct sadb_msg *);
122 	PFKEYSTAT_INC(out_msgtype[msg->sadb_msg_type]);
123 	if (len != PFKEY_UNUNIT64(msg->sadb_msg_len)) {
124 		PFKEYSTAT_INC(out_invlen);
125 		error = EINVAL;
126 		goto end;
127 	}
128 
129 	error = key_parse(m, so);
130 	m = NULL;
131 end:
132 	if (m)
133 		m_freem(m);
134 	return error;
135 }
136 
137 /*
138  * send message to the socket.
139  */
140 static int
key_sendup0(struct rawcb * rp,struct mbuf * m,int promisc)141 key_sendup0(struct rawcb *rp, struct mbuf *m, int promisc)
142 {
143 
144 	if (promisc) {
145 		struct sadb_msg *pmsg;
146 
147 		M_PREPEND(m, sizeof(struct sadb_msg), M_NOWAIT);
148 		if (m == NULL) {
149 			PFKEYSTAT_INC(in_nomem);
150 			return (ENOBUFS);
151 		}
152 		pmsg = mtod(m, struct sadb_msg *);
153 		bzero(pmsg, sizeof(*pmsg));
154 		pmsg->sadb_msg_version = PF_KEY_V2;
155 		pmsg->sadb_msg_type = SADB_X_PROMISC;
156 		pmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
157 		/* pid and seq? */
158 
159 		PFKEYSTAT_INC(in_msgtype[pmsg->sadb_msg_type]);
160 	}
161 
162 	if (!sbappendaddr(&rp->rcb_socket->so_rcv, (struct sockaddr *)&key_src,
163 	    m, NULL)) {
164 		PFKEYSTAT_INC(in_nomem);
165 		m_freem(m);
166 		soroverflow(rp->rcb_socket);
167 		return ENOBUFS;
168 	}
169 
170 	sorwakeup(rp->rcb_socket);
171 	return 0;
172 }
173 
174 /* so can be NULL if target != KEY_SENDUP_ONE */
175 int
key_sendup_mbuf(struct socket * so,struct mbuf * m,int target)176 key_sendup_mbuf(struct socket *so, struct mbuf *m, int target)
177 {
178 	struct mbuf *n;
179 	struct keycb *kp;
180 	struct rawcb *rp;
181 	int error = 0;
182 
183 	KASSERT(m != NULL, ("NULL mbuf pointer was passed."));
184 	KASSERT(so != NULL || target != KEY_SENDUP_ONE,
185 	    ("NULL socket pointer was passed."));
186 	KASSERT(target == KEY_SENDUP_ONE || target == KEY_SENDUP_ALL ||
187 	    target == KEY_SENDUP_REGISTERED, ("Wrong target %d", target));
188 
189 	PFKEYSTAT_INC(in_total);
190 	PFKEYSTAT_ADD(in_bytes, m->m_pkthdr.len);
191 	if (m->m_len < sizeof(struct sadb_msg)) {
192 		m = m_pullup(m, sizeof(struct sadb_msg));
193 		if (m == NULL) {
194 			PFKEYSTAT_INC(in_nomem);
195 			return ENOBUFS;
196 		}
197 	}
198 	if (m->m_len >= sizeof(struct sadb_msg)) {
199 		struct sadb_msg *msg;
200 		msg = mtod(m, struct sadb_msg *);
201 		PFKEYSTAT_INC(in_msgtype[msg->sadb_msg_type]);
202 	}
203 	mtx_lock(&rawcb_mtx);
204 	if (V_key_cb.any_count == 0) {
205 		mtx_unlock(&rawcb_mtx);
206 		m_freem(m);
207 		return (0);
208 	}
209 	LIST_FOREACH(rp, &V_rawcb_list, list)
210 	{
211 		if (rp->rcb_proto.sp_family != PF_KEY)
212 			continue;
213 		if (rp->rcb_proto.sp_protocol
214 		 && rp->rcb_proto.sp_protocol != PF_KEY_V2) {
215 			continue;
216 		}
217 
218 		/*
219 		 * If you are in promiscuous mode, and when you get broadcasted
220 		 * reply, you'll get two PF_KEY messages.
221 		 * (based on pf_key@inner.net message on 14 Oct 1998)
222 		 */
223 		kp = (struct keycb *)rp;
224 		if (kp->kp_promisc) {
225 			n = m_copym(m, 0, M_COPYALL, M_NOWAIT);
226 			if (n != NULL)
227 				key_sendup0(rp, n, 1);
228 			else
229 				PFKEYSTAT_INC(in_nomem);
230 		}
231 
232 		/* the exact target will be processed later */
233 		if (so && sotorawcb(so) == rp)
234 			continue;
235 
236 		if (target == KEY_SENDUP_ONE || (
237 		    target == KEY_SENDUP_REGISTERED && kp->kp_registered == 0))
238 			continue;
239 
240 		/* KEY_SENDUP_ALL + KEY_SENDUP_REGISTERED */
241 		n = m_copym(m, 0, M_COPYALL, M_NOWAIT);
242 		if (n == NULL) {
243 			PFKEYSTAT_INC(in_nomem);
244 			/* Try send to another socket */
245 			continue;
246 		}
247 
248 		if (key_sendup0(rp, n, 0) == 0)
249 			PFKEYSTAT_INC(in_msgtarget[target]);
250 	}
251 
252 	if (so)	{ /* KEY_SENDUP_ONE */
253 		error = key_sendup0(sotorawcb(so), m, 0);
254 		if (error == 0)
255 			PFKEYSTAT_INC(in_msgtarget[KEY_SENDUP_ONE]);
256 	} else {
257 		error = 0;
258 		m_freem(m);
259 	}
260 	mtx_unlock(&rawcb_mtx);
261 	return (error);
262 }
263 
264 /*
265  * key_abort()
266  * derived from net/rtsock.c:rts_abort()
267  */
268 static void
key_abort(struct socket * so)269 key_abort(struct socket *so)
270 {
271 	raw_usrreqs.pru_abort(so);
272 }
273 
274 /*
275  * key_attach()
276  * derived from net/rtsock.c:rts_attach()
277  */
278 static int
key_attach(struct socket * so,int proto,struct thread * td)279 key_attach(struct socket *so, int proto, struct thread *td)
280 {
281 	struct keycb *kp;
282 	int error;
283 
284 	KASSERT(so->so_pcb == NULL, ("key_attach: so_pcb != NULL"));
285 
286 	if (td != NULL) {
287 		error = priv_check(td, PRIV_NET_RAW);
288 		if (error)
289 			return error;
290 	}
291 
292 	/* XXX */
293 	kp = malloc(sizeof *kp, M_PCB, M_WAITOK | M_ZERO);
294 	if (kp == NULL)
295 		return ENOBUFS;
296 
297 	so->so_pcb = (caddr_t)kp;
298 	error = raw_attach(so, proto);
299 	kp = (struct keycb *)sotorawcb(so);
300 	if (error) {
301 		free(kp, M_PCB);
302 		so->so_pcb = (caddr_t) 0;
303 		return error;
304 	}
305 
306 	kp->kp_promisc = kp->kp_registered = 0;
307 
308 	if (kp->kp_raw.rcb_proto.sp_protocol == PF_KEY) /* XXX: AF_KEY */
309 		V_key_cb.key_count++;
310 	V_key_cb.any_count++;
311 	soisconnected(so);
312 	so->so_options |= SO_USELOOPBACK;
313 
314 	return 0;
315 }
316 
317 /*
318  * key_bind()
319  * derived from net/rtsock.c:rts_bind()
320  */
321 static int
key_bind(struct socket * so,struct sockaddr * nam,struct thread * td)322 key_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
323 {
324 	return EINVAL;
325 }
326 
327 /*
328  * key_close()
329  * derived from net/rtsock.c:rts_close().
330  */
331 static void
key_close(struct socket * so)332 key_close(struct socket *so)
333 {
334 
335 	raw_usrreqs.pru_close(so);
336 }
337 
338 /*
339  * key_connect()
340  * derived from net/rtsock.c:rts_connect()
341  */
342 static int
key_connect(struct socket * so,struct sockaddr * nam,struct thread * td)343 key_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
344 {
345 	return EINVAL;
346 }
347 
348 /*
349  * key_detach()
350  * derived from net/rtsock.c:rts_detach()
351  */
352 static void
key_detach(struct socket * so)353 key_detach(struct socket *so)
354 {
355 	struct keycb *kp = (struct keycb *)sotorawcb(so);
356 
357 	KASSERT(kp != NULL, ("key_detach: kp == NULL"));
358 	if (kp->kp_raw.rcb_proto.sp_protocol
359 	    == PF_KEY) /* XXX: AF_KEY */
360 		V_key_cb.key_count--;
361 	V_key_cb.any_count--;
362 
363 	key_freereg(so);
364 	raw_usrreqs.pru_detach(so);
365 }
366 
367 /*
368  * key_disconnect()
369  * derived from net/rtsock.c:key_disconnect()
370  */
371 static int
key_disconnect(struct socket * so)372 key_disconnect(struct socket *so)
373 {
374 	return(raw_usrreqs.pru_disconnect(so));
375 }
376 
377 /*
378  * key_peeraddr()
379  * derived from net/rtsock.c:rts_peeraddr()
380  */
381 static int
key_peeraddr(struct socket * so,struct sockaddr ** nam)382 key_peeraddr(struct socket *so, struct sockaddr **nam)
383 {
384 	return(raw_usrreqs.pru_peeraddr(so, nam));
385 }
386 
387 /*
388  * key_send()
389  * derived from net/rtsock.c:rts_send()
390  */
391 static int
key_send(struct socket * so,int flags,struct mbuf * m,struct sockaddr * nam,struct mbuf * control,struct thread * td)392 key_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
393 	 struct mbuf *control, struct thread *td)
394 {
395 	return(raw_usrreqs.pru_send(so, flags, m, nam, control, td));
396 }
397 
398 /*
399  * key_shutdown()
400  * derived from net/rtsock.c:rts_shutdown()
401  */
402 static int
key_shutdown(struct socket * so)403 key_shutdown(struct socket *so)
404 {
405 	return(raw_usrreqs.pru_shutdown(so));
406 }
407 
408 /*
409  * key_sockaddr()
410  * derived from net/rtsock.c:rts_sockaddr()
411  */
412 static int
key_sockaddr(struct socket * so,struct sockaddr ** nam)413 key_sockaddr(struct socket *so, struct sockaddr **nam)
414 {
415 	return(raw_usrreqs.pru_sockaddr(so, nam));
416 }
417 
418 struct pr_usrreqs key_usrreqs = {
419 	.pru_abort =		key_abort,
420 	.pru_attach =		key_attach,
421 	.pru_bind =		key_bind,
422 	.pru_connect =		key_connect,
423 	.pru_detach =		key_detach,
424 	.pru_disconnect =	key_disconnect,
425 	.pru_peeraddr =		key_peeraddr,
426 	.pru_send =		key_send,
427 	.pru_shutdown =		key_shutdown,
428 	.pru_sockaddr =		key_sockaddr,
429 	.pru_close =		key_close,
430 };
431 
432 /* sysctl */
433 SYSCTL_NODE(_net, PF_KEY, key, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
434     "Key Family");
435 
436 /*
437  * Definitions of protocols supported in the KEY domain.
438  */
439 
440 extern struct domain keydomain;
441 
442 struct protosw keysw[] = {
443 {
444 	.pr_type =		SOCK_RAW,
445 	.pr_domain =		&keydomain,
446 	.pr_protocol =		PF_KEY_V2,
447 	.pr_flags =		PR_ATOMIC|PR_ADDR,
448 	.pr_output =		key_output,
449 	.pr_ctlinput =		raw_ctlinput,
450 	.pr_init =		raw_init,
451 	.pr_usrreqs =		&key_usrreqs
452 }
453 };
454 
455 static void
key_init0(void)456 key_init0(void)
457 {
458 
459 	bzero((caddr_t)&V_key_cb, sizeof(V_key_cb));
460 	key_init();
461 }
462 
463 struct domain keydomain = {
464 	.dom_family =		PF_KEY,
465 	.dom_name =		"key",
466 	.dom_init =		key_init0,
467 #ifdef VIMAGE
468 	.dom_destroy =		key_destroy,
469 #endif
470 	.dom_protosw =		keysw,
471 	.dom_protoswNPROTOSW =	&keysw[nitems(keysw)]
472 };
473 
474 VNET_DOMAIN_SET(key);
475