keysock.c revision 160566
1/*	$FreeBSD: head/sys/netipsec/keysock.c 160566 2006-07-22 09:18:02Z gnn $	*/
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/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
53#include <net/raw_cb.h>
54#include <net/route.h>
55
56#include <net/pfkeyv2.h>
57#include <netipsec/key.h>
58#include <netipsec/keysock.h>
59#include <netipsec/key_debug.h>
60
61#include <machine/stdarg.h>
62
63struct key_cb {
64	int key_count;
65	int any_count;
66};
67static struct key_cb key_cb;
68
69static struct sockaddr key_dst = { 2, PF_KEY, };
70static struct sockaddr key_src = { 2, PF_KEY, };
71
72static int key_sendup0 __P((struct rawcb *, struct mbuf *, int));
73
74struct pfkeystat pfkeystat;
75
76/*
77 * key_output()
78 */
79int
80key_output(struct mbuf *m, struct socket *so)
81{
82	struct sadb_msg *msg;
83	int len, error = 0;
84	int s;
85
86	if (m == 0)
87		panic("%s: NULL pointer was passed.\n", __func__);
88
89	pfkeystat.out_total++;
90	pfkeystat.out_bytes += m->m_pkthdr.len;
91
92	len = m->m_pkthdr.len;
93	if (len < sizeof(struct sadb_msg)) {
94		pfkeystat.out_tooshort++;
95		error = EINVAL;
96		goto end;
97	}
98
99	if (m->m_len < sizeof(struct sadb_msg)) {
100		if ((m = m_pullup(m, sizeof(struct sadb_msg))) == 0) {
101			pfkeystat.out_nomem++;
102			error = ENOBUFS;
103			goto end;
104		}
105	}
106
107	M_ASSERTPKTHDR(m);
108
109	KEYDEBUG(KEYDEBUG_KEY_DUMP, kdebug_mbuf(m));
110
111	msg = mtod(m, struct sadb_msg *);
112	pfkeystat.out_msgtype[msg->sadb_msg_type]++;
113	if (len != PFKEY_UNUNIT64(msg->sadb_msg_len)) {
114		pfkeystat.out_invlen++;
115		error = EINVAL;
116		goto end;
117	}
118
119	/*XXX giant lock*/
120	s = splnet();
121	error = key_parse(m, so);
122	m = NULL;
123	splx(s);
124end:
125	if (m)
126		m_freem(m);
127	return error;
128}
129
130/*
131 * send message to the socket.
132 */
133static int
134key_sendup0(rp, m, promisc)
135	struct rawcb *rp;
136	struct mbuf *m;
137	int promisc;
138{
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			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		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		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	struct mbuf *m, *n, *mprev;
184	int tlen;
185
186	/* sanity check */
187	if (so == 0 || msg == 0)
188		panic("%s: NULL pointer was passed.\n", __func__);
189
190	KEYDEBUG(KEYDEBUG_KEY_DUMP,
191		printf("%s: \n", __func__);
192		kdebug_sadb(msg));
193
194	/*
195	 * we increment statistics here, just in case we have ENOBUFS
196	 * in this function.
197	 */
198	pfkeystat.in_total++;
199	pfkeystat.in_bytes += len;
200	pfkeystat.in_msgtype[msg->sadb_msg_type]++;
201
202	/*
203	 * Get mbuf chain whenever possible (not clusters),
204	 * to save socket buffer.  We'll be generating many SADB_ACQUIRE
205	 * messages to listening key sockets.  If we simply allocate clusters,
206	 * sbappendaddr() will raise ENOBUFS due to too little sbspace().
207	 * sbspace() computes # of actual data bytes AND mbuf region.
208	 *
209	 * TODO: SADB_ACQUIRE filters should be implemented.
210	 */
211	tlen = len;
212	m = mprev = NULL;
213	while (tlen > 0) {
214		if (tlen == len) {
215			MGETHDR(n, M_DONTWAIT, MT_DATA);
216			n->m_len = MHLEN;
217		} else {
218			MGET(n, M_DONTWAIT, MT_DATA);
219			n->m_len = MLEN;
220		}
221		if (!n) {
222			pfkeystat.in_nomem++;
223			return ENOBUFS;
224		}
225		if (tlen >= MCLBYTES) {	/*XXX better threshold? */
226			MCLGET(n, M_DONTWAIT);
227			if ((n->m_flags & M_EXT) == 0) {
228				m_free(n);
229				m_freem(m);
230				pfkeystat.in_nomem++;
231				return ENOBUFS;
232			}
233			n->m_len = MCLBYTES;
234		}
235
236		if (tlen < n->m_len)
237			n->m_len = tlen;
238		n->m_next = NULL;
239		if (m == NULL)
240			m = mprev = n;
241		else {
242			mprev->m_next = n;
243			mprev = n;
244		}
245		tlen -= n->m_len;
246		n = NULL;
247	}
248	m->m_pkthdr.len = len;
249	m->m_pkthdr.rcvif = NULL;
250	m_copyback(m, 0, len, (caddr_t)msg);
251
252	/* avoid duplicated statistics */
253	pfkeystat.in_total--;
254	pfkeystat.in_bytes -= len;
255	pfkeystat.in_msgtype[msg->sadb_msg_type]--;
256
257	return key_sendup_mbuf(so, m, target);
258}
259
260/* so can be NULL if target != KEY_SENDUP_ONE */
261int
262key_sendup_mbuf(so, m, target)
263	struct socket *so;
264	struct mbuf *m;
265	int target;
266{
267	struct mbuf *n;
268	struct keycb *kp;
269	int sendup;
270	struct rawcb *rp;
271	int error = 0;
272
273	if (m == NULL)
274		panic("key_sendup_mbuf: NULL pointer was passed.\n");
275	if (so == NULL && target == KEY_SENDUP_ONE)
276		panic("%s: NULL pointer was passed.\n", __func__);
277
278	pfkeystat.in_total++;
279	pfkeystat.in_bytes += m->m_pkthdr.len;
280	if (m->m_len < sizeof(struct sadb_msg)) {
281#if 1
282		m = m_pullup(m, sizeof(struct sadb_msg));
283		if (m == NULL) {
284			pfkeystat.in_nomem++;
285			return ENOBUFS;
286		}
287#else
288		/* don't bother pulling it up just for stats */
289#endif
290	}
291	if (m->m_len >= sizeof(struct sadb_msg)) {
292		struct sadb_msg *msg;
293		msg = mtod(m, struct sadb_msg *);
294		pfkeystat.in_msgtype[msg->sadb_msg_type]++;
295	}
296
297	LIST_FOREACH(rp, &rawcb_list, list)
298	{
299		if (rp->rcb_proto.sp_family != PF_KEY)
300			continue;
301		if (rp->rcb_proto.sp_protocol
302		 && rp->rcb_proto.sp_protocol != PF_KEY_V2) {
303			continue;
304		}
305
306		kp = (struct keycb *)rp;
307
308		/*
309		 * If you are in promiscuous mode, and when you get broadcasted
310		 * reply, you'll get two PF_KEY messages.
311		 * (based on pf_key@inner.net message on 14 Oct 1998)
312		 */
313		if (((struct keycb *)rp)->kp_promisc) {
314			if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
315				(void)key_sendup0(rp, n, 1);
316				n = NULL;
317			}
318		}
319
320		/* the exact target will be processed later */
321		if (so && sotorawcb(so) == rp)
322			continue;
323
324		sendup = 0;
325		switch (target) {
326		case KEY_SENDUP_ONE:
327			/* the statement has no effect */
328			if (so && sotorawcb(so) == rp)
329				sendup++;
330			break;
331		case KEY_SENDUP_ALL:
332			sendup++;
333			break;
334		case KEY_SENDUP_REGISTERED:
335			if (kp->kp_registered)
336				sendup++;
337			break;
338		}
339		pfkeystat.in_msgtarget[target]++;
340
341		if (!sendup)
342			continue;
343
344		if ((n = m_copy(m, 0, (int)M_COPYALL)) == NULL) {
345			m_freem(m);
346			pfkeystat.in_nomem++;
347			return ENOBUFS;
348		}
349
350		if ((error = key_sendup0(rp, n, 0)) != 0) {
351			m_freem(m);
352			return error;
353		}
354
355		n = NULL;
356	}
357
358	if (so) {
359		error = key_sendup0(sotorawcb(so), m, 0);
360		m = NULL;
361	} else {
362		error = 0;
363		m_freem(m);
364	}
365	return error;
366}
367
368/*
369 * key_abort()
370 * derived from net/rtsock.c:rts_abort()
371 */
372static void
373key_abort(struct socket *so)
374{
375
376	raw_usrreqs.pru_abort(so);
377}
378
379/*
380 * key_attach()
381 * derived from net/rtsock.c:rts_attach()
382 */
383static int
384key_attach(struct socket *so, int proto, struct thread *td)
385{
386	struct keycb *kp;
387	int s, error;
388
389	if (sotorawcb(so) != 0)
390		return EISCONN;	/* XXX panic? */
391	kp = (struct keycb *)malloc(sizeof *kp, M_PCB, M_WAITOK|M_ZERO); /* XXX */
392	if (kp == 0)
393		return ENOBUFS;
394
395	/*
396	 * The splnet() is necessary to block protocols from sending
397	 * error notifications (like RTM_REDIRECT or RTM_LOSING) while
398	 * this PCB is extant but incompletely initialized.
399	 * Probably we should try to do more of this work beforehand and
400	 * eliminate the spl.
401	 */
402	s = splnet();
403	so->so_pcb = (caddr_t)kp;
404	error = raw_usrreqs.pru_attach(so, proto, td);
405	kp = (struct keycb *)sotorawcb(so);
406	if (error) {
407		free(kp, M_PCB);
408		so->so_pcb = (caddr_t) 0;
409		splx(s);
410		return error;
411	}
412
413	kp->kp_promisc = kp->kp_registered = 0;
414
415	if (kp->kp_raw.rcb_proto.sp_protocol == PF_KEY) /* XXX: AF_KEY */
416		key_cb.key_count++;
417	key_cb.any_count++;
418	kp->kp_raw.rcb_laddr = &key_src;
419	kp->kp_raw.rcb_faddr = &key_dst;
420	soisconnected(so);
421	so->so_options |= SO_USELOOPBACK;
422
423	splx(s);
424	return 0;
425}
426
427/*
428 * key_bind()
429 * derived from net/rtsock.c:rts_bind()
430 */
431static int
432key_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
433{
434	int s, error;
435	s = splnet();
436	error = raw_usrreqs.pru_bind(so, nam, td); /* xxx just EINVAL */
437	splx(s);
438	return error;
439}
440
441/*
442 * key_close()
443 * derived from net/rtsock.c:rts_close().
444 */
445static void
446key_close(struct socket *so)
447{
448
449	raw_usrreqs.pru_close(so);
450}
451
452/*
453 * key_connect()
454 * derived from net/rtsock.c:rts_connect()
455 */
456static int
457key_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
458{
459	int s, error;
460	s = splnet();
461	error = raw_usrreqs.pru_connect(so, nam, td); /* XXX just EINVAL */
462	splx(s);
463	return error;
464}
465
466/*
467 * key_detach()
468 * derived from net/rtsock.c:rts_detach()
469 */
470static void
471key_detach(struct socket *so)
472{
473	struct keycb *kp = (struct keycb *)sotorawcb(so);
474
475	KASSERT(kp != NULL, ("key_detach: kp == NULL"));
476	if (kp->kp_raw.rcb_proto.sp_protocol
477	    == PF_KEY) /* XXX: AF_KEY */
478		key_cb.key_count--;
479	key_cb.any_count--;
480
481	key_freereg(so);
482	raw_usrreqs.pru_detach(so);
483}
484
485/*
486 * key_disconnect()
487 * derived from net/rtsock.c:key_disconnect()
488 */
489static int
490key_disconnect(struct socket *so)
491{
492	int s, error;
493	s = splnet();
494	error = raw_usrreqs.pru_disconnect(so);
495	splx(s);
496	return error;
497}
498
499/*
500 * key_peeraddr()
501 * derived from net/rtsock.c:rts_peeraddr()
502 */
503static int
504key_peeraddr(struct socket *so, struct sockaddr **nam)
505{
506	int s, error;
507	s = splnet();
508	error = raw_usrreqs.pru_peeraddr(so, nam);
509	splx(s);
510	return error;
511}
512
513/*
514 * key_send()
515 * derived from net/rtsock.c:rts_send()
516 */
517static int
518key_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
519	 struct mbuf *control, struct thread *td)
520{
521	int s, error;
522	s = splnet();
523	error = raw_usrreqs.pru_send(so, flags, m, nam, control, td);
524	splx(s);
525	return error;
526}
527
528/*
529 * key_shutdown()
530 * derived from net/rtsock.c:rts_shutdown()
531 */
532static int
533key_shutdown(struct socket *so)
534{
535	int s, error;
536	s = splnet();
537	error = raw_usrreqs.pru_shutdown(so);
538	splx(s);
539	return error;
540}
541
542/*
543 * key_sockaddr()
544 * derived from net/rtsock.c:rts_sockaddr()
545 */
546static int
547key_sockaddr(struct socket *so, struct sockaddr **nam)
548{
549	int s, error;
550	s = splnet();
551	error = raw_usrreqs.pru_sockaddr(so, nam);
552	splx(s);
553	return error;
554}
555
556struct pr_usrreqs key_usrreqs = {
557	.pru_abort =		key_abort,
558	.pru_attach =		key_attach,
559	.pru_bind =		key_bind,
560	.pru_connect =		key_connect,
561	.pru_detach =		key_detach,
562	.pru_disconnect =	key_disconnect,
563	.pru_peeraddr =		key_peeraddr,
564	.pru_send =		key_send,
565	.pru_shutdown =		key_shutdown,
566	.pru_sockaddr =		key_sockaddr,
567	.pru_close =		key_close,
568};
569
570/* sysctl */
571SYSCTL_NODE(_net, PF_KEY, key, CTLFLAG_RW, 0, "Key Family");
572
573/*
574 * Definitions of protocols supported in the KEY domain.
575 */
576
577extern struct domain keydomain;
578
579struct protosw keysw[] = {
580{
581	.pr_type =		SOCK_RAW,
582	.pr_domain =		&keydomain,
583	.pr_protocol =		PF_KEY_V2,
584	.pr_flags =		PR_ATOMIC|PR_ADDR,
585	.pr_output =		key_output,
586	.pr_ctlinput =		raw_ctlinput,
587	.pr_init =		raw_init,
588	.pr_usrreqs =		&key_usrreqs
589}
590};
591
592static void
593key_init0(void)
594{
595	bzero((caddr_t)&key_cb, sizeof(key_cb));
596	key_init();
597}
598
599struct domain keydomain = {
600	.dom_family =		PF_KEY,
601	.dom_name =		"key",
602	.dom_init =		key_init0,
603	.dom_protosw =		keysw,
604	.dom_protoswNPROTOSW =	&keysw[sizeof(keysw)/sizeof(keysw[0])]
605};
606
607DOMAIN_SET(key);
608