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