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.18 2008/06/21 12:30:19 aggelos 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>
65 #include <sys/thread2.h>
68 #include <net/route.h>
69 #include <net/raw_cb.h>
71 #include <netinet/in.h>
72 #include <netinet/in_var.h>
74 #include <netinet/ip6.h>
75 #include <netinet6/ip6_var.h>
76 #include <netinet6/ip6_mroute.h>
77 #include <netinet6/pim6.h>
78 #include <netinet6/pim6_var.h>
80 #include <net/net_osdep.h>
82 static MALLOC_DEFINE(M_MRTABLE, "mf6c", "multicast forwarding cache entry");
84 #define M_HASCL(m) ((m)->m_flags & M_EXT)
86 static int ip6_mdq (struct mbuf *, struct ifnet *, struct mf6c *);
87 static void phyint_send (struct ip6_hdr *, struct mif6 *, struct mbuf *);
89 static int set_pim6 (int *);
90 static int socket_send (struct socket *, struct mbuf *,
91 struct sockaddr_in6 *);
92 static int register_send (struct ip6_hdr *, struct mif6 *,
96 * Globals. All but ip6_mrouter, ip6_mrtproto and mrt6stat could be static,
97 * except for netstat or debugging purposes.
99 struct socket *ip6_mrouter = NULL;
100 int ip6_mrouter_ver = 0;
101 int ip6_mrtproto = IPPROTO_PIM; /* for netstat only */
102 struct mrt6stat mrt6stat;
104 #define NO_RTE_FOUND 0x1
105 #define RTE_FOUND 0x2
107 struct mf6c *mf6ctable[MF6CTBLSIZ];
108 u_char n6expire[MF6CTBLSIZ];
109 static struct mif6 mif6table[MAXMIFS];
111 u_int mrt6debug = 0; /* debug level */
112 #define DEBUG_MFC 0x02
113 #define DEBUG_FORWARD 0x04
114 #define DEBUG_EXPIRE 0x08
115 #define DEBUG_XMIT 0x10
116 #define DEBUG_REG 0x20
117 #define DEBUG_PIM 0x40
120 static void expire_upcalls (void *);
121 #define EXPIRE_TIMEOUT (hz / 4) /* 4x / second */
122 #define UPCALL_EXPIRE 6 /* number of timeouts */
126 extern struct socket *ip_mrouter;
131 * 'Interfaces' associated with decapsulator (so we can tell
132 * packets that went through it from ones that get reflected
133 * by a broken gateway). These interfaces are never linked into
134 * the system ifnet list & no routes point to them. I.e., packets
135 * can't be sent this way. They only exist as a placeholder for
136 * multicast source verification.
138 struct ifnet multicast_register_if;
140 #define ENCAP_HOPS 64
145 static mifi_t nummifs = 0;
146 static mifi_t reg_mif_num = (mifi_t)-1;
148 static struct pim6stat pim6stat;
152 * Hash function for a source, group entry
154 #define MF6CHASH(a, g) MF6CHASHMOD((a).s6_addr32[0] ^ (a).s6_addr32[1] ^ \
155 (a).s6_addr32[2] ^ (a).s6_addr32[3] ^ \
156 (g).s6_addr32[0] ^ (g).s6_addr32[1] ^ \
157 (g).s6_addr32[2] ^ (g).s6_addr32[3])
160 * Find a route for a given origin IPv6 address and Multicast group address.
161 * Quality of service parameter to be added in the future!!!
164 #define MF6CFIND(o, g, rt) do { \
165 struct mf6c *_rt = mf6ctable[MF6CHASH(o,g)]; \
167 mrt6stat.mrt6s_mfc_lookups++; \
169 if (IN6_ARE_ADDR_EQUAL(&_rt->mf6c_origin.sin6_addr, &(o)) && \
170 IN6_ARE_ADDR_EQUAL(&_rt->mf6c_mcastgrp.sin6_addr, &(g)) && \
171 (_rt->mf6c_stall == NULL)) { \
175 _rt = _rt->mf6c_next; \
178 mrt6stat.mrt6s_mfc_misses++; \
183 * Macros to compute elapsed time efficiently
184 * Borrowed from Van Jacobson's scheduling code
186 #define TV_DELTA(a, b, delta) do { \
189 delta = (a).tv_usec - (b).tv_usec; \
190 if ((xxs = (a).tv_sec - (b).tv_sec)) { \
199 delta += (1000000 * xxs); \
204 #define TV_LT(a, b) (((a).tv_usec < (b).tv_usec && \
205 (a).tv_sec <= (b).tv_sec) || (a).tv_sec < (b).tv_sec)
208 #define UPCALL_MAX 50
209 u_long upcall_data[UPCALL_MAX + 1];
210 static void collate();
211 #endif /* UPCALL_TIMING */
213 static int get_sg_cnt (struct sioc_sg_req6 *);
214 static int get_mif6_cnt (struct sioc_mif_req6 *);
215 static int ip6_mrouter_init (struct socket *, struct mbuf *, int);
216 static int add_m6if (struct mif6ctl *);
217 static int del_m6if (mifi_t *);
218 static int add_m6fc (struct mf6cctl *);
219 static int del_m6fc (struct mf6cctl *);
221 static struct callout expire_upcalls_ch;
224 * Handle MRT setsockopt commands to modify the multicast routing tables.
227 ip6_mrouter_set(struct socket *so, struct sockopt *sopt)
232 if (so != ip6_mrouter && sopt->sopt_name != MRT6_INIT)
235 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
237 soopt_to_mbuf(sopt, m); /* XXX */
239 switch (sopt->sopt_name) {
244 error = ip6_mrouter_init(so, m, sopt->sopt_name);
247 error = ip6_mrouter_done();
250 error = add_m6if(mtod(m, struct mif6ctl *));
253 error = del_m6if(mtod(m, mifi_t *));
256 error = add_m6fc(mtod(m, struct mf6cctl *));
259 error = del_m6fc(mtod(m, struct mf6cctl *));
262 error = set_pim6(mtod(m, int *));
274 * Handle MRT getsockopt commands
277 ip6_mrouter_get(struct socket *so, struct sockopt *sopt)
281 if (so != ip6_mrouter) return EACCES;
283 switch (sopt->sopt_name) {
285 soopt_from_kbuf(sopt, &pim6, sizeof(pim6));
292 * Handle ioctl commands to obtain information from the cache
295 mrt6_ioctl(int cmd, caddr_t data)
300 case SIOCGETSGCNT_IN6:
301 return (get_sg_cnt((struct sioc_sg_req6 *)data));
302 break; /* for safety */
303 case SIOCGETMIFCNT_IN6:
304 return (get_mif6_cnt((struct sioc_mif_req6 *)data));
305 break; /* for safety */
314 * returns the packet, byte, rpf-failure count for the source group provided
317 get_sg_cnt(struct sioc_sg_req6 *req)
322 MF6CFIND(req->src.sin6_addr, req->grp.sin6_addr, rt);
325 req->pktcnt = rt->mf6c_pkt_cnt;
326 req->bytecnt = rt->mf6c_byte_cnt;
327 req->wrong_if = rt->mf6c_wrong_if;
331 req->pktcnt = req->bytecnt = req->wrong_if = 0xffffffff;
338 * returns the input and output packet and byte counts on the mif provided
341 get_mif6_cnt(struct sioc_mif_req6 *req)
343 mifi_t mifi = req->mifi;
348 req->icount = mif6table[mifi].m6_pkt_in;
349 req->ocount = mif6table[mifi].m6_pkt_out;
350 req->ibytes = mif6table[mifi].m6_bytes_in;
351 req->obytes = mif6table[mifi].m6_bytes_out;
359 if ((*i != 1) && (*i != 0))
368 * Enable multicast routing
371 ip6_mrouter_init(struct socket *so, struct mbuf *m, int cmd)
378 "ip6_mrouter_init: so_type = %d, pr_protocol = %d\n",
379 so->so_type, so->so_proto->pr_protocol);
382 if (so->so_type != SOCK_RAW ||
383 so->so_proto->pr_protocol != IPPROTO_ICMPV6)
386 if (!m || (m->m_len != sizeof(int *)))
393 if (ip6_mrouter != NULL) return EADDRINUSE;
396 ip6_mrouter_ver = cmd;
398 bzero((caddr_t)mf6ctable, sizeof(mf6ctable));
399 bzero((caddr_t)n6expire, sizeof(n6expire));
401 pim6 = 0;/* used for stubbing out/in pim stuff */
403 callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT,
404 expire_upcalls, NULL);
408 log(LOG_DEBUG, "ip6_mrouter_init\n");
415 * Disable multicast routing
418 ip6_mrouter_done(void)
423 struct in6_ifreq ifr;
428 * For each phyint in use, disable promiscuous reception of all IPv6
434 * If there is still IPv4 multicast routing daemon,
435 * we remain interfaces to receive all muliticasted packets.
436 * XXX: there may be an interface in which the IPv4 multicast
437 * daemon is not interested...
443 for (mifi = 0; mifi < nummifs; mifi++) {
444 if (mif6table[mifi].m6_ifp &&
445 !(mif6table[mifi].m6_flags & MIFF_REGISTER)) {
446 ifr.ifr_addr.sin6_family = AF_INET6;
447 ifr.ifr_addr.sin6_addr= kin6addr_any;
448 ifp = mif6table[mifi].m6_ifp;
449 ifnet_serialize_all(ifp);
450 ifp->if_ioctl(ifp, SIOCDELMULTI,
451 (caddr_t)&ifr, NULL);
452 ifnet_deserialize_all(ifp);
457 bzero((caddr_t)qtable, sizeof(qtable));
458 bzero((caddr_t)tbftable, sizeof(tbftable));
460 bzero((caddr_t)mif6table, sizeof(mif6table));
463 pim6 = 0; /* used to stub out/in pim specific code */
465 callout_stop(&expire_upcalls_ch);
468 * Free all multicast forwarding cache entries.
470 for (i = 0; i < MF6CTBLSIZ; i++) {
475 for (rte = rt->mf6c_stall; rte != NULL; ) {
476 struct rtdetq *n = rte->next;
479 kfree(rte, M_MRTABLE);
484 kfree(frt, M_MRTABLE);
488 bzero((caddr_t)mf6ctable, sizeof(mf6ctable));
491 * Reset de-encapsulation cache
500 log(LOG_DEBUG, "ip6_mrouter_done\n");
506 static struct sockaddr_in6 sin6 = { sizeof(sin6), AF_INET6 };
509 * Add a mif to the mif table
512 add_m6if(struct mif6ctl *mifcp)
518 struct tbf *m_tbf = tbftable + mifcp->mif6c_mifi;
521 if (mifcp->mif6c_mifi >= MAXMIFS)
523 mifp = mif6table + mifcp->mif6c_mifi;
525 return EADDRINUSE; /* XXX: is it appropriate? */
526 if (mifcp->mif6c_pifi == 0 || mifcp->mif6c_pifi > if_index)
528 ifp = ifindex2ifnet[mifcp->mif6c_pifi];
530 if (mifcp->mif6c_flags & MIFF_REGISTER) {
531 if (reg_mif_num == (mifi_t)-1) {
532 strlcpy(multicast_register_if.if_xname, "register_mif",
534 multicast_register_if.if_flags |= IFF_LOOPBACK;
535 multicast_register_if.if_index = mifcp->mif6c_mifi;
536 reg_mif_num = mifcp->mif6c_mifi;
539 ifp = &multicast_register_if;
543 /* Make sure the interface supports multicast */
544 if ((ifp->if_flags & IFF_MULTICAST) == 0)
548 error = if_allmulti(ifp, 1);
555 mifp->m6_flags = mifcp->mif6c_flags;
558 /* scaling up here allows division by 1024 in critical code */
559 mifp->m6_rate_limit = mifcp->mif6c_rate_limit * 1024 / 1000;
561 /* initialize per mif pkt counters */
563 mifp->m6_pkt_out = 0;
564 mifp->m6_bytes_in = 0;
565 mifp->m6_bytes_out = 0;
568 /* Adjust nummifs up if the mifi is higher than nummifs */
569 if (nummifs <= mifcp->mif6c_mifi)
570 nummifs = mifcp->mif6c_mifi + 1;
575 "add_mif #%d, phyint %s\n",
584 * Delete a mif from the mif table
587 del_m6if(mifi_t *mifip)
589 struct mif6 *mifp = mif6table + *mifip;
593 if (*mifip >= nummifs)
595 if (mifp->m6_ifp == NULL)
600 if (!(mifp->m6_flags & MIFF_REGISTER)) {
602 * XXX: what if there is yet IPv4 multicast daemon
603 * using the interface?
611 bzero((caddr_t)qtable[*mifip], sizeof(qtable[*mifip]));
612 bzero((caddr_t)mifp->m6_tbf, sizeof(*(mifp->m6_tbf)));
614 bzero((caddr_t)mifp, sizeof (*mifp));
616 /* Adjust nummifs down */
617 for (mifi = nummifs; mifi > 0; mifi--)
618 if (mif6table[mifi - 1].m6_ifp)
626 log(LOG_DEBUG, "del_m6if %d, nummifs %d\n", *mifip, nummifs);
636 add_m6fc(struct mf6cctl *mfccp)
643 MF6CFIND(mfccp->mf6cc_origin.sin6_addr,
644 mfccp->mf6cc_mcastgrp.sin6_addr, rt);
646 /* If an entry already exists, just update the fields */
649 if (mrt6debug & DEBUG_MFC)
651 "add_m6fc no upcall h %d o %s g %s p %x\n",
652 ip6_sprintf(&mfccp->mf6cc_origin.sin6_addr),
653 ip6_sprintf(&mfccp->mf6cc_mcastgrp.sin6_addr),
654 mfccp->mf6cc_parent);
658 rt->mf6c_parent = mfccp->mf6cc_parent;
659 rt->mf6c_ifset = mfccp->mf6cc_ifset;
665 * Find the entry for which the upcall was made and update
668 hash = MF6CHASH(mfccp->mf6cc_origin.sin6_addr,
669 mfccp->mf6cc_mcastgrp.sin6_addr);
670 for (rt = mf6ctable[hash], nstl = 0; rt; rt = rt->mf6c_next) {
671 if (IN6_ARE_ADDR_EQUAL(&rt->mf6c_origin.sin6_addr,
672 &mfccp->mf6cc_origin.sin6_addr) &&
673 IN6_ARE_ADDR_EQUAL(&rt->mf6c_mcastgrp.sin6_addr,
674 &mfccp->mf6cc_mcastgrp.sin6_addr) &&
675 (rt->mf6c_stall != NULL)) {
679 "add_m6fc: %s o %s g %s p %x dbx %p\n",
680 "multiple kernel entries",
681 ip6_sprintf(&mfccp->mf6cc_origin.sin6_addr),
682 ip6_sprintf(&mfccp->mf6cc_mcastgrp.sin6_addr),
683 mfccp->mf6cc_parent, rt->mf6c_stall);
686 if (mrt6debug & DEBUG_MFC)
688 "add_m6fc o %s g %s p %x dbg %x\n",
689 ip6_sprintf(&mfccp->mf6cc_origin.sin6_addr),
690 ip6_sprintf(&mfccp->mf6cc_mcastgrp.sin6_addr),
691 mfccp->mf6cc_parent, rt->mf6c_stall);
694 rt->mf6c_origin = mfccp->mf6cc_origin;
695 rt->mf6c_mcastgrp = mfccp->mf6cc_mcastgrp;
696 rt->mf6c_parent = mfccp->mf6cc_parent;
697 rt->mf6c_ifset = mfccp->mf6cc_ifset;
698 /* initialize pkt counters per src-grp */
699 rt->mf6c_pkt_cnt = 0;
700 rt->mf6c_byte_cnt = 0;
701 rt->mf6c_wrong_if = 0;
703 rt->mf6c_expire = 0; /* Don't clean this guy up */
706 /* free packets Qed at the end of this entry */
707 for (rte = rt->mf6c_stall; rte != NULL; ) {
708 struct rtdetq *n = rte->next;
709 ip6_mdq(rte->m, rte->ifp, rt);
713 #endif /* UPCALL_TIMING */
714 kfree(rte, M_MRTABLE);
717 rt->mf6c_stall = NULL;
722 * It is possible that an entry is being inserted without an upcall
726 if (mrt6debug & DEBUG_MFC)
727 log(LOG_DEBUG,"add_mfc no upcall h %d o %s g %s p %x\n",
729 ip6_sprintf(&mfccp->mf6cc_origin.sin6_addr),
730 ip6_sprintf(&mfccp->mf6cc_mcastgrp.sin6_addr),
731 mfccp->mf6cc_parent);
734 for (rt = mf6ctable[hash]; rt; rt = rt->mf6c_next) {
736 if (IN6_ARE_ADDR_EQUAL(&rt->mf6c_origin.sin6_addr,
737 &mfccp->mf6cc_origin.sin6_addr)&&
738 IN6_ARE_ADDR_EQUAL(&rt->mf6c_mcastgrp.sin6_addr,
739 &mfccp->mf6cc_mcastgrp.sin6_addr)) {
741 rt->mf6c_origin = mfccp->mf6cc_origin;
742 rt->mf6c_mcastgrp = mfccp->mf6cc_mcastgrp;
743 rt->mf6c_parent = mfccp->mf6cc_parent;
744 rt->mf6c_ifset = mfccp->mf6cc_ifset;
745 /* initialize pkt counters per src-grp */
746 rt->mf6c_pkt_cnt = 0;
747 rt->mf6c_byte_cnt = 0;
748 rt->mf6c_wrong_if = 0;
756 /* no upcall, so make a new entry */
757 rt = (struct mf6c *)kmalloc(sizeof(*rt), M_MRTABLE,
764 /* insert new entry at head of hash chain */
765 rt->mf6c_origin = mfccp->mf6cc_origin;
766 rt->mf6c_mcastgrp = mfccp->mf6cc_mcastgrp;
767 rt->mf6c_parent = mfccp->mf6cc_parent;
768 rt->mf6c_ifset = mfccp->mf6cc_ifset;
769 /* initialize pkt counters per src-grp */
770 rt->mf6c_pkt_cnt = 0;
771 rt->mf6c_byte_cnt = 0;
772 rt->mf6c_wrong_if = 0;
774 rt->mf6c_stall = NULL;
776 /* link into table */
777 rt->mf6c_next = mf6ctable[hash];
778 mf6ctable[hash] = rt;
787 * collect delay statistics on the upcalls
790 collate(struct timeval *t)
800 TV_DELTA(tp, *t, delta);
809 #endif /* UPCALL_TIMING */
812 * Delete an mfc entry
815 del_m6fc(struct mf6cctl *mfccp)
817 struct sockaddr_in6 origin;
818 struct sockaddr_in6 mcastgrp;
823 origin = mfccp->mf6cc_origin;
824 mcastgrp = mfccp->mf6cc_mcastgrp;
825 hash = MF6CHASH(origin.sin6_addr, mcastgrp.sin6_addr);
828 if (mrt6debug & DEBUG_MFC)
829 log(LOG_DEBUG,"del_m6fc orig %s mcastgrp %s\n",
830 ip6_sprintf(&origin.sin6_addr),
831 ip6_sprintf(&mcastgrp.sin6_addr));
836 nptr = &mf6ctable[hash];
837 while ((rt = *nptr) != NULL) {
838 if (IN6_ARE_ADDR_EQUAL(&origin.sin6_addr,
839 &rt->mf6c_origin.sin6_addr) &&
840 IN6_ARE_ADDR_EQUAL(&mcastgrp.sin6_addr,
841 &rt->mf6c_mcastgrp.sin6_addr) &&
842 rt->mf6c_stall == NULL)
845 nptr = &rt->mf6c_next;
849 return EADDRNOTAVAIL;
852 *nptr = rt->mf6c_next;
853 kfree(rt, M_MRTABLE);
861 socket_send(struct socket *so, struct mbuf *mm, struct sockaddr_in6 *src)
864 lwkt_gettoken(&so->so_rcv.ssb_token);
865 if (ssb_appendaddr(&so->so_rcv,
866 (struct sockaddr *)src,
869 lwkt_reltoken(&so->so_rcv.ssb_token);
872 lwkt_reltoken(&so->so_rcv.ssb_token);
879 * IPv6 multicast forwarding function. This function assumes that the packet
880 * pointed to by "ip6" has arrived on (or is about to be sent to) the interface
881 * pointed to by "ifp", and the packet is to be relayed to other networks
882 * that have members of the packet's destination IPv6 multicast group.
884 * The packet is returned unscathed to the caller, unless it is
885 * erroneous, in which case a non-zero return value tells the caller to
890 ip6_mforward(struct ip6_hdr *ip6, struct ifnet *ifp, struct mbuf *m)
898 if (mrt6debug & DEBUG_FORWARD)
899 log(LOG_DEBUG, "ip6_mforward: src %s, dst %s, ifindex %d\n",
900 ip6_sprintf(&ip6->ip6_src), ip6_sprintf(&ip6->ip6_dst),
905 * Don't forward a packet with Hop limit of zero or one,
906 * or a packet destined to a local-only group.
908 if (ip6->ip6_hlim <= 1 || IN6_IS_ADDR_MC_NODELOCAL(&ip6->ip6_dst) ||
909 IN6_IS_ADDR_MC_LINKLOCAL(&ip6->ip6_dst))
914 * Source address check: do not forward packets with unspecified
915 * source. It was discussed in July 2000, on ipngwg mailing list.
916 * This is rather more serious than unicast cases, because some
917 * MLD packets can be sent with the unspecified source address
918 * (although such packets must normally set 1 to the hop limit field).
920 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
921 ip6stat.ip6s_cantforward++;
922 if (ip6_log_time + ip6_log_interval < time_second) {
923 ip6_log_time = time_second;
926 "from %s to %s nxt %d received on %s\n",
927 ip6_sprintf(&ip6->ip6_src),
928 ip6_sprintf(&ip6->ip6_dst),
930 if_name(m->m_pkthdr.rcvif));
936 * Determine forwarding mifs from the forwarding cache table
939 MF6CFIND(ip6->ip6_src, ip6->ip6_dst, rt);
941 /* Entry exists, so forward if necessary */
944 return (ip6_mdq(m, ifp, rt));
947 * If we don't have a route for packet's origin,
948 * Make a copy of the packet &
949 * send message to routing daemon
960 #endif /* UPCALL_TIMING */
962 mrt6stat.mrt6s_no_route++;
964 if (mrt6debug & (DEBUG_FORWARD | DEBUG_MFC))
965 log(LOG_DEBUG, "ip6_mforward: no rte s %s g %s\n",
966 ip6_sprintf(&ip6->ip6_src),
967 ip6_sprintf(&ip6->ip6_dst));
971 * Allocate mbufs early so that we don't do extra work if we
972 * are just going to fail anyway.
974 rte = (struct rtdetq *)kmalloc(sizeof(*rte), M_MRTABLE,
980 mb0 = m_copy(m, 0, M_COPYALL);
982 * Pullup packet header if needed before storing it,
983 * as other references may modify it in the meantime.
986 (M_HASCL(mb0) || mb0->m_len < sizeof(struct ip6_hdr)))
987 mb0 = m_pullup(mb0, sizeof(struct ip6_hdr));
989 kfree(rte, M_MRTABLE);
994 /* is there an upcall waiting for this packet? */
995 hash = MF6CHASH(ip6->ip6_src, ip6->ip6_dst);
996 for (rt = mf6ctable[hash]; rt; rt = rt->mf6c_next) {
997 if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_src,
998 &rt->mf6c_origin.sin6_addr) &&
999 IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
1000 &rt->mf6c_mcastgrp.sin6_addr) &&
1001 (rt->mf6c_stall != NULL))
1008 struct omrt6msg *oim;
1011 /* no upcall, so make a new entry */
1012 rt = (struct mf6c *)kmalloc(sizeof(*rt), M_MRTABLE,
1015 kfree(rte, M_MRTABLE);
1021 * Make a copy of the header to send to the user
1024 mm = m_copy(mb0, 0, sizeof(struct ip6_hdr));
1027 kfree(rte, M_MRTABLE);
1029 kfree(rt, M_MRTABLE);
1035 * Send message to routing daemon
1037 sin6.sin6_addr = ip6->ip6_src;
1043 switch (ip6_mrouter_ver) {
1046 oim = mtod(mm, struct omrt6msg *);
1047 oim->im6_msgtype = MRT6MSG_NOCACHE;
1052 im = mtod(mm, struct mrt6msg *);
1053 im->im6_msgtype = MRT6MSG_NOCACHE;
1057 kfree(rte, M_MRTABLE);
1059 kfree(rt, M_MRTABLE);
1065 if (mrt6debug & DEBUG_FORWARD)
1067 "getting the iif info in the kernel\n");
1070 for (mifp = mif6table, mifi = 0;
1071 mifi < nummifs && mifp->m6_ifp != ifp;
1075 switch (ip6_mrouter_ver) {
1078 oim->im6_mif = mifi;
1086 if (socket_send(ip6_mrouter, mm, &sin6) < 0) {
1087 log(LOG_WARNING, "ip6_mforward: ip6_mrouter "
1088 "socket queue full\n");
1089 mrt6stat.mrt6s_upq_sockfull++;
1090 kfree(rte, M_MRTABLE);
1092 kfree(rt, M_MRTABLE);
1097 mrt6stat.mrt6s_upcalls++;
1099 /* insert new entry at head of hash chain */
1100 bzero(rt, sizeof(*rt));
1101 rt->mf6c_origin.sin6_family = AF_INET6;
1102 rt->mf6c_origin.sin6_len = sizeof(struct sockaddr_in6);
1103 rt->mf6c_origin.sin6_addr = ip6->ip6_src;
1104 rt->mf6c_mcastgrp.sin6_family = AF_INET6;
1105 rt->mf6c_mcastgrp.sin6_len = sizeof(struct sockaddr_in6);
1106 rt->mf6c_mcastgrp.sin6_addr = ip6->ip6_dst;
1107 rt->mf6c_expire = UPCALL_EXPIRE;
1109 rt->mf6c_parent = MF6C_INCOMPLETE_PARENT;
1111 /* link into table */
1112 rt->mf6c_next = mf6ctable[hash];
1113 mf6ctable[hash] = rt;
1114 /* Add this entry to the end of the queue */
1115 rt->mf6c_stall = rte;
1117 /* determine if q has overflowed */
1121 for (p = &rt->mf6c_stall; *p != NULL; p = &(*p)->next)
1122 if (++npkts > MAX_UPQ6) {
1123 mrt6stat.mrt6s_upq_ovflw++;
1124 kfree(rte, M_MRTABLE);
1130 /* Add this entry to the end of the queue */
1137 #ifdef UPCALL_TIMING
1139 #endif /* UPCALL_TIMING */
1148 * Clean up cache entries if upcalls are not serviced
1149 * Call from the Slow Timeout mechanism, every half second.
1152 expire_upcalls(void *unused)
1155 struct mf6c *mfc, **nptr;
1159 for (i = 0; i < MF6CTBLSIZ; i++) {
1160 if (n6expire[i] == 0)
1162 nptr = &mf6ctable[i];
1163 while ((mfc = *nptr) != NULL) {
1164 rte = mfc->mf6c_stall;
1166 * Skip real cache entries
1167 * Make sure it wasn't marked to not expire (shouldn't happen)
1171 mfc->mf6c_expire != 0 &&
1172 --mfc->mf6c_expire == 0) {
1174 if (mrt6debug & DEBUG_EXPIRE)
1175 log(LOG_DEBUG, "expire_upcalls: expiring (%s %s)\n",
1176 ip6_sprintf(&mfc->mf6c_origin.sin6_addr),
1177 ip6_sprintf(&mfc->mf6c_mcastgrp.sin6_addr));
1180 * drop all the packets
1181 * free the mbuf with the pkt, if, timing info
1184 struct rtdetq *n = rte->next;
1186 kfree(rte, M_MRTABLE);
1188 } while (rte != NULL);
1189 mrt6stat.mrt6s_cache_cleanups++;
1192 *nptr = mfc->mf6c_next;
1193 kfree(mfc, M_MRTABLE);
1195 nptr = &mfc->mf6c_next;
1200 callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT,
1201 expire_upcalls, NULL);
1205 * Packet forwarding routine once entry in the cache is made
1208 ip6_mdq(struct mbuf *m, struct ifnet *ifp, struct mf6c *rt)
1210 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1213 int plen = m->m_pkthdr.len;
1216 * Macro to send packet on mif. Since RSVP packets don't get counted on
1217 * input, they shouldn't get counted on output, so statistics keeping is
1221 #define MC6_SEND(ip6, mifp, m) do { \
1222 if ((mifp)->m6_flags & MIFF_REGISTER) \
1223 register_send((ip6), (mifp), (m)); \
1225 phyint_send((ip6), (mifp), (m)); \
1229 * Don't forward if it didn't arrive from the parent mif
1232 mifi = rt->mf6c_parent;
1233 if ((mifi >= nummifs) || (mif6table[mifi].m6_ifp != ifp)) {
1234 /* came in the wrong interface */
1236 if (mrt6debug & DEBUG_FORWARD)
1238 "wrong if: ifid %d mifi %d mififid %x\n",
1239 ifp->if_index, mifi,
1240 mif6table[mifi].m6_ifp->if_index);
1242 mrt6stat.mrt6s_wrong_if++;
1243 rt->mf6c_wrong_if++;
1245 * If we are doing PIM processing, and we are forwarding
1246 * packets on this interface, send a message to the
1249 /* have to make sure this is a valid mif */
1250 if (mifi < nummifs && mif6table[mifi].m6_ifp)
1251 if (pim6 && (m->m_flags & M_LOOP) == 0) {
1253 * Check the M_LOOP flag to avoid an
1254 * unnecessary PIM assert.
1255 * XXX: M_LOOP is an ad-hoc hack...
1257 static struct sockaddr_in6 sin6 =
1258 { sizeof(sin6), AF_INET6 };
1263 struct omrt6msg *oim;
1266 mm = m_copy(m, 0, sizeof(struct ip6_hdr));
1269 mm->m_len < sizeof(struct ip6_hdr)))
1270 mm = m_pullup(mm, sizeof(struct ip6_hdr));
1278 switch (ip6_mrouter_ver) {
1281 oim = mtod(mm, struct omrt6msg *);
1282 oim->im6_msgtype = MRT6MSG_WRONGMIF;
1287 im = mtod(mm, struct mrt6msg *);
1288 im->im6_msgtype = MRT6MSG_WRONGMIF;
1296 for (mifp = mif6table, iif = 0;
1297 iif < nummifs && mifp &&
1298 mifp->m6_ifp != ifp;
1302 switch (ip6_mrouter_ver) {
1306 sin6.sin6_addr = oim->im6_src;
1311 sin6.sin6_addr = im->im6_src;
1315 mrt6stat.mrt6s_upcalls++;
1317 if (socket_send(ip6_mrouter, mm, &sin6) < 0) {
1320 log(LOG_WARNING, "mdq, ip6_mrouter socket queue full\n");
1322 ++mrt6stat.mrt6s_upq_sockfull;
1324 } /* if socket Q full */
1327 } /* if wrong iif */
1329 /* If I sourced this packet, it counts as output, else it was input. */
1330 if (m->m_pkthdr.rcvif == NULL) {
1331 /* XXX: is rcvif really NULL when output?? */
1332 mif6table[mifi].m6_pkt_out++;
1333 mif6table[mifi].m6_bytes_out += plen;
1335 mif6table[mifi].m6_pkt_in++;
1336 mif6table[mifi].m6_bytes_in += plen;
1339 rt->mf6c_byte_cnt += plen;
1342 * For each mif, forward a copy of the packet if there are group
1343 * members downstream on the interface.
1345 for (mifp = mif6table, mifi = 0; mifi < nummifs; mifp++, mifi++)
1346 if (IF_ISSET(mifi, &rt->mf6c_ifset)) {
1348 * check if the outgoing packet is going to break
1350 * XXX For packets through PIM register tunnel
1351 * interface, we believe a routing daemon.
1353 if ((mif6table[rt->mf6c_parent].m6_flags &
1354 MIFF_REGISTER) == 0 &&
1355 (mif6table[mifi].m6_flags & MIFF_REGISTER) == 0 &&
1356 (in6_addr2scopeid(ifp, &ip6->ip6_dst) !=
1357 in6_addr2scopeid(mif6table[mifi].m6_ifp,
1359 in6_addr2scopeid(ifp, &ip6->ip6_src) !=
1360 in6_addr2scopeid(mif6table[mifi].m6_ifp,
1362 ip6stat.ip6s_badscope++;
1367 mifp->m6_bytes_out += plen;
1368 MC6_SEND(ip6, mifp, m);
1374 phyint_send(struct ip6_hdr *ip6, struct mif6 *mifp, struct mbuf *m)
1376 struct mbuf *mb_copy;
1377 struct ifnet *ifp = mifp->m6_ifp;
1379 static struct route_in6 ro;
1380 struct in6_multi *in6m;
1381 struct sockaddr_in6 *dst6;
1383 crit_enter(); /* needs to protect static "ro" below. */
1386 * Make a new reference to the packet; make sure that
1387 * the IPv6 header is actually copied, not just referenced,
1388 * so that ip6_output() only scribbles on the copy.
1390 mb_copy = m_copy(m, 0, M_COPYALL);
1392 (M_HASCL(mb_copy) || mb_copy->m_len < sizeof(struct ip6_hdr)))
1393 mb_copy = m_pullup(mb_copy, sizeof(struct ip6_hdr));
1394 if (mb_copy == NULL) {
1398 /* set MCAST flag to the outgoing packet */
1399 mb_copy->m_flags |= M_MCAST;
1402 * If we sourced the packet, call ip6_output since we may devide
1403 * the packet into fragments when the packet is too big for the
1404 * outgoing interface.
1405 * Otherwise, we can simply send the packet to the interface
1408 if (m->m_pkthdr.rcvif == NULL) {
1409 struct ip6_moptions im6o;
1411 im6o.im6o_multicast_ifp = ifp;
1412 /* XXX: ip6_output will override ip6->ip6_hlim */
1413 im6o.im6o_multicast_hlim = ip6->ip6_hlim;
1414 im6o.im6o_multicast_loop = 1;
1415 error = ip6_output(mb_copy, NULL, &ro,
1416 IPV6_FORWARDING, &im6o, NULL, NULL);
1419 if (mrt6debug & DEBUG_XMIT)
1420 log(LOG_DEBUG, "phyint_send on mif %d err %d\n",
1421 mifp - mif6table, error);
1428 * If we belong to the destination multicast group
1429 * on the outgoing interface, loop back a copy.
1431 dst6 = (struct sockaddr_in6 *)&ro.ro_dst;
1432 IN6_LOOKUP_MULTI(ip6->ip6_dst, ifp, in6m);
1434 dst6->sin6_len = sizeof(struct sockaddr_in6);
1435 dst6->sin6_family = AF_INET6;
1436 dst6->sin6_addr = ip6->ip6_dst;
1437 ip6_mloopback(ifp, m, (struct sockaddr_in6 *)&ro.ro_dst);
1440 * Put the packet into the sending queue of the outgoing interface
1441 * if it would fit in the MTU of the interface.
1443 if (mb_copy->m_pkthdr.len <= ifp->if_mtu || ifp->if_mtu < IPV6_MMTU) {
1444 dst6->sin6_len = sizeof(struct sockaddr_in6);
1445 dst6->sin6_family = AF_INET6;
1446 dst6->sin6_addr = ip6->ip6_dst;
1448 * We just call if_output instead of nd6_output here, since
1449 * we need no ND for a multicast forwarded packet...right?
1451 error = ifp->if_output(ifp, mb_copy,
1452 (struct sockaddr *)&ro.ro_dst, NULL);
1454 if (mrt6debug & DEBUG_XMIT)
1455 log(LOG_DEBUG, "phyint_send on mif %d err %d\n",
1456 mifp - mif6table, error);
1459 #ifdef MULTICAST_PMTUD
1460 icmp6_error(mb_copy, ICMP6_PACKET_TOO_BIG, 0, ifp->if_mtu);
1463 if (mrt6debug & DEBUG_XMIT)
1465 "phyint_send: packet too big on %s o %s g %s"
1466 " size %d(discarded)\n",
1468 ip6_sprintf(&ip6->ip6_src),
1469 ip6_sprintf(&ip6->ip6_dst),
1470 mb_copy->m_pkthdr.len);
1471 #endif /* MRT6DEBUG */
1472 m_freem(mb_copy); /* simply discard the packet */
1480 register_send(struct ip6_hdr *ip6, struct mif6 *mif, struct mbuf *m)
1483 int i, len = m->m_pkthdr.len;
1484 static struct sockaddr_in6 sin6 = { sizeof(sin6), AF_INET6 };
1485 struct mrt6msg *im6;
1489 log(LOG_DEBUG, "** IPv6 register_send **\n src %s dst %s\n",
1490 ip6_sprintf(&ip6->ip6_src), ip6_sprintf(&ip6->ip6_dst));
1492 ++pim6stat.pim6s_snd_registers;
1494 /* Make a copy of the packet to send to the user level process */
1495 MGETHDR(mm, MB_DONTWAIT, MT_HEADER);
1498 mm->m_pkthdr.rcvif = NULL;
1499 mm->m_data += max_linkhdr;
1500 mm->m_len = sizeof(struct ip6_hdr);
1502 if ((mm->m_next = m_copy(m, 0, M_COPYALL)) == NULL) {
1506 i = MHLEN - M_LEADINGSPACE(mm);
1509 mm = m_pullup(mm, i);
1512 /* TODO: check it! */
1513 mm->m_pkthdr.len = len + sizeof(struct ip6_hdr);
1516 * Send message to routing daemon
1518 sin6.sin6_addr = ip6->ip6_src;
1520 im6 = mtod(mm, struct mrt6msg *);
1521 im6->im6_msgtype = MRT6MSG_WHOLEPKT;
1524 im6->im6_mif = mif - mif6table;
1526 /* iif info is not given for reg. encap.n */
1527 mrt6stat.mrt6s_upcalls++;
1529 if (socket_send(ip6_mrouter, mm, &sin6) < 0) {
1533 "register_send: ip6_mrouter socket queue full\n");
1535 ++mrt6stat.mrt6s_upq_sockfull;
1542 * PIM sparse mode hook
1543 * Receives the pim control messages, and passes them up to the listening
1544 * socket, using rip6_input.
1545 * The only message processed is the REGISTER pim message; the pim header
1546 * is stripped off, and the inner packet is passed to register_mforward.
1549 pim6_input(struct mbuf **mp, int *offp, int proto)
1551 struct pim *pim; /* pointer to a pim struct */
1552 struct ip6_hdr *ip6;
1554 struct mbuf *m = *mp;
1558 ++pim6stat.pim6s_rcv_total;
1560 ip6 = mtod(m, struct ip6_hdr *);
1561 pimlen = m->m_pkthdr.len - *offp;
1566 if (pimlen < PIM_MINLEN) {
1567 ++pim6stat.pim6s_rcv_tooshort;
1569 if (mrt6debug & DEBUG_PIM)
1570 log(LOG_DEBUG,"pim6_input: PIM packet too short\n");
1573 return (IPPROTO_DONE);
1577 * if the packet is at least as big as a REGISTER, go ahead
1578 * and grab the PIM REGISTER header size, to avoid another
1579 * possible m_pullup() later.
1581 * PIM_MINLEN == pimhdr + u_int32 == 8
1582 * PIM6_REG_MINLEN == pimhdr + reghdr + eip6hdr == 4 + 4 + 40
1584 minlen = (pimlen >= PIM6_REG_MINLEN) ? PIM6_REG_MINLEN : PIM_MINLEN;
1587 * Make sure that the IP6 and PIM headers in contiguous memory, and
1588 * possibly the PIM REGISTER header
1590 #ifndef PULLDOWN_TEST
1591 IP6_EXTHDR_CHECK(m, off, minlen, IPPROTO_DONE);
1592 /* adjust pointer */
1593 ip6 = mtod(m, struct ip6_hdr *);
1595 /* adjust mbuf to point to the PIM header */
1596 pim = (struct pim *)((caddr_t)ip6 + off);
1598 IP6_EXTHDR_GET(pim, struct pim *, m, off, minlen);
1600 pim6stat.pim6s_rcv_tooshort++;
1601 return IPPROTO_DONE;
1605 #define PIM6_CHECKSUM
1606 #ifdef PIM6_CHECKSUM
1611 * Validate checksum.
1612 * If PIM REGISTER, exclude the data packet
1614 if (pim->pim_type == PIM_REGISTER)
1615 cksumlen = PIM_MINLEN;
1619 if (in6_cksum(m, IPPROTO_PIM, off, cksumlen)) {
1620 ++pim6stat.pim6s_rcv_badsum;
1622 if (mrt6debug & DEBUG_PIM)
1624 "pim6_input: invalid checksum\n");
1627 return (IPPROTO_DONE);
1630 #endif /* PIM_CHECKSUM */
1632 /* PIM version check */
1633 if (pim->pim_ver != PIM_VERSION) {
1634 ++pim6stat.pim6s_rcv_badversion;
1637 "pim6_input: incorrect version %d, expecting %d\n",
1638 pim->pim_ver, PIM_VERSION);
1641 return (IPPROTO_DONE);
1644 if (pim->pim_type == PIM_REGISTER) {
1646 * since this is a REGISTER, we'll make a copy of the register
1647 * headers ip6+pim+u_int32_t+encap_ip6, to be passed up to the
1650 static struct sockaddr_in6 dst = { sizeof(dst), AF_INET6 };
1653 struct ip6_hdr *eip6;
1657 ++pim6stat.pim6s_rcv_registers;
1659 if ((reg_mif_num >= nummifs) || (reg_mif_num == (mifi_t) -1)) {
1661 if (mrt6debug & DEBUG_PIM)
1663 "pim6_input: register mif not set: %d\n",
1667 return (IPPROTO_DONE);
1670 reghdr = (u_int32_t *)(pim + 1);
1672 if ((ntohl(*reghdr) & PIM_NULL_REGISTER))
1673 goto pim6_input_to_daemon;
1678 if (pimlen < PIM6_REG_MINLEN) {
1679 ++pim6stat.pim6s_rcv_tooshort;
1680 ++pim6stat.pim6s_rcv_badregisters;
1683 "pim6_input: register packet size too "
1684 "small %d from %s\n",
1685 pimlen, ip6_sprintf(&ip6->ip6_src));
1688 return (IPPROTO_DONE);
1691 eip6 = (struct ip6_hdr *) (reghdr + 1);
1693 if (mrt6debug & DEBUG_PIM)
1695 "pim6_input[register], eip6: %s -> %s, "
1697 ip6_sprintf(&eip6->ip6_src),
1698 ip6_sprintf(&eip6->ip6_dst),
1699 ntohs(eip6->ip6_plen));
1702 /* verify the version number of the inner packet */
1703 if ((eip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
1704 ++pim6stat.pim6s_rcv_badregisters;
1706 log(LOG_DEBUG, "pim6_input: invalid IP version (%d) "
1707 "of the inner packet\n",
1708 (eip6->ip6_vfc & IPV6_VERSION));
1711 return (IPPROTO_NONE);
1714 /* verify the inner packet is destined to a mcast group */
1715 if (!IN6_IS_ADDR_MULTICAST(&eip6->ip6_dst)) {
1716 ++pim6stat.pim6s_rcv_badregisters;
1718 if (mrt6debug & DEBUG_PIM)
1720 "pim6_input: inner packet of register "
1721 "is not multicast %s\n",
1722 ip6_sprintf(&eip6->ip6_dst));
1725 return (IPPROTO_DONE);
1729 * make a copy of the whole header to pass to the daemon later.
1731 mcp = m_copy(m, 0, off + PIM6_REG_MINLEN);
1735 "pim6_input: pim register: "
1736 "could not copy register head\n");
1739 return (IPPROTO_DONE);
1743 * forward the inner ip6 packet; point m_data at the inner ip6.
1745 m_adj(m, off + PIM_MINLEN);
1747 if (mrt6debug & DEBUG_PIM) {
1749 "pim6_input: forwarding decapsulated register: "
1750 "src %s, dst %s, mif %d\n",
1751 ip6_sprintf(&eip6->ip6_src),
1752 ip6_sprintf(&eip6->ip6_dst),
1757 rc = if_simloop(mif6table[reg_mif_num].m6_ifp, m,
1758 dst.sin6_family, 0);
1760 /* prepare the register head to send to the mrouting daemon */
1765 * Pass the PIM message up to the daemon; if it is a register message
1766 * pass the 'head' only up to the daemon. This includes the
1767 * encapsulator ip6 header, pim header, register header and the
1768 * encapsulated ip6 header.
1770 pim6_input_to_daemon:
1771 rip6_input(&m, offp, proto);
1772 return (IPPROTO_DONE);