Merge from vendor branch OPENSSL:
[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.8 2004/06/03 18:30:03 joerg 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 #include <netinet/ipprotosw.h>
82
83 #ifdef INET6
84 #include <netinet/ip6.h>
85 #include <netinet6/ip6_var.h>
86 #include <netinet6/ip6protosw.h>
87 #endif
88
89 #include <machine/stdarg.h>
90
91 #include <net/net_osdep.h>
92
93 #include <sys/kernel.h>
94 #include <sys/malloc.h>
95 MALLOC_DEFINE(M_NETADDR, "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 *, const struct encaptab *);
101
102 #ifndef LIST_HEAD_INITIALIZER
103 /* rely upon BSS initialization */
104 LIST_HEAD(, encaptab) encaptab;
105 #else
106 LIST_HEAD(, encaptab) encaptab = LIST_HEAD_INITIALIZER(&encaptab);
107 #endif
108
109 void     (*ipip_input)(struct mbuf *, int, int); /* hook for mrouting */
110
111 void
112 encap_init()
113 {
114         static int initialized = 0;
115
116         if (initialized)
117                 return;
118         initialized++;
119 #if 0
120         /*
121          * we cannot use LIST_INIT() here, since drivers may want to call
122          * encap_attach(), on driver attach.  encap_init() will be called
123          * on AF_INET{,6} initialization, which happens after driver
124          * initialization - using LIST_INIT() here can nuke encap_attach()
125          * from drivers.
126          */
127         LIST_INIT(&encaptab);
128 #endif
129 }
130
131 #ifdef INET
132 void
133 encap4_input(struct mbuf *m, ...)
134 {
135         int off, proto;
136         struct ip *ip;
137         struct sockaddr_in s, d;
138         const struct ipprotosw *psw;
139         struct encaptab *ep, *match;
140         int prio, matchprio;
141         __va_list ap;
142
143         __va_start(ap, m);
144         off = __va_arg(ap, int);
145         proto = __va_arg(ap, int);
146         __va_end(ap);
147
148         ip = mtod(m, struct ip *);
149
150         bzero(&s, sizeof(s));
151         s.sin_family = AF_INET;
152         s.sin_len = sizeof(struct sockaddr_in);
153         s.sin_addr = ip->ip_src;
154         bzero(&d, sizeof(d));
155         d.sin_family = AF_INET;
156         d.sin_len = sizeof(struct sockaddr_in);
157         d.sin_addr = ip->ip_dst;
158
159         match = NULL;
160         matchprio = 0;
161         for (ep = LIST_FIRST(&encaptab); ep; ep = LIST_NEXT(ep, chain)) {
162                 if (ep->af != AF_INET)
163                         continue;
164                 if (ep->proto >= 0 && ep->proto != proto)
165                         continue;
166                 if (ep->func)
167                         prio = (*ep->func)(m, off, proto, ep->arg);
168                 else {
169                         /*
170                          * it's inbound traffic, we need to match in reverse
171                          * order
172                          */
173                         prio = mask_match(ep, (struct sockaddr *)&d,
174                             (struct sockaddr *)&s);
175                 }
176
177                 /*
178                  * We prioritize the matches by using bit length of the
179                  * matches.  mask_match() and user-supplied matching function
180                  * should return the bit length of the matches (for example,
181                  * if both src/dst are matched for IPv4, 64 should be returned).
182                  * 0 or negative return value means "it did not match".
183                  *
184                  * The question is, since we have two "mask" portion, we
185                  * cannot really define total order between entries.
186                  * For example, which of these should be preferred?
187                  * mask_match() returns 48 (32 + 16) for both of them.
188                  *      src=3ffe::/16, dst=3ffe:501::/32
189                  *      src=3ffe:501::/32, dst=3ffe::/16
190                  *
191                  * We need to loop through all the possible candidates
192                  * to get the best match - the search takes O(n) for
193                  * n attachments (i.e. interfaces).
194                  */
195                 if (prio <= 0)
196                         continue;
197                 if (prio > matchprio) {
198                         matchprio = prio;
199                         match = ep;
200                 }
201         }
202
203         if (match) {
204                 /* found a match, "match" has the best one */
205                 psw = (const struct ipprotosw *)match->psw;
206                 if (psw && psw->pr_input) {
207                         encap_fillarg(m, match);
208                         (*psw->pr_input)(m, off, proto);
209                 } else
210                         m_freem(m);
211                 return;
212         }
213
214         /* for backward compatibility */
215         if (proto == IPPROTO_IPV4 && ipip_input) {
216                 ipip_input(m, off, proto);
217                 return;
218         }
219
220         /* last resort: inject to raw socket */
221         rip_input(m, off, proto);
222 }
223 #endif
224
225 #ifdef INET6
226 int
227 encap6_input(mp, offp, proto)
228         struct mbuf **mp;
229         int *offp;
230         int proto;
231 {
232         struct mbuf *m = *mp;
233         struct ip6_hdr *ip6;
234         struct sockaddr_in6 s, d;
235         const struct ip6protosw *psw;
236         struct encaptab *ep, *match;
237         int prio, matchprio;
238
239         ip6 = mtod(m, struct ip6_hdr *);
240
241         bzero(&s, sizeof(s));
242         s.sin6_family = AF_INET6;
243         s.sin6_len = sizeof(struct sockaddr_in6);
244         s.sin6_addr = ip6->ip6_src;
245         bzero(&d, sizeof(d));
246         d.sin6_family = AF_INET6;
247         d.sin6_len = sizeof(struct sockaddr_in6);
248         d.sin6_addr = ip6->ip6_dst;
249
250         match = NULL;
251         matchprio = 0;
252         for (ep = LIST_FIRST(&encaptab); ep; ep = LIST_NEXT(ep, chain)) {
253                 if (ep->af != AF_INET6)
254                         continue;
255                 if (ep->proto >= 0 && ep->proto != proto)
256                         continue;
257                 if (ep->func)
258                         prio = (*ep->func)(m, *offp, proto, ep->arg);
259                 else {
260                         /*
261                          * it's inbound traffic, we need to match in reverse
262                          * order
263                          */
264                         prio = mask_match(ep, (struct sockaddr *)&d,
265                             (struct sockaddr *)&s);
266                 }
267
268                 /* see encap4_input() for issues here */
269                 if (prio <= 0)
270                         continue;
271                 if (prio > matchprio) {
272                         matchprio = prio;
273                         match = ep;
274                 }
275         }
276
277         if (match) {
278                 /* found a match */
279                 psw = (const struct ip6protosw *)match->psw;
280                 if (psw && psw->pr_input) {
281                         encap_fillarg(m, match);
282                         return (*psw->pr_input)(mp, offp, proto);
283                 } else {
284                         m_freem(m);
285                         return IPPROTO_DONE;
286                 }
287         }
288
289         /* last resort: inject to raw socket */
290         return rip6_input(mp, offp, proto);
291 }
292 #endif
293
294 static void
295 encap_add(ep)
296         struct encaptab *ep;
297 {
298
299         LIST_INSERT_HEAD(&encaptab, ep, chain);
300 }
301
302 /*
303  * sp (src ptr) is always my side, and dp (dst ptr) is always remote side.
304  * length of mask (sm and dm) is assumed to be same as sp/dp.
305  * Return value will be necessary as input (cookie) for encap_detach().
306  */
307 const struct encaptab *
308 encap_attach(af, proto, sp, sm, dp, dm, psw, arg)
309         int af;
310         int proto;
311         const struct sockaddr *sp, *sm;
312         const struct sockaddr *dp, *dm;
313         const struct protosw *psw;
314         void *arg;
315 {
316         struct encaptab *ep;
317         int error;
318         int s;
319
320         s = splnet();
321         /* sanity check on args */
322         if (sp->sa_len > sizeof(ep->src) || dp->sa_len > sizeof(ep->dst)) {
323                 error = EINVAL;
324                 goto fail;
325         }
326         if (sp->sa_len != dp->sa_len) {
327                 error = EINVAL;
328                 goto fail;
329         }
330         if (af != sp->sa_family || af != dp->sa_family) {
331                 error = EINVAL;
332                 goto fail;
333         }
334
335         /* check if anyone have already attached with exactly same config */
336         for (ep = LIST_FIRST(&encaptab); ep; ep = LIST_NEXT(ep, chain)) {
337                 if (ep->af != af)
338                         continue;
339                 if (ep->proto != proto)
340                         continue;
341                 if (ep->src.ss_len != sp->sa_len ||
342                     bcmp(&ep->src, sp, sp->sa_len) != 0 ||
343                     bcmp(&ep->srcmask, sm, sp->sa_len) != 0)
344                         continue;
345                 if (ep->dst.ss_len != dp->sa_len ||
346                     bcmp(&ep->dst, dp, dp->sa_len) != 0 ||
347                     bcmp(&ep->dstmask, dm, dp->sa_len) != 0)
348                         continue;
349
350                 error = EEXIST;
351                 goto fail;
352         }
353
354         ep = malloc(sizeof(*ep), M_NETADDR, M_INTWAIT | M_ZERO | M_NULLOK);
355         if (ep == NULL) {
356                 error = ENOBUFS;
357                 goto fail;
358         }
359
360         ep->af = af;
361         ep->proto = proto;
362         bcopy(sp, &ep->src, sp->sa_len);
363         bcopy(sm, &ep->srcmask, sp->sa_len);
364         bcopy(dp, &ep->dst, dp->sa_len);
365         bcopy(dm, &ep->dstmask, dp->sa_len);
366         ep->psw = psw;
367         ep->arg = arg;
368
369         encap_add(ep);
370
371         error = 0;
372         splx(s);
373         return ep;
374
375 fail:
376         splx(s);
377         return NULL;
378 }
379
380 const struct encaptab *
381 encap_attach_func(af, proto, func, psw, arg)
382         int af;
383         int proto;
384         int (*func) (const struct mbuf *, int, int, void *);
385         const struct protosw *psw;
386         void *arg;
387 {
388         struct encaptab *ep;
389         int error;
390         int s;
391
392         s = splnet();
393         /* sanity check on args */
394         if (!func) {
395                 error = EINVAL;
396                 goto fail;
397         }
398
399         ep = malloc(sizeof(*ep), M_NETADDR, M_INTWAIT | M_ZERO | M_NULLOK);
400         if (ep == NULL) {
401                 error = ENOBUFS;
402                 goto fail;
403         }
404
405         ep->af = af;
406         ep->proto = proto;
407         ep->func = func;
408         ep->psw = psw;
409         ep->arg = arg;
410
411         encap_add(ep);
412
413         error = 0;
414         splx(s);
415         return ep;
416
417 fail:
418         splx(s);
419         return NULL;
420 }
421
422 int
423 encap_detach(cookie)
424         const struct encaptab *cookie;
425 {
426         const struct encaptab *ep = cookie;
427         struct encaptab *p;
428
429         for (p = LIST_FIRST(&encaptab); p; p = LIST_NEXT(p, chain)) {
430                 if (p == ep) {
431                         LIST_REMOVE(p, chain);
432                         free(p, M_NETADDR);     /*XXX*/
433                         return 0;
434                 }
435         }
436
437         return EINVAL;
438 }
439
440 static int
441 mask_match(ep, sp, dp)
442         const struct encaptab *ep;
443         const struct sockaddr *sp;
444         const struct sockaddr *dp;
445 {
446         struct sockaddr_storage s;
447         struct sockaddr_storage d;
448         int i;
449         const u_int8_t *p, *q;
450         u_int8_t *r;
451         int matchlen;
452
453         if (sp->sa_len > sizeof(s) || dp->sa_len > sizeof(d))
454                 return 0;
455         if (sp->sa_family != ep->af || dp->sa_family != ep->af)
456                 return 0;
457         if (sp->sa_len != ep->src.ss_len || dp->sa_len != ep->dst.ss_len)
458                 return 0;
459
460         matchlen = 0;
461
462         p = (const u_int8_t *)sp;
463         q = (const u_int8_t *)&ep->srcmask;
464         r = (u_int8_t *)&s;
465         for (i = 0 ; i < sp->sa_len; i++) {
466                 r[i] = p[i] & q[i];
467                 /* XXX estimate */
468                 matchlen += (q[i] ? 8 : 0);
469         }
470
471         p = (const u_int8_t *)dp;
472         q = (const u_int8_t *)&ep->dstmask;
473         r = (u_int8_t *)&d;
474         for (i = 0 ; i < dp->sa_len; i++) {
475                 r[i] = p[i] & q[i];
476                 /* XXX rough estimate */
477                 matchlen += (q[i] ? 8 : 0);
478         }
479
480         /* need to overwrite len/family portion as we don't compare them */
481         s.ss_len = sp->sa_len;
482         s.ss_family = sp->sa_family;
483         d.ss_len = dp->sa_len;
484         d.ss_family = dp->sa_family;
485
486         if (bcmp(&s, &ep->src, ep->src.ss_len) == 0 &&
487             bcmp(&d, &ep->dst, ep->dst.ss_len) == 0) {
488                 return matchlen;
489         } else
490                 return 0;
491 }
492
493 static void
494 encap_fillarg(m, ep)
495         struct mbuf *m;
496         const struct encaptab *ep;
497 {
498         struct m_tag *tag;
499
500         tag = m_tag_get(PACKET_TAG_ENCAP, sizeof (void*), MB_DONTWAIT);
501         if (tag) {
502                 *(void**)(tag+1) = ep->arg;
503                 m_tag_prepend(m, tag);
504         }
505 }
506
507 void *
508 encap_getarg(m)
509         struct mbuf *m;
510 {
511         void *p = NULL;
512         struct m_tag *tag;
513
514         tag = m_tag_find(m, PACKET_TAG_ENCAP, NULL);
515         if (tag) {
516                 p = *(void**)(tag+1);
517                 m_tag_delete(m, tag);
518         }
519         return p;
520 }