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.5 2003/09/15 23:38:13 hsu 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>
44 #include <net/netisr.h>
45 #include <net/route.h>
46 #include <netinet/in.h>
47 #include <netinet/igmp.h>
48 #include <netinet/in_systm.h>
49 #include <netinet/in_var.h>
50 #include <netinet/ip.h>
51 #include "ip_mroute.h"
52 #include <netinet/ip_var.h>
54 #include <netinet/pim.h>
55 #include <netinet/pim_var.h>
57 #include <netinet/udp.h>
58 #include <machine/in_cksum.h>
61 * Control debugging code for rsvp and multicast routing code.
62 * Can only set them with the debugger.
64 static u_int rsvpdebug; /* non-zero enables debugging */
66 static u_int mrtdebug; /* any set of the flags below */
68 #define DEBUG_MFC 0x02
69 #define DEBUG_FORWARD 0x04
70 #define DEBUG_EXPIRE 0x08
71 #define DEBUG_XMIT 0x10
72 #define DEBUG_PIM 0x20
74 #define VIFI_INVALID ((vifi_t) -1)
76 #define M_HASCL(m) ((m)->m_flags & M_EXT)
78 static MALLOC_DEFINE(M_MRTABLE, "mroutetbl", "multicast routing tables");
80 static struct mrtstat mrtstat;
81 SYSCTL_STRUCT(_net_inet_ip, OID_AUTO, mrtstat, CTLFLAG_RW,
83 "Multicast Routing Statistics (struct mrtstat, netinet/ip_mroute.h)");
85 static struct mfc *mfctable[MFCTBLSIZ];
86 SYSCTL_OPAQUE(_net_inet_ip, OID_AUTO, mfctable, CTLFLAG_RD,
87 &mfctable, sizeof(mfctable), "S,*mfc[MFCTBLSIZ]",
88 "Multicast Forwarding Table (struct *mfc[MFCTBLSIZ], netinet/ip_mroute.h)");
90 static struct vif viftable[MAXVIFS];
91 SYSCTL_OPAQUE(_net_inet_ip, OID_AUTO, viftable, CTLFLAG_RD,
92 &viftable, sizeof(viftable), "S,vif[MAXVIFS]",
93 "Multicast Virtual Interfaces (struct vif[MAXVIFS], netinet/ip_mroute.h)");
95 static u_char nexpire[MFCTBLSIZ];
97 static struct callout_handle expire_upcalls_ch;
99 #define EXPIRE_TIMEOUT (hz / 4) /* 4x / second */
100 #define UPCALL_EXPIRE 6 /* number of timeouts */
103 * Define the token bucket filter structures
104 * tbftable -> each vif has one of these for storing info
107 static struct tbf tbftable[MAXVIFS];
108 #define TBF_REPROCESS (hz / 100) /* 100x / second */
111 * 'Interfaces' associated with decapsulator (so we can tell
112 * packets that went through it from ones that get reflected
113 * by a broken gateway). These interfaces are never linked into
114 * the system ifnet list & no routes point to them. I.e., packets
115 * can't be sent this way. They only exist as a placeholder for
116 * multicast source verification.
118 static struct ifnet multicast_decap_if[MAXVIFS];
121 #define ENCAP_PROTO IPPROTO_IPIP /* 4 */
123 /* prototype IP hdr for encapsulated packets */
124 static struct ip multicast_encap_iphdr = {
125 #if BYTE_ORDER == LITTLE_ENDIAN
126 sizeof(struct ip) >> 2, IPVERSION,
128 IPVERSION, sizeof(struct ip) >> 2,
131 sizeof(struct ip), /* total length */
134 ENCAP_TTL, ENCAP_PROTO,
139 * Bandwidth meter variables and constants
141 static MALLOC_DEFINE(M_BWMETER, "bwmeter", "multicast upcall bw meters");
143 * Pending timeouts are stored in a hash table, the key being the
144 * expiration time. Periodically, the entries are analysed and processed.
146 #define BW_METER_BUCKETS 1024
147 static struct bw_meter *bw_meter_timers[BW_METER_BUCKETS];
148 static struct callout_handle bw_meter_ch;
149 #define BW_METER_PERIOD (hz) /* periodical handling of bw meters */
152 * Pending upcalls are stored in a vector which is flushed when
153 * full, or periodically
155 static struct bw_upcall bw_upcalls[BW_UPCALLS_MAX];
156 static u_int bw_upcalls_n; /* # of pending upcalls */
157 static struct callout_handle bw_upcalls_ch;
158 #define BW_UPCALLS_PERIOD (hz) /* periodical flush of bw upcalls */
161 static struct pimstat pimstat;
162 SYSCTL_STRUCT(_net_inet_pim, PIMCTL_STATS, stats, CTLFLAG_RD,
164 "PIM Statistics (struct pimstat, netinet/pim_var.h)");
167 * Note: the PIM Register encapsulation adds the following in front of a
170 * struct pim_encap_hdr {
172 * struct pim_encap_pimhdr pim;
177 struct pim_encap_pimhdr {
182 static struct ip pim_encap_iphdr = {
183 #if BYTE_ORDER == LITTLE_ENDIAN
184 sizeof(struct ip) >> 2,
188 sizeof(struct ip) >> 2,
191 sizeof(struct ip), /* total length */
199 static struct pim_encap_pimhdr pim_encap_pimhdr = {
201 PIM_MAKE_VT(PIM_VERSION, PIM_REGISTER), /* PIM vers and message type */
208 static struct ifnet multicast_register_if;
209 static vifi_t reg_vif_num = VIFI_INVALID;
215 static vifi_t numvifs;
216 static int have_encap_tunnel;
219 * one-back cache used by ipip_input to locate a tunnel's vif
220 * given a datagram's src ip address.
222 static u_long last_encap_src;
223 static struct vif *last_encap_vif;
225 static u_long X_ip_mcast_src(int vifi);
226 static int X_ip_mforward(struct ip *ip, struct ifnet *ifp,
227 struct mbuf *m, struct ip_moptions *imo);
228 static int X_ip_mrouter_done(void);
229 static int X_ip_mrouter_get(struct socket *so, struct sockopt *m);
230 static int X_ip_mrouter_set(struct socket *so, struct sockopt *m);
231 static int X_legal_vif_num(int vif);
232 static int X_mrt_ioctl(int cmd, caddr_t data);
234 static int get_sg_cnt(struct sioc_sg_req *);
235 static int get_vif_cnt(struct sioc_vif_req *);
236 static int ip_mrouter_init(struct socket *, int);
237 static int add_vif(struct vifctl *);
238 static int del_vif(vifi_t);
239 static int add_mfc(struct mfcctl2 *);
240 static int del_mfc(struct mfcctl2 *);
241 static int set_api_config(uint32_t *); /* chose API capabilities */
242 static int socket_send(struct socket *, struct mbuf *, struct sockaddr_in *);
243 static int set_assert(int);
244 static void expire_upcalls(void *);
245 static int ip_mdq(struct mbuf *, struct ifnet *, struct mfc *, vifi_t);
246 static void phyint_send(struct ip *, struct vif *, struct mbuf *);
247 static void encap_send(struct ip *, struct vif *, struct mbuf *);
248 static void tbf_control(struct vif *, struct mbuf *, struct ip *, u_long);
249 static void tbf_queue(struct vif *, struct mbuf *);
250 static void tbf_process_q(struct vif *);
251 static void tbf_reprocess_q(void *);
252 static int tbf_dq_sel(struct vif *, struct ip *);
253 static void tbf_send_packet(struct vif *, struct mbuf *);
254 static void tbf_update_tokens(struct vif *);
255 static int priority(struct vif *, struct ip *);
258 * Bandwidth monitoring
260 static void free_bw_list(struct bw_meter *list);
261 static int add_bw_upcall(struct bw_upcall *);
262 static int del_bw_upcall(struct bw_upcall *);
263 static void bw_meter_receive_packet(struct bw_meter *x, int plen,
264 struct timeval *nowp);
265 static void bw_meter_prepare_upcall(struct bw_meter *x, struct timeval *nowp);
266 static void bw_upcalls_send(void);
267 static void schedule_bw_meter(struct bw_meter *x, struct timeval *nowp);
268 static void unschedule_bw_meter(struct bw_meter *x);
269 static void bw_meter_process(void);
270 static void expire_bw_upcalls_send(void *);
271 static void expire_bw_meter_process(void *);
274 static int pim_register_send(struct ip *, struct vif *,
275 struct mbuf *, struct mfc *);
276 static int pim_register_send_rp(struct ip *, struct vif *,
277 struct mbuf *, struct mfc *);
278 static int pim_register_send_upcall(struct ip *, struct vif *,
279 struct mbuf *, struct mfc *);
280 static struct mbuf *pim_register_prepare(struct ip *, struct mbuf *);
284 * whether or not special PIM assert processing is enabled.
286 static int pim_assert;
288 * Rate limit for assert notification messages, in usec
290 #define ASSERT_MSG_TIME 3000000
293 * Kernel multicast routing API capabilities and setup.
294 * If more API capabilities are added to the kernel, they should be
295 * recorded in `mrt_api_support'.
297 static const uint32_t mrt_api_support = (MRT_MFC_FLAGS_DISABLE_WRONGVIF |
298 MRT_MFC_FLAGS_BORDER_VIF |
301 static uint32_t mrt_api_config = 0;
304 * Hash function for a source, group entry
306 #define MFCHASH(a, g) MFCHASHMOD(((a) >> 20) ^ ((a) >> 10) ^ (a) ^ \
307 ((g) >> 20) ^ ((g) >> 10) ^ (g))
310 * Find a route for a given origin IP address and Multicast group address
311 * Type of service parameter to be added in the future!!!
312 * Statistics are updated by the caller if needed
313 * (mrtstat.mrts_mfc_lookups and mrtstat.mrts_mfc_misses)
316 mfc_find(in_addr_t o, in_addr_t g)
320 for (rt = mfctable[MFCHASH(o,g)]; rt; rt = rt->mfc_next)
321 if ((rt->mfc_origin.s_addr == o) &&
322 (rt->mfc_mcastgrp.s_addr == g) && (rt->mfc_stall == NULL))
328 * Macros to compute elapsed time efficiently
329 * Borrowed from Van Jacobson's scheduling code
331 #define TV_DELTA(a, b, delta) { \
333 delta = (a).tv_usec - (b).tv_usec; \
334 if ((xxs = (a).tv_sec - (b).tv_sec)) { \
343 delta += (1000000 * xxs); \
348 #define TV_LT(a, b) (((a).tv_usec < (b).tv_usec && \
349 (a).tv_sec <= (b).tv_sec) || (a).tv_sec < (b).tv_sec)
352 * Handle MRT setsockopt commands to modify the multicast routing tables.
355 X_ip_mrouter_set(struct socket *so, struct sockopt *sopt)
361 struct bw_upcall bw_upcall;
364 if (so != ip_mrouter && sopt->sopt_name != MRT_INIT)
368 switch (sopt->sopt_name) {
370 error = sooptcopyin(sopt, &optval, sizeof optval, sizeof optval);
373 error = ip_mrouter_init(so, optval);
377 error = ip_mrouter_done();
381 error = sooptcopyin(sopt, &vifc, sizeof vifc, sizeof vifc);
384 error = add_vif(&vifc);
388 error = sooptcopyin(sopt, &vifi, sizeof vifi, sizeof vifi);
391 error = del_vif(vifi);
397 * select data size depending on API version.
399 if (sopt->sopt_name == MRT_ADD_MFC &&
400 mrt_api_config & MRT_API_FLAGS_ALL) {
401 error = sooptcopyin(sopt, &mfc, sizeof(struct mfcctl2),
402 sizeof(struct mfcctl2));
404 error = sooptcopyin(sopt, &mfc, sizeof(struct mfcctl),
405 sizeof(struct mfcctl));
406 bzero((caddr_t)&mfc + sizeof(struct mfcctl),
407 sizeof(mfc) - sizeof(struct mfcctl));
411 if (sopt->sopt_name == MRT_ADD_MFC)
412 error = add_mfc(&mfc);
414 error = del_mfc(&mfc);
418 error = sooptcopyin(sopt, &optval, sizeof optval, sizeof optval);
425 error = sooptcopyin(sopt, &i, sizeof i, sizeof i);
427 error = set_api_config(&i);
429 error = sooptcopyout(sopt, &i, sizeof i);
432 case MRT_ADD_BW_UPCALL:
433 case MRT_DEL_BW_UPCALL:
434 error = sooptcopyin(sopt, &bw_upcall, sizeof bw_upcall,
438 if (sopt->sopt_name == MRT_ADD_BW_UPCALL)
439 error = add_bw_upcall(&bw_upcall);
441 error = del_bw_upcall(&bw_upcall);
452 * Handle MRT getsockopt commands
455 X_ip_mrouter_get(struct socket *so, struct sockopt *sopt)
458 static int version = 0x0305; /* !!! why is this here? XXX */
460 switch (sopt->sopt_name) {
462 error = sooptcopyout(sopt, &version, sizeof version);
466 error = sooptcopyout(sopt, &pim_assert, sizeof pim_assert);
469 case MRT_API_SUPPORT:
470 error = sooptcopyout(sopt, &mrt_api_support, sizeof mrt_api_support);
474 error = sooptcopyout(sopt, &mrt_api_config, sizeof mrt_api_config);
485 * Handle ioctl commands to obtain information from the cache
488 X_mrt_ioctl(int cmd, caddr_t data)
494 error = get_vif_cnt((struct sioc_vif_req *)data);
498 error = get_sg_cnt((struct sioc_sg_req *)data);
509 * returns the packet, byte, rpf-failure count for the source group provided
512 get_sg_cnt(struct sioc_sg_req *req)
518 rt = mfc_find(req->src.s_addr, req->grp.s_addr);
521 req->pktcnt = req->bytecnt = req->wrong_if = 0xffffffff;
522 return EADDRNOTAVAIL;
524 req->pktcnt = rt->mfc_pkt_cnt;
525 req->bytecnt = rt->mfc_byte_cnt;
526 req->wrong_if = rt->mfc_wrong_if;
531 * returns the input and output packet and byte counts on the vif provided
534 get_vif_cnt(struct sioc_vif_req *req)
536 vifi_t vifi = req->vifi;
541 req->icount = viftable[vifi].v_pkt_in;
542 req->ocount = viftable[vifi].v_pkt_out;
543 req->ibytes = viftable[vifi].v_bytes_in;
544 req->obytes = viftable[vifi].v_bytes_out;
550 * Enable multicast routing
553 ip_mrouter_init(struct socket *so, int version)
556 log(LOG_DEBUG, "ip_mrouter_init: so_type = %d, pr_protocol = %d\n",
557 so->so_type, so->so_proto->pr_protocol);
559 if (so->so_type != SOCK_RAW || so->so_proto->pr_protocol != IPPROTO_IGMP)
565 if (ip_mrouter != NULL)
570 bzero((caddr_t)mfctable, sizeof(mfctable));
571 bzero((caddr_t)nexpire, sizeof(nexpire));
575 expire_upcalls_ch = timeout(expire_upcalls, NULL, EXPIRE_TIMEOUT);
578 bzero((caddr_t)bw_meter_timers, sizeof(bw_meter_timers));
579 bw_upcalls_ch = timeout(expire_bw_upcalls_send, NULL, BW_UPCALLS_PERIOD);
580 bw_meter_ch = timeout(expire_bw_meter_process, NULL, BW_METER_PERIOD);
585 log(LOG_DEBUG, "ip_mrouter_init\n");
591 * Disable multicast routing
594 X_ip_mrouter_done(void)
607 * For each phyint in use, disable promiscuous reception of all IP
610 for (vifi = 0; vifi < numvifs; vifi++) {
611 if (viftable[vifi].v_lcl_addr.s_addr != 0 &&
612 !(viftable[vifi].v_flags & (VIFF_TUNNEL | VIFF_REGISTER))) {
613 struct sockaddr_in *so = (struct sockaddr_in *)&(ifr.ifr_addr);
615 so->sin_len = sizeof(struct sockaddr_in);
616 so->sin_family = AF_INET;
617 so->sin_addr.s_addr = INADDR_ANY;
618 ifp = viftable[vifi].v_ifp;
622 bzero((caddr_t)tbftable, sizeof(tbftable));
623 bzero((caddr_t)viftable, sizeof(viftable));
627 untimeout(expire_upcalls, NULL, expire_upcalls_ch);
631 untimeout(expire_bw_upcalls_send, NULL, bw_upcalls_ch);
632 untimeout(expire_bw_meter_process, NULL, bw_meter_ch);
635 * Free all multicast forwarding cache entries.
637 for (i = 0; i < MFCTBLSIZ; i++) {
638 for (rt = mfctable[i]; rt != NULL; ) {
639 struct mfc *nr = rt->mfc_next;
641 for (rte = rt->mfc_stall; rte != NULL; ) {
642 struct rtdetq *n = rte->next;
645 free(rte, M_MRTABLE);
648 free_bw_list(rt->mfc_bw_meter);
654 bzero((caddr_t)mfctable, sizeof(mfctable));
656 bzero(bw_meter_timers, sizeof(bw_meter_timers));
659 * Reset de-encapsulation cache
661 last_encap_src = INADDR_ANY;
662 last_encap_vif = NULL;
664 reg_vif_num = VIFI_INVALID;
666 have_encap_tunnel = 0;
673 log(LOG_DEBUG, "ip_mrouter_done\n");
679 * Set PIM assert processing global
684 if ((i != 1) && (i != 0))
693 * Configure API capabilities
696 set_api_config(uint32_t *apival)
701 * We can set the API capabilities only if it is the first operation
702 * after MRT_INIT. I.e.:
703 * - there are no vifs installed
704 * - pim_assert is not enabled
705 * - the MFC table is empty
715 for (i = 0; i < MFCTBLSIZ; i++) {
716 if (mfctable[i] != NULL) {
722 mrt_api_config = *apival & mrt_api_support;
723 *apival = mrt_api_config;
729 * Add a vif to the vif table
732 add_vif(struct vifctl *vifcp)
734 struct vif *vifp = viftable + vifcp->vifc_vifi;
735 struct sockaddr_in sin = {sizeof sin, AF_INET};
739 struct tbf *v_tbf = tbftable + vifcp->vifc_vifi;
741 if (vifcp->vifc_vifi >= MAXVIFS)
743 if (vifp->v_lcl_addr.s_addr != INADDR_ANY)
745 if (vifcp->vifc_lcl_addr.s_addr == INADDR_ANY)
746 return EADDRNOTAVAIL;
748 /* Find the interface with an address in AF_INET family */
750 if (vifcp->vifc_flags & VIFF_REGISTER) {
752 * XXX: Because VIFF_REGISTER does not really need a valid
753 * local interface (e.g. it could be 127.0.0.2), we don't
760 sin.sin_addr = vifcp->vifc_lcl_addr;
761 ifa = ifa_ifwithaddr((struct sockaddr *)&sin);
763 return EADDRNOTAVAIL;
767 if (vifcp->vifc_flags & VIFF_TUNNEL) {
768 if ((vifcp->vifc_flags & VIFF_SRCRT) == 0) {
770 * An encapsulating tunnel is wanted. Tell ipip_input() to
771 * start paying attention to encapsulated packets.
773 if (have_encap_tunnel == 0) {
774 have_encap_tunnel = 1;
775 for (s = 0; s < MAXVIFS; ++s) {
776 multicast_decap_if[s].if_name = "mdecap";
777 multicast_decap_if[s].if_unit = s;
781 * Set interface to fake encapsulator interface
783 ifp = &multicast_decap_if[vifcp->vifc_vifi];
785 * Prepare cached route entry
787 bzero(&vifp->v_route, sizeof(vifp->v_route));
789 log(LOG_ERR, "source routed tunnels not supported\n");
793 } else if (vifcp->vifc_flags & VIFF_REGISTER) {
794 ifp = &multicast_register_if;
796 log(LOG_DEBUG, "Adding a register vif, ifp: %p\n",
797 (void *)&multicast_register_if);
798 if (reg_vif_num == VIFI_INVALID) {
799 multicast_register_if.if_name = "register_vif";
800 multicast_register_if.if_unit = 0;
801 multicast_register_if.if_flags = IFF_LOOPBACK;
802 bzero(&vifp->v_route, sizeof(vifp->v_route));
803 reg_vif_num = vifcp->vifc_vifi;
806 } else { /* Make sure the interface supports multicast */
807 if ((ifp->if_flags & IFF_MULTICAST) == 0)
810 /* Enable promiscuous reception of all IP multicasts from the if */
812 error = if_allmulti(ifp, 1);
819 /* define parameters for the tbf structure */
821 GET_TIME(vifp->v_tbf->tbf_last_pkt_t);
822 vifp->v_tbf->tbf_n_tok = 0;
823 vifp->v_tbf->tbf_q_len = 0;
824 vifp->v_tbf->tbf_max_q_len = MAXQSIZE;
825 vifp->v_tbf->tbf_q = vifp->v_tbf->tbf_t = NULL;
827 vifp->v_flags = vifcp->vifc_flags;
828 vifp->v_threshold = vifcp->vifc_threshold;
829 vifp->v_lcl_addr = vifcp->vifc_lcl_addr;
830 vifp->v_rmt_addr = vifcp->vifc_rmt_addr;
832 /* scaling up here allows division by 1024 in critical code */
833 vifp->v_rate_limit= vifcp->vifc_rate_limit * 1024 / 1000;
835 vifp->v_rsvpd = NULL;
836 /* initialize per vif pkt counters */
839 vifp->v_bytes_in = 0;
840 vifp->v_bytes_out = 0;
843 /* Adjust numvifs up if the vifi is higher than numvifs */
844 if (numvifs <= vifcp->vifc_vifi) numvifs = vifcp->vifc_vifi + 1;
847 log(LOG_DEBUG, "add_vif #%d, lcladdr %lx, %s %lx, thresh %x, rate %d\n",
849 (u_long)ntohl(vifcp->vifc_lcl_addr.s_addr),
850 (vifcp->vifc_flags & VIFF_TUNNEL) ? "rmtaddr" : "mask",
851 (u_long)ntohl(vifcp->vifc_rmt_addr.s_addr),
852 vifcp->vifc_threshold,
853 vifcp->vifc_rate_limit);
859 * Delete a vif from the vif table
869 vifp = &viftable[vifi];
870 if (vifp->v_lcl_addr.s_addr == INADDR_ANY)
871 return EADDRNOTAVAIL;
875 if (!(vifp->v_flags & (VIFF_TUNNEL | VIFF_REGISTER)))
876 if_allmulti(vifp->v_ifp, 0);
878 if (vifp == last_encap_vif) {
879 last_encap_vif = NULL;
880 last_encap_src = INADDR_ANY;
884 * Free packets queued at the interface
886 while (vifp->v_tbf->tbf_q) {
887 struct mbuf *m = vifp->v_tbf->tbf_q;
889 vifp->v_tbf->tbf_q = m->m_act;
894 if (vifp->v_flags & VIFF_REGISTER)
895 reg_vif_num = VIFI_INVALID;
898 bzero((caddr_t)vifp->v_tbf, sizeof(*(vifp->v_tbf)));
899 bzero((caddr_t)vifp, sizeof (*vifp));
902 log(LOG_DEBUG, "del_vif %d, numvifs %d\n", vifi, numvifs);
904 /* Adjust numvifs down */
905 for (vifi = numvifs; vifi > 0; vifi--)
906 if (viftable[vifi-1].v_lcl_addr.s_addr != INADDR_ANY)
916 * update an mfc entry without resetting counters and S,G addresses.
919 update_mfc_params(struct mfc *rt, struct mfcctl2 *mfccp)
923 rt->mfc_parent = mfccp->mfcc_parent;
924 for (i = 0; i < numvifs; i++) {
925 rt->mfc_ttls[i] = mfccp->mfcc_ttls[i];
926 rt->mfc_flags[i] = mfccp->mfcc_flags[i] & mrt_api_config &
929 /* set the RP address */
930 if (mrt_api_config & MRT_MFC_RP)
931 rt->mfc_rp = mfccp->mfcc_rp;
933 rt->mfc_rp.s_addr = INADDR_ANY;
937 * fully initialize an mfc entry from the parameter.
940 init_mfc_params(struct mfc *rt, struct mfcctl2 *mfccp)
942 rt->mfc_origin = mfccp->mfcc_origin;
943 rt->mfc_mcastgrp = mfccp->mfcc_mcastgrp;
945 update_mfc_params(rt, mfccp);
947 /* initialize pkt counters per src-grp */
949 rt->mfc_byte_cnt = 0;
950 rt->mfc_wrong_if = 0;
951 rt->mfc_last_assert.tv_sec = rt->mfc_last_assert.tv_usec = 0;
959 add_mfc(struct mfcctl2 *mfccp)
967 rt = mfc_find(mfccp->mfcc_origin.s_addr, mfccp->mfcc_mcastgrp.s_addr);
969 /* If an entry already exists, just update the fields */
971 if (mrtdebug & DEBUG_MFC)
972 log(LOG_DEBUG,"add_mfc update o %lx g %lx p %x\n",
973 (u_long)ntohl(mfccp->mfcc_origin.s_addr),
974 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
978 update_mfc_params(rt, mfccp);
984 * Find the entry for which the upcall was made and update
987 hash = MFCHASH(mfccp->mfcc_origin.s_addr, mfccp->mfcc_mcastgrp.s_addr);
988 for (rt = mfctable[hash], nstl = 0; rt; rt = rt->mfc_next) {
990 if ((rt->mfc_origin.s_addr == mfccp->mfcc_origin.s_addr) &&
991 (rt->mfc_mcastgrp.s_addr == mfccp->mfcc_mcastgrp.s_addr) &&
992 (rt->mfc_stall != NULL)) {
995 log(LOG_ERR, "add_mfc %s o %lx g %lx p %x dbx %p\n",
996 "multiple kernel entries",
997 (u_long)ntohl(mfccp->mfcc_origin.s_addr),
998 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
999 mfccp->mfcc_parent, (void *)rt->mfc_stall);
1001 if (mrtdebug & DEBUG_MFC)
1002 log(LOG_DEBUG,"add_mfc o %lx g %lx p %x dbg %p\n",
1003 (u_long)ntohl(mfccp->mfcc_origin.s_addr),
1004 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
1005 mfccp->mfcc_parent, (void *)rt->mfc_stall);
1007 init_mfc_params(rt, mfccp);
1009 rt->mfc_expire = 0; /* Don't clean this guy up */
1012 /* free packets Qed at the end of this entry */
1013 for (rte = rt->mfc_stall; rte != NULL; ) {
1014 struct rtdetq *n = rte->next;
1016 ip_mdq(rte->m, rte->ifp, rt, -1);
1018 free(rte, M_MRTABLE);
1021 rt->mfc_stall = NULL;
1026 * It is possible that an entry is being inserted without an upcall
1029 if (mrtdebug & DEBUG_MFC)
1030 log(LOG_DEBUG,"add_mfc no upcall h %lu o %lx g %lx p %x\n",
1031 hash, (u_long)ntohl(mfccp->mfcc_origin.s_addr),
1032 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
1033 mfccp->mfcc_parent);
1035 for (rt = mfctable[hash]; rt != NULL; rt = rt->mfc_next) {
1036 if ((rt->mfc_origin.s_addr == mfccp->mfcc_origin.s_addr) &&
1037 (rt->mfc_mcastgrp.s_addr == mfccp->mfcc_mcastgrp.s_addr)) {
1038 init_mfc_params(rt, mfccp);
1045 if (rt == NULL) { /* no upcall, so make a new entry */
1046 rt = (struct mfc *)malloc(sizeof(*rt), M_MRTABLE, M_NOWAIT);
1052 init_mfc_params(rt, mfccp);
1054 rt->mfc_stall = NULL;
1056 rt->mfc_bw_meter = NULL;
1057 /* insert new entry at head of hash chain */
1058 rt->mfc_next = mfctable[hash];
1059 mfctable[hash] = rt;
1067 * Delete an mfc entry
1070 del_mfc(struct mfcctl2 *mfccp)
1072 struct in_addr origin;
1073 struct in_addr mcastgrp;
1078 struct bw_meter *list;
1080 origin = mfccp->mfcc_origin;
1081 mcastgrp = mfccp->mfcc_mcastgrp;
1083 if (mrtdebug & DEBUG_MFC)
1084 log(LOG_DEBUG,"del_mfc orig %lx mcastgrp %lx\n",
1085 (u_long)ntohl(origin.s_addr), (u_long)ntohl(mcastgrp.s_addr));
1089 hash = MFCHASH(origin.s_addr, mcastgrp.s_addr);
1090 for (nptr = &mfctable[hash]; (rt = *nptr) != NULL; nptr = &rt->mfc_next)
1091 if (origin.s_addr == rt->mfc_origin.s_addr &&
1092 mcastgrp.s_addr == rt->mfc_mcastgrp.s_addr &&
1093 rt->mfc_stall == NULL)
1097 return EADDRNOTAVAIL;
1100 *nptr = rt->mfc_next;
1103 * free the bw_meter entries
1105 list = rt->mfc_bw_meter;
1106 rt->mfc_bw_meter = NULL;
1108 free(rt, M_MRTABLE);
1118 * Send a message to mrouted on the multicast routing socket
1121 socket_send(struct socket *s, struct mbuf *mm, struct sockaddr_in *src)
1124 if (sbappendaddr(&s->so_rcv, (struct sockaddr *)src, mm, NULL) != 0) {
1134 * IP multicast forwarding function. This function assumes that the packet
1135 * pointed to by "ip" has arrived on (or is about to be sent to) the interface
1136 * pointed to by "ifp", and the packet is to be relayed to other networks
1137 * that have members of the packet's destination IP multicast group.
1139 * The packet is returned unscathed to the caller, unless it is
1140 * erroneous, in which case a non-zero return value tells the caller to
1144 #define TUNNEL_LEN 12 /* # bytes of IP option for tunnel encapsulation */
1147 X_ip_mforward(struct ip *ip, struct ifnet *ifp, struct mbuf *m,
1148 struct ip_moptions *imo)
1154 if (mrtdebug & DEBUG_FORWARD)
1155 log(LOG_DEBUG, "ip_mforward: src %lx, dst %lx, ifp %p\n",
1156 (u_long)ntohl(ip->ip_src.s_addr), (u_long)ntohl(ip->ip_dst.s_addr),
1159 if (ip->ip_hl < (sizeof(struct ip) + TUNNEL_LEN) >> 2 ||
1160 ((u_char *)(ip + 1))[1] != IPOPT_LSRR ) {
1162 * Packet arrived via a physical interface or
1163 * an encapsulated tunnel or a register_vif.
1167 * Packet arrived through a source-route tunnel.
1168 * Source-route tunnels are no longer supported.
1170 static int last_log;
1171 if (last_log != time_second) {
1172 last_log = time_second;
1174 "ip_mforward: received source-routed packet from %lx\n",
1175 (u_long)ntohl(ip->ip_src.s_addr));
1180 if (imo && ((vifi = imo->imo_multicast_vif) < numvifs)) {
1181 if (ip->ip_ttl < 255)
1182 ip->ip_ttl++; /* compensate for -1 in *_send routines */
1183 if (rsvpdebug && ip->ip_p == IPPROTO_RSVP) {
1184 struct vif *vifp = viftable + vifi;
1186 printf("Sending IPPROTO_RSVP from %lx to %lx on vif %d (%s%s%d)\n",
1187 (long)ntohl(ip->ip_src.s_addr), (long)ntohl(ip->ip_dst.s_addr),
1189 (vifp->v_flags & VIFF_TUNNEL) ? "tunnel on " : "",
1190 vifp->v_ifp->if_name, vifp->v_ifp->if_unit);
1192 return ip_mdq(m, ifp, NULL, vifi);
1194 if (rsvpdebug && ip->ip_p == IPPROTO_RSVP) {
1195 printf("Warning: IPPROTO_RSVP from %lx to %lx without vif option\n",
1196 (long)ntohl(ip->ip_src.s_addr), (long)ntohl(ip->ip_dst.s_addr));
1198 printf("In fact, no options were specified at all\n");
1202 * Don't forward a packet with time-to-live of zero or one,
1203 * or a packet destined to a local-only group.
1205 if (ip->ip_ttl <= 1 || ntohl(ip->ip_dst.s_addr) <= INADDR_MAX_LOCAL_GROUP)
1209 * Determine forwarding vifs from the forwarding cache table
1212 ++mrtstat.mrts_mfc_lookups;
1213 rt = mfc_find(ip->ip_src.s_addr, ip->ip_dst.s_addr);
1215 /* Entry exists, so forward if necessary */
1218 return ip_mdq(m, ifp, rt, -1);
1221 * If we don't have a route for packet's origin,
1222 * Make a copy of the packet & send message to routing daemon
1228 int hlen = ip->ip_hl << 2;
1230 ++mrtstat.mrts_mfc_misses;
1232 mrtstat.mrts_no_route++;
1233 if (mrtdebug & (DEBUG_FORWARD | DEBUG_MFC))
1234 log(LOG_DEBUG, "ip_mforward: no rte s %lx g %lx\n",
1235 (u_long)ntohl(ip->ip_src.s_addr),
1236 (u_long)ntohl(ip->ip_dst.s_addr));
1239 * Allocate mbufs early so that we don't do extra work if we are
1240 * just going to fail anyway. Make sure to pullup the header so
1241 * that other people can't step on it.
1243 rte = (struct rtdetq *)malloc((sizeof *rte), M_MRTABLE, M_NOWAIT);
1248 mb0 = m_copypacket(m, M_DONTWAIT);
1249 if (mb0 && (M_HASCL(mb0) || mb0->m_len < hlen))
1250 mb0 = m_pullup(mb0, hlen);
1252 free(rte, M_MRTABLE);
1257 /* is there an upcall waiting for this flow ? */
1258 hash = MFCHASH(ip->ip_src.s_addr, ip->ip_dst.s_addr);
1259 for (rt = mfctable[hash]; rt; rt = rt->mfc_next) {
1260 if ((ip->ip_src.s_addr == rt->mfc_origin.s_addr) &&
1261 (ip->ip_dst.s_addr == rt->mfc_mcastgrp.s_addr) &&
1262 (rt->mfc_stall != NULL))
1269 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
1273 * Locate the vifi for the incoming interface for this packet.
1274 * If none found, drop packet.
1276 for (vifi=0; vifi < numvifs && viftable[vifi].v_ifp != ifp; vifi++)
1278 if (vifi >= numvifs) /* vif not found, drop packet */
1281 /* no upcall, so make a new entry */
1282 rt = (struct mfc *)malloc(sizeof(*rt), M_MRTABLE, M_NOWAIT);
1285 /* Make a copy of the header to send to the user level process */
1286 mm = m_copy(mb0, 0, hlen);
1291 * Send message to routing daemon to install
1292 * a route into the kernel table
1295 im = mtod(mm, struct igmpmsg *);
1296 im->im_msgtype = IGMPMSG_NOCACHE;
1300 mrtstat.mrts_upcalls++;
1302 k_igmpsrc.sin_addr = ip->ip_src;
1303 if (socket_send(ip_mrouter, mm, &k_igmpsrc) < 0) {
1304 log(LOG_WARNING, "ip_mforward: ip_mrouter socket queue full\n");
1305 ++mrtstat.mrts_upq_sockfull;
1307 free(rt, M_MRTABLE);
1309 free(rte, M_MRTABLE);
1315 /* insert new entry at head of hash chain */
1316 rt->mfc_origin.s_addr = ip->ip_src.s_addr;
1317 rt->mfc_mcastgrp.s_addr = ip->ip_dst.s_addr;
1318 rt->mfc_expire = UPCALL_EXPIRE;
1320 for (i = 0; i < numvifs; i++) {
1321 rt->mfc_ttls[i] = 0;
1322 rt->mfc_flags[i] = 0;
1324 rt->mfc_parent = -1;
1326 rt->mfc_rp.s_addr = INADDR_ANY; /* clear the RP address */
1328 rt->mfc_bw_meter = NULL;
1330 /* link into table */
1331 rt->mfc_next = mfctable[hash];
1332 mfctable[hash] = rt;
1333 rt->mfc_stall = rte;
1336 /* determine if q has overflowed */
1341 * XXX ouch! we need to append to the list, but we
1342 * only have a pointer to the front, so we have to
1343 * scan the entire list every time.
1345 for (p = &rt->mfc_stall; *p != NULL; p = &(*p)->next)
1348 if (npkts > MAX_UPQ) {
1349 mrtstat.mrts_upq_ovflw++;
1351 free(rte, M_MRTABLE);
1357 /* Add this entry to the end of the queue */
1372 * Clean up the cache entry if upcall is not serviced
1375 expire_upcalls(void *unused)
1378 struct mfc *mfc, **nptr;
1383 for (i = 0; i < MFCTBLSIZ; i++) {
1384 if (nexpire[i] == 0)
1386 nptr = &mfctable[i];
1387 for (mfc = *nptr; mfc != NULL; mfc = *nptr) {
1389 * Skip real cache entries
1390 * Make sure it wasn't marked to not expire (shouldn't happen)
1393 if (mfc->mfc_stall != NULL && mfc->mfc_expire != 0 &&
1394 --mfc->mfc_expire == 0) {
1395 if (mrtdebug & DEBUG_EXPIRE)
1396 log(LOG_DEBUG, "expire_upcalls: expiring (%lx %lx)\n",
1397 (u_long)ntohl(mfc->mfc_origin.s_addr),
1398 (u_long)ntohl(mfc->mfc_mcastgrp.s_addr));
1400 * drop all the packets
1401 * free the mbuf with the pkt, if, timing info
1403 for (rte = mfc->mfc_stall; rte; ) {
1404 struct rtdetq *n = rte->next;
1407 free(rte, M_MRTABLE);
1410 ++mrtstat.mrts_cache_cleanups;
1414 * free the bw_meter entries
1416 while (mfc->mfc_bw_meter != NULL) {
1417 struct bw_meter *x = mfc->mfc_bw_meter;
1419 mfc->mfc_bw_meter = x->bm_mfc_next;
1423 *nptr = mfc->mfc_next;
1424 free(mfc, M_MRTABLE);
1426 nptr = &mfc->mfc_next;
1431 expire_upcalls_ch = timeout(expire_upcalls, NULL, EXPIRE_TIMEOUT);
1435 * Packet forwarding routine once entry in the cache is made
1438 ip_mdq(struct mbuf *m, struct ifnet *ifp, struct mfc *rt, vifi_t xmt_vif)
1440 struct ip *ip = mtod(m, struct ip *);
1442 int plen = ip->ip_len;
1445 * Macro to send packet on vif. Since RSVP packets don't get counted on
1446 * input, they shouldn't get counted on output, so statistics keeping is
1449 #define MC_SEND(ip,vifp,m) { \
1450 if ((vifp)->v_flags & VIFF_TUNNEL) \
1451 encap_send((ip), (vifp), (m)); \
1453 phyint_send((ip), (vifp), (m)); \
1457 * If xmt_vif is not -1, send on only the requested vif.
1459 * (since vifi_t is u_short, -1 becomes MAXUSHORT, which > numvifs.)
1461 if (xmt_vif < numvifs) {
1463 if (viftable[xmt_vif].v_flags & VIFF_REGISTER)
1464 pim_register_send(ip, viftable + xmt_vif, m, rt);
1467 MC_SEND(ip, viftable + xmt_vif, m);
1472 * Don't forward if it didn't arrive from the parent vif for its origin.
1474 vifi = rt->mfc_parent;
1475 if ((vifi >= numvifs) || (viftable[vifi].v_ifp != ifp)) {
1476 /* came in the wrong interface */
1477 if (mrtdebug & DEBUG_FORWARD)
1478 log(LOG_DEBUG, "wrong if: ifp %p vifi %d vififp %p\n",
1479 (void *)ifp, vifi, (void *)viftable[vifi].v_ifp);
1480 ++mrtstat.mrts_wrong_if;
1483 * If we are doing PIM assert processing, send a message
1484 * to the routing daemon.
1486 * XXX: A PIM-SM router needs the WRONGVIF detection so it
1487 * can complete the SPT switch, regardless of the type
1488 * of the iif (broadcast media, GRE tunnel, etc).
1490 if (pim_assert && (vifi < numvifs) && viftable[vifi].v_ifp) {
1495 if (ifp == &multicast_register_if)
1496 pimstat.pims_rcv_registers_wrongiif++;
1499 /* Get vifi for the incoming packet */
1500 for (vifi=0; vifi < numvifs && viftable[vifi].v_ifp != ifp; vifi++)
1502 if (vifi >= numvifs)
1503 return 0; /* The iif is not found: ignore the packet. */
1505 if (rt->mfc_flags[vifi] & MRT_MFC_FLAGS_DISABLE_WRONGVIF)
1506 return 0; /* WRONGVIF disabled: ignore the packet */
1510 TV_DELTA(rt->mfc_last_assert, now, delta);
1512 if (delta > ASSERT_MSG_TIME) {
1513 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
1515 int hlen = ip->ip_hl << 2;
1516 struct mbuf *mm = m_copy(m, 0, hlen);
1518 if (mm && (M_HASCL(mm) || mm->m_len < hlen))
1519 mm = m_pullup(mm, hlen);
1523 rt->mfc_last_assert = now;
1525 im = mtod(mm, struct igmpmsg *);
1526 im->im_msgtype = IGMPMSG_WRONGVIF;
1530 mrtstat.mrts_upcalls++;
1532 k_igmpsrc.sin_addr = im->im_src;
1533 if (socket_send(ip_mrouter, mm, &k_igmpsrc) < 0) {
1535 "ip_mforward: ip_mrouter socket queue full\n");
1536 ++mrtstat.mrts_upq_sockfull;
1544 /* If I sourced this packet, it counts as output, else it was input. */
1545 if (ip->ip_src.s_addr == viftable[vifi].v_lcl_addr.s_addr) {
1546 viftable[vifi].v_pkt_out++;
1547 viftable[vifi].v_bytes_out += plen;
1549 viftable[vifi].v_pkt_in++;
1550 viftable[vifi].v_bytes_in += plen;
1553 rt->mfc_byte_cnt += plen;
1556 * For each vif, decide if a copy of the packet should be forwarded.
1558 * - the ttl exceeds the vif's threshold
1559 * - there are group members downstream on interface
1561 for (vifi = 0; vifi < numvifs; vifi++)
1562 if ((rt->mfc_ttls[vifi] > 0) && (ip->ip_ttl > rt->mfc_ttls[vifi])) {
1563 viftable[vifi].v_pkt_out++;
1564 viftable[vifi].v_bytes_out += plen;
1566 if (viftable[vifi].v_flags & VIFF_REGISTER)
1567 pim_register_send(ip, viftable + vifi, m, rt);
1570 MC_SEND(ip, viftable+vifi, m);
1574 * Perform upcall-related bw measuring.
1576 if (rt->mfc_bw_meter != NULL) {
1581 for (x = rt->mfc_bw_meter; x != NULL; x = x->bm_mfc_next)
1582 bw_meter_receive_packet(x, plen, &now);
1589 * check if a vif number is legal/ok. This is used by ip_output.
1592 X_legal_vif_num(int vif)
1594 return (vif >= 0 && vif < numvifs);
1598 * Return the local address used by this vif
1601 X_ip_mcast_src(int vifi)
1603 if (vifi >= 0 && vifi < numvifs)
1604 return viftable[vifi].v_lcl_addr.s_addr;
1610 phyint_send(struct ip *ip, struct vif *vifp, struct mbuf *m)
1612 struct mbuf *mb_copy;
1613 int hlen = ip->ip_hl << 2;
1616 * Make a new reference to the packet; make sure that
1617 * the IP header is actually copied, not just referenced,
1618 * so that ip_output() only scribbles on the copy.
1620 mb_copy = m_copypacket(m, M_DONTWAIT);
1621 if (mb_copy && (M_HASCL(mb_copy) || mb_copy->m_len < hlen))
1622 mb_copy = m_pullup(mb_copy, hlen);
1623 if (mb_copy == NULL)
1626 if (vifp->v_rate_limit == 0)
1627 tbf_send_packet(vifp, mb_copy);
1629 tbf_control(vifp, mb_copy, mtod(mb_copy, struct ip *), ip->ip_len);
1633 encap_send(struct ip *ip, struct vif *vifp, struct mbuf *m)
1635 struct mbuf *mb_copy;
1637 int i, len = ip->ip_len;
1639 /* Take care of delayed checksums */
1640 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
1641 in_delayed_cksum(m);
1642 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
1646 * copy the old packet & pullup its IP header into the
1647 * new mbuf so we can modify it. Try to fill the new
1648 * mbuf since if we don't the ethernet driver will.
1650 MGETHDR(mb_copy, M_DONTWAIT, MT_HEADER);
1651 if (mb_copy == NULL)
1653 mb_copy->m_data += max_linkhdr;
1654 mb_copy->m_len = sizeof(multicast_encap_iphdr);
1656 if ((mb_copy->m_next = m_copypacket(m, M_DONTWAIT)) == NULL) {
1660 i = MHLEN - M_LEADINGSPACE(mb_copy);
1663 mb_copy = m_pullup(mb_copy, i);
1664 if (mb_copy == NULL)
1666 mb_copy->m_pkthdr.len = len + sizeof(multicast_encap_iphdr);
1669 * fill in the encapsulating IP header.
1671 ip_copy = mtod(mb_copy, struct ip *);
1672 *ip_copy = multicast_encap_iphdr;
1674 ip_copy->ip_id = ip_randomid();
1676 ip_copy->ip_id = htons(ip_id++);
1678 ip_copy->ip_len += len;
1679 ip_copy->ip_src = vifp->v_lcl_addr;
1680 ip_copy->ip_dst = vifp->v_rmt_addr;
1683 * turn the encapsulated IP header back into a valid one.
1685 ip = (struct ip *)((caddr_t)ip_copy + sizeof(multicast_encap_iphdr));
1687 ip->ip_len = htons(ip->ip_len);
1688 ip->ip_off = htons(ip->ip_off);
1690 mb_copy->m_data += sizeof(multicast_encap_iphdr);
1691 ip->ip_sum = in_cksum(mb_copy, ip->ip_hl << 2);
1692 mb_copy->m_data -= sizeof(multicast_encap_iphdr);
1694 if (vifp->v_rate_limit == 0)
1695 tbf_send_packet(vifp, mb_copy);
1697 tbf_control(vifp, mb_copy, ip, ip_copy->ip_len);
1701 * De-encapsulate a packet and feed it back through ip input (this
1702 * routine is called whenever IP gets a packet with proto type
1703 * ENCAP_PROTO and a local destination address).
1705 * This is similar to mroute_encapcheck() + mroute_encap_input() in -current.
1708 X_ipip_input(struct mbuf *m, int off, int proto)
1710 struct ip *ip = mtod(m, struct ip *);
1711 int hlen = ip->ip_hl << 2;
1713 if (!have_encap_tunnel) {
1714 rip_input(m, off, proto);
1718 * dump the packet if it's not to a multicast destination or if
1719 * we don't have an encapsulating tunnel with the source.
1720 * Note: This code assumes that the remote site IP address
1721 * uniquely identifies the tunnel (i.e., that this site has
1722 * at most one tunnel with the remote site).
1724 if (!IN_MULTICAST(ntohl(((struct ip *)((char *)ip+hlen))->ip_dst.s_addr))) {
1725 ++mrtstat.mrts_bad_tunnel;
1729 if (ip->ip_src.s_addr != last_encap_src) {
1730 struct vif *vifp = viftable;
1731 struct vif *vife = vifp + numvifs;
1733 last_encap_src = ip->ip_src.s_addr;
1734 last_encap_vif = NULL;
1735 for ( ; vifp < vife; ++vifp)
1736 if (vifp->v_rmt_addr.s_addr == ip->ip_src.s_addr) {
1737 if ((vifp->v_flags & (VIFF_TUNNEL|VIFF_SRCRT))
1739 last_encap_vif = vifp;
1743 if (last_encap_vif == NULL) {
1744 last_encap_src = INADDR_ANY;
1745 mrtstat.mrts_cant_tunnel++; /*XXX*/
1748 log(LOG_DEBUG, "ip_mforward: no tunnel with %lx\n",
1749 (u_long)ntohl(ip->ip_src.s_addr));
1753 if (hlen > sizeof(struct ip))
1754 ip_stripoptions(m, NULL);
1755 m->m_data += sizeof(struct ip);
1756 m->m_len -= sizeof(struct ip);
1757 m->m_pkthdr.len -= sizeof(struct ip);
1758 m->m_pkthdr.rcvif = last_encap_vif->v_ifp;
1760 netisr_queue(NETISR_IP, m);
1764 * Token bucket filter module
1768 tbf_control(struct vif *vifp, struct mbuf *m, struct ip *ip, u_long p_len)
1770 struct tbf *t = vifp->v_tbf;
1772 if (p_len > MAX_BKT_SIZE) { /* drop if packet is too large */
1773 mrtstat.mrts_pkt2large++;
1778 tbf_update_tokens(vifp);
1780 if (t->tbf_q_len == 0) { /* queue empty... */
1781 if (p_len <= t->tbf_n_tok) { /* send packet if enough tokens */
1782 t->tbf_n_tok -= p_len;
1783 tbf_send_packet(vifp, m);
1784 } else { /* no, queue packet and try later */
1786 timeout(tbf_reprocess_q, (caddr_t)vifp, TBF_REPROCESS);
1788 } else if (t->tbf_q_len < t->tbf_max_q_len) {
1789 /* finite queue length, so queue pkts and process queue */
1791 tbf_process_q(vifp);
1793 /* queue full, try to dq and queue and process */
1794 if (!tbf_dq_sel(vifp, ip)) {
1795 mrtstat.mrts_q_overflow++;
1799 tbf_process_q(vifp);
1805 * adds a packet to the queue at the interface
1808 tbf_queue(struct vif *vifp, struct mbuf *m)
1811 struct tbf *t = vifp->v_tbf;
1813 if (t->tbf_t == NULL) /* Queue was empty */
1815 else /* Insert at tail */
1816 t->tbf_t->m_act = m;
1818 t->tbf_t = m; /* Set new tail pointer */
1821 /* Make sure we didn't get fed a bogus mbuf */
1823 panic("tbf_queue: m_act");
1833 * processes the queue at the interface
1836 tbf_process_q(struct vif *vifp)
1839 struct tbf *t = vifp->v_tbf;
1841 /* loop through the queue at the interface and send as many packets
1844 while (t->tbf_q_len > 0) {
1845 struct mbuf *m = t->tbf_q;
1846 int len = mtod(m, struct ip *)->ip_len;
1848 /* determine if the packet can be sent */
1849 if (len > t->tbf_n_tok) /* not enough tokens, we are done */
1851 /* ok, reduce no of tokens, dequeue and send the packet. */
1852 t->tbf_n_tok -= len;
1854 t->tbf_q = m->m_act;
1855 if (--t->tbf_q_len == 0)
1859 tbf_send_packet(vifp, m);
1865 tbf_reprocess_q(void *xvifp)
1867 struct vif *vifp = xvifp;
1869 if (ip_mrouter == NULL)
1871 tbf_update_tokens(vifp);
1872 tbf_process_q(vifp);
1873 if (vifp->v_tbf->tbf_q_len)
1874 timeout(tbf_reprocess_q, (caddr_t)vifp, TBF_REPROCESS);
1877 /* function that will selectively discard a member of the queue
1878 * based on the precedence value and the priority
1881 tbf_dq_sel(struct vif *vifp, struct ip *ip)
1885 struct mbuf *m, *last;
1887 struct tbf *t = vifp->v_tbf;
1889 p = priority(vifp, ip);
1893 while ((m = *np) != NULL) {
1894 if (p > priority(vifp, mtod(m, struct ip *))) {
1896 /* If we're removing the last packet, fix the tail pointer */
1900 /* It's impossible for the queue to be empty, but check anyways. */
1901 if (--t->tbf_q_len == 0)
1904 mrtstat.mrts_drop_sel++;
1915 tbf_send_packet(struct vif *vifp, struct mbuf *m)
1919 if (vifp->v_flags & VIFF_TUNNEL) /* If tunnel options */
1920 ip_output(m, NULL, &vifp->v_route, IP_FORWARDING, NULL, NULL);
1922 struct ip_moptions imo;
1924 static struct route ro; /* XXX check this */
1926 imo.imo_multicast_ifp = vifp->v_ifp;
1927 imo.imo_multicast_ttl = mtod(m, struct ip *)->ip_ttl - 1;
1928 imo.imo_multicast_loop = 1;
1929 imo.imo_multicast_vif = -1;
1932 * Re-entrancy should not be a problem here, because
1933 * the packets that we send out and are looped back at us
1934 * should get rejected because they appear to come from
1935 * the loopback interface, thus preventing looping.
1937 error = ip_output(m, NULL, &ro, IP_FORWARDING, &imo, NULL);
1939 if (mrtdebug & DEBUG_XMIT)
1940 log(LOG_DEBUG, "phyint_send on vif %d err %d\n",
1941 (int)(vifp - viftable), error);
1946 /* determine the current time and then
1947 * the elapsed time (between the last time and time now)
1948 * in milliseconds & update the no. of tokens in the bucket
1951 tbf_update_tokens(struct vif *vifp)
1956 struct tbf *t = vifp->v_tbf;
1960 TV_DELTA(tp, t->tbf_last_pkt_t, tm);
1963 * This formula is actually
1964 * "time in seconds" * "bytes/second".
1966 * (tm / 1000000) * (v_rate_limit * 1000 * (1000/1024) / 8)
1968 * The (1000/1024) was introduced in add_vif to optimize
1969 * this divide into a shift.
1971 t->tbf_n_tok += tm * vifp->v_rate_limit / 1024 / 8;
1972 t->tbf_last_pkt_t = tp;
1974 if (t->tbf_n_tok > MAX_BKT_SIZE)
1975 t->tbf_n_tok = MAX_BKT_SIZE;
1981 priority(struct vif *vifp, struct ip *ip)
1983 int prio = 50; /* the lowest priority -- default case */
1985 /* temporary hack; may add general packet classifier some day */
1988 * The UDP port space is divided up into four priority ranges:
1989 * [0, 16384) : unclassified - lowest priority
1990 * [16384, 32768) : audio - highest priority
1991 * [32768, 49152) : whiteboard - medium priority
1992 * [49152, 65536) : video - low priority
1994 * Everything else gets lowest priority.
1996 if (ip->ip_p == IPPROTO_UDP) {
1997 struct udphdr *udp = (struct udphdr *)(((char *)ip) + (ip->ip_hl << 2));
1998 switch (ntohs(udp->uh_dport) & 0xc000) {
2014 * End of token bucket filter modifications
2018 X_ip_rsvp_vif(struct socket *so, struct sockopt *sopt)
2022 if (so->so_type != SOCK_RAW || so->so_proto->pr_protocol != IPPROTO_RSVP)
2025 error = sooptcopyin(sopt, &vifi, sizeof vifi, sizeof vifi);
2031 if (vifi < 0 || vifi >= numvifs) { /* Error if vif is invalid */
2033 return EADDRNOTAVAIL;
2036 if (sopt->sopt_name == IP_RSVP_VIF_ON) {
2037 /* Check if socket is available. */
2038 if (viftable[vifi].v_rsvpd != NULL) {
2043 viftable[vifi].v_rsvpd = so;
2044 /* This may seem silly, but we need to be sure we don't over-increment
2045 * the RSVP counter, in case something slips up.
2047 if (!viftable[vifi].v_rsvp_on) {
2048 viftable[vifi].v_rsvp_on = 1;
2051 } else { /* must be VIF_OFF */
2053 * XXX as an additional consistency check, one could make sure
2054 * that viftable[vifi].v_rsvpd == so, otherwise passing so as
2055 * first parameter is pretty useless.
2057 viftable[vifi].v_rsvpd = NULL;
2059 * This may seem silly, but we need to be sure we don't over-decrement
2060 * the RSVP counter, in case something slips up.
2062 if (viftable[vifi].v_rsvp_on) {
2063 viftable[vifi].v_rsvp_on = 0;
2072 X_ip_rsvp_force_done(struct socket *so)
2077 /* Don't bother if it is not the right type of socket. */
2078 if (so->so_type != SOCK_RAW || so->so_proto->pr_protocol != IPPROTO_RSVP)
2083 /* The socket may be attached to more than one vif...this
2084 * is perfectly legal.
2086 for (vifi = 0; vifi < numvifs; vifi++) {
2087 if (viftable[vifi].v_rsvpd == so) {
2088 viftable[vifi].v_rsvpd = NULL;
2089 /* This may seem silly, but we need to be sure we don't
2090 * over-decrement the RSVP counter, in case something slips up.
2092 if (viftable[vifi].v_rsvp_on) {
2093 viftable[vifi].v_rsvp_on = 0;
2103 X_rsvp_input(struct mbuf *m, int off, int proto)
2106 struct ip *ip = mtod(m, struct ip *);
2107 struct sockaddr_in rsvp_src = { sizeof rsvp_src, AF_INET };
2112 printf("rsvp_input: rsvp_on %d\n",rsvp_on);
2114 /* Can still get packets with rsvp_on = 0 if there is a local member
2115 * of the group to which the RSVP packet is addressed. But in this
2116 * case we want to throw the packet away.
2126 printf("rsvp_input: check vifs\n");
2129 if (!(m->m_flags & M_PKTHDR))
2130 panic("rsvp_input no hdr");
2133 ifp = m->m_pkthdr.rcvif;
2134 /* Find which vif the packet arrived on. */
2135 for (vifi = 0; vifi < numvifs; vifi++)
2136 if (viftable[vifi].v_ifp == ifp)
2139 if (vifi == numvifs || viftable[vifi].v_rsvpd == NULL) {
2141 * If the old-style non-vif-associated socket is set,
2142 * then use it. Otherwise, drop packet since there
2143 * is no specific socket for this vif.
2145 if (ip_rsvpd != NULL) {
2147 printf("rsvp_input: Sending packet up old-style socket\n");
2148 rip_input(m, off, proto); /* xxx */
2150 if (rsvpdebug && vifi == numvifs)
2151 printf("rsvp_input: Can't find vif for packet.\n");
2152 else if (rsvpdebug && viftable[vifi].v_rsvpd == NULL)
2153 printf("rsvp_input: No socket defined for vif %d\n",vifi);
2159 rsvp_src.sin_addr = ip->ip_src;
2162 printf("rsvp_input: m->m_len = %d, sbspace() = %ld\n",
2163 m->m_len,sbspace(&(viftable[vifi].v_rsvpd->so_rcv)));
2165 if (socket_send(viftable[vifi].v_rsvpd, m, &rsvp_src) < 0) {
2167 printf("rsvp_input: Failed to append to socket\n");
2170 printf("rsvp_input: send packet up\n");
2177 * Code for bandwidth monitors
2181 * Define common interface for timeval-related methods
2183 #define BW_TIMEVALCMP(tvp, uvp, cmp) timevalcmp((tvp), (uvp), cmp)
2184 #define BW_TIMEVALDECR(vvp, uvp) timevalsub((vvp), (uvp))
2185 #define BW_TIMEVALADD(vvp, uvp) timevaladd((vvp), (uvp))
2188 compute_bw_meter_flags(struct bw_upcall *req)
2192 if (req->bu_flags & BW_UPCALL_UNIT_PACKETS)
2193 flags |= BW_METER_UNIT_PACKETS;
2194 if (req->bu_flags & BW_UPCALL_UNIT_BYTES)
2195 flags |= BW_METER_UNIT_BYTES;
2196 if (req->bu_flags & BW_UPCALL_GEQ)
2197 flags |= BW_METER_GEQ;
2198 if (req->bu_flags & BW_UPCALL_LEQ)
2199 flags |= BW_METER_LEQ;
2205 * Add a bw_meter entry
2208 add_bw_upcall(struct bw_upcall *req)
2211 struct timeval delta = { BW_UPCALL_THRESHOLD_INTERVAL_MIN_SEC,
2212 BW_UPCALL_THRESHOLD_INTERVAL_MIN_USEC };
2218 if (!(mrt_api_config & MRT_MFC_BW_UPCALL))
2221 /* Test if the flags are valid */
2222 if (!(req->bu_flags & (BW_UPCALL_UNIT_PACKETS | BW_UPCALL_UNIT_BYTES)))
2224 if (!(req->bu_flags & (BW_UPCALL_GEQ | BW_UPCALL_LEQ)))
2226 if ((req->bu_flags & (BW_UPCALL_GEQ | BW_UPCALL_LEQ))
2227 == (BW_UPCALL_GEQ | BW_UPCALL_LEQ))
2230 /* Test if the threshold time interval is valid */
2231 if (BW_TIMEVALCMP(&req->bu_threshold.b_time, &delta, <))
2234 flags = compute_bw_meter_flags(req);
2237 * Find if we have already same bw_meter entry
2240 mfc = mfc_find(req->bu_src.s_addr, req->bu_dst.s_addr);
2243 return EADDRNOTAVAIL;
2245 for (x = mfc->mfc_bw_meter; x != NULL; x = x->bm_mfc_next) {
2246 if ((BW_TIMEVALCMP(&x->bm_threshold.b_time,
2247 &req->bu_threshold.b_time, ==)) &&
2248 (x->bm_threshold.b_packets == req->bu_threshold.b_packets) &&
2249 (x->bm_threshold.b_bytes == req->bu_threshold.b_bytes) &&
2250 (x->bm_flags & BW_METER_USER_FLAGS) == flags) {
2252 return 0; /* XXX Already installed */
2257 /* Allocate the new bw_meter entry */
2258 x = (struct bw_meter *)malloc(sizeof(*x), M_BWMETER, M_NOWAIT);
2262 /* Set the new bw_meter entry */
2263 x->bm_threshold.b_time = req->bu_threshold.b_time;
2265 x->bm_start_time = now;
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_measured.b_packets = 0;
2269 x->bm_measured.b_bytes = 0;
2270 x->bm_flags = flags;
2271 x->bm_time_next = NULL;
2272 x->bm_time_hash = BW_METER_BUCKETS;
2274 /* Add the new bw_meter entry to the front of entries for this MFC */
2277 x->bm_mfc_next = mfc->mfc_bw_meter;
2278 mfc->mfc_bw_meter = x;
2279 schedule_bw_meter(x, &now);
2286 free_bw_list(struct bw_meter *list)
2288 while (list != NULL) {
2289 struct bw_meter *x = list;
2291 list = list->bm_mfc_next;
2292 unschedule_bw_meter(x);
2298 * Delete one or multiple bw_meter entries
2301 del_bw_upcall(struct bw_upcall *req)
2307 if (!(mrt_api_config & MRT_MFC_BW_UPCALL))
2311 /* Find the corresponding MFC entry */
2312 mfc = mfc_find(req->bu_src.s_addr, req->bu_dst.s_addr);
2315 return EADDRNOTAVAIL;
2316 } else if (req->bu_flags & BW_UPCALL_DELETE_ALL) {
2318 * Delete all bw_meter entries for this mfc
2320 struct bw_meter *list;
2322 list = mfc->mfc_bw_meter;
2323 mfc->mfc_bw_meter = NULL;
2327 } else { /* Delete a single bw_meter entry */
2328 struct bw_meter *prev;
2331 flags = compute_bw_meter_flags(req);
2333 /* Find the bw_meter entry to delete */
2334 for (prev = NULL, x = mfc->mfc_bw_meter; x != NULL;
2335 x = x->bm_mfc_next) {
2336 if ((BW_TIMEVALCMP(&x->bm_threshold.b_time,
2337 &req->bu_threshold.b_time, ==)) &&
2338 (x->bm_threshold.b_packets == req->bu_threshold.b_packets) &&
2339 (x->bm_threshold.b_bytes == req->bu_threshold.b_bytes) &&
2340 (x->bm_flags & BW_METER_USER_FLAGS) == flags)
2343 if (x != NULL) { /* Delete entry from the list for this MFC */
2345 prev->bm_mfc_next = x->bm_mfc_next; /* remove from middle*/
2347 x->bm_mfc->mfc_bw_meter = x->bm_mfc_next;/* new head of list */
2350 unschedule_bw_meter(x);
2351 /* Free the bw_meter entry */
2363 * Perform bandwidth measurement processing that may result in an upcall
2366 bw_meter_receive_packet(struct bw_meter *x, int plen, struct timeval *nowp)
2368 struct timeval delta;
2373 BW_TIMEVALDECR(&delta, &x->bm_start_time);
2375 if (x->bm_flags & BW_METER_GEQ) {
2377 * Processing for ">=" type of bw_meter entry
2379 if (BW_TIMEVALCMP(&delta, &x->bm_threshold.b_time, >)) {
2380 /* Reset the bw_meter entry */
2381 x->bm_start_time = *nowp;
2382 x->bm_measured.b_packets = 0;
2383 x->bm_measured.b_bytes = 0;
2384 x->bm_flags &= ~BW_METER_UPCALL_DELIVERED;
2387 /* Record that a packet is received */
2388 x->bm_measured.b_packets++;
2389 x->bm_measured.b_bytes += plen;
2392 * Test if we should deliver an upcall
2394 if (!(x->bm_flags & BW_METER_UPCALL_DELIVERED)) {
2395 if (((x->bm_flags & BW_METER_UNIT_PACKETS) &&
2396 (x->bm_measured.b_packets >= x->bm_threshold.b_packets)) ||
2397 ((x->bm_flags & BW_METER_UNIT_BYTES) &&
2398 (x->bm_measured.b_bytes >= x->bm_threshold.b_bytes))) {
2399 /* Prepare an upcall for delivery */
2400 bw_meter_prepare_upcall(x, nowp);
2401 x->bm_flags |= BW_METER_UPCALL_DELIVERED;
2404 } else if (x->bm_flags & BW_METER_LEQ) {
2406 * Processing for "<=" type of bw_meter entry
2408 if (BW_TIMEVALCMP(&delta, &x->bm_threshold.b_time, >)) {
2410 * We are behind time with the multicast forwarding table
2411 * scanning for "<=" type of bw_meter entries, so test now
2412 * if we should deliver an upcall.
2414 if (((x->bm_flags & BW_METER_UNIT_PACKETS) &&
2415 (x->bm_measured.b_packets <= x->bm_threshold.b_packets)) ||
2416 ((x->bm_flags & BW_METER_UNIT_BYTES) &&
2417 (x->bm_measured.b_bytes <= x->bm_threshold.b_bytes))) {
2418 /* Prepare an upcall for delivery */
2419 bw_meter_prepare_upcall(x, nowp);
2421 /* Reschedule the bw_meter entry */
2422 unschedule_bw_meter(x);
2423 schedule_bw_meter(x, nowp);
2426 /* Record that a packet is received */
2427 x->bm_measured.b_packets++;
2428 x->bm_measured.b_bytes += plen;
2431 * Test if we should restart the measuring interval
2433 if ((x->bm_flags & BW_METER_UNIT_PACKETS &&
2434 x->bm_measured.b_packets <= x->bm_threshold.b_packets) ||
2435 (x->bm_flags & BW_METER_UNIT_BYTES &&
2436 x->bm_measured.b_bytes <= x->bm_threshold.b_bytes)) {
2437 /* Don't restart the measuring interval */
2439 /* Do restart the measuring interval */
2441 * XXX: note that we don't unschedule and schedule, because this
2442 * might be too much overhead per packet. Instead, when we process
2443 * all entries for a given timer hash bin, we check whether it is
2444 * really a timeout. If not, we reschedule at that time.
2446 x->bm_start_time = *nowp;
2447 x->bm_measured.b_packets = 0;
2448 x->bm_measured.b_bytes = 0;
2449 x->bm_flags &= ~BW_METER_UPCALL_DELIVERED;
2456 * Prepare a bandwidth-related upcall
2459 bw_meter_prepare_upcall(struct bw_meter *x, struct timeval *nowp)
2461 struct timeval delta;
2462 struct bw_upcall *u;
2468 * Compute the measured time interval
2471 BW_TIMEVALDECR(&delta, &x->bm_start_time);
2474 * If there are too many pending upcalls, deliver them now
2476 if (bw_upcalls_n >= BW_UPCALLS_MAX)
2480 * Set the bw_upcall entry
2482 u = &bw_upcalls[bw_upcalls_n++];
2483 u->bu_src = x->bm_mfc->mfc_origin;
2484 u->bu_dst = x->bm_mfc->mfc_mcastgrp;
2485 u->bu_threshold.b_time = x->bm_threshold.b_time;
2486 u->bu_threshold.b_packets = x->bm_threshold.b_packets;
2487 u->bu_threshold.b_bytes = x->bm_threshold.b_bytes;
2488 u->bu_measured.b_time = delta;
2489 u->bu_measured.b_packets = x->bm_measured.b_packets;
2490 u->bu_measured.b_bytes = x->bm_measured.b_bytes;
2492 if (x->bm_flags & BW_METER_UNIT_PACKETS)
2493 u->bu_flags |= BW_UPCALL_UNIT_PACKETS;
2494 if (x->bm_flags & BW_METER_UNIT_BYTES)
2495 u->bu_flags |= BW_UPCALL_UNIT_BYTES;
2496 if (x->bm_flags & BW_METER_GEQ)
2497 u->bu_flags |= BW_UPCALL_GEQ;
2498 if (x->bm_flags & BW_METER_LEQ)
2499 u->bu_flags |= BW_UPCALL_LEQ;
2505 * Send the pending bandwidth-related upcalls
2508 bw_upcalls_send(void)
2511 int len = bw_upcalls_n * sizeof(bw_upcalls[0]);
2512 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
2513 static struct igmpmsg igmpmsg = { 0, /* unused1 */
2515 IGMPMSG_BW_UPCALL,/* im_msgtype */
2520 { 0 } }; /* im_dst */
2522 if (bw_upcalls_n == 0)
2523 return; /* No pending upcalls */
2528 * Allocate a new mbuf, initialize it with the header and
2529 * the payload for the pending calls.
2531 MGETHDR(m, M_DONTWAIT, MT_HEADER);
2533 log(LOG_WARNING, "bw_upcalls_send: cannot allocate mbuf\n");
2537 m->m_len = m->m_pkthdr.len = 0;
2538 m_copyback(m, 0, sizeof(struct igmpmsg), (caddr_t)&igmpmsg);
2539 m_copyback(m, sizeof(struct igmpmsg), len, (caddr_t)&bw_upcalls[0]);
2543 * XXX do we need to set the address in k_igmpsrc ?
2545 mrtstat.mrts_upcalls++;
2546 if (socket_send(ip_mrouter, m, &k_igmpsrc) < 0) {
2547 log(LOG_WARNING, "bw_upcalls_send: ip_mrouter socket queue full\n");
2548 ++mrtstat.mrts_upq_sockfull;
2553 * Compute the timeout hash value for the bw_meter entries
2555 #define BW_METER_TIMEHASH(bw_meter, hash) \
2557 struct timeval next_timeval = (bw_meter)->bm_start_time; \
2559 BW_TIMEVALADD(&next_timeval, &(bw_meter)->bm_threshold.b_time); \
2560 (hash) = next_timeval.tv_sec; \
2561 if (next_timeval.tv_usec) \
2562 (hash)++; /* XXX: make sure we don't timeout early */ \
2563 (hash) %= BW_METER_BUCKETS; \
2567 * Schedule a timer to process periodically bw_meter entry of type "<="
2568 * by linking the entry in the proper hash bucket.
2571 schedule_bw_meter(struct bw_meter *x, struct timeval *nowp)
2575 if (!(x->bm_flags & BW_METER_LEQ))
2576 return; /* XXX: we schedule timers only for "<=" entries */
2579 * Reset the bw_meter entry
2582 x->bm_start_time = *nowp;
2583 x->bm_measured.b_packets = 0;
2584 x->bm_measured.b_bytes = 0;
2585 x->bm_flags &= ~BW_METER_UPCALL_DELIVERED;
2589 * Compute the timeout hash value and insert the entry
2591 BW_METER_TIMEHASH(x, time_hash);
2592 x->bm_time_next = bw_meter_timers[time_hash];
2593 bw_meter_timers[time_hash] = x;
2594 x->bm_time_hash = time_hash;
2598 * Unschedule the periodic timer that processes bw_meter entry of type "<="
2599 * by removing the entry from the proper hash bucket.
2602 unschedule_bw_meter(struct bw_meter *x)
2605 struct bw_meter *prev, *tmp;
2607 if (!(x->bm_flags & BW_METER_LEQ))
2608 return; /* XXX: we schedule timers only for "<=" entries */
2611 * Compute the timeout hash value and delete the entry
2613 time_hash = x->bm_time_hash;
2614 if (time_hash >= BW_METER_BUCKETS)
2615 return; /* Entry was not scheduled */
2617 for (prev = NULL, tmp = bw_meter_timers[time_hash];
2618 tmp != NULL; prev = tmp, tmp = tmp->bm_time_next)
2623 panic("unschedule_bw_meter: bw_meter entry not found");
2626 prev->bm_time_next = x->bm_time_next;
2628 bw_meter_timers[time_hash] = x->bm_time_next;
2630 x->bm_time_next = NULL;
2631 x->bm_time_hash = BW_METER_BUCKETS;
2636 * Process all "<=" type of bw_meter that should be processed now,
2637 * and for each entry prepare an upcall if necessary. Each processed
2638 * entry is rescheduled again for the (periodic) processing.
2640 * This is run periodically (once per second normally). On each round,
2641 * all the potentially matching entries are in the hash slot that we are
2647 static uint32_t last_tv_sec; /* last time we processed this */
2651 struct timeval now, process_endtime;
2654 if (last_tv_sec == now.tv_sec)
2655 return; /* nothing to do */
2658 loops = now.tv_sec - last_tv_sec;
2659 last_tv_sec = now.tv_sec;
2660 if (loops > BW_METER_BUCKETS)
2661 loops = BW_METER_BUCKETS;
2664 * Process all bins of bw_meter entries from the one after the last
2665 * processed to the current one. On entry, i points to the last bucket
2666 * visited, so we need to increment i at the beginning of the loop.
2668 for (i = (now.tv_sec - loops) % BW_METER_BUCKETS; loops > 0; loops--) {
2669 struct bw_meter *x, *tmp_list;
2671 if (++i >= BW_METER_BUCKETS)
2674 /* Disconnect the list of bw_meter entries from the bin */
2675 tmp_list = bw_meter_timers[i];
2676 bw_meter_timers[i] = NULL;
2678 /* Process the list of bw_meter entries */
2679 while (tmp_list != NULL) {
2681 tmp_list = tmp_list->bm_time_next;
2683 /* Test if the time interval is over */
2684 process_endtime = x->bm_start_time;
2685 BW_TIMEVALADD(&process_endtime, &x->bm_threshold.b_time);
2686 if (BW_TIMEVALCMP(&process_endtime, &now, >)) {
2687 /* Not yet: reschedule, but don't reset */
2690 BW_METER_TIMEHASH(x, time_hash);
2691 if (time_hash == i && process_endtime.tv_sec == now.tv_sec) {
2693 * XXX: somehow the bin processing is a bit ahead of time.
2694 * Put the entry in the next bin.
2696 if (++time_hash >= BW_METER_BUCKETS)
2699 x->bm_time_next = bw_meter_timers[time_hash];
2700 bw_meter_timers[time_hash] = x;
2701 x->bm_time_hash = time_hash;
2707 * Test if we should deliver an upcall
2709 if (((x->bm_flags & BW_METER_UNIT_PACKETS) &&
2710 (x->bm_measured.b_packets <= x->bm_threshold.b_packets)) ||
2711 ((x->bm_flags & BW_METER_UNIT_BYTES) &&
2712 (x->bm_measured.b_bytes <= x->bm_threshold.b_bytes))) {
2713 /* Prepare an upcall for delivery */
2714 bw_meter_prepare_upcall(x, &now);
2718 * Reschedule for next processing
2720 schedule_bw_meter(x, &now);
2725 /* Send all upcalls that are pending delivery */
2730 * A periodic function for sending all upcalls that are pending delivery
2733 expire_bw_upcalls_send(void *unused)
2737 bw_upcalls_ch = timeout(expire_bw_upcalls_send, NULL, BW_UPCALLS_PERIOD);
2741 * A periodic function for periodic scanning of the multicast forwarding
2742 * table for processing all "<=" bw_meter entries.
2745 expire_bw_meter_process(void *unused)
2747 if (mrt_api_config & MRT_MFC_BW_UPCALL)
2750 bw_meter_ch = timeout(expire_bw_meter_process, NULL, BW_METER_PERIOD);
2754 * End of bandwidth monitoring code
2759 * Send the packet up to the user daemon, or eventually do kernel encapsulation
2763 pim_register_send(struct ip *ip, struct vif *vifp,
2764 struct mbuf *m, struct mfc *rt)
2766 struct mbuf *mb_copy, *mm;
2768 if (mrtdebug & DEBUG_PIM)
2769 log(LOG_DEBUG, "pim_register_send: ");
2771 mb_copy = pim_register_prepare(ip, m);
2772 if (mb_copy == NULL)
2776 * Send all the fragments. Note that the mbuf for each fragment
2777 * is freed by the sending machinery.
2779 for (mm = mb_copy; mm; mm = mb_copy) {
2780 mb_copy = mm->m_nextpkt;
2782 mm = m_pullup(mm, sizeof(struct ip));
2784 ip = mtod(mm, struct ip *);
2785 if ((mrt_api_config & MRT_MFC_RP) &&
2786 (rt->mfc_rp.s_addr != INADDR_ANY)) {
2787 pim_register_send_rp(ip, vifp, mm, rt);
2789 pim_register_send_upcall(ip, vifp, mm, rt);
2798 * Return a copy of the data packet that is ready for PIM Register
2800 * XXX: Note that in the returned copy the IP header is a valid one.
2802 static struct mbuf *
2803 pim_register_prepare(struct ip *ip, struct mbuf *m)
2805 struct mbuf *mb_copy = NULL;
2808 /* Take care of delayed checksums */
2809 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
2810 in_delayed_cksum(m);
2811 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
2815 * Copy the old packet & pullup its IP header into the
2816 * new mbuf so we can modify it.
2818 mb_copy = m_copypacket(m, M_DONTWAIT);
2819 if (mb_copy == NULL)
2821 mb_copy = m_pullup(mb_copy, ip->ip_hl << 2);
2822 if (mb_copy == NULL)
2825 /* take care of the TTL */
2826 ip = mtod(mb_copy, struct ip *);
2829 /* Compute the MTU after the PIM Register encapsulation */
2830 mtu = 0xffff - sizeof(pim_encap_iphdr) - sizeof(pim_encap_pimhdr);
2832 if (ip->ip_len <= mtu) {
2833 /* Turn the IP header into a valid one */
2834 ip->ip_len = htons(ip->ip_len);
2835 ip->ip_off = htons(ip->ip_off);
2837 ip->ip_sum = in_cksum(mb_copy, ip->ip_hl << 2);
2839 /* Fragment the packet */
2840 if (ip_fragment(ip, &mb_copy, mtu, 0, CSUM_DELAY_IP) != 0) {
2849 * Send an upcall with the data packet to the user-level process.
2852 pim_register_send_upcall(struct ip *ip, struct vif *vifp,
2853 struct mbuf *mb_copy, struct mfc *rt)
2855 struct mbuf *mb_first;
2856 int len = ntohs(ip->ip_len);
2858 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
2861 * Add a new mbuf with an upcall header
2863 MGETHDR(mb_first, M_DONTWAIT, MT_HEADER);
2864 if (mb_first == NULL) {
2868 mb_first->m_data += max_linkhdr;
2869 mb_first->m_pkthdr.len = len + sizeof(struct igmpmsg);
2870 mb_first->m_len = sizeof(struct igmpmsg);
2871 mb_first->m_next = mb_copy;
2873 /* Send message to routing daemon */
2874 im = mtod(mb_first, struct igmpmsg *);
2875 im->im_msgtype = IGMPMSG_WHOLEPKT;
2877 im->im_vif = vifp - viftable;
2878 im->im_src = ip->ip_src;
2879 im->im_dst = ip->ip_dst;
2881 k_igmpsrc.sin_addr = ip->ip_src;
2883 mrtstat.mrts_upcalls++;
2885 if (socket_send(ip_mrouter, mb_first, &k_igmpsrc) < 0) {
2886 if (mrtdebug & DEBUG_PIM)
2888 "mcast: pim_register_send_upcall: ip_mrouter socket queue full");
2889 ++mrtstat.mrts_upq_sockfull;
2893 /* Keep statistics */
2894 pimstat.pims_snd_registers_msgs++;
2895 pimstat.pims_snd_registers_bytes += len;
2901 * Encapsulate the data packet in PIM Register message and send it to the RP.
2904 pim_register_send_rp(struct ip *ip, struct vif *vifp,
2905 struct mbuf *mb_copy, struct mfc *rt)
2907 struct mbuf *mb_first;
2908 struct ip *ip_outer;
2909 struct pim_encap_pimhdr *pimhdr;
2910 int len = ntohs(ip->ip_len);
2911 vifi_t vifi = rt->mfc_parent;
2913 if ((vifi >= numvifs) || (viftable[vifi].v_lcl_addr.s_addr == 0)) {
2915 return EADDRNOTAVAIL; /* The iif vif is invalid */
2919 * Add a new mbuf with the encapsulating header
2921 MGETHDR(mb_first, M_DONTWAIT, MT_HEADER);
2922 if (mb_first == NULL) {
2926 mb_first->m_data += max_linkhdr;
2927 mb_first->m_len = sizeof(pim_encap_iphdr) + sizeof(pim_encap_pimhdr);
2928 mb_first->m_next = mb_copy;
2930 mb_first->m_pkthdr.len = len + mb_first->m_len;
2933 * Fill in the encapsulating IP and PIM header
2935 ip_outer = mtod(mb_first, struct ip *);
2936 *ip_outer = pim_encap_iphdr;
2938 ip_outer->ip_id = ip_randomid();
2940 ip_outer->ip_id = htons(ip_id++);
2942 ip_outer->ip_len = len + sizeof(pim_encap_iphdr) + sizeof(pim_encap_pimhdr);
2943 ip_outer->ip_src = viftable[vifi].v_lcl_addr;
2944 ip_outer->ip_dst = rt->mfc_rp;
2946 * Copy the inner header TOS to the outer header, and take care of the
2949 ip_outer->ip_tos = ip->ip_tos;
2950 if (ntohs(ip->ip_off) & IP_DF)
2951 ip_outer->ip_off |= IP_DF;
2952 pimhdr = (struct pim_encap_pimhdr *)((caddr_t)ip_outer
2953 + sizeof(pim_encap_iphdr));
2954 *pimhdr = pim_encap_pimhdr;
2955 /* If the iif crosses a border, set the Border-bit */
2956 if (rt->mfc_flags[vifi] & MRT_MFC_FLAGS_BORDER_VIF & mrt_api_config)
2957 pimhdr->flags |= htonl(PIM_BORDER_REGISTER);
2959 mb_first->m_data += sizeof(pim_encap_iphdr);
2960 pimhdr->pim.pim_cksum = in_cksum(mb_first, sizeof(pim_encap_pimhdr));
2961 mb_first->m_data -= sizeof(pim_encap_iphdr);
2963 if (vifp->v_rate_limit == 0)
2964 tbf_send_packet(vifp, mb_first);
2966 tbf_control(vifp, mb_first, ip, ip_outer->ip_len);
2968 /* Keep statistics */
2969 pimstat.pims_snd_registers_msgs++;
2970 pimstat.pims_snd_registers_bytes += len;
2976 * PIM-SMv2 and PIM-DM messages processing.
2977 * Receives and verifies the PIM control messages, and passes them
2978 * up to the listening socket, using rip_input().
2979 * The only message with special processing is the PIM_REGISTER message
2980 * (used by PIM-SM): the PIM header is stripped off, and the inner packet
2981 * is passed to if_simloop().
2984 pim_input(struct mbuf *m, int off, int proto)
2986 struct ip *ip = mtod(m, struct ip *);
2989 int datalen = ip->ip_len;
2993 /* Keep statistics */
2994 pimstat.pims_rcv_total_msgs++;
2995 pimstat.pims_rcv_total_bytes += datalen;
3000 if (datalen < PIM_MINLEN) {
3001 pimstat.pims_rcv_tooshort++;
3002 log(LOG_ERR, "pim_input: packet size too small %d from %lx\n",
3003 datalen, (u_long)ip->ip_src.s_addr);
3009 * If the packet is at least as big as a REGISTER, go agead
3010 * and grab the PIM REGISTER header size, to avoid another
3011 * possible m_pullup() later.
3013 * PIM_MINLEN == pimhdr + u_int32_t == 4 + 4 = 8
3014 * PIM_REG_MINLEN == pimhdr + reghdr + encap_iphdr == 4 + 4 + 20 = 28
3016 minlen = iphlen + (datalen >= PIM_REG_MINLEN ? PIM_REG_MINLEN : PIM_MINLEN);
3018 * Get the IP and PIM headers in contiguous memory, and
3019 * possibly the PIM REGISTER header.
3021 if ((m->m_flags & M_EXT || m->m_len < minlen) &&
3022 (m = m_pullup(m, minlen)) == 0) {
3023 log(LOG_ERR, "pim_input: m_pullup failure\n");
3026 /* m_pullup() may have given us a new mbuf so reset ip. */
3027 ip = mtod(m, struct ip *);
3028 ip_tos = ip->ip_tos;
3030 /* adjust mbuf to point to the PIM header */
3031 m->m_data += iphlen;
3033 pim = mtod(m, struct pim *);
3036 * Validate checksum. If PIM REGISTER, exclude the data packet.
3038 * XXX: some older PIMv2 implementations don't make this distinction,
3039 * so for compatibility reason perform the checksum over part of the
3040 * message, and if error, then over the whole message.
3042 if (PIM_VT_T(pim->pim_vt) == PIM_REGISTER && in_cksum(m, PIM_MINLEN) == 0) {
3043 /* do nothing, checksum okay */
3044 } else if (in_cksum(m, datalen)) {
3045 pimstat.pims_rcv_badsum++;
3046 if (mrtdebug & DEBUG_PIM)
3047 log(LOG_DEBUG, "pim_input: invalid checksum");
3052 /* PIM version check */
3053 if (PIM_VT_V(pim->pim_vt) < PIM_VERSION) {
3054 pimstat.pims_rcv_badversion++;
3055 log(LOG_ERR, "pim_input: incorrect version %d, expecting %d\n",
3056 PIM_VT_V(pim->pim_vt), PIM_VERSION);
3061 /* restore mbuf back to the outer IP */
3062 m->m_data -= iphlen;
3065 if (PIM_VT_T(pim->pim_vt) == PIM_REGISTER) {
3067 * Since this is a REGISTER, we'll make a copy of the register
3068 * headers ip + pim + u_int32 + encap_ip, to be passed up to the
3071 struct sockaddr_in dst = { sizeof(dst), AF_INET };
3073 struct ip *encap_ip;
3076 if ((reg_vif_num >= numvifs) || (reg_vif_num == VIFI_INVALID)) {
3077 if (mrtdebug & DEBUG_PIM)
3079 "pim_input: register vif not set: %d\n", reg_vif_num);
3087 if (datalen < PIM_REG_MINLEN) {
3088 pimstat.pims_rcv_tooshort++;
3089 pimstat.pims_rcv_badregisters++;
3091 "pim_input: register packet size too small %d from %lx\n",
3092 datalen, (u_long)ip->ip_src.s_addr);
3097 reghdr = (u_int32_t *)(pim + 1);
3098 encap_ip = (struct ip *)(reghdr + 1);
3100 if (mrtdebug & DEBUG_PIM) {
3102 "pim_input[register], encap_ip: %lx -> %lx, encap_ip len %d\n",
3103 (u_long)ntohl(encap_ip->ip_src.s_addr),
3104 (u_long)ntohl(encap_ip->ip_dst.s_addr),
3105 ntohs(encap_ip->ip_len));
3108 /* verify the version number of the inner packet */
3109 if (encap_ip->ip_v != IPVERSION) {
3110 pimstat.pims_rcv_badregisters++;
3111 if (mrtdebug & DEBUG_PIM) {
3112 log(LOG_DEBUG, "pim_input: invalid IP version (%d) "
3113 "of the inner packet\n", encap_ip->ip_v);
3119 /* verify the inner packet is destined to a mcast group */
3120 if (!IN_MULTICAST(ntohl(encap_ip->ip_dst.s_addr))) {
3121 pimstat.pims_rcv_badregisters++;
3122 if (mrtdebug & DEBUG_PIM)
3124 "pim_input: inner packet of register is not "
3126 (u_long)ntohl(encap_ip->ip_dst.s_addr));
3132 * Copy the TOS from the outer IP header to the inner IP header.
3134 if (encap_ip->ip_tos != ip_tos) {
3135 /* Outer TOS -> inner TOS */
3136 encap_ip->ip_tos = ip_tos;
3137 /* Recompute the inner header checksum. Sigh... */
3139 /* adjust mbuf to point to the inner IP header */
3140 m->m_data += (iphlen + PIM_MINLEN);
3141 m->m_len -= (iphlen + PIM_MINLEN);
3143 encap_ip->ip_sum = 0;
3144 encap_ip->ip_sum = in_cksum(m, encap_ip->ip_hl << 2);
3146 /* restore mbuf to point back to the outer IP header */
3147 m->m_data -= (iphlen + PIM_MINLEN);
3148 m->m_len += (iphlen + PIM_MINLEN);
3151 /* If a NULL_REGISTER, pass it to the daemon */
3152 if ((ntohl(*reghdr) & PIM_NULL_REGISTER))
3153 goto pim_input_to_daemon;
3156 * Decapsulate the inner IP packet and loopback to forward it
3157 * as a normal multicast packet. Also, make a copy of the
3158 * outer_iphdr + pimhdr + reghdr + encap_iphdr
3159 * to pass to the daemon later, so it can take the appropriate
3160 * actions (e.g., send back PIM_REGISTER_STOP).
3161 * XXX: here m->m_data points to the outer IP header.
3163 mcp = m_copy(m, 0, iphlen + PIM_REG_MINLEN);
3166 "pim_input: pim register: could not copy register head\n");
3171 /* Keep statistics */
3172 /* XXX: registers_bytes include only the encap. mcast pkt */
3173 pimstat.pims_rcv_registers_msgs++;
3174 pimstat.pims_rcv_registers_bytes += ntohs(encap_ip->ip_len);
3177 * forward the inner ip packet; point m_data at the inner ip.
3179 m_adj(m, iphlen + PIM_MINLEN);
3181 if (mrtdebug & DEBUG_PIM) {
3183 "pim_input: forwarding decapsulated register: "
3184 "src %lx, dst %lx, vif %d\n",
3185 (u_long)ntohl(encap_ip->ip_src.s_addr),
3186 (u_long)ntohl(encap_ip->ip_dst.s_addr),
3189 if_simloop(viftable[reg_vif_num].v_ifp, m, dst.sin_family, 0);
3191 /* prepare the register head to send to the mrouting daemon */
3195 pim_input_to_daemon:
3197 * Pass the PIM message up to the daemon; if it is a Register message,
3198 * pass the 'head' only up to the daemon. This includes the
3199 * outer IP header, PIM header, PIM-Register header and the
3201 * XXX: the outer IP header pkt size of a Register is not adjust to
3202 * reflect the fact that the inner multicast data is truncated.
3204 rip_input(m, iphlen, proto);
3211 ip_mroute_modevent(module_t mod, int type, void *unused)
3218 /* XXX Protect against multiple loading */
3219 ip_mcast_src = X_ip_mcast_src;
3220 ip_mforward = X_ip_mforward;
3221 ip_mrouter_done = X_ip_mrouter_done;
3222 ip_mrouter_get = X_ip_mrouter_get;
3223 ip_mrouter_set = X_ip_mrouter_set;
3224 ip_rsvp_force_done = X_ip_rsvp_force_done;
3225 ip_rsvp_vif = X_ip_rsvp_vif;
3226 ipip_input = X_ipip_input;
3227 legal_vif_num = X_legal_vif_num;
3228 mrt_ioctl = X_mrt_ioctl;
3229 rsvp_input_p = X_rsvp_input;
3238 ip_mcast_src = NULL;
3240 ip_mrouter_done = NULL;
3241 ip_mrouter_get = NULL;
3242 ip_mrouter_set = NULL;
3243 ip_rsvp_force_done = NULL;
3246 legal_vif_num = NULL;
3248 rsvp_input_p = NULL;
3255 static moduledata_t ip_mroutemod = {
3260 DECLARE_MODULE(ip_mroute, ip_mroutemod, SI_SUB_PSEUDO, SI_ORDER_ANY);