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