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