1 /* $FreeBSD: src/sys/netinet6/ip6_mroute.c,v 1.2.2.9 2003/01/23 21:06:47 sam Exp $ */
2 /* $DragonFly: src/sys/netinet6/ip6_mroute.c,v 1.6 2004/03/23 22:19:08 hsu Exp $ */
3 /* $KAME: ip6_mroute.c,v 1.58 2001/12/18 02:36:31 itojun Exp $ */
6 * Copyright (C) 1998 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 /* BSDI ip_mroute.c,v 2.10 1996/11/14 00:29:52 jch Exp */
37 * IP multicast forwarding procedures
39 * Written by David Waitzman, BBN Labs, August 1988.
40 * Modified by Steve Deering, Stanford, February 1989.
41 * Modified by Mark J. Steiglitz, Stanford, May, 1991
42 * Modified by Van Jacobson, LBL, January 1993
43 * Modified by Ajit Thyagarajan, PARC, August 1993
44 * Modified by Bill Fenenr, PARC, April 1994
46 * MROUTING Revision: 3.5.1.2 + PIM-SMv2 (pimd) Support
50 #include "opt_inet6.h"
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/callout.h>
55 #include <sys/malloc.h>
57 #include <sys/socket.h>
58 #include <sys/socketvar.h>
59 #include <sys/sockio.h>
60 #include <sys/protosw.h>
61 #include <sys/errno.h>
63 #include <sys/kernel.h>
64 #include <sys/syslog.h>
67 #include <net/route.h>
68 #include <net/raw_cb.h>
70 #include <netinet/in.h>
71 #include <netinet/in_var.h>
73 #include <netinet/ip6.h>
74 #include <netinet6/ip6_var.h>
75 #include <netinet6/ip6_mroute.h>
76 #include <netinet6/pim6.h>
77 #include <netinet6/pim6_var.h>
79 #include <net/net_osdep.h>
81 static MALLOC_DEFINE(M_MRTABLE, "mf6c", "multicast forwarding cache entry");
83 #define M_HASCL(m) ((m)->m_flags & M_EXT)
85 static int ip6_mdq (struct mbuf *, struct ifnet *, struct mf6c *);
86 static void phyint_send (struct ip6_hdr *, struct mif6 *, struct mbuf *);
88 static int set_pim6 (int *);
89 static int socket_send (struct socket *, struct mbuf *,
90 struct sockaddr_in6 *);
91 static int register_send (struct ip6_hdr *, struct mif6 *,
95 * Globals. All but ip6_mrouter, ip6_mrtproto and mrt6stat could be static,
96 * except for netstat or debugging purposes.
98 struct socket *ip6_mrouter = NULL;
99 int ip6_mrouter_ver = 0;
100 int ip6_mrtproto = IPPROTO_PIM; /* for netstat only */
101 struct mrt6stat mrt6stat;
103 #define NO_RTE_FOUND 0x1
104 #define RTE_FOUND 0x2
106 struct mf6c *mf6ctable[MF6CTBLSIZ];
107 u_char n6expire[MF6CTBLSIZ];
108 static struct mif6 mif6table[MAXMIFS];
110 u_int mrt6debug = 0; /* debug level */
111 #define DEBUG_MFC 0x02
112 #define DEBUG_FORWARD 0x04
113 #define DEBUG_EXPIRE 0x08
114 #define DEBUG_XMIT 0x10
115 #define DEBUG_REG 0x20
116 #define DEBUG_PIM 0x40
119 static void expire_upcalls (void *);
120 #define EXPIRE_TIMEOUT (hz / 4) /* 4x / second */
121 #define UPCALL_EXPIRE 6 /* number of timeouts */
125 extern struct socket *ip_mrouter;
130 * 'Interfaces' associated with decapsulator (so we can tell
131 * packets that went through it from ones that get reflected
132 * by a broken gateway). These interfaces are never linked into
133 * the system ifnet list & no routes point to them. I.e., packets
134 * can't be sent this way. They only exist as a placeholder for
135 * multicast source verification.
137 struct ifnet multicast_register_if;
139 #define ENCAP_HOPS 64
144 static mifi_t nummifs = 0;
145 static mifi_t reg_mif_num = (mifi_t)-1;
147 static struct pim6stat pim6stat;
151 * Hash function for a source, group entry
153 #define MF6CHASH(a, g) MF6CHASHMOD((a).s6_addr32[0] ^ (a).s6_addr32[1] ^ \
154 (a).s6_addr32[2] ^ (a).s6_addr32[3] ^ \
155 (g).s6_addr32[0] ^ (g).s6_addr32[1] ^ \
156 (g).s6_addr32[2] ^ (g).s6_addr32[3])
159 * Find a route for a given origin IPv6 address and Multicast group address.
160 * Quality of service parameter to be added in the future!!!
163 #define MF6CFIND(o, g, rt) do { \
164 struct mf6c *_rt = mf6ctable[MF6CHASH(o,g)]; \
166 mrt6stat.mrt6s_mfc_lookups++; \
168 if (IN6_ARE_ADDR_EQUAL(&_rt->mf6c_origin.sin6_addr, &(o)) && \
169 IN6_ARE_ADDR_EQUAL(&_rt->mf6c_mcastgrp.sin6_addr, &(g)) && \
170 (_rt->mf6c_stall == NULL)) { \
174 _rt = _rt->mf6c_next; \
177 mrt6stat.mrt6s_mfc_misses++; \
182 * Macros to compute elapsed time efficiently
183 * Borrowed from Van Jacobson's scheduling code
185 #define TV_DELTA(a, b, delta) do { \
188 delta = (a).tv_usec - (b).tv_usec; \
189 if ((xxs = (a).tv_sec - (b).tv_sec)) { \
198 delta += (1000000 * xxs); \
203 #define TV_LT(a, b) (((a).tv_usec < (b).tv_usec && \
204 (a).tv_sec <= (b).tv_sec) || (a).tv_sec < (b).tv_sec)
207 #define UPCALL_MAX 50
208 u_long upcall_data[UPCALL_MAX + 1];
209 static void collate();
210 #endif /* UPCALL_TIMING */
212 static int get_sg_cnt (struct sioc_sg_req6 *);
213 static int get_mif6_cnt (struct sioc_mif_req6 *);
214 static int ip6_mrouter_init (struct socket *, struct mbuf *, int);
215 static int add_m6if (struct mif6ctl *);
216 static int del_m6if (mifi_t *);
217 static int add_m6fc (struct mf6cctl *);
218 static int del_m6fc (struct mf6cctl *);
220 static struct callout expire_upcalls_ch;
223 * Handle MRT setsockopt commands to modify the multicast routing tables.
226 ip6_mrouter_set(so, sopt)
228 struct sockopt *sopt;
233 if (so != ip6_mrouter && sopt->sopt_name != MRT6_INIT)
236 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
238 if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
241 switch (sopt->sopt_name) {
246 error = ip6_mrouter_init(so, m, sopt->sopt_name);
249 error = ip6_mrouter_done();
252 error = add_m6if(mtod(m, struct mif6ctl *));
255 error = del_m6if(mtod(m, mifi_t *));
258 error = add_m6fc(mtod(m, struct mf6cctl *));
261 error = del_m6fc(mtod(m, struct mf6cctl *));
264 error = set_pim6(mtod(m, int *));
276 * Handle MRT getsockopt commands
279 ip6_mrouter_get(so, sopt)
281 struct sockopt *sopt;
285 if (so != ip6_mrouter) return EACCES;
287 switch (sopt->sopt_name) {
289 error = sooptcopyout(sopt, &pim6, sizeof(pim6));
296 * Handle ioctl commands to obtain information from the cache
299 mrt6_ioctl(cmd, data)
306 case SIOCGETSGCNT_IN6:
307 return(get_sg_cnt((struct sioc_sg_req6 *)data));
308 break; /* for safety */
309 case SIOCGETMIFCNT_IN6:
310 return(get_mif6_cnt((struct sioc_mif_req6 *)data));
311 break; /* for safety */
320 * returns the packet, byte, rpf-failure count for the source group provided
324 struct sioc_sg_req6 *req;
330 MF6CFIND(req->src.sin6_addr, req->grp.sin6_addr, rt);
333 req->pktcnt = rt->mf6c_pkt_cnt;
334 req->bytecnt = rt->mf6c_byte_cnt;
335 req->wrong_if = rt->mf6c_wrong_if;
339 req->pktcnt = req->bytecnt = req->wrong_if = 0xffffffff;
346 * returns the input and output packet and byte counts on the mif provided
350 struct sioc_mif_req6 *req;
352 mifi_t mifi = req->mifi;
357 req->icount = mif6table[mifi].m6_pkt_in;
358 req->ocount = mif6table[mifi].m6_pkt_out;
359 req->ibytes = mif6table[mifi].m6_bytes_in;
360 req->obytes = mif6table[mifi].m6_bytes_out;
369 if ((*i != 1) && (*i != 0))
378 * Enable multicast routing
381 ip6_mrouter_init(so, m, cmd)
391 "ip6_mrouter_init: so_type = %d, pr_protocol = %d\n",
392 so->so_type, so->so_proto->pr_protocol);
395 if (so->so_type != SOCK_RAW ||
396 so->so_proto->pr_protocol != IPPROTO_ICMPV6)
399 if (!m || (m->m_len != sizeof(int *)))
406 if (ip6_mrouter != NULL) return EADDRINUSE;
409 ip6_mrouter_ver = cmd;
411 bzero((caddr_t)mf6ctable, sizeof(mf6ctable));
412 bzero((caddr_t)n6expire, sizeof(n6expire));
414 pim6 = 0;/* used for stubbing out/in pim stuff */
416 callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT,
417 expire_upcalls, NULL);
421 log(LOG_DEBUG, "ip6_mrouter_init\n");
428 * Disable multicast routing
436 struct in6_ifreq ifr;
444 * For each phyint in use, disable promiscuous reception of all IPv6
450 * If there is still IPv4 multicast routing daemon,
451 * we remain interfaces to receive all muliticasted packets.
452 * XXX: there may be an interface in which the IPv4 multicast
453 * daemon is not interested...
459 for (mifi = 0; mifi < nummifs; mifi++) {
460 if (mif6table[mifi].m6_ifp &&
461 !(mif6table[mifi].m6_flags & MIFF_REGISTER)) {
462 ifr.ifr_addr.sin6_family = AF_INET6;
463 ifr.ifr_addr.sin6_addr= in6addr_any;
464 ifp = mif6table[mifi].m6_ifp;
465 (*ifp->if_ioctl)(ifp, SIOCDELMULTI,
467 (struct ucred *)NULL);
472 bzero((caddr_t)qtable, sizeof(qtable));
473 bzero((caddr_t)tbftable, sizeof(tbftable));
475 bzero((caddr_t)mif6table, sizeof(mif6table));
478 pim6 = 0; /* used to stub out/in pim specific code */
480 callout_stop(&expire_upcalls_ch);
483 * Free all multicast forwarding cache entries.
485 for (i = 0; i < MF6CTBLSIZ; i++) {
490 for (rte = rt->mf6c_stall; rte != NULL; ) {
491 struct rtdetq *n = rte->next;
494 free(rte, M_MRTABLE);
499 free(frt, M_MRTABLE);
503 bzero((caddr_t)mf6ctable, sizeof(mf6ctable));
506 * Reset de-encapsulation cache
517 log(LOG_DEBUG, "ip6_mrouter_done\n");
523 static struct sockaddr_in6 sin6 = { sizeof(sin6), AF_INET6 };
526 * Add a mif to the mif table
530 struct mif6ctl *mifcp;
536 struct tbf *m_tbf = tbftable + mifcp->mif6c_mifi;
539 if (mifcp->mif6c_mifi >= MAXMIFS)
541 mifp = mif6table + mifcp->mif6c_mifi;
543 return EADDRINUSE; /* XXX: is it appropriate? */
544 if (mifcp->mif6c_pifi == 0 || mifcp->mif6c_pifi > if_index)
546 ifp = ifindex2ifnet[mifcp->mif6c_pifi];
548 if (mifcp->mif6c_flags & MIFF_REGISTER) {
549 if (reg_mif_num == (mifi_t)-1) {
550 strlcpy(multicast_register_if.if_xname, "register_mif",
552 multicast_register_if.if_flags |= IFF_LOOPBACK;
553 multicast_register_if.if_index = mifcp->mif6c_mifi;
554 reg_mif_num = mifcp->mif6c_mifi;
557 ifp = &multicast_register_if;
561 /* Make sure the interface supports multicast */
562 if ((ifp->if_flags & IFF_MULTICAST) == 0)
566 error = if_allmulti(ifp, 1);
573 mifp->m6_flags = mifcp->mif6c_flags;
576 /* scaling up here allows division by 1024 in critical code */
577 mifp->m6_rate_limit = mifcp->mif6c_rate_limit * 1024 / 1000;
579 /* initialize per mif pkt counters */
581 mifp->m6_pkt_out = 0;
582 mifp->m6_bytes_in = 0;
583 mifp->m6_bytes_out = 0;
586 /* Adjust nummifs up if the mifi is higher than nummifs */
587 if (nummifs <= mifcp->mif6c_mifi)
588 nummifs = mifcp->mif6c_mifi + 1;
593 "add_mif #%d, phyint %s\n",
602 * Delete a mif from the mif table
608 struct mif6 *mifp = mif6table + *mifip;
613 if (*mifip >= nummifs)
615 if (mifp->m6_ifp == NULL)
620 if (!(mifp->m6_flags & MIFF_REGISTER)) {
622 * XXX: what if there is yet IPv4 multicast daemon
623 * using the interface?
631 bzero((caddr_t)qtable[*mifip], sizeof(qtable[*mifip]));
632 bzero((caddr_t)mifp->m6_tbf, sizeof(*(mifp->m6_tbf)));
634 bzero((caddr_t)mifp, sizeof (*mifp));
636 /* Adjust nummifs down */
637 for (mifi = nummifs; mifi > 0; mifi--)
638 if (mif6table[mifi - 1].m6_ifp)
646 log(LOG_DEBUG, "del_m6if %d, nummifs %d\n", *mifip, nummifs);
657 struct mf6cctl *mfccp;
665 MF6CFIND(mfccp->mf6cc_origin.sin6_addr,
666 mfccp->mf6cc_mcastgrp.sin6_addr, rt);
668 /* If an entry already exists, just update the fields */
671 if (mrt6debug & DEBUG_MFC)
673 "add_m6fc no upcall h %d o %s g %s p %x\n",
674 ip6_sprintf(&mfccp->mf6cc_origin.sin6_addr),
675 ip6_sprintf(&mfccp->mf6cc_mcastgrp.sin6_addr),
676 mfccp->mf6cc_parent);
680 rt->mf6c_parent = mfccp->mf6cc_parent;
681 rt->mf6c_ifset = mfccp->mf6cc_ifset;
687 * Find the entry for which the upcall was made and update
690 hash = MF6CHASH(mfccp->mf6cc_origin.sin6_addr,
691 mfccp->mf6cc_mcastgrp.sin6_addr);
692 for (rt = mf6ctable[hash], nstl = 0; rt; rt = rt->mf6c_next) {
693 if (IN6_ARE_ADDR_EQUAL(&rt->mf6c_origin.sin6_addr,
694 &mfccp->mf6cc_origin.sin6_addr) &&
695 IN6_ARE_ADDR_EQUAL(&rt->mf6c_mcastgrp.sin6_addr,
696 &mfccp->mf6cc_mcastgrp.sin6_addr) &&
697 (rt->mf6c_stall != NULL)) {
701 "add_m6fc: %s o %s g %s p %x dbx %p\n",
702 "multiple kernel entries",
703 ip6_sprintf(&mfccp->mf6cc_origin.sin6_addr),
704 ip6_sprintf(&mfccp->mf6cc_mcastgrp.sin6_addr),
705 mfccp->mf6cc_parent, rt->mf6c_stall);
708 if (mrt6debug & DEBUG_MFC)
710 "add_m6fc o %s g %s p %x dbg %x\n",
711 ip6_sprintf(&mfccp->mf6cc_origin.sin6_addr),
712 ip6_sprintf(&mfccp->mf6cc_mcastgrp.sin6_addr),
713 mfccp->mf6cc_parent, rt->mf6c_stall);
716 rt->mf6c_origin = mfccp->mf6cc_origin;
717 rt->mf6c_mcastgrp = mfccp->mf6cc_mcastgrp;
718 rt->mf6c_parent = mfccp->mf6cc_parent;
719 rt->mf6c_ifset = mfccp->mf6cc_ifset;
720 /* initialize pkt counters per src-grp */
721 rt->mf6c_pkt_cnt = 0;
722 rt->mf6c_byte_cnt = 0;
723 rt->mf6c_wrong_if = 0;
725 rt->mf6c_expire = 0; /* Don't clean this guy up */
728 /* free packets Qed at the end of this entry */
729 for (rte = rt->mf6c_stall; rte != NULL; ) {
730 struct rtdetq *n = rte->next;
731 ip6_mdq(rte->m, rte->ifp, rt);
735 #endif /* UPCALL_TIMING */
736 free(rte, M_MRTABLE);
739 rt->mf6c_stall = NULL;
744 * It is possible that an entry is being inserted without an upcall
748 if (mrt6debug & DEBUG_MFC)
749 log(LOG_DEBUG,"add_mfc no upcall h %d o %s g %s p %x\n",
751 ip6_sprintf(&mfccp->mf6cc_origin.sin6_addr),
752 ip6_sprintf(&mfccp->mf6cc_mcastgrp.sin6_addr),
753 mfccp->mf6cc_parent);
756 for (rt = mf6ctable[hash]; rt; rt = rt->mf6c_next) {
758 if (IN6_ARE_ADDR_EQUAL(&rt->mf6c_origin.sin6_addr,
759 &mfccp->mf6cc_origin.sin6_addr)&&
760 IN6_ARE_ADDR_EQUAL(&rt->mf6c_mcastgrp.sin6_addr,
761 &mfccp->mf6cc_mcastgrp.sin6_addr)) {
763 rt->mf6c_origin = mfccp->mf6cc_origin;
764 rt->mf6c_mcastgrp = mfccp->mf6cc_mcastgrp;
765 rt->mf6c_parent = mfccp->mf6cc_parent;
766 rt->mf6c_ifset = mfccp->mf6cc_ifset;
767 /* initialize pkt counters per src-grp */
768 rt->mf6c_pkt_cnt = 0;
769 rt->mf6c_byte_cnt = 0;
770 rt->mf6c_wrong_if = 0;
778 /* no upcall, so make a new entry */
779 rt = (struct mf6c *)malloc(sizeof(*rt), M_MRTABLE,
786 /* insert new entry at head of hash chain */
787 rt->mf6c_origin = mfccp->mf6cc_origin;
788 rt->mf6c_mcastgrp = mfccp->mf6cc_mcastgrp;
789 rt->mf6c_parent = mfccp->mf6cc_parent;
790 rt->mf6c_ifset = mfccp->mf6cc_ifset;
791 /* initialize pkt counters per src-grp */
792 rt->mf6c_pkt_cnt = 0;
793 rt->mf6c_byte_cnt = 0;
794 rt->mf6c_wrong_if = 0;
796 rt->mf6c_stall = NULL;
798 /* link into table */
799 rt->mf6c_next = mf6ctable[hash];
800 mf6ctable[hash] = rt;
809 * collect delay statistics on the upcalls
823 TV_DELTA(tp, *t, delta);
832 #endif /* UPCALL_TIMING */
835 * Delete an mfc entry
839 struct mf6cctl *mfccp;
841 struct sockaddr_in6 origin;
842 struct sockaddr_in6 mcastgrp;
848 origin = mfccp->mf6cc_origin;
849 mcastgrp = mfccp->mf6cc_mcastgrp;
850 hash = MF6CHASH(origin.sin6_addr, mcastgrp.sin6_addr);
853 if (mrt6debug & DEBUG_MFC)
854 log(LOG_DEBUG,"del_m6fc orig %s mcastgrp %s\n",
855 ip6_sprintf(&origin.sin6_addr),
856 ip6_sprintf(&mcastgrp.sin6_addr));
861 nptr = &mf6ctable[hash];
862 while ((rt = *nptr) != NULL) {
863 if (IN6_ARE_ADDR_EQUAL(&origin.sin6_addr,
864 &rt->mf6c_origin.sin6_addr) &&
865 IN6_ARE_ADDR_EQUAL(&mcastgrp.sin6_addr,
866 &rt->mf6c_mcastgrp.sin6_addr) &&
867 rt->mf6c_stall == NULL)
870 nptr = &rt->mf6c_next;
874 return EADDRNOTAVAIL;
877 *nptr = rt->mf6c_next;
886 socket_send(s, mm, src)
889 struct sockaddr_in6 *src;
892 if (sbappendaddr(&s->so_rcv,
893 (struct sockaddr *)src,
894 mm, (struct mbuf *)0) != 0) {
904 * IPv6 multicast forwarding function. This function assumes that the packet
905 * pointed to by "ip6" has arrived on (or is about to be sent to) the interface
906 * pointed to by "ifp", and the packet is to be relayed to other networks
907 * that have members of the packet's destination IPv6 multicast group.
909 * The packet is returned unscathed to the caller, unless it is
910 * erroneous, in which case a non-zero return value tells the caller to
915 ip6_mforward(ip6, ifp, m)
927 if (mrt6debug & DEBUG_FORWARD)
928 log(LOG_DEBUG, "ip6_mforward: src %s, dst %s, ifindex %d\n",
929 ip6_sprintf(&ip6->ip6_src), ip6_sprintf(&ip6->ip6_dst),
934 * Don't forward a packet with Hop limit of zero or one,
935 * or a packet destined to a local-only group.
937 if (ip6->ip6_hlim <= 1 || IN6_IS_ADDR_MC_NODELOCAL(&ip6->ip6_dst) ||
938 IN6_IS_ADDR_MC_LINKLOCAL(&ip6->ip6_dst))
943 * Source address check: do not forward packets with unspecified
944 * source. It was discussed in July 2000, on ipngwg mailing list.
945 * This is rather more serious than unicast cases, because some
946 * MLD packets can be sent with the unspecified source address
947 * (although such packets must normally set 1 to the hop limit field).
949 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
950 ip6stat.ip6s_cantforward++;
951 if (ip6_log_time + ip6_log_interval < time_second) {
952 ip6_log_time = time_second;
955 "from %s to %s nxt %d received on %s\n",
956 ip6_sprintf(&ip6->ip6_src),
957 ip6_sprintf(&ip6->ip6_dst),
959 if_name(m->m_pkthdr.rcvif));
965 * Determine forwarding mifs from the forwarding cache table
968 MF6CFIND(ip6->ip6_src, ip6->ip6_dst, rt);
970 /* Entry exists, so forward if necessary */
973 return (ip6_mdq(m, ifp, rt));
976 * If we don't have a route for packet's origin,
977 * Make a copy of the packet &
978 * send message to routing daemon
989 #endif /* UPCALL_TIMING */
991 mrt6stat.mrt6s_no_route++;
993 if (mrt6debug & (DEBUG_FORWARD | DEBUG_MFC))
994 log(LOG_DEBUG, "ip6_mforward: no rte s %s g %s\n",
995 ip6_sprintf(&ip6->ip6_src),
996 ip6_sprintf(&ip6->ip6_dst));
1000 * Allocate mbufs early so that we don't do extra work if we
1001 * are just going to fail anyway.
1003 rte = (struct rtdetq *)malloc(sizeof(*rte), M_MRTABLE,
1009 mb0 = m_copy(m, 0, M_COPYALL);
1011 * Pullup packet header if needed before storing it,
1012 * as other references may modify it in the meantime.
1015 (M_HASCL(mb0) || mb0->m_len < sizeof(struct ip6_hdr)))
1016 mb0 = m_pullup(mb0, sizeof(struct ip6_hdr));
1018 free(rte, M_MRTABLE);
1023 /* is there an upcall waiting for this packet? */
1024 hash = MF6CHASH(ip6->ip6_src, ip6->ip6_dst);
1025 for (rt = mf6ctable[hash]; rt; rt = rt->mf6c_next) {
1026 if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_src,
1027 &rt->mf6c_origin.sin6_addr) &&
1028 IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
1029 &rt->mf6c_mcastgrp.sin6_addr) &&
1030 (rt->mf6c_stall != NULL))
1037 struct omrt6msg *oim;
1040 /* no upcall, so make a new entry */
1041 rt = (struct mf6c *)malloc(sizeof(*rt), M_MRTABLE,
1044 free(rte, M_MRTABLE);
1050 * Make a copy of the header to send to the user
1053 mm = m_copy(mb0, 0, sizeof(struct ip6_hdr));
1056 free(rte, M_MRTABLE);
1058 free(rt, M_MRTABLE);
1064 * Send message to routing daemon
1066 sin6.sin6_addr = ip6->ip6_src;
1072 switch (ip6_mrouter_ver) {
1075 oim = mtod(mm, struct omrt6msg *);
1076 oim->im6_msgtype = MRT6MSG_NOCACHE;
1081 im = mtod(mm, struct mrt6msg *);
1082 im->im6_msgtype = MRT6MSG_NOCACHE;
1086 free(rte, M_MRTABLE);
1088 free(rt, M_MRTABLE);
1094 if (mrt6debug & DEBUG_FORWARD)
1096 "getting the iif info in the kernel\n");
1099 for (mifp = mif6table, mifi = 0;
1100 mifi < nummifs && mifp->m6_ifp != ifp;
1104 switch (ip6_mrouter_ver) {
1107 oim->im6_mif = mifi;
1115 if (socket_send(ip6_mrouter, mm, &sin6) < 0) {
1116 log(LOG_WARNING, "ip6_mforward: ip6_mrouter "
1117 "socket queue full\n");
1118 mrt6stat.mrt6s_upq_sockfull++;
1119 free(rte, M_MRTABLE);
1121 free(rt, M_MRTABLE);
1126 mrt6stat.mrt6s_upcalls++;
1128 /* insert new entry at head of hash chain */
1129 bzero(rt, sizeof(*rt));
1130 rt->mf6c_origin.sin6_family = AF_INET6;
1131 rt->mf6c_origin.sin6_len = sizeof(struct sockaddr_in6);
1132 rt->mf6c_origin.sin6_addr = ip6->ip6_src;
1133 rt->mf6c_mcastgrp.sin6_family = AF_INET6;
1134 rt->mf6c_mcastgrp.sin6_len = sizeof(struct sockaddr_in6);
1135 rt->mf6c_mcastgrp.sin6_addr = ip6->ip6_dst;
1136 rt->mf6c_expire = UPCALL_EXPIRE;
1138 rt->mf6c_parent = MF6C_INCOMPLETE_PARENT;
1140 /* link into table */
1141 rt->mf6c_next = mf6ctable[hash];
1142 mf6ctable[hash] = rt;
1143 /* Add this entry to the end of the queue */
1144 rt->mf6c_stall = rte;
1146 /* determine if q has overflowed */
1150 for (p = &rt->mf6c_stall; *p != NULL; p = &(*p)->next)
1151 if (++npkts > MAX_UPQ6) {
1152 mrt6stat.mrt6s_upq_ovflw++;
1153 free(rte, M_MRTABLE);
1159 /* Add this entry to the end of the queue */
1166 #ifdef UPCALL_TIMING
1168 #endif /* UPCALL_TIMING */
1177 * Clean up cache entries if upcalls are not serviced
1178 * Call from the Slow Timeout mechanism, every half second.
1181 expire_upcalls(unused)
1185 struct mf6c *mfc, **nptr;
1190 for (i = 0; i < MF6CTBLSIZ; i++) {
1191 if (n6expire[i] == 0)
1193 nptr = &mf6ctable[i];
1194 while ((mfc = *nptr) != NULL) {
1195 rte = mfc->mf6c_stall;
1197 * Skip real cache entries
1198 * Make sure it wasn't marked to not expire (shouldn't happen)
1202 mfc->mf6c_expire != 0 &&
1203 --mfc->mf6c_expire == 0) {
1205 if (mrt6debug & DEBUG_EXPIRE)
1206 log(LOG_DEBUG, "expire_upcalls: expiring (%s %s)\n",
1207 ip6_sprintf(&mfc->mf6c_origin.sin6_addr),
1208 ip6_sprintf(&mfc->mf6c_mcastgrp.sin6_addr));
1211 * drop all the packets
1212 * free the mbuf with the pkt, if, timing info
1215 struct rtdetq *n = rte->next;
1217 free(rte, M_MRTABLE);
1219 } while (rte != NULL);
1220 mrt6stat.mrt6s_cache_cleanups++;
1223 *nptr = mfc->mf6c_next;
1224 free(mfc, M_MRTABLE);
1226 nptr = &mfc->mf6c_next;
1231 callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT,
1232 expire_upcalls, NULL);
1236 * Packet forwarding routine once entry in the cache is made
1244 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1247 int plen = m->m_pkthdr.len;
1250 * Macro to send packet on mif. Since RSVP packets don't get counted on
1251 * input, they shouldn't get counted on output, so statistics keeping is
1255 #define MC6_SEND(ip6, mifp, m) do { \
1256 if ((mifp)->m6_flags & MIFF_REGISTER) \
1257 register_send((ip6), (mifp), (m)); \
1259 phyint_send((ip6), (mifp), (m)); \
1263 * Don't forward if it didn't arrive from the parent mif
1266 mifi = rt->mf6c_parent;
1267 if ((mifi >= nummifs) || (mif6table[mifi].m6_ifp != ifp)) {
1268 /* came in the wrong interface */
1270 if (mrt6debug & DEBUG_FORWARD)
1272 "wrong if: ifid %d mifi %d mififid %x\n",
1273 ifp->if_index, mifi,
1274 mif6table[mifi].m6_ifp->if_index);
1276 mrt6stat.mrt6s_wrong_if++;
1277 rt->mf6c_wrong_if++;
1279 * If we are doing PIM processing, and we are forwarding
1280 * packets on this interface, send a message to the
1283 /* have to make sure this is a valid mif */
1284 if (mifi < nummifs && mif6table[mifi].m6_ifp)
1285 if (pim6 && (m->m_flags & M_LOOP) == 0) {
1287 * Check the M_LOOP flag to avoid an
1288 * unnecessary PIM assert.
1289 * XXX: M_LOOP is an ad-hoc hack...
1291 static struct sockaddr_in6 sin6 =
1292 { sizeof(sin6), AF_INET6 };
1297 struct omrt6msg *oim;
1300 mm = m_copy(m, 0, sizeof(struct ip6_hdr));
1303 mm->m_len < sizeof(struct ip6_hdr)))
1304 mm = m_pullup(mm, sizeof(struct ip6_hdr));
1312 switch (ip6_mrouter_ver) {
1315 oim = mtod(mm, struct omrt6msg *);
1316 oim->im6_msgtype = MRT6MSG_WRONGMIF;
1321 im = mtod(mm, struct mrt6msg *);
1322 im->im6_msgtype = MRT6MSG_WRONGMIF;
1330 for (mifp = mif6table, iif = 0;
1331 iif < nummifs && mifp &&
1332 mifp->m6_ifp != ifp;
1336 switch (ip6_mrouter_ver) {
1340 sin6.sin6_addr = oim->im6_src;
1345 sin6.sin6_addr = im->im6_src;
1349 mrt6stat.mrt6s_upcalls++;
1351 if (socket_send(ip6_mrouter, mm, &sin6) < 0) {
1354 log(LOG_WARNING, "mdq, ip6_mrouter socket queue full\n");
1356 ++mrt6stat.mrt6s_upq_sockfull;
1358 } /* if socket Q full */
1361 } /* if wrong iif */
1363 /* If I sourced this packet, it counts as output, else it was input. */
1364 if (m->m_pkthdr.rcvif == NULL) {
1365 /* XXX: is rcvif really NULL when output?? */
1366 mif6table[mifi].m6_pkt_out++;
1367 mif6table[mifi].m6_bytes_out += plen;
1369 mif6table[mifi].m6_pkt_in++;
1370 mif6table[mifi].m6_bytes_in += plen;
1373 rt->mf6c_byte_cnt += plen;
1376 * For each mif, forward a copy of the packet if there are group
1377 * members downstream on the interface.
1379 for (mifp = mif6table, mifi = 0; mifi < nummifs; mifp++, mifi++)
1380 if (IF_ISSET(mifi, &rt->mf6c_ifset)) {
1382 * check if the outgoing packet is going to break
1384 * XXX For packets through PIM register tunnel
1385 * interface, we believe a routing daemon.
1387 if ((mif6table[rt->mf6c_parent].m6_flags &
1388 MIFF_REGISTER) == 0 &&
1389 (mif6table[mifi].m6_flags & MIFF_REGISTER) == 0 &&
1390 (in6_addr2scopeid(ifp, &ip6->ip6_dst) !=
1391 in6_addr2scopeid(mif6table[mifi].m6_ifp,
1393 in6_addr2scopeid(ifp, &ip6->ip6_src) !=
1394 in6_addr2scopeid(mif6table[mifi].m6_ifp,
1396 ip6stat.ip6s_badscope++;
1401 mifp->m6_bytes_out += plen;
1402 MC6_SEND(ip6, mifp, m);
1408 phyint_send(ip6, mifp, m)
1409 struct ip6_hdr *ip6;
1413 struct mbuf *mb_copy;
1414 struct ifnet *ifp = mifp->m6_ifp;
1416 int s = splnet(); /* needs to protect static "ro" below. */
1417 static struct route_in6 ro;
1418 struct in6_multi *in6m;
1419 struct sockaddr_in6 *dst6;
1422 * Make a new reference to the packet; make sure that
1423 * the IPv6 header is actually copied, not just referenced,
1424 * so that ip6_output() only scribbles on the copy.
1426 mb_copy = m_copy(m, 0, M_COPYALL);
1428 (M_HASCL(mb_copy) || mb_copy->m_len < sizeof(struct ip6_hdr)))
1429 mb_copy = m_pullup(mb_copy, sizeof(struct ip6_hdr));
1430 if (mb_copy == NULL) {
1434 /* set MCAST flag to the outgoing packet */
1435 mb_copy->m_flags |= M_MCAST;
1438 * If we sourced the packet, call ip6_output since we may devide
1439 * the packet into fragments when the packet is too big for the
1440 * outgoing interface.
1441 * Otherwise, we can simply send the packet to the interface
1444 if (m->m_pkthdr.rcvif == NULL) {
1445 struct ip6_moptions im6o;
1447 im6o.im6o_multicast_ifp = ifp;
1448 /* XXX: ip6_output will override ip6->ip6_hlim */
1449 im6o.im6o_multicast_hlim = ip6->ip6_hlim;
1450 im6o.im6o_multicast_loop = 1;
1451 error = ip6_output(mb_copy, NULL, &ro,
1452 IPV6_FORWARDING, &im6o, NULL, NULL);
1455 if (mrt6debug & DEBUG_XMIT)
1456 log(LOG_DEBUG, "phyint_send on mif %d err %d\n",
1457 mifp - mif6table, error);
1464 * If we belong to the destination multicast group
1465 * on the outgoing interface, loop back a copy.
1467 dst6 = (struct sockaddr_in6 *)&ro.ro_dst;
1468 IN6_LOOKUP_MULTI(ip6->ip6_dst, ifp, in6m);
1470 dst6->sin6_len = sizeof(struct sockaddr_in6);
1471 dst6->sin6_family = AF_INET6;
1472 dst6->sin6_addr = ip6->ip6_dst;
1473 ip6_mloopback(ifp, m, (struct sockaddr_in6 *)&ro.ro_dst);
1476 * Put the packet into the sending queue of the outgoing interface
1477 * if it would fit in the MTU of the interface.
1479 if (mb_copy->m_pkthdr.len <= ifp->if_mtu || ifp->if_mtu < IPV6_MMTU) {
1480 dst6->sin6_len = sizeof(struct sockaddr_in6);
1481 dst6->sin6_family = AF_INET6;
1482 dst6->sin6_addr = ip6->ip6_dst;
1484 * We just call if_output instead of nd6_output here, since
1485 * we need no ND for a multicast forwarded packet...right?
1487 error = (*ifp->if_output)(ifp, mb_copy,
1488 (struct sockaddr *)&ro.ro_dst, NULL);
1490 if (mrt6debug & DEBUG_XMIT)
1491 log(LOG_DEBUG, "phyint_send on mif %d err %d\n",
1492 mifp - mif6table, error);
1495 #ifdef MULTICAST_PMTUD
1496 icmp6_error(mb_copy, ICMP6_PACKET_TOO_BIG, 0, ifp->if_mtu);
1499 if (mrt6debug & DEBUG_XMIT)
1501 "phyint_send: packet too big on %s o %s g %s"
1502 " size %d(discarded)\n",
1504 ip6_sprintf(&ip6->ip6_src),
1505 ip6_sprintf(&ip6->ip6_dst),
1506 mb_copy->m_pkthdr.len);
1507 #endif /* MRT6DEBUG */
1508 m_freem(mb_copy); /* simply discard the packet */
1516 register_send(ip6, mif, m)
1517 struct ip6_hdr *ip6;
1522 int i, len = m->m_pkthdr.len;
1523 static struct sockaddr_in6 sin6 = { sizeof(sin6), AF_INET6 };
1524 struct mrt6msg *im6;
1528 log(LOG_DEBUG, "** IPv6 register_send **\n src %s dst %s\n",
1529 ip6_sprintf(&ip6->ip6_src), ip6_sprintf(&ip6->ip6_dst));
1531 ++pim6stat.pim6s_snd_registers;
1533 /* Make a copy of the packet to send to the user level process */
1534 MGETHDR(mm, M_DONTWAIT, MT_HEADER);
1537 mm->m_pkthdr.rcvif = NULL;
1538 mm->m_data += max_linkhdr;
1539 mm->m_len = sizeof(struct ip6_hdr);
1541 if ((mm->m_next = m_copy(m, 0, M_COPYALL)) == NULL) {
1545 i = MHLEN - M_LEADINGSPACE(mm);
1548 mm = m_pullup(mm, i);
1551 /* TODO: check it! */
1552 mm->m_pkthdr.len = len + sizeof(struct ip6_hdr);
1555 * Send message to routing daemon
1557 sin6.sin6_addr = ip6->ip6_src;
1559 im6 = mtod(mm, struct mrt6msg *);
1560 im6->im6_msgtype = MRT6MSG_WHOLEPKT;
1563 im6->im6_mif = mif - mif6table;
1565 /* iif info is not given for reg. encap.n */
1566 mrt6stat.mrt6s_upcalls++;
1568 if (socket_send(ip6_mrouter, mm, &sin6) < 0) {
1572 "register_send: ip6_mrouter socket queue full\n");
1574 ++mrt6stat.mrt6s_upq_sockfull;
1581 * PIM sparse mode hook
1582 * Receives the pim control messages, and passes them up to the listening
1583 * socket, using rip6_input.
1584 * The only message processed is the REGISTER pim message; the pim header
1585 * is stripped off, and the inner packet is passed to register_mforward.
1588 pim6_input(mp, offp, proto)
1592 struct pim *pim; /* pointer to a pim struct */
1593 struct ip6_hdr *ip6;
1595 struct mbuf *m = *mp;
1599 ++pim6stat.pim6s_rcv_total;
1601 ip6 = mtod(m, struct ip6_hdr *);
1602 pimlen = m->m_pkthdr.len - *offp;
1607 if (pimlen < PIM_MINLEN) {
1608 ++pim6stat.pim6s_rcv_tooshort;
1610 if (mrt6debug & DEBUG_PIM)
1611 log(LOG_DEBUG,"pim6_input: PIM packet too short\n");
1614 return(IPPROTO_DONE);
1618 * if the packet is at least as big as a REGISTER, go ahead
1619 * and grab the PIM REGISTER header size, to avoid another
1620 * possible m_pullup() later.
1622 * PIM_MINLEN == pimhdr + u_int32 == 8
1623 * PIM6_REG_MINLEN == pimhdr + reghdr + eip6hdr == 4 + 4 + 40
1625 minlen = (pimlen >= PIM6_REG_MINLEN) ? PIM6_REG_MINLEN : PIM_MINLEN;
1628 * Make sure that the IP6 and PIM headers in contiguous memory, and
1629 * possibly the PIM REGISTER header
1631 #ifndef PULLDOWN_TEST
1632 IP6_EXTHDR_CHECK(m, off, minlen, IPPROTO_DONE);
1633 /* adjust pointer */
1634 ip6 = mtod(m, struct ip6_hdr *);
1636 /* adjust mbuf to point to the PIM header */
1637 pim = (struct pim *)((caddr_t)ip6 + off);
1639 IP6_EXTHDR_GET(pim, struct pim *, m, off, minlen);
1641 pim6stat.pim6s_rcv_tooshort++;
1642 return IPPROTO_DONE;
1646 #define PIM6_CHECKSUM
1647 #ifdef PIM6_CHECKSUM
1652 * Validate checksum.
1653 * If PIM REGISTER, exclude the data packet
1655 if (pim->pim_type == PIM_REGISTER)
1656 cksumlen = PIM_MINLEN;
1660 if (in6_cksum(m, IPPROTO_PIM, off, cksumlen)) {
1661 ++pim6stat.pim6s_rcv_badsum;
1663 if (mrt6debug & DEBUG_PIM)
1665 "pim6_input: invalid checksum\n");
1668 return(IPPROTO_DONE);
1671 #endif /* PIM_CHECKSUM */
1673 /* PIM version check */
1674 if (pim->pim_ver != PIM_VERSION) {
1675 ++pim6stat.pim6s_rcv_badversion;
1678 "pim6_input: incorrect version %d, expecting %d\n",
1679 pim->pim_ver, PIM_VERSION);
1682 return(IPPROTO_DONE);
1685 if (pim->pim_type == PIM_REGISTER) {
1687 * since this is a REGISTER, we'll make a copy of the register
1688 * headers ip6+pim+u_int32_t+encap_ip6, to be passed up to the
1691 static struct sockaddr_in6 dst = { sizeof(dst), AF_INET6 };
1694 struct ip6_hdr *eip6;
1698 ++pim6stat.pim6s_rcv_registers;
1700 if ((reg_mif_num >= nummifs) || (reg_mif_num == (mifi_t) -1)) {
1702 if (mrt6debug & DEBUG_PIM)
1704 "pim6_input: register mif not set: %d\n",
1708 return(IPPROTO_DONE);
1711 reghdr = (u_int32_t *)(pim + 1);
1713 if ((ntohl(*reghdr) & PIM_NULL_REGISTER))
1714 goto pim6_input_to_daemon;
1719 if (pimlen < PIM6_REG_MINLEN) {
1720 ++pim6stat.pim6s_rcv_tooshort;
1721 ++pim6stat.pim6s_rcv_badregisters;
1724 "pim6_input: register packet size too "
1725 "small %d from %s\n",
1726 pimlen, ip6_sprintf(&ip6->ip6_src));
1729 return(IPPROTO_DONE);
1732 eip6 = (struct ip6_hdr *) (reghdr + 1);
1734 if (mrt6debug & DEBUG_PIM)
1736 "pim6_input[register], eip6: %s -> %s, "
1738 ip6_sprintf(&eip6->ip6_src),
1739 ip6_sprintf(&eip6->ip6_dst),
1740 ntohs(eip6->ip6_plen));
1743 /* verify the version number of the inner packet */
1744 if ((eip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
1745 ++pim6stat.pim6s_rcv_badregisters;
1747 log(LOG_DEBUG, "pim6_input: invalid IP version (%d) "
1748 "of the inner packet\n",
1749 (eip6->ip6_vfc & IPV6_VERSION));
1752 return(IPPROTO_NONE);
1755 /* verify the inner packet is destined to a mcast group */
1756 if (!IN6_IS_ADDR_MULTICAST(&eip6->ip6_dst)) {
1757 ++pim6stat.pim6s_rcv_badregisters;
1759 if (mrt6debug & DEBUG_PIM)
1761 "pim6_input: inner packet of register "
1762 "is not multicast %s\n",
1763 ip6_sprintf(&eip6->ip6_dst));
1766 return(IPPROTO_DONE);
1770 * make a copy of the whole header to pass to the daemon later.
1772 mcp = m_copy(m, 0, off + PIM6_REG_MINLEN);
1776 "pim6_input: pim register: "
1777 "could not copy register head\n");
1780 return(IPPROTO_DONE);
1784 * forward the inner ip6 packet; point m_data at the inner ip6.
1786 m_adj(m, off + PIM_MINLEN);
1788 if (mrt6debug & DEBUG_PIM) {
1790 "pim6_input: forwarding decapsulated register: "
1791 "src %s, dst %s, mif %d\n",
1792 ip6_sprintf(&eip6->ip6_src),
1793 ip6_sprintf(&eip6->ip6_dst),
1798 rc = if_simloop(mif6table[reg_mif_num].m6_ifp, m,
1799 dst.sin6_family, NULL);
1801 /* prepare the register head to send to the mrouting daemon */
1806 * Pass the PIM message up to the daemon; if it is a register message
1807 * pass the 'head' only up to the daemon. This includes the
1808 * encapsulator ip6 header, pim header, register header and the
1809 * encapsulated ip6 header.
1811 pim6_input_to_daemon:
1812 rip6_input(&m, offp, proto);
1813 return(IPPROTO_DONE);