1 /* $FreeBSD: src/sys/netinet6/ip6_mroute.c,v 1.2.2.9 2003/01/23 21:06:47 sam Exp $ */
2 /* $KAME: ip6_mroute.c,v 1.58 2001/12/18 02:36:31 itojun Exp $ */
5 * Copyright (C) 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 /* BSDI ip_mroute.c,v 2.10 1996/11/14 00:29:52 jch Exp */
36 * IP multicast forwarding procedures
38 * Written by David Waitzman, BBN Labs, August 1988.
39 * Modified by Steve Deering, Stanford, February 1989.
40 * Modified by Mark J. Steiglitz, Stanford, May, 1991
41 * Modified by Van Jacobson, LBL, January 1993
42 * Modified by Ajit Thyagarajan, PARC, August 1993
43 * Modified by Bill Fenenr, PARC, April 1994
45 * MROUTING Revision: 3.5.1.2 + PIM-SMv2 (pimd) Support
49 #include "opt_inet6.h"
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/callout.h>
54 #include <sys/malloc.h>
56 #include <sys/socket.h>
57 #include <sys/socketvar.h>
58 #include <sys/sockio.h>
59 #include <sys/protosw.h>
60 #include <sys/errno.h>
62 #include <sys/kernel.h>
63 #include <sys/syslog.h>
66 #include <net/route.h>
67 #include <net/raw_cb.h>
69 #include <netinet/in.h>
70 #include <netinet/in_var.h>
72 #include <netinet/ip6.h>
73 #include <netinet6/ip6_var.h>
74 #include <netinet6/ip6_mroute.h>
75 #include <netinet6/pim6.h>
76 #include <netinet6/pim6_var.h>
78 #include <net/net_osdep.h>
80 static MALLOC_DEFINE(M_MRTABLE, "mf6c", "multicast forwarding cache entry");
82 #define M_HASCL(m) ((m)->m_flags & M_EXT)
84 static int ip6_mdq __P((struct mbuf *, struct ifnet *, struct mf6c *));
85 static void phyint_send __P((struct ip6_hdr *, struct mif6 *, struct mbuf *));
87 static int set_pim6 __P((int *));
88 static int socket_send __P((struct socket *, struct mbuf *,
89 struct sockaddr_in6 *));
90 static int register_send __P((struct ip6_hdr *, struct mif6 *,
94 * Globals. All but ip6_mrouter, ip6_mrtproto and mrt6stat could be static,
95 * except for netstat or debugging purposes.
97 struct socket *ip6_mrouter = NULL;
98 int ip6_mrouter_ver = 0;
99 int ip6_mrtproto = IPPROTO_PIM; /* for netstat only */
100 struct mrt6stat mrt6stat;
102 #define NO_RTE_FOUND 0x1
103 #define RTE_FOUND 0x2
105 struct mf6c *mf6ctable[MF6CTBLSIZ];
106 u_char n6expire[MF6CTBLSIZ];
107 static struct mif6 mif6table[MAXMIFS];
109 u_int mrt6debug = 0; /* debug level */
110 #define DEBUG_MFC 0x02
111 #define DEBUG_FORWARD 0x04
112 #define DEBUG_EXPIRE 0x08
113 #define DEBUG_XMIT 0x10
114 #define DEBUG_REG 0x20
115 #define DEBUG_PIM 0x40
118 static void expire_upcalls __P((void *));
119 #define EXPIRE_TIMEOUT (hz / 4) /* 4x / second */
120 #define UPCALL_EXPIRE 6 /* number of timeouts */
124 extern struct socket *ip_mrouter;
129 * 'Interfaces' associated with decapsulator (so we can tell
130 * packets that went through it from ones that get reflected
131 * by a broken gateway). These interfaces are never linked into
132 * the system ifnet list & no routes point to them. I.e., packets
133 * can't be sent this way. They only exist as a placeholder for
134 * multicast source verification.
136 struct ifnet multicast_register_if;
138 #define ENCAP_HOPS 64
143 static mifi_t nummifs = 0;
144 static mifi_t reg_mif_num = (mifi_t)-1;
146 static struct pim6stat pim6stat;
150 * Hash function for a source, group entry
152 #define MF6CHASH(a, g) MF6CHASHMOD((a).s6_addr32[0] ^ (a).s6_addr32[1] ^ \
153 (a).s6_addr32[2] ^ (a).s6_addr32[3] ^ \
154 (g).s6_addr32[0] ^ (g).s6_addr32[1] ^ \
155 (g).s6_addr32[2] ^ (g).s6_addr32[3])
158 * Find a route for a given origin IPv6 address and Multicast group address.
159 * Quality of service parameter to be added in the future!!!
162 #define MF6CFIND(o, g, rt) do { \
163 struct mf6c *_rt = mf6ctable[MF6CHASH(o,g)]; \
165 mrt6stat.mrt6s_mfc_lookups++; \
167 if (IN6_ARE_ADDR_EQUAL(&_rt->mf6c_origin.sin6_addr, &(o)) && \
168 IN6_ARE_ADDR_EQUAL(&_rt->mf6c_mcastgrp.sin6_addr, &(g)) && \
169 (_rt->mf6c_stall == NULL)) { \
173 _rt = _rt->mf6c_next; \
176 mrt6stat.mrt6s_mfc_misses++; \
181 * Macros to compute elapsed time efficiently
182 * Borrowed from Van Jacobson's scheduling code
184 #define TV_DELTA(a, b, delta) do { \
187 delta = (a).tv_usec - (b).tv_usec; \
188 if ((xxs = (a).tv_sec - (b).tv_sec)) { \
197 delta += (1000000 * xxs); \
202 #define TV_LT(a, b) (((a).tv_usec < (b).tv_usec && \
203 (a).tv_sec <= (b).tv_sec) || (a).tv_sec < (b).tv_sec)
206 #define UPCALL_MAX 50
207 u_long upcall_data[UPCALL_MAX + 1];
208 static void collate();
209 #endif /* UPCALL_TIMING */
211 static int get_sg_cnt __P((struct sioc_sg_req6 *));
212 static int get_mif6_cnt __P((struct sioc_mif_req6 *));
213 static int ip6_mrouter_init __P((struct socket *, struct mbuf *, int));
214 static int add_m6if __P((struct mif6ctl *));
215 static int del_m6if __P((mifi_t *));
216 static int add_m6fc __P((struct mf6cctl *));
217 static int del_m6fc __P((struct mf6cctl *));
219 static struct callout expire_upcalls_ch;
222 * Handle MRT setsockopt commands to modify the multicast routing tables.
225 ip6_mrouter_set(so, sopt)
227 struct sockopt *sopt;
232 if (so != ip6_mrouter && sopt->sopt_name != MRT6_INIT)
235 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
237 if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
240 switch (sopt->sopt_name) {
245 error = ip6_mrouter_init(so, m, sopt->sopt_name);
248 error = ip6_mrouter_done();
251 error = add_m6if(mtod(m, struct mif6ctl *));
254 error = del_m6if(mtod(m, mifi_t *));
257 error = add_m6fc(mtod(m, struct mf6cctl *));
260 error = del_m6fc(mtod(m, struct mf6cctl *));
263 error = set_pim6(mtod(m, int *));
275 * Handle MRT getsockopt commands
278 ip6_mrouter_get(so, sopt)
280 struct sockopt *sopt;
284 if (so != ip6_mrouter) return EACCES;
286 switch (sopt->sopt_name) {
288 error = sooptcopyout(sopt, &pim6, sizeof(pim6));
295 * Handle ioctl commands to obtain information from the cache
298 mrt6_ioctl(cmd, data)
305 case SIOCGETSGCNT_IN6:
306 return(get_sg_cnt((struct sioc_sg_req6 *)data));
307 break; /* for safety */
308 case SIOCGETMIFCNT_IN6:
309 return(get_mif6_cnt((struct sioc_mif_req6 *)data));
310 break; /* for safety */
319 * returns the packet, byte, rpf-failure count for the source group provided
323 struct sioc_sg_req6 *req;
329 MF6CFIND(req->src.sin6_addr, req->grp.sin6_addr, rt);
332 req->pktcnt = rt->mf6c_pkt_cnt;
333 req->bytecnt = rt->mf6c_byte_cnt;
334 req->wrong_if = rt->mf6c_wrong_if;
338 req->pktcnt = req->bytecnt = req->wrong_if = 0xffffffff;
345 * returns the input and output packet and byte counts on the mif provided
349 struct sioc_mif_req6 *req;
351 mifi_t mifi = req->mifi;
356 req->icount = mif6table[mifi].m6_pkt_in;
357 req->ocount = mif6table[mifi].m6_pkt_out;
358 req->ibytes = mif6table[mifi].m6_bytes_in;
359 req->obytes = mif6table[mifi].m6_bytes_out;
368 if ((*i != 1) && (*i != 0))
377 * Enable multicast routing
380 ip6_mrouter_init(so, m, cmd)
390 "ip6_mrouter_init: so_type = %d, pr_protocol = %d\n",
391 so->so_type, so->so_proto->pr_protocol);
394 if (so->so_type != SOCK_RAW ||
395 so->so_proto->pr_protocol != IPPROTO_ICMPV6)
398 if (!m || (m->m_len != sizeof(int *)))
405 if (ip6_mrouter != NULL) return EADDRINUSE;
408 ip6_mrouter_ver = cmd;
410 bzero((caddr_t)mf6ctable, sizeof(mf6ctable));
411 bzero((caddr_t)n6expire, sizeof(n6expire));
413 pim6 = 0;/* used for stubbing out/in pim stuff */
415 callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT,
416 expire_upcalls, NULL);
420 log(LOG_DEBUG, "ip6_mrouter_init\n");
427 * Disable multicast routing
435 struct in6_ifreq ifr;
443 * For each phyint in use, disable promiscuous reception of all IPv6
449 * If there is still IPv4 multicast routing daemon,
450 * we remain interfaces to receive all muliticasted packets.
451 * XXX: there may be an interface in which the IPv4 multicast
452 * daemon is not interested...
458 for (mifi = 0; mifi < nummifs; mifi++) {
459 if (mif6table[mifi].m6_ifp &&
460 !(mif6table[mifi].m6_flags & MIFF_REGISTER)) {
461 ifr.ifr_addr.sin6_family = AF_INET6;
462 ifr.ifr_addr.sin6_addr= in6addr_any;
463 ifp = mif6table[mifi].m6_ifp;
464 (*ifp->if_ioctl)(ifp, SIOCDELMULTI,
470 bzero((caddr_t)qtable, sizeof(qtable));
471 bzero((caddr_t)tbftable, sizeof(tbftable));
473 bzero((caddr_t)mif6table, sizeof(mif6table));
476 pim6 = 0; /* used to stub out/in pim specific code */
478 callout_stop(&expire_upcalls_ch);
481 * Free all multicast forwarding cache entries.
483 for (i = 0; i < MF6CTBLSIZ; i++) {
488 for (rte = rt->mf6c_stall; rte != NULL; ) {
489 struct rtdetq *n = rte->next;
492 free(rte, M_MRTABLE);
497 free(frt, M_MRTABLE);
501 bzero((caddr_t)mf6ctable, sizeof(mf6ctable));
504 * Reset de-encapsulation cache
515 log(LOG_DEBUG, "ip6_mrouter_done\n");
521 static struct sockaddr_in6 sin6 = { sizeof(sin6), AF_INET6 };
524 * Add a mif to the mif table
528 struct mif6ctl *mifcp;
534 struct tbf *m_tbf = tbftable + mifcp->mif6c_mifi;
537 if (mifcp->mif6c_mifi >= MAXMIFS)
539 mifp = mif6table + mifcp->mif6c_mifi;
541 return EADDRINUSE; /* XXX: is it appropriate? */
542 if (mifcp->mif6c_pifi == 0 || mifcp->mif6c_pifi > if_index)
544 ifp = ifindex2ifnet[mifcp->mif6c_pifi];
546 if (mifcp->mif6c_flags & MIFF_REGISTER) {
547 if (reg_mif_num == (mifi_t)-1) {
548 multicast_register_if.if_name = "register_mif";
549 multicast_register_if.if_flags |= IFF_LOOPBACK;
550 multicast_register_if.if_index = mifcp->mif6c_mifi;
551 reg_mif_num = mifcp->mif6c_mifi;
554 ifp = &multicast_register_if;
558 /* Make sure the interface supports multicast */
559 if ((ifp->if_flags & IFF_MULTICAST) == 0)
563 error = if_allmulti(ifp, 1);
570 mifp->m6_flags = mifcp->mif6c_flags;
573 /* scaling up here allows division by 1024 in critical code */
574 mifp->m6_rate_limit = mifcp->mif6c_rate_limit * 1024 / 1000;
576 /* initialize per mif pkt counters */
578 mifp->m6_pkt_out = 0;
579 mifp->m6_bytes_in = 0;
580 mifp->m6_bytes_out = 0;
583 /* Adjust nummifs up if the mifi is higher than nummifs */
584 if (nummifs <= mifcp->mif6c_mifi)
585 nummifs = mifcp->mif6c_mifi + 1;
590 "add_mif #%d, phyint %s%d\n",
592 ifp->if_name, ifp->if_unit);
599 * Delete a mif from the mif table
605 struct mif6 *mifp = mif6table + *mifip;
610 if (*mifip >= nummifs)
612 if (mifp->m6_ifp == NULL)
617 if (!(mifp->m6_flags & MIFF_REGISTER)) {
619 * XXX: what if there is yet IPv4 multicast daemon
620 * using the interface?
628 bzero((caddr_t)qtable[*mifip], sizeof(qtable[*mifip]));
629 bzero((caddr_t)mifp->m6_tbf, sizeof(*(mifp->m6_tbf)));
631 bzero((caddr_t)mifp, sizeof (*mifp));
633 /* Adjust nummifs down */
634 for (mifi = nummifs; mifi > 0; mifi--)
635 if (mif6table[mifi - 1].m6_ifp)
643 log(LOG_DEBUG, "del_m6if %d, nummifs %d\n", *mifip, nummifs);
654 struct mf6cctl *mfccp;
662 MF6CFIND(mfccp->mf6cc_origin.sin6_addr,
663 mfccp->mf6cc_mcastgrp.sin6_addr, rt);
665 /* If an entry already exists, just update the fields */
668 if (mrt6debug & DEBUG_MFC)
670 "add_m6fc no upcall h %d o %s g %s p %x\n",
671 ip6_sprintf(&mfccp->mf6cc_origin.sin6_addr),
672 ip6_sprintf(&mfccp->mf6cc_mcastgrp.sin6_addr),
673 mfccp->mf6cc_parent);
677 rt->mf6c_parent = mfccp->mf6cc_parent;
678 rt->mf6c_ifset = mfccp->mf6cc_ifset;
684 * Find the entry for which the upcall was made and update
687 hash = MF6CHASH(mfccp->mf6cc_origin.sin6_addr,
688 mfccp->mf6cc_mcastgrp.sin6_addr);
689 for (rt = mf6ctable[hash], nstl = 0; rt; rt = rt->mf6c_next) {
690 if (IN6_ARE_ADDR_EQUAL(&rt->mf6c_origin.sin6_addr,
691 &mfccp->mf6cc_origin.sin6_addr) &&
692 IN6_ARE_ADDR_EQUAL(&rt->mf6c_mcastgrp.sin6_addr,
693 &mfccp->mf6cc_mcastgrp.sin6_addr) &&
694 (rt->mf6c_stall != NULL)) {
698 "add_m6fc: %s o %s g %s p %x dbx %p\n",
699 "multiple kernel entries",
700 ip6_sprintf(&mfccp->mf6cc_origin.sin6_addr),
701 ip6_sprintf(&mfccp->mf6cc_mcastgrp.sin6_addr),
702 mfccp->mf6cc_parent, rt->mf6c_stall);
705 if (mrt6debug & DEBUG_MFC)
707 "add_m6fc o %s g %s p %x dbg %x\n",
708 ip6_sprintf(&mfccp->mf6cc_origin.sin6_addr),
709 ip6_sprintf(&mfccp->mf6cc_mcastgrp.sin6_addr),
710 mfccp->mf6cc_parent, rt->mf6c_stall);
713 rt->mf6c_origin = mfccp->mf6cc_origin;
714 rt->mf6c_mcastgrp = mfccp->mf6cc_mcastgrp;
715 rt->mf6c_parent = mfccp->mf6cc_parent;
716 rt->mf6c_ifset = mfccp->mf6cc_ifset;
717 /* initialize pkt counters per src-grp */
718 rt->mf6c_pkt_cnt = 0;
719 rt->mf6c_byte_cnt = 0;
720 rt->mf6c_wrong_if = 0;
722 rt->mf6c_expire = 0; /* Don't clean this guy up */
725 /* free packets Qed at the end of this entry */
726 for (rte = rt->mf6c_stall; rte != NULL; ) {
727 struct rtdetq *n = rte->next;
728 ip6_mdq(rte->m, rte->ifp, rt);
732 #endif /* UPCALL_TIMING */
733 free(rte, M_MRTABLE);
736 rt->mf6c_stall = NULL;
741 * It is possible that an entry is being inserted without an upcall
745 if (mrt6debug & DEBUG_MFC)
746 log(LOG_DEBUG,"add_mfc no upcall h %d o %s g %s p %x\n",
748 ip6_sprintf(&mfccp->mf6cc_origin.sin6_addr),
749 ip6_sprintf(&mfccp->mf6cc_mcastgrp.sin6_addr),
750 mfccp->mf6cc_parent);
753 for (rt = mf6ctable[hash]; rt; rt = rt->mf6c_next) {
755 if (IN6_ARE_ADDR_EQUAL(&rt->mf6c_origin.sin6_addr,
756 &mfccp->mf6cc_origin.sin6_addr)&&
757 IN6_ARE_ADDR_EQUAL(&rt->mf6c_mcastgrp.sin6_addr,
758 &mfccp->mf6cc_mcastgrp.sin6_addr)) {
760 rt->mf6c_origin = mfccp->mf6cc_origin;
761 rt->mf6c_mcastgrp = mfccp->mf6cc_mcastgrp;
762 rt->mf6c_parent = mfccp->mf6cc_parent;
763 rt->mf6c_ifset = mfccp->mf6cc_ifset;
764 /* initialize pkt counters per src-grp */
765 rt->mf6c_pkt_cnt = 0;
766 rt->mf6c_byte_cnt = 0;
767 rt->mf6c_wrong_if = 0;
775 /* no upcall, so make a new entry */
776 rt = (struct mf6c *)malloc(sizeof(*rt), M_MRTABLE,
783 /* insert new entry at head of hash chain */
784 rt->mf6c_origin = mfccp->mf6cc_origin;
785 rt->mf6c_mcastgrp = mfccp->mf6cc_mcastgrp;
786 rt->mf6c_parent = mfccp->mf6cc_parent;
787 rt->mf6c_ifset = mfccp->mf6cc_ifset;
788 /* initialize pkt counters per src-grp */
789 rt->mf6c_pkt_cnt = 0;
790 rt->mf6c_byte_cnt = 0;
791 rt->mf6c_wrong_if = 0;
793 rt->mf6c_stall = NULL;
795 /* link into table */
796 rt->mf6c_next = mf6ctable[hash];
797 mf6ctable[hash] = rt;
806 * collect delay statistics on the upcalls
820 TV_DELTA(tp, *t, delta);
829 #endif /* UPCALL_TIMING */
832 * Delete an mfc entry
836 struct mf6cctl *mfccp;
838 struct sockaddr_in6 origin;
839 struct sockaddr_in6 mcastgrp;
845 origin = mfccp->mf6cc_origin;
846 mcastgrp = mfccp->mf6cc_mcastgrp;
847 hash = MF6CHASH(origin.sin6_addr, mcastgrp.sin6_addr);
850 if (mrt6debug & DEBUG_MFC)
851 log(LOG_DEBUG,"del_m6fc orig %s mcastgrp %s\n",
852 ip6_sprintf(&origin.sin6_addr),
853 ip6_sprintf(&mcastgrp.sin6_addr));
858 nptr = &mf6ctable[hash];
859 while ((rt = *nptr) != NULL) {
860 if (IN6_ARE_ADDR_EQUAL(&origin.sin6_addr,
861 &rt->mf6c_origin.sin6_addr) &&
862 IN6_ARE_ADDR_EQUAL(&mcastgrp.sin6_addr,
863 &rt->mf6c_mcastgrp.sin6_addr) &&
864 rt->mf6c_stall == NULL)
867 nptr = &rt->mf6c_next;
871 return EADDRNOTAVAIL;
874 *nptr = rt->mf6c_next;
883 socket_send(s, mm, src)
886 struct sockaddr_in6 *src;
889 if (sbappendaddr(&s->so_rcv,
890 (struct sockaddr *)src,
891 mm, (struct mbuf *)0) != 0) {
901 * IPv6 multicast forwarding function. This function assumes that the packet
902 * pointed to by "ip6" has arrived on (or is about to be sent to) the interface
903 * pointed to by "ifp", and the packet is to be relayed to other networks
904 * that have members of the packet's destination IPv6 multicast group.
906 * The packet is returned unscathed to the caller, unless it is
907 * erroneous, in which case a non-zero return value tells the caller to
912 ip6_mforward(ip6, ifp, m)
924 if (mrt6debug & DEBUG_FORWARD)
925 log(LOG_DEBUG, "ip6_mforward: src %s, dst %s, ifindex %d\n",
926 ip6_sprintf(&ip6->ip6_src), ip6_sprintf(&ip6->ip6_dst),
931 * Don't forward a packet with Hop limit of zero or one,
932 * or a packet destined to a local-only group.
934 if (ip6->ip6_hlim <= 1 || IN6_IS_ADDR_MC_NODELOCAL(&ip6->ip6_dst) ||
935 IN6_IS_ADDR_MC_LINKLOCAL(&ip6->ip6_dst))
940 * Source address check: do not forward packets with unspecified
941 * source. It was discussed in July 2000, on ipngwg mailing list.
942 * This is rather more serious than unicast cases, because some
943 * MLD packets can be sent with the unspecified source address
944 * (although such packets must normally set 1 to the hop limit field).
946 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
947 ip6stat.ip6s_cantforward++;
948 if (ip6_log_time + ip6_log_interval < time_second) {
949 ip6_log_time = time_second;
952 "from %s to %s nxt %d received on %s\n",
953 ip6_sprintf(&ip6->ip6_src),
954 ip6_sprintf(&ip6->ip6_dst),
956 if_name(m->m_pkthdr.rcvif));
962 * Determine forwarding mifs from the forwarding cache table
965 MF6CFIND(ip6->ip6_src, ip6->ip6_dst, rt);
967 /* Entry exists, so forward if necessary */
970 return (ip6_mdq(m, ifp, rt));
973 * If we don't have a route for packet's origin,
974 * Make a copy of the packet &
975 * send message to routing daemon
986 #endif /* UPCALL_TIMING */
988 mrt6stat.mrt6s_no_route++;
990 if (mrt6debug & (DEBUG_FORWARD | DEBUG_MFC))
991 log(LOG_DEBUG, "ip6_mforward: no rte s %s g %s\n",
992 ip6_sprintf(&ip6->ip6_src),
993 ip6_sprintf(&ip6->ip6_dst));
997 * Allocate mbufs early so that we don't do extra work if we
998 * are just going to fail anyway.
1000 rte = (struct rtdetq *)malloc(sizeof(*rte), M_MRTABLE,
1006 mb0 = m_copy(m, 0, M_COPYALL);
1008 * Pullup packet header if needed before storing it,
1009 * as other references may modify it in the meantime.
1012 (M_HASCL(mb0) || mb0->m_len < sizeof(struct ip6_hdr)))
1013 mb0 = m_pullup(mb0, sizeof(struct ip6_hdr));
1015 free(rte, M_MRTABLE);
1020 /* is there an upcall waiting for this packet? */
1021 hash = MF6CHASH(ip6->ip6_src, ip6->ip6_dst);
1022 for (rt = mf6ctable[hash]; rt; rt = rt->mf6c_next) {
1023 if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_src,
1024 &rt->mf6c_origin.sin6_addr) &&
1025 IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
1026 &rt->mf6c_mcastgrp.sin6_addr) &&
1027 (rt->mf6c_stall != NULL))
1034 struct omrt6msg *oim;
1037 /* no upcall, so make a new entry */
1038 rt = (struct mf6c *)malloc(sizeof(*rt), M_MRTABLE,
1041 free(rte, M_MRTABLE);
1047 * Make a copy of the header to send to the user
1050 mm = m_copy(mb0, 0, sizeof(struct ip6_hdr));
1053 free(rte, M_MRTABLE);
1055 free(rt, M_MRTABLE);
1061 * Send message to routing daemon
1063 sin6.sin6_addr = ip6->ip6_src;
1069 switch (ip6_mrouter_ver) {
1072 oim = mtod(mm, struct omrt6msg *);
1073 oim->im6_msgtype = MRT6MSG_NOCACHE;
1078 im = mtod(mm, struct mrt6msg *);
1079 im->im6_msgtype = MRT6MSG_NOCACHE;
1083 free(rte, M_MRTABLE);
1085 free(rt, M_MRTABLE);
1091 if (mrt6debug & DEBUG_FORWARD)
1093 "getting the iif info in the kernel\n");
1096 for (mifp = mif6table, mifi = 0;
1097 mifi < nummifs && mifp->m6_ifp != ifp;
1101 switch (ip6_mrouter_ver) {
1104 oim->im6_mif = mifi;
1112 if (socket_send(ip6_mrouter, mm, &sin6) < 0) {
1113 log(LOG_WARNING, "ip6_mforward: ip6_mrouter "
1114 "socket queue full\n");
1115 mrt6stat.mrt6s_upq_sockfull++;
1116 free(rte, M_MRTABLE);
1118 free(rt, M_MRTABLE);
1123 mrt6stat.mrt6s_upcalls++;
1125 /* insert new entry at head of hash chain */
1126 bzero(rt, sizeof(*rt));
1127 rt->mf6c_origin.sin6_family = AF_INET6;
1128 rt->mf6c_origin.sin6_len = sizeof(struct sockaddr_in6);
1129 rt->mf6c_origin.sin6_addr = ip6->ip6_src;
1130 rt->mf6c_mcastgrp.sin6_family = AF_INET6;
1131 rt->mf6c_mcastgrp.sin6_len = sizeof(struct sockaddr_in6);
1132 rt->mf6c_mcastgrp.sin6_addr = ip6->ip6_dst;
1133 rt->mf6c_expire = UPCALL_EXPIRE;
1135 rt->mf6c_parent = MF6C_INCOMPLETE_PARENT;
1137 /* link into table */
1138 rt->mf6c_next = mf6ctable[hash];
1139 mf6ctable[hash] = rt;
1140 /* Add this entry to the end of the queue */
1141 rt->mf6c_stall = rte;
1143 /* determine if q has overflowed */
1147 for (p = &rt->mf6c_stall; *p != NULL; p = &(*p)->next)
1148 if (++npkts > MAX_UPQ6) {
1149 mrt6stat.mrt6s_upq_ovflw++;
1150 free(rte, M_MRTABLE);
1156 /* Add this entry to the end of the queue */
1163 #ifdef UPCALL_TIMING
1165 #endif /* UPCALL_TIMING */
1174 * Clean up cache entries if upcalls are not serviced
1175 * Call from the Slow Timeout mechanism, every half second.
1178 expire_upcalls(unused)
1182 struct mf6c *mfc, **nptr;
1187 for (i = 0; i < MF6CTBLSIZ; i++) {
1188 if (n6expire[i] == 0)
1190 nptr = &mf6ctable[i];
1191 while ((mfc = *nptr) != NULL) {
1192 rte = mfc->mf6c_stall;
1194 * Skip real cache entries
1195 * Make sure it wasn't marked to not expire (shouldn't happen)
1199 mfc->mf6c_expire != 0 &&
1200 --mfc->mf6c_expire == 0) {
1202 if (mrt6debug & DEBUG_EXPIRE)
1203 log(LOG_DEBUG, "expire_upcalls: expiring (%s %s)\n",
1204 ip6_sprintf(&mfc->mf6c_origin.sin6_addr),
1205 ip6_sprintf(&mfc->mf6c_mcastgrp.sin6_addr));
1208 * drop all the packets
1209 * free the mbuf with the pkt, if, timing info
1212 struct rtdetq *n = rte->next;
1214 free(rte, M_MRTABLE);
1216 } while (rte != NULL);
1217 mrt6stat.mrt6s_cache_cleanups++;
1220 *nptr = mfc->mf6c_next;
1221 free(mfc, M_MRTABLE);
1223 nptr = &mfc->mf6c_next;
1228 callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT,
1229 expire_upcalls, NULL);
1233 * Packet forwarding routine once entry in the cache is made
1241 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1244 int plen = m->m_pkthdr.len;
1247 * Macro to send packet on mif. Since RSVP packets don't get counted on
1248 * input, they shouldn't get counted on output, so statistics keeping is
1252 #define MC6_SEND(ip6, mifp, m) do { \
1253 if ((mifp)->m6_flags & MIFF_REGISTER) \
1254 register_send((ip6), (mifp), (m)); \
1256 phyint_send((ip6), (mifp), (m)); \
1260 * Don't forward if it didn't arrive from the parent mif
1263 mifi = rt->mf6c_parent;
1264 if ((mifi >= nummifs) || (mif6table[mifi].m6_ifp != ifp)) {
1265 /* came in the wrong interface */
1267 if (mrt6debug & DEBUG_FORWARD)
1269 "wrong if: ifid %d mifi %d mififid %x\n",
1270 ifp->if_index, mifi,
1271 mif6table[mifi].m6_ifp->if_index);
1273 mrt6stat.mrt6s_wrong_if++;
1274 rt->mf6c_wrong_if++;
1276 * If we are doing PIM processing, and we are forwarding
1277 * packets on this interface, send a message to the
1280 /* have to make sure this is a valid mif */
1281 if (mifi < nummifs && mif6table[mifi].m6_ifp)
1282 if (pim6 && (m->m_flags & M_LOOP) == 0) {
1284 * Check the M_LOOP flag to avoid an
1285 * unnecessary PIM assert.
1286 * XXX: M_LOOP is an ad-hoc hack...
1288 static struct sockaddr_in6 sin6 =
1289 { sizeof(sin6), AF_INET6 };
1294 struct omrt6msg *oim;
1297 mm = m_copy(m, 0, sizeof(struct ip6_hdr));
1300 mm->m_len < sizeof(struct ip6_hdr)))
1301 mm = m_pullup(mm, sizeof(struct ip6_hdr));
1309 switch (ip6_mrouter_ver) {
1312 oim = mtod(mm, struct omrt6msg *);
1313 oim->im6_msgtype = MRT6MSG_WRONGMIF;
1318 im = mtod(mm, struct mrt6msg *);
1319 im->im6_msgtype = MRT6MSG_WRONGMIF;
1327 for (mifp = mif6table, iif = 0;
1328 iif < nummifs && mifp &&
1329 mifp->m6_ifp != ifp;
1333 switch (ip6_mrouter_ver) {
1337 sin6.sin6_addr = oim->im6_src;
1342 sin6.sin6_addr = im->im6_src;
1346 mrt6stat.mrt6s_upcalls++;
1348 if (socket_send(ip6_mrouter, mm, &sin6) < 0) {
1351 log(LOG_WARNING, "mdq, ip6_mrouter socket queue full\n");
1353 ++mrt6stat.mrt6s_upq_sockfull;
1355 } /* if socket Q full */
1358 } /* if wrong iif */
1360 /* If I sourced this packet, it counts as output, else it was input. */
1361 if (m->m_pkthdr.rcvif == NULL) {
1362 /* XXX: is rcvif really NULL when output?? */
1363 mif6table[mifi].m6_pkt_out++;
1364 mif6table[mifi].m6_bytes_out += plen;
1366 mif6table[mifi].m6_pkt_in++;
1367 mif6table[mifi].m6_bytes_in += plen;
1370 rt->mf6c_byte_cnt += plen;
1373 * For each mif, forward a copy of the packet if there are group
1374 * members downstream on the interface.
1376 for (mifp = mif6table, mifi = 0; mifi < nummifs; mifp++, mifi++)
1377 if (IF_ISSET(mifi, &rt->mf6c_ifset)) {
1379 * check if the outgoing packet is going to break
1381 * XXX For packets through PIM register tunnel
1382 * interface, we believe a routing daemon.
1384 if ((mif6table[rt->mf6c_parent].m6_flags &
1385 MIFF_REGISTER) == 0 &&
1386 (mif6table[mifi].m6_flags & MIFF_REGISTER) == 0 &&
1387 (in6_addr2scopeid(ifp, &ip6->ip6_dst) !=
1388 in6_addr2scopeid(mif6table[mifi].m6_ifp,
1390 in6_addr2scopeid(ifp, &ip6->ip6_src) !=
1391 in6_addr2scopeid(mif6table[mifi].m6_ifp,
1393 ip6stat.ip6s_badscope++;
1398 mifp->m6_bytes_out += plen;
1399 MC6_SEND(ip6, mifp, m);
1405 phyint_send(ip6, mifp, m)
1406 struct ip6_hdr *ip6;
1410 struct mbuf *mb_copy;
1411 struct ifnet *ifp = mifp->m6_ifp;
1413 int s = splnet(); /* needs to protect static "ro" below. */
1414 static struct route_in6 ro;
1415 struct in6_multi *in6m;
1416 struct sockaddr_in6 *dst6;
1419 * Make a new reference to the packet; make sure that
1420 * the IPv6 header is actually copied, not just referenced,
1421 * so that ip6_output() only scribbles on the copy.
1423 mb_copy = m_copy(m, 0, M_COPYALL);
1425 (M_HASCL(mb_copy) || mb_copy->m_len < sizeof(struct ip6_hdr)))
1426 mb_copy = m_pullup(mb_copy, sizeof(struct ip6_hdr));
1427 if (mb_copy == NULL) {
1431 /* set MCAST flag to the outgoing packet */
1432 mb_copy->m_flags |= M_MCAST;
1435 * If we sourced the packet, call ip6_output since we may devide
1436 * the packet into fragments when the packet is too big for the
1437 * outgoing interface.
1438 * Otherwise, we can simply send the packet to the interface
1441 if (m->m_pkthdr.rcvif == NULL) {
1442 struct ip6_moptions im6o;
1444 im6o.im6o_multicast_ifp = ifp;
1445 /* XXX: ip6_output will override ip6->ip6_hlim */
1446 im6o.im6o_multicast_hlim = ip6->ip6_hlim;
1447 im6o.im6o_multicast_loop = 1;
1448 error = ip6_output(mb_copy, NULL, &ro,
1449 IPV6_FORWARDING, &im6o, NULL, NULL);
1452 if (mrt6debug & DEBUG_XMIT)
1453 log(LOG_DEBUG, "phyint_send on mif %d err %d\n",
1454 mifp - mif6table, error);
1461 * If we belong to the destination multicast group
1462 * on the outgoing interface, loop back a copy.
1464 dst6 = (struct sockaddr_in6 *)&ro.ro_dst;
1465 IN6_LOOKUP_MULTI(ip6->ip6_dst, ifp, in6m);
1467 dst6->sin6_len = sizeof(struct sockaddr_in6);
1468 dst6->sin6_family = AF_INET6;
1469 dst6->sin6_addr = ip6->ip6_dst;
1470 ip6_mloopback(ifp, m, (struct sockaddr_in6 *)&ro.ro_dst);
1473 * Put the packet into the sending queue of the outgoing interface
1474 * if it would fit in the MTU of the interface.
1476 if (mb_copy->m_pkthdr.len <= ifp->if_mtu || ifp->if_mtu < IPV6_MMTU) {
1477 dst6->sin6_len = sizeof(struct sockaddr_in6);
1478 dst6->sin6_family = AF_INET6;
1479 dst6->sin6_addr = ip6->ip6_dst;
1481 * We just call if_output instead of nd6_output here, since
1482 * we need no ND for a multicast forwarded packet...right?
1484 error = (*ifp->if_output)(ifp, mb_copy,
1485 (struct sockaddr *)&ro.ro_dst, NULL);
1487 if (mrt6debug & DEBUG_XMIT)
1488 log(LOG_DEBUG, "phyint_send on mif %d err %d\n",
1489 mifp - mif6table, error);
1492 #ifdef MULTICAST_PMTUD
1493 icmp6_error(mb_copy, ICMP6_PACKET_TOO_BIG, 0, ifp->if_mtu);
1496 if (mrt6debug & DEBUG_XMIT)
1498 "phyint_send: packet too big on %s o %s g %s"
1499 " size %d(discarded)\n",
1501 ip6_sprintf(&ip6->ip6_src),
1502 ip6_sprintf(&ip6->ip6_dst),
1503 mb_copy->m_pkthdr.len);
1504 #endif /* MRT6DEBUG */
1505 m_freem(mb_copy); /* simply discard the packet */
1513 register_send(ip6, mif, m)
1514 struct ip6_hdr *ip6;
1519 int i, len = m->m_pkthdr.len;
1520 static struct sockaddr_in6 sin6 = { sizeof(sin6), AF_INET6 };
1521 struct mrt6msg *im6;
1525 log(LOG_DEBUG, "** IPv6 register_send **\n src %s dst %s\n",
1526 ip6_sprintf(&ip6->ip6_src), ip6_sprintf(&ip6->ip6_dst));
1528 ++pim6stat.pim6s_snd_registers;
1530 /* Make a copy of the packet to send to the user level process */
1531 MGETHDR(mm, M_DONTWAIT, MT_HEADER);
1534 mm->m_pkthdr.rcvif = NULL;
1535 mm->m_data += max_linkhdr;
1536 mm->m_len = sizeof(struct ip6_hdr);
1538 if ((mm->m_next = m_copy(m, 0, M_COPYALL)) == NULL) {
1542 i = MHLEN - M_LEADINGSPACE(mm);
1545 mm = m_pullup(mm, i);
1548 /* TODO: check it! */
1549 mm->m_pkthdr.len = len + sizeof(struct ip6_hdr);
1552 * Send message to routing daemon
1554 sin6.sin6_addr = ip6->ip6_src;
1556 im6 = mtod(mm, struct mrt6msg *);
1557 im6->im6_msgtype = MRT6MSG_WHOLEPKT;
1560 im6->im6_mif = mif - mif6table;
1562 /* iif info is not given for reg. encap.n */
1563 mrt6stat.mrt6s_upcalls++;
1565 if (socket_send(ip6_mrouter, mm, &sin6) < 0) {
1569 "register_send: ip6_mrouter socket queue full\n");
1571 ++mrt6stat.mrt6s_upq_sockfull;
1578 * PIM sparse mode hook
1579 * Receives the pim control messages, and passes them up to the listening
1580 * socket, using rip6_input.
1581 * The only message processed is the REGISTER pim message; the pim header
1582 * is stripped off, and the inner packet is passed to register_mforward.
1585 pim6_input(mp, offp, proto)
1589 struct pim *pim; /* pointer to a pim struct */
1590 struct ip6_hdr *ip6;
1592 struct mbuf *m = *mp;
1596 ++pim6stat.pim6s_rcv_total;
1598 ip6 = mtod(m, struct ip6_hdr *);
1599 pimlen = m->m_pkthdr.len - *offp;
1604 if (pimlen < PIM_MINLEN) {
1605 ++pim6stat.pim6s_rcv_tooshort;
1607 if (mrt6debug & DEBUG_PIM)
1608 log(LOG_DEBUG,"pim6_input: PIM packet too short\n");
1611 return(IPPROTO_DONE);
1615 * if the packet is at least as big as a REGISTER, go ahead
1616 * and grab the PIM REGISTER header size, to avoid another
1617 * possible m_pullup() later.
1619 * PIM_MINLEN == pimhdr + u_int32 == 8
1620 * PIM6_REG_MINLEN == pimhdr + reghdr + eip6hdr == 4 + 4 + 40
1622 minlen = (pimlen >= PIM6_REG_MINLEN) ? PIM6_REG_MINLEN : PIM_MINLEN;
1625 * Make sure that the IP6 and PIM headers in contiguous memory, and
1626 * possibly the PIM REGISTER header
1628 #ifndef PULLDOWN_TEST
1629 IP6_EXTHDR_CHECK(m, off, minlen, IPPROTO_DONE);
1630 /* adjust pointer */
1631 ip6 = mtod(m, struct ip6_hdr *);
1633 /* adjust mbuf to point to the PIM header */
1634 pim = (struct pim *)((caddr_t)ip6 + off);
1636 IP6_EXTHDR_GET(pim, struct pim *, m, off, minlen);
1638 pim6stat.pim6s_rcv_tooshort++;
1639 return IPPROTO_DONE;
1643 #define PIM6_CHECKSUM
1644 #ifdef PIM6_CHECKSUM
1649 * Validate checksum.
1650 * If PIM REGISTER, exclude the data packet
1652 if (pim->pim_type == PIM_REGISTER)
1653 cksumlen = PIM_MINLEN;
1657 if (in6_cksum(m, IPPROTO_PIM, off, cksumlen)) {
1658 ++pim6stat.pim6s_rcv_badsum;
1660 if (mrt6debug & DEBUG_PIM)
1662 "pim6_input: invalid checksum\n");
1665 return(IPPROTO_DONE);
1668 #endif /* PIM_CHECKSUM */
1670 /* PIM version check */
1671 if (pim->pim_ver != PIM_VERSION) {
1672 ++pim6stat.pim6s_rcv_badversion;
1675 "pim6_input: incorrect version %d, expecting %d\n",
1676 pim->pim_ver, PIM_VERSION);
1679 return(IPPROTO_DONE);
1682 if (pim->pim_type == PIM_REGISTER) {
1684 * since this is a REGISTER, we'll make a copy of the register
1685 * headers ip6+pim+u_int32_t+encap_ip6, to be passed up to the
1688 static struct sockaddr_in6 dst = { sizeof(dst), AF_INET6 };
1691 struct ip6_hdr *eip6;
1695 ++pim6stat.pim6s_rcv_registers;
1697 if ((reg_mif_num >= nummifs) || (reg_mif_num == (mifi_t) -1)) {
1699 if (mrt6debug & DEBUG_PIM)
1701 "pim6_input: register mif not set: %d\n",
1705 return(IPPROTO_DONE);
1708 reghdr = (u_int32_t *)(pim + 1);
1710 if ((ntohl(*reghdr) & PIM_NULL_REGISTER))
1711 goto pim6_input_to_daemon;
1716 if (pimlen < PIM6_REG_MINLEN) {
1717 ++pim6stat.pim6s_rcv_tooshort;
1718 ++pim6stat.pim6s_rcv_badregisters;
1721 "pim6_input: register packet size too "
1722 "small %d from %s\n",
1723 pimlen, ip6_sprintf(&ip6->ip6_src));
1726 return(IPPROTO_DONE);
1729 eip6 = (struct ip6_hdr *) (reghdr + 1);
1731 if (mrt6debug & DEBUG_PIM)
1733 "pim6_input[register], eip6: %s -> %s, "
1735 ip6_sprintf(&eip6->ip6_src),
1736 ip6_sprintf(&eip6->ip6_dst),
1737 ntohs(eip6->ip6_plen));
1740 /* verify the version number of the inner packet */
1741 if ((eip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
1742 ++pim6stat.pim6s_rcv_badregisters;
1744 log(LOG_DEBUG, "pim6_input: invalid IP version (%d) "
1745 "of the inner packet\n",
1746 (eip6->ip6_vfc & IPV6_VERSION));
1749 return(IPPROTO_NONE);
1752 /* verify the inner packet is destined to a mcast group */
1753 if (!IN6_IS_ADDR_MULTICAST(&eip6->ip6_dst)) {
1754 ++pim6stat.pim6s_rcv_badregisters;
1756 if (mrt6debug & DEBUG_PIM)
1758 "pim6_input: inner packet of register "
1759 "is not multicast %s\n",
1760 ip6_sprintf(&eip6->ip6_dst));
1763 return(IPPROTO_DONE);
1767 * make a copy of the whole header to pass to the daemon later.
1769 mcp = m_copy(m, 0, off + PIM6_REG_MINLEN);
1773 "pim6_input: pim register: "
1774 "could not copy register head\n");
1777 return(IPPROTO_DONE);
1781 * forward the inner ip6 packet; point m_data at the inner ip6.
1783 m_adj(m, off + PIM_MINLEN);
1785 if (mrt6debug & DEBUG_PIM) {
1787 "pim6_input: forwarding decapsulated register: "
1788 "src %s, dst %s, mif %d\n",
1789 ip6_sprintf(&eip6->ip6_src),
1790 ip6_sprintf(&eip6->ip6_dst),
1795 rc = if_simloop(mif6table[reg_mif_num].m6_ifp, m,
1796 dst.sin6_family, NULL);
1798 /* prepare the register head to send to the mrouting daemon */
1803 * Pass the PIM message up to the daemon; if it is a register message
1804 * pass the 'head' only up to the daemon. This includes the
1805 * encapsulator ip6 header, pim header, register header and the
1806 * encapsulated ip6 header.
1808 pim6_input_to_daemon:
1809 rip6_input(&m, offp, proto);
1810 return(IPPROTO_DONE);