ip_encap.c revision 1.21 1 /* $KAME: ip_encap.c,v 1.73 2001/10/02 08:30:58 itojun Exp $ */
2
3 /*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31 /*
32 * My grandfather said that there's a devil inside tunnelling technology...
33 *
34 * We have surprisingly many protocols that want packets with IP protocol
35 * #4 or #41. Here's a list of protocols that want protocol #41:
36 * RFC1933 configured tunnel
37 * RFC1933 automatic tunnel
38 * RFC2401 IPsec tunnel
39 * RFC2473 IPv6 generic packet tunnelling
40 * RFC2529 6over4 tunnel
41 * RFC3056 6to4 tunnel
42 * isatap tunnel
43 * mobile-ip6 (uses RFC2473)
44 * Here's a list of protocol that want protocol #4:
45 * RFC1853 IPv4-in-IPv4 tunnelling
46 * RFC2003 IPv4 encapsulation within IPv4
47 * RFC2344 reverse tunnelling for mobile-ip4
48 * RFC2401 IPsec tunnel
49 * Well, what can I say. They impose different en/decapsulation mechanism
50 * from each other, so they need separate protocol handler. The only one
51 * we can easily determine by protocol # is IPsec, which always has
52 * AH/ESP/IPComp header right after outer IP header.
53 *
54 * So, clearly good old protosw does not work for protocol #4 and #41.
55 * The code will let you match protocol via src/dst address pair.
56 */
57 /* XXX is M_NETADDR correct? */
58
59 /*
60 * With USE_RADIX the code will use radix table for tunnel lookup, for
61 * tunnels registered with encap_attach() with a addr/mask pair.
62 * Faster on machines with thousands of tunnel registerations (= interfaces).
63 *
64 * The code assumes that radix table code can handle non-continuous netmask,
65 * as it will pass radix table memory region with (src + dst) sockaddr pair.
66 *
67 * FreeBSD is excluded here as they make max_keylen a static variable, and
68 * thus forbid definition of radix table other than proper domains.
69 */
70 #define USE_RADIX
71
72 #include <sys/cdefs.h>
73 __KERNEL_RCSID(0, "$NetBSD: ip_encap.c,v 1.21 2005/01/24 04:46:49 enami Exp $");
74
75 #include "opt_mrouting.h"
76 #include "opt_inet.h"
77
78 #include <sys/param.h>
79 #include <sys/systm.h>
80 #include <sys/socket.h>
81 #include <sys/sockio.h>
82 #include <sys/mbuf.h>
83 #include <sys/errno.h>
84 #include <sys/protosw.h>
85 #include <sys/queue.h>
86
87 #include <net/if.h>
88 #include <net/route.h>
89
90 #include <netinet/in.h>
91 #include <netinet/in_systm.h>
92 #include <netinet/ip.h>
93 #include <netinet/ip_var.h>
94 #include <netinet/ip_encap.h>
95 #ifdef MROUTING
96 #include <netinet/ip_mroute.h>
97 #endif /* MROUTING */
98
99 #ifdef INET6
100 #include <netinet/ip6.h>
101 #include <netinet6/ip6_var.h>
102 #include <netinet6/ip6protosw.h>
103 #include <netinet6/in6_var.h>
104 #include <netinet6/in6_pcb.h>
105 #include <netinet/icmp6.h>
106 #endif
107
108 #include <machine/stdarg.h>
109
110 #include <net/net_osdep.h>
111
112 /* to lookup a pair of address using radix tree */
113 struct sockaddr_pack {
114 u_int8_t sp_len;
115 u_int8_t sp_family; /* not really used */
116 /* followed by variable-length data */
117 };
118
119 struct pack4 {
120 struct sockaddr_pack p;
121 struct sockaddr_in mine;
122 struct sockaddr_in yours;
123 };
124 struct pack6 {
125 struct sockaddr_pack p;
126 struct sockaddr_in6 mine;
127 struct sockaddr_in6 yours;
128 };
129
130 enum direction { INBOUND, OUTBOUND };
131
132 #ifdef INET
133 static struct encaptab *encap4_lookup __P((struct mbuf *, int, int,
134 enum direction));
135 #endif
136 #ifdef INET6
137 static struct encaptab *encap6_lookup __P((struct mbuf *, int, int,
138 enum direction));
139 #endif
140 static int encap_add __P((struct encaptab *));
141 static int encap_remove __P((struct encaptab *));
142 static int encap_afcheck __P((int, const struct sockaddr *, const struct sockaddr *));
143 #ifdef USE_RADIX
144 static struct radix_node_head *encap_rnh __P((int));
145 static int mask_matchlen __P((const struct sockaddr *));
146 #endif
147 #ifndef USE_RADIX
148 static int mask_match __P((const struct encaptab *, const struct sockaddr *,
149 const struct sockaddr *));
150 #endif
151 static void encap_fillarg __P((struct mbuf *, const struct encaptab *));
152
153 LIST_HEAD(, encaptab) encaptab = LIST_HEAD_INITIALIZER(&encaptab);
154
155 #ifdef USE_RADIX
156 extern int max_keylen; /* radix.c */
157 struct radix_node_head *encap_head[2]; /* 0 for AF_INET, 1 for AF_INET6 */
158 #endif
159
160 void
161 encap_setkeylen()
162 {
163 #ifdef USE_RADIX
164 if (sizeof(struct pack4) > max_keylen)
165 max_keylen = sizeof(struct pack4);
166 #ifdef INET6
167 if (sizeof(struct pack6) > max_keylen)
168 max_keylen = sizeof(struct pack6);
169 #endif
170 #endif
171 }
172
173 void
174 encap_init()
175 {
176 static int initialized = 0;
177
178 if (initialized)
179 return;
180 initialized++;
181 #if 0
182 /*
183 * we cannot use LIST_INIT() here, since drivers may want to call
184 * encap_attach(), on driver attach. encap_init() will be called
185 * on AF_INET{,6} initialization, which happens after driver
186 * initialization - using LIST_INIT() here can nuke encap_attach()
187 * from drivers.
188 */
189 LIST_INIT(&encaptab);
190 #endif
191
192 #ifdef USE_RADIX
193 /*
194 * initialize radix lookup table.
195 * max_keylen initialization happen in the rn_init().
196 */
197 rn_init();
198 rn_inithead((void *)&encap_head[0], sizeof(struct sockaddr_pack) << 3);
199 #ifdef INET6
200 rn_inithead((void *)&encap_head[1], sizeof(struct sockaddr_pack) << 3);
201 #endif
202 #endif
203 }
204
205 #ifdef INET
206 static struct encaptab *
207 encap4_lookup(m, off, proto, dir)
208 struct mbuf *m;
209 int off;
210 int proto;
211 enum direction dir;
212 {
213 struct ip *ip;
214 struct pack4 pack;
215 struct encaptab *ep, *match;
216 int prio, matchprio;
217 #ifdef USE_RADIX
218 struct radix_node_head *rnh = encap_rnh(AF_INET);
219 struct radix_node *rn;
220 #endif
221
222 #ifdef DIAGNOSTIC
223 if (m->m_len < sizeof(*ip))
224 panic("encap4_lookup");
225 #endif
226 ip = mtod(m, struct ip *);
227
228 bzero(&pack, sizeof(pack));
229 pack.p.sp_len = sizeof(pack);
230 pack.mine.sin_family = pack.yours.sin_family = AF_INET;
231 pack.mine.sin_len = pack.yours.sin_len = sizeof(struct sockaddr_in);
232 if (dir == INBOUND) {
233 pack.mine.sin_addr = ip->ip_dst;
234 pack.yours.sin_addr = ip->ip_src;
235 } else {
236 pack.mine.sin_addr = ip->ip_src;
237 pack.yours.sin_addr = ip->ip_dst;
238 }
239
240 match = NULL;
241 matchprio = 0;
242
243 #ifdef USE_RADIX
244 rn = rnh->rnh_matchaddr((caddr_t)&pack, rnh);
245 if (rn && (rn->rn_flags & RNF_ROOT) == 0) {
246 match = (struct encaptab *)rn;
247 matchprio = mask_matchlen(match->srcmask) +
248 mask_matchlen(match->dstmask);
249 }
250 #endif
251
252 for (ep = LIST_FIRST(&encaptab); ep; ep = LIST_NEXT(ep, chain)) {
253 if (ep->af != AF_INET)
254 continue;
255 if (ep->proto >= 0 && ep->proto != proto)
256 continue;
257 if (ep->func)
258 prio = (*ep->func)(m, off, proto, ep->arg);
259 else {
260 #ifdef USE_RADIX
261 continue;
262 #else
263 prio = mask_match(ep, (struct sockaddr *)&pack.mine,
264 (struct sockaddr *)&pack.yours);
265 #endif
266 }
267
268 /*
269 * We prioritize the matches by using bit length of the
270 * matches. mask_match() and user-supplied matching function
271 * should return the bit length of the matches (for example,
272 * if both src/dst are matched for IPv4, 64 should be returned).
273 * 0 or negative return value means "it did not match".
274 *
275 * The question is, since we have two "mask" portion, we
276 * cannot really define total order between entries.
277 * For example, which of these should be preferred?
278 * mask_match() returns 48 (32 + 16) for both of them.
279 * src=3ffe::/16, dst=3ffe:501::/32
280 * src=3ffe:501::/32, dst=3ffe::/16
281 *
282 * We need to loop through all the possible candidates
283 * to get the best match - the search takes O(n) for
284 * n attachments (i.e. interfaces).
285 *
286 * For radix-based lookup, I guess source takes precedence.
287 * See rn_{refines,lexobetter} for the correct answer.
288 */
289 if (prio <= 0)
290 continue;
291 if (prio > matchprio) {
292 matchprio = prio;
293 match = ep;
294 }
295 }
296
297 return match;
298 #undef s
299 #undef d
300 }
301
302 void
303 encap4_input(struct mbuf *m, ...)
304 {
305 int off, proto;
306 va_list ap;
307 const struct protosw *psw;
308 struct encaptab *match;
309
310 va_start(ap, m);
311 off = va_arg(ap, int);
312 proto = va_arg(ap, int);
313 va_end(ap);
314
315 match = encap4_lookup(m, off, proto, INBOUND);
316
317 if (match) {
318 /* found a match, "match" has the best one */
319 psw = match->psw;
320 if (psw && psw->pr_input) {
321 encap_fillarg(m, match);
322 (*psw->pr_input)(m, off, proto);
323 } else
324 m_freem(m);
325 return;
326 }
327
328 /* last resort: inject to raw socket */
329 rip_input(m, off, proto);
330 }
331 #endif
332
333 #ifdef INET6
334 static struct encaptab *
335 encap6_lookup(m, off, proto, dir)
336 struct mbuf *m;
337 int off;
338 int proto;
339 enum direction dir;
340 {
341 struct ip6_hdr *ip6;
342 struct pack6 pack;
343 int prio, matchprio;
344 struct encaptab *ep, *match;
345 #ifdef USE_RADIX
346 struct radix_node_head *rnh = encap_rnh(AF_INET6);
347 struct radix_node *rn;
348 #endif
349
350 #ifdef DIAGNOSTIC
351 if (m->m_len < sizeof(*ip6))
352 panic("encap6_lookup");
353 #endif
354 ip6 = mtod(m, struct ip6_hdr *);
355
356 bzero(&pack, sizeof(pack));
357 pack.p.sp_len = sizeof(pack);
358 pack.mine.sin6_family = pack.yours.sin6_family = AF_INET6;
359 pack.mine.sin6_len = pack.yours.sin6_len = sizeof(struct sockaddr_in6);
360 if (dir == INBOUND) {
361 pack.mine.sin6_addr = ip6->ip6_dst;
362 pack.yours.sin6_addr = ip6->ip6_src;
363 } else {
364 pack.mine.sin6_addr = ip6->ip6_src;
365 pack.yours.sin6_addr = ip6->ip6_dst;
366 }
367
368 match = NULL;
369 matchprio = 0;
370
371 #ifdef USE_RADIX
372 rn = rnh->rnh_matchaddr((caddr_t)&pack, rnh);
373 if (rn && (rn->rn_flags & RNF_ROOT) == 0) {
374 match = (struct encaptab *)rn;
375 matchprio = mask_matchlen(match->srcmask) +
376 mask_matchlen(match->dstmask);
377 }
378 #endif
379
380 for (ep = LIST_FIRST(&encaptab); ep; ep = LIST_NEXT(ep, chain)) {
381 if (ep->af != AF_INET6)
382 continue;
383 if (ep->proto >= 0 && ep->proto != proto)
384 continue;
385 if (ep->func)
386 prio = (*ep->func)(m, off, proto, ep->arg);
387 else {
388 #ifdef USE_RADIX
389 continue;
390 #else
391 prio = mask_match(ep, (struct sockaddr *)&pack.mine,
392 (struct sockaddr *)&pack.yours);
393 #endif
394 }
395
396 /* see encap4_lookup() for issues here */
397 if (prio <= 0)
398 continue;
399 if (prio > matchprio) {
400 matchprio = prio;
401 match = ep;
402 }
403 }
404
405 return match;
406 #undef s
407 #undef d
408 }
409
410 int
411 encap6_input(mp, offp, proto)
412 struct mbuf **mp;
413 int *offp;
414 int proto;
415 {
416 struct mbuf *m = *mp;
417 const struct ip6protosw *psw;
418 struct encaptab *match;
419
420 match = encap6_lookup(m, *offp, proto, INBOUND);
421
422 if (match) {
423 /* found a match */
424 psw = (const struct ip6protosw *)match->psw;
425 if (psw && psw->pr_input) {
426 encap_fillarg(m, match);
427 return (*psw->pr_input)(mp, offp, proto);
428 } else {
429 m_freem(m);
430 return IPPROTO_DONE;
431 }
432 }
433
434 /* last resort: inject to raw socket */
435 return rip6_input(mp, offp, proto);
436 }
437 #endif
438
439 static int
440 encap_add(ep)
441 struct encaptab *ep;
442 {
443 #ifdef USE_RADIX
444 struct radix_node_head *rnh = encap_rnh(ep->af);
445 #endif
446 int error = 0;
447
448 LIST_INSERT_HEAD(&encaptab, ep, chain);
449 #ifdef USE_RADIX
450 if (!ep->func && rnh) {
451 if (!rnh->rnh_addaddr((caddr_t)ep->addrpack,
452 (caddr_t)ep->maskpack, rnh, ep->nodes)) {
453 error = EEXIST;
454 goto fail;
455 }
456 }
457 #endif
458 return error;
459
460 fail:
461 LIST_REMOVE(ep, chain);
462 return error;
463 }
464
465 static int
466 encap_remove(ep)
467 struct encaptab *ep;
468 {
469 #ifdef USE_RADIX
470 struct radix_node_head *rnh = encap_rnh(ep->af);
471 #endif
472 int error = 0;
473
474 LIST_REMOVE(ep, chain);
475 #ifdef USE_RADIX
476 if (!ep->func && rnh) {
477 if (!rnh->rnh_deladdr((caddr_t)ep->addrpack,
478 (caddr_t)ep->maskpack, rnh))
479 error = ESRCH;
480 }
481 #endif
482 return error;
483 }
484
485 static int
486 encap_afcheck(af, sp, dp)
487 int af;
488 const struct sockaddr *sp;
489 const struct sockaddr *dp;
490 {
491 if (sp && dp) {
492 if (sp->sa_len != dp->sa_len)
493 return EINVAL;
494 if (af != sp->sa_family || af != dp->sa_family)
495 return EINVAL;
496 } else if (!sp && !dp)
497 ;
498 else
499 return EINVAL;
500
501 switch (af) {
502 case AF_INET:
503 if (sp && sp->sa_len != sizeof(struct sockaddr_in))
504 return EINVAL;
505 if (dp && dp->sa_len != sizeof(struct sockaddr_in))
506 return EINVAL;
507 break;
508 #ifdef INET6
509 case AF_INET6:
510 if (sp && sp->sa_len != sizeof(struct sockaddr_in6))
511 return EINVAL;
512 if (dp && dp->sa_len != sizeof(struct sockaddr_in6))
513 return EINVAL;
514 break;
515 #endif
516 default:
517 return EAFNOSUPPORT;
518 }
519
520 return 0;
521 }
522
523 /*
524 * sp (src ptr) is always my side, and dp (dst ptr) is always remote side.
525 * length of mask (sm and dm) is assumed to be same as sp/dp.
526 * Return value will be necessary as input (cookie) for encap_detach().
527 */
528 const struct encaptab *
529 encap_attach(af, proto, sp, sm, dp, dm, psw, arg)
530 int af;
531 int proto;
532 const struct sockaddr *sp, *sm;
533 const struct sockaddr *dp, *dm;
534 const struct protosw *psw;
535 void *arg;
536 {
537 struct encaptab *ep;
538 int error;
539 int s;
540 size_t l;
541 struct pack4 *pack4;
542 #ifdef INET6
543 struct pack6 *pack6;
544 #endif
545
546 s = splsoftnet();
547 /* sanity check on args */
548 error = encap_afcheck(af, sp, dp);
549 if (error)
550 goto fail;
551
552 /* check if anyone have already attached with exactly same config */
553 for (ep = LIST_FIRST(&encaptab); ep; ep = LIST_NEXT(ep, chain)) {
554 if (ep->af != af)
555 continue;
556 if (ep->proto != proto)
557 continue;
558 if (ep->func)
559 continue;
560 #ifdef DIAGNOSTIC
561 if (!ep->src || !ep->dst || !ep->srcmask || !ep->dstmask)
562 panic("null pointers in encaptab");
563 #endif
564 if (ep->src->sa_len != sp->sa_len ||
565 bcmp(ep->src, sp, sp->sa_len) != 0 ||
566 bcmp(ep->srcmask, sm, sp->sa_len) != 0)
567 continue;
568 if (ep->dst->sa_len != dp->sa_len ||
569 bcmp(ep->dst, dp, dp->sa_len) != 0 ||
570 bcmp(ep->dstmask, dm, dp->sa_len) != 0)
571 continue;
572
573 error = EEXIST;
574 goto fail;
575 }
576
577 switch (af) {
578 case AF_INET:
579 l = sizeof(*pack4);
580 break;
581 #ifdef INET6
582 case AF_INET6:
583 l = sizeof(*pack6);
584 break;
585 #endif
586 default:
587 goto fail;
588 }
589
590 #ifdef DIAGNOSTIC
591 /* if l exceeds the value sa_len can possibly express, it's wrong. */
592 if (l > (1 << (8 * sizeof(ep->addrpack->sa_len)))) {
593 error = EINVAL;
594 goto fail;
595 }
596 #endif
597
598 /* M_NETADDR ok? */
599 ep = malloc(sizeof(*ep), M_NETADDR, M_NOWAIT|M_ZERO);
600 if (ep == NULL) {
601 error = ENOBUFS;
602 goto fail;
603 }
604 ep->addrpack = malloc(l, M_NETADDR, M_NOWAIT|M_ZERO);
605 if (ep->addrpack == NULL) {
606 error = ENOBUFS;
607 goto gc;
608 }
609 ep->maskpack = malloc(l, M_NETADDR, M_NOWAIT|M_ZERO);
610 if (ep->maskpack == NULL) {
611 error = ENOBUFS;
612 goto gc;
613 }
614
615 ep->af = af;
616 ep->proto = proto;
617 ep->addrpack->sa_len = l & 0xff;
618 ep->maskpack->sa_len = l & 0xff;
619 switch (af) {
620 case AF_INET:
621 pack4 = (struct pack4 *)ep->addrpack;
622 ep->src = (struct sockaddr *)&pack4->mine;
623 ep->dst = (struct sockaddr *)&pack4->yours;
624 pack4 = (struct pack4 *)ep->maskpack;
625 ep->srcmask = (struct sockaddr *)&pack4->mine;
626 ep->dstmask = (struct sockaddr *)&pack4->yours;
627 break;
628 #ifdef INET6
629 case AF_INET6:
630 pack6 = (struct pack6 *)ep->addrpack;
631 ep->src = (struct sockaddr *)&pack6->mine;
632 ep->dst = (struct sockaddr *)&pack6->yours;
633 pack6 = (struct pack6 *)ep->maskpack;
634 ep->srcmask = (struct sockaddr *)&pack6->mine;
635 ep->dstmask = (struct sockaddr *)&pack6->yours;
636 break;
637 #endif
638 }
639
640 bcopy(sp, ep->src, sp->sa_len);
641 bcopy(sm, ep->srcmask, sp->sa_len);
642 bcopy(dp, ep->dst, dp->sa_len);
643 bcopy(dm, ep->dstmask, dp->sa_len);
644 ep->psw = psw;
645 ep->arg = arg;
646
647 error = encap_add(ep);
648 if (error)
649 goto gc;
650
651 error = 0;
652 splx(s);
653 return ep;
654
655 gc:
656 if (ep->addrpack)
657 free(ep->addrpack, M_NETADDR);
658 if (ep->maskpack)
659 free(ep->maskpack, M_NETADDR);
660 if (ep)
661 free(ep, M_NETADDR);
662 fail:
663 splx(s);
664 return NULL;
665 }
666
667 const struct encaptab *
668 encap_attach_func(af, proto, func, psw, arg)
669 int af;
670 int proto;
671 int (*func) __P((const struct mbuf *, int, int, void *));
672 const struct protosw *psw;
673 void *arg;
674 {
675 struct encaptab *ep;
676 int error;
677 int s;
678
679 s = splsoftnet();
680 /* sanity check on args */
681 if (!func) {
682 error = EINVAL;
683 goto fail;
684 }
685
686 error = encap_afcheck(af, NULL, NULL);
687 if (error)
688 goto fail;
689
690 ep = malloc(sizeof(*ep), M_NETADDR, M_NOWAIT); /*XXX*/
691 if (ep == NULL) {
692 error = ENOBUFS;
693 goto fail;
694 }
695 bzero(ep, sizeof(*ep));
696
697 ep->af = af;
698 ep->proto = proto;
699 ep->func = func;
700 ep->psw = psw;
701 ep->arg = arg;
702
703 error = encap_add(ep);
704 if (error)
705 goto fail;
706
707 error = 0;
708 splx(s);
709 return ep;
710
711 fail:
712 splx(s);
713 return NULL;
714 }
715
716 /* XXX encap4_ctlinput() is necessary if we set DF=1 on outer IPv4 header */
717
718 #ifdef INET6
719 void
720 encap6_ctlinput(cmd, sa, d0)
721 int cmd;
722 struct sockaddr *sa;
723 void *d0;
724 {
725 void *d = d0;
726 struct ip6_hdr *ip6;
727 struct mbuf *m;
728 int off;
729 struct ip6ctlparam *ip6cp = NULL;
730 int nxt;
731 struct encaptab *ep;
732 const struct ip6protosw *psw;
733
734 if (sa->sa_family != AF_INET6 ||
735 sa->sa_len != sizeof(struct sockaddr_in6))
736 return;
737
738 if ((unsigned)cmd >= PRC_NCMDS)
739 return;
740 if (cmd == PRC_HOSTDEAD)
741 d = NULL;
742 else if (cmd == PRC_MSGSIZE)
743 ; /* special code is present, see below */
744 else if (inet6ctlerrmap[cmd] == 0)
745 return;
746
747 /* if the parameter is from icmp6, decode it. */
748 if (d != NULL) {
749 ip6cp = (struct ip6ctlparam *)d;
750 m = ip6cp->ip6c_m;
751 ip6 = ip6cp->ip6c_ip6;
752 off = ip6cp->ip6c_off;
753 nxt = ip6cp->ip6c_nxt;
754
755 if (ip6 && cmd == PRC_MSGSIZE) {
756 int valid = 0;
757 struct encaptab *match;
758
759 /*
760 * Check to see if we have a valid encap configuration.
761 */
762 match = encap6_lookup(m, off, nxt, OUTBOUND);
763 if (match)
764 valid++;
765
766 /*
767 * Depending on the value of "valid" and routing table
768 * size (mtudisc_{hi,lo}wat), we will:
769 * - recalcurate the new MTU and create the
770 * corresponding routing entry, or
771 * - ignore the MTU change notification.
772 */
773 icmp6_mtudisc_update((struct ip6ctlparam *)d, valid);
774 }
775 } else {
776 m = NULL;
777 ip6 = NULL;
778 nxt = -1;
779 }
780
781 /* inform all listeners */
782 for (ep = LIST_FIRST(&encaptab); ep; ep = LIST_NEXT(ep, chain)) {
783 if (ep->af != AF_INET6)
784 continue;
785 if (ep->proto >= 0 && ep->proto != nxt)
786 continue;
787
788 /* should optimize by looking at address pairs */
789
790 /* XXX need to pass ep->arg or ep itself to listeners */
791 psw = (const struct ip6protosw *)ep->psw;
792 if (psw && psw->pr_ctlinput)
793 (*psw->pr_ctlinput)(cmd, sa, d);
794 }
795
796 rip6_ctlinput(cmd, sa, d0);
797 }
798 #endif
799
800 int
801 encap_detach(cookie)
802 const struct encaptab *cookie;
803 {
804 const struct encaptab *ep = cookie;
805 struct encaptab *p;
806 int error;
807
808 for (p = LIST_FIRST(&encaptab); p; p = LIST_NEXT(p, chain)) {
809 if (p == ep) {
810 error = encap_remove(p);
811 if (error)
812 return error;
813 if (!ep->func) {
814 free(p->addrpack, M_NETADDR);
815 free(p->maskpack, M_NETADDR);
816 }
817 free(p, M_NETADDR); /*XXX*/
818 return 0;
819 }
820 }
821
822 return ENOENT;
823 }
824
825 #ifdef USE_RADIX
826 static struct radix_node_head *
827 encap_rnh(af)
828 int af;
829 {
830
831 switch (af) {
832 case AF_INET:
833 return encap_head[0];
834 #ifdef INET6
835 case AF_INET6:
836 return encap_head[1];
837 #endif
838 default:
839 return NULL;
840 }
841 }
842
843 static int
844 mask_matchlen(sa)
845 const struct sockaddr *sa;
846 {
847 const char *p, *ep;
848 int l;
849
850 p = (const char *)sa;
851 ep = p + sa->sa_len;
852 p += 2; /* sa_len + sa_family */
853
854 l = 0;
855 while (p < ep) {
856 l += (*p ? 8 : 0); /* estimate */
857 p++;
858 }
859 return l;
860 }
861 #endif
862
863 #ifndef USE_RADIX
864 static int
865 mask_match(ep, sp, dp)
866 const struct encaptab *ep;
867 const struct sockaddr *sp;
868 const struct sockaddr *dp;
869 {
870 struct sockaddr_storage s;
871 struct sockaddr_storage d;
872 int i;
873 const u_int8_t *p, *q;
874 u_int8_t *r;
875 int matchlen;
876
877 #ifdef DIAGNOSTIC
878 if (ep->func)
879 panic("wrong encaptab passed to mask_match");
880 #endif
881 if (sp->sa_len > sizeof(s) || dp->sa_len > sizeof(d))
882 return 0;
883 if (sp->sa_family != ep->af || dp->sa_family != ep->af)
884 return 0;
885 if (sp->sa_len != ep->src->sa_len || dp->sa_len != ep->dst->sa_len)
886 return 0;
887
888 matchlen = 0;
889
890 p = (const u_int8_t *)sp;
891 q = (const u_int8_t *)ep->srcmask;
892 r = (u_int8_t *)&s;
893 for (i = 0 ; i < sp->sa_len; i++) {
894 r[i] = p[i] & q[i];
895 /* XXX estimate */
896 matchlen += (q[i] ? 8 : 0);
897 }
898
899 p = (const u_int8_t *)dp;
900 q = (const u_int8_t *)ep->dstmask;
901 r = (u_int8_t *)&d;
902 for (i = 0 ; i < dp->sa_len; i++) {
903 r[i] = p[i] & q[i];
904 /* XXX rough estimate */
905 matchlen += (q[i] ? 8 : 0);
906 }
907
908 /* need to overwrite len/family portion as we don't compare them */
909 s.ss_len = sp->sa_len;
910 s.ss_family = sp->sa_family;
911 d.ss_len = dp->sa_len;
912 d.ss_family = dp->sa_family;
913
914 if (bcmp(&s, ep->src, ep->src->sa_len) == 0 &&
915 bcmp(&d, ep->dst, ep->dst->sa_len) == 0) {
916 return matchlen;
917 } else
918 return 0;
919 }
920 #endif
921
922 static void
923 encap_fillarg(m, ep)
924 struct mbuf *m;
925 const struct encaptab *ep;
926 {
927 struct m_tag *mtag;
928
929 mtag = m_tag_get(PACKET_TAG_ENCAP, sizeof(void *), M_NOWAIT);
930 if (mtag) {
931 *(void **)(mtag + 1) = ep->arg;
932 m_tag_prepend(m, mtag);
933 }
934 }
935
936 void *
937 encap_getarg(m)
938 struct mbuf *m;
939 {
940 void *p;
941 struct m_tag *mtag;
942
943 p = NULL;
944 mtag = m_tag_find(m, PACKET_TAG_ENCAP, NULL);
945 if (mtag != NULL) {
946 p = *(void **)(mtag + 1);
947 m_tag_delete(m, mtag);
948 }
949 return p;
950 }
951