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