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