xref: /freebsd/sys/netinet6/send.c (revision 42249ef2)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2009-2010 Ana Kukec <anchie@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/malloc.h>
35 #include <sys/mbuf.h>
36 #include <sys/module.h>
37 #include <sys/priv.h>
38 #include <sys/protosw.h>
39 #include <sys/sdt.h>
40 #include <sys/systm.h>
41 #include <sys/socket.h>
42 #include <sys/sockbuf.h>
43 #include <sys/socketvar.h>
44 #include <sys/types.h>
45 
46 #include <net/route.h>
47 #include <net/if.h>
48 #include <net/if_var.h>
49 #include <net/vnet.h>
50 
51 #include <netinet/in.h>
52 #include <netinet/in_kdtrace.h>
53 #include <netinet/ip_var.h>
54 #include <netinet/ip6.h>
55 #include <netinet/icmp6.h>
56 
57 #include <netinet6/in6_var.h>
58 #include <netinet6/nd6.h>
59 #include <netinet6/scope6_var.h>
60 #include <netinet6/send.h>
61 
62 static MALLOC_DEFINE(M_SEND, "send", "Secure Neighbour Discovery");
63 
64 /*
65  * The socket used to communicate with the SeND daemon.
66  */
67 VNET_DEFINE_STATIC(struct socket *, send_so);
68 #define	V_send_so	VNET(send_so)
69 
70 u_long	send_sendspace	= 8 * (1024 + sizeof(struct sockaddr_send));
71 u_long	send_recvspace	= 9216;
72 
73 struct mtx	send_mtx;
74 #define SEND_LOCK_INIT()	mtx_init(&send_mtx, "send_mtx", NULL, MTX_DEF)
75 #define SEND_LOCK()		mtx_lock(&send_mtx)
76 #define SEND_UNLOCK()		mtx_unlock(&send_mtx)
77 #define SEND_LOCK_DESTROY()     mtx_destroy(&send_mtx)
78 
79 static int
80 send_attach(struct socket *so, int proto, struct thread *td)
81 {
82 	int error;
83 
84 	SEND_LOCK();
85 	if (V_send_so != NULL) {
86 		SEND_UNLOCK();
87 		return (EEXIST);
88 	}
89 
90 	error = priv_check(td, PRIV_NETINET_RAW);
91 	if (error) {
92 		SEND_UNLOCK();
93 		return(error);
94 	}
95 
96 	if (proto != IPPROTO_SEND) {
97 		SEND_UNLOCK();
98 		return (EPROTONOSUPPORT);
99 	}
100 	error = soreserve(so, send_sendspace, send_recvspace);
101 	if (error) {
102 		SEND_UNLOCK();
103 		return(error);
104 	}
105 
106 	V_send_so = so;
107 	SEND_UNLOCK();
108 
109 	return (0);
110 }
111 
112 static int
113 send_output(struct mbuf *m, struct ifnet *ifp, int direction)
114 {
115 	struct ip6_hdr *ip6;
116 	struct sockaddr_in6 dst;
117 	struct icmp6_hdr *icmp6;
118 	int icmp6len;
119 
120 	/*
121 	 * Receive incoming (SeND-protected) or outgoing traffic
122 	 * (SeND-validated) from the SeND user space application.
123 	 */
124 
125 	switch (direction) {
126 	case SND_IN:
127 		if (m->m_len < (sizeof(struct ip6_hdr) +
128 		    sizeof(struct icmp6_hdr))) {
129 			m = m_pullup(m, sizeof(struct ip6_hdr) +
130 			    sizeof(struct icmp6_hdr));
131 			if (!m)
132 				return (ENOBUFS);
133 		}
134 
135 		/* Before passing off the mbuf record the proper interface. */
136 		m->m_pkthdr.rcvif = ifp;
137 
138 		if (m->m_flags & M_PKTHDR)
139 			icmp6len = m->m_pkthdr.len - sizeof(struct ip6_hdr);
140 		else
141 			panic("Doh! not the first mbuf.");
142 
143 		ip6 = mtod(m, struct ip6_hdr *);
144 		icmp6 = (struct icmp6_hdr *)(ip6 + 1);
145 
146 		/*
147 		 * Output the packet as icmp6.c:icpm6_input() would do.
148 		 * The mbuf is always consumed, so we do not have to
149 		 * care about that.
150 		 */
151 		switch (icmp6->icmp6_type) {
152 		case ND_NEIGHBOR_SOLICIT:
153 			nd6_ns_input(m, sizeof(struct ip6_hdr), icmp6len);
154 			break;
155 		case ND_NEIGHBOR_ADVERT:
156 			nd6_na_input(m, sizeof(struct ip6_hdr), icmp6len);
157 			break;
158 		case ND_REDIRECT:
159 			icmp6_redirect_input(m, sizeof(struct ip6_hdr));
160 			break;
161 		case ND_ROUTER_SOLICIT:
162 			nd6_rs_input(m, sizeof(struct ip6_hdr), icmp6len);
163 			break;
164 		case ND_ROUTER_ADVERT:
165 			nd6_ra_input(m, sizeof(struct ip6_hdr), icmp6len);
166 			break;
167 		default:
168 			m_freem(m);
169 			return (ENOSYS);
170 		}
171 		return (0);
172 
173 	case SND_OUT:
174 		if (m->m_len < sizeof(struct ip6_hdr)) {
175 			m = m_pullup(m, sizeof(struct ip6_hdr));
176 			if (!m)
177 				return (ENOBUFS);
178 		}
179 		ip6 = mtod(m, struct ip6_hdr *);
180 		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst))
181 			m->m_flags |= M_MCAST;
182 
183 		bzero(&dst, sizeof(dst));
184 		dst.sin6_family = AF_INET6;
185 		dst.sin6_len = sizeof(dst);
186 		dst.sin6_addr = ip6->ip6_dst;
187 
188 		m_clrprotoflags(m);	/* Avoid confusing lower layers. */
189 
190 		IP_PROBE(send, NULL, NULL, ip6, ifp, NULL, ip6);
191 
192 		/*
193 		 * Output the packet as nd6.c:nd6_output_lle() would do.
194 		 * The mbuf is always consumed, so we do not have to care
195 		 * about that.
196 		 * XXX-BZ as we added data, what about fragmenting,
197 		 * if now needed?
198 		 */
199 		int error;
200 		error = ((*ifp->if_output)(ifp, m, (struct sockaddr *)&dst,
201 		    NULL));
202 		if (error)
203 			error = ENOENT;
204 		return (error);
205 
206 	default:
207 		panic("%s: direction %d neither SND_IN nor SND_OUT.",
208 		     __func__, direction);
209 	}
210 }
211 
212 /*
213  * Receive a SeND message from user space to be either send out by the kernel
214  * or, with SeND ICMPv6 options removed, to be further processed by the icmp6
215  * input path.
216  */
217 static int
218 send_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
219     struct mbuf *control, struct thread *td)
220 {
221 	struct sockaddr_send *sendsrc;
222 	struct ifnet *ifp;
223 	int error;
224 
225 	KASSERT(V_send_so == so, ("%s: socket %p not send socket %p",
226 		__func__, so, V_send_so));
227 
228 	sendsrc = (struct sockaddr_send *)nam;
229 	ifp = ifnet_byindex_ref(sendsrc->send_ifidx);
230 	if (ifp == NULL) {
231 		error = ENETUNREACH;
232 		goto err;
233 	}
234 
235 	error = send_output(m, ifp, sendsrc->send_direction);
236 	if_rele(ifp);
237 	m = NULL;
238 
239 err:
240 	if (m != NULL)
241 		m_freem(m);
242 	return (error);
243 }
244 
245 static void
246 send_close(struct socket *so)
247 {
248 
249 	SEND_LOCK();
250 	if (V_send_so)
251 		V_send_so = NULL;
252 	SEND_UNLOCK();
253 }
254 
255 /*
256  * Send a SeND message to user space, that was either received and has to be
257  * validated or was about to be send out and has to be handled by the SEND
258  * daemon adding SeND ICMPv6 options.
259  */
260 static int
261 send_input(struct mbuf *m, struct ifnet *ifp, int direction, int msglen __unused)
262 {
263 	struct ip6_hdr *ip6;
264 	struct sockaddr_send sendsrc;
265 
266 	SEND_LOCK();
267 	if (V_send_so == NULL) {
268 		SEND_UNLOCK();
269 		return (-1);
270 	}
271 
272 	/*
273 	 * Make sure to clear any possible internally embedded scope before
274 	 * passing the packet to user space for SeND cryptographic signature
275 	 * validation to succeed.
276 	 */
277 	ip6 = mtod(m, struct ip6_hdr *);
278 	in6_clearscope(&ip6->ip6_src);
279 	in6_clearscope(&ip6->ip6_dst);
280 
281 	bzero(&sendsrc, sizeof(sendsrc));
282 	sendsrc.send_len = sizeof(sendsrc);
283 	sendsrc.send_family = AF_INET6;
284 	sendsrc.send_direction = direction;
285 	sendsrc.send_ifidx = ifp->if_index;
286 
287 	/*
288 	 * Send incoming or outgoing traffic to user space either to be
289 	 * protected (outgoing) or validated (incoming) according to rfc3971.
290 	 */
291 	SOCKBUF_LOCK(&V_send_so->so_rcv);
292 	if (sbappendaddr_locked(&V_send_so->so_rcv,
293 	    (struct sockaddr *)&sendsrc, m, NULL) == 0) {
294 		SOCKBUF_UNLOCK(&V_send_so->so_rcv);
295 		/* XXX stats. */
296 		m_freem(m);
297 	} else {
298 		sorwakeup_locked(V_send_so);
299 	}
300 
301 	SEND_UNLOCK();
302 	return (0);
303 }
304 
305 struct pr_usrreqs send_usrreqs = {
306 	.pru_attach =		send_attach,
307 	.pru_send =		send_send,
308 	.pru_detach =		send_close
309 };
310 struct protosw send_protosw = {
311 	.pr_type =		SOCK_RAW,
312 	.pr_flags =		PR_ATOMIC|PR_ADDR,
313 	.pr_protocol =		IPPROTO_SEND,
314 	.pr_usrreqs =		&send_usrreqs
315 };
316 
317 static int
318 send_modevent(module_t mod, int type, void *unused)
319 {
320 #ifdef __notyet__
321 	VNET_ITERATOR_DECL(vnet_iter);
322 #endif
323 	int error;
324 
325 	switch (type) {
326 	case MOD_LOAD:
327 		SEND_LOCK_INIT();
328 
329 		error = pf_proto_register(PF_INET6, &send_protosw);
330 		if (error != 0) {
331 			printf("%s:%d: MOD_LOAD pf_proto_register(): %d\n",
332 			   __func__, __LINE__, error);
333 			SEND_LOCK_DESTROY();
334 			break;
335 		}
336 		send_sendso_input_hook = send_input;
337 		break;
338 	case MOD_UNLOAD:
339 		/* Do not allow unloading w/o locking. */
340 		return (EBUSY);
341 #ifdef __notyet__
342 		VNET_LIST_RLOCK_NOSLEEP();
343 		SEND_LOCK();
344 		VNET_FOREACH(vnet_iter) {
345 			CURVNET_SET(vnet_iter);
346 			if (V_send_so != NULL) {
347 				CURVNET_RESTORE();
348 				SEND_UNLOCK();
349 				VNET_LIST_RUNLOCK_NOSLEEP();
350 				return (EBUSY);
351 			}
352 			CURVNET_RESTORE();
353 		}
354 		SEND_UNLOCK();
355 		VNET_LIST_RUNLOCK_NOSLEEP();
356 		error = pf_proto_unregister(PF_INET6, IPPROTO_SEND, SOCK_RAW);
357 		if (error == 0)
358 			SEND_LOCK_DESTROY();
359 		send_sendso_input_hook = NULL;
360 		break;
361 #endif
362 	default:
363 		error = 0;
364 		break;
365 	}
366 
367 	return (error);
368 }
369 
370 static moduledata_t sendmod = {
371 	"send",
372 	send_modevent,
373 	0
374 };
375 
376 DECLARE_MODULE(send, sendmod, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);
377