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