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.17 2005/06/15 18:29:30 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>
42 #include <sys/thread2.h>
44 #include <sys/in_cksum.h>
46 #include <machine/stdarg.h>
49 #include <net/netisr.h>
50 #include <net/route.h>
51 #include <netinet/in.h>
52 #include <netinet/igmp.h>
53 #include <netinet/in_systm.h>
54 #include <netinet/in_var.h>
55 #include <netinet/ip.h>
56 #include "ip_mroute.h"
57 #include <netinet/ip_var.h>
59 #include <netinet/pim.h>
60 #include <netinet/pim_var.h>
63 #include <netinet/in_pcb.h>
65 #include <netinet/udp.h>
68 * Control debugging code for rsvp and multicast routing code.
69 * Can only set them with the debugger.
71 static u_int rsvpdebug; /* non-zero enables debugging */
73 static u_int mrtdebug; /* any set of the flags below */
75 #define DEBUG_MFC 0x02
76 #define DEBUG_FORWARD 0x04
77 #define DEBUG_EXPIRE 0x08
78 #define DEBUG_XMIT 0x10
79 #define DEBUG_PIM 0x20
81 #define VIFI_INVALID ((vifi_t) -1)
83 #define M_HASCL(m) ((m)->m_flags & M_EXT)
85 static MALLOC_DEFINE(M_MRTABLE, "mroutetbl", "multicast routing tables");
87 static struct mrtstat mrtstat;
88 SYSCTL_STRUCT(_net_inet_ip, OID_AUTO, mrtstat, CTLFLAG_RW,
90 "Multicast Routing Statistics (struct mrtstat, netinet/ip_mroute.h)");
92 static struct mfc *mfctable[MFCTBLSIZ];
93 SYSCTL_OPAQUE(_net_inet_ip, OID_AUTO, mfctable, CTLFLAG_RD,
94 &mfctable, sizeof(mfctable), "S,*mfc[MFCTBLSIZ]",
95 "Multicast Forwarding Table (struct *mfc[MFCTBLSIZ], netinet/ip_mroute.h)");
97 static struct vif viftable[MAXVIFS];
98 SYSCTL_OPAQUE(_net_inet_ip, OID_AUTO, viftable, CTLFLAG_RD,
99 &viftable, sizeof(viftable), "S,vif[MAXVIFS]",
100 "Multicast Virtual Interfaces (struct vif[MAXVIFS], netinet/ip_mroute.h)");
102 static u_char nexpire[MFCTBLSIZ];
104 static struct callout expire_upcalls_ch;
105 static struct callout tbf_reprocess_q_ch;
106 #define EXPIRE_TIMEOUT (hz / 4) /* 4x / second */
107 #define UPCALL_EXPIRE 6 /* number of timeouts */
110 * Define the token bucket filter structures
111 * tbftable -> each vif has one of these for storing info
114 static struct tbf tbftable[MAXVIFS];
115 #define TBF_REPROCESS (hz / 100) /* 100x / second */
118 * 'Interfaces' associated with decapsulator (so we can tell
119 * packets that went through it from ones that get reflected
120 * by a broken gateway). These interfaces are never linked into
121 * the system ifnet list & no routes point to them. I.e., packets
122 * can't be sent this way. They only exist as a placeholder for
123 * multicast source verification.
125 static struct ifnet multicast_decap_if[MAXVIFS];
128 #define ENCAP_PROTO IPPROTO_IPIP /* 4 */
130 /* prototype IP hdr for encapsulated packets */
131 static struct ip multicast_encap_iphdr = {
132 #if BYTE_ORDER == LITTLE_ENDIAN
133 sizeof(struct ip) >> 2, IPVERSION,
135 IPVERSION, sizeof(struct ip) >> 2,
138 sizeof(struct ip), /* total length */
141 ENCAP_TTL, ENCAP_PROTO,
146 * Bandwidth meter variables and constants
148 static MALLOC_DEFINE(M_BWMETER, "bwmeter", "multicast upcall bw meters");
150 * Pending timeouts are stored in a hash table, the key being the
151 * expiration time. Periodically, the entries are analysed and processed.
153 #define BW_METER_BUCKETS 1024
154 static struct bw_meter *bw_meter_timers[BW_METER_BUCKETS];
155 static struct callout bw_meter_ch;
156 #define BW_METER_PERIOD (hz) /* periodical handling of bw meters */
159 * Pending upcalls are stored in a vector which is flushed when
160 * full, or periodically
162 static struct bw_upcall bw_upcalls[BW_UPCALLS_MAX];
163 static u_int bw_upcalls_n; /* # of pending upcalls */
164 static struct callout bw_upcalls_ch;
165 #define BW_UPCALLS_PERIOD (hz) /* periodical flush of bw upcalls */
168 static struct pimstat pimstat;
169 SYSCTL_STRUCT(_net_inet_pim, PIMCTL_STATS, stats, CTLFLAG_RD,
171 "PIM Statistics (struct pimstat, netinet/pim_var.h)");
174 * Note: the PIM Register encapsulation adds the following in front of a
177 * struct pim_encap_hdr {
179 * struct pim_encap_pimhdr pim;
184 struct pim_encap_pimhdr {
189 static struct ip pim_encap_iphdr = {
190 #if BYTE_ORDER == LITTLE_ENDIAN
191 sizeof(struct ip) >> 2,
195 sizeof(struct ip) >> 2,
198 sizeof(struct ip), /* total length */
206 static struct pim_encap_pimhdr pim_encap_pimhdr = {
208 PIM_MAKE_VT(PIM_VERSION, PIM_REGISTER), /* PIM vers and message type */
215 static struct ifnet multicast_register_if;
216 static vifi_t reg_vif_num = VIFI_INVALID;
222 static vifi_t numvifs;
223 static int have_encap_tunnel;
226 * one-back cache used by ipip_input to locate a tunnel's vif
227 * given a datagram's src ip address.
229 static u_long last_encap_src;
230 static struct vif *last_encap_vif;
232 static u_long X_ip_mcast_src(int vifi);
233 static int X_ip_mforward(struct ip *ip, struct ifnet *ifp,
234 struct mbuf *m, struct ip_moptions *imo);
235 static int X_ip_mrouter_done(void);
236 static int X_ip_mrouter_get(struct socket *so, struct sockopt *m);
237 static int X_ip_mrouter_set(struct socket *so, struct sockopt *m);
238 static int X_legal_vif_num(int vif);
239 static int X_mrt_ioctl(int cmd, caddr_t data);
241 static int get_sg_cnt(struct sioc_sg_req *);
242 static int get_vif_cnt(struct sioc_vif_req *);
243 static int ip_mrouter_init(struct socket *, int);
244 static int add_vif(struct vifctl *);
245 static int del_vif(vifi_t);
246 static int add_mfc(struct mfcctl2 *);
247 static int del_mfc(struct mfcctl2 *);
248 static int set_api_config(uint32_t *); /* chose API capabilities */
249 static int socket_send(struct socket *, struct mbuf *, struct sockaddr_in *);
250 static int set_assert(int);
251 static void expire_upcalls(void *);
252 static int ip_mdq(struct mbuf *, struct ifnet *, struct mfc *, vifi_t);
253 static void phyint_send(struct ip *, struct vif *, struct mbuf *);
254 static void encap_send(struct ip *, struct vif *, struct mbuf *);
255 static void tbf_control(struct vif *, struct mbuf *, struct ip *, u_long);
256 static void tbf_queue(struct vif *, struct mbuf *);
257 static void tbf_process_q(struct vif *);
258 static void tbf_reprocess_q(void *);
259 static int tbf_dq_sel(struct vif *, struct ip *);
260 static void tbf_send_packet(struct vif *, struct mbuf *);
261 static void tbf_update_tokens(struct vif *);
262 static int priority(struct vif *, struct ip *);
265 * Bandwidth monitoring
267 static void free_bw_list(struct bw_meter *list);
268 static int add_bw_upcall(struct bw_upcall *);
269 static int del_bw_upcall(struct bw_upcall *);
270 static void bw_meter_receive_packet(struct bw_meter *x, int plen,
271 struct timeval *nowp);
272 static void bw_meter_prepare_upcall(struct bw_meter *x, struct timeval *nowp);
273 static void bw_upcalls_send(void);
274 static void schedule_bw_meter(struct bw_meter *x, struct timeval *nowp);
275 static void unschedule_bw_meter(struct bw_meter *x);
276 static void bw_meter_process(void);
277 static void expire_bw_upcalls_send(void *);
278 static void expire_bw_meter_process(void *);
281 static int pim_register_send(struct ip *, struct vif *,
282 struct mbuf *, struct mfc *);
283 static int pim_register_send_rp(struct ip *, struct vif *,
284 struct mbuf *, struct mfc *);
285 static int pim_register_send_upcall(struct ip *, struct vif *,
286 struct mbuf *, struct mfc *);
287 static struct mbuf *pim_register_prepare(struct ip *, struct mbuf *);
291 * whether or not special PIM assert processing is enabled.
293 static int pim_assert;
295 * Rate limit for assert notification messages, in usec
297 #define ASSERT_MSG_TIME 3000000
300 * Kernel multicast routing API capabilities and setup.
301 * If more API capabilities are added to the kernel, they should be
302 * recorded in `mrt_api_support'.
304 static const uint32_t mrt_api_support = (MRT_MFC_FLAGS_DISABLE_WRONGVIF |
305 MRT_MFC_FLAGS_BORDER_VIF |
308 static uint32_t mrt_api_config = 0;
311 * Hash function for a source, group entry
313 #define MFCHASH(a, g) MFCHASHMOD(((a) >> 20) ^ ((a) >> 10) ^ (a) ^ \
314 ((g) >> 20) ^ ((g) >> 10) ^ (g))
317 * Find a route for a given origin IP address and Multicast group address
318 * Type of service parameter to be added in the future!!!
319 * Statistics are updated by the caller if needed
320 * (mrtstat.mrts_mfc_lookups and mrtstat.mrts_mfc_misses)
323 mfc_find(in_addr_t o, in_addr_t g)
327 for (rt = mfctable[MFCHASH(o,g)]; rt; rt = rt->mfc_next)
328 if ((rt->mfc_origin.s_addr == o) &&
329 (rt->mfc_mcastgrp.s_addr == g) && (rt->mfc_stall == NULL))
335 * Macros to compute elapsed time efficiently
336 * Borrowed from Van Jacobson's scheduling code
338 #define TV_DELTA(a, b, delta) { \
340 delta = (a).tv_usec - (b).tv_usec; \
341 if ((xxs = (a).tv_sec - (b).tv_sec)) { \
350 delta += (1000000 * xxs); \
355 #define TV_LT(a, b) (((a).tv_usec < (b).tv_usec && \
356 (a).tv_sec <= (b).tv_sec) || (a).tv_sec < (b).tv_sec)
359 * Handle MRT setsockopt commands to modify the multicast routing tables.
362 X_ip_mrouter_set(struct socket *so, struct sockopt *sopt)
368 struct bw_upcall bw_upcall;
371 if (so != ip_mrouter && sopt->sopt_name != MRT_INIT)
375 switch (sopt->sopt_name) {
377 error = sooptcopyin(sopt, &optval, sizeof optval, sizeof optval);
380 error = ip_mrouter_init(so, optval);
384 error = ip_mrouter_done();
388 error = sooptcopyin(sopt, &vifc, sizeof vifc, sizeof vifc);
391 error = add_vif(&vifc);
395 error = sooptcopyin(sopt, &vifi, sizeof vifi, sizeof vifi);
398 error = del_vif(vifi);
404 * select data size depending on API version.
406 if (sopt->sopt_name == MRT_ADD_MFC &&
407 mrt_api_config & MRT_API_FLAGS_ALL) {
408 error = sooptcopyin(sopt, &mfc, sizeof(struct mfcctl2),
409 sizeof(struct mfcctl2));
411 error = sooptcopyin(sopt, &mfc, sizeof(struct mfcctl),
412 sizeof(struct mfcctl));
413 bzero((caddr_t)&mfc + sizeof(struct mfcctl),
414 sizeof(mfc) - sizeof(struct mfcctl));
418 if (sopt->sopt_name == MRT_ADD_MFC)
419 error = add_mfc(&mfc);
421 error = del_mfc(&mfc);
425 error = sooptcopyin(sopt, &optval, sizeof optval, sizeof optval);
432 error = sooptcopyin(sopt, &i, sizeof i, sizeof i);
434 error = set_api_config(&i);
436 error = sooptcopyout(sopt, &i, sizeof i);
439 case MRT_ADD_BW_UPCALL:
440 case MRT_DEL_BW_UPCALL:
441 error = sooptcopyin(sopt, &bw_upcall, sizeof bw_upcall,
445 if (sopt->sopt_name == MRT_ADD_BW_UPCALL)
446 error = add_bw_upcall(&bw_upcall);
448 error = del_bw_upcall(&bw_upcall);
459 * Handle MRT getsockopt commands
462 X_ip_mrouter_get(struct socket *so, struct sockopt *sopt)
465 static int version = 0x0305; /* !!! why is this here? XXX */
467 switch (sopt->sopt_name) {
469 error = sooptcopyout(sopt, &version, sizeof version);
473 error = sooptcopyout(sopt, &pim_assert, sizeof pim_assert);
476 case MRT_API_SUPPORT:
477 error = sooptcopyout(sopt, &mrt_api_support, sizeof mrt_api_support);
481 error = sooptcopyout(sopt, &mrt_api_config, sizeof mrt_api_config);
492 * Handle ioctl commands to obtain information from the cache
495 X_mrt_ioctl(int cmd, caddr_t data)
501 error = get_vif_cnt((struct sioc_vif_req *)data);
505 error = get_sg_cnt((struct sioc_sg_req *)data);
516 * returns the packet, byte, rpf-failure count for the source group provided
519 get_sg_cnt(struct sioc_sg_req *req)
524 rt = mfc_find(req->src.s_addr, req->grp.s_addr);
527 req->pktcnt = req->bytecnt = req->wrong_if = 0xffffffff;
528 return EADDRNOTAVAIL;
530 req->pktcnt = rt->mfc_pkt_cnt;
531 req->bytecnt = rt->mfc_byte_cnt;
532 req->wrong_if = rt->mfc_wrong_if;
537 * returns the input and output packet and byte counts on the vif provided
540 get_vif_cnt(struct sioc_vif_req *req)
542 vifi_t vifi = req->vifi;
547 req->icount = viftable[vifi].v_pkt_in;
548 req->ocount = viftable[vifi].v_pkt_out;
549 req->ibytes = viftable[vifi].v_bytes_in;
550 req->obytes = viftable[vifi].v_bytes_out;
556 * Enable multicast routing
559 ip_mrouter_init(struct socket *so, int version)
562 log(LOG_DEBUG, "ip_mrouter_init: so_type = %d, pr_protocol = %d\n",
563 so->so_type, so->so_proto->pr_protocol);
565 if (so->so_type != SOCK_RAW || so->so_proto->pr_protocol != IPPROTO_IGMP)
571 if (ip_mrouter != NULL)
576 bzero((caddr_t)mfctable, sizeof(mfctable));
577 bzero((caddr_t)nexpire, sizeof(nexpire));
581 bzero((caddr_t)bw_meter_timers, sizeof(bw_meter_timers));
583 callout_init(&expire_upcalls_ch);
584 callout_init(&bw_upcalls_ch);
585 callout_init(&bw_meter_ch);
586 callout_init(&tbf_reprocess_q_ch);
588 callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT, expire_upcalls, NULL);
589 callout_reset(&bw_upcalls_ch, BW_UPCALLS_PERIOD,
590 expire_bw_upcalls_send, NULL);
591 callout_reset(&bw_meter_ch, BW_METER_PERIOD, expire_bw_meter_process, NULL);
596 log(LOG_DEBUG, "ip_mrouter_init\n");
602 * Disable multicast routing
605 X_ip_mrouter_done(void)
617 * For each phyint in use, disable promiscuous reception of all IP
620 for (vifi = 0; vifi < numvifs; vifi++) {
621 if (viftable[vifi].v_lcl_addr.s_addr != 0 &&
622 !(viftable[vifi].v_flags & (VIFF_TUNNEL | VIFF_REGISTER))) {
623 struct sockaddr_in *so = (struct sockaddr_in *)&(ifr.ifr_addr);
625 so->sin_len = sizeof(struct sockaddr_in);
626 so->sin_family = AF_INET;
627 so->sin_addr.s_addr = INADDR_ANY;
628 ifp = viftable[vifi].v_ifp;
632 bzero((caddr_t)tbftable, sizeof(tbftable));
633 bzero((caddr_t)viftable, sizeof(viftable));
637 callout_stop(&expire_upcalls_ch);
641 callout_stop(&bw_upcalls_ch);
642 callout_stop(&bw_meter_ch);
643 callout_stop(&tbf_reprocess_q_ch);
646 * Free all multicast forwarding cache entries.
648 for (i = 0; i < MFCTBLSIZ; i++) {
649 for (rt = mfctable[i]; rt != NULL; ) {
650 struct mfc *nr = rt->mfc_next;
652 for (rte = rt->mfc_stall; rte != NULL; ) {
653 struct rtdetq *n = rte->next;
656 free(rte, M_MRTABLE);
659 free_bw_list(rt->mfc_bw_meter);
665 bzero((caddr_t)mfctable, sizeof(mfctable));
667 bzero(bw_meter_timers, sizeof(bw_meter_timers));
670 * Reset de-encapsulation cache
672 last_encap_src = INADDR_ANY;
673 last_encap_vif = NULL;
675 reg_vif_num = VIFI_INVALID;
677 have_encap_tunnel = 0;
684 log(LOG_DEBUG, "ip_mrouter_done\n");
690 * Set PIM assert processing global
695 if ((i != 1) && (i != 0))
704 * Configure API capabilities
707 set_api_config(uint32_t *apival)
712 * We can set the API capabilities only if it is the first operation
713 * after MRT_INIT. I.e.:
714 * - there are no vifs installed
715 * - pim_assert is not enabled
716 * - the MFC table is empty
726 for (i = 0; i < MFCTBLSIZ; i++) {
727 if (mfctable[i] != NULL) {
733 mrt_api_config = *apival & mrt_api_support;
734 *apival = mrt_api_config;
740 * Add a vif to the vif table
743 add_vif(struct vifctl *vifcp)
745 struct vif *vifp = viftable + vifcp->vifc_vifi;
746 struct sockaddr_in sin = {sizeof sin, AF_INET};
750 struct tbf *v_tbf = tbftable + vifcp->vifc_vifi;
752 if (vifcp->vifc_vifi >= MAXVIFS)
754 if (vifp->v_lcl_addr.s_addr != INADDR_ANY)
756 if (vifcp->vifc_lcl_addr.s_addr == INADDR_ANY)
757 return EADDRNOTAVAIL;
759 /* Find the interface with an address in AF_INET family */
761 if (vifcp->vifc_flags & VIFF_REGISTER) {
763 * XXX: Because VIFF_REGISTER does not really need a valid
764 * local interface (e.g. it could be 127.0.0.2), we don't
771 sin.sin_addr = vifcp->vifc_lcl_addr;
772 ifa = ifa_ifwithaddr((struct sockaddr *)&sin);
774 return EADDRNOTAVAIL;
778 if (vifcp->vifc_flags & VIFF_TUNNEL) {
779 if ((vifcp->vifc_flags & VIFF_SRCRT) == 0) {
781 * An encapsulating tunnel is wanted. Tell ipip_input() to
782 * start paying attention to encapsulated packets.
784 if (have_encap_tunnel == 0) {
785 have_encap_tunnel = 1;
786 for (i = 0; i < MAXVIFS; i++) {
787 if_initname(&multicast_decap_if[i], "mdecap", i);
791 * Set interface to fake encapsulator interface
793 ifp = &multicast_decap_if[vifcp->vifc_vifi];
795 * Prepare cached route entry
797 bzero(&vifp->v_route, sizeof(vifp->v_route));
799 log(LOG_ERR, "source routed tunnels not supported\n");
803 } else if (vifcp->vifc_flags & VIFF_REGISTER) {
804 ifp = &multicast_register_if;
806 log(LOG_DEBUG, "Adding a register vif, ifp: %p\n",
807 (void *)&multicast_register_if);
808 if (reg_vif_num == VIFI_INVALID) {
809 if_initname(&multicast_register_if, "register_vif", 0);
810 multicast_register_if.if_flags = IFF_LOOPBACK;
811 bzero(&vifp->v_route, sizeof(vifp->v_route));
812 reg_vif_num = vifcp->vifc_vifi;
815 } else { /* Make sure the interface supports multicast */
816 if ((ifp->if_flags & IFF_MULTICAST) == 0)
819 /* Enable promiscuous reception of all IP multicasts from the if */
821 error = if_allmulti(ifp, 1);
828 /* define parameters for the tbf structure */
830 GET_TIME(vifp->v_tbf->tbf_last_pkt_t);
831 vifp->v_tbf->tbf_n_tok = 0;
832 vifp->v_tbf->tbf_q_len = 0;
833 vifp->v_tbf->tbf_max_q_len = MAXQSIZE;
834 vifp->v_tbf->tbf_q = vifp->v_tbf->tbf_t = NULL;
836 vifp->v_flags = vifcp->vifc_flags;
837 vifp->v_threshold = vifcp->vifc_threshold;
838 vifp->v_lcl_addr = vifcp->vifc_lcl_addr;
839 vifp->v_rmt_addr = vifcp->vifc_rmt_addr;
841 /* scaling up here allows division by 1024 in critical code */
842 vifp->v_rate_limit= vifcp->vifc_rate_limit * 1024 / 1000;
844 vifp->v_rsvpd = NULL;
845 /* initialize per vif pkt counters */
848 vifp->v_bytes_in = 0;
849 vifp->v_bytes_out = 0;
852 /* Adjust numvifs up if the vifi is higher than numvifs */
853 if (numvifs <= vifcp->vifc_vifi) numvifs = vifcp->vifc_vifi + 1;
856 log(LOG_DEBUG, "add_vif #%d, lcladdr %lx, %s %lx, thresh %x, rate %d\n",
858 (u_long)ntohl(vifcp->vifc_lcl_addr.s_addr),
859 (vifcp->vifc_flags & VIFF_TUNNEL) ? "rmtaddr" : "mask",
860 (u_long)ntohl(vifcp->vifc_rmt_addr.s_addr),
861 vifcp->vifc_threshold,
862 vifcp->vifc_rate_limit);
868 * Delete a vif from the vif table
877 vifp = &viftable[vifi];
878 if (vifp->v_lcl_addr.s_addr == INADDR_ANY)
879 return EADDRNOTAVAIL;
883 if (!(vifp->v_flags & (VIFF_TUNNEL | VIFF_REGISTER)))
884 if_allmulti(vifp->v_ifp, 0);
886 if (vifp == last_encap_vif) {
887 last_encap_vif = NULL;
888 last_encap_src = INADDR_ANY;
892 * Free packets queued at the interface
894 while (vifp->v_tbf->tbf_q) {
895 struct mbuf *m = vifp->v_tbf->tbf_q;
897 vifp->v_tbf->tbf_q = m->m_nextpkt;
902 if (vifp->v_flags & VIFF_REGISTER)
903 reg_vif_num = VIFI_INVALID;
906 bzero((caddr_t)vifp->v_tbf, sizeof(*(vifp->v_tbf)));
907 bzero((caddr_t)vifp, sizeof (*vifp));
910 log(LOG_DEBUG, "del_vif %d, numvifs %d\n", vifi, numvifs);
912 /* Adjust numvifs down */
913 for (vifi = numvifs; vifi > 0; vifi--)
914 if (viftable[vifi-1].v_lcl_addr.s_addr != INADDR_ANY)
924 * update an mfc entry without resetting counters and S,G addresses.
927 update_mfc_params(struct mfc *rt, struct mfcctl2 *mfccp)
931 rt->mfc_parent = mfccp->mfcc_parent;
932 for (i = 0; i < numvifs; i++) {
933 rt->mfc_ttls[i] = mfccp->mfcc_ttls[i];
934 rt->mfc_flags[i] = mfccp->mfcc_flags[i] & mrt_api_config &
937 /* set the RP address */
938 if (mrt_api_config & MRT_MFC_RP)
939 rt->mfc_rp = mfccp->mfcc_rp;
941 rt->mfc_rp.s_addr = INADDR_ANY;
945 * fully initialize an mfc entry from the parameter.
948 init_mfc_params(struct mfc *rt, struct mfcctl2 *mfccp)
950 rt->mfc_origin = mfccp->mfcc_origin;
951 rt->mfc_mcastgrp = mfccp->mfcc_mcastgrp;
953 update_mfc_params(rt, mfccp);
955 /* initialize pkt counters per src-grp */
957 rt->mfc_byte_cnt = 0;
958 rt->mfc_wrong_if = 0;
959 rt->mfc_last_assert.tv_sec = rt->mfc_last_assert.tv_usec = 0;
967 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;
1084 struct bw_meter *list;
1086 origin = mfccp->mfcc_origin;
1087 mcastgrp = mfccp->mfcc_mcastgrp;
1089 if (mrtdebug & DEBUG_MFC)
1090 log(LOG_DEBUG,"del_mfc orig %lx mcastgrp %lx\n",
1091 (u_long)ntohl(origin.s_addr), (u_long)ntohl(mcastgrp.s_addr));
1095 hash = MFCHASH(origin.s_addr, mcastgrp.s_addr);
1096 for (nptr = &mfctable[hash]; (rt = *nptr) != NULL; nptr = &rt->mfc_next)
1097 if (origin.s_addr == rt->mfc_origin.s_addr &&
1098 mcastgrp.s_addr == rt->mfc_mcastgrp.s_addr &&
1099 rt->mfc_stall == NULL)
1103 return EADDRNOTAVAIL;
1106 *nptr = rt->mfc_next;
1109 * free the bw_meter entries
1111 list = rt->mfc_bw_meter;
1112 rt->mfc_bw_meter = NULL;
1114 free(rt, M_MRTABLE);
1124 * Send a message to mrouted on the multicast routing socket
1127 socket_send(struct socket *s, struct mbuf *mm, struct sockaddr_in *src)
1130 if (sbappendaddr(&s->so_rcv, (struct sockaddr *)src, mm, NULL) != 0) {
1140 * IP multicast forwarding function. This function assumes that the packet
1141 * pointed to by "ip" has arrived on (or is about to be sent to) the interface
1142 * pointed to by "ifp", and the packet is to be relayed to other networks
1143 * that have members of the packet's destination IP multicast group.
1145 * The packet is returned unscathed to the caller, unless it is
1146 * erroneous, in which case a non-zero return value tells the caller to
1150 #define TUNNEL_LEN 12 /* # bytes of IP option for tunnel encapsulation */
1153 X_ip_mforward(struct ip *ip, struct ifnet *ifp, struct mbuf *m,
1154 struct ip_moptions *imo)
1159 if (mrtdebug & DEBUG_FORWARD)
1160 log(LOG_DEBUG, "ip_mforward: src %lx, dst %lx, ifp %p\n",
1161 (u_long)ntohl(ip->ip_src.s_addr), (u_long)ntohl(ip->ip_dst.s_addr),
1164 if (ip->ip_hl < (sizeof(struct ip) + TUNNEL_LEN) >> 2 ||
1165 ((u_char *)(ip + 1))[1] != IPOPT_LSRR ) {
1167 * Packet arrived via a physical interface or
1168 * an encapsulated tunnel or a register_vif.
1172 * Packet arrived through a source-route tunnel.
1173 * Source-route tunnels are no longer supported.
1175 static int last_log;
1176 if (last_log != time_second) {
1177 last_log = time_second;
1179 "ip_mforward: received source-routed packet from %lx\n",
1180 (u_long)ntohl(ip->ip_src.s_addr));
1185 if (imo && ((vifi = imo->imo_multicast_vif) < numvifs)) {
1186 if (ip->ip_ttl < 255)
1187 ip->ip_ttl++; /* compensate for -1 in *_send routines */
1188 if (rsvpdebug && ip->ip_p == IPPROTO_RSVP) {
1189 struct vif *vifp = viftable + vifi;
1191 printf("Sending IPPROTO_RSVP from %lx to %lx on vif %d (%s%s)\n",
1192 (long)ntohl(ip->ip_src.s_addr), (long)ntohl(ip->ip_dst.s_addr),
1194 (vifp->v_flags & VIFF_TUNNEL) ? "tunnel on " : "",
1195 vifp->v_ifp->if_xname);
1197 return ip_mdq(m, ifp, NULL, vifi);
1199 if (rsvpdebug && ip->ip_p == IPPROTO_RSVP) {
1200 printf("Warning: IPPROTO_RSVP from %lx to %lx without vif option\n",
1201 (long)ntohl(ip->ip_src.s_addr), (long)ntohl(ip->ip_dst.s_addr));
1203 printf("In fact, no options were specified at all\n");
1207 * Don't forward a packet with time-to-live of zero or one,
1208 * or a packet destined to a local-only group.
1210 if (ip->ip_ttl <= 1 || ntohl(ip->ip_dst.s_addr) <= INADDR_MAX_LOCAL_GROUP)
1214 * Determine forwarding vifs from the forwarding cache table
1217 ++mrtstat.mrts_mfc_lookups;
1218 rt = mfc_find(ip->ip_src.s_addr, ip->ip_dst.s_addr);
1220 /* Entry exists, so forward if necessary */
1223 return ip_mdq(m, ifp, rt, -1);
1226 * If we don't have a route for packet's origin,
1227 * Make a copy of the packet & send message to routing daemon
1233 int hlen = ip->ip_hl << 2;
1235 ++mrtstat.mrts_mfc_misses;
1237 mrtstat.mrts_no_route++;
1238 if (mrtdebug & (DEBUG_FORWARD | DEBUG_MFC))
1239 log(LOG_DEBUG, "ip_mforward: no rte s %lx g %lx\n",
1240 (u_long)ntohl(ip->ip_src.s_addr),
1241 (u_long)ntohl(ip->ip_dst.s_addr));
1244 * Allocate mbufs early so that we don't do extra work if we are
1245 * just going to fail anyway. Make sure to pullup the header so
1246 * that other people can't step on it.
1248 rte = malloc((sizeof *rte), M_MRTABLE, M_INTWAIT | M_NULLOK);
1254 mb0 = m_copypacket(m, MB_DONTWAIT);
1255 if (mb0 && (M_HASCL(mb0) || mb0->m_len < hlen))
1256 mb0 = m_pullup(mb0, hlen);
1258 free(rte, M_MRTABLE);
1263 /* is there an upcall waiting for this flow ? */
1264 hash = MFCHASH(ip->ip_src.s_addr, ip->ip_dst.s_addr);
1265 for (rt = mfctable[hash]; rt; rt = rt->mfc_next) {
1266 if ((ip->ip_src.s_addr == rt->mfc_origin.s_addr) &&
1267 (ip->ip_dst.s_addr == rt->mfc_mcastgrp.s_addr) &&
1268 (rt->mfc_stall != NULL))
1275 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
1279 * Locate the vifi for the incoming interface for this packet.
1280 * If none found, drop packet.
1282 for (vifi=0; vifi < numvifs && viftable[vifi].v_ifp != ifp; vifi++)
1284 if (vifi >= numvifs) /* vif not found, drop packet */
1287 /* no upcall, so make a new entry */
1288 rt = malloc(sizeof(*rt), M_MRTABLE, M_INTWAIT | M_NULLOK);
1292 /* Make a copy of the header to send to the user level process */
1293 mm = m_copy(mb0, 0, hlen);
1298 * Send message to routing daemon to install
1299 * a route into the kernel table
1302 im = mtod(mm, struct igmpmsg *);
1303 im->im_msgtype = IGMPMSG_NOCACHE;
1307 mrtstat.mrts_upcalls++;
1309 k_igmpsrc.sin_addr = ip->ip_src;
1310 if (socket_send(ip_mrouter, mm, &k_igmpsrc) < 0) {
1311 log(LOG_WARNING, "ip_mforward: ip_mrouter socket queue full\n");
1312 ++mrtstat.mrts_upq_sockfull;
1314 free(rt, M_MRTABLE);
1316 free(rte, M_MRTABLE);
1322 /* insert new entry at head of hash chain */
1323 rt->mfc_origin.s_addr = ip->ip_src.s_addr;
1324 rt->mfc_mcastgrp.s_addr = ip->ip_dst.s_addr;
1325 rt->mfc_expire = UPCALL_EXPIRE;
1327 for (i = 0; i < numvifs; i++) {
1328 rt->mfc_ttls[i] = 0;
1329 rt->mfc_flags[i] = 0;
1331 rt->mfc_parent = -1;
1333 rt->mfc_rp.s_addr = INADDR_ANY; /* clear the RP address */
1335 rt->mfc_bw_meter = NULL;
1337 /* link into table */
1338 rt->mfc_next = mfctable[hash];
1339 mfctable[hash] = rt;
1340 rt->mfc_stall = rte;
1343 /* determine if q has overflowed */
1348 * XXX ouch! we need to append to the list, but we
1349 * only have a pointer to the front, so we have to
1350 * scan the entire list every time.
1352 for (p = &rt->mfc_stall; *p != NULL; p = &(*p)->next)
1355 if (npkts > MAX_UPQ) {
1356 mrtstat.mrts_upq_ovflw++;
1358 free(rte, M_MRTABLE);
1364 /* Add this entry to the end of the queue */
1378 * Clean up the cache entry if upcall is not serviced
1381 expire_upcalls(void *unused)
1384 struct mfc *mfc, **nptr;
1388 for (i = 0; i < MFCTBLSIZ; i++) {
1389 if (nexpire[i] == 0)
1391 nptr = &mfctable[i];
1392 for (mfc = *nptr; mfc != NULL; mfc = *nptr) {
1394 * Skip real cache entries
1395 * Make sure it wasn't marked to not expire (shouldn't happen)
1398 if (mfc->mfc_stall != NULL && mfc->mfc_expire != 0 &&
1399 --mfc->mfc_expire == 0) {
1400 if (mrtdebug & DEBUG_EXPIRE)
1401 log(LOG_DEBUG, "expire_upcalls: expiring (%lx %lx)\n",
1402 (u_long)ntohl(mfc->mfc_origin.s_addr),
1403 (u_long)ntohl(mfc->mfc_mcastgrp.s_addr));
1405 * drop all the packets
1406 * free the mbuf with the pkt, if, timing info
1408 for (rte = mfc->mfc_stall; rte; ) {
1409 struct rtdetq *n = rte->next;
1412 free(rte, M_MRTABLE);
1415 ++mrtstat.mrts_cache_cleanups;
1419 * free the bw_meter entries
1421 while (mfc->mfc_bw_meter != NULL) {
1422 struct bw_meter *x = mfc->mfc_bw_meter;
1424 mfc->mfc_bw_meter = x->bm_mfc_next;
1428 *nptr = mfc->mfc_next;
1429 free(mfc, M_MRTABLE);
1431 nptr = &mfc->mfc_next;
1435 callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT, expire_upcalls, NULL);
1440 * Packet forwarding routine once entry in the cache is made
1443 ip_mdq(struct mbuf *m, struct ifnet *ifp, struct mfc *rt, vifi_t xmt_vif)
1445 struct ip *ip = mtod(m, struct ip *);
1447 int plen = ip->ip_len;
1450 * Macro to send packet on vif. Since RSVP packets don't get counted on
1451 * input, they shouldn't get counted on output, so statistics keeping is
1454 #define MC_SEND(ip,vifp,m) { \
1455 if ((vifp)->v_flags & VIFF_TUNNEL) \
1456 encap_send((ip), (vifp), (m)); \
1458 phyint_send((ip), (vifp), (m)); \
1462 * If xmt_vif is not -1, send on only the requested vif.
1464 * (since vifi_t is u_short, -1 becomes MAXUSHORT, which > numvifs.)
1466 if (xmt_vif < numvifs) {
1468 if (viftable[xmt_vif].v_flags & VIFF_REGISTER)
1469 pim_register_send(ip, viftable + xmt_vif, m, rt);
1472 MC_SEND(ip, viftable + xmt_vif, m);
1477 * Don't forward if it didn't arrive from the parent vif for its origin.
1479 vifi = rt->mfc_parent;
1480 if ((vifi >= numvifs) || (viftable[vifi].v_ifp != ifp)) {
1481 /* came in the wrong interface */
1482 if (mrtdebug & DEBUG_FORWARD)
1483 log(LOG_DEBUG, "wrong if: ifp %p vifi %d vififp %p\n",
1484 (void *)ifp, vifi, (void *)viftable[vifi].v_ifp);
1485 ++mrtstat.mrts_wrong_if;
1488 * If we are doing PIM assert processing, send a message
1489 * to the routing daemon.
1491 * XXX: A PIM-SM router needs the WRONGVIF detection so it
1492 * can complete the SPT switch, regardless of the type
1493 * of the iif (broadcast media, GRE tunnel, etc).
1495 if (pim_assert && (vifi < numvifs) && viftable[vifi].v_ifp) {
1500 if (ifp == &multicast_register_if)
1501 pimstat.pims_rcv_registers_wrongiif++;
1504 /* Get vifi for the incoming packet */
1505 for (vifi=0; vifi < numvifs && viftable[vifi].v_ifp != ifp; vifi++)
1507 if (vifi >= numvifs)
1508 return 0; /* The iif is not found: ignore the packet. */
1510 if (rt->mfc_flags[vifi] & MRT_MFC_FLAGS_DISABLE_WRONGVIF)
1511 return 0; /* WRONGVIF disabled: ignore the packet */
1515 TV_DELTA(rt->mfc_last_assert, now, delta);
1517 if (delta > ASSERT_MSG_TIME) {
1518 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
1520 int hlen = ip->ip_hl << 2;
1521 struct mbuf *mm = m_copy(m, 0, hlen);
1523 if (mm && (M_HASCL(mm) || mm->m_len < hlen))
1524 mm = m_pullup(mm, hlen);
1528 rt->mfc_last_assert = now;
1530 im = mtod(mm, struct igmpmsg *);
1531 im->im_msgtype = IGMPMSG_WRONGVIF;
1535 mrtstat.mrts_upcalls++;
1537 k_igmpsrc.sin_addr = im->im_src;
1538 if (socket_send(ip_mrouter, mm, &k_igmpsrc) < 0) {
1540 "ip_mforward: ip_mrouter socket queue full\n");
1541 ++mrtstat.mrts_upq_sockfull;
1549 /* If I sourced this packet, it counts as output, else it was input. */
1550 if (ip->ip_src.s_addr == viftable[vifi].v_lcl_addr.s_addr) {
1551 viftable[vifi].v_pkt_out++;
1552 viftable[vifi].v_bytes_out += plen;
1554 viftable[vifi].v_pkt_in++;
1555 viftable[vifi].v_bytes_in += plen;
1558 rt->mfc_byte_cnt += plen;
1561 * For each vif, decide if a copy of the packet should be forwarded.
1563 * - the ttl exceeds the vif's threshold
1564 * - there are group members downstream on interface
1566 for (vifi = 0; vifi < numvifs; vifi++)
1567 if ((rt->mfc_ttls[vifi] > 0) && (ip->ip_ttl > rt->mfc_ttls[vifi])) {
1568 viftable[vifi].v_pkt_out++;
1569 viftable[vifi].v_bytes_out += plen;
1571 if (viftable[vifi].v_flags & VIFF_REGISTER)
1572 pim_register_send(ip, viftable + vifi, m, rt);
1575 MC_SEND(ip, viftable+vifi, m);
1579 * Perform upcall-related bw measuring.
1581 if (rt->mfc_bw_meter != NULL) {
1586 for (x = rt->mfc_bw_meter; x != NULL; x = x->bm_mfc_next)
1587 bw_meter_receive_packet(x, plen, &now);
1594 * check if a vif number is legal/ok. This is used by ip_output.
1597 X_legal_vif_num(int vif)
1599 return (vif >= 0 && vif < numvifs);
1603 * Return the local address used by this vif
1606 X_ip_mcast_src(int vifi)
1608 if (vifi >= 0 && vifi < numvifs)
1609 return viftable[vifi].v_lcl_addr.s_addr;
1615 phyint_send(struct ip *ip, struct vif *vifp, struct mbuf *m)
1617 struct mbuf *mb_copy;
1618 int hlen = ip->ip_hl << 2;
1621 * Make a new reference to the packet; make sure that
1622 * the IP header is actually copied, not just referenced,
1623 * so that ip_output() only scribbles on the copy.
1625 mb_copy = m_copypacket(m, MB_DONTWAIT);
1626 if (mb_copy && (M_HASCL(mb_copy) || mb_copy->m_len < hlen))
1627 mb_copy = m_pullup(mb_copy, hlen);
1628 if (mb_copy == NULL)
1631 if (vifp->v_rate_limit == 0)
1632 tbf_send_packet(vifp, mb_copy);
1634 tbf_control(vifp, mb_copy, mtod(mb_copy, struct ip *), ip->ip_len);
1638 encap_send(struct ip *ip, struct vif *vifp, struct mbuf *m)
1640 struct mbuf *mb_copy;
1642 int i, len = ip->ip_len;
1644 /* Take care of delayed checksums */
1645 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
1646 in_delayed_cksum(m);
1647 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
1651 * copy the old packet & pullup its IP header into the
1652 * new mbuf so we can modify it. Try to fill the new
1653 * mbuf since if we don't the ethernet driver will.
1655 MGETHDR(mb_copy, MB_DONTWAIT, MT_HEADER);
1656 if (mb_copy == NULL)
1658 mb_copy->m_data += max_linkhdr;
1659 mb_copy->m_len = sizeof(multicast_encap_iphdr);
1661 if ((mb_copy->m_next = m_copypacket(m, MB_DONTWAIT)) == NULL) {
1665 i = MHLEN - M_LEADINGSPACE(mb_copy);
1668 mb_copy = m_pullup(mb_copy, i);
1669 if (mb_copy == NULL)
1671 mb_copy->m_pkthdr.len = len + sizeof(multicast_encap_iphdr);
1674 * fill in the encapsulating IP header.
1676 ip_copy = mtod(mb_copy, struct ip *);
1677 *ip_copy = multicast_encap_iphdr;
1679 ip_copy->ip_id = ip_randomid();
1681 ip_copy->ip_id = htons(ip_id++);
1683 ip_copy->ip_len += len;
1684 ip_copy->ip_src = vifp->v_lcl_addr;
1685 ip_copy->ip_dst = vifp->v_rmt_addr;
1688 * turn the encapsulated IP header back into a valid one.
1690 ip = (struct ip *)((caddr_t)ip_copy + sizeof(multicast_encap_iphdr));
1692 ip->ip_len = htons(ip->ip_len);
1693 ip->ip_off = htons(ip->ip_off);
1695 mb_copy->m_data += sizeof(multicast_encap_iphdr);
1696 ip->ip_sum = in_cksum(mb_copy, ip->ip_hl << 2);
1697 mb_copy->m_data -= sizeof(multicast_encap_iphdr);
1699 if (vifp->v_rate_limit == 0)
1700 tbf_send_packet(vifp, mb_copy);
1702 tbf_control(vifp, mb_copy, ip, ip_copy->ip_len);
1706 * De-encapsulate a packet and feed it back through ip input (this
1707 * routine is called whenever IP gets a packet with proto type
1708 * ENCAP_PROTO and a local destination address).
1710 * This is similar to mroute_encapcheck() + mroute_encap_input() in -current.
1713 X_ipip_input(struct mbuf *m, int off, int proto)
1715 struct ip *ip = mtod(m, struct ip *);
1716 int hlen = ip->ip_hl << 2;
1718 if (!have_encap_tunnel) {
1719 rip_input(m, off, proto);
1723 * dump the packet if it's not to a multicast destination or if
1724 * we don't have an encapsulating tunnel with the source.
1725 * Note: This code assumes that the remote site IP address
1726 * uniquely identifies the tunnel (i.e., that this site has
1727 * at most one tunnel with the remote site).
1729 if (!IN_MULTICAST(ntohl(((struct ip *)((char *)ip+hlen))->ip_dst.s_addr))) {
1730 ++mrtstat.mrts_bad_tunnel;
1734 if (ip->ip_src.s_addr != last_encap_src) {
1735 struct vif *vifp = viftable;
1736 struct vif *vife = vifp + numvifs;
1738 last_encap_src = ip->ip_src.s_addr;
1739 last_encap_vif = NULL;
1740 for ( ; vifp < vife; ++vifp)
1741 if (vifp->v_rmt_addr.s_addr == ip->ip_src.s_addr) {
1742 if ((vifp->v_flags & (VIFF_TUNNEL|VIFF_SRCRT))
1744 last_encap_vif = vifp;
1748 if (last_encap_vif == NULL) {
1749 last_encap_src = INADDR_ANY;
1750 mrtstat.mrts_cant_tunnel++; /*XXX*/
1753 log(LOG_DEBUG, "ip_mforward: no tunnel with %lx\n",
1754 (u_long)ntohl(ip->ip_src.s_addr));
1758 if (hlen > sizeof(struct ip))
1760 m->m_data += sizeof(struct ip);
1761 m->m_len -= sizeof(struct ip);
1762 m->m_pkthdr.len -= sizeof(struct ip);
1763 m->m_pkthdr.rcvif = last_encap_vif->v_ifp;
1765 netisr_queue(NETISR_IP, m);
1769 * Token bucket filter module
1773 tbf_control(struct vif *vifp, struct mbuf *m, struct ip *ip, u_long p_len)
1775 struct tbf *t = vifp->v_tbf;
1777 if (p_len > MAX_BKT_SIZE) { /* drop if packet is too large */
1778 mrtstat.mrts_pkt2large++;
1783 tbf_update_tokens(vifp);
1785 if (t->tbf_q_len == 0) { /* queue empty... */
1786 if (p_len <= t->tbf_n_tok) { /* send packet if enough tokens */
1787 t->tbf_n_tok -= p_len;
1788 tbf_send_packet(vifp, m);
1789 } else { /* no, queue packet and try later */
1791 callout_reset(&tbf_reprocess_q_ch, TBF_REPROCESS,
1792 tbf_reprocess_q, vifp);
1794 } else if (t->tbf_q_len < t->tbf_max_q_len) {
1795 /* finite queue length, so queue pkts and process queue */
1797 tbf_process_q(vifp);
1799 /* queue full, try to dq and queue and process */
1800 if (!tbf_dq_sel(vifp, ip)) {
1801 mrtstat.mrts_q_overflow++;
1805 tbf_process_q(vifp);
1811 * adds a packet to the queue at the interface
1814 tbf_queue(struct vif *vifp, struct mbuf *m)
1816 struct tbf *t = vifp->v_tbf;
1820 if (t->tbf_t == NULL) /* Queue was empty */
1822 else /* Insert at tail */
1823 t->tbf_t->m_nextpkt = m;
1825 t->tbf_t = m; /* Set new tail pointer */
1828 /* Make sure we didn't get fed a bogus mbuf */
1830 panic("tbf_queue: m_nextpkt");
1832 m->m_nextpkt = NULL;
1840 * processes the queue at the interface
1843 tbf_process_q(struct vif *vifp)
1845 struct tbf *t = vifp->v_tbf;
1849 /* loop through the queue at the interface and send as many packets
1852 while (t->tbf_q_len > 0) {
1853 struct mbuf *m = t->tbf_q;
1854 int len = mtod(m, struct ip *)->ip_len;
1856 /* determine if the packet can be sent */
1857 if (len > t->tbf_n_tok) /* not enough tokens, we are done */
1859 /* ok, reduce no of tokens, dequeue and send the packet. */
1860 t->tbf_n_tok -= len;
1862 t->tbf_q = m->m_nextpkt;
1863 if (--t->tbf_q_len == 0)
1866 m->m_nextpkt = NULL;
1867 tbf_send_packet(vifp, m);
1873 tbf_reprocess_q(void *xvifp)
1875 struct vif *vifp = xvifp;
1877 if (ip_mrouter == NULL)
1879 tbf_update_tokens(vifp);
1880 tbf_process_q(vifp);
1881 if (vifp->v_tbf->tbf_q_len)
1882 callout_reset(&tbf_reprocess_q_ch, TBF_REPROCESS,
1883 tbf_reprocess_q, vifp);
1886 /* function that will selectively discard a member of the queue
1887 * based on the precedence value and the priority
1890 tbf_dq_sel(struct vif *vifp, struct ip *ip)
1893 struct mbuf *m, *last;
1895 struct tbf *t = vifp->v_tbf;
1899 p = priority(vifp, ip);
1903 while ((m = *np) != NULL) {
1904 if (p > priority(vifp, mtod(m, struct ip *))) {
1906 /* If we're removing the last packet, fix the tail pointer */
1910 /* It's impossible for the queue to be empty, but check anyways. */
1911 if (--t->tbf_q_len == 0)
1914 mrtstat.mrts_drop_sel++;
1925 tbf_send_packet(struct vif *vifp, struct mbuf *m)
1929 if (vifp->v_flags & VIFF_TUNNEL) /* If tunnel options */
1930 ip_output(m, NULL, &vifp->v_route, IP_FORWARDING, NULL, NULL);
1932 struct ip_moptions imo;
1934 static struct route ro; /* XXX check this */
1936 imo.imo_multicast_ifp = vifp->v_ifp;
1937 imo.imo_multicast_ttl = mtod(m, struct ip *)->ip_ttl - 1;
1938 imo.imo_multicast_loop = 1;
1939 imo.imo_multicast_vif = -1;
1942 * Re-entrancy should not be a problem here, because
1943 * the packets that we send out and are looped back at us
1944 * should get rejected because they appear to come from
1945 * the loopback interface, thus preventing looping.
1947 error = ip_output(m, NULL, &ro, IP_FORWARDING, &imo, NULL);
1949 if (mrtdebug & DEBUG_XMIT)
1950 log(LOG_DEBUG, "phyint_send on vif %d err %d\n",
1951 (int)(vifp - viftable), error);
1956 /* determine the current time and then
1957 * the elapsed time (between the last time and time now)
1958 * in milliseconds & update the no. of tokens in the bucket
1961 tbf_update_tokens(struct vif *vifp)
1965 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)
2087 /* Don't bother if it is not the right type of socket. */
2088 if (so->so_type != SOCK_RAW || so->so_proto->pr_protocol != IPPROTO_RSVP)
2093 /* The socket may be attached to more than one vif...this
2094 * is perfectly legal.
2096 for (vifi = 0; vifi < numvifs; vifi++) {
2097 if (viftable[vifi].v_rsvpd == so) {
2098 viftable[vifi].v_rsvpd = NULL;
2099 /* This may seem silly, but we need to be sure we don't
2100 * over-decrement the RSVP counter, in case something slips up.
2102 if (viftable[vifi].v_rsvp_on) {
2103 viftable[vifi].v_rsvp_on = 0;
2113 X_rsvp_input(struct mbuf *m, ...)
2116 struct ip *ip = mtod(m, struct ip *);
2117 struct sockaddr_in rsvp_src = { sizeof rsvp_src, AF_INET };
2121 /* support IP_RECVIF used by rsvpd rel4.2a1 */
2129 off = __va_arg(ap, int);
2130 proto = __va_arg(ap, int);
2134 printf("rsvp_input: rsvp_on %d\n",rsvp_on);
2136 /* Can still get packets with rsvp_on = 0 if there is a local member
2137 * of the group to which the RSVP packet is addressed. But in this
2138 * case we want to throw the packet away.
2148 printf("rsvp_input: check vifs\n");
2151 if (!(m->m_flags & M_PKTHDR))
2152 panic("rsvp_input no hdr");
2155 ifp = m->m_pkthdr.rcvif;
2156 /* Find which vif the packet arrived on. */
2157 for (vifi = 0; vifi < numvifs; vifi++)
2158 if (viftable[vifi].v_ifp == ifp)
2162 if (vifi == numvifs || (so = viftable[vifi].v_rsvpd) == NULL) {
2164 if (vifi == numvifs || viftable[vifi].v_rsvpd == NULL) {
2167 * If the old-style non-vif-associated socket is set,
2168 * then use it. Otherwise, drop packet since there
2169 * is no specific socket for this vif.
2171 if (ip_rsvpd != NULL) {
2173 printf("rsvp_input: Sending packet up old-style socket\n");
2174 rip_input(m, off, proto); /* xxx */
2176 if (rsvpdebug && vifi == numvifs)
2177 printf("rsvp_input: Can't find vif for packet.\n");
2178 else if (rsvpdebug && viftable[vifi].v_rsvpd == NULL)
2179 printf("rsvp_input: No socket defined for vif %d\n",vifi);
2185 rsvp_src.sin_addr = ip->ip_src;
2188 printf("rsvp_input: m->m_len = %d, sbspace() = %ld\n",
2189 m->m_len,sbspace(&(viftable[vifi].v_rsvpd->so_rcv)));
2193 inp = (struct inpcb *)so->so_pcb;
2194 if (inp->inp_flags & INP_CONTROLOPTS ||
2195 inp->inp_socket->so_options & SO_TIMESTAMP)
2196 ip_savecontrol(inp, &opts, ip, m);
2197 if (sbappendaddr(&so->so_rcv,
2198 (struct sockaddr *)&rsvp_src,m, opts) == 0) {
2203 printf("rsvp_input: Failed to append to socket\n");
2208 printf("rsvp_input: send packet up\n");
2211 if (socket_send(viftable[vifi].v_rsvpd, m, &rsvp_src) < 0) {
2213 printf("rsvp_input: Failed to append to socket\n");
2216 printf("rsvp_input: send packet up\n");
2224 * Code for bandwidth monitors
2228 * Define common interface for timeval-related methods
2230 #define BW_TIMEVALCMP(tvp, uvp, cmp) timevalcmp((tvp), (uvp), cmp)
2231 #define BW_TIMEVALDECR(vvp, uvp) timevalsub((vvp), (uvp))
2232 #define BW_TIMEVALADD(vvp, uvp) timevaladd((vvp), (uvp))
2235 compute_bw_meter_flags(struct bw_upcall *req)
2239 if (req->bu_flags & BW_UPCALL_UNIT_PACKETS)
2240 flags |= BW_METER_UNIT_PACKETS;
2241 if (req->bu_flags & BW_UPCALL_UNIT_BYTES)
2242 flags |= BW_METER_UNIT_BYTES;
2243 if (req->bu_flags & BW_UPCALL_GEQ)
2244 flags |= BW_METER_GEQ;
2245 if (req->bu_flags & BW_UPCALL_LEQ)
2246 flags |= BW_METER_LEQ;
2252 * Add a bw_meter entry
2255 add_bw_upcall(struct bw_upcall *req)
2258 struct timeval delta = { BW_UPCALL_THRESHOLD_INTERVAL_MIN_SEC,
2259 BW_UPCALL_THRESHOLD_INTERVAL_MIN_USEC };
2264 if (!(mrt_api_config & MRT_MFC_BW_UPCALL))
2267 /* Test if the flags are valid */
2268 if (!(req->bu_flags & (BW_UPCALL_UNIT_PACKETS | BW_UPCALL_UNIT_BYTES)))
2270 if (!(req->bu_flags & (BW_UPCALL_GEQ | BW_UPCALL_LEQ)))
2272 if ((req->bu_flags & (BW_UPCALL_GEQ | BW_UPCALL_LEQ))
2273 == (BW_UPCALL_GEQ | BW_UPCALL_LEQ))
2276 /* Test if the threshold time interval is valid */
2277 if (BW_TIMEVALCMP(&req->bu_threshold.b_time, &delta, <))
2280 flags = compute_bw_meter_flags(req);
2283 * Find if we have already same bw_meter entry
2286 mfc = mfc_find(req->bu_src.s_addr, req->bu_dst.s_addr);
2289 return EADDRNOTAVAIL;
2291 for (x = mfc->mfc_bw_meter; x != NULL; x = x->bm_mfc_next) {
2292 if ((BW_TIMEVALCMP(&x->bm_threshold.b_time,
2293 &req->bu_threshold.b_time, ==)) &&
2294 (x->bm_threshold.b_packets == req->bu_threshold.b_packets) &&
2295 (x->bm_threshold.b_bytes == req->bu_threshold.b_bytes) &&
2296 (x->bm_flags & BW_METER_USER_FLAGS) == flags) {
2298 return 0; /* XXX Already installed */
2303 /* Allocate the new bw_meter entry */
2304 x = malloc(sizeof(*x), M_BWMETER, M_INTWAIT);
2306 /* Set the new bw_meter entry */
2307 x->bm_threshold.b_time = req->bu_threshold.b_time;
2309 x->bm_start_time = now;
2310 x->bm_threshold.b_packets = req->bu_threshold.b_packets;
2311 x->bm_threshold.b_bytes = req->bu_threshold.b_bytes;
2312 x->bm_measured.b_packets = 0;
2313 x->bm_measured.b_bytes = 0;
2314 x->bm_flags = flags;
2315 x->bm_time_next = NULL;
2316 x->bm_time_hash = BW_METER_BUCKETS;
2318 /* Add the new bw_meter entry to the front of entries for this MFC */
2321 x->bm_mfc_next = mfc->mfc_bw_meter;
2322 mfc->mfc_bw_meter = x;
2323 schedule_bw_meter(x, &now);
2330 free_bw_list(struct bw_meter *list)
2332 while (list != NULL) {
2333 struct bw_meter *x = list;
2335 list = list->bm_mfc_next;
2336 unschedule_bw_meter(x);
2342 * Delete one or multiple bw_meter entries
2345 del_bw_upcall(struct bw_upcall *req)
2350 if (!(mrt_api_config & MRT_MFC_BW_UPCALL))
2354 /* Find the corresponding MFC entry */
2355 mfc = mfc_find(req->bu_src.s_addr, req->bu_dst.s_addr);
2358 return EADDRNOTAVAIL;
2359 } else if (req->bu_flags & BW_UPCALL_DELETE_ALL) {
2361 * Delete all bw_meter entries for this mfc
2363 struct bw_meter *list;
2365 list = mfc->mfc_bw_meter;
2366 mfc->mfc_bw_meter = NULL;
2370 } else { /* Delete a single bw_meter entry */
2371 struct bw_meter *prev;
2374 flags = compute_bw_meter_flags(req);
2376 /* Find the bw_meter entry to delete */
2377 for (prev = NULL, x = mfc->mfc_bw_meter; x != NULL;
2378 prev = x, x = x->bm_mfc_next) {
2379 if ((BW_TIMEVALCMP(&x->bm_threshold.b_time,
2380 &req->bu_threshold.b_time, ==)) &&
2381 (x->bm_threshold.b_packets == req->bu_threshold.b_packets) &&
2382 (x->bm_threshold.b_bytes == req->bu_threshold.b_bytes) &&
2383 (x->bm_flags & BW_METER_USER_FLAGS) == flags)
2386 if (x != NULL) { /* Delete entry from the list for this MFC */
2388 prev->bm_mfc_next = x->bm_mfc_next; /* remove from middle*/
2390 x->bm_mfc->mfc_bw_meter = x->bm_mfc_next;/* new head of list */
2393 unschedule_bw_meter(x);
2394 /* Free the bw_meter entry */
2406 * Perform bandwidth measurement processing that may result in an upcall
2409 bw_meter_receive_packet(struct bw_meter *x, int plen, struct timeval *nowp)
2411 struct timeval delta;
2415 BW_TIMEVALDECR(&delta, &x->bm_start_time);
2417 if (x->bm_flags & BW_METER_GEQ) {
2419 * Processing for ">=" type of bw_meter entry
2421 if (BW_TIMEVALCMP(&delta, &x->bm_threshold.b_time, >)) {
2422 /* Reset the bw_meter entry */
2423 x->bm_start_time = *nowp;
2424 x->bm_measured.b_packets = 0;
2425 x->bm_measured.b_bytes = 0;
2426 x->bm_flags &= ~BW_METER_UPCALL_DELIVERED;
2429 /* Record that a packet is received */
2430 x->bm_measured.b_packets++;
2431 x->bm_measured.b_bytes += plen;
2434 * Test if we should deliver an upcall
2436 if (!(x->bm_flags & BW_METER_UPCALL_DELIVERED)) {
2437 if (((x->bm_flags & BW_METER_UNIT_PACKETS) &&
2438 (x->bm_measured.b_packets >= x->bm_threshold.b_packets)) ||
2439 ((x->bm_flags & BW_METER_UNIT_BYTES) &&
2440 (x->bm_measured.b_bytes >= x->bm_threshold.b_bytes))) {
2441 /* Prepare an upcall for delivery */
2442 bw_meter_prepare_upcall(x, nowp);
2443 x->bm_flags |= BW_METER_UPCALL_DELIVERED;
2446 } else if (x->bm_flags & BW_METER_LEQ) {
2448 * Processing for "<=" type of bw_meter entry
2450 if (BW_TIMEVALCMP(&delta, &x->bm_threshold.b_time, >)) {
2452 * We are behind time with the multicast forwarding table
2453 * scanning for "<=" type of bw_meter entries, so test now
2454 * if we should deliver an upcall.
2456 if (((x->bm_flags & BW_METER_UNIT_PACKETS) &&
2457 (x->bm_measured.b_packets <= x->bm_threshold.b_packets)) ||
2458 ((x->bm_flags & BW_METER_UNIT_BYTES) &&
2459 (x->bm_measured.b_bytes <= x->bm_threshold.b_bytes))) {
2460 /* Prepare an upcall for delivery */
2461 bw_meter_prepare_upcall(x, nowp);
2463 /* Reschedule the bw_meter entry */
2464 unschedule_bw_meter(x);
2465 schedule_bw_meter(x, nowp);
2468 /* Record that a packet is received */
2469 x->bm_measured.b_packets++;
2470 x->bm_measured.b_bytes += plen;
2473 * Test if we should restart the measuring interval
2475 if ((x->bm_flags & BW_METER_UNIT_PACKETS &&
2476 x->bm_measured.b_packets <= x->bm_threshold.b_packets) ||
2477 (x->bm_flags & BW_METER_UNIT_BYTES &&
2478 x->bm_measured.b_bytes <= x->bm_threshold.b_bytes)) {
2479 /* Don't restart the measuring interval */
2481 /* Do restart the measuring interval */
2483 * XXX: note that we don't unschedule and schedule, because this
2484 * might be too much overhead per packet. Instead, when we process
2485 * all entries for a given timer hash bin, we check whether it is
2486 * really a timeout. If not, we reschedule at that time.
2488 x->bm_start_time = *nowp;
2489 x->bm_measured.b_packets = 0;
2490 x->bm_measured.b_bytes = 0;
2491 x->bm_flags &= ~BW_METER_UPCALL_DELIVERED;
2498 * Prepare a bandwidth-related upcall
2501 bw_meter_prepare_upcall(struct bw_meter *x, struct timeval *nowp)
2503 struct timeval delta;
2504 struct bw_upcall *u;
2509 * Compute the measured time interval
2512 BW_TIMEVALDECR(&delta, &x->bm_start_time);
2515 * If there are too many pending upcalls, deliver them now
2517 if (bw_upcalls_n >= BW_UPCALLS_MAX)
2521 * Set the bw_upcall entry
2523 u = &bw_upcalls[bw_upcalls_n++];
2524 u->bu_src = x->bm_mfc->mfc_origin;
2525 u->bu_dst = x->bm_mfc->mfc_mcastgrp;
2526 u->bu_threshold.b_time = x->bm_threshold.b_time;
2527 u->bu_threshold.b_packets = x->bm_threshold.b_packets;
2528 u->bu_threshold.b_bytes = x->bm_threshold.b_bytes;
2529 u->bu_measured.b_time = delta;
2530 u->bu_measured.b_packets = x->bm_measured.b_packets;
2531 u->bu_measured.b_bytes = x->bm_measured.b_bytes;
2533 if (x->bm_flags & BW_METER_UNIT_PACKETS)
2534 u->bu_flags |= BW_UPCALL_UNIT_PACKETS;
2535 if (x->bm_flags & BW_METER_UNIT_BYTES)
2536 u->bu_flags |= BW_UPCALL_UNIT_BYTES;
2537 if (x->bm_flags & BW_METER_GEQ)
2538 u->bu_flags |= BW_UPCALL_GEQ;
2539 if (x->bm_flags & BW_METER_LEQ)
2540 u->bu_flags |= BW_UPCALL_LEQ;
2546 * Send the pending bandwidth-related upcalls
2549 bw_upcalls_send(void)
2552 int len = bw_upcalls_n * sizeof(bw_upcalls[0]);
2553 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
2554 static struct igmpmsg igmpmsg = { 0, /* unused1 */
2556 IGMPMSG_BW_UPCALL,/* im_msgtype */
2561 { 0 } }; /* im_dst */
2563 if (bw_upcalls_n == 0)
2564 return; /* No pending upcalls */
2569 * Allocate a new mbuf, initialize it with the header and
2570 * the payload for the pending calls.
2572 MGETHDR(m, MB_DONTWAIT, MT_HEADER);
2574 log(LOG_WARNING, "bw_upcalls_send: cannot allocate mbuf\n");
2578 m->m_len = m->m_pkthdr.len = 0;
2579 m_copyback(m, 0, sizeof(struct igmpmsg), (caddr_t)&igmpmsg);
2580 m_copyback(m, sizeof(struct igmpmsg), len, (caddr_t)&bw_upcalls[0]);
2584 * XXX do we need to set the address in k_igmpsrc ?
2586 mrtstat.mrts_upcalls++;
2587 if (socket_send(ip_mrouter, m, &k_igmpsrc) < 0) {
2588 log(LOG_WARNING, "bw_upcalls_send: ip_mrouter socket queue full\n");
2589 ++mrtstat.mrts_upq_sockfull;
2594 * Compute the timeout hash value for the bw_meter entries
2596 #define BW_METER_TIMEHASH(bw_meter, hash) \
2598 struct timeval next_timeval = (bw_meter)->bm_start_time; \
2600 BW_TIMEVALADD(&next_timeval, &(bw_meter)->bm_threshold.b_time); \
2601 (hash) = next_timeval.tv_sec; \
2602 if (next_timeval.tv_usec) \
2603 (hash)++; /* XXX: make sure we don't timeout early */ \
2604 (hash) %= BW_METER_BUCKETS; \
2608 * Schedule a timer to process periodically bw_meter entry of type "<="
2609 * by linking the entry in the proper hash bucket.
2612 schedule_bw_meter(struct bw_meter *x, struct timeval *nowp)
2616 if (!(x->bm_flags & BW_METER_LEQ))
2617 return; /* XXX: we schedule timers only for "<=" entries */
2620 * Reset the bw_meter entry
2623 x->bm_start_time = *nowp;
2624 x->bm_measured.b_packets = 0;
2625 x->bm_measured.b_bytes = 0;
2626 x->bm_flags &= ~BW_METER_UPCALL_DELIVERED;
2630 * Compute the timeout hash value and insert the entry
2632 BW_METER_TIMEHASH(x, time_hash);
2633 x->bm_time_next = bw_meter_timers[time_hash];
2634 bw_meter_timers[time_hash] = x;
2635 x->bm_time_hash = time_hash;
2639 * Unschedule the periodic timer that processes bw_meter entry of type "<="
2640 * by removing the entry from the proper hash bucket.
2643 unschedule_bw_meter(struct bw_meter *x)
2646 struct bw_meter *prev, *tmp;
2648 if (!(x->bm_flags & BW_METER_LEQ))
2649 return; /* XXX: we schedule timers only for "<=" entries */
2652 * Compute the timeout hash value and delete the entry
2654 time_hash = x->bm_time_hash;
2655 if (time_hash >= BW_METER_BUCKETS)
2656 return; /* Entry was not scheduled */
2658 for (prev = NULL, tmp = bw_meter_timers[time_hash];
2659 tmp != NULL; prev = tmp, tmp = tmp->bm_time_next)
2664 panic("unschedule_bw_meter: bw_meter entry not found");
2667 prev->bm_time_next = x->bm_time_next;
2669 bw_meter_timers[time_hash] = x->bm_time_next;
2671 x->bm_time_next = NULL;
2672 x->bm_time_hash = BW_METER_BUCKETS;
2677 * Process all "<=" type of bw_meter that should be processed now,
2678 * and for each entry prepare an upcall if necessary. Each processed
2679 * entry is rescheduled again for the (periodic) processing.
2681 * This is run periodically (once per second normally). On each round,
2682 * all the potentially matching entries are in the hash slot that we are
2688 static uint32_t last_tv_sec; /* last time we processed this */
2692 struct timeval now, process_endtime;
2695 if (last_tv_sec == now.tv_sec)
2696 return; /* nothing to do */
2699 loops = now.tv_sec - last_tv_sec;
2700 last_tv_sec = now.tv_sec;
2701 if (loops > BW_METER_BUCKETS)
2702 loops = BW_METER_BUCKETS;
2705 * Process all bins of bw_meter entries from the one after the last
2706 * processed to the current one. On entry, i points to the last bucket
2707 * visited, so we need to increment i at the beginning of the loop.
2709 for (i = (now.tv_sec - loops) % BW_METER_BUCKETS; loops > 0; loops--) {
2710 struct bw_meter *x, *tmp_list;
2712 if (++i >= BW_METER_BUCKETS)
2715 /* Disconnect the list of bw_meter entries from the bin */
2716 tmp_list = bw_meter_timers[i];
2717 bw_meter_timers[i] = NULL;
2719 /* Process the list of bw_meter entries */
2720 while (tmp_list != NULL) {
2722 tmp_list = tmp_list->bm_time_next;
2724 /* Test if the time interval is over */
2725 process_endtime = x->bm_start_time;
2726 BW_TIMEVALADD(&process_endtime, &x->bm_threshold.b_time);
2727 if (BW_TIMEVALCMP(&process_endtime, &now, >)) {
2728 /* Not yet: reschedule, but don't reset */
2731 BW_METER_TIMEHASH(x, time_hash);
2732 if (time_hash == i && process_endtime.tv_sec == now.tv_sec) {
2734 * XXX: somehow the bin processing is a bit ahead of time.
2735 * Put the entry in the next bin.
2737 if (++time_hash >= BW_METER_BUCKETS)
2740 x->bm_time_next = bw_meter_timers[time_hash];
2741 bw_meter_timers[time_hash] = x;
2742 x->bm_time_hash = time_hash;
2748 * Test if we should deliver an upcall
2750 if (((x->bm_flags & BW_METER_UNIT_PACKETS) &&
2751 (x->bm_measured.b_packets <= x->bm_threshold.b_packets)) ||
2752 ((x->bm_flags & BW_METER_UNIT_BYTES) &&
2753 (x->bm_measured.b_bytes <= x->bm_threshold.b_bytes))) {
2754 /* Prepare an upcall for delivery */
2755 bw_meter_prepare_upcall(x, &now);
2759 * Reschedule for next processing
2761 schedule_bw_meter(x, &now);
2766 /* Send all upcalls that are pending delivery */
2771 * A periodic function for sending all upcalls that are pending delivery
2774 expire_bw_upcalls_send(void *unused)
2778 callout_reset(&bw_upcalls_ch, BW_UPCALLS_PERIOD,
2779 expire_bw_upcalls_send, NULL);
2783 * A periodic function for periodic scanning of the multicast forwarding
2784 * table for processing all "<=" bw_meter entries.
2787 expire_bw_meter_process(void *unused)
2789 if (mrt_api_config & MRT_MFC_BW_UPCALL)
2792 callout_reset(&bw_meter_ch, BW_METER_PERIOD,
2793 expire_bw_meter_process, NULL);
2797 * End of bandwidth monitoring code
2802 * Send the packet up to the user daemon, or eventually do kernel encapsulation
2806 pim_register_send(struct ip *ip, struct vif *vifp,
2807 struct mbuf *m, struct mfc *rt)
2809 struct mbuf *mb_copy, *mm;
2811 if (mrtdebug & DEBUG_PIM)
2812 log(LOG_DEBUG, "pim_register_send: ");
2814 mb_copy = pim_register_prepare(ip, m);
2815 if (mb_copy == NULL)
2819 * Send all the fragments. Note that the mbuf for each fragment
2820 * is freed by the sending machinery.
2822 for (mm = mb_copy; mm; mm = mb_copy) {
2823 mb_copy = mm->m_nextpkt;
2825 mm = m_pullup(mm, sizeof(struct ip));
2827 ip = mtod(mm, struct ip *);
2828 if ((mrt_api_config & MRT_MFC_RP) &&
2829 (rt->mfc_rp.s_addr != INADDR_ANY)) {
2830 pim_register_send_rp(ip, vifp, mm, rt);
2832 pim_register_send_upcall(ip, vifp, mm, rt);
2841 * Return a copy of the data packet that is ready for PIM Register
2843 * XXX: Note that in the returned copy the IP header is a valid one.
2845 static struct mbuf *
2846 pim_register_prepare(struct ip *ip, struct mbuf *m)
2848 struct mbuf *mb_copy = NULL;
2851 /* Take care of delayed checksums */
2852 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
2853 in_delayed_cksum(m);
2854 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
2858 * Copy the old packet & pullup its IP header into the
2859 * new mbuf so we can modify it.
2861 mb_copy = m_copypacket(m, MB_DONTWAIT);
2862 if (mb_copy == NULL)
2864 mb_copy = m_pullup(mb_copy, ip->ip_hl << 2);
2865 if (mb_copy == NULL)
2868 /* take care of the TTL */
2869 ip = mtod(mb_copy, struct ip *);
2872 /* Compute the MTU after the PIM Register encapsulation */
2873 mtu = 0xffff - sizeof(pim_encap_iphdr) - sizeof(pim_encap_pimhdr);
2875 if (ip->ip_len <= mtu) {
2876 /* Turn the IP header into a valid one */
2877 ip->ip_len = htons(ip->ip_len);
2878 ip->ip_off = htons(ip->ip_off);
2880 ip->ip_sum = in_cksum(mb_copy, ip->ip_hl << 2);
2882 /* Fragment the packet */
2883 if (ip_fragment(ip, &mb_copy, mtu, 0, CSUM_DELAY_IP) != 0) {
2892 * Send an upcall with the data packet to the user-level process.
2895 pim_register_send_upcall(struct ip *ip, struct vif *vifp,
2896 struct mbuf *mb_copy, struct mfc *rt)
2898 struct mbuf *mb_first;
2899 int len = ntohs(ip->ip_len);
2901 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
2904 * Add a new mbuf with an upcall header
2906 MGETHDR(mb_first, MB_DONTWAIT, MT_HEADER);
2907 if (mb_first == NULL) {
2911 mb_first->m_data += max_linkhdr;
2912 mb_first->m_pkthdr.len = len + sizeof(struct igmpmsg);
2913 mb_first->m_len = sizeof(struct igmpmsg);
2914 mb_first->m_next = mb_copy;
2916 /* Send message to routing daemon */
2917 im = mtod(mb_first, struct igmpmsg *);
2918 im->im_msgtype = IGMPMSG_WHOLEPKT;
2920 im->im_vif = vifp - viftable;
2921 im->im_src = ip->ip_src;
2922 im->im_dst = ip->ip_dst;
2924 k_igmpsrc.sin_addr = ip->ip_src;
2926 mrtstat.mrts_upcalls++;
2928 if (socket_send(ip_mrouter, mb_first, &k_igmpsrc) < 0) {
2929 if (mrtdebug & DEBUG_PIM)
2931 "mcast: pim_register_send_upcall: ip_mrouter socket queue full");
2932 ++mrtstat.mrts_upq_sockfull;
2936 /* Keep statistics */
2937 pimstat.pims_snd_registers_msgs++;
2938 pimstat.pims_snd_registers_bytes += len;
2944 * Encapsulate the data packet in PIM Register message and send it to the RP.
2947 pim_register_send_rp(struct ip *ip, struct vif *vifp,
2948 struct mbuf *mb_copy, struct mfc *rt)
2950 struct mbuf *mb_first;
2951 struct ip *ip_outer;
2952 struct pim_encap_pimhdr *pimhdr;
2953 int len = ntohs(ip->ip_len);
2954 vifi_t vifi = rt->mfc_parent;
2956 if ((vifi >= numvifs) || (viftable[vifi].v_lcl_addr.s_addr == 0)) {
2958 return EADDRNOTAVAIL; /* The iif vif is invalid */
2962 * Add a new mbuf with the encapsulating header
2964 MGETHDR(mb_first, MB_DONTWAIT, MT_HEADER);
2965 if (mb_first == NULL) {
2969 mb_first->m_data += max_linkhdr;
2970 mb_first->m_len = sizeof(pim_encap_iphdr) + sizeof(pim_encap_pimhdr);
2971 mb_first->m_next = mb_copy;
2973 mb_first->m_pkthdr.len = len + mb_first->m_len;
2976 * Fill in the encapsulating IP and PIM header
2978 ip_outer = mtod(mb_first, struct ip *);
2979 *ip_outer = pim_encap_iphdr;
2981 ip_outer->ip_id = ip_randomid();
2983 ip_outer->ip_id = htons(ip_id++);
2985 ip_outer->ip_len = len + sizeof(pim_encap_iphdr) + sizeof(pim_encap_pimhdr);
2986 ip_outer->ip_src = viftable[vifi].v_lcl_addr;
2987 ip_outer->ip_dst = rt->mfc_rp;
2989 * Copy the inner header TOS to the outer header, and take care of the
2992 ip_outer->ip_tos = ip->ip_tos;
2993 if (ntohs(ip->ip_off) & IP_DF)
2994 ip_outer->ip_off |= IP_DF;
2995 pimhdr = (struct pim_encap_pimhdr *)((caddr_t)ip_outer
2996 + sizeof(pim_encap_iphdr));
2997 *pimhdr = pim_encap_pimhdr;
2998 /* If the iif crosses a border, set the Border-bit */
2999 if (rt->mfc_flags[vifi] & MRT_MFC_FLAGS_BORDER_VIF & mrt_api_config)
3000 pimhdr->flags |= htonl(PIM_BORDER_REGISTER);
3002 mb_first->m_data += sizeof(pim_encap_iphdr);
3003 pimhdr->pim.pim_cksum = in_cksum(mb_first, sizeof(pim_encap_pimhdr));
3004 mb_first->m_data -= sizeof(pim_encap_iphdr);
3006 if (vifp->v_rate_limit == 0)
3007 tbf_send_packet(vifp, mb_first);
3009 tbf_control(vifp, mb_first, ip, ip_outer->ip_len);
3011 /* Keep statistics */
3012 pimstat.pims_snd_registers_msgs++;
3013 pimstat.pims_snd_registers_bytes += len;
3019 * PIM-SMv2 and PIM-DM messages processing.
3020 * Receives and verifies the PIM control messages, and passes them
3021 * up to the listening socket, using rip_input().
3022 * The only message with special processing is the PIM_REGISTER message
3023 * (used by PIM-SM): the PIM header is stripped off, and the inner packet
3024 * is passed to if_simloop().
3027 pim_input(struct mbuf *m, ...)
3030 struct ip *ip = mtod(m, struct ip *);
3033 int datalen = ip->ip_len;
3039 off = __va_arg(ap, int);
3040 proto = __va_arg(ap, int);
3045 /* Keep statistics */
3046 pimstat.pims_rcv_total_msgs++;
3047 pimstat.pims_rcv_total_bytes += datalen;
3052 if (datalen < PIM_MINLEN) {
3053 pimstat.pims_rcv_tooshort++;
3054 log(LOG_ERR, "pim_input: packet size too small %d from %lx\n",
3055 datalen, (u_long)ip->ip_src.s_addr);
3061 * If the packet is at least as big as a REGISTER, go agead
3062 * and grab the PIM REGISTER header size, to avoid another
3063 * possible m_pullup() later.
3065 * PIM_MINLEN == pimhdr + u_int32_t == 4 + 4 = 8
3066 * PIM_REG_MINLEN == pimhdr + reghdr + encap_iphdr == 4 + 4 + 20 = 28
3068 minlen = iphlen + (datalen >= PIM_REG_MINLEN ? PIM_REG_MINLEN : PIM_MINLEN);
3070 * Get the IP and PIM headers in contiguous memory, and
3071 * possibly the PIM REGISTER header.
3073 if ((m->m_flags & M_EXT || m->m_len < minlen) &&
3074 (m = m_pullup(m, minlen)) == 0) {
3075 log(LOG_ERR, "pim_input: m_pullup failure\n");
3078 /* m_pullup() may have given us a new mbuf so reset ip. */
3079 ip = mtod(m, struct ip *);
3080 ip_tos = ip->ip_tos;
3082 /* adjust mbuf to point to the PIM header */
3083 m->m_data += iphlen;
3085 pim = mtod(m, struct pim *);
3088 * Validate checksum. If PIM REGISTER, exclude the data packet.
3090 * XXX: some older PIMv2 implementations don't make this distinction,
3091 * so for compatibility reason perform the checksum over part of the
3092 * message, and if error, then over the whole message.
3094 if (PIM_VT_T(pim->pim_vt) == PIM_REGISTER && in_cksum(m, PIM_MINLEN) == 0) {
3095 /* do nothing, checksum okay */
3096 } else if (in_cksum(m, datalen)) {
3097 pimstat.pims_rcv_badsum++;
3098 if (mrtdebug & DEBUG_PIM)
3099 log(LOG_DEBUG, "pim_input: invalid checksum");
3104 /* PIM version check */
3105 if (PIM_VT_V(pim->pim_vt) < PIM_VERSION) {
3106 pimstat.pims_rcv_badversion++;
3107 log(LOG_ERR, "pim_input: incorrect version %d, expecting %d\n",
3108 PIM_VT_V(pim->pim_vt), PIM_VERSION);
3113 /* restore mbuf back to the outer IP */
3114 m->m_data -= iphlen;
3117 if (PIM_VT_T(pim->pim_vt) == PIM_REGISTER) {
3119 * Since this is a REGISTER, we'll make a copy of the register
3120 * headers ip + pim + u_int32 + encap_ip, to be passed up to the
3123 struct sockaddr_in dst = { sizeof(dst), AF_INET };
3125 struct ip *encap_ip;
3128 if ((reg_vif_num >= numvifs) || (reg_vif_num == VIFI_INVALID)) {
3129 if (mrtdebug & DEBUG_PIM)
3131 "pim_input: register vif not set: %d\n", reg_vif_num);
3139 if (datalen < PIM_REG_MINLEN) {
3140 pimstat.pims_rcv_tooshort++;
3141 pimstat.pims_rcv_badregisters++;
3143 "pim_input: register packet size too small %d from %lx\n",
3144 datalen, (u_long)ip->ip_src.s_addr);
3149 reghdr = (u_int32_t *)(pim + 1);
3150 encap_ip = (struct ip *)(reghdr + 1);
3152 if (mrtdebug & DEBUG_PIM) {
3154 "pim_input[register], encap_ip: %lx -> %lx, encap_ip len %d\n",
3155 (u_long)ntohl(encap_ip->ip_src.s_addr),
3156 (u_long)ntohl(encap_ip->ip_dst.s_addr),
3157 ntohs(encap_ip->ip_len));
3160 /* verify the version number of the inner packet */
3161 if (encap_ip->ip_v != IPVERSION) {
3162 pimstat.pims_rcv_badregisters++;
3163 if (mrtdebug & DEBUG_PIM) {
3164 log(LOG_DEBUG, "pim_input: invalid IP version (%d) "
3165 "of the inner packet\n", encap_ip->ip_v);
3171 /* verify the inner packet is destined to a mcast group */
3172 if (!IN_MULTICAST(ntohl(encap_ip->ip_dst.s_addr))) {
3173 pimstat.pims_rcv_badregisters++;
3174 if (mrtdebug & DEBUG_PIM)
3176 "pim_input: inner packet of register is not "
3178 (u_long)ntohl(encap_ip->ip_dst.s_addr));
3183 /* If a NULL_REGISTER, pass it to the daemon */
3184 if ((ntohl(*reghdr) & PIM_NULL_REGISTER))
3185 goto pim_input_to_daemon;
3188 * Copy the TOS from the outer IP header to the inner IP header.
3190 if (encap_ip->ip_tos != ip_tos) {
3191 /* Outer TOS -> inner TOS */
3192 encap_ip->ip_tos = ip_tos;
3193 /* Recompute the inner header checksum. Sigh... */
3195 /* adjust mbuf to point to the inner IP header */
3196 m->m_data += (iphlen + PIM_MINLEN);
3197 m->m_len -= (iphlen + PIM_MINLEN);
3199 encap_ip->ip_sum = 0;
3200 encap_ip->ip_sum = in_cksum(m, encap_ip->ip_hl << 2);
3202 /* restore mbuf to point back to the outer IP header */
3203 m->m_data -= (iphlen + PIM_MINLEN);
3204 m->m_len += (iphlen + PIM_MINLEN);
3208 * Decapsulate the inner IP packet and loopback to forward it
3209 * as a normal multicast packet. Also, make a copy of the
3210 * outer_iphdr + pimhdr + reghdr + encap_iphdr
3211 * to pass to the daemon later, so it can take the appropriate
3212 * actions (e.g., send back PIM_REGISTER_STOP).
3213 * XXX: here m->m_data points to the outer IP header.
3215 mcp = m_copy(m, 0, iphlen + PIM_REG_MINLEN);
3218 "pim_input: pim register: could not copy register head\n");
3223 /* Keep statistics */
3224 /* XXX: registers_bytes include only the encap. mcast pkt */
3225 pimstat.pims_rcv_registers_msgs++;
3226 pimstat.pims_rcv_registers_bytes += ntohs(encap_ip->ip_len);
3229 * forward the inner ip packet; point m_data at the inner ip.
3231 m_adj(m, iphlen + PIM_MINLEN);
3233 if (mrtdebug & DEBUG_PIM) {
3235 "pim_input: forwarding decapsulated register: "
3236 "src %lx, dst %lx, vif %d\n",
3237 (u_long)ntohl(encap_ip->ip_src.s_addr),
3238 (u_long)ntohl(encap_ip->ip_dst.s_addr),
3241 if_simloop(viftable[reg_vif_num].v_ifp, m, dst.sin_family, 0);
3243 /* prepare the register head to send to the mrouting daemon */
3247 pim_input_to_daemon:
3249 * Pass the PIM message up to the daemon; if it is a Register message,
3250 * pass the 'head' only up to the daemon. This includes the
3251 * outer IP header, PIM header, PIM-Register header and the
3253 * XXX: the outer IP header pkt size of a Register is not adjust to
3254 * reflect the fact that the inner multicast data is truncated.
3256 rip_input(m, iphlen, proto);
3263 ip_mroute_modevent(module_t mod, int type, void *unused)
3268 /* XXX Protect against multiple loading */
3269 ip_mcast_src = X_ip_mcast_src;
3270 ip_mforward = X_ip_mforward;
3271 ip_mrouter_done = X_ip_mrouter_done;
3272 ip_mrouter_get = X_ip_mrouter_get;
3273 ip_mrouter_set = X_ip_mrouter_set;
3274 ip_rsvp_force_done = X_ip_rsvp_force_done;
3275 ip_rsvp_vif = X_ip_rsvp_vif;
3276 ipip_input = X_ipip_input;
3277 legal_vif_num = X_legal_vif_num;
3278 mrt_ioctl = X_mrt_ioctl;
3279 rsvp_input_p = X_rsvp_input;
3288 ip_mcast_src = NULL;
3290 ip_mrouter_done = NULL;
3291 ip_mrouter_get = NULL;
3292 ip_mrouter_set = NULL;
3293 ip_rsvp_force_done = NULL;
3296 legal_vif_num = NULL;
3298 rsvp_input_p = NULL;
3305 static moduledata_t ip_mroutemod = {
3310 DECLARE_MODULE(ip_mroute, ip_mroutemod, SI_SUB_PSEUDO, SI_ORDER_ANY);