]> git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/ip_encap.c
xnu-344.12.2.tar.gz
[apple/xnu.git] / bsd / netinet / ip_encap.c
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 */
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 $ */
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:
66 * RFC1853 IPv4-in-IPv4 tunnelling
67 * RFC2003 IPv4 encapsulation within IPv4
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 */
78 /* XXX is M_NETADDR correct? */
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>
87 #include <sys/queue.h>
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 */
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
110 #ifndef __APPLE__
111 #include <sys/kernel.h>
112 #include <sys/malloc.h>
113 MALLOC_DEFINE(M_NETADDR, "Export Host", "Export host address structure");
114 #endif
115
116 static void encap_add __P((struct encaptab *));
117 static int mask_match __P((const struct encaptab *, const struct sockaddr *,
118 const struct sockaddr *));
119 static void encap_fillarg __P((struct mbuf *, const struct encaptab *));
120
121 #ifndef LIST_HEAD_INITIALIZER
122 /* rely upon BSS initialization */
123 LIST_HEAD(, encaptab) encaptab;
124 #else
125 LIST_HEAD(, encaptab) encaptab = LIST_HEAD_INITIALIZER(&encaptab);
126 #endif
127
128 void
129 encap_init()
130 {
131 static int initialized = 0;
132
133 if (initialized)
134 return;
135 initialized++;
136 #if 0
137 /*
138 * we cannot use LIST_INIT() here, since drivers may want to call
139 * encap_attach(), on driver attach. encap_init() will be called
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
148 #if INET
149 void
150 encap4_input(m, off)
151 struct mbuf *m;
152 int off;
153 {
154 int proto;
155 struct ip *ip;
156 struct sockaddr_in s, d;
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
167
168 ip = mtod(m, struct ip *);
169 #ifdef __APPLE__
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
182 match = NULL;
183 matchprio = 0;
184 for (ep = LIST_FIRST(&encaptab); ep; ep = LIST_NEXT(ep, chain)) {
185 if (ep->af != AF_INET)
186 continue;
187 if (ep->proto >= 0 && ep->proto != proto)
188 continue;
189 if (ep->func)
190 prio = (*ep->func)(m, off, proto, ep->arg);
191 else {
192 /*
193 * it's inbound traffic, we need to match in reverse
194 * order
195 */
196 prio = mask_match(ep, (struct sockaddr *)&d,
197 (struct sockaddr *)&s);
198 }
199
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);
232 } else
233 m_freem(m);
234 return;
235 }
236
237 /* for backward compatibility */
238 # if MROUTING
239 # define COMPATFUNC ipip_input
240 # endif /*MROUTING*/
241
242 #if COMPATFUNC
243 if (proto == IPPROTO_IPV4) {
244 COMPATFUNC(m, off);
245 return;
246 }
247 #endif
248
249 /* last resort: inject to raw socket */
250 rip_input(m, off);
251 }
252 #endif
253
254 #if INET6
255 int
256 encap6_input(mp, offp)
257 struct mbuf **mp;
258 int *offp;
259 {
260 struct mbuf *m = *mp;
261 struct ip6_hdr *ip6;
262 struct sockaddr_in6 s, d;
263 const struct ip6protosw *psw;
264 struct encaptab *ep, *match;
265 int prio, matchprio;
266 int proto;
267
268 ip6 = mtod(m, struct ip6_hdr *);
269 proto = ip6->ip6_nxt;
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
280 match = NULL;
281 matchprio = 0;
282 for (ep = LIST_FIRST(&encaptab); ep; ep = LIST_NEXT(ep, chain)) {
283 if (ep->af != AF_INET6)
284 continue;
285 if (ep->proto >= 0 && ep->proto != proto)
286 continue;
287 if (ep->func)
288 prio = (*ep->func)(m, *offp, proto, ep->arg);
289 else {
290 /*
291 * it's inbound traffic, we need to match in reverse
292 * order
293 */
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;
304 }
305 }
306
307 if (match) {
308 /* found a match */
309 psw = (const struct ip6protosw *)match->psw;
310 if (psw && psw->pr_input) {
311 encap_fillarg(m, match);
312 return (*psw->pr_input)(mp, offp);
313 } else {
314 m_freem(m);
315 return IPPROTO_DONE;
316 }
317 }
318
319 /* last resort: inject to raw socket */
320 return rip6_input(mp, offp);
321 }
322 #endif
323
324 static void
325 encap_add(ep)
326 struct encaptab *ep;
327 {
328
329 LIST_INSERT_HEAD(&encaptab, ep, chain);
330 }
331
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 */
337 const struct encaptab *
338 encap_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
350 s = splnet();
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
384 ep = _MALLOC(sizeof(*ep), M_NETADDR, M_WAITOK); /*XXX*/
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
400 encap_add(ep);
401
402 error = 0;
403 splx(s);
404 return ep;
405
406 fail:
407 splx(s);
408 return NULL;
409 }
410
411 const struct encaptab *
412 encap_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
423 s = splnet();
424 /* sanity check on args */
425 if (!func) {
426 error = EINVAL;
427 goto fail;
428 }
429
430 ep = _MALLOC(sizeof(*ep), M_NETADDR, M_WAITOK); /*XXX*/
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
443 encap_add(ep);
444
445 error = 0;
446 splx(s);
447 return ep;
448
449 fail:
450 splx(s);
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 static void
526 encap_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
543 void *
544 encap_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 }