2 * Copyright (C) 1998 WIDE Project.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $KAME: ip6_mroute.c,v 1.58 2001/12/18 02:36:31 itojun Exp $
33 * Copyright (c) 1989 Stephen Deering
34 * Copyright (c) 1992, 1993
35 * The Regents of the University of California. All rights reserved.
37 * This code is derived from software contributed to Berkeley by
38 * Stephen Deering of Stanford University.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 4. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * @(#)ip_mroute.c 8.2 (Berkeley) 11/15/93
65 * BSDI ip_mroute.c,v 2.10 1996/11/14 00:29:52 jch Exp
69 * IP multicast forwarding procedures
71 * Written by David Waitzman, BBN Labs, August 1988.
72 * Modified by Steve Deering, Stanford, February 1989.
73 * Modified by Mark J. Steiglitz, Stanford, May, 1991
74 * Modified by Van Jacobson, LBL, January 1993
75 * Modified by Ajit Thyagarajan, PARC, August 1993
76 * Modified by Bill Fenner, PARC, April 1994
78 * MROUTING Revision: 3.5.1.2 + PIM-SMv2 (pimd) Support
81 #include <sys/cdefs.h>
82 __FBSDID("$FreeBSD$");
84 #include "opt_inet6.h"
86 #include <sys/param.h>
87 #include <sys/callout.h>
88 #include <sys/errno.h>
89 #include <sys/kernel.h>
91 #include <sys/malloc.h>
93 #include <sys/module.h>
94 #include <sys/domain.h>
95 #include <sys/protosw.h>
96 #include <sys/signalvar.h>
97 #include <sys/socket.h>
98 #include <sys/socketvar.h>
99 #include <sys/sockio.h>
101 #include <sys/sysctl.h>
102 #include <sys/syslog.h>
103 #include <sys/systm.h>
104 #include <sys/time.h>
107 #include <net/if_types.h>
108 #include <net/raw_cb.h>
109 #include <net/vnet.h>
111 #include <netinet/in.h>
112 #include <netinet/in_var.h>
113 #include <netinet/icmp6.h>
114 #include <netinet/ip_encap.h>
116 #include <netinet/ip6.h>
117 #include <netinet6/ip6_var.h>
118 #include <netinet6/scope6_var.h>
119 #include <netinet6/nd6.h>
120 #include <netinet6/ip6_mroute.h>
121 #include <netinet6/ip6protosw.h>
122 #include <netinet6/pim6.h>
123 #include <netinet6/pim6_var.h>
125 static MALLOC_DEFINE(M_MRTABLE6, "mf6c", "multicast forwarding cache entry");
127 /* XXX: this is a very common idiom; move to <sys/mbuf.h> ? */
128 #define M_HASCL(m) ((m)->m_flags & M_EXT)
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 const struct ip6protosw in6_pim_protosw = {
143 .pr_domain = &inet6domain,
144 .pr_protocol = IPPROTO_PIM,
145 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
146 .pr_input = pim6_input,
147 .pr_output = rip6_output,
148 .pr_ctloutput = rip6_ctloutput,
149 .pr_usrreqs = &rip6_usrreqs
151 static int pim6_encapcheck(const struct mbuf *, int, int, void *);
153 static VNET_DEFINE(int, ip6_mrouter_ver) = 0;
154 #define V_ip6_mrouter_ver VNET(ip6_mrouter_ver)
156 SYSCTL_DECL(_net_inet6);
157 SYSCTL_DECL(_net_inet6_ip6);
158 SYSCTL_NODE(_net_inet6, IPPROTO_PIM, pim, CTLFLAG_RW, 0, "PIM");
160 static struct mrt6stat mrt6stat;
161 SYSCTL_STRUCT(_net_inet6_ip6, OID_AUTO, mrt6stat, CTLFLAG_RW,
163 "Multicast Routing Statistics (struct mrt6stat, netinet6/ip6_mroute.h)");
165 #define NO_RTE_FOUND 0x1
166 #define RTE_FOUND 0x2
168 static struct mtx mrouter6_mtx;
169 #define MROUTER6_LOCK() mtx_lock(&mrouter6_mtx)
170 #define MROUTER6_UNLOCK() mtx_unlock(&mrouter6_mtx)
171 #define MROUTER6_LOCK_ASSERT() do { \
172 mtx_assert(&mrouter6_mtx, MA_OWNED); \
173 NET_ASSERT_GIANT(); \
175 #define MROUTER6_LOCK_INIT() \
176 mtx_init(&mrouter6_mtx, "IPv6 multicast forwarding", NULL, MTX_DEF)
177 #define MROUTER6_LOCK_DESTROY() mtx_destroy(&mrouter6_mtx)
179 static struct mf6c *mf6ctable[MF6CTBLSIZ];
180 SYSCTL_OPAQUE(_net_inet6_ip6, OID_AUTO, mf6ctable, CTLFLAG_RD,
181 &mf6ctable, sizeof(mf6ctable), "S,*mf6ctable[MF6CTBLSIZ]",
182 "IPv6 Multicast Forwarding Table (struct *mf6ctable[MF6CTBLSIZ], "
183 "netinet6/ip6_mroute.h)");
185 static struct mtx mfc6_mtx;
186 #define MFC6_LOCK() mtx_lock(&mfc6_mtx)
187 #define MFC6_UNLOCK() mtx_unlock(&mfc6_mtx)
188 #define MFC6_LOCK_ASSERT() do { \
189 mtx_assert(&mfc6_mtx, MA_OWNED); \
190 NET_ASSERT_GIANT(); \
192 #define MFC6_LOCK_INIT() \
193 mtx_init(&mfc6_mtx, "IPv6 multicast forwarding cache", NULL, MTX_DEF)
194 #define MFC6_LOCK_DESTROY() mtx_destroy(&mfc6_mtx)
196 static u_char n6expire[MF6CTBLSIZ];
198 static struct mif6 mif6table[MAXMIFS];
199 SYSCTL_OPAQUE(_net_inet6_ip6, OID_AUTO, mif6table, CTLFLAG_RD,
200 &mif6table, sizeof(mif6table), "S,mif6[MAXMIFS]",
201 "IPv6 Multicast Interfaces (struct mif6[MAXMIFS], netinet6/ip6_mroute.h)");
203 static struct mtx mif6_mtx;
204 #define MIF6_LOCK() mtx_lock(&mif6_mtx)
205 #define MIF6_UNLOCK() mtx_unlock(&mif6_mtx)
206 #define MIF6_LOCK_ASSERT() mtx_assert(&mif6_mtx, MA_OWNED)
207 #define MIF6_LOCK_INIT() \
208 mtx_init(&mif6_mtx, "IPv6 multicast interfaces", NULL, MTX_DEF)
209 #define MIF6_LOCK_DESTROY() mtx_destroy(&mif6_mtx)
212 static VNET_DEFINE(u_int, mrt6debug) = 0; /* debug level */
213 #define V_mrt6debug VNET(mrt6debug)
214 #define DEBUG_MFC 0x02
215 #define DEBUG_FORWARD 0x04
216 #define DEBUG_EXPIRE 0x08
217 #define DEBUG_XMIT 0x10
218 #define DEBUG_REG 0x20
219 #define DEBUG_PIM 0x40
222 static void expire_upcalls(void *);
223 #define EXPIRE_TIMEOUT (hz / 4) /* 4x / second */
224 #define UPCALL_EXPIRE 6 /* number of timeouts */
227 * XXX TODO: maintain a count to if_allmulti() calls in struct ifnet.
231 * 'Interfaces' associated with decapsulator (so we can tell
232 * packets that went through it from ones that get reflected
233 * by a broken gateway). Different from IPv4 register_if,
234 * these interfaces are linked into the system ifnet list,
235 * because per-interface IPv6 statistics are maintained in
236 * ifp->if_afdata. But it does not have any routes point
237 * to them. I.e., packets can't be sent this way. They
238 * only exist as a placeholder for multicast source
241 static struct ifnet *multicast_register_if6;
243 #define ENCAP_HOPS 64
248 static mifi_t nummifs = 0;
249 static mifi_t reg_mif_num = (mifi_t)-1;
251 static struct pim6stat pim6stat;
252 SYSCTL_STRUCT(_net_inet6_pim, PIM6CTL_STATS, stats, CTLFLAG_RD,
254 "PIM Statistics (struct pim6stat, netinet6/pim_var.h)");
256 static VNET_DEFINE(int, pim6);
257 #define V_pim6 VNET(pim6)
260 * Hash function for a source, group entry
262 #define MF6CHASH(a, g) MF6CHASHMOD((a).s6_addr32[0] ^ (a).s6_addr32[1] ^ \
263 (a).s6_addr32[2] ^ (a).s6_addr32[3] ^ \
264 (g).s6_addr32[0] ^ (g).s6_addr32[1] ^ \
265 (g).s6_addr32[2] ^ (g).s6_addr32[3])
268 * Find a route for a given origin IPv6 address and Multicast group address.
270 #define MF6CFIND(o, g, rt) do { \
271 struct mf6c *_rt = mf6ctable[MF6CHASH(o,g)]; \
273 mrt6stat.mrt6s_mfc_lookups++; \
275 if (IN6_ARE_ADDR_EQUAL(&_rt->mf6c_origin.sin6_addr, &(o)) && \
276 IN6_ARE_ADDR_EQUAL(&_rt->mf6c_mcastgrp.sin6_addr, &(g)) && \
277 (_rt->mf6c_stall == NULL)) { \
281 _rt = _rt->mf6c_next; \
284 mrt6stat.mrt6s_mfc_misses++; \
286 } while (/*CONSTCOND*/ 0)
289 * Macros to compute elapsed time efficiently
290 * Borrowed from Van Jacobson's scheduling code
291 * XXX: replace with timersub() ?
293 #define TV_DELTA(a, b, delta) do { \
296 delta = (a).tv_usec - (b).tv_usec; \
297 if ((xxs = (a).tv_sec - (b).tv_sec)) { \
306 delta += (1000000 * xxs); \
309 } while (/*CONSTCOND*/ 0)
311 /* XXX: replace with timercmp(a, b, <) ? */
312 #define TV_LT(a, b) (((a).tv_usec < (b).tv_usec && \
313 (a).tv_sec <= (b).tv_sec) || (a).tv_sec < (b).tv_sec)
316 #define UPCALL_MAX 50
317 static u_long upcall_data[UPCALL_MAX + 1];
318 static void collate();
319 #endif /* UPCALL_TIMING */
321 static int ip6_mrouter_init(struct socket *, int, int);
322 static int add_m6fc(struct mf6cctl *);
323 static int add_m6if(struct mif6ctl *);
324 static int del_m6fc(struct mf6cctl *);
325 static int del_m6if(mifi_t *);
326 static int del_m6if_locked(mifi_t *);
327 static int get_mif6_cnt(struct sioc_mif_req6 *);
328 static int get_sg_cnt(struct sioc_sg_req6 *);
330 static struct callout expire_upcalls_ch;
332 int X_ip6_mforward(struct ip6_hdr *, struct ifnet *, struct mbuf *);
333 int X_ip6_mrouter_done(void);
334 int X_ip6_mrouter_set(struct socket *, struct sockopt *);
335 int X_ip6_mrouter_get(struct socket *, struct sockopt *);
336 int X_mrt6_ioctl(u_long, caddr_t);
339 * Handle MRT setsockopt commands to modify the multicast routing tables.
342 X_ip6_mrouter_set(struct socket *so, struct sockopt *sopt)
350 if (so != V_ip6_mrouter && sopt->sopt_name != MRT6_INIT)
353 switch (sopt->sopt_name) {
358 error = sooptcopyin(sopt, &optval, sizeof(optval),
362 error = ip6_mrouter_init(so, optval, sopt->sopt_name);
365 error = X_ip6_mrouter_done();
368 error = sooptcopyin(sopt, &mifc, sizeof(mifc), sizeof(mifc));
371 error = add_m6if(&mifc);
374 error = sooptcopyin(sopt, &mfcc, sizeof(mfcc), sizeof(mfcc));
377 error = add_m6fc(&mfcc);
380 error = sooptcopyin(sopt, &mfcc, sizeof(mfcc), sizeof(mfcc));
383 error = del_m6fc(&mfcc);
386 error = sooptcopyin(sopt, &mifi, sizeof(mifi), sizeof(mifi));
389 error = del_m6if(&mifi);
392 error = sooptcopyin(sopt, &optval, sizeof(optval),
396 error = set_pim6(&optval);
407 * Handle MRT getsockopt commands
410 X_ip6_mrouter_get(struct socket *so, struct sockopt *sopt)
414 if (so != V_ip6_mrouter)
417 switch (sopt->sopt_name) {
419 error = sooptcopyout(sopt, &V_pim6, sizeof(V_pim6));
426 * Handle ioctl commands to obtain information from the cache
429 X_mrt6_ioctl(u_long cmd, caddr_t data)
436 case SIOCGETSGCNT_IN6:
437 ret = get_sg_cnt((struct sioc_sg_req6 *)data);
440 case SIOCGETMIFCNT_IN6:
441 ret = get_mif6_cnt((struct sioc_mif_req6 *)data);
452 * returns the packet, byte, rpf-failure count for the source group provided
455 get_sg_cnt(struct sioc_sg_req6 *req)
464 MF6CFIND(req->src.sin6_addr, req->grp.sin6_addr, rt);
468 req->pktcnt = rt->mf6c_pkt_cnt;
469 req->bytecnt = rt->mf6c_byte_cnt;
470 req->wrong_if = rt->mf6c_wrong_if;
479 * returns the input and output packet and byte counts on the mif provided
482 get_mif6_cnt(struct sioc_mif_req6 *req)
492 if (mifi >= nummifs) {
495 req->icount = mif6table[mifi].m6_pkt_in;
496 req->ocount = mif6table[mifi].m6_pkt_out;
497 req->ibytes = mif6table[mifi].m6_bytes_in;
498 req->obytes = mif6table[mifi].m6_bytes_out;
509 if ((*i != 1) && (*i != 0))
518 * Enable multicast routing
521 ip6_mrouter_init(struct socket *so, int v, int cmd)
527 "ip6_mrouter_init: so_type = %d, pr_protocol = %d\n",
528 so->so_type, so->so_proto->pr_protocol);
531 if (so->so_type != SOCK_RAW ||
532 so->so_proto->pr_protocol != IPPROTO_ICMPV6)
536 return (ENOPROTOOPT);
540 if (V_ip6_mrouter != NULL) {
546 V_ip6_mrouter_ver = cmd;
548 bzero((caddr_t)mf6ctable, sizeof(mf6ctable));
549 bzero((caddr_t)n6expire, sizeof(n6expire));
551 V_pim6 = 0;/* used for stubbing out/in pim stuff */
553 callout_init(&expire_upcalls_ch, 0);
554 callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT,
555 expire_upcalls, NULL);
561 log(LOG_DEBUG, "ip6_mrouter_init\n");
568 * Disable IPv6 multicast forwarding.
571 X_ip6_mrouter_done(void)
580 if (V_ip6_mrouter == NULL) {
586 * For each phyint in use, disable promiscuous reception of all IPv6
589 for (mifi = 0; mifi < nummifs; mifi++) {
590 if (mif6table[mifi].m6_ifp &&
591 !(mif6table[mifi].m6_flags & MIFF_REGISTER)) {
592 if_allmulti(mif6table[mifi].m6_ifp, 0);
595 bzero((caddr_t)mif6table, sizeof(mif6table));
598 V_pim6 = 0; /* used to stub out/in pim specific code */
600 callout_stop(&expire_upcalls_ch);
603 * Free all multicast forwarding cache entries.
606 for (i = 0; i < MF6CTBLSIZ; i++) {
611 for (rte = rt->mf6c_stall; rte != NULL; ) {
612 struct rtdetq *n = rte->next;
615 free(rte, M_MRTABLE6);
620 free(frt, M_MRTABLE6);
623 bzero((caddr_t)mf6ctable, sizeof(mf6ctable));
627 * Reset register interface
629 if (reg_mif_num != (mifi_t)-1 && multicast_register_if6 != NULL) {
630 if_detach(multicast_register_if6);
631 if_free(multicast_register_if6);
632 reg_mif_num = (mifi_t)-1;
633 multicast_register_if6 = NULL;
636 V_ip6_mrouter = NULL;
637 V_ip6_mrouter_ver = 0;
643 log(LOG_DEBUG, "ip6_mrouter_done\n");
649 static struct sockaddr_in6 sin6 = { sizeof(sin6), AF_INET6 };
652 * Add a mif to the mif table
655 add_m6if(struct mif6ctl *mifcp)
663 if (mifcp->mif6c_mifi >= MAXMIFS) {
667 mifp = mif6table + mifcp->mif6c_mifi;
668 if (mifp->m6_ifp != NULL) {
670 return (EADDRINUSE); /* XXX: is it appropriate? */
672 if (mifcp->mif6c_pifi == 0 || mifcp->mif6c_pifi > V_if_index) {
677 ifp = ifnet_byindex(mifcp->mif6c_pifi);
679 if (mifcp->mif6c_flags & MIFF_REGISTER) {
680 if (reg_mif_num == (mifi_t)-1) {
681 ifp = if_alloc(IFT_OTHER);
683 if_initname(ifp, "register_mif", 0);
684 ifp->if_flags |= IFF_LOOPBACK;
686 multicast_register_if6 = ifp;
687 reg_mif_num = mifcp->mif6c_mifi;
689 * it is impossible to guess the ifindex of the
690 * register interface. So mif6c_pifi is automatically
693 mifcp->mif6c_pifi = ifp->if_index;
695 ifp = multicast_register_if6;
698 /* Make sure the interface supports multicast */
699 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
704 error = if_allmulti(ifp, 1);
711 mifp->m6_flags = mifcp->mif6c_flags;
714 /* initialize per mif pkt counters */
716 mifp->m6_pkt_out = 0;
717 mifp->m6_bytes_in = 0;
718 mifp->m6_bytes_out = 0;
719 bzero(&mifp->m6_route, sizeof(mifp->m6_route));
721 /* Adjust nummifs up if the mifi is higher than nummifs */
722 if (nummifs <= mifcp->mif6c_mifi)
723 nummifs = mifcp->mif6c_mifi + 1;
730 "add_mif #%d, phyint %s\n",
739 * Delete a mif from the mif table
742 del_m6if_locked(mifi_t *mifip)
744 struct mif6 *mifp = mif6table + *mifip;
750 if (*mifip >= nummifs)
752 if (mifp->m6_ifp == NULL)
755 if (!(mifp->m6_flags & MIFF_REGISTER)) {
756 /* XXX: TODO: Maintain an ALLMULTI refcount in struct ifnet. */
760 if (reg_mif_num != (mifi_t)-1 &&
761 multicast_register_if6 != NULL) {
762 if_detach(multicast_register_if6);
763 if_free(multicast_register_if6);
764 reg_mif_num = (mifi_t)-1;
765 multicast_register_if6 = NULL;
769 bzero((caddr_t)mifp, sizeof(*mifp));
771 /* Adjust nummifs down */
772 for (mifi = nummifs; mifi > 0; mifi--)
773 if (mif6table[mifi - 1].m6_ifp)
779 log(LOG_DEBUG, "del_m6if %d, nummifs %d\n", *mifip, nummifs);
786 del_m6if(mifi_t *mifip)
791 cc = del_m6if_locked(mifip);
801 add_m6fc(struct mf6cctl *mfccp)
807 char ip6bufo[INET6_ADDRSTRLEN], ip6bufg[INET6_ADDRSTRLEN];
811 MF6CFIND(mfccp->mf6cc_origin.sin6_addr,
812 mfccp->mf6cc_mcastgrp.sin6_addr, rt);
814 /* If an entry already exists, just update the fields */
817 if (V_mrt6debug & DEBUG_MFC) {
819 "add_m6fc no upcall h %d o %s g %s p %x\n",
820 ip6_sprintf(ip6bufo, &mfccp->mf6cc_origin.sin6_addr),
821 ip6_sprintf(ip6bufg, &mfccp->mf6cc_mcastgrp.sin6_addr),
822 mfccp->mf6cc_parent);
826 rt->mf6c_parent = mfccp->mf6cc_parent;
827 rt->mf6c_ifset = mfccp->mf6cc_ifset;
834 * Find the entry for which the upcall was made and update
836 hash = MF6CHASH(mfccp->mf6cc_origin.sin6_addr,
837 mfccp->mf6cc_mcastgrp.sin6_addr);
838 for (rt = mf6ctable[hash], nstl = 0; rt; rt = rt->mf6c_next) {
839 if (IN6_ARE_ADDR_EQUAL(&rt->mf6c_origin.sin6_addr,
840 &mfccp->mf6cc_origin.sin6_addr) &&
841 IN6_ARE_ADDR_EQUAL(&rt->mf6c_mcastgrp.sin6_addr,
842 &mfccp->mf6cc_mcastgrp.sin6_addr) &&
843 (rt->mf6c_stall != NULL)) {
847 "add_m6fc: %s o %s g %s p %x dbx %p\n",
848 "multiple kernel entries",
850 &mfccp->mf6cc_origin.sin6_addr),
852 &mfccp->mf6cc_mcastgrp.sin6_addr),
853 mfccp->mf6cc_parent, rt->mf6c_stall);
856 if (V_mrt6debug & DEBUG_MFC)
858 "add_m6fc o %s g %s p %x dbg %x\n",
860 &mfccp->mf6cc_origin.sin6_addr),
862 &mfccp->mf6cc_mcastgrp.sin6_addr),
863 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
898 if (V_mrt6debug & DEBUG_MFC)
900 "add_mfc no upcall h %d o %s g %s p %x\n",
902 ip6_sprintf(ip6bufo, &mfccp->mf6cc_origin.sin6_addr),
903 ip6_sprintf(ip6bufg, &mfccp->mf6cc_mcastgrp.sin6_addr),
904 mfccp->mf6cc_parent);
907 for (rt = mf6ctable[hash]; rt; rt = rt->mf6c_next) {
909 if (IN6_ARE_ADDR_EQUAL(&rt->mf6c_origin.sin6_addr,
910 &mfccp->mf6cc_origin.sin6_addr)&&
911 IN6_ARE_ADDR_EQUAL(&rt->mf6c_mcastgrp.sin6_addr,
912 &mfccp->mf6cc_mcastgrp.sin6_addr)) {
914 rt->mf6c_origin = mfccp->mf6cc_origin;
915 rt->mf6c_mcastgrp = mfccp->mf6cc_mcastgrp;
916 rt->mf6c_parent = mfccp->mf6cc_parent;
917 rt->mf6c_ifset = mfccp->mf6cc_ifset;
918 /* initialize pkt counters per src-grp */
919 rt->mf6c_pkt_cnt = 0;
920 rt->mf6c_byte_cnt = 0;
921 rt->mf6c_wrong_if = 0;
929 /* no upcall, so make a new entry */
930 rt = (struct mf6c *)malloc(sizeof(*rt), M_MRTABLE6,
937 /* insert new entry at head of hash chain */
938 rt->mf6c_origin = mfccp->mf6cc_origin;
939 rt->mf6c_mcastgrp = mfccp->mf6cc_mcastgrp;
940 rt->mf6c_parent = mfccp->mf6cc_parent;
941 rt->mf6c_ifset = mfccp->mf6cc_ifset;
942 /* initialize pkt counters per src-grp */
943 rt->mf6c_pkt_cnt = 0;
944 rt->mf6c_byte_cnt = 0;
945 rt->mf6c_wrong_if = 0;
947 rt->mf6c_stall = NULL;
949 /* link into table */
950 rt->mf6c_next = mf6ctable[hash];
951 mf6ctable[hash] = rt;
961 * collect delay statistics on the upcalls
964 collate(struct timeval *t)
974 TV_DELTA(tp, *t, delta);
983 #endif /* UPCALL_TIMING */
986 * Delete an mfc entry
989 del_m6fc(struct mf6cctl *mfccp)
991 struct sockaddr_in6 origin;
992 struct sockaddr_in6 mcastgrp;
997 origin = mfccp->mf6cc_origin;
998 mcastgrp = mfccp->mf6cc_mcastgrp;
999 hash = MF6CHASH(origin.sin6_addr, mcastgrp.sin6_addr);
1002 if (V_mrt6debug & DEBUG_MFC) {
1003 char ip6bufo[INET6_ADDRSTRLEN], ip6bufg[INET6_ADDRSTRLEN];
1004 log(LOG_DEBUG,"del_m6fc orig %s mcastgrp %s\n",
1005 ip6_sprintf(ip6bufo, &origin.sin6_addr),
1006 ip6_sprintf(ip6bufg, &mcastgrp.sin6_addr));
1012 nptr = &mf6ctable[hash];
1013 while ((rt = *nptr) != NULL) {
1014 if (IN6_ARE_ADDR_EQUAL(&origin.sin6_addr,
1015 &rt->mf6c_origin.sin6_addr) &&
1016 IN6_ARE_ADDR_EQUAL(&mcastgrp.sin6_addr,
1017 &rt->mf6c_mcastgrp.sin6_addr) &&
1018 rt->mf6c_stall == NULL)
1021 nptr = &rt->mf6c_next;
1025 return (EADDRNOTAVAIL);
1028 *nptr = rt->mf6c_next;
1029 free(rt, M_MRTABLE6);
1037 socket_send(struct socket *s, struct mbuf *mm, struct sockaddr_in6 *src)
1041 if (sbappendaddr(&s->so_rcv,
1042 (struct sockaddr *)src,
1043 mm, (struct mbuf *)0) != 0) {
1053 * IPv6 multicast forwarding function. This function assumes that the packet
1054 * pointed to by "ip6" has arrived on (or is about to be sent to) the interface
1055 * pointed to by "ifp", and the packet is to be relayed to other networks
1056 * that have members of the packet's destination IPv6 multicast group.
1058 * The packet is returned unscathed to the caller, unless it is
1059 * erroneous, in which case a non-zero return value tells the caller to
1062 * NOTE: this implementation assumes that m->m_pkthdr.rcvif is NULL iff
1063 * this function is called in the originating context (i.e., not when
1064 * forwarding a packet from other node). ip6_output(), which is currently the
1065 * only function that calls this function is called in the originating context,
1066 * explicitly ensures this condition. It is caller's responsibility to ensure
1067 * that if this function is called from somewhere else in the originating
1068 * context in the future.
1071 X_ip6_mforward(struct ip6_hdr *ip6, struct ifnet *ifp, struct mbuf *m)
1077 char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
1080 if (V_mrt6debug & DEBUG_FORWARD)
1081 log(LOG_DEBUG, "ip6_mforward: src %s, dst %s, ifindex %d\n",
1082 ip6_sprintf(ip6bufs, &ip6->ip6_src),
1083 ip6_sprintf(ip6bufd, &ip6->ip6_dst),
1088 * Don't forward a packet with Hop limit of zero or one,
1089 * or a packet destined to a local-only group.
1091 if (ip6->ip6_hlim <= 1 || IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst) ||
1092 IN6_IS_ADDR_MC_LINKLOCAL(&ip6->ip6_dst))
1097 * Source address check: do not forward packets with unspecified
1098 * source. It was discussed in July 2000, on ipngwg mailing list.
1099 * This is rather more serious than unicast cases, because some
1100 * MLD packets can be sent with the unspecified source address
1101 * (although such packets must normally set 1 to the hop limit field).
1103 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
1104 V_ip6stat.ip6s_cantforward++;
1105 if (V_ip6_log_time + V_ip6_log_interval < time_second) {
1106 V_ip6_log_time = time_second;
1109 "from %s to %s nxt %d received on %s\n",
1110 ip6_sprintf(ip6bufs, &ip6->ip6_src),
1111 ip6_sprintf(ip6bufd, &ip6->ip6_dst),
1113 if_name(m->m_pkthdr.rcvif));
1121 * Determine forwarding mifs from the forwarding cache table
1123 MF6CFIND(ip6->ip6_src, ip6->ip6_dst, rt);
1125 /* Entry exists, so forward if necessary */
1128 return (ip6_mdq(m, ifp, rt));
1131 * If we don't have a route for packet's origin,
1132 * Make a copy of the packet &
1133 * send message to routing daemon
1140 #ifdef UPCALL_TIMING
1144 #endif /* UPCALL_TIMING */
1146 mrt6stat.mrt6s_no_route++;
1148 if (V_mrt6debug & (DEBUG_FORWARD | DEBUG_MFC))
1149 log(LOG_DEBUG, "ip6_mforward: no rte s %s g %s\n",
1150 ip6_sprintf(ip6bufs, &ip6->ip6_src),
1151 ip6_sprintf(ip6bufd, &ip6->ip6_dst));
1155 * Allocate mbufs early so that we don't do extra work if we
1156 * are just going to fail anyway.
1158 rte = (struct rtdetq *)malloc(sizeof(*rte), M_MRTABLE6,
1164 mb0 = m_copy(m, 0, M_COPYALL);
1166 * Pullup packet header if needed before storing it,
1167 * as other references may modify it in the meantime.
1170 (M_HASCL(mb0) || mb0->m_len < sizeof(struct ip6_hdr)))
1171 mb0 = m_pullup(mb0, sizeof(struct ip6_hdr));
1173 free(rte, M_MRTABLE6);
1178 /* is there an upcall waiting for this packet? */
1179 hash = MF6CHASH(ip6->ip6_src, ip6->ip6_dst);
1180 for (rt = mf6ctable[hash]; rt; rt = rt->mf6c_next) {
1181 if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_src,
1182 &rt->mf6c_origin.sin6_addr) &&
1183 IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
1184 &rt->mf6c_mcastgrp.sin6_addr) &&
1185 (rt->mf6c_stall != NULL))
1192 struct omrt6msg *oim;
1195 /* no upcall, so make a new entry */
1196 rt = (struct mf6c *)malloc(sizeof(*rt), M_MRTABLE6,
1199 free(rte, M_MRTABLE6);
1205 * Make a copy of the header to send to the user
1208 mm = m_copy(mb0, 0, sizeof(struct ip6_hdr));
1211 free(rte, M_MRTABLE6);
1213 free(rt, M_MRTABLE6);
1219 * Send message to routing daemon
1221 sin6.sin6_addr = ip6->ip6_src;
1227 switch (V_ip6_mrouter_ver) {
1230 oim = mtod(mm, struct omrt6msg *);
1231 oim->im6_msgtype = MRT6MSG_NOCACHE;
1236 im = mtod(mm, struct mrt6msg *);
1237 im->im6_msgtype = MRT6MSG_NOCACHE;
1241 free(rte, M_MRTABLE6);
1243 free(rt, M_MRTABLE6);
1249 if (V_mrt6debug & DEBUG_FORWARD)
1251 "getting the iif info in the kernel\n");
1254 for (mifp = mif6table, mifi = 0;
1255 mifi < nummifs && mifp->m6_ifp != ifp;
1259 switch (V_ip6_mrouter_ver) {
1262 oim->im6_mif = mifi;
1270 if (socket_send(V_ip6_mrouter, mm, &sin6) < 0) {
1271 log(LOG_WARNING, "ip6_mforward: ip6_mrouter "
1272 "socket queue full\n");
1273 mrt6stat.mrt6s_upq_sockfull++;
1274 free(rte, M_MRTABLE6);
1276 free(rt, M_MRTABLE6);
1281 mrt6stat.mrt6s_upcalls++;
1283 /* insert new entry at head of hash chain */
1284 bzero(rt, sizeof(*rt));
1285 rt->mf6c_origin.sin6_family = AF_INET6;
1286 rt->mf6c_origin.sin6_len = sizeof(struct sockaddr_in6);
1287 rt->mf6c_origin.sin6_addr = ip6->ip6_src;
1288 rt->mf6c_mcastgrp.sin6_family = AF_INET6;
1289 rt->mf6c_mcastgrp.sin6_len = sizeof(struct sockaddr_in6);
1290 rt->mf6c_mcastgrp.sin6_addr = ip6->ip6_dst;
1291 rt->mf6c_expire = UPCALL_EXPIRE;
1293 rt->mf6c_parent = MF6C_INCOMPLETE_PARENT;
1295 /* link into table */
1296 rt->mf6c_next = mf6ctable[hash];
1297 mf6ctable[hash] = rt;
1298 /* Add this entry to the end of the queue */
1299 rt->mf6c_stall = rte;
1301 /* determine if q has overflowed */
1305 for (p = &rt->mf6c_stall; *p != NULL; p = &(*p)->next)
1306 if (++npkts > MAX_UPQ6) {
1307 mrt6stat.mrt6s_upq_ovflw++;
1308 free(rte, M_MRTABLE6);
1314 /* Add this entry to the end of the queue */
1321 #ifdef UPCALL_TIMING
1323 #endif /* UPCALL_TIMING */
1332 * Clean up cache entries if upcalls are not serviced
1333 * Call from the Slow Timeout mechanism, every half second.
1336 expire_upcalls(void *unused)
1339 struct mf6c *mfc, **nptr;
1343 for (i = 0; i < MF6CTBLSIZ; i++) {
1344 if (n6expire[i] == 0)
1346 nptr = &mf6ctable[i];
1347 while ((mfc = *nptr) != NULL) {
1348 rte = mfc->mf6c_stall;
1350 * Skip real cache entries
1351 * Make sure it wasn't marked to not expire (shouldn't happen)
1355 mfc->mf6c_expire != 0 &&
1356 --mfc->mf6c_expire == 0) {
1358 if (V_mrt6debug & DEBUG_EXPIRE) {
1359 char ip6bufo[INET6_ADDRSTRLEN];
1360 char ip6bufg[INET6_ADDRSTRLEN];
1361 log(LOG_DEBUG, "expire_upcalls: expiring (%s %s)\n",
1362 ip6_sprintf(ip6bufo, &mfc->mf6c_origin.sin6_addr),
1363 ip6_sprintf(ip6bufg, &mfc->mf6c_mcastgrp.sin6_addr));
1367 * drop all the packets
1368 * free the mbuf with the pkt, if, timing info
1371 struct rtdetq *n = rte->next;
1373 free(rte, M_MRTABLE6);
1375 } while (rte != NULL);
1376 mrt6stat.mrt6s_cache_cleanups++;
1379 *nptr = mfc->mf6c_next;
1380 free(mfc, M_MRTABLE6);
1382 nptr = &mfc->mf6c_next;
1387 callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT,
1388 expire_upcalls, NULL);
1392 * Packet forwarding routine once entry in the cache is made
1395 ip6_mdq(struct mbuf *m, struct ifnet *ifp, struct mf6c *rt)
1397 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1400 int plen = m->m_pkthdr.len;
1401 struct in6_addr src0, dst0; /* copies for local work */
1402 u_int32_t iszone, idzone, oszone, odzone;
1406 * Macro to send packet on mif. Since RSVP packets don't get counted on
1407 * input, they shouldn't get counted on output, so statistics keeping is
1411 #define MC6_SEND(ip6, mifp, m) do { \
1412 if ((mifp)->m6_flags & MIFF_REGISTER) \
1413 register_send((ip6), (mifp), (m)); \
1415 phyint_send((ip6), (mifp), (m)); \
1416 } while (/*CONSTCOND*/ 0)
1419 * Don't forward if it didn't arrive from the parent mif
1422 mifi = rt->mf6c_parent;
1423 if ((mifi >= nummifs) || (mif6table[mifi].m6_ifp != ifp)) {
1424 /* came in the wrong interface */
1426 if (V_mrt6debug & DEBUG_FORWARD)
1428 "wrong if: ifid %d mifi %d mififid %x\n",
1429 ifp->if_index, mifi,
1430 mif6table[mifi].m6_ifp->if_index);
1432 mrt6stat.mrt6s_wrong_if++;
1433 rt->mf6c_wrong_if++;
1435 * If we are doing PIM processing, and we are forwarding
1436 * packets on this interface, send a message to the
1439 /* have to make sure this is a valid mif */
1440 if (mifi < nummifs && mif6table[mifi].m6_ifp)
1441 if (V_pim6 && (m->m_flags & M_LOOP) == 0) {
1443 * Check the M_LOOP flag to avoid an
1444 * unnecessary PIM assert.
1445 * XXX: M_LOOP is an ad-hoc hack...
1447 static struct sockaddr_in6 sin6 =
1448 { sizeof(sin6), AF_INET6 };
1453 struct omrt6msg *oim;
1456 mm = m_copy(m, 0, sizeof(struct ip6_hdr));
1459 mm->m_len < sizeof(struct ip6_hdr)))
1460 mm = m_pullup(mm, sizeof(struct ip6_hdr));
1468 switch (V_ip6_mrouter_ver) {
1471 oim = mtod(mm, struct omrt6msg *);
1472 oim->im6_msgtype = MRT6MSG_WRONGMIF;
1477 im = mtod(mm, struct mrt6msg *);
1478 im->im6_msgtype = MRT6MSG_WRONGMIF;
1486 for (mifp = mif6table, iif = 0;
1487 iif < nummifs && mifp &&
1488 mifp->m6_ifp != ifp;
1492 switch (V_ip6_mrouter_ver) {
1496 sin6.sin6_addr = oim->im6_src;
1501 sin6.sin6_addr = im->im6_src;
1505 mrt6stat.mrt6s_upcalls++;
1507 if (socket_send(V_ip6_mrouter, mm, &sin6) < 0) {
1510 log(LOG_WARNING, "mdq, ip6_mrouter socket queue full\n");
1512 ++mrt6stat.mrt6s_upq_sockfull;
1514 } /* if socket Q full */
1517 } /* if wrong iif */
1519 /* If I sourced this packet, it counts as output, else it was input. */
1520 if (m->m_pkthdr.rcvif == NULL) {
1521 /* XXX: is rcvif really NULL when output?? */
1522 mif6table[mifi].m6_pkt_out++;
1523 mif6table[mifi].m6_bytes_out += plen;
1525 mif6table[mifi].m6_pkt_in++;
1526 mif6table[mifi].m6_bytes_in += plen;
1529 rt->mf6c_byte_cnt += plen;
1532 * For each mif, forward a copy of the packet if there are group
1533 * members downstream on the interface.
1535 src0 = ip6->ip6_src;
1536 dst0 = ip6->ip6_dst;
1537 if ((error = in6_setscope(&src0, ifp, &iszone)) != 0 ||
1538 (error = in6_setscope(&dst0, ifp, &idzone)) != 0) {
1539 V_ip6stat.ip6s_badscope++;
1542 for (mifp = mif6table, mifi = 0; mifi < nummifs; mifp++, mifi++) {
1543 if (IF_ISSET(mifi, &rt->mf6c_ifset)) {
1545 * check if the outgoing packet is going to break
1547 * XXX For packets through PIM register tunnel
1548 * interface, we believe a routing daemon.
1550 if (!(mif6table[rt->mf6c_parent].m6_flags &
1552 !(mif6table[mifi].m6_flags & MIFF_REGISTER)) {
1553 if (in6_setscope(&src0, mif6table[mifi].m6_ifp,
1555 in6_setscope(&dst0, mif6table[mifi].m6_ifp,
1559 V_ip6stat.ip6s_badscope++;
1565 mifp->m6_bytes_out += plen;
1566 MC6_SEND(ip6, mifp, m);
1573 phyint_send(struct ip6_hdr *ip6, struct mif6 *mifp, struct mbuf *m)
1575 struct mbuf *mb_copy;
1576 struct ifnet *ifp = mifp->m6_ifp;
1578 struct sockaddr_in6 *dst6;
1581 dst6 = &mifp->m6_route.ro_dst;
1584 * Make a new reference to the packet; make sure that
1585 * the IPv6 header is actually copied, not just referenced,
1586 * so that ip6_output() only scribbles on the copy.
1588 mb_copy = m_copy(m, 0, M_COPYALL);
1590 (M_HASCL(mb_copy) || mb_copy->m_len < sizeof(struct ip6_hdr)))
1591 mb_copy = m_pullup(mb_copy, sizeof(struct ip6_hdr));
1592 if (mb_copy == NULL) {
1595 /* set MCAST flag to the outgoing packet */
1596 mb_copy->m_flags |= M_MCAST;
1599 * If we sourced the packet, call ip6_output since we may devide
1600 * the packet into fragments when the packet is too big for the
1601 * outgoing interface.
1602 * Otherwise, we can simply send the packet to the interface
1605 if (m->m_pkthdr.rcvif == NULL) {
1606 struct ip6_moptions im6o;
1608 im6o.im6o_multicast_ifp = ifp;
1609 /* XXX: ip6_output will override ip6->ip6_hlim */
1610 im6o.im6o_multicast_hlim = ip6->ip6_hlim;
1611 im6o.im6o_multicast_loop = 1;
1612 error = ip6_output(mb_copy, NULL, &mifp->m6_route,
1613 IPV6_FORWARDING, &im6o, NULL, NULL);
1616 if (V_mrt6debug & DEBUG_XMIT)
1617 log(LOG_DEBUG, "phyint_send on mif %d err %d\n",
1618 mifp - mif6table, error);
1624 * If configured to loop back multicasts by default,
1625 * loop back a copy now.
1627 if (in6_mcast_loop) {
1628 dst6->sin6_len = sizeof(struct sockaddr_in6);
1629 dst6->sin6_family = AF_INET6;
1630 dst6->sin6_addr = ip6->ip6_dst;
1631 ip6_mloopback(ifp, m, &mifp->m6_route.ro_dst);
1635 * Put the packet into the sending queue of the outgoing interface
1636 * if it would fit in the MTU of the interface.
1638 linkmtu = IN6_LINKMTU(ifp);
1639 if (mb_copy->m_pkthdr.len <= linkmtu || linkmtu < IPV6_MMTU) {
1640 dst6->sin6_len = sizeof(struct sockaddr_in6);
1641 dst6->sin6_family = AF_INET6;
1642 dst6->sin6_addr = ip6->ip6_dst;
1644 * We just call if_output instead of nd6_output here, since
1645 * we need no ND for a multicast forwarded packet...right?
1647 error = (*ifp->if_output)(ifp, mb_copy,
1648 (struct sockaddr *)&mifp->m6_route.ro_dst, NULL);
1650 if (V_mrt6debug & DEBUG_XMIT)
1651 log(LOG_DEBUG, "phyint_send on mif %d err %d\n",
1652 mifp - mif6table, error);
1656 * pMTU discovery is intentionally disabled by default, since
1657 * various router may notify pMTU in multicast, which can be
1658 * a DDoS to a router
1660 if (V_ip6_mcast_pmtu)
1661 icmp6_error(mb_copy, ICMP6_PACKET_TOO_BIG, 0, linkmtu);
1664 if (V_mrt6debug & DEBUG_XMIT) {
1665 char ip6bufs[INET6_ADDRSTRLEN];
1666 char ip6bufd[INET6_ADDRSTRLEN];
1668 "phyint_send: packet too big on %s o %s "
1669 "g %s size %d(discarded)\n",
1671 ip6_sprintf(ip6bufs, &ip6->ip6_src),
1672 ip6_sprintf(ip6bufd, &ip6->ip6_dst),
1673 mb_copy->m_pkthdr.len);
1675 #endif /* MRT6DEBUG */
1676 m_freem(mb_copy); /* simply discard the packet */
1682 register_send(struct ip6_hdr *ip6, struct mif6 *mif, struct mbuf *m)
1685 int i, len = m->m_pkthdr.len;
1686 static struct sockaddr_in6 sin6 = { sizeof(sin6), AF_INET6 };
1687 struct mrt6msg *im6;
1691 char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
1692 log(LOG_DEBUG, "** IPv6 register_send **\n src %s dst %s\n",
1693 ip6_sprintf(ip6bufs, &ip6->ip6_src),
1694 ip6_sprintf(ip6bufd, &ip6->ip6_dst));
1697 ++pim6stat.pim6s_snd_registers;
1699 /* Make a copy of the packet to send to the user level process */
1700 MGETHDR(mm, M_DONTWAIT, MT_HEADER);
1703 mm->m_pkthdr.rcvif = NULL;
1704 mm->m_data += max_linkhdr;
1705 mm->m_len = sizeof(struct ip6_hdr);
1707 if ((mm->m_next = m_copy(m, 0, M_COPYALL)) == NULL) {
1711 i = MHLEN - M_LEADINGSPACE(mm);
1714 mm = m_pullup(mm, i);
1717 /* TODO: check it! */
1718 mm->m_pkthdr.len = len + sizeof(struct ip6_hdr);
1721 * Send message to routing daemon
1723 sin6.sin6_addr = ip6->ip6_src;
1725 im6 = mtod(mm, struct mrt6msg *);
1726 im6->im6_msgtype = MRT6MSG_WHOLEPKT;
1729 im6->im6_mif = mif - mif6table;
1731 /* iif info is not given for reg. encap.n */
1732 mrt6stat.mrt6s_upcalls++;
1734 if (socket_send(V_ip6_mrouter, mm, &sin6) < 0) {
1738 "register_send: ip6_mrouter socket queue full\n");
1740 ++mrt6stat.mrt6s_upq_sockfull;
1747 * pim6_encapcheck() is called by the encap6_input() path at runtime to
1748 * determine if a packet is for PIM; allowing PIM to be dynamically loaded
1752 pim6_encapcheck(const struct mbuf *m, int off, int proto, void *arg)
1756 KASSERT(proto == IPPROTO_PIM, ("not for IPPROTO_PIM"));
1758 if (proto != IPPROTO_PIM)
1759 return 0; /* not for us; reject the datagram. */
1761 return 64; /* claim the datagram. */
1765 * PIM sparse mode hook
1766 * Receives the pim control messages, and passes them up to the listening
1767 * socket, using rip6_input.
1768 * The only message processed is the REGISTER pim message; the pim header
1769 * is stripped off, and the inner packet is passed to register_mforward.
1772 pim6_input(struct mbuf **mp, int *offp, int proto)
1774 struct pim *pim; /* pointer to a pim struct */
1775 struct ip6_hdr *ip6;
1777 struct mbuf *m = *mp;
1781 ++pim6stat.pim6s_rcv_total;
1783 ip6 = mtod(m, struct ip6_hdr *);
1784 pimlen = m->m_pkthdr.len - *offp;
1789 if (pimlen < PIM_MINLEN) {
1790 ++pim6stat.pim6s_rcv_tooshort;
1792 if (V_mrt6debug & DEBUG_PIM)
1793 log(LOG_DEBUG,"pim6_input: PIM packet too short\n");
1796 return (IPPROTO_DONE);
1800 * if the packet is at least as big as a REGISTER, go ahead
1801 * and grab the PIM REGISTER header size, to avoid another
1802 * possible m_pullup() later.
1804 * PIM_MINLEN == pimhdr + u_int32 == 8
1805 * PIM6_REG_MINLEN == pimhdr + reghdr + eip6hdr == 4 + 4 + 40
1807 minlen = (pimlen >= PIM6_REG_MINLEN) ? PIM6_REG_MINLEN : PIM_MINLEN;
1810 * Make sure that the IP6 and PIM headers in contiguous memory, and
1811 * possibly the PIM REGISTER header
1813 #ifndef PULLDOWN_TEST
1814 IP6_EXTHDR_CHECK(m, off, minlen, IPPROTO_DONE);
1815 /* adjust pointer */
1816 ip6 = mtod(m, struct ip6_hdr *);
1818 /* adjust mbuf to point to the PIM header */
1819 pim = (struct pim *)((caddr_t)ip6 + off);
1821 IP6_EXTHDR_GET(pim, struct pim *, m, off, minlen);
1823 pim6stat.pim6s_rcv_tooshort++;
1824 return (IPPROTO_DONE);
1828 #define PIM6_CHECKSUM
1829 #ifdef PIM6_CHECKSUM
1834 * Validate checksum.
1835 * If PIM REGISTER, exclude the data packet
1837 if (pim->pim_type == PIM_REGISTER)
1838 cksumlen = PIM_MINLEN;
1842 if (in6_cksum(m, IPPROTO_PIM, off, cksumlen)) {
1843 ++pim6stat.pim6s_rcv_badsum;
1845 if (V_mrt6debug & DEBUG_PIM)
1847 "pim6_input: invalid checksum\n");
1850 return (IPPROTO_DONE);
1853 #endif /* PIM_CHECKSUM */
1855 /* PIM version check */
1856 if (pim->pim_ver != PIM_VERSION) {
1857 ++pim6stat.pim6s_rcv_badversion;
1860 "pim6_input: incorrect version %d, expecting %d\n",
1861 pim->pim_ver, PIM_VERSION);
1864 return (IPPROTO_DONE);
1867 if (pim->pim_type == PIM_REGISTER) {
1869 * since this is a REGISTER, we'll make a copy of the register
1870 * headers ip6+pim+u_int32_t+encap_ip6, to be passed up to the
1873 static struct sockaddr_in6 dst = { sizeof(dst), AF_INET6 };
1876 struct ip6_hdr *eip6;
1880 char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
1883 ++pim6stat.pim6s_rcv_registers;
1885 if ((reg_mif_num >= nummifs) || (reg_mif_num == (mifi_t) -1)) {
1887 if (V_mrt6debug & DEBUG_PIM)
1889 "pim6_input: register mif not set: %d\n",
1893 return (IPPROTO_DONE);
1896 reghdr = (u_int32_t *)(pim + 1);
1898 if ((ntohl(*reghdr) & PIM_NULL_REGISTER))
1899 goto pim6_input_to_daemon;
1904 if (pimlen < PIM6_REG_MINLEN) {
1905 ++pim6stat.pim6s_rcv_tooshort;
1906 ++pim6stat.pim6s_rcv_badregisters;
1909 "pim6_input: register packet size too "
1910 "small %d from %s\n",
1911 pimlen, ip6_sprintf(ip6bufs, &ip6->ip6_src));
1914 return (IPPROTO_DONE);
1917 eip6 = (struct ip6_hdr *) (reghdr + 1);
1919 if (V_mrt6debug & DEBUG_PIM)
1921 "pim6_input[register], eip6: %s -> %s, "
1923 ip6_sprintf(ip6bufs, &eip6->ip6_src),
1924 ip6_sprintf(ip6bufd, &eip6->ip6_dst),
1925 ntohs(eip6->ip6_plen));
1928 /* verify the version number of the inner packet */
1929 if ((eip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
1930 ++pim6stat.pim6s_rcv_badregisters;
1932 log(LOG_DEBUG, "pim6_input: invalid IP version (%d) "
1933 "of the inner packet\n",
1934 (eip6->ip6_vfc & IPV6_VERSION));
1937 return (IPPROTO_NONE);
1940 /* verify the inner packet is destined to a mcast group */
1941 if (!IN6_IS_ADDR_MULTICAST(&eip6->ip6_dst)) {
1942 ++pim6stat.pim6s_rcv_badregisters;
1944 if (V_mrt6debug & DEBUG_PIM)
1946 "pim6_input: inner packet of register "
1947 "is not multicast %s\n",
1948 ip6_sprintf(ip6bufd, &eip6->ip6_dst));
1951 return (IPPROTO_DONE);
1955 * make a copy of the whole header to pass to the daemon later.
1957 mcp = m_copy(m, 0, off + PIM6_REG_MINLEN);
1961 "pim6_input: pim register: "
1962 "could not copy register head\n");
1965 return (IPPROTO_DONE);
1969 * forward the inner ip6 packet; point m_data at the inner ip6.
1971 m_adj(m, off + PIM_MINLEN);
1973 if (V_mrt6debug & DEBUG_PIM) {
1975 "pim6_input: forwarding decapsulated register: "
1976 "src %s, dst %s, mif %d\n",
1977 ip6_sprintf(ip6bufs, &eip6->ip6_src),
1978 ip6_sprintf(ip6bufd, &eip6->ip6_dst),
1983 rc = if_simloop(mif6table[reg_mif_num].m6_ifp, m,
1984 dst.sin6_family, 0);
1986 /* prepare the register head to send to the mrouting daemon */
1991 * Pass the PIM message up to the daemon; if it is a register message
1992 * pass the 'head' only up to the daemon. This includes the
1993 * encapsulator ip6 header, pim header, register header and the
1994 * encapsulated ip6 header.
1996 pim6_input_to_daemon:
1997 rip6_input(&m, offp, proto);
1998 return (IPPROTO_DONE);
2002 ip6_mroute_modevent(module_t mod, int type, void *unused)
2007 MROUTER6_LOCK_INIT();
2011 pim6_encap_cookie = encap_attach_func(AF_INET6, IPPROTO_PIM,
2013 (const struct protosw *)&in6_pim_protosw, NULL);
2014 if (pim6_encap_cookie == NULL) {
2015 printf("ip6_mroute: unable to attach pim6 encap\n");
2016 MIF6_LOCK_DESTROY();
2017 MFC6_LOCK_DESTROY();
2018 MROUTER6_LOCK_DESTROY();
2022 ip6_mforward = X_ip6_mforward;
2023 ip6_mrouter_done = X_ip6_mrouter_done;
2024 ip6_mrouter_get = X_ip6_mrouter_get;
2025 ip6_mrouter_set = X_ip6_mrouter_set;
2026 mrt6_ioctl = X_mrt6_ioctl;
2030 if (V_ip6_mrouter != NULL)
2033 if (pim6_encap_cookie) {
2034 encap_detach(pim6_encap_cookie);
2035 pim6_encap_cookie = NULL;
2037 X_ip6_mrouter_done();
2038 ip6_mforward = NULL;
2039 ip6_mrouter_done = NULL;
2040 ip6_mrouter_get = NULL;
2041 ip6_mrouter_set = NULL;
2044 MIF6_LOCK_DESTROY();
2045 MFC6_LOCK_DESTROY();
2046 MROUTER6_LOCK_DESTROY();
2050 return (EOPNOTSUPP);
2056 static moduledata_t ip6_mroutemod = {
2058 ip6_mroute_modevent,
2062 DECLARE_MODULE(ip6_mroute, ip6_mroutemod, SI_SUB_PSEUDO, SI_ORDER_ANY);