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