keysock.c revision 185419
1/*	$FreeBSD: head/sys/netipsec/keysock.c 185419 2008-11-28 23:30:51Z zec $	*/
2/*	$KAME: keysock.c,v 1.25 2001/08/13 20:07:41 itojun Exp $	*/
3
4/*-
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#include "opt_ipsec.h"
34
35/* This code has derived from sys/net/rtsock.c on FreeBSD2.2.5 */
36
37#include <sys/types.h>
38#include <sys/param.h>
39#include <sys/domain.h>
40#include <sys/errno.h>
41#include <sys/kernel.h>
42#include <sys/lock.h>
43#include <sys/malloc.h>
44#include <sys/mbuf.h>
45#include <sys/mutex.h>
46#include <sys/priv.h>
47#include <sys/protosw.h>
48#include <sys/signalvar.h>
49#include <sys/socket.h>
50#include <sys/socketvar.h>
51#include <sys/sysctl.h>
52#include <sys/systm.h>
53#include <sys/vimage.h>
54
55#include <net/if.h>
56#include <net/raw_cb.h>
57#include <net/route.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#ifdef VIMAGE_GLOBALS
70static struct key_cb key_cb;
71struct pfkeystat pfkeystat;
72#endif
73
74static struct sockaddr key_src = { 2, PF_KEY };
75
76static int key_sendup0 __P((struct rawcb *, struct mbuf *, int));
77
78/*
79 * key_output()
80 */
81int
82key_output(struct mbuf *m, struct socket *so)
83{
84	INIT_VNET_IPSEC(curvnet);
85	struct sadb_msg *msg;
86	int len, error = 0;
87
88	if (m == 0)
89		panic("%s: NULL pointer was passed.\n", __func__);
90
91	V_pfkeystat.out_total++;
92	V_pfkeystat.out_bytes += m->m_pkthdr.len;
93
94	len = m->m_pkthdr.len;
95	if (len < sizeof(struct sadb_msg)) {
96		V_pfkeystat.out_tooshort++;
97		error = EINVAL;
98		goto end;
99	}
100
101	if (m->m_len < sizeof(struct sadb_msg)) {
102		if ((m = m_pullup(m, sizeof(struct sadb_msg))) == 0) {
103			V_pfkeystat.out_nomem++;
104			error = ENOBUFS;
105			goto end;
106		}
107	}
108
109	M_ASSERTPKTHDR(m);
110
111	KEYDEBUG(KEYDEBUG_KEY_DUMP, kdebug_mbuf(m));
112
113	msg = mtod(m, struct sadb_msg *);
114	V_pfkeystat.out_msgtype[msg->sadb_msg_type]++;
115	if (len != PFKEY_UNUNIT64(msg->sadb_msg_len)) {
116		V_pfkeystat.out_invlen++;
117		error = EINVAL;
118		goto end;
119	}
120
121	error = key_parse(m, so);
122	m = NULL;
123end:
124	if (m)
125		m_freem(m);
126	return error;
127}
128
129/*
130 * send message to the socket.
131 */
132static int
133key_sendup0(rp, m, promisc)
134	struct rawcb *rp;
135	struct mbuf *m;
136	int promisc;
137{
138	INIT_VNET_IPSEC(curvnet);
139	int error;
140
141	if (promisc) {
142		struct sadb_msg *pmsg;
143
144		M_PREPEND(m, sizeof(struct sadb_msg), M_DONTWAIT);
145		if (m && m->m_len < sizeof(struct sadb_msg))
146			m = m_pullup(m, sizeof(struct sadb_msg));
147		if (!m) {
148			V_pfkeystat.in_nomem++;
149			m_freem(m);
150			return ENOBUFS;
151		}
152		m->m_pkthdr.len += sizeof(*pmsg);
153
154		pmsg = mtod(m, struct sadb_msg *);
155		bzero(pmsg, sizeof(*pmsg));
156		pmsg->sadb_msg_version = PF_KEY_V2;
157		pmsg->sadb_msg_type = SADB_X_PROMISC;
158		pmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
159		/* pid and seq? */
160
161		V_pfkeystat.in_msgtype[pmsg->sadb_msg_type]++;
162	}
163
164	if (!sbappendaddr(&rp->rcb_socket->so_rcv, (struct sockaddr *)&key_src,
165	    m, NULL)) {
166		V_pfkeystat.in_nomem++;
167		m_freem(m);
168		error = ENOBUFS;
169	} else
170		error = 0;
171	sorwakeup(rp->rcb_socket);
172	return error;
173}
174
175/* XXX this interface should be obsoleted. */
176int
177key_sendup(so, msg, len, target)
178	struct socket *so;
179	struct sadb_msg *msg;
180	u_int len;
181	int target;	/*target of the resulting message*/
182{
183	INIT_VNET_IPSEC(curvnet);
184	struct mbuf *m, *n, *mprev;
185	int tlen;
186
187	/* sanity check */
188	if (so == 0 || msg == 0)
189		panic("%s: NULL pointer was passed.\n", __func__);
190
191	KEYDEBUG(KEYDEBUG_KEY_DUMP,
192		printf("%s: \n", __func__);
193		kdebug_sadb(msg));
194
195	/*
196	 * we increment statistics here, just in case we have ENOBUFS
197	 * in this function.
198	 */
199	V_pfkeystat.in_total++;
200	V_pfkeystat.in_bytes += len;
201	V_pfkeystat.in_msgtype[msg->sadb_msg_type]++;
202
203	/*
204	 * Get mbuf chain whenever possible (not clusters),
205	 * to save socket buffer.  We'll be generating many SADB_ACQUIRE
206	 * messages to listening key sockets.  If we simply allocate clusters,
207	 * sbappendaddr() will raise ENOBUFS due to too little sbspace().
208	 * sbspace() computes # of actual data bytes AND mbuf region.
209	 *
210	 * TODO: SADB_ACQUIRE filters should be implemented.
211	 */
212	tlen = len;
213	m = mprev = NULL;
214	while (tlen > 0) {
215		if (tlen == len) {
216			MGETHDR(n, M_DONTWAIT, MT_DATA);
217			if (n == NULL) {
218				V_pfkeystat.in_nomem++;
219				return ENOBUFS;
220			}
221			n->m_len = MHLEN;
222		} else {
223			MGET(n, M_DONTWAIT, MT_DATA);
224			if (n == NULL) {
225				V_pfkeystat.in_nomem++;
226				return ENOBUFS;
227			}
228			n->m_len = MLEN;
229		}
230		if (tlen >= MCLBYTES) {	/*XXX better threshold? */
231			MCLGET(n, M_DONTWAIT);
232			if ((n->m_flags & M_EXT) == 0) {
233				m_free(n);
234				m_freem(m);
235				V_pfkeystat.in_nomem++;
236				return ENOBUFS;
237			}
238			n->m_len = MCLBYTES;
239		}
240
241		if (tlen < n->m_len)
242			n->m_len = tlen;
243		n->m_next = NULL;
244		if (m == NULL)
245			m = mprev = n;
246		else {
247			mprev->m_next = n;
248			mprev = n;
249		}
250		tlen -= n->m_len;
251		n = NULL;
252	}
253	m->m_pkthdr.len = len;
254	m->m_pkthdr.rcvif = NULL;
255	m_copyback(m, 0, len, (caddr_t)msg);
256
257	/* avoid duplicated statistics */
258	V_pfkeystat.in_total--;
259	V_pfkeystat.in_bytes -= len;
260	V_pfkeystat.in_msgtype[msg->sadb_msg_type]--;
261
262	return key_sendup_mbuf(so, m, target);
263}
264
265/* so can be NULL if target != KEY_SENDUP_ONE */
266int
267key_sendup_mbuf(so, m, target)
268	struct socket *so;
269	struct mbuf *m;
270	int target;
271{
272	INIT_VNET_NET(curvnet);
273	INIT_VNET_IPSEC(curvnet);
274	struct mbuf *n;
275	struct keycb *kp;
276	int sendup;
277	struct rawcb *rp;
278	int error = 0;
279
280	if (m == NULL)
281		panic("key_sendup_mbuf: NULL pointer was passed.\n");
282	if (so == NULL && target == KEY_SENDUP_ONE)
283		panic("%s: NULL pointer was passed.\n", __func__);
284
285	V_pfkeystat.in_total++;
286	V_pfkeystat.in_bytes += m->m_pkthdr.len;
287	if (m->m_len < sizeof(struct sadb_msg)) {
288		m = m_pullup(m, sizeof(struct sadb_msg));
289		if (m == NULL) {
290			V_pfkeystat.in_nomem++;
291			return ENOBUFS;
292		}
293	}
294	if (m->m_len >= sizeof(struct sadb_msg)) {
295		struct sadb_msg *msg;
296		msg = mtod(m, struct sadb_msg *);
297		V_pfkeystat.in_msgtype[msg->sadb_msg_type]++;
298	}
299	mtx_lock(&rawcb_mtx);
300	LIST_FOREACH(rp, &V_rawcb_list, list)
301	{
302		if (rp->rcb_proto.sp_family != PF_KEY)
303			continue;
304		if (rp->rcb_proto.sp_protocol
305		 && rp->rcb_proto.sp_protocol != PF_KEY_V2) {
306			continue;
307		}
308
309		kp = (struct keycb *)rp;
310
311		/*
312		 * If you are in promiscuous mode, and when you get broadcasted
313		 * reply, you'll get two PF_KEY messages.
314		 * (based on pf_key@inner.net message on 14 Oct 1998)
315		 */
316		if (((struct keycb *)rp)->kp_promisc) {
317			if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
318				(void)key_sendup0(rp, n, 1);
319				n = NULL;
320			}
321		}
322
323		/* the exact target will be processed later */
324		if (so && sotorawcb(so) == rp)
325			continue;
326
327		sendup = 0;
328		switch (target) {
329		case KEY_SENDUP_ONE:
330			/* the statement has no effect */
331			if (so && sotorawcb(so) == rp)
332				sendup++;
333			break;
334		case KEY_SENDUP_ALL:
335			sendup++;
336			break;
337		case KEY_SENDUP_REGISTERED:
338			if (kp->kp_registered)
339				sendup++;
340			break;
341		}
342		V_pfkeystat.in_msgtarget[target]++;
343
344		if (!sendup)
345			continue;
346
347		if ((n = m_copy(m, 0, (int)M_COPYALL)) == NULL) {
348			m_freem(m);
349			V_pfkeystat.in_nomem++;
350			mtx_unlock(&rawcb_mtx);
351			return ENOBUFS;
352		}
353
354		if ((error = key_sendup0(rp, n, 0)) != 0) {
355			m_freem(m);
356			mtx_unlock(&rawcb_mtx);
357			return error;
358		}
359
360		n = NULL;
361	}
362
363	if (so) {
364		error = key_sendup0(sotorawcb(so), m, 0);
365		m = NULL;
366	} else {
367		error = 0;
368		m_freem(m);
369	}
370	mtx_unlock(&rawcb_mtx);
371	return error;
372}
373
374/*
375 * key_abort()
376 * derived from net/rtsock.c:rts_abort()
377 */
378static void
379key_abort(struct socket *so)
380{
381	raw_usrreqs.pru_abort(so);
382}
383
384/*
385 * key_attach()
386 * derived from net/rtsock.c:rts_attach()
387 */
388static int
389key_attach(struct socket *so, int proto, struct thread *td)
390{
391	INIT_VNET_IPSEC(curvnet);
392	struct keycb *kp;
393	int error;
394
395	KASSERT(so->so_pcb == NULL, ("key_attach: so_pcb != NULL"));
396
397	if (td != NULL) {
398		error = priv_check(td, PRIV_NET_RAW);
399		if (error)
400			return error;
401	}
402
403	/* XXX */
404	kp = malloc(sizeof *kp, M_PCB, M_WAITOK | M_ZERO);
405	if (kp == 0)
406		return ENOBUFS;
407
408	so->so_pcb = (caddr_t)kp;
409	error = raw_attach(so, proto);
410	kp = (struct keycb *)sotorawcb(so);
411	if (error) {
412		free(kp, M_PCB);
413		so->so_pcb = (caddr_t) 0;
414		return error;
415	}
416
417	kp->kp_promisc = kp->kp_registered = 0;
418
419	if (kp->kp_raw.rcb_proto.sp_protocol == PF_KEY) /* XXX: AF_KEY */
420		V_key_cb.key_count++;
421	V_key_cb.any_count++;
422	soisconnected(so);
423	so->so_options |= SO_USELOOPBACK;
424
425	return 0;
426}
427
428/*
429 * key_bind()
430 * derived from net/rtsock.c:rts_bind()
431 */
432static int
433key_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
434{
435  return EINVAL;
436}
437
438/*
439 * key_close()
440 * derived from net/rtsock.c:rts_close().
441 */
442static void
443key_close(struct socket *so)
444{
445
446	raw_usrreqs.pru_close(so);
447}
448
449/*
450 * key_connect()
451 * derived from net/rtsock.c:rts_connect()
452 */
453static int
454key_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
455{
456	return EINVAL;
457}
458
459/*
460 * key_detach()
461 * derived from net/rtsock.c:rts_detach()
462 */
463static void
464key_detach(struct socket *so)
465{
466	INIT_VNET_IPSEC(curvnet);
467	struct keycb *kp = (struct keycb *)sotorawcb(so);
468
469	KASSERT(kp != NULL, ("key_detach: kp == NULL"));
470	if (kp->kp_raw.rcb_proto.sp_protocol
471	    == PF_KEY) /* XXX: AF_KEY */
472		V_key_cb.key_count--;
473	V_key_cb.any_count--;
474
475	key_freereg(so);
476	raw_usrreqs.pru_detach(so);
477}
478
479/*
480 * key_disconnect()
481 * derived from net/rtsock.c:key_disconnect()
482 */
483static int
484key_disconnect(struct socket *so)
485{
486	return(raw_usrreqs.pru_disconnect(so));
487}
488
489/*
490 * key_peeraddr()
491 * derived from net/rtsock.c:rts_peeraddr()
492 */
493static int
494key_peeraddr(struct socket *so, struct sockaddr **nam)
495{
496	return(raw_usrreqs.pru_peeraddr(so, nam));
497}
498
499/*
500 * key_send()
501 * derived from net/rtsock.c:rts_send()
502 */
503static int
504key_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
505	 struct mbuf *control, struct thread *td)
506{
507	return(raw_usrreqs.pru_send(so, flags, m, nam, control, td));
508}
509
510/*
511 * key_shutdown()
512 * derived from net/rtsock.c:rts_shutdown()
513 */
514static int
515key_shutdown(struct socket *so)
516{
517	return(raw_usrreqs.pru_shutdown(so));
518}
519
520/*
521 * key_sockaddr()
522 * derived from net/rtsock.c:rts_sockaddr()
523 */
524static int
525key_sockaddr(struct socket *so, struct sockaddr **nam)
526{
527	return(raw_usrreqs.pru_sockaddr(so, nam));
528}
529
530struct pr_usrreqs key_usrreqs = {
531	.pru_abort =		key_abort,
532	.pru_attach =		key_attach,
533	.pru_bind =		key_bind,
534	.pru_connect =		key_connect,
535	.pru_detach =		key_detach,
536	.pru_disconnect =	key_disconnect,
537	.pru_peeraddr =		key_peeraddr,
538	.pru_send =		key_send,
539	.pru_shutdown =		key_shutdown,
540	.pru_sockaddr =		key_sockaddr,
541	.pru_close =		key_close,
542};
543
544/* sysctl */
545SYSCTL_NODE(_net, PF_KEY, key, CTLFLAG_RW, 0, "Key Family");
546
547/*
548 * Definitions of protocols supported in the KEY domain.
549 */
550
551extern struct domain keydomain;
552
553struct protosw keysw[] = {
554{
555	.pr_type =		SOCK_RAW,
556	.pr_domain =		&keydomain,
557	.pr_protocol =		PF_KEY_V2,
558	.pr_flags =		PR_ATOMIC|PR_ADDR,
559	.pr_output =		key_output,
560	.pr_ctlinput =		raw_ctlinput,
561	.pr_init =		raw_init,
562	.pr_usrreqs =		&key_usrreqs
563}
564};
565
566static void
567key_init0(void)
568{
569	INIT_VNET_IPSEC(curvnet);
570
571	bzero((caddr_t)&V_key_cb, sizeof(V_key_cb));
572	ipsec_init();
573	key_init();
574}
575
576struct domain keydomain = {
577	.dom_family =		PF_KEY,
578	.dom_name =		"key",
579	.dom_init =		key_init0,
580	.dom_protosw =		keysw,
581	.dom_protoswNPROTOSW =	&keysw[sizeof(keysw)/sizeof(keysw[0])]
582};
583
584DOMAIN_SET(key);
585