send.c revision 227293
1249269Sed/*-
2249269Sed * Copyright (c) 2009-2010 Ana Kukec <anchie@FreeBSD.org>
3249269Sed * All rights reserved.
4249269Sed *
5249269Sed * Redistribution and use in source and binary forms, with or without
6249269Sed * modification, are permitted provided that the following conditions
7249269Sed * are met:
8249269Sed * 1. Redistributions of source code must retain the above copyright
9249269Sed *    notice, this list of conditions and the following disclaimer.
10249269Sed * 2. Redistributions in binary form must reproduce the above copyright
11249269Sed *    notice, this list of conditions and the following disclaimer in the
12249269Sed *    documentation and/or other materials provided with the distribution.
13249269Sed *
14249269Sed * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15249269Sed * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16249269Sed * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17249269Sed * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18249269Sed * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19249269Sed * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20249269Sed * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21249269Sed * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22249269Sed * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23249269Sed * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24249269Sed * SUCH DAMAGE.
25249269Sed */
26249269Sed
27249269Sed#include <sys/cdefs.h>
28249269Sed__FBSDID("$FreeBSD: head/sys/netinet6/send.c 227293 2011-11-07 06:44:47Z ed $");
29249269Sed
30249269Sed#include <sys/param.h>
31249269Sed#include <sys/kernel.h>
32249269Sed#include <sys/mbuf.h>
33249269Sed#include <sys/module.h>
34249269Sed#include <sys/priv.h>
35249269Sed#include <sys/protosw.h>
36249269Sed#include <sys/systm.h>
37249269Sed#include <sys/socket.h>
38249269Sed#include <sys/sockstate.h>
39249269Sed#include <sys/sockbuf.h>
40249269Sed#include <sys/socketvar.h>
41249269Sed#include <sys/types.h>
42249269Sed
43249269Sed#include <net/route.h>
44249269Sed#include <net/if.h>
45249269Sed#include <net/if_var.h>
46249269Sed#include <net/vnet.h>
47249269Sed
48249269Sed#include <netinet/in.h>
49249269Sed#include <netinet/ip_var.h>
50249269Sed#include <netinet/ip6.h>
51249269Sed#include <netinet/icmp6.h>
52249269Sed
53249269Sed#include <netinet6/in6_var.h>
54249269Sed#include <netinet6/nd6.h>
55249269Sed#include <netinet6/scope6_var.h>
56249269Sed#include <netinet6/send.h>
57249269Sed
58249269Sedstatic MALLOC_DEFINE(M_SEND, "send", "Secure Neighbour Discovery");
59249269Sed
60249269Sed/*
61249269Sed * The socket used to communicate with the SeND daemon.
62249269Sed */
63249269Sedstatic VNET_DEFINE(struct socket *, send_so);
64249269Sed#define	V_send_so	VNET(send_so)
65249269Sed
66249269Sedu_long	send_sendspace	= 8 * (1024 + sizeof(struct sockaddr_send));
67249269Sedu_long	send_recvspace	= 9216;
68249269Sed
69249269Sedstruct mtx	send_mtx;
70249269Sed#define SEND_LOCK_INIT()	mtx_init(&send_mtx, "send_mtx", NULL, MTX_DEF)
71249269Sed#define SEND_LOCK()		mtx_lock(&send_mtx)
72249269Sed#define SEND_UNLOCK()		mtx_unlock(&send_mtx)
73249269Sed#define SEND_LOCK_DESTROY()     mtx_destroy(&send_mtx)
74249269Sed
75249269Sedstatic int
76249269Sedsend_attach(struct socket *so, int proto, struct thread *td)
77249269Sed{
78249269Sed	int error;
79249269Sed
80249269Sed	SEND_LOCK();
81249269Sed	if (V_send_so != NULL) {
82249269Sed		SEND_UNLOCK();
83249269Sed		return (EEXIST);
84249269Sed	}
85249269Sed
86249269Sed	error = priv_check(td, PRIV_NETINET_RAW);
87249269Sed	if (error) {
88249269Sed		SEND_UNLOCK();
89249269Sed		return(error);
90249269Sed	}
91249269Sed
92249269Sed	if (proto != IPPROTO_SEND) {
93249269Sed		SEND_UNLOCK();
94249269Sed		return (EPROTONOSUPPORT);
95249269Sed	}
96249269Sed	error = soreserve(so, send_sendspace, send_recvspace);
97249269Sed	if (error) {
98249269Sed		SEND_UNLOCK();
99249269Sed		return(error);
100249269Sed	}
101249269Sed
102249269Sed	V_send_so = so;
103249269Sed	SEND_UNLOCK();
104249269Sed
105249269Sed	return (0);
106249269Sed}
107249269Sed
108249269Sedstatic int
109249269Sedsend_output(struct mbuf *m, struct ifnet *ifp, int direction)
110249269Sed{
111249269Sed	struct ip6_hdr *ip6;
112249269Sed	struct sockaddr_in6 dst;
113249269Sed	struct icmp6_hdr *icmp6;
114249269Sed	int icmp6len;
115249269Sed
116249269Sed	/*
117249269Sed	 * Receive incoming (SeND-protected) or outgoing traffic
118249269Sed	 * (SeND-validated) from the SeND user space application.
119249269Sed	 */
120249269Sed
121249269Sed	switch (direction) {
122249269Sed	case SND_IN:
123249269Sed		if (m->m_len < (sizeof(struct ip6_hdr) +
124249269Sed		    sizeof(struct icmp6_hdr))) {
125249269Sed			m = m_pullup(m, sizeof(struct ip6_hdr) +
126249269Sed			    sizeof(struct icmp6_hdr));
127249269Sed			if (!m)
128249269Sed				return (ENOBUFS);
129249269Sed		}
130249269Sed
131249269Sed		/* Before passing off the mbuf record the proper interface. */
132249269Sed		m->m_pkthdr.rcvif = ifp;
133249269Sed
134249269Sed		if (m->m_flags & M_PKTHDR)
135249269Sed			icmp6len = m->m_pkthdr.len - sizeof(struct ip6_hdr);
136249269Sed		else
137249269Sed			panic("Doh! not the first mbuf.");
138249269Sed
139249269Sed		ip6 = mtod(m, struct ip6_hdr *);
140249269Sed		icmp6 = (struct icmp6_hdr *)(ip6 + 1);
141249269Sed
142249269Sed		/*
143249269Sed		 * Output the packet as icmp6.c:icpm6_input() would do.
144249269Sed		 * The mbuf is always consumed, so we do not have to
145249269Sed		 * care about that.
146249269Sed		 */
147249269Sed		switch (icmp6->icmp6_type) {
148249269Sed		case ND_NEIGHBOR_SOLICIT:
149249269Sed			nd6_ns_input(m, sizeof(struct ip6_hdr), icmp6len);
150249269Sed			break;
151249269Sed		case ND_NEIGHBOR_ADVERT:
152249269Sed			nd6_na_input(m, sizeof(struct ip6_hdr), icmp6len);
153			break;
154		case ND_REDIRECT:
155			icmp6_redirect_input(m, sizeof(struct ip6_hdr));
156			break;
157		case ND_ROUTER_SOLICIT:
158			nd6_rs_input(m, sizeof(struct ip6_hdr), icmp6len);
159			break;
160		case ND_ROUTER_ADVERT:
161			nd6_ra_input(m, sizeof(struct ip6_hdr), icmp6len);
162			break;
163		default:
164			return (ENOSYS);
165		}
166		return (0);
167
168	case SND_OUT:
169		if (m->m_len < sizeof(struct ip6_hdr)) {
170			m = m_pullup(m, sizeof(struct ip6_hdr));
171			if (!m)
172				return (ENOBUFS);
173		}
174		ip6 = mtod(m, struct ip6_hdr *);
175		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst))
176			m->m_flags |= M_MCAST;
177
178		bzero(&dst, sizeof(dst));
179		dst.sin6_family = AF_INET6;
180		dst.sin6_len = sizeof(dst);
181		dst.sin6_addr = ip6->ip6_dst;
182
183		/*
184		 * Output the packet as nd6.c:nd6_output_lle() would do.
185		 * The mbuf is always consumed, so we do not have to care
186		 * about that.
187		 * XXX-BZ as we added data, what about fragmenting,
188		 * if now needed?
189		 */
190		int error;
191		error = ((*ifp->if_output)(ifp, m, (struct sockaddr *)&dst,
192		    NULL));
193		if (error)
194			error = ENOENT;
195		return (error);
196
197	default:
198		panic("%s: direction %d neither SND_IN nor SND_OUT.",
199		     __func__, direction);
200	}
201}
202
203/*
204 * Receive a SeND message from user space to be either send out by the kernel
205 * or, with SeND ICMPv6 options removed, to be further processed by the icmp6
206 * input path.
207 */
208static int
209send_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
210    struct mbuf *control, struct thread *td)
211{
212	struct sockaddr_send *sendsrc;
213	struct ifnet *ifp;
214	int error;
215
216	KASSERT(V_send_so == so, ("%s: socket %p not send socket %p",
217		__func__, so, V_send_so));
218
219	sendsrc = (struct sockaddr_send *)nam;
220	ifp = ifnet_byindex_ref(sendsrc->send_ifidx);
221	if (ifp == NULL) {
222		error = ENETUNREACH;
223		goto err;
224	}
225
226	error = send_output(m, ifp, sendsrc->send_direction);
227	if_rele(ifp);
228	m = NULL;
229
230err:
231	if (m != NULL)
232		m_freem(m);
233	return (error);
234}
235
236static void
237send_close(struct socket *so)
238{
239
240	SEND_LOCK();
241	if (V_send_so)
242		V_send_so = NULL;
243	SEND_UNLOCK();
244}
245
246/*
247 * Send a SeND message to user space, that was either received and has to be
248 * validated or was about to be send out and has to be handled by the SEND
249 * daemon adding SeND ICMPv6 options.
250 */
251static int
252send_input(struct mbuf *m, struct ifnet *ifp, int direction, int msglen __unused)
253{
254	struct ip6_hdr *ip6;
255	struct sockaddr_send sendsrc;
256
257	SEND_LOCK();
258	if (V_send_so == NULL) {
259		SEND_UNLOCK();
260		return (-1);
261	}
262
263	/*
264	 * Make sure to clear any possible internally embedded scope before
265	 * passing the packet to user space for SeND cryptographic signature
266	 * validation to succeed.
267	 */
268	ip6 = mtod(m, struct ip6_hdr *);
269	in6_clearscope(&ip6->ip6_src);
270	in6_clearscope(&ip6->ip6_dst);
271
272	bzero(&sendsrc, sizeof(sendsrc));
273	sendsrc.send_len = sizeof(sendsrc);
274	sendsrc.send_family = AF_INET6;
275	sendsrc.send_direction = direction;
276	sendsrc.send_ifidx = ifp->if_index;
277
278	/*
279	 * Send incoming or outgoing traffic to user space either to be
280	 * protected (outgoing) or validated (incoming) according to rfc3971.
281	 */
282	SOCKBUF_LOCK(&V_send_so->so_rcv);
283	if (sbappendaddr_locked(&V_send_so->so_rcv,
284	    (struct sockaddr *)&sendsrc, m, NULL) == 0) {
285		SOCKBUF_UNLOCK(&V_send_so->so_rcv);
286		/* XXX stats. */
287		m_freem(m);
288	} else {
289		sorwakeup_locked(V_send_so);
290	}
291
292	SEND_UNLOCK();
293	return (0);
294}
295
296struct pr_usrreqs send_usrreqs = {
297	.pru_attach =		send_attach,
298	.pru_send =		send_send,
299	.pru_detach =		send_close
300};
301struct protosw send_protosw = {
302	.pr_type =		SOCK_RAW,
303	.pr_flags =		PR_ATOMIC|PR_ADDR,
304	.pr_protocol =		IPPROTO_SEND,
305	.pr_usrreqs =		&send_usrreqs
306};
307
308static int
309send_modevent(module_t mod, int type, void *unused)
310{
311#ifdef __notyet__
312	VNET_ITERATOR_DECL(vnet_iter);
313#endif
314	int error;
315
316	switch (type) {
317	case MOD_LOAD:
318		SEND_LOCK_INIT();
319
320		error = pf_proto_register(PF_INET6, &send_protosw);
321		if (error != 0) {
322			printf("%s:%d: MOD_LOAD pf_proto_register(): %d\n",
323			   __func__, __LINE__, error);
324			SEND_LOCK_DESTROY();
325			break;
326		}
327		send_sendso_input_hook = send_input;
328		break;
329	case MOD_UNLOAD:
330		/* Do not allow unloading w/o locking. */
331		return (EBUSY);
332#ifdef __notyet__
333		VNET_LIST_RLOCK_NOSLEEP();
334		SEND_LOCK();
335		VNET_FOREACH(vnet_iter) {
336			CURVNET_SET(vnet_iter);
337			if (V_send_so != NULL) {
338				CURVNET_RESTORE();
339				SEND_UNLOCK();
340				VNET_LIST_RUNLOCK_NOSLEEP();
341				return (EBUSY);
342			}
343			CURVNET_RESTORE();
344		}
345		SEND_UNLOCK();
346		VNET_LIST_RUNLOCK_NOSLEEP();
347		error = pf_proto_unregister(PF_INET6, IPPROTO_SEND, SOCK_RAW);
348		if (error == 0)
349			SEND_LOCK_DESTROY();
350		send_sendso_input_hook = NULL;
351		break;
352#endif
353	default:
354		error = 0;
355		break;
356	}
357
358	return (error);
359}
360
361static moduledata_t sendmod = {
362	"send",
363	send_modevent,
364	0
365};
366
367DECLARE_MODULE(send, sendmod, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);
368