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