1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2009-2010 Ana Kukec <[email protected]>
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 #include <sys/param.h>
31 #include <sys/kernel.h>
32 #include <sys/malloc.h>
33 #include <sys/mbuf.h>
34 #include <sys/module.h>
35 #include <sys/priv.h>
36 #include <sys/protosw.h>
37 #include <sys/sdt.h>
38 #include <sys/systm.h>
39 #include <sys/socket.h>
40 #include <sys/sockbuf.h>
41 #include <sys/socketvar.h>
42 #include <sys/types.h>
43
44 #include <net/route.h>
45 #include <net/if.h>
46 #include <net/if_var.h>
47 #include <net/if_private.h>
48 #include <net/vnet.h>
49
50 #include <netinet/in.h>
51 #include <netinet/in_kdtrace.h>
52 #include <netinet/ip_var.h>
53 #include <netinet/ip6.h>
54 #include <netinet/icmp6.h>
55
56 #include <netinet6/in6_var.h>
57 #include <netinet6/nd6.h>
58 #include <netinet6/scope6_var.h>
59 #include <netinet6/send.h>
60
61 static MALLOC_DEFINE(M_SEND, "send", "Secure Neighbour Discovery");
62
63 /*
64 * The socket used to communicate with the SeND daemon.
65 */
66 VNET_DEFINE_STATIC(struct socket *, send_so);
67 #define V_send_so VNET(send_so)
68
69 u_long send_sendspace = 8 * (1024 + sizeof(struct sockaddr_send));
70 u_long send_recvspace = 9216;
71
72 struct mtx send_mtx;
73 #define SEND_LOCK_INIT() mtx_init(&send_mtx, "send_mtx", NULL, MTX_DEF)
74 #define SEND_LOCK() mtx_lock(&send_mtx)
75 #define SEND_UNLOCK() mtx_unlock(&send_mtx)
76 #define SEND_LOCK_DESTROY() mtx_destroy(&send_mtx)
77
78 static int
send_attach(struct socket * so,int proto,struct thread * td)79 send_attach(struct socket *so, int proto, struct thread *td)
80 {
81 int error;
82
83 SEND_LOCK();
84 if (V_send_so != NULL) {
85 SEND_UNLOCK();
86 return (EEXIST);
87 }
88
89 error = priv_check(td, PRIV_NETINET_RAW);
90 if (error) {
91 SEND_UNLOCK();
92 return(error);
93 }
94
95 if (proto != IPPROTO_SEND) {
96 SEND_UNLOCK();
97 return (EPROTONOSUPPORT);
98 }
99 error = soreserve(so, send_sendspace, send_recvspace);
100 if (error) {
101 SEND_UNLOCK();
102 return(error);
103 }
104
105 V_send_so = so;
106 SEND_UNLOCK();
107
108 return (0);
109 }
110
111 static int
send_output(struct mbuf * m,struct ifnet * ifp,int direction)112 send_output(struct mbuf *m, struct ifnet *ifp, int direction)
113 {
114 struct ip6_hdr *ip6;
115 struct sockaddr_in6 dst;
116 struct icmp6_hdr *icmp6;
117 struct epoch_tracker et;
118 int icmp6len;
119 int error;
120
121 /*
122 * Receive incoming (SeND-protected) or outgoing traffic
123 * (SeND-validated) from the SeND user space application.
124 */
125
126 switch (direction) {
127 case SND_IN:
128 if (m->m_len < (sizeof(struct ip6_hdr) +
129 sizeof(struct icmp6_hdr))) {
130 m = m_pullup(m, sizeof(struct ip6_hdr) +
131 sizeof(struct icmp6_hdr));
132 if (!m)
133 return (ENOBUFS);
134 }
135
136 /* Before passing off the mbuf record the proper interface. */
137 m->m_pkthdr.rcvif = ifp;
138
139 if (m->m_flags & M_PKTHDR)
140 icmp6len = m->m_pkthdr.len - sizeof(struct ip6_hdr);
141 else
142 panic("Doh! not the first mbuf.");
143
144 ip6 = mtod(m, struct ip6_hdr *);
145 icmp6 = (struct icmp6_hdr *)(ip6 + 1);
146 error = 0;
147
148 /*
149 * Output the packet as icmp6.c:icpm6_input() would do.
150 * The mbuf is always consumed, so we do not have to
151 * care about that.
152 */
153 NET_EPOCH_ENTER(et);
154 switch (icmp6->icmp6_type) {
155 case ND_NEIGHBOR_SOLICIT:
156 nd6_ns_input(m, sizeof(struct ip6_hdr), icmp6len);
157 break;
158 case ND_NEIGHBOR_ADVERT:
159 nd6_na_input(m, sizeof(struct ip6_hdr), icmp6len);
160 break;
161 case ND_REDIRECT:
162 icmp6_redirect_input(m, sizeof(struct ip6_hdr));
163 break;
164 case ND_ROUTER_SOLICIT:
165 nd6_rs_input(m, sizeof(struct ip6_hdr), icmp6len);
166 break;
167 case ND_ROUTER_ADVERT:
168 nd6_ra_input(m, sizeof(struct ip6_hdr), icmp6len);
169 break;
170 default:
171 m_freem(m);
172 error = ENOSYS;
173 }
174 NET_EPOCH_EXIT(et);
175
176 return (error);
177
178 case SND_OUT:
179 if (m->m_len < sizeof(struct ip6_hdr)) {
180 m = m_pullup(m, sizeof(struct ip6_hdr));
181 if (!m)
182 return (ENOBUFS);
183 }
184 ip6 = mtod(m, struct ip6_hdr *);
185 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst))
186 m->m_flags |= M_MCAST;
187
188 bzero(&dst, sizeof(dst));
189 dst.sin6_family = AF_INET6;
190 dst.sin6_len = sizeof(dst);
191 dst.sin6_addr = ip6->ip6_dst;
192
193 m_clrprotoflags(m); /* Avoid confusing lower layers. */
194
195 IP_PROBE(send, NULL, NULL, ip6, ifp, NULL, ip6);
196
197 /*
198 * Output the packet as nd6.c:nd6_output_lle() would do.
199 * The mbuf is always consumed, so we do not have to care
200 * about that.
201 * XXX-BZ as we added data, what about fragmenting,
202 * if now needed?
203 */
204 error = ((*ifp->if_output)(ifp, m, (struct sockaddr *)&dst,
205 NULL));
206 if (error)
207 error = ENOENT;
208 return (error);
209
210 default:
211 panic("%s: direction %d neither SND_IN nor SND_OUT.",
212 __func__, direction);
213 }
214 }
215
216 /*
217 * Receive a SeND message from user space to be either send out by the kernel
218 * or, with SeND ICMPv6 options removed, to be further processed by the icmp6
219 * input path.
220 */
221 static int
send_send(struct socket * so,int flags,struct mbuf * m,struct sockaddr * nam,struct mbuf * control,struct thread * td)222 send_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
223 struct mbuf *control, struct thread *td)
224 {
225 struct sockaddr_send *sendsrc;
226 struct ifnet *ifp;
227 int error;
228
229 KASSERT(V_send_so == so, ("%s: socket %p not send socket %p",
230 __func__, so, V_send_so));
231
232 sendsrc = (struct sockaddr_send *)nam;
233 if (sendsrc->send_family != AF_INET6) {
234 error = EAFNOSUPPORT;
235 goto err;
236 }
237 if (sendsrc->send_len != sizeof(*sendsrc)) {
238 error = EINVAL;
239 goto err;
240 }
241 ifp = ifnet_byindex_ref(sendsrc->send_ifidx);
242 if (ifp == NULL) {
243 error = ENETUNREACH;
244 goto err;
245 }
246
247 error = send_output(m, ifp, sendsrc->send_direction);
248 if_rele(ifp);
249 m = NULL;
250
251 err:
252 if (control != NULL)
253 m_freem(control);
254 if (m != NULL)
255 m_freem(m);
256
257 return (error);
258 }
259
260 static void
send_close(struct socket * so)261 send_close(struct socket *so)
262 {
263
264 SEND_LOCK();
265 if (V_send_so)
266 V_send_so = NULL;
267 SEND_UNLOCK();
268 }
269
270 /*
271 * Send a SeND message to user space, that was either received and has to be
272 * validated or was about to be send out and has to be handled by the SEND
273 * daemon adding SeND ICMPv6 options.
274 */
275 static int
send_input(struct mbuf * m,struct ifnet * ifp,int direction,int msglen __unused)276 send_input(struct mbuf *m, struct ifnet *ifp, int direction, int msglen __unused)
277 {
278 struct ip6_hdr *ip6;
279 struct sockaddr_send sendsrc;
280
281 SEND_LOCK();
282 if (V_send_so == NULL) {
283 SEND_UNLOCK();
284 return (-1);
285 }
286
287 /*
288 * Make sure to clear any possible internally embedded scope before
289 * passing the packet to user space for SeND cryptographic signature
290 * validation to succeed.
291 */
292 ip6 = mtod(m, struct ip6_hdr *);
293 in6_clearscope(&ip6->ip6_src);
294 in6_clearscope(&ip6->ip6_dst);
295
296 bzero(&sendsrc, sizeof(sendsrc));
297 sendsrc.send_len = sizeof(sendsrc);
298 sendsrc.send_family = AF_INET6;
299 sendsrc.send_direction = direction;
300 sendsrc.send_ifidx = ifp->if_index;
301
302 /*
303 * Send incoming or outgoing traffic to user space either to be
304 * protected (outgoing) or validated (incoming) according to rfc3971.
305 */
306 SOCKBUF_LOCK(&V_send_so->so_rcv);
307 if (sbappendaddr_locked(&V_send_so->so_rcv,
308 (struct sockaddr *)&sendsrc, m, NULL) == 0) {
309 soroverflow_locked(V_send_so);
310 /* XXX stats. */
311 m_freem(m);
312 } else {
313 sorwakeup_locked(V_send_so);
314 }
315
316 SEND_UNLOCK();
317 return (0);
318 }
319
320 static struct protosw send_protosw = {
321 .pr_type = SOCK_RAW,
322 .pr_flags = PR_ATOMIC|PR_ADDR,
323 .pr_protocol = IPPROTO_SEND,
324 .pr_attach = send_attach,
325 .pr_send = send_send,
326 .pr_detach = send_close
327 };
328
329 static int
send_modevent(module_t mod,int type,void * unused)330 send_modevent(module_t mod, int type, void *unused)
331 {
332 #ifdef __notyet__
333 VNET_ITERATOR_DECL(vnet_iter);
334 #endif
335 int error;
336
337 switch (type) {
338 case MOD_LOAD:
339 SEND_LOCK_INIT();
340
341 error = protosw_register(&inet6domain, &send_protosw);
342 if (error != 0) {
343 printf("%s:%d: MOD_LOAD pf_proto_register(): %d\n",
344 __func__, __LINE__, error);
345 SEND_LOCK_DESTROY();
346 break;
347 }
348 send_sendso_input_hook = send_input;
349 break;
350 case MOD_UNLOAD:
351 /* Do not allow unloading w/o locking. */
352 return (EBUSY);
353 #ifdef __notyet__
354 VNET_LIST_RLOCK_NOSLEEP();
355 SEND_LOCK();
356 VNET_FOREACH(vnet_iter) {
357 CURVNET_SET(vnet_iter);
358 if (V_send_so != NULL) {
359 CURVNET_RESTORE();
360 SEND_UNLOCK();
361 VNET_LIST_RUNLOCK_NOSLEEP();
362 return (EBUSY);
363 }
364 CURVNET_RESTORE();
365 }
366 SEND_UNLOCK();
367 VNET_LIST_RUNLOCK_NOSLEEP();
368 error = protosw_unregister(&send_protosw);
369 if (error == 0)
370 SEND_LOCK_DESTROY();
371 send_sendso_input_hook = NULL;
372 break;
373 #endif
374 default:
375 error = 0;
376 break;
377 }
378
379 return (error);
380 }
381
382 static moduledata_t sendmod = {
383 "send",
384 send_modevent,
385 0
386 };
387
388 DECLARE_MODULE(send, sendmod, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);
389