2 * IP multicast forwarding procedures
4 * Written by David Waitzman, BBN Labs, August 1988.
5 * Modified by Steve Deering, Stanford, February 1989.
6 * Modified by Mark J. Steiglitz, Stanford, May, 1991
7 * Modified by Van Jacobson, LBL, January 1993
8 * Modified by Ajit Thyagarajan, PARC, August 1993
9 * Modified by Bill Fenner, PARC, April 1995
10 * Modified by Ahmed Helmy, SGI, June 1996
11 * Modified by George Edmond Eddy (Rusty), ISI, February 1998
12 * Modified by Pavlin Radoslavov, USC/ISI, May 1998, August 1999, October 2000
13 * Modified by Hitoshi Asaeda, WIDE, August 2000
14 * Modified by Pavlin Radoslavov, ICSI, October 2002
16 * MROUTING Revision: 3.5
17 * and PIM-SMv2 and PIM-DM support, advanced API support,
18 * bandwidth metering and signaling
24 #include "opt_mrouting.h"
30 #include <sys/param.h>
31 #include <sys/kernel.h>
34 #include <sys/malloc.h>
36 #include <sys/module.h>
37 #include <sys/protosw.h>
38 #include <sys/signalvar.h>
39 #include <sys/socket.h>
40 #include <sys/socketvar.h>
41 #include <sys/sockio.h>
43 #include <sys/sysctl.h>
44 #include <sys/syslog.h>
45 #include <sys/systm.h>
48 #include <net/netisr.h>
49 #include <net/route.h>
50 #include <netinet/in.h>
51 #include <netinet/igmp.h>
52 #include <netinet/in_systm.h>
53 #include <netinet/in_var.h>
54 #include <netinet/ip.h>
55 #include <netinet/ip_encap.h>
56 #include <netinet/ip_mroute.h>
57 #include <netinet/ip_var.h>
59 #include <netinet/pim.h>
60 #include <netinet/pim_var.h>
62 #include <netinet/udp.h>
63 #include <machine/in_cksum.h>
66 * Control debugging code for rsvp and multicast routing code.
67 * Can only set them with the debugger.
69 static u_int rsvpdebug; /* non-zero enables debugging */
71 static u_int mrtdebug; /* any set of the flags below */
72 #define DEBUG_MFC 0x02
73 #define DEBUG_FORWARD 0x04
74 #define DEBUG_EXPIRE 0x08
75 #define DEBUG_XMIT 0x10
76 #define DEBUG_PIM 0x20
78 #define VIFI_INVALID ((vifi_t) -1)
80 #define M_HASCL(m) ((m)->m_flags & M_EXT)
82 static MALLOC_DEFINE(M_MRTABLE, "mroutetbl", "multicast routing tables");
85 * Locking. We use two locks: one for the virtual interface table and
86 * one for the forwarding table. These locks may be nested in which case
87 * the VIF lock must always be taken first. Note that each lock is used
88 * to cover not only the specific data structure but also related data
89 * structures. It may be better to add more fine-grained locking later;
90 * it's not clear how performance-critical this code is.
93 static struct mrtstat mrtstat;
94 SYSCTL_STRUCT(_net_inet_ip, OID_AUTO, mrtstat, CTLFLAG_RW,
96 "Multicast Routing Statistics (struct mrtstat, netinet/ip_mroute.h)");
98 static struct mfc *mfctable[MFCTBLSIZ];
99 SYSCTL_OPAQUE(_net_inet_ip, OID_AUTO, mfctable, CTLFLAG_RD,
100 &mfctable, sizeof(mfctable), "S,*mfc[MFCTBLSIZ]",
101 "Multicast Forwarding Table (struct *mfc[MFCTBLSIZ], netinet/ip_mroute.h)");
103 static struct mtx mfc_mtx;
104 #define MFC_LOCK() mtx_lock(&mfc_mtx)
105 #define MFC_UNLOCK() mtx_unlock(&mfc_mtx)
106 #define MFC_LOCK_ASSERT() do { \
107 mtx_assert(&mfc_mtx, MA_OWNED); \
108 NET_ASSERT_GIANT(); \
110 #define MFC_LOCK_INIT() mtx_init(&mfc_mtx, "mroute mfc table", NULL, MTX_DEF)
111 #define MFC_LOCK_DESTROY() mtx_destroy(&mfc_mtx)
113 static struct vif viftable[MAXVIFS];
114 SYSCTL_OPAQUE(_net_inet_ip, OID_AUTO, viftable, CTLFLAG_RD,
115 &viftable, sizeof(viftable), "S,vif[MAXVIFS]",
116 "Multicast Virtual Interfaces (struct vif[MAXVIFS], netinet/ip_mroute.h)");
118 static struct mtx vif_mtx;
119 #define VIF_LOCK() mtx_lock(&vif_mtx)
120 #define VIF_UNLOCK() mtx_unlock(&vif_mtx)
121 #define VIF_LOCK_ASSERT() mtx_assert(&vif_mtx, MA_OWNED)
122 #define VIF_LOCK_INIT() mtx_init(&vif_mtx, "mroute vif table", NULL, MTX_DEF)
123 #define VIF_LOCK_DESTROY() mtx_destroy(&vif_mtx)
125 static u_char nexpire[MFCTBLSIZ];
127 static struct callout expire_upcalls_ch;
129 #define EXPIRE_TIMEOUT (hz / 4) /* 4x / second */
130 #define UPCALL_EXPIRE 6 /* number of timeouts */
133 * Define the token bucket filter structures
134 * tbftable -> each vif has one of these for storing info
137 static struct tbf tbftable[MAXVIFS];
138 #define TBF_REPROCESS (hz / 100) /* 100x / second */
141 * 'Interfaces' associated with decapsulator (so we can tell
142 * packets that went through it from ones that get reflected
143 * by a broken gateway). These interfaces are never linked into
144 * the system ifnet list & no routes point to them. I.e., packets
145 * can't be sent this way. They only exist as a placeholder for
146 * multicast source verification.
148 static struct ifnet multicast_decap_if[MAXVIFS];
151 #define ENCAP_PROTO IPPROTO_IPIP /* 4 */
153 /* prototype IP hdr for encapsulated packets */
154 static struct ip multicast_encap_iphdr = {
155 #if BYTE_ORDER == LITTLE_ENDIAN
156 sizeof(struct ip) >> 2, IPVERSION,
158 IPVERSION, sizeof(struct ip) >> 2,
161 sizeof(struct ip), /* total length */
164 ENCAP_TTL, ENCAP_PROTO,
169 * Bandwidth meter variables and constants
171 static MALLOC_DEFINE(M_BWMETER, "bwmeter", "multicast upcall bw meters");
173 * Pending timeouts are stored in a hash table, the key being the
174 * expiration time. Periodically, the entries are analysed and processed.
176 #define BW_METER_BUCKETS 1024
177 static struct bw_meter *bw_meter_timers[BW_METER_BUCKETS];
178 static struct callout bw_meter_ch;
179 #define BW_METER_PERIOD (hz) /* periodical handling of bw meters */
182 * Pending upcalls are stored in a vector which is flushed when
183 * full, or periodically
185 static struct bw_upcall bw_upcalls[BW_UPCALLS_MAX];
186 static u_int bw_upcalls_n; /* # of pending upcalls */
187 static struct callout bw_upcalls_ch;
188 #define BW_UPCALLS_PERIOD (hz) /* periodical flush of bw upcalls */
191 static struct pimstat pimstat;
192 SYSCTL_STRUCT(_net_inet_pim, PIMCTL_STATS, stats, CTLFLAG_RD,
194 "PIM Statistics (struct pimstat, netinet/pim_var.h)");
197 * Note: the PIM Register encapsulation adds the following in front of a
200 * struct pim_encap_hdr {
202 * struct pim_encap_pimhdr pim;
207 struct pim_encap_pimhdr {
212 static struct ip pim_encap_iphdr = {
213 #if BYTE_ORDER == LITTLE_ENDIAN
214 sizeof(struct ip) >> 2,
218 sizeof(struct ip) >> 2,
221 sizeof(struct ip), /* total length */
229 static struct pim_encap_pimhdr pim_encap_pimhdr = {
231 PIM_MAKE_VT(PIM_VERSION, PIM_REGISTER), /* PIM vers and message type */
238 static struct ifnet multicast_register_if;
239 static vifi_t reg_vif_num = VIFI_INVALID;
245 static vifi_t numvifs;
246 static const struct encaptab *encap_cookie;
249 * one-back cache used by mroute_encapcheck to locate a tunnel's vif
250 * given a datagram's src ip address.
252 static u_long last_encap_src;
253 static struct vif *last_encap_vif;
256 * Callout for queue processing.
258 static struct callout tbf_reprocess_ch;
260 static u_long X_ip_mcast_src(int vifi);
261 static int X_ip_mforward(struct ip *ip, struct ifnet *ifp,
262 struct mbuf *m, struct ip_moptions *imo);
263 static int X_ip_mrouter_done(void);
264 static int X_ip_mrouter_get(struct socket *so, struct sockopt *m);
265 static int X_ip_mrouter_set(struct socket *so, struct sockopt *m);
266 static int X_legal_vif_num(int vif);
267 static int X_mrt_ioctl(int cmd, caddr_t data);
269 static int get_sg_cnt(struct sioc_sg_req *);
270 static int get_vif_cnt(struct sioc_vif_req *);
271 static int ip_mrouter_init(struct socket *, int);
272 static int add_vif(struct vifctl *);
273 static int del_vif(vifi_t);
274 static int add_mfc(struct mfcctl2 *);
275 static int del_mfc(struct mfcctl2 *);
276 static int set_api_config(uint32_t *); /* chose API capabilities */
277 static int socket_send(struct socket *, struct mbuf *, struct sockaddr_in *);
278 static int set_assert(int);
279 static void expire_upcalls(void *);
280 static int ip_mdq(struct mbuf *, struct ifnet *, struct mfc *, vifi_t);
281 static void phyint_send(struct ip *, struct vif *, struct mbuf *);
282 static void encap_send(struct ip *, struct vif *, struct mbuf *);
283 static void tbf_control(struct vif *, struct mbuf *, struct ip *, u_long);
284 static void tbf_queue(struct vif *, struct mbuf *);
285 static void tbf_process_q(struct vif *);
286 static void tbf_reprocess_q(void *);
287 static int tbf_dq_sel(struct vif *, struct ip *);
288 static void tbf_send_packet(struct vif *, struct mbuf *);
289 static void tbf_update_tokens(struct vif *);
290 static int priority(struct vif *, struct ip *);
293 * Bandwidth monitoring
295 static void free_bw_list(struct bw_meter *list);
296 static int add_bw_upcall(struct bw_upcall *);
297 static int del_bw_upcall(struct bw_upcall *);
298 static void bw_meter_receive_packet(struct bw_meter *x, int plen,
299 struct timeval *nowp);
300 static void bw_meter_prepare_upcall(struct bw_meter *x, struct timeval *nowp);
301 static void bw_upcalls_send(void);
302 static void schedule_bw_meter(struct bw_meter *x, struct timeval *nowp);
303 static void unschedule_bw_meter(struct bw_meter *x);
304 static void bw_meter_process(void);
305 static void expire_bw_upcalls_send(void *);
306 static void expire_bw_meter_process(void *);
309 static int pim_register_send(struct ip *, struct vif *,
310 struct mbuf *, struct mfc *);
311 static int pim_register_send_rp(struct ip *, struct vif *,
312 struct mbuf *, struct mfc *);
313 static int pim_register_send_upcall(struct ip *, struct vif *,
314 struct mbuf *, struct mfc *);
315 static struct mbuf *pim_register_prepare(struct ip *, struct mbuf *);
319 * whether or not special PIM assert processing is enabled.
321 static int pim_assert;
323 * Rate limit for assert notification messages, in usec
325 #define ASSERT_MSG_TIME 3000000
328 * Kernel multicast routing API capabilities and setup.
329 * If more API capabilities are added to the kernel, they should be
330 * recorded in `mrt_api_support'.
332 static const uint32_t mrt_api_support = (MRT_MFC_FLAGS_DISABLE_WRONGVIF |
333 MRT_MFC_FLAGS_BORDER_VIF |
336 static uint32_t mrt_api_config = 0;
339 * Hash function for a source, group entry
341 #define MFCHASH(a, g) MFCHASHMOD(((a) >> 20) ^ ((a) >> 10) ^ (a) ^ \
342 ((g) >> 20) ^ ((g) >> 10) ^ (g))
345 * Find a route for a given origin IP address and Multicast group address
346 * Type of service parameter to be added in the future!!!
347 * Statistics are updated by the caller if needed
348 * (mrtstat.mrts_mfc_lookups and mrtstat.mrts_mfc_misses)
351 mfc_find(in_addr_t o, in_addr_t g)
357 for (rt = mfctable[MFCHASH(o,g)]; rt; rt = rt->mfc_next)
358 if ((rt->mfc_origin.s_addr == o) &&
359 (rt->mfc_mcastgrp.s_addr == g) && (rt->mfc_stall == NULL))
365 * Macros to compute elapsed time efficiently
366 * Borrowed from Van Jacobson's scheduling code
368 #define TV_DELTA(a, b, delta) { \
370 delta = (a).tv_usec - (b).tv_usec; \
371 if ((xxs = (a).tv_sec - (b).tv_sec)) { \
380 delta += (1000000 * xxs); \
385 #define TV_LT(a, b) (((a).tv_usec < (b).tv_usec && \
386 (a).tv_sec <= (b).tv_sec) || (a).tv_sec < (b).tv_sec)
389 * Handle MRT setsockopt commands to modify the multicast routing tables.
392 X_ip_mrouter_set(struct socket *so, struct sockopt *sopt)
398 struct bw_upcall bw_upcall;
401 if (so != ip_mrouter && sopt->sopt_name != MRT_INIT)
405 switch (sopt->sopt_name) {
407 error = sooptcopyin(sopt, &optval, sizeof optval, sizeof optval);
410 error = ip_mrouter_init(so, optval);
414 error = ip_mrouter_done();
418 error = sooptcopyin(sopt, &vifc, sizeof vifc, sizeof vifc);
421 error = add_vif(&vifc);
425 error = sooptcopyin(sopt, &vifi, sizeof vifi, sizeof vifi);
428 error = del_vif(vifi);
434 * select data size depending on API version.
436 if (sopt->sopt_name == MRT_ADD_MFC &&
437 mrt_api_config & MRT_API_FLAGS_ALL) {
438 error = sooptcopyin(sopt, &mfc, sizeof(struct mfcctl2),
439 sizeof(struct mfcctl2));
441 error = sooptcopyin(sopt, &mfc, sizeof(struct mfcctl),
442 sizeof(struct mfcctl));
443 bzero((caddr_t)&mfc + sizeof(struct mfcctl),
444 sizeof(mfc) - sizeof(struct mfcctl));
448 if (sopt->sopt_name == MRT_ADD_MFC)
449 error = add_mfc(&mfc);
451 error = del_mfc(&mfc);
455 error = sooptcopyin(sopt, &optval, sizeof optval, sizeof optval);
462 error = sooptcopyin(sopt, &i, sizeof i, sizeof i);
464 error = set_api_config(&i);
466 error = sooptcopyout(sopt, &i, sizeof i);
469 case MRT_ADD_BW_UPCALL:
470 case MRT_DEL_BW_UPCALL:
471 error = sooptcopyin(sopt, &bw_upcall, sizeof bw_upcall,
475 if (sopt->sopt_name == MRT_ADD_BW_UPCALL)
476 error = add_bw_upcall(&bw_upcall);
478 error = del_bw_upcall(&bw_upcall);
489 * Handle MRT getsockopt commands
492 X_ip_mrouter_get(struct socket *so, struct sockopt *sopt)
495 static int version = 0x0305; /* !!! why is this here? XXX */
497 switch (sopt->sopt_name) {
499 error = sooptcopyout(sopt, &version, sizeof version);
503 error = sooptcopyout(sopt, &pim_assert, sizeof pim_assert);
506 case MRT_API_SUPPORT:
507 error = sooptcopyout(sopt, &mrt_api_support, sizeof mrt_api_support);
511 error = sooptcopyout(sopt, &mrt_api_config, sizeof mrt_api_config);
522 * Handle ioctl commands to obtain information from the cache
525 X_mrt_ioctl(int cmd, caddr_t data)
530 * Currently the only function calling this ioctl routine is rtioctl().
531 * Typically, only root can create the raw socket in order to execute
532 * this ioctl method, however the request might be coming from a prison
534 error = suser(curthread);
538 case (SIOCGETVIFCNT):
539 error = get_vif_cnt((struct sioc_vif_req *)data);
543 error = get_sg_cnt((struct sioc_sg_req *)data);
554 * returns the packet, byte, rpf-failure count for the source group provided
557 get_sg_cnt(struct sioc_sg_req *req)
562 rt = mfc_find(req->src.s_addr, req->grp.s_addr);
565 req->pktcnt = req->bytecnt = req->wrong_if = 0xffffffff;
566 return EADDRNOTAVAIL;
568 req->pktcnt = rt->mfc_pkt_cnt;
569 req->bytecnt = rt->mfc_byte_cnt;
570 req->wrong_if = rt->mfc_wrong_if;
576 * returns the input and output packet and byte counts on the vif provided
579 get_vif_cnt(struct sioc_vif_req *req)
581 vifi_t vifi = req->vifi;
584 if (vifi >= numvifs) {
589 req->icount = viftable[vifi].v_pkt_in;
590 req->ocount = viftable[vifi].v_pkt_out;
591 req->ibytes = viftable[vifi].v_bytes_in;
592 req->obytes = viftable[vifi].v_bytes_out;
599 ip_mrouter_reset(void)
601 bzero((caddr_t)mfctable, sizeof(mfctable));
602 bzero((caddr_t)nexpire, sizeof(nexpire));
607 callout_init(&expire_upcalls_ch, NET_CALLOUT_MPSAFE);
610 bzero((caddr_t)bw_meter_timers, sizeof(bw_meter_timers));
611 callout_init(&bw_upcalls_ch, NET_CALLOUT_MPSAFE);
612 callout_init(&bw_meter_ch, NET_CALLOUT_MPSAFE);
614 callout_init(&tbf_reprocess_ch, NET_CALLOUT_MPSAFE);
617 static struct mtx mrouter_mtx; /* used to synch init/done work */
620 * Enable multicast routing
623 ip_mrouter_init(struct socket *so, int version)
626 log(LOG_DEBUG, "ip_mrouter_init: so_type = %d, pr_protocol = %d\n",
627 so->so_type, so->so_proto->pr_protocol);
629 if (so->so_type != SOCK_RAW || so->so_proto->pr_protocol != IPPROTO_IGMP)
635 mtx_lock(&mrouter_mtx);
637 if (ip_mrouter != NULL) {
638 mtx_unlock(&mrouter_mtx);
642 callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT, expire_upcalls, NULL);
644 callout_reset(&bw_upcalls_ch, BW_UPCALLS_PERIOD,
645 expire_bw_upcalls_send, NULL);
646 callout_reset(&bw_meter_ch, BW_METER_PERIOD, expire_bw_meter_process, NULL);
650 mtx_unlock(&mrouter_mtx);
653 log(LOG_DEBUG, "ip_mrouter_init\n");
659 * Disable multicast routing
662 X_ip_mrouter_done(void)
671 mtx_lock(&mrouter_mtx);
673 if (ip_mrouter == NULL) {
674 mtx_unlock(&mrouter_mtx);
679 * Detach/disable hooks to the reset of the system.
686 const struct encaptab *c = encap_cookie;
692 callout_stop(&tbf_reprocess_ch);
696 * For each phyint in use, disable promiscuous reception of all IP
699 for (vifi = 0; vifi < numvifs; vifi++) {
700 if (viftable[vifi].v_lcl_addr.s_addr != 0 &&
701 !(viftable[vifi].v_flags & (VIFF_TUNNEL | VIFF_REGISTER))) {
702 struct sockaddr_in *so = (struct sockaddr_in *)&(ifr.ifr_addr);
704 so->sin_len = sizeof(struct sockaddr_in);
705 so->sin_family = AF_INET;
706 so->sin_addr.s_addr = INADDR_ANY;
707 ifp = viftable[vifi].v_ifp;
711 bzero((caddr_t)tbftable, sizeof(tbftable));
712 bzero((caddr_t)viftable, sizeof(viftable));
718 * Free all multicast forwarding cache entries.
720 callout_stop(&expire_upcalls_ch);
721 callout_stop(&bw_upcalls_ch);
722 callout_stop(&bw_meter_ch);
725 for (i = 0; i < MFCTBLSIZ; i++) {
726 for (rt = mfctable[i]; rt != NULL; ) {
727 struct mfc *nr = rt->mfc_next;
729 for (rte = rt->mfc_stall; rte != NULL; ) {
730 struct rtdetq *n = rte->next;
733 free(rte, M_MRTABLE);
736 free_bw_list(rt->mfc_bw_meter);
741 bzero((caddr_t)mfctable, sizeof(mfctable));
742 bzero((caddr_t)nexpire, sizeof(nexpire));
744 bzero(bw_meter_timers, sizeof(bw_meter_timers));
748 * Reset de-encapsulation cache
750 last_encap_src = INADDR_ANY;
751 last_encap_vif = NULL;
753 reg_vif_num = VIFI_INVALID;
756 mtx_unlock(&mrouter_mtx);
759 log(LOG_DEBUG, "ip_mrouter_done\n");
765 * Set PIM assert processing global
770 if ((i != 1) && (i != 0))
779 * Configure API capabilities
782 set_api_config(uint32_t *apival)
787 * We can set the API capabilities only if it is the first operation
788 * after MRT_INIT. I.e.:
789 * - there are no vifs installed
790 * - pim_assert is not enabled
791 * - the MFC table is empty
801 for (i = 0; i < MFCTBLSIZ; i++) {
802 if (mfctable[i] != NULL) {
808 mrt_api_config = *apival & mrt_api_support;
809 *apival = mrt_api_config;
815 * Decide if a packet is from a tunnelled peer.
816 * Return 0 if not, 64 if so. XXX yuck.. 64 ???
819 mroute_encapcheck(const struct mbuf *m, int off, int proto, void *arg)
821 struct ip *ip = mtod(m, struct ip *);
822 int hlen = ip->ip_hl << 2;
825 * don't claim the packet if it's not to a multicast destination or if
826 * we don't have an encapsulating tunnel with the source.
827 * Note: This code assumes that the remote site IP address
828 * uniquely identifies the tunnel (i.e., that this site has
829 * at most one tunnel with the remote site).
831 if (!IN_MULTICAST(ntohl(((struct ip *)((char *)ip+hlen))->ip_dst.s_addr)))
833 if (ip->ip_src.s_addr != last_encap_src) {
834 struct vif *vifp = viftable;
835 struct vif *vife = vifp + numvifs;
837 last_encap_src = ip->ip_src.s_addr;
838 last_encap_vif = NULL;
839 for ( ; vifp < vife; ++vifp)
840 if (vifp->v_rmt_addr.s_addr == ip->ip_src.s_addr) {
841 if ((vifp->v_flags & (VIFF_TUNNEL|VIFF_SRCRT)) == VIFF_TUNNEL)
842 last_encap_vif = vifp;
846 if (last_encap_vif == NULL) {
847 last_encap_src = INADDR_ANY;
854 * De-encapsulate a packet and feed it back through ip input (this
855 * routine is called whenever IP gets a packet that mroute_encap_func()
859 mroute_encap_input(struct mbuf *m, int off)
861 struct ip *ip = mtod(m, struct ip *);
862 int hlen = ip->ip_hl << 2;
864 if (hlen > sizeof(struct ip))
865 ip_stripoptions(m, (struct mbuf *) 0);
866 m->m_data += sizeof(struct ip);
867 m->m_len -= sizeof(struct ip);
868 m->m_pkthdr.len -= sizeof(struct ip);
870 m->m_pkthdr.rcvif = last_encap_vif->v_ifp;
872 netisr_queue(NETISR_IP, m); /* mbuf is free'd on failure. */
874 * normally we would need a "schednetisr(NETISR_IP)"
875 * here but we were called by ip_input and it is going
876 * to loop back & try to dequeue the packet we just
877 * queued as soon as we return so we avoid the
878 * unnecessary software interrrupt.
881 * This no longer holds - we may have direct-dispatched the packet,
882 * or there may be a queue processing limit.
886 extern struct domain inetdomain;
887 static struct protosw mroute_encap_protosw =
888 { SOCK_RAW, &inetdomain, IPPROTO_IPV4, PR_ATOMIC|PR_ADDR,
889 mroute_encap_input, 0, 0, rip_ctloutput,
896 * Add a vif to the vif table
899 add_vif(struct vifctl *vifcp)
901 struct vif *vifp = viftable + vifcp->vifc_vifi;
902 struct sockaddr_in sin = {sizeof sin, AF_INET};
906 struct tbf *v_tbf = tbftable + vifcp->vifc_vifi;
909 if (vifcp->vifc_vifi >= MAXVIFS) {
913 if (vifp->v_lcl_addr.s_addr != INADDR_ANY) {
917 if (vifcp->vifc_lcl_addr.s_addr == INADDR_ANY) {
919 return EADDRNOTAVAIL;
922 /* Find the interface with an address in AF_INET family */
924 if (vifcp->vifc_flags & VIFF_REGISTER) {
926 * XXX: Because VIFF_REGISTER does not really need a valid
927 * local interface (e.g. it could be 127.0.0.2), we don't
934 sin.sin_addr = vifcp->vifc_lcl_addr;
935 ifa = ifa_ifwithaddr((struct sockaddr *)&sin);
938 return EADDRNOTAVAIL;
943 if (vifcp->vifc_flags & VIFF_TUNNEL) {
944 if ((vifcp->vifc_flags & VIFF_SRCRT) == 0) {
946 * An encapsulating tunnel is wanted. Tell
947 * mroute_encap_input() to start paying attention
948 * to encapsulated packets.
950 if (encap_cookie == NULL) {
953 encap_cookie = encap_attach_func(AF_INET, IPPROTO_IPV4,
955 (struct protosw *)&mroute_encap_protosw, NULL);
957 if (encap_cookie == NULL) {
958 printf("ip_mroute: unable to attach encap\n");
960 return EIO; /* XXX */
962 for (i = 0; i < MAXVIFS; ++i) {
963 if_initname(&multicast_decap_if[i], "mdecap", i);
967 * Set interface to fake encapsulator interface
969 ifp = &multicast_decap_if[vifcp->vifc_vifi];
971 * Prepare cached route entry
973 bzero(&vifp->v_route, sizeof(vifp->v_route));
975 log(LOG_ERR, "source routed tunnels not supported\n");
980 } else if (vifcp->vifc_flags & VIFF_REGISTER) {
981 ifp = &multicast_register_if;
983 log(LOG_DEBUG, "Adding a register vif, ifp: %p\n",
984 (void *)&multicast_register_if);
985 if (reg_vif_num == VIFI_INVALID) {
986 if_initname(&multicast_register_if, "register_vif", 0);
987 multicast_register_if.if_flags = IFF_LOOPBACK;
988 bzero(&vifp->v_route, sizeof(vifp->v_route));
989 reg_vif_num = vifcp->vifc_vifi;
992 } else { /* Make sure the interface supports multicast */
993 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
998 /* Enable promiscuous reception of all IP multicasts from the if */
999 error = if_allmulti(ifp, 1);
1006 /* define parameters for the tbf structure */
1007 vifp->v_tbf = v_tbf;
1008 GET_TIME(vifp->v_tbf->tbf_last_pkt_t);
1009 vifp->v_tbf->tbf_n_tok = 0;
1010 vifp->v_tbf->tbf_q_len = 0;
1011 vifp->v_tbf->tbf_max_q_len = MAXQSIZE;
1012 vifp->v_tbf->tbf_q = vifp->v_tbf->tbf_t = NULL;
1014 vifp->v_flags = vifcp->vifc_flags;
1015 vifp->v_threshold = vifcp->vifc_threshold;
1016 vifp->v_lcl_addr = vifcp->vifc_lcl_addr;
1017 vifp->v_rmt_addr = vifcp->vifc_rmt_addr;
1019 /* scaling up here allows division by 1024 in critical code */
1020 vifp->v_rate_limit= vifcp->vifc_rate_limit * 1024 / 1000;
1021 vifp->v_rsvp_on = 0;
1022 vifp->v_rsvpd = NULL;
1023 /* initialize per vif pkt counters */
1025 vifp->v_pkt_out = 0;
1026 vifp->v_bytes_in = 0;
1027 vifp->v_bytes_out = 0;
1029 /* Adjust numvifs up if the vifi is higher than numvifs */
1030 if (numvifs <= vifcp->vifc_vifi) numvifs = vifcp->vifc_vifi + 1;
1035 log(LOG_DEBUG, "add_vif #%d, lcladdr %lx, %s %lx, thresh %x, rate %d\n",
1037 (u_long)ntohl(vifcp->vifc_lcl_addr.s_addr),
1038 (vifcp->vifc_flags & VIFF_TUNNEL) ? "rmtaddr" : "mask",
1039 (u_long)ntohl(vifcp->vifc_rmt_addr.s_addr),
1040 vifcp->vifc_threshold,
1041 vifcp->vifc_rate_limit);
1047 * Delete a vif from the vif table
1050 del_vif(vifi_t vifi)
1056 if (vifi >= numvifs) {
1060 vifp = &viftable[vifi];
1061 if (vifp->v_lcl_addr.s_addr == INADDR_ANY) {
1063 return EADDRNOTAVAIL;
1066 if (!(vifp->v_flags & (VIFF_TUNNEL | VIFF_REGISTER)))
1067 if_allmulti(vifp->v_ifp, 0);
1069 if (vifp == last_encap_vif) {
1070 last_encap_vif = NULL;
1071 last_encap_src = INADDR_ANY;
1075 * Free packets queued at the interface
1077 while (vifp->v_tbf->tbf_q) {
1078 struct mbuf *m = vifp->v_tbf->tbf_q;
1080 vifp->v_tbf->tbf_q = m->m_act;
1085 if (vifp->v_flags & VIFF_REGISTER)
1086 reg_vif_num = VIFI_INVALID;
1089 bzero((caddr_t)vifp->v_tbf, sizeof(*(vifp->v_tbf)));
1090 bzero((caddr_t)vifp, sizeof (*vifp));
1093 log(LOG_DEBUG, "del_vif %d, numvifs %d\n", vifi, numvifs);
1095 /* Adjust numvifs down */
1096 for (vifi = numvifs; vifi > 0; vifi--)
1097 if (viftable[vifi-1].v_lcl_addr.s_addr != INADDR_ANY)
1107 * update an mfc entry without resetting counters and S,G addresses.
1110 update_mfc_params(struct mfc *rt, struct mfcctl2 *mfccp)
1114 rt->mfc_parent = mfccp->mfcc_parent;
1115 for (i = 0; i < numvifs; i++) {
1116 rt->mfc_ttls[i] = mfccp->mfcc_ttls[i];
1117 rt->mfc_flags[i] = mfccp->mfcc_flags[i] & mrt_api_config &
1120 /* set the RP address */
1121 if (mrt_api_config & MRT_MFC_RP)
1122 rt->mfc_rp = mfccp->mfcc_rp;
1124 rt->mfc_rp.s_addr = INADDR_ANY;
1128 * fully initialize an mfc entry from the parameter.
1131 init_mfc_params(struct mfc *rt, struct mfcctl2 *mfccp)
1133 rt->mfc_origin = mfccp->mfcc_origin;
1134 rt->mfc_mcastgrp = mfccp->mfcc_mcastgrp;
1136 update_mfc_params(rt, mfccp);
1138 /* initialize pkt counters per src-grp */
1139 rt->mfc_pkt_cnt = 0;
1140 rt->mfc_byte_cnt = 0;
1141 rt->mfc_wrong_if = 0;
1142 rt->mfc_last_assert.tv_sec = rt->mfc_last_assert.tv_usec = 0;
1150 add_mfc(struct mfcctl2 *mfccp)
1160 rt = mfc_find(mfccp->mfcc_origin.s_addr, mfccp->mfcc_mcastgrp.s_addr);
1162 /* If an entry already exists, just update the fields */
1164 if (mrtdebug & DEBUG_MFC)
1165 log(LOG_DEBUG,"add_mfc update o %lx g %lx p %x\n",
1166 (u_long)ntohl(mfccp->mfcc_origin.s_addr),
1167 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
1168 mfccp->mfcc_parent);
1170 update_mfc_params(rt, mfccp);
1177 * Find the entry for which the upcall was made and update
1179 hash = MFCHASH(mfccp->mfcc_origin.s_addr, mfccp->mfcc_mcastgrp.s_addr);
1180 for (rt = mfctable[hash], nstl = 0; rt; rt = rt->mfc_next) {
1182 if ((rt->mfc_origin.s_addr == mfccp->mfcc_origin.s_addr) &&
1183 (rt->mfc_mcastgrp.s_addr == mfccp->mfcc_mcastgrp.s_addr) &&
1184 (rt->mfc_stall != NULL)) {
1187 log(LOG_ERR, "add_mfc %s o %lx g %lx p %x dbx %p\n",
1188 "multiple kernel entries",
1189 (u_long)ntohl(mfccp->mfcc_origin.s_addr),
1190 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
1191 mfccp->mfcc_parent, (void *)rt->mfc_stall);
1193 if (mrtdebug & DEBUG_MFC)
1194 log(LOG_DEBUG,"add_mfc o %lx g %lx p %x dbg %p\n",
1195 (u_long)ntohl(mfccp->mfcc_origin.s_addr),
1196 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
1197 mfccp->mfcc_parent, (void *)rt->mfc_stall);
1199 init_mfc_params(rt, mfccp);
1201 rt->mfc_expire = 0; /* Don't clean this guy up */
1204 /* free packets Qed at the end of this entry */
1205 for (rte = rt->mfc_stall; rte != NULL; ) {
1206 struct rtdetq *n = rte->next;
1208 ip_mdq(rte->m, rte->ifp, rt, -1);
1210 free(rte, M_MRTABLE);
1213 rt->mfc_stall = NULL;
1218 * It is possible that an entry is being inserted without an upcall
1221 if (mrtdebug & DEBUG_MFC)
1222 log(LOG_DEBUG,"add_mfc no upcall h %lu o %lx g %lx p %x\n",
1223 hash, (u_long)ntohl(mfccp->mfcc_origin.s_addr),
1224 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
1225 mfccp->mfcc_parent);
1227 for (rt = mfctable[hash]; rt != NULL; rt = rt->mfc_next) {
1228 if ((rt->mfc_origin.s_addr == mfccp->mfcc_origin.s_addr) &&
1229 (rt->mfc_mcastgrp.s_addr == mfccp->mfcc_mcastgrp.s_addr)) {
1230 init_mfc_params(rt, mfccp);
1237 if (rt == NULL) { /* no upcall, so make a new entry */
1238 rt = (struct mfc *)malloc(sizeof(*rt), M_MRTABLE, M_NOWAIT);
1245 init_mfc_params(rt, mfccp);
1247 rt->mfc_stall = NULL;
1249 rt->mfc_bw_meter = NULL;
1250 /* insert new entry at head of hash chain */
1251 rt->mfc_next = mfctable[hash];
1252 mfctable[hash] = rt;
1261 * Delete an mfc entry
1264 del_mfc(struct mfcctl2 *mfccp)
1266 struct in_addr origin;
1267 struct in_addr mcastgrp;
1271 struct bw_meter *list;
1273 origin = mfccp->mfcc_origin;
1274 mcastgrp = mfccp->mfcc_mcastgrp;
1276 if (mrtdebug & DEBUG_MFC)
1277 log(LOG_DEBUG,"del_mfc orig %lx mcastgrp %lx\n",
1278 (u_long)ntohl(origin.s_addr), (u_long)ntohl(mcastgrp.s_addr));
1282 hash = MFCHASH(origin.s_addr, mcastgrp.s_addr);
1283 for (nptr = &mfctable[hash]; (rt = *nptr) != NULL; nptr = &rt->mfc_next)
1284 if (origin.s_addr == rt->mfc_origin.s_addr &&
1285 mcastgrp.s_addr == rt->mfc_mcastgrp.s_addr &&
1286 rt->mfc_stall == NULL)
1290 return EADDRNOTAVAIL;
1293 *nptr = rt->mfc_next;
1296 * free the bw_meter entries
1298 list = rt->mfc_bw_meter;
1299 rt->mfc_bw_meter = NULL;
1301 free(rt, M_MRTABLE);
1311 * Send a message to mrouted on the multicast routing socket
1314 socket_send(struct socket *s, struct mbuf *mm, struct sockaddr_in *src)
1317 SOCKBUF_LOCK(&s->so_rcv);
1318 if (sbappendaddr_locked(&s->so_rcv, (struct sockaddr *)src, mm,
1320 sorwakeup_locked(s);
1323 SOCKBUF_UNLOCK(&s->so_rcv);
1330 * IP multicast forwarding function. This function assumes that the packet
1331 * pointed to by "ip" has arrived on (or is about to be sent to) the interface
1332 * pointed to by "ifp", and the packet is to be relayed to other networks
1333 * that have members of the packet's destination IP multicast group.
1335 * The packet is returned unscathed to the caller, unless it is
1336 * erroneous, in which case a non-zero return value tells the caller to
1340 #define TUNNEL_LEN 12 /* # bytes of IP option for tunnel encapsulation */
1343 X_ip_mforward(struct ip *ip, struct ifnet *ifp, struct mbuf *m,
1344 struct ip_moptions *imo)
1350 if (mrtdebug & DEBUG_FORWARD)
1351 log(LOG_DEBUG, "ip_mforward: src %lx, dst %lx, ifp %p\n",
1352 (u_long)ntohl(ip->ip_src.s_addr), (u_long)ntohl(ip->ip_dst.s_addr),
1355 if (ip->ip_hl < (sizeof(struct ip) + TUNNEL_LEN) >> 2 ||
1356 ((u_char *)(ip + 1))[1] != IPOPT_LSRR ) {
1358 * Packet arrived via a physical interface or
1359 * an encapsulated tunnel or a register_vif.
1363 * Packet arrived through a source-route tunnel.
1364 * Source-route tunnels are no longer supported.
1366 static int last_log;
1367 if (last_log != time_second) {
1368 last_log = time_second;
1370 "ip_mforward: received source-routed packet from %lx\n",
1371 (u_long)ntohl(ip->ip_src.s_addr));
1378 if (imo && ((vifi = imo->imo_multicast_vif) < numvifs)) {
1379 if (ip->ip_ttl < 255)
1380 ip->ip_ttl++; /* compensate for -1 in *_send routines */
1381 if (rsvpdebug && ip->ip_p == IPPROTO_RSVP) {
1382 struct vif *vifp = viftable + vifi;
1384 printf("Sending IPPROTO_RSVP from %lx to %lx on vif %d (%s%s)\n",
1385 (long)ntohl(ip->ip_src.s_addr), (long)ntohl(ip->ip_dst.s_addr),
1387 (vifp->v_flags & VIFF_TUNNEL) ? "tunnel on " : "",
1388 vifp->v_ifp->if_xname);
1390 error = ip_mdq(m, ifp, NULL, vifi);
1395 if (rsvpdebug && ip->ip_p == IPPROTO_RSVP) {
1396 printf("Warning: IPPROTO_RSVP from %lx to %lx without vif option\n",
1397 (long)ntohl(ip->ip_src.s_addr), (long)ntohl(ip->ip_dst.s_addr));
1399 printf("In fact, no options were specified at all\n");
1403 * Don't forward a packet with time-to-live of zero or one,
1404 * or a packet destined to a local-only group.
1406 if (ip->ip_ttl <= 1 || ntohl(ip->ip_dst.s_addr) <= INADDR_MAX_LOCAL_GROUP) {
1413 * Determine forwarding vifs from the forwarding cache table
1415 ++mrtstat.mrts_mfc_lookups;
1416 rt = mfc_find(ip->ip_src.s_addr, ip->ip_dst.s_addr);
1418 /* Entry exists, so forward if necessary */
1420 error = ip_mdq(m, ifp, rt, -1);
1426 * If we don't have a route for packet's origin,
1427 * Make a copy of the packet & send message to routing daemon
1433 int hlen = ip->ip_hl << 2;
1435 ++mrtstat.mrts_mfc_misses;
1437 mrtstat.mrts_no_route++;
1438 if (mrtdebug & (DEBUG_FORWARD | DEBUG_MFC))
1439 log(LOG_DEBUG, "ip_mforward: no rte s %lx g %lx\n",
1440 (u_long)ntohl(ip->ip_src.s_addr),
1441 (u_long)ntohl(ip->ip_dst.s_addr));
1444 * Allocate mbufs early so that we don't do extra work if we are
1445 * just going to fail anyway. Make sure to pullup the header so
1446 * that other people can't step on it.
1448 rte = (struct rtdetq *)malloc((sizeof *rte), M_MRTABLE, M_NOWAIT);
1454 mb0 = m_copypacket(m, M_DONTWAIT);
1455 if (mb0 && (M_HASCL(mb0) || mb0->m_len < hlen))
1456 mb0 = m_pullup(mb0, hlen);
1458 free(rte, M_MRTABLE);
1464 /* is there an upcall waiting for this flow ? */
1465 hash = MFCHASH(ip->ip_src.s_addr, ip->ip_dst.s_addr);
1466 for (rt = mfctable[hash]; rt; rt = rt->mfc_next) {
1467 if ((ip->ip_src.s_addr == rt->mfc_origin.s_addr) &&
1468 (ip->ip_dst.s_addr == rt->mfc_mcastgrp.s_addr) &&
1469 (rt->mfc_stall != NULL))
1476 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
1480 * Locate the vifi for the incoming interface for this packet.
1481 * If none found, drop packet.
1483 for (vifi=0; vifi < numvifs && viftable[vifi].v_ifp != ifp; vifi++)
1485 if (vifi >= numvifs) /* vif not found, drop packet */
1488 /* no upcall, so make a new entry */
1489 rt = (struct mfc *)malloc(sizeof(*rt), M_MRTABLE, M_NOWAIT);
1492 /* Make a copy of the header to send to the user level process */
1493 mm = m_copy(mb0, 0, hlen);
1498 * Send message to routing daemon to install
1499 * a route into the kernel table
1502 im = mtod(mm, struct igmpmsg *);
1503 im->im_msgtype = IGMPMSG_NOCACHE;
1507 mrtstat.mrts_upcalls++;
1509 k_igmpsrc.sin_addr = ip->ip_src;
1510 if (socket_send(ip_mrouter, mm, &k_igmpsrc) < 0) {
1511 log(LOG_WARNING, "ip_mforward: ip_mrouter socket queue full\n");
1512 ++mrtstat.mrts_upq_sockfull;
1514 free(rt, M_MRTABLE);
1516 free(rte, M_MRTABLE);
1523 /* insert new entry at head of hash chain */
1524 rt->mfc_origin.s_addr = ip->ip_src.s_addr;
1525 rt->mfc_mcastgrp.s_addr = ip->ip_dst.s_addr;
1526 rt->mfc_expire = UPCALL_EXPIRE;
1528 for (i = 0; i < numvifs; i++) {
1529 rt->mfc_ttls[i] = 0;
1530 rt->mfc_flags[i] = 0;
1532 rt->mfc_parent = -1;
1534 rt->mfc_rp.s_addr = INADDR_ANY; /* clear the RP address */
1536 rt->mfc_bw_meter = NULL;
1538 /* link into table */
1539 rt->mfc_next = mfctable[hash];
1540 mfctable[hash] = rt;
1541 rt->mfc_stall = rte;
1544 /* determine if q has overflowed */
1549 * XXX ouch! we need to append to the list, but we
1550 * only have a pointer to the front, so we have to
1551 * scan the entire list every time.
1553 for (p = &rt->mfc_stall; *p != NULL; p = &(*p)->next)
1556 if (npkts > MAX_UPQ) {
1557 mrtstat.mrts_upq_ovflw++;
1559 free(rte, M_MRTABLE);
1566 /* Add this entry to the end of the queue */
1582 * Clean up the cache entry if upcall is not serviced
1585 expire_upcalls(void *unused)
1588 struct mfc *mfc, **nptr;
1592 for (i = 0; i < MFCTBLSIZ; i++) {
1593 if (nexpire[i] == 0)
1595 nptr = &mfctable[i];
1596 for (mfc = *nptr; mfc != NULL; mfc = *nptr) {
1598 * Skip real cache entries
1599 * Make sure it wasn't marked to not expire (shouldn't happen)
1602 if (mfc->mfc_stall != NULL && mfc->mfc_expire != 0 &&
1603 --mfc->mfc_expire == 0) {
1604 if (mrtdebug & DEBUG_EXPIRE)
1605 log(LOG_DEBUG, "expire_upcalls: expiring (%lx %lx)\n",
1606 (u_long)ntohl(mfc->mfc_origin.s_addr),
1607 (u_long)ntohl(mfc->mfc_mcastgrp.s_addr));
1609 * drop all the packets
1610 * free the mbuf with the pkt, if, timing info
1612 for (rte = mfc->mfc_stall; rte; ) {
1613 struct rtdetq *n = rte->next;
1616 free(rte, M_MRTABLE);
1619 ++mrtstat.mrts_cache_cleanups;
1623 * free the bw_meter entries
1625 while (mfc->mfc_bw_meter != NULL) {
1626 struct bw_meter *x = mfc->mfc_bw_meter;
1628 mfc->mfc_bw_meter = x->bm_mfc_next;
1632 *nptr = mfc->mfc_next;
1633 free(mfc, M_MRTABLE);
1635 nptr = &mfc->mfc_next;
1641 callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT, expire_upcalls, NULL);
1645 * Packet forwarding routine once entry in the cache is made
1648 ip_mdq(struct mbuf *m, struct ifnet *ifp, struct mfc *rt, vifi_t xmt_vif)
1650 struct ip *ip = mtod(m, struct ip *);
1652 int plen = ip->ip_len;
1656 * Macro to send packet on vif. Since RSVP packets don't get counted on
1657 * input, they shouldn't get counted on output, so statistics keeping is
1660 #define MC_SEND(ip,vifp,m) { \
1661 if ((vifp)->v_flags & VIFF_TUNNEL) \
1662 encap_send((ip), (vifp), (m)); \
1664 phyint_send((ip), (vifp), (m)); \
1668 * If xmt_vif is not -1, send on only the requested vif.
1670 * (since vifi_t is u_short, -1 becomes MAXUSHORT, which > numvifs.)
1672 if (xmt_vif < numvifs) {
1674 if (viftable[xmt_vif].v_flags & VIFF_REGISTER)
1675 pim_register_send(ip, viftable + xmt_vif, m, rt);
1678 MC_SEND(ip, viftable + xmt_vif, m);
1683 * Don't forward if it didn't arrive from the parent vif for its origin.
1685 vifi = rt->mfc_parent;
1686 if ((vifi >= numvifs) || (viftable[vifi].v_ifp != ifp)) {
1687 /* came in the wrong interface */
1688 if (mrtdebug & DEBUG_FORWARD)
1689 log(LOG_DEBUG, "wrong if: ifp %p vifi %d vififp %p\n",
1690 (void *)ifp, vifi, (void *)viftable[vifi].v_ifp);
1691 ++mrtstat.mrts_wrong_if;
1694 * If we are doing PIM assert processing, send a message
1695 * to the routing daemon.
1697 * XXX: A PIM-SM router needs the WRONGVIF detection so it
1698 * can complete the SPT switch, regardless of the type
1699 * of the iif (broadcast media, GRE tunnel, etc).
1701 if (pim_assert && (vifi < numvifs) && viftable[vifi].v_ifp) {
1706 if (ifp == &multicast_register_if)
1707 pimstat.pims_rcv_registers_wrongiif++;
1710 /* Get vifi for the incoming packet */
1711 for (vifi=0; vifi < numvifs && viftable[vifi].v_ifp != ifp; vifi++)
1713 if (vifi >= numvifs)
1714 return 0; /* The iif is not found: ignore the packet. */
1716 if (rt->mfc_flags[vifi] & MRT_MFC_FLAGS_DISABLE_WRONGVIF)
1717 return 0; /* WRONGVIF disabled: ignore the packet */
1721 TV_DELTA(rt->mfc_last_assert, now, delta);
1723 if (delta > ASSERT_MSG_TIME) {
1724 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
1726 int hlen = ip->ip_hl << 2;
1727 struct mbuf *mm = m_copy(m, 0, hlen);
1729 if (mm && (M_HASCL(mm) || mm->m_len < hlen))
1730 mm = m_pullup(mm, hlen);
1734 rt->mfc_last_assert = now;
1736 im = mtod(mm, struct igmpmsg *);
1737 im->im_msgtype = IGMPMSG_WRONGVIF;
1741 mrtstat.mrts_upcalls++;
1743 k_igmpsrc.sin_addr = im->im_src;
1744 if (socket_send(ip_mrouter, mm, &k_igmpsrc) < 0) {
1746 "ip_mforward: ip_mrouter socket queue full\n");
1747 ++mrtstat.mrts_upq_sockfull;
1755 /* If I sourced this packet, it counts as output, else it was input. */
1756 if (ip->ip_src.s_addr == viftable[vifi].v_lcl_addr.s_addr) {
1757 viftable[vifi].v_pkt_out++;
1758 viftable[vifi].v_bytes_out += plen;
1760 viftable[vifi].v_pkt_in++;
1761 viftable[vifi].v_bytes_in += plen;
1764 rt->mfc_byte_cnt += plen;
1767 * For each vif, decide if a copy of the packet should be forwarded.
1769 * - the ttl exceeds the vif's threshold
1770 * - there are group members downstream on interface
1772 for (vifi = 0; vifi < numvifs; vifi++)
1773 if ((rt->mfc_ttls[vifi] > 0) && (ip->ip_ttl > rt->mfc_ttls[vifi])) {
1774 viftable[vifi].v_pkt_out++;
1775 viftable[vifi].v_bytes_out += plen;
1777 if (viftable[vifi].v_flags & VIFF_REGISTER)
1778 pim_register_send(ip, viftable + vifi, m, rt);
1781 MC_SEND(ip, viftable+vifi, m);
1785 * Perform upcall-related bw measuring.
1787 if (rt->mfc_bw_meter != NULL) {
1793 for (x = rt->mfc_bw_meter; x != NULL; x = x->bm_mfc_next)
1794 bw_meter_receive_packet(x, plen, &now);
1801 * check if a vif number is legal/ok. This is used by ip_output.
1804 X_legal_vif_num(int vif)
1806 /* XXX unlocked, matter? */
1807 return (vif >= 0 && vif < numvifs);
1811 * Return the local address used by this vif
1814 X_ip_mcast_src(int vifi)
1816 /* XXX unlocked, matter? */
1817 if (vifi >= 0 && vifi < numvifs)
1818 return viftable[vifi].v_lcl_addr.s_addr;
1824 phyint_send(struct ip *ip, struct vif *vifp, struct mbuf *m)
1826 struct mbuf *mb_copy;
1827 int hlen = ip->ip_hl << 2;
1832 * Make a new reference to the packet; make sure that
1833 * the IP header is actually copied, not just referenced,
1834 * so that ip_output() only scribbles on the copy.
1836 mb_copy = m_copypacket(m, M_DONTWAIT);
1837 if (mb_copy && (M_HASCL(mb_copy) || mb_copy->m_len < hlen))
1838 mb_copy = m_pullup(mb_copy, hlen);
1839 if (mb_copy == NULL)
1842 if (vifp->v_rate_limit == 0)
1843 tbf_send_packet(vifp, mb_copy);
1845 tbf_control(vifp, mb_copy, mtod(mb_copy, struct ip *), ip->ip_len);
1849 encap_send(struct ip *ip, struct vif *vifp, struct mbuf *m)
1851 struct mbuf *mb_copy;
1853 int i, len = ip->ip_len;
1857 /* Take care of delayed checksums */
1858 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
1859 in_delayed_cksum(m);
1860 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
1864 * copy the old packet & pullup its IP header into the
1865 * new mbuf so we can modify it. Try to fill the new
1866 * mbuf since if we don't the ethernet driver will.
1868 MGETHDR(mb_copy, M_DONTWAIT, MT_HEADER);
1869 if (mb_copy == NULL)
1872 mac_create_mbuf_multicast_encap(m, vifp->v_ifp, mb_copy);
1874 mb_copy->m_data += max_linkhdr;
1875 mb_copy->m_len = sizeof(multicast_encap_iphdr);
1877 if ((mb_copy->m_next = m_copypacket(m, M_DONTWAIT)) == NULL) {
1881 i = MHLEN - M_LEADINGSPACE(mb_copy);
1884 mb_copy = m_pullup(mb_copy, i);
1885 if (mb_copy == NULL)
1887 mb_copy->m_pkthdr.len = len + sizeof(multicast_encap_iphdr);
1890 * fill in the encapsulating IP header.
1892 ip_copy = mtod(mb_copy, struct ip *);
1893 *ip_copy = multicast_encap_iphdr;
1894 ip_copy->ip_id = ip_newid();
1895 ip_copy->ip_len += len;
1896 ip_copy->ip_src = vifp->v_lcl_addr;
1897 ip_copy->ip_dst = vifp->v_rmt_addr;
1900 * turn the encapsulated IP header back into a valid one.
1902 ip = (struct ip *)((caddr_t)ip_copy + sizeof(multicast_encap_iphdr));
1904 ip->ip_len = htons(ip->ip_len);
1905 ip->ip_off = htons(ip->ip_off);
1907 mb_copy->m_data += sizeof(multicast_encap_iphdr);
1908 ip->ip_sum = in_cksum(mb_copy, ip->ip_hl << 2);
1909 mb_copy->m_data -= sizeof(multicast_encap_iphdr);
1911 if (vifp->v_rate_limit == 0)
1912 tbf_send_packet(vifp, mb_copy);
1914 tbf_control(vifp, mb_copy, ip, ip_copy->ip_len);
1918 * Token bucket filter module
1922 tbf_control(struct vif *vifp, struct mbuf *m, struct ip *ip, u_long p_len)
1924 struct tbf *t = vifp->v_tbf;
1928 if (p_len > MAX_BKT_SIZE) { /* drop if packet is too large */
1929 mrtstat.mrts_pkt2large++;
1934 tbf_update_tokens(vifp);
1936 if (t->tbf_q_len == 0) { /* queue empty... */
1937 if (p_len <= t->tbf_n_tok) { /* send packet if enough tokens */
1938 t->tbf_n_tok -= p_len;
1939 tbf_send_packet(vifp, m);
1940 } else { /* no, queue packet and try later */
1942 callout_reset(&tbf_reprocess_ch, TBF_REPROCESS,
1943 tbf_reprocess_q, vifp);
1945 } else if (t->tbf_q_len < t->tbf_max_q_len) {
1946 /* finite queue length, so queue pkts and process queue */
1948 tbf_process_q(vifp);
1950 /* queue full, try to dq and queue and process */
1951 if (!tbf_dq_sel(vifp, ip)) {
1952 mrtstat.mrts_q_overflow++;
1956 tbf_process_q(vifp);
1962 * adds a packet to the queue at the interface
1965 tbf_queue(struct vif *vifp, struct mbuf *m)
1967 struct tbf *t = vifp->v_tbf;
1971 if (t->tbf_t == NULL) /* Queue was empty */
1973 else /* Insert at tail */
1974 t->tbf_t->m_act = m;
1976 t->tbf_t = m; /* Set new tail pointer */
1979 /* Make sure we didn't get fed a bogus mbuf */
1981 panic("tbf_queue: m_act");
1989 * processes the queue at the interface
1992 tbf_process_q(struct vif *vifp)
1994 struct tbf *t = vifp->v_tbf;
1998 /* loop through the queue at the interface and send as many packets
2001 while (t->tbf_q_len > 0) {
2002 struct mbuf *m = t->tbf_q;
2003 int len = mtod(m, struct ip *)->ip_len;
2005 /* determine if the packet can be sent */
2006 if (len > t->tbf_n_tok) /* not enough tokens, we are done */
2008 /* ok, reduce no of tokens, dequeue and send the packet. */
2009 t->tbf_n_tok -= len;
2011 t->tbf_q = m->m_act;
2012 if (--t->tbf_q_len == 0)
2016 tbf_send_packet(vifp, m);
2021 tbf_reprocess_q(void *xvifp)
2023 struct vif *vifp = xvifp;
2025 if (ip_mrouter == NULL)
2028 tbf_update_tokens(vifp);
2029 tbf_process_q(vifp);
2030 if (vifp->v_tbf->tbf_q_len)
2031 callout_reset(&tbf_reprocess_ch, TBF_REPROCESS, tbf_reprocess_q, vifp);
2035 /* function that will selectively discard a member of the queue
2036 * based on the precedence value and the priority
2039 tbf_dq_sel(struct vif *vifp, struct ip *ip)
2042 struct mbuf *m, *last;
2044 struct tbf *t = vifp->v_tbf;
2048 p = priority(vifp, ip);
2052 while ((m = *np) != NULL) {
2053 if (p > priority(vifp, mtod(m, struct ip *))) {
2055 /* If we're removing the last packet, fix the tail pointer */
2059 /* It's impossible for the queue to be empty, but check anyways. */
2060 if (--t->tbf_q_len == 0)
2062 mrtstat.mrts_drop_sel++;
2072 tbf_send_packet(struct vif *vifp, struct mbuf *m)
2076 if (vifp->v_flags & VIFF_TUNNEL) /* If tunnel options */
2077 ip_output(m, NULL, &vifp->v_route, IP_FORWARDING, NULL, NULL);
2079 struct ip_moptions imo;
2081 static struct route ro; /* XXX check this */
2083 imo.imo_multicast_ifp = vifp->v_ifp;
2084 imo.imo_multicast_ttl = mtod(m, struct ip *)->ip_ttl - 1;
2085 imo.imo_multicast_loop = 1;
2086 imo.imo_multicast_vif = -1;
2089 * Re-entrancy should not be a problem here, because
2090 * the packets that we send out and are looped back at us
2091 * should get rejected because they appear to come from
2092 * the loopback interface, thus preventing looping.
2094 error = ip_output(m, NULL, &ro, IP_FORWARDING, &imo, NULL);
2096 if (mrtdebug & DEBUG_XMIT)
2097 log(LOG_DEBUG, "phyint_send on vif %d err %d\n",
2098 (int)(vifp - viftable), error);
2102 /* determine the current time and then
2103 * the elapsed time (between the last time and time now)
2104 * in milliseconds & update the no. of tokens in the bucket
2107 tbf_update_tokens(struct vif *vifp)
2111 struct tbf *t = vifp->v_tbf;
2117 TV_DELTA(tp, t->tbf_last_pkt_t, tm);
2120 * This formula is actually
2121 * "time in seconds" * "bytes/second".
2123 * (tm / 1000000) * (v_rate_limit * 1000 * (1000/1024) / 8)
2125 * The (1000/1024) was introduced in add_vif to optimize
2126 * this divide into a shift.
2128 t->tbf_n_tok += tm * vifp->v_rate_limit / 1024 / 8;
2129 t->tbf_last_pkt_t = tp;
2131 if (t->tbf_n_tok > MAX_BKT_SIZE)
2132 t->tbf_n_tok = MAX_BKT_SIZE;
2136 priority(struct vif *vifp, struct ip *ip)
2138 int prio = 50; /* the lowest priority -- default case */
2140 /* temporary hack; may add general packet classifier some day */
2143 * The UDP port space is divided up into four priority ranges:
2144 * [0, 16384) : unclassified - lowest priority
2145 * [16384, 32768) : audio - highest priority
2146 * [32768, 49152) : whiteboard - medium priority
2147 * [49152, 65536) : video - low priority
2149 * Everything else gets lowest priority.
2151 if (ip->ip_p == IPPROTO_UDP) {
2152 struct udphdr *udp = (struct udphdr *)(((char *)ip) + (ip->ip_hl << 2));
2153 switch (ntohs(udp->uh_dport) & 0xc000) {
2169 * End of token bucket filter modifications
2173 X_ip_rsvp_vif(struct socket *so, struct sockopt *sopt)
2177 if (so->so_type != SOCK_RAW || so->so_proto->pr_protocol != IPPROTO_RSVP)
2180 error = sooptcopyin(sopt, &vifi, sizeof vifi, sizeof vifi);
2186 if (vifi < 0 || vifi >= numvifs) { /* Error if vif is invalid */
2188 return EADDRNOTAVAIL;
2191 if (sopt->sopt_name == IP_RSVP_VIF_ON) {
2192 /* Check if socket is available. */
2193 if (viftable[vifi].v_rsvpd != NULL) {
2198 viftable[vifi].v_rsvpd = so;
2199 /* This may seem silly, but we need to be sure we don't over-increment
2200 * the RSVP counter, in case something slips up.
2202 if (!viftable[vifi].v_rsvp_on) {
2203 viftable[vifi].v_rsvp_on = 1;
2206 } else { /* must be VIF_OFF */
2208 * XXX as an additional consistency check, one could make sure
2209 * that viftable[vifi].v_rsvpd == so, otherwise passing so as
2210 * first parameter is pretty useless.
2212 viftable[vifi].v_rsvpd = NULL;
2214 * This may seem silly, but we need to be sure we don't over-decrement
2215 * the RSVP counter, in case something slips up.
2217 if (viftable[vifi].v_rsvp_on) {
2218 viftable[vifi].v_rsvp_on = 0;
2227 X_ip_rsvp_force_done(struct socket *so)
2231 /* Don't bother if it is not the right type of socket. */
2232 if (so->so_type != SOCK_RAW || so->so_proto->pr_protocol != IPPROTO_RSVP)
2237 /* The socket may be attached to more than one vif...this
2238 * is perfectly legal.
2240 for (vifi = 0; vifi < numvifs; vifi++) {
2241 if (viftable[vifi].v_rsvpd == so) {
2242 viftable[vifi].v_rsvpd = NULL;
2243 /* This may seem silly, but we need to be sure we don't
2244 * over-decrement the RSVP counter, in case something slips up.
2246 if (viftable[vifi].v_rsvp_on) {
2247 viftable[vifi].v_rsvp_on = 0;
2257 X_rsvp_input(struct mbuf *m, int off)
2260 struct ip *ip = mtod(m, struct ip *);
2261 struct sockaddr_in rsvp_src = { sizeof rsvp_src, AF_INET };
2265 printf("rsvp_input: rsvp_on %d\n",rsvp_on);
2267 /* Can still get packets with rsvp_on = 0 if there is a local member
2268 * of the group to which the RSVP packet is addressed. But in this
2269 * case we want to throw the packet away.
2277 printf("rsvp_input: check vifs\n");
2283 ifp = m->m_pkthdr.rcvif;
2286 /* Find which vif the packet arrived on. */
2287 for (vifi = 0; vifi < numvifs; vifi++)
2288 if (viftable[vifi].v_ifp == ifp)
2291 if (vifi == numvifs || viftable[vifi].v_rsvpd == NULL) {
2293 * Drop the lock here to avoid holding it across rip_input.
2294 * This could make rsvpdebug printfs wrong. If you care,
2295 * record the state of stuff before dropping the lock.
2299 * If the old-style non-vif-associated socket is set,
2300 * then use it. Otherwise, drop packet since there
2301 * is no specific socket for this vif.
2303 if (ip_rsvpd != NULL) {
2305 printf("rsvp_input: Sending packet up old-style socket\n");
2306 rip_input(m, off); /* xxx */
2308 if (rsvpdebug && vifi == numvifs)
2309 printf("rsvp_input: Can't find vif for packet.\n");
2310 else if (rsvpdebug && viftable[vifi].v_rsvpd == NULL)
2311 printf("rsvp_input: No socket defined for vif %d\n",vifi);
2316 rsvp_src.sin_addr = ip->ip_src;
2319 printf("rsvp_input: m->m_len = %d, sbspace() = %ld\n",
2320 m->m_len,sbspace(&(viftable[vifi].v_rsvpd->so_rcv)));
2322 if (socket_send(viftable[vifi].v_rsvpd, m, &rsvp_src) < 0) {
2324 printf("rsvp_input: Failed to append to socket\n");
2327 printf("rsvp_input: send packet up\n");
2333 * Code for bandwidth monitors
2337 * Define common interface for timeval-related methods
2339 #define BW_TIMEVALCMP(tvp, uvp, cmp) timevalcmp((tvp), (uvp), cmp)
2340 #define BW_TIMEVALDECR(vvp, uvp) timevalsub((vvp), (uvp))
2341 #define BW_TIMEVALADD(vvp, uvp) timevaladd((vvp), (uvp))
2344 compute_bw_meter_flags(struct bw_upcall *req)
2348 if (req->bu_flags & BW_UPCALL_UNIT_PACKETS)
2349 flags |= BW_METER_UNIT_PACKETS;
2350 if (req->bu_flags & BW_UPCALL_UNIT_BYTES)
2351 flags |= BW_METER_UNIT_BYTES;
2352 if (req->bu_flags & BW_UPCALL_GEQ)
2353 flags |= BW_METER_GEQ;
2354 if (req->bu_flags & BW_UPCALL_LEQ)
2355 flags |= BW_METER_LEQ;
2361 * Add a bw_meter entry
2364 add_bw_upcall(struct bw_upcall *req)
2367 struct timeval delta = { BW_UPCALL_THRESHOLD_INTERVAL_MIN_SEC,
2368 BW_UPCALL_THRESHOLD_INTERVAL_MIN_USEC };
2373 if (!(mrt_api_config & MRT_MFC_BW_UPCALL))
2376 /* Test if the flags are valid */
2377 if (!(req->bu_flags & (BW_UPCALL_UNIT_PACKETS | BW_UPCALL_UNIT_BYTES)))
2379 if (!(req->bu_flags & (BW_UPCALL_GEQ | BW_UPCALL_LEQ)))
2381 if ((req->bu_flags & (BW_UPCALL_GEQ | BW_UPCALL_LEQ))
2382 == (BW_UPCALL_GEQ | BW_UPCALL_LEQ))
2385 /* Test if the threshold time interval is valid */
2386 if (BW_TIMEVALCMP(&req->bu_threshold.b_time, &delta, <))
2389 flags = compute_bw_meter_flags(req);
2392 * Find if we have already same bw_meter entry
2395 mfc = mfc_find(req->bu_src.s_addr, req->bu_dst.s_addr);
2398 return EADDRNOTAVAIL;
2400 for (x = mfc->mfc_bw_meter; x != NULL; x = x->bm_mfc_next) {
2401 if ((BW_TIMEVALCMP(&x->bm_threshold.b_time,
2402 &req->bu_threshold.b_time, ==)) &&
2403 (x->bm_threshold.b_packets == req->bu_threshold.b_packets) &&
2404 (x->bm_threshold.b_bytes == req->bu_threshold.b_bytes) &&
2405 (x->bm_flags & BW_METER_USER_FLAGS) == flags) {
2407 return 0; /* XXX Already installed */
2411 /* Allocate the new bw_meter entry */
2412 x = (struct bw_meter *)malloc(sizeof(*x), M_BWMETER, M_NOWAIT);
2418 /* Set the new bw_meter entry */
2419 x->bm_threshold.b_time = req->bu_threshold.b_time;
2421 x->bm_start_time = now;
2422 x->bm_threshold.b_packets = req->bu_threshold.b_packets;
2423 x->bm_threshold.b_bytes = req->bu_threshold.b_bytes;
2424 x->bm_measured.b_packets = 0;
2425 x->bm_measured.b_bytes = 0;
2426 x->bm_flags = flags;
2427 x->bm_time_next = NULL;
2428 x->bm_time_hash = BW_METER_BUCKETS;
2430 /* Add the new bw_meter entry to the front of entries for this MFC */
2432 x->bm_mfc_next = mfc->mfc_bw_meter;
2433 mfc->mfc_bw_meter = x;
2434 schedule_bw_meter(x, &now);
2441 free_bw_list(struct bw_meter *list)
2443 while (list != NULL) {
2444 struct bw_meter *x = list;
2446 list = list->bm_mfc_next;
2447 unschedule_bw_meter(x);
2453 * Delete one or multiple bw_meter entries
2456 del_bw_upcall(struct bw_upcall *req)
2461 if (!(mrt_api_config & MRT_MFC_BW_UPCALL))
2465 /* Find the corresponding MFC entry */
2466 mfc = mfc_find(req->bu_src.s_addr, req->bu_dst.s_addr);
2469 return EADDRNOTAVAIL;
2470 } else if (req->bu_flags & BW_UPCALL_DELETE_ALL) {
2472 * Delete all bw_meter entries for this mfc
2474 struct bw_meter *list;
2476 list = mfc->mfc_bw_meter;
2477 mfc->mfc_bw_meter = NULL;
2481 } else { /* Delete a single bw_meter entry */
2482 struct bw_meter *prev;
2485 flags = compute_bw_meter_flags(req);
2487 /* Find the bw_meter entry to delete */
2488 for (prev = NULL, x = mfc->mfc_bw_meter; x != NULL;
2489 prev = x, x = x->bm_mfc_next) {
2490 if ((BW_TIMEVALCMP(&x->bm_threshold.b_time,
2491 &req->bu_threshold.b_time, ==)) &&
2492 (x->bm_threshold.b_packets == req->bu_threshold.b_packets) &&
2493 (x->bm_threshold.b_bytes == req->bu_threshold.b_bytes) &&
2494 (x->bm_flags & BW_METER_USER_FLAGS) == flags)
2497 if (x != NULL) { /* Delete entry from the list for this MFC */
2499 prev->bm_mfc_next = x->bm_mfc_next; /* remove from middle*/
2501 x->bm_mfc->mfc_bw_meter = x->bm_mfc_next;/* new head of list */
2503 unschedule_bw_meter(x);
2505 /* Free the bw_meter entry */
2517 * Perform bandwidth measurement processing that may result in an upcall
2520 bw_meter_receive_packet(struct bw_meter *x, int plen, struct timeval *nowp)
2522 struct timeval delta;
2527 BW_TIMEVALDECR(&delta, &x->bm_start_time);
2529 if (x->bm_flags & BW_METER_GEQ) {
2531 * Processing for ">=" type of bw_meter entry
2533 if (BW_TIMEVALCMP(&delta, &x->bm_threshold.b_time, >)) {
2534 /* Reset the bw_meter entry */
2535 x->bm_start_time = *nowp;
2536 x->bm_measured.b_packets = 0;
2537 x->bm_measured.b_bytes = 0;
2538 x->bm_flags &= ~BW_METER_UPCALL_DELIVERED;
2541 /* Record that a packet is received */
2542 x->bm_measured.b_packets++;
2543 x->bm_measured.b_bytes += plen;
2546 * Test if we should deliver an upcall
2548 if (!(x->bm_flags & BW_METER_UPCALL_DELIVERED)) {
2549 if (((x->bm_flags & BW_METER_UNIT_PACKETS) &&
2550 (x->bm_measured.b_packets >= x->bm_threshold.b_packets)) ||
2551 ((x->bm_flags & BW_METER_UNIT_BYTES) &&
2552 (x->bm_measured.b_bytes >= x->bm_threshold.b_bytes))) {
2553 /* Prepare an upcall for delivery */
2554 bw_meter_prepare_upcall(x, nowp);
2555 x->bm_flags |= BW_METER_UPCALL_DELIVERED;
2558 } else if (x->bm_flags & BW_METER_LEQ) {
2560 * Processing for "<=" type of bw_meter entry
2562 if (BW_TIMEVALCMP(&delta, &x->bm_threshold.b_time, >)) {
2564 * We are behind time with the multicast forwarding table
2565 * scanning for "<=" type of bw_meter entries, so test now
2566 * if we should deliver an upcall.
2568 if (((x->bm_flags & BW_METER_UNIT_PACKETS) &&
2569 (x->bm_measured.b_packets <= x->bm_threshold.b_packets)) ||
2570 ((x->bm_flags & BW_METER_UNIT_BYTES) &&
2571 (x->bm_measured.b_bytes <= x->bm_threshold.b_bytes))) {
2572 /* Prepare an upcall for delivery */
2573 bw_meter_prepare_upcall(x, nowp);
2575 /* Reschedule the bw_meter entry */
2576 unschedule_bw_meter(x);
2577 schedule_bw_meter(x, nowp);
2580 /* Record that a packet is received */
2581 x->bm_measured.b_packets++;
2582 x->bm_measured.b_bytes += plen;
2585 * Test if we should restart the measuring interval
2587 if ((x->bm_flags & BW_METER_UNIT_PACKETS &&
2588 x->bm_measured.b_packets <= x->bm_threshold.b_packets) ||
2589 (x->bm_flags & BW_METER_UNIT_BYTES &&
2590 x->bm_measured.b_bytes <= x->bm_threshold.b_bytes)) {
2591 /* Don't restart the measuring interval */
2593 /* Do restart the measuring interval */
2595 * XXX: note that we don't unschedule and schedule, because this
2596 * might be too much overhead per packet. Instead, when we process
2597 * all entries for a given timer hash bin, we check whether it is
2598 * really a timeout. If not, we reschedule at that time.
2600 x->bm_start_time = *nowp;
2601 x->bm_measured.b_packets = 0;
2602 x->bm_measured.b_bytes = 0;
2603 x->bm_flags &= ~BW_METER_UPCALL_DELIVERED;
2609 * Prepare a bandwidth-related upcall
2612 bw_meter_prepare_upcall(struct bw_meter *x, struct timeval *nowp)
2614 struct timeval delta;
2615 struct bw_upcall *u;
2620 * Compute the measured time interval
2623 BW_TIMEVALDECR(&delta, &x->bm_start_time);
2626 * If there are too many pending upcalls, deliver them now
2628 if (bw_upcalls_n >= BW_UPCALLS_MAX)
2632 * Set the bw_upcall entry
2634 u = &bw_upcalls[bw_upcalls_n++];
2635 u->bu_src = x->bm_mfc->mfc_origin;
2636 u->bu_dst = x->bm_mfc->mfc_mcastgrp;
2637 u->bu_threshold.b_time = x->bm_threshold.b_time;
2638 u->bu_threshold.b_packets = x->bm_threshold.b_packets;
2639 u->bu_threshold.b_bytes = x->bm_threshold.b_bytes;
2640 u->bu_measured.b_time = delta;
2641 u->bu_measured.b_packets = x->bm_measured.b_packets;
2642 u->bu_measured.b_bytes = x->bm_measured.b_bytes;
2644 if (x->bm_flags & BW_METER_UNIT_PACKETS)
2645 u->bu_flags |= BW_UPCALL_UNIT_PACKETS;
2646 if (x->bm_flags & BW_METER_UNIT_BYTES)
2647 u->bu_flags |= BW_UPCALL_UNIT_BYTES;
2648 if (x->bm_flags & BW_METER_GEQ)
2649 u->bu_flags |= BW_UPCALL_GEQ;
2650 if (x->bm_flags & BW_METER_LEQ)
2651 u->bu_flags |= BW_UPCALL_LEQ;
2655 * Send the pending bandwidth-related upcalls
2658 bw_upcalls_send(void)
2661 int len = bw_upcalls_n * sizeof(bw_upcalls[0]);
2662 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
2663 static struct igmpmsg igmpmsg = { 0, /* unused1 */
2665 IGMPMSG_BW_UPCALL,/* im_msgtype */
2670 { 0 } }; /* im_dst */
2674 if (bw_upcalls_n == 0)
2675 return; /* No pending upcalls */
2680 * Allocate a new mbuf, initialize it with the header and
2681 * the payload for the pending calls.
2683 MGETHDR(m, M_DONTWAIT, MT_HEADER);
2685 log(LOG_WARNING, "bw_upcalls_send: cannot allocate mbuf\n");
2689 m->m_len = m->m_pkthdr.len = 0;
2690 m_copyback(m, 0, sizeof(struct igmpmsg), (caddr_t)&igmpmsg);
2691 m_copyback(m, sizeof(struct igmpmsg), len, (caddr_t)&bw_upcalls[0]);
2695 * XXX do we need to set the address in k_igmpsrc ?
2697 mrtstat.mrts_upcalls++;
2698 if (socket_send(ip_mrouter, m, &k_igmpsrc) < 0) {
2699 log(LOG_WARNING, "bw_upcalls_send: ip_mrouter socket queue full\n");
2700 ++mrtstat.mrts_upq_sockfull;
2705 * Compute the timeout hash value for the bw_meter entries
2707 #define BW_METER_TIMEHASH(bw_meter, hash) \
2709 struct timeval next_timeval = (bw_meter)->bm_start_time; \
2711 BW_TIMEVALADD(&next_timeval, &(bw_meter)->bm_threshold.b_time); \
2712 (hash) = next_timeval.tv_sec; \
2713 if (next_timeval.tv_usec) \
2714 (hash)++; /* XXX: make sure we don't timeout early */ \
2715 (hash) %= BW_METER_BUCKETS; \
2719 * Schedule a timer to process periodically bw_meter entry of type "<="
2720 * by linking the entry in the proper hash bucket.
2723 schedule_bw_meter(struct bw_meter *x, struct timeval *nowp)
2729 if (!(x->bm_flags & BW_METER_LEQ))
2730 return; /* XXX: we schedule timers only for "<=" entries */
2733 * Reset the bw_meter entry
2735 x->bm_start_time = *nowp;
2736 x->bm_measured.b_packets = 0;
2737 x->bm_measured.b_bytes = 0;
2738 x->bm_flags &= ~BW_METER_UPCALL_DELIVERED;
2741 * Compute the timeout hash value and insert the entry
2743 BW_METER_TIMEHASH(x, time_hash);
2744 x->bm_time_next = bw_meter_timers[time_hash];
2745 bw_meter_timers[time_hash] = x;
2746 x->bm_time_hash = time_hash;
2750 * Unschedule the periodic timer that processes bw_meter entry of type "<="
2751 * by removing the entry from the proper hash bucket.
2754 unschedule_bw_meter(struct bw_meter *x)
2757 struct bw_meter *prev, *tmp;
2761 if (!(x->bm_flags & BW_METER_LEQ))
2762 return; /* XXX: we schedule timers only for "<=" entries */
2765 * Compute the timeout hash value and delete the entry
2767 time_hash = x->bm_time_hash;
2768 if (time_hash >= BW_METER_BUCKETS)
2769 return; /* Entry was not scheduled */
2771 for (prev = NULL, tmp = bw_meter_timers[time_hash];
2772 tmp != NULL; prev = tmp, tmp = tmp->bm_time_next)
2777 panic("unschedule_bw_meter: bw_meter entry not found");
2780 prev->bm_time_next = x->bm_time_next;
2782 bw_meter_timers[time_hash] = x->bm_time_next;
2784 x->bm_time_next = NULL;
2785 x->bm_time_hash = BW_METER_BUCKETS;
2790 * Process all "<=" type of bw_meter that should be processed now,
2791 * and for each entry prepare an upcall if necessary. Each processed
2792 * entry is rescheduled again for the (periodic) processing.
2794 * This is run periodically (once per second normally). On each round,
2795 * all the potentially matching entries are in the hash slot that we are
2801 static uint32_t last_tv_sec; /* last time we processed this */
2805 struct timeval now, process_endtime;
2808 if (last_tv_sec == now.tv_sec)
2809 return; /* nothing to do */
2811 loops = now.tv_sec - last_tv_sec;
2812 last_tv_sec = now.tv_sec;
2813 if (loops > BW_METER_BUCKETS)
2814 loops = BW_METER_BUCKETS;
2818 * Process all bins of bw_meter entries from the one after the last
2819 * processed to the current one. On entry, i points to the last bucket
2820 * visited, so we need to increment i at the beginning of the loop.
2822 for (i = (now.tv_sec - loops) % BW_METER_BUCKETS; loops > 0; loops--) {
2823 struct bw_meter *x, *tmp_list;
2825 if (++i >= BW_METER_BUCKETS)
2828 /* Disconnect the list of bw_meter entries from the bin */
2829 tmp_list = bw_meter_timers[i];
2830 bw_meter_timers[i] = NULL;
2832 /* Process the list of bw_meter entries */
2833 while (tmp_list != NULL) {
2835 tmp_list = tmp_list->bm_time_next;
2837 /* Test if the time interval is over */
2838 process_endtime = x->bm_start_time;
2839 BW_TIMEVALADD(&process_endtime, &x->bm_threshold.b_time);
2840 if (BW_TIMEVALCMP(&process_endtime, &now, >)) {
2841 /* Not yet: reschedule, but don't reset */
2844 BW_METER_TIMEHASH(x, time_hash);
2845 if (time_hash == i && process_endtime.tv_sec == now.tv_sec) {
2847 * XXX: somehow the bin processing is a bit ahead of time.
2848 * Put the entry in the next bin.
2850 if (++time_hash >= BW_METER_BUCKETS)
2853 x->bm_time_next = bw_meter_timers[time_hash];
2854 bw_meter_timers[time_hash] = x;
2855 x->bm_time_hash = time_hash;
2861 * Test if we should deliver an upcall
2863 if (((x->bm_flags & BW_METER_UNIT_PACKETS) &&
2864 (x->bm_measured.b_packets <= x->bm_threshold.b_packets)) ||
2865 ((x->bm_flags & BW_METER_UNIT_BYTES) &&
2866 (x->bm_measured.b_bytes <= x->bm_threshold.b_bytes))) {
2867 /* Prepare an upcall for delivery */
2868 bw_meter_prepare_upcall(x, &now);
2872 * Reschedule for next processing
2874 schedule_bw_meter(x, &now);
2878 /* Send all upcalls that are pending delivery */
2885 * A periodic function for sending all upcalls that are pending delivery
2888 expire_bw_upcalls_send(void *unused)
2894 callout_reset(&bw_upcalls_ch, BW_UPCALLS_PERIOD,
2895 expire_bw_upcalls_send, NULL);
2899 * A periodic function for periodic scanning of the multicast forwarding
2900 * table for processing all "<=" bw_meter entries.
2903 expire_bw_meter_process(void *unused)
2905 if (mrt_api_config & MRT_MFC_BW_UPCALL)
2908 callout_reset(&bw_meter_ch, BW_METER_PERIOD, expire_bw_meter_process, NULL);
2912 * End of bandwidth monitoring code
2917 * Send the packet up to the user daemon, or eventually do kernel encapsulation
2921 pim_register_send(struct ip *ip, struct vif *vifp,
2922 struct mbuf *m, struct mfc *rt)
2924 struct mbuf *mb_copy, *mm;
2926 if (mrtdebug & DEBUG_PIM)
2927 log(LOG_DEBUG, "pim_register_send: ");
2929 mb_copy = pim_register_prepare(ip, m);
2930 if (mb_copy == NULL)
2934 * Send all the fragments. Note that the mbuf for each fragment
2935 * is freed by the sending machinery.
2937 for (mm = mb_copy; mm; mm = mb_copy) {
2938 mb_copy = mm->m_nextpkt;
2940 mm = m_pullup(mm, sizeof(struct ip));
2942 ip = mtod(mm, struct ip *);
2943 if ((mrt_api_config & MRT_MFC_RP) &&
2944 (rt->mfc_rp.s_addr != INADDR_ANY)) {
2945 pim_register_send_rp(ip, vifp, mm, rt);
2947 pim_register_send_upcall(ip, vifp, mm, rt);
2956 * Return a copy of the data packet that is ready for PIM Register
2958 * XXX: Note that in the returned copy the IP header is a valid one.
2960 static struct mbuf *
2961 pim_register_prepare(struct ip *ip, struct mbuf *m)
2963 struct mbuf *mb_copy = NULL;
2966 /* Take care of delayed checksums */
2967 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
2968 in_delayed_cksum(m);
2969 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
2973 * Copy the old packet & pullup its IP header into the
2974 * new mbuf so we can modify it.
2976 mb_copy = m_copypacket(m, M_DONTWAIT);
2977 if (mb_copy == NULL)
2979 mb_copy = m_pullup(mb_copy, ip->ip_hl << 2);
2980 if (mb_copy == NULL)
2983 /* take care of the TTL */
2984 ip = mtod(mb_copy, struct ip *);
2987 /* Compute the MTU after the PIM Register encapsulation */
2988 mtu = 0xffff - sizeof(pim_encap_iphdr) - sizeof(pim_encap_pimhdr);
2990 if (ip->ip_len <= mtu) {
2991 /* Turn the IP header into a valid one */
2992 ip->ip_len = htons(ip->ip_len);
2993 ip->ip_off = htons(ip->ip_off);
2995 ip->ip_sum = in_cksum(mb_copy, ip->ip_hl << 2);
2997 /* Fragment the packet */
2998 if (ip_fragment(ip, &mb_copy, mtu, 0, CSUM_DELAY_IP) != 0) {
3007 * Send an upcall with the data packet to the user-level process.
3010 pim_register_send_upcall(struct ip *ip, struct vif *vifp,
3011 struct mbuf *mb_copy, struct mfc *rt)
3013 struct mbuf *mb_first;
3014 int len = ntohs(ip->ip_len);
3016 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
3021 * Add a new mbuf with an upcall header
3023 MGETHDR(mb_first, M_DONTWAIT, MT_HEADER);
3024 if (mb_first == NULL) {
3028 mb_first->m_data += max_linkhdr;
3029 mb_first->m_pkthdr.len = len + sizeof(struct igmpmsg);
3030 mb_first->m_len = sizeof(struct igmpmsg);
3031 mb_first->m_next = mb_copy;
3033 /* Send message to routing daemon */
3034 im = mtod(mb_first, struct igmpmsg *);
3035 im->im_msgtype = IGMPMSG_WHOLEPKT;
3037 im->im_vif = vifp - viftable;
3038 im->im_src = ip->ip_src;
3039 im->im_dst = ip->ip_dst;
3041 k_igmpsrc.sin_addr = ip->ip_src;
3043 mrtstat.mrts_upcalls++;
3045 if (socket_send(ip_mrouter, mb_first, &k_igmpsrc) < 0) {
3046 if (mrtdebug & DEBUG_PIM)
3048 "mcast: pim_register_send_upcall: ip_mrouter socket queue full");
3049 ++mrtstat.mrts_upq_sockfull;
3053 /* Keep statistics */
3054 pimstat.pims_snd_registers_msgs++;
3055 pimstat.pims_snd_registers_bytes += len;
3061 * Encapsulate the data packet in PIM Register message and send it to the RP.
3064 pim_register_send_rp(struct ip *ip, struct vif *vifp,
3065 struct mbuf *mb_copy, struct mfc *rt)
3067 struct mbuf *mb_first;
3068 struct ip *ip_outer;
3069 struct pim_encap_pimhdr *pimhdr;
3070 int len = ntohs(ip->ip_len);
3071 vifi_t vifi = rt->mfc_parent;
3075 if ((vifi >= numvifs) || (viftable[vifi].v_lcl_addr.s_addr == 0)) {
3077 return EADDRNOTAVAIL; /* The iif vif is invalid */
3081 * Add a new mbuf with the encapsulating header
3083 MGETHDR(mb_first, M_DONTWAIT, MT_HEADER);
3084 if (mb_first == NULL) {
3088 mb_first->m_data += max_linkhdr;
3089 mb_first->m_len = sizeof(pim_encap_iphdr) + sizeof(pim_encap_pimhdr);
3090 mb_first->m_next = mb_copy;
3092 mb_first->m_pkthdr.len = len + mb_first->m_len;
3095 * Fill in the encapsulating IP and PIM header
3097 ip_outer = mtod(mb_first, struct ip *);
3098 *ip_outer = pim_encap_iphdr;
3099 ip_outer->ip_id = ip_newid();
3100 ip_outer->ip_len = len + sizeof(pim_encap_iphdr) + sizeof(pim_encap_pimhdr);
3101 ip_outer->ip_src = viftable[vifi].v_lcl_addr;
3102 ip_outer->ip_dst = rt->mfc_rp;
3104 * Copy the inner header TOS to the outer header, and take care of the
3107 ip_outer->ip_tos = ip->ip_tos;
3108 if (ntohs(ip->ip_off) & IP_DF)
3109 ip_outer->ip_off |= IP_DF;
3110 pimhdr = (struct pim_encap_pimhdr *)((caddr_t)ip_outer
3111 + sizeof(pim_encap_iphdr));
3112 *pimhdr = pim_encap_pimhdr;
3113 /* If the iif crosses a border, set the Border-bit */
3114 if (rt->mfc_flags[vifi] & MRT_MFC_FLAGS_BORDER_VIF & mrt_api_config)
3115 pimhdr->flags |= htonl(PIM_BORDER_REGISTER);
3117 mb_first->m_data += sizeof(pim_encap_iphdr);
3118 pimhdr->pim.pim_cksum = in_cksum(mb_first, sizeof(pim_encap_pimhdr));
3119 mb_first->m_data -= sizeof(pim_encap_iphdr);
3121 if (vifp->v_rate_limit == 0)
3122 tbf_send_packet(vifp, mb_first);
3124 tbf_control(vifp, mb_first, ip, ip_outer->ip_len);
3126 /* Keep statistics */
3127 pimstat.pims_snd_registers_msgs++;
3128 pimstat.pims_snd_registers_bytes += len;
3134 * PIM-SMv2 and PIM-DM messages processing.
3135 * Receives and verifies the PIM control messages, and passes them
3136 * up to the listening socket, using rip_input().
3137 * The only message with special processing is the PIM_REGISTER message
3138 * (used by PIM-SM): the PIM header is stripped off, and the inner packet
3139 * is passed to if_simloop().
3142 pim_input(struct mbuf *m, int off)
3144 struct ip *ip = mtod(m, struct ip *);
3147 int datalen = ip->ip_len;
3151 /* Keep statistics */
3152 pimstat.pims_rcv_total_msgs++;
3153 pimstat.pims_rcv_total_bytes += datalen;
3158 if (datalen < PIM_MINLEN) {
3159 pimstat.pims_rcv_tooshort++;
3160 log(LOG_ERR, "pim_input: packet size too small %d from %lx\n",
3161 datalen, (u_long)ip->ip_src.s_addr);
3167 * If the packet is at least as big as a REGISTER, go agead
3168 * and grab the PIM REGISTER header size, to avoid another
3169 * possible m_pullup() later.
3171 * PIM_MINLEN == pimhdr + u_int32_t == 4 + 4 = 8
3172 * PIM_REG_MINLEN == pimhdr + reghdr + encap_iphdr == 4 + 4 + 20 = 28
3174 minlen = iphlen + (datalen >= PIM_REG_MINLEN ? PIM_REG_MINLEN : PIM_MINLEN);
3176 * Get the IP and PIM headers in contiguous memory, and
3177 * possibly the PIM REGISTER header.
3179 if ((m->m_flags & M_EXT || m->m_len < minlen) &&
3180 (m = m_pullup(m, minlen)) == 0) {
3181 log(LOG_ERR, "pim_input: m_pullup failure\n");
3184 /* m_pullup() may have given us a new mbuf so reset ip. */
3185 ip = mtod(m, struct ip *);
3186 ip_tos = ip->ip_tos;
3188 /* adjust mbuf to point to the PIM header */
3189 m->m_data += iphlen;
3191 pim = mtod(m, struct pim *);
3194 * Validate checksum. If PIM REGISTER, exclude the data packet.
3196 * XXX: some older PIMv2 implementations don't make this distinction,
3197 * so for compatibility reason perform the checksum over part of the
3198 * message, and if error, then over the whole message.
3200 if (PIM_VT_T(pim->pim_vt) == PIM_REGISTER && in_cksum(m, PIM_MINLEN) == 0) {
3201 /* do nothing, checksum okay */
3202 } else if (in_cksum(m, datalen)) {
3203 pimstat.pims_rcv_badsum++;
3204 if (mrtdebug & DEBUG_PIM)
3205 log(LOG_DEBUG, "pim_input: invalid checksum");
3210 /* PIM version check */
3211 if (PIM_VT_V(pim->pim_vt) < PIM_VERSION) {
3212 pimstat.pims_rcv_badversion++;
3213 log(LOG_ERR, "pim_input: incorrect version %d, expecting %d\n",
3214 PIM_VT_V(pim->pim_vt), PIM_VERSION);
3219 /* restore mbuf back to the outer IP */
3220 m->m_data -= iphlen;
3223 if (PIM_VT_T(pim->pim_vt) == PIM_REGISTER) {
3225 * Since this is a REGISTER, we'll make a copy of the register
3226 * headers ip + pim + u_int32 + encap_ip, to be passed up to the
3229 struct sockaddr_in dst = { sizeof(dst), AF_INET };
3231 struct ip *encap_ip;
3236 if ((reg_vif_num >= numvifs) || (reg_vif_num == VIFI_INVALID)) {
3238 if (mrtdebug & DEBUG_PIM)
3240 "pim_input: register vif not set: %d\n", reg_vif_num);
3244 /* XXX need refcnt? */
3245 vifp = viftable[reg_vif_num].v_ifp;
3251 if (datalen < PIM_REG_MINLEN) {
3252 pimstat.pims_rcv_tooshort++;
3253 pimstat.pims_rcv_badregisters++;
3255 "pim_input: register packet size too small %d from %lx\n",
3256 datalen, (u_long)ip->ip_src.s_addr);
3261 reghdr = (u_int32_t *)(pim + 1);
3262 encap_ip = (struct ip *)(reghdr + 1);
3264 if (mrtdebug & DEBUG_PIM) {
3266 "pim_input[register], encap_ip: %lx -> %lx, encap_ip len %d\n",
3267 (u_long)ntohl(encap_ip->ip_src.s_addr),
3268 (u_long)ntohl(encap_ip->ip_dst.s_addr),
3269 ntohs(encap_ip->ip_len));
3272 /* verify the version number of the inner packet */
3273 if (encap_ip->ip_v != IPVERSION) {
3274 pimstat.pims_rcv_badregisters++;
3275 if (mrtdebug & DEBUG_PIM) {
3276 log(LOG_DEBUG, "pim_input: invalid IP version (%d) "
3277 "of the inner packet\n", encap_ip->ip_v);
3283 /* verify the inner packet is destined to a mcast group */
3284 if (!IN_MULTICAST(ntohl(encap_ip->ip_dst.s_addr))) {
3285 pimstat.pims_rcv_badregisters++;
3286 if (mrtdebug & DEBUG_PIM)
3288 "pim_input: inner packet of register is not "
3290 (u_long)ntohl(encap_ip->ip_dst.s_addr));
3295 /* If a NULL_REGISTER, pass it to the daemon */
3296 if ((ntohl(*reghdr) & PIM_NULL_REGISTER))
3297 goto pim_input_to_daemon;
3300 * Copy the TOS from the outer IP header to the inner IP header.
3302 if (encap_ip->ip_tos != ip_tos) {
3303 /* Outer TOS -> inner TOS */
3304 encap_ip->ip_tos = ip_tos;
3305 /* Recompute the inner header checksum. Sigh... */
3307 /* adjust mbuf to point to the inner IP header */
3308 m->m_data += (iphlen + PIM_MINLEN);
3309 m->m_len -= (iphlen + PIM_MINLEN);
3311 encap_ip->ip_sum = 0;
3312 encap_ip->ip_sum = in_cksum(m, encap_ip->ip_hl << 2);
3314 /* restore mbuf to point back to the outer IP header */
3315 m->m_data -= (iphlen + PIM_MINLEN);
3316 m->m_len += (iphlen + PIM_MINLEN);
3320 * Decapsulate the inner IP packet and loopback to forward it
3321 * as a normal multicast packet. Also, make a copy of the
3322 * outer_iphdr + pimhdr + reghdr + encap_iphdr
3323 * to pass to the daemon later, so it can take the appropriate
3324 * actions (e.g., send back PIM_REGISTER_STOP).
3325 * XXX: here m->m_data points to the outer IP header.
3327 mcp = m_copy(m, 0, iphlen + PIM_REG_MINLEN);
3330 "pim_input: pim register: could not copy register head\n");
3335 /* Keep statistics */
3336 /* XXX: registers_bytes include only the encap. mcast pkt */
3337 pimstat.pims_rcv_registers_msgs++;
3338 pimstat.pims_rcv_registers_bytes += ntohs(encap_ip->ip_len);
3341 * forward the inner ip packet; point m_data at the inner ip.
3343 m_adj(m, iphlen + PIM_MINLEN);
3345 if (mrtdebug & DEBUG_PIM) {
3347 "pim_input: forwarding decapsulated register: "
3348 "src %lx, dst %lx, vif %d\n",
3349 (u_long)ntohl(encap_ip->ip_src.s_addr),
3350 (u_long)ntohl(encap_ip->ip_dst.s_addr),
3353 /* NB: vifp was collected above; can it change on us? */
3354 if_simloop(vifp, m, dst.sin_family, 0);
3356 /* prepare the register head to send to the mrouting daemon */
3360 pim_input_to_daemon:
3362 * Pass the PIM message up to the daemon; if it is a Register message,
3363 * pass the 'head' only up to the daemon. This includes the
3364 * outer IP header, PIM header, PIM-Register header and the
3366 * XXX: the outer IP header pkt size of a Register is not adjust to
3367 * reflect the fact that the inner multicast data is truncated.
3369 rip_input(m, iphlen);
3376 ip_mroute_modevent(module_t mod, int type, void *unused)
3380 mtx_init(&mrouter_mtx, "mrouter initialization", NULL, MTX_DEF);
3384 ip_mcast_src = X_ip_mcast_src;
3385 ip_mforward = X_ip_mforward;
3386 ip_mrouter_done = X_ip_mrouter_done;
3387 ip_mrouter_get = X_ip_mrouter_get;
3388 ip_mrouter_set = X_ip_mrouter_set;
3389 ip_rsvp_force_done = X_ip_rsvp_force_done;
3390 ip_rsvp_vif = X_ip_rsvp_vif;
3391 legal_vif_num = X_legal_vif_num;
3392 mrt_ioctl = X_mrt_ioctl;
3393 rsvp_input_p = X_rsvp_input;
3398 * Typically module unload happens after the user-level
3399 * process has shutdown the kernel services (the check
3400 * below insures someone can't just yank the module out
3401 * from under a running process). But if the module is
3402 * just loaded and then unloaded w/o starting up a user
3403 * process we still need to cleanup.
3408 X_ip_mrouter_done();
3409 ip_mcast_src = NULL;
3411 ip_mrouter_done = NULL;
3412 ip_mrouter_get = NULL;
3413 ip_mrouter_set = NULL;
3414 ip_rsvp_force_done = NULL;
3416 legal_vif_num = NULL;
3418 rsvp_input_p = NULL;
3421 mtx_destroy(&mrouter_mtx);
3429 static moduledata_t ip_mroutemod = {
3434 DECLARE_MODULE(ip_mroute, ip_mroutemod, SI_SUB_PSEUDO, SI_ORDER_ANY);