2 * Copyright (c) 2009-2010 Ana Kukec <anchie@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/kernel.h>
33 #include <sys/module.h>
35 #include <sys/protosw.h>
36 #include <sys/systm.h>
37 #include <sys/socket.h>
38 #include <sys/sockstate.h>
39 #include <sys/sockbuf.h>
40 #include <sys/socketvar.h>
41 #include <sys/types.h>
43 #include <net/route.h>
45 #include <net/if_var.h>
48 #include <netinet/in.h>
49 #include <netinet/ip_var.h>
50 #include <netinet/ip6.h>
51 #include <netinet/icmp6.h>
53 #include <netinet6/in6_var.h>
54 #include <netinet6/nd6.h>
55 #include <netinet6/scope6_var.h>
56 #include <netinet6/send.h>
58 MALLOC_DEFINE(M_SEND, "send", "Secure Neighbour Discovery");
61 * The socket used to communicate with the SeND daemon.
63 static VNET_DEFINE(struct socket *, send_so);
64 #define V_send_so VNET(send_so)
66 u_long send_sendspace = 8 * (1024 + sizeof(struct sockaddr_send));
67 u_long send_recvspace = 9216;
70 #define SEND_LOCK_INIT() mtx_init(&send_mtx, "send_mtx", NULL, MTX_DEF)
71 #define SEND_LOCK() mtx_lock(&send_mtx)
72 #define SEND_UNLOCK() mtx_unlock(&send_mtx)
73 #define SEND_LOCK_DESTROY() mtx_destroy(&send_mtx)
76 send_attach(struct socket *so, int proto, struct thread *td)
81 if (V_send_so != NULL) {
86 error = priv_check(td, PRIV_NETINET_RAW);
92 if (proto != IPPROTO_SEND) {
94 return (EPROTONOSUPPORT);
96 error = soreserve(so, send_sendspace, send_recvspace);
109 send_output(struct mbuf *m, struct ifnet *ifp, int direction)
112 struct sockaddr_in6 dst;
113 struct icmp6_hdr *icmp6;
117 * Receive incoming (SeND-protected) or outgoing traffic
118 * (SeND-validated) from the SeND user space application.
123 if (m->m_len < (sizeof(struct ip6_hdr) +
124 sizeof(struct icmp6_hdr))) {
125 m = m_pullup(m, sizeof(struct ip6_hdr) +
126 sizeof(struct icmp6_hdr));
131 /* Before passing off the mbuf record the proper interface. */
132 m->m_pkthdr.rcvif = ifp;
134 if (m->m_flags & M_PKTHDR)
135 icmp6len = m->m_pkthdr.len - sizeof(struct ip6_hdr);
137 panic("Doh! not the first mbuf.");
139 ip6 = mtod(m, struct ip6_hdr *);
140 icmp6 = (struct icmp6_hdr *)(ip6 + 1);
143 * Output the packet as icmp6.c:icpm6_input() would do.
144 * The mbuf is always consumed, so we do not have to
147 switch (icmp6->icmp6_type) {
148 case ND_NEIGHBOR_SOLICIT:
149 nd6_ns_input(m, sizeof(struct ip6_hdr), icmp6len);
151 case ND_NEIGHBOR_ADVERT:
152 nd6_na_input(m, sizeof(struct ip6_hdr), icmp6len);
155 icmp6_redirect_input(m, sizeof(struct ip6_hdr));
157 case ND_ROUTER_SOLICIT:
158 nd6_rs_input(m, sizeof(struct ip6_hdr), icmp6len);
160 case ND_ROUTER_ADVERT:
161 nd6_ra_input(m, sizeof(struct ip6_hdr), icmp6len);
169 if (m->m_len < sizeof(struct ip6_hdr)) {
170 m = m_pullup(m, sizeof(struct ip6_hdr));
174 ip6 = mtod(m, struct ip6_hdr *);
175 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst))
176 m->m_flags |= M_MCAST;
178 bzero(&dst, sizeof(dst));
179 dst.sin6_family = AF_INET6;
180 dst.sin6_len = sizeof(dst);
181 dst.sin6_addr = ip6->ip6_dst;
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
187 * XXX-BZ as we added data, what about fragmenting,
191 error = ((*ifp->if_output)(ifp, m, (struct sockaddr *)&dst,
198 panic("%s: direction %d neither SND_IN nor SND_OUT.",
199 __func__, direction);
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
209 send_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
210 struct mbuf *control, struct thread *td)
212 struct sockaddr_send *sendsrc;
216 KASSERT(V_send_so == so, ("%s: socket %p not send socket %p",
217 __func__, so, V_send_so));
219 sendsrc = (struct sockaddr_send *)nam;
220 ifp = ifnet_byindex_ref(sendsrc->send_ifidx);
226 error = send_output(m, ifp, sendsrc->send_direction);
237 send_close(struct socket *so)
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.
252 send_input(struct mbuf *m, struct ifnet *ifp, int direction, int msglen __unused)
255 struct sockaddr_send sendsrc;
258 if (V_send_so == NULL) {
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.
268 ip6 = mtod(m, struct ip6_hdr *);
269 in6_clearscope(&ip6->ip6_src);
270 in6_clearscope(&ip6->ip6_dst);
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;
279 * Send incoming or outgoing traffic to user space either to be
280 * protected (outgoing) or validated (incoming) according to rfc3971.
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);
289 sorwakeup_locked(V_send_so);
296 struct pr_usrreqs send_usrreqs = {
297 .pru_attach = send_attach,
298 .pru_send = send_send,
299 .pru_detach = send_close
301 struct protosw send_protosw = {
303 .pr_flags = PR_ATOMIC|PR_ADDR,
304 .pr_protocol = IPPROTO_SEND,
305 .pr_usrreqs = &send_usrreqs
309 send_modevent(module_t mod, int type, void *unused)
312 VNET_ITERATOR_DECL(vnet_iter);
320 error = pf_proto_register(PF_INET6, &send_protosw);
322 printf("%s:%d: MOD_LOAD pf_proto_register(): %d\n",
323 __func__, __LINE__, error);
327 send_sendso_input_hook = send_input;
330 /* Do not allow unloading w/o locking. */
333 VNET_LIST_RLOCK_NOSLEEP();
335 VNET_FOREACH(vnet_iter) {
336 CURVNET_SET(vnet_iter);
337 if (V_send_so != NULL) {
340 VNET_LIST_RUNLOCK_NOSLEEP();
346 VNET_LIST_RUNLOCK_NOSLEEP();
347 error = pf_proto_unregister(PF_INET6, IPPROTO_SEND, SOCK_RAW);
350 send_sendso_input_hook = NULL;
361 static moduledata_t sendmod = {
367 DECLARE_MODULE(send, sendmod, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);