xref: /dragonfly/sys/netinet/ip_encap.c (revision 2020c8fe)
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.15 2006/09/05 00:55:48 dillon 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 #include <sys/thread2.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 int     (*ipip_input)(struct mbuf **, int *, int); /* hook for mrouting */
110 
111 void
112 encap_init(void)
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 int
133 encap4_input(struct mbuf **mp, int *offp, int proto)
134 {
135 	struct mbuf *m = *mp;
136 	int off = *offp;
137 	struct ip *ip;
138 	struct sockaddr_in s, d;
139 	const struct protosw *psw;
140 	struct encaptab *ep, *match;
141 	int prio, matchprio;
142 
143 	ip = mtod(m, struct ip *);
144 	*mp = NULL;
145 
146 	bzero(&s, sizeof s);
147 	s.sin_family = AF_INET;
148 	s.sin_len = sizeof(struct sockaddr_in);
149 	s.sin_addr = ip->ip_src;
150 	bzero(&d, sizeof d);
151 	d.sin_family = AF_INET;
152 	d.sin_len = sizeof(struct sockaddr_in);
153 	d.sin_addr = ip->ip_dst;
154 
155 	match = NULL;
156 	matchprio = 0;
157 	for (ep = LIST_FIRST(&encaptab); ep; ep = LIST_NEXT(ep, chain)) {
158 		if (ep->af != AF_INET)
159 			continue;
160 		if (ep->proto >= 0 && ep->proto != proto)
161 			continue;
162 		if (ep->func)
163 			prio = (*ep->func)(m, off, proto, ep->arg);
164 		else {
165 			/*
166 			 * it's inbound traffic, we need to match in reverse
167 			 * order
168 			 */
169 			prio = mask_match(ep,
170 					  (struct sockaddr *)&d,
171 					  (struct sockaddr *)&s);
172 		}
173 
174 		/*
175 		 * We prioritize the matches by using bit length of the
176 		 * matches.  mask_match() and user-supplied matching function
177 		 * should return the bit length of the matches (for example,
178 		 * if both src/dst are matched for IPv4, 64 should be returned).
179 		 * 0 or negative return value means "it did not match".
180 		 *
181 		 * The question is, since we have two "mask" portion, we
182 		 * cannot really define total order between entries.
183 		 * For example, which of these should be preferred?
184 		 * mask_match() returns 48 (32 + 16) for both of them.
185 		 *	src=3ffe::/16, dst=3ffe:501::/32
186 		 *	src=3ffe:501::/32, dst=3ffe::/16
187 		 *
188 		 * We need to loop through all the possible candidates
189 		 * to get the best match - the search takes O(n) for
190 		 * n attachments (i.e. interfaces).
191 		 */
192 		if (prio <= 0)
193 			continue;
194 		if (prio > matchprio) {
195 			matchprio = prio;
196 			match = ep;
197 		}
198 	}
199 
200 	if (match) {
201 		/* found a match, "match" has the best one */
202 		psw = match->psw;
203 		if (psw && psw->pr_input) {
204 			encap_fillarg(m, match);
205 			*mp = m;
206 			(*psw->pr_input)(mp, offp, proto);
207 		} else {
208 			m_freem(m);
209 		}
210 		return(IPPROTO_DONE);
211 	}
212 
213 	/* for backward compatibility */
214 	if (proto == IPPROTO_IPV4 && ipip_input) {
215 		*mp = m;
216 		ipip_input(mp, offp, proto);
217 		return(IPPROTO_DONE);
218 	}
219 
220 	/* last resort: inject to raw socket */
221 	*mp = m;
222 	rip_input(mp, offp, proto);
223 	return(IPPROTO_DONE);
224 }
225 #endif
226 
227 #ifdef INET6
228 int
229 encap6_input(struct mbuf **mp, int *offp, int proto)
230 {
231 	struct mbuf *m = *mp;
232 	struct ip6_hdr *ip6;
233 	struct sockaddr_in6 s, d;
234 	const struct protosw *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 = 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(struct encaptab *ep)
295 {
296 
297 	LIST_INSERT_HEAD(&encaptab, ep, chain);
298 }
299 
300 /*
301  * sp (src ptr) is always my side, and dp (dst ptr) is always remote side.
302  * length of mask (sm and dm) is assumed to be same as sp/dp.
303  * Return value will be necessary as input (cookie) for encap_detach().
304  */
305 const struct encaptab *
306 encap_attach(int af, int proto, const struct sockaddr *sp,
307 	     const struct sockaddr *sm, const struct sockaddr *dp,
308 	     const struct sockaddr *dm, const struct protosw *psw, void *arg)
309 {
310 	struct encaptab *ep;
311 	int error;
312 
313 	crit_enter();
314 	/* sanity check on args */
315 	if (sp->sa_len > sizeof ep->src || dp->sa_len > sizeof ep->dst) {
316 		error = EINVAL;
317 		goto fail;
318 	}
319 	if (sp->sa_len != dp->sa_len) {
320 		error = EINVAL;
321 		goto fail;
322 	}
323 	if (af != sp->sa_family || af != dp->sa_family) {
324 		error = EINVAL;
325 		goto fail;
326 	}
327 
328 	/* check if anyone have already attached with exactly same config */
329 	for (ep = LIST_FIRST(&encaptab); ep; ep = LIST_NEXT(ep, chain)) {
330 		if (ep->af != af)
331 			continue;
332 		if (ep->proto != proto)
333 			continue;
334 		if (ep->src.ss_len != sp->sa_len ||
335 		    bcmp(&ep->src, sp, sp->sa_len) != 0 ||
336 		    bcmp(&ep->srcmask, sm, sp->sa_len) != 0)
337 			continue;
338 		if (ep->dst.ss_len != dp->sa_len ||
339 		    bcmp(&ep->dst, dp, dp->sa_len) != 0 ||
340 		    bcmp(&ep->dstmask, dm, dp->sa_len) != 0)
341 			continue;
342 
343 		error = EEXIST;
344 		goto fail;
345 	}
346 
347 	ep = kmalloc(sizeof *ep, M_NETADDR, M_INTWAIT | M_ZERO | M_NULLOK);
348 	if (ep == NULL) {
349 		error = ENOBUFS;
350 		goto fail;
351 	}
352 
353 	ep->af = af;
354 	ep->proto = proto;
355 	bcopy(sp, &ep->src, sp->sa_len);
356 	bcopy(sm, &ep->srcmask, sp->sa_len);
357 	bcopy(dp, &ep->dst, dp->sa_len);
358 	bcopy(dm, &ep->dstmask, dp->sa_len);
359 	ep->psw = psw;
360 	ep->arg = arg;
361 
362 	encap_add(ep);
363 
364 	error = 0;
365 	crit_exit();
366 	return ep;
367 
368 fail:
369 	crit_exit();
370 	return NULL;
371 }
372 
373 const struct encaptab *
374 encap_attach_func(int af, int proto,
375 		  int (*func)(const struct mbuf *, int, int, void *),
376 		  const struct protosw *psw, void *arg)
377 {
378 	struct encaptab *ep;
379 	int error;
380 
381 	crit_enter();
382 	/* sanity check on args */
383 	if (!func) {
384 		error = EINVAL;
385 		goto fail;
386 	}
387 
388 	ep = kmalloc(sizeof *ep, M_NETADDR, M_INTWAIT | M_ZERO | M_NULLOK);
389 	if (ep == NULL) {
390 		error = ENOBUFS;
391 		goto fail;
392 	}
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 	crit_exit();
404 	return ep;
405 
406 fail:
407 	crit_exit();
408 	return NULL;
409 }
410 
411 int
412 encap_detach(const struct encaptab *cookie)
413 {
414 	const struct encaptab *ep = cookie;
415 	struct encaptab *p;
416 
417 	for (p = LIST_FIRST(&encaptab); p; p = LIST_NEXT(p, chain)) {
418 		if (p == ep) {
419 			LIST_REMOVE(p, chain);
420 			kfree(p, M_NETADDR);	/*XXX*/
421 			return 0;
422 		}
423 	}
424 
425 	return EINVAL;
426 }
427 
428 static int
429 mask_match(const struct encaptab *ep, const struct sockaddr *sp,
430 	   const struct sockaddr *dp)
431 {
432 	struct sockaddr_storage s;
433 	struct sockaddr_storage d;
434 	int i;
435 	const u_int8_t *p, *q;
436 	u_int8_t *r;
437 	int matchlen;
438 
439 	if (sp->sa_len > sizeof s || dp->sa_len > sizeof d)
440 		return 0;
441 	if (sp->sa_family != ep->af || dp->sa_family != ep->af)
442 		return 0;
443 	if (sp->sa_len != ep->src.ss_len || dp->sa_len != ep->dst.ss_len)
444 		return 0;
445 
446 	matchlen = 0;
447 
448 	p = (const u_int8_t *)sp;
449 	q = (const u_int8_t *)&ep->srcmask;
450 	r = (u_int8_t *)&s;
451 	for (i = 0 ; i < sp->sa_len; i++) {
452 		r[i] = p[i] & q[i];
453 		/* XXX estimate */
454 		matchlen += (q[i] ? 8 : 0);
455 	}
456 
457 	p = (const u_int8_t *)dp;
458 	q = (const u_int8_t *)&ep->dstmask;
459 	r = (u_int8_t *)&d;
460 	for (i = 0 ; i < dp->sa_len; i++) {
461 		r[i] = p[i] & q[i];
462 		/* XXX rough estimate */
463 		matchlen += (q[i] ? 8 : 0);
464 	}
465 
466 	/* need to overwrite len/family portion as we don't compare them */
467 	s.ss_len = sp->sa_len;
468 	s.ss_family = sp->sa_family;
469 	d.ss_len = dp->sa_len;
470 	d.ss_family = dp->sa_family;
471 
472 	if (bcmp(&s, &ep->src, ep->src.ss_len) == 0 &&
473 	    bcmp(&d, &ep->dst, ep->dst.ss_len) == 0) {
474 		return matchlen;
475 	} else
476 		return 0;
477 }
478 
479 static void
480 encap_fillarg(struct mbuf *m, const struct encaptab *ep)
481 {
482 	struct m_tag *tag;
483 
484 	tag = m_tag_get(PACKET_TAG_ENCAP, sizeof(void *), MB_DONTWAIT);
485 	if (tag != NULL) {
486 		*(void **)m_tag_data(tag) = ep->arg;
487 		m_tag_prepend(m, tag);
488 	}
489 }
490 
491 void *
492 encap_getarg(struct mbuf *m)
493 {
494 	void *p = NULL;
495 	struct m_tag *tag;
496 
497 	tag = m_tag_find(m, PACKET_TAG_ENCAP, NULL);
498 	if (tag != NULL) {
499 		p = *(void **)m_tag_data(tag);
500 		m_tag_delete(m, tag);
501 	}
502 	return p;
503 }
504