]> git.saurik.com Git - apple/xnu.git/blob - bsd/netinet6/ip6_mroute.c
xnu-123.5.tar.gz
[apple/xnu.git] / bsd / netinet6 / ip6_mroute.c
1 /* $KAME: ip6_mroute.c,v 1.15 2000/02/22 14:04:21 itojun Exp $ */
2
3 /*
4 * Copyright (C) 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 /* BSDI ip_mroute.c,v 2.10 1996/11/14 00:29:52 jch Exp */
33
34 /*
35 * IP multicast forwarding procedures
36 *
37 * Written by David Waitzman, BBN Labs, August 1988.
38 * Modified by Steve Deering, Stanford, February 1989.
39 * Modified by Mark J. Steiglitz, Stanford, May, 1991
40 * Modified by Van Jacobson, LBL, January 1993
41 * Modified by Ajit Thyagarajan, PARC, August 1993
42 * Modified by Bill Fenenr, PARC, April 1994
43 *
44 * MROUTING Revision: 3.5.1.2 + PIM-SMv2 (pimd) Support
45 */
46
47 #if (defined(__FreeBSD__) && __FreeBSD__ >= 3) || defined(__NetBSD__)
48 #include "opt_inet.h"
49 #endif
50
51 #ifndef _KERNEL
52 # ifdef KERNEL
53 # define _KERNEL
54 # endif
55 #endif
56
57 #include <sys/param.h>
58 #include <sys/systm.h>
59 #if defined(__FreeBSD__) && __FreeBSD__ >= 3 || defined (__APPLE__)
60 #include <sys/malloc.h>
61 #endif
62 #include <sys/mbuf.h>
63 #include <sys/socket.h>
64 #include <sys/socketvar.h>
65 #include <sys/sockio.h>
66 #include <sys/protosw.h>
67 #include <sys/errno.h>
68 #include <sys/time.h>
69 #include <sys/kernel.h>
70 #if !(defined(__FreeBSD__) && __FreeBSD__ >= 3)
71 #include <sys/ioctl.h>
72 #endif
73 #include <sys/syslog.h>
74
75 #include <net/if.h>
76 #include <net/route.h>
77 #include <net/raw_cb.h>
78
79 #include <netinet/in.h>
80 #include <netinet/in_var.h>
81
82 #include <netinet/ip6.h>
83 #include <netinet6/ip6_var.h>
84 #include <netinet6/ip6_mroute.h>
85 #include <netinet6/pim6.h>
86 #include <netinet6/pim6_var.h>
87
88 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
89 static MALLOC_DEFINE(M_MRTABLE, "mf6c", "multicast forwarding cache entry");
90 #endif
91
92 #define M_HASCL(m) ((m)->m_flags & M_EXT)
93
94 static int ip6_mdq __P((struct mbuf *, struct ifnet *, struct mf6c *));
95 static void phyint_send __P((struct ip6_hdr *, struct mif6 *, struct mbuf *));
96
97 static int set_pim6 __P((int *));
98 #if !(defined(__FreeBSD__) && __FreeBSD__ >= 3) && !defined(__APPLE__)
99 static int get_pim6 __P((struct mbuf *));
100 #endif
101 static int socket_send __P((struct socket *, struct mbuf *,
102 struct sockaddr_in6 *));
103 static int register_send __P((struct ip6_hdr *, struct mif6 *,
104 struct mbuf *));
105
106 /*
107 * Globals. All but ip6_mrouter, ip6_mrtproto and mrt6stat could be static,
108 * except for netstat or debugging purposes.
109 */
110 struct socket *ip6_mrouter = NULL;
111 int ip6_mrtproto = IPPROTO_PIM; /* for netstat only */
112 struct mrt6stat mrt6stat;
113
114 #define NO_RTE_FOUND 0x1
115 #define RTE_FOUND 0x2
116
117 struct mf6c *mf6ctable[MF6CTBLSIZ];
118 u_char nexpire[MF6CTBLSIZ];
119 static struct mif6 mif6table[MAXMIFS];
120 #if MRT6DEBUG
121 u_int mrt6debug = 0; /* debug level */
122 #define DEBUG_MFC 0x02
123 #define DEBUG_FORWARD 0x04
124 #define DEBUG_EXPIRE 0x08
125 #define DEBUG_XMIT 0x10
126 #define DEBUG_REG 0x20
127 #define DEBUG_PIM 0x40
128 #endif
129
130 static void expire_upcalls __P((void *));
131 #define EXPIRE_TIMEOUT (hz / 4) /* 4x / second */
132 #define UPCALL_EXPIRE 6 /* number of timeouts */
133
134 #if INET
135 #if MROUTING
136 extern struct socket *ip_mrouter;
137 #endif
138 #endif
139
140 static u_long lo_dl_tag = 0;
141 /*
142 * 'Interfaces' associated with decapsulator (so we can tell
143 * packets that went through it from ones that get reflected
144 * by a broken gateway). These interfaces are never linked into
145 * the system ifnet list & no routes point to them. I.e., packets
146 * can't be sent this way. They only exist as a placeholder for
147 * multicast source verification.
148 */
149 struct ifnet multicast_register_if;
150
151 #define ENCAP_HOPS 64
152
153 /*
154 * Private variables.
155 */
156 static mifi_t nummifs = 0;
157 static mifi_t reg_mif_num = (mifi_t)-1;
158
159 static struct pim6stat pim6stat;
160
161 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
162 static struct callout_handle expire_upcalls_ch;
163 #endif
164
165 /*
166 * one-back cache used by ipip_input to locate a tunnel's mif
167 * given a datagram's src ip address.
168 */
169 static int pim6;
170
171 /*
172 * Hash function for a source, group entry
173 */
174 #define MF6CHASH(a, g) MF6CHASHMOD((a).s6_addr32[0] ^ (a).s6_addr32[1] ^ \
175 (a).s6_addr32[2] ^ (a).s6_addr32[3] ^ \
176 (g).s6_addr32[0] ^ (g).s6_addr32[1] ^ \
177 (g).s6_addr32[2] ^ (g).s6_addr32[3])
178
179 /*
180 * Find a route for a given origin IPv6 address and Multicast group address.
181 * Quality of service parameter to be added in the future!!!
182 */
183
184 #define MF6CFIND(o, g, rt) { \
185 register struct mf6c *_rt = mf6ctable[MF6CHASH(o,g)]; \
186 rt = NULL; \
187 mrt6stat.mrt6s_mfc_lookups++; \
188 while (_rt) { \
189 if (IN6_ARE_ADDR_EQUAL(&_rt->mf6c_origin.sin6_addr, &(o)) && \
190 IN6_ARE_ADDR_EQUAL(&_rt->mf6c_mcastgrp.sin6_addr, &(g)) && \
191 (_rt->mf6c_stall == NULL)) { \
192 rt = _rt; \
193 break; \
194 } \
195 _rt = _rt->mf6c_next; \
196 } \
197 if (rt == NULL) { \
198 mrt6stat.mrt6s_mfc_misses++; \
199 } \
200 }
201
202 /*
203 * Macros to compute elapsed time efficiently
204 * Borrowed from Van Jacobson's scheduling code
205 */
206 #define TV_DELTA(a, b, delta) { \
207 register int xxs; \
208 \
209 delta = (a).tv_usec - (b).tv_usec; \
210 if ((xxs = (a).tv_sec - (b).tv_sec)) { \
211 switch (xxs) { \
212 case 2: \
213 delta += 1000000; \
214 /* fall through */ \
215 case 1: \
216 delta += 1000000; \
217 break; \
218 default: \
219 delta += (1000000 * xxs); \
220 } \
221 } \
222 }
223
224 #define TV_LT(a, b) (((a).tv_usec < (b).tv_usec && \
225 (a).tv_sec <= (b).tv_sec) || (a).tv_sec < (b).tv_sec)
226
227 #if UPCALL_TIMING
228 #define UPCALL_MAX 50
229 u_long upcall_data[UPCALL_MAX + 1];
230 static void collate();
231 #endif /* UPCALL_TIMING */
232
233 static int get_sg_cnt __P((struct sioc_sg_req6 *));
234 static int get_mif6_cnt __P((struct sioc_mif_req6 *));
235 static int ip6_mrouter_init __P((struct socket *, struct mbuf *));
236 static int add_m6if __P((struct mif6ctl *));
237 static int del_m6if __P((mifi_t *));
238 static int add_m6fc __P((struct mf6cctl *));
239 static int del_m6fc __P((struct mf6cctl *));
240
241 /*
242 * Handle MRT setsockopt commands to modify the multicast routing tables.
243 */
244 #if defined(__FreeBSD__) && __FreeBSD__ >= 3 || defined (__APPLE__)
245 int
246 ip6_mrouter_set(so, sopt)
247 struct socket *so;
248 struct sockopt *sopt;
249 {
250 int error = 0;
251 struct mbuf *m;
252
253 if (so != ip6_mrouter && sopt->sopt_name != MRT6_INIT)
254 return (EACCES);
255
256 if (error = sooptgetm(sopt, &m)) /* XXX */
257 return (error);
258 if (error = sooptmcopyin(sopt, m)) /* XXX */
259 return (error);
260
261 switch (sopt->sopt_name) {
262 case MRT6_INIT:
263 error = ip6_mrouter_init(so, m);
264 break;
265 case MRT6_DONE:
266 error = ip6_mrouter_done();
267 break;
268 case MRT6_ADD_MIF:
269 error = add_m6if(mtod(m, struct mif6ctl *));
270 break;
271 case MRT6_DEL_MIF:
272 error = del_m6if(mtod(m, mifi_t *));
273 break;
274 case MRT6_ADD_MFC:
275 error = add_m6fc(mtod(m, struct mf6cctl *));
276 break;
277 case MRT6_DEL_MFC:
278 error = del_m6fc(mtod(m, struct mf6cctl *));
279 break;
280 case MRT6_PIM:
281 error = set_pim6(mtod(m, int *));
282 break;
283 default:
284 error = EOPNOTSUPP;
285 break;
286 }
287
288 (void)m_freem(m);
289 return(error);
290 }
291 #else
292 int
293 ip6_mrouter_set(cmd, so, m)
294 int cmd;
295 struct socket *so;
296 struct mbuf *m;
297 {
298 if (cmd != MRT6_INIT && so != ip6_mrouter)
299 return EACCES;
300
301 switch (cmd) {
302 case MRT6_INIT: return ip6_mrouter_init(so, m);
303 case MRT6_DONE: return ip6_mrouter_done();
304 case MRT6_ADD_MIF: return add_m6if(mtod(m, struct mif6ctl *));
305 case MRT6_DEL_MIF: return del_m6if(mtod(m, mifi_t *));
306 case MRT6_ADD_MFC: return add_m6fc(mtod(m, struct mf6cctl *));
307 case MRT6_DEL_MFC: return del_m6fc(mtod(m, struct mf6cctl *));
308 case MRT6_PIM: return set_pim6(mtod(m, int *));
309 default: return EOPNOTSUPP;
310 }
311 }
312 #endif
313
314 /*
315 * Handle MRT getsockopt commands
316 */
317 #if defined(__FreeBSD__) && __FreeBSD__ >= 3 || defined (__APPLE__)
318 int
319 ip6_mrouter_get(so, sopt)
320 struct socket *so;
321 struct sockopt *sopt;
322 {
323 int error = 0;
324
325 if (so != ip6_mrouter) return EACCES;
326
327 switch (sopt->sopt_name) {
328 case MRT6_PIM:
329 error = sooptcopyout(sopt, &pim6, sizeof(pim6));
330 break;
331 }
332 return (error);
333 }
334 #else
335 int
336 ip6_mrouter_get(cmd, so, m)
337 int cmd;
338 struct socket *so;
339 struct mbuf **m;
340 {
341 struct mbuf *mb;
342
343 if (so != ip6_mrouter) return EACCES;
344
345 *m = mb = m_get(M_WAIT, MT_SOOPTS);
346
347 switch (cmd) {
348 case MRT6_PIM: return get_pim6(mb);
349 default:
350 m_free(mb);
351 return EOPNOTSUPP;
352 }
353 }
354 #endif
355
356 /*
357 * Handle ioctl commands to obtain information from the cache
358 */
359 int
360 mrt6_ioctl(cmd, data)
361 int cmd;
362 caddr_t data;
363 {
364 int error = 0;
365
366 switch (cmd) {
367 case SIOCGETSGCNT_IN6:
368 return(get_sg_cnt((struct sioc_sg_req6 *)data));
369 break; /* for safety */
370 case SIOCGETMIFCNT_IN6:
371 return(get_mif6_cnt((struct sioc_mif_req6 *)data));
372 break; /* for safety */
373 default:
374 return (EINVAL);
375 break;
376 }
377 return error;
378 }
379
380 /*
381 * returns the packet, byte, rpf-failure count for the source group provided
382 */
383 static int
384 get_sg_cnt(req)
385 register struct sioc_sg_req6 *req;
386 {
387 register struct mf6c *rt;
388 int s;
389
390 #if __NetBSD__
391 s = splsoftnet();
392 #else
393 s = splnet();
394 #endif
395 MF6CFIND(req->src.sin6_addr, req->grp.sin6_addr, rt);
396 splx(s);
397 if (rt != NULL) {
398 req->pktcnt = rt->mf6c_pkt_cnt;
399 req->bytecnt = rt->mf6c_byte_cnt;
400 req->wrong_if = rt->mf6c_wrong_if;
401 } else
402 return(ESRCH);
403 #if 0
404 req->pktcnt = req->bytecnt = req->wrong_if = 0xffffffff;
405 #endif
406
407 return 0;
408 }
409
410 /*
411 * returns the input and output packet and byte counts on the mif provided
412 */
413 static int
414 get_mif6_cnt(req)
415 register struct sioc_mif_req6 *req;
416 {
417 register mifi_t mifi = req->mifi;
418
419 if (mifi >= nummifs)
420 return EINVAL;
421
422 req->icount = mif6table[mifi].m6_pkt_in;
423 req->ocount = mif6table[mifi].m6_pkt_out;
424 req->ibytes = mif6table[mifi].m6_bytes_in;
425 req->obytes = mif6table[mifi].m6_bytes_out;
426
427 return 0;
428 }
429
430 #if !(defined(__FreeBSD__) && __FreeBSD__ >=3) || !defined(__APPLE__)
431 /*
432 * Get PIM processiong global
433 */
434 static int
435 get_pim6(m)
436 struct mbuf *m;
437 {
438 int *i;
439
440 i = mtod(m, int *);
441
442 *i = pim6;
443
444 return 0;
445 }
446 #endif
447
448 static int
449 set_pim6(i)
450 int *i;
451 {
452 if ((*i != 1) && (*i != 0))
453 return EINVAL;
454
455 pim6 = *i;
456
457 return 0;
458 }
459
460 /*
461 * Enable multicast routing
462 */
463 static int
464 ip6_mrouter_init(so, m)
465 struct socket *so;
466 struct mbuf *m;
467 {
468 int *v;
469
470 #if MRT6DEBUG
471 if (mrt6debug)
472 log(LOG_DEBUG,
473 "ip6_mrouter_init: so_type = %d, pr_protocol = %d\n",
474 so->so_type, so->so_proto->pr_protocol);
475 #endif
476
477 if (so->so_type != SOCK_RAW ||
478 so->so_proto->pr_protocol != IPPROTO_ICMPV6)
479 return EOPNOTSUPP;
480
481 if (!m || (m->m_len != sizeof(int *)))
482 return ENOPROTOOPT;
483
484 v = mtod(m, int *);
485 if (*v != 1)
486 return ENOPROTOOPT;
487
488 if (ip6_mrouter != NULL) return EADDRINUSE;
489
490 ip6_mrouter = so;
491
492 bzero((caddr_t)mf6ctable, sizeof(mf6ctable));
493 bzero((caddr_t)nexpire, sizeof(nexpire));
494
495 pim6 = 0;/* used for stubbing out/in pim stuff */
496
497 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
498 expire_upcalls_ch =
499 #endif
500 timeout(expire_upcalls, (caddr_t)NULL, EXPIRE_TIMEOUT);
501
502 #if MRT6DEBUG
503 if (mrt6debug)
504 log(LOG_DEBUG, "ip6_mrouter_init\n");
505 #endif
506
507 return 0;
508 }
509
510 /*
511 * Disable multicast routing
512 */
513 int
514 ip6_mrouter_done()
515 {
516 mifi_t mifi;
517 int i;
518 struct ifnet *ifp;
519 struct in6_ifreq ifr;
520 struct mf6c *rt;
521 struct rtdetq *rte;
522 int s;
523
524 #if __NetBSD__
525 s = splsoftnet();
526 #else
527 s = splnet();
528 #endif
529
530 /*
531 * For each phyint in use, disable promiscuous reception of all IPv6
532 * multicasts.
533 */
534 #if INET
535 #if MROUTING
536 /*
537 * If there is still IPv4 multicast routing daemon,
538 * we remain interfaces to receive all muliticasted packets.
539 * XXX: there may be an interface in which the IPv4 multicast
540 * daemon is not interested...
541 */
542 if (!ip_mrouter)
543 #endif
544 #endif
545 {
546 for (mifi = 0; mifi < nummifs; mifi++) {
547 if (mif6table[mifi].m6_ifp &&
548 !(mif6table[mifi].m6_flags & MIFF_REGISTER)) {
549 ifr.ifr_addr.sin6_family = AF_INET6;
550 ifr.ifr_addr.sin6_addr= in6addr_any;
551 ifp = mif6table[mifi].m6_ifp;
552 #ifdef __APPLE__
553 dlil_ioctl(0, ifp, SIOCDELMULTI,
554 (caddr_t)&ifr);
555 #else
556 (*ifp->if_ioctl)(ifp, SIOCDELMULTI,
557 (caddr_t)&ifr);
558 #endif
559 }
560 }
561 }
562 #if notyet
563 bzero((caddr_t)qtable, sizeof(qtable));
564 bzero((caddr_t)tbftable, sizeof(tbftable));
565 #endif
566 bzero((caddr_t)mif6table, sizeof(mif6table));
567 nummifs = 0;
568
569 pim6 = 0; /* used to stub out/in pim specific code */
570
571 untimeout(expire_upcalls, (caddr_t)NULL
572 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
573 , expire_upcalls_ch
574 #endif
575 );
576
577 /*
578 * Free all multicast forwarding cache entries.
579 */
580 for (i = 0; i < MF6CTBLSIZ; i++) {
581 rt = mf6ctable[i];
582 while (rt) {
583 struct mf6c *frt;
584
585 for (rte = rt->mf6c_stall; rte != NULL; ) {
586 struct rtdetq *n = rte->next;
587
588 m_free(rte->m);
589 _FREE(rte, M_MRTABLE);
590 rte = n;
591 }
592 frt = rt;
593 rt = rt->mf6c_next;
594 _FREE(frt, M_MRTABLE);
595 }
596 }
597
598 bzero((caddr_t)mf6ctable, sizeof(mf6ctable));
599
600 /*
601 * Reset de-encapsulation cache
602 */
603 reg_mif_num = -1;
604
605 ip6_mrouter = NULL;
606
607 splx(s);
608
609 #if MRT6DEBUG
610 if (mrt6debug)
611 log(LOG_DEBUG, "ip6_mrouter_done\n");
612 #endif
613
614 return 0;
615 }
616
617 static struct sockaddr_in6 sin6 = { sizeof(sin6), AF_INET6 };
618
619 /*
620 * Add a mif to the mif table
621 */
622 static int
623 add_m6if(mifcp)
624 register struct mif6ctl *mifcp;
625 {
626 register struct mif6 *mifp;
627 struct ifnet *ifp;
628 #if !(defined(__FreeBSD__) && __FreeBSD__ >= 3) && !defined(__APPLE__)
629 struct in6_ifreq ifr;
630 #endif
631 int error, s;
632 #if notyet
633 struct tbf *m_tbf = tbftable + mifcp->mif6c_mifi;
634 #endif
635
636 if (mifcp->mif6c_mifi >= MAXMIFS)
637 return EINVAL;
638 mifp = mif6table + mifcp->mif6c_mifi;
639 if (mifp->m6_ifp)
640 return EADDRINUSE; /* XXX: is it appropriate? */
641 if (mifcp->mif6c_pifi == 0 || mifcp->mif6c_pifi > if_index)
642 return ENXIO;
643 ifp = ifindex2ifnet[mifcp->mif6c_pifi];
644
645 if (mifcp->mif6c_flags & MIFF_REGISTER) {
646 if (reg_mif_num == (mifi_t)-1) {
647 #if defined(__NetBSD__) || defined(__OpenBSD__)
648 strcpy(multicast_register_if.if_xname,
649 "register_mif"); /* XXX */
650 #else
651 multicast_register_if.if_name = "register_mif";
652 #endif
653 multicast_register_if.if_flags |= IFF_LOOPBACK;
654 multicast_register_if.if_index = mifcp->mif6c_mifi;
655 reg_mif_num = mifcp->mif6c_mifi;
656 }
657
658 ifp = &multicast_register_if;
659
660 } /* if REGISTER */
661 else {
662 /* Make sure the interface supports multicast */
663 if ((ifp->if_flags & IFF_MULTICAST) == 0)
664 return EOPNOTSUPP;
665
666 #if __NetBSD__
667 s = splsoftnet();
668 #else
669 s = splnet();
670 #endif
671 #if (defined(__FreeBSD__) && __FreeBSD__ >= 3) || defined (__APPLE__)
672 error = if_allmulti(ifp, 1);
673 #else
674 /*
675 * Enable promiscuous reception of all IPv6 multicasts
676 * from the interface.
677 */
678 ifr.ifr_addr.sin6_family = AF_INET6;
679 ifr.ifr_addr.sin6_addr = in6addr_any;
680 error = (*ifp->if_ioctl)(ifp, SIOCADDMULTI, (caddr_t)&ifr);
681 #endif
682 splx(s);
683 if (error)
684 return error;
685 }
686
687 #if __NetBSD__
688 s = splsoftnet();
689 #else
690 s = splnet();
691 #endif
692 mifp->m6_flags = mifcp->mif6c_flags;
693 mifp->m6_ifp = ifp;
694 #if notyet
695 /* scaling up here allows division by 1024 in critical code */
696 mifp->m6_rate_limit = mifcp->mif6c_rate_limit * 1024 / 1000;
697 #endif
698 /* initialize per mif pkt counters */
699 mifp->m6_pkt_in = 0;
700 mifp->m6_pkt_out = 0;
701 mifp->m6_bytes_in = 0;
702 mifp->m6_bytes_out = 0;
703 splx(s);
704
705 /* Adjust nummifs up if the mifi is higher than nummifs */
706 if (nummifs <= mifcp->mif6c_mifi)
707 nummifs = mifcp->mif6c_mifi + 1;
708
709 #if MRT6DEBUG
710 if (mrt6debug)
711 log(LOG_DEBUG,
712 "add_mif #%d, phyint %s%d\n",
713 mifcp->mif6c_mifi,
714 ifp->if_name, ifp->if_unit);
715 #endif
716
717 return 0;
718 }
719
720 /*
721 * Delete a mif from the mif table
722 */
723 static int
724 del_m6if(mifip)
725 mifi_t *mifip;
726 {
727 register struct mif6 *mifp = mif6table + *mifip;
728 register mifi_t mifi;
729 struct ifnet *ifp;
730 #if !(defined(__FreeBSD__) && __FreeBSD__ >= 3) && !defined(__APPLE__)
731 struct in6_ifreq ifr;
732 #endif
733 int s;
734
735 if (*mifip >= nummifs)
736 return EINVAL;
737 if (mifp->m6_ifp == NULL)
738 return EINVAL;
739
740 #if __NetBSD__
741 s = splsoftnet();
742 #else
743 s = splnet();
744 #endif
745
746 if (!(mifp->m6_flags & MIFF_REGISTER)) {
747 /*
748 * XXX: what if there is yet IPv4 multicast daemon
749 * using the interface?
750 */
751 ifp = mifp->m6_ifp;
752
753 #if (defined(__FreeBSD__) && __FreeBSD__ >= 3) || defined (__APPLE__)
754 if_allmulti(ifp, 0);
755 #else
756 ifr.ifr_addr.sin6_family = AF_INET6;
757 ifr.ifr_addr.sin6_addr = in6addr_any;
758 (*ifp->if_ioctl)(ifp, SIOCDELMULTI, (caddr_t)&ifr);
759 #endif
760 }
761
762 #if notyet
763 bzero((caddr_t)qtable[*mifip], sizeof(qtable[*mifip]));
764 bzero((caddr_t)mifp->m6_tbf, sizeof(*(mifp->m6_tbf)));
765 #endif
766 bzero((caddr_t)mifp, sizeof (*mifp));
767
768 /* Adjust nummifs down */
769 for (mifi = nummifs; mifi > 0; mifi--)
770 if (mif6table[mifi - 1].m6_ifp)
771 break;
772 nummifs = mifi;
773
774 splx(s);
775
776 #if MRT6DEBUG
777 if (mrt6debug)
778 log(LOG_DEBUG, "del_m6if %d, nummifs %d\n", *mifip, nummifs);
779 #endif
780
781 return 0;
782 }
783
784 /*
785 * Add an mfc entry
786 */
787 static int
788 add_m6fc(mfccp)
789 struct mf6cctl *mfccp;
790 {
791 struct mf6c *rt;
792 u_long hash;
793 struct rtdetq *rte;
794 register u_short nstl;
795 int s;
796
797 MF6CFIND(mfccp->mf6cc_origin.sin6_addr,
798 mfccp->mf6cc_mcastgrp.sin6_addr, rt);
799
800 /* If an entry already exists, just update the fields */
801 if (rt) {
802 #if MRT6DEBUG
803 if (mrt6debug & DEBUG_MFC)
804 log(LOG_DEBUG,"add_m6fc update o %s g %s p %x\n",
805 ip6_sprintf(&mfccp->mf6cc_origin.sin6_addr),
806 ip6_sprintf(&mfccp->mf6cc_mcastgrp.sin6_addr),
807 mfccp->mf6cc_parent);
808 #endif
809
810 #if __NetBSD__
811 s = splsoftnet();
812 #else
813 s = splnet();
814 #endif
815 rt->mf6c_parent = mfccp->mf6cc_parent;
816 rt->mf6c_ifset = mfccp->mf6cc_ifset;
817 splx(s);
818 return 0;
819 }
820
821 /*
822 * Find the entry for which the upcall was made and update
823 */
824 #if __NetBSD__
825 s = splsoftnet();
826 #else
827 s = splnet();
828 #endif
829 hash = MF6CHASH(mfccp->mf6cc_origin.sin6_addr,
830 mfccp->mf6cc_mcastgrp.sin6_addr);
831 for (rt = mf6ctable[hash], nstl = 0; rt; rt = rt->mf6c_next) {
832 if (IN6_ARE_ADDR_EQUAL(&rt->mf6c_origin.sin6_addr,
833 &mfccp->mf6cc_origin.sin6_addr) &&
834 IN6_ARE_ADDR_EQUAL(&rt->mf6c_mcastgrp.sin6_addr,
835 &mfccp->mf6cc_mcastgrp.sin6_addr) &&
836 (rt->mf6c_stall != NULL)) {
837
838 if (nstl++)
839 log(LOG_ERR,
840 "add_m6fc: %s o %s g %s p %x dbx %p\n",
841 "multiple kernel entries",
842 ip6_sprintf(&mfccp->mf6cc_origin.sin6_addr),
843 ip6_sprintf(&mfccp->mf6cc_mcastgrp.sin6_addr),
844 mfccp->mf6cc_parent, rt->mf6c_stall);
845
846 #if MRT6DEBUG
847 if (mrt6debug & DEBUG_MFC)
848 log(LOG_DEBUG,
849 "add_m6fc o %s g %s p %x dbg %x\n",
850 ip6_sprintf(&mfccp->mf6cc_origin.sin6_addr),
851 ip6_sprintf(&mfccp->mf6cc_mcastgrp.sin6_addr),
852 mfccp->mf6cc_parent, rt->mf6c_stall);
853 #endif
854
855 rt->mf6c_origin = mfccp->mf6cc_origin;
856 rt->mf6c_mcastgrp = mfccp->mf6cc_mcastgrp;
857 rt->mf6c_parent = mfccp->mf6cc_parent;
858 rt->mf6c_ifset = mfccp->mf6cc_ifset;
859 /* initialize pkt counters per src-grp */
860 rt->mf6c_pkt_cnt = 0;
861 rt->mf6c_byte_cnt = 0;
862 rt->mf6c_wrong_if = 0;
863
864 rt->mf6c_expire = 0; /* Don't clean this guy up */
865 nexpire[hash]--;
866
867 /* free packets Qed at the end of this entry */
868 for (rte = rt->mf6c_stall; rte != NULL; ) {
869 struct rtdetq *n = rte->next;
870 ip6_mdq(rte->m, rte->ifp, rt);
871 m_freem(rte->m);
872 #if UPCALL_TIMING
873 collate(&(rte->t));
874 #endif /* UPCALL_TIMING */
875 _FREE(rte, M_MRTABLE);
876 rte = n;
877 }
878 rt->mf6c_stall = NULL;
879 }
880 }
881
882 /*
883 * It is possible that an entry is being inserted without an upcall
884 */
885 if (nstl == 0) {
886 #if MRT6DEBUG
887 if (mrt6debug & DEBUG_MFC)
888 log(LOG_DEBUG,"add_mfc no upcall h %d o %s g %s p %x\n",
889 hash,
890 ip6_sprintf(&mfccp->mf6cc_origin.sin6_addr),
891 ip6_sprintf(&mfccp->mf6cc_mcastgrp.sin6_addr),
892 mfccp->mf6cc_parent);
893 #endif
894
895 for (rt = mf6ctable[hash]; rt; rt = rt->mf6c_next) {
896
897 if (IN6_ARE_ADDR_EQUAL(&rt->mf6c_origin.sin6_addr,
898 &mfccp->mf6cc_origin.sin6_addr)&&
899 IN6_ARE_ADDR_EQUAL(&rt->mf6c_mcastgrp.sin6_addr,
900 &mfccp->mf6cc_mcastgrp.sin6_addr)) {
901
902 rt->mf6c_origin = mfccp->mf6cc_origin;
903 rt->mf6c_mcastgrp = mfccp->mf6cc_mcastgrp;
904 rt->mf6c_parent = mfccp->mf6cc_parent;
905 /* initialize pkt counters per src-grp */
906 rt->mf6c_pkt_cnt = 0;
907 rt->mf6c_byte_cnt = 0;
908 rt->mf6c_wrong_if = 0;
909
910 if (rt->mf6c_expire)
911 nexpire[hash]--;
912 rt->mf6c_expire = 0;
913 }
914 }
915 if (rt == NULL) {
916 /* no upcall, so make a new entry */
917 rt = (struct mf6c *)_MALLOC(sizeof(*rt), M_MRTABLE,
918 M_NOWAIT);
919 if (rt == NULL) {
920 splx(s);
921 return ENOBUFS;
922 }
923
924 /* insert new entry at head of hash chain */
925 rt->mf6c_origin = mfccp->mf6cc_origin;
926 rt->mf6c_mcastgrp = mfccp->mf6cc_mcastgrp;
927 rt->mf6c_parent = mfccp->mf6cc_parent;
928 /* initialize pkt counters per src-grp */
929 rt->mf6c_pkt_cnt = 0;
930 rt->mf6c_byte_cnt = 0;
931 rt->mf6c_wrong_if = 0;
932 rt->mf6c_expire = 0;
933 rt->mf6c_stall = NULL;
934
935 /* link into table */
936 rt->mf6c_next = mf6ctable[hash];
937 mf6ctable[hash] = rt;
938 }
939 }
940 splx(s);
941 return 0;
942 }
943
944 #if UPCALL_TIMING
945 /*
946 * collect delay statistics on the upcalls
947 */
948 static void
949 collate(t)
950 register struct timeval *t;
951 {
952 register u_long d;
953 register struct timeval tp;
954 register u_long delta;
955
956 GET_TIME(tp);
957
958 if (TV_LT(*t, tp))
959 {
960 TV_DELTA(tp, *t, delta);
961
962 d = delta >> 10;
963 if (d > UPCALL_MAX)
964 d = UPCALL_MAX;
965
966 ++upcall_data[d];
967 }
968 }
969 #endif /* UPCALL_TIMING */
970
971 /*
972 * Delete an mfc entry
973 */
974 static int
975 del_m6fc(mfccp)
976 struct mf6cctl *mfccp;
977 {
978 struct sockaddr_in6 origin;
979 struct sockaddr_in6 mcastgrp;
980 struct mf6c *rt;
981 struct mf6c **nptr;
982 u_long hash;
983 int s;
984
985 origin = mfccp->mf6cc_origin;
986 mcastgrp = mfccp->mf6cc_mcastgrp;
987 hash = MF6CHASH(origin.sin6_addr, mcastgrp.sin6_addr);
988
989 #if MRT6DEBUG
990 if (mrt6debug & DEBUG_MFC)
991 log(LOG_DEBUG,"del_m6fc orig %s mcastgrp %s\n",
992 ip6_sprintf(&origin.sin6_addr),
993 ip6_sprintf(&mcastgrp.sin6_addr));
994 #endif
995
996 #if __NetBSD__
997 s = splsoftnet();
998 #else
999 s = splnet();
1000 #endif
1001
1002 nptr = &mf6ctable[hash];
1003 while ((rt = *nptr) != NULL) {
1004 if (IN6_ARE_ADDR_EQUAL(&origin.sin6_addr,
1005 &rt->mf6c_origin.sin6_addr) &&
1006 IN6_ARE_ADDR_EQUAL(&mcastgrp.sin6_addr,
1007 &rt->mf6c_mcastgrp.sin6_addr) &&
1008 rt->mf6c_stall == NULL)
1009 break;
1010
1011 nptr = &rt->mf6c_next;
1012 }
1013 if (rt == NULL) {
1014 splx(s);
1015 return EADDRNOTAVAIL;
1016 }
1017
1018 *nptr = rt->mf6c_next;
1019 _FREE(rt, M_MRTABLE);
1020
1021 splx(s);
1022
1023 return 0;
1024 }
1025
1026 static int
1027 socket_send(s, mm, src)
1028 struct socket *s;
1029 struct mbuf *mm;
1030 struct sockaddr_in6 *src;
1031 {
1032 if (s) {
1033 if (sbappendaddr(&s->so_rcv,
1034 (struct sockaddr *)src,
1035 mm, (struct mbuf *)0) != 0) {
1036 sorwakeup(s);
1037 return 0;
1038 }
1039 }
1040 m_freem(mm);
1041 return -1;
1042 }
1043
1044 /*
1045 * IPv6 multicast forwarding function. This function assumes that the packet
1046 * pointed to by "ip6" has arrived on (or is about to be sent to) the interface
1047 * pointed to by "ifp", and the packet is to be relayed to other networks
1048 * that have members of the packet's destination IPv6 multicast group.
1049 *
1050 * The packet is returned unscathed to the caller, unless it is
1051 * erroneous, in which case a non-zero return value tells the caller to
1052 * discard it.
1053 */
1054
1055 int
1056 ip6_mforward(ip6, ifp, m)
1057 register struct ip6_hdr *ip6;
1058 struct ifnet *ifp;
1059 struct mbuf *m;
1060 {
1061 register struct mf6c *rt;
1062 register struct mif6 *mifp;
1063 register struct mbuf *mm;
1064 int s;
1065 mifi_t mifi;
1066
1067 #if MRT6DEBUG
1068 if (mrt6debug & DEBUG_FORWARD)
1069 log(LOG_DEBUG, "ip6_mforward: src %s, dst %s, ifindex %d\n",
1070 ip6_sprintf(&ip6->ip6_src), ip6_sprintf(&ip6->ip6_dst),
1071 ifp->if_index);
1072 #endif
1073
1074 /*
1075 * Don't forward a packet with Hop limit of zero or one,
1076 * or a packet destined to a local-only group.
1077 */
1078 if (ip6->ip6_hlim <= 1 || IN6_IS_ADDR_MC_NODELOCAL(&ip6->ip6_dst) ||
1079 IN6_IS_ADDR_MC_LINKLOCAL(&ip6->ip6_dst))
1080 return 0;
1081 ip6->ip6_hlim--;
1082
1083 /*
1084 * Determine forwarding mifs from the forwarding cache table
1085 */
1086 #if __NetBSD__
1087 s = splsoftnet();
1088 #else
1089 s = splnet();
1090 #endif
1091 MF6CFIND(ip6->ip6_src, ip6->ip6_dst, rt);
1092
1093 /* Entry exists, so forward if necessary */
1094 if (rt) {
1095 splx(s);
1096 return (ip6_mdq(m, ifp, rt));
1097 } else {
1098 /*
1099 * If we don't have a route for packet's origin,
1100 * Make a copy of the packet &
1101 * send message to routing daemon
1102 */
1103
1104 register struct mbuf *mb0;
1105 register struct rtdetq *rte;
1106 register u_long hash;
1107 /* register int i, npkts;*/
1108 #if UPCALL_TIMING
1109 struct timeval tp;
1110
1111 GET_TIME(tp);
1112 #endif /* UPCALL_TIMING */
1113
1114 mrt6stat.mrt6s_no_route++;
1115 #if MRT6DEBUG
1116 if (mrt6debug & (DEBUG_FORWARD | DEBUG_MFC))
1117 log(LOG_DEBUG, "ip6_mforward: no rte s %s g %s\n",
1118 ip6_sprintf(&ip6->ip6_src),
1119 ip6_sprintf(&ip6->ip6_dst));
1120 #endif
1121
1122 /*
1123 * Allocate mbufs early so that we don't do extra work if we
1124 * are just going to fail anyway.
1125 */
1126 rte = (struct rtdetq *)_MALLOC(sizeof(*rte), M_MRTABLE,
1127 M_NOWAIT);
1128 if (rte == NULL) {
1129 splx(s);
1130 return ENOBUFS;
1131 }
1132 mb0 = m_copy(m, 0, M_COPYALL);
1133 /*
1134 * Pullup packet header if needed before storing it,
1135 * as other references may modify it in the meantime.
1136 */
1137 if (mb0 &&
1138 (M_HASCL(mb0) || mb0->m_len < sizeof(struct ip6_hdr)))
1139 mb0 = m_pullup(mb0, sizeof(struct ip6_hdr));
1140 if (mb0 == NULL) {
1141 _FREE(rte, M_MRTABLE);
1142 splx(s);
1143 return ENOBUFS;
1144 }
1145
1146 /* is there an upcall waiting for this packet? */
1147 hash = MF6CHASH(ip6->ip6_src, ip6->ip6_dst);
1148 for (rt = mf6ctable[hash]; rt; rt = rt->mf6c_next) {
1149 if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_src,
1150 &rt->mf6c_origin.sin6_addr) &&
1151 IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
1152 &rt->mf6c_mcastgrp.sin6_addr) &&
1153 (rt->mf6c_stall != NULL))
1154 break;
1155 }
1156
1157 if (rt == NULL) {
1158 struct mrt6msg *im;
1159
1160 /* no upcall, so make a new entry */
1161 rt = (struct mf6c *)_MALLOC(sizeof(*rt), M_MRTABLE,
1162 M_NOWAIT);
1163 if (rt == NULL) {
1164 _FREE(rte, M_MRTABLE);
1165 m_freem(mb0);
1166 splx(s);
1167 return ENOBUFS;
1168 }
1169 /*
1170 * Make a copy of the header to send to the user
1171 * level process
1172 */
1173 mm = m_copy(mb0, 0, sizeof(struct ip6_hdr));
1174
1175 if (mm == NULL) {
1176 _FREE(rte, M_MRTABLE);
1177 m_freem(mb0);
1178 _FREE(rt, M_MRTABLE);
1179 splx(s);
1180 return ENOBUFS;
1181 }
1182
1183 /*
1184 * Send message to routing daemon
1185 */
1186 sin6.sin6_addr = ip6->ip6_src;
1187
1188 im = mtod(mm, struct mrt6msg *);
1189 im->im6_msgtype = MRT6MSG_NOCACHE;
1190 im->im6_mbz = 0;
1191
1192 #if MRT6DEBUG
1193 if (mrt6debug & DEBUG_FORWARD)
1194 log(LOG_DEBUG,
1195 "getting the iif info in the kernel\n");
1196 #endif
1197
1198 for (mifp = mif6table, mifi = 0;
1199 mifi < nummifs && mifp->m6_ifp != ifp;
1200 mifp++, mifi++)
1201 ;
1202
1203 im->im6_mif = mifi;
1204
1205 if (socket_send(ip6_mrouter, mm, &sin6) < 0) {
1206 log(LOG_WARNING, "ip6_mforward: ip6_mrouter "
1207 "socket queue full\n");
1208 mrt6stat.mrt6s_upq_sockfull++;
1209 _FREE(rte, M_MRTABLE);
1210 m_freem(mb0);
1211 _FREE(rt, M_MRTABLE);
1212 splx(s);
1213 return ENOBUFS;
1214 }
1215
1216 mrt6stat.mrt6s_upcalls++;
1217
1218 /* insert new entry at head of hash chain */
1219 bzero(rt, sizeof(*rt));
1220 rt->mf6c_origin.sin6_family = AF_INET6;
1221 rt->mf6c_origin.sin6_len = sizeof(struct sockaddr_in6);
1222 rt->mf6c_origin.sin6_addr = ip6->ip6_src;
1223 rt->mf6c_mcastgrp.sin6_family = AF_INET6;
1224 rt->mf6c_mcastgrp.sin6_len = sizeof(struct sockaddr_in6);
1225 rt->mf6c_mcastgrp.sin6_addr = ip6->ip6_dst;
1226 rt->mf6c_expire = UPCALL_EXPIRE;
1227 nexpire[hash]++;
1228 rt->mf6c_parent = MF6C_INCOMPLETE_PARENT;
1229
1230 /* link into table */
1231 rt->mf6c_next = mf6ctable[hash];
1232 mf6ctable[hash] = rt;
1233 /* Add this entry to the end of the queue */
1234 rt->mf6c_stall = rte;
1235 } else {
1236 /* determine if q has overflowed */
1237 struct rtdetq **p;
1238 register int npkts = 0;
1239
1240 for (p = &rt->mf6c_stall; *p != NULL; p = &(*p)->next)
1241 if (++npkts > MAX_UPQ6) {
1242 mrt6stat.mrt6s_upq_ovflw++;
1243 _FREE(rte, M_MRTABLE);
1244 m_freem(mb0);
1245 splx(s);
1246 return 0;
1247 }
1248
1249 /* Add this entry to the end of the queue */
1250 *p = rte;
1251 }
1252
1253 rte->next = NULL;
1254 rte->m = mb0;
1255 rte->ifp = ifp;
1256 #if UPCALL_TIMING
1257 rte->t = tp;
1258 #endif /* UPCALL_TIMING */
1259
1260 splx(s);
1261
1262 return 0;
1263 }
1264 }
1265
1266 /*
1267 * Clean up cache entries if upcalls are not serviced
1268 * Call from the Slow Timeout mechanism, every half second.
1269 */
1270 static void
1271 expire_upcalls(unused)
1272 void *unused;
1273 {
1274 struct rtdetq *rte;
1275 struct mf6c *mfc, **nptr;
1276 int i;
1277 int s;
1278 #ifdef __APPLE__
1279 boolean_t funnel_state;
1280 funnel_state = thread_set_funneled(TRUE);
1281 #endif
1282
1283 #if __NetBSD__
1284 s = splsoftnet();
1285 #else
1286 s = splnet();
1287 #endif
1288 for (i = 0; i < MF6CTBLSIZ; i++) {
1289 if (nexpire[i] == 0)
1290 continue;
1291 nptr = &mf6ctable[i];
1292 while ((mfc = *nptr) != NULL) {
1293 rte = mfc->mf6c_stall;
1294 /*
1295 * Skip real cache entries
1296 * Make sure it wasn't marked to not expire (shouldn't happen)
1297 * If it expires now
1298 */
1299 if (rte != NULL &&
1300 mfc->mf6c_expire != 0 &&
1301 --mfc->mf6c_expire == 0) {
1302 #if MRT6DEBUG
1303 if (mrt6debug & DEBUG_EXPIRE)
1304 log(LOG_DEBUG, "expire_upcalls: expiring (%s %s)\n",
1305 ip6_sprintf(&mfc->mf6c_origin.sin6_addr),
1306 ip6_sprintf(&mfc->mf6c_mcastgrp.sin6_addr));
1307 #endif
1308 /*
1309 * drop all the packets
1310 * free the mbuf with the pkt, if, timing info
1311 */
1312 do {
1313 struct rtdetq *n = rte->next;
1314 m_freem(rte->m);
1315 _FREE(rte, M_MRTABLE);
1316 rte = n;
1317 } while (rte != NULL);
1318 mrt6stat.mrt6s_cache_cleanups++;
1319 nexpire[i]--;
1320
1321 *nptr = mfc->mf6c_next;
1322 _FREE(mfc, M_MRTABLE);
1323 } else {
1324 nptr = &mfc->mf6c_next;
1325 }
1326 }
1327 }
1328 splx(s);
1329 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
1330 expire_upcalls_ch =
1331 #endif
1332 timeout(expire_upcalls, (caddr_t)NULL, EXPIRE_TIMEOUT);
1333 #ifdef __APPLE__
1334 (void) thread_set_funneled(funnel_state);
1335 #endif
1336 }
1337
1338 /*
1339 * Packet forwarding routine once entry in the cache is made
1340 */
1341 static int
1342 ip6_mdq(m, ifp, rt)
1343 register struct mbuf *m;
1344 register struct ifnet *ifp;
1345 register struct mf6c *rt;
1346 {
1347 register struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1348 register mifi_t mifi, iif;
1349 register struct mif6 *mifp;
1350 register int plen = m->m_pkthdr.len;
1351
1352 /*
1353 * Macro to send packet on mif. Since RSVP packets don't get counted on
1354 * input, they shouldn't get counted on output, so statistics keeping is
1355 * seperate.
1356 */
1357
1358 #define MC6_SEND(ip6,mifp,m) { \
1359 if ((mifp)->m6_flags & MIFF_REGISTER) \
1360 register_send((ip6), (mifp), (m)); \
1361 else \
1362 phyint_send((ip6), (mifp), (m)); \
1363 }
1364
1365 /*
1366 * Don't forward if it didn't arrive from the parent mif
1367 * for its origin.
1368 */
1369 mifi = rt->mf6c_parent;
1370 if ((mifi >= nummifs) || (mif6table[mifi].m6_ifp != ifp)) {
1371 /* came in the wrong interface */
1372 #if MRT6DEBUG
1373 if (mrt6debug & DEBUG_FORWARD)
1374 log(LOG_DEBUG,
1375 "wrong if: ifid %d mifi %d mififid %x\n",
1376 ifp->if_index, mifi,
1377 mif6table[mifi].m6_ifp->if_index);
1378 #endif
1379 mrt6stat.mrt6s_wrong_if++;
1380 rt->mf6c_wrong_if++;
1381 /*
1382 * If we are doing PIM processing, and we are forwarding
1383 * packets on this interface, send a message to the
1384 * routing daemon.
1385 */
1386 if(mifi < nummifs) /* have to make sure this is a valid mif */
1387 if(mif6table[mifi].m6_ifp)
1388
1389 if (pim6 && (m->m_flags & M_LOOP) == 0) {
1390 /*
1391 * Check the M_LOOP flag to avoid an
1392 * unnecessary PIM assert.
1393 * XXX: M_LOOP is an ad-hoc hack...
1394 */
1395 static struct sockaddr_in6 sin6 =
1396 { sizeof(sin6), AF_INET6 };
1397
1398 register struct mbuf *mm;
1399 struct mrt6msg *im;
1400
1401 mm = m_copy(m, 0,
1402 sizeof(struct ip6_hdr));
1403 if (mm &&
1404 (M_HASCL(mm) ||
1405 mm->m_len < sizeof(struct ip6_hdr)))
1406 mm = m_pullup(mm, sizeof(struct ip6_hdr));
1407 if (mm == NULL)
1408 return ENOBUFS;
1409
1410 im = mtod(mm, struct mrt6msg *);
1411 im->im6_msgtype = MRT6MSG_WRONGMIF;
1412 im->im6_mbz = 0;
1413
1414 for (mifp = mif6table, iif = 0;
1415 iif < nummifs && mifp &&
1416 mifp->m6_ifp != ifp;
1417 mifp++, iif++);
1418
1419 im->im6_mif = iif;
1420
1421 sin6.sin6_addr = im->im6_src;
1422
1423 mrt6stat.mrt6s_upcalls++;
1424
1425 if (socket_send(ip6_mrouter, mm,
1426 &sin6) < 0) {
1427 #if MRT6DEBUG
1428 if (mrt6debug)
1429 log(LOG_WARNING, "mdq, ip6_mrouter socket queue full\n");
1430 #endif
1431 ++mrt6stat.mrt6s_upq_sockfull;
1432 return ENOBUFS;
1433 } /* if socket Q full */
1434 } /* if PIM */
1435 return 0;
1436 } /* if wrong iif */
1437
1438 /* If I sourced this packet, it counts as output, else it was input. */
1439 if (m->m_pkthdr.rcvif == NULL) {
1440 /* XXX: is rcvif really NULL when output?? */
1441 mif6table[mifi].m6_pkt_out++;
1442 mif6table[mifi].m6_bytes_out += plen;
1443 } else {
1444 mif6table[mifi].m6_pkt_in++;
1445 mif6table[mifi].m6_bytes_in += plen;
1446 }
1447 rt->mf6c_pkt_cnt++;
1448 rt->mf6c_byte_cnt += plen;
1449
1450 /*
1451 * For each mif, forward a copy of the packet if there are group
1452 * members downstream on the interface.
1453 */
1454 for (mifp = mif6table, mifi = 0; mifi < nummifs; mifp++, mifi++)
1455 if (IF_ISSET(mifi, &rt->mf6c_ifset)) {
1456 mifp->m6_pkt_out++;
1457 mifp->m6_bytes_out += plen;
1458 MC6_SEND(ip6, mifp, m);
1459 }
1460 return 0;
1461 }
1462
1463 static void
1464 phyint_send(ip6, mifp, m)
1465 struct ip6_hdr *ip6;
1466 struct mif6 *mifp;
1467 struct mbuf *m;
1468 {
1469 register struct mbuf *mb_copy;
1470 struct ifnet *ifp = mifp->m6_ifp;
1471 int error = 0;
1472 #if __NetBSD__
1473 int s = splsoftnet();
1474 #else
1475 int s = splnet();
1476 #endif
1477 static struct route_in6 ro6;
1478 struct in6_multi *in6m;
1479
1480 /*
1481 * Make a new reference to the packet; make sure that
1482 * the IPv6 header is actually copied, not just referenced,
1483 * so that ip6_output() only scribbles on the copy.
1484 */
1485 mb_copy = m_copy(m, 0, M_COPYALL);
1486 if (mb_copy &&
1487 (M_HASCL(mb_copy) || mb_copy->m_len < sizeof(struct ip6_hdr)))
1488 mb_copy = m_pullup(mb_copy, sizeof(struct ip6_hdr));
1489 if (mb_copy == NULL)
1490 return;
1491 /* set MCAST flag to the outgoing packet */
1492 mb_copy->m_flags |= M_MCAST;
1493
1494 /*
1495 * If we sourced the packet, call ip6_output since we may devide
1496 * the packet into fragments when the packet is too big for the
1497 * outgoing interface.
1498 * Otherwise, we can simply send the packet to the interface
1499 * sending queue.
1500 */
1501 if (m->m_pkthdr.rcvif == NULL) {
1502 struct ip6_moptions im6o;
1503
1504 im6o.im6o_multicast_ifp = ifp;
1505 /* XXX: ip6_output will override ip6->ip6_hlim */
1506 im6o.im6o_multicast_hlim = ip6->ip6_hlim;
1507 im6o.im6o_multicast_loop = 1;
1508 error = ip6_output(mb_copy, NULL, &ro6,
1509 IPV6_FORWARDING, &im6o, NULL);
1510
1511 #if MRT6DEBUG
1512 if (mrt6debug & DEBUG_XMIT)
1513 log(LOG_DEBUG, "phyint_send on mif %d err %d\n",
1514 mifp - mif6table, error);
1515 #endif
1516 splx(s);
1517 return;
1518 }
1519
1520 /*
1521 * If we belong to the destination multicast group
1522 * on the outgoing interface, loop back a copy.
1523 */
1524 IN6_LOOKUP_MULTI(ip6->ip6_dst, ifp, in6m);
1525 if (in6m != NULL) {
1526 ro6.ro_dst.sin6_len = sizeof(struct sockaddr_in6);
1527 ro6.ro_dst.sin6_family = AF_INET6;
1528 ro6.ro_dst.sin6_addr = ip6->ip6_dst;
1529 ip6_mloopback(ifp, m, &ro6.ro_dst);
1530 }
1531 /*
1532 * Put the packet into the sending queue of the outgoing interface
1533 * if it would fit in the MTU of the interface.
1534 */
1535 if (mb_copy->m_pkthdr.len < ifp->if_mtu || ifp->if_mtu < IPV6_MMTU) {
1536 ro6.ro_dst.sin6_len = sizeof(struct sockaddr_in6);
1537 ro6.ro_dst.sin6_family = AF_INET6;
1538 ro6.ro_dst.sin6_addr = ip6->ip6_dst;
1539 /*
1540 * We just call if_output instead of nd6_output here, since
1541 * we need no ND for a multicast forwarded packet...right?
1542 */
1543 #ifdef __APPLE__
1544 error = dlil_output(ifptodlt(ifp, PF_INET6), mb_copy,
1545 NULL, (struct sockaddr *)&ro6.ro_dst, 0);
1546 #else
1547 error = (*ifp->if_output)(ifp, mb_copy,
1548 (struct sockaddr *)&ro6.ro_dst,
1549 NULL);
1550 #endif
1551 #if MRT6DEBUG
1552 if (mrt6debug & DEBUG_XMIT)
1553 log(LOG_DEBUG, "phyint_send on mif %d err %d\n",
1554 mifp - mif6table, error);
1555 #endif
1556 }
1557 else {
1558 #if MULTICAST_PMTUD
1559 icmp6_error(mb_copy, ICMP6_PACKET_TOO_BIG, 0, ifp->if_mtu);
1560 return;
1561 #else
1562 #if MRT6DEBUG
1563 #if __NetBSD__
1564 if (mrt6debug & DEBUG_DEBUG_XMIT)
1565 log(LOG_DEBUG,
1566 "phyint_send: packet too big on %s o %s g %s"
1567 " size %d(discarded)\n",
1568 ifp->if_xname,
1569 ip6_sprintf(&ip6->ip6_src),
1570 ip6_sprintf(&ip6->ip6_dst),
1571 mb_copy->m_pkthdr.len);
1572 #else
1573 if (mrt6debug & DEBUG_XMIT)
1574 log(LOG_DEBUG,
1575 "phyint_send: packet too big on %s%u o %s g %s"
1576 " size %d(discarded)\n",
1577 ifp->if_name, ifp->if_unit,
1578 ip6_sprintf(&ip6->ip6_src),
1579 ip6_sprintf(&ip6->ip6_dst),
1580 mb_copy->m_pkthdr.len);
1581 #endif /* __NetBSD__ */
1582 #endif /* MRT6DEBUG */
1583 m_freem(mb_copy); /* simply discard the packet */
1584 return;
1585 #endif
1586 }
1587 }
1588
1589 static int
1590 register_send(ip6, mif, m)
1591 register struct ip6_hdr *ip6;
1592 struct mif6 *mif;
1593 register struct mbuf *m;
1594 {
1595 register struct mbuf *mm;
1596 register int i, len = m->m_pkthdr.len;
1597 static struct sockaddr_in6 sin6 = { sizeof(sin6), AF_INET6 };
1598 struct mrt6msg *im6;
1599
1600 #if MRT6DEBUG
1601 if (mrt6debug)
1602 log(LOG_DEBUG, "** IPv6 register_send **\n src %s dst %s\n",
1603 ip6_sprintf(&ip6->ip6_src), ip6_sprintf(&ip6->ip6_dst));
1604 #endif
1605 ++pim6stat.pim6s_snd_registers;
1606
1607 /* Make a copy of the packet to send to the user level process */
1608 MGETHDR(mm, M_DONTWAIT, MT_HEADER);
1609 if (mm == NULL)
1610 return ENOBUFS;
1611 mm->m_data += max_linkhdr;
1612 mm->m_len = sizeof(struct ip6_hdr);
1613
1614 if ((mm->m_next = m_copy(m, 0, M_COPYALL)) == NULL) {
1615 m_freem(mm);
1616 return ENOBUFS;
1617 }
1618 i = MHLEN - M_LEADINGSPACE(mm);
1619 if (i > len)
1620 i = len;
1621 mm = m_pullup(mm, i);
1622 if (mm == NULL){
1623 m_freem(mm);
1624 return ENOBUFS;
1625 }
1626 /* TODO: check it! */
1627 mm->m_pkthdr.len = len + sizeof(struct ip6_hdr);
1628
1629 /*
1630 * Send message to routing daemon
1631 */
1632 sin6.sin6_addr = ip6->ip6_src;
1633
1634 im6 = mtod(mm, struct mrt6msg *);
1635 im6->im6_msgtype = MRT6MSG_WHOLEPKT;
1636 im6->im6_mbz = 0;
1637
1638 im6->im6_mif = mif - mif6table;
1639
1640 /* iif info is not given for reg. encap.n */
1641 mrt6stat.mrt6s_upcalls++;
1642
1643 if (socket_send(ip6_mrouter, mm, &sin6) < 0) {
1644 #if MRT6DEBUG
1645 if (mrt6debug)
1646 log(LOG_WARNING,
1647 "register_send: ip_mrouter socket queue full\n");
1648 #endif
1649 ++mrt6stat.mrt6s_upq_sockfull;
1650 return ENOBUFS;
1651 }
1652 return 0;
1653 }
1654
1655 /*
1656 * PIM sparse mode hook
1657 * Receives the pim control messages, and passes them up to the listening
1658 * socket, using rip6_input.
1659 * The only message processed is the REGISTER pim message; the pim header
1660 * is stripped off, and the inner packet is passed to register_mforward.
1661 */
1662 int
1663 pim6_input(mp, offp, proto)
1664 struct mbuf **mp;
1665 int *offp, proto;
1666 {
1667 register struct pim *pim; /* pointer to a pim struct */
1668 register struct ip6_hdr *ip6;
1669 register int pimlen;
1670 struct mbuf *m = *mp;
1671 int minlen;
1672 int off = *offp;
1673
1674 ++pim6stat.pim6s_rcv_total;
1675
1676 ip6 = mtod(m, struct ip6_hdr *);
1677 pimlen = m->m_pkthdr.len - *offp;
1678
1679 /*
1680 * Validate lengths
1681 */
1682 if (pimlen < PIM_MINLEN) {
1683 ++pim6stat.pim6s_rcv_tooshort;
1684 #if MRT6DEBUG
1685 if (mrt6debug & DEBUG_PIM)
1686 log(LOG_DEBUG,"pim6_input: PIM packet too short\n");
1687 #endif
1688 m_freem(m);
1689 return(IPPROTO_DONE);
1690 }
1691
1692 /*
1693 * if the packet is at least as big as a REGISTER, go ahead
1694 * and grab the PIM REGISTER header size, to avoid another
1695 * possible m_pullup() later.
1696 *
1697 * PIM_MINLEN == pimhdr + u_int32 == 8
1698 * PIM6_REG_MINLEN == pimhdr + reghdr + eip6hdr == 4 + 4 + 40
1699 */
1700 minlen = (pimlen >= PIM6_REG_MINLEN) ? PIM6_REG_MINLEN : PIM_MINLEN;
1701
1702 /*
1703 * Make sure that the IP6 and PIM headers in contiguous memory, and
1704 * possibly the PIM REGISTER header
1705 */
1706 #ifndef PULLDOWN_TEST
1707 IP6_EXTHDR_CHECK(m, off, minlen, IPPROTO_DONE);
1708 /* adjust pointer */
1709 ip6 = mtod(m, struct ip6_hdr *);
1710
1711 /* adjust mbuf to point to the PIM header */
1712 pim = (struct pim *)((caddr_t)ip6 + off);
1713 #else
1714 IP6_EXTHDR_GET(pim, struct pim *, m, off, minlen);
1715 if (pim == NULL) {
1716 pim6stat.pim6s_rcv_tooshort++;
1717 return IPPROTO_DONE;
1718 }
1719 #endif
1720
1721
1722 #define PIM6_CHECKSUM 1
1723 #if PIM6_CHECKSUM
1724 {
1725 int cksumlen;
1726
1727 /*
1728 * Validate checksum.
1729 * If PIM REGISTER, exclude the data packet
1730 */
1731 if (pim->pim_type == PIM_REGISTER)
1732 cksumlen = PIM_MINLEN;
1733 else
1734 cksumlen = pimlen;
1735
1736 if (in6_cksum(m, IPPROTO_PIM, off, cksumlen)) {
1737 ++pim6stat.pim6s_rcv_badsum;
1738 #if MRT6DEBUG
1739 if (mrt6debug & DEBUG_PIM)
1740 log(LOG_DEBUG,
1741 "pim6_input: invalid checksum\n");
1742 #endif
1743 m_freem(m);
1744 return(IPPROTO_DONE);
1745 }
1746 }
1747 #endif /* PIM_CHECKSUM */
1748
1749 /* PIM version check */
1750 if (pim->pim_ver != PIM_VERSION) {
1751 ++pim6stat.pim6s_rcv_badversion;
1752 #if MRT6DEBUG
1753 log(LOG_ERR,
1754 "pim6_input: incorrect version %d, expecting %d\n",
1755 pim->pim_ver, PIM_VERSION);
1756 #endif
1757 m_freem(m);
1758 return(IPPROTO_DONE);
1759 }
1760
1761 if (pim->pim_type == PIM_REGISTER) {
1762 /*
1763 * since this is a REGISTER, we'll make a copy of the register
1764 * headers ip6+pim+u_int32_t+encap_ip6, to be passed up to the
1765 * routing daemon.
1766 */
1767 static struct sockaddr_in6 dst = { sizeof(dst), AF_INET6 };
1768
1769 struct mbuf *mcp;
1770 struct ip6_hdr *eip6;
1771 u_int32_t *reghdr;
1772 int rc;
1773
1774 ++pim6stat.pim6s_rcv_registers;
1775
1776 if ((reg_mif_num >= nummifs) || (reg_mif_num == (mifi_t) -1)) {
1777 #if MRT6DEBUG
1778 if (mrt6debug & DEBUG_PIM)
1779 log(LOG_DEBUG,
1780 "pim6_input: register mif not set: %d\n",
1781 reg_mif_num);
1782 #endif
1783 m_freem(m);
1784 return(IPPROTO_DONE);
1785 }
1786
1787 reghdr = (u_int32_t *)(pim + 1);
1788
1789 if ((ntohl(*reghdr) & PIM_NULL_REGISTER))
1790 goto pim6_input_to_daemon;
1791
1792 /*
1793 * Validate length
1794 */
1795 if (pimlen < PIM6_REG_MINLEN) {
1796 ++pim6stat.pim6s_rcv_tooshort;
1797 ++pim6stat.pim6s_rcv_badregisters;
1798 #if MRT6DEBUG
1799 log(LOG_ERR,
1800 "pim6_input: register packet size too "
1801 "small %d from %s\n",
1802 pimlen, ip6_sprintf(&ip6->ip6_src));
1803 #endif
1804 m_freem(m);
1805 return(IPPROTO_DONE);
1806 }
1807
1808 eip6 = (struct ip6_hdr *) (reghdr + 1);
1809 #if MRT6DEBUG
1810 if (mrt6debug & DEBUG_PIM)
1811 log(LOG_DEBUG,
1812 "pim6_input[register], eip6: %s -> %s, "
1813 "eip6 plen %d\n",
1814 ip6_sprintf(&eip6->ip6_src),
1815 ip6_sprintf(&eip6->ip6_dst),
1816 ntohs(eip6->ip6_plen));
1817 #endif
1818
1819 /* verify the inner packet is destined to a mcast group */
1820 if (!IN6_IS_ADDR_MULTICAST(&eip6->ip6_dst)) {
1821 ++pim6stat.pim6s_rcv_badregisters;
1822 #if MRT6DEBUG
1823 if (mrt6debug & DEBUG_PIM)
1824 log(LOG_DEBUG,
1825 "pim6_input: inner packet of register "
1826 "is not multicast %s\n",
1827 ip6_sprintf(&eip6->ip6_dst));
1828 #endif
1829 m_freem(m);
1830 return(IPPROTO_DONE);
1831 }
1832
1833 /*
1834 * make a copy of the whole header to pass to the daemon later.
1835 */
1836 mcp = m_copy(m, 0, off + PIM6_REG_MINLEN);
1837 if (mcp == NULL) {
1838 #if MRT6DEBUG
1839 log(LOG_ERR,
1840 "pim6_input: pim register: "
1841 "could not copy register head\n");
1842 #endif
1843 m_freem(m);
1844 return(IPPROTO_DONE);
1845 }
1846
1847 /*
1848 * forward the inner ip6 packet; point m_data at the inner ip6.
1849 */
1850 m_adj(m, off + PIM_MINLEN);
1851 #if MRT6DEBUG
1852 if (mrt6debug & DEBUG_PIM) {
1853 log(LOG_DEBUG,
1854 "pim6_input: forwarding decapsulated register: "
1855 "src %s, dst %s, mif %d\n",
1856 ip6_sprintf(&eip6->ip6_src),
1857 ip6_sprintf(&eip6->ip6_dst),
1858 reg_mif_num);
1859 }
1860 #endif
1861
1862 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
1863 rc = if_simloop(mif6table[reg_mif_num].m6_ifp, m,
1864 (struct sockaddr *) &dst, NULL);
1865 #else
1866 #if defined (__APPLE__)
1867 if (lo_dl_tag == 0)
1868 dlil_find_dltag(APPLE_IF_FAM_LOOPBACK, 0, PF_INET6, &lo_dl_tag);
1869
1870 if (lo_dl_tag)
1871 dlil_output(lo_dl_tag, m, 0, (struct sockaddr *) &dst, 0);
1872 else {
1873 printf("Warning: pim6_input call to dlil_find_dltag failed!\n");
1874 m_freem(m);
1875 }
1876 #else
1877 rc = looutput(mif6table[reg_mif_num].m6_ifp, m,
1878 (struct sockaddr *) &dst,
1879 (struct rtentry *) NULL);
1880 #endif
1881 #endif
1882
1883 /* prepare the register head to send to the mrouting daemon */
1884 m = mcp;
1885 }
1886
1887 /*
1888 * Pass the PIM message up to the daemon; if it is a register message
1889 * pass the 'head' only up to the daemon. This includes the
1890 * encapsulator ip6 header, pim header, register header and the
1891 * encapsulated ip6 header.
1892 */
1893 pim6_input_to_daemon:
1894 rip6_input(&m, offp, proto);
1895 return(IPPROTO_DONE);
1896 }