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.15 2004/09/16 23:30:10 joerg Exp $
24 #include "opt_mrouting.h"
25 #include "opt_random_ip_id.h"
31 #include <sys/param.h>
32 #include <sys/kernel.h>
33 #include <sys/malloc.h>
35 #include <sys/protosw.h>
36 #include <sys/socket.h>
37 #include <sys/socketvar.h>
38 #include <sys/sockio.h>
39 #include <sys/sysctl.h>
40 #include <sys/syslog.h>
41 #include <sys/systm.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>
61 #include <netinet/udp.h>
64 * Control debugging code for rsvp and multicast routing code.
65 * Can only set them with the debugger.
67 static u_int rsvpdebug; /* non-zero enables debugging */
69 static u_int mrtdebug; /* any set of the flags below */
71 #define DEBUG_MFC 0x02
72 #define DEBUG_FORWARD 0x04
73 #define DEBUG_EXPIRE 0x08
74 #define DEBUG_XMIT 0x10
75 #define DEBUG_PIM 0x20
77 #define VIFI_INVALID ((vifi_t) -1)
79 #define M_HASCL(m) ((m)->m_flags & M_EXT)
81 static MALLOC_DEFINE(M_MRTABLE, "mroutetbl", "multicast routing tables");
83 static struct mrtstat mrtstat;
84 SYSCTL_STRUCT(_net_inet_ip, OID_AUTO, mrtstat, CTLFLAG_RW,
86 "Multicast Routing Statistics (struct mrtstat, netinet/ip_mroute.h)");
88 static struct mfc *mfctable[MFCTBLSIZ];
89 SYSCTL_OPAQUE(_net_inet_ip, OID_AUTO, mfctable, CTLFLAG_RD,
90 &mfctable, sizeof(mfctable), "S,*mfc[MFCTBLSIZ]",
91 "Multicast Forwarding Table (struct *mfc[MFCTBLSIZ], netinet/ip_mroute.h)");
93 static struct vif viftable[MAXVIFS];
94 SYSCTL_OPAQUE(_net_inet_ip, OID_AUTO, viftable, CTLFLAG_RD,
95 &viftable, sizeof(viftable), "S,vif[MAXVIFS]",
96 "Multicast Virtual Interfaces (struct vif[MAXVIFS], netinet/ip_mroute.h)");
98 static u_char nexpire[MFCTBLSIZ];
100 static struct callout expire_upcalls_ch;
101 static struct callout tbf_reprocess_q_ch;
102 #define EXPIRE_TIMEOUT (hz / 4) /* 4x / second */
103 #define UPCALL_EXPIRE 6 /* number of timeouts */
106 * Define the token bucket filter structures
107 * tbftable -> each vif has one of these for storing info
110 static struct tbf tbftable[MAXVIFS];
111 #define TBF_REPROCESS (hz / 100) /* 100x / second */
114 * 'Interfaces' associated with decapsulator (so we can tell
115 * packets that went through it from ones that get reflected
116 * by a broken gateway). These interfaces are never linked into
117 * the system ifnet list & no routes point to them. I.e., packets
118 * can't be sent this way. They only exist as a placeholder for
119 * multicast source verification.
121 static struct ifnet multicast_decap_if[MAXVIFS];
124 #define ENCAP_PROTO IPPROTO_IPIP /* 4 */
126 /* prototype IP hdr for encapsulated packets */
127 static struct ip multicast_encap_iphdr = {
128 #if BYTE_ORDER == LITTLE_ENDIAN
129 sizeof(struct ip) >> 2, IPVERSION,
131 IPVERSION, sizeof(struct ip) >> 2,
134 sizeof(struct ip), /* total length */
137 ENCAP_TTL, ENCAP_PROTO,
142 * Bandwidth meter variables and constants
144 static MALLOC_DEFINE(M_BWMETER, "bwmeter", "multicast upcall bw meters");
146 * Pending timeouts are stored in a hash table, the key being the
147 * expiration time. Periodically, the entries are analysed and processed.
149 #define BW_METER_BUCKETS 1024
150 static struct bw_meter *bw_meter_timers[BW_METER_BUCKETS];
151 static struct callout bw_meter_ch;
152 #define BW_METER_PERIOD (hz) /* periodical handling of bw meters */
155 * Pending upcalls are stored in a vector which is flushed when
156 * full, or periodically
158 static struct bw_upcall bw_upcalls[BW_UPCALLS_MAX];
159 static u_int bw_upcalls_n; /* # of pending upcalls */
160 static struct callout bw_upcalls_ch;
161 #define BW_UPCALLS_PERIOD (hz) /* periodical flush of bw upcalls */
164 static struct pimstat pimstat;
165 SYSCTL_STRUCT(_net_inet_pim, PIMCTL_STATS, stats, CTLFLAG_RD,
167 "PIM Statistics (struct pimstat, netinet/pim_var.h)");
170 * Note: the PIM Register encapsulation adds the following in front of a
173 * struct pim_encap_hdr {
175 * struct pim_encap_pimhdr pim;
180 struct pim_encap_pimhdr {
185 static struct ip pim_encap_iphdr = {
186 #if BYTE_ORDER == LITTLE_ENDIAN
187 sizeof(struct ip) >> 2,
191 sizeof(struct ip) >> 2,
194 sizeof(struct ip), /* total length */
202 static struct pim_encap_pimhdr pim_encap_pimhdr = {
204 PIM_MAKE_VT(PIM_VERSION, PIM_REGISTER), /* PIM vers and message type */
211 static struct ifnet multicast_register_if;
212 static vifi_t reg_vif_num = VIFI_INVALID;
218 static vifi_t numvifs;
219 static int have_encap_tunnel;
222 * one-back cache used by ipip_input to locate a tunnel's vif
223 * given a datagram's src ip address.
225 static u_long last_encap_src;
226 static struct vif *last_encap_vif;
228 static u_long X_ip_mcast_src(int vifi);
229 static int X_ip_mforward(struct ip *ip, struct ifnet *ifp,
230 struct mbuf *m, struct ip_moptions *imo);
231 static int X_ip_mrouter_done(void);
232 static int X_ip_mrouter_get(struct socket *so, struct sockopt *m);
233 static int X_ip_mrouter_set(struct socket *so, struct sockopt *m);
234 static int X_legal_vif_num(int vif);
235 static int X_mrt_ioctl(int cmd, caddr_t data);
237 static int get_sg_cnt(struct sioc_sg_req *);
238 static int get_vif_cnt(struct sioc_vif_req *);
239 static int ip_mrouter_init(struct socket *, int);
240 static int add_vif(struct vifctl *);
241 static int del_vif(vifi_t);
242 static int add_mfc(struct mfcctl2 *);
243 static int del_mfc(struct mfcctl2 *);
244 static int set_api_config(uint32_t *); /* chose API capabilities */
245 static int socket_send(struct socket *, struct mbuf *, struct sockaddr_in *);
246 static int set_assert(int);
247 static void expire_upcalls(void *);
248 static int ip_mdq(struct mbuf *, struct ifnet *, struct mfc *, vifi_t);
249 static void phyint_send(struct ip *, struct vif *, struct mbuf *);
250 static void encap_send(struct ip *, struct vif *, struct mbuf *);
251 static void tbf_control(struct vif *, struct mbuf *, struct ip *, u_long);
252 static void tbf_queue(struct vif *, struct mbuf *);
253 static void tbf_process_q(struct vif *);
254 static void tbf_reprocess_q(void *);
255 static int tbf_dq_sel(struct vif *, struct ip *);
256 static void tbf_send_packet(struct vif *, struct mbuf *);
257 static void tbf_update_tokens(struct vif *);
258 static int priority(struct vif *, struct ip *);
261 * Bandwidth monitoring
263 static void free_bw_list(struct bw_meter *list);
264 static int add_bw_upcall(struct bw_upcall *);
265 static int del_bw_upcall(struct bw_upcall *);
266 static void bw_meter_receive_packet(struct bw_meter *x, int plen,
267 struct timeval *nowp);
268 static void bw_meter_prepare_upcall(struct bw_meter *x, struct timeval *nowp);
269 static void bw_upcalls_send(void);
270 static void schedule_bw_meter(struct bw_meter *x, struct timeval *nowp);
271 static void unschedule_bw_meter(struct bw_meter *x);
272 static void bw_meter_process(void);
273 static void expire_bw_upcalls_send(void *);
274 static void expire_bw_meter_process(void *);
277 static int pim_register_send(struct ip *, struct vif *,
278 struct mbuf *, struct mfc *);
279 static int pim_register_send_rp(struct ip *, struct vif *,
280 struct mbuf *, struct mfc *);
281 static int pim_register_send_upcall(struct ip *, struct vif *,
282 struct mbuf *, struct mfc *);
283 static struct mbuf *pim_register_prepare(struct ip *, struct mbuf *);
287 * whether or not special PIM assert processing is enabled.
289 static int pim_assert;
291 * Rate limit for assert notification messages, in usec
293 #define ASSERT_MSG_TIME 3000000
296 * Kernel multicast routing API capabilities and setup.
297 * If more API capabilities are added to the kernel, they should be
298 * recorded in `mrt_api_support'.
300 static const uint32_t mrt_api_support = (MRT_MFC_FLAGS_DISABLE_WRONGVIF |
301 MRT_MFC_FLAGS_BORDER_VIF |
304 static uint32_t mrt_api_config = 0;
307 * Hash function for a source, group entry
309 #define MFCHASH(a, g) MFCHASHMOD(((a) >> 20) ^ ((a) >> 10) ^ (a) ^ \
310 ((g) >> 20) ^ ((g) >> 10) ^ (g))
313 * Find a route for a given origin IP address and Multicast group address
314 * Type of service parameter to be added in the future!!!
315 * Statistics are updated by the caller if needed
316 * (mrtstat.mrts_mfc_lookups and mrtstat.mrts_mfc_misses)
319 mfc_find(in_addr_t o, in_addr_t g)
323 for (rt = mfctable[MFCHASH(o,g)]; rt; rt = rt->mfc_next)
324 if ((rt->mfc_origin.s_addr == o) &&
325 (rt->mfc_mcastgrp.s_addr == g) && (rt->mfc_stall == NULL))
331 * Macros to compute elapsed time efficiently
332 * Borrowed from Van Jacobson's scheduling code
334 #define TV_DELTA(a, b, delta) { \
336 delta = (a).tv_usec - (b).tv_usec; \
337 if ((xxs = (a).tv_sec - (b).tv_sec)) { \
346 delta += (1000000 * xxs); \
351 #define TV_LT(a, b) (((a).tv_usec < (b).tv_usec && \
352 (a).tv_sec <= (b).tv_sec) || (a).tv_sec < (b).tv_sec)
355 * Handle MRT setsockopt commands to modify the multicast routing tables.
358 X_ip_mrouter_set(struct socket *so, struct sockopt *sopt)
364 struct bw_upcall bw_upcall;
367 if (so != ip_mrouter && sopt->sopt_name != MRT_INIT)
371 switch (sopt->sopt_name) {
373 error = sooptcopyin(sopt, &optval, sizeof optval, sizeof optval);
376 error = ip_mrouter_init(so, optval);
380 error = ip_mrouter_done();
384 error = sooptcopyin(sopt, &vifc, sizeof vifc, sizeof vifc);
387 error = add_vif(&vifc);
391 error = sooptcopyin(sopt, &vifi, sizeof vifi, sizeof vifi);
394 error = del_vif(vifi);
400 * select data size depending on API version.
402 if (sopt->sopt_name == MRT_ADD_MFC &&
403 mrt_api_config & MRT_API_FLAGS_ALL) {
404 error = sooptcopyin(sopt, &mfc, sizeof(struct mfcctl2),
405 sizeof(struct mfcctl2));
407 error = sooptcopyin(sopt, &mfc, sizeof(struct mfcctl),
408 sizeof(struct mfcctl));
409 bzero((caddr_t)&mfc + sizeof(struct mfcctl),
410 sizeof(mfc) - sizeof(struct mfcctl));
414 if (sopt->sopt_name == MRT_ADD_MFC)
415 error = add_mfc(&mfc);
417 error = del_mfc(&mfc);
421 error = sooptcopyin(sopt, &optval, sizeof optval, sizeof optval);
428 error = sooptcopyin(sopt, &i, sizeof i, sizeof i);
430 error = set_api_config(&i);
432 error = sooptcopyout(sopt, &i, sizeof i);
435 case MRT_ADD_BW_UPCALL:
436 case MRT_DEL_BW_UPCALL:
437 error = sooptcopyin(sopt, &bw_upcall, sizeof bw_upcall,
441 if (sopt->sopt_name == MRT_ADD_BW_UPCALL)
442 error = add_bw_upcall(&bw_upcall);
444 error = del_bw_upcall(&bw_upcall);
455 * Handle MRT getsockopt commands
458 X_ip_mrouter_get(struct socket *so, struct sockopt *sopt)
461 static int version = 0x0305; /* !!! why is this here? XXX */
463 switch (sopt->sopt_name) {
465 error = sooptcopyout(sopt, &version, sizeof version);
469 error = sooptcopyout(sopt, &pim_assert, sizeof pim_assert);
472 case MRT_API_SUPPORT:
473 error = sooptcopyout(sopt, &mrt_api_support, sizeof mrt_api_support);
477 error = sooptcopyout(sopt, &mrt_api_config, sizeof mrt_api_config);
488 * Handle ioctl commands to obtain information from the cache
491 X_mrt_ioctl(int cmd, caddr_t data)
497 error = get_vif_cnt((struct sioc_vif_req *)data);
501 error = get_sg_cnt((struct sioc_sg_req *)data);
512 * returns the packet, byte, rpf-failure count for the source group provided
515 get_sg_cnt(struct sioc_sg_req *req)
521 rt = mfc_find(req->src.s_addr, req->grp.s_addr);
524 req->pktcnt = req->bytecnt = req->wrong_if = 0xffffffff;
525 return EADDRNOTAVAIL;
527 req->pktcnt = rt->mfc_pkt_cnt;
528 req->bytecnt = rt->mfc_byte_cnt;
529 req->wrong_if = rt->mfc_wrong_if;
534 * returns the input and output packet and byte counts on the vif provided
537 get_vif_cnt(struct sioc_vif_req *req)
539 vifi_t vifi = req->vifi;
544 req->icount = viftable[vifi].v_pkt_in;
545 req->ocount = viftable[vifi].v_pkt_out;
546 req->ibytes = viftable[vifi].v_bytes_in;
547 req->obytes = viftable[vifi].v_bytes_out;
553 * Enable multicast routing
556 ip_mrouter_init(struct socket *so, int version)
559 log(LOG_DEBUG, "ip_mrouter_init: so_type = %d, pr_protocol = %d\n",
560 so->so_type, so->so_proto->pr_protocol);
562 if (so->so_type != SOCK_RAW || so->so_proto->pr_protocol != IPPROTO_IGMP)
568 if (ip_mrouter != NULL)
573 bzero((caddr_t)mfctable, sizeof(mfctable));
574 bzero((caddr_t)nexpire, sizeof(nexpire));
578 bzero((caddr_t)bw_meter_timers, sizeof(bw_meter_timers));
580 callout_init(&expire_upcalls_ch);
581 callout_init(&bw_upcalls_ch);
582 callout_init(&bw_meter_ch);
583 callout_init(&tbf_reprocess_q_ch);
585 callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT, expire_upcalls, NULL);
586 callout_reset(&bw_upcalls_ch, BW_UPCALLS_PERIOD,
587 expire_bw_upcalls_send, NULL);
588 callout_reset(&bw_meter_ch, BW_METER_PERIOD, expire_bw_meter_process, NULL);
593 log(LOG_DEBUG, "ip_mrouter_init\n");
599 * Disable multicast routing
602 X_ip_mrouter_done(void)
615 * For each phyint in use, disable promiscuous reception of all IP
618 for (vifi = 0; vifi < numvifs; vifi++) {
619 if (viftable[vifi].v_lcl_addr.s_addr != 0 &&
620 !(viftable[vifi].v_flags & (VIFF_TUNNEL | VIFF_REGISTER))) {
621 struct sockaddr_in *so = (struct sockaddr_in *)&(ifr.ifr_addr);
623 so->sin_len = sizeof(struct sockaddr_in);
624 so->sin_family = AF_INET;
625 so->sin_addr.s_addr = INADDR_ANY;
626 ifp = viftable[vifi].v_ifp;
630 bzero((caddr_t)tbftable, sizeof(tbftable));
631 bzero((caddr_t)viftable, sizeof(viftable));
635 callout_stop(&expire_upcalls_ch);
639 callout_stop(&bw_upcalls_ch);
640 callout_stop(&bw_meter_ch);
641 callout_stop(&tbf_reprocess_q_ch);
644 * Free all multicast forwarding cache entries.
646 for (i = 0; i < MFCTBLSIZ; i++) {
647 for (rt = mfctable[i]; rt != NULL; ) {
648 struct mfc *nr = rt->mfc_next;
650 for (rte = rt->mfc_stall; rte != NULL; ) {
651 struct rtdetq *n = rte->next;
654 free(rte, M_MRTABLE);
657 free_bw_list(rt->mfc_bw_meter);
663 bzero((caddr_t)mfctable, sizeof(mfctable));
665 bzero(bw_meter_timers, sizeof(bw_meter_timers));
668 * Reset de-encapsulation cache
670 last_encap_src = INADDR_ANY;
671 last_encap_vif = NULL;
673 reg_vif_num = VIFI_INVALID;
675 have_encap_tunnel = 0;
682 log(LOG_DEBUG, "ip_mrouter_done\n");
688 * Set PIM assert processing global
693 if ((i != 1) && (i != 0))
702 * Configure API capabilities
705 set_api_config(uint32_t *apival)
710 * We can set the API capabilities only if it is the first operation
711 * after MRT_INIT. I.e.:
712 * - there are no vifs installed
713 * - pim_assert is not enabled
714 * - the MFC table is empty
724 for (i = 0; i < MFCTBLSIZ; i++) {
725 if (mfctable[i] != NULL) {
731 mrt_api_config = *apival & mrt_api_support;
732 *apival = mrt_api_config;
738 * Add a vif to the vif table
741 add_vif(struct vifctl *vifcp)
743 struct vif *vifp = viftable + vifcp->vifc_vifi;
744 struct sockaddr_in sin = {sizeof sin, AF_INET};
748 struct tbf *v_tbf = tbftable + vifcp->vifc_vifi;
750 if (vifcp->vifc_vifi >= MAXVIFS)
752 if (vifp->v_lcl_addr.s_addr != INADDR_ANY)
754 if (vifcp->vifc_lcl_addr.s_addr == INADDR_ANY)
755 return EADDRNOTAVAIL;
757 /* Find the interface with an address in AF_INET family */
759 if (vifcp->vifc_flags & VIFF_REGISTER) {
761 * XXX: Because VIFF_REGISTER does not really need a valid
762 * local interface (e.g. it could be 127.0.0.2), we don't
769 sin.sin_addr = vifcp->vifc_lcl_addr;
770 ifa = ifa_ifwithaddr((struct sockaddr *)&sin);
772 return EADDRNOTAVAIL;
776 if (vifcp->vifc_flags & VIFF_TUNNEL) {
777 if ((vifcp->vifc_flags & VIFF_SRCRT) == 0) {
779 * An encapsulating tunnel is wanted. Tell ipip_input() to
780 * start paying attention to encapsulated packets.
782 if (have_encap_tunnel == 0) {
783 have_encap_tunnel = 1;
784 for (s = 0; s < MAXVIFS; ++s) {
785 if_initname(&multicast_decap_if[s], "mdecap", s);
789 * Set interface to fake encapsulator interface
791 ifp = &multicast_decap_if[vifcp->vifc_vifi];
793 * Prepare cached route entry
795 bzero(&vifp->v_route, sizeof(vifp->v_route));
797 log(LOG_ERR, "source routed tunnels not supported\n");
801 } else if (vifcp->vifc_flags & VIFF_REGISTER) {
802 ifp = &multicast_register_if;
804 log(LOG_DEBUG, "Adding a register vif, ifp: %p\n",
805 (void *)&multicast_register_if);
806 if (reg_vif_num == VIFI_INVALID) {
807 if_initname(&multicast_register_if, "register_vif", 0);
808 multicast_register_if.if_flags = IFF_LOOPBACK;
809 bzero(&vifp->v_route, sizeof(vifp->v_route));
810 reg_vif_num = vifcp->vifc_vifi;
813 } else { /* Make sure the interface supports multicast */
814 if ((ifp->if_flags & IFF_MULTICAST) == 0)
817 /* Enable promiscuous reception of all IP multicasts from the if */
819 error = if_allmulti(ifp, 1);
826 /* define parameters for the tbf structure */
828 GET_TIME(vifp->v_tbf->tbf_last_pkt_t);
829 vifp->v_tbf->tbf_n_tok = 0;
830 vifp->v_tbf->tbf_q_len = 0;
831 vifp->v_tbf->tbf_max_q_len = MAXQSIZE;
832 vifp->v_tbf->tbf_q = vifp->v_tbf->tbf_t = NULL;
834 vifp->v_flags = vifcp->vifc_flags;
835 vifp->v_threshold = vifcp->vifc_threshold;
836 vifp->v_lcl_addr = vifcp->vifc_lcl_addr;
837 vifp->v_rmt_addr = vifcp->vifc_rmt_addr;
839 /* scaling up here allows division by 1024 in critical code */
840 vifp->v_rate_limit= vifcp->vifc_rate_limit * 1024 / 1000;
842 vifp->v_rsvpd = NULL;
843 /* initialize per vif pkt counters */
846 vifp->v_bytes_in = 0;
847 vifp->v_bytes_out = 0;
850 /* Adjust numvifs up if the vifi is higher than numvifs */
851 if (numvifs <= vifcp->vifc_vifi) numvifs = vifcp->vifc_vifi + 1;
854 log(LOG_DEBUG, "add_vif #%d, lcladdr %lx, %s %lx, thresh %x, rate %d\n",
856 (u_long)ntohl(vifcp->vifc_lcl_addr.s_addr),
857 (vifcp->vifc_flags & VIFF_TUNNEL) ? "rmtaddr" : "mask",
858 (u_long)ntohl(vifcp->vifc_rmt_addr.s_addr),
859 vifcp->vifc_threshold,
860 vifcp->vifc_rate_limit);
866 * Delete a vif from the vif table
876 vifp = &viftable[vifi];
877 if (vifp->v_lcl_addr.s_addr == INADDR_ANY)
878 return EADDRNOTAVAIL;
882 if (!(vifp->v_flags & (VIFF_TUNNEL | VIFF_REGISTER)))
883 if_allmulti(vifp->v_ifp, 0);
885 if (vifp == last_encap_vif) {
886 last_encap_vif = NULL;
887 last_encap_src = INADDR_ANY;
891 * Free packets queued at the interface
893 while (vifp->v_tbf->tbf_q) {
894 struct mbuf *m = vifp->v_tbf->tbf_q;
896 vifp->v_tbf->tbf_q = m->m_nextpkt;
901 if (vifp->v_flags & VIFF_REGISTER)
902 reg_vif_num = VIFI_INVALID;
905 bzero((caddr_t)vifp->v_tbf, sizeof(*(vifp->v_tbf)));
906 bzero((caddr_t)vifp, sizeof (*vifp));
909 log(LOG_DEBUG, "del_vif %d, numvifs %d\n", vifi, numvifs);
911 /* Adjust numvifs down */
912 for (vifi = numvifs; vifi > 0; vifi--)
913 if (viftable[vifi-1].v_lcl_addr.s_addr != INADDR_ANY)
923 * update an mfc entry without resetting counters and S,G addresses.
926 update_mfc_params(struct mfc *rt, struct mfcctl2 *mfccp)
930 rt->mfc_parent = mfccp->mfcc_parent;
931 for (i = 0; i < numvifs; i++) {
932 rt->mfc_ttls[i] = mfccp->mfcc_ttls[i];
933 rt->mfc_flags[i] = mfccp->mfcc_flags[i] & mrt_api_config &
936 /* set the RP address */
937 if (mrt_api_config & MRT_MFC_RP)
938 rt->mfc_rp = mfccp->mfcc_rp;
940 rt->mfc_rp.s_addr = INADDR_ANY;
944 * fully initialize an mfc entry from the parameter.
947 init_mfc_params(struct mfc *rt, struct mfcctl2 *mfccp)
949 rt->mfc_origin = mfccp->mfcc_origin;
950 rt->mfc_mcastgrp = mfccp->mfcc_mcastgrp;
952 update_mfc_params(rt, mfccp);
954 /* initialize pkt counters per src-grp */
956 rt->mfc_byte_cnt = 0;
957 rt->mfc_wrong_if = 0;
958 rt->mfc_last_assert.tv_sec = rt->mfc_last_assert.tv_usec = 0;
966 add_mfc(struct mfcctl2 *mfccp)
974 rt = mfc_find(mfccp->mfcc_origin.s_addr, mfccp->mfcc_mcastgrp.s_addr);
976 /* If an entry already exists, just update the fields */
978 if (mrtdebug & DEBUG_MFC)
979 log(LOG_DEBUG,"add_mfc update o %lx g %lx p %x\n",
980 (u_long)ntohl(mfccp->mfcc_origin.s_addr),
981 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
985 update_mfc_params(rt, mfccp);
991 * Find the entry for which the upcall was made and update
994 hash = MFCHASH(mfccp->mfcc_origin.s_addr, mfccp->mfcc_mcastgrp.s_addr);
995 for (rt = mfctable[hash], nstl = 0; rt; rt = rt->mfc_next) {
997 if ((rt->mfc_origin.s_addr == mfccp->mfcc_origin.s_addr) &&
998 (rt->mfc_mcastgrp.s_addr == mfccp->mfcc_mcastgrp.s_addr) &&
999 (rt->mfc_stall != NULL)) {
1002 log(LOG_ERR, "add_mfc %s o %lx g %lx p %x dbx %p\n",
1003 "multiple kernel entries",
1004 (u_long)ntohl(mfccp->mfcc_origin.s_addr),
1005 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
1006 mfccp->mfcc_parent, (void *)rt->mfc_stall);
1008 if (mrtdebug & DEBUG_MFC)
1009 log(LOG_DEBUG,"add_mfc o %lx g %lx p %x dbg %p\n",
1010 (u_long)ntohl(mfccp->mfcc_origin.s_addr),
1011 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
1012 mfccp->mfcc_parent, (void *)rt->mfc_stall);
1014 init_mfc_params(rt, mfccp);
1016 rt->mfc_expire = 0; /* Don't clean this guy up */
1019 /* free packets Qed at the end of this entry */
1020 for (rte = rt->mfc_stall; rte != NULL; ) {
1021 struct rtdetq *n = rte->next;
1023 ip_mdq(rte->m, rte->ifp, rt, -1);
1025 free(rte, M_MRTABLE);
1028 rt->mfc_stall = NULL;
1033 * It is possible that an entry is being inserted without an upcall
1036 if (mrtdebug & DEBUG_MFC)
1037 log(LOG_DEBUG,"add_mfc no upcall h %lu o %lx g %lx p %x\n",
1038 hash, (u_long)ntohl(mfccp->mfcc_origin.s_addr),
1039 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
1040 mfccp->mfcc_parent);
1042 for (rt = mfctable[hash]; rt != NULL; rt = rt->mfc_next) {
1043 if ((rt->mfc_origin.s_addr == mfccp->mfcc_origin.s_addr) &&
1044 (rt->mfc_mcastgrp.s_addr == mfccp->mfcc_mcastgrp.s_addr)) {
1045 init_mfc_params(rt, mfccp);
1052 if (rt == NULL) { /* no upcall, so make a new entry */
1053 rt = malloc(sizeof(*rt), M_MRTABLE, M_INTWAIT | M_NULLOK);
1059 init_mfc_params(rt, mfccp);
1061 rt->mfc_stall = NULL;
1063 rt->mfc_bw_meter = NULL;
1064 /* insert new entry at head of hash chain */
1065 rt->mfc_next = mfctable[hash];
1066 mfctable[hash] = rt;
1074 * Delete an mfc entry
1077 del_mfc(struct mfcctl2 *mfccp)
1079 struct in_addr origin;
1080 struct in_addr mcastgrp;
1085 struct bw_meter *list;
1087 origin = mfccp->mfcc_origin;
1088 mcastgrp = mfccp->mfcc_mcastgrp;
1090 if (mrtdebug & DEBUG_MFC)
1091 log(LOG_DEBUG,"del_mfc orig %lx mcastgrp %lx\n",
1092 (u_long)ntohl(origin.s_addr), (u_long)ntohl(mcastgrp.s_addr));
1096 hash = MFCHASH(origin.s_addr, mcastgrp.s_addr);
1097 for (nptr = &mfctable[hash]; (rt = *nptr) != NULL; nptr = &rt->mfc_next)
1098 if (origin.s_addr == rt->mfc_origin.s_addr &&
1099 mcastgrp.s_addr == rt->mfc_mcastgrp.s_addr &&
1100 rt->mfc_stall == NULL)
1104 return EADDRNOTAVAIL;
1107 *nptr = rt->mfc_next;
1110 * free the bw_meter entries
1112 list = rt->mfc_bw_meter;
1113 rt->mfc_bw_meter = NULL;
1115 free(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 (sbappendaddr(&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)
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 printf("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 printf("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 printf("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
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 */
1225 return ip_mdq(m, ifp, rt, -1);
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 = malloc((sizeof *rte), M_MRTABLE, M_INTWAIT | M_NULLOK);
1256 mb0 = m_copypacket(m, MB_DONTWAIT);
1257 if (mb0 && (M_HASCL(mb0) || mb0->m_len < hlen))
1258 mb0 = m_pullup(mb0, hlen);
1260 free(rte, M_MRTABLE);
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 = malloc(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 free(rt, M_MRTABLE);
1318 free(rte, M_MRTABLE);
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 free(rte, M_MRTABLE);
1366 /* Add this entry to the end of the queue */
1381 * Clean up the cache entry if upcall is not serviced
1384 expire_upcalls(void *unused)
1387 struct mfc *mfc, **nptr;
1392 for (i = 0; i < MFCTBLSIZ; i++) {
1393 if (nexpire[i] == 0)
1395 nptr = &mfctable[i];
1396 for (mfc = *nptr; mfc != NULL; mfc = *nptr) {
1398 * Skip real cache entries
1399 * Make sure it wasn't marked to not expire (shouldn't happen)
1402 if (mfc->mfc_stall != NULL && mfc->mfc_expire != 0 &&
1403 --mfc->mfc_expire == 0) {
1404 if (mrtdebug & DEBUG_EXPIRE)
1405 log(LOG_DEBUG, "expire_upcalls: expiring (%lx %lx)\n",
1406 (u_long)ntohl(mfc->mfc_origin.s_addr),
1407 (u_long)ntohl(mfc->mfc_mcastgrp.s_addr));
1409 * drop all the packets
1410 * free the mbuf with the pkt, if, timing info
1412 for (rte = mfc->mfc_stall; rte; ) {
1413 struct rtdetq *n = rte->next;
1416 free(rte, M_MRTABLE);
1419 ++mrtstat.mrts_cache_cleanups;
1423 * free the bw_meter entries
1425 while (mfc->mfc_bw_meter != NULL) {
1426 struct bw_meter *x = mfc->mfc_bw_meter;
1428 mfc->mfc_bw_meter = x->bm_mfc_next;
1432 *nptr = mfc->mfc_next;
1433 free(mfc, M_MRTABLE);
1435 nptr = &mfc->mfc_next;
1440 callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT, expire_upcalls, NULL);
1444 * Packet forwarding routine once entry in the cache is made
1447 ip_mdq(struct mbuf *m, struct ifnet *ifp, struct mfc *rt, vifi_t xmt_vif)
1449 struct ip *ip = mtod(m, struct ip *);
1451 int plen = ip->ip_len;
1454 * Macro to send packet on vif. Since RSVP packets don't get counted on
1455 * input, they shouldn't get counted on output, so statistics keeping is
1458 #define MC_SEND(ip,vifp,m) { \
1459 if ((vifp)->v_flags & VIFF_TUNNEL) \
1460 encap_send((ip), (vifp), (m)); \
1462 phyint_send((ip), (vifp), (m)); \
1466 * If xmt_vif is not -1, send on only the requested vif.
1468 * (since vifi_t is u_short, -1 becomes MAXUSHORT, which > numvifs.)
1470 if (xmt_vif < numvifs) {
1472 if (viftable[xmt_vif].v_flags & VIFF_REGISTER)
1473 pim_register_send(ip, viftable + xmt_vif, m, rt);
1476 MC_SEND(ip, viftable + xmt_vif, m);
1481 * Don't forward if it didn't arrive from the parent vif for its origin.
1483 vifi = rt->mfc_parent;
1484 if ((vifi >= numvifs) || (viftable[vifi].v_ifp != ifp)) {
1485 /* came in the wrong interface */
1486 if (mrtdebug & DEBUG_FORWARD)
1487 log(LOG_DEBUG, "wrong if: ifp %p vifi %d vififp %p\n",
1488 (void *)ifp, vifi, (void *)viftable[vifi].v_ifp);
1489 ++mrtstat.mrts_wrong_if;
1492 * If we are doing PIM assert processing, send a message
1493 * to the routing daemon.
1495 * XXX: A PIM-SM router needs the WRONGVIF detection so it
1496 * can complete the SPT switch, regardless of the type
1497 * of the iif (broadcast media, GRE tunnel, etc).
1499 if (pim_assert && (vifi < numvifs) && viftable[vifi].v_ifp) {
1504 if (ifp == &multicast_register_if)
1505 pimstat.pims_rcv_registers_wrongiif++;
1508 /* Get vifi for the incoming packet */
1509 for (vifi=0; vifi < numvifs && viftable[vifi].v_ifp != ifp; vifi++)
1511 if (vifi >= numvifs)
1512 return 0; /* The iif is not found: ignore the packet. */
1514 if (rt->mfc_flags[vifi] & MRT_MFC_FLAGS_DISABLE_WRONGVIF)
1515 return 0; /* WRONGVIF disabled: ignore the packet */
1519 TV_DELTA(rt->mfc_last_assert, now, delta);
1521 if (delta > ASSERT_MSG_TIME) {
1522 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
1524 int hlen = ip->ip_hl << 2;
1525 struct mbuf *mm = m_copy(m, 0, hlen);
1527 if (mm && (M_HASCL(mm) || mm->m_len < hlen))
1528 mm = m_pullup(mm, hlen);
1532 rt->mfc_last_assert = now;
1534 im = mtod(mm, struct igmpmsg *);
1535 im->im_msgtype = IGMPMSG_WRONGVIF;
1539 mrtstat.mrts_upcalls++;
1541 k_igmpsrc.sin_addr = im->im_src;
1542 if (socket_send(ip_mrouter, mm, &k_igmpsrc) < 0) {
1544 "ip_mforward: ip_mrouter socket queue full\n");
1545 ++mrtstat.mrts_upq_sockfull;
1553 /* If I sourced this packet, it counts as output, else it was input. */
1554 if (ip->ip_src.s_addr == viftable[vifi].v_lcl_addr.s_addr) {
1555 viftable[vifi].v_pkt_out++;
1556 viftable[vifi].v_bytes_out += plen;
1558 viftable[vifi].v_pkt_in++;
1559 viftable[vifi].v_bytes_in += plen;
1562 rt->mfc_byte_cnt += plen;
1565 * For each vif, decide if a copy of the packet should be forwarded.
1567 * - the ttl exceeds the vif's threshold
1568 * - there are group members downstream on interface
1570 for (vifi = 0; vifi < numvifs; vifi++)
1571 if ((rt->mfc_ttls[vifi] > 0) && (ip->ip_ttl > rt->mfc_ttls[vifi])) {
1572 viftable[vifi].v_pkt_out++;
1573 viftable[vifi].v_bytes_out += plen;
1575 if (viftable[vifi].v_flags & VIFF_REGISTER)
1576 pim_register_send(ip, viftable + vifi, m, rt);
1579 MC_SEND(ip, viftable+vifi, m);
1583 * Perform upcall-related bw measuring.
1585 if (rt->mfc_bw_meter != NULL) {
1590 for (x = rt->mfc_bw_meter; x != NULL; x = x->bm_mfc_next)
1591 bw_meter_receive_packet(x, plen, &now);
1598 * check if a vif number is legal/ok. This is used by ip_output.
1601 X_legal_vif_num(int vif)
1603 return (vif >= 0 && vif < numvifs);
1607 * Return the local address used by this vif
1610 X_ip_mcast_src(int vifi)
1612 if (vifi >= 0 && vifi < numvifs)
1613 return viftable[vifi].v_lcl_addr.s_addr;
1619 phyint_send(struct ip *ip, struct vif *vifp, struct mbuf *m)
1621 struct mbuf *mb_copy;
1622 int hlen = ip->ip_hl << 2;
1625 * Make a new reference to the packet; make sure that
1626 * the IP header is actually copied, not just referenced,
1627 * so that ip_output() only scribbles on the copy.
1629 mb_copy = m_copypacket(m, MB_DONTWAIT);
1630 if (mb_copy && (M_HASCL(mb_copy) || mb_copy->m_len < hlen))
1631 mb_copy = m_pullup(mb_copy, hlen);
1632 if (mb_copy == NULL)
1635 if (vifp->v_rate_limit == 0)
1636 tbf_send_packet(vifp, mb_copy);
1638 tbf_control(vifp, mb_copy, mtod(mb_copy, struct ip *), ip->ip_len);
1642 encap_send(struct ip *ip, struct vif *vifp, struct mbuf *m)
1644 struct mbuf *mb_copy;
1646 int i, len = ip->ip_len;
1648 /* Take care of delayed checksums */
1649 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
1650 in_delayed_cksum(m);
1651 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
1655 * copy the old packet & pullup its IP header into the
1656 * new mbuf so we can modify it. Try to fill the new
1657 * mbuf since if we don't the ethernet driver will.
1659 MGETHDR(mb_copy, MB_DONTWAIT, MT_HEADER);
1660 if (mb_copy == NULL)
1662 mb_copy->m_data += max_linkhdr;
1663 mb_copy->m_len = sizeof(multicast_encap_iphdr);
1665 if ((mb_copy->m_next = m_copypacket(m, MB_DONTWAIT)) == NULL) {
1669 i = MHLEN - M_LEADINGSPACE(mb_copy);
1672 mb_copy = m_pullup(mb_copy, i);
1673 if (mb_copy == NULL)
1675 mb_copy->m_pkthdr.len = len + sizeof(multicast_encap_iphdr);
1678 * fill in the encapsulating IP header.
1680 ip_copy = mtod(mb_copy, struct ip *);
1681 *ip_copy = multicast_encap_iphdr;
1683 ip_copy->ip_id = ip_randomid();
1685 ip_copy->ip_id = htons(ip_id++);
1687 ip_copy->ip_len += len;
1688 ip_copy->ip_src = vifp->v_lcl_addr;
1689 ip_copy->ip_dst = vifp->v_rmt_addr;
1692 * turn the encapsulated IP header back into a valid one.
1694 ip = (struct ip *)((caddr_t)ip_copy + sizeof(multicast_encap_iphdr));
1696 ip->ip_len = htons(ip->ip_len);
1697 ip->ip_off = htons(ip->ip_off);
1699 mb_copy->m_data += sizeof(multicast_encap_iphdr);
1700 ip->ip_sum = in_cksum(mb_copy, ip->ip_hl << 2);
1701 mb_copy->m_data -= sizeof(multicast_encap_iphdr);
1703 if (vifp->v_rate_limit == 0)
1704 tbf_send_packet(vifp, mb_copy);
1706 tbf_control(vifp, mb_copy, ip, ip_copy->ip_len);
1710 * De-encapsulate a packet and feed it back through ip input (this
1711 * routine is called whenever IP gets a packet with proto type
1712 * ENCAP_PROTO and a local destination address).
1714 * This is similar to mroute_encapcheck() + mroute_encap_input() in -current.
1717 X_ipip_input(struct mbuf *m, int off, int proto)
1719 struct ip *ip = mtod(m, struct ip *);
1720 int hlen = ip->ip_hl << 2;
1722 if (!have_encap_tunnel) {
1723 rip_input(m, off, proto);
1727 * dump the packet if it's not to a multicast destination or if
1728 * we don't have an encapsulating tunnel with the source.
1729 * Note: This code assumes that the remote site IP address
1730 * uniquely identifies the tunnel (i.e., that this site has
1731 * at most one tunnel with the remote site).
1733 if (!IN_MULTICAST(ntohl(((struct ip *)((char *)ip+hlen))->ip_dst.s_addr))) {
1734 ++mrtstat.mrts_bad_tunnel;
1738 if (ip->ip_src.s_addr != last_encap_src) {
1739 struct vif *vifp = viftable;
1740 struct vif *vife = vifp + numvifs;
1742 last_encap_src = ip->ip_src.s_addr;
1743 last_encap_vif = NULL;
1744 for ( ; vifp < vife; ++vifp)
1745 if (vifp->v_rmt_addr.s_addr == ip->ip_src.s_addr) {
1746 if ((vifp->v_flags & (VIFF_TUNNEL|VIFF_SRCRT))
1748 last_encap_vif = vifp;
1752 if (last_encap_vif == NULL) {
1753 last_encap_src = INADDR_ANY;
1754 mrtstat.mrts_cant_tunnel++; /*XXX*/
1757 log(LOG_DEBUG, "ip_mforward: no tunnel with %lx\n",
1758 (u_long)ntohl(ip->ip_src.s_addr));
1762 if (hlen > sizeof(struct ip))
1764 m->m_data += sizeof(struct ip);
1765 m->m_len -= sizeof(struct ip);
1766 m->m_pkthdr.len -= sizeof(struct ip);
1767 m->m_pkthdr.rcvif = last_encap_vif->v_ifp;
1769 netisr_queue(NETISR_IP, m);
1773 * Token bucket filter module
1777 tbf_control(struct vif *vifp, struct mbuf *m, struct ip *ip, u_long p_len)
1779 struct tbf *t = vifp->v_tbf;
1781 if (p_len > MAX_BKT_SIZE) { /* drop if packet is too large */
1782 mrtstat.mrts_pkt2large++;
1787 tbf_update_tokens(vifp);
1789 if (t->tbf_q_len == 0) { /* queue empty... */
1790 if (p_len <= t->tbf_n_tok) { /* send packet if enough tokens */
1791 t->tbf_n_tok -= p_len;
1792 tbf_send_packet(vifp, m);
1793 } else { /* no, queue packet and try later */
1795 callout_reset(&tbf_reprocess_q_ch, TBF_REPROCESS,
1796 tbf_reprocess_q, vifp);
1798 } else if (t->tbf_q_len < t->tbf_max_q_len) {
1799 /* finite queue length, so queue pkts and process queue */
1801 tbf_process_q(vifp);
1803 /* queue full, try to dq and queue and process */
1804 if (!tbf_dq_sel(vifp, ip)) {
1805 mrtstat.mrts_q_overflow++;
1809 tbf_process_q(vifp);
1815 * adds a packet to the queue at the interface
1818 tbf_queue(struct vif *vifp, struct mbuf *m)
1821 struct tbf *t = vifp->v_tbf;
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;
1843 * processes the queue at the interface
1846 tbf_process_q(struct vif *vifp)
1849 struct tbf *t = vifp->v_tbf;
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);
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)
1896 struct mbuf *m, *last;
1898 struct tbf *t = vifp->v_tbf;
1900 p = priority(vifp, ip);
1904 while ((m = *np) != NULL) {
1905 if (p > priority(vifp, mtod(m, struct ip *))) {
1907 /* If we're removing the last packet, fix the tail pointer */
1911 /* It's impossible for the queue to be empty, but check anyways. */
1912 if (--t->tbf_q_len == 0)
1915 mrtstat.mrts_drop_sel++;
1926 tbf_send_packet(struct vif *vifp, struct mbuf *m)
1930 if (vifp->v_flags & VIFF_TUNNEL) /* If tunnel options */
1931 ip_output(m, NULL, &vifp->v_route, IP_FORWARDING, NULL, NULL);
1933 struct ip_moptions imo;
1935 static struct route ro; /* XXX check this */
1937 imo.imo_multicast_ifp = vifp->v_ifp;
1938 imo.imo_multicast_ttl = mtod(m, struct ip *)->ip_ttl - 1;
1939 imo.imo_multicast_loop = 1;
1940 imo.imo_multicast_vif = -1;
1943 * Re-entrancy should not be a problem here, because
1944 * the packets that we send out and are looped back at us
1945 * should get rejected because they appear to come from
1946 * the loopback interface, thus preventing looping.
1948 error = ip_output(m, NULL, &ro, IP_FORWARDING, &imo, NULL);
1950 if (mrtdebug & DEBUG_XMIT)
1951 log(LOG_DEBUG, "phyint_send on vif %d err %d\n",
1952 (int)(vifp - viftable), error);
1957 /* determine the current time and then
1958 * the elapsed time (between the last time and time now)
1959 * in milliseconds & update the no. of tokens in the bucket
1962 tbf_update_tokens(struct vif *vifp)
1967 struct tbf *t = vifp->v_tbf;
1971 TV_DELTA(tp, t->tbf_last_pkt_t, tm);
1974 * This formula is actually
1975 * "time in seconds" * "bytes/second".
1977 * (tm / 1000000) * (v_rate_limit * 1000 * (1000/1024) / 8)
1979 * The (1000/1024) was introduced in add_vif to optimize
1980 * this divide into a shift.
1982 t->tbf_n_tok += tm * vifp->v_rate_limit / 1024 / 8;
1983 t->tbf_last_pkt_t = tp;
1985 if (t->tbf_n_tok > MAX_BKT_SIZE)
1986 t->tbf_n_tok = MAX_BKT_SIZE;
1992 priority(struct vif *vifp, struct ip *ip)
1994 int prio = 50; /* the lowest priority -- default case */
1996 /* temporary hack; may add general packet classifier some day */
1999 * The UDP port space is divided up into four priority ranges:
2000 * [0, 16384) : unclassified - lowest priority
2001 * [16384, 32768) : audio - highest priority
2002 * [32768, 49152) : whiteboard - medium priority
2003 * [49152, 65536) : video - low priority
2005 * Everything else gets lowest priority.
2007 if (ip->ip_p == IPPROTO_UDP) {
2008 struct udphdr *udp = (struct udphdr *)(((char *)ip) + (ip->ip_hl << 2));
2009 switch (ntohs(udp->uh_dport) & 0xc000) {
2025 * End of token bucket filter modifications
2029 X_ip_rsvp_vif(struct socket *so, struct sockopt *sopt)
2033 if (so->so_type != SOCK_RAW || so->so_proto->pr_protocol != IPPROTO_RSVP)
2036 error = sooptcopyin(sopt, &vifi, sizeof vifi, sizeof vifi);
2042 if (vifi < 0 || vifi >= numvifs) { /* Error if vif is invalid */
2044 return EADDRNOTAVAIL;
2047 if (sopt->sopt_name == IP_RSVP_VIF_ON) {
2048 /* Check if socket is available. */
2049 if (viftable[vifi].v_rsvpd != NULL) {
2054 viftable[vifi].v_rsvpd = so;
2055 /* This may seem silly, but we need to be sure we don't over-increment
2056 * the RSVP counter, in case something slips up.
2058 if (!viftable[vifi].v_rsvp_on) {
2059 viftable[vifi].v_rsvp_on = 1;
2062 } else { /* must be VIF_OFF */
2064 * XXX as an additional consistency check, one could make sure
2065 * that viftable[vifi].v_rsvpd == so, otherwise passing so as
2066 * first parameter is pretty useless.
2068 viftable[vifi].v_rsvpd = NULL;
2070 * This may seem silly, but we need to be sure we don't over-decrement
2071 * the RSVP counter, in case something slips up.
2073 if (viftable[vifi].v_rsvp_on) {
2074 viftable[vifi].v_rsvp_on = 0;
2083 X_ip_rsvp_force_done(struct socket *so)
2088 /* Don't bother if it is not the right type of socket. */
2089 if (so->so_type != SOCK_RAW || so->so_proto->pr_protocol != IPPROTO_RSVP)
2094 /* The socket may be attached to more than one vif...this
2095 * is perfectly legal.
2097 for (vifi = 0; vifi < numvifs; vifi++) {
2098 if (viftable[vifi].v_rsvpd == so) {
2099 viftable[vifi].v_rsvpd = NULL;
2100 /* This may seem silly, but we need to be sure we don't
2101 * over-decrement the RSVP counter, in case something slips up.
2103 if (viftable[vifi].v_rsvp_on) {
2104 viftable[vifi].v_rsvp_on = 0;
2114 X_rsvp_input(struct mbuf *m, ...)
2117 struct ip *ip = mtod(m, struct ip *);
2118 struct sockaddr_in rsvp_src = { sizeof rsvp_src, AF_INET };
2125 off = __va_arg(ap, int);
2126 proto = __va_arg(ap, int);
2130 printf("rsvp_input: rsvp_on %d\n",rsvp_on);
2132 /* Can still get packets with rsvp_on = 0 if there is a local member
2133 * of the group to which the RSVP packet is addressed. But in this
2134 * case we want to throw the packet away.
2144 printf("rsvp_input: check vifs\n");
2147 if (!(m->m_flags & M_PKTHDR))
2148 panic("rsvp_input no hdr");
2151 ifp = m->m_pkthdr.rcvif;
2152 /* Find which vif the packet arrived on. */
2153 for (vifi = 0; vifi < numvifs; vifi++)
2154 if (viftable[vifi].v_ifp == ifp)
2157 if (vifi == numvifs || viftable[vifi].v_rsvpd == NULL) {
2159 * If the old-style non-vif-associated socket is set,
2160 * then use it. Otherwise, drop packet since there
2161 * is no specific socket for this vif.
2163 if (ip_rsvpd != NULL) {
2165 printf("rsvp_input: Sending packet up old-style socket\n");
2166 rip_input(m, off, proto); /* xxx */
2168 if (rsvpdebug && vifi == numvifs)
2169 printf("rsvp_input: Can't find vif for packet.\n");
2170 else if (rsvpdebug && viftable[vifi].v_rsvpd == NULL)
2171 printf("rsvp_input: No socket defined for vif %d\n",vifi);
2177 rsvp_src.sin_addr = ip->ip_src;
2180 printf("rsvp_input: m->m_len = %d, sbspace() = %ld\n",
2181 m->m_len,sbspace(&(viftable[vifi].v_rsvpd->so_rcv)));
2183 if (socket_send(viftable[vifi].v_rsvpd, m, &rsvp_src) < 0) {
2185 printf("rsvp_input: Failed to append to socket\n");
2188 printf("rsvp_input: send packet up\n");
2195 * Code for bandwidth monitors
2199 * Define common interface for timeval-related methods
2201 #define BW_TIMEVALCMP(tvp, uvp, cmp) timevalcmp((tvp), (uvp), cmp)
2202 #define BW_TIMEVALDECR(vvp, uvp) timevalsub((vvp), (uvp))
2203 #define BW_TIMEVALADD(vvp, uvp) timevaladd((vvp), (uvp))
2206 compute_bw_meter_flags(struct bw_upcall *req)
2210 if (req->bu_flags & BW_UPCALL_UNIT_PACKETS)
2211 flags |= BW_METER_UNIT_PACKETS;
2212 if (req->bu_flags & BW_UPCALL_UNIT_BYTES)
2213 flags |= BW_METER_UNIT_BYTES;
2214 if (req->bu_flags & BW_UPCALL_GEQ)
2215 flags |= BW_METER_GEQ;
2216 if (req->bu_flags & BW_UPCALL_LEQ)
2217 flags |= BW_METER_LEQ;
2223 * Add a bw_meter entry
2226 add_bw_upcall(struct bw_upcall *req)
2229 struct timeval delta = { BW_UPCALL_THRESHOLD_INTERVAL_MIN_SEC,
2230 BW_UPCALL_THRESHOLD_INTERVAL_MIN_USEC };
2236 if (!(mrt_api_config & MRT_MFC_BW_UPCALL))
2239 /* Test if the flags are valid */
2240 if (!(req->bu_flags & (BW_UPCALL_UNIT_PACKETS | BW_UPCALL_UNIT_BYTES)))
2242 if (!(req->bu_flags & (BW_UPCALL_GEQ | BW_UPCALL_LEQ)))
2244 if ((req->bu_flags & (BW_UPCALL_GEQ | BW_UPCALL_LEQ))
2245 == (BW_UPCALL_GEQ | BW_UPCALL_LEQ))
2248 /* Test if the threshold time interval is valid */
2249 if (BW_TIMEVALCMP(&req->bu_threshold.b_time, &delta, <))
2252 flags = compute_bw_meter_flags(req);
2255 * Find if we have already same bw_meter entry
2258 mfc = mfc_find(req->bu_src.s_addr, req->bu_dst.s_addr);
2261 return EADDRNOTAVAIL;
2263 for (x = mfc->mfc_bw_meter; x != NULL; x = x->bm_mfc_next) {
2264 if ((BW_TIMEVALCMP(&x->bm_threshold.b_time,
2265 &req->bu_threshold.b_time, ==)) &&
2266 (x->bm_threshold.b_packets == req->bu_threshold.b_packets) &&
2267 (x->bm_threshold.b_bytes == req->bu_threshold.b_bytes) &&
2268 (x->bm_flags & BW_METER_USER_FLAGS) == flags) {
2270 return 0; /* XXX Already installed */
2275 /* Allocate the new bw_meter entry */
2276 x = malloc(sizeof(*x), M_BWMETER, M_INTWAIT);
2278 /* Set the new bw_meter entry */
2279 x->bm_threshold.b_time = req->bu_threshold.b_time;
2281 x->bm_start_time = now;
2282 x->bm_threshold.b_packets = req->bu_threshold.b_packets;
2283 x->bm_threshold.b_bytes = req->bu_threshold.b_bytes;
2284 x->bm_measured.b_packets = 0;
2285 x->bm_measured.b_bytes = 0;
2286 x->bm_flags = flags;
2287 x->bm_time_next = NULL;
2288 x->bm_time_hash = BW_METER_BUCKETS;
2290 /* Add the new bw_meter entry to the front of entries for this MFC */
2293 x->bm_mfc_next = mfc->mfc_bw_meter;
2294 mfc->mfc_bw_meter = x;
2295 schedule_bw_meter(x, &now);
2302 free_bw_list(struct bw_meter *list)
2304 while (list != NULL) {
2305 struct bw_meter *x = list;
2307 list = list->bm_mfc_next;
2308 unschedule_bw_meter(x);
2314 * Delete one or multiple bw_meter entries
2317 del_bw_upcall(struct bw_upcall *req)
2323 if (!(mrt_api_config & MRT_MFC_BW_UPCALL))
2327 /* Find the corresponding MFC entry */
2328 mfc = mfc_find(req->bu_src.s_addr, req->bu_dst.s_addr);
2331 return EADDRNOTAVAIL;
2332 } else if (req->bu_flags & BW_UPCALL_DELETE_ALL) {
2334 * Delete all bw_meter entries for this mfc
2336 struct bw_meter *list;
2338 list = mfc->mfc_bw_meter;
2339 mfc->mfc_bw_meter = NULL;
2343 } else { /* Delete a single bw_meter entry */
2344 struct bw_meter *prev;
2347 flags = compute_bw_meter_flags(req);
2349 /* Find the bw_meter entry to delete */
2350 for (prev = NULL, x = mfc->mfc_bw_meter; x != NULL;
2351 prev = x, x = x->bm_mfc_next) {
2352 if ((BW_TIMEVALCMP(&x->bm_threshold.b_time,
2353 &req->bu_threshold.b_time, ==)) &&
2354 (x->bm_threshold.b_packets == req->bu_threshold.b_packets) &&
2355 (x->bm_threshold.b_bytes == req->bu_threshold.b_bytes) &&
2356 (x->bm_flags & BW_METER_USER_FLAGS) == flags)
2359 if (x != NULL) { /* Delete entry from the list for this MFC */
2361 prev->bm_mfc_next = x->bm_mfc_next; /* remove from middle*/
2363 x->bm_mfc->mfc_bw_meter = x->bm_mfc_next;/* new head of list */
2366 unschedule_bw_meter(x);
2367 /* Free the bw_meter entry */
2379 * Perform bandwidth measurement processing that may result in an upcall
2382 bw_meter_receive_packet(struct bw_meter *x, int plen, struct timeval *nowp)
2384 struct timeval delta;
2389 BW_TIMEVALDECR(&delta, &x->bm_start_time);
2391 if (x->bm_flags & BW_METER_GEQ) {
2393 * Processing for ">=" type of bw_meter entry
2395 if (BW_TIMEVALCMP(&delta, &x->bm_threshold.b_time, >)) {
2396 /* Reset the bw_meter entry */
2397 x->bm_start_time = *nowp;
2398 x->bm_measured.b_packets = 0;
2399 x->bm_measured.b_bytes = 0;
2400 x->bm_flags &= ~BW_METER_UPCALL_DELIVERED;
2403 /* Record that a packet is received */
2404 x->bm_measured.b_packets++;
2405 x->bm_measured.b_bytes += plen;
2408 * Test if we should deliver an upcall
2410 if (!(x->bm_flags & BW_METER_UPCALL_DELIVERED)) {
2411 if (((x->bm_flags & BW_METER_UNIT_PACKETS) &&
2412 (x->bm_measured.b_packets >= x->bm_threshold.b_packets)) ||
2413 ((x->bm_flags & BW_METER_UNIT_BYTES) &&
2414 (x->bm_measured.b_bytes >= x->bm_threshold.b_bytes))) {
2415 /* Prepare an upcall for delivery */
2416 bw_meter_prepare_upcall(x, nowp);
2417 x->bm_flags |= BW_METER_UPCALL_DELIVERED;
2420 } else if (x->bm_flags & BW_METER_LEQ) {
2422 * Processing for "<=" type of bw_meter entry
2424 if (BW_TIMEVALCMP(&delta, &x->bm_threshold.b_time, >)) {
2426 * We are behind time with the multicast forwarding table
2427 * scanning for "<=" type of bw_meter entries, so test now
2428 * if we should deliver an upcall.
2430 if (((x->bm_flags & BW_METER_UNIT_PACKETS) &&
2431 (x->bm_measured.b_packets <= x->bm_threshold.b_packets)) ||
2432 ((x->bm_flags & BW_METER_UNIT_BYTES) &&
2433 (x->bm_measured.b_bytes <= x->bm_threshold.b_bytes))) {
2434 /* Prepare an upcall for delivery */
2435 bw_meter_prepare_upcall(x, nowp);
2437 /* Reschedule the bw_meter entry */
2438 unschedule_bw_meter(x);
2439 schedule_bw_meter(x, nowp);
2442 /* Record that a packet is received */
2443 x->bm_measured.b_packets++;
2444 x->bm_measured.b_bytes += plen;
2447 * Test if we should restart the measuring interval
2449 if ((x->bm_flags & BW_METER_UNIT_PACKETS &&
2450 x->bm_measured.b_packets <= x->bm_threshold.b_packets) ||
2451 (x->bm_flags & BW_METER_UNIT_BYTES &&
2452 x->bm_measured.b_bytes <= x->bm_threshold.b_bytes)) {
2453 /* Don't restart the measuring interval */
2455 /* Do restart the measuring interval */
2457 * XXX: note that we don't unschedule and schedule, because this
2458 * might be too much overhead per packet. Instead, when we process
2459 * all entries for a given timer hash bin, we check whether it is
2460 * really a timeout. If not, we reschedule at that time.
2462 x->bm_start_time = *nowp;
2463 x->bm_measured.b_packets = 0;
2464 x->bm_measured.b_bytes = 0;
2465 x->bm_flags &= ~BW_METER_UPCALL_DELIVERED;
2472 * Prepare a bandwidth-related upcall
2475 bw_meter_prepare_upcall(struct bw_meter *x, struct timeval *nowp)
2477 struct timeval delta;
2478 struct bw_upcall *u;
2484 * Compute the measured time interval
2487 BW_TIMEVALDECR(&delta, &x->bm_start_time);
2490 * If there are too many pending upcalls, deliver them now
2492 if (bw_upcalls_n >= BW_UPCALLS_MAX)
2496 * Set the bw_upcall entry
2498 u = &bw_upcalls[bw_upcalls_n++];
2499 u->bu_src = x->bm_mfc->mfc_origin;
2500 u->bu_dst = x->bm_mfc->mfc_mcastgrp;
2501 u->bu_threshold.b_time = x->bm_threshold.b_time;
2502 u->bu_threshold.b_packets = x->bm_threshold.b_packets;
2503 u->bu_threshold.b_bytes = x->bm_threshold.b_bytes;
2504 u->bu_measured.b_time = delta;
2505 u->bu_measured.b_packets = x->bm_measured.b_packets;
2506 u->bu_measured.b_bytes = x->bm_measured.b_bytes;
2508 if (x->bm_flags & BW_METER_UNIT_PACKETS)
2509 u->bu_flags |= BW_UPCALL_UNIT_PACKETS;
2510 if (x->bm_flags & BW_METER_UNIT_BYTES)
2511 u->bu_flags |= BW_UPCALL_UNIT_BYTES;
2512 if (x->bm_flags & BW_METER_GEQ)
2513 u->bu_flags |= BW_UPCALL_GEQ;
2514 if (x->bm_flags & BW_METER_LEQ)
2515 u->bu_flags |= BW_UPCALL_LEQ;
2521 * Send the pending bandwidth-related upcalls
2524 bw_upcalls_send(void)
2527 int len = bw_upcalls_n * sizeof(bw_upcalls[0]);
2528 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
2529 static struct igmpmsg igmpmsg = { 0, /* unused1 */
2531 IGMPMSG_BW_UPCALL,/* im_msgtype */
2536 { 0 } }; /* im_dst */
2538 if (bw_upcalls_n == 0)
2539 return; /* No pending upcalls */
2544 * Allocate a new mbuf, initialize it with the header and
2545 * the payload for the pending calls.
2547 MGETHDR(m, MB_DONTWAIT, MT_HEADER);
2549 log(LOG_WARNING, "bw_upcalls_send: cannot allocate mbuf\n");
2553 m->m_len = m->m_pkthdr.len = 0;
2554 m_copyback(m, 0, sizeof(struct igmpmsg), (caddr_t)&igmpmsg);
2555 m_copyback(m, sizeof(struct igmpmsg), len, (caddr_t)&bw_upcalls[0]);
2559 * XXX do we need to set the address in k_igmpsrc ?
2561 mrtstat.mrts_upcalls++;
2562 if (socket_send(ip_mrouter, m, &k_igmpsrc) < 0) {
2563 log(LOG_WARNING, "bw_upcalls_send: ip_mrouter socket queue full\n");
2564 ++mrtstat.mrts_upq_sockfull;
2569 * Compute the timeout hash value for the bw_meter entries
2571 #define BW_METER_TIMEHASH(bw_meter, hash) \
2573 struct timeval next_timeval = (bw_meter)->bm_start_time; \
2575 BW_TIMEVALADD(&next_timeval, &(bw_meter)->bm_threshold.b_time); \
2576 (hash) = next_timeval.tv_sec; \
2577 if (next_timeval.tv_usec) \
2578 (hash)++; /* XXX: make sure we don't timeout early */ \
2579 (hash) %= BW_METER_BUCKETS; \
2583 * Schedule a timer to process periodically bw_meter entry of type "<="
2584 * by linking the entry in the proper hash bucket.
2587 schedule_bw_meter(struct bw_meter *x, struct timeval *nowp)
2591 if (!(x->bm_flags & BW_METER_LEQ))
2592 return; /* XXX: we schedule timers only for "<=" entries */
2595 * Reset the bw_meter entry
2598 x->bm_start_time = *nowp;
2599 x->bm_measured.b_packets = 0;
2600 x->bm_measured.b_bytes = 0;
2601 x->bm_flags &= ~BW_METER_UPCALL_DELIVERED;
2605 * Compute the timeout hash value and insert the entry
2607 BW_METER_TIMEHASH(x, time_hash);
2608 x->bm_time_next = bw_meter_timers[time_hash];
2609 bw_meter_timers[time_hash] = x;
2610 x->bm_time_hash = time_hash;
2614 * Unschedule the periodic timer that processes bw_meter entry of type "<="
2615 * by removing the entry from the proper hash bucket.
2618 unschedule_bw_meter(struct bw_meter *x)
2621 struct bw_meter *prev, *tmp;
2623 if (!(x->bm_flags & BW_METER_LEQ))
2624 return; /* XXX: we schedule timers only for "<=" entries */
2627 * Compute the timeout hash value and delete the entry
2629 time_hash = x->bm_time_hash;
2630 if (time_hash >= BW_METER_BUCKETS)
2631 return; /* Entry was not scheduled */
2633 for (prev = NULL, tmp = bw_meter_timers[time_hash];
2634 tmp != NULL; prev = tmp, tmp = tmp->bm_time_next)
2639 panic("unschedule_bw_meter: bw_meter entry not found");
2642 prev->bm_time_next = x->bm_time_next;
2644 bw_meter_timers[time_hash] = x->bm_time_next;
2646 x->bm_time_next = NULL;
2647 x->bm_time_hash = BW_METER_BUCKETS;
2652 * Process all "<=" type of bw_meter that should be processed now,
2653 * and for each entry prepare an upcall if necessary. Each processed
2654 * entry is rescheduled again for the (periodic) processing.
2656 * This is run periodically (once per second normally). On each round,
2657 * all the potentially matching entries are in the hash slot that we are
2663 static uint32_t last_tv_sec; /* last time we processed this */
2667 struct timeval now, process_endtime;
2670 if (last_tv_sec == now.tv_sec)
2671 return; /* nothing to do */
2674 loops = now.tv_sec - last_tv_sec;
2675 last_tv_sec = now.tv_sec;
2676 if (loops > BW_METER_BUCKETS)
2677 loops = BW_METER_BUCKETS;
2680 * Process all bins of bw_meter entries from the one after the last
2681 * processed to the current one. On entry, i points to the last bucket
2682 * visited, so we need to increment i at the beginning of the loop.
2684 for (i = (now.tv_sec - loops) % BW_METER_BUCKETS; loops > 0; loops--) {
2685 struct bw_meter *x, *tmp_list;
2687 if (++i >= BW_METER_BUCKETS)
2690 /* Disconnect the list of bw_meter entries from the bin */
2691 tmp_list = bw_meter_timers[i];
2692 bw_meter_timers[i] = NULL;
2694 /* Process the list of bw_meter entries */
2695 while (tmp_list != NULL) {
2697 tmp_list = tmp_list->bm_time_next;
2699 /* Test if the time interval is over */
2700 process_endtime = x->bm_start_time;
2701 BW_TIMEVALADD(&process_endtime, &x->bm_threshold.b_time);
2702 if (BW_TIMEVALCMP(&process_endtime, &now, >)) {
2703 /* Not yet: reschedule, but don't reset */
2706 BW_METER_TIMEHASH(x, time_hash);
2707 if (time_hash == i && process_endtime.tv_sec == now.tv_sec) {
2709 * XXX: somehow the bin processing is a bit ahead of time.
2710 * Put the entry in the next bin.
2712 if (++time_hash >= BW_METER_BUCKETS)
2715 x->bm_time_next = bw_meter_timers[time_hash];
2716 bw_meter_timers[time_hash] = x;
2717 x->bm_time_hash = time_hash;
2723 * Test if we should deliver an upcall
2725 if (((x->bm_flags & BW_METER_UNIT_PACKETS) &&
2726 (x->bm_measured.b_packets <= x->bm_threshold.b_packets)) ||
2727 ((x->bm_flags & BW_METER_UNIT_BYTES) &&
2728 (x->bm_measured.b_bytes <= x->bm_threshold.b_bytes))) {
2729 /* Prepare an upcall for delivery */
2730 bw_meter_prepare_upcall(x, &now);
2734 * Reschedule for next processing
2736 schedule_bw_meter(x, &now);
2741 /* Send all upcalls that are pending delivery */
2746 * A periodic function for sending all upcalls that are pending delivery
2749 expire_bw_upcalls_send(void *unused)
2753 callout_reset(&bw_upcalls_ch, BW_UPCALLS_PERIOD,
2754 expire_bw_upcalls_send, NULL);
2758 * A periodic function for periodic scanning of the multicast forwarding
2759 * table for processing all "<=" bw_meter entries.
2762 expire_bw_meter_process(void *unused)
2764 if (mrt_api_config & MRT_MFC_BW_UPCALL)
2767 callout_reset(&bw_meter_ch, BW_METER_PERIOD,
2768 expire_bw_meter_process, NULL);
2772 * End of bandwidth monitoring code
2777 * Send the packet up to the user daemon, or eventually do kernel encapsulation
2781 pim_register_send(struct ip *ip, struct vif *vifp,
2782 struct mbuf *m, struct mfc *rt)
2784 struct mbuf *mb_copy, *mm;
2786 if (mrtdebug & DEBUG_PIM)
2787 log(LOG_DEBUG, "pim_register_send: ");
2789 mb_copy = pim_register_prepare(ip, m);
2790 if (mb_copy == NULL)
2794 * Send all the fragments. Note that the mbuf for each fragment
2795 * is freed by the sending machinery.
2797 for (mm = mb_copy; mm; mm = mb_copy) {
2798 mb_copy = mm->m_nextpkt;
2800 mm = m_pullup(mm, sizeof(struct ip));
2802 ip = mtod(mm, struct ip *);
2803 if ((mrt_api_config & MRT_MFC_RP) &&
2804 (rt->mfc_rp.s_addr != INADDR_ANY)) {
2805 pim_register_send_rp(ip, vifp, mm, rt);
2807 pim_register_send_upcall(ip, vifp, mm, rt);
2816 * Return a copy of the data packet that is ready for PIM Register
2818 * XXX: Note that in the returned copy the IP header is a valid one.
2820 static struct mbuf *
2821 pim_register_prepare(struct ip *ip, struct mbuf *m)
2823 struct mbuf *mb_copy = NULL;
2826 /* Take care of delayed checksums */
2827 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
2828 in_delayed_cksum(m);
2829 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
2833 * Copy the old packet & pullup its IP header into the
2834 * new mbuf so we can modify it.
2836 mb_copy = m_copypacket(m, MB_DONTWAIT);
2837 if (mb_copy == NULL)
2839 mb_copy = m_pullup(mb_copy, ip->ip_hl << 2);
2840 if (mb_copy == NULL)
2843 /* take care of the TTL */
2844 ip = mtod(mb_copy, struct ip *);
2847 /* Compute the MTU after the PIM Register encapsulation */
2848 mtu = 0xffff - sizeof(pim_encap_iphdr) - sizeof(pim_encap_pimhdr);
2850 if (ip->ip_len <= mtu) {
2851 /* Turn the IP header into a valid one */
2852 ip->ip_len = htons(ip->ip_len);
2853 ip->ip_off = htons(ip->ip_off);
2855 ip->ip_sum = in_cksum(mb_copy, ip->ip_hl << 2);
2857 /* Fragment the packet */
2858 if (ip_fragment(ip, &mb_copy, mtu, 0, CSUM_DELAY_IP) != 0) {
2867 * Send an upcall with the data packet to the user-level process.
2870 pim_register_send_upcall(struct ip *ip, struct vif *vifp,
2871 struct mbuf *mb_copy, struct mfc *rt)
2873 struct mbuf *mb_first;
2874 int len = ntohs(ip->ip_len);
2876 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
2879 * Add a new mbuf with an upcall header
2881 MGETHDR(mb_first, MB_DONTWAIT, MT_HEADER);
2882 if (mb_first == NULL) {
2886 mb_first->m_data += max_linkhdr;
2887 mb_first->m_pkthdr.len = len + sizeof(struct igmpmsg);
2888 mb_first->m_len = sizeof(struct igmpmsg);
2889 mb_first->m_next = mb_copy;
2891 /* Send message to routing daemon */
2892 im = mtod(mb_first, struct igmpmsg *);
2893 im->im_msgtype = IGMPMSG_WHOLEPKT;
2895 im->im_vif = vifp - viftable;
2896 im->im_src = ip->ip_src;
2897 im->im_dst = ip->ip_dst;
2899 k_igmpsrc.sin_addr = ip->ip_src;
2901 mrtstat.mrts_upcalls++;
2903 if (socket_send(ip_mrouter, mb_first, &k_igmpsrc) < 0) {
2904 if (mrtdebug & DEBUG_PIM)
2906 "mcast: pim_register_send_upcall: ip_mrouter socket queue full");
2907 ++mrtstat.mrts_upq_sockfull;
2911 /* Keep statistics */
2912 pimstat.pims_snd_registers_msgs++;
2913 pimstat.pims_snd_registers_bytes += len;
2919 * Encapsulate the data packet in PIM Register message and send it to the RP.
2922 pim_register_send_rp(struct ip *ip, struct vif *vifp,
2923 struct mbuf *mb_copy, struct mfc *rt)
2925 struct mbuf *mb_first;
2926 struct ip *ip_outer;
2927 struct pim_encap_pimhdr *pimhdr;
2928 int len = ntohs(ip->ip_len);
2929 vifi_t vifi = rt->mfc_parent;
2931 if ((vifi >= numvifs) || (viftable[vifi].v_lcl_addr.s_addr == 0)) {
2933 return EADDRNOTAVAIL; /* The iif vif is invalid */
2937 * Add a new mbuf with the encapsulating header
2939 MGETHDR(mb_first, MB_DONTWAIT, MT_HEADER);
2940 if (mb_first == NULL) {
2944 mb_first->m_data += max_linkhdr;
2945 mb_first->m_len = sizeof(pim_encap_iphdr) + sizeof(pim_encap_pimhdr);
2946 mb_first->m_next = mb_copy;
2948 mb_first->m_pkthdr.len = len + mb_first->m_len;
2951 * Fill in the encapsulating IP and PIM header
2953 ip_outer = mtod(mb_first, struct ip *);
2954 *ip_outer = pim_encap_iphdr;
2956 ip_outer->ip_id = ip_randomid();
2958 ip_outer->ip_id = htons(ip_id++);
2960 ip_outer->ip_len = len + sizeof(pim_encap_iphdr) + sizeof(pim_encap_pimhdr);
2961 ip_outer->ip_src = viftable[vifi].v_lcl_addr;
2962 ip_outer->ip_dst = rt->mfc_rp;
2964 * Copy the inner header TOS to the outer header, and take care of the
2967 ip_outer->ip_tos = ip->ip_tos;
2968 if (ntohs(ip->ip_off) & IP_DF)
2969 ip_outer->ip_off |= IP_DF;
2970 pimhdr = (struct pim_encap_pimhdr *)((caddr_t)ip_outer
2971 + sizeof(pim_encap_iphdr));
2972 *pimhdr = pim_encap_pimhdr;
2973 /* If the iif crosses a border, set the Border-bit */
2974 if (rt->mfc_flags[vifi] & MRT_MFC_FLAGS_BORDER_VIF & mrt_api_config)
2975 pimhdr->flags |= htonl(PIM_BORDER_REGISTER);
2977 mb_first->m_data += sizeof(pim_encap_iphdr);
2978 pimhdr->pim.pim_cksum = in_cksum(mb_first, sizeof(pim_encap_pimhdr));
2979 mb_first->m_data -= sizeof(pim_encap_iphdr);
2981 if (vifp->v_rate_limit == 0)
2982 tbf_send_packet(vifp, mb_first);
2984 tbf_control(vifp, mb_first, ip, ip_outer->ip_len);
2986 /* Keep statistics */
2987 pimstat.pims_snd_registers_msgs++;
2988 pimstat.pims_snd_registers_bytes += len;
2994 * PIM-SMv2 and PIM-DM messages processing.
2995 * Receives and verifies the PIM control messages, and passes them
2996 * up to the listening socket, using rip_input().
2997 * The only message with special processing is the PIM_REGISTER message
2998 * (used by PIM-SM): the PIM header is stripped off, and the inner packet
2999 * is passed to if_simloop().
3002 pim_input(struct mbuf *m, ...)
3005 struct ip *ip = mtod(m, struct ip *);
3008 int datalen = ip->ip_len;
3014 off = __va_arg(ap, int);
3015 proto = __va_arg(ap, int);
3020 /* Keep statistics */
3021 pimstat.pims_rcv_total_msgs++;
3022 pimstat.pims_rcv_total_bytes += datalen;
3027 if (datalen < PIM_MINLEN) {
3028 pimstat.pims_rcv_tooshort++;
3029 log(LOG_ERR, "pim_input: packet size too small %d from %lx\n",
3030 datalen, (u_long)ip->ip_src.s_addr);
3036 * If the packet is at least as big as a REGISTER, go agead
3037 * and grab the PIM REGISTER header size, to avoid another
3038 * possible m_pullup() later.
3040 * PIM_MINLEN == pimhdr + u_int32_t == 4 + 4 = 8
3041 * PIM_REG_MINLEN == pimhdr + reghdr + encap_iphdr == 4 + 4 + 20 = 28
3043 minlen = iphlen + (datalen >= PIM_REG_MINLEN ? PIM_REG_MINLEN : PIM_MINLEN);
3045 * Get the IP and PIM headers in contiguous memory, and
3046 * possibly the PIM REGISTER header.
3048 if ((m->m_flags & M_EXT || m->m_len < minlen) &&
3049 (m = m_pullup(m, minlen)) == 0) {
3050 log(LOG_ERR, "pim_input: m_pullup failure\n");
3053 /* m_pullup() may have given us a new mbuf so reset ip. */
3054 ip = mtod(m, struct ip *);
3055 ip_tos = ip->ip_tos;
3057 /* adjust mbuf to point to the PIM header */
3058 m->m_data += iphlen;
3060 pim = mtod(m, struct pim *);
3063 * Validate checksum. If PIM REGISTER, exclude the data packet.
3065 * XXX: some older PIMv2 implementations don't make this distinction,
3066 * so for compatibility reason perform the checksum over part of the
3067 * message, and if error, then over the whole message.
3069 if (PIM_VT_T(pim->pim_vt) == PIM_REGISTER && in_cksum(m, PIM_MINLEN) == 0) {
3070 /* do nothing, checksum okay */
3071 } else if (in_cksum(m, datalen)) {
3072 pimstat.pims_rcv_badsum++;
3073 if (mrtdebug & DEBUG_PIM)
3074 log(LOG_DEBUG, "pim_input: invalid checksum");
3079 /* PIM version check */
3080 if (PIM_VT_V(pim->pim_vt) < PIM_VERSION) {
3081 pimstat.pims_rcv_badversion++;
3082 log(LOG_ERR, "pim_input: incorrect version %d, expecting %d\n",
3083 PIM_VT_V(pim->pim_vt), PIM_VERSION);
3088 /* restore mbuf back to the outer IP */
3089 m->m_data -= iphlen;
3092 if (PIM_VT_T(pim->pim_vt) == PIM_REGISTER) {
3094 * Since this is a REGISTER, we'll make a copy of the register
3095 * headers ip + pim + u_int32 + encap_ip, to be passed up to the
3098 struct sockaddr_in dst = { sizeof(dst), AF_INET };
3100 struct ip *encap_ip;
3103 if ((reg_vif_num >= numvifs) || (reg_vif_num == VIFI_INVALID)) {
3104 if (mrtdebug & DEBUG_PIM)
3106 "pim_input: register vif not set: %d\n", reg_vif_num);
3114 if (datalen < PIM_REG_MINLEN) {
3115 pimstat.pims_rcv_tooshort++;
3116 pimstat.pims_rcv_badregisters++;
3118 "pim_input: register packet size too small %d from %lx\n",
3119 datalen, (u_long)ip->ip_src.s_addr);
3124 reghdr = (u_int32_t *)(pim + 1);
3125 encap_ip = (struct ip *)(reghdr + 1);
3127 if (mrtdebug & DEBUG_PIM) {
3129 "pim_input[register], encap_ip: %lx -> %lx, encap_ip len %d\n",
3130 (u_long)ntohl(encap_ip->ip_src.s_addr),
3131 (u_long)ntohl(encap_ip->ip_dst.s_addr),
3132 ntohs(encap_ip->ip_len));
3135 /* verify the version number of the inner packet */
3136 if (encap_ip->ip_v != IPVERSION) {
3137 pimstat.pims_rcv_badregisters++;
3138 if (mrtdebug & DEBUG_PIM) {
3139 log(LOG_DEBUG, "pim_input: invalid IP version (%d) "
3140 "of the inner packet\n", encap_ip->ip_v);
3146 /* verify the inner packet is destined to a mcast group */
3147 if (!IN_MULTICAST(ntohl(encap_ip->ip_dst.s_addr))) {
3148 pimstat.pims_rcv_badregisters++;
3149 if (mrtdebug & DEBUG_PIM)
3151 "pim_input: inner packet of register is not "
3153 (u_long)ntohl(encap_ip->ip_dst.s_addr));
3158 /* If a NULL_REGISTER, pass it to the daemon */
3159 if ((ntohl(*reghdr) & PIM_NULL_REGISTER))
3160 goto pim_input_to_daemon;
3163 * Copy the TOS from the outer IP header to the inner IP header.
3165 if (encap_ip->ip_tos != ip_tos) {
3166 /* Outer TOS -> inner TOS */
3167 encap_ip->ip_tos = ip_tos;
3168 /* Recompute the inner header checksum. Sigh... */
3170 /* adjust mbuf to point to the inner IP header */
3171 m->m_data += (iphlen + PIM_MINLEN);
3172 m->m_len -= (iphlen + PIM_MINLEN);
3174 encap_ip->ip_sum = 0;
3175 encap_ip->ip_sum = in_cksum(m, encap_ip->ip_hl << 2);
3177 /* restore mbuf to point back to the outer IP header */
3178 m->m_data -= (iphlen + PIM_MINLEN);
3179 m->m_len += (iphlen + PIM_MINLEN);
3183 * Decapsulate the inner IP packet and loopback to forward it
3184 * as a normal multicast packet. Also, make a copy of the
3185 * outer_iphdr + pimhdr + reghdr + encap_iphdr
3186 * to pass to the daemon later, so it can take the appropriate
3187 * actions (e.g., send back PIM_REGISTER_STOP).
3188 * XXX: here m->m_data points to the outer IP header.
3190 mcp = m_copy(m, 0, iphlen + PIM_REG_MINLEN);
3193 "pim_input: pim register: could not copy register head\n");
3198 /* Keep statistics */
3199 /* XXX: registers_bytes include only the encap. mcast pkt */
3200 pimstat.pims_rcv_registers_msgs++;
3201 pimstat.pims_rcv_registers_bytes += ntohs(encap_ip->ip_len);
3204 * forward the inner ip packet; point m_data at the inner ip.
3206 m_adj(m, iphlen + PIM_MINLEN);
3208 if (mrtdebug & DEBUG_PIM) {
3210 "pim_input: forwarding decapsulated register: "
3211 "src %lx, dst %lx, vif %d\n",
3212 (u_long)ntohl(encap_ip->ip_src.s_addr),
3213 (u_long)ntohl(encap_ip->ip_dst.s_addr),
3216 if_simloop(viftable[reg_vif_num].v_ifp, m, dst.sin_family, 0);
3218 /* prepare the register head to send to the mrouting daemon */
3222 pim_input_to_daemon:
3224 * Pass the PIM message up to the daemon; if it is a Register message,
3225 * pass the 'head' only up to the daemon. This includes the
3226 * outer IP header, PIM header, PIM-Register header and the
3228 * XXX: the outer IP header pkt size of a Register is not adjust to
3229 * reflect the fact that the inner multicast data is truncated.
3231 rip_input(m, iphlen, proto);
3238 ip_mroute_modevent(module_t mod, int type, void *unused)
3245 /* XXX Protect against multiple loading */
3246 ip_mcast_src = X_ip_mcast_src;
3247 ip_mforward = X_ip_mforward;
3248 ip_mrouter_done = X_ip_mrouter_done;
3249 ip_mrouter_get = X_ip_mrouter_get;
3250 ip_mrouter_set = X_ip_mrouter_set;
3251 ip_rsvp_force_done = X_ip_rsvp_force_done;
3252 ip_rsvp_vif = X_ip_rsvp_vif;
3253 ipip_input = X_ipip_input;
3254 legal_vif_num = X_legal_vif_num;
3255 mrt_ioctl = X_mrt_ioctl;
3256 rsvp_input_p = X_rsvp_input;
3265 ip_mcast_src = NULL;
3267 ip_mrouter_done = NULL;
3268 ip_mrouter_get = NULL;
3269 ip_mrouter_set = NULL;
3270 ip_rsvp_force_done = NULL;
3273 legal_vif_num = NULL;
3275 rsvp_input_p = NULL;
3282 static moduledata_t ip_mroutemod = {
3287 DECLARE_MODULE(ip_mroute, ip_mroutemod, SI_SUB_PSEUDO, SI_ORDER_ANY);