2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (C) 1998 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * $KAME: ip6_mroute.c,v 1.58 2001/12/18 02:36:31 itojun Exp $
35 * Copyright (c) 1989 Stephen Deering
36 * Copyright (c) 1992, 1993
37 * The Regents of the University of California. All rights reserved.
39 * This code is derived from software contributed to Berkeley by
40 * Stephen Deering of Stanford University.
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * @(#)ip_mroute.c 8.2 (Berkeley) 11/15/93
67 * BSDI ip_mroute.c,v 2.10 1996/11/14 00:29:52 jch Exp
71 * IP multicast forwarding procedures
73 * Written by David Waitzman, BBN Labs, August 1988.
74 * Modified by Steve Deering, Stanford, February 1989.
75 * Modified by Mark J. Steiglitz, Stanford, May, 1991
76 * Modified by Van Jacobson, LBL, January 1993
77 * Modified by Ajit Thyagarajan, PARC, August 1993
78 * Modified by Bill Fenner, PARC, April 1994
80 * MROUTING Revision: 3.5.1.2 + PIM-SMv2 (pimd) Support
83 #include <sys/cdefs.h>
84 __FBSDID("$FreeBSD$");
86 #include "opt_inet6.h"
88 #include <sys/param.h>
89 #include <sys/callout.h>
90 #include <sys/errno.h>
91 #include <sys/kernel.h>
93 #include <sys/malloc.h>
95 #include <sys/module.h>
96 #include <sys/domain.h>
97 #include <sys/protosw.h>
99 #include <sys/signalvar.h>
100 #include <sys/socket.h>
101 #include <sys/socketvar.h>
102 #include <sys/sockio.h>
104 #include <sys/sysctl.h>
105 #include <sys/syslog.h>
106 #include <sys/systm.h>
107 #include <sys/time.h>
110 #include <net/if_var.h>
111 #include <net/if_types.h>
112 #include <net/vnet.h>
114 #include <netinet/in.h>
115 #include <netinet/in_var.h>
116 #include <netinet/icmp6.h>
117 #include <netinet/ip_encap.h>
119 #include <netinet/ip6.h>
120 #include <netinet/in_kdtrace.h>
121 #include <netinet6/ip6_var.h>
122 #include <netinet6/scope6_var.h>
123 #include <netinet6/nd6.h>
124 #include <netinet6/ip6_mroute.h>
125 #include <netinet6/pim6.h>
126 #include <netinet6/pim6_var.h>
128 static MALLOC_DEFINE(M_MRTABLE6, "mf6c", "multicast forwarding cache entry");
130 static int ip6_mdq(struct mbuf *, struct ifnet *, struct mf6c *);
131 static void phyint_send(struct ip6_hdr *, struct mif6 *, struct mbuf *);
132 static int register_send(struct ip6_hdr *, struct mif6 *, struct mbuf *);
133 static int set_pim6(int *);
134 static int socket_send(struct socket *, struct mbuf *,
135 struct sockaddr_in6 *);
137 extern int in6_mcast_loop;
138 extern struct domain inet6domain;
140 static const struct encaptab *pim6_encap_cookie;
141 static int pim6_encapcheck(const struct mbuf *, int, int, void *);
142 static int pim6_input(struct mbuf *, int, int, void *);
144 static const struct encap_config ipv6_encap_cfg = {
145 .proto = IPPROTO_PIM,
146 .min_length = sizeof(struct ip6_hdr) + PIM_MINLEN,
148 .check = pim6_encapcheck,
152 VNET_DEFINE_STATIC(int, ip6_mrouter_ver) = 0;
153 #define V_ip6_mrouter_ver VNET(ip6_mrouter_ver)
155 SYSCTL_DECL(_net_inet6);
156 SYSCTL_DECL(_net_inet6_ip6);
157 static SYSCTL_NODE(_net_inet6, IPPROTO_PIM, pim,
158 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
161 static struct mrt6stat mrt6stat;
162 SYSCTL_STRUCT(_net_inet6_ip6, OID_AUTO, mrt6stat, CTLFLAG_RW,
164 "Multicast Routing Statistics (struct mrt6stat, netinet6/ip6_mroute.h)");
166 #define MRT6STAT_INC(name) mrt6stat.name += 1
167 #define NO_RTE_FOUND 0x1
168 #define RTE_FOUND 0x2
170 static struct sx mrouter6_mtx;
171 #define MROUTER6_LOCKPTR() (&mrouter6_mtx)
172 #define MROUTER6_LOCK() sx_xlock(MROUTER6_LOCKPTR())
173 #define MROUTER6_UNLOCK() sx_xunlock(MROUTER6_LOCKPTR())
174 #define MROUTER6_LOCK_ASSERT() sx_assert(MROUTER6_LOCKPTR(), SA_XLOCKED
175 #define MROUTER6_LOCK_INIT() sx_init(MROUTER6_LOCKPTR(), "mrouter6")
176 #define MROUTER6_LOCK_DESTROY() sx_destroy(MROUTER6_LOCKPTR())
178 static struct mf6c *mf6ctable[MF6CTBLSIZ];
179 SYSCTL_OPAQUE(_net_inet6_ip6, OID_AUTO, mf6ctable, CTLFLAG_RD,
180 &mf6ctable, sizeof(mf6ctable), "S,*mf6ctable[MF6CTBLSIZ]",
181 "IPv6 Multicast Forwarding Table (struct *mf6ctable[MF6CTBLSIZ], "
182 "netinet6/ip6_mroute.h)");
184 static struct mtx mfc6_mtx;
185 #define MFC6_LOCKPTR() (&mfc6_mtx)
186 #define MFC6_LOCK() mtx_lock(MFC6_LOCKPTR())
187 #define MFC6_UNLOCK() mtx_unlock(MFC6_LOCKPTR())
188 #define MFC6_LOCK_ASSERT() mtx_assert(MFC6_LOCKPTR(), MA_OWNED)
189 #define MFC6_LOCK_INIT() mtx_init(MFC6_LOCKPTR(), \
190 "IPv6 multicast forwarding cache", \
192 #define MFC6_LOCK_DESTROY() mtx_destroy(MFC6_LOCKPTR())
194 static u_char n6expire[MF6CTBLSIZ];
196 static struct mif6 mif6table[MAXMIFS];
198 sysctl_mif6table(SYSCTL_HANDLER_ARGS)
200 struct mif6_sctl *out;
203 out = malloc(sizeof(struct mif6_sctl) * MAXMIFS, M_TEMP,
205 for (int i = 0; i < MAXMIFS; i++) {
206 out[i].m6_flags = mif6table[i].m6_flags;
207 out[i].m6_rate_limit = mif6table[i].m6_rate_limit;
208 out[i].m6_lcl_addr = mif6table[i].m6_lcl_addr;
209 if (mif6table[i].m6_ifp != NULL)
210 out[i].m6_ifp = mif6table[i].m6_ifp->if_index;
213 out[i].m6_pkt_in = mif6table[i].m6_pkt_in;
214 out[i].m6_pkt_out = mif6table[i].m6_pkt_out;
215 out[i].m6_bytes_in = mif6table[i].m6_bytes_in;
216 out[i].m6_bytes_out = mif6table[i].m6_bytes_out;
218 error = SYSCTL_OUT(req, out, sizeof(struct mif6_sctl) * MAXMIFS);
222 SYSCTL_PROC(_net_inet6_ip6, OID_AUTO, mif6table,
223 CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
224 NULL, 0, sysctl_mif6table, "S,mif6_sctl[MAXMIFS]",
225 "IPv6 Multicast Interfaces (struct mif6_sctl[MAXMIFS], "
226 "netinet6/ip6_mroute.h)");
228 static struct mtx mif6_mtx;
229 #define MIF6_LOCKPTR() (&mif6_mtx)
230 #define MIF6_LOCK() mtx_lock(MIF6_LOCKPTR())
231 #define MIF6_UNLOCK() mtx_unlock(MIF6_LOCKPTR())
232 #define MIF6_LOCK_ASSERT() mtx_assert(MIF6_LOCKPTR(), MA_OWNED)
233 #define MIF6_LOCK_INIT() \
234 mtx_init(MIF6_LOCKPTR(), "IPv6 multicast interfaces", NULL, MTX_DEF)
235 #define MIF6_LOCK_DESTROY() mtx_destroy(MIF6_LOCKPTR())
238 VNET_DEFINE_STATIC(u_int, mrt6debug) = 0; /* debug level */
239 #define V_mrt6debug VNET(mrt6debug)
240 #define DEBUG_MFC 0x02
241 #define DEBUG_FORWARD 0x04
242 #define DEBUG_EXPIRE 0x08
243 #define DEBUG_XMIT 0x10
244 #define DEBUG_REG 0x20
245 #define DEBUG_PIM 0x40
246 #define DEBUG_ERR 0x80
247 #define DEBUG_ANY 0x7f
248 #define MRT6_DLOG(m, fmt, ...) \
249 if (V_mrt6debug & (m)) \
250 log(((m) & DEBUG_ERR) ? LOG_ERR: LOG_DEBUG, \
251 "%s: " fmt "\n", __func__, ##__VA_ARGS__)
253 #define MRT6_DLOG(m, fmt, ...)
256 static void expire_upcalls(void *);
257 #define EXPIRE_TIMEOUT (hz / 4) /* 4x / second */
258 #define UPCALL_EXPIRE 6 /* number of timeouts */
261 * XXX TODO: maintain a count to if_allmulti() calls in struct ifnet.
265 * 'Interfaces' associated with decapsulator (so we can tell
266 * packets that went through it from ones that get reflected
267 * by a broken gateway). Different from IPv4 register_if,
268 * these interfaces are linked into the system ifnet list,
269 * because per-interface IPv6 statistics are maintained in
270 * ifp->if_afdata. But it does not have any routes point
271 * to them. I.e., packets can't be sent this way. They
272 * only exist as a placeholder for multicast source
275 static struct ifnet *multicast_register_if6;
277 #define ENCAP_HOPS 64
282 static mifi_t nummifs = 0;
283 static mifi_t reg_mif_num = (mifi_t)-1;
285 static struct pim6stat pim6stat;
286 SYSCTL_STRUCT(_net_inet6_pim, PIM6CTL_STATS, stats, CTLFLAG_RW,
288 "PIM Statistics (struct pim6stat, netinet6/pim6_var.h)");
290 #define PIM6STAT_INC(name) pim6stat.name += 1
291 VNET_DEFINE_STATIC(int, pim6);
292 #define V_pim6 VNET(pim6)
295 * Hash function for a source, group entry
297 #define MF6CHASH(a, g) MF6CHASHMOD((a).s6_addr32[0] ^ (a).s6_addr32[1] ^ \
298 (a).s6_addr32[2] ^ (a).s6_addr32[3] ^ \
299 (g).s6_addr32[0] ^ (g).s6_addr32[1] ^ \
300 (g).s6_addr32[2] ^ (g).s6_addr32[3])
303 * Find a route for a given origin IPv6 address and Multicast group address.
305 #define MF6CFIND(o, g, rt) do { \
306 struct mf6c *_rt = mf6ctable[MF6CHASH(o,g)]; \
309 if (IN6_ARE_ADDR_EQUAL(&_rt->mf6c_origin.sin6_addr, &(o)) && \
310 IN6_ARE_ADDR_EQUAL(&_rt->mf6c_mcastgrp.sin6_addr, &(g)) && \
311 (_rt->mf6c_stall == NULL)) { \
315 _rt = _rt->mf6c_next; \
318 MRT6STAT_INC(mrt6s_mfc_misses); \
320 } while (/*CONSTCOND*/ 0)
323 * Macros to compute elapsed time efficiently
324 * Borrowed from Van Jacobson's scheduling code
325 * XXX: replace with timersub() ?
327 #define TV_DELTA(a, b, delta) do { \
330 delta = (a).tv_usec - (b).tv_usec; \
331 if ((xxs = (a).tv_sec - (b).tv_sec)) { \
340 delta += (1000000 * xxs); \
343 } while (/*CONSTCOND*/ 0)
345 /* XXX: replace with timercmp(a, b, <) ? */
346 #define TV_LT(a, b) (((a).tv_usec < (b).tv_usec && \
347 (a).tv_sec <= (b).tv_sec) || (a).tv_sec < (b).tv_sec)
350 #define UPCALL_MAX 50
351 static u_long upcall_data[UPCALL_MAX + 1];
352 static void collate();
353 #endif /* UPCALL_TIMING */
355 static int ip6_mrouter_init(struct socket *, int, int);
356 static int add_m6fc(struct mf6cctl *);
357 static int add_m6if(struct mif6ctl *);
358 static int del_m6fc(struct mf6cctl *);
359 static int del_m6if(mifi_t *);
360 static int del_m6if_locked(mifi_t *);
361 static int get_mif6_cnt(struct sioc_mif_req6 *);
362 static int get_sg_cnt(struct sioc_sg_req6 *);
364 static struct callout expire_upcalls_ch;
366 int X_ip6_mforward(struct ip6_hdr *, struct ifnet *, struct mbuf *);
367 int X_ip6_mrouter_done(void);
368 int X_ip6_mrouter_set(struct socket *, struct sockopt *);
369 int X_ip6_mrouter_get(struct socket *, struct sockopt *);
370 int X_mrt6_ioctl(u_long, caddr_t);
373 * Handle MRT setsockopt commands to modify the multicast routing tables.
376 X_ip6_mrouter_set(struct socket *so, struct sockopt *sopt)
384 if (so != V_ip6_mrouter && sopt->sopt_name != MRT6_INIT)
387 switch (sopt->sopt_name) {
392 error = sooptcopyin(sopt, &optval, sizeof(optval),
396 error = ip6_mrouter_init(so, optval, sopt->sopt_name);
399 error = X_ip6_mrouter_done();
402 error = sooptcopyin(sopt, &mifc, sizeof(mifc), sizeof(mifc));
405 error = add_m6if(&mifc);
408 error = sooptcopyin(sopt, &mfcc, sizeof(mfcc), sizeof(mfcc));
411 error = add_m6fc(&mfcc);
414 error = sooptcopyin(sopt, &mfcc, sizeof(mfcc), sizeof(mfcc));
417 error = del_m6fc(&mfcc);
420 error = sooptcopyin(sopt, &mifi, sizeof(mifi), sizeof(mifi));
423 error = del_m6if(&mifi);
426 error = sooptcopyin(sopt, &optval, sizeof(optval),
430 error = set_pim6(&optval);
441 * Handle MRT getsockopt commands
444 X_ip6_mrouter_get(struct socket *so, struct sockopt *sopt)
448 if (so != V_ip6_mrouter)
451 switch (sopt->sopt_name) {
453 error = sooptcopyout(sopt, &V_pim6, sizeof(V_pim6));
460 * Handle ioctl commands to obtain information from the cache
463 X_mrt6_ioctl(u_long cmd, caddr_t data)
470 case SIOCGETSGCNT_IN6:
471 ret = get_sg_cnt((struct sioc_sg_req6 *)data);
474 case SIOCGETMIFCNT_IN6:
475 ret = get_mif6_cnt((struct sioc_mif_req6 *)data);
486 * returns the packet, byte, rpf-failure count for the source group provided
489 get_sg_cnt(struct sioc_sg_req6 *req)
498 MF6CFIND(req->src.sin6_addr, req->grp.sin6_addr, rt);
502 req->pktcnt = rt->mf6c_pkt_cnt;
503 req->bytecnt = rt->mf6c_byte_cnt;
504 req->wrong_if = rt->mf6c_wrong_if;
513 * returns the input and output packet and byte counts on the mif provided
516 get_mif6_cnt(struct sioc_mif_req6 *req)
526 if (mifi >= nummifs) {
529 req->icount = mif6table[mifi].m6_pkt_in;
530 req->ocount = mif6table[mifi].m6_pkt_out;
531 req->ibytes = mif6table[mifi].m6_bytes_in;
532 req->obytes = mif6table[mifi].m6_bytes_out;
543 if ((*i != 1) && (*i != 0))
552 * Enable multicast routing
555 ip6_mrouter_init(struct socket *so, int v, int cmd)
558 MRT6_DLOG(DEBUG_ANY, "%s: socket %p", __func__, so);
561 return (ENOPROTOOPT);
565 if (V_ip6_mrouter != NULL) {
571 V_ip6_mrouter_ver = cmd;
573 bzero((caddr_t)mf6ctable, sizeof(mf6ctable));
574 bzero((caddr_t)n6expire, sizeof(n6expire));
576 V_pim6 = 0;/* used for stubbing out/in pim stuff */
578 callout_init_mtx(&expire_upcalls_ch, MFC6_LOCKPTR(), 0);
579 callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT,
580 expire_upcalls, NULL);
584 MRT6_DLOG(DEBUG_ANY, "finished");
590 * Disable IPv6 multicast forwarding.
593 X_ip6_mrouter_done(void)
602 if (V_ip6_mrouter == NULL) {
608 * For each phyint in use, disable promiscuous reception of all IPv6
611 for (mifi = 0; mifi < nummifs; mifi++) {
612 if (mif6table[mifi].m6_ifp &&
613 !(mif6table[mifi].m6_flags & MIFF_REGISTER)) {
614 if_allmulti(mif6table[mifi].m6_ifp, 0);
617 bzero((caddr_t)mif6table, sizeof(mif6table));
620 V_pim6 = 0; /* used to stub out/in pim specific code */
623 * Free all multicast forwarding cache entries.
626 for (i = 0; i < MF6CTBLSIZ; i++) {
631 for (rte = rt->mf6c_stall; rte != NULL; ) {
632 struct rtdetq *n = rte->next;
635 free(rte, M_MRTABLE6);
640 free(frt, M_MRTABLE6);
643 bzero((caddr_t)mf6ctable, sizeof(mf6ctable));
646 callout_drain(&expire_upcalls_ch);
649 * Reset register interface
651 if (reg_mif_num != (mifi_t)-1 && multicast_register_if6 != NULL) {
652 if_detach(multicast_register_if6);
653 if_free(multicast_register_if6);
654 reg_mif_num = (mifi_t)-1;
655 multicast_register_if6 = NULL;
658 V_ip6_mrouter = NULL;
659 V_ip6_mrouter_ver = 0;
662 MRT6_DLOG(DEBUG_ANY, "finished");
667 static struct sockaddr_in6 sin6 = { sizeof(sin6), AF_INET6 };
670 * Add a mif to the mif table
673 add_m6if(struct mif6ctl *mifcp)
675 struct epoch_tracker et;
682 if (mifcp->mif6c_mifi >= MAXMIFS) {
686 mifp = mif6table + mifcp->mif6c_mifi;
687 if (mifp->m6_ifp != NULL) {
689 return (EADDRINUSE); /* XXX: is it appropriate? */
693 if ((ifp = ifnet_byindex(mifcp->mif6c_pifi)) == NULL) {
698 NET_EPOCH_EXIT(et); /* XXXGL: unsafe ifp */
700 if (mifcp->mif6c_flags & MIFF_REGISTER) {
701 if (reg_mif_num == (mifi_t)-1) {
702 ifp = if_alloc(IFT_OTHER);
704 if_initname(ifp, "register_mif", 0);
705 ifp->if_flags |= IFF_LOOPBACK;
707 multicast_register_if6 = ifp;
708 reg_mif_num = mifcp->mif6c_mifi;
710 * it is impossible to guess the ifindex of the
711 * register interface. So mif6c_pifi is automatically
714 mifcp->mif6c_pifi = ifp->if_index;
716 ifp = multicast_register_if6;
719 /* Make sure the interface supports multicast */
720 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
725 error = if_allmulti(ifp, 1);
732 mifp->m6_flags = mifcp->mif6c_flags;
735 /* initialize per mif pkt counters */
737 mifp->m6_pkt_out = 0;
738 mifp->m6_bytes_in = 0;
739 mifp->m6_bytes_out = 0;
741 /* Adjust nummifs up if the mifi is higher than nummifs */
742 if (nummifs <= mifcp->mif6c_mifi)
743 nummifs = mifcp->mif6c_mifi + 1;
746 MRT6_DLOG(DEBUG_ANY, "mif #%d, phyint %s", mifcp->mif6c_mifi,
753 * Delete a mif from the mif table
756 del_m6if_locked(mifi_t *mifip)
758 struct mif6 *mifp = mif6table + *mifip;
764 if (*mifip >= nummifs)
766 if (mifp->m6_ifp == NULL)
769 if (!(mifp->m6_flags & MIFF_REGISTER)) {
770 /* XXX: TODO: Maintain an ALLMULTI refcount in struct ifnet. */
774 if (reg_mif_num != (mifi_t)-1 &&
775 multicast_register_if6 != NULL) {
776 if_detach(multicast_register_if6);
777 if_free(multicast_register_if6);
778 reg_mif_num = (mifi_t)-1;
779 multicast_register_if6 = NULL;
783 bzero((caddr_t)mifp, sizeof(*mifp));
785 /* Adjust nummifs down */
786 for (mifi = nummifs; mifi > 0; mifi--)
787 if (mif6table[mifi - 1].m6_ifp)
790 MRT6_DLOG(DEBUG_ANY, "mif %d, nummifs %d", *mifip, nummifs);
796 del_m6if(mifi_t *mifip)
801 cc = del_m6if_locked(mifip);
811 add_m6fc(struct mf6cctl *mfccp)
817 char ip6bufo[INET6_ADDRSTRLEN], ip6bufg[INET6_ADDRSTRLEN];
821 MF6CFIND(mfccp->mf6cc_origin.sin6_addr,
822 mfccp->mf6cc_mcastgrp.sin6_addr, rt);
824 /* If an entry already exists, just update the fields */
826 MRT6_DLOG(DEBUG_MFC, "no upcall o %s g %s p %x",
827 ip6_sprintf(ip6bufo, &mfccp->mf6cc_origin.sin6_addr),
828 ip6_sprintf(ip6bufg, &mfccp->mf6cc_mcastgrp.sin6_addr),
829 mfccp->mf6cc_parent);
831 rt->mf6c_parent = mfccp->mf6cc_parent;
832 rt->mf6c_ifset = mfccp->mf6cc_ifset;
839 * Find the entry for which the upcall was made and update
841 hash = MF6CHASH(mfccp->mf6cc_origin.sin6_addr,
842 mfccp->mf6cc_mcastgrp.sin6_addr);
843 for (rt = mf6ctable[hash], nstl = 0; rt; rt = rt->mf6c_next) {
844 if (IN6_ARE_ADDR_EQUAL(&rt->mf6c_origin.sin6_addr,
845 &mfccp->mf6cc_origin.sin6_addr) &&
846 IN6_ARE_ADDR_EQUAL(&rt->mf6c_mcastgrp.sin6_addr,
847 &mfccp->mf6cc_mcastgrp.sin6_addr) &&
848 (rt->mf6c_stall != NULL)) {
851 "add_m6fc: %s o %s g %s p %x dbx %p\n",
852 "multiple kernel entries",
854 &mfccp->mf6cc_origin.sin6_addr),
856 &mfccp->mf6cc_mcastgrp.sin6_addr),
857 mfccp->mf6cc_parent, rt->mf6c_stall);
859 MRT6_DLOG(DEBUG_MFC, "o %s g %s p %x dbg %p",
861 &mfccp->mf6cc_origin.sin6_addr),
863 &mfccp->mf6cc_mcastgrp.sin6_addr),
864 mfccp->mf6cc_parent, rt->mf6c_stall);
866 rt->mf6c_origin = mfccp->mf6cc_origin;
867 rt->mf6c_mcastgrp = mfccp->mf6cc_mcastgrp;
868 rt->mf6c_parent = mfccp->mf6cc_parent;
869 rt->mf6c_ifset = mfccp->mf6cc_ifset;
870 /* initialize pkt counters per src-grp */
871 rt->mf6c_pkt_cnt = 0;
872 rt->mf6c_byte_cnt = 0;
873 rt->mf6c_wrong_if = 0;
875 rt->mf6c_expire = 0; /* Don't clean this guy up */
878 /* free packets Qed at the end of this entry */
879 for (rte = rt->mf6c_stall; rte != NULL; ) {
880 struct rtdetq *n = rte->next;
881 ip6_mdq(rte->m, rte->ifp, rt);
885 #endif /* UPCALL_TIMING */
886 free(rte, M_MRTABLE6);
889 rt->mf6c_stall = NULL;
894 * It is possible that an entry is being inserted without an upcall
897 MRT6_DLOG(DEBUG_MFC, "no upcall h %lu o %s g %s p %x", hash,
898 ip6_sprintf(ip6bufo, &mfccp->mf6cc_origin.sin6_addr),
899 ip6_sprintf(ip6bufg, &mfccp->mf6cc_mcastgrp.sin6_addr),
900 mfccp->mf6cc_parent);
902 for (rt = mf6ctable[hash]; rt; rt = rt->mf6c_next) {
903 if (IN6_ARE_ADDR_EQUAL(&rt->mf6c_origin.sin6_addr,
904 &mfccp->mf6cc_origin.sin6_addr)&&
905 IN6_ARE_ADDR_EQUAL(&rt->mf6c_mcastgrp.sin6_addr,
906 &mfccp->mf6cc_mcastgrp.sin6_addr)) {
907 rt->mf6c_origin = mfccp->mf6cc_origin;
908 rt->mf6c_mcastgrp = mfccp->mf6cc_mcastgrp;
909 rt->mf6c_parent = mfccp->mf6cc_parent;
910 rt->mf6c_ifset = mfccp->mf6cc_ifset;
911 /* initialize pkt counters per src-grp */
912 rt->mf6c_pkt_cnt = 0;
913 rt->mf6c_byte_cnt = 0;
914 rt->mf6c_wrong_if = 0;
922 /* no upcall, so make a new entry */
923 rt = (struct mf6c *)malloc(sizeof(*rt), M_MRTABLE6,
930 /* insert new entry at head of hash chain */
931 rt->mf6c_origin = mfccp->mf6cc_origin;
932 rt->mf6c_mcastgrp = mfccp->mf6cc_mcastgrp;
933 rt->mf6c_parent = mfccp->mf6cc_parent;
934 rt->mf6c_ifset = mfccp->mf6cc_ifset;
935 /* initialize pkt counters per src-grp */
936 rt->mf6c_pkt_cnt = 0;
937 rt->mf6c_byte_cnt = 0;
938 rt->mf6c_wrong_if = 0;
940 rt->mf6c_stall = NULL;
942 /* link into table */
943 rt->mf6c_next = mf6ctable[hash];
944 mf6ctable[hash] = rt;
954 * collect delay statistics on the upcalls
957 collate(struct timeval *t)
967 TV_DELTA(tp, *t, delta);
976 #endif /* UPCALL_TIMING */
979 * Delete an mfc entry
982 del_m6fc(struct mf6cctl *mfccp)
985 char ip6bufo[INET6_ADDRSTRLEN], ip6bufg[INET6_ADDRSTRLEN];
987 struct sockaddr_in6 origin;
988 struct sockaddr_in6 mcastgrp;
993 origin = mfccp->mf6cc_origin;
994 mcastgrp = mfccp->mf6cc_mcastgrp;
995 hash = MF6CHASH(origin.sin6_addr, mcastgrp.sin6_addr);
997 MRT6_DLOG(DEBUG_MFC, "orig %s mcastgrp %s",
998 ip6_sprintf(ip6bufo, &origin.sin6_addr),
999 ip6_sprintf(ip6bufg, &mcastgrp.sin6_addr));
1003 nptr = &mf6ctable[hash];
1004 while ((rt = *nptr) != NULL) {
1005 if (IN6_ARE_ADDR_EQUAL(&origin.sin6_addr,
1006 &rt->mf6c_origin.sin6_addr) &&
1007 IN6_ARE_ADDR_EQUAL(&mcastgrp.sin6_addr,
1008 &rt->mf6c_mcastgrp.sin6_addr) &&
1009 rt->mf6c_stall == NULL)
1012 nptr = &rt->mf6c_next;
1016 return (EADDRNOTAVAIL);
1019 *nptr = rt->mf6c_next;
1020 free(rt, M_MRTABLE6);
1028 socket_send(struct socket *s, struct mbuf *mm, struct sockaddr_in6 *src)
1032 if (sbappendaddr(&s->so_rcv,
1033 (struct sockaddr *)src,
1034 mm, (struct mbuf *)0) != 0) {
1045 * IPv6 multicast forwarding function. This function assumes that the packet
1046 * pointed to by "ip6" has arrived on (or is about to be sent to) the interface
1047 * pointed to by "ifp", and the packet is to be relayed to other networks
1048 * that have members of the packet's destination IPv6 multicast group.
1050 * The packet is returned unscathed to the caller, unless it is
1051 * erroneous, in which case a non-zero return value tells the caller to
1054 * NOTE: this implementation assumes that m->m_pkthdr.rcvif is NULL iff
1055 * this function is called in the originating context (i.e., not when
1056 * forwarding a packet from other node). ip6_output(), which is currently the
1057 * only function that calls this function is called in the originating context,
1058 * explicitly ensures this condition. It is caller's responsibility to ensure
1059 * that if this function is called from somewhere else in the originating
1060 * context in the future.
1063 X_ip6_mforward(struct ip6_hdr *ip6, struct ifnet *ifp, struct mbuf *m)
1072 char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
1073 #ifdef UPCALL_TIMING
1077 #endif /* UPCALL_TIMING */
1079 MRT6_DLOG(DEBUG_FORWARD, "src %s, dst %s, ifindex %d",
1080 ip6_sprintf(ip6bufs, &ip6->ip6_src),
1081 ip6_sprintf(ip6bufd, &ip6->ip6_dst), ifp->if_index);
1084 * Don't forward a packet with Hop limit of zero or one,
1085 * or a packet destined to a local-only group.
1087 if (ip6->ip6_hlim <= 1 || IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst) ||
1088 IN6_IS_ADDR_MC_LINKLOCAL(&ip6->ip6_dst))
1093 * Source address check: do not forward packets with unspecified
1094 * source. It was discussed in July 2000, on ipngwg mailing list.
1095 * This is rather more serious than unicast cases, because some
1096 * MLD packets can be sent with the unspecified source address
1097 * (although such packets must normally set 1 to the hop limit field).
1099 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
1100 IP6STAT_INC(ip6s_cantforward);
1101 if (V_ip6_log_time + V_ip6_log_interval < time_uptime) {
1102 V_ip6_log_time = time_uptime;
1105 "from %s to %s nxt %d received on %s\n",
1106 ip6_sprintf(ip6bufs, &ip6->ip6_src),
1107 ip6_sprintf(ip6bufd, &ip6->ip6_dst),
1109 if_name(m->m_pkthdr.rcvif));
1117 * Determine forwarding mifs from the forwarding cache table
1119 MF6CFIND(ip6->ip6_src, ip6->ip6_dst, rt);
1120 MRT6STAT_INC(mrt6s_mfc_lookups);
1122 /* Entry exists, so forward if necessary */
1125 return (ip6_mdq(m, ifp, rt));
1129 * If we don't have a route for packet's origin,
1130 * Make a copy of the packet & send message to routing daemon.
1132 MRT6STAT_INC(mrt6s_no_route);
1133 MRT6_DLOG(DEBUG_FORWARD | DEBUG_MFC, "no rte s %s g %s",
1134 ip6_sprintf(ip6bufs, &ip6->ip6_src),
1135 ip6_sprintf(ip6bufd, &ip6->ip6_dst));
1138 * Allocate mbufs early so that we don't do extra work if we
1139 * are just going to fail anyway.
1141 rte = (struct rtdetq *)malloc(sizeof(*rte), M_MRTABLE6, M_NOWAIT);
1146 mb0 = m_copym(m, 0, M_COPYALL, M_NOWAIT);
1148 * Pullup packet header if needed before storing it,
1149 * as other references may modify it in the meantime.
1151 if (mb0 && (!M_WRITABLE(mb0) || mb0->m_len < sizeof(struct ip6_hdr)))
1152 mb0 = m_pullup(mb0, sizeof(struct ip6_hdr));
1154 free(rte, M_MRTABLE6);
1159 /* is there an upcall waiting for this packet? */
1160 hash = MF6CHASH(ip6->ip6_src, ip6->ip6_dst);
1161 for (rt = mf6ctable[hash]; rt; rt = rt->mf6c_next) {
1162 if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_src,
1163 &rt->mf6c_origin.sin6_addr) &&
1164 IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
1165 &rt->mf6c_mcastgrp.sin6_addr) && (rt->mf6c_stall != NULL))
1172 struct omrt6msg *oim;
1174 /* no upcall, so make a new entry */
1175 rt = (struct mf6c *)malloc(sizeof(*rt), M_MRTABLE6, M_NOWAIT);
1177 free(rte, M_MRTABLE6);
1183 * Make a copy of the header to send to the user
1186 mm = m_copym(mb0, 0, sizeof(struct ip6_hdr), M_NOWAIT);
1188 free(rte, M_MRTABLE6);
1190 free(rt, M_MRTABLE6);
1196 * Send message to routing daemon
1198 sin6.sin6_addr = ip6->ip6_src;
1203 switch (V_ip6_mrouter_ver) {
1206 oim = mtod(mm, struct omrt6msg *);
1207 oim->im6_msgtype = MRT6MSG_NOCACHE;
1212 im = mtod(mm, struct mrt6msg *);
1213 im->im6_msgtype = MRT6MSG_NOCACHE;
1217 free(rte, M_MRTABLE6);
1219 free(rt, M_MRTABLE6);
1224 MRT6_DLOG(DEBUG_FORWARD, "getting the iif info in the kernel");
1225 for (mifp = mif6table, mifi = 0;
1226 mifi < nummifs && mifp->m6_ifp != ifp; mifp++, mifi++)
1229 switch (V_ip6_mrouter_ver) {
1232 oim->im6_mif = mifi;
1240 if (socket_send(V_ip6_mrouter, mm, &sin6) < 0) {
1241 log(LOG_WARNING, "ip6_mforward: ip6_mrouter "
1242 "socket queue full\n");
1243 MRT6STAT_INC(mrt6s_upq_sockfull);
1244 free(rte, M_MRTABLE6);
1246 free(rt, M_MRTABLE6);
1251 MRT6STAT_INC(mrt6s_upcalls);
1253 /* insert new entry at head of hash chain */
1254 bzero(rt, sizeof(*rt));
1255 rt->mf6c_origin.sin6_family = AF_INET6;
1256 rt->mf6c_origin.sin6_len = sizeof(struct sockaddr_in6);
1257 rt->mf6c_origin.sin6_addr = ip6->ip6_src;
1258 rt->mf6c_mcastgrp.sin6_family = AF_INET6;
1259 rt->mf6c_mcastgrp.sin6_len = sizeof(struct sockaddr_in6);
1260 rt->mf6c_mcastgrp.sin6_addr = ip6->ip6_dst;
1261 rt->mf6c_expire = UPCALL_EXPIRE;
1263 rt->mf6c_parent = MF6C_INCOMPLETE_PARENT;
1265 /* link into table */
1266 rt->mf6c_next = mf6ctable[hash];
1267 mf6ctable[hash] = rt;
1268 /* Add this entry to the end of the queue */
1269 rt->mf6c_stall = rte;
1271 /* determine if q has overflowed */
1275 for (p = &rt->mf6c_stall; *p != NULL; p = &(*p)->next)
1276 if (++npkts > MAX_UPQ6) {
1277 MRT6STAT_INC(mrt6s_upq_ovflw);
1278 free(rte, M_MRTABLE6);
1284 /* Add this entry to the end of the queue */
1291 #ifdef UPCALL_TIMING
1293 #endif /* UPCALL_TIMING */
1301 * Clean up cache entries if upcalls are not serviced
1302 * Call from the Slow Timeout mechanism, every half second.
1305 expire_upcalls(void *unused)
1308 char ip6bufo[INET6_ADDRSTRLEN], ip6bufg[INET6_ADDRSTRLEN];
1311 struct mf6c *mfc, **nptr;
1316 for (i = 0; i < MF6CTBLSIZ; i++) {
1317 if (n6expire[i] == 0)
1319 nptr = &mf6ctable[i];
1320 while ((mfc = *nptr) != NULL) {
1321 rte = mfc->mf6c_stall;
1323 * Skip real cache entries
1324 * Make sure it wasn't marked to not expire (shouldn't happen)
1328 mfc->mf6c_expire != 0 &&
1329 --mfc->mf6c_expire == 0) {
1330 MRT6_DLOG(DEBUG_EXPIRE, "expiring (%s %s)",
1331 ip6_sprintf(ip6bufo, &mfc->mf6c_origin.sin6_addr),
1332 ip6_sprintf(ip6bufg, &mfc->mf6c_mcastgrp.sin6_addr));
1334 * drop all the packets
1335 * free the mbuf with the pkt, if, timing info
1338 struct rtdetq *n = rte->next;
1340 free(rte, M_MRTABLE6);
1342 } while (rte != NULL);
1343 MRT6STAT_INC(mrt6s_cache_cleanups);
1346 *nptr = mfc->mf6c_next;
1347 free(mfc, M_MRTABLE6);
1349 nptr = &mfc->mf6c_next;
1353 callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT,
1354 expire_upcalls, NULL);
1358 * Packet forwarding routine once entry in the cache is made
1361 ip6_mdq(struct mbuf *m, struct ifnet *ifp, struct mf6c *rt)
1363 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1366 int plen = m->m_pkthdr.len;
1367 struct in6_addr src0, dst0; /* copies for local work */
1368 u_int32_t iszone, idzone, oszone, odzone;
1372 * Don't forward if it didn't arrive from the parent mif
1375 mifi = rt->mf6c_parent;
1376 if ((mifi >= nummifs) || (mif6table[mifi].m6_ifp != ifp)) {
1377 /* came in the wrong interface */
1378 MRT6_DLOG(DEBUG_FORWARD,
1379 "wrong if: ifid %d mifi %d mififid %x", ifp->if_index,
1380 mifi, mif6table[mifi].m6_ifp->if_index);
1381 MRT6STAT_INC(mrt6s_wrong_if);
1382 rt->mf6c_wrong_if++;
1384 * If we are doing PIM processing, and we are forwarding
1385 * packets on this interface, send a message to the
1388 /* have to make sure this is a valid mif */
1389 if (mifi < nummifs && mif6table[mifi].m6_ifp)
1390 if (V_pim6 && (m->m_flags & M_LOOP) == 0) {
1392 * Check the M_LOOP flag to avoid an
1393 * unnecessary PIM assert.
1394 * XXX: M_LOOP is an ad-hoc hack...
1396 static struct sockaddr_in6 sin6 =
1397 { sizeof(sin6), AF_INET6 };
1402 struct omrt6msg *oim;
1405 mm = m_copym(m, 0, sizeof(struct ip6_hdr),
1409 mm->m_len < sizeof(struct ip6_hdr)))
1410 mm = m_pullup(mm, sizeof(struct ip6_hdr));
1418 switch (V_ip6_mrouter_ver) {
1421 oim = mtod(mm, struct omrt6msg *);
1422 oim->im6_msgtype = MRT6MSG_WRONGMIF;
1427 im = mtod(mm, struct mrt6msg *);
1428 im->im6_msgtype = MRT6MSG_WRONGMIF;
1436 for (mifp = mif6table, iif = 0;
1437 iif < nummifs && mifp &&
1438 mifp->m6_ifp != ifp;
1442 switch (V_ip6_mrouter_ver) {
1446 sin6.sin6_addr = oim->im6_src;
1451 sin6.sin6_addr = im->im6_src;
1455 MRT6STAT_INC(mrt6s_upcalls);
1457 if (socket_send(V_ip6_mrouter, mm, &sin6) < 0) {
1458 MRT6_DLOG(DEBUG_ANY,
1459 "ip6_mrouter socket queue full");
1460 MRT6STAT_INC(mrt6s_upq_sockfull);
1462 } /* if socket Q full */
1465 } /* if wrong iif */
1467 /* If I sourced this packet, it counts as output, else it was input. */
1468 if (m->m_pkthdr.rcvif == NULL) {
1469 /* XXX: is rcvif really NULL when output?? */
1470 mif6table[mifi].m6_pkt_out++;
1471 mif6table[mifi].m6_bytes_out += plen;
1473 mif6table[mifi].m6_pkt_in++;
1474 mif6table[mifi].m6_bytes_in += plen;
1477 rt->mf6c_byte_cnt += plen;
1480 * For each mif, forward a copy of the packet if there are group
1481 * members downstream on the interface.
1483 src0 = ip6->ip6_src;
1484 dst0 = ip6->ip6_dst;
1485 if ((error = in6_setscope(&src0, ifp, &iszone)) != 0 ||
1486 (error = in6_setscope(&dst0, ifp, &idzone)) != 0) {
1487 IP6STAT_INC(ip6s_badscope);
1490 for (mifp = mif6table, mifi = 0; mifi < nummifs; mifp++, mifi++) {
1491 if (IF_ISSET(mifi, &rt->mf6c_ifset)) {
1493 * check if the outgoing packet is going to break
1495 * XXX For packets through PIM register tunnel
1496 * interface, we believe a routing daemon.
1498 if (!(mif6table[rt->mf6c_parent].m6_flags &
1500 !(mif6table[mifi].m6_flags & MIFF_REGISTER)) {
1501 if (in6_setscope(&src0, mif6table[mifi].m6_ifp,
1503 in6_setscope(&dst0, mif6table[mifi].m6_ifp,
1507 IP6STAT_INC(ip6s_badscope);
1513 mifp->m6_bytes_out += plen;
1514 if (mifp->m6_flags & MIFF_REGISTER)
1515 register_send(ip6, mifp, m);
1517 phyint_send(ip6, mifp, m);
1524 phyint_send(struct ip6_hdr *ip6, struct mif6 *mifp, struct mbuf *m)
1527 char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
1529 struct mbuf *mb_copy;
1530 struct ifnet *ifp = mifp->m6_ifp;
1531 int error __unused = 0;
1535 * Make a new reference to the packet; make sure that
1536 * the IPv6 header is actually copied, not just referenced,
1537 * so that ip6_output() only scribbles on the copy.
1539 mb_copy = m_copym(m, 0, M_COPYALL, M_NOWAIT);
1541 (!M_WRITABLE(mb_copy) || mb_copy->m_len < sizeof(struct ip6_hdr)))
1542 mb_copy = m_pullup(mb_copy, sizeof(struct ip6_hdr));
1543 if (mb_copy == NULL) {
1546 /* set MCAST flag to the outgoing packet */
1547 mb_copy->m_flags |= M_MCAST;
1550 * If we sourced the packet, call ip6_output since we may devide
1551 * the packet into fragments when the packet is too big for the
1552 * outgoing interface.
1553 * Otherwise, we can simply send the packet to the interface
1556 if (m->m_pkthdr.rcvif == NULL) {
1557 struct ip6_moptions im6o;
1558 struct epoch_tracker et;
1560 im6o.im6o_multicast_ifp = ifp;
1561 /* XXX: ip6_output will override ip6->ip6_hlim */
1562 im6o.im6o_multicast_hlim = ip6->ip6_hlim;
1563 im6o.im6o_multicast_loop = 1;
1564 NET_EPOCH_ENTER(et);
1565 error = ip6_output(mb_copy, NULL, NULL, IPV6_FORWARDING, &im6o,
1569 MRT6_DLOG(DEBUG_XMIT, "mif %u err %d",
1570 (uint16_t)(mifp - mif6table), error);
1575 * If configured to loop back multicasts by default,
1576 * loop back a copy now.
1579 ip6_mloopback(ifp, m);
1582 * Put the packet into the sending queue of the outgoing interface
1583 * if it would fit in the MTU of the interface.
1585 linkmtu = IN6_LINKMTU(ifp);
1586 if (mb_copy->m_pkthdr.len <= linkmtu || linkmtu < IPV6_MMTU) {
1587 struct sockaddr_in6 dst6;
1589 bzero(&dst6, sizeof(dst6));
1590 dst6.sin6_len = sizeof(struct sockaddr_in6);
1591 dst6.sin6_family = AF_INET6;
1592 dst6.sin6_addr = ip6->ip6_dst;
1594 IP_PROBE(send, NULL, NULL, ip6, ifp, NULL, ip6);
1596 * We just call if_output instead of nd6_output here, since
1597 * we need no ND for a multicast forwarded packet...right?
1599 m_clrprotoflags(m); /* Avoid confusing lower layers. */
1600 error = (*ifp->if_output)(ifp, mb_copy,
1601 (struct sockaddr *)&dst6, NULL);
1602 MRT6_DLOG(DEBUG_XMIT, "mif %u err %d",
1603 (uint16_t)(mifp - mif6table), error);
1606 * pMTU discovery is intentionally disabled by default, since
1607 * various router may notify pMTU in multicast, which can be
1608 * a DDoS to a router
1610 if (V_ip6_mcast_pmtu)
1611 icmp6_error(mb_copy, ICMP6_PACKET_TOO_BIG, 0, linkmtu);
1613 MRT6_DLOG(DEBUG_XMIT, " packet too big on %s o %s "
1614 "g %s size %d (discarded)", if_name(ifp),
1615 ip6_sprintf(ip6bufs, &ip6->ip6_src),
1616 ip6_sprintf(ip6bufd, &ip6->ip6_dst),
1617 mb_copy->m_pkthdr.len);
1618 m_freem(mb_copy); /* simply discard the packet */
1624 register_send(struct ip6_hdr *ip6, struct mif6 *mif, struct mbuf *m)
1627 char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
1630 int i, len = m->m_pkthdr.len;
1631 static struct sockaddr_in6 sin6 = { sizeof(sin6), AF_INET6 };
1632 struct mrt6msg *im6;
1634 MRT6_DLOG(DEBUG_ANY, "src %s dst %s",
1635 ip6_sprintf(ip6bufs, &ip6->ip6_src),
1636 ip6_sprintf(ip6bufd, &ip6->ip6_dst));
1637 PIM6STAT_INC(pim6s_snd_registers);
1639 /* Make a copy of the packet to send to the user level process. */
1640 mm = m_gethdr(M_NOWAIT, MT_DATA);
1643 mm->m_data += max_linkhdr;
1644 mm->m_len = sizeof(struct ip6_hdr);
1646 if ((mm->m_next = m_copym(m, 0, M_COPYALL, M_NOWAIT)) == NULL) {
1650 i = MHLEN - M_LEADINGSPACE(mm);
1653 mm = m_pullup(mm, i);
1656 /* TODO: check it! */
1657 mm->m_pkthdr.len = len + sizeof(struct ip6_hdr);
1660 * Send message to routing daemon
1662 sin6.sin6_addr = ip6->ip6_src;
1664 im6 = mtod(mm, struct mrt6msg *);
1665 im6->im6_msgtype = MRT6MSG_WHOLEPKT;
1668 im6->im6_mif = mif - mif6table;
1670 /* iif info is not given for reg. encap.n */
1671 MRT6STAT_INC(mrt6s_upcalls);
1673 if (socket_send(V_ip6_mrouter, mm, &sin6) < 0) {
1674 MRT6_DLOG(DEBUG_ANY, "ip6_mrouter socket queue full");
1675 MRT6STAT_INC(mrt6s_upq_sockfull);
1682 * pim6_encapcheck() is called by the encap6_input() path at runtime to
1683 * determine if a packet is for PIM; allowing PIM to be dynamically loaded
1687 pim6_encapcheck(const struct mbuf *m __unused, int off __unused,
1688 int proto __unused, void *arg __unused)
1691 KASSERT(proto == IPPROTO_PIM, ("not for IPPROTO_PIM"));
1692 return (8); /* claim the datagram. */
1696 * PIM sparse mode hook
1697 * Receives the pim control messages, and passes them up to the listening
1698 * socket, using rip6_input.
1699 * The only message processed is the REGISTER pim message; the pim header
1700 * is stripped off, and the inner packet is passed to register_mforward.
1703 pim6_input(struct mbuf *m, int off, int proto, void *arg __unused)
1705 struct pim *pim; /* pointer to a pim struct */
1706 struct ip6_hdr *ip6;
1710 PIM6STAT_INC(pim6s_rcv_total);
1715 pimlen = m->m_pkthdr.len - off;
1716 if (pimlen < PIM_MINLEN) {
1717 PIM6STAT_INC(pim6s_rcv_tooshort);
1718 MRT6_DLOG(DEBUG_PIM, "PIM packet too short");
1720 return (IPPROTO_DONE);
1724 * if the packet is at least as big as a REGISTER, go ahead
1725 * and grab the PIM REGISTER header size, to avoid another
1726 * possible m_pullup() later.
1728 * PIM_MINLEN == pimhdr + u_int32 == 8
1729 * PIM6_REG_MINLEN == pimhdr + reghdr + eip6hdr == 4 + 4 + 40
1731 minlen = (pimlen >= PIM6_REG_MINLEN) ? PIM6_REG_MINLEN : PIM_MINLEN;
1734 * Make sure that the IP6 and PIM headers in contiguous memory, and
1735 * possibly the PIM REGISTER header
1737 if (m->m_len < off + minlen) {
1738 m = m_pullup(m, off + minlen);
1740 IP6STAT_INC(ip6s_exthdrtoolong);
1741 return (IPPROTO_DONE);
1744 ip6 = mtod(m, struct ip6_hdr *);
1745 pim = (struct pim *)((caddr_t)ip6 + off);
1747 #define PIM6_CHECKSUM
1748 #ifdef PIM6_CHECKSUM
1753 * Validate checksum.
1754 * If PIM REGISTER, exclude the data packet
1756 if (pim->pim_type == PIM_REGISTER)
1757 cksumlen = PIM_MINLEN;
1761 if (in6_cksum(m, IPPROTO_PIM, off, cksumlen)) {
1762 PIM6STAT_INC(pim6s_rcv_badsum);
1763 MRT6_DLOG(DEBUG_PIM, "invalid checksum");
1765 return (IPPROTO_DONE);
1768 #endif /* PIM_CHECKSUM */
1770 /* PIM version check */
1771 if (pim->pim_ver != PIM_VERSION) {
1772 PIM6STAT_INC(pim6s_rcv_badversion);
1773 MRT6_DLOG(DEBUG_ANY | DEBUG_ERR,
1774 "incorrect version %d, expecting %d",
1775 pim->pim_ver, PIM_VERSION);
1777 return (IPPROTO_DONE);
1780 if (pim->pim_type == PIM_REGISTER) {
1782 * since this is a REGISTER, we'll make a copy of the register
1783 * headers ip6+pim+u_int32_t+encap_ip6, to be passed up to the
1786 static struct sockaddr_in6 dst = { sizeof(dst), AF_INET6 };
1789 struct ip6_hdr *eip6;
1792 char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
1795 PIM6STAT_INC(pim6s_rcv_registers);
1797 if ((reg_mif_num >= nummifs) || (reg_mif_num == (mifi_t) -1)) {
1798 MRT6_DLOG(DEBUG_PIM, "register mif not set: %d",
1801 return (IPPROTO_DONE);
1804 reghdr = (u_int32_t *)(pim + 1);
1806 if ((ntohl(*reghdr) & PIM_NULL_REGISTER))
1807 goto pim6_input_to_daemon;
1812 if (pimlen < PIM6_REG_MINLEN) {
1813 PIM6STAT_INC(pim6s_rcv_tooshort);
1814 PIM6STAT_INC(pim6s_rcv_badregisters);
1815 MRT6_DLOG(DEBUG_ANY | DEBUG_ERR, "register packet "
1816 "size too small %d from %s",
1817 pimlen, ip6_sprintf(ip6bufs, &ip6->ip6_src));
1819 return (IPPROTO_DONE);
1822 eip6 = (struct ip6_hdr *) (reghdr + 1);
1823 MRT6_DLOG(DEBUG_PIM, "eip6: %s -> %s, eip6 plen %d",
1824 ip6_sprintf(ip6bufs, &eip6->ip6_src),
1825 ip6_sprintf(ip6bufd, &eip6->ip6_dst),
1826 ntohs(eip6->ip6_plen));
1828 /* verify the version number of the inner packet */
1829 if ((eip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
1830 PIM6STAT_INC(pim6s_rcv_badregisters);
1831 MRT6_DLOG(DEBUG_ANY, "invalid IP version (%d) "
1832 "of the inner packet",
1833 (eip6->ip6_vfc & IPV6_VERSION));
1835 return (IPPROTO_DONE);
1838 /* verify the inner packet is destined to a mcast group */
1839 if (!IN6_IS_ADDR_MULTICAST(&eip6->ip6_dst)) {
1840 PIM6STAT_INC(pim6s_rcv_badregisters);
1841 MRT6_DLOG(DEBUG_PIM, "inner packet of register "
1842 "is not multicast %s",
1843 ip6_sprintf(ip6bufd, &eip6->ip6_dst));
1845 return (IPPROTO_DONE);
1849 * make a copy of the whole header to pass to the daemon later.
1851 mcp = m_copym(m, 0, off + PIM6_REG_MINLEN, M_NOWAIT);
1853 MRT6_DLOG(DEBUG_ANY | DEBUG_ERR, "pim register: "
1854 "could not copy register head");
1856 return (IPPROTO_DONE);
1860 * forward the inner ip6 packet; point m_data at the inner ip6.
1862 m_adj(m, off + PIM_MINLEN);
1863 MRT6_DLOG(DEBUG_PIM, "forwarding decapsulated register: "
1864 "src %s, dst %s, mif %d",
1865 ip6_sprintf(ip6bufs, &eip6->ip6_src),
1866 ip6_sprintf(ip6bufd, &eip6->ip6_dst), reg_mif_num);
1868 if_simloop(mif6table[reg_mif_num].m6_ifp, m,
1869 dst.sin6_family, 0);
1871 /* prepare the register head to send to the mrouting daemon */
1876 * Pass the PIM message up to the daemon; if it is a register message
1877 * pass the 'head' only up to the daemon. This includes the
1878 * encapsulator ip6 header, pim header, register header and the
1879 * encapsulated ip6 header.
1881 pim6_input_to_daemon:
1882 return (rip6_input(&m, &off, proto));
1886 ip6_mroute_modevent(module_t mod, int type, void *unused)
1891 MROUTER6_LOCK_INIT();
1895 pim6_encap_cookie = ip6_encap_attach(&ipv6_encap_cfg,
1897 if (pim6_encap_cookie == NULL) {
1898 printf("ip6_mroute: unable to attach pim6 encap\n");
1899 MIF6_LOCK_DESTROY();
1900 MFC6_LOCK_DESTROY();
1901 MROUTER6_LOCK_DESTROY();
1905 ip6_mforward = X_ip6_mforward;
1906 ip6_mrouter_done = X_ip6_mrouter_done;
1907 ip6_mrouter_get = X_ip6_mrouter_get;
1908 ip6_mrouter_set = X_ip6_mrouter_set;
1909 mrt6_ioctl = X_mrt6_ioctl;
1913 if (V_ip6_mrouter != NULL)
1916 if (pim6_encap_cookie) {
1917 ip6_encap_detach(pim6_encap_cookie);
1918 pim6_encap_cookie = NULL;
1920 X_ip6_mrouter_done();
1921 ip6_mforward = NULL;
1922 ip6_mrouter_done = NULL;
1923 ip6_mrouter_get = NULL;
1924 ip6_mrouter_set = NULL;
1927 MIF6_LOCK_DESTROY();
1928 MFC6_LOCK_DESTROY();
1929 MROUTER6_LOCK_DESTROY();
1933 return (EOPNOTSUPP);
1939 static moduledata_t ip6_mroutemod = {
1941 ip6_mroute_modevent,
1945 DECLARE_MODULE(ip6_mroute, ip6_mroutemod, SI_SUB_PROTO_MC, SI_ORDER_ANY);