]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet/ip_encap.c
MFV r323530,r323533,r323534: 7431 ZFS Channel Programs, and followups
[FreeBSD/FreeBSD.git] / sys / netinet / ip_encap.c
1 /*      $KAME: ip_encap.c,v 1.41 2001/03/15 08:35:08 itojun Exp $       */
2
3 /*-
4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
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  * 3. Neither the name of the project nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 /*
32  * My grandfather said that there's a devil inside tunnelling technology...
33  *
34  * We have surprisingly many protocols that want packets with IP protocol
35  * #4 or #41.  Here's a list of protocols that want protocol #41:
36  *      RFC1933 configured tunnel
37  *      RFC1933 automatic tunnel
38  *      RFC2401 IPsec tunnel
39  *      RFC2473 IPv6 generic packet tunnelling
40  *      RFC2529 6over4 tunnel
41  *      mobile-ip6 (uses RFC2473)
42  *      RFC3056 6to4 tunnel
43  *      isatap tunnel
44  * Here's a list of protocol that want protocol #4:
45  *      RFC1853 IPv4-in-IPv4 tunnelling
46  *      RFC2003 IPv4 encapsulation within IPv4
47  *      RFC2344 reverse tunnelling for mobile-ip4
48  *      RFC2401 IPsec tunnel
49  * Well, what can I say.  They impose different en/decapsulation mechanism
50  * from each other, so they need separate protocol handler.  The only one
51  * we can easily determine by protocol # is IPsec, which always has
52  * AH/ESP/IPComp header right after outer IP header.
53  *
54  * So, clearly good old protosw does not work for protocol #4 and #41.
55  * The code will let you match protocol via src/dst address pair.
56  */
57 /* XXX is M_NETADDR correct? */
58
59 #include <sys/cdefs.h>
60 __FBSDID("$FreeBSD$");
61
62 #include "opt_mrouting.h"
63 #include "opt_inet.h"
64 #include "opt_inet6.h"
65
66 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/lock.h>
69 #include <sys/mutex.h>
70 #include <sys/socket.h>
71 #include <sys/sockio.h>
72 #include <sys/mbuf.h>
73 #include <sys/errno.h>
74 #include <sys/protosw.h>
75 #include <sys/queue.h>
76
77 #include <net/if.h>
78 #include <net/route.h>
79
80 #include <netinet/in.h>
81 #include <netinet/in_systm.h>
82 #include <netinet/ip.h>
83 #include <netinet/ip_var.h>
84 #include <netinet/ip_encap.h>
85
86 #ifdef INET6
87 #include <netinet/ip6.h>
88 #include <netinet6/ip6_var.h>
89 #endif
90
91 #include <machine/stdarg.h>
92
93 #include <sys/kernel.h>
94 #include <sys/malloc.h>
95 static MALLOC_DEFINE(M_NETADDR, "encap_export_host", "Export host address structure");
96
97 static void encap_add(struct encaptab *);
98 static int mask_match(const struct encaptab *, const struct sockaddr *,
99                 const struct sockaddr *);
100 static void encap_fillarg(struct mbuf *, void *);
101
102 /*
103  * All global variables in ip_encap.c are locked using encapmtx.
104  */
105 static struct mtx encapmtx;
106 MTX_SYSINIT(encapmtx, &encapmtx, "encapmtx", MTX_DEF);
107 static LIST_HEAD(, encaptab) encaptab = LIST_HEAD_INITIALIZER(encaptab);
108
109 /*
110  * We currently keey encap_init() for source code compatibility reasons --
111  * it's referenced by KAME pieces in netinet6.
112  */
113 void
114 encap_init(void)
115 {
116 }
117
118 #ifdef INET
119 int
120 encap4_input(struct mbuf **mp, int *offp, int proto)
121 {
122         struct ip *ip;
123         struct mbuf *m;
124         struct sockaddr_in s, d;
125         const struct protosw *psw;
126         struct encaptab *ep, *match;
127         void *arg;
128         int matchprio, off, prio;
129
130         m = *mp;
131         off = *offp;
132         ip = mtod(m, struct ip *);
133
134         bzero(&s, sizeof(s));
135         s.sin_family = AF_INET;
136         s.sin_len = sizeof(struct sockaddr_in);
137         s.sin_addr = ip->ip_src;
138         bzero(&d, sizeof(d));
139         d.sin_family = AF_INET;
140         d.sin_len = sizeof(struct sockaddr_in);
141         d.sin_addr = ip->ip_dst;
142
143         arg = NULL;
144         psw = NULL;
145         match = NULL;
146         matchprio = 0;
147         mtx_lock(&encapmtx);
148         LIST_FOREACH(ep, &encaptab, chain) {
149                 if (ep->af != AF_INET)
150                         continue;
151                 if (ep->proto >= 0 && ep->proto != proto)
152                         continue;
153                 if (ep->func)
154                         prio = (*ep->func)(m, off, proto, ep->arg);
155                 else {
156                         /*
157                          * it's inbound traffic, we need to match in reverse
158                          * order
159                          */
160                         prio = mask_match(ep, (struct sockaddr *)&d,
161                             (struct sockaddr *)&s);
162                 }
163
164                 /*
165                  * We prioritize the matches by using bit length of the
166                  * matches.  mask_match() and user-supplied matching function
167                  * should return the bit length of the matches (for example,
168                  * if both src/dst are matched for IPv4, 64 should be returned).
169                  * 0 or negative return value means "it did not match".
170                  *
171                  * The question is, since we have two "mask" portion, we
172                  * cannot really define total order between entries.
173                  * For example, which of these should be preferred?
174                  * mask_match() returns 48 (32 + 16) for both of them.
175                  *      src=3ffe::/16, dst=3ffe:501::/32
176                  *      src=3ffe:501::/32, dst=3ffe::/16
177                  *
178                  * We need to loop through all the possible candidates
179                  * to get the best match - the search takes O(n) for
180                  * n attachments (i.e. interfaces).
181                  */
182                 if (prio <= 0)
183                         continue;
184                 if (prio > matchprio) {
185                         matchprio = prio;
186                         match = ep;
187                 }
188         }
189         if (match != NULL) {
190                 psw = match->psw;
191                 arg = match->arg;
192         }
193         mtx_unlock(&encapmtx);
194
195         if (match != NULL) {
196                 /* found a match, "match" has the best one */
197                 if (psw != NULL && psw->pr_input != NULL) {
198                         encap_fillarg(m, arg);
199                         (*psw->pr_input)(mp, offp, proto);
200                 } else
201                         m_freem(m);
202                 return (IPPROTO_DONE);
203         }
204
205         /* last resort: inject to raw socket */
206         return (rip_input(mp, offp, proto));
207 }
208 #endif
209
210 #ifdef INET6
211 int
212 encap6_input(struct mbuf **mp, int *offp, int proto)
213 {
214         struct mbuf *m = *mp;
215         struct ip6_hdr *ip6;
216         struct sockaddr_in6 s, d;
217         const struct protosw *psw;
218         struct encaptab *ep, *match;
219         void *arg;
220         int prio, matchprio;
221
222         ip6 = mtod(m, struct ip6_hdr *);
223
224         bzero(&s, sizeof(s));
225         s.sin6_family = AF_INET6;
226         s.sin6_len = sizeof(struct sockaddr_in6);
227         s.sin6_addr = ip6->ip6_src;
228         bzero(&d, sizeof(d));
229         d.sin6_family = AF_INET6;
230         d.sin6_len = sizeof(struct sockaddr_in6);
231         d.sin6_addr = ip6->ip6_dst;
232
233         arg = NULL;
234         psw = NULL;
235         match = NULL;
236         matchprio = 0;
237         mtx_lock(&encapmtx);
238         LIST_FOREACH(ep, &encaptab, chain) {
239                 if (ep->af != AF_INET6)
240                         continue;
241                 if (ep->proto >= 0 && ep->proto != proto)
242                         continue;
243                 if (ep->func)
244                         prio = (*ep->func)(m, *offp, proto, ep->arg);
245                 else {
246                         /*
247                          * it's inbound traffic, we need to match in reverse
248                          * order
249                          */
250                         prio = mask_match(ep, (struct sockaddr *)&d,
251                             (struct sockaddr *)&s);
252                 }
253
254                 /* see encap4_input() for issues here */
255                 if (prio <= 0)
256                         continue;
257                 if (prio > matchprio) {
258                         matchprio = prio;
259                         match = ep;
260                 }
261         }
262         if (match != NULL) {
263                 psw = match->psw;
264                 arg = match->arg;
265         }
266         mtx_unlock(&encapmtx);
267
268         if (match != NULL) {
269                 /* found a match */
270                 if (psw != NULL && psw->pr_input != NULL) {
271                         encap_fillarg(m, arg);
272                         return (*psw->pr_input)(mp, offp, proto);
273                 } else {
274                         m_freem(m);
275                         return (IPPROTO_DONE);
276                 }
277         }
278
279         /* last resort: inject to raw socket */
280         return rip6_input(mp, offp, proto);
281 }
282 #endif
283
284 /*lint -sem(encap_add, custodial(1)) */
285 static void
286 encap_add(struct encaptab *ep)
287 {
288
289         mtx_assert(&encapmtx, MA_OWNED);
290         LIST_INSERT_HEAD(&encaptab, ep, chain);
291 }
292
293 /*
294  * sp (src ptr) is always my side, and dp (dst ptr) is always remote side.
295  * length of mask (sm and dm) is assumed to be same as sp/dp.
296  * Return value will be necessary as input (cookie) for encap_detach().
297  */
298 const struct encaptab *
299 encap_attach(int af, int proto, const struct sockaddr *sp,
300     const struct sockaddr *sm, const struct sockaddr *dp,
301     const struct sockaddr *dm, const struct protosw *psw, void *arg)
302 {
303         struct encaptab *ep;
304
305         /* sanity check on args */
306         if (sp->sa_len > sizeof(ep->src) || dp->sa_len > sizeof(ep->dst))
307                 return (NULL);
308         if (sp->sa_len != dp->sa_len)
309                 return (NULL);
310         if (af != sp->sa_family || af != dp->sa_family)
311                 return (NULL);
312
313         /* check if anyone have already attached with exactly same config */
314         mtx_lock(&encapmtx);
315         LIST_FOREACH(ep, &encaptab, chain) {
316                 if (ep->af != af)
317                         continue;
318                 if (ep->proto != proto)
319                         continue;
320                 if (ep->src.ss_len != sp->sa_len ||
321                     bcmp(&ep->src, sp, sp->sa_len) != 0 ||
322                     bcmp(&ep->srcmask, sm, sp->sa_len) != 0)
323                         continue;
324                 if (ep->dst.ss_len != dp->sa_len ||
325                     bcmp(&ep->dst, dp, dp->sa_len) != 0 ||
326                     bcmp(&ep->dstmask, dm, dp->sa_len) != 0)
327                         continue;
328
329                 mtx_unlock(&encapmtx);
330                 return (NULL);
331         }
332
333         ep = malloc(sizeof(*ep), M_NETADDR, M_NOWAIT);  /*XXX*/
334         if (ep == NULL) {
335                 mtx_unlock(&encapmtx);
336                 return (NULL);
337         }
338         bzero(ep, sizeof(*ep));
339
340         ep->af = af;
341         ep->proto = proto;
342         bcopy(sp, &ep->src, sp->sa_len);
343         bcopy(sm, &ep->srcmask, sp->sa_len);
344         bcopy(dp, &ep->dst, dp->sa_len);
345         bcopy(dm, &ep->dstmask, dp->sa_len);
346         ep->psw = psw;
347         ep->arg = arg;
348
349         encap_add(ep);
350         mtx_unlock(&encapmtx);
351         return (ep);
352 }
353
354 const struct encaptab *
355 encap_attach_func(int af, int proto,
356     int (*func)(const struct mbuf *, int, int, void *),
357     const struct protosw *psw, void *arg)
358 {
359         struct encaptab *ep;
360
361         /* sanity check on args */
362         if (!func)
363                 return (NULL);
364
365         ep = malloc(sizeof(*ep), M_NETADDR, M_NOWAIT);  /*XXX*/
366         if (ep == NULL)
367                 return (NULL);
368         bzero(ep, sizeof(*ep));
369
370         ep->af = af;
371         ep->proto = proto;
372         ep->func = func;
373         ep->psw = psw;
374         ep->arg = arg;
375
376         mtx_lock(&encapmtx);
377         encap_add(ep);
378         mtx_unlock(&encapmtx);
379         return (ep);
380 }
381
382 int
383 encap_detach(const struct encaptab *cookie)
384 {
385         const struct encaptab *ep = cookie;
386         struct encaptab *p;
387
388         mtx_lock(&encapmtx);
389         LIST_FOREACH(p, &encaptab, chain) {
390                 if (p == ep) {
391                         LIST_REMOVE(p, chain);
392                         mtx_unlock(&encapmtx);
393                         free(p, M_NETADDR);     /*XXX*/
394                         return 0;
395                 }
396         }
397         mtx_unlock(&encapmtx);
398
399         return EINVAL;
400 }
401
402 static int
403 mask_match(const struct encaptab *ep, const struct sockaddr *sp,
404     const struct sockaddr *dp)
405 {
406         struct sockaddr_storage s;
407         struct sockaddr_storage d;
408         int i;
409         const u_int8_t *p, *q;
410         u_int8_t *r;
411         int matchlen;
412
413         if (sp->sa_len > sizeof(s) || dp->sa_len > sizeof(d))
414                 return 0;
415         if (sp->sa_family != ep->af || dp->sa_family != ep->af)
416                 return 0;
417         if (sp->sa_len != ep->src.ss_len || dp->sa_len != ep->dst.ss_len)
418                 return 0;
419
420         matchlen = 0;
421
422         p = (const u_int8_t *)sp;
423         q = (const u_int8_t *)&ep->srcmask;
424         r = (u_int8_t *)&s;
425         for (i = 0 ; i < sp->sa_len; i++) {
426                 r[i] = p[i] & q[i];
427                 /* XXX estimate */
428                 matchlen += (q[i] ? 8 : 0);
429         }
430
431         p = (const u_int8_t *)dp;
432         q = (const u_int8_t *)&ep->dstmask;
433         r = (u_int8_t *)&d;
434         for (i = 0 ; i < dp->sa_len; i++) {
435                 r[i] = p[i] & q[i];
436                 /* XXX rough estimate */
437                 matchlen += (q[i] ? 8 : 0);
438         }
439
440         /* need to overwrite len/family portion as we don't compare them */
441         s.ss_len = sp->sa_len;
442         s.ss_family = sp->sa_family;
443         d.ss_len = dp->sa_len;
444         d.ss_family = dp->sa_family;
445
446         if (bcmp(&s, &ep->src, ep->src.ss_len) == 0 &&
447             bcmp(&d, &ep->dst, ep->dst.ss_len) == 0) {
448                 return matchlen;
449         } else
450                 return 0;
451 }
452
453 static void
454 encap_fillarg(struct mbuf *m, void *arg)
455 {
456         struct m_tag *tag;
457
458         if (arg != NULL) {
459                 tag = m_tag_get(PACKET_TAG_ENCAP, sizeof(void *), M_NOWAIT);
460                 if (tag != NULL) {
461                         *(void**)(tag+1) = arg;
462                         m_tag_prepend(m, tag);
463                 }
464         }
465 }
466
467 void *
468 encap_getarg(struct mbuf *m)
469 {
470         void *p = NULL;
471         struct m_tag *tag;
472
473         tag = m_tag_find(m, PACKET_TAG_ENCAP, NULL);
474         if (tag) {
475                 p = *(void**)(tag+1);
476                 m_tag_delete(m, tag);
477         }
478         return p;
479 }