xref: /freebsd/sys/netinet/ip_encap.c (revision b601c69bdbe8755d26570261d7fd4c02ee4eff74)
1 /*	$FreeBSD$	*/
2 /*	$KAME: ip_encap.c,v 1.36 2000/06/17 20:34:24 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 
71 #include <net/if.h>
72 #include <net/route.h>
73 
74 #include <netinet/in.h>
75 #include <netinet/in_systm.h>
76 #include <netinet/ip.h>
77 #include <netinet/ip_var.h>
78 #include <netinet/ip_encap.h>
79 #ifdef MROUTING
80 #include <netinet/ip_mroute.h>
81 #endif /* MROUTING */
82 #include <netinet/ipprotosw.h>
83 
84 #ifdef INET6
85 #include <netinet/ip6.h>
86 #include <netinet6/ip6_var.h>
87 #include <netinet6/ip6protosw.h>
88 #endif
89 
90 #include <machine/stdarg.h>
91 
92 #include <net/net_osdep.h>
93 
94 #include <sys/kernel.h>
95 #include <sys/malloc.h>
96 MALLOC_DEFINE(M_NETADDR, "Export Host", "Export host address structure");
97 
98 static void encap_add __P((struct encaptab *));
99 static int mask_match __P((const struct encaptab *, const struct sockaddr *,
100 		const struct sockaddr *));
101 static void encap_fillarg __P((struct mbuf *, const struct encaptab *));
102 
103 /* rely upon BSS initialization */
104 LIST_HEAD(, encaptab) encaptab;
105 
106 void
107 encap_init()
108 {
109 #if 0
110 	/*
111 	 * we cannot use LIST_INIT() here, since drivers may want to call
112 	 * encap_attach(), on driver attach.  encap_init() will be called
113 	 * on AF_INET{,6} initialization, which happens after driver
114 	 * initialization - using LIST_INIT() here can nuke encap_attach()
115 	 * from drivers.
116 	 */
117 	LIST_INIT(&encaptab);
118 #endif
119 }
120 
121 void
122 #if __STDC__
123 encap4_input(struct mbuf *m, ...)
124 #else
125 encap4_input(m, va_alist)
126 	struct mbuf *m;
127 	va_dcl
128 #endif
129 {
130 	int off, proto;
131 	struct ip *ip;
132 	struct sockaddr_in s, d;
133 	const struct ipprotosw *psw;
134 	struct encaptab *ep, *match;
135 	va_list ap;
136 	int prio, matchprio;
137 
138 	va_start(ap, m);
139 	off = va_arg(ap, int);
140 	proto = va_arg(ap, int);
141 	va_end(ap);
142 
143 	ip = mtod(m, struct ip *);
144 
145 	bzero(&s, sizeof(s));
146 	s.sin_family = AF_INET;
147 	s.sin_len = sizeof(struct sockaddr_in);
148 	s.sin_addr = ip->ip_src;
149 	bzero(&d, sizeof(d));
150 	d.sin_family = AF_INET;
151 	d.sin_len = sizeof(struct sockaddr_in);
152 	d.sin_addr = ip->ip_dst;
153 
154 	match = NULL;
155 	matchprio = 0;
156 	for (ep = LIST_FIRST(&encaptab); ep; ep = LIST_NEXT(ep, chain)) {
157 		if (ep->af != AF_INET)
158 			continue;
159 		if (ep->proto >= 0 && ep->proto != proto)
160 			continue;
161 		if (ep->func)
162 			prio = (*ep->func)(m, off, proto, ep->arg);
163 		else {
164 			/*
165 			 * it's inbound traffic, we need to match in reverse
166 			 * order
167 			 */
168 			prio = mask_match(ep, (struct sockaddr *)&d,
169 			    (struct sockaddr *)&s);
170 		}
171 
172 		/*
173 		 * We prioritize the matches by using bit length of the
174 		 * matches.  mask_match() and user-supplied matching function
175 		 * should return the bit length of the matches (for example,
176 		 * if both src/dst are matched for IPv4, 64 should be returned).
177 		 * 0 or negative return value means "it did not match".
178 		 *
179 		 * The question is, since we have two "mask" portion, we
180 		 * cannot really define total order between entries.
181 		 * For example, which of these should be preferred?
182 		 * mask_match() returns 48 (32 + 16) for both of them.
183 		 *	src=3ffe::/16, dst=3ffe:501::/32
184 		 *	src=3ffe:501::/32, dst=3ffe::/16
185 		 *
186 		 * We need to loop through all the possible candidates
187 		 * to get the best match - the search takes O(n) for
188 		 * n attachments (i.e. interfaces).
189 		 */
190 		if (prio <= 0)
191 			continue;
192 		if (prio > matchprio) {
193 			matchprio = prio;
194 			match = ep;
195 		}
196 	}
197 
198 	if (match) {
199 		/* found a match, "match" has the best one */
200 		psw = (const struct ipprotosw *)match->psw;
201 		if (psw && psw->pr_input) {
202 			encap_fillarg(m, match);
203 			(*psw->pr_input)(m, off, proto);
204 		} else
205 			m_freem(m);
206 		return;
207 	}
208 
209 	/* for backward compatibility */
210 # ifdef MROUTING
211 #  define COMPATFUNC	ipip_input
212 # endif /*MROUTING*/
213 
214 #ifdef COMPATFUNC
215 	if (proto == IPPROTO_IPV4) {
216 		COMPATFUNC(m, off, proto);
217 		return;
218 	}
219 #endif
220 
221 	/* last resort: inject to raw socket */
222 	rip_input(m, off, proto);
223 }
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_NOWAIT);	/*XXX*/
355 	if (ep == NULL) {
356 		error = ENOBUFS;
357 		goto fail;
358 	}
359 	bzero(ep, sizeof(*ep));
360 
361 	ep->af = af;
362 	ep->proto = proto;
363 	bcopy(sp, &ep->src, sp->sa_len);
364 	bcopy(sm, &ep->srcmask, sp->sa_len);
365 	bcopy(dp, &ep->dst, dp->sa_len);
366 	bcopy(dm, &ep->dstmask, dp->sa_len);
367 	ep->psw = psw;
368 	ep->arg = arg;
369 
370 	encap_add(ep);
371 
372 	error = 0;
373 	splx(s);
374 	return ep;
375 
376 fail:
377 	splx(s);
378 	return NULL;
379 }
380 
381 const struct encaptab *
382 encap_attach_func(af, proto, func, psw, arg)
383 	int af;
384 	int proto;
385 	int (*func) __P((const struct mbuf *, int, int, void *));
386 	const struct protosw *psw;
387 	void *arg;
388 {
389 	struct encaptab *ep;
390 	int error;
391 	int s;
392 
393 	s = splnet();
394 	/* sanity check on args */
395 	if (!func) {
396 		error = EINVAL;
397 		goto fail;
398 	}
399 
400 	ep = malloc(sizeof(*ep), M_NETADDR, M_NOWAIT);	/*XXX*/
401 	if (ep == NULL) {
402 		error = ENOBUFS;
403 		goto fail;
404 	}
405 	bzero(ep, sizeof(*ep));
406 
407 	ep->af = af;
408 	ep->proto = proto;
409 	ep->func = func;
410 	ep->psw = psw;
411 	ep->arg = arg;
412 
413 	encap_add(ep);
414 
415 	error = 0;
416 	splx(s);
417 	return ep;
418 
419 fail:
420 	splx(s);
421 	return NULL;
422 }
423 
424 int
425 encap_detach(cookie)
426 	const struct encaptab *cookie;
427 {
428 	const struct encaptab *ep = cookie;
429 	struct encaptab *p;
430 
431 	for (p = LIST_FIRST(&encaptab); p; p = LIST_NEXT(p, chain)) {
432 		if (p == ep) {
433 			LIST_REMOVE(p, chain);
434 			free(p, M_NETADDR);	/*XXX*/
435 			return 0;
436 		}
437 	}
438 
439 	return EINVAL;
440 }
441 
442 static int
443 mask_match(ep, sp, dp)
444 	const struct encaptab *ep;
445 	const struct sockaddr *sp;
446 	const struct sockaddr *dp;
447 {
448 	struct sockaddr_storage s;
449 	struct sockaddr_storage d;
450 	int i;
451 	const u_int8_t *p, *q;
452 	u_int8_t *r;
453 	int matchlen;
454 
455 	if (sp->sa_len > sizeof(s) || dp->sa_len > sizeof(d))
456 		return 0;
457 	if (sp->sa_family != ep->af || dp->sa_family != ep->af)
458 		return 0;
459 	if (sp->sa_len != ep->src.ss_len || dp->sa_len != ep->dst.ss_len)
460 		return 0;
461 
462 	matchlen = 0;
463 
464 	p = (const u_int8_t *)sp;
465 	q = (const u_int8_t *)&ep->srcmask;
466 	r = (u_int8_t *)&s;
467 	for (i = 0 ; i < sp->sa_len; i++) {
468 		r[i] = p[i] & q[i];
469 		/* XXX estimate */
470 		matchlen += (q[i] ? 8 : 0);
471 	}
472 
473 	p = (const u_int8_t *)dp;
474 	q = (const u_int8_t *)&ep->dstmask;
475 	r = (u_int8_t *)&d;
476 	for (i = 0 ; i < dp->sa_len; i++) {
477 		r[i] = p[i] & q[i];
478 		/* XXX rough estimate */
479 		matchlen += (q[i] ? 8 : 0);
480 	}
481 
482 	/* need to overwrite len/family portion as we don't compare them */
483 	s.ss_len = sp->sa_len;
484 	s.ss_family = sp->sa_family;
485 	d.ss_len = dp->sa_len;
486 	d.ss_family = dp->sa_family;
487 
488 	if (bcmp(&s, &ep->src, ep->src.ss_len) == 0 &&
489 	    bcmp(&d, &ep->dst, ep->dst.ss_len) == 0) {
490 		return matchlen;
491 	} else
492 		return 0;
493 }
494 
495 static void
496 encap_fillarg(m, ep)
497 	struct mbuf *m;
498 	const struct encaptab *ep;
499 {
500 #if 0
501 	m->m_pkthdr.aux = ep->arg;
502 #else
503 	struct mbuf *n;
504 
505 	n = m_aux_add(m, AF_INET, IPPROTO_IPV4);
506 	if (n) {
507 		*mtod(n, void **) = ep->arg;
508 		n->m_len = sizeof(void *);
509 	}
510 #endif
511 }
512 
513 void *
514 encap_getarg(m)
515 	struct mbuf *m;
516 {
517 	void *p;
518 #if 0
519 	p = m->m_pkthdr.aux;
520 	m->m_pkthdr.aux = NULL;
521 	return p;
522 #else
523 	struct mbuf *n;
524 
525 	p = NULL;
526 	n = m_aux_find(m, AF_INET, IPPROTO_IPV4);
527 	if (n) {
528 		if (n->m_len == sizeof(void *))
529 			p = *mtod(n, void **);
530 		m_aux_delete(m, n);
531 	}
532 	return p;
533 #endif
534 }
535