2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2009-2010 Ana Kukec <anchie@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/malloc.h>
36 #include <sys/module.h>
38 #include <sys/protosw.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>
46 #include <net/route.h>
48 #include <net/if_var.h>
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>
57 #include <netinet6/in6_var.h>
58 #include <netinet6/nd6.h>
59 #include <netinet6/scope6_var.h>
60 #include <netinet6/send.h>
62 static MALLOC_DEFINE(M_SEND, "send", "Secure Neighbour Discovery");
65 * The socket used to communicate with the SeND daemon.
67 VNET_DEFINE_STATIC(struct socket *, send_so);
68 #define V_send_so VNET(send_so)
70 u_long send_sendspace = 8 * (1024 + sizeof(struct sockaddr_send));
71 u_long send_recvspace = 9216;
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)
80 send_attach(struct socket *so, int proto, struct thread *td)
85 if (V_send_so != NULL) {
90 error = priv_check(td, PRIV_NETINET_RAW);
96 if (proto != IPPROTO_SEND) {
98 return (EPROTONOSUPPORT);
100 error = soreserve(so, send_sendspace, send_recvspace);
113 send_output(struct mbuf *m, struct ifnet *ifp, int direction)
116 struct sockaddr_in6 dst;
117 struct icmp6_hdr *icmp6;
118 struct epoch_tracker et;
123 * Receive incoming (SeND-protected) or outgoing traffic
124 * (SeND-validated) from the SeND user space application.
129 if (m->m_len < (sizeof(struct ip6_hdr) +
130 sizeof(struct icmp6_hdr))) {
131 m = m_pullup(m, sizeof(struct ip6_hdr) +
132 sizeof(struct icmp6_hdr));
137 /* Before passing off the mbuf record the proper interface. */
138 m->m_pkthdr.rcvif = ifp;
140 if (m->m_flags & M_PKTHDR)
141 icmp6len = m->m_pkthdr.len - sizeof(struct ip6_hdr);
143 panic("Doh! not the first mbuf.");
145 ip6 = mtod(m, struct ip6_hdr *);
146 icmp6 = (struct icmp6_hdr *)(ip6 + 1);
150 * Output the packet as icmp6.c:icpm6_input() would do.
151 * The mbuf is always consumed, so we do not have to
155 switch (icmp6->icmp6_type) {
156 case ND_NEIGHBOR_SOLICIT:
158 nd6_ns_input(m, sizeof(struct ip6_hdr), icmp6len);
161 case ND_NEIGHBOR_ADVERT:
162 nd6_na_input(m, sizeof(struct ip6_hdr), icmp6len);
165 icmp6_redirect_input(m, sizeof(struct ip6_hdr));
167 case ND_ROUTER_SOLICIT:
168 nd6_rs_input(m, sizeof(struct ip6_hdr), icmp6len);
170 case ND_ROUTER_ADVERT:
171 nd6_ra_input(m, sizeof(struct ip6_hdr), icmp6len);
182 if (m->m_len < sizeof(struct ip6_hdr)) {
183 m = m_pullup(m, sizeof(struct ip6_hdr));
187 ip6 = mtod(m, struct ip6_hdr *);
188 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst))
189 m->m_flags |= M_MCAST;
191 bzero(&dst, sizeof(dst));
192 dst.sin6_family = AF_INET6;
193 dst.sin6_len = sizeof(dst);
194 dst.sin6_addr = ip6->ip6_dst;
196 m_clrprotoflags(m); /* Avoid confusing lower layers. */
198 IP_PROBE(send, NULL, NULL, ip6, ifp, NULL, ip6);
201 * Output the packet as nd6.c:nd6_output_lle() would do.
202 * The mbuf is always consumed, so we do not have to care
204 * XXX-BZ as we added data, what about fragmenting,
207 error = ((*ifp->if_output)(ifp, m, (struct sockaddr *)&dst,
214 panic("%s: direction %d neither SND_IN nor SND_OUT.",
215 __func__, direction);
220 * Receive a SeND message from user space to be either send out by the kernel
221 * or, with SeND ICMPv6 options removed, to be further processed by the icmp6
225 send_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
226 struct mbuf *control, struct thread *td)
228 struct sockaddr_send *sendsrc;
232 KASSERT(V_send_so == so, ("%s: socket %p not send socket %p",
233 __func__, so, V_send_so));
235 sendsrc = (struct sockaddr_send *)nam;
236 if (sendsrc->send_family != AF_INET6) {
237 error = EAFNOSUPPORT;
240 if (sendsrc->send_len != sizeof(*sendsrc)) {
244 ifp = ifnet_byindex_ref(sendsrc->send_ifidx);
250 error = send_output(m, ifp, sendsrc->send_direction);
264 send_close(struct socket *so)
274 * Send a SeND message to user space, that was either received and has to be
275 * validated or was about to be send out and has to be handled by the SEND
276 * daemon adding SeND ICMPv6 options.
279 send_input(struct mbuf *m, struct ifnet *ifp, int direction, int msglen __unused)
282 struct sockaddr_send sendsrc;
285 if (V_send_so == NULL) {
291 * Make sure to clear any possible internally embedded scope before
292 * passing the packet to user space for SeND cryptographic signature
293 * validation to succeed.
295 ip6 = mtod(m, struct ip6_hdr *);
296 in6_clearscope(&ip6->ip6_src);
297 in6_clearscope(&ip6->ip6_dst);
299 bzero(&sendsrc, sizeof(sendsrc));
300 sendsrc.send_len = sizeof(sendsrc);
301 sendsrc.send_family = AF_INET6;
302 sendsrc.send_direction = direction;
303 sendsrc.send_ifidx = ifp->if_index;
306 * Send incoming or outgoing traffic to user space either to be
307 * protected (outgoing) or validated (incoming) according to rfc3971.
309 SOCKBUF_LOCK(&V_send_so->so_rcv);
310 if (sbappendaddr_locked(&V_send_so->so_rcv,
311 (struct sockaddr *)&sendsrc, m, NULL) == 0) {
312 SOCKBUF_UNLOCK(&V_send_so->so_rcv);
316 sorwakeup_locked(V_send_so);
323 struct pr_usrreqs send_usrreqs = {
324 .pru_attach = send_attach,
325 .pru_send = send_send,
326 .pru_detach = send_close
328 struct protosw send_protosw = {
330 .pr_flags = PR_ATOMIC|PR_ADDR,
331 .pr_protocol = IPPROTO_SEND,
332 .pr_usrreqs = &send_usrreqs
336 send_modevent(module_t mod, int type, void *unused)
339 VNET_ITERATOR_DECL(vnet_iter);
347 error = pf_proto_register(PF_INET6, &send_protosw);
349 printf("%s:%d: MOD_LOAD pf_proto_register(): %d\n",
350 __func__, __LINE__, error);
354 send_sendso_input_hook = send_input;
357 /* Do not allow unloading w/o locking. */
360 VNET_LIST_RLOCK_NOSLEEP();
362 VNET_FOREACH(vnet_iter) {
363 CURVNET_SET(vnet_iter);
364 if (V_send_so != NULL) {
367 VNET_LIST_RUNLOCK_NOSLEEP();
373 VNET_LIST_RUNLOCK_NOSLEEP();
374 error = pf_proto_unregister(PF_INET6, IPPROTO_SEND, SOCK_RAW);
377 send_sendso_input_hook = NULL;
388 static moduledata_t sendmod = {
394 DECLARE_MODULE(send, sendmod, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);