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