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$");
85 #include "opt_inet6.h"
87 #include <sys/param.h>
88 #include <sys/callout.h>
89 #include <sys/errno.h>
90 #include <sys/kernel.h>
92 #include <sys/malloc.h>
94 #include <sys/protosw.h>
95 #include <sys/signalvar.h>
96 #include <sys/socket.h>
97 #include <sys/socketvar.h>
98 #include <sys/sockio.h>
100 #include <sys/sysctl.h>
101 #include <sys/syslog.h>
102 #include <sys/systm.h>
103 #include <sys/time.h>
104 #include <sys/vimage.h>
107 #include <net/if_types.h>
108 #include <net/raw_cb.h>
109 #include <net/route.h>
111 #include <netinet/in.h>
112 #include <netinet/in_var.h>
113 #include <netinet/icmp6.h>
115 #include <netinet/ip6.h>
116 #include <netinet6/ip6_var.h>
117 #include <netinet6/scope6_var.h>
118 #include <netinet6/nd6.h>
119 #include <netinet6/ip6_mroute.h>
120 #include <netinet6/ip6protosw.h>
121 #include <netinet6/pim6.h>
122 #include <netinet6/pim6_var.h>
124 static MALLOC_DEFINE(M_MRTABLE6, "mf6c", "multicast forwarding cache entry");
126 /* XXX: this is a very common idiom; move to <sys/mbuf.h> ? */
127 #define M_HASCL(m) ((m)->m_flags & M_EXT)
129 static int ip6_mdq(struct mbuf *, struct ifnet *, struct mf6c *);
130 static void phyint_send(struct ip6_hdr *, struct mif6 *, struct mbuf *);
132 static int set_pim6(int *);
133 static int socket_send __P((struct socket *, struct mbuf *,
134 struct sockaddr_in6 *));
135 static int register_send __P((struct ip6_hdr *, struct mif6 *,
138 extern struct domain inet6domain;
140 /* XXX: referenced from ip_mroute.c for dynamically loading this code. */
141 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
152 static int ip6_mrouter_ver = 0;
154 SYSCTL_DECL(_net_inet6);
155 SYSCTL_DECL(_net_inet6_ip6);
156 SYSCTL_NODE(_net_inet6, IPPROTO_PIM, pim, CTLFLAG_RW, 0, "PIM");
158 static struct mrt6stat mrt6stat;
159 SYSCTL_STRUCT(_net_inet6_ip6, OID_AUTO, mrt6stat, CTLFLAG_RW,
161 "Multicast Routing Statistics (struct mrt6stat, netinet6/ip6_mroute.h)");
163 #define NO_RTE_FOUND 0x1
164 #define RTE_FOUND 0x2
166 static struct mf6c *mf6ctable[MF6CTBLSIZ];
167 SYSCTL_OPAQUE(_net_inet6_ip6, OID_AUTO, mf6ctable, CTLFLAG_RD,
168 &mf6ctable, sizeof(mf6ctable), "S,*mf6ctable[MF6CTBLSIZ]",
169 "Multicast Forwarding Table (struct *mf6ctable[MF6CTBLSIZ], "
170 "netinet6/ip6_mroute.h)");
172 static u_char n6expire[MF6CTBLSIZ];
174 static struct mif6 mif6table[MAXMIFS];
175 SYSCTL_OPAQUE(_net_inet6_ip6, OID_AUTO, mif6table, CTLFLAG_RD,
176 &mif6table, sizeof(mif6table), "S,vif[MAXMIFS]",
177 "Multicast Interfaces (struct mif[MAXMIFS], netinet6/ip6_mroute.h)");
180 static u_int mrt6debug = 0; /* debug level */
181 #define DEBUG_MFC 0x02
182 #define DEBUG_FORWARD 0x04
183 #define DEBUG_EXPIRE 0x08
184 #define DEBUG_XMIT 0x10
185 #define DEBUG_REG 0x20
186 #define DEBUG_PIM 0x40
189 static void expire_upcalls(void *);
190 #define EXPIRE_TIMEOUT (hz / 4) /* 4x / second */
191 #define UPCALL_EXPIRE 6 /* number of timeouts */
195 extern struct socket *ip_mrouter;
200 * 'Interfaces' associated with decapsulator (so we can tell
201 * packets that went through it from ones that get reflected
202 * by a broken gateway). Different from IPv4 register_if,
203 * these interfaces are linked into the system ifnet list,
204 * because per-interface IPv6 statistics are maintained in
205 * ifp->if_afdata. But it does not have any routes point
206 * to them. I.e., packets can't be sent this way. They
207 * only exist as a placeholder for multicast source
210 static struct ifnet *multicast_register_if6;
212 #define ENCAP_HOPS 64
217 static mifi_t nummifs = 0;
218 static mifi_t reg_mif_num = (mifi_t)-1;
220 static struct pim6stat pim6stat;
221 SYSCTL_STRUCT(_net_inet6_pim, PIM6CTL_STATS, stats, CTLFLAG_RD,
223 "PIM Statistics (struct pim6stat, netinet6/pim_var.h)");
228 * Hash function for a source, group entry
230 #define MF6CHASH(a, g) MF6CHASHMOD((a).s6_addr32[0] ^ (a).s6_addr32[1] ^ \
231 (a).s6_addr32[2] ^ (a).s6_addr32[3] ^ \
232 (g).s6_addr32[0] ^ (g).s6_addr32[1] ^ \
233 (g).s6_addr32[2] ^ (g).s6_addr32[3])
236 * Find a route for a given origin IPv6 address and Multicast group address.
238 #define MF6CFIND(o, g, rt) do { \
239 struct mf6c *_rt = mf6ctable[MF6CHASH(o,g)]; \
241 mrt6stat.mrt6s_mfc_lookups++; \
243 if (IN6_ARE_ADDR_EQUAL(&_rt->mf6c_origin.sin6_addr, &(o)) && \
244 IN6_ARE_ADDR_EQUAL(&_rt->mf6c_mcastgrp.sin6_addr, &(g)) && \
245 (_rt->mf6c_stall == NULL)) { \
249 _rt = _rt->mf6c_next; \
252 mrt6stat.mrt6s_mfc_misses++; \
254 } while (/*CONSTCOND*/ 0)
257 * Macros to compute elapsed time efficiently
258 * Borrowed from Van Jacobson's scheduling code
259 * XXX: replace with timersub() ?
261 #define TV_DELTA(a, b, delta) do { \
264 delta = (a).tv_usec - (b).tv_usec; \
265 if ((xxs = (a).tv_sec - (b).tv_sec)) { \
274 delta += (1000000 * xxs); \
277 } while (/*CONSTCOND*/ 0)
279 /* XXX: replace with timercmp(a, b, <) ? */
280 #define TV_LT(a, b) (((a).tv_usec < (b).tv_usec && \
281 (a).tv_sec <= (b).tv_sec) || (a).tv_sec < (b).tv_sec)
284 #define UPCALL_MAX 50
285 static u_long upcall_data[UPCALL_MAX + 1];
286 static void collate();
287 #endif /* UPCALL_TIMING */
289 static int get_sg_cnt(struct sioc_sg_req6 *);
290 static int get_mif6_cnt(struct sioc_mif_req6 *);
291 static int ip6_mrouter_init(struct socket *, int, int);
292 static int add_m6if(struct mif6ctl *);
293 static int del_m6if(mifi_t *);
294 static int add_m6fc(struct mf6cctl *);
295 static int del_m6fc(struct mf6cctl *);
297 static struct callout expire_upcalls_ch;
299 int X_ip6_mforward(struct ip6_hdr *ip6, struct ifnet *ifp, struct mbuf *m);
300 int X_ip6_mrouter_done(void);
301 int X_ip6_mrouter_set(struct socket *so, struct sockopt *sopt);
302 int X_ip6_mrouter_get(struct socket *so, struct sockopt *sopt);
303 int X_mrt6_ioctl(int cmd, caddr_t data);
306 * Handle MRT setsockopt commands to modify the multicast routing tables.
309 X_ip6_mrouter_set(struct socket *so, struct sockopt *sopt)
317 if (so != ip6_mrouter && sopt->sopt_name != MRT6_INIT)
320 switch (sopt->sopt_name) {
325 error = sooptcopyin(sopt, &optval, sizeof(optval),
329 error = ip6_mrouter_init(so, optval, sopt->sopt_name);
332 error = X_ip6_mrouter_done();
335 error = sooptcopyin(sopt, &mifc, sizeof(mifc), sizeof(mifc));
338 error = add_m6if(&mifc);
341 error = sooptcopyin(sopt, &mfcc, sizeof(mfcc), sizeof(mfcc));
344 error = add_m6fc(&mfcc);
347 error = sooptcopyin(sopt, &mfcc, sizeof(mfcc), sizeof(mfcc));
350 error = del_m6fc(&mfcc);
353 error = sooptcopyin(sopt, &mifi, sizeof(mifi), sizeof(mifi));
356 error = del_m6if(&mifi);
359 error = sooptcopyin(sopt, &optval, sizeof(optval),
363 error = set_pim6(&optval);
374 * Handle MRT getsockopt commands
377 X_ip6_mrouter_get(struct socket *so, struct sockopt *sopt)
379 INIT_VNET_INET6(curvnet);
382 if (so != ip6_mrouter)
385 switch (sopt->sopt_name) {
387 error = sooptcopyout(sopt, &V_pim6, sizeof(V_pim6));
394 * Handle ioctl commands to obtain information from the cache
397 X_mrt6_ioctl(int cmd, caddr_t data)
400 case SIOCGETSGCNT_IN6:
401 return (get_sg_cnt((struct sioc_sg_req6 *)data));
402 case SIOCGETMIFCNT_IN6:
403 return (get_mif6_cnt((struct sioc_mif_req6 *)data));
410 * returns the packet, byte, rpf-failure count for the source group provided
413 get_sg_cnt(struct sioc_sg_req6 *req)
419 MF6CFIND(req->src.sin6_addr, req->grp.sin6_addr, rt);
422 req->pktcnt = rt->mf6c_pkt_cnt;
423 req->bytecnt = rt->mf6c_byte_cnt;
424 req->wrong_if = rt->mf6c_wrong_if;
428 req->pktcnt = req->bytecnt = req->wrong_if = 0xffffffff;
435 * returns the input and output packet and byte counts on the mif provided
438 get_mif6_cnt(struct sioc_mif_req6 *req)
440 mifi_t mifi = req->mifi;
445 req->icount = mif6table[mifi].m6_pkt_in;
446 req->ocount = mif6table[mifi].m6_pkt_out;
447 req->ibytes = mif6table[mifi].m6_bytes_in;
448 req->obytes = mif6table[mifi].m6_bytes_out;
456 INIT_VNET_INET6(curvnet);
457 if ((*i != 1) && (*i != 0))
466 * Enable multicast routing
469 ip6_mrouter_init(struct socket *so, int v, int cmd)
471 INIT_VNET_INET6(curvnet);
476 "ip6_mrouter_init: so_type = %d, pr_protocol = %d\n",
477 so->so_type, so->so_proto->pr_protocol);
480 if (so->so_type != SOCK_RAW ||
481 so->so_proto->pr_protocol != IPPROTO_ICMPV6)
485 return (ENOPROTOOPT);
487 if (ip6_mrouter != NULL)
491 V_ip6_mrouter_ver = cmd;
493 bzero((caddr_t)mf6ctable, sizeof(mf6ctable));
494 bzero((caddr_t)n6expire, sizeof(n6expire));
496 V_pim6 = 0;/* used for stubbing out/in pim stuff */
498 callout_init(&expire_upcalls_ch, 0);
499 callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT,
500 expire_upcalls, NULL);
504 log(LOG_DEBUG, "ip6_mrouter_init\n");
511 * Disable multicast routing
514 X_ip6_mrouter_done(void)
516 INIT_VNET_INET6(curvnet);
526 * For each phyint in use, disable promiscuous reception of all IPv6
532 * If there is still IPv4 multicast routing daemon,
533 * we remain interfaces to receive all muliticasted packets.
534 * XXX: there may be an interface in which the IPv4 multicast
535 * daemon is not interested...
541 for (mifi = 0; mifi < nummifs; mifi++) {
542 if (mif6table[mifi].m6_ifp &&
543 !(mif6table[mifi].m6_flags & MIFF_REGISTER)) {
544 if_allmulti(mif6table[mifi].m6_ifp, 0);
548 bzero((caddr_t)mif6table, sizeof(mif6table));
551 V_pim6 = 0; /* used to stub out/in pim specific code */
553 callout_stop(&expire_upcalls_ch);
556 * Free all multicast forwarding cache entries.
558 for (i = 0; i < MF6CTBLSIZ; i++) {
563 for (rte = rt->mf6c_stall; rte != NULL; ) {
564 struct rtdetq *n = rte->next;
567 free(rte, M_MRTABLE6);
572 free(frt, M_MRTABLE6);
576 bzero((caddr_t)mf6ctable, sizeof(mf6ctable));
579 * Reset register interface
581 if (reg_mif_num != (mifi_t)-1 && multicast_register_if6 != NULL) {
582 if_detach(multicast_register_if6);
583 if_free(multicast_register_if6);
584 reg_mif_num = (mifi_t)-1;
585 multicast_register_if6 = NULL;
589 V_ip6_mrouter_ver = 0;
595 log(LOG_DEBUG, "ip6_mrouter_done\n");
601 static struct sockaddr_in6 sin6 = { sizeof(sin6), AF_INET6 };
604 * Add a mif to the mif table
607 add_m6if(struct mif6ctl *mifcp)
609 INIT_VNET_NET(curvnet);
614 if (mifcp->mif6c_mifi >= MAXMIFS)
616 mifp = mif6table + mifcp->mif6c_mifi;
618 return (EADDRINUSE); /* XXX: is it appropriate? */
619 if (mifcp->mif6c_pifi == 0 || mifcp->mif6c_pifi > V_if_index)
621 ifp = ifnet_byindex(mifcp->mif6c_pifi);
623 if (mifcp->mif6c_flags & MIFF_REGISTER) {
624 if (reg_mif_num == (mifi_t)-1) {
625 ifp = if_alloc(IFT_OTHER);
627 if_initname(ifp, "register_mif", 0);
628 ifp->if_flags |= IFF_LOOPBACK;
630 multicast_register_if6 = ifp;
631 reg_mif_num = mifcp->mif6c_mifi;
633 * it is impossible to guess the ifindex of the
634 * register interface. So mif6c_pifi is automatically
637 mifcp->mif6c_pifi = ifp->if_index;
639 ifp = multicast_register_if6;
644 /* Make sure the interface supports multicast */
645 if ((ifp->if_flags & IFF_MULTICAST) == 0)
649 error = if_allmulti(ifp, 1);
656 mifp->m6_flags = mifcp->mif6c_flags;
659 /* initialize per mif pkt counters */
661 mifp->m6_pkt_out = 0;
662 mifp->m6_bytes_in = 0;
663 mifp->m6_bytes_out = 0;
666 /* Adjust nummifs up if the mifi is higher than nummifs */
667 if (nummifs <= mifcp->mif6c_mifi)
668 nummifs = mifcp->mif6c_mifi + 1;
673 "add_mif #%d, phyint %s\n",
682 * Delete a mif from the mif table
685 del_m6if(mifi_t *mifip)
687 struct mif6 *mifp = mif6table + *mifip;
692 if (*mifip >= nummifs)
694 if (mifp->m6_ifp == NULL)
699 if (!(mifp->m6_flags & MIFF_REGISTER)) {
701 * XXX: what if there is yet IPv4 multicast daemon
702 * using the interface?
708 if (reg_mif_num != (mifi_t)-1 &&
709 multicast_register_if6 != NULL) {
710 if_detach(multicast_register_if6);
711 if_free(multicast_register_if6);
712 reg_mif_num = (mifi_t)-1;
713 multicast_register_if6 = NULL;
717 bzero((caddr_t)mifp, sizeof(*mifp));
719 /* Adjust nummifs down */
720 for (mifi = nummifs; mifi > 0; mifi--)
721 if (mif6table[mifi - 1].m6_ifp)
729 log(LOG_DEBUG, "del_m6if %d, nummifs %d\n", *mifip, nummifs);
739 add_m6fc(struct mf6cctl *mfccp)
746 char ip6bufo[INET6_ADDRSTRLEN], ip6bufg[INET6_ADDRSTRLEN];
748 MF6CFIND(mfccp->mf6cc_origin.sin6_addr,
749 mfccp->mf6cc_mcastgrp.sin6_addr, rt);
751 /* If an entry already exists, just update the fields */
754 if (V_mrt6debug & DEBUG_MFC) {
756 "add_m6fc no upcall h %d o %s g %s p %x\n",
757 ip6_sprintf(ip6bufo, &mfccp->mf6cc_origin.sin6_addr),
758 ip6_sprintf(ip6bufg, &mfccp->mf6cc_mcastgrp.sin6_addr),
759 mfccp->mf6cc_parent);
764 rt->mf6c_parent = mfccp->mf6cc_parent;
765 rt->mf6c_ifset = mfccp->mf6cc_ifset;
771 * Find the entry for which the upcall was made and update
774 hash = MF6CHASH(mfccp->mf6cc_origin.sin6_addr,
775 mfccp->mf6cc_mcastgrp.sin6_addr);
776 for (rt = mf6ctable[hash], nstl = 0; rt; rt = rt->mf6c_next) {
777 if (IN6_ARE_ADDR_EQUAL(&rt->mf6c_origin.sin6_addr,
778 &mfccp->mf6cc_origin.sin6_addr) &&
779 IN6_ARE_ADDR_EQUAL(&rt->mf6c_mcastgrp.sin6_addr,
780 &mfccp->mf6cc_mcastgrp.sin6_addr) &&
781 (rt->mf6c_stall != NULL)) {
785 "add_m6fc: %s o %s g %s p %x dbx %p\n",
786 "multiple kernel entries",
788 &mfccp->mf6cc_origin.sin6_addr),
790 &mfccp->mf6cc_mcastgrp.sin6_addr),
791 mfccp->mf6cc_parent, rt->mf6c_stall);
794 if (V_mrt6debug & DEBUG_MFC)
796 "add_m6fc o %s g %s p %x dbg %x\n",
798 &mfccp->mf6cc_origin.sin6_addr),
800 &mfccp->mf6cc_mcastgrp.sin6_addr),
801 mfccp->mf6cc_parent, rt->mf6c_stall);
804 rt->mf6c_origin = mfccp->mf6cc_origin;
805 rt->mf6c_mcastgrp = mfccp->mf6cc_mcastgrp;
806 rt->mf6c_parent = mfccp->mf6cc_parent;
807 rt->mf6c_ifset = mfccp->mf6cc_ifset;
808 /* initialize pkt counters per src-grp */
809 rt->mf6c_pkt_cnt = 0;
810 rt->mf6c_byte_cnt = 0;
811 rt->mf6c_wrong_if = 0;
813 rt->mf6c_expire = 0; /* Don't clean this guy up */
816 /* free packets Qed at the end of this entry */
817 for (rte = rt->mf6c_stall; rte != NULL; ) {
818 struct rtdetq *n = rte->next;
819 ip6_mdq(rte->m, rte->ifp, rt);
823 #endif /* UPCALL_TIMING */
824 free(rte, M_MRTABLE6);
827 rt->mf6c_stall = NULL;
832 * It is possible that an entry is being inserted without an upcall
836 if (V_mrt6debug & DEBUG_MFC)
838 "add_mfc no upcall h %d o %s g %s p %x\n",
840 ip6_sprintf(ip6bufo, &mfccp->mf6cc_origin.sin6_addr),
841 ip6_sprintf(ip6bufg, &mfccp->mf6cc_mcastgrp.sin6_addr),
842 mfccp->mf6cc_parent);
845 for (rt = mf6ctable[hash]; rt; rt = rt->mf6c_next) {
847 if (IN6_ARE_ADDR_EQUAL(&rt->mf6c_origin.sin6_addr,
848 &mfccp->mf6cc_origin.sin6_addr)&&
849 IN6_ARE_ADDR_EQUAL(&rt->mf6c_mcastgrp.sin6_addr,
850 &mfccp->mf6cc_mcastgrp.sin6_addr)) {
852 rt->mf6c_origin = mfccp->mf6cc_origin;
853 rt->mf6c_mcastgrp = mfccp->mf6cc_mcastgrp;
854 rt->mf6c_parent = mfccp->mf6cc_parent;
855 rt->mf6c_ifset = mfccp->mf6cc_ifset;
856 /* initialize pkt counters per src-grp */
857 rt->mf6c_pkt_cnt = 0;
858 rt->mf6c_byte_cnt = 0;
859 rt->mf6c_wrong_if = 0;
867 /* no upcall, so make a new entry */
868 rt = (struct mf6c *)malloc(sizeof(*rt), M_MRTABLE6,
875 /* insert new entry at head of hash chain */
876 rt->mf6c_origin = mfccp->mf6cc_origin;
877 rt->mf6c_mcastgrp = mfccp->mf6cc_mcastgrp;
878 rt->mf6c_parent = mfccp->mf6cc_parent;
879 rt->mf6c_ifset = mfccp->mf6cc_ifset;
880 /* initialize pkt counters per src-grp */
881 rt->mf6c_pkt_cnt = 0;
882 rt->mf6c_byte_cnt = 0;
883 rt->mf6c_wrong_if = 0;
885 rt->mf6c_stall = NULL;
887 /* link into table */
888 rt->mf6c_next = mf6ctable[hash];
889 mf6ctable[hash] = rt;
898 * collect delay statistics on the upcalls
901 collate(struct timeval *t)
911 TV_DELTA(tp, *t, delta);
920 #endif /* UPCALL_TIMING */
923 * Delete an mfc entry
926 del_m6fc(struct mf6cctl *mfccp)
928 struct sockaddr_in6 origin;
929 struct sockaddr_in6 mcastgrp;
935 origin = mfccp->mf6cc_origin;
936 mcastgrp = mfccp->mf6cc_mcastgrp;
937 hash = MF6CHASH(origin.sin6_addr, mcastgrp.sin6_addr);
940 if (V_mrt6debug & DEBUG_MFC) {
941 char ip6bufo[INET6_ADDRSTRLEN], ip6bufg[INET6_ADDRSTRLEN];
942 log(LOG_DEBUG,"del_m6fc orig %s mcastgrp %s\n",
943 ip6_sprintf(ip6bufo, &origin.sin6_addr),
944 ip6_sprintf(ip6bufg, &mcastgrp.sin6_addr));
950 nptr = &mf6ctable[hash];
951 while ((rt = *nptr) != NULL) {
952 if (IN6_ARE_ADDR_EQUAL(&origin.sin6_addr,
953 &rt->mf6c_origin.sin6_addr) &&
954 IN6_ARE_ADDR_EQUAL(&mcastgrp.sin6_addr,
955 &rt->mf6c_mcastgrp.sin6_addr) &&
956 rt->mf6c_stall == NULL)
959 nptr = &rt->mf6c_next;
963 return (EADDRNOTAVAIL);
966 *nptr = rt->mf6c_next;
967 free(rt, M_MRTABLE6);
975 socket_send(struct socket *s, struct mbuf *mm, struct sockaddr_in6 *src)
979 if (sbappendaddr(&s->so_rcv,
980 (struct sockaddr *)src,
981 mm, (struct mbuf *)0) != 0) {
991 * IPv6 multicast forwarding function. This function assumes that the packet
992 * pointed to by "ip6" has arrived on (or is about to be sent to) the interface
993 * pointed to by "ifp", and the packet is to be relayed to other networks
994 * that have members of the packet's destination IPv6 multicast group.
996 * The packet is returned unscathed to the caller, unless it is
997 * erroneous, in which case a non-zero return value tells the caller to
1000 * NOTE: this implementation assumes that m->m_pkthdr.rcvif is NULL iff
1001 * this function is called in the originating context (i.e., not when
1002 * forwarding a packet from other node). ip6_output(), which is currently the
1003 * only function that calls this function is called in the originating context,
1004 * explicitly ensures this condition. It is caller's responsibility to ensure
1005 * that if this function is called from somewhere else in the originating
1006 * context in the future.
1009 X_ip6_mforward(struct ip6_hdr *ip6, struct ifnet *ifp, struct mbuf *m)
1011 INIT_VNET_INET6(curvnet);
1017 char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
1020 if (V_mrt6debug & DEBUG_FORWARD)
1021 log(LOG_DEBUG, "ip6_mforward: src %s, dst %s, ifindex %d\n",
1022 ip6_sprintf(ip6bufs, &ip6->ip6_src),
1023 ip6_sprintf(ip6bufd, &ip6->ip6_dst),
1028 * Don't forward a packet with Hop limit of zero or one,
1029 * or a packet destined to a local-only group.
1031 if (ip6->ip6_hlim <= 1 || IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst) ||
1032 IN6_IS_ADDR_MC_LINKLOCAL(&ip6->ip6_dst))
1037 * Source address check: do not forward packets with unspecified
1038 * source. It was discussed in July 2000, on ipngwg mailing list.
1039 * This is rather more serious than unicast cases, because some
1040 * MLD packets can be sent with the unspecified source address
1041 * (although such packets must normally set 1 to the hop limit field).
1043 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
1044 V_ip6stat.ip6s_cantforward++;
1045 if (V_ip6_log_time + V_ip6_log_interval < time_second) {
1046 V_ip6_log_time = time_second;
1049 "from %s to %s nxt %d received on %s\n",
1050 ip6_sprintf(ip6bufs, &ip6->ip6_src),
1051 ip6_sprintf(ip6bufd, &ip6->ip6_dst),
1053 if_name(m->m_pkthdr.rcvif));
1059 * Determine forwarding mifs from the forwarding cache table
1062 MF6CFIND(ip6->ip6_src, ip6->ip6_dst, rt);
1064 /* Entry exists, so forward if necessary */
1067 return (ip6_mdq(m, ifp, rt));
1070 * If we don't have a route for packet's origin,
1071 * Make a copy of the packet &
1072 * send message to routing daemon
1079 #ifdef UPCALL_TIMING
1083 #endif /* UPCALL_TIMING */
1085 mrt6stat.mrt6s_no_route++;
1087 if (V_mrt6debug & (DEBUG_FORWARD | DEBUG_MFC))
1088 log(LOG_DEBUG, "ip6_mforward: no rte s %s g %s\n",
1089 ip6_sprintf(ip6bufs, &ip6->ip6_src),
1090 ip6_sprintf(ip6bufd, &ip6->ip6_dst));
1094 * Allocate mbufs early so that we don't do extra work if we
1095 * are just going to fail anyway.
1097 rte = (struct rtdetq *)malloc(sizeof(*rte), M_MRTABLE6,
1103 mb0 = m_copy(m, 0, M_COPYALL);
1105 * Pullup packet header if needed before storing it,
1106 * as other references may modify it in the meantime.
1109 (M_HASCL(mb0) || mb0->m_len < sizeof(struct ip6_hdr)))
1110 mb0 = m_pullup(mb0, sizeof(struct ip6_hdr));
1112 free(rte, M_MRTABLE6);
1117 /* is there an upcall waiting for this packet? */
1118 hash = MF6CHASH(ip6->ip6_src, ip6->ip6_dst);
1119 for (rt = mf6ctable[hash]; rt; rt = rt->mf6c_next) {
1120 if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_src,
1121 &rt->mf6c_origin.sin6_addr) &&
1122 IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
1123 &rt->mf6c_mcastgrp.sin6_addr) &&
1124 (rt->mf6c_stall != NULL))
1131 struct omrt6msg *oim;
1134 /* no upcall, so make a new entry */
1135 rt = (struct mf6c *)malloc(sizeof(*rt), M_MRTABLE6,
1138 free(rte, M_MRTABLE6);
1144 * Make a copy of the header to send to the user
1147 mm = m_copy(mb0, 0, sizeof(struct ip6_hdr));
1150 free(rte, M_MRTABLE6);
1152 free(rt, M_MRTABLE6);
1158 * Send message to routing daemon
1160 sin6.sin6_addr = ip6->ip6_src;
1166 switch (V_ip6_mrouter_ver) {
1169 oim = mtod(mm, struct omrt6msg *);
1170 oim->im6_msgtype = MRT6MSG_NOCACHE;
1175 im = mtod(mm, struct mrt6msg *);
1176 im->im6_msgtype = MRT6MSG_NOCACHE;
1180 free(rte, M_MRTABLE6);
1182 free(rt, M_MRTABLE6);
1188 if (V_mrt6debug & DEBUG_FORWARD)
1190 "getting the iif info in the kernel\n");
1193 for (mifp = mif6table, mifi = 0;
1194 mifi < nummifs && mifp->m6_ifp != ifp;
1198 switch (V_ip6_mrouter_ver) {
1201 oim->im6_mif = mifi;
1209 if (socket_send(ip6_mrouter, mm, &sin6) < 0) {
1210 log(LOG_WARNING, "ip6_mforward: ip6_mrouter "
1211 "socket queue full\n");
1212 mrt6stat.mrt6s_upq_sockfull++;
1213 free(rte, M_MRTABLE6);
1215 free(rt, M_MRTABLE6);
1220 mrt6stat.mrt6s_upcalls++;
1222 /* insert new entry at head of hash chain */
1223 bzero(rt, sizeof(*rt));
1224 rt->mf6c_origin.sin6_family = AF_INET6;
1225 rt->mf6c_origin.sin6_len = sizeof(struct sockaddr_in6);
1226 rt->mf6c_origin.sin6_addr = ip6->ip6_src;
1227 rt->mf6c_mcastgrp.sin6_family = AF_INET6;
1228 rt->mf6c_mcastgrp.sin6_len = sizeof(struct sockaddr_in6);
1229 rt->mf6c_mcastgrp.sin6_addr = ip6->ip6_dst;
1230 rt->mf6c_expire = UPCALL_EXPIRE;
1232 rt->mf6c_parent = MF6C_INCOMPLETE_PARENT;
1234 /* link into table */
1235 rt->mf6c_next = mf6ctable[hash];
1236 mf6ctable[hash] = rt;
1237 /* Add this entry to the end of the queue */
1238 rt->mf6c_stall = rte;
1240 /* determine if q has overflowed */
1244 for (p = &rt->mf6c_stall; *p != NULL; p = &(*p)->next)
1245 if (++npkts > MAX_UPQ6) {
1246 mrt6stat.mrt6s_upq_ovflw++;
1247 free(rte, M_MRTABLE6);
1253 /* Add this entry to the end of the queue */
1260 #ifdef UPCALL_TIMING
1262 #endif /* UPCALL_TIMING */
1271 * Clean up cache entries if upcalls are not serviced
1272 * Call from the Slow Timeout mechanism, every half second.
1275 expire_upcalls(void *unused)
1278 struct mf6c *mfc, **nptr;
1283 for (i = 0; i < MF6CTBLSIZ; i++) {
1284 if (n6expire[i] == 0)
1286 nptr = &mf6ctable[i];
1287 while ((mfc = *nptr) != NULL) {
1288 rte = mfc->mf6c_stall;
1290 * Skip real cache entries
1291 * Make sure it wasn't marked to not expire (shouldn't happen)
1295 mfc->mf6c_expire != 0 &&
1296 --mfc->mf6c_expire == 0) {
1298 if (V_mrt6debug & DEBUG_EXPIRE) {
1299 char ip6bufo[INET6_ADDRSTRLEN];
1300 char ip6bufg[INET6_ADDRSTRLEN];
1301 log(LOG_DEBUG, "expire_upcalls: expiring (%s %s)\n",
1302 ip6_sprintf(ip6bufo, &mfc->mf6c_origin.sin6_addr),
1303 ip6_sprintf(ip6bufg, &mfc->mf6c_mcastgrp.sin6_addr));
1307 * drop all the packets
1308 * free the mbuf with the pkt, if, timing info
1311 struct rtdetq *n = rte->next;
1313 free(rte, M_MRTABLE6);
1315 } while (rte != NULL);
1316 mrt6stat.mrt6s_cache_cleanups++;
1319 *nptr = mfc->mf6c_next;
1320 free(mfc, M_MRTABLE6);
1322 nptr = &mfc->mf6c_next;
1327 callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT,
1328 expire_upcalls, NULL);
1332 * Packet forwarding routine once entry in the cache is made
1335 ip6_mdq(struct mbuf *m, struct ifnet *ifp, struct mf6c *rt)
1337 INIT_VNET_INET6(curvnet);
1338 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1341 int plen = m->m_pkthdr.len;
1342 struct in6_addr src0, dst0; /* copies for local work */
1343 u_int32_t iszone, idzone, oszone, odzone;
1347 * Macro to send packet on mif. Since RSVP packets don't get counted on
1348 * input, they shouldn't get counted on output, so statistics keeping is
1352 #define MC6_SEND(ip6, mifp, m) do { \
1353 if ((mifp)->m6_flags & MIFF_REGISTER) \
1354 register_send((ip6), (mifp), (m)); \
1356 phyint_send((ip6), (mifp), (m)); \
1357 } while (/*CONSTCOND*/ 0)
1360 * Don't forward if it didn't arrive from the parent mif
1363 mifi = rt->mf6c_parent;
1364 if ((mifi >= nummifs) || (mif6table[mifi].m6_ifp != ifp)) {
1365 /* came in the wrong interface */
1367 if (V_mrt6debug & DEBUG_FORWARD)
1369 "wrong if: ifid %d mifi %d mififid %x\n",
1370 ifp->if_index, mifi,
1371 mif6table[mifi].m6_ifp->if_index);
1373 mrt6stat.mrt6s_wrong_if++;
1374 rt->mf6c_wrong_if++;
1376 * If we are doing PIM processing, and we are forwarding
1377 * packets on this interface, send a message to the
1380 /* have to make sure this is a valid mif */
1381 if (mifi < nummifs && mif6table[mifi].m6_ifp)
1382 if (V_pim6 && (m->m_flags & M_LOOP) == 0) {
1384 * Check the M_LOOP flag to avoid an
1385 * unnecessary PIM assert.
1386 * XXX: M_LOOP is an ad-hoc hack...
1388 static struct sockaddr_in6 sin6 =
1389 { sizeof(sin6), AF_INET6 };
1394 struct omrt6msg *oim;
1397 mm = m_copy(m, 0, sizeof(struct ip6_hdr));
1400 mm->m_len < sizeof(struct ip6_hdr)))
1401 mm = m_pullup(mm, sizeof(struct ip6_hdr));
1409 switch (V_ip6_mrouter_ver) {
1412 oim = mtod(mm, struct omrt6msg *);
1413 oim->im6_msgtype = MRT6MSG_WRONGMIF;
1418 im = mtod(mm, struct mrt6msg *);
1419 im->im6_msgtype = MRT6MSG_WRONGMIF;
1427 for (mifp = mif6table, iif = 0;
1428 iif < nummifs && mifp &&
1429 mifp->m6_ifp != ifp;
1433 switch (V_ip6_mrouter_ver) {
1437 sin6.sin6_addr = oim->im6_src;
1442 sin6.sin6_addr = im->im6_src;
1446 mrt6stat.mrt6s_upcalls++;
1448 if (socket_send(ip6_mrouter, mm, &sin6) < 0) {
1451 log(LOG_WARNING, "mdq, ip6_mrouter socket queue full\n");
1453 ++mrt6stat.mrt6s_upq_sockfull;
1455 } /* if socket Q full */
1458 } /* if wrong iif */
1460 /* If I sourced this packet, it counts as output, else it was input. */
1461 if (m->m_pkthdr.rcvif == NULL) {
1462 /* XXX: is rcvif really NULL when output?? */
1463 mif6table[mifi].m6_pkt_out++;
1464 mif6table[mifi].m6_bytes_out += plen;
1466 mif6table[mifi].m6_pkt_in++;
1467 mif6table[mifi].m6_bytes_in += plen;
1470 rt->mf6c_byte_cnt += plen;
1473 * For each mif, forward a copy of the packet if there are group
1474 * members downstream on the interface.
1476 src0 = ip6->ip6_src;
1477 dst0 = ip6->ip6_dst;
1478 if ((error = in6_setscope(&src0, ifp, &iszone)) != 0 ||
1479 (error = in6_setscope(&dst0, ifp, &idzone)) != 0) {
1480 V_ip6stat.ip6s_badscope++;
1483 for (mifp = mif6table, mifi = 0; mifi < nummifs; mifp++, mifi++) {
1484 if (IF_ISSET(mifi, &rt->mf6c_ifset)) {
1486 * check if the outgoing packet is going to break
1488 * XXX For packets through PIM register tunnel
1489 * interface, we believe a routing daemon.
1491 if (!(mif6table[rt->mf6c_parent].m6_flags &
1493 !(mif6table[mifi].m6_flags & MIFF_REGISTER)) {
1494 if (in6_setscope(&src0, mif6table[mifi].m6_ifp,
1496 in6_setscope(&dst0, mif6table[mifi].m6_ifp,
1500 V_ip6stat.ip6s_badscope++;
1506 mifp->m6_bytes_out += plen;
1507 MC6_SEND(ip6, mifp, m);
1514 phyint_send(struct ip6_hdr *ip6, struct mif6 *mifp, struct mbuf *m)
1516 INIT_VNET_INET6(curvnet);
1517 struct mbuf *mb_copy;
1518 struct ifnet *ifp = mifp->m6_ifp;
1520 int s = splnet(); /* needs to protect static "ro" below. */
1521 static struct route_in6 ro;
1522 struct in6_multi *in6m;
1523 struct sockaddr_in6 *dst6;
1527 * Make a new reference to the packet; make sure that
1528 * the IPv6 header is actually copied, not just referenced,
1529 * so that ip6_output() only scribbles on the copy.
1531 mb_copy = m_copy(m, 0, M_COPYALL);
1533 (M_HASCL(mb_copy) || mb_copy->m_len < sizeof(struct ip6_hdr)))
1534 mb_copy = m_pullup(mb_copy, sizeof(struct ip6_hdr));
1535 if (mb_copy == NULL) {
1539 /* set MCAST flag to the outgoing packet */
1540 mb_copy->m_flags |= M_MCAST;
1543 * If we sourced the packet, call ip6_output since we may devide
1544 * the packet into fragments when the packet is too big for the
1545 * outgoing interface.
1546 * Otherwise, we can simply send the packet to the interface
1549 if (m->m_pkthdr.rcvif == NULL) {
1550 struct ip6_moptions im6o;
1552 im6o.im6o_multicast_ifp = ifp;
1553 /* XXX: ip6_output will override ip6->ip6_hlim */
1554 im6o.im6o_multicast_hlim = ip6->ip6_hlim;
1555 im6o.im6o_multicast_loop = 1;
1556 error = ip6_output(mb_copy, NULL, &ro,
1557 IPV6_FORWARDING, &im6o, NULL, NULL);
1560 if (V_mrt6debug & DEBUG_XMIT)
1561 log(LOG_DEBUG, "phyint_send on mif %d err %d\n",
1562 mifp - mif6table, error);
1569 * If we belong to the destination multicast group
1570 * on the outgoing interface, loop back a copy.
1572 dst6 = (struct sockaddr_in6 *)&ro.ro_dst;
1573 IN6_LOOKUP_MULTI(ip6->ip6_dst, ifp, in6m);
1575 dst6->sin6_len = sizeof(struct sockaddr_in6);
1576 dst6->sin6_family = AF_INET6;
1577 dst6->sin6_addr = ip6->ip6_dst;
1578 ip6_mloopback(ifp, m, (struct sockaddr_in6 *)&ro.ro_dst);
1581 * Put the packet into the sending queue of the outgoing interface
1582 * if it would fit in the MTU of the interface.
1584 linkmtu = IN6_LINKMTU(ifp);
1585 if (mb_copy->m_pkthdr.len <= linkmtu || linkmtu < IPV6_MMTU) {
1586 dst6->sin6_len = sizeof(struct sockaddr_in6);
1587 dst6->sin6_family = AF_INET6;
1588 dst6->sin6_addr = ip6->ip6_dst;
1590 * We just call if_output instead of nd6_output here, since
1591 * we need no ND for a multicast forwarded packet...right?
1593 error = (*ifp->if_output)(ifp, mb_copy,
1594 (struct sockaddr *)&ro.ro_dst, NULL);
1596 if (V_mrt6debug & DEBUG_XMIT)
1597 log(LOG_DEBUG, "phyint_send on mif %d err %d\n",
1598 mifp - mif6table, error);
1602 * pMTU discovery is intentionally disabled by default, since
1603 * various router may notify pMTU in multicast, which can be
1604 * a DDoS to a router
1606 if (V_ip6_mcast_pmtu)
1607 icmp6_error(mb_copy, ICMP6_PACKET_TOO_BIG, 0, linkmtu);
1610 if (V_mrt6debug & DEBUG_XMIT) {
1611 char ip6bufs[INET6_ADDRSTRLEN];
1612 char ip6bufd[INET6_ADDRSTRLEN];
1614 "phyint_send: packet too big on %s o %s "
1615 "g %s size %d(discarded)\n",
1617 ip6_sprintf(ip6bufs, &ip6->ip6_src),
1618 ip6_sprintf(ip6bufd, &ip6->ip6_dst),
1619 mb_copy->m_pkthdr.len);
1621 #endif /* MRT6DEBUG */
1622 m_freem(mb_copy); /* simply discard the packet */
1630 register_send(struct ip6_hdr *ip6, struct mif6 *mif, struct mbuf *m)
1633 int i, len = m->m_pkthdr.len;
1634 static struct sockaddr_in6 sin6 = { sizeof(sin6), AF_INET6 };
1635 struct mrt6msg *im6;
1639 char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
1640 log(LOG_DEBUG, "** IPv6 register_send **\n src %s dst %s\n",
1641 ip6_sprintf(ip6bufs, &ip6->ip6_src),
1642 ip6_sprintf(ip6bufd, &ip6->ip6_dst));
1645 ++pim6stat.pim6s_snd_registers;
1647 /* Make a copy of the packet to send to the user level process */
1648 MGETHDR(mm, M_DONTWAIT, MT_HEADER);
1651 mm->m_pkthdr.rcvif = NULL;
1652 mm->m_data += max_linkhdr;
1653 mm->m_len = sizeof(struct ip6_hdr);
1655 if ((mm->m_next = m_copy(m, 0, M_COPYALL)) == NULL) {
1659 i = MHLEN - M_LEADINGSPACE(mm);
1662 mm = m_pullup(mm, i);
1665 /* TODO: check it! */
1666 mm->m_pkthdr.len = len + sizeof(struct ip6_hdr);
1669 * Send message to routing daemon
1671 sin6.sin6_addr = ip6->ip6_src;
1673 im6 = mtod(mm, struct mrt6msg *);
1674 im6->im6_msgtype = MRT6MSG_WHOLEPKT;
1677 im6->im6_mif = mif - mif6table;
1679 /* iif info is not given for reg. encap.n */
1680 mrt6stat.mrt6s_upcalls++;
1682 if (socket_send(ip6_mrouter, mm, &sin6) < 0) {
1686 "register_send: ip6_mrouter socket queue full\n");
1688 ++mrt6stat.mrt6s_upq_sockfull;
1695 * PIM sparse mode hook
1696 * Receives the pim control messages, and passes them up to the listening
1697 * socket, using rip6_input.
1698 * The only message processed is the REGISTER pim message; the pim header
1699 * is stripped off, and the inner packet is passed to register_mforward.
1702 pim6_input(struct mbuf **mp, int *offp, int proto)
1704 INIT_VNET_INET6(curvnet);
1705 struct pim *pim; /* pointer to a pim struct */
1706 struct ip6_hdr *ip6;
1708 struct mbuf *m = *mp;
1712 ++pim6stat.pim6s_rcv_total;
1714 ip6 = mtod(m, struct ip6_hdr *);
1715 pimlen = m->m_pkthdr.len - *offp;
1720 if (pimlen < PIM_MINLEN) {
1721 ++pim6stat.pim6s_rcv_tooshort;
1723 if (V_mrt6debug & DEBUG_PIM)
1724 log(LOG_DEBUG,"pim6_input: PIM packet too short\n");
1727 return (IPPROTO_DONE);
1731 * if the packet is at least as big as a REGISTER, go ahead
1732 * and grab the PIM REGISTER header size, to avoid another
1733 * possible m_pullup() later.
1735 * PIM_MINLEN == pimhdr + u_int32 == 8
1736 * PIM6_REG_MINLEN == pimhdr + reghdr + eip6hdr == 4 + 4 + 40
1738 minlen = (pimlen >= PIM6_REG_MINLEN) ? PIM6_REG_MINLEN : PIM_MINLEN;
1741 * Make sure that the IP6 and PIM headers in contiguous memory, and
1742 * possibly the PIM REGISTER header
1744 #ifndef PULLDOWN_TEST
1745 IP6_EXTHDR_CHECK(m, off, minlen, IPPROTO_DONE);
1746 /* adjust pointer */
1747 ip6 = mtod(m, struct ip6_hdr *);
1749 /* adjust mbuf to point to the PIM header */
1750 pim = (struct pim *)((caddr_t)ip6 + off);
1752 IP6_EXTHDR_GET(pim, struct pim *, m, off, minlen);
1754 pim6stat.pim6s_rcv_tooshort++;
1755 return (IPPROTO_DONE);
1759 #define PIM6_CHECKSUM
1760 #ifdef PIM6_CHECKSUM
1765 * Validate checksum.
1766 * If PIM REGISTER, exclude the data packet
1768 if (pim->pim_type == PIM_REGISTER)
1769 cksumlen = PIM_MINLEN;
1773 if (in6_cksum(m, IPPROTO_PIM, off, cksumlen)) {
1774 ++pim6stat.pim6s_rcv_badsum;
1776 if (V_mrt6debug & DEBUG_PIM)
1778 "pim6_input: invalid checksum\n");
1781 return (IPPROTO_DONE);
1784 #endif /* PIM_CHECKSUM */
1786 /* PIM version check */
1787 if (pim->pim_ver != PIM_VERSION) {
1788 ++pim6stat.pim6s_rcv_badversion;
1791 "pim6_input: incorrect version %d, expecting %d\n",
1792 pim->pim_ver, PIM_VERSION);
1795 return (IPPROTO_DONE);
1798 if (pim->pim_type == PIM_REGISTER) {
1800 * since this is a REGISTER, we'll make a copy of the register
1801 * headers ip6+pim+u_int32_t+encap_ip6, to be passed up to the
1804 static struct sockaddr_in6 dst = { sizeof(dst), AF_INET6 };
1807 struct ip6_hdr *eip6;
1811 char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
1814 ++pim6stat.pim6s_rcv_registers;
1816 if ((reg_mif_num >= nummifs) || (reg_mif_num == (mifi_t) -1)) {
1818 if (V_mrt6debug & DEBUG_PIM)
1820 "pim6_input: register mif not set: %d\n",
1824 return (IPPROTO_DONE);
1827 reghdr = (u_int32_t *)(pim + 1);
1829 if ((ntohl(*reghdr) & PIM_NULL_REGISTER))
1830 goto pim6_input_to_daemon;
1835 if (pimlen < PIM6_REG_MINLEN) {
1836 ++pim6stat.pim6s_rcv_tooshort;
1837 ++pim6stat.pim6s_rcv_badregisters;
1840 "pim6_input: register packet size too "
1841 "small %d from %s\n",
1842 pimlen, ip6_sprintf(ip6bufs, &ip6->ip6_src));
1845 return (IPPROTO_DONE);
1848 eip6 = (struct ip6_hdr *) (reghdr + 1);
1850 if (V_mrt6debug & DEBUG_PIM)
1852 "pim6_input[register], eip6: %s -> %s, "
1854 ip6_sprintf(ip6bufs, &eip6->ip6_src),
1855 ip6_sprintf(ip6bufd, &eip6->ip6_dst),
1856 ntohs(eip6->ip6_plen));
1859 /* verify the version number of the inner packet */
1860 if ((eip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
1861 ++pim6stat.pim6s_rcv_badregisters;
1863 log(LOG_DEBUG, "pim6_input: invalid IP version (%d) "
1864 "of the inner packet\n",
1865 (eip6->ip6_vfc & IPV6_VERSION));
1868 return (IPPROTO_NONE);
1871 /* verify the inner packet is destined to a mcast group */
1872 if (!IN6_IS_ADDR_MULTICAST(&eip6->ip6_dst)) {
1873 ++pim6stat.pim6s_rcv_badregisters;
1875 if (V_mrt6debug & DEBUG_PIM)
1877 "pim6_input: inner packet of register "
1878 "is not multicast %s\n",
1879 ip6_sprintf(ip6bufd, &eip6->ip6_dst));
1882 return (IPPROTO_DONE);
1886 * make a copy of the whole header to pass to the daemon later.
1888 mcp = m_copy(m, 0, off + PIM6_REG_MINLEN);
1892 "pim6_input: pim register: "
1893 "could not copy register head\n");
1896 return (IPPROTO_DONE);
1900 * forward the inner ip6 packet; point m_data at the inner ip6.
1902 m_adj(m, off + PIM_MINLEN);
1904 if (V_mrt6debug & DEBUG_PIM) {
1906 "pim6_input: forwarding decapsulated register: "
1907 "src %s, dst %s, mif %d\n",
1908 ip6_sprintf(ip6bufs, &eip6->ip6_src),
1909 ip6_sprintf(ip6bufd, &eip6->ip6_dst),
1914 rc = if_simloop(mif6table[reg_mif_num].m6_ifp, m,
1915 dst.sin6_family, 0);
1917 /* prepare the register head to send to the mrouting daemon */
1922 * Pass the PIM message up to the daemon; if it is a register message
1923 * pass the 'head' only up to the daemon. This includes the
1924 * encapsulator ip6 header, pim header, register header and the
1925 * encapsulated ip6 header.
1927 pim6_input_to_daemon:
1928 rip6_input(&m, offp, proto);
1929 return (IPPROTO_DONE);