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