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