]> git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/ip_encap.c
xnu-2050.18.24.tar.gz
[apple/xnu.git] / bsd / netinet / ip_encap.c
1 /*
2 * Copyright (c) 2000-2011 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
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
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
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 $ */
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:
72 * RFC1853 IPv4-in-IPv4 tunnelling
73 * RFC2003 IPv4 encapsulation within IPv4
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 */
84 /* XXX is M_NETADDR correct? */
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/mcache.h>
92 #include <sys/errno.h>
93 #include <sys/protosw.h>
94 #include <sys/queue.h>
95
96 #include <net/if.h>
97 #include <net/route.h>
98
99 #include <netinet/in.h>
100 #include <netinet/in_systm.h>
101 #include <netinet/ip.h>
102 #include <netinet/ip_var.h>
103 #include <netinet/ip_encap.h>
104 #if MROUTING
105 #include <netinet/ip_mroute.h>
106 #endif /* MROUTING */
107
108 #if INET6
109 #include <netinet/ip6.h>
110 #include <netinet6/ip6_var.h>
111 #include <netinet6/ip6protosw.h>
112 #endif
113
114
115 #include <net/net_osdep.h>
116
117 #ifndef __APPLE__
118 #include <sys/kernel.h>
119 #include <sys/malloc.h>
120 MALLOC_DEFINE(M_NETADDR, "Export Host", "Export host address structure");
121 #endif
122
123 static void encap_add(struct encaptab *);
124 static int mask_match(const struct encaptab *, const struct sockaddr *,
125 const struct sockaddr *);
126 static void encap_fillarg(struct mbuf *, const struct encaptab *);
127
128 #ifndef LIST_HEAD_INITIALIZER
129 /* rely upon BSS initialization */
130 LIST_HEAD(, encaptab) encaptab;
131 #else
132 LIST_HEAD(, encaptab) encaptab = LIST_HEAD_INITIALIZER(&encaptab);
133 #endif
134
135 void
136 encap_init()
137 {
138 static int initialized = 0;
139
140 if (initialized)
141 return;
142 initialized++;
143 #if 0
144 /*
145 * we cannot use LIST_INIT() here, since drivers may want to call
146 * encap_attach(), on driver attach. encap_init() will be called
147 * on AF_INET{,6} initialization, which happens after driver
148 * initialization - using LIST_INIT() here can nuke encap_attach()
149 * from drivers.
150 */
151 LIST_INIT(&encaptab);
152 #endif
153 }
154
155 #if INET
156 void
157 encap4_input(m, off)
158 struct mbuf *m;
159 int off;
160 {
161 int proto;
162 struct ip *ip;
163 struct sockaddr_in s, d;
164 const struct protosw *psw;
165 struct encaptab *ep, *match;
166 int prio, matchprio;
167
168 #ifndef __APPLE__
169 va_start(ap, m);
170 off = va_arg(ap, int);
171 proto = va_arg(ap, int);
172 va_end(ap);
173 #endif
174
175 /* Expect 32-bit aligned data pointer on strict-align platforms */
176 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m);
177
178 ip = mtod(m, struct ip *);
179 #ifdef __APPLE__
180 proto = ip->ip_p;
181 #endif
182
183 bzero(&s, sizeof(s));
184 s.sin_family = AF_INET;
185 s.sin_len = sizeof(struct sockaddr_in);
186 s.sin_addr = ip->ip_src;
187 bzero(&d, sizeof(d));
188 d.sin_family = AF_INET;
189 d.sin_len = sizeof(struct sockaddr_in);
190 d.sin_addr = ip->ip_dst;
191
192 match = NULL;
193 matchprio = 0;
194 for (ep = LIST_FIRST(&encaptab); ep; ep = LIST_NEXT(ep, chain)) {
195 if (ep->af != AF_INET)
196 continue;
197 if (ep->proto >= 0 && ep->proto != proto)
198 continue;
199 if (ep->func)
200 prio = (*ep->func)(m, off, proto, ep->arg);
201 else {
202 /*
203 * it's inbound traffic, we need to match in reverse
204 * order
205 */
206 prio = mask_match(ep, (struct sockaddr *)&d,
207 (struct sockaddr *)&s);
208 }
209
210 /*
211 * We prioritize the matches by using bit length of the
212 * matches. mask_match() and user-supplied matching function
213 * should return the bit length of the matches (for example,
214 * if both src/dst are matched for IPv4, 64 should be returned).
215 * 0 or negative return value means "it did not match".
216 *
217 * The question is, since we have two "mask" portion, we
218 * cannot really define total order between entries.
219 * For example, which of these should be preferred?
220 * mask_match() returns 48 (32 + 16) for both of them.
221 * src=3ffe::/16, dst=3ffe:501::/32
222 * src=3ffe:501::/32, dst=3ffe::/16
223 *
224 * We need to loop through all the possible candidates
225 * to get the best match - the search takes O(n) for
226 * n attachments (i.e. interfaces).
227 */
228 if (prio <= 0)
229 continue;
230 if (prio > matchprio) {
231 matchprio = prio;
232 match = ep;
233 }
234 }
235
236 if (match) {
237 /* found a match, "match" has the best one */
238 psw = (const struct protosw *)match->psw;
239 if (psw && psw->pr_input) {
240 encap_fillarg(m, match);
241 (*psw->pr_input)(m, off);
242 } else
243 m_freem(m);
244 return;
245 }
246
247 /* for backward compatibility */
248 # if MROUTING
249 # define COMPATFUNC ipip_input
250 # endif /*MROUTING*/
251
252 #if COMPATFUNC
253 if (proto == IPPROTO_IPV4) {
254 COMPATFUNC(m, off);
255 return;
256 }
257 #endif
258
259 /* last resort: inject to raw socket */
260 rip_input(m, off);
261 }
262 #endif
263
264 #if INET6
265 int
266 encap6_input(struct mbuf **mp, int *offp, int proto)
267 {
268 struct mbuf *m = *mp;
269 struct ip6_hdr *ip6;
270 struct sockaddr_in6 s, d;
271 const struct ip6protosw *psw;
272 struct encaptab *ep, *match;
273 int prio, matchprio;
274
275 /* Expect 32-bit aligned data pointer on strict-align platforms */
276 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m);
277
278 ip6 = mtod(m, struct ip6_hdr *);
279 bzero(&s, sizeof(s));
280 s.sin6_family = AF_INET6;
281 s.sin6_len = sizeof(struct sockaddr_in6);
282 s.sin6_addr = ip6->ip6_src;
283 bzero(&d, sizeof(d));
284 d.sin6_family = AF_INET6;
285 d.sin6_len = sizeof(struct sockaddr_in6);
286 d.sin6_addr = ip6->ip6_dst;
287
288 match = NULL;
289 matchprio = 0;
290 for (ep = LIST_FIRST(&encaptab); ep; ep = LIST_NEXT(ep, chain)) {
291 if (ep->af != AF_INET6)
292 continue;
293 if (ep->proto >= 0 && ep->proto != proto)
294 continue;
295 if (ep->func)
296 prio = (*ep->func)(m, *offp, proto, ep->arg);
297 else {
298 /*
299 * it's inbound traffic, we need to match in reverse
300 * order
301 */
302 prio = mask_match(ep, (struct sockaddr *)&d,
303 (struct sockaddr *)&s);
304 }
305
306 /* see encap4_input() for issues here */
307 if (prio <= 0)
308 continue;
309 if (prio > matchprio) {
310 matchprio = prio;
311 match = ep;
312 }
313 }
314
315 if (match) {
316 /* found a match */
317 psw = (const struct ip6protosw *)match->psw;
318 if (psw && psw->pr_input) {
319 encap_fillarg(m, match);
320 return (*psw->pr_input)(mp, offp, proto);
321 } else {
322 m_freem(m);
323 return IPPROTO_DONE;
324 }
325 }
326
327 /* last resort: inject to raw socket */
328 return rip6_input(mp, offp, proto);
329 }
330 #endif
331
332 static void
333 encap_add(ep)
334 struct encaptab *ep;
335 {
336
337 LIST_INSERT_HEAD(&encaptab, ep, chain);
338 }
339
340 /*
341 * sp (src ptr) is always my side, and dp (dst ptr) is always remote side.
342 * length of mask (sm and dm) is assumed to be same as sp/dp.
343 * Return value will be necessary as input (cookie) for encap_detach().
344 */
345 const struct encaptab *
346 encap_attach(af, proto, sp, sm, dp, dm, psw, arg)
347 int af;
348 int proto;
349 const struct sockaddr *sp, *sm;
350 const struct sockaddr *dp, *dm;
351 const struct protosw *psw;
352 void *arg;
353 {
354 struct encaptab *ep;
355 int error;
356
357 /* sanity check on args */
358 if (sp->sa_len > sizeof(ep->src) || dp->sa_len > sizeof(ep->dst)) {
359 error = EINVAL;
360 goto fail;
361 }
362 if (sp->sa_len != dp->sa_len) {
363 error = EINVAL;
364 goto fail;
365 }
366 if (af != sp->sa_family || af != dp->sa_family) {
367 error = EINVAL;
368 goto fail;
369 }
370
371 /* check if anyone have already attached with exactly same config */
372 for (ep = LIST_FIRST(&encaptab); ep; ep = LIST_NEXT(ep, chain)) {
373 if (ep->af != af)
374 continue;
375 if (ep->proto != proto)
376 continue;
377 if (ep->src.ss_len != sp->sa_len ||
378 bcmp(&ep->src, sp, sp->sa_len) != 0 ||
379 bcmp(&ep->srcmask, sm, sp->sa_len) != 0)
380 continue;
381 if (ep->dst.ss_len != dp->sa_len ||
382 bcmp(&ep->dst, dp, dp->sa_len) != 0 ||
383 bcmp(&ep->dstmask, dm, dp->sa_len) != 0)
384 continue;
385
386 error = EEXIST;
387 goto fail;
388 }
389
390 ep = _MALLOC(sizeof(*ep), M_NETADDR, M_WAITOK); /*XXX*/
391 if (ep == NULL) {
392 error = ENOBUFS;
393 goto fail;
394 }
395 bzero(ep, sizeof(*ep));
396
397 ep->af = af;
398 ep->proto = proto;
399 bcopy(sp, &ep->src, sp->sa_len);
400 bcopy(sm, &ep->srcmask, sp->sa_len);
401 bcopy(dp, &ep->dst, dp->sa_len);
402 bcopy(dm, &ep->dstmask, dp->sa_len);
403 ep->psw = psw;
404 ep->arg = arg;
405
406 encap_add(ep);
407
408 error = 0;
409 return ep;
410
411 fail:
412 return NULL;
413 }
414
415 const struct encaptab *
416 encap_attach_func(af, proto, func, psw, arg)
417 int af;
418 int proto;
419 int (*func)(const struct mbuf *, int, int, void *);
420 const struct protosw *psw;
421 void *arg;
422 {
423 struct encaptab *ep;
424 int error;
425
426 /* sanity check on args */
427 if (!func) {
428 error = EINVAL;
429 goto fail;
430 }
431
432 ep = _MALLOC(sizeof(*ep), M_NETADDR, M_WAITOK); /*XXX*/
433 if (ep == NULL) {
434 error = ENOBUFS;
435 goto fail;
436 }
437 bzero(ep, sizeof(*ep));
438
439 ep->af = af;
440 ep->proto = proto;
441 ep->func = func;
442 ep->psw = psw;
443 ep->arg = arg;
444
445 encap_add(ep);
446
447 error = 0;
448 return ep;
449
450 fail:
451 return NULL;
452 }
453
454 int
455 encap_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
472 static int
473 mask_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;
481 const u_int8_t *p, *q;
482 u_int8_t *r;
483 int matchlen;
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
492 matchlen = 0;
493
494 p = (const u_int8_t *)sp;
495 q = (const u_int8_t *)&ep->srcmask;
496 r = (u_int8_t *)&s;
497 for (i = 0 ; i < sp->sa_len; i++) {
498 r[i] = p[i] & q[i];
499 /* XXX estimate */
500 matchlen += (q[i] ? 8 : 0);
501 }
502
503 p = (const u_int8_t *)dp;
504 q = (const u_int8_t *)&ep->dstmask;
505 r = (u_int8_t *)&d;
506 for (i = 0 ; i < dp->sa_len; i++) {
507 r[i] = p[i] & q[i];
508 /* XXX rough estimate */
509 matchlen += (q[i] ? 8 : 0);
510 }
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) {
520 return matchlen;
521 } else
522 return 0;
523 }
524
525 struct encaptabtag {
526 void* *arg;
527 };
528
529 static void
530 encap_fillarg(
531 struct mbuf *m,
532 const struct encaptab *ep)
533 {
534 struct m_tag *tag;
535 struct encaptabtag *et;
536
537 tag = m_tag_create(KERNEL_MODULE_TAG_ID, KERNEL_TAG_TYPE_ENCAP,
538 sizeof(struct encaptabtag), M_WAITOK, m);
539
540 if (tag != NULL) {
541 et = (struct encaptabtag*)(tag + 1);
542 et->arg = ep->arg;
543 m_tag_prepend(m, tag);
544 }
545 }
546
547 void *
548 encap_getarg(m)
549 struct mbuf *m;
550 {
551 struct m_tag *tag;
552 struct encaptabtag *et;
553 void *p = NULL;
554
555 tag = m_tag_locate(m, KERNEL_MODULE_TAG_ID, KERNEL_TAG_TYPE_ENCAP, NULL);
556 if (tag) {
557 et = (struct encaptabtag*)(tag + 1);
558 p = et->arg;
559 m_tag_delete(m, tag);
560 }
561
562 return p;
563 }