]> git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/ip_encap.c
xnu-7195.81.3.tar.gz
[apple/xnu.git] / bsd / netinet / ip_encap.c
1 /*
2 * Copyright (c) 2000-2016 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 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/domain.h>
94 #include <sys/protosw.h>
95 #include <sys/queue.h>
96
97 #include <net/if.h>
98 #include <net/route.h>
99
100 #include <netinet/in.h>
101 #include <netinet/in_systm.h>
102 #include <netinet/ip.h>
103 #include <netinet/ip_var.h>
104 #include <netinet/ip_encap.h>
105
106 #include <netinet/ip6.h>
107 #include <netinet6/ip6_var.h>
108 #include <netinet6/ip6protosw.h>
109
110 #include <net/net_osdep.h>
111
112 #ifndef __APPLE__
113 #include <sys/kernel.h>
114 #include <sys/malloc.h>
115 MALLOC_DEFINE(M_NETADDR, "Export Host", "Export host address structure");
116 #endif
117
118 static void encap_init(struct protosw *, struct domain *);
119 static void encap_add_locked(struct encaptab *);
120 static int mask_match(const struct encaptab *, const struct sockaddr *,
121 const struct sockaddr *);
122 static void encap_fillarg(struct mbuf *, void *arg);
123
124 #ifndef LIST_HEAD_INITIALIZER
125 /* rely upon BSS initialization */
126 LIST_HEAD(, encaptab) encaptab;
127 #else
128 LIST_HEAD(, encaptab) encaptab = LIST_HEAD_INITIALIZER(&encaptab);
129 #endif
130
131 decl_lck_rw_data(static, encaptab_lock);
132
133 static void
134 encap_init(struct protosw *pp, struct domain *dp)
135 {
136 #pragma unused(dp)
137 static int encap_initialized = 0;
138 lck_grp_attr_t *encaptab_grp_attrib = NULL;
139 lck_attr_t *encaptab_lck_attrib = NULL;
140 lck_grp_t *encaptab_lck_group = NULL;
141
142 VERIFY((pp->pr_flags & (PR_INITIALIZED | PR_ATTACHED)) == PR_ATTACHED);
143
144 /* This gets called by more than one protocols, so initialize once */
145 if (encap_initialized) {
146 return;
147 }
148
149 encaptab_grp_attrib = lck_grp_attr_alloc_init();
150 encaptab_lck_group = lck_grp_alloc_init("encaptab lock", encaptab_grp_attrib);
151 lck_grp_attr_free(encaptab_grp_attrib);
152
153 encaptab_lck_attrib = lck_attr_alloc_init();
154 lck_rw_init(&encaptab_lock, encaptab_lck_group, encaptab_lck_attrib);
155
156 lck_grp_free(encaptab_lck_group);
157 lck_attr_free(encaptab_lck_attrib);
158
159 encap_initialized = 1;
160 #if 0
161 /*
162 * we cannot use LIST_INIT() here, since drivers may want to call
163 * encap_attach(), on driver attach. encap_init() will be called
164 * on AF_INET{,6} initialization, which happens after driver
165 * initialization - using LIST_INIT() here can nuke encap_attach()
166 * from drivers.
167 */
168 LIST_INIT(&encaptab);
169 #endif
170 }
171
172 void
173 encap4_init(struct protosw *pp, struct domain *dp)
174 {
175 encap_init(pp, dp);
176 }
177
178 void
179 encap6_init(struct ip6protosw *pp, struct domain *dp)
180 {
181 encap_init((struct protosw *)pp, dp);
182 }
183
184 #if INET
185 void
186 encap4_input(struct mbuf *m, int off)
187 {
188 int proto;
189 struct ip *ip;
190 struct sockaddr_in s, d;
191 const struct protosw *psw;
192 struct encaptab *ep, *match;
193 int prio, matchprio;
194 void *match_arg = NULL;
195
196 #ifndef __APPLE__
197 va_start(ap, m);
198 off = va_arg(ap, int);
199 proto = va_arg(ap, int);
200 va_end(ap);
201 #endif
202
203 /* Expect 32-bit aligned data pointer on strict-align platforms */
204 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m);
205
206 ip = mtod(m, struct ip *);
207 #ifdef __APPLE__
208 proto = ip->ip_p;
209 #endif
210
211 bzero(&s, sizeof(s));
212 s.sin_family = AF_INET;
213 s.sin_len = sizeof(struct sockaddr_in);
214 s.sin_addr = ip->ip_src;
215 bzero(&d, sizeof(d));
216 d.sin_family = AF_INET;
217 d.sin_len = sizeof(struct sockaddr_in);
218 d.sin_addr = ip->ip_dst;
219
220 match = NULL;
221 matchprio = 0;
222
223 lck_rw_lock_shared(&encaptab_lock);
224 for (ep = LIST_FIRST(&encaptab); ep; ep = LIST_NEXT(ep, chain)) {
225 if (ep->af != AF_INET) {
226 continue;
227 }
228 if (ep->proto >= 0 && ep->proto != proto) {
229 continue;
230 }
231 if (ep->func) {
232 prio = (*ep->func)(m, off, proto, ep->arg);
233 } else {
234 /*
235 * it's inbound traffic, we need to match in reverse
236 * order
237 */
238 prio = mask_match(ep, (struct sockaddr *)&d,
239 (struct sockaddr *)&s);
240 }
241
242 /*
243 * We prioritize the matches by using bit length of the
244 * matches. mask_match() and user-supplied matching function
245 * should return the bit length of the matches (for example,
246 * if both src/dst are matched for IPv4, 64 should be returned).
247 * 0 or negative return value means "it did not match".
248 *
249 * The question is, since we have two "mask" portion, we
250 * cannot really define total order between entries.
251 * For example, which of these should be preferred?
252 * mask_match() returns 48 (32 + 16) for both of them.
253 * src=3ffe::/16, dst=3ffe:501::/32
254 * src=3ffe:501::/32, dst=3ffe::/16
255 *
256 * We need to loop through all the possible candidates
257 * to get the best match - the search takes O(n) for
258 * n attachments (i.e. interfaces).
259 */
260 if (prio <= 0) {
261 continue;
262 }
263 if (prio > matchprio) {
264 matchprio = prio;
265 match = ep;
266 psw = (const struct protosw *)match->psw;
267 match_arg = ep->arg;
268 }
269 }
270 lck_rw_unlock_shared(&encaptab_lock);
271
272 if (match) {
273 /* found a match, "match" has the best one */
274 if (psw && psw->pr_input) {
275 encap_fillarg(m, match_arg);
276 (*psw->pr_input)(m, off);
277 } else {
278 m_freem(m);
279 }
280 return;
281 }
282
283 /* last resort: inject to raw socket */
284 rip_input(m, off);
285 }
286 #endif
287
288 int
289 encap6_input(struct mbuf **mp, int *offp, int proto)
290 {
291 struct mbuf *m = *mp;
292 struct ip6_hdr *ip6;
293 struct sockaddr_in6 s, d;
294 const struct ip6protosw *psw;
295 struct encaptab *ep, *match;
296 int prio, matchprio;
297 void *match_arg = NULL;
298
299 /* Expect 32-bit aligned data pointer on strict-align platforms */
300 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m);
301
302 ip6 = mtod(m, struct ip6_hdr *);
303 bzero(&s, sizeof(s));
304 s.sin6_family = AF_INET6;
305 s.sin6_len = sizeof(struct sockaddr_in6);
306 s.sin6_addr = ip6->ip6_src;
307 bzero(&d, sizeof(d));
308 d.sin6_family = AF_INET6;
309 d.sin6_len = sizeof(struct sockaddr_in6);
310 d.sin6_addr = ip6->ip6_dst;
311
312 match = NULL;
313 matchprio = 0;
314
315 lck_rw_lock_shared(&encaptab_lock);
316 for (ep = LIST_FIRST(&encaptab); ep; ep = LIST_NEXT(ep, chain)) {
317 if (ep->af != AF_INET6) {
318 continue;
319 }
320 if (ep->proto >= 0 && ep->proto != proto) {
321 continue;
322 }
323 if (ep->func) {
324 prio = (*ep->func)(m, *offp, proto, ep->arg);
325 } else {
326 /*
327 * it's inbound traffic, we need to match in reverse
328 * order
329 */
330 prio = mask_match(ep, (struct sockaddr *)&d,
331 (struct sockaddr *)&s);
332 }
333
334 /* see encap4_input() for issues here */
335 if (prio <= 0) {
336 continue;
337 }
338 if (prio > matchprio) {
339 matchprio = prio;
340 match = ep;
341 psw = (const struct ip6protosw *)match->psw;
342 match_arg = ep->arg;
343 }
344 }
345 lck_rw_unlock_shared(&encaptab_lock);
346
347 if (match) {
348 /* found a match */
349 if (psw && psw->pr_input) {
350 encap_fillarg(m, match_arg);
351 return (*psw->pr_input)(mp, offp, proto);
352 } else {
353 m_freem(m);
354 return IPPROTO_DONE;
355 }
356 }
357
358 /* last resort: inject to raw socket */
359 return rip6_input(mp, offp, proto);
360 }
361
362 static void
363 encap_add_locked(struct encaptab *ep)
364 {
365 LCK_RW_ASSERT(&encaptab_lock, LCK_RW_ASSERT_EXCLUSIVE);
366 LIST_INSERT_HEAD(&encaptab, ep, chain);
367 }
368
369 /*
370 * sp (src ptr) is always my side, and dp (dst ptr) is always remote side.
371 * length of mask (sm and dm) is assumed to be same as sp/dp.
372 * Return value will be necessary as input (cookie) for encap_detach().
373 */
374 const struct encaptab *
375 encap_attach(int af, int proto, const struct sockaddr *sp,
376 const struct sockaddr *sm, const struct sockaddr *dp,
377 const struct sockaddr *dm, const struct protosw *psw, void *arg)
378 {
379 struct encaptab *ep = NULL;
380 struct encaptab *new_ep = NULL;
381 int error;
382
383 /* sanity check on args */
384 if (sp->sa_len > sizeof(new_ep->src) || dp->sa_len > sizeof(new_ep->dst)) {
385 error = EINVAL;
386 goto fail;
387 }
388 if (sp->sa_len != dp->sa_len) {
389 error = EINVAL;
390 goto fail;
391 }
392 if (af != sp->sa_family || af != dp->sa_family) {
393 error = EINVAL;
394 goto fail;
395 }
396
397 new_ep = _MALLOC(sizeof(*new_ep), M_NETADDR, M_WAITOK | M_ZERO);
398 if (new_ep == NULL) {
399 error = ENOBUFS;
400 goto fail;
401 }
402
403 /* check if anyone have already attached with exactly same config */
404 lck_rw_lock_exclusive(&encaptab_lock);
405 for (ep = LIST_FIRST(&encaptab); ep; ep = LIST_NEXT(ep, chain)) {
406 if (ep->af != af) {
407 continue;
408 }
409 if (ep->proto != proto) {
410 continue;
411 }
412 if (ep->src.ss_len != sp->sa_len ||
413 bcmp(&ep->src, sp, sp->sa_len) != 0 ||
414 bcmp(&ep->srcmask, sm, sp->sa_len) != 0) {
415 continue;
416 }
417 if (ep->dst.ss_len != dp->sa_len ||
418 bcmp(&ep->dst, dp, dp->sa_len) != 0 ||
419 bcmp(&ep->dstmask, dm, dp->sa_len) != 0) {
420 continue;
421 }
422
423 error = EEXIST;
424 goto fail_locked;
425 }
426
427 new_ep->af = af;
428 new_ep->proto = proto;
429 bcopy(sp, &new_ep->src, sp->sa_len);
430 bcopy(sm, &new_ep->srcmask, sp->sa_len);
431 bcopy(dp, &new_ep->dst, dp->sa_len);
432 bcopy(dm, &new_ep->dstmask, dp->sa_len);
433 new_ep->psw = psw;
434 new_ep->arg = arg;
435
436 encap_add_locked(new_ep);
437 lck_rw_unlock_exclusive(&encaptab_lock);
438
439 error = 0;
440 return new_ep;
441
442 fail_locked:
443 lck_rw_unlock_exclusive(&encaptab_lock);
444 if (new_ep != NULL) {
445 _FREE(new_ep, M_NETADDR);
446 }
447 fail:
448 return NULL;
449 }
450
451 const struct encaptab *
452 encap_attach_func( int af, int proto,
453 int (*func)(const struct mbuf *, int, int, void *),
454 const struct protosw *psw, void *arg)
455 {
456 struct encaptab *ep;
457 int error;
458
459 /* sanity check on args */
460 if (!func) {
461 error = EINVAL;
462 goto fail;
463 }
464
465 ep = _MALLOC(sizeof(*ep), M_NETADDR, M_WAITOK | M_ZERO); /* XXX */
466 if (ep == NULL) {
467 error = ENOBUFS;
468 goto fail;
469 }
470
471 ep->af = af;
472 ep->proto = proto;
473 ep->func = func;
474 ep->psw = psw;
475 ep->arg = arg;
476
477 lck_rw_lock_exclusive(&encaptab_lock);
478 encap_add_locked(ep);
479 lck_rw_unlock_exclusive(&encaptab_lock);
480
481 error = 0;
482 return ep;
483
484 fail:
485 return NULL;
486 }
487
488 int
489 encap_detach(const struct encaptab *cookie)
490 {
491 const struct encaptab *ep = cookie;
492 struct encaptab *p;
493
494 lck_rw_lock_exclusive(&encaptab_lock);
495 for (p = LIST_FIRST(&encaptab); p; p = LIST_NEXT(p, chain)) {
496 if (p == ep) {
497 LIST_REMOVE(p, chain);
498 lck_rw_unlock_exclusive(&encaptab_lock);
499 _FREE(p, M_NETADDR); /*XXX*/
500 return 0;
501 }
502 }
503 lck_rw_unlock_exclusive(&encaptab_lock);
504
505 return EINVAL;
506 }
507
508 static int
509 mask_match(const struct encaptab *ep, const struct sockaddr *sp,
510 const struct sockaddr *dp)
511 {
512 struct sockaddr_storage s;
513 struct sockaddr_storage d;
514 int i;
515 const u_int8_t *p, *q;
516 u_int8_t *r;
517 int matchlen;
518
519 if (sp->sa_len > sizeof(s) || dp->sa_len > sizeof(d)) {
520 return 0;
521 }
522 if (sp->sa_family != ep->af || dp->sa_family != ep->af) {
523 return 0;
524 }
525 if (sp->sa_len != ep->src.ss_len || dp->sa_len != ep->dst.ss_len) {
526 return 0;
527 }
528
529 matchlen = 0;
530
531 p = (const u_int8_t *)sp;
532 q = (const u_int8_t *)&ep->srcmask;
533 r = (u_int8_t *)&s;
534 for (i = 0; i < sp->sa_len; i++) {
535 r[i] = p[i] & q[i];
536 /* XXX estimate */
537 matchlen += (q[i] ? 8 : 0);
538 }
539
540 p = (const u_int8_t *)dp;
541 q = (const u_int8_t *)&ep->dstmask;
542 r = (u_int8_t *)&d;
543 for (i = 0; i < dp->sa_len; i++) {
544 r[i] = p[i] & q[i];
545 /* XXX rough estimate */
546 matchlen += (q[i] ? 8 : 0);
547 }
548
549 /* need to overwrite len/family portion as we don't compare them */
550 s.ss_len = sp->sa_len;
551 s.ss_family = sp->sa_family;
552 d.ss_len = dp->sa_len;
553 d.ss_family = dp->sa_family;
554
555 if (bcmp(&s, &ep->src, ep->src.ss_len) == 0 &&
556 bcmp(&d, &ep->dst, ep->dst.ss_len) == 0) {
557 return matchlen;
558 } else {
559 return 0;
560 }
561 }
562
563 struct encaptabtag {
564 void* *arg;
565 };
566
567 static void
568 encap_fillarg(
569 struct mbuf *m,
570 void *arg)
571 {
572 struct m_tag *tag;
573 struct encaptabtag *et;
574
575 tag = m_tag_create(KERNEL_MODULE_TAG_ID, KERNEL_TAG_TYPE_ENCAP,
576 sizeof(struct encaptabtag), M_WAITOK, m);
577
578 if (tag != NULL) {
579 et = (struct encaptabtag*)(tag + 1);
580 et->arg = arg;
581 m_tag_prepend(m, tag);
582 }
583 }
584
585 void *
586 encap_getarg(struct mbuf *m)
587 {
588 struct m_tag *tag;
589 struct encaptabtag *et;
590 void *p = NULL;
591
592 tag = m_tag_locate(m, KERNEL_MODULE_TAG_ID, KERNEL_TAG_TYPE_ENCAP, NULL);
593 if (tag) {
594 et = (struct encaptabtag*)(tag + 1);
595 p = et->arg;
596 m_tag_delete(m, tag);
597 }
598
599 return p;
600 }