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 $
21 * $DragonFly: src/sys/net/ip_mroute/ip_mroute.c,v 1.23 2008/06/17 20:50:11 aggelos Exp $
24 #include "opt_mrouting.h"
30 #include <sys/param.h>
31 #include <sys/kernel.h>
32 #include <sys/malloc.h>
34 #include <sys/protosw.h>
35 #include <sys/socket.h>
36 #include <sys/socketvar.h>
37 #include <sys/sockio.h>
38 #include <sys/sysctl.h>
39 #include <sys/syslog.h>
40 #include <sys/systm.h>
41 #include <sys/thread2.h>
43 #include <sys/in_cksum.h>
45 #include <machine/stdarg.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 "ip_mroute.h"
56 #include <netinet/ip_var.h>
58 #include <netinet/pim.h>
59 #include <netinet/pim_var.h>
62 #include <netinet/in_pcb.h>
64 #include <netinet/udp.h>
67 * Control debugging code for rsvp and multicast routing code.
68 * Can only set them with the debugger.
70 static u_int rsvpdebug; /* non-zero enables debugging */
72 static u_int mrtdebug; /* any set of the flags below */
74 #define DEBUG_MFC 0x02
75 #define DEBUG_FORWARD 0x04
76 #define DEBUG_EXPIRE 0x08
77 #define DEBUG_XMIT 0x10
78 #define DEBUG_PIM 0x20
80 #define VIFI_INVALID ((vifi_t) -1)
82 #define M_HASCL(m) ((m)->m_flags & M_EXT)
84 static MALLOC_DEFINE(M_MRTABLE, "mroutetbl", "multicast routing tables");
86 static struct mrtstat mrtstat;
87 SYSCTL_STRUCT(_net_inet_ip, OID_AUTO, mrtstat, CTLFLAG_RW,
89 "Multicast Routing Statistics (struct mrtstat, netinet/ip_mroute.h)");
91 static struct mfc *mfctable[MFCTBLSIZ];
92 SYSCTL_OPAQUE(_net_inet_ip, OID_AUTO, mfctable, CTLFLAG_RD,
93 &mfctable, sizeof(mfctable), "S,*mfc[MFCTBLSIZ]",
94 "Multicast Forwarding Table (struct *mfc[MFCTBLSIZ], netinet/ip_mroute.h)");
96 static struct vif viftable[MAXVIFS];
97 SYSCTL_OPAQUE(_net_inet_ip, OID_AUTO, viftable, CTLFLAG_RD,
98 &viftable, sizeof(viftable), "S,vif[MAXVIFS]",
99 "Multicast Virtual Interfaces (struct vif[MAXVIFS], netinet/ip_mroute.h)");
101 static u_char nexpire[MFCTBLSIZ];
103 struct lwkt_token mroute_token = LWKT_TOKEN_INITIALIZER(mroute_token);
106 static struct callout expire_upcalls_ch;
107 static struct callout tbf_reprocess_q_ch;
108 #define EXPIRE_TIMEOUT (hz / 4) /* 4x / second */
109 #define UPCALL_EXPIRE 6 /* number of timeouts */
112 * Define the token bucket filter structures
113 * tbftable -> each vif has one of these for storing info
116 static struct tbf tbftable[MAXVIFS];
117 #define TBF_REPROCESS (hz / 100) /* 100x / second */
120 * 'Interfaces' associated with decapsulator (so we can tell
121 * packets that went through it from ones that get reflected
122 * by a broken gateway). These interfaces are never linked into
123 * the system ifnet list & no routes point to them. I.e., packets
124 * can't be sent this way. They only exist as a placeholder for
125 * multicast source verification.
127 static struct ifnet multicast_decap_if[MAXVIFS];
130 #define ENCAP_PROTO IPPROTO_IPIP /* 4 */
132 /* prototype IP hdr for encapsulated packets */
133 static struct ip multicast_encap_iphdr = {
134 #if BYTE_ORDER == LITTLE_ENDIAN
135 sizeof(struct ip) >> 2, IPVERSION,
137 IPVERSION, sizeof(struct ip) >> 2,
140 sizeof(struct ip), /* total length */
143 ENCAP_TTL, ENCAP_PROTO,
148 * Bandwidth meter variables and constants
150 static MALLOC_DEFINE(M_BWMETER, "bwmeter", "multicast upcall bw meters");
152 * Pending timeouts are stored in a hash table, the key being the
153 * expiration time. Periodically, the entries are analysed and processed.
155 #define BW_METER_BUCKETS 1024
156 static struct bw_meter *bw_meter_timers[BW_METER_BUCKETS];
157 static struct callout bw_meter_ch;
158 #define BW_METER_PERIOD (hz) /* periodical handling of bw meters */
161 * Pending upcalls are stored in a vector which is flushed when
162 * full, or periodically
164 static struct bw_upcall bw_upcalls[BW_UPCALLS_MAX];
165 static u_int bw_upcalls_n; /* # of pending upcalls */
166 static struct callout bw_upcalls_ch;
167 #define BW_UPCALLS_PERIOD (hz) /* periodical flush of bw upcalls */
170 static struct pimstat pimstat;
171 SYSCTL_STRUCT(_net_inet_pim, PIMCTL_STATS, stats, CTLFLAG_RD,
173 "PIM Statistics (struct pimstat, netinet/pim_var.h)");
176 * Note: the PIM Register encapsulation adds the following in front of a
179 * struct pim_encap_hdr {
181 * struct pim_encap_pimhdr pim;
186 struct pim_encap_pimhdr {
191 static struct ip pim_encap_iphdr = {
192 #if BYTE_ORDER == LITTLE_ENDIAN
193 sizeof(struct ip) >> 2,
197 sizeof(struct ip) >> 2,
200 sizeof(struct ip), /* total length */
208 static struct pim_encap_pimhdr pim_encap_pimhdr = {
210 PIM_MAKE_VT(PIM_VERSION, PIM_REGISTER), /* PIM vers and message type */
217 static struct ifnet multicast_register_if;
218 static vifi_t reg_vif_num = VIFI_INVALID;
224 static vifi_t numvifs;
225 static int have_encap_tunnel;
228 * one-back cache used by ipip_input to locate a tunnel's vif
229 * given a datagram's src ip address.
231 static u_long last_encap_src;
232 static struct vif *last_encap_vif;
234 static u_long X_ip_mcast_src(int vifi);
235 static int X_ip_mforward(struct ip *ip, struct ifnet *ifp,
236 struct mbuf *m, struct ip_moptions *imo);
237 static int X_ip_mrouter_done(void);
238 static int X_ip_mrouter_get(struct socket *so, struct sockopt *m);
239 static int X_ip_mrouter_set(struct socket *so, struct sockopt *m);
240 static int X_legal_vif_num(int vif);
241 static int X_mrt_ioctl(int cmd, caddr_t data);
243 static int get_sg_cnt(struct sioc_sg_req *);
244 static int get_vif_cnt(struct sioc_vif_req *);
245 static int ip_mrouter_init(struct socket *, int);
246 static int add_vif(struct vifctl *);
247 static int del_vif(vifi_t);
248 static int add_mfc(struct mfcctl2 *);
249 static int del_mfc(struct mfcctl2 *);
250 static int set_api_config(uint32_t *); /* chose API capabilities */
251 static int socket_send(struct socket *, struct mbuf *, struct sockaddr_in *);
252 static int set_assert(int);
253 static void expire_upcalls(void *);
254 static int ip_mdq(struct mbuf *, struct ifnet *, struct mfc *, vifi_t);
255 static void phyint_send(struct ip *, struct vif *, struct mbuf *);
256 static void encap_send(struct ip *, struct vif *, struct mbuf *);
257 static void tbf_control(struct vif *, struct mbuf *, struct ip *, u_long);
258 static void tbf_queue(struct vif *, struct mbuf *);
259 static void tbf_process_q(struct vif *);
260 static void tbf_reprocess_q(void *);
261 static int tbf_dq_sel(struct vif *, struct ip *);
262 static void tbf_send_packet(struct vif *, struct mbuf *);
263 static void tbf_update_tokens(struct vif *);
264 static int priority(struct vif *, struct ip *);
267 * Bandwidth monitoring
269 static void free_bw_list(struct bw_meter *list);
270 static int add_bw_upcall(struct bw_upcall *);
271 static int del_bw_upcall(struct bw_upcall *);
272 static void bw_meter_receive_packet(struct bw_meter *x, int plen,
273 struct timeval *nowp);
274 static void bw_meter_prepare_upcall(struct bw_meter *x, struct timeval *nowp);
275 static void bw_upcalls_send(void);
276 static void schedule_bw_meter(struct bw_meter *x, struct timeval *nowp);
277 static void unschedule_bw_meter(struct bw_meter *x);
278 static void bw_meter_process(void);
279 static void expire_bw_upcalls_send(void *);
280 static void expire_bw_meter_process(void *);
283 static int pim_register_send(struct ip *, struct vif *,
284 struct mbuf *, struct mfc *);
285 static int pim_register_send_rp(struct ip *, struct vif *,
286 struct mbuf *, struct mfc *);
287 static int pim_register_send_upcall(struct ip *, struct vif *,
288 struct mbuf *, struct mfc *);
289 static struct mbuf *pim_register_prepare(struct ip *, struct mbuf *);
293 * whether or not special PIM assert processing is enabled.
295 static int pim_assert;
297 * Rate limit for assert notification messages, in usec
299 #define ASSERT_MSG_TIME 3000000
302 * Kernel multicast routing API capabilities and setup.
303 * If more API capabilities are added to the kernel, they should be
304 * recorded in `mrt_api_support'.
306 static const uint32_t mrt_api_support = (MRT_MFC_FLAGS_DISABLE_WRONGVIF |
307 MRT_MFC_FLAGS_BORDER_VIF |
310 static uint32_t mrt_api_config = 0;
313 * Hash function for a source, group entry
315 #define MFCHASH(a, g) MFCHASHMOD(((a) >> 20) ^ ((a) >> 10) ^ (a) ^ \
316 ((g) >> 20) ^ ((g) >> 10) ^ (g))
319 * Find a route for a given origin IP address and Multicast group address
320 * Type of service parameter to be added in the future!!!
321 * Statistics are updated by the caller if needed
322 * (mrtstat.mrts_mfc_lookups and mrtstat.mrts_mfc_misses)
325 mfc_find(in_addr_t o, in_addr_t g)
329 for (rt = mfctable[MFCHASH(o,g)]; rt; rt = rt->mfc_next)
330 if ((rt->mfc_origin.s_addr == o) &&
331 (rt->mfc_mcastgrp.s_addr == g) && (rt->mfc_stall == NULL))
337 * Macros to compute elapsed time efficiently
338 * Borrowed from Van Jacobson's scheduling code
340 #define TV_DELTA(a, b, delta) { \
342 delta = (a).tv_usec - (b).tv_usec; \
343 if ((xxs = (a).tv_sec - (b).tv_sec)) { \
352 delta += (1000000 * xxs); \
357 #define TV_LT(a, b) (((a).tv_usec < (b).tv_usec && \
358 (a).tv_sec <= (b).tv_sec) || (a).tv_sec < (b).tv_sec)
361 * Handle MRT setsockopt commands to modify the multicast routing tables.
364 X_ip_mrouter_set(struct socket *so, struct sockopt *sopt)
370 struct bw_upcall bw_upcall;
373 if (so != ip_mrouter && sopt->sopt_name != MRT_INIT)
377 switch (sopt->sopt_name) {
379 error = soopt_to_kbuf(sopt, &optval, sizeof optval, sizeof optval);
382 error = ip_mrouter_init(so, optval);
386 error = ip_mrouter_done();
390 error = soopt_to_kbuf(sopt, &vifc, sizeof vifc, sizeof vifc);
393 error = add_vif(&vifc);
397 error = soopt_to_kbuf(sopt, &vifi, sizeof vifi, sizeof vifi);
400 error = del_vif(vifi);
406 * select data size depending on API version.
408 if (sopt->sopt_name == MRT_ADD_MFC &&
409 mrt_api_config & MRT_API_FLAGS_ALL) {
410 error = soopt_to_kbuf(sopt, &mfc, sizeof(struct mfcctl2),
411 sizeof(struct mfcctl2));
413 error = soopt_to_kbuf(sopt, &mfc, sizeof(struct mfcctl),
414 sizeof(struct mfcctl));
415 bzero((caddr_t)&mfc + sizeof(struct mfcctl),
416 sizeof(mfc) - sizeof(struct mfcctl));
420 if (sopt->sopt_name == MRT_ADD_MFC)
421 error = add_mfc(&mfc);
423 error = del_mfc(&mfc);
427 error = soopt_to_kbuf(sopt, &optval, sizeof optval, sizeof optval);
434 error = soopt_to_kbuf(sopt, &i, sizeof i, sizeof i);
436 error = set_api_config(&i);
438 soopt_from_kbuf(sopt, &i, sizeof i);
441 case MRT_ADD_BW_UPCALL:
442 case MRT_DEL_BW_UPCALL:
443 error = soopt_to_kbuf(sopt, &bw_upcall, sizeof bw_upcall, sizeof bw_upcall);
446 if (sopt->sopt_name == MRT_ADD_BW_UPCALL)
447 error = add_bw_upcall(&bw_upcall);
449 error = del_bw_upcall(&bw_upcall);
460 * Handle MRT getsockopt commands
463 X_ip_mrouter_get(struct socket *so, struct sockopt *sopt)
466 static int version = 0x0305; /* !!! why is this here? XXX */
469 switch (sopt->sopt_name) {
471 soopt_from_kbuf(sopt, &version, sizeof version);
475 soopt_from_kbuf(sopt, &pim_assert, sizeof pim_assert);
478 case MRT_API_SUPPORT:
479 soopt_from_kbuf(sopt, &mrt_api_support, sizeof mrt_api_support);
483 soopt_from_kbuf(sopt, &mrt_api_config, sizeof mrt_api_config);
494 * Handle ioctl commands to obtain information from the cache
497 X_mrt_ioctl(int cmd, caddr_t data)
503 error = get_vif_cnt((struct sioc_vif_req *)data);
507 error = get_sg_cnt((struct sioc_sg_req *)data);
518 * returns the packet, byte, rpf-failure count for the source group provided
521 get_sg_cnt(struct sioc_sg_req *req)
525 lwkt_gettoken(&mroute_token);
526 rt = mfc_find(req->src.s_addr, req->grp.s_addr);
528 req->pktcnt = req->bytecnt = req->wrong_if = 0xffffffff;
529 lwkt_reltoken(&mroute_token);
530 return EADDRNOTAVAIL;
532 req->pktcnt = rt->mfc_pkt_cnt;
533 req->bytecnt = rt->mfc_byte_cnt;
534 req->wrong_if = rt->mfc_wrong_if;
535 lwkt_reltoken(&mroute_token);
540 * returns the input and output packet and byte counts on the vif provided
543 get_vif_cnt(struct sioc_vif_req *req)
545 vifi_t vifi = req->vifi;
550 req->icount = viftable[vifi].v_pkt_in;
551 req->ocount = viftable[vifi].v_pkt_out;
552 req->ibytes = viftable[vifi].v_bytes_in;
553 req->obytes = viftable[vifi].v_bytes_out;
559 * Enable multicast routing
562 ip_mrouter_init(struct socket *so, int version)
565 log(LOG_DEBUG, "ip_mrouter_init: so_type = %d, pr_protocol = %d\n",
566 so->so_type, so->so_proto->pr_protocol);
568 if (so->so_type != SOCK_RAW || so->so_proto->pr_protocol != IPPROTO_IGMP)
574 if (ip_mrouter != NULL)
579 bzero((caddr_t)mfctable, sizeof(mfctable));
580 bzero((caddr_t)nexpire, sizeof(nexpire));
584 bzero((caddr_t)bw_meter_timers, sizeof(bw_meter_timers));
586 callout_init(&expire_upcalls_ch);
587 callout_init(&bw_upcalls_ch);
588 callout_init(&bw_meter_ch);
589 callout_init(&tbf_reprocess_q_ch);
591 callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT, expire_upcalls, NULL);
592 callout_reset(&bw_upcalls_ch, BW_UPCALLS_PERIOD,
593 expire_bw_upcalls_send, NULL);
594 callout_reset(&bw_meter_ch, BW_METER_PERIOD, expire_bw_meter_process, NULL);
599 log(LOG_DEBUG, "ip_mrouter_init\n");
605 * Disable multicast routing
608 X_ip_mrouter_done(void)
617 lwkt_gettoken(&mroute_token);
620 * For each phyint in use, disable promiscuous reception of all IP
623 for (vifi = 0; vifi < numvifs; vifi++) {
624 if (viftable[vifi].v_lcl_addr.s_addr != 0 &&
625 !(viftable[vifi].v_flags & (VIFF_TUNNEL | VIFF_REGISTER))) {
626 struct sockaddr_in *so = (struct sockaddr_in *)&(ifr.ifr_addr);
628 so->sin_len = sizeof(struct sockaddr_in);
629 so->sin_family = AF_INET;
630 so->sin_addr.s_addr = INADDR_ANY;
631 ifp = viftable[vifi].v_ifp;
635 bzero((caddr_t)tbftable, sizeof(tbftable));
636 bzero((caddr_t)viftable, sizeof(viftable));
640 callout_stop(&expire_upcalls_ch);
644 callout_stop(&bw_upcalls_ch);
645 callout_stop(&bw_meter_ch);
646 callout_stop(&tbf_reprocess_q_ch);
649 * Free all multicast forwarding cache entries.
651 for (i = 0; i < MFCTBLSIZ; i++) {
652 for (rt = mfctable[i]; rt != NULL; ) {
653 struct mfc *nr = rt->mfc_next;
655 for (rte = rt->mfc_stall; rte != NULL; ) {
656 struct rtdetq *n = rte->next;
659 kfree(rte, M_MRTABLE);
662 free_bw_list(rt->mfc_bw_meter);
663 kfree(rt, M_MRTABLE);
668 bzero((caddr_t)mfctable, sizeof(mfctable));
670 bzero(bw_meter_timers, sizeof(bw_meter_timers));
673 * Reset de-encapsulation cache
675 last_encap_src = INADDR_ANY;
676 last_encap_vif = NULL;
678 reg_vif_num = VIFI_INVALID;
680 have_encap_tunnel = 0;
684 lwkt_reltoken(&mroute_token);
687 log(LOG_DEBUG, "ip_mrouter_done\n");
693 * Set PIM assert processing global
698 if ((i != 1) && (i != 0))
707 * Configure API capabilities
710 set_api_config(uint32_t *apival)
715 * We can set the API capabilities only if it is the first operation
716 * after MRT_INIT. I.e.:
717 * - there are no vifs installed
718 * - pim_assert is not enabled
719 * - the MFC table is empty
729 for (i = 0; i < MFCTBLSIZ; i++) {
730 if (mfctable[i] != NULL) {
736 mrt_api_config = *apival & mrt_api_support;
737 *apival = mrt_api_config;
743 * Add a vif to the vif table
746 add_vif(struct vifctl *vifcp)
748 struct vif *vifp = viftable + vifcp->vifc_vifi;
749 struct sockaddr_in sin = {sizeof sin, AF_INET};
753 struct tbf *v_tbf = tbftable + vifcp->vifc_vifi;
755 if (vifcp->vifc_vifi >= MAXVIFS)
757 if (vifp->v_lcl_addr.s_addr != INADDR_ANY)
759 if (vifcp->vifc_lcl_addr.s_addr == INADDR_ANY)
760 return EADDRNOTAVAIL;
762 /* Find the interface with an address in AF_INET family */
764 if (vifcp->vifc_flags & VIFF_REGISTER) {
766 * XXX: Because VIFF_REGISTER does not really need a valid
767 * local interface (e.g. it could be 127.0.0.2), we don't
774 sin.sin_addr = vifcp->vifc_lcl_addr;
775 ifa = ifa_ifwithaddr((struct sockaddr *)&sin);
777 return EADDRNOTAVAIL;
781 if (vifcp->vifc_flags & VIFF_TUNNEL) {
782 if ((vifcp->vifc_flags & VIFF_SRCRT) == 0) {
784 * An encapsulating tunnel is wanted. Tell ipip_input() to
785 * start paying attention to encapsulated packets.
787 if (have_encap_tunnel == 0) {
788 have_encap_tunnel = 1;
789 for (i = 0; i < MAXVIFS; i++) {
790 if_initname(&multicast_decap_if[i], "mdecap", i);
794 * Set interface to fake encapsulator interface
796 ifp = &multicast_decap_if[vifcp->vifc_vifi];
798 * Prepare cached route entry
800 bzero(&vifp->v_route, sizeof(vifp->v_route));
802 log(LOG_ERR, "source routed tunnels not supported\n");
806 } else if (vifcp->vifc_flags & VIFF_REGISTER) {
807 ifp = &multicast_register_if;
809 log(LOG_DEBUG, "Adding a register vif, ifp: %p\n",
810 (void *)&multicast_register_if);
811 if (reg_vif_num == VIFI_INVALID) {
812 if_initname(&multicast_register_if, "register_vif", 0);
813 multicast_register_if.if_flags = IFF_LOOPBACK;
814 bzero(&vifp->v_route, sizeof(vifp->v_route));
815 reg_vif_num = vifcp->vifc_vifi;
818 } else { /* Make sure the interface supports multicast */
819 if ((ifp->if_flags & IFF_MULTICAST) == 0)
822 /* Enable promiscuous reception of all IP multicasts from the if */
823 lwkt_gettoken(&mroute_token);
824 error = if_allmulti(ifp, 1);
825 lwkt_reltoken(&mroute_token);
830 lwkt_gettoken(&mroute_token);
831 /* define parameters for the tbf structure */
833 GET_TIME(vifp->v_tbf->tbf_last_pkt_t);
834 vifp->v_tbf->tbf_n_tok = 0;
835 vifp->v_tbf->tbf_q_len = 0;
836 vifp->v_tbf->tbf_max_q_len = MAXQSIZE;
837 vifp->v_tbf->tbf_q = vifp->v_tbf->tbf_t = NULL;
839 vifp->v_flags = vifcp->vifc_flags;
840 vifp->v_threshold = vifcp->vifc_threshold;
841 vifp->v_lcl_addr = vifcp->vifc_lcl_addr;
842 vifp->v_rmt_addr = vifcp->vifc_rmt_addr;
844 /* scaling up here allows division by 1024 in critical code */
845 vifp->v_rate_limit= vifcp->vifc_rate_limit * 1024 / 1000;
847 vifp->v_rsvpd = NULL;
848 /* initialize per vif pkt counters */
851 vifp->v_bytes_in = 0;
852 vifp->v_bytes_out = 0;
854 /* Adjust numvifs up if the vifi is higher than numvifs */
855 if (numvifs <= vifcp->vifc_vifi) numvifs = vifcp->vifc_vifi + 1;
857 lwkt_reltoken(&mroute_token);
860 log(LOG_DEBUG, "add_vif #%d, lcladdr %lx, %s %lx, thresh %x, rate %d\n",
862 (u_long)ntohl(vifcp->vifc_lcl_addr.s_addr),
863 (vifcp->vifc_flags & VIFF_TUNNEL) ? "rmtaddr" : "mask",
864 (u_long)ntohl(vifcp->vifc_rmt_addr.s_addr),
865 vifcp->vifc_threshold,
866 vifcp->vifc_rate_limit);
872 * Delete a vif from the vif table
881 vifp = &viftable[vifi];
882 if (vifp->v_lcl_addr.s_addr == INADDR_ANY)
883 return EADDRNOTAVAIL;
885 lwkt_gettoken(&mroute_token);
887 if (!(vifp->v_flags & (VIFF_TUNNEL | VIFF_REGISTER)))
888 if_allmulti(vifp->v_ifp, 0);
890 if (vifp == last_encap_vif) {
891 last_encap_vif = NULL;
892 last_encap_src = INADDR_ANY;
896 * Free packets queued at the interface
898 while (vifp->v_tbf->tbf_q) {
899 struct mbuf *m = vifp->v_tbf->tbf_q;
901 vifp->v_tbf->tbf_q = m->m_nextpkt;
906 if (vifp->v_flags & VIFF_REGISTER)
907 reg_vif_num = VIFI_INVALID;
910 bzero((caddr_t)vifp->v_tbf, sizeof(*(vifp->v_tbf)));
911 bzero((caddr_t)vifp, sizeof (*vifp));
914 log(LOG_DEBUG, "del_vif %d, numvifs %d\n", vifi, numvifs);
916 /* Adjust numvifs down */
917 for (vifi = numvifs; vifi > 0; vifi--)
918 if (viftable[vifi-1].v_lcl_addr.s_addr != INADDR_ANY)
922 lwkt_reltoken(&mroute_token);
928 * update an mfc entry without resetting counters and S,G addresses.
931 update_mfc_params(struct mfc *rt, struct mfcctl2 *mfccp)
935 rt->mfc_parent = mfccp->mfcc_parent;
936 for (i = 0; i < numvifs; i++) {
937 rt->mfc_ttls[i] = mfccp->mfcc_ttls[i];
938 rt->mfc_flags[i] = mfccp->mfcc_flags[i] & mrt_api_config &
941 /* set the RP address */
942 if (mrt_api_config & MRT_MFC_RP)
943 rt->mfc_rp = mfccp->mfcc_rp;
945 rt->mfc_rp.s_addr = INADDR_ANY;
949 * fully initialize an mfc entry from the parameter.
952 init_mfc_params(struct mfc *rt, struct mfcctl2 *mfccp)
954 rt->mfc_origin = mfccp->mfcc_origin;
955 rt->mfc_mcastgrp = mfccp->mfcc_mcastgrp;
957 update_mfc_params(rt, mfccp);
959 /* initialize pkt counters per src-grp */
961 rt->mfc_byte_cnt = 0;
962 rt->mfc_wrong_if = 0;
963 rt->mfc_last_assert.tv_sec = rt->mfc_last_assert.tv_usec = 0;
971 add_mfc(struct mfcctl2 *mfccp)
978 rt = mfc_find(mfccp->mfcc_origin.s_addr, mfccp->mfcc_mcastgrp.s_addr);
980 /* If an entry already exists, just update the fields */
982 if (mrtdebug & DEBUG_MFC)
983 log(LOG_DEBUG,"add_mfc update o %lx g %lx p %x\n",
984 (u_long)ntohl(mfccp->mfcc_origin.s_addr),
985 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
988 lwkt_gettoken(&mroute_token);
989 update_mfc_params(rt, mfccp);
990 lwkt_reltoken(&mroute_token);
995 * Find the entry for which the upcall was made and update
997 lwkt_gettoken(&mroute_token);
998 hash = MFCHASH(mfccp->mfcc_origin.s_addr, mfccp->mfcc_mcastgrp.s_addr);
999 for (rt = mfctable[hash], nstl = 0; rt; rt = rt->mfc_next) {
1001 if ((rt->mfc_origin.s_addr == mfccp->mfcc_origin.s_addr) &&
1002 (rt->mfc_mcastgrp.s_addr == mfccp->mfcc_mcastgrp.s_addr) &&
1003 (rt->mfc_stall != NULL)) {
1006 log(LOG_ERR, "add_mfc %s o %lx g %lx p %x dbx %p\n",
1007 "multiple kernel entries",
1008 (u_long)ntohl(mfccp->mfcc_origin.s_addr),
1009 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
1010 mfccp->mfcc_parent, (void *)rt->mfc_stall);
1012 if (mrtdebug & DEBUG_MFC)
1013 log(LOG_DEBUG,"add_mfc o %lx g %lx p %x dbg %p\n",
1014 (u_long)ntohl(mfccp->mfcc_origin.s_addr),
1015 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
1016 mfccp->mfcc_parent, (void *)rt->mfc_stall);
1018 init_mfc_params(rt, mfccp);
1020 rt->mfc_expire = 0; /* Don't clean this guy up */
1023 /* free packets Qed at the end of this entry */
1024 for (rte = rt->mfc_stall; rte != NULL; ) {
1025 struct rtdetq *n = rte->next;
1027 ip_mdq(rte->m, rte->ifp, rt, -1);
1029 kfree(rte, M_MRTABLE);
1032 rt->mfc_stall = NULL;
1037 * It is possible that an entry is being inserted without an upcall
1040 if (mrtdebug & DEBUG_MFC)
1041 log(LOG_DEBUG,"add_mfc no upcall h %lu o %lx g %lx p %x\n",
1042 hash, (u_long)ntohl(mfccp->mfcc_origin.s_addr),
1043 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
1044 mfccp->mfcc_parent);
1046 for (rt = mfctable[hash]; rt != NULL; rt = rt->mfc_next) {
1047 if ((rt->mfc_origin.s_addr == mfccp->mfcc_origin.s_addr) &&
1048 (rt->mfc_mcastgrp.s_addr == mfccp->mfcc_mcastgrp.s_addr)) {
1049 init_mfc_params(rt, mfccp);
1056 if (rt == NULL) { /* no upcall, so make a new entry */
1057 rt = kmalloc(sizeof(*rt), M_MRTABLE, M_INTWAIT | M_NULLOK);
1059 lwkt_reltoken(&mroute_token);
1063 init_mfc_params(rt, mfccp);
1065 rt->mfc_stall = NULL;
1067 rt->mfc_bw_meter = NULL;
1068 /* insert new entry at head of hash chain */
1069 rt->mfc_next = mfctable[hash];
1070 mfctable[hash] = rt;
1073 lwkt_reltoken(&mroute_token);
1078 * Delete an mfc entry
1081 del_mfc(struct mfcctl2 *mfccp)
1083 struct in_addr origin;
1084 struct in_addr mcastgrp;
1088 struct bw_meter *list;
1090 origin = mfccp->mfcc_origin;
1091 mcastgrp = mfccp->mfcc_mcastgrp;
1093 if (mrtdebug & DEBUG_MFC)
1094 log(LOG_DEBUG,"del_mfc orig %lx mcastgrp %lx\n",
1095 (u_long)ntohl(origin.s_addr), (u_long)ntohl(mcastgrp.s_addr));
1097 lwkt_gettoken(&mroute_token);
1099 hash = MFCHASH(origin.s_addr, mcastgrp.s_addr);
1100 for (nptr = &mfctable[hash]; (rt = *nptr) != NULL; nptr = &rt->mfc_next)
1101 if (origin.s_addr == rt->mfc_origin.s_addr &&
1102 mcastgrp.s_addr == rt->mfc_mcastgrp.s_addr &&
1103 rt->mfc_stall == NULL)
1106 lwkt_reltoken(&mroute_token);
1107 return EADDRNOTAVAIL;
1110 *nptr = rt->mfc_next;
1113 * free the bw_meter entries
1115 list = rt->mfc_bw_meter;
1116 rt->mfc_bw_meter = NULL;
1117 lwkt_reltoken(&mroute_token);
1119 kfree(rt, M_MRTABLE);
1126 * Send a message to mrouted on the multicast routing socket
1129 socket_send(struct socket *s, struct mbuf *mm, struct sockaddr_in *src)
1132 if (ssb_appendaddr(&s->so_rcv, (struct sockaddr *)src, mm, NULL) != 0) {
1142 * IP multicast forwarding function. This function assumes that the packet
1143 * pointed to by "ip" has arrived on (or is about to be sent to) the interface
1144 * pointed to by "ifp", and the packet is to be relayed to other networks
1145 * that have members of the packet's destination IP multicast group.
1147 * The packet is returned unscathed to the caller, unless it is
1148 * erroneous, in which case a non-zero return value tells the caller to
1152 #define TUNNEL_LEN 12 /* # bytes of IP option for tunnel encapsulation */
1155 X_ip_mforward(struct ip *ip, struct ifnet *ifp, struct mbuf *m,
1156 struct ip_moptions *imo)
1161 if (mrtdebug & DEBUG_FORWARD)
1162 log(LOG_DEBUG, "ip_mforward: src %lx, dst %lx, ifp %p\n",
1163 (u_long)ntohl(ip->ip_src.s_addr), (u_long)ntohl(ip->ip_dst.s_addr),
1166 if (ip->ip_hl < (sizeof(struct ip) + TUNNEL_LEN) >> 2 ||
1167 ((u_char *)(ip + 1))[1] != IPOPT_LSRR ) {
1169 * Packet arrived via a physical interface or
1170 * an encapsulated tunnel or a register_vif.
1174 * Packet arrived through a source-route tunnel.
1175 * Source-route tunnels are no longer supported.
1177 static int last_log;
1178 if (last_log != time_second) {
1179 last_log = time_second;
1181 "ip_mforward: received source-routed packet from %lx\n",
1182 (u_long)ntohl(ip->ip_src.s_addr));
1187 if (imo && ((vifi = imo->imo_multicast_vif) < numvifs)) {
1188 if (ip->ip_ttl < 255)
1189 ip->ip_ttl++; /* compensate for -1 in *_send routines */
1190 if (rsvpdebug && ip->ip_p == IPPROTO_RSVP) {
1191 struct vif *vifp = viftable + vifi;
1193 kprintf("Sending IPPROTO_RSVP from %lx to %lx on vif %d (%s%s)\n",
1194 (long)ntohl(ip->ip_src.s_addr), (long)ntohl(ip->ip_dst.s_addr),
1196 (vifp->v_flags & VIFF_TUNNEL) ? "tunnel on " : "",
1197 vifp->v_ifp->if_xname);
1199 return ip_mdq(m, ifp, NULL, vifi);
1201 if (rsvpdebug && ip->ip_p == IPPROTO_RSVP) {
1202 kprintf("Warning: IPPROTO_RSVP from %lx to %lx without vif option\n",
1203 (long)ntohl(ip->ip_src.s_addr), (long)ntohl(ip->ip_dst.s_addr));
1205 kprintf("In fact, no options were specified at all\n");
1209 * Don't forward a packet with time-to-live of zero or one,
1210 * or a packet destined to a local-only group.
1212 if (ip->ip_ttl <= 1 || ntohl(ip->ip_dst.s_addr) <= INADDR_MAX_LOCAL_GROUP)
1216 * Determine forwarding vifs from the forwarding cache table
1218 lwkt_gettoken(&mroute_token);
1219 ++mrtstat.mrts_mfc_lookups;
1220 rt = mfc_find(ip->ip_src.s_addr, ip->ip_dst.s_addr);
1222 /* Entry exists, so forward if necessary */
1224 int ipres = ip_mdq(m, ifp, rt, -1);
1225 lwkt_reltoken(&mroute_token);
1229 * If we don't have a route for packet's origin,
1230 * Make a copy of the packet & send message to routing daemon
1236 int hlen = ip->ip_hl << 2;
1238 ++mrtstat.mrts_mfc_misses;
1240 mrtstat.mrts_no_route++;
1241 if (mrtdebug & (DEBUG_FORWARD | DEBUG_MFC))
1242 log(LOG_DEBUG, "ip_mforward: no rte s %lx g %lx\n",
1243 (u_long)ntohl(ip->ip_src.s_addr),
1244 (u_long)ntohl(ip->ip_dst.s_addr));
1247 * Allocate mbufs early so that we don't do extra work if we are
1248 * just going to fail anyway. Make sure to pullup the header so
1249 * that other people can't step on it.
1251 rte = kmalloc((sizeof *rte), M_MRTABLE, M_INTWAIT | M_NULLOK);
1253 lwkt_reltoken(&mroute_token);
1257 mb0 = m_copypacket(m, MB_DONTWAIT);
1258 if (mb0 && (M_HASCL(mb0) || mb0->m_len < hlen))
1259 mb0 = m_pullup(mb0, hlen);
1261 kfree(rte, M_MRTABLE);
1262 lwkt_reltoken(&mroute_token);
1266 /* is there an upcall waiting for this flow ? */
1267 hash = MFCHASH(ip->ip_src.s_addr, ip->ip_dst.s_addr);
1268 for (rt = mfctable[hash]; rt; rt = rt->mfc_next) {
1269 if ((ip->ip_src.s_addr == rt->mfc_origin.s_addr) &&
1270 (ip->ip_dst.s_addr == rt->mfc_mcastgrp.s_addr) &&
1271 (rt->mfc_stall != NULL))
1278 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
1282 * Locate the vifi for the incoming interface for this packet.
1283 * If none found, drop packet.
1285 for (vifi=0; vifi < numvifs && viftable[vifi].v_ifp != ifp; vifi++)
1287 if (vifi >= numvifs) /* vif not found, drop packet */
1290 /* no upcall, so make a new entry */
1291 rt = kmalloc(sizeof(*rt), M_MRTABLE, M_INTWAIT | M_NULLOK);
1295 /* Make a copy of the header to send to the user level process */
1296 mm = m_copy(mb0, 0, hlen);
1301 * Send message to routing daemon to install
1302 * a route into the kernel table
1305 im = mtod(mm, struct igmpmsg *);
1306 im->im_msgtype = IGMPMSG_NOCACHE;
1310 mrtstat.mrts_upcalls++;
1312 k_igmpsrc.sin_addr = ip->ip_src;
1313 if (socket_send(ip_mrouter, mm, &k_igmpsrc) < 0) {
1314 log(LOG_WARNING, "ip_mforward: ip_mrouter socket queue full\n");
1315 ++mrtstat.mrts_upq_sockfull;
1317 kfree(rt, M_MRTABLE);
1319 kfree(rte, M_MRTABLE);
1321 lwkt_reltoken(&mroute_token);
1325 /* insert new entry at head of hash chain */
1326 rt->mfc_origin.s_addr = ip->ip_src.s_addr;
1327 rt->mfc_mcastgrp.s_addr = ip->ip_dst.s_addr;
1328 rt->mfc_expire = UPCALL_EXPIRE;
1330 for (i = 0; i < numvifs; i++) {
1331 rt->mfc_ttls[i] = 0;
1332 rt->mfc_flags[i] = 0;
1334 rt->mfc_parent = -1;
1336 rt->mfc_rp.s_addr = INADDR_ANY; /* clear the RP address */
1338 rt->mfc_bw_meter = NULL;
1340 /* link into table */
1341 rt->mfc_next = mfctable[hash];
1342 mfctable[hash] = rt;
1343 rt->mfc_stall = rte;
1346 /* determine if q has overflowed */
1351 * XXX ouch! we need to append to the list, but we
1352 * only have a pointer to the front, so we have to
1353 * scan the entire list every time.
1355 for (p = &rt->mfc_stall; *p != NULL; p = &(*p)->next)
1358 if (npkts > MAX_UPQ) {
1359 mrtstat.mrts_upq_ovflw++;
1361 kfree(rte, M_MRTABLE);
1363 lwkt_reltoken(&mroute_token);
1367 /* Add this entry to the end of the queue */
1375 lwkt_reltoken(&mroute_token);
1381 * Clean up the cache entry if upcall is not serviced
1384 expire_upcalls(void *unused)
1387 struct mfc *mfc, **nptr;
1390 lwkt_gettoken(&mroute_token);
1391 for (i = 0; i < MFCTBLSIZ; i++) {
1392 if (nexpire[i] == 0)
1394 nptr = &mfctable[i];
1395 for (mfc = *nptr; mfc != NULL; mfc = *nptr) {
1397 * Skip real cache entries
1398 * Make sure it wasn't marked to not expire (shouldn't happen)
1401 if (mfc->mfc_stall != NULL && mfc->mfc_expire != 0 &&
1402 --mfc->mfc_expire == 0) {
1403 if (mrtdebug & DEBUG_EXPIRE)
1404 log(LOG_DEBUG, "expire_upcalls: expiring (%lx %lx)\n",
1405 (u_long)ntohl(mfc->mfc_origin.s_addr),
1406 (u_long)ntohl(mfc->mfc_mcastgrp.s_addr));
1408 * drop all the packets
1409 * free the mbuf with the pkt, if, timing info
1411 for (rte = mfc->mfc_stall; rte; ) {
1412 struct rtdetq *n = rte->next;
1415 kfree(rte, M_MRTABLE);
1418 ++mrtstat.mrts_cache_cleanups;
1422 * free the bw_meter entries
1424 while (mfc->mfc_bw_meter != NULL) {
1425 struct bw_meter *x = mfc->mfc_bw_meter;
1427 mfc->mfc_bw_meter = x->bm_mfc_next;
1428 kfree(x, M_BWMETER);
1431 *nptr = mfc->mfc_next;
1432 kfree(mfc, M_MRTABLE);
1434 nptr = &mfc->mfc_next;
1438 callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT, expire_upcalls, NULL);
1439 lwkt_reltoken(&mroute_token);
1443 * Packet forwarding routine once entry in the cache is made
1446 ip_mdq(struct mbuf *m, struct ifnet *ifp, struct mfc *rt, vifi_t xmt_vif)
1448 struct ip *ip = mtod(m, struct ip *);
1450 int plen = ip->ip_len;
1453 * Macro to send packet on vif. Since RSVP packets don't get counted on
1454 * input, they shouldn't get counted on output, so statistics keeping is
1457 #define MC_SEND(ip,vifp,m) { \
1458 if ((vifp)->v_flags & VIFF_TUNNEL) \
1459 encap_send((ip), (vifp), (m)); \
1461 phyint_send((ip), (vifp), (m)); \
1465 * If xmt_vif is not -1, send on only the requested vif.
1467 * (since vifi_t is u_short, -1 becomes MAXUSHORT, which > numvifs.)
1469 if (xmt_vif < numvifs) {
1471 if (viftable[xmt_vif].v_flags & VIFF_REGISTER)
1472 pim_register_send(ip, viftable + xmt_vif, m, rt);
1475 MC_SEND(ip, viftable + xmt_vif, m);
1480 * Don't forward if it didn't arrive from the parent vif for its origin.
1482 vifi = rt->mfc_parent;
1483 if ((vifi >= numvifs) || (viftable[vifi].v_ifp != ifp)) {
1484 /* came in the wrong interface */
1485 if (mrtdebug & DEBUG_FORWARD)
1486 log(LOG_DEBUG, "wrong if: ifp %p vifi %d vififp %p\n",
1487 (void *)ifp, vifi, (void *)viftable[vifi].v_ifp);
1488 ++mrtstat.mrts_wrong_if;
1491 * If we are doing PIM assert processing, send a message
1492 * to the routing daemon.
1494 * XXX: A PIM-SM router needs the WRONGVIF detection so it
1495 * can complete the SPT switch, regardless of the type
1496 * of the iif (broadcast media, GRE tunnel, etc).
1498 if (pim_assert && (vifi < numvifs) && viftable[vifi].v_ifp) {
1503 if (ifp == &multicast_register_if)
1504 pimstat.pims_rcv_registers_wrongiif++;
1507 /* Get vifi for the incoming packet */
1508 for (vifi=0; vifi < numvifs && viftable[vifi].v_ifp != ifp; vifi++)
1510 if (vifi >= numvifs)
1511 return 0; /* The iif is not found: ignore the packet. */
1513 if (rt->mfc_flags[vifi] & MRT_MFC_FLAGS_DISABLE_WRONGVIF)
1514 return 0; /* WRONGVIF disabled: ignore the packet */
1518 TV_DELTA(rt->mfc_last_assert, now, delta);
1520 if (delta > ASSERT_MSG_TIME) {
1521 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
1523 int hlen = ip->ip_hl << 2;
1524 struct mbuf *mm = m_copy(m, 0, hlen);
1526 if (mm && (M_HASCL(mm) || mm->m_len < hlen))
1527 mm = m_pullup(mm, hlen);
1531 rt->mfc_last_assert = now;
1533 im = mtod(mm, struct igmpmsg *);
1534 im->im_msgtype = IGMPMSG_WRONGVIF;
1538 mrtstat.mrts_upcalls++;
1540 k_igmpsrc.sin_addr = im->im_src;
1541 if (socket_send(ip_mrouter, mm, &k_igmpsrc) < 0) {
1543 "ip_mforward: ip_mrouter socket queue full\n");
1544 ++mrtstat.mrts_upq_sockfull;
1552 /* If I sourced this packet, it counts as output, else it was input. */
1553 if (ip->ip_src.s_addr == viftable[vifi].v_lcl_addr.s_addr) {
1554 viftable[vifi].v_pkt_out++;
1555 viftable[vifi].v_bytes_out += plen;
1557 viftable[vifi].v_pkt_in++;
1558 viftable[vifi].v_bytes_in += plen;
1561 rt->mfc_byte_cnt += plen;
1564 * For each vif, decide if a copy of the packet should be forwarded.
1566 * - the ttl exceeds the vif's threshold
1567 * - there are group members downstream on interface
1569 for (vifi = 0; vifi < numvifs; vifi++)
1570 if ((rt->mfc_ttls[vifi] > 0) && (ip->ip_ttl > rt->mfc_ttls[vifi])) {
1571 viftable[vifi].v_pkt_out++;
1572 viftable[vifi].v_bytes_out += plen;
1574 if (viftable[vifi].v_flags & VIFF_REGISTER)
1575 pim_register_send(ip, viftable + vifi, m, rt);
1578 MC_SEND(ip, viftable+vifi, m);
1582 * Perform upcall-related bw measuring.
1584 if (rt->mfc_bw_meter != NULL) {
1589 for (x = rt->mfc_bw_meter; x != NULL; x = x->bm_mfc_next)
1590 bw_meter_receive_packet(x, plen, &now);
1597 * check if a vif number is legal/ok. This is used by ip_output.
1600 X_legal_vif_num(int vif)
1602 return (vif >= 0 && vif < numvifs);
1606 * Return the local address used by this vif
1609 X_ip_mcast_src(int vifi)
1611 if (vifi >= 0 && vifi < numvifs)
1612 return viftable[vifi].v_lcl_addr.s_addr;
1618 phyint_send(struct ip *ip, struct vif *vifp, struct mbuf *m)
1620 struct mbuf *mb_copy;
1621 int hlen = ip->ip_hl << 2;
1624 * Make a new reference to the packet; make sure that
1625 * the IP header is actually copied, not just referenced,
1626 * so that ip_output() only scribbles on the copy.
1628 mb_copy = m_copypacket(m, MB_DONTWAIT);
1629 if (mb_copy && (M_HASCL(mb_copy) || mb_copy->m_len < hlen))
1630 mb_copy = m_pullup(mb_copy, hlen);
1631 if (mb_copy == NULL)
1634 if (vifp->v_rate_limit == 0)
1635 tbf_send_packet(vifp, mb_copy);
1637 tbf_control(vifp, mb_copy, mtod(mb_copy, struct ip *), ip->ip_len);
1641 encap_send(struct ip *ip, struct vif *vifp, struct mbuf *m)
1643 struct mbuf *mb_copy;
1645 int i, len = ip->ip_len;
1647 /* Take care of delayed checksums */
1648 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
1649 in_delayed_cksum(m);
1650 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
1654 * copy the old packet & pullup its IP header into the
1655 * new mbuf so we can modify it. Try to fill the new
1656 * mbuf since if we don't the ethernet driver will.
1658 MGETHDR(mb_copy, MB_DONTWAIT, MT_HEADER);
1659 if (mb_copy == NULL)
1661 mb_copy->m_data += max_linkhdr;
1662 mb_copy->m_len = sizeof(multicast_encap_iphdr);
1664 if ((mb_copy->m_next = m_copypacket(m, MB_DONTWAIT)) == NULL) {
1668 i = MHLEN - M_LEADINGSPACE(mb_copy);
1671 mb_copy = m_pullup(mb_copy, i);
1672 if (mb_copy == NULL)
1674 mb_copy->m_pkthdr.len = len + sizeof(multicast_encap_iphdr);
1677 * fill in the encapsulating IP header.
1679 ip_copy = mtod(mb_copy, struct ip *);
1680 *ip_copy = multicast_encap_iphdr;
1681 ip_copy->ip_id = ip_newid();
1682 ip_copy->ip_len += len;
1683 ip_copy->ip_src = vifp->v_lcl_addr;
1684 ip_copy->ip_dst = vifp->v_rmt_addr;
1687 * turn the encapsulated IP header back into a valid one.
1689 ip = (struct ip *)((caddr_t)ip_copy + sizeof(multicast_encap_iphdr));
1691 ip->ip_len = htons(ip->ip_len);
1692 ip->ip_off = htons(ip->ip_off);
1694 mb_copy->m_data += sizeof(multicast_encap_iphdr);
1695 ip->ip_sum = in_cksum(mb_copy, ip->ip_hl << 2);
1696 mb_copy->m_data -= sizeof(multicast_encap_iphdr);
1698 if (vifp->v_rate_limit == 0)
1699 tbf_send_packet(vifp, mb_copy);
1701 tbf_control(vifp, mb_copy, ip, ip_copy->ip_len);
1705 * De-encapsulate a packet and feed it back through ip input (this
1706 * routine is called whenever IP gets a packet with proto type
1707 * ENCAP_PROTO and a local destination address).
1709 * This is similar to mroute_encapcheck() + mroute_encap_input() in -current.
1712 X_ipip_input(struct mbuf **mp, int *offp, int proto)
1714 struct mbuf *m = *mp;
1715 struct ip *ip = mtod(m, struct ip *);
1716 int hlen = ip->ip_hl << 2;
1718 if (!have_encap_tunnel) {
1719 rip_input(mp, offp, proto);
1720 return(IPPROTO_DONE);
1725 * dump the packet if it's not to a multicast destination or if
1726 * we don't have an encapsulating tunnel with the source.
1727 * Note: This code assumes that the remote site IP address
1728 * uniquely identifies the tunnel (i.e., that this site has
1729 * at most one tunnel with the remote site).
1731 if (!IN_MULTICAST(ntohl(((struct ip *)((char *)ip+hlen))->ip_dst.s_addr))) {
1732 ++mrtstat.mrts_bad_tunnel;
1734 return(IPPROTO_DONE);
1736 if (ip->ip_src.s_addr != last_encap_src) {
1737 struct vif *vifp = viftable;
1738 struct vif *vife = vifp + numvifs;
1740 last_encap_src = ip->ip_src.s_addr;
1741 last_encap_vif = NULL;
1742 for ( ; vifp < vife; ++vifp)
1743 if (vifp->v_rmt_addr.s_addr == ip->ip_src.s_addr) {
1744 if ((vifp->v_flags & (VIFF_TUNNEL|VIFF_SRCRT))
1746 last_encap_vif = vifp;
1750 if (last_encap_vif == NULL) {
1751 last_encap_src = INADDR_ANY;
1752 mrtstat.mrts_cant_tunnel++; /*XXX*/
1755 log(LOG_DEBUG, "ip_mforward: no tunnel with %lx\n",
1756 (u_long)ntohl(ip->ip_src.s_addr));
1757 return(IPPROTO_DONE);
1760 if (hlen > sizeof(struct ip))
1762 m->m_data += sizeof(struct ip);
1763 m->m_len -= sizeof(struct ip);
1764 m->m_pkthdr.len -= sizeof(struct ip);
1765 m->m_pkthdr.rcvif = last_encap_vif->v_ifp;
1767 netisr_queue(NETISR_IP, m);
1768 return(IPPROTO_DONE);
1772 * Token bucket filter module
1776 tbf_control(struct vif *vifp, struct mbuf *m, struct ip *ip, u_long p_len)
1778 struct tbf *t = vifp->v_tbf;
1780 if (p_len > MAX_BKT_SIZE) { /* drop if packet is too large */
1781 mrtstat.mrts_pkt2large++;
1786 tbf_update_tokens(vifp);
1788 if (t->tbf_q_len == 0) { /* queue empty... */
1789 if (p_len <= t->tbf_n_tok) { /* send packet if enough tokens */
1790 t->tbf_n_tok -= p_len;
1791 tbf_send_packet(vifp, m);
1792 } else { /* no, queue packet and try later */
1794 callout_reset(&tbf_reprocess_q_ch, TBF_REPROCESS,
1795 tbf_reprocess_q, vifp);
1797 } else if (t->tbf_q_len < t->tbf_max_q_len) {
1798 /* finite queue length, so queue pkts and process queue */
1800 tbf_process_q(vifp);
1802 /* queue full, try to dq and queue and process */
1803 if (!tbf_dq_sel(vifp, ip)) {
1804 mrtstat.mrts_q_overflow++;
1808 tbf_process_q(vifp);
1814 * adds a packet to the queue at the interface
1817 tbf_queue(struct vif *vifp, struct mbuf *m)
1819 struct tbf *t = vifp->v_tbf;
1821 lwkt_gettoken(&mroute_token);
1823 if (t->tbf_t == NULL) /* Queue was empty */
1825 else /* Insert at tail */
1826 t->tbf_t->m_nextpkt = m;
1828 t->tbf_t = m; /* Set new tail pointer */
1831 /* Make sure we didn't get fed a bogus mbuf */
1833 panic("tbf_queue: m_nextpkt");
1835 m->m_nextpkt = NULL;
1839 lwkt_reltoken(&mroute_token);
1843 * processes the queue at the interface
1846 tbf_process_q(struct vif *vifp)
1848 struct tbf *t = vifp->v_tbf;
1850 lwkt_gettoken(&mroute_token);
1852 /* loop through the queue at the interface and send as many packets
1855 while (t->tbf_q_len > 0) {
1856 struct mbuf *m = t->tbf_q;
1857 int len = mtod(m, struct ip *)->ip_len;
1859 /* determine if the packet can be sent */
1860 if (len > t->tbf_n_tok) /* not enough tokens, we are done */
1862 /* ok, reduce no of tokens, dequeue and send the packet. */
1863 t->tbf_n_tok -= len;
1865 t->tbf_q = m->m_nextpkt;
1866 if (--t->tbf_q_len == 0)
1869 m->m_nextpkt = NULL;
1870 tbf_send_packet(vifp, m);
1872 lwkt_reltoken(&mroute_token);
1876 tbf_reprocess_q(void *xvifp)
1878 struct vif *vifp = xvifp;
1880 if (ip_mrouter == NULL)
1882 tbf_update_tokens(vifp);
1883 tbf_process_q(vifp);
1884 if (vifp->v_tbf->tbf_q_len)
1885 callout_reset(&tbf_reprocess_q_ch, TBF_REPROCESS,
1886 tbf_reprocess_q, vifp);
1889 /* function that will selectively discard a member of the queue
1890 * based on the precedence value and the priority
1893 tbf_dq_sel(struct vif *vifp, struct ip *ip)
1896 struct mbuf *m, *last;
1898 struct tbf *t = vifp->v_tbf;
1900 lwkt_gettoken(&mroute_token);
1902 p = priority(vifp, ip);
1906 while ((m = *np) != NULL) {
1907 if (p > priority(vifp, mtod(m, struct ip *))) {
1909 /* If we're removing the last packet, fix the tail pointer */
1913 /* It's impossible for the queue to be empty, but check anyways. */
1914 if (--t->tbf_q_len == 0)
1916 mrtstat.mrts_drop_sel++;
1917 lwkt_reltoken(&mroute_token);
1923 lwkt_reltoken(&mroute_token);
1928 tbf_send_packet(struct vif *vifp, struct mbuf *m)
1930 lwkt_gettoken(&mroute_token);
1932 if (vifp->v_flags & VIFF_TUNNEL) /* If tunnel options */
1933 ip_output(m, NULL, &vifp->v_route, IP_FORWARDING, NULL, NULL);
1935 struct ip_moptions imo;
1937 static struct route ro; /* XXX check this */
1939 imo.imo_multicast_ifp = vifp->v_ifp;
1940 imo.imo_multicast_ttl = mtod(m, struct ip *)->ip_ttl - 1;
1941 imo.imo_multicast_loop = 1;
1942 imo.imo_multicast_vif = -1;
1945 * Re-entrancy should not be a problem here, because
1946 * the packets that we send out and are looped back at us
1947 * should get rejected because they appear to come from
1948 * the loopback interface, thus preventing looping.
1950 error = ip_output(m, NULL, &ro, IP_FORWARDING, &imo, NULL);
1952 if (mrtdebug & DEBUG_XMIT)
1953 log(LOG_DEBUG, "phyint_send on vif %d err %d\n",
1954 (int)(vifp - viftable), error);
1956 lwkt_reltoken(&mroute_token);
1959 /* determine the current time and then
1960 * the elapsed time (between the last time and time now)
1961 * in milliseconds & update the no. of tokens in the bucket
1964 tbf_update_tokens(struct vif *vifp)
1968 struct tbf *t = vifp->v_tbf;
1970 lwkt_gettoken(&mroute_token);
1974 TV_DELTA(tp, t->tbf_last_pkt_t, tm);
1977 * This formula is actually
1978 * "time in seconds" * "bytes/second".
1980 * (tm / 1000000) * (v_rate_limit * 1000 * (1000/1024) / 8)
1982 * The (1000/1024) was introduced in add_vif to optimize
1983 * this divide into a shift.
1985 t->tbf_n_tok += tm * vifp->v_rate_limit / 1024 / 8;
1986 t->tbf_last_pkt_t = tp;
1988 if (t->tbf_n_tok > MAX_BKT_SIZE)
1989 t->tbf_n_tok = MAX_BKT_SIZE;
1991 lwkt_reltoken(&mroute_token);
1995 priority(struct vif *vifp, struct ip *ip)
1997 int prio = 50; /* the lowest priority -- default case */
1999 /* temporary hack; may add general packet classifier some day */
2002 * The UDP port space is divided up into four priority ranges:
2003 * [0, 16384) : unclassified - lowest priority
2004 * [16384, 32768) : audio - highest priority
2005 * [32768, 49152) : whiteboard - medium priority
2006 * [49152, 65536) : video - low priority
2008 * Everything else gets lowest priority.
2010 if (ip->ip_p == IPPROTO_UDP) {
2011 struct udphdr *udp = (struct udphdr *)(((char *)ip) + (ip->ip_hl << 2));
2012 switch (ntohs(udp->uh_dport) & 0xc000) {
2028 * End of token bucket filter modifications
2032 X_ip_rsvp_vif(struct socket *so, struct sockopt *sopt)
2036 if (so->so_type != SOCK_RAW || so->so_proto->pr_protocol != IPPROTO_RSVP)
2039 error = soopt_to_kbuf(sopt, &vifi, sizeof vifi, sizeof vifi);
2043 lwkt_gettoken(&mroute_token);
2045 if (vifi < 0 || vifi >= numvifs) { /* Error if vif is invalid */
2046 lwkt_reltoken(&mroute_token);
2047 return EADDRNOTAVAIL;
2050 if (sopt->sopt_name == IP_RSVP_VIF_ON) {
2051 /* Check if socket is available. */
2052 if (viftable[vifi].v_rsvpd != NULL) {
2053 lwkt_reltoken(&mroute_token);
2057 viftable[vifi].v_rsvpd = so;
2058 /* This may seem silly, but we need to be sure we don't over-increment
2059 * the RSVP counter, in case something slips up.
2061 if (!viftable[vifi].v_rsvp_on) {
2062 viftable[vifi].v_rsvp_on = 1;
2065 } else { /* must be VIF_OFF */
2067 * XXX as an additional consistency check, one could make sure
2068 * that viftable[vifi].v_rsvpd == so, otherwise passing so as
2069 * first parameter is pretty useless.
2071 viftable[vifi].v_rsvpd = NULL;
2073 * This may seem silly, but we need to be sure we don't over-decrement
2074 * the RSVP counter, in case something slips up.
2076 if (viftable[vifi].v_rsvp_on) {
2077 viftable[vifi].v_rsvp_on = 0;
2081 lwkt_reltoken(&mroute_token);
2086 X_ip_rsvp_force_done(struct socket *so)
2090 /* Don't bother if it is not the right type of socket. */
2091 if (so->so_type != SOCK_RAW || so->so_proto->pr_protocol != IPPROTO_RSVP)
2094 lwkt_gettoken(&mroute_token);
2096 /* The socket may be attached to more than one vif...this
2097 * is perfectly legal.
2099 for (vifi = 0; vifi < numvifs; vifi++) {
2100 if (viftable[vifi].v_rsvpd == so) {
2101 viftable[vifi].v_rsvpd = NULL;
2102 /* This may seem silly, but we need to be sure we don't
2103 * over-decrement the RSVP counter, in case something slips up.
2105 if (viftable[vifi].v_rsvp_on) {
2106 viftable[vifi].v_rsvp_on = 0;
2112 lwkt_reltoken(&mroute_token);
2116 X_rsvp_input(struct mbuf **mp, int *offp, int proto)
2119 struct mbuf *m = *mp;
2120 struct ip *ip = mtod(m, struct ip *);
2121 struct sockaddr_in rsvp_src = { sizeof rsvp_src, AF_INET };
2125 /* support IP_RECVIF used by rsvpd rel4.2a1 */
2135 kprintf("rsvp_input: rsvp_on %d\n",rsvp_on);
2137 /* Can still get packets with rsvp_on = 0 if there is a local member
2138 * of the group to which the RSVP packet is addressed. But in this
2139 * case we want to throw the packet away.
2143 return(IPPROTO_DONE);
2146 lwkt_gettoken(&mroute_token);
2149 kprintf("rsvp_input: check vifs\n");
2152 if (!(m->m_flags & M_PKTHDR))
2153 panic("rsvp_input no hdr");
2156 ifp = m->m_pkthdr.rcvif;
2157 /* Find which vif the packet arrived on. */
2158 for (vifi = 0; vifi < numvifs; vifi++)
2159 if (viftable[vifi].v_ifp == ifp)
2163 if (vifi == numvifs || (so = viftable[vifi].v_rsvpd) == NULL) {
2165 if (vifi == numvifs || viftable[vifi].v_rsvpd == NULL) {
2168 * If the old-style non-vif-associated socket is set,
2169 * then use it. Otherwise, drop packet since there
2170 * is no specific socket for this vif.
2172 if (ip_rsvpd != NULL) {
2174 kprintf("rsvp_input: Sending packet up old-style socket\n");
2176 rip_input(mp, offp, proto); /* xxx */
2178 if (rsvpdebug && vifi == numvifs)
2179 kprintf("rsvp_input: Can't find vif for packet.\n");
2180 else if (rsvpdebug && viftable[vifi].v_rsvpd == NULL)
2181 kprintf("rsvp_input: No socket defined for vif %d\n",vifi);
2184 lwkt_reltoken(&mroute_token);
2185 return(IPPROTO_DONE);
2187 rsvp_src.sin_addr = ip->ip_src;
2190 kprintf("rsvp_input: m->m_len = %d, ssb_space() = %ld\n",
2191 m->m_len,ssb_space(&(viftable[vifi].v_rsvpd->so_rcv)));
2195 inp = (struct inpcb *)so->so_pcb;
2196 if (inp->inp_flags & INP_CONTROLOPTS ||
2197 inp->inp_socket->so_options & SO_TIMESTAMP) {
2198 ip_savecontrol(inp, &opts, ip, m);
2200 if (ssb_appendaddr(&so->so_rcv,
2201 (struct sockaddr *)&rsvp_src,m, opts) == 0) {
2206 kprintf("rsvp_input: Failed to append to socket\n");
2211 kprintf("rsvp_input: send packet up\n");
2214 if (socket_send(viftable[vifi].v_rsvpd, m, &rsvp_src) < 0) {
2216 kprintf("rsvp_input: Failed to append to socket\n");
2219 kprintf("rsvp_input: send packet up\n");
2222 lwkt_reltoken(&mroute_token);
2223 return(IPPROTO_DONE);
2227 * Code for bandwidth monitors
2231 * Define common interface for timeval-related methods
2233 #define BW_TIMEVALCMP(tvp, uvp, cmp) timevalcmp((tvp), (uvp), cmp)
2234 #define BW_TIMEVALDECR(vvp, uvp) timevalsub((vvp), (uvp))
2235 #define BW_TIMEVALADD(vvp, uvp) timevaladd((vvp), (uvp))
2238 compute_bw_meter_flags(struct bw_upcall *req)
2242 if (req->bu_flags & BW_UPCALL_UNIT_PACKETS)
2243 flags |= BW_METER_UNIT_PACKETS;
2244 if (req->bu_flags & BW_UPCALL_UNIT_BYTES)
2245 flags |= BW_METER_UNIT_BYTES;
2246 if (req->bu_flags & BW_UPCALL_GEQ)
2247 flags |= BW_METER_GEQ;
2248 if (req->bu_flags & BW_UPCALL_LEQ)
2249 flags |= BW_METER_LEQ;
2255 * Add a bw_meter entry
2258 add_bw_upcall(struct bw_upcall *req)
2261 struct timeval delta = { BW_UPCALL_THRESHOLD_INTERVAL_MIN_SEC,
2262 BW_UPCALL_THRESHOLD_INTERVAL_MIN_USEC };
2267 if (!(mrt_api_config & MRT_MFC_BW_UPCALL))
2270 /* Test if the flags are valid */
2271 if (!(req->bu_flags & (BW_UPCALL_UNIT_PACKETS | BW_UPCALL_UNIT_BYTES)))
2273 if (!(req->bu_flags & (BW_UPCALL_GEQ | BW_UPCALL_LEQ)))
2275 if ((req->bu_flags & (BW_UPCALL_GEQ | BW_UPCALL_LEQ))
2276 == (BW_UPCALL_GEQ | BW_UPCALL_LEQ))
2279 /* Test if the threshold time interval is valid */
2280 if (BW_TIMEVALCMP(&req->bu_threshold.b_time, &delta, <))
2283 flags = compute_bw_meter_flags(req);
2286 * Find if we have already same bw_meter entry
2288 lwkt_gettoken(&mroute_token);
2289 mfc = mfc_find(req->bu_src.s_addr, req->bu_dst.s_addr);
2291 lwkt_reltoken(&mroute_token);
2292 return EADDRNOTAVAIL;
2294 for (x = mfc->mfc_bw_meter; x != NULL; x = x->bm_mfc_next) {
2295 if ((BW_TIMEVALCMP(&x->bm_threshold.b_time,
2296 &req->bu_threshold.b_time, ==)) &&
2297 (x->bm_threshold.b_packets == req->bu_threshold.b_packets) &&
2298 (x->bm_threshold.b_bytes == req->bu_threshold.b_bytes) &&
2299 (x->bm_flags & BW_METER_USER_FLAGS) == flags) {
2300 lwkt_reltoken(&mroute_token);
2301 return 0; /* XXX Already installed */
2304 lwkt_reltoken(&mroute_token);
2306 /* Allocate the new bw_meter entry */
2307 x = kmalloc(sizeof(*x), M_BWMETER, M_INTWAIT);
2309 /* Set the new bw_meter entry */
2310 x->bm_threshold.b_time = req->bu_threshold.b_time;
2312 x->bm_start_time = now;
2313 x->bm_threshold.b_packets = req->bu_threshold.b_packets;
2314 x->bm_threshold.b_bytes = req->bu_threshold.b_bytes;
2315 x->bm_measured.b_packets = 0;
2316 x->bm_measured.b_bytes = 0;
2317 x->bm_flags = flags;
2318 x->bm_time_next = NULL;
2319 x->bm_time_hash = BW_METER_BUCKETS;
2321 /* Add the new bw_meter entry to the front of entries for this MFC */
2322 lwkt_gettoken(&mroute_token);
2324 x->bm_mfc_next = mfc->mfc_bw_meter;
2325 mfc->mfc_bw_meter = x;
2326 schedule_bw_meter(x, &now);
2327 lwkt_reltoken(&mroute_token);
2333 free_bw_list(struct bw_meter *list)
2335 while (list != NULL) {
2336 struct bw_meter *x = list;
2338 list = list->bm_mfc_next;
2339 unschedule_bw_meter(x);
2340 kfree(x, M_BWMETER);
2345 * Delete one or multiple bw_meter entries
2348 del_bw_upcall(struct bw_upcall *req)
2353 if (!(mrt_api_config & MRT_MFC_BW_UPCALL))
2356 lwkt_gettoken(&mroute_token);
2357 /* Find the corresponding MFC entry */
2358 mfc = mfc_find(req->bu_src.s_addr, req->bu_dst.s_addr);
2360 lwkt_reltoken(&mroute_token);
2361 return EADDRNOTAVAIL;
2362 } else if (req->bu_flags & BW_UPCALL_DELETE_ALL) {
2364 * Delete all bw_meter entries for this mfc
2366 struct bw_meter *list;
2368 list = mfc->mfc_bw_meter;
2369 mfc->mfc_bw_meter = NULL;
2370 lwkt_reltoken(&mroute_token);
2373 } else { /* Delete a single bw_meter entry */
2374 struct bw_meter *prev;
2377 flags = compute_bw_meter_flags(req);
2379 /* Find the bw_meter entry to delete */
2380 for (prev = NULL, x = mfc->mfc_bw_meter; x != NULL;
2381 prev = x, x = x->bm_mfc_next) {
2382 if ((BW_TIMEVALCMP(&x->bm_threshold.b_time,
2383 &req->bu_threshold.b_time, ==)) &&
2384 (x->bm_threshold.b_packets == req->bu_threshold.b_packets) &&
2385 (x->bm_threshold.b_bytes == req->bu_threshold.b_bytes) &&
2386 (x->bm_flags & BW_METER_USER_FLAGS) == flags)
2389 if (x != NULL) { /* Delete entry from the list for this MFC */
2391 prev->bm_mfc_next = x->bm_mfc_next; /* remove from middle*/
2393 x->bm_mfc->mfc_bw_meter = x->bm_mfc_next;/* new head of list */
2394 unschedule_bw_meter(x);
2395 lwkt_reltoken(&mroute_token);
2396 /* Free the bw_meter entry */
2397 kfree(x, M_BWMETER);
2400 lwkt_reltoken(&mroute_token);
2408 * Perform bandwidth measurement processing that may result in an upcall
2411 bw_meter_receive_packet(struct bw_meter *x, int plen, struct timeval *nowp)
2413 struct timeval delta;
2415 lwkt_gettoken(&mroute_token);
2417 BW_TIMEVALDECR(&delta, &x->bm_start_time);
2419 if (x->bm_flags & BW_METER_GEQ) {
2421 * Processing for ">=" type of bw_meter entry
2423 if (BW_TIMEVALCMP(&delta, &x->bm_threshold.b_time, >)) {
2424 /* Reset the bw_meter entry */
2425 x->bm_start_time = *nowp;
2426 x->bm_measured.b_packets = 0;
2427 x->bm_measured.b_bytes = 0;
2428 x->bm_flags &= ~BW_METER_UPCALL_DELIVERED;
2431 /* Record that a packet is received */
2432 x->bm_measured.b_packets++;
2433 x->bm_measured.b_bytes += plen;
2436 * Test if we should deliver an upcall
2438 if (!(x->bm_flags & BW_METER_UPCALL_DELIVERED)) {
2439 if (((x->bm_flags & BW_METER_UNIT_PACKETS) &&
2440 (x->bm_measured.b_packets >= x->bm_threshold.b_packets)) ||
2441 ((x->bm_flags & BW_METER_UNIT_BYTES) &&
2442 (x->bm_measured.b_bytes >= x->bm_threshold.b_bytes))) {
2443 /* Prepare an upcall for delivery */
2444 bw_meter_prepare_upcall(x, nowp);
2445 x->bm_flags |= BW_METER_UPCALL_DELIVERED;
2448 } else if (x->bm_flags & BW_METER_LEQ) {
2450 * Processing for "<=" type of bw_meter entry
2452 if (BW_TIMEVALCMP(&delta, &x->bm_threshold.b_time, >)) {
2454 * We are behind time with the multicast forwarding table
2455 * scanning for "<=" type of bw_meter entries, so test now
2456 * if we should deliver an upcall.
2458 if (((x->bm_flags & BW_METER_UNIT_PACKETS) &&
2459 (x->bm_measured.b_packets <= x->bm_threshold.b_packets)) ||
2460 ((x->bm_flags & BW_METER_UNIT_BYTES) &&
2461 (x->bm_measured.b_bytes <= x->bm_threshold.b_bytes))) {
2462 /* Prepare an upcall for delivery */
2463 bw_meter_prepare_upcall(x, nowp);
2465 /* Reschedule the bw_meter entry */
2466 unschedule_bw_meter(x);
2467 schedule_bw_meter(x, nowp);
2470 /* Record that a packet is received */
2471 x->bm_measured.b_packets++;
2472 x->bm_measured.b_bytes += plen;
2475 * Test if we should restart the measuring interval
2477 if ((x->bm_flags & BW_METER_UNIT_PACKETS &&
2478 x->bm_measured.b_packets <= x->bm_threshold.b_packets) ||
2479 (x->bm_flags & BW_METER_UNIT_BYTES &&
2480 x->bm_measured.b_bytes <= x->bm_threshold.b_bytes)) {
2481 /* Don't restart the measuring interval */
2483 /* Do restart the measuring interval */
2485 * XXX: note that we don't unschedule and schedule, because this
2486 * might be too much overhead per packet. Instead, when we process
2487 * all entries for a given timer hash bin, we check whether it is
2488 * really a timeout. If not, we reschedule at that time.
2490 x->bm_start_time = *nowp;
2491 x->bm_measured.b_packets = 0;
2492 x->bm_measured.b_bytes = 0;
2493 x->bm_flags &= ~BW_METER_UPCALL_DELIVERED;
2496 lwkt_reltoken(&mroute_token);
2500 * Prepare a bandwidth-related upcall
2503 bw_meter_prepare_upcall(struct bw_meter *x, struct timeval *nowp)
2505 struct timeval delta;
2506 struct bw_upcall *u;
2508 lwkt_gettoken(&mroute_token);
2511 * Compute the measured time interval
2514 BW_TIMEVALDECR(&delta, &x->bm_start_time);
2517 * If there are too many pending upcalls, deliver them now
2519 if (bw_upcalls_n >= BW_UPCALLS_MAX)
2523 * Set the bw_upcall entry
2525 u = &bw_upcalls[bw_upcalls_n++];
2526 u->bu_src = x->bm_mfc->mfc_origin;
2527 u->bu_dst = x->bm_mfc->mfc_mcastgrp;
2528 u->bu_threshold.b_time = x->bm_threshold.b_time;
2529 u->bu_threshold.b_packets = x->bm_threshold.b_packets;
2530 u->bu_threshold.b_bytes = x->bm_threshold.b_bytes;
2531 u->bu_measured.b_time = delta;
2532 u->bu_measured.b_packets = x->bm_measured.b_packets;
2533 u->bu_measured.b_bytes = x->bm_measured.b_bytes;
2535 if (x->bm_flags & BW_METER_UNIT_PACKETS)
2536 u->bu_flags |= BW_UPCALL_UNIT_PACKETS;
2537 if (x->bm_flags & BW_METER_UNIT_BYTES)
2538 u->bu_flags |= BW_UPCALL_UNIT_BYTES;
2539 if (x->bm_flags & BW_METER_GEQ)
2540 u->bu_flags |= BW_UPCALL_GEQ;
2541 if (x->bm_flags & BW_METER_LEQ)
2542 u->bu_flags |= BW_UPCALL_LEQ;
2544 lwkt_reltoken(&mroute_token);
2548 * Send the pending bandwidth-related upcalls
2551 bw_upcalls_send(void)
2554 int len = bw_upcalls_n * sizeof(bw_upcalls[0]);
2555 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
2556 static struct igmpmsg igmpmsg = { 0, /* unused1 */
2558 IGMPMSG_BW_UPCALL,/* im_msgtype */
2563 { 0 } }; /* im_dst */
2565 if (bw_upcalls_n == 0)
2566 return; /* No pending upcalls */
2571 * Allocate a new mbuf, initialize it with the header and
2572 * the payload for the pending calls.
2574 MGETHDR(m, MB_DONTWAIT, MT_HEADER);
2576 log(LOG_WARNING, "bw_upcalls_send: cannot allocate mbuf\n");
2580 m->m_len = m->m_pkthdr.len = 0;
2581 m_copyback(m, 0, sizeof(struct igmpmsg), (caddr_t)&igmpmsg);
2582 m_copyback(m, sizeof(struct igmpmsg), len, (caddr_t)&bw_upcalls[0]);
2586 * XXX do we need to set the address in k_igmpsrc ?
2588 mrtstat.mrts_upcalls++;
2589 if (socket_send(ip_mrouter, m, &k_igmpsrc) < 0) {
2590 log(LOG_WARNING, "bw_upcalls_send: ip_mrouter socket queue full\n");
2591 ++mrtstat.mrts_upq_sockfull;
2596 * Compute the timeout hash value for the bw_meter entries
2598 #define BW_METER_TIMEHASH(bw_meter, hash) \
2600 struct timeval next_timeval = (bw_meter)->bm_start_time; \
2602 BW_TIMEVALADD(&next_timeval, &(bw_meter)->bm_threshold.b_time); \
2603 (hash) = next_timeval.tv_sec; \
2604 if (next_timeval.tv_usec) \
2605 (hash)++; /* XXX: make sure we don't timeout early */ \
2606 (hash) %= BW_METER_BUCKETS; \
2610 * Schedule a timer to process periodically bw_meter entry of type "<="
2611 * by linking the entry in the proper hash bucket.
2614 schedule_bw_meter(struct bw_meter *x, struct timeval *nowp)
2618 if (!(x->bm_flags & BW_METER_LEQ))
2619 return; /* XXX: we schedule timers only for "<=" entries */
2622 * Reset the bw_meter entry
2624 lwkt_gettoken(&mroute_token);
2625 x->bm_start_time = *nowp;
2626 x->bm_measured.b_packets = 0;
2627 x->bm_measured.b_bytes = 0;
2628 x->bm_flags &= ~BW_METER_UPCALL_DELIVERED;
2631 * Compute the timeout hash value and insert the entry
2633 BW_METER_TIMEHASH(x, time_hash);
2634 x->bm_time_next = bw_meter_timers[time_hash];
2635 bw_meter_timers[time_hash] = x;
2636 x->bm_time_hash = time_hash;
2638 lwkt_reltoken(&mroute_token);
2642 * Unschedule the periodic timer that processes bw_meter entry of type "<="
2643 * by removing the entry from the proper hash bucket.
2646 unschedule_bw_meter(struct bw_meter *x)
2649 struct bw_meter *prev, *tmp;
2651 if (!(x->bm_flags & BW_METER_LEQ))
2652 return; /* XXX: we schedule timers only for "<=" entries */
2655 * Compute the timeout hash value and delete the entry
2657 time_hash = x->bm_time_hash;
2658 if (time_hash >= BW_METER_BUCKETS)
2659 return; /* Entry was not scheduled */
2661 for (prev = NULL, tmp = bw_meter_timers[time_hash];
2662 tmp != NULL; prev = tmp, tmp = tmp->bm_time_next)
2667 panic("unschedule_bw_meter: bw_meter entry not found");
2670 prev->bm_time_next = x->bm_time_next;
2672 bw_meter_timers[time_hash] = x->bm_time_next;
2674 x->bm_time_next = NULL;
2675 x->bm_time_hash = BW_METER_BUCKETS;
2680 * Process all "<=" type of bw_meter that should be processed now,
2681 * and for each entry prepare an upcall if necessary. Each processed
2682 * entry is rescheduled again for the (periodic) processing.
2684 * This is run periodically (once per second normally). On each round,
2685 * all the potentially matching entries are in the hash slot that we are
2689 bw_meter_process(void)
2691 static uint32_t last_tv_sec; /* last time we processed this */
2695 struct timeval now, process_endtime;
2698 if (last_tv_sec == now.tv_sec)
2699 return; /* nothing to do */
2701 lwkt_gettoken(&mroute_token);
2702 loops = now.tv_sec - last_tv_sec;
2703 last_tv_sec = now.tv_sec;
2704 if (loops > BW_METER_BUCKETS)
2705 loops = BW_METER_BUCKETS;
2708 * Process all bins of bw_meter entries from the one after the last
2709 * processed to the current one. On entry, i points to the last bucket
2710 * visited, so we need to increment i at the beginning of the loop.
2712 for (i = (now.tv_sec - loops) % BW_METER_BUCKETS; loops > 0; loops--) {
2713 struct bw_meter *x, *tmp_list;
2715 if (++i >= BW_METER_BUCKETS)
2718 /* Disconnect the list of bw_meter entries from the bin */
2719 tmp_list = bw_meter_timers[i];
2720 bw_meter_timers[i] = NULL;
2722 /* Process the list of bw_meter entries */
2723 while (tmp_list != NULL) {
2725 tmp_list = tmp_list->bm_time_next;
2727 /* Test if the time interval is over */
2728 process_endtime = x->bm_start_time;
2729 BW_TIMEVALADD(&process_endtime, &x->bm_threshold.b_time);
2730 if (BW_TIMEVALCMP(&process_endtime, &now, >)) {
2731 /* Not yet: reschedule, but don't reset */
2734 BW_METER_TIMEHASH(x, time_hash);
2735 if (time_hash == i && process_endtime.tv_sec == now.tv_sec) {
2737 * XXX: somehow the bin processing is a bit ahead of time.
2738 * Put the entry in the next bin.
2740 if (++time_hash >= BW_METER_BUCKETS)
2743 x->bm_time_next = bw_meter_timers[time_hash];
2744 bw_meter_timers[time_hash] = x;
2745 x->bm_time_hash = time_hash;
2751 * Test if we should deliver an upcall
2753 if (((x->bm_flags & BW_METER_UNIT_PACKETS) &&
2754 (x->bm_measured.b_packets <= x->bm_threshold.b_packets)) ||
2755 ((x->bm_flags & BW_METER_UNIT_BYTES) &&
2756 (x->bm_measured.b_bytes <= x->bm_threshold.b_bytes))) {
2757 /* Prepare an upcall for delivery */
2758 bw_meter_prepare_upcall(x, &now);
2762 * Reschedule for next processing
2764 schedule_bw_meter(x, &now);
2767 /* Send all upcalls that are pending delivery */
2769 lwkt_reltoken(&mroute_token);
2773 * A periodic function for sending all upcalls that are pending delivery
2776 expire_bw_upcalls_send(void *unused)
2780 callout_reset(&bw_upcalls_ch, BW_UPCALLS_PERIOD,
2781 expire_bw_upcalls_send, NULL);
2785 * A periodic function for periodic scanning of the multicast forwarding
2786 * table for processing all "<=" bw_meter entries.
2789 expire_bw_meter_process(void *unused)
2791 if (mrt_api_config & MRT_MFC_BW_UPCALL)
2794 callout_reset(&bw_meter_ch, BW_METER_PERIOD,
2795 expire_bw_meter_process, NULL);
2799 * End of bandwidth monitoring code
2804 * Send the packet up to the user daemon, or eventually do kernel encapsulation
2808 pim_register_send(struct ip *ip, struct vif *vifp,
2809 struct mbuf *m, struct mfc *rt)
2811 struct mbuf *mb_copy, *mm;
2813 if (mrtdebug & DEBUG_PIM)
2814 log(LOG_DEBUG, "pim_register_send: ");
2816 mb_copy = pim_register_prepare(ip, m);
2817 if (mb_copy == NULL)
2821 * Send all the fragments. Note that the mbuf for each fragment
2822 * is freed by the sending machinery.
2824 for (mm = mb_copy; mm; mm = mb_copy) {
2825 mb_copy = mm->m_nextpkt;
2827 mm = m_pullup(mm, sizeof(struct ip));
2829 ip = mtod(mm, struct ip *);
2830 if ((mrt_api_config & MRT_MFC_RP) &&
2831 (rt->mfc_rp.s_addr != INADDR_ANY)) {
2832 pim_register_send_rp(ip, vifp, mm, rt);
2834 pim_register_send_upcall(ip, vifp, mm, rt);
2843 * Return a copy of the data packet that is ready for PIM Register
2845 * XXX: Note that in the returned copy the IP header is a valid one.
2847 static struct mbuf *
2848 pim_register_prepare(struct ip *ip, struct mbuf *m)
2850 struct mbuf *mb_copy = NULL;
2853 /* Take care of delayed checksums */
2854 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
2855 in_delayed_cksum(m);
2856 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
2860 * Copy the old packet & pullup its IP header into the
2861 * new mbuf so we can modify it.
2863 mb_copy = m_copypacket(m, MB_DONTWAIT);
2864 if (mb_copy == NULL)
2866 mb_copy = m_pullup(mb_copy, ip->ip_hl << 2);
2867 if (mb_copy == NULL)
2870 /* take care of the TTL */
2871 ip = mtod(mb_copy, struct ip *);
2874 /* Compute the MTU after the PIM Register encapsulation */
2875 mtu = 0xffff - sizeof(pim_encap_iphdr) - sizeof(pim_encap_pimhdr);
2877 if (ip->ip_len <= mtu) {
2878 /* Turn the IP header into a valid one */
2879 ip->ip_len = htons(ip->ip_len);
2880 ip->ip_off = htons(ip->ip_off);
2882 ip->ip_sum = in_cksum(mb_copy, ip->ip_hl << 2);
2884 /* Fragment the packet */
2885 if (ip_fragment(ip, &mb_copy, mtu, 0, CSUM_DELAY_IP) != 0) {
2894 * Send an upcall with the data packet to the user-level process.
2897 pim_register_send_upcall(struct ip *ip, struct vif *vifp,
2898 struct mbuf *mb_copy, struct mfc *rt)
2900 struct mbuf *mb_first;
2901 int len = ntohs(ip->ip_len);
2903 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
2906 * Add a new mbuf with an upcall header
2908 MGETHDR(mb_first, MB_DONTWAIT, MT_HEADER);
2909 if (mb_first == NULL) {
2913 mb_first->m_data += max_linkhdr;
2914 mb_first->m_pkthdr.len = len + sizeof(struct igmpmsg);
2915 mb_first->m_len = sizeof(struct igmpmsg);
2916 mb_first->m_next = mb_copy;
2918 /* Send message to routing daemon */
2919 im = mtod(mb_first, struct igmpmsg *);
2920 im->im_msgtype = IGMPMSG_WHOLEPKT;
2922 im->im_vif = vifp - viftable;
2923 im->im_src = ip->ip_src;
2924 im->im_dst = ip->ip_dst;
2926 k_igmpsrc.sin_addr = ip->ip_src;
2928 mrtstat.mrts_upcalls++;
2930 if (socket_send(ip_mrouter, mb_first, &k_igmpsrc) < 0) {
2931 if (mrtdebug & DEBUG_PIM)
2933 "mcast: pim_register_send_upcall: ip_mrouter socket queue full");
2934 ++mrtstat.mrts_upq_sockfull;
2938 /* Keep statistics */
2939 pimstat.pims_snd_registers_msgs++;
2940 pimstat.pims_snd_registers_bytes += len;
2946 * Encapsulate the data packet in PIM Register message and send it to the RP.
2949 pim_register_send_rp(struct ip *ip, struct vif *vifp,
2950 struct mbuf *mb_copy, struct mfc *rt)
2952 struct mbuf *mb_first;
2953 struct ip *ip_outer;
2954 struct pim_encap_pimhdr *pimhdr;
2955 int len = ntohs(ip->ip_len);
2956 vifi_t vifi = rt->mfc_parent;
2958 if ((vifi >= numvifs) || (viftable[vifi].v_lcl_addr.s_addr == 0)) {
2960 return EADDRNOTAVAIL; /* The iif vif is invalid */
2964 * Add a new mbuf with the encapsulating header
2966 MGETHDR(mb_first, MB_DONTWAIT, MT_HEADER);
2967 if (mb_first == NULL) {
2971 mb_first->m_data += max_linkhdr;
2972 mb_first->m_len = sizeof(pim_encap_iphdr) + sizeof(pim_encap_pimhdr);
2973 mb_first->m_next = mb_copy;
2975 mb_first->m_pkthdr.len = len + mb_first->m_len;
2978 * Fill in the encapsulating IP and PIM header
2980 ip_outer = mtod(mb_first, struct ip *);
2981 *ip_outer = pim_encap_iphdr;
2982 ip_outer->ip_id = ip_newid();
2983 ip_outer->ip_len = len + sizeof(pim_encap_iphdr) + sizeof(pim_encap_pimhdr);
2984 ip_outer->ip_src = viftable[vifi].v_lcl_addr;
2985 ip_outer->ip_dst = rt->mfc_rp;
2987 * Copy the inner header TOS to the outer header, and take care of the
2990 ip_outer->ip_tos = ip->ip_tos;
2991 if (ntohs(ip->ip_off) & IP_DF)
2992 ip_outer->ip_off |= IP_DF;
2993 pimhdr = (struct pim_encap_pimhdr *)((caddr_t)ip_outer
2994 + sizeof(pim_encap_iphdr));
2995 *pimhdr = pim_encap_pimhdr;
2996 /* If the iif crosses a border, set the Border-bit */
2997 if (rt->mfc_flags[vifi] & MRT_MFC_FLAGS_BORDER_VIF & mrt_api_config)
2998 pimhdr->flags |= htonl(PIM_BORDER_REGISTER);
3000 mb_first->m_data += sizeof(pim_encap_iphdr);
3001 pimhdr->pim.pim_cksum = in_cksum(mb_first, sizeof(pim_encap_pimhdr));
3002 mb_first->m_data -= sizeof(pim_encap_iphdr);
3004 if (vifp->v_rate_limit == 0)
3005 tbf_send_packet(vifp, mb_first);
3007 tbf_control(vifp, mb_first, ip, ip_outer->ip_len);
3009 /* Keep statistics */
3010 pimstat.pims_snd_registers_msgs++;
3011 pimstat.pims_snd_registers_bytes += len;
3017 * PIM-SMv2 and PIM-DM messages processing.
3018 * Receives and verifies the PIM control messages, and passes them
3019 * up to the listening socket, using rip_input().
3020 * The only message with special processing is the PIM_REGISTER message
3021 * (used by PIM-SM): the PIM header is stripped off, and the inner packet
3022 * is passed to if_simloop().
3025 pim_input(struct mbuf **mp, int *offp, int proto)
3027 struct mbuf *m = *mp;
3028 struct ip *ip = mtod(m, struct ip *);
3031 int datalen = ip->ip_len;
3038 /* Keep statistics */
3039 pimstat.pims_rcv_total_msgs++;
3040 pimstat.pims_rcv_total_bytes += datalen;
3045 if (datalen < PIM_MINLEN) {
3046 pimstat.pims_rcv_tooshort++;
3047 log(LOG_ERR, "pim_input: packet size too small %d from %lx\n",
3048 datalen, (u_long)ip->ip_src.s_addr);
3050 return(IPPROTO_DONE);
3054 * If the packet is at least as big as a REGISTER, go agead
3055 * and grab the PIM REGISTER header size, to avoid another
3056 * possible m_pullup() later.
3058 * PIM_MINLEN == pimhdr + u_int32_t == 4 + 4 = 8
3059 * PIM_REG_MINLEN == pimhdr + reghdr + encap_iphdr == 4 + 4 + 20 = 28
3061 minlen = iphlen + (datalen >= PIM_REG_MINLEN ? PIM_REG_MINLEN : PIM_MINLEN);
3063 * Get the IP and PIM headers in contiguous memory, and
3064 * possibly the PIM REGISTER header.
3066 if ((m->m_flags & M_EXT || m->m_len < minlen) &&
3067 (m = m_pullup(m, minlen)) == 0) {
3068 log(LOG_ERR, "pim_input: m_pullup failure\n");
3069 return(IPPROTO_DONE);
3071 /* m_pullup() may have given us a new mbuf so reset ip. */
3072 ip = mtod(m, struct ip *);
3073 ip_tos = ip->ip_tos;
3075 /* adjust mbuf to point to the PIM header */
3076 m->m_data += iphlen;
3078 pim = mtod(m, struct pim *);
3081 * Validate checksum. If PIM REGISTER, exclude the data packet.
3083 * XXX: some older PIMv2 implementations don't make this distinction,
3084 * so for compatibility reason perform the checksum over part of the
3085 * message, and if error, then over the whole message.
3087 if (PIM_VT_T(pim->pim_vt) == PIM_REGISTER && in_cksum(m, PIM_MINLEN) == 0) {
3088 /* do nothing, checksum okay */
3089 } else if (in_cksum(m, datalen)) {
3090 pimstat.pims_rcv_badsum++;
3091 if (mrtdebug & DEBUG_PIM)
3092 log(LOG_DEBUG, "pim_input: invalid checksum");
3094 return(IPPROTO_DONE);
3097 /* PIM version check */
3098 if (PIM_VT_V(pim->pim_vt) < PIM_VERSION) {
3099 pimstat.pims_rcv_badversion++;
3100 log(LOG_ERR, "pim_input: incorrect version %d, expecting %d\n",
3101 PIM_VT_V(pim->pim_vt), PIM_VERSION);
3103 return(IPPROTO_DONE);
3106 /* restore mbuf back to the outer IP */
3107 m->m_data -= iphlen;
3110 if (PIM_VT_T(pim->pim_vt) == PIM_REGISTER) {
3112 * Since this is a REGISTER, we'll make a copy of the register
3113 * headers ip + pim + u_int32 + encap_ip, to be passed up to the
3116 struct sockaddr_in dst = { sizeof(dst), AF_INET };
3118 struct ip *encap_ip;
3121 if ((reg_vif_num >= numvifs) || (reg_vif_num == VIFI_INVALID)) {
3122 if (mrtdebug & DEBUG_PIM)
3124 "pim_input: register vif not set: %d\n", reg_vif_num);
3126 return(IPPROTO_DONE);
3132 if (datalen < PIM_REG_MINLEN) {
3133 pimstat.pims_rcv_tooshort++;
3134 pimstat.pims_rcv_badregisters++;
3136 "pim_input: register packet size too small %d from %lx\n",
3137 datalen, (u_long)ip->ip_src.s_addr);
3139 return(IPPROTO_DONE);
3142 reghdr = (u_int32_t *)(pim + 1);
3143 encap_ip = (struct ip *)(reghdr + 1);
3145 if (mrtdebug & DEBUG_PIM) {
3147 "pim_input[register], encap_ip: %lx -> %lx, encap_ip len %d\n",
3148 (u_long)ntohl(encap_ip->ip_src.s_addr),
3149 (u_long)ntohl(encap_ip->ip_dst.s_addr),
3150 ntohs(encap_ip->ip_len));
3153 /* verify the version number of the inner packet */
3154 if (encap_ip->ip_v != IPVERSION) {
3155 pimstat.pims_rcv_badregisters++;
3156 if (mrtdebug & DEBUG_PIM) {
3157 log(LOG_DEBUG, "pim_input: invalid IP version (%d) "
3158 "of the inner packet\n", encap_ip->ip_v);
3161 return(IPPROTO_DONE);
3164 /* verify the inner packet is destined to a mcast group */
3165 if (!IN_MULTICAST(ntohl(encap_ip->ip_dst.s_addr))) {
3166 pimstat.pims_rcv_badregisters++;
3167 if (mrtdebug & DEBUG_PIM)
3169 "pim_input: inner packet of register is not "
3171 (u_long)ntohl(encap_ip->ip_dst.s_addr));
3173 return(IPPROTO_DONE);
3176 /* If a NULL_REGISTER, pass it to the daemon */
3177 if ((ntohl(*reghdr) & PIM_NULL_REGISTER))
3178 goto pim_input_to_daemon;
3181 * Copy the TOS from the outer IP header to the inner IP header.
3183 if (encap_ip->ip_tos != ip_tos) {
3184 /* Outer TOS -> inner TOS */
3185 encap_ip->ip_tos = ip_tos;
3186 /* Recompute the inner header checksum. Sigh... */
3188 /* adjust mbuf to point to the inner IP header */
3189 m->m_data += (iphlen + PIM_MINLEN);
3190 m->m_len -= (iphlen + PIM_MINLEN);
3192 encap_ip->ip_sum = 0;
3193 encap_ip->ip_sum = in_cksum(m, encap_ip->ip_hl << 2);
3195 /* restore mbuf to point back to the outer IP header */
3196 m->m_data -= (iphlen + PIM_MINLEN);
3197 m->m_len += (iphlen + PIM_MINLEN);
3201 * Decapsulate the inner IP packet and loopback to forward it
3202 * as a normal multicast packet. Also, make a copy of the
3203 * outer_iphdr + pimhdr + reghdr + encap_iphdr
3204 * to pass to the daemon later, so it can take the appropriate
3205 * actions (e.g., send back PIM_REGISTER_STOP).
3206 * XXX: here m->m_data points to the outer IP header.
3208 mcp = m_copy(m, 0, iphlen + PIM_REG_MINLEN);
3211 "pim_input: pim register: could not copy register head\n");
3213 return(IPPROTO_DONE);
3216 /* Keep statistics */
3217 /* XXX: registers_bytes include only the encap. mcast pkt */
3218 pimstat.pims_rcv_registers_msgs++;
3219 pimstat.pims_rcv_registers_bytes += ntohs(encap_ip->ip_len);
3222 * forward the inner ip packet; point m_data at the inner ip.
3224 m_adj(m, iphlen + PIM_MINLEN);
3226 if (mrtdebug & DEBUG_PIM) {
3228 "pim_input: forwarding decapsulated register: "
3229 "src %lx, dst %lx, vif %d\n",
3230 (u_long)ntohl(encap_ip->ip_src.s_addr),
3231 (u_long)ntohl(encap_ip->ip_dst.s_addr),
3234 if_simloop(viftable[reg_vif_num].v_ifp, m, dst.sin_family, 0);
3236 /* prepare the register head to send to the mrouting daemon */
3240 pim_input_to_daemon:
3242 * Pass the PIM message up to the daemon; if it is a Register message,
3243 * pass the 'head' only up to the daemon. This includes the
3244 * outer IP header, PIM header, PIM-Register header and the
3246 * XXX: the outer IP header pkt size of a Register is not adjust to
3247 * reflect the fact that the inner multicast data is truncated.
3251 rip_input(mp, offp, proto);
3252 return(IPPROTO_DONE);
3257 ip_mroute_modevent(module_t mod, int type, void *unused)
3261 lwkt_gettoken(&mroute_token);
3262 /* XXX Protect against multiple loading */
3263 ip_mcast_src = X_ip_mcast_src;
3264 ip_mforward = X_ip_mforward;
3265 ip_mrouter_done = X_ip_mrouter_done;
3266 ip_mrouter_get = X_ip_mrouter_get;
3267 ip_mrouter_set = X_ip_mrouter_set;
3268 ip_rsvp_force_done = X_ip_rsvp_force_done;
3269 ip_rsvp_vif = X_ip_rsvp_vif;
3270 ipip_input = X_ipip_input;
3271 legal_vif_num = X_legal_vif_num;
3272 mrt_ioctl = X_mrt_ioctl;
3273 rsvp_input_p = X_rsvp_input;
3274 lwkt_reltoken(&mroute_token);
3281 lwkt_gettoken(&mroute_token);
3282 ip_mcast_src = NULL;
3284 ip_mrouter_done = NULL;
3285 ip_mrouter_get = NULL;
3286 ip_mrouter_set = NULL;
3287 ip_rsvp_force_done = NULL;
3290 legal_vif_num = NULL;
3292 rsvp_input_p = NULL;
3293 lwkt_reltoken(&mroute_token);
3299 static moduledata_t ip_mroutemod = {
3304 DECLARE_MODULE(ip_mroute, ip_mroutemod, SI_SUB_PSEUDO, SI_ORDER_ANY);