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
20 * $FreeBSD: src/sys/netinet/ip_mroute.c,v 1.56.2.10 2003/08/24 21:37:34 hsu Exp $
23 #include "opt_mrouting.h"
29 #include <sys/param.h>
30 #include <sys/kernel.h>
31 #include <sys/malloc.h>
33 #include <sys/protosw.h>
34 #include <sys/socket.h>
35 #include <sys/socketvar.h>
36 #include <sys/sockio.h>
37 #include <sys/sysctl.h>
38 #include <sys/syslog.h>
39 #include <sys/systm.h>
40 #include <sys/thread2.h>
42 #include <sys/in_cksum.h>
44 #include <machine/stdarg.h>
47 #include <net/netisr.h>
48 #include <net/route.h>
49 #include <netinet/in.h>
50 #include <netinet/igmp.h>
51 #include <netinet/in_systm.h>
52 #include <netinet/in_var.h>
53 #include <netinet/ip.h>
54 #include "ip_mroute.h"
55 #include <netinet/ip_var.h>
57 #include <netinet/pim.h>
58 #include <netinet/pim_var.h>
61 #include <netinet/in_pcb.h>
63 #include <netinet/udp.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 */
73 #define DEBUG_MFC 0x02
74 #define DEBUG_FORWARD 0x04
75 #define DEBUG_EXPIRE 0x08
76 #define DEBUG_XMIT 0x10
77 #define DEBUG_PIM 0x20
79 #define VIFI_INVALID ((vifi_t) -1)
81 #define M_HASCL(m) ((m)->m_flags & M_EXT)
83 static MALLOC_DEFINE(M_MRTABLE, "mroutetbl", "multicast routing tables");
85 static struct mrtstat mrtstat;
86 SYSCTL_STRUCT(_net_inet_ip, OID_AUTO, mrtstat, CTLFLAG_RW,
88 "Multicast Routing Statistics (struct mrtstat, netinet/ip_mroute.h)");
90 static struct mfc *mfctable[MFCTBLSIZ];
91 SYSCTL_OPAQUE(_net_inet_ip, OID_AUTO, mfctable, CTLFLAG_RD,
92 &mfctable, sizeof(mfctable), "S,*mfc[MFCTBLSIZ]",
93 "Multicast Forwarding Table (struct *mfc[MFCTBLSIZ], netinet/ip_mroute.h)");
95 static struct vif viftable[MAXVIFS];
96 SYSCTL_OPAQUE(_net_inet_ip, OID_AUTO, viftable, CTLFLAG_RD,
97 &viftable, sizeof(viftable), "S,vif[MAXVIFS]",
98 "Multicast Virtual Interfaces (struct vif[MAXVIFS], netinet/ip_mroute.h)");
100 static u_char nexpire[MFCTBLSIZ];
102 struct lwkt_token mroute_token = LWKT_TOKEN_INITIALIZER(mroute_token);
105 static struct callout expire_upcalls_ch;
106 static struct callout tbf_reprocess_q_ch;
107 #define EXPIRE_TIMEOUT (hz / 4) /* 4x / second */
108 #define UPCALL_EXPIRE 6 /* number of timeouts */
111 * Define the token bucket filter structures
112 * tbftable -> each vif has one of these for storing info
115 static struct tbf tbftable[MAXVIFS];
116 #define TBF_REPROCESS (hz / 100) /* 100x / second */
119 * 'Interfaces' associated with decapsulator (so we can tell
120 * packets that went through it from ones that get reflected
121 * by a broken gateway). These interfaces are never linked into
122 * the system ifnet list & no routes point to them. I.e., packets
123 * can't be sent this way. They only exist as a placeholder for
124 * multicast source verification.
126 static struct ifnet multicast_decap_if[MAXVIFS];
129 #define ENCAP_PROTO IPPROTO_IPIP /* 4 */
131 /* prototype IP hdr for encapsulated packets */
132 static struct ip multicast_encap_iphdr = {
133 #if BYTE_ORDER == LITTLE_ENDIAN
134 sizeof(struct ip) >> 2, IPVERSION,
136 IPVERSION, sizeof(struct ip) >> 2,
139 sizeof(struct ip), /* total length */
142 ENCAP_TTL, ENCAP_PROTO,
147 * Bandwidth meter variables and constants
149 static MALLOC_DEFINE(M_BWMETER, "bwmeter", "multicast upcall bw meters");
151 * Pending timeouts are stored in a hash table, the key being the
152 * expiration time. Periodically, the entries are analysed and processed.
154 #define BW_METER_BUCKETS 1024
155 static struct bw_meter *bw_meter_timers[BW_METER_BUCKETS];
156 static struct callout bw_meter_ch;
157 #define BW_METER_PERIOD (hz) /* periodical handling of bw meters */
160 * Pending upcalls are stored in a vector which is flushed when
161 * full, or periodically
163 static struct bw_upcall bw_upcalls[BW_UPCALLS_MAX];
164 static u_int bw_upcalls_n; /* # of pending upcalls */
165 static struct callout bw_upcalls_ch;
166 #define BW_UPCALLS_PERIOD (hz) /* periodical flush of bw upcalls */
169 static struct pimstat pimstat;
170 SYSCTL_STRUCT(_net_inet_pim, PIMCTL_STATS, stats, CTLFLAG_RD,
172 "PIM Statistics (struct pimstat, netinet/pim_var.h)");
175 * Note: the PIM Register encapsulation adds the following in front of a
178 * struct pim_encap_hdr {
180 * struct pim_encap_pimhdr pim;
185 struct pim_encap_pimhdr {
190 static struct ip pim_encap_iphdr = {
191 #if BYTE_ORDER == LITTLE_ENDIAN
192 sizeof(struct ip) >> 2,
196 sizeof(struct ip) >> 2,
199 sizeof(struct ip), /* total length */
207 static struct pim_encap_pimhdr pim_encap_pimhdr = {
209 PIM_MAKE_VT(PIM_VERSION, PIM_REGISTER), /* PIM vers and message type */
216 static struct ifnet multicast_register_if;
217 static vifi_t reg_vif_num = VIFI_INVALID;
223 static vifi_t numvifs;
224 static int have_encap_tunnel;
227 * one-back cache used by ipip_input to locate a tunnel's vif
228 * given a datagram's src ip address.
230 static u_long last_encap_src;
231 static struct vif *last_encap_vif;
233 static u_long X_ip_mcast_src(int vifi);
234 static int X_ip_mforward(struct ip *ip, struct ifnet *ifp,
235 struct mbuf *m, struct ip_moptions *imo);
236 static int X_ip_mrouter_done(void);
237 static int X_ip_mrouter_get(struct socket *so, struct sockopt *m);
238 static int X_ip_mrouter_set(struct socket *so, struct sockopt *m);
239 static int X_legal_vif_num(int vif);
240 static int X_mrt_ioctl(int cmd, caddr_t data);
242 static int get_sg_cnt(struct sioc_sg_req *);
243 static int get_vif_cnt(struct sioc_vif_req *);
244 static int ip_mrouter_init(struct socket *, int);
245 static int add_vif(struct vifctl *);
246 static int del_vif(vifi_t);
247 static int add_mfc(struct mfcctl2 *);
248 static int del_mfc(struct mfcctl2 *);
249 static int set_api_config(uint32_t *); /* chose API capabilities */
250 static int socket_send(struct socket *, struct mbuf *, struct sockaddr_in *);
251 static int set_assert(int);
252 static void expire_upcalls(void *);
253 static int ip_mdq(struct mbuf *, struct ifnet *, struct mfc *, vifi_t);
254 static void phyint_send(struct ip *, struct vif *, struct mbuf *);
255 static void encap_send(struct ip *, struct vif *, struct mbuf *);
256 static void tbf_control(struct vif *, struct mbuf *, struct ip *, u_long);
257 static void tbf_queue(struct vif *, struct mbuf *);
258 static void tbf_process_q(struct vif *);
259 static void tbf_reprocess_q(void *);
260 static int tbf_dq_sel(struct vif *, struct ip *);
261 static void tbf_send_packet(struct vif *, struct mbuf *);
262 static void tbf_update_tokens(struct vif *);
263 static int priority(struct vif *, struct ip *);
266 * Bandwidth monitoring
268 static void free_bw_list(struct bw_meter *list);
269 static int add_bw_upcall(struct bw_upcall *);
270 static int del_bw_upcall(struct bw_upcall *);
271 static void bw_meter_receive_packet(struct bw_meter *x, int plen,
272 struct timeval *nowp);
273 static void bw_meter_prepare_upcall(struct bw_meter *x, struct timeval *nowp);
274 static void bw_upcalls_send(void);
275 static void schedule_bw_meter(struct bw_meter *x, struct timeval *nowp);
276 static void unschedule_bw_meter(struct bw_meter *x);
277 static void bw_meter_process(void);
278 static void expire_bw_upcalls_send(void *);
279 static void expire_bw_meter_process(void *);
282 static int pim_register_send(struct ip *, struct vif *,
283 struct mbuf *, struct mfc *);
284 static int pim_register_send_rp(struct ip *, struct vif *,
285 struct mbuf *, struct mfc *);
286 static int pim_register_send_upcall(struct ip *, struct vif *,
287 struct mbuf *, struct mfc *);
288 static struct mbuf *pim_register_prepare(struct ip *, struct mbuf *);
292 * whether or not special PIM assert processing is enabled.
294 static int pim_assert;
296 * Rate limit for assert notification messages, in usec
298 #define ASSERT_MSG_TIME 3000000
301 * Kernel multicast routing API capabilities and setup.
302 * If more API capabilities are added to the kernel, they should be
303 * recorded in `mrt_api_support'.
305 static const uint32_t mrt_api_support = (MRT_MFC_FLAGS_DISABLE_WRONGVIF |
306 MRT_MFC_FLAGS_BORDER_VIF |
309 static uint32_t mrt_api_config = 0;
312 * Hash function for a source, group entry
314 #define MFCHASH(a, g) MFCHASHMOD(((a) >> 20) ^ ((a) >> 10) ^ (a) ^ \
315 ((g) >> 20) ^ ((g) >> 10) ^ (g))
318 * Find a route for a given origin IP address and Multicast group address
319 * Type of service parameter to be added in the future!!!
320 * Statistics are updated by the caller if needed
321 * (mrtstat.mrts_mfc_lookups and mrtstat.mrts_mfc_misses)
324 mfc_find(in_addr_t o, in_addr_t g)
328 for (rt = mfctable[MFCHASH(o,g)]; rt; rt = rt->mfc_next)
329 if ((rt->mfc_origin.s_addr == o) &&
330 (rt->mfc_mcastgrp.s_addr == g) && (rt->mfc_stall == NULL))
336 * Macros to compute elapsed time efficiently
337 * Borrowed from Van Jacobson's scheduling code
339 #define TV_DELTA(a, b, delta) { \
341 delta = (a).tv_usec - (b).tv_usec; \
342 if ((xxs = (a).tv_sec - (b).tv_sec)) { \
351 delta += (1000000 * xxs); \
356 #define TV_LT(a, b) (((a).tv_usec < (b).tv_usec && \
357 (a).tv_sec <= (b).tv_sec) || (a).tv_sec < (b).tv_sec)
360 * Handle MRT setsockopt commands to modify the multicast routing tables.
363 X_ip_mrouter_set(struct socket *so, struct sockopt *sopt)
369 struct bw_upcall bw_upcall;
372 if (so != ip_mrouter && sopt->sopt_name != MRT_INIT)
376 switch (sopt->sopt_name) {
378 error = soopt_to_kbuf(sopt, &optval, sizeof optval, sizeof optval);
381 error = ip_mrouter_init(so, optval);
385 error = ip_mrouter_done();
389 error = soopt_to_kbuf(sopt, &vifc, sizeof vifc, sizeof vifc);
392 error = add_vif(&vifc);
396 error = soopt_to_kbuf(sopt, &vifi, sizeof vifi, sizeof vifi);
399 error = del_vif(vifi);
405 * select data size depending on API version.
407 if (sopt->sopt_name == MRT_ADD_MFC &&
408 mrt_api_config & MRT_API_FLAGS_ALL) {
409 error = soopt_to_kbuf(sopt, &mfc, sizeof(struct mfcctl2),
410 sizeof(struct mfcctl2));
412 error = soopt_to_kbuf(sopt, &mfc, sizeof(struct mfcctl),
413 sizeof(struct mfcctl));
414 bzero((caddr_t)&mfc + sizeof(struct mfcctl),
415 sizeof(mfc) - sizeof(struct mfcctl));
419 if (sopt->sopt_name == MRT_ADD_MFC)
420 error = add_mfc(&mfc);
422 error = del_mfc(&mfc);
426 error = soopt_to_kbuf(sopt, &optval, sizeof optval, sizeof optval);
433 error = soopt_to_kbuf(sopt, &i, sizeof i, sizeof i);
435 error = set_api_config(&i);
437 soopt_from_kbuf(sopt, &i, sizeof i);
440 case MRT_ADD_BW_UPCALL:
441 case MRT_DEL_BW_UPCALL:
442 error = soopt_to_kbuf(sopt, &bw_upcall, sizeof bw_upcall, sizeof bw_upcall);
445 if (sopt->sopt_name == MRT_ADD_BW_UPCALL)
446 error = add_bw_upcall(&bw_upcall);
448 error = del_bw_upcall(&bw_upcall);
459 * Handle MRT getsockopt commands
462 X_ip_mrouter_get(struct socket *so, struct sockopt *sopt)
465 static int version = 0x0305; /* !!! why is this here? XXX */
468 switch (sopt->sopt_name) {
470 soopt_from_kbuf(sopt, &version, sizeof version);
474 soopt_from_kbuf(sopt, &pim_assert, sizeof pim_assert);
477 case MRT_API_SUPPORT:
478 soopt_from_kbuf(sopt, &mrt_api_support, sizeof mrt_api_support);
482 soopt_from_kbuf(sopt, &mrt_api_config, sizeof mrt_api_config);
493 * Handle ioctl commands to obtain information from the cache
496 X_mrt_ioctl(int cmd, caddr_t data)
502 error = get_vif_cnt((struct sioc_vif_req *)data);
506 error = get_sg_cnt((struct sioc_sg_req *)data);
517 * returns the packet, byte, rpf-failure count for the source group provided
520 get_sg_cnt(struct sioc_sg_req *req)
524 lwkt_gettoken(&mroute_token);
525 rt = mfc_find(req->src.s_addr, req->grp.s_addr);
527 req->pktcnt = req->bytecnt = req->wrong_if = 0xffffffff;
528 lwkt_reltoken(&mroute_token);
529 return EADDRNOTAVAIL;
531 req->pktcnt = rt->mfc_pkt_cnt;
532 req->bytecnt = rt->mfc_byte_cnt;
533 req->wrong_if = rt->mfc_wrong_if;
534 lwkt_reltoken(&mroute_token);
539 * returns the input and output packet and byte counts on the vif provided
542 get_vif_cnt(struct sioc_vif_req *req)
544 vifi_t vifi = req->vifi;
549 req->icount = viftable[vifi].v_pkt_in;
550 req->ocount = viftable[vifi].v_pkt_out;
551 req->ibytes = viftable[vifi].v_bytes_in;
552 req->obytes = viftable[vifi].v_bytes_out;
558 * Enable multicast routing
561 ip_mrouter_init(struct socket *so, int version)
564 log(LOG_DEBUG, "ip_mrouter_init: so_type = %d, pr_protocol = %d\n",
565 so->so_type, so->so_proto->pr_protocol);
567 if (so->so_type != SOCK_RAW || so->so_proto->pr_protocol != IPPROTO_IGMP)
573 if (ip_mrouter != NULL)
578 bzero((caddr_t)mfctable, sizeof(mfctable));
579 bzero((caddr_t)nexpire, sizeof(nexpire));
583 bzero((caddr_t)bw_meter_timers, sizeof(bw_meter_timers));
585 callout_init(&expire_upcalls_ch);
586 callout_init(&bw_upcalls_ch);
587 callout_init(&bw_meter_ch);
588 callout_init(&tbf_reprocess_q_ch);
590 callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT, expire_upcalls, NULL);
591 callout_reset(&bw_upcalls_ch, BW_UPCALLS_PERIOD,
592 expire_bw_upcalls_send, NULL);
593 callout_reset(&bw_meter_ch, BW_METER_PERIOD, expire_bw_meter_process, NULL);
598 log(LOG_DEBUG, "ip_mrouter_init\n");
604 * Disable multicast routing
607 X_ip_mrouter_done(void)
616 lwkt_gettoken(&mroute_token);
619 * For each phyint in use, disable promiscuous reception of all IP
622 for (vifi = 0; vifi < numvifs; vifi++) {
623 if (viftable[vifi].v_lcl_addr.s_addr != 0 &&
624 !(viftable[vifi].v_flags & (VIFF_TUNNEL | VIFF_REGISTER))) {
625 struct sockaddr_in *so = (struct sockaddr_in *)&(ifr.ifr_addr);
627 so->sin_len = sizeof(struct sockaddr_in);
628 so->sin_family = AF_INET;
629 so->sin_addr.s_addr = INADDR_ANY;
630 ifp = viftable[vifi].v_ifp;
634 bzero((caddr_t)tbftable, sizeof(tbftable));
635 bzero((caddr_t)viftable, sizeof(viftable));
639 callout_stop(&expire_upcalls_ch);
643 callout_stop(&bw_upcalls_ch);
644 callout_stop(&bw_meter_ch);
645 callout_stop(&tbf_reprocess_q_ch);
648 * Free all multicast forwarding cache entries.
650 for (i = 0; i < MFCTBLSIZ; i++) {
651 for (rt = mfctable[i]; rt != NULL; ) {
652 struct mfc *nr = rt->mfc_next;
654 for (rte = rt->mfc_stall; rte != NULL; ) {
655 struct rtdetq *n = rte->next;
658 kfree(rte, M_MRTABLE);
661 free_bw_list(rt->mfc_bw_meter);
662 kfree(rt, M_MRTABLE);
667 bzero((caddr_t)mfctable, sizeof(mfctable));
669 bzero(bw_meter_timers, sizeof(bw_meter_timers));
672 * Reset de-encapsulation cache
674 last_encap_src = INADDR_ANY;
675 last_encap_vif = NULL;
677 reg_vif_num = VIFI_INVALID;
679 have_encap_tunnel = 0;
683 lwkt_reltoken(&mroute_token);
686 log(LOG_DEBUG, "ip_mrouter_done\n");
692 * Set PIM assert processing global
697 if ((i != 1) && (i != 0))
706 * Configure API capabilities
709 set_api_config(uint32_t *apival)
714 * We can set the API capabilities only if it is the first operation
715 * after MRT_INIT. I.e.:
716 * - there are no vifs installed
717 * - pim_assert is not enabled
718 * - the MFC table is empty
728 for (i = 0; i < MFCTBLSIZ; i++) {
729 if (mfctable[i] != NULL) {
735 mrt_api_config = *apival & mrt_api_support;
736 *apival = mrt_api_config;
742 * Add a vif to the vif table
745 add_vif(struct vifctl *vifcp)
747 struct vif *vifp = viftable + vifcp->vifc_vifi;
748 struct sockaddr_in sin = {sizeof sin, AF_INET};
752 struct tbf *v_tbf = tbftable + vifcp->vifc_vifi;
754 if (vifcp->vifc_vifi >= MAXVIFS)
756 if (vifp->v_lcl_addr.s_addr != INADDR_ANY)
758 if (vifcp->vifc_lcl_addr.s_addr == INADDR_ANY)
759 return EADDRNOTAVAIL;
761 /* Find the interface with an address in AF_INET family */
763 if (vifcp->vifc_flags & VIFF_REGISTER) {
765 * XXX: Because VIFF_REGISTER does not really need a valid
766 * local interface (e.g. it could be 127.0.0.2), we don't
773 sin.sin_addr = vifcp->vifc_lcl_addr;
774 ifa = ifa_ifwithaddr((struct sockaddr *)&sin);
776 return EADDRNOTAVAIL;
780 if (vifcp->vifc_flags & VIFF_TUNNEL) {
781 if ((vifcp->vifc_flags & VIFF_SRCRT) == 0) {
783 * An encapsulating tunnel is wanted. Tell ipip_input() to
784 * start paying attention to encapsulated packets.
786 if (have_encap_tunnel == 0) {
787 have_encap_tunnel = 1;
788 for (i = 0; i < MAXVIFS; i++) {
789 if_initname(&multicast_decap_if[i], "mdecap", i);
793 * Set interface to fake encapsulator interface
795 ifp = &multicast_decap_if[vifcp->vifc_vifi];
797 * Prepare cached route entry
799 bzero(&vifp->v_route, sizeof(vifp->v_route));
801 log(LOG_ERR, "source routed tunnels not supported\n");
805 } else if (vifcp->vifc_flags & VIFF_REGISTER) {
806 ifp = &multicast_register_if;
808 log(LOG_DEBUG, "Adding a register vif, ifp: %p\n",
809 (void *)&multicast_register_if);
810 if (reg_vif_num == VIFI_INVALID) {
811 if_initname(&multicast_register_if, "register_vif", 0);
812 multicast_register_if.if_flags = IFF_LOOPBACK;
813 bzero(&vifp->v_route, sizeof(vifp->v_route));
814 reg_vif_num = vifcp->vifc_vifi;
817 } else { /* Make sure the interface supports multicast */
818 if ((ifp->if_flags & IFF_MULTICAST) == 0)
821 /* Enable promiscuous reception of all IP multicasts from the if */
822 lwkt_gettoken(&mroute_token);
823 error = if_allmulti(ifp, 1);
824 lwkt_reltoken(&mroute_token);
829 lwkt_gettoken(&mroute_token);
830 /* define parameters for the tbf structure */
832 GET_TIME(vifp->v_tbf->tbf_last_pkt_t);
833 vifp->v_tbf->tbf_n_tok = 0;
834 vifp->v_tbf->tbf_q_len = 0;
835 vifp->v_tbf->tbf_max_q_len = MAXQSIZE;
836 vifp->v_tbf->tbf_q = vifp->v_tbf->tbf_t = NULL;
838 vifp->v_flags = vifcp->vifc_flags;
839 vifp->v_threshold = vifcp->vifc_threshold;
840 vifp->v_lcl_addr = vifcp->vifc_lcl_addr;
841 vifp->v_rmt_addr = vifcp->vifc_rmt_addr;
843 /* scaling up here allows division by 1024 in critical code */
844 vifp->v_rate_limit= vifcp->vifc_rate_limit * 1024 / 1000;
846 vifp->v_rsvpd = NULL;
847 /* initialize per vif pkt counters */
850 vifp->v_bytes_in = 0;
851 vifp->v_bytes_out = 0;
853 /* Adjust numvifs up if the vifi is higher than numvifs */
854 if (numvifs <= vifcp->vifc_vifi) numvifs = vifcp->vifc_vifi + 1;
856 lwkt_reltoken(&mroute_token);
859 log(LOG_DEBUG, "add_vif #%d, lcladdr %lx, %s %lx, thresh %x, rate %d\n",
861 (u_long)ntohl(vifcp->vifc_lcl_addr.s_addr),
862 (vifcp->vifc_flags & VIFF_TUNNEL) ? "rmtaddr" : "mask",
863 (u_long)ntohl(vifcp->vifc_rmt_addr.s_addr),
864 vifcp->vifc_threshold,
865 vifcp->vifc_rate_limit);
871 * Delete a vif from the vif table
880 vifp = &viftable[vifi];
881 if (vifp->v_lcl_addr.s_addr == INADDR_ANY)
882 return EADDRNOTAVAIL;
884 lwkt_gettoken(&mroute_token);
886 if (!(vifp->v_flags & (VIFF_TUNNEL | VIFF_REGISTER)))
887 if_allmulti(vifp->v_ifp, 0);
889 if (vifp == last_encap_vif) {
890 last_encap_vif = NULL;
891 last_encap_src = INADDR_ANY;
895 * Free packets queued at the interface
897 while (vifp->v_tbf->tbf_q) {
898 struct mbuf *m = vifp->v_tbf->tbf_q;
900 vifp->v_tbf->tbf_q = m->m_nextpkt;
905 if (vifp->v_flags & VIFF_REGISTER)
906 reg_vif_num = VIFI_INVALID;
909 bzero((caddr_t)vifp->v_tbf, sizeof(*(vifp->v_tbf)));
910 bzero((caddr_t)vifp, sizeof (*vifp));
913 log(LOG_DEBUG, "del_vif %d, numvifs %d\n", vifi, numvifs);
915 /* Adjust numvifs down */
916 for (vifi = numvifs; vifi > 0; vifi--)
917 if (viftable[vifi-1].v_lcl_addr.s_addr != INADDR_ANY)
921 lwkt_reltoken(&mroute_token);
927 * update an mfc entry without resetting counters and S,G addresses.
930 update_mfc_params(struct mfc *rt, struct mfcctl2 *mfccp)
934 rt->mfc_parent = mfccp->mfcc_parent;
935 for (i = 0; i < numvifs; i++) {
936 rt->mfc_ttls[i] = mfccp->mfcc_ttls[i];
937 rt->mfc_flags[i] = mfccp->mfcc_flags[i] & mrt_api_config &
940 /* set the RP address */
941 if (mrt_api_config & MRT_MFC_RP)
942 rt->mfc_rp = mfccp->mfcc_rp;
944 rt->mfc_rp.s_addr = INADDR_ANY;
948 * fully initialize an mfc entry from the parameter.
951 init_mfc_params(struct mfc *rt, struct mfcctl2 *mfccp)
953 rt->mfc_origin = mfccp->mfcc_origin;
954 rt->mfc_mcastgrp = mfccp->mfcc_mcastgrp;
956 update_mfc_params(rt, mfccp);
958 /* initialize pkt counters per src-grp */
960 rt->mfc_byte_cnt = 0;
961 rt->mfc_wrong_if = 0;
962 rt->mfc_last_assert.tv_sec = rt->mfc_last_assert.tv_usec = 0;
970 add_mfc(struct mfcctl2 *mfccp)
977 rt = mfc_find(mfccp->mfcc_origin.s_addr, mfccp->mfcc_mcastgrp.s_addr);
979 /* If an entry already exists, just update the fields */
981 if (mrtdebug & DEBUG_MFC)
982 log(LOG_DEBUG,"add_mfc update o %lx g %lx p %x\n",
983 (u_long)ntohl(mfccp->mfcc_origin.s_addr),
984 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
987 lwkt_gettoken(&mroute_token);
988 update_mfc_params(rt, mfccp);
989 lwkt_reltoken(&mroute_token);
994 * Find the entry for which the upcall was made and update
996 lwkt_gettoken(&mroute_token);
997 hash = MFCHASH(mfccp->mfcc_origin.s_addr, mfccp->mfcc_mcastgrp.s_addr);
998 for (rt = mfctable[hash], nstl = 0; rt; rt = rt->mfc_next) {
1000 if ((rt->mfc_origin.s_addr == mfccp->mfcc_origin.s_addr) &&
1001 (rt->mfc_mcastgrp.s_addr == mfccp->mfcc_mcastgrp.s_addr) &&
1002 (rt->mfc_stall != NULL)) {
1005 log(LOG_ERR, "add_mfc %s o %lx g %lx p %x dbx %p\n",
1006 "multiple kernel entries",
1007 (u_long)ntohl(mfccp->mfcc_origin.s_addr),
1008 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
1009 mfccp->mfcc_parent, (void *)rt->mfc_stall);
1011 if (mrtdebug & DEBUG_MFC)
1012 log(LOG_DEBUG,"add_mfc o %lx g %lx p %x dbg %p\n",
1013 (u_long)ntohl(mfccp->mfcc_origin.s_addr),
1014 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
1015 mfccp->mfcc_parent, (void *)rt->mfc_stall);
1017 init_mfc_params(rt, mfccp);
1019 rt->mfc_expire = 0; /* Don't clean this guy up */
1022 /* free packets Qed at the end of this entry */
1023 for (rte = rt->mfc_stall; rte != NULL; ) {
1024 struct rtdetq *n = rte->next;
1026 ip_mdq(rte->m, rte->ifp, rt, -1);
1028 kfree(rte, M_MRTABLE);
1031 rt->mfc_stall = NULL;
1036 * It is possible that an entry is being inserted without an upcall
1039 if (mrtdebug & DEBUG_MFC)
1040 log(LOG_DEBUG,"add_mfc no upcall h %lu o %lx g %lx p %x\n",
1041 hash, (u_long)ntohl(mfccp->mfcc_origin.s_addr),
1042 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
1043 mfccp->mfcc_parent);
1045 for (rt = mfctable[hash]; rt != NULL; rt = rt->mfc_next) {
1046 if ((rt->mfc_origin.s_addr == mfccp->mfcc_origin.s_addr) &&
1047 (rt->mfc_mcastgrp.s_addr == mfccp->mfcc_mcastgrp.s_addr)) {
1048 init_mfc_params(rt, mfccp);
1055 if (rt == NULL) { /* no upcall, so make a new entry */
1056 rt = kmalloc(sizeof(*rt), M_MRTABLE, M_INTWAIT | M_NULLOK);
1058 lwkt_reltoken(&mroute_token);
1062 init_mfc_params(rt, mfccp);
1064 rt->mfc_stall = NULL;
1066 rt->mfc_bw_meter = NULL;
1067 /* insert new entry at head of hash chain */
1068 rt->mfc_next = mfctable[hash];
1069 mfctable[hash] = rt;
1072 lwkt_reltoken(&mroute_token);
1077 * Delete an mfc entry
1080 del_mfc(struct mfcctl2 *mfccp)
1082 struct in_addr origin;
1083 struct in_addr mcastgrp;
1087 struct bw_meter *list;
1089 origin = mfccp->mfcc_origin;
1090 mcastgrp = mfccp->mfcc_mcastgrp;
1092 if (mrtdebug & DEBUG_MFC)
1093 log(LOG_DEBUG,"del_mfc orig %lx mcastgrp %lx\n",
1094 (u_long)ntohl(origin.s_addr), (u_long)ntohl(mcastgrp.s_addr));
1096 lwkt_gettoken(&mroute_token);
1098 hash = MFCHASH(origin.s_addr, mcastgrp.s_addr);
1099 for (nptr = &mfctable[hash]; (rt = *nptr) != NULL; nptr = &rt->mfc_next)
1100 if (origin.s_addr == rt->mfc_origin.s_addr &&
1101 mcastgrp.s_addr == rt->mfc_mcastgrp.s_addr &&
1102 rt->mfc_stall == NULL)
1105 lwkt_reltoken(&mroute_token);
1106 return EADDRNOTAVAIL;
1109 *nptr = rt->mfc_next;
1112 * free the bw_meter entries
1114 list = rt->mfc_bw_meter;
1115 rt->mfc_bw_meter = NULL;
1116 lwkt_reltoken(&mroute_token);
1118 kfree(rt, M_MRTABLE);
1125 * Send a message to mrouted on the multicast routing socket
1128 socket_send(struct socket *s, struct mbuf *mm, struct sockaddr_in *src)
1131 if (ssb_appendaddr(&s->so_rcv, (struct sockaddr *)src, mm, NULL) != 0) {
1141 * IP multicast forwarding function. This function assumes that the packet
1142 * pointed to by "ip" has arrived on (or is about to be sent to) the interface
1143 * pointed to by "ifp", and the packet is to be relayed to other networks
1144 * that have members of the packet's destination IP multicast group.
1146 * The packet is returned unscathed to the caller, unless it is
1147 * erroneous, in which case a non-zero return value tells the caller to
1151 #define TUNNEL_LEN 12 /* # bytes of IP option for tunnel encapsulation */
1154 X_ip_mforward(struct ip *ip, struct ifnet *ifp, struct mbuf *m,
1155 struct ip_moptions *imo)
1160 if (mrtdebug & DEBUG_FORWARD)
1161 log(LOG_DEBUG, "ip_mforward: src %lx, dst %lx, ifp %p\n",
1162 (u_long)ntohl(ip->ip_src.s_addr), (u_long)ntohl(ip->ip_dst.s_addr),
1165 if (ip->ip_hl < (sizeof(struct ip) + TUNNEL_LEN) >> 2 ||
1166 ((u_char *)(ip + 1))[1] != IPOPT_LSRR ) {
1168 * Packet arrived via a physical interface or
1169 * an encapsulated tunnel or a register_vif.
1173 * Packet arrived through a source-route tunnel.
1174 * Source-route tunnels are no longer supported.
1176 static time_t last_log;
1177 if (last_log != time_uptime) {
1178 last_log = time_uptime;
1180 "ip_mforward: received source-routed packet from %lx\n",
1181 (u_long)ntohl(ip->ip_src.s_addr));
1186 if (imo && ((vifi = imo->imo_multicast_vif) < numvifs)) {
1187 if (ip->ip_ttl < 255)
1188 ip->ip_ttl++; /* compensate for -1 in *_send routines */
1189 if (rsvpdebug && ip->ip_p == IPPROTO_RSVP) {
1190 struct vif *vifp = viftable + vifi;
1192 kprintf("Sending IPPROTO_RSVP from %lx to %lx on vif %d (%s%s)\n",
1193 (long)ntohl(ip->ip_src.s_addr), (long)ntohl(ip->ip_dst.s_addr),
1195 (vifp->v_flags & VIFF_TUNNEL) ? "tunnel on " : "",
1196 vifp->v_ifp->if_xname);
1198 return ip_mdq(m, ifp, NULL, vifi);
1200 if (rsvpdebug && ip->ip_p == IPPROTO_RSVP) {
1201 kprintf("Warning: IPPROTO_RSVP from %lx to %lx without vif option\n",
1202 (long)ntohl(ip->ip_src.s_addr), (long)ntohl(ip->ip_dst.s_addr));
1204 kprintf("In fact, no options were specified at all\n");
1208 * Don't forward a packet with time-to-live of zero or one,
1209 * or a packet destined to a local-only group.
1211 if (ip->ip_ttl <= 1 || ntohl(ip->ip_dst.s_addr) <= INADDR_MAX_LOCAL_GROUP)
1215 * Determine forwarding vifs from the forwarding cache table
1217 lwkt_gettoken(&mroute_token);
1218 ++mrtstat.mrts_mfc_lookups;
1219 rt = mfc_find(ip->ip_src.s_addr, ip->ip_dst.s_addr);
1221 /* Entry exists, so forward if necessary */
1223 int ipres = ip_mdq(m, ifp, rt, -1);
1224 lwkt_reltoken(&mroute_token);
1228 * If we don't have a route for packet's origin,
1229 * Make a copy of the packet & send message to routing daemon
1235 int hlen = ip->ip_hl << 2;
1237 ++mrtstat.mrts_mfc_misses;
1239 mrtstat.mrts_no_route++;
1240 if (mrtdebug & (DEBUG_FORWARD | DEBUG_MFC))
1241 log(LOG_DEBUG, "ip_mforward: no rte s %lx g %lx\n",
1242 (u_long)ntohl(ip->ip_src.s_addr),
1243 (u_long)ntohl(ip->ip_dst.s_addr));
1246 * Allocate mbufs early so that we don't do extra work if we are
1247 * just going to fail anyway. Make sure to pullup the header so
1248 * that other people can't step on it.
1250 rte = kmalloc((sizeof *rte), M_MRTABLE, M_INTWAIT | M_NULLOK);
1252 lwkt_reltoken(&mroute_token);
1256 mb0 = m_copypacket(m, MB_DONTWAIT);
1257 if (mb0 && (M_HASCL(mb0) || mb0->m_len < hlen))
1258 mb0 = m_pullup(mb0, hlen);
1260 kfree(rte, M_MRTABLE);
1261 lwkt_reltoken(&mroute_token);
1265 /* is there an upcall waiting for this flow ? */
1266 hash = MFCHASH(ip->ip_src.s_addr, ip->ip_dst.s_addr);
1267 for (rt = mfctable[hash]; rt; rt = rt->mfc_next) {
1268 if ((ip->ip_src.s_addr == rt->mfc_origin.s_addr) &&
1269 (ip->ip_dst.s_addr == rt->mfc_mcastgrp.s_addr) &&
1270 (rt->mfc_stall != NULL))
1277 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
1281 * Locate the vifi for the incoming interface for this packet.
1282 * If none found, drop packet.
1284 for (vifi=0; vifi < numvifs && viftable[vifi].v_ifp != ifp; vifi++)
1286 if (vifi >= numvifs) /* vif not found, drop packet */
1289 /* no upcall, so make a new entry */
1290 rt = kmalloc(sizeof(*rt), M_MRTABLE, M_INTWAIT | M_NULLOK);
1294 /* Make a copy of the header to send to the user level process */
1295 mm = m_copy(mb0, 0, hlen);
1300 * Send message to routing daemon to install
1301 * a route into the kernel table
1304 im = mtod(mm, struct igmpmsg *);
1305 im->im_msgtype = IGMPMSG_NOCACHE;
1309 mrtstat.mrts_upcalls++;
1311 k_igmpsrc.sin_addr = ip->ip_src;
1312 if (socket_send(ip_mrouter, mm, &k_igmpsrc) < 0) {
1313 log(LOG_WARNING, "ip_mforward: ip_mrouter socket queue full\n");
1314 ++mrtstat.mrts_upq_sockfull;
1316 kfree(rt, M_MRTABLE);
1318 kfree(rte, M_MRTABLE);
1320 lwkt_reltoken(&mroute_token);
1324 /* insert new entry at head of hash chain */
1325 rt->mfc_origin.s_addr = ip->ip_src.s_addr;
1326 rt->mfc_mcastgrp.s_addr = ip->ip_dst.s_addr;
1327 rt->mfc_expire = UPCALL_EXPIRE;
1329 for (i = 0; i < numvifs; i++) {
1330 rt->mfc_ttls[i] = 0;
1331 rt->mfc_flags[i] = 0;
1333 rt->mfc_parent = -1;
1335 rt->mfc_rp.s_addr = INADDR_ANY; /* clear the RP address */
1337 rt->mfc_bw_meter = NULL;
1339 /* link into table */
1340 rt->mfc_next = mfctable[hash];
1341 mfctable[hash] = rt;
1342 rt->mfc_stall = rte;
1345 /* determine if q has overflowed */
1350 * XXX ouch! we need to append to the list, but we
1351 * only have a pointer to the front, so we have to
1352 * scan the entire list every time.
1354 for (p = &rt->mfc_stall; *p != NULL; p = &(*p)->next)
1357 if (npkts > MAX_UPQ) {
1358 mrtstat.mrts_upq_ovflw++;
1360 kfree(rte, M_MRTABLE);
1362 lwkt_reltoken(&mroute_token);
1366 /* Add this entry to the end of the queue */
1374 lwkt_reltoken(&mroute_token);
1380 * Clean up the cache entry if upcall is not serviced
1383 expire_upcalls(void *unused)
1386 struct mfc *mfc, **nptr;
1389 lwkt_gettoken(&mroute_token);
1390 for (i = 0; i < MFCTBLSIZ; i++) {
1391 if (nexpire[i] == 0)
1393 nptr = &mfctable[i];
1394 for (mfc = *nptr; mfc != NULL; mfc = *nptr) {
1396 * Skip real cache entries
1397 * Make sure it wasn't marked to not expire (shouldn't happen)
1400 if (mfc->mfc_stall != NULL && mfc->mfc_expire != 0 &&
1401 --mfc->mfc_expire == 0) {
1402 if (mrtdebug & DEBUG_EXPIRE)
1403 log(LOG_DEBUG, "expire_upcalls: expiring (%lx %lx)\n",
1404 (u_long)ntohl(mfc->mfc_origin.s_addr),
1405 (u_long)ntohl(mfc->mfc_mcastgrp.s_addr));
1407 * drop all the packets
1408 * free the mbuf with the pkt, if, timing info
1410 for (rte = mfc->mfc_stall; rte; ) {
1411 struct rtdetq *n = rte->next;
1414 kfree(rte, M_MRTABLE);
1417 ++mrtstat.mrts_cache_cleanups;
1421 * free the bw_meter entries
1423 while (mfc->mfc_bw_meter != NULL) {
1424 struct bw_meter *x = mfc->mfc_bw_meter;
1426 mfc->mfc_bw_meter = x->bm_mfc_next;
1427 kfree(x, M_BWMETER);
1430 *nptr = mfc->mfc_next;
1431 kfree(mfc, M_MRTABLE);
1433 nptr = &mfc->mfc_next;
1437 callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT, expire_upcalls, NULL);
1438 lwkt_reltoken(&mroute_token);
1442 * Packet forwarding routine once entry in the cache is made
1445 ip_mdq(struct mbuf *m, struct ifnet *ifp, struct mfc *rt, vifi_t xmt_vif)
1447 struct ip *ip = mtod(m, struct ip *);
1449 int plen = ip->ip_len;
1452 * Macro to send packet on vif. Since RSVP packets don't get counted on
1453 * input, they shouldn't get counted on output, so statistics keeping is
1456 #define MC_SEND(ip,vifp,m) { \
1457 if ((vifp)->v_flags & VIFF_TUNNEL) \
1458 encap_send((ip), (vifp), (m)); \
1460 phyint_send((ip), (vifp), (m)); \
1464 * If xmt_vif is not -1, send on only the requested vif.
1466 * (since vifi_t is u_short, -1 becomes MAXUSHORT, which > numvifs.)
1468 if (xmt_vif < numvifs) {
1470 if (viftable[xmt_vif].v_flags & VIFF_REGISTER)
1471 pim_register_send(ip, viftable + xmt_vif, m, rt);
1474 MC_SEND(ip, viftable + xmt_vif, m);
1479 * Don't forward if it didn't arrive from the parent vif for its origin.
1481 vifi = rt->mfc_parent;
1482 if ((vifi >= numvifs) || (viftable[vifi].v_ifp != ifp)) {
1483 /* came in the wrong interface */
1484 if (mrtdebug & DEBUG_FORWARD)
1485 log(LOG_DEBUG, "wrong if: ifp %p vifi %d vififp %p\n",
1486 (void *)ifp, vifi, (void *)viftable[vifi].v_ifp);
1487 ++mrtstat.mrts_wrong_if;
1490 * If we are doing PIM assert processing, send a message
1491 * to the routing daemon.
1493 * XXX: A PIM-SM router needs the WRONGVIF detection so it
1494 * can complete the SPT switch, regardless of the type
1495 * of the iif (broadcast media, GRE tunnel, etc).
1497 if (pim_assert && (vifi < numvifs) && viftable[vifi].v_ifp) {
1502 if (ifp == &multicast_register_if)
1503 pimstat.pims_rcv_registers_wrongiif++;
1506 /* Get vifi for the incoming packet */
1507 for (vifi=0; vifi < numvifs && viftable[vifi].v_ifp != ifp; vifi++)
1509 if (vifi >= numvifs)
1510 return 0; /* The iif is not found: ignore the packet. */
1512 if (rt->mfc_flags[vifi] & MRT_MFC_FLAGS_DISABLE_WRONGVIF)
1513 return 0; /* WRONGVIF disabled: ignore the packet */
1517 TV_DELTA(rt->mfc_last_assert, now, delta);
1519 if (delta > ASSERT_MSG_TIME) {
1520 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
1522 int hlen = ip->ip_hl << 2;
1523 struct mbuf *mm = m_copy(m, 0, hlen);
1525 if (mm && (M_HASCL(mm) || mm->m_len < hlen))
1526 mm = m_pullup(mm, hlen);
1530 rt->mfc_last_assert = now;
1532 im = mtod(mm, struct igmpmsg *);
1533 im->im_msgtype = IGMPMSG_WRONGVIF;
1537 mrtstat.mrts_upcalls++;
1539 k_igmpsrc.sin_addr = im->im_src;
1540 if (socket_send(ip_mrouter, mm, &k_igmpsrc) < 0) {
1542 "ip_mforward: ip_mrouter socket queue full\n");
1543 ++mrtstat.mrts_upq_sockfull;
1551 /* If I sourced this packet, it counts as output, else it was input. */
1552 if (ip->ip_src.s_addr == viftable[vifi].v_lcl_addr.s_addr) {
1553 viftable[vifi].v_pkt_out++;
1554 viftable[vifi].v_bytes_out += plen;
1556 viftable[vifi].v_pkt_in++;
1557 viftable[vifi].v_bytes_in += plen;
1560 rt->mfc_byte_cnt += plen;
1563 * For each vif, decide if a copy of the packet should be forwarded.
1565 * - the ttl exceeds the vif's threshold
1566 * - there are group members downstream on interface
1568 for (vifi = 0; vifi < numvifs; vifi++)
1569 if ((rt->mfc_ttls[vifi] > 0) && (ip->ip_ttl > rt->mfc_ttls[vifi])) {
1570 viftable[vifi].v_pkt_out++;
1571 viftable[vifi].v_bytes_out += plen;
1573 if (viftable[vifi].v_flags & VIFF_REGISTER)
1574 pim_register_send(ip, viftable + vifi, m, rt);
1577 MC_SEND(ip, viftable+vifi, m);
1581 * Perform upcall-related bw measuring.
1583 if (rt->mfc_bw_meter != NULL) {
1588 for (x = rt->mfc_bw_meter; x != NULL; x = x->bm_mfc_next)
1589 bw_meter_receive_packet(x, plen, &now);
1596 * check if a vif number is legal/ok. This is used by ip_output.
1599 X_legal_vif_num(int vif)
1601 return (vif >= 0 && vif < numvifs);
1605 * Return the local address used by this vif
1608 X_ip_mcast_src(int vifi)
1610 if (vifi >= 0 && vifi < numvifs)
1611 return viftable[vifi].v_lcl_addr.s_addr;
1617 phyint_send(struct ip *ip, struct vif *vifp, struct mbuf *m)
1619 struct mbuf *mb_copy;
1620 int hlen = ip->ip_hl << 2;
1623 * Make a new reference to the packet; make sure that
1624 * the IP header is actually copied, not just referenced,
1625 * so that ip_output() only scribbles on the copy.
1627 mb_copy = m_copypacket(m, MB_DONTWAIT);
1628 if (mb_copy && (M_HASCL(mb_copy) || mb_copy->m_len < hlen))
1629 mb_copy = m_pullup(mb_copy, hlen);
1630 if (mb_copy == NULL)
1633 if (vifp->v_rate_limit == 0)
1634 tbf_send_packet(vifp, mb_copy);
1636 tbf_control(vifp, mb_copy, mtod(mb_copy, struct ip *), ip->ip_len);
1640 encap_send(struct ip *ip, struct vif *vifp, struct mbuf *m)
1642 struct mbuf *mb_copy;
1644 int i, len = ip->ip_len;
1646 /* Take care of delayed checksums */
1647 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
1648 in_delayed_cksum(m);
1649 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
1653 * copy the old packet & pullup its IP header into the
1654 * new mbuf so we can modify it. Try to fill the new
1655 * mbuf since if we don't the ethernet driver will.
1657 MGETHDR(mb_copy, MB_DONTWAIT, MT_HEADER);
1658 if (mb_copy == NULL)
1660 mb_copy->m_data += max_linkhdr;
1661 mb_copy->m_len = sizeof(multicast_encap_iphdr);
1663 if ((mb_copy->m_next = m_copypacket(m, MB_DONTWAIT)) == NULL) {
1667 i = MHLEN - M_LEADINGSPACE(mb_copy);
1670 mb_copy = m_pullup(mb_copy, i);
1671 if (mb_copy == NULL)
1673 mb_copy->m_pkthdr.len = len + sizeof(multicast_encap_iphdr);
1676 * fill in the encapsulating IP header.
1678 ip_copy = mtod(mb_copy, struct ip *);
1679 *ip_copy = multicast_encap_iphdr;
1680 ip_copy->ip_id = ip_newid();
1681 ip_copy->ip_len += len;
1682 ip_copy->ip_src = vifp->v_lcl_addr;
1683 ip_copy->ip_dst = vifp->v_rmt_addr;
1686 * turn the encapsulated IP header back into a valid one.
1688 ip = (struct ip *)((caddr_t)ip_copy + sizeof(multicast_encap_iphdr));
1690 ip->ip_len = htons(ip->ip_len);
1691 ip->ip_off = htons(ip->ip_off);
1693 mb_copy->m_data += sizeof(multicast_encap_iphdr);
1694 ip->ip_sum = in_cksum(mb_copy, ip->ip_hl << 2);
1695 mb_copy->m_data -= sizeof(multicast_encap_iphdr);
1697 if (vifp->v_rate_limit == 0)
1698 tbf_send_packet(vifp, mb_copy);
1700 tbf_control(vifp, mb_copy, ip, ip_copy->ip_len);
1704 * De-encapsulate a packet and feed it back through ip input (this
1705 * routine is called whenever IP gets a packet with proto type
1706 * ENCAP_PROTO and a local destination address).
1708 * This is similar to mroute_encapcheck() + mroute_encap_input() in -current.
1711 X_ipip_input(struct mbuf **mp, int *offp, int proto)
1713 struct mbuf *m = *mp;
1714 struct ip *ip = mtod(m, struct ip *);
1715 int hlen = ip->ip_hl << 2;
1717 if (!have_encap_tunnel) {
1718 rip_input(mp, offp, proto);
1719 return(IPPROTO_DONE);
1724 * dump the packet if it's not to a multicast destination or if
1725 * we don't have an encapsulating tunnel with the source.
1726 * Note: This code assumes that the remote site IP address
1727 * uniquely identifies the tunnel (i.e., that this site has
1728 * at most one tunnel with the remote site).
1730 if (!IN_MULTICAST(ntohl(((struct ip *)((char *)ip+hlen))->ip_dst.s_addr))) {
1731 ++mrtstat.mrts_bad_tunnel;
1733 return(IPPROTO_DONE);
1735 if (ip->ip_src.s_addr != last_encap_src) {
1736 struct vif *vifp = viftable;
1737 struct vif *vife = vifp + numvifs;
1739 last_encap_src = ip->ip_src.s_addr;
1740 last_encap_vif = NULL;
1741 for ( ; vifp < vife; ++vifp)
1742 if (vifp->v_rmt_addr.s_addr == ip->ip_src.s_addr) {
1743 if ((vifp->v_flags & (VIFF_TUNNEL|VIFF_SRCRT))
1745 last_encap_vif = vifp;
1749 if (last_encap_vif == NULL) {
1750 last_encap_src = INADDR_ANY;
1751 mrtstat.mrts_cant_tunnel++; /*XXX*/
1754 log(LOG_DEBUG, "ip_mforward: no tunnel with %lx\n",
1755 (u_long)ntohl(ip->ip_src.s_addr));
1756 return(IPPROTO_DONE);
1759 if (hlen > sizeof(struct ip))
1761 m->m_data += sizeof(struct ip);
1762 m->m_len -= sizeof(struct ip);
1763 m->m_pkthdr.len -= sizeof(struct ip);
1764 m->m_pkthdr.rcvif = last_encap_vif->v_ifp;
1766 netisr_queue(NETISR_IP, m);
1767 return(IPPROTO_DONE);
1771 * Token bucket filter module
1775 tbf_control(struct vif *vifp, struct mbuf *m, struct ip *ip, u_long p_len)
1777 struct tbf *t = vifp->v_tbf;
1779 if (p_len > MAX_BKT_SIZE) { /* drop if packet is too large */
1780 mrtstat.mrts_pkt2large++;
1785 tbf_update_tokens(vifp);
1787 if (t->tbf_q_len == 0) { /* queue empty... */
1788 if (p_len <= t->tbf_n_tok) { /* send packet if enough tokens */
1789 t->tbf_n_tok -= p_len;
1790 tbf_send_packet(vifp, m);
1791 } else { /* no, queue packet and try later */
1793 callout_reset(&tbf_reprocess_q_ch, TBF_REPROCESS,
1794 tbf_reprocess_q, vifp);
1796 } else if (t->tbf_q_len < t->tbf_max_q_len) {
1797 /* finite queue length, so queue pkts and process queue */
1799 tbf_process_q(vifp);
1801 /* queue full, try to dq and queue and process */
1802 if (!tbf_dq_sel(vifp, ip)) {
1803 mrtstat.mrts_q_overflow++;
1807 tbf_process_q(vifp);
1813 * adds a packet to the queue at the interface
1816 tbf_queue(struct vif *vifp, struct mbuf *m)
1818 struct tbf *t = vifp->v_tbf;
1820 lwkt_gettoken(&mroute_token);
1822 if (t->tbf_t == NULL) /* Queue was empty */
1824 else /* Insert at tail */
1825 t->tbf_t->m_nextpkt = m;
1827 t->tbf_t = m; /* Set new tail pointer */
1830 /* Make sure we didn't get fed a bogus mbuf */
1832 panic("tbf_queue: m_nextpkt");
1834 m->m_nextpkt = NULL;
1838 lwkt_reltoken(&mroute_token);
1842 * processes the queue at the interface
1845 tbf_process_q(struct vif *vifp)
1847 struct tbf *t = vifp->v_tbf;
1849 lwkt_gettoken(&mroute_token);
1851 /* loop through the queue at the interface and send as many packets
1854 while (t->tbf_q_len > 0) {
1855 struct mbuf *m = t->tbf_q;
1856 int len = mtod(m, struct ip *)->ip_len;
1858 /* determine if the packet can be sent */
1859 if (len > t->tbf_n_tok) /* not enough tokens, we are done */
1861 /* ok, reduce no of tokens, dequeue and send the packet. */
1862 t->tbf_n_tok -= len;
1864 t->tbf_q = m->m_nextpkt;
1865 if (--t->tbf_q_len == 0)
1868 m->m_nextpkt = NULL;
1869 tbf_send_packet(vifp, m);
1871 lwkt_reltoken(&mroute_token);
1875 tbf_reprocess_q(void *xvifp)
1877 struct vif *vifp = xvifp;
1879 if (ip_mrouter == NULL)
1881 tbf_update_tokens(vifp);
1882 tbf_process_q(vifp);
1883 if (vifp->v_tbf->tbf_q_len)
1884 callout_reset(&tbf_reprocess_q_ch, TBF_REPROCESS,
1885 tbf_reprocess_q, vifp);
1888 /* function that will selectively discard a member of the queue
1889 * based on the precedence value and the priority
1892 tbf_dq_sel(struct vif *vifp, struct ip *ip)
1895 struct mbuf *m, *last;
1897 struct tbf *t = vifp->v_tbf;
1899 lwkt_gettoken(&mroute_token);
1901 p = priority(vifp, ip);
1905 while ((m = *np) != NULL) {
1906 if (p > priority(vifp, mtod(m, struct ip *))) {
1908 /* If we're removing the last packet, fix the tail pointer */
1912 /* It's impossible for the queue to be empty, but check anyways. */
1913 if (--t->tbf_q_len == 0)
1915 mrtstat.mrts_drop_sel++;
1916 lwkt_reltoken(&mroute_token);
1922 lwkt_reltoken(&mroute_token);
1927 tbf_send_packet(struct vif *vifp, struct mbuf *m)
1929 lwkt_gettoken(&mroute_token);
1931 if (vifp->v_flags & VIFF_TUNNEL) /* If tunnel options */
1932 ip_output(m, NULL, &vifp->v_route, IP_FORWARDING, NULL, NULL);
1934 struct ip_moptions imo;
1936 static struct route ro; /* XXX check this */
1938 imo.imo_multicast_ifp = vifp->v_ifp;
1939 imo.imo_multicast_ttl = mtod(m, struct ip *)->ip_ttl - 1;
1940 imo.imo_multicast_loop = 1;
1941 imo.imo_multicast_vif = -1;
1944 * Re-entrancy should not be a problem here, because
1945 * the packets that we send out and are looped back at us
1946 * should get rejected because they appear to come from
1947 * the loopback interface, thus preventing looping.
1949 error = ip_output(m, NULL, &ro, IP_FORWARDING, &imo, NULL);
1951 if (mrtdebug & DEBUG_XMIT)
1952 log(LOG_DEBUG, "phyint_send on vif %d err %d\n",
1953 (int)(vifp - viftable), error);
1955 lwkt_reltoken(&mroute_token);
1958 /* determine the current time and then
1959 * the elapsed time (between the last time and time now)
1960 * in milliseconds & update the no. of tokens in the bucket
1963 tbf_update_tokens(struct vif *vifp)
1967 struct tbf *t = vifp->v_tbf;
1969 lwkt_gettoken(&mroute_token);
1973 TV_DELTA(tp, t->tbf_last_pkt_t, tm);
1976 * This formula is actually
1977 * "time in seconds" * "bytes/second".
1979 * (tm / 1000000) * (v_rate_limit * 1000 * (1000/1024) / 8)
1981 * The (1000/1024) was introduced in add_vif to optimize
1982 * this divide into a shift.
1984 t->tbf_n_tok += tm * vifp->v_rate_limit / 1024 / 8;
1985 t->tbf_last_pkt_t = tp;
1987 if (t->tbf_n_tok > MAX_BKT_SIZE)
1988 t->tbf_n_tok = MAX_BKT_SIZE;
1990 lwkt_reltoken(&mroute_token);
1994 priority(struct vif *vifp, struct ip *ip)
1996 int prio = 50; /* the lowest priority -- default case */
1998 /* temporary hack; may add general packet classifier some day */
2001 * The UDP port space is divided up into four priority ranges:
2002 * [0, 16384) : unclassified - lowest priority
2003 * [16384, 32768) : audio - highest priority
2004 * [32768, 49152) : whiteboard - medium priority
2005 * [49152, 65536) : video - low priority
2007 * Everything else gets lowest priority.
2009 if (ip->ip_p == IPPROTO_UDP) {
2010 struct udphdr *udp = (struct udphdr *)(((char *)ip) + (ip->ip_hl << 2));
2011 switch (ntohs(udp->uh_dport) & 0xc000) {
2027 * End of token bucket filter modifications
2031 X_ip_rsvp_vif(struct socket *so, struct sockopt *sopt)
2035 if (so->so_type != SOCK_RAW || so->so_proto->pr_protocol != IPPROTO_RSVP)
2038 error = soopt_to_kbuf(sopt, &vifi, sizeof vifi, sizeof vifi);
2042 lwkt_gettoken(&mroute_token);
2044 if (vifi < 0 || vifi >= numvifs) { /* Error if vif is invalid */
2045 lwkt_reltoken(&mroute_token);
2046 return EADDRNOTAVAIL;
2049 if (sopt->sopt_name == IP_RSVP_VIF_ON) {
2050 /* Check if socket is available. */
2051 if (viftable[vifi].v_rsvpd != NULL) {
2052 lwkt_reltoken(&mroute_token);
2056 viftable[vifi].v_rsvpd = so;
2057 /* This may seem silly, but we need to be sure we don't over-increment
2058 * the RSVP counter, in case something slips up.
2060 if (!viftable[vifi].v_rsvp_on) {
2061 viftable[vifi].v_rsvp_on = 1;
2064 } else { /* must be VIF_OFF */
2066 * XXX as an additional consistency check, one could make sure
2067 * that viftable[vifi].v_rsvpd == so, otherwise passing so as
2068 * first parameter is pretty useless.
2070 viftable[vifi].v_rsvpd = NULL;
2072 * This may seem silly, but we need to be sure we don't over-decrement
2073 * the RSVP counter, in case something slips up.
2075 if (viftable[vifi].v_rsvp_on) {
2076 viftable[vifi].v_rsvp_on = 0;
2080 lwkt_reltoken(&mroute_token);
2085 X_ip_rsvp_force_done(struct socket *so)
2089 /* Don't bother if it is not the right type of socket. */
2090 if (so->so_type != SOCK_RAW || so->so_proto->pr_protocol != IPPROTO_RSVP)
2093 lwkt_gettoken(&mroute_token);
2095 /* The socket may be attached to more than one vif...this
2096 * is perfectly legal.
2098 for (vifi = 0; vifi < numvifs; vifi++) {
2099 if (viftable[vifi].v_rsvpd == so) {
2100 viftable[vifi].v_rsvpd = NULL;
2101 /* This may seem silly, but we need to be sure we don't
2102 * over-decrement the RSVP counter, in case something slips up.
2104 if (viftable[vifi].v_rsvp_on) {
2105 viftable[vifi].v_rsvp_on = 0;
2111 lwkt_reltoken(&mroute_token);
2115 X_rsvp_input(struct mbuf **mp, int *offp, int proto)
2118 struct mbuf *m = *mp;
2119 struct ip *ip = mtod(m, struct ip *);
2120 struct sockaddr_in rsvp_src = { sizeof rsvp_src, AF_INET };
2123 /* support IP_RECVIF used by rsvpd rel4.2a1 */
2132 kprintf("rsvp_input: rsvp_on %d\n",rsvp_on);
2134 /* Can still get packets with rsvp_on = 0 if there is a local member
2135 * of the group to which the RSVP packet is addressed. But in this
2136 * case we want to throw the packet away.
2140 return(IPPROTO_DONE);
2143 lwkt_gettoken(&mroute_token);
2146 kprintf("rsvp_input: check vifs\n");
2149 if (!(m->m_flags & M_PKTHDR))
2150 panic("rsvp_input no hdr");
2153 ifp = m->m_pkthdr.rcvif;
2154 /* Find which vif the packet arrived on. */
2155 for (vifi = 0; vifi < numvifs; vifi++)
2156 if (viftable[vifi].v_ifp == ifp)
2160 if (vifi == numvifs || (so = viftable[vifi].v_rsvpd) == NULL) {
2162 if (vifi == numvifs || viftable[vifi].v_rsvpd == NULL) {
2165 * If the old-style non-vif-associated socket is set,
2166 * then use it. Otherwise, drop packet since there
2167 * is no specific socket for this vif.
2169 if (ip_rsvpd != NULL) {
2171 kprintf("rsvp_input: Sending packet up old-style socket\n");
2173 rip_input(mp, offp, proto); /* xxx */
2175 if (rsvpdebug && vifi == numvifs)
2176 kprintf("rsvp_input: Can't find vif for packet.\n");
2177 else if (rsvpdebug && viftable[vifi].v_rsvpd == NULL)
2178 kprintf("rsvp_input: No socket defined for vif %d\n",vifi);
2181 lwkt_reltoken(&mroute_token);
2182 return(IPPROTO_DONE);
2184 rsvp_src.sin_addr = ip->ip_src;
2187 kprintf("rsvp_input: m->m_len = %d, ssb_space() = %ld\n",
2188 m->m_len,ssb_space(&(viftable[vifi].v_rsvpd->so_rcv)));
2192 inp = (struct inpcb *)so->so_pcb;
2193 if (inp->inp_flags & INP_CONTROLOPTS ||
2194 inp->inp_socket->so_options & SO_TIMESTAMP) {
2195 ip_savecontrol(inp, &opts, ip, m);
2197 if (ssb_appendaddr(&so->so_rcv,
2198 (struct sockaddr *)&rsvp_src,m, opts) == 0) {
2203 kprintf("rsvp_input: Failed to append to socket\n");
2208 kprintf("rsvp_input: send packet up\n");
2211 if (socket_send(viftable[vifi].v_rsvpd, m, &rsvp_src) < 0) {
2213 kprintf("rsvp_input: Failed to append to socket\n");
2216 kprintf("rsvp_input: send packet up\n");
2219 lwkt_reltoken(&mroute_token);
2220 return(IPPROTO_DONE);
2224 * Code for bandwidth monitors
2228 * Define common interface for timeval-related methods
2230 #define BW_TIMEVALCMP(tvp, uvp, cmp) timevalcmp((tvp), (uvp), cmp)
2231 #define BW_TIMEVALDECR(vvp, uvp) timevalsub((vvp), (uvp))
2232 #define BW_TIMEVALADD(vvp, uvp) timevaladd((vvp), (uvp))
2235 compute_bw_meter_flags(struct bw_upcall *req)
2239 if (req->bu_flags & BW_UPCALL_UNIT_PACKETS)
2240 flags |= BW_METER_UNIT_PACKETS;
2241 if (req->bu_flags & BW_UPCALL_UNIT_BYTES)
2242 flags |= BW_METER_UNIT_BYTES;
2243 if (req->bu_flags & BW_UPCALL_GEQ)
2244 flags |= BW_METER_GEQ;
2245 if (req->bu_flags & BW_UPCALL_LEQ)
2246 flags |= BW_METER_LEQ;
2252 * Add a bw_meter entry
2255 add_bw_upcall(struct bw_upcall *req)
2258 struct timeval delta = { BW_UPCALL_THRESHOLD_INTERVAL_MIN_SEC,
2259 BW_UPCALL_THRESHOLD_INTERVAL_MIN_USEC };
2264 if (!(mrt_api_config & MRT_MFC_BW_UPCALL))
2267 /* Test if the flags are valid */
2268 if (!(req->bu_flags & (BW_UPCALL_UNIT_PACKETS | BW_UPCALL_UNIT_BYTES)))
2270 if (!(req->bu_flags & (BW_UPCALL_GEQ | BW_UPCALL_LEQ)))
2272 if ((req->bu_flags & (BW_UPCALL_GEQ | BW_UPCALL_LEQ))
2273 == (BW_UPCALL_GEQ | BW_UPCALL_LEQ))
2276 /* Test if the threshold time interval is valid */
2277 if (BW_TIMEVALCMP(&req->bu_threshold.b_time, &delta, <))
2280 flags = compute_bw_meter_flags(req);
2283 * Find if we have already same bw_meter entry
2285 lwkt_gettoken(&mroute_token);
2286 mfc = mfc_find(req->bu_src.s_addr, req->bu_dst.s_addr);
2288 lwkt_reltoken(&mroute_token);
2289 return EADDRNOTAVAIL;
2291 for (x = mfc->mfc_bw_meter; x != NULL; x = x->bm_mfc_next) {
2292 if ((BW_TIMEVALCMP(&x->bm_threshold.b_time,
2293 &req->bu_threshold.b_time, ==)) &&
2294 (x->bm_threshold.b_packets == req->bu_threshold.b_packets) &&
2295 (x->bm_threshold.b_bytes == req->bu_threshold.b_bytes) &&
2296 (x->bm_flags & BW_METER_USER_FLAGS) == flags) {
2297 lwkt_reltoken(&mroute_token);
2298 return 0; /* XXX Already installed */
2301 lwkt_reltoken(&mroute_token);
2303 /* Allocate the new bw_meter entry */
2304 x = kmalloc(sizeof(*x), M_BWMETER, M_INTWAIT);
2306 /* Set the new bw_meter entry */
2307 x->bm_threshold.b_time = req->bu_threshold.b_time;
2309 x->bm_start_time = now;
2310 x->bm_threshold.b_packets = req->bu_threshold.b_packets;
2311 x->bm_threshold.b_bytes = req->bu_threshold.b_bytes;
2312 x->bm_measured.b_packets = 0;
2313 x->bm_measured.b_bytes = 0;
2314 x->bm_flags = flags;
2315 x->bm_time_next = NULL;
2316 x->bm_time_hash = BW_METER_BUCKETS;
2318 /* Add the new bw_meter entry to the front of entries for this MFC */
2319 lwkt_gettoken(&mroute_token);
2321 x->bm_mfc_next = mfc->mfc_bw_meter;
2322 mfc->mfc_bw_meter = x;
2323 schedule_bw_meter(x, &now);
2324 lwkt_reltoken(&mroute_token);
2330 free_bw_list(struct bw_meter *list)
2332 while (list != NULL) {
2333 struct bw_meter *x = list;
2335 list = list->bm_mfc_next;
2336 unschedule_bw_meter(x);
2337 kfree(x, M_BWMETER);
2342 * Delete one or multiple bw_meter entries
2345 del_bw_upcall(struct bw_upcall *req)
2350 if (!(mrt_api_config & MRT_MFC_BW_UPCALL))
2353 lwkt_gettoken(&mroute_token);
2354 /* Find the corresponding MFC entry */
2355 mfc = mfc_find(req->bu_src.s_addr, req->bu_dst.s_addr);
2357 lwkt_reltoken(&mroute_token);
2358 return EADDRNOTAVAIL;
2359 } else if (req->bu_flags & BW_UPCALL_DELETE_ALL) {
2361 * Delete all bw_meter entries for this mfc
2363 struct bw_meter *list;
2365 list = mfc->mfc_bw_meter;
2366 mfc->mfc_bw_meter = NULL;
2367 lwkt_reltoken(&mroute_token);
2370 } else { /* Delete a single bw_meter entry */
2371 struct bw_meter *prev;
2374 flags = compute_bw_meter_flags(req);
2376 /* Find the bw_meter entry to delete */
2377 for (prev = NULL, x = mfc->mfc_bw_meter; x != NULL;
2378 prev = x, x = x->bm_mfc_next) {
2379 if ((BW_TIMEVALCMP(&x->bm_threshold.b_time,
2380 &req->bu_threshold.b_time, ==)) &&
2381 (x->bm_threshold.b_packets == req->bu_threshold.b_packets) &&
2382 (x->bm_threshold.b_bytes == req->bu_threshold.b_bytes) &&
2383 (x->bm_flags & BW_METER_USER_FLAGS) == flags)
2386 if (x != NULL) { /* Delete entry from the list for this MFC */
2388 prev->bm_mfc_next = x->bm_mfc_next; /* remove from middle*/
2390 x->bm_mfc->mfc_bw_meter = x->bm_mfc_next;/* new head of list */
2391 unschedule_bw_meter(x);
2392 lwkt_reltoken(&mroute_token);
2393 /* Free the bw_meter entry */
2394 kfree(x, M_BWMETER);
2397 lwkt_reltoken(&mroute_token);
2405 * Perform bandwidth measurement processing that may result in an upcall
2408 bw_meter_receive_packet(struct bw_meter *x, int plen, struct timeval *nowp)
2410 struct timeval delta;
2412 lwkt_gettoken(&mroute_token);
2414 BW_TIMEVALDECR(&delta, &x->bm_start_time);
2416 if (x->bm_flags & BW_METER_GEQ) {
2418 * Processing for ">=" type of bw_meter entry
2420 if (BW_TIMEVALCMP(&delta, &x->bm_threshold.b_time, >)) {
2421 /* Reset the bw_meter entry */
2422 x->bm_start_time = *nowp;
2423 x->bm_measured.b_packets = 0;
2424 x->bm_measured.b_bytes = 0;
2425 x->bm_flags &= ~BW_METER_UPCALL_DELIVERED;
2428 /* Record that a packet is received */
2429 x->bm_measured.b_packets++;
2430 x->bm_measured.b_bytes += plen;
2433 * Test if we should deliver an upcall
2435 if (!(x->bm_flags & BW_METER_UPCALL_DELIVERED)) {
2436 if (((x->bm_flags & BW_METER_UNIT_PACKETS) &&
2437 (x->bm_measured.b_packets >= x->bm_threshold.b_packets)) ||
2438 ((x->bm_flags & BW_METER_UNIT_BYTES) &&
2439 (x->bm_measured.b_bytes >= x->bm_threshold.b_bytes))) {
2440 /* Prepare an upcall for delivery */
2441 bw_meter_prepare_upcall(x, nowp);
2442 x->bm_flags |= BW_METER_UPCALL_DELIVERED;
2445 } else if (x->bm_flags & BW_METER_LEQ) {
2447 * Processing for "<=" type of bw_meter entry
2449 if (BW_TIMEVALCMP(&delta, &x->bm_threshold.b_time, >)) {
2451 * We are behind time with the multicast forwarding table
2452 * scanning for "<=" type of bw_meter entries, so test now
2453 * if we should deliver an upcall.
2455 if (((x->bm_flags & BW_METER_UNIT_PACKETS) &&
2456 (x->bm_measured.b_packets <= x->bm_threshold.b_packets)) ||
2457 ((x->bm_flags & BW_METER_UNIT_BYTES) &&
2458 (x->bm_measured.b_bytes <= x->bm_threshold.b_bytes))) {
2459 /* Prepare an upcall for delivery */
2460 bw_meter_prepare_upcall(x, nowp);
2462 /* Reschedule the bw_meter entry */
2463 unschedule_bw_meter(x);
2464 schedule_bw_meter(x, nowp);
2467 /* Record that a packet is received */
2468 x->bm_measured.b_packets++;
2469 x->bm_measured.b_bytes += plen;
2472 * Test if we should restart the measuring interval
2474 if ((x->bm_flags & BW_METER_UNIT_PACKETS &&
2475 x->bm_measured.b_packets <= x->bm_threshold.b_packets) ||
2476 (x->bm_flags & BW_METER_UNIT_BYTES &&
2477 x->bm_measured.b_bytes <= x->bm_threshold.b_bytes)) {
2478 /* Don't restart the measuring interval */
2480 /* Do restart the measuring interval */
2482 * XXX: note that we don't unschedule and schedule, because this
2483 * might be too much overhead per packet. Instead, when we process
2484 * all entries for a given timer hash bin, we check whether it is
2485 * really a timeout. If not, we reschedule at that time.
2487 x->bm_start_time = *nowp;
2488 x->bm_measured.b_packets = 0;
2489 x->bm_measured.b_bytes = 0;
2490 x->bm_flags &= ~BW_METER_UPCALL_DELIVERED;
2493 lwkt_reltoken(&mroute_token);
2497 * Prepare a bandwidth-related upcall
2500 bw_meter_prepare_upcall(struct bw_meter *x, struct timeval *nowp)
2502 struct timeval delta;
2503 struct bw_upcall *u;
2505 lwkt_gettoken(&mroute_token);
2508 * Compute the measured time interval
2511 BW_TIMEVALDECR(&delta, &x->bm_start_time);
2514 * If there are too many pending upcalls, deliver them now
2516 if (bw_upcalls_n >= BW_UPCALLS_MAX)
2520 * Set the bw_upcall entry
2522 u = &bw_upcalls[bw_upcalls_n++];
2523 u->bu_src = x->bm_mfc->mfc_origin;
2524 u->bu_dst = x->bm_mfc->mfc_mcastgrp;
2525 u->bu_threshold.b_time = x->bm_threshold.b_time;
2526 u->bu_threshold.b_packets = x->bm_threshold.b_packets;
2527 u->bu_threshold.b_bytes = x->bm_threshold.b_bytes;
2528 u->bu_measured.b_time = delta;
2529 u->bu_measured.b_packets = x->bm_measured.b_packets;
2530 u->bu_measured.b_bytes = x->bm_measured.b_bytes;
2532 if (x->bm_flags & BW_METER_UNIT_PACKETS)
2533 u->bu_flags |= BW_UPCALL_UNIT_PACKETS;
2534 if (x->bm_flags & BW_METER_UNIT_BYTES)
2535 u->bu_flags |= BW_UPCALL_UNIT_BYTES;
2536 if (x->bm_flags & BW_METER_GEQ)
2537 u->bu_flags |= BW_UPCALL_GEQ;
2538 if (x->bm_flags & BW_METER_LEQ)
2539 u->bu_flags |= BW_UPCALL_LEQ;
2541 lwkt_reltoken(&mroute_token);
2545 * Send the pending bandwidth-related upcalls
2548 bw_upcalls_send(void)
2551 int len = bw_upcalls_n * sizeof(bw_upcalls[0]);
2552 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
2553 static struct igmpmsg igmpmsg = { 0, /* unused1 */
2555 IGMPMSG_BW_UPCALL,/* im_msgtype */
2560 { 0 } }; /* im_dst */
2562 if (bw_upcalls_n == 0)
2563 return; /* No pending upcalls */
2568 * Allocate a new mbuf, initialize it with the header and
2569 * the payload for the pending calls.
2571 MGETHDR(m, MB_DONTWAIT, MT_HEADER);
2573 log(LOG_WARNING, "bw_upcalls_send: cannot allocate mbuf\n");
2577 m->m_len = m->m_pkthdr.len = 0;
2578 m_copyback(m, 0, sizeof(struct igmpmsg), (caddr_t)&igmpmsg);
2579 m_copyback(m, sizeof(struct igmpmsg), len, (caddr_t)&bw_upcalls[0]);
2583 * XXX do we need to set the address in k_igmpsrc ?
2585 mrtstat.mrts_upcalls++;
2586 if (socket_send(ip_mrouter, m, &k_igmpsrc) < 0) {
2587 log(LOG_WARNING, "bw_upcalls_send: ip_mrouter socket queue full\n");
2588 ++mrtstat.mrts_upq_sockfull;
2593 * Compute the timeout hash value for the bw_meter entries
2595 #define BW_METER_TIMEHASH(bw_meter, hash) \
2597 struct timeval next_timeval = (bw_meter)->bm_start_time; \
2599 BW_TIMEVALADD(&next_timeval, &(bw_meter)->bm_threshold.b_time); \
2600 (hash) = next_timeval.tv_sec; \
2601 if (next_timeval.tv_usec) \
2602 (hash)++; /* XXX: make sure we don't timeout early */ \
2603 (hash) %= BW_METER_BUCKETS; \
2607 * Schedule a timer to process periodically bw_meter entry of type "<="
2608 * by linking the entry in the proper hash bucket.
2611 schedule_bw_meter(struct bw_meter *x, struct timeval *nowp)
2615 if (!(x->bm_flags & BW_METER_LEQ))
2616 return; /* XXX: we schedule timers only for "<=" entries */
2619 * Reset the bw_meter entry
2621 lwkt_gettoken(&mroute_token);
2622 x->bm_start_time = *nowp;
2623 x->bm_measured.b_packets = 0;
2624 x->bm_measured.b_bytes = 0;
2625 x->bm_flags &= ~BW_METER_UPCALL_DELIVERED;
2628 * Compute the timeout hash value and insert the entry
2630 BW_METER_TIMEHASH(x, time_hash);
2631 x->bm_time_next = bw_meter_timers[time_hash];
2632 bw_meter_timers[time_hash] = x;
2633 x->bm_time_hash = time_hash;
2635 lwkt_reltoken(&mroute_token);
2639 * Unschedule the periodic timer that processes bw_meter entry of type "<="
2640 * by removing the entry from the proper hash bucket.
2643 unschedule_bw_meter(struct bw_meter *x)
2646 struct bw_meter *prev, *tmp;
2648 if (!(x->bm_flags & BW_METER_LEQ))
2649 return; /* XXX: we schedule timers only for "<=" entries */
2652 * Compute the timeout hash value and delete the entry
2654 time_hash = x->bm_time_hash;
2655 if (time_hash >= BW_METER_BUCKETS)
2656 return; /* Entry was not scheduled */
2658 for (prev = NULL, tmp = bw_meter_timers[time_hash];
2659 tmp != NULL; prev = tmp, tmp = tmp->bm_time_next)
2664 panic("unschedule_bw_meter: bw_meter entry not found");
2667 prev->bm_time_next = x->bm_time_next;
2669 bw_meter_timers[time_hash] = x->bm_time_next;
2671 x->bm_time_next = NULL;
2672 x->bm_time_hash = BW_METER_BUCKETS;
2677 * Process all "<=" type of bw_meter that should be processed now,
2678 * and for each entry prepare an upcall if necessary. Each processed
2679 * entry is rescheduled again for the (periodic) processing.
2681 * This is run periodically (once per second normally). On each round,
2682 * all the potentially matching entries are in the hash slot that we are
2686 bw_meter_process(void)
2688 static uint32_t last_tv_sec; /* last time we processed this */
2692 struct timeval now, process_endtime;
2695 if (last_tv_sec == now.tv_sec)
2696 return; /* nothing to do */
2698 lwkt_gettoken(&mroute_token);
2699 loops = now.tv_sec - last_tv_sec;
2700 last_tv_sec = now.tv_sec;
2701 if (loops > BW_METER_BUCKETS)
2702 loops = BW_METER_BUCKETS;
2705 * Process all bins of bw_meter entries from the one after the last
2706 * processed to the current one. On entry, i points to the last bucket
2707 * visited, so we need to increment i at the beginning of the loop.
2709 for (i = (now.tv_sec - loops) % BW_METER_BUCKETS; loops > 0; loops--) {
2710 struct bw_meter *x, *tmp_list;
2712 if (++i >= BW_METER_BUCKETS)
2715 /* Disconnect the list of bw_meter entries from the bin */
2716 tmp_list = bw_meter_timers[i];
2717 bw_meter_timers[i] = NULL;
2719 /* Process the list of bw_meter entries */
2720 while (tmp_list != NULL) {
2722 tmp_list = tmp_list->bm_time_next;
2724 /* Test if the time interval is over */
2725 process_endtime = x->bm_start_time;
2726 BW_TIMEVALADD(&process_endtime, &x->bm_threshold.b_time);
2727 if (BW_TIMEVALCMP(&process_endtime, &now, >)) {
2728 /* Not yet: reschedule, but don't reset */
2731 BW_METER_TIMEHASH(x, time_hash);
2732 if (time_hash == i && process_endtime.tv_sec == now.tv_sec) {
2734 * XXX: somehow the bin processing is a bit ahead of time.
2735 * Put the entry in the next bin.
2737 if (++time_hash >= BW_METER_BUCKETS)
2740 x->bm_time_next = bw_meter_timers[time_hash];
2741 bw_meter_timers[time_hash] = x;
2742 x->bm_time_hash = time_hash;
2748 * Test if we should deliver an upcall
2750 if (((x->bm_flags & BW_METER_UNIT_PACKETS) &&
2751 (x->bm_measured.b_packets <= x->bm_threshold.b_packets)) ||
2752 ((x->bm_flags & BW_METER_UNIT_BYTES) &&
2753 (x->bm_measured.b_bytes <= x->bm_threshold.b_bytes))) {
2754 /* Prepare an upcall for delivery */
2755 bw_meter_prepare_upcall(x, &now);
2759 * Reschedule for next processing
2761 schedule_bw_meter(x, &now);
2764 /* Send all upcalls that are pending delivery */
2766 lwkt_reltoken(&mroute_token);
2770 * A periodic function for sending all upcalls that are pending delivery
2773 expire_bw_upcalls_send(void *unused)
2777 callout_reset(&bw_upcalls_ch, BW_UPCALLS_PERIOD,
2778 expire_bw_upcalls_send, NULL);
2782 * A periodic function for periodic scanning of the multicast forwarding
2783 * table for processing all "<=" bw_meter entries.
2786 expire_bw_meter_process(void *unused)
2788 if (mrt_api_config & MRT_MFC_BW_UPCALL)
2791 callout_reset(&bw_meter_ch, BW_METER_PERIOD,
2792 expire_bw_meter_process, NULL);
2796 * End of bandwidth monitoring code
2801 * Send the packet up to the user daemon, or eventually do kernel encapsulation
2805 pim_register_send(struct ip *ip, struct vif *vifp,
2806 struct mbuf *m, struct mfc *rt)
2808 struct mbuf *mb_copy, *mm;
2810 if (mrtdebug & DEBUG_PIM)
2811 log(LOG_DEBUG, "pim_register_send: ");
2813 mb_copy = pim_register_prepare(ip, m);
2814 if (mb_copy == NULL)
2818 * Send all the fragments. Note that the mbuf for each fragment
2819 * is freed by the sending machinery.
2821 for (mm = mb_copy; mm; mm = mb_copy) {
2822 mb_copy = mm->m_nextpkt;
2824 mm = m_pullup(mm, sizeof(struct ip));
2826 ip = mtod(mm, struct ip *);
2827 if ((mrt_api_config & MRT_MFC_RP) &&
2828 (rt->mfc_rp.s_addr != INADDR_ANY)) {
2829 pim_register_send_rp(ip, vifp, mm, rt);
2831 pim_register_send_upcall(ip, vifp, mm, rt);
2840 * Return a copy of the data packet that is ready for PIM Register
2842 * XXX: Note that in the returned copy the IP header is a valid one.
2844 static struct mbuf *
2845 pim_register_prepare(struct ip *ip, struct mbuf *m)
2847 struct mbuf *mb_copy = NULL;
2850 /* Take care of delayed checksums */
2851 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
2852 in_delayed_cksum(m);
2853 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
2857 * Copy the old packet & pullup its IP header into the
2858 * new mbuf so we can modify it.
2860 mb_copy = m_copypacket(m, MB_DONTWAIT);
2861 if (mb_copy == NULL)
2863 mb_copy = m_pullup(mb_copy, ip->ip_hl << 2);
2864 if (mb_copy == NULL)
2867 /* take care of the TTL */
2868 ip = mtod(mb_copy, struct ip *);
2871 /* Compute the MTU after the PIM Register encapsulation */
2872 mtu = 0xffff - sizeof(pim_encap_iphdr) - sizeof(pim_encap_pimhdr);
2874 if (ip->ip_len <= mtu) {
2875 /* Turn the IP header into a valid one */
2876 ip->ip_len = htons(ip->ip_len);
2877 ip->ip_off = htons(ip->ip_off);
2879 ip->ip_sum = in_cksum(mb_copy, ip->ip_hl << 2);
2881 /* Fragment the packet */
2882 if (ip_fragment(ip, &mb_copy, mtu, 0, CSUM_DELAY_IP) != 0) {
2891 * Send an upcall with the data packet to the user-level process.
2894 pim_register_send_upcall(struct ip *ip, struct vif *vifp,
2895 struct mbuf *mb_copy, struct mfc *rt)
2897 struct mbuf *mb_first;
2898 int len = ntohs(ip->ip_len);
2900 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
2903 * Add a new mbuf with an upcall header
2905 MGETHDR(mb_first, MB_DONTWAIT, MT_HEADER);
2906 if (mb_first == NULL) {
2910 mb_first->m_data += max_linkhdr;
2911 mb_first->m_pkthdr.len = len + sizeof(struct igmpmsg);
2912 mb_first->m_len = sizeof(struct igmpmsg);
2913 mb_first->m_next = mb_copy;
2915 /* Send message to routing daemon */
2916 im = mtod(mb_first, struct igmpmsg *);
2917 im->im_msgtype = IGMPMSG_WHOLEPKT;
2919 im->im_vif = vifp - viftable;
2920 im->im_src = ip->ip_src;
2921 im->im_dst = ip->ip_dst;
2923 k_igmpsrc.sin_addr = ip->ip_src;
2925 mrtstat.mrts_upcalls++;
2927 if (socket_send(ip_mrouter, mb_first, &k_igmpsrc) < 0) {
2928 if (mrtdebug & DEBUG_PIM)
2930 "mcast: pim_register_send_upcall: ip_mrouter socket queue full");
2931 ++mrtstat.mrts_upq_sockfull;
2935 /* Keep statistics */
2936 pimstat.pims_snd_registers_msgs++;
2937 pimstat.pims_snd_registers_bytes += len;
2943 * Encapsulate the data packet in PIM Register message and send it to the RP.
2946 pim_register_send_rp(struct ip *ip, struct vif *vifp,
2947 struct mbuf *mb_copy, struct mfc *rt)
2949 struct mbuf *mb_first;
2950 struct ip *ip_outer;
2951 struct pim_encap_pimhdr *pimhdr;
2952 int len = ntohs(ip->ip_len);
2953 vifi_t vifi = rt->mfc_parent;
2955 if ((vifi >= numvifs) || (viftable[vifi].v_lcl_addr.s_addr == 0)) {
2957 return EADDRNOTAVAIL; /* The iif vif is invalid */
2961 * Add a new mbuf with the encapsulating header
2963 MGETHDR(mb_first, MB_DONTWAIT, MT_HEADER);
2964 if (mb_first == NULL) {
2968 mb_first->m_data += max_linkhdr;
2969 mb_first->m_len = sizeof(pim_encap_iphdr) + sizeof(pim_encap_pimhdr);
2970 mb_first->m_next = mb_copy;
2972 mb_first->m_pkthdr.len = len + mb_first->m_len;
2975 * Fill in the encapsulating IP and PIM header
2977 ip_outer = mtod(mb_first, struct ip *);
2978 *ip_outer = pim_encap_iphdr;
2979 ip_outer->ip_id = ip_newid();
2980 ip_outer->ip_len = len + sizeof(pim_encap_iphdr) + sizeof(pim_encap_pimhdr);
2981 ip_outer->ip_src = viftable[vifi].v_lcl_addr;
2982 ip_outer->ip_dst = rt->mfc_rp;
2984 * Copy the inner header TOS to the outer header, and take care of the
2987 ip_outer->ip_tos = ip->ip_tos;
2988 if (ntohs(ip->ip_off) & IP_DF)
2989 ip_outer->ip_off |= IP_DF;
2990 pimhdr = (struct pim_encap_pimhdr *)((caddr_t)ip_outer
2991 + sizeof(pim_encap_iphdr));
2992 *pimhdr = pim_encap_pimhdr;
2993 /* If the iif crosses a border, set the Border-bit */
2994 if (rt->mfc_flags[vifi] & MRT_MFC_FLAGS_BORDER_VIF & mrt_api_config)
2995 pimhdr->flags |= htonl(PIM_BORDER_REGISTER);
2997 mb_first->m_data += sizeof(pim_encap_iphdr);
2998 pimhdr->pim.pim_cksum = in_cksum(mb_first, sizeof(pim_encap_pimhdr));
2999 mb_first->m_data -= sizeof(pim_encap_iphdr);
3001 if (vifp->v_rate_limit == 0)
3002 tbf_send_packet(vifp, mb_first);
3004 tbf_control(vifp, mb_first, ip, ip_outer->ip_len);
3006 /* Keep statistics */
3007 pimstat.pims_snd_registers_msgs++;
3008 pimstat.pims_snd_registers_bytes += len;
3014 * PIM-SMv2 and PIM-DM messages processing.
3015 * Receives and verifies the PIM control messages, and passes them
3016 * up to the listening socket, using rip_input().
3017 * The only message with special processing is the PIM_REGISTER message
3018 * (used by PIM-SM): the PIM header is stripped off, and the inner packet
3019 * is passed to if_simloop().
3022 pim_input(struct mbuf **mp, int *offp, int proto)
3024 struct mbuf *m = *mp;
3025 struct ip *ip = mtod(m, struct ip *);
3028 int datalen = ip->ip_len;
3035 /* Keep statistics */
3036 pimstat.pims_rcv_total_msgs++;
3037 pimstat.pims_rcv_total_bytes += datalen;
3042 if (datalen < PIM_MINLEN) {
3043 pimstat.pims_rcv_tooshort++;
3044 log(LOG_ERR, "pim_input: packet size too small %d from %lx\n",
3045 datalen, (u_long)ip->ip_src.s_addr);
3047 return(IPPROTO_DONE);
3051 * If the packet is at least as big as a REGISTER, go agead
3052 * and grab the PIM REGISTER header size, to avoid another
3053 * possible m_pullup() later.
3055 * PIM_MINLEN == pimhdr + u_int32_t == 4 + 4 = 8
3056 * PIM_REG_MINLEN == pimhdr + reghdr + encap_iphdr == 4 + 4 + 20 = 28
3058 minlen = iphlen + (datalen >= PIM_REG_MINLEN ? PIM_REG_MINLEN : PIM_MINLEN);
3060 * Get the IP and PIM headers in contiguous memory, and
3061 * possibly the PIM REGISTER header.
3063 if ((m->m_flags & M_EXT || m->m_len < minlen) &&
3064 (m = m_pullup(m, minlen)) == NULL) {
3065 log(LOG_ERR, "pim_input: m_pullup failure\n");
3066 return(IPPROTO_DONE);
3068 /* m_pullup() may have given us a new mbuf so reset ip. */
3069 ip = mtod(m, struct ip *);
3070 ip_tos = ip->ip_tos;
3072 /* adjust mbuf to point to the PIM header */
3073 m->m_data += iphlen;
3075 pim = mtod(m, struct pim *);
3078 * Validate checksum. If PIM REGISTER, exclude the data packet.
3080 * XXX: some older PIMv2 implementations don't make this distinction,
3081 * so for compatibility reason perform the checksum over part of the
3082 * message, and if error, then over the whole message.
3084 if (PIM_VT_T(pim->pim_vt) == PIM_REGISTER && in_cksum(m, PIM_MINLEN) == 0) {
3085 /* do nothing, checksum okay */
3086 } else if (in_cksum(m, datalen)) {
3087 pimstat.pims_rcv_badsum++;
3088 if (mrtdebug & DEBUG_PIM)
3089 log(LOG_DEBUG, "pim_input: invalid checksum");
3091 return(IPPROTO_DONE);
3094 /* PIM version check */
3095 if (PIM_VT_V(pim->pim_vt) < PIM_VERSION) {
3096 pimstat.pims_rcv_badversion++;
3097 log(LOG_ERR, "pim_input: incorrect version %d, expecting %d\n",
3098 PIM_VT_V(pim->pim_vt), PIM_VERSION);
3100 return(IPPROTO_DONE);
3103 /* restore mbuf back to the outer IP */
3104 m->m_data -= iphlen;
3107 if (PIM_VT_T(pim->pim_vt) == PIM_REGISTER) {
3109 * Since this is a REGISTER, we'll make a copy of the register
3110 * headers ip + pim + u_int32 + encap_ip, to be passed up to the
3113 struct sockaddr_in dst = { sizeof(dst), AF_INET };
3115 struct ip *encap_ip;
3118 if ((reg_vif_num >= numvifs) || (reg_vif_num == VIFI_INVALID)) {
3119 if (mrtdebug & DEBUG_PIM)
3121 "pim_input: register vif not set: %d\n", reg_vif_num);
3123 return(IPPROTO_DONE);
3129 if (datalen < PIM_REG_MINLEN) {
3130 pimstat.pims_rcv_tooshort++;
3131 pimstat.pims_rcv_badregisters++;
3133 "pim_input: register packet size too small %d from %lx\n",
3134 datalen, (u_long)ip->ip_src.s_addr);
3136 return(IPPROTO_DONE);
3139 reghdr = (u_int32_t *)(pim + 1);
3140 encap_ip = (struct ip *)(reghdr + 1);
3142 if (mrtdebug & DEBUG_PIM) {
3144 "pim_input[register], encap_ip: %lx -> %lx, encap_ip len %d\n",
3145 (u_long)ntohl(encap_ip->ip_src.s_addr),
3146 (u_long)ntohl(encap_ip->ip_dst.s_addr),
3147 ntohs(encap_ip->ip_len));
3150 /* verify the version number of the inner packet */
3151 if (encap_ip->ip_v != IPVERSION) {
3152 pimstat.pims_rcv_badregisters++;
3153 if (mrtdebug & DEBUG_PIM) {
3154 log(LOG_DEBUG, "pim_input: invalid IP version (%d) "
3155 "of the inner packet\n", encap_ip->ip_v);
3158 return(IPPROTO_DONE);
3161 /* verify the inner packet is destined to a mcast group */
3162 if (!IN_MULTICAST(ntohl(encap_ip->ip_dst.s_addr))) {
3163 pimstat.pims_rcv_badregisters++;
3164 if (mrtdebug & DEBUG_PIM)
3166 "pim_input: inner packet of register is not "
3168 (u_long)ntohl(encap_ip->ip_dst.s_addr));
3170 return(IPPROTO_DONE);
3173 /* If a NULL_REGISTER, pass it to the daemon */
3174 if ((ntohl(*reghdr) & PIM_NULL_REGISTER))
3175 goto pim_input_to_daemon;
3178 * Copy the TOS from the outer IP header to the inner IP header.
3180 if (encap_ip->ip_tos != ip_tos) {
3181 /* Outer TOS -> inner TOS */
3182 encap_ip->ip_tos = ip_tos;
3183 /* Recompute the inner header checksum. Sigh... */
3185 /* adjust mbuf to point to the inner IP header */
3186 m->m_data += (iphlen + PIM_MINLEN);
3187 m->m_len -= (iphlen + PIM_MINLEN);
3189 encap_ip->ip_sum = 0;
3190 encap_ip->ip_sum = in_cksum(m, encap_ip->ip_hl << 2);
3192 /* restore mbuf to point back to the outer IP header */
3193 m->m_data -= (iphlen + PIM_MINLEN);
3194 m->m_len += (iphlen + PIM_MINLEN);
3198 * Decapsulate the inner IP packet and loopback to forward it
3199 * as a normal multicast packet. Also, make a copy of the
3200 * outer_iphdr + pimhdr + reghdr + encap_iphdr
3201 * to pass to the daemon later, so it can take the appropriate
3202 * actions (e.g., send back PIM_REGISTER_STOP).
3203 * XXX: here m->m_data points to the outer IP header.
3205 mcp = m_copy(m, 0, iphlen + PIM_REG_MINLEN);
3208 "pim_input: pim register: could not copy register head\n");
3210 return(IPPROTO_DONE);
3213 /* Keep statistics */
3214 /* XXX: registers_bytes include only the encap. mcast pkt */
3215 pimstat.pims_rcv_registers_msgs++;
3216 pimstat.pims_rcv_registers_bytes += ntohs(encap_ip->ip_len);
3219 * forward the inner ip packet; point m_data at the inner ip.
3221 m_adj(m, iphlen + PIM_MINLEN);
3223 if (mrtdebug & DEBUG_PIM) {
3225 "pim_input: forwarding decapsulated register: "
3226 "src %lx, dst %lx, vif %d\n",
3227 (u_long)ntohl(encap_ip->ip_src.s_addr),
3228 (u_long)ntohl(encap_ip->ip_dst.s_addr),
3231 if_simloop(viftable[reg_vif_num].v_ifp, m, dst.sin_family, 0);
3233 /* prepare the register head to send to the mrouting daemon */
3237 pim_input_to_daemon:
3239 * Pass the PIM message up to the daemon; if it is a Register message,
3240 * pass the 'head' only up to the daemon. This includes the
3241 * outer IP header, PIM header, PIM-Register header and the
3243 * XXX: the outer IP header pkt size of a Register is not adjust to
3244 * reflect the fact that the inner multicast data is truncated.
3248 rip_input(mp, offp, proto);
3249 return(IPPROTO_DONE);
3254 ip_mroute_modevent(module_t mod, int type, void *unused)
3258 lwkt_gettoken(&mroute_token);
3259 /* XXX Protect against multiple loading */
3260 ip_mcast_src = X_ip_mcast_src;
3261 ip_mforward = X_ip_mforward;
3262 ip_mrouter_done = X_ip_mrouter_done;
3263 ip_mrouter_get = X_ip_mrouter_get;
3264 ip_mrouter_set = X_ip_mrouter_set;
3265 ip_rsvp_force_done = X_ip_rsvp_force_done;
3266 ip_rsvp_vif = X_ip_rsvp_vif;
3267 ipip_input = X_ipip_input;
3268 legal_vif_num = X_legal_vif_num;
3269 mrt_ioctl = X_mrt_ioctl;
3270 rsvp_input_p = X_rsvp_input;
3271 lwkt_reltoken(&mroute_token);
3278 lwkt_gettoken(&mroute_token);
3279 ip_mcast_src = NULL;
3281 ip_mrouter_done = NULL;
3282 ip_mrouter_get = NULL;
3283 ip_mrouter_set = NULL;
3284 ip_rsvp_force_done = NULL;
3287 legal_vif_num = NULL;
3289 rsvp_input_p = NULL;
3290 lwkt_reltoken(&mroute_token);
3296 static moduledata_t ip_mroutemod = {
3301 DECLARE_MODULE(ip_mroute, ip_mroutemod, SI_SUB_PSEUDO, SI_ORDER_ANY);