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
24 #include "opt_mrouting.h"
25 #include "opt_random_ip_id.h"
31 #include <sys/param.h>
32 #include <sys/kernel.h>
35 #include <sys/malloc.h>
37 #include <sys/protosw.h>
38 #include <sys/signalvar.h>
39 #include <sys/socket.h>
40 #include <sys/socketvar.h>
41 #include <sys/sockio.h>
43 #include <sys/sysctl.h>
44 #include <sys/syslog.h>
45 #include <sys/systm.h>
48 #include <net/netisr.h>
49 #include <net/route.h>
50 #include <netinet/in.h>
51 #include <netinet/igmp.h>
52 #include <netinet/in_systm.h>
53 #include <netinet/in_var.h>
54 #include <netinet/ip.h>
55 #include <netinet/ip_encap.h>
56 #include <netinet/ip_mroute.h>
57 #include <netinet/ip_var.h>
59 #include <netinet/pim.h>
60 #include <netinet/pim_var.h>
62 #include <netinet/udp.h>
63 #include <machine/in_cksum.h>
66 * Control debugging code for rsvp and multicast routing code.
67 * Can only set them with the debugger.
69 static u_int rsvpdebug; /* non-zero enables debugging */
71 static u_int mrtdebug; /* any set of the flags below */
72 #define DEBUG_MFC 0x02
73 #define DEBUG_FORWARD 0x04
74 #define DEBUG_EXPIRE 0x08
75 #define DEBUG_XMIT 0x10
76 #define DEBUG_PIM 0x20
78 #define VIFI_INVALID ((vifi_t) -1)
80 #define M_HASCL(m) ((m)->m_flags & M_EXT)
82 static MALLOC_DEFINE(M_MRTABLE, "mroutetbl", "multicast routing tables");
84 static struct mrtstat mrtstat;
85 SYSCTL_STRUCT(_net_inet_ip, OID_AUTO, mrtstat, CTLFLAG_RW,
87 "Multicast Routing Statistics (struct mrtstat, netinet/ip_mroute.h)");
89 static struct mfc *mfctable[MFCTBLSIZ];
90 SYSCTL_OPAQUE(_net_inet_ip, OID_AUTO, mfctable, CTLFLAG_RD,
91 &mfctable, sizeof(mfctable), "S,*mfc[MFCTBLSIZ]",
92 "Multicast Forwarding Table (struct *mfc[MFCTBLSIZ], netinet/ip_mroute.h)");
94 static struct vif viftable[MAXVIFS];
95 SYSCTL_OPAQUE(_net_inet_ip, OID_AUTO, viftable, CTLFLAG_RD,
96 &viftable, sizeof(viftable), "S,vif[MAXVIFS]",
97 "Multicast Virtual Interfaces (struct vif[MAXVIFS], netinet/ip_mroute.h)");
99 static u_char nexpire[MFCTBLSIZ];
101 static struct callout_handle expire_upcalls_ch;
103 #define EXPIRE_TIMEOUT (hz / 4) /* 4x / second */
104 #define UPCALL_EXPIRE 6 /* number of timeouts */
107 * Define the token bucket filter structures
108 * tbftable -> each vif has one of these for storing info
111 static struct tbf tbftable[MAXVIFS];
112 #define TBF_REPROCESS (hz / 100) /* 100x / second */
115 * 'Interfaces' associated with decapsulator (so we can tell
116 * packets that went through it from ones that get reflected
117 * by a broken gateway). These interfaces are never linked into
118 * the system ifnet list & no routes point to them. I.e., packets
119 * can't be sent this way. They only exist as a placeholder for
120 * multicast source verification.
122 static struct ifnet multicast_decap_if[MAXVIFS];
125 #define ENCAP_PROTO IPPROTO_IPIP /* 4 */
127 /* prototype IP hdr for encapsulated packets */
128 static struct ip multicast_encap_iphdr = {
129 #if BYTE_ORDER == LITTLE_ENDIAN
130 sizeof(struct ip) >> 2, IPVERSION,
132 IPVERSION, sizeof(struct ip) >> 2,
135 sizeof(struct ip), /* total length */
138 ENCAP_TTL, ENCAP_PROTO,
143 * Bandwidth meter variables and constants
145 static MALLOC_DEFINE(M_BWMETER, "bwmeter", "multicast upcall bw meters");
147 * Pending timeouts are stored in a hash table, the key being the
148 * expiration time. Periodically, the entries are analysed and processed.
150 #define BW_METER_BUCKETS 1024
151 static struct bw_meter *bw_meter_timers[BW_METER_BUCKETS];
152 static struct callout_handle bw_meter_ch;
153 #define BW_METER_PERIOD (hz) /* periodical handling of bw meters */
156 * Pending upcalls are stored in a vector which is flushed when
157 * full, or periodically
159 static struct bw_upcall bw_upcalls[BW_UPCALLS_MAX];
160 static u_int bw_upcalls_n; /* # of pending upcalls */
161 static struct callout_handle bw_upcalls_ch;
162 #define BW_UPCALLS_PERIOD (hz) /* periodical flush of bw upcalls */
165 static struct pimstat pimstat;
166 SYSCTL_STRUCT(_net_inet_pim, PIMCTL_STATS, stats, CTLFLAG_RD,
168 "PIM Statistics (struct pimstat, netinet/pim_var.h)");
171 * Note: the PIM Register encapsulation adds the following in front of a
174 * struct pim_encap_hdr {
176 * struct pim_encap_pimhdr pim;
181 struct pim_encap_pimhdr {
186 static struct ip pim_encap_iphdr = {
187 #if BYTE_ORDER == LITTLE_ENDIAN
188 sizeof(struct ip) >> 2,
192 sizeof(struct ip) >> 2,
195 sizeof(struct ip), /* total length */
203 static struct pim_encap_pimhdr pim_encap_pimhdr = {
205 PIM_MAKE_VT(PIM_VERSION, PIM_REGISTER), /* PIM vers and message type */
212 static struct ifnet multicast_register_if;
213 static vifi_t reg_vif_num = VIFI_INVALID;
219 static vifi_t numvifs;
220 static const struct encaptab *encap_cookie;
223 * one-back cache used by mroute_encapcheck to locate a tunnel's vif
224 * given a datagram's src ip address.
226 static u_long last_encap_src;
227 static struct vif *last_encap_vif;
229 static u_long X_ip_mcast_src(int vifi);
230 static int X_ip_mforward(struct ip *ip, struct ifnet *ifp,
231 struct mbuf *m, struct ip_moptions *imo);
232 static int X_ip_mrouter_done(void);
233 static int X_ip_mrouter_get(struct socket *so, struct sockopt *m);
234 static int X_ip_mrouter_set(struct socket *so, struct sockopt *m);
235 static int X_legal_vif_num(int vif);
236 static int X_mrt_ioctl(int cmd, caddr_t data);
238 static int get_sg_cnt(struct sioc_sg_req *);
239 static int get_vif_cnt(struct sioc_vif_req *);
240 static int ip_mrouter_init(struct socket *, int);
241 static int add_vif(struct vifctl *);
242 static int del_vif(vifi_t);
243 static int add_mfc(struct mfcctl2 *);
244 static int del_mfc(struct mfcctl2 *);
245 static int set_api_config(uint32_t *); /* chose API capabilities */
246 static int socket_send(struct socket *, struct mbuf *, struct sockaddr_in *);
247 static int set_assert(int);
248 static void expire_upcalls(void *);
249 static int ip_mdq(struct mbuf *, struct ifnet *, struct mfc *, vifi_t);
250 static void phyint_send(struct ip *, struct vif *, struct mbuf *);
251 static void encap_send(struct ip *, struct vif *, struct mbuf *);
252 static void tbf_control(struct vif *, struct mbuf *, struct ip *, u_long);
253 static void tbf_queue(struct vif *, struct mbuf *);
254 static void tbf_process_q(struct vif *);
255 static void tbf_reprocess_q(void *);
256 static int tbf_dq_sel(struct vif *, struct ip *);
257 static void tbf_send_packet(struct vif *, struct mbuf *);
258 static void tbf_update_tokens(struct vif *);
259 static int priority(struct vif *, struct ip *);
262 * Bandwidth monitoring
264 static void free_bw_list(struct bw_meter *list);
265 static int add_bw_upcall(struct bw_upcall *);
266 static int del_bw_upcall(struct bw_upcall *);
267 static void bw_meter_receive_packet(struct bw_meter *x, int plen,
268 struct timeval *nowp);
269 static void bw_meter_prepare_upcall(struct bw_meter *x, struct timeval *nowp);
270 static void bw_upcalls_send(void);
271 static void schedule_bw_meter(struct bw_meter *x, struct timeval *nowp);
272 static void unschedule_bw_meter(struct bw_meter *x);
273 static void bw_meter_process(void);
274 static void expire_bw_upcalls_send(void *);
275 static void expire_bw_meter_process(void *);
278 static int pim_register_send(struct ip *, struct vif *,
279 struct mbuf *, struct mfc *);
280 static int pim_register_send_rp(struct ip *, struct vif *,
281 struct mbuf *, struct mfc *);
282 static int pim_register_send_upcall(struct ip *, struct vif *,
283 struct mbuf *, struct mfc *);
284 static struct mbuf *pim_register_prepare(struct ip *, struct mbuf *);
288 * whether or not special PIM assert processing is enabled.
290 static int pim_assert;
292 * Rate limit for assert notification messages, in usec
294 #define ASSERT_MSG_TIME 3000000
297 * Kernel multicast routing API capabilities and setup.
298 * If more API capabilities are added to the kernel, they should be
299 * recorded in `mrt_api_support'.
301 static const uint32_t mrt_api_support = (MRT_MFC_FLAGS_DISABLE_WRONGVIF |
302 MRT_MFC_FLAGS_BORDER_VIF |
305 static uint32_t mrt_api_config = 0;
308 * Hash function for a source, group entry
310 #define MFCHASH(a, g) MFCHASHMOD(((a) >> 20) ^ ((a) >> 10) ^ (a) ^ \
311 ((g) >> 20) ^ ((g) >> 10) ^ (g))
314 * Find a route for a given origin IP address and Multicast group address
315 * Type of service parameter to be added in the future!!!
316 * Statistics are updated by the caller if needed
317 * (mrtstat.mrts_mfc_lookups and mrtstat.mrts_mfc_misses)
320 mfc_find(in_addr_t o, in_addr_t g)
324 for (rt = mfctable[MFCHASH(o,g)]; rt; rt = rt->mfc_next)
325 if ((rt->mfc_origin.s_addr == o) &&
326 (rt->mfc_mcastgrp.s_addr == g) && (rt->mfc_stall == NULL))
332 * Macros to compute elapsed time efficiently
333 * Borrowed from Van Jacobson's scheduling code
335 #define TV_DELTA(a, b, delta) { \
337 delta = (a).tv_usec - (b).tv_usec; \
338 if ((xxs = (a).tv_sec - (b).tv_sec)) { \
347 delta += (1000000 * xxs); \
352 #define TV_LT(a, b) (((a).tv_usec < (b).tv_usec && \
353 (a).tv_sec <= (b).tv_sec) || (a).tv_sec < (b).tv_sec)
356 * Handle MRT setsockopt commands to modify the multicast routing tables.
359 X_ip_mrouter_set(struct socket *so, struct sockopt *sopt)
365 struct bw_upcall bw_upcall;
368 if (so != ip_mrouter && sopt->sopt_name != MRT_INIT)
372 switch (sopt->sopt_name) {
374 error = sooptcopyin(sopt, &optval, sizeof optval, sizeof optval);
377 error = ip_mrouter_init(so, optval);
381 error = ip_mrouter_done();
385 error = sooptcopyin(sopt, &vifc, sizeof vifc, sizeof vifc);
388 error = add_vif(&vifc);
392 error = sooptcopyin(sopt, &vifi, sizeof vifi, sizeof vifi);
395 error = del_vif(vifi);
401 * select data size depending on API version.
403 if (sopt->sopt_name == MRT_ADD_MFC &&
404 mrt_api_config & MRT_API_FLAGS_ALL) {
405 error = sooptcopyin(sopt, &mfc, sizeof(struct mfcctl2),
406 sizeof(struct mfcctl2));
408 error = sooptcopyin(sopt, &mfc, sizeof(struct mfcctl),
409 sizeof(struct mfcctl));
410 bzero((caddr_t)&mfc + sizeof(struct mfcctl),
411 sizeof(mfc) - sizeof(struct mfcctl));
415 if (sopt->sopt_name == MRT_ADD_MFC)
416 error = add_mfc(&mfc);
418 error = del_mfc(&mfc);
422 error = sooptcopyin(sopt, &optval, sizeof optval, sizeof optval);
429 error = sooptcopyin(sopt, &i, sizeof i, sizeof i);
431 error = set_api_config(&i);
433 error = sooptcopyout(sopt, &i, sizeof i);
436 case MRT_ADD_BW_UPCALL:
437 case MRT_DEL_BW_UPCALL:
438 error = sooptcopyin(sopt, &bw_upcall, sizeof bw_upcall,
442 if (sopt->sopt_name == MRT_ADD_BW_UPCALL)
443 error = add_bw_upcall(&bw_upcall);
445 error = del_bw_upcall(&bw_upcall);
456 * Handle MRT getsockopt commands
459 X_ip_mrouter_get(struct socket *so, struct sockopt *sopt)
462 static int version = 0x0305; /* !!! why is this here? XXX */
464 switch (sopt->sopt_name) {
466 error = sooptcopyout(sopt, &version, sizeof version);
470 error = sooptcopyout(sopt, &pim_assert, sizeof pim_assert);
473 case MRT_API_SUPPORT:
474 error = sooptcopyout(sopt, &mrt_api_support, sizeof mrt_api_support);
478 error = sooptcopyout(sopt, &mrt_api_config, sizeof mrt_api_config);
489 * Handle ioctl commands to obtain information from the cache
492 X_mrt_ioctl(int cmd, caddr_t data)
497 case (SIOCGETVIFCNT):
498 error = get_vif_cnt((struct sioc_vif_req *)data);
502 error = get_sg_cnt((struct sioc_sg_req *)data);
513 * returns the packet, byte, rpf-failure count for the source group provided
516 get_sg_cnt(struct sioc_sg_req *req)
522 rt = mfc_find(req->src.s_addr, req->grp.s_addr);
525 req->pktcnt = req->bytecnt = req->wrong_if = 0xffffffff;
526 return EADDRNOTAVAIL;
528 req->pktcnt = rt->mfc_pkt_cnt;
529 req->bytecnt = rt->mfc_byte_cnt;
530 req->wrong_if = rt->mfc_wrong_if;
535 * returns the input and output packet and byte counts on the vif provided
538 get_vif_cnt(struct sioc_vif_req *req)
540 vifi_t vifi = req->vifi;
545 req->icount = viftable[vifi].v_pkt_in;
546 req->ocount = viftable[vifi].v_pkt_out;
547 req->ibytes = viftable[vifi].v_bytes_in;
548 req->obytes = viftable[vifi].v_bytes_out;
554 * Enable multicast routing
557 ip_mrouter_init(struct socket *so, int version)
560 log(LOG_DEBUG, "ip_mrouter_init: so_type = %d, pr_protocol = %d\n",
561 so->so_type, so->so_proto->pr_protocol);
563 if (so->so_type != SOCK_RAW || so->so_proto->pr_protocol != IPPROTO_IGMP)
569 if (ip_mrouter != NULL)
574 bzero((caddr_t)mfctable, sizeof(mfctable));
575 bzero((caddr_t)nexpire, sizeof(nexpire));
579 expire_upcalls_ch = timeout(expire_upcalls, NULL, EXPIRE_TIMEOUT);
582 bzero((caddr_t)bw_meter_timers, sizeof(bw_meter_timers));
583 bw_upcalls_ch = timeout(expire_bw_upcalls_send, NULL, BW_UPCALLS_PERIOD);
584 bw_meter_ch = timeout(expire_bw_meter_process, NULL, BW_METER_PERIOD);
589 log(LOG_DEBUG, "ip_mrouter_init\n");
595 * Disable multicast routing
598 X_ip_mrouter_done(void)
611 * For each phyint in use, disable promiscuous reception of all IP
614 for (vifi = 0; vifi < numvifs; vifi++) {
615 if (viftable[vifi].v_lcl_addr.s_addr != 0 &&
616 !(viftable[vifi].v_flags & (VIFF_TUNNEL | VIFF_REGISTER))) {
617 struct sockaddr_in *so = (struct sockaddr_in *)&(ifr.ifr_addr);
619 so->sin_len = sizeof(struct sockaddr_in);
620 so->sin_family = AF_INET;
621 so->sin_addr.s_addr = INADDR_ANY;
622 ifp = viftable[vifi].v_ifp;
626 bzero((caddr_t)tbftable, sizeof(tbftable));
627 bzero((caddr_t)viftable, sizeof(viftable));
631 untimeout(expire_upcalls, NULL, expire_upcalls_ch);
635 untimeout(expire_bw_upcalls_send, NULL, bw_upcalls_ch);
636 untimeout(expire_bw_meter_process, NULL, bw_meter_ch);
639 * Free all multicast forwarding cache entries.
641 for (i = 0; i < MFCTBLSIZ; i++) {
642 for (rt = mfctable[i]; rt != NULL; ) {
643 struct mfc *nr = rt->mfc_next;
645 for (rte = rt->mfc_stall; rte != NULL; ) {
646 struct rtdetq *n = rte->next;
649 free(rte, M_MRTABLE);
652 free_bw_list(rt->mfc_bw_meter);
658 bzero((caddr_t)mfctable, sizeof(mfctable));
660 bzero(bw_meter_timers, sizeof(bw_meter_timers));
663 * Reset de-encapsulation cache
665 last_encap_src = INADDR_ANY;
666 last_encap_vif = NULL;
668 reg_vif_num = VIFI_INVALID;
672 encap_detach(encap_cookie);
681 log(LOG_DEBUG, "ip_mrouter_done\n");
687 * Set PIM assert processing global
692 if ((i != 1) && (i != 0))
701 * Configure API capabilities
704 set_api_config(uint32_t *apival)
709 * We can set the API capabilities only if it is the first operation
710 * after MRT_INIT. I.e.:
711 * - there are no vifs installed
712 * - pim_assert is not enabled
713 * - the MFC table is empty
723 for (i = 0; i < MFCTBLSIZ; i++) {
724 if (mfctable[i] != NULL) {
730 mrt_api_config = *apival & mrt_api_support;
731 *apival = mrt_api_config;
737 * Decide if a packet is from a tunnelled peer.
738 * Return 0 if not, 64 if so. XXX yuck.. 64 ???
741 mroute_encapcheck(const struct mbuf *m, int off, int proto, void *arg)
743 struct ip *ip = mtod(m, struct ip *);
744 int hlen = ip->ip_hl << 2;
747 * don't claim the packet if it's not to a multicast destination or if
748 * we don't have an encapsulating tunnel with the source.
749 * Note: This code assumes that the remote site IP address
750 * uniquely identifies the tunnel (i.e., that this site has
751 * at most one tunnel with the remote site).
753 if (!IN_MULTICAST(ntohl(((struct ip *)((char *)ip+hlen))->ip_dst.s_addr)))
755 if (ip->ip_src.s_addr != last_encap_src) {
756 struct vif *vifp = viftable;
757 struct vif *vife = vifp + numvifs;
759 last_encap_src = ip->ip_src.s_addr;
760 last_encap_vif = NULL;
761 for ( ; vifp < vife; ++vifp)
762 if (vifp->v_rmt_addr.s_addr == ip->ip_src.s_addr) {
763 if ((vifp->v_flags & (VIFF_TUNNEL|VIFF_SRCRT)) == VIFF_TUNNEL)
764 last_encap_vif = vifp;
768 if (last_encap_vif == NULL) {
769 last_encap_src = INADDR_ANY;
776 * De-encapsulate a packet and feed it back through ip input (this
777 * routine is called whenever IP gets a packet that mroute_encap_func()
781 mroute_encap_input(struct mbuf *m, int off)
783 struct ip *ip = mtod(m, struct ip *);
784 int hlen = ip->ip_hl << 2;
786 if (hlen > sizeof(struct ip))
787 ip_stripoptions(m, (struct mbuf *) 0);
788 m->m_data += sizeof(struct ip);
789 m->m_len -= sizeof(struct ip);
790 m->m_pkthdr.len -= sizeof(struct ip);
792 m->m_pkthdr.rcvif = last_encap_vif->v_ifp;
794 netisr_queue(NETISR_IP, m);
796 * normally we would need a "schednetisr(NETISR_IP)"
797 * here but we were called by ip_input and it is going
798 * to loop back & try to dequeue the packet we just
799 * queued as soon as we return so we avoid the
800 * unnecessary software interrrupt.
803 * This no longer holds - we may have direct-dispatched the packet,
804 * or there may be a queue processing limit.
808 extern struct domain inetdomain;
809 static struct protosw mroute_encap_protosw =
810 { SOCK_RAW, &inetdomain, IPPROTO_IPV4, PR_ATOMIC|PR_ADDR,
811 mroute_encap_input, 0, 0, rip_ctloutput,
818 * Add a vif to the vif table
821 add_vif(struct vifctl *vifcp)
823 struct vif *vifp = viftable + vifcp->vifc_vifi;
824 struct sockaddr_in sin = {sizeof sin, AF_INET};
828 struct tbf *v_tbf = tbftable + vifcp->vifc_vifi;
830 if (vifcp->vifc_vifi >= MAXVIFS)
832 if (vifp->v_lcl_addr.s_addr != INADDR_ANY)
834 if (vifcp->vifc_lcl_addr.s_addr == INADDR_ANY)
835 return EADDRNOTAVAIL;
837 /* Find the interface with an address in AF_INET family */
839 if (vifcp->vifc_flags & VIFF_REGISTER) {
841 * XXX: Because VIFF_REGISTER does not really need a valid
842 * local interface (e.g. it could be 127.0.0.2), we don't
849 sin.sin_addr = vifcp->vifc_lcl_addr;
850 ifa = ifa_ifwithaddr((struct sockaddr *)&sin);
852 return EADDRNOTAVAIL;
856 if (vifcp->vifc_flags & VIFF_TUNNEL) {
857 if ((vifcp->vifc_flags & VIFF_SRCRT) == 0) {
859 * An encapsulating tunnel is wanted. Tell
860 * mroute_encap_input() to start paying attention
861 * to encapsulated packets.
863 if (encap_cookie == NULL) {
864 encap_cookie = encap_attach_func(AF_INET, IPPROTO_IPV4,
866 (struct protosw *)&mroute_encap_protosw, NULL);
868 if (encap_cookie == NULL) {
869 printf("ip_mroute: unable to attach encap\n");
870 return EIO; /* XXX */
872 for (s = 0; s < MAXVIFS; ++s) {
873 multicast_decap_if[s].if_name = "mdecap";
874 multicast_decap_if[s].if_unit = s;
878 * Set interface to fake encapsulator interface
880 ifp = &multicast_decap_if[vifcp->vifc_vifi];
882 * Prepare cached route entry
884 bzero(&vifp->v_route, sizeof(vifp->v_route));
886 log(LOG_ERR, "source routed tunnels not supported\n");
890 } else if (vifcp->vifc_flags & VIFF_REGISTER) {
891 ifp = &multicast_register_if;
893 log(LOG_DEBUG, "Adding a register vif, ifp: %p\n",
894 (void *)&multicast_register_if);
895 if (reg_vif_num == VIFI_INVALID) {
896 multicast_register_if.if_name = "register_vif";
897 multicast_register_if.if_unit = 0;
898 multicast_register_if.if_flags = IFF_LOOPBACK;
899 bzero(&vifp->v_route, sizeof(vifp->v_route));
900 reg_vif_num = vifcp->vifc_vifi;
903 } else { /* Make sure the interface supports multicast */
904 if ((ifp->if_flags & IFF_MULTICAST) == 0)
907 /* Enable promiscuous reception of all IP multicasts from the if */
909 error = if_allmulti(ifp, 1);
916 /* define parameters for the tbf structure */
918 GET_TIME(vifp->v_tbf->tbf_last_pkt_t);
919 vifp->v_tbf->tbf_n_tok = 0;
920 vifp->v_tbf->tbf_q_len = 0;
921 vifp->v_tbf->tbf_max_q_len = MAXQSIZE;
922 vifp->v_tbf->tbf_q = vifp->v_tbf->tbf_t = NULL;
924 vifp->v_flags = vifcp->vifc_flags;
925 vifp->v_threshold = vifcp->vifc_threshold;
926 vifp->v_lcl_addr = vifcp->vifc_lcl_addr;
927 vifp->v_rmt_addr = vifcp->vifc_rmt_addr;
929 /* scaling up here allows division by 1024 in critical code */
930 vifp->v_rate_limit= vifcp->vifc_rate_limit * 1024 / 1000;
932 vifp->v_rsvpd = NULL;
933 /* initialize per vif pkt counters */
936 vifp->v_bytes_in = 0;
937 vifp->v_bytes_out = 0;
940 /* Adjust numvifs up if the vifi is higher than numvifs */
941 if (numvifs <= vifcp->vifc_vifi) numvifs = vifcp->vifc_vifi + 1;
944 log(LOG_DEBUG, "add_vif #%d, lcladdr %lx, %s %lx, thresh %x, rate %d\n",
946 (u_long)ntohl(vifcp->vifc_lcl_addr.s_addr),
947 (vifcp->vifc_flags & VIFF_TUNNEL) ? "rmtaddr" : "mask",
948 (u_long)ntohl(vifcp->vifc_rmt_addr.s_addr),
949 vifcp->vifc_threshold,
950 vifcp->vifc_rate_limit);
956 * Delete a vif from the vif table
966 vifp = &viftable[vifi];
967 if (vifp->v_lcl_addr.s_addr == INADDR_ANY)
968 return EADDRNOTAVAIL;
972 if (!(vifp->v_flags & (VIFF_TUNNEL | VIFF_REGISTER)))
973 if_allmulti(vifp->v_ifp, 0);
975 if (vifp == last_encap_vif) {
976 last_encap_vif = NULL;
977 last_encap_src = INADDR_ANY;
981 * Free packets queued at the interface
983 while (vifp->v_tbf->tbf_q) {
984 struct mbuf *m = vifp->v_tbf->tbf_q;
986 vifp->v_tbf->tbf_q = m->m_act;
991 if (vifp->v_flags & VIFF_REGISTER)
992 reg_vif_num = VIFI_INVALID;
995 bzero((caddr_t)vifp->v_tbf, sizeof(*(vifp->v_tbf)));
996 bzero((caddr_t)vifp, sizeof (*vifp));
999 log(LOG_DEBUG, "del_vif %d, numvifs %d\n", vifi, numvifs);
1001 /* Adjust numvifs down */
1002 for (vifi = numvifs; vifi > 0; vifi--)
1003 if (viftable[vifi-1].v_lcl_addr.s_addr != INADDR_ANY)
1013 * update an mfc entry without resetting counters and S,G addresses.
1016 update_mfc_params(struct mfc *rt, struct mfcctl2 *mfccp)
1020 rt->mfc_parent = mfccp->mfcc_parent;
1021 for (i = 0; i < numvifs; i++) {
1022 rt->mfc_ttls[i] = mfccp->mfcc_ttls[i];
1023 rt->mfc_flags[i] = mfccp->mfcc_flags[i] & mrt_api_config &
1026 /* set the RP address */
1027 if (mrt_api_config & MRT_MFC_RP)
1028 rt->mfc_rp = mfccp->mfcc_rp;
1030 rt->mfc_rp.s_addr = INADDR_ANY;
1034 * fully initialize an mfc entry from the parameter.
1037 init_mfc_params(struct mfc *rt, struct mfcctl2 *mfccp)
1039 rt->mfc_origin = mfccp->mfcc_origin;
1040 rt->mfc_mcastgrp = mfccp->mfcc_mcastgrp;
1042 update_mfc_params(rt, mfccp);
1044 /* initialize pkt counters per src-grp */
1045 rt->mfc_pkt_cnt = 0;
1046 rt->mfc_byte_cnt = 0;
1047 rt->mfc_wrong_if = 0;
1048 rt->mfc_last_assert.tv_sec = rt->mfc_last_assert.tv_usec = 0;
1056 add_mfc(struct mfcctl2 *mfccp)
1064 rt = mfc_find(mfccp->mfcc_origin.s_addr, mfccp->mfcc_mcastgrp.s_addr);
1066 /* If an entry already exists, just update the fields */
1068 if (mrtdebug & DEBUG_MFC)
1069 log(LOG_DEBUG,"add_mfc update o %lx g %lx p %x\n",
1070 (u_long)ntohl(mfccp->mfcc_origin.s_addr),
1071 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
1072 mfccp->mfcc_parent);
1075 update_mfc_params(rt, mfccp);
1081 * Find the entry for which the upcall was made and update
1084 hash = MFCHASH(mfccp->mfcc_origin.s_addr, mfccp->mfcc_mcastgrp.s_addr);
1085 for (rt = mfctable[hash], nstl = 0; rt; rt = rt->mfc_next) {
1087 if ((rt->mfc_origin.s_addr == mfccp->mfcc_origin.s_addr) &&
1088 (rt->mfc_mcastgrp.s_addr == mfccp->mfcc_mcastgrp.s_addr) &&
1089 (rt->mfc_stall != NULL)) {
1092 log(LOG_ERR, "add_mfc %s o %lx g %lx p %x dbx %p\n",
1093 "multiple kernel entries",
1094 (u_long)ntohl(mfccp->mfcc_origin.s_addr),
1095 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
1096 mfccp->mfcc_parent, (void *)rt->mfc_stall);
1098 if (mrtdebug & DEBUG_MFC)
1099 log(LOG_DEBUG,"add_mfc o %lx g %lx p %x dbg %p\n",
1100 (u_long)ntohl(mfccp->mfcc_origin.s_addr),
1101 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
1102 mfccp->mfcc_parent, (void *)rt->mfc_stall);
1104 init_mfc_params(rt, mfccp);
1106 rt->mfc_expire = 0; /* Don't clean this guy up */
1109 /* free packets Qed at the end of this entry */
1110 for (rte = rt->mfc_stall; rte != NULL; ) {
1111 struct rtdetq *n = rte->next;
1113 ip_mdq(rte->m, rte->ifp, rt, -1);
1115 free(rte, M_MRTABLE);
1118 rt->mfc_stall = NULL;
1123 * It is possible that an entry is being inserted without an upcall
1126 if (mrtdebug & DEBUG_MFC)
1127 log(LOG_DEBUG,"add_mfc no upcall h %lu o %lx g %lx p %x\n",
1128 hash, (u_long)ntohl(mfccp->mfcc_origin.s_addr),
1129 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
1130 mfccp->mfcc_parent);
1132 for (rt = mfctable[hash]; rt != NULL; rt = rt->mfc_next) {
1133 if ((rt->mfc_origin.s_addr == mfccp->mfcc_origin.s_addr) &&
1134 (rt->mfc_mcastgrp.s_addr == mfccp->mfcc_mcastgrp.s_addr)) {
1135 init_mfc_params(rt, mfccp);
1142 if (rt == NULL) { /* no upcall, so make a new entry */
1143 rt = (struct mfc *)malloc(sizeof(*rt), M_MRTABLE, M_NOWAIT);
1149 init_mfc_params(rt, mfccp);
1151 rt->mfc_stall = NULL;
1153 rt->mfc_bw_meter = NULL;
1154 /* insert new entry at head of hash chain */
1155 rt->mfc_next = mfctable[hash];
1156 mfctable[hash] = rt;
1164 * Delete an mfc entry
1167 del_mfc(struct mfcctl2 *mfccp)
1169 struct in_addr origin;
1170 struct in_addr mcastgrp;
1175 struct bw_meter *list;
1177 origin = mfccp->mfcc_origin;
1178 mcastgrp = mfccp->mfcc_mcastgrp;
1180 if (mrtdebug & DEBUG_MFC)
1181 log(LOG_DEBUG,"del_mfc orig %lx mcastgrp %lx\n",
1182 (u_long)ntohl(origin.s_addr), (u_long)ntohl(mcastgrp.s_addr));
1186 hash = MFCHASH(origin.s_addr, mcastgrp.s_addr);
1187 for (nptr = &mfctable[hash]; (rt = *nptr) != NULL; nptr = &rt->mfc_next)
1188 if (origin.s_addr == rt->mfc_origin.s_addr &&
1189 mcastgrp.s_addr == rt->mfc_mcastgrp.s_addr &&
1190 rt->mfc_stall == NULL)
1194 return EADDRNOTAVAIL;
1197 *nptr = rt->mfc_next;
1200 * free the bw_meter entries
1202 list = rt->mfc_bw_meter;
1203 rt->mfc_bw_meter = NULL;
1205 free(rt, M_MRTABLE);
1215 * Send a message to mrouted on the multicast routing socket
1218 socket_send(struct socket *s, struct mbuf *mm, struct sockaddr_in *src)
1221 if (sbappendaddr(&s->so_rcv, (struct sockaddr *)src, mm, NULL) != 0) {
1231 * IP multicast forwarding function. This function assumes that the packet
1232 * pointed to by "ip" has arrived on (or is about to be sent to) the interface
1233 * pointed to by "ifp", and the packet is to be relayed to other networks
1234 * that have members of the packet's destination IP multicast group.
1236 * The packet is returned unscathed to the caller, unless it is
1237 * erroneous, in which case a non-zero return value tells the caller to
1241 #define TUNNEL_LEN 12 /* # bytes of IP option for tunnel encapsulation */
1244 X_ip_mforward(struct ip *ip, struct ifnet *ifp, struct mbuf *m,
1245 struct ip_moptions *imo)
1251 if (mrtdebug & DEBUG_FORWARD)
1252 log(LOG_DEBUG, "ip_mforward: src %lx, dst %lx, ifp %p\n",
1253 (u_long)ntohl(ip->ip_src.s_addr), (u_long)ntohl(ip->ip_dst.s_addr),
1256 if (ip->ip_hl < (sizeof(struct ip) + TUNNEL_LEN) >> 2 ||
1257 ((u_char *)(ip + 1))[1] != IPOPT_LSRR ) {
1259 * Packet arrived via a physical interface or
1260 * an encapsulated tunnel or a register_vif.
1264 * Packet arrived through a source-route tunnel.
1265 * Source-route tunnels are no longer supported.
1267 static int last_log;
1268 if (last_log != time_second) {
1269 last_log = time_second;
1271 "ip_mforward: received source-routed packet from %lx\n",
1272 (u_long)ntohl(ip->ip_src.s_addr));
1277 if (imo && ((vifi = imo->imo_multicast_vif) < numvifs)) {
1278 if (ip->ip_ttl < 255)
1279 ip->ip_ttl++; /* compensate for -1 in *_send routines */
1280 if (rsvpdebug && ip->ip_p == IPPROTO_RSVP) {
1281 struct vif *vifp = viftable + vifi;
1283 printf("Sending IPPROTO_RSVP from %lx to %lx on vif %d (%s%s%d)\n",
1284 (long)ntohl(ip->ip_src.s_addr), (long)ntohl(ip->ip_dst.s_addr),
1286 (vifp->v_flags & VIFF_TUNNEL) ? "tunnel on " : "",
1287 vifp->v_ifp->if_name, vifp->v_ifp->if_unit);
1289 return ip_mdq(m, ifp, NULL, vifi);
1291 if (rsvpdebug && ip->ip_p == IPPROTO_RSVP) {
1292 printf("Warning: IPPROTO_RSVP from %lx to %lx without vif option\n",
1293 (long)ntohl(ip->ip_src.s_addr), (long)ntohl(ip->ip_dst.s_addr));
1295 printf("In fact, no options were specified at all\n");
1299 * Don't forward a packet with time-to-live of zero or one,
1300 * or a packet destined to a local-only group.
1302 if (ip->ip_ttl <= 1 || ntohl(ip->ip_dst.s_addr) <= INADDR_MAX_LOCAL_GROUP)
1306 * Determine forwarding vifs from the forwarding cache table
1309 ++mrtstat.mrts_mfc_lookups;
1310 rt = mfc_find(ip->ip_src.s_addr, ip->ip_dst.s_addr);
1312 /* Entry exists, so forward if necessary */
1315 return ip_mdq(m, ifp, rt, -1);
1318 * If we don't have a route for packet's origin,
1319 * Make a copy of the packet & send message to routing daemon
1325 int hlen = ip->ip_hl << 2;
1327 ++mrtstat.mrts_mfc_misses;
1329 mrtstat.mrts_no_route++;
1330 if (mrtdebug & (DEBUG_FORWARD | DEBUG_MFC))
1331 log(LOG_DEBUG, "ip_mforward: no rte s %lx g %lx\n",
1332 (u_long)ntohl(ip->ip_src.s_addr),
1333 (u_long)ntohl(ip->ip_dst.s_addr));
1336 * Allocate mbufs early so that we don't do extra work if we are
1337 * just going to fail anyway. Make sure to pullup the header so
1338 * that other people can't step on it.
1340 rte = (struct rtdetq *)malloc((sizeof *rte), M_MRTABLE, M_NOWAIT);
1345 mb0 = m_copypacket(m, M_DONTWAIT);
1346 if (mb0 && (M_HASCL(mb0) || mb0->m_len < hlen))
1347 mb0 = m_pullup(mb0, hlen);
1349 free(rte, M_MRTABLE);
1354 /* is there an upcall waiting for this flow ? */
1355 hash = MFCHASH(ip->ip_src.s_addr, ip->ip_dst.s_addr);
1356 for (rt = mfctable[hash]; rt; rt = rt->mfc_next) {
1357 if ((ip->ip_src.s_addr == rt->mfc_origin.s_addr) &&
1358 (ip->ip_dst.s_addr == rt->mfc_mcastgrp.s_addr) &&
1359 (rt->mfc_stall != NULL))
1366 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
1370 * Locate the vifi for the incoming interface for this packet.
1371 * If none found, drop packet.
1373 for (vifi=0; vifi < numvifs && viftable[vifi].v_ifp != ifp; vifi++)
1375 if (vifi >= numvifs) /* vif not found, drop packet */
1378 /* no upcall, so make a new entry */
1379 rt = (struct mfc *)malloc(sizeof(*rt), M_MRTABLE, M_NOWAIT);
1382 /* Make a copy of the header to send to the user level process */
1383 mm = m_copy(mb0, 0, hlen);
1388 * Send message to routing daemon to install
1389 * a route into the kernel table
1392 im = mtod(mm, struct igmpmsg *);
1393 im->im_msgtype = IGMPMSG_NOCACHE;
1397 mrtstat.mrts_upcalls++;
1399 k_igmpsrc.sin_addr = ip->ip_src;
1400 if (socket_send(ip_mrouter, mm, &k_igmpsrc) < 0) {
1401 log(LOG_WARNING, "ip_mforward: ip_mrouter socket queue full\n");
1402 ++mrtstat.mrts_upq_sockfull;
1404 free(rt, M_MRTABLE);
1406 free(rte, M_MRTABLE);
1412 /* insert new entry at head of hash chain */
1413 rt->mfc_origin.s_addr = ip->ip_src.s_addr;
1414 rt->mfc_mcastgrp.s_addr = ip->ip_dst.s_addr;
1415 rt->mfc_expire = UPCALL_EXPIRE;
1417 for (i = 0; i < numvifs; i++) {
1418 rt->mfc_ttls[i] = 0;
1419 rt->mfc_flags[i] = 0;
1421 rt->mfc_parent = -1;
1423 rt->mfc_rp.s_addr = INADDR_ANY; /* clear the RP address */
1425 rt->mfc_bw_meter = NULL;
1427 /* link into table */
1428 rt->mfc_next = mfctable[hash];
1429 mfctable[hash] = rt;
1430 rt->mfc_stall = rte;
1433 /* determine if q has overflowed */
1438 * XXX ouch! we need to append to the list, but we
1439 * only have a pointer to the front, so we have to
1440 * scan the entire list every time.
1442 for (p = &rt->mfc_stall; *p != NULL; p = &(*p)->next)
1445 if (npkts > MAX_UPQ) {
1446 mrtstat.mrts_upq_ovflw++;
1448 free(rte, M_MRTABLE);
1454 /* Add this entry to the end of the queue */
1469 * Clean up the cache entry if upcall is not serviced
1472 expire_upcalls(void *unused)
1475 struct mfc *mfc, **nptr;
1480 for (i = 0; i < MFCTBLSIZ; i++) {
1481 if (nexpire[i] == 0)
1483 nptr = &mfctable[i];
1484 for (mfc = *nptr; mfc != NULL; mfc = *nptr) {
1486 * Skip real cache entries
1487 * Make sure it wasn't marked to not expire (shouldn't happen)
1490 if (mfc->mfc_stall != NULL && mfc->mfc_expire != 0 &&
1491 --mfc->mfc_expire == 0) {
1492 if (mrtdebug & DEBUG_EXPIRE)
1493 log(LOG_DEBUG, "expire_upcalls: expiring (%lx %lx)\n",
1494 (u_long)ntohl(mfc->mfc_origin.s_addr),
1495 (u_long)ntohl(mfc->mfc_mcastgrp.s_addr));
1497 * drop all the packets
1498 * free the mbuf with the pkt, if, timing info
1500 for (rte = mfc->mfc_stall; rte; ) {
1501 struct rtdetq *n = rte->next;
1504 free(rte, M_MRTABLE);
1507 ++mrtstat.mrts_cache_cleanups;
1511 * free the bw_meter entries
1513 while (mfc->mfc_bw_meter != NULL) {
1514 struct bw_meter *x = mfc->mfc_bw_meter;
1516 mfc->mfc_bw_meter = x->bm_mfc_next;
1520 *nptr = mfc->mfc_next;
1521 free(mfc, M_MRTABLE);
1523 nptr = &mfc->mfc_next;
1528 expire_upcalls_ch = timeout(expire_upcalls, NULL, EXPIRE_TIMEOUT);
1532 * Packet forwarding routine once entry in the cache is made
1535 ip_mdq(struct mbuf *m, struct ifnet *ifp, struct mfc *rt, vifi_t xmt_vif)
1537 struct ip *ip = mtod(m, struct ip *);
1539 int plen = ip->ip_len;
1542 * Macro to send packet on vif. Since RSVP packets don't get counted on
1543 * input, they shouldn't get counted on output, so statistics keeping is
1546 #define MC_SEND(ip,vifp,m) { \
1547 if ((vifp)->v_flags & VIFF_TUNNEL) \
1548 encap_send((ip), (vifp), (m)); \
1550 phyint_send((ip), (vifp), (m)); \
1554 * If xmt_vif is not -1, send on only the requested vif.
1556 * (since vifi_t is u_short, -1 becomes MAXUSHORT, which > numvifs.)
1558 if (xmt_vif < numvifs) {
1560 if (viftable[xmt_vif].v_flags & VIFF_REGISTER)
1561 pim_register_send(ip, viftable + xmt_vif, m, rt);
1564 MC_SEND(ip, viftable + xmt_vif, m);
1569 * Don't forward if it didn't arrive from the parent vif for its origin.
1571 vifi = rt->mfc_parent;
1572 if ((vifi >= numvifs) || (viftable[vifi].v_ifp != ifp)) {
1573 /* came in the wrong interface */
1574 if (mrtdebug & DEBUG_FORWARD)
1575 log(LOG_DEBUG, "wrong if: ifp %p vifi %d vififp %p\n",
1576 (void *)ifp, vifi, (void *)viftable[vifi].v_ifp);
1577 ++mrtstat.mrts_wrong_if;
1580 * If we are doing PIM assert processing, send a message
1581 * to the routing daemon.
1583 * XXX: A PIM-SM router needs the WRONGVIF detection so it
1584 * can complete the SPT switch, regardless of the type
1585 * of the iif (broadcast media, GRE tunnel, etc).
1587 if (pim_assert && (vifi < numvifs) && viftable[vifi].v_ifp) {
1592 if (ifp == &multicast_register_if)
1593 pimstat.pims_rcv_registers_wrongiif++;
1596 /* Get vifi for the incoming packet */
1597 for (vifi=0; vifi < numvifs && viftable[vifi].v_ifp != ifp; vifi++)
1599 if (vifi >= numvifs)
1600 return 0; /* The iif is not found: ignore the packet. */
1602 if (rt->mfc_flags[vifi] & MRT_MFC_FLAGS_DISABLE_WRONGVIF)
1603 return 0; /* WRONGVIF disabled: ignore the packet */
1607 TV_DELTA(rt->mfc_last_assert, now, delta);
1609 if (delta > ASSERT_MSG_TIME) {
1610 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
1612 int hlen = ip->ip_hl << 2;
1613 struct mbuf *mm = m_copy(m, 0, hlen);
1615 if (mm && (M_HASCL(mm) || mm->m_len < hlen))
1616 mm = m_pullup(mm, hlen);
1620 rt->mfc_last_assert = now;
1622 im = mtod(mm, struct igmpmsg *);
1623 im->im_msgtype = IGMPMSG_WRONGVIF;
1627 mrtstat.mrts_upcalls++;
1629 k_igmpsrc.sin_addr = im->im_src;
1630 if (socket_send(ip_mrouter, mm, &k_igmpsrc) < 0) {
1632 "ip_mforward: ip_mrouter socket queue full\n");
1633 ++mrtstat.mrts_upq_sockfull;
1641 /* If I sourced this packet, it counts as output, else it was input. */
1642 if (ip->ip_src.s_addr == viftable[vifi].v_lcl_addr.s_addr) {
1643 viftable[vifi].v_pkt_out++;
1644 viftable[vifi].v_bytes_out += plen;
1646 viftable[vifi].v_pkt_in++;
1647 viftable[vifi].v_bytes_in += plen;
1650 rt->mfc_byte_cnt += plen;
1653 * For each vif, decide if a copy of the packet should be forwarded.
1655 * - the ttl exceeds the vif's threshold
1656 * - there are group members downstream on interface
1658 for (vifi = 0; vifi < numvifs; vifi++)
1659 if ((rt->mfc_ttls[vifi] > 0) && (ip->ip_ttl > rt->mfc_ttls[vifi])) {
1660 viftable[vifi].v_pkt_out++;
1661 viftable[vifi].v_bytes_out += plen;
1663 if (viftable[vifi].v_flags & VIFF_REGISTER)
1664 pim_register_send(ip, viftable + vifi, m, rt);
1667 MC_SEND(ip, viftable+vifi, m);
1671 * Perform upcall-related bw measuring.
1673 if (rt->mfc_bw_meter != NULL) {
1678 for (x = rt->mfc_bw_meter; x != NULL; x = x->bm_mfc_next)
1679 bw_meter_receive_packet(x, plen, &now);
1686 * check if a vif number is legal/ok. This is used by ip_output.
1689 X_legal_vif_num(int vif)
1691 return (vif >= 0 && vif < numvifs);
1695 * Return the local address used by this vif
1698 X_ip_mcast_src(int vifi)
1700 if (vifi >= 0 && vifi < numvifs)
1701 return viftable[vifi].v_lcl_addr.s_addr;
1707 phyint_send(struct ip *ip, struct vif *vifp, struct mbuf *m)
1709 struct mbuf *mb_copy;
1710 int hlen = ip->ip_hl << 2;
1713 * Make a new reference to the packet; make sure that
1714 * the IP header is actually copied, not just referenced,
1715 * so that ip_output() only scribbles on the copy.
1717 mb_copy = m_copypacket(m, M_DONTWAIT);
1718 if (mb_copy && (M_HASCL(mb_copy) || mb_copy->m_len < hlen))
1719 mb_copy = m_pullup(mb_copy, hlen);
1720 if (mb_copy == NULL)
1723 if (vifp->v_rate_limit == 0)
1724 tbf_send_packet(vifp, mb_copy);
1726 tbf_control(vifp, mb_copy, mtod(mb_copy, struct ip *), ip->ip_len);
1730 encap_send(struct ip *ip, struct vif *vifp, struct mbuf *m)
1732 struct mbuf *mb_copy;
1734 int i, len = ip->ip_len;
1736 /* Take care of delayed checksums */
1737 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
1738 in_delayed_cksum(m);
1739 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
1743 * copy the old packet & pullup its IP header into the
1744 * new mbuf so we can modify it. Try to fill the new
1745 * mbuf since if we don't the ethernet driver will.
1747 MGETHDR(mb_copy, M_DONTWAIT, MT_HEADER);
1748 if (mb_copy == NULL)
1751 mac_create_mbuf_multicast_encap(m, vifp->v_ifp, mb_copy);
1753 mb_copy->m_data += max_linkhdr;
1754 mb_copy->m_len = sizeof(multicast_encap_iphdr);
1756 if ((mb_copy->m_next = m_copypacket(m, M_DONTWAIT)) == NULL) {
1760 i = MHLEN - M_LEADINGSPACE(mb_copy);
1763 mb_copy = m_pullup(mb_copy, i);
1764 if (mb_copy == NULL)
1766 mb_copy->m_pkthdr.len = len + sizeof(multicast_encap_iphdr);
1769 * fill in the encapsulating IP header.
1771 ip_copy = mtod(mb_copy, struct ip *);
1772 *ip_copy = multicast_encap_iphdr;
1774 ip_copy->ip_id = ip_randomid();
1776 ip_copy->ip_id = htons(ip_id++);
1778 ip_copy->ip_len += len;
1779 ip_copy->ip_src = vifp->v_lcl_addr;
1780 ip_copy->ip_dst = vifp->v_rmt_addr;
1783 * turn the encapsulated IP header back into a valid one.
1785 ip = (struct ip *)((caddr_t)ip_copy + sizeof(multicast_encap_iphdr));
1787 ip->ip_len = htons(ip->ip_len);
1788 ip->ip_off = htons(ip->ip_off);
1790 mb_copy->m_data += sizeof(multicast_encap_iphdr);
1791 ip->ip_sum = in_cksum(mb_copy, ip->ip_hl << 2);
1792 mb_copy->m_data -= sizeof(multicast_encap_iphdr);
1794 if (vifp->v_rate_limit == 0)
1795 tbf_send_packet(vifp, mb_copy);
1797 tbf_control(vifp, mb_copy, ip, ip_copy->ip_len);
1801 * Token bucket filter module
1805 tbf_control(struct vif *vifp, struct mbuf *m, struct ip *ip, u_long p_len)
1807 struct tbf *t = vifp->v_tbf;
1809 if (p_len > MAX_BKT_SIZE) { /* drop if packet is too large */
1810 mrtstat.mrts_pkt2large++;
1815 tbf_update_tokens(vifp);
1817 if (t->tbf_q_len == 0) { /* queue empty... */
1818 if (p_len <= t->tbf_n_tok) { /* send packet if enough tokens */
1819 t->tbf_n_tok -= p_len;
1820 tbf_send_packet(vifp, m);
1821 } else { /* no, queue packet and try later */
1823 timeout(tbf_reprocess_q, (caddr_t)vifp, TBF_REPROCESS);
1825 } else if (t->tbf_q_len < t->tbf_max_q_len) {
1826 /* finite queue length, so queue pkts and process queue */
1828 tbf_process_q(vifp);
1830 /* queue full, try to dq and queue and process */
1831 if (!tbf_dq_sel(vifp, ip)) {
1832 mrtstat.mrts_q_overflow++;
1836 tbf_process_q(vifp);
1842 * adds a packet to the queue at the interface
1845 tbf_queue(struct vif *vifp, struct mbuf *m)
1848 struct tbf *t = vifp->v_tbf;
1850 if (t->tbf_t == NULL) /* Queue was empty */
1852 else /* Insert at tail */
1853 t->tbf_t->m_act = m;
1855 t->tbf_t = m; /* Set new tail pointer */
1858 /* Make sure we didn't get fed a bogus mbuf */
1860 panic("tbf_queue: m_act");
1870 * processes the queue at the interface
1873 tbf_process_q(struct vif *vifp)
1876 struct tbf *t = vifp->v_tbf;
1878 /* loop through the queue at the interface and send as many packets
1881 while (t->tbf_q_len > 0) {
1882 struct mbuf *m = t->tbf_q;
1883 int len = mtod(m, struct ip *)->ip_len;
1885 /* determine if the packet can be sent */
1886 if (len > t->tbf_n_tok) /* not enough tokens, we are done */
1888 /* ok, reduce no of tokens, dequeue and send the packet. */
1889 t->tbf_n_tok -= len;
1891 t->tbf_q = m->m_act;
1892 if (--t->tbf_q_len == 0)
1896 tbf_send_packet(vifp, m);
1902 tbf_reprocess_q(void *xvifp)
1904 struct vif *vifp = xvifp;
1906 if (ip_mrouter == NULL)
1908 tbf_update_tokens(vifp);
1909 tbf_process_q(vifp);
1910 if (vifp->v_tbf->tbf_q_len)
1911 timeout(tbf_reprocess_q, (caddr_t)vifp, TBF_REPROCESS);
1914 /* function that will selectively discard a member of the queue
1915 * based on the precedence value and the priority
1918 tbf_dq_sel(struct vif *vifp, struct ip *ip)
1922 struct mbuf *m, *last;
1924 struct tbf *t = vifp->v_tbf;
1926 p = priority(vifp, ip);
1930 while ((m = *np) != NULL) {
1931 if (p > priority(vifp, mtod(m, struct ip *))) {
1933 /* If we're removing the last packet, fix the tail pointer */
1937 /* It's impossible for the queue to be empty, but check anyways. */
1938 if (--t->tbf_q_len == 0)
1941 mrtstat.mrts_drop_sel++;
1952 tbf_send_packet(struct vif *vifp, struct mbuf *m)
1956 if (vifp->v_flags & VIFF_TUNNEL) /* If tunnel options */
1957 ip_output(m, NULL, &vifp->v_route, IP_FORWARDING, NULL, NULL);
1959 struct ip_moptions imo;
1961 static struct route ro; /* XXX check this */
1963 imo.imo_multicast_ifp = vifp->v_ifp;
1964 imo.imo_multicast_ttl = mtod(m, struct ip *)->ip_ttl - 1;
1965 imo.imo_multicast_loop = 1;
1966 imo.imo_multicast_vif = -1;
1969 * Re-entrancy should not be a problem here, because
1970 * the packets that we send out and are looped back at us
1971 * should get rejected because they appear to come from
1972 * the loopback interface, thus preventing looping.
1974 error = ip_output(m, NULL, &ro, IP_FORWARDING, &imo, NULL);
1976 if (mrtdebug & DEBUG_XMIT)
1977 log(LOG_DEBUG, "phyint_send on vif %d err %d\n",
1978 (int)(vifp - viftable), error);
1983 /* determine the current time and then
1984 * the elapsed time (between the last time and time now)
1985 * in milliseconds & update the no. of tokens in the bucket
1988 tbf_update_tokens(struct vif *vifp)
1993 struct tbf *t = vifp->v_tbf;
1997 TV_DELTA(tp, t->tbf_last_pkt_t, tm);
2000 * This formula is actually
2001 * "time in seconds" * "bytes/second".
2003 * (tm / 1000000) * (v_rate_limit * 1000 * (1000/1024) / 8)
2005 * The (1000/1024) was introduced in add_vif to optimize
2006 * this divide into a shift.
2008 t->tbf_n_tok += tm * vifp->v_rate_limit / 1024 / 8;
2009 t->tbf_last_pkt_t = tp;
2011 if (t->tbf_n_tok > MAX_BKT_SIZE)
2012 t->tbf_n_tok = MAX_BKT_SIZE;
2018 priority(struct vif *vifp, struct ip *ip)
2020 int prio = 50; /* the lowest priority -- default case */
2022 /* temporary hack; may add general packet classifier some day */
2025 * The UDP port space is divided up into four priority ranges:
2026 * [0, 16384) : unclassified - lowest priority
2027 * [16384, 32768) : audio - highest priority
2028 * [32768, 49152) : whiteboard - medium priority
2029 * [49152, 65536) : video - low priority
2031 * Everything else gets lowest priority.
2033 if (ip->ip_p == IPPROTO_UDP) {
2034 struct udphdr *udp = (struct udphdr *)(((char *)ip) + (ip->ip_hl << 2));
2035 switch (ntohs(udp->uh_dport) & 0xc000) {
2051 * End of token bucket filter modifications
2055 X_ip_rsvp_vif(struct socket *so, struct sockopt *sopt)
2059 if (so->so_type != SOCK_RAW || so->so_proto->pr_protocol != IPPROTO_RSVP)
2062 error = sooptcopyin(sopt, &vifi, sizeof vifi, sizeof vifi);
2068 if (vifi < 0 || vifi >= numvifs) { /* Error if vif is invalid */
2070 return EADDRNOTAVAIL;
2073 if (sopt->sopt_name == IP_RSVP_VIF_ON) {
2074 /* Check if socket is available. */
2075 if (viftable[vifi].v_rsvpd != NULL) {
2080 viftable[vifi].v_rsvpd = so;
2081 /* This may seem silly, but we need to be sure we don't over-increment
2082 * the RSVP counter, in case something slips up.
2084 if (!viftable[vifi].v_rsvp_on) {
2085 viftable[vifi].v_rsvp_on = 1;
2088 } else { /* must be VIF_OFF */
2090 * XXX as an additional consistency check, one could make sure
2091 * that viftable[vifi].v_rsvpd == so, otherwise passing so as
2092 * first parameter is pretty useless.
2094 viftable[vifi].v_rsvpd = NULL;
2096 * This may seem silly, but we need to be sure we don't over-decrement
2097 * the RSVP counter, in case something slips up.
2099 if (viftable[vifi].v_rsvp_on) {
2100 viftable[vifi].v_rsvp_on = 0;
2109 X_ip_rsvp_force_done(struct socket *so)
2114 /* Don't bother if it is not the right type of socket. */
2115 if (so->so_type != SOCK_RAW || so->so_proto->pr_protocol != IPPROTO_RSVP)
2120 /* The socket may be attached to more than one vif...this
2121 * is perfectly legal.
2123 for (vifi = 0; vifi < numvifs; vifi++) {
2124 if (viftable[vifi].v_rsvpd == so) {
2125 viftable[vifi].v_rsvpd = NULL;
2126 /* This may seem silly, but we need to be sure we don't
2127 * over-decrement the RSVP counter, in case something slips up.
2129 if (viftable[vifi].v_rsvp_on) {
2130 viftable[vifi].v_rsvp_on = 0;
2140 X_rsvp_input(struct mbuf *m, int off)
2143 struct ip *ip = mtod(m, struct ip *);
2144 struct sockaddr_in rsvp_src = { sizeof rsvp_src, AF_INET };
2149 printf("rsvp_input: rsvp_on %d\n",rsvp_on);
2151 /* Can still get packets with rsvp_on = 0 if there is a local member
2152 * of the group to which the RSVP packet is addressed. But in this
2153 * case we want to throw the packet away.
2163 printf("rsvp_input: check vifs\n");
2169 ifp = m->m_pkthdr.rcvif;
2170 /* Find which vif the packet arrived on. */
2171 for (vifi = 0; vifi < numvifs; vifi++)
2172 if (viftable[vifi].v_ifp == ifp)
2175 if (vifi == numvifs || viftable[vifi].v_rsvpd == NULL) {
2177 * If the old-style non-vif-associated socket is set,
2178 * then use it. Otherwise, drop packet since there
2179 * is no specific socket for this vif.
2181 if (ip_rsvpd != NULL) {
2183 printf("rsvp_input: Sending packet up old-style socket\n");
2184 rip_input(m, off); /* xxx */
2186 if (rsvpdebug && vifi == numvifs)
2187 printf("rsvp_input: Can't find vif for packet.\n");
2188 else if (rsvpdebug && viftable[vifi].v_rsvpd == NULL)
2189 printf("rsvp_input: No socket defined for vif %d\n",vifi);
2195 rsvp_src.sin_addr = ip->ip_src;
2198 printf("rsvp_input: m->m_len = %d, sbspace() = %ld\n",
2199 m->m_len,sbspace(&(viftable[vifi].v_rsvpd->so_rcv)));
2201 if (socket_send(viftable[vifi].v_rsvpd, m, &rsvp_src) < 0) {
2203 printf("rsvp_input: Failed to append to socket\n");
2206 printf("rsvp_input: send packet up\n");
2213 * Code for bandwidth monitors
2217 * Define common interface for timeval-related methods
2219 #define BW_TIMEVALCMP(tvp, uvp, cmp) timevalcmp((tvp), (uvp), cmp)
2220 #define BW_TIMEVALDECR(vvp, uvp) timevalsub((vvp), (uvp))
2221 #define BW_TIMEVALADD(vvp, uvp) timevaladd((vvp), (uvp))
2224 compute_bw_meter_flags(struct bw_upcall *req)
2228 if (req->bu_flags & BW_UPCALL_UNIT_PACKETS)
2229 flags |= BW_METER_UNIT_PACKETS;
2230 if (req->bu_flags & BW_UPCALL_UNIT_BYTES)
2231 flags |= BW_METER_UNIT_BYTES;
2232 if (req->bu_flags & BW_UPCALL_GEQ)
2233 flags |= BW_METER_GEQ;
2234 if (req->bu_flags & BW_UPCALL_LEQ)
2235 flags |= BW_METER_LEQ;
2241 * Add a bw_meter entry
2244 add_bw_upcall(struct bw_upcall *req)
2247 struct timeval delta = { BW_UPCALL_THRESHOLD_INTERVAL_MIN_SEC,
2248 BW_UPCALL_THRESHOLD_INTERVAL_MIN_USEC };
2254 if (!(mrt_api_config & MRT_MFC_BW_UPCALL))
2257 /* Test if the flags are valid */
2258 if (!(req->bu_flags & (BW_UPCALL_UNIT_PACKETS | BW_UPCALL_UNIT_BYTES)))
2260 if (!(req->bu_flags & (BW_UPCALL_GEQ | BW_UPCALL_LEQ)))
2262 if ((req->bu_flags & (BW_UPCALL_GEQ | BW_UPCALL_LEQ))
2263 == (BW_UPCALL_GEQ | BW_UPCALL_LEQ))
2266 /* Test if the threshold time interval is valid */
2267 if (BW_TIMEVALCMP(&req->bu_threshold.b_time, &delta, <))
2270 flags = compute_bw_meter_flags(req);
2273 * Find if we have already same bw_meter entry
2276 mfc = mfc_find(req->bu_src.s_addr, req->bu_dst.s_addr);
2279 return EADDRNOTAVAIL;
2281 for (x = mfc->mfc_bw_meter; x != NULL; x = x->bm_mfc_next) {
2282 if ((BW_TIMEVALCMP(&x->bm_threshold.b_time,
2283 &req->bu_threshold.b_time, ==)) &&
2284 (x->bm_threshold.b_packets == req->bu_threshold.b_packets) &&
2285 (x->bm_threshold.b_bytes == req->bu_threshold.b_bytes) &&
2286 (x->bm_flags & BW_METER_USER_FLAGS) == flags) {
2288 return 0; /* XXX Already installed */
2293 /* Allocate the new bw_meter entry */
2294 x = (struct bw_meter *)malloc(sizeof(*x), M_BWMETER, M_NOWAIT);
2298 /* Set the new bw_meter entry */
2299 x->bm_threshold.b_time = req->bu_threshold.b_time;
2301 x->bm_start_time = now;
2302 x->bm_threshold.b_packets = req->bu_threshold.b_packets;
2303 x->bm_threshold.b_bytes = req->bu_threshold.b_bytes;
2304 x->bm_measured.b_packets = 0;
2305 x->bm_measured.b_bytes = 0;
2306 x->bm_flags = flags;
2307 x->bm_time_next = NULL;
2308 x->bm_time_hash = BW_METER_BUCKETS;
2310 /* Add the new bw_meter entry to the front of entries for this MFC */
2313 x->bm_mfc_next = mfc->mfc_bw_meter;
2314 mfc->mfc_bw_meter = x;
2315 schedule_bw_meter(x, &now);
2322 free_bw_list(struct bw_meter *list)
2324 while (list != NULL) {
2325 struct bw_meter *x = list;
2327 list = list->bm_mfc_next;
2328 unschedule_bw_meter(x);
2334 * Delete one or multiple bw_meter entries
2337 del_bw_upcall(struct bw_upcall *req)
2343 if (!(mrt_api_config & MRT_MFC_BW_UPCALL))
2347 /* Find the corresponding MFC entry */
2348 mfc = mfc_find(req->bu_src.s_addr, req->bu_dst.s_addr);
2351 return EADDRNOTAVAIL;
2352 } else if (req->bu_flags & BW_UPCALL_DELETE_ALL) {
2354 * Delete all bw_meter entries for this mfc
2356 struct bw_meter *list;
2358 list = mfc->mfc_bw_meter;
2359 mfc->mfc_bw_meter = NULL;
2363 } else { /* Delete a single bw_meter entry */
2364 struct bw_meter *prev;
2367 flags = compute_bw_meter_flags(req);
2369 /* Find the bw_meter entry to delete */
2370 for (prev = NULL, x = mfc->mfc_bw_meter; x != NULL;
2371 x = x->bm_mfc_next) {
2372 if ((BW_TIMEVALCMP(&x->bm_threshold.b_time,
2373 &req->bu_threshold.b_time, ==)) &&
2374 (x->bm_threshold.b_packets == req->bu_threshold.b_packets) &&
2375 (x->bm_threshold.b_bytes == req->bu_threshold.b_bytes) &&
2376 (x->bm_flags & BW_METER_USER_FLAGS) == flags)
2379 if (x != NULL) { /* Delete entry from the list for this MFC */
2381 prev->bm_mfc_next = x->bm_mfc_next; /* remove from middle*/
2383 x->bm_mfc->mfc_bw_meter = x->bm_mfc_next;/* new head of list */
2386 unschedule_bw_meter(x);
2387 /* Free the bw_meter entry */
2399 * Perform bandwidth measurement processing that may result in an upcall
2402 bw_meter_receive_packet(struct bw_meter *x, int plen, struct timeval *nowp)
2404 struct timeval delta;
2409 BW_TIMEVALDECR(&delta, &x->bm_start_time);
2411 if (x->bm_flags & BW_METER_GEQ) {
2413 * Processing for ">=" type of bw_meter entry
2415 if (BW_TIMEVALCMP(&delta, &x->bm_threshold.b_time, >)) {
2416 /* Reset the bw_meter entry */
2417 x->bm_start_time = *nowp;
2418 x->bm_measured.b_packets = 0;
2419 x->bm_measured.b_bytes = 0;
2420 x->bm_flags &= ~BW_METER_UPCALL_DELIVERED;
2423 /* Record that a packet is received */
2424 x->bm_measured.b_packets++;
2425 x->bm_measured.b_bytes += plen;
2428 * Test if we should deliver an upcall
2430 if (!(x->bm_flags & BW_METER_UPCALL_DELIVERED)) {
2431 if (((x->bm_flags & BW_METER_UNIT_PACKETS) &&
2432 (x->bm_measured.b_packets >= x->bm_threshold.b_packets)) ||
2433 ((x->bm_flags & BW_METER_UNIT_BYTES) &&
2434 (x->bm_measured.b_bytes >= x->bm_threshold.b_bytes))) {
2435 /* Prepare an upcall for delivery */
2436 bw_meter_prepare_upcall(x, nowp);
2437 x->bm_flags |= BW_METER_UPCALL_DELIVERED;
2440 } else if (x->bm_flags & BW_METER_LEQ) {
2442 * Processing for "<=" type of bw_meter entry
2444 if (BW_TIMEVALCMP(&delta, &x->bm_threshold.b_time, >)) {
2446 * We are behind time with the multicast forwarding table
2447 * scanning for "<=" type of bw_meter entries, so test now
2448 * if we should deliver an upcall.
2450 if (((x->bm_flags & BW_METER_UNIT_PACKETS) &&
2451 (x->bm_measured.b_packets <= x->bm_threshold.b_packets)) ||
2452 ((x->bm_flags & BW_METER_UNIT_BYTES) &&
2453 (x->bm_measured.b_bytes <= x->bm_threshold.b_bytes))) {
2454 /* Prepare an upcall for delivery */
2455 bw_meter_prepare_upcall(x, nowp);
2457 /* Reschedule the bw_meter entry */
2458 unschedule_bw_meter(x);
2459 schedule_bw_meter(x, nowp);
2462 /* Record that a packet is received */
2463 x->bm_measured.b_packets++;
2464 x->bm_measured.b_bytes += plen;
2467 * Test if we should restart the measuring interval
2469 if ((x->bm_flags & BW_METER_UNIT_PACKETS &&
2470 x->bm_measured.b_packets <= x->bm_threshold.b_packets) ||
2471 (x->bm_flags & BW_METER_UNIT_BYTES &&
2472 x->bm_measured.b_bytes <= x->bm_threshold.b_bytes)) {
2473 /* Don't restart the measuring interval */
2475 /* Do restart the measuring interval */
2477 * XXX: note that we don't unschedule and schedule, because this
2478 * might be too much overhead per packet. Instead, when we process
2479 * all entries for a given timer hash bin, we check whether it is
2480 * really a timeout. If not, we reschedule at that time.
2482 x->bm_start_time = *nowp;
2483 x->bm_measured.b_packets = 0;
2484 x->bm_measured.b_bytes = 0;
2485 x->bm_flags &= ~BW_METER_UPCALL_DELIVERED;
2492 * Prepare a bandwidth-related upcall
2495 bw_meter_prepare_upcall(struct bw_meter *x, struct timeval *nowp)
2497 struct timeval delta;
2498 struct bw_upcall *u;
2504 * Compute the measured time interval
2507 BW_TIMEVALDECR(&delta, &x->bm_start_time);
2510 * If there are too many pending upcalls, deliver them now
2512 if (bw_upcalls_n >= BW_UPCALLS_MAX)
2516 * Set the bw_upcall entry
2518 u = &bw_upcalls[bw_upcalls_n++];
2519 u->bu_src = x->bm_mfc->mfc_origin;
2520 u->bu_dst = x->bm_mfc->mfc_mcastgrp;
2521 u->bu_threshold.b_time = x->bm_threshold.b_time;
2522 u->bu_threshold.b_packets = x->bm_threshold.b_packets;
2523 u->bu_threshold.b_bytes = x->bm_threshold.b_bytes;
2524 u->bu_measured.b_time = delta;
2525 u->bu_measured.b_packets = x->bm_measured.b_packets;
2526 u->bu_measured.b_bytes = x->bm_measured.b_bytes;
2528 if (x->bm_flags & BW_METER_UNIT_PACKETS)
2529 u->bu_flags |= BW_UPCALL_UNIT_PACKETS;
2530 if (x->bm_flags & BW_METER_UNIT_BYTES)
2531 u->bu_flags |= BW_UPCALL_UNIT_BYTES;
2532 if (x->bm_flags & BW_METER_GEQ)
2533 u->bu_flags |= BW_UPCALL_GEQ;
2534 if (x->bm_flags & BW_METER_LEQ)
2535 u->bu_flags |= BW_UPCALL_LEQ;
2541 * Send the pending bandwidth-related upcalls
2544 bw_upcalls_send(void)
2547 int len = bw_upcalls_n * sizeof(bw_upcalls[0]);
2548 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
2549 static struct igmpmsg igmpmsg = { 0, /* unused1 */
2551 IGMPMSG_BW_UPCALL,/* im_msgtype */
2556 { 0 } }; /* im_dst */
2558 if (bw_upcalls_n == 0)
2559 return; /* No pending upcalls */
2564 * Allocate a new mbuf, initialize it with the header and
2565 * the payload for the pending calls.
2567 MGETHDR(m, M_DONTWAIT, MT_HEADER);
2569 log(LOG_WARNING, "bw_upcalls_send: cannot allocate mbuf\n");
2573 m->m_len = m->m_pkthdr.len = 0;
2574 m_copyback(m, 0, sizeof(struct igmpmsg), (caddr_t)&igmpmsg);
2575 m_copyback(m, sizeof(struct igmpmsg), len, (caddr_t)&bw_upcalls[0]);
2579 * XXX do we need to set the address in k_igmpsrc ?
2581 mrtstat.mrts_upcalls++;
2582 if (socket_send(ip_mrouter, m, &k_igmpsrc) < 0) {
2583 log(LOG_WARNING, "bw_upcalls_send: ip_mrouter socket queue full\n");
2584 ++mrtstat.mrts_upq_sockfull;
2589 * Compute the timeout hash value for the bw_meter entries
2591 #define BW_METER_TIMEHASH(bw_meter, hash) \
2593 struct timeval next_timeval = (bw_meter)->bm_start_time; \
2595 BW_TIMEVALADD(&next_timeval, &(bw_meter)->bm_threshold.b_time); \
2596 (hash) = next_timeval.tv_sec; \
2597 if (next_timeval.tv_usec) \
2598 (hash)++; /* XXX: make sure we don't timeout early */ \
2599 (hash) %= BW_METER_BUCKETS; \
2603 * Schedule a timer to process periodically bw_meter entry of type "<="
2604 * by linking the entry in the proper hash bucket.
2607 schedule_bw_meter(struct bw_meter *x, struct timeval *nowp)
2611 if (!(x->bm_flags & BW_METER_LEQ))
2612 return; /* XXX: we schedule timers only for "<=" entries */
2615 * Reset the bw_meter entry
2618 x->bm_start_time = *nowp;
2619 x->bm_measured.b_packets = 0;
2620 x->bm_measured.b_bytes = 0;
2621 x->bm_flags &= ~BW_METER_UPCALL_DELIVERED;
2625 * Compute the timeout hash value and insert the entry
2627 BW_METER_TIMEHASH(x, time_hash);
2628 x->bm_time_next = bw_meter_timers[time_hash];
2629 bw_meter_timers[time_hash] = x;
2630 x->bm_time_hash = time_hash;
2634 * Unschedule the periodic timer that processes bw_meter entry of type "<="
2635 * by removing the entry from the proper hash bucket.
2638 unschedule_bw_meter(struct bw_meter *x)
2641 struct bw_meter *prev, *tmp;
2643 if (!(x->bm_flags & BW_METER_LEQ))
2644 return; /* XXX: we schedule timers only for "<=" entries */
2647 * Compute the timeout hash value and delete the entry
2649 time_hash = x->bm_time_hash;
2650 if (time_hash >= BW_METER_BUCKETS)
2651 return; /* Entry was not scheduled */
2653 for (prev = NULL, tmp = bw_meter_timers[time_hash];
2654 tmp != NULL; prev = tmp, tmp = tmp->bm_time_next)
2659 panic("unschedule_bw_meter: bw_meter entry not found");
2662 prev->bm_time_next = x->bm_time_next;
2664 bw_meter_timers[time_hash] = x->bm_time_next;
2666 x->bm_time_next = NULL;
2667 x->bm_time_hash = BW_METER_BUCKETS;
2672 * Process all "<=" type of bw_meter that should be processed now,
2673 * and for each entry prepare an upcall if necessary. Each processed
2674 * entry is rescheduled again for the (periodic) processing.
2676 * This is run periodically (once per second normally). On each round,
2677 * all the potentially matching entries are in the hash slot that we are
2683 static uint32_t last_tv_sec; /* last time we processed this */
2687 struct timeval now, process_endtime;
2690 if (last_tv_sec == now.tv_sec)
2691 return; /* nothing to do */
2694 loops = now.tv_sec - last_tv_sec;
2695 last_tv_sec = now.tv_sec;
2696 if (loops > BW_METER_BUCKETS)
2697 loops = BW_METER_BUCKETS;
2700 * Process all bins of bw_meter entries from the one after the last
2701 * processed to the current one. On entry, i points to the last bucket
2702 * visited, so we need to increment i at the beginning of the loop.
2704 for (i = (now.tv_sec - loops) % BW_METER_BUCKETS; loops > 0; loops--) {
2705 struct bw_meter *x, *tmp_list;
2707 if (++i >= BW_METER_BUCKETS)
2710 /* Disconnect the list of bw_meter entries from the bin */
2711 tmp_list = bw_meter_timers[i];
2712 bw_meter_timers[i] = NULL;
2714 /* Process the list of bw_meter entries */
2715 while (tmp_list != NULL) {
2717 tmp_list = tmp_list->bm_time_next;
2719 /* Test if the time interval is over */
2720 process_endtime = x->bm_start_time;
2721 BW_TIMEVALADD(&process_endtime, &x->bm_threshold.b_time);
2722 if (BW_TIMEVALCMP(&process_endtime, &now, >)) {
2723 /* Not yet: reschedule, but don't reset */
2726 BW_METER_TIMEHASH(x, time_hash);
2727 if (time_hash == i && process_endtime.tv_sec == now.tv_sec) {
2729 * XXX: somehow the bin processing is a bit ahead of time.
2730 * Put the entry in the next bin.
2732 if (++time_hash >= BW_METER_BUCKETS)
2735 x->bm_time_next = bw_meter_timers[time_hash];
2736 bw_meter_timers[time_hash] = x;
2737 x->bm_time_hash = time_hash;
2743 * Test if we should deliver an upcall
2745 if (((x->bm_flags & BW_METER_UNIT_PACKETS) &&
2746 (x->bm_measured.b_packets <= x->bm_threshold.b_packets)) ||
2747 ((x->bm_flags & BW_METER_UNIT_BYTES) &&
2748 (x->bm_measured.b_bytes <= x->bm_threshold.b_bytes))) {
2749 /* Prepare an upcall for delivery */
2750 bw_meter_prepare_upcall(x, &now);
2754 * Reschedule for next processing
2756 schedule_bw_meter(x, &now);
2761 /* Send all upcalls that are pending delivery */
2766 * A periodic function for sending all upcalls that are pending delivery
2769 expire_bw_upcalls_send(void *unused)
2773 bw_upcalls_ch = timeout(expire_bw_upcalls_send, NULL, BW_UPCALLS_PERIOD);
2777 * A periodic function for periodic scanning of the multicast forwarding
2778 * table for processing all "<=" bw_meter entries.
2781 expire_bw_meter_process(void *unused)
2783 if (mrt_api_config & MRT_MFC_BW_UPCALL)
2786 bw_meter_ch = timeout(expire_bw_meter_process, NULL, BW_METER_PERIOD);
2790 * End of bandwidth monitoring code
2795 * Send the packet up to the user daemon, or eventually do kernel encapsulation
2799 pim_register_send(struct ip *ip, struct vif *vifp,
2800 struct mbuf *m, struct mfc *rt)
2802 struct mbuf *mb_copy, *mm;
2804 if (mrtdebug & DEBUG_PIM)
2805 log(LOG_DEBUG, "pim_register_send: ");
2807 mb_copy = pim_register_prepare(ip, m);
2808 if (mb_copy == NULL)
2812 * Send all the fragments. Note that the mbuf for each fragment
2813 * is freed by the sending machinery.
2815 for (mm = mb_copy; mm; mm = mb_copy) {
2816 mb_copy = mm->m_nextpkt;
2818 mm = m_pullup(mm, sizeof(struct ip));
2820 ip = mtod(mm, struct ip *);
2821 if ((mrt_api_config & MRT_MFC_RP) &&
2822 (rt->mfc_rp.s_addr != INADDR_ANY)) {
2823 pim_register_send_rp(ip, vifp, mm, rt);
2825 pim_register_send_upcall(ip, vifp, mm, rt);
2834 * Return a copy of the data packet that is ready for PIM Register
2836 * XXX: Note that in the returned copy the IP header is a valid one.
2838 static struct mbuf *
2839 pim_register_prepare(struct ip *ip, struct mbuf *m)
2841 struct mbuf *mb_copy = NULL;
2844 /* Take care of delayed checksums */
2845 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
2846 in_delayed_cksum(m);
2847 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
2851 * Copy the old packet & pullup its IP header into the
2852 * new mbuf so we can modify it.
2854 mb_copy = m_copypacket(m, M_DONTWAIT);
2855 if (mb_copy == NULL)
2857 mb_copy = m_pullup(mb_copy, ip->ip_hl << 2);
2858 if (mb_copy == NULL)
2861 /* take care of the TTL */
2862 ip = mtod(mb_copy, struct ip *);
2865 /* Compute the MTU after the PIM Register encapsulation */
2866 mtu = 0xffff - sizeof(pim_encap_iphdr) - sizeof(pim_encap_pimhdr);
2868 if (ip->ip_len <= mtu) {
2869 /* Turn the IP header into a valid one */
2870 ip->ip_len = htons(ip->ip_len);
2871 ip->ip_off = htons(ip->ip_off);
2873 ip->ip_sum = in_cksum(mb_copy, ip->ip_hl << 2);
2875 /* Fragment the packet */
2876 if (ip_fragment(ip, &mb_copy, mtu, 0, CSUM_DELAY_IP) != 0) {
2885 * Send an upcall with the data packet to the user-level process.
2888 pim_register_send_upcall(struct ip *ip, struct vif *vifp,
2889 struct mbuf *mb_copy, struct mfc *rt)
2891 struct mbuf *mb_first;
2892 int len = ntohs(ip->ip_len);
2894 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
2897 * Add a new mbuf with an upcall header
2899 MGETHDR(mb_first, M_DONTWAIT, MT_HEADER);
2900 if (mb_first == NULL) {
2904 mb_first->m_data += max_linkhdr;
2905 mb_first->m_pkthdr.len = len + sizeof(struct igmpmsg);
2906 mb_first->m_len = sizeof(struct igmpmsg);
2907 mb_first->m_next = mb_copy;
2909 /* Send message to routing daemon */
2910 im = mtod(mb_first, struct igmpmsg *);
2911 im->im_msgtype = IGMPMSG_WHOLEPKT;
2913 im->im_vif = vifp - viftable;
2914 im->im_src = ip->ip_src;
2915 im->im_dst = ip->ip_dst;
2917 k_igmpsrc.sin_addr = ip->ip_src;
2919 mrtstat.mrts_upcalls++;
2921 if (socket_send(ip_mrouter, mb_first, &k_igmpsrc) < 0) {
2922 if (mrtdebug & DEBUG_PIM)
2924 "mcast: pim_register_send_upcall: ip_mrouter socket queue full");
2925 ++mrtstat.mrts_upq_sockfull;
2929 /* Keep statistics */
2930 pimstat.pims_snd_registers_msgs++;
2931 pimstat.pims_snd_registers_bytes += len;
2937 * Encapsulate the data packet in PIM Register message and send it to the RP.
2940 pim_register_send_rp(struct ip *ip, struct vif *vifp,
2941 struct mbuf *mb_copy, struct mfc *rt)
2943 struct mbuf *mb_first;
2944 struct ip *ip_outer;
2945 struct pim_encap_pimhdr *pimhdr;
2946 int len = ntohs(ip->ip_len);
2947 vifi_t vifi = rt->mfc_parent;
2949 if ((vifi >= numvifs) || (viftable[vifi].v_lcl_addr.s_addr == 0)) {
2951 return EADDRNOTAVAIL; /* The iif vif is invalid */
2955 * Add a new mbuf with the encapsulating header
2957 MGETHDR(mb_first, M_DONTWAIT, MT_HEADER);
2958 if (mb_first == NULL) {
2962 mb_first->m_data += max_linkhdr;
2963 mb_first->m_len = sizeof(pim_encap_iphdr) + sizeof(pim_encap_pimhdr);
2964 mb_first->m_next = mb_copy;
2966 mb_first->m_pkthdr.len = len + mb_first->m_len;
2969 * Fill in the encapsulating IP and PIM header
2971 ip_outer = mtod(mb_first, struct ip *);
2972 *ip_outer = pim_encap_iphdr;
2974 ip_outer->ip_id = ip_randomid();
2976 ip_outer->ip_id = htons(ip_id++);
2978 ip_outer->ip_len = len + sizeof(pim_encap_iphdr) + sizeof(pim_encap_pimhdr);
2979 ip_outer->ip_src = viftable[vifi].v_lcl_addr;
2980 ip_outer->ip_dst = rt->mfc_rp;
2982 * Copy the inner header TOS to the outer header, and take care of the
2985 ip_outer->ip_tos = ip->ip_tos;
2986 if (ntohs(ip->ip_off) & IP_DF)
2987 ip_outer->ip_off |= IP_DF;
2988 pimhdr = (struct pim_encap_pimhdr *)((caddr_t)ip_outer
2989 + sizeof(pim_encap_iphdr));
2990 *pimhdr = pim_encap_pimhdr;
2991 /* If the iif crosses a border, set the Border-bit */
2992 if (rt->mfc_flags[vifi] & MRT_MFC_FLAGS_BORDER_VIF & mrt_api_config)
2993 pimhdr->flags |= htonl(PIM_BORDER_REGISTER);
2995 mb_first->m_data += sizeof(pim_encap_iphdr);
2996 pimhdr->pim.pim_cksum = in_cksum(mb_first, sizeof(pim_encap_pimhdr));
2997 mb_first->m_data -= sizeof(pim_encap_iphdr);
2999 if (vifp->v_rate_limit == 0)
3000 tbf_send_packet(vifp, mb_first);
3002 tbf_control(vifp, mb_first, ip, ip_outer->ip_len);
3004 /* Keep statistics */
3005 pimstat.pims_snd_registers_msgs++;
3006 pimstat.pims_snd_registers_bytes += len;
3012 * PIM-SMv2 and PIM-DM messages processing.
3013 * Receives and verifies the PIM control messages, and passes them
3014 * up to the listening socket, using rip_input().
3015 * The only message with special processing is the PIM_REGISTER message
3016 * (used by PIM-SM): the PIM header is stripped off, and the inner packet
3017 * is passed to if_simloop().
3020 pim_input(struct mbuf *m, int off)
3022 struct ip *ip = mtod(m, struct ip *);
3025 int datalen = ip->ip_len;
3029 /* Keep statistics */
3030 pimstat.pims_rcv_total_msgs++;
3031 pimstat.pims_rcv_total_bytes += datalen;
3036 if (datalen < PIM_MINLEN) {
3037 pimstat.pims_rcv_tooshort++;
3038 log(LOG_ERR, "pim_input: packet size too small %d from %lx\n",
3039 datalen, (u_long)ip->ip_src.s_addr);
3045 * If the packet is at least as big as a REGISTER, go agead
3046 * and grab the PIM REGISTER header size, to avoid another
3047 * possible m_pullup() later.
3049 * PIM_MINLEN == pimhdr + u_int32_t == 4 + 4 = 8
3050 * PIM_REG_MINLEN == pimhdr + reghdr + encap_iphdr == 4 + 4 + 20 = 28
3052 minlen = iphlen + (datalen >= PIM_REG_MINLEN ? PIM_REG_MINLEN : PIM_MINLEN);
3054 * Get the IP and PIM headers in contiguous memory, and
3055 * possibly the PIM REGISTER header.
3057 if ((m->m_flags & M_EXT || m->m_len < minlen) &&
3058 (m = m_pullup(m, minlen)) == 0) {
3059 log(LOG_ERR, "pim_input: m_pullup failure\n");
3062 /* m_pullup() may have given us a new mbuf so reset ip. */
3063 ip = mtod(m, struct ip *);
3064 ip_tos = ip->ip_tos;
3066 /* adjust mbuf to point to the PIM header */
3067 m->m_data += iphlen;
3069 pim = mtod(m, struct pim *);
3072 * Validate checksum. If PIM REGISTER, exclude the data packet.
3074 * XXX: some older PIMv2 implementations don't make this distinction,
3075 * so for compatibility reason perform the checksum over part of the
3076 * message, and if error, then over the whole message.
3078 if (PIM_VT_T(pim->pim_vt) == PIM_REGISTER && in_cksum(m, PIM_MINLEN) == 0) {
3079 /* do nothing, checksum okay */
3080 } else if (in_cksum(m, datalen)) {
3081 pimstat.pims_rcv_badsum++;
3082 if (mrtdebug & DEBUG_PIM)
3083 log(LOG_DEBUG, "pim_input: invalid checksum");
3088 /* PIM version check */
3089 if (PIM_VT_V(pim->pim_vt) < PIM_VERSION) {
3090 pimstat.pims_rcv_badversion++;
3091 log(LOG_ERR, "pim_input: incorrect version %d, expecting %d\n",
3092 PIM_VT_V(pim->pim_vt), PIM_VERSION);
3097 /* restore mbuf back to the outer IP */
3098 m->m_data -= iphlen;
3101 if (PIM_VT_T(pim->pim_vt) == PIM_REGISTER) {
3103 * Since this is a REGISTER, we'll make a copy of the register
3104 * headers ip + pim + u_int32 + encap_ip, to be passed up to the
3107 struct sockaddr_in dst = { sizeof(dst), AF_INET };
3109 struct ip *encap_ip;
3112 if ((reg_vif_num >= numvifs) || (reg_vif_num == VIFI_INVALID)) {
3113 if (mrtdebug & DEBUG_PIM)
3115 "pim_input: register vif not set: %d\n", reg_vif_num);
3123 if (datalen < PIM_REG_MINLEN) {
3124 pimstat.pims_rcv_tooshort++;
3125 pimstat.pims_rcv_badregisters++;
3127 "pim_input: register packet size too small %d from %lx\n",
3128 datalen, (u_long)ip->ip_src.s_addr);
3133 reghdr = (u_int32_t *)(pim + 1);
3134 encap_ip = (struct ip *)(reghdr + 1);
3136 if (mrtdebug & DEBUG_PIM) {
3138 "pim_input[register], encap_ip: %lx -> %lx, encap_ip len %d\n",
3139 (u_long)ntohl(encap_ip->ip_src.s_addr),
3140 (u_long)ntohl(encap_ip->ip_dst.s_addr),
3141 ntohs(encap_ip->ip_len));
3144 /* verify the version number of the inner packet */
3145 if (encap_ip->ip_v != IPVERSION) {
3146 pimstat.pims_rcv_badregisters++;
3147 if (mrtdebug & DEBUG_PIM) {
3148 log(LOG_DEBUG, "pim_input: invalid IP version (%d) "
3149 "of the inner packet\n", encap_ip->ip_v);
3155 /* verify the inner packet is destined to a mcast group */
3156 if (!IN_MULTICAST(ntohl(encap_ip->ip_dst.s_addr))) {
3157 pimstat.pims_rcv_badregisters++;
3158 if (mrtdebug & DEBUG_PIM)
3160 "pim_input: inner packet of register is not "
3162 (u_long)ntohl(encap_ip->ip_dst.s_addr));
3168 * Copy the TOS from the outer IP header to the inner IP header.
3170 if (encap_ip->ip_tos != ip_tos) {
3171 /* Outer TOS -> inner TOS */
3172 encap_ip->ip_tos = ip_tos;
3173 /* Recompute the inner header checksum. Sigh... */
3175 /* adjust mbuf to point to the inner IP header */
3176 m->m_data += (iphlen + PIM_MINLEN);
3177 m->m_len -= (iphlen + PIM_MINLEN);
3179 encap_ip->ip_sum = 0;
3180 encap_ip->ip_sum = in_cksum(m, encap_ip->ip_hl << 2);
3182 /* restore mbuf to point back to the outer IP header */
3183 m->m_data -= (iphlen + PIM_MINLEN);
3184 m->m_len += (iphlen + PIM_MINLEN);
3187 /* If a NULL_REGISTER, pass it to the daemon */
3188 if ((ntohl(*reghdr) & PIM_NULL_REGISTER))
3189 goto pim_input_to_daemon;
3192 * Decapsulate the inner IP packet and loopback to forward it
3193 * as a normal multicast packet. Also, make a copy of the
3194 * outer_iphdr + pimhdr + reghdr + encap_iphdr
3195 * to pass to the daemon later, so it can take the appropriate
3196 * actions (e.g., send back PIM_REGISTER_STOP).
3197 * XXX: here m->m_data points to the outer IP header.
3199 mcp = m_copy(m, 0, iphlen + PIM_REG_MINLEN);
3202 "pim_input: pim register: could not copy register head\n");
3207 /* Keep statistics */
3208 /* XXX: registers_bytes include only the encap. mcast pkt */
3209 pimstat.pims_rcv_registers_msgs++;
3210 pimstat.pims_rcv_registers_bytes += ntohs(encap_ip->ip_len);
3213 * forward the inner ip packet; point m_data at the inner ip.
3215 m_adj(m, iphlen + PIM_MINLEN);
3217 if (mrtdebug & DEBUG_PIM) {
3219 "pim_input: forwarding decapsulated register: "
3220 "src %lx, dst %lx, vif %d\n",
3221 (u_long)ntohl(encap_ip->ip_src.s_addr),
3222 (u_long)ntohl(encap_ip->ip_dst.s_addr),
3225 if_simloop(viftable[reg_vif_num].v_ifp, m, dst.sin_family, 0);
3227 /* prepare the register head to send to the mrouting daemon */
3231 pim_input_to_daemon:
3233 * Pass the PIM message up to the daemon; if it is a Register message,
3234 * pass the 'head' only up to the daemon. This includes the
3235 * outer IP header, PIM header, PIM-Register header and the
3237 * XXX: the outer IP header pkt size of a Register is not adjust to
3238 * reflect the fact that the inner multicast data is truncated.
3240 rip_input(m, iphlen);
3247 ip_mroute_modevent(module_t mod, int type, void *unused)
3254 /* XXX Protect against multiple loading */
3255 ip_mcast_src = X_ip_mcast_src;
3256 ip_mforward = X_ip_mforward;
3257 ip_mrouter_done = X_ip_mrouter_done;
3258 ip_mrouter_get = X_ip_mrouter_get;
3259 ip_mrouter_set = X_ip_mrouter_set;
3260 ip_rsvp_force_done = X_ip_rsvp_force_done;
3261 ip_rsvp_vif = X_ip_rsvp_vif;
3262 legal_vif_num = X_legal_vif_num;
3263 mrt_ioctl = X_mrt_ioctl;
3264 rsvp_input_p = X_rsvp_input;
3273 ip_mcast_src = NULL;
3275 ip_mrouter_done = NULL;
3276 ip_mrouter_get = NULL;
3277 ip_mrouter_set = NULL;
3278 ip_rsvp_force_done = NULL;
3280 legal_vif_num = NULL;
3282 rsvp_input_p = NULL;
3289 static moduledata_t ip_mroutemod = {
3294 DECLARE_MODULE(ip_mroute, ip_mroutemod, SI_SUB_PSEUDO, SI_ORDER_ANY);