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>
32 #include <sys/malloc.h>
34 #include <sys/module.h>
36 #include <sys/protosw.h>
38 #include <sys/systm.h>
39 #include <sys/socket.h>
40 #include <sys/sockstate.h>
41 #include <sys/sockbuf.h>
42 #include <sys/socketvar.h>
43 #include <sys/types.h>
45 #include <net/route.h>
47 #include <net/if_var.h>
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>
56 #include <netinet6/in6_var.h>
57 #include <netinet6/nd6.h>
58 #include <netinet6/scope6_var.h>
59 #include <netinet6/send.h>
61 static MALLOC_DEFINE(M_SEND, "send", "Secure Neighbour Discovery");
64 * The socket used to communicate with the SeND daemon.
66 static VNET_DEFINE(struct socket *, send_so);
67 #define V_send_so VNET(send_so)
69 u_long send_sendspace = 8 * (1024 + sizeof(struct sockaddr_send));
70 u_long send_recvspace = 9216;
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)
79 send_attach(struct socket *so, int proto, struct thread *td)
84 if (V_send_so != NULL) {
89 error = priv_check(td, PRIV_NETINET_RAW);
95 if (proto != IPPROTO_SEND) {
97 return (EPROTONOSUPPORT);
99 error = soreserve(so, send_sendspace, send_recvspace);
112 send_output(struct mbuf *m, struct ifnet *ifp, int direction)
115 struct sockaddr_in6 dst;
116 struct icmp6_hdr *icmp6;
120 * Receive incoming (SeND-protected) or outgoing traffic
121 * (SeND-validated) from the SeND user space application.
126 if (m->m_len < (sizeof(struct ip6_hdr) +
127 sizeof(struct icmp6_hdr))) {
128 m = m_pullup(m, sizeof(struct ip6_hdr) +
129 sizeof(struct icmp6_hdr));
134 /* Before passing off the mbuf record the proper interface. */
135 m->m_pkthdr.rcvif = ifp;
137 if (m->m_flags & M_PKTHDR)
138 icmp6len = m->m_pkthdr.len - sizeof(struct ip6_hdr);
140 panic("Doh! not the first mbuf.");
142 ip6 = mtod(m, struct ip6_hdr *);
143 icmp6 = (struct icmp6_hdr *)(ip6 + 1);
146 * Output the packet as icmp6.c:icpm6_input() would do.
147 * The mbuf is always consumed, so we do not have to
150 switch (icmp6->icmp6_type) {
151 case ND_NEIGHBOR_SOLICIT:
152 nd6_ns_input(m, sizeof(struct ip6_hdr), icmp6len);
154 case ND_NEIGHBOR_ADVERT:
155 nd6_na_input(m, sizeof(struct ip6_hdr), icmp6len);
158 icmp6_redirect_input(m, sizeof(struct ip6_hdr));
160 case ND_ROUTER_SOLICIT:
161 nd6_rs_input(m, sizeof(struct ip6_hdr), icmp6len);
163 case ND_ROUTER_ADVERT:
164 nd6_ra_input(m, sizeof(struct ip6_hdr), icmp6len);
173 if (m->m_len < sizeof(struct ip6_hdr)) {
174 m = m_pullup(m, sizeof(struct ip6_hdr));
178 ip6 = mtod(m, struct ip6_hdr *);
179 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst))
180 m->m_flags |= M_MCAST;
182 bzero(&dst, sizeof(dst));
183 dst.sin6_family = AF_INET6;
184 dst.sin6_len = sizeof(dst);
185 dst.sin6_addr = ip6->ip6_dst;
187 m_clrprotoflags(m); /* Avoid confusing lower layers. */
189 IP_PROBE(send, NULL, NULL, ip6, ifp, NULL, ip6);
192 * Output the packet as nd6.c:nd6_output_lle() would do.
193 * The mbuf is always consumed, so we do not have to care
195 * XXX-BZ as we added data, what about fragmenting,
199 error = ((*ifp->if_output)(ifp, m, (struct sockaddr *)&dst,
206 panic("%s: direction %d neither SND_IN nor SND_OUT.",
207 __func__, direction);
212 * Receive a SeND message from user space to be either send out by the kernel
213 * or, with SeND ICMPv6 options removed, to be further processed by the icmp6
217 send_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
218 struct mbuf *control, struct thread *td)
220 struct sockaddr_send *sendsrc;
224 KASSERT(V_send_so == so, ("%s: socket %p not send socket %p",
225 __func__, so, V_send_so));
227 sendsrc = (struct sockaddr_send *)nam;
228 ifp = ifnet_byindex_ref(sendsrc->send_ifidx);
234 error = send_output(m, ifp, sendsrc->send_direction);
245 send_close(struct socket *so)
255 * Send a SeND message to user space, that was either received and has to be
256 * validated or was about to be send out and has to be handled by the SEND
257 * daemon adding SeND ICMPv6 options.
260 send_input(struct mbuf *m, struct ifnet *ifp, int direction, int msglen __unused)
263 struct sockaddr_send sendsrc;
266 if (V_send_so == NULL) {
272 * Make sure to clear any possible internally embedded scope before
273 * passing the packet to user space for SeND cryptographic signature
274 * validation to succeed.
276 ip6 = mtod(m, struct ip6_hdr *);
277 in6_clearscope(&ip6->ip6_src);
278 in6_clearscope(&ip6->ip6_dst);
280 bzero(&sendsrc, sizeof(sendsrc));
281 sendsrc.send_len = sizeof(sendsrc);
282 sendsrc.send_family = AF_INET6;
283 sendsrc.send_direction = direction;
284 sendsrc.send_ifidx = ifp->if_index;
287 * Send incoming or outgoing traffic to user space either to be
288 * protected (outgoing) or validated (incoming) according to rfc3971.
290 SOCKBUF_LOCK(&V_send_so->so_rcv);
291 if (sbappendaddr_locked(&V_send_so->so_rcv,
292 (struct sockaddr *)&sendsrc, m, NULL) == 0) {
293 SOCKBUF_UNLOCK(&V_send_so->so_rcv);
297 sorwakeup_locked(V_send_so);
304 struct pr_usrreqs send_usrreqs = {
305 .pru_attach = send_attach,
306 .pru_send = send_send,
307 .pru_detach = send_close
309 struct protosw send_protosw = {
311 .pr_flags = PR_ATOMIC|PR_ADDR,
312 .pr_protocol = IPPROTO_SEND,
313 .pr_usrreqs = &send_usrreqs
317 send_modevent(module_t mod, int type, void *unused)
320 VNET_ITERATOR_DECL(vnet_iter);
328 error = pf_proto_register(PF_INET6, &send_protosw);
330 printf("%s:%d: MOD_LOAD pf_proto_register(): %d\n",
331 __func__, __LINE__, error);
335 send_sendso_input_hook = send_input;
338 /* Do not allow unloading w/o locking. */
341 VNET_LIST_RLOCK_NOSLEEP();
343 VNET_FOREACH(vnet_iter) {
344 CURVNET_SET(vnet_iter);
345 if (V_send_so != NULL) {
348 VNET_LIST_RUNLOCK_NOSLEEP();
354 VNET_LIST_RUNLOCK_NOSLEEP();
355 error = pf_proto_unregister(PF_INET6, IPPROTO_SEND, SOCK_RAW);
358 send_sendso_input_hook = NULL;
369 static moduledata_t sendmod = {
375 DECLARE_MODULE(send, sendmod, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);