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
11 * MROUTING Revision: 3.5
15 #include "opt_mrouting.h"
16 #include "opt_random_ip_id.h"
18 #include <sys/param.h>
19 #include <sys/systm.h>
20 #include <sys/malloc.h>
22 #include <sys/socket.h>
23 #include <sys/socketvar.h>
24 #include <sys/protosw.h>
26 #include <sys/kernel.h>
27 #include <sys/sysctl.h>
28 #include <sys/sockio.h>
29 #include <sys/syslog.h>
31 #include <net/route.h>
32 #include <netinet/in.h>
33 #include <netinet/in_systm.h>
34 #include <netinet/ip.h>
35 #include <netinet/ip_var.h>
36 #include <netinet/in_var.h>
37 #include <netinet/igmp.h>
38 #include <netinet/ip_encap.h>
39 #include <netinet/ip_mroute.h>
40 #include <netinet/udp.h>
41 #include <machine/in_cksum.h>
44 extern u_long _ip_mcast_src(int vifi);
45 extern int _ip_mforward(struct ip *ip, struct ifnet *ifp, struct mbuf *m,
46 struct ip_moptions *imo);
47 extern int _ip_mrouter_done(void);
48 extern int _ip_mrouter_get(struct socket *so, struct sockopt *sopt);
49 extern int _ip_mrouter_set(struct socket *so, struct sockopt *sopt);
50 extern int _mrt_ioctl(int req, caddr_t data);
53 * Dummy routines and globals used when multicast routing is not compiled in.
56 struct socket *ip_mrouter = NULL;
60 _ip_mrouter_set(so, sopt)
67 int (*ip_mrouter_set)(struct socket *, struct sockopt *) = _ip_mrouter_set;
71 _ip_mrouter_get(so, sopt)
78 int (*ip_mrouter_get)(struct socket *, struct sockopt *) = _ip_mrouter_get;
86 int (*ip_mrouter_done)(void) = _ip_mrouter_done;
89 _ip_mforward(ip, ifp, m, imo)
93 struct ip_moptions *imo;
98 int (*ip_mforward)(struct ip *, struct ifnet *, struct mbuf *,
99 struct ip_moptions *) = _ip_mforward;
102 _mrt_ioctl(int req, caddr_t data)
107 int (*mrt_ioctl)(int, caddr_t) = _mrt_ioctl;
110 rsvp_input(m, off) /* XXX must fixup manually */
114 /* Can still get packets with rsvp_on = 0 if there is a local member
115 * of the group to which the RSVP packet is addressed. But in this
116 * case we want to throw the packet away.
123 if (ip_rsvpd != NULL) {
125 printf("rsvp_input: Sending packet up old-style socket\n");
129 /* Drop the packet */
133 int (*legal_vif_num)(int) = 0;
136 * This should never be called, since IP_MULTICAST_VIF should fail, but
137 * just in case it does get called, the code a little lower in ip_output
138 * will assign the packet a local address.
141 _ip_mcast_src(int vifi) { return INADDR_ANY; }
142 u_long (*ip_mcast_src)(int) = _ip_mcast_src;
145 ip_rsvp_vif_init(so, sopt)
147 struct sockopt *sopt;
153 ip_rsvp_vif_done(so, sopt)
155 struct sockopt *sopt;
161 ip_rsvp_force_done(so)
169 #define M_HASCL(m) ((m)->m_flags & M_EXT)
171 static MALLOC_DEFINE(M_MRTABLE, "mroutetbl", "multicast routing tables");
174 /* The socket used to communicate with the multicast routing daemon. */
175 struct socket *ip_mrouter = NULL;
178 #if defined(MROUTING) || defined(MROUTE_KLD)
179 static struct mrtstat mrtstat;
180 SYSCTL_STRUCT(_net_inet_ip, OID_AUTO, mrtstat, CTLFLAG_RW,
181 &mrtstat, mrtstat, "Multicast Routing Statistics (struct mrtstat, netinet/ip_mroute.h)");
184 static struct mfc *mfctable[MFCTBLSIZ];
185 static u_char nexpire[MFCTBLSIZ];
186 static struct vif viftable[MAXVIFS];
187 static u_int mrtdebug = 0; /* debug level */
188 #define DEBUG_MFC 0x02
189 #define DEBUG_FORWARD 0x04
190 #define DEBUG_EXPIRE 0x08
191 #define DEBUG_XMIT 0x10
192 static u_int tbfdebug = 0; /* tbf debug level */
193 static u_int rsvpdebug = 0; /* rsvp debug level */
195 static struct callout_handle expire_upcalls_ch;
197 #define EXPIRE_TIMEOUT (hz / 4) /* 4x / second */
198 #define UPCALL_EXPIRE 6 /* number of timeouts */
201 * Define the token bucket filter structures
202 * tbftable -> each vif has one of these for storing info
205 static struct tbf tbftable[MAXVIFS];
206 #define TBF_REPROCESS (hz / 100) /* 100x / second */
209 * 'Interfaces' associated with decapsulator (so we can tell
210 * packets that went through it from ones that get reflected
211 * by a broken gateway). These interfaces are never linked into
212 * the system ifnet list & no routes point to them. I.e., packets
213 * can't be sent this way. They only exist as a placeholder for
214 * multicast source verification.
216 static struct ifnet multicast_decap_if[MAXVIFS];
219 #define ENCAP_PROTO IPPROTO_IPIP /* 4 */
221 /* prototype IP hdr for encapsulated packets */
222 static struct ip multicast_encap_iphdr = {
223 #if BYTE_ORDER == LITTLE_ENDIAN
224 sizeof(struct ip) >> 2, IPVERSION,
226 IPVERSION, sizeof(struct ip) >> 2,
229 sizeof(struct ip), /* total length */
232 ENCAP_TTL, ENCAP_PROTO,
239 static vifi_t numvifs = 0;
240 static const struct encaptab *encap_cookie = NULL;
243 * one-back cache used by mroute_encapcheck to locate a tunnel's vif
244 * given a datagram's src ip address.
246 static u_long last_encap_src;
247 static struct vif *last_encap_vif;
249 static u_long X_ip_mcast_src(int vifi);
250 static int X_ip_mforward(struct ip *ip, struct ifnet *ifp, struct mbuf *m, struct ip_moptions *imo);
251 static int X_ip_mrouter_done(void);
252 static int X_ip_mrouter_get(struct socket *so, struct sockopt *m);
253 static int X_ip_mrouter_set(struct socket *so, struct sockopt *m);
254 static int X_legal_vif_num(int vif);
255 static int X_mrt_ioctl(int cmd, caddr_t data);
257 static int get_sg_cnt(struct sioc_sg_req *);
258 static int get_vif_cnt(struct sioc_vif_req *);
259 static int ip_mrouter_init(struct socket *, int);
260 static int add_vif(struct vifctl *);
261 static int del_vif(vifi_t);
262 static int add_mfc(struct mfcctl *);
263 static int del_mfc(struct mfcctl *);
264 static int socket_send(struct socket *, struct mbuf *, struct sockaddr_in *);
265 static int set_assert(int);
266 static void expire_upcalls(void *);
267 static int ip_mdq(struct mbuf *, struct ifnet *, struct mfc *,
269 static void phyint_send(struct ip *, struct vif *, struct mbuf *);
270 static void encap_send(struct ip *, struct vif *, struct mbuf *);
271 static void tbf_control(struct vif *, struct mbuf *, struct ip *, u_long);
272 static void tbf_queue(struct vif *, struct mbuf *);
273 static void tbf_process_q(struct vif *);
274 static void tbf_reprocess_q(void *);
275 static int tbf_dq_sel(struct vif *, struct ip *);
276 static void tbf_send_packet(struct vif *, struct mbuf *);
277 static void tbf_update_tokens(struct vif *);
278 static int priority(struct vif *, struct ip *);
281 * whether or not special PIM assert processing is enabled.
283 static int pim_assert;
285 * Rate limit for assert notification messages, in usec
287 #define ASSERT_MSG_TIME 3000000
290 * Hash function for a source, group entry
292 #define MFCHASH(a, g) MFCHASHMOD(((a) >> 20) ^ ((a) >> 10) ^ (a) ^ \
293 ((g) >> 20) ^ ((g) >> 10) ^ (g))
296 * Find a route for a given origin IP address and Multicast group address
297 * Type of service parameter to be added in the future!!!
300 #define MFCFIND(o, g, rt) { \
301 register struct mfc *_rt = mfctable[MFCHASH(o,g)]; \
303 ++mrtstat.mrts_mfc_lookups; \
305 if ((_rt->mfc_origin.s_addr == o) && \
306 (_rt->mfc_mcastgrp.s_addr == g) && \
307 (_rt->mfc_stall == NULL)) { \
311 _rt = _rt->mfc_next; \
314 ++mrtstat.mrts_mfc_misses; \
320 * Macros to compute elapsed time efficiently
321 * Borrowed from Van Jacobson's scheduling code
323 #define TV_DELTA(a, b, delta) { \
326 delta = (a).tv_usec - (b).tv_usec; \
327 if ((xxs = (a).tv_sec - (b).tv_sec)) { \
336 delta += (1000000 * xxs); \
341 #define TV_LT(a, b) (((a).tv_usec < (b).tv_usec && \
342 (a).tv_sec <= (b).tv_sec) || (a).tv_sec < (b).tv_sec)
345 u_long upcall_data[51];
346 static void collate(struct timeval *);
347 #endif /* UPCALL_TIMING */
351 * Handle MRT setsockopt commands to modify the multicast routing tables.
354 X_ip_mrouter_set(so, sopt)
356 struct sockopt *sopt;
363 if (so != ip_mrouter && sopt->sopt_name != MRT_INIT)
367 switch (sopt->sopt_name) {
369 error = sooptcopyin(sopt, &optval, sizeof optval,
373 error = ip_mrouter_init(so, optval);
377 error = ip_mrouter_done();
381 error = sooptcopyin(sopt, &vifc, sizeof vifc, sizeof vifc);
384 error = add_vif(&vifc);
388 error = sooptcopyin(sopt, &vifi, sizeof vifi, sizeof vifi);
391 error = del_vif(vifi);
396 error = sooptcopyin(sopt, &mfc, sizeof mfc, sizeof mfc);
399 if (sopt->sopt_name == MRT_ADD_MFC)
400 error = add_mfc(&mfc);
402 error = del_mfc(&mfc);
406 error = sooptcopyin(sopt, &optval, sizeof optval,
421 int (*ip_mrouter_set)(struct socket *, struct sockopt *) = X_ip_mrouter_set;
425 * Handle MRT getsockopt commands
428 X_ip_mrouter_get(so, sopt)
430 struct sockopt *sopt;
433 static int version = 0x0305; /* !!! why is this here? XXX */
435 switch (sopt->sopt_name) {
437 error = sooptcopyout(sopt, &version, sizeof version);
441 error = sooptcopyout(sopt, &pim_assert, sizeof pim_assert);
451 int (*ip_mrouter_get)(struct socket *, struct sockopt *) = X_ip_mrouter_get;
455 * Handle ioctl commands to obtain information from the cache
458 X_mrt_ioctl(cmd, data)
465 case (SIOCGETVIFCNT):
466 return (get_vif_cnt((struct sioc_vif_req *)data));
469 return (get_sg_cnt((struct sioc_sg_req *)data));
479 int (*mrt_ioctl)(int, caddr_t) = X_mrt_ioctl;
483 * returns the packet, byte, rpf-failure count for the source group provided
487 register struct sioc_sg_req *req;
489 register struct mfc *rt;
493 MFCFIND(req->src.s_addr, req->grp.s_addr, rt);
496 req->pktcnt = rt->mfc_pkt_cnt;
497 req->bytecnt = rt->mfc_byte_cnt;
498 req->wrong_if = rt->mfc_wrong_if;
500 req->pktcnt = req->bytecnt = req->wrong_if = 0xffffffff;
506 * returns the input and output packet and byte counts on the vif provided
510 register struct sioc_vif_req *req;
512 register vifi_t vifi = req->vifi;
514 if (vifi >= numvifs) return EINVAL;
516 req->icount = viftable[vifi].v_pkt_in;
517 req->ocount = viftable[vifi].v_pkt_out;
518 req->ibytes = viftable[vifi].v_bytes_in;
519 req->obytes = viftable[vifi].v_bytes_out;
525 * Enable multicast routing
528 ip_mrouter_init(so, version)
533 log(LOG_DEBUG,"ip_mrouter_init: so_type = %d, pr_protocol = %d\n",
534 so->so_type, so->so_proto->pr_protocol);
536 if (so->so_type != SOCK_RAW ||
537 so->so_proto->pr_protocol != IPPROTO_IGMP) return EOPNOTSUPP;
542 if (ip_mrouter != NULL) return EADDRINUSE;
546 bzero((caddr_t)mfctable, sizeof(mfctable));
547 bzero((caddr_t)nexpire, sizeof(nexpire));
551 expire_upcalls_ch = timeout(expire_upcalls, (caddr_t)NULL, EXPIRE_TIMEOUT);
554 log(LOG_DEBUG, "ip_mrouter_init\n");
560 * Disable multicast routing
576 * For each phyint in use, disable promiscuous reception of all IP
579 for (vifi = 0; vifi < numvifs; vifi++) {
580 if (viftable[vifi].v_lcl_addr.s_addr != 0 &&
581 !(viftable[vifi].v_flags & VIFF_TUNNEL)) {
582 ((struct sockaddr_in *)&(ifr.ifr_addr))->sin_family = AF_INET;
583 ((struct sockaddr_in *)&(ifr.ifr_addr))->sin_addr.s_addr
585 ifp = viftable[vifi].v_ifp;
589 bzero((caddr_t)tbftable, sizeof(tbftable));
590 bzero((caddr_t)viftable, sizeof(viftable));
594 untimeout(expire_upcalls, (caddr_t)NULL, expire_upcalls_ch);
597 * Free all multicast forwarding cache entries.
599 for (i = 0; i < MFCTBLSIZ; i++) {
600 for (rt = mfctable[i]; rt != NULL; ) {
601 struct mfc *nr = rt->mfc_next;
603 for (rte = rt->mfc_stall; rte != NULL; ) {
604 struct rtdetq *n = rte->next;
607 free(rte, M_MRTABLE);
615 bzero((caddr_t)mfctable, sizeof(mfctable));
618 * Reset de-encapsulation cache
621 last_encap_vif = NULL;
623 encap_detach(encap_cookie);
632 log(LOG_DEBUG, "ip_mrouter_done\n");
638 int (*ip_mrouter_done)(void) = X_ip_mrouter_done;
642 * Set PIM assert processing global
648 if ((i != 1) && (i != 0))
657 * Decide if a packet is from a tunnelled peer.
658 * Return 0 if not, 64 if so.
661 mroute_encapcheck(const struct mbuf *m, int off, int proto, void *arg)
663 struct ip *ip = mtod(m, struct ip *);
664 int hlen = ip->ip_hl << 2;
665 register struct vif *vifp;
668 * don't claim the packet if it's not to a multicast destination or if
669 * we don't have an encapsulating tunnel with the source.
670 * Note: This code assumes that the remote site IP address
671 * uniquely identifies the tunnel (i.e., that this site has
672 * at most one tunnel with the remote site).
674 if (! IN_MULTICAST(ntohl(((struct ip *)((char *)ip + hlen))->ip_dst.s_addr))) {
677 if (ip->ip_src.s_addr != last_encap_src) {
678 register struct vif *vife;
681 vife = vifp + numvifs;
682 last_encap_src = ip->ip_src.s_addr;
684 for ( ; vifp < vife; ++vifp)
685 if (vifp->v_rmt_addr.s_addr == ip->ip_src.s_addr) {
686 if ((vifp->v_flags & (VIFF_TUNNEL|VIFF_SRCRT))
688 last_encap_vif = vifp;
692 if ((vifp = last_encap_vif) == 0) {
700 * De-encapsulate a packet and feed it back through ip input (this
701 * routine is called whenever IP gets a packet that mroute_encap_func()
705 mroute_encap_input(struct mbuf *m, int off)
707 struct ip *ip = mtod(m, struct ip *);
708 int hlen = ip->ip_hl << 2;
710 if (hlen > sizeof(struct ip))
711 ip_stripoptions(m, (struct mbuf *) 0);
712 m->m_data += sizeof(struct ip);
713 m->m_len -= sizeof(struct ip);
714 m->m_pkthdr.len -= sizeof(struct ip);
716 m->m_pkthdr.rcvif = last_encap_vif->v_ifp;
718 (void) IF_HANDOFF(&ipintrq, m, NULL);
720 * normally we would need a "schednetisr(NETISR_IP)"
721 * here but we were called by ip_input and it is going
722 * to loop back & try to dequeue the packet we just
723 * queued as soon as we return so we avoid the
724 * unnecessary software interrrupt.
728 extern struct domain inetdomain;
729 static struct protosw mroute_encap_protosw =
730 { SOCK_RAW, &inetdomain, IPPROTO_IPV4, PR_ATOMIC|PR_ADDR,
731 mroute_encap_input, 0, 0, rip_ctloutput,
738 * Add a vif to the vif table
742 register struct vifctl *vifcp;
744 register struct vif *vifp = viftable + vifcp->vifc_vifi;
745 static struct sockaddr_in sin = {sizeof sin, AF_INET};
749 struct tbf *v_tbf = tbftable + vifcp->vifc_vifi;
751 if (vifcp->vifc_vifi >= MAXVIFS) return EINVAL;
752 if (vifp->v_lcl_addr.s_addr != 0) return EADDRINUSE;
754 /* Find the interface with an address in AF_INET family */
755 sin.sin_addr = vifcp->vifc_lcl_addr;
756 ifa = ifa_ifwithaddr((struct sockaddr *)&sin);
757 if (ifa == 0) return EADDRNOTAVAIL;
760 if (vifcp->vifc_flags & VIFF_TUNNEL) {
761 if ((vifcp->vifc_flags & VIFF_SRCRT) == 0) {
763 * An encapsulating tunnel is wanted. Tell
764 * mroute_encap_input() to start paying attention
765 * to encapsulated packets.
767 if (encap_cookie == NULL) {
768 encap_cookie = encap_attach_func(AF_INET, -1,
770 (struct protosw *)&mroute_encap_protosw, NULL);
772 if (encap_cookie == NULL) {
773 printf("ip_mroute: unable to attach encap\n");
774 return (EIO); /* XXX */
776 for (s = 0; s < MAXVIFS; ++s) {
777 multicast_decap_if[s].if_name = "mdecap";
778 multicast_decap_if[s].if_unit = s;
782 * Set interface to fake encapsulator interface
784 ifp = &multicast_decap_if[vifcp->vifc_vifi];
786 * Prepare cached route entry
788 bzero(&vifp->v_route, sizeof(vifp->v_route));
790 log(LOG_ERR, "source routed tunnels not supported\n");
794 /* Make sure the interface supports multicast */
795 if ((ifp->if_flags & IFF_MULTICAST) == 0)
798 /* Enable promiscuous reception of all IP multicasts from the if */
800 error = if_allmulti(ifp, 1);
807 /* define parameters for the tbf structure */
809 GET_TIME(vifp->v_tbf->tbf_last_pkt_t);
810 vifp->v_tbf->tbf_n_tok = 0;
811 vifp->v_tbf->tbf_q_len = 0;
812 vifp->v_tbf->tbf_max_q_len = MAXQSIZE;
813 vifp->v_tbf->tbf_q = vifp->v_tbf->tbf_t = NULL;
815 vifp->v_flags = vifcp->vifc_flags;
816 vifp->v_threshold = vifcp->vifc_threshold;
817 vifp->v_lcl_addr = vifcp->vifc_lcl_addr;
818 vifp->v_rmt_addr = vifcp->vifc_rmt_addr;
820 /* scaling up here allows division by 1024 in critical code */
821 vifp->v_rate_limit= vifcp->vifc_rate_limit * 1024 / 1000;
823 vifp->v_rsvpd = NULL;
824 /* initialize per vif pkt counters */
827 vifp->v_bytes_in = 0;
828 vifp->v_bytes_out = 0;
831 /* Adjust numvifs up if the vifi is higher than numvifs */
832 if (numvifs <= vifcp->vifc_vifi) numvifs = vifcp->vifc_vifi + 1;
835 log(LOG_DEBUG, "add_vif #%d, lcladdr %lx, %s %lx, thresh %x, rate %d\n",
837 (u_long)ntohl(vifcp->vifc_lcl_addr.s_addr),
838 (vifcp->vifc_flags & VIFF_TUNNEL) ? "rmtaddr" : "mask",
839 (u_long)ntohl(vifcp->vifc_rmt_addr.s_addr),
840 vifcp->vifc_threshold,
841 vifcp->vifc_rate_limit);
847 * Delete a vif from the vif table
853 register struct vif *vifp = &viftable[vifi];
854 register struct mbuf *m;
859 if (vifi >= numvifs) return EINVAL;
860 if (vifp->v_lcl_addr.s_addr == 0) return EADDRNOTAVAIL;
864 if (!(vifp->v_flags & VIFF_TUNNEL)) {
865 ((struct sockaddr_in *)&(ifr.ifr_addr))->sin_family = AF_INET;
866 ((struct sockaddr_in *)&(ifr.ifr_addr))->sin_addr.s_addr = INADDR_ANY;
871 if (vifp == last_encap_vif) {
877 * Free packets queued at the interface
879 while (vifp->v_tbf->tbf_q) {
880 m = vifp->v_tbf->tbf_q;
881 vifp->v_tbf->tbf_q = m->m_act;
885 bzero((caddr_t)vifp->v_tbf, sizeof(*(vifp->v_tbf)));
886 bzero((caddr_t)vifp, sizeof (*vifp));
889 log(LOG_DEBUG, "del_vif %d, numvifs %d\n", vifi, numvifs);
891 /* Adjust numvifs down */
892 for (vifi = numvifs; vifi > 0; vifi--)
893 if (viftable[vifi-1].v_lcl_addr.s_addr != 0) break;
906 struct mfcctl *mfccp;
911 register u_short nstl;
915 MFCFIND(mfccp->mfcc_origin.s_addr, mfccp->mfcc_mcastgrp.s_addr, rt);
917 /* If an entry already exists, just update the fields */
919 if (mrtdebug & DEBUG_MFC)
920 log(LOG_DEBUG,"add_mfc update o %lx g %lx p %x\n",
921 (u_long)ntohl(mfccp->mfcc_origin.s_addr),
922 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
926 rt->mfc_parent = mfccp->mfcc_parent;
927 for (i = 0; i < numvifs; i++)
928 rt->mfc_ttls[i] = mfccp->mfcc_ttls[i];
934 * Find the entry for which the upcall was made and update
937 hash = MFCHASH(mfccp->mfcc_origin.s_addr, mfccp->mfcc_mcastgrp.s_addr);
938 for (rt = mfctable[hash], nstl = 0; rt; rt = rt->mfc_next) {
940 if ((rt->mfc_origin.s_addr == mfccp->mfcc_origin.s_addr) &&
941 (rt->mfc_mcastgrp.s_addr == mfccp->mfcc_mcastgrp.s_addr) &&
942 (rt->mfc_stall != NULL)) {
945 log(LOG_ERR, "add_mfc %s o %lx g %lx p %x dbx %p\n",
946 "multiple kernel entries",
947 (u_long)ntohl(mfccp->mfcc_origin.s_addr),
948 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
949 mfccp->mfcc_parent, (void *)rt->mfc_stall);
951 if (mrtdebug & DEBUG_MFC)
952 log(LOG_DEBUG,"add_mfc o %lx g %lx p %x dbg %p\n",
953 (u_long)ntohl(mfccp->mfcc_origin.s_addr),
954 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
955 mfccp->mfcc_parent, (void *)rt->mfc_stall);
957 rt->mfc_origin = mfccp->mfcc_origin;
958 rt->mfc_mcastgrp = mfccp->mfcc_mcastgrp;
959 rt->mfc_parent = mfccp->mfcc_parent;
960 for (i = 0; i < numvifs; i++)
961 rt->mfc_ttls[i] = mfccp->mfcc_ttls[i];
962 /* initialize pkt counters per src-grp */
964 rt->mfc_byte_cnt = 0;
965 rt->mfc_wrong_if = 0;
966 rt->mfc_last_assert.tv_sec = rt->mfc_last_assert.tv_usec = 0;
968 rt->mfc_expire = 0; /* Don't clean this guy up */
971 /* free packets Qed at the end of this entry */
972 for (rte = rt->mfc_stall; rte != NULL; ) {
973 struct rtdetq *n = rte->next;
975 ip_mdq(rte->m, rte->ifp, rt, -1);
979 #endif /* UPCALL_TIMING */
980 free(rte, M_MRTABLE);
983 rt->mfc_stall = NULL;
988 * It is possible that an entry is being inserted without an upcall
991 if (mrtdebug & DEBUG_MFC)
992 log(LOG_DEBUG,"add_mfc no upcall h %lu o %lx g %lx p %x\n",
993 hash, (u_long)ntohl(mfccp->mfcc_origin.s_addr),
994 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
997 for (rt = mfctable[hash]; rt != NULL; rt = rt->mfc_next) {
999 if ((rt->mfc_origin.s_addr == mfccp->mfcc_origin.s_addr) &&
1000 (rt->mfc_mcastgrp.s_addr == mfccp->mfcc_mcastgrp.s_addr)) {
1002 rt->mfc_origin = mfccp->mfcc_origin;
1003 rt->mfc_mcastgrp = mfccp->mfcc_mcastgrp;
1004 rt->mfc_parent = mfccp->mfcc_parent;
1005 for (i = 0; i < numvifs; i++)
1006 rt->mfc_ttls[i] = mfccp->mfcc_ttls[i];
1007 /* initialize pkt counters per src-grp */
1008 rt->mfc_pkt_cnt = 0;
1009 rt->mfc_byte_cnt = 0;
1010 rt->mfc_wrong_if = 0;
1011 rt->mfc_last_assert.tv_sec = rt->mfc_last_assert.tv_usec = 0;
1018 /* no upcall, so make a new entry */
1019 rt = (struct mfc *)malloc(sizeof(*rt), M_MRTABLE, M_NOWAIT);
1025 /* insert new entry at head of hash chain */
1026 rt->mfc_origin = mfccp->mfcc_origin;
1027 rt->mfc_mcastgrp = mfccp->mfcc_mcastgrp;
1028 rt->mfc_parent = mfccp->mfcc_parent;
1029 for (i = 0; i < numvifs; i++)
1030 rt->mfc_ttls[i] = mfccp->mfcc_ttls[i];
1031 /* initialize pkt counters per src-grp */
1032 rt->mfc_pkt_cnt = 0;
1033 rt->mfc_byte_cnt = 0;
1034 rt->mfc_wrong_if = 0;
1035 rt->mfc_last_assert.tv_sec = rt->mfc_last_assert.tv_usec = 0;
1037 rt->mfc_stall = NULL;
1039 /* link into table */
1040 rt->mfc_next = mfctable[hash];
1041 mfctable[hash] = rt;
1048 #ifdef UPCALL_TIMING
1050 * collect delay statistics on the upcalls
1052 static void collate(t)
1053 register struct timeval *t;
1056 register struct timeval tp;
1057 register u_long delta;
1063 TV_DELTA(tp, *t, delta);
1072 #endif /* UPCALL_TIMING */
1075 * Delete an mfc entry
1079 struct mfcctl *mfccp;
1081 struct in_addr origin;
1082 struct in_addr mcastgrp;
1088 origin = mfccp->mfcc_origin;
1089 mcastgrp = mfccp->mfcc_mcastgrp;
1090 hash = MFCHASH(origin.s_addr, mcastgrp.s_addr);
1092 if (mrtdebug & DEBUG_MFC)
1093 log(LOG_DEBUG,"del_mfc orig %lx mcastgrp %lx\n",
1094 (u_long)ntohl(origin.s_addr), (u_long)ntohl(mcastgrp.s_addr));
1098 nptr = &mfctable[hash];
1099 while ((rt = *nptr) != NULL) {
1100 if (origin.s_addr == rt->mfc_origin.s_addr &&
1101 mcastgrp.s_addr == rt->mfc_mcastgrp.s_addr &&
1102 rt->mfc_stall == NULL)
1105 nptr = &rt->mfc_next;
1109 return EADDRNOTAVAIL;
1112 *nptr = rt->mfc_next;
1113 free(rt, M_MRTABLE);
1121 * Send a message to mrouted on the multicast routing socket
1124 socket_send(s, mm, src)
1127 struct sockaddr_in *src;
1130 if (sbappendaddr(&s->so_rcv,
1131 (struct sockaddr *)src,
1132 mm, (struct mbuf *)0) != 0) {
1142 * IP multicast forwarding function. This function assumes that the packet
1143 * pointed to by "ip" has arrived on (or is about to be sent to) the interface
1144 * pointed to by "ifp", and the packet is to be relayed to other networks
1145 * that have members of the packet's destination IP multicast group.
1147 * The packet is returned unscathed to the caller, unless it is
1148 * erroneous, in which case a non-zero return value tells the caller to
1152 #define TUNNEL_LEN 12 /* # bytes of IP option for tunnel encapsulation */
1155 X_ip_mforward(ip, ifp, m, imo)
1156 register struct ip *ip;
1159 struct ip_moptions *imo;
1161 register struct mfc *rt;
1162 register u_char *ipoptions;
1163 static struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
1164 static int srctun = 0;
1165 register struct mbuf *mm;
1170 if (mrtdebug & DEBUG_FORWARD)
1171 log(LOG_DEBUG, "ip_mforward: src %lx, dst %lx, ifp %p\n",
1172 (u_long)ntohl(ip->ip_src.s_addr), (u_long)ntohl(ip->ip_dst.s_addr),
1175 if (ip->ip_hl < (sizeof(struct ip) + TUNNEL_LEN) >> 2 ||
1176 (ipoptions = (u_char *)(ip + 1))[1] != IPOPT_LSRR ) {
1178 * Packet arrived via a physical interface or
1179 * an encapsulated tunnel.
1183 * Packet arrived through a source-route tunnel.
1184 * Source-route tunnels are no longer supported.
1186 if ((srctun++ % 1000) == 0)
1188 "ip_mforward: received source-routed packet from %lx\n",
1189 (u_long)ntohl(ip->ip_src.s_addr));
1194 if ((imo) && ((vifi = imo->imo_multicast_vif) < numvifs)) {
1195 if (ip->ip_ttl < 255)
1196 ip->ip_ttl++; /* compensate for -1 in *_send routines */
1197 if (rsvpdebug && ip->ip_p == IPPROTO_RSVP) {
1198 vifp = viftable + vifi;
1199 printf("Sending IPPROTO_RSVP from %lx to %lx on vif %d (%s%s%d)\n",
1200 (long)ntohl(ip->ip_src.s_addr), (long)ntohl(ip->ip_dst.s_addr),
1202 (vifp->v_flags & VIFF_TUNNEL) ? "tunnel on " : "",
1203 vifp->v_ifp->if_name, vifp->v_ifp->if_unit);
1205 return (ip_mdq(m, ifp, NULL, vifi));
1207 if (rsvpdebug && ip->ip_p == IPPROTO_RSVP) {
1208 printf("Warning: IPPROTO_RSVP from %lx to %lx without vif option\n",
1209 (long)ntohl(ip->ip_src.s_addr), (long)ntohl(ip->ip_dst.s_addr));
1211 printf("In fact, no options were specified at all\n");
1215 * Don't forward a packet with time-to-live of zero or one,
1216 * or a packet destined to a local-only group.
1218 if (ip->ip_ttl <= 1 ||
1219 ntohl(ip->ip_dst.s_addr) <= INADDR_MAX_LOCAL_GROUP)
1223 * Determine forwarding vifs from the forwarding cache table
1226 MFCFIND(ip->ip_src.s_addr, ip->ip_dst.s_addr, rt);
1228 /* Entry exists, so forward if necessary */
1231 return (ip_mdq(m, ifp, rt, -1));
1234 * If we don't have a route for packet's origin,
1235 * Make a copy of the packet &
1236 * send message to routing daemon
1239 register struct mbuf *mb0;
1240 register struct rtdetq *rte;
1241 register u_long hash;
1242 int hlen = ip->ip_hl << 2;
1243 #ifdef UPCALL_TIMING
1249 mrtstat.mrts_no_route++;
1250 if (mrtdebug & (DEBUG_FORWARD | DEBUG_MFC))
1251 log(LOG_DEBUG, "ip_mforward: no rte s %lx g %lx\n",
1252 (u_long)ntohl(ip->ip_src.s_addr),
1253 (u_long)ntohl(ip->ip_dst.s_addr));
1256 * Allocate mbufs early so that we don't do extra work if we are
1257 * just going to fail anyway. Make sure to pullup the header so
1258 * that other people can't step on it.
1260 rte = (struct rtdetq *)malloc((sizeof *rte), M_MRTABLE, M_NOWAIT);
1265 mb0 = m_copy(m, 0, M_COPYALL);
1266 if (mb0 && (M_HASCL(mb0) || mb0->m_len < hlen))
1267 mb0 = m_pullup(mb0, hlen);
1269 free(rte, M_MRTABLE);
1274 /* is there an upcall waiting for this packet? */
1275 hash = MFCHASH(ip->ip_src.s_addr, ip->ip_dst.s_addr);
1276 for (rt = mfctable[hash]; rt; rt = rt->mfc_next) {
1277 if ((ip->ip_src.s_addr == rt->mfc_origin.s_addr) &&
1278 (ip->ip_dst.s_addr == rt->mfc_mcastgrp.s_addr) &&
1279 (rt->mfc_stall != NULL))
1287 /* no upcall, so make a new entry */
1288 rt = (struct mfc *)malloc(sizeof(*rt), M_MRTABLE, M_NOWAIT);
1290 free(rte, M_MRTABLE);
1295 /* Make a copy of the header to send to the user level process */
1296 mm = m_copy(mb0, 0, hlen);
1298 free(rte, M_MRTABLE);
1300 free(rt, M_MRTABLE);
1306 * Send message to routing daemon to install
1307 * a route into the kernel table
1309 k_igmpsrc.sin_addr = ip->ip_src;
1311 im = mtod(mm, struct igmpmsg *);
1312 im->im_msgtype = IGMPMSG_NOCACHE;
1315 mrtstat.mrts_upcalls++;
1317 if (socket_send(ip_mrouter, mm, &k_igmpsrc) < 0) {
1318 log(LOG_WARNING, "ip_mforward: ip_mrouter socket queue full\n");
1319 ++mrtstat.mrts_upq_sockfull;
1320 free(rte, M_MRTABLE);
1322 free(rt, M_MRTABLE);
1327 /* insert new entry at head of hash chain */
1328 rt->mfc_origin.s_addr = ip->ip_src.s_addr;
1329 rt->mfc_mcastgrp.s_addr = ip->ip_dst.s_addr;
1330 rt->mfc_expire = UPCALL_EXPIRE;
1332 for (i = 0; i < numvifs; i++)
1333 rt->mfc_ttls[i] = 0;
1334 rt->mfc_parent = -1;
1336 /* link into table */
1337 rt->mfc_next = mfctable[hash];
1338 mfctable[hash] = rt;
1339 rt->mfc_stall = rte;
1342 /* determine if q has overflowed */
1346 for (p = &rt->mfc_stall; *p != NULL; p = &(*p)->next)
1349 if (npkts > MAX_UPQ) {
1350 mrtstat.mrts_upq_ovflw++;
1351 free(rte, M_MRTABLE);
1357 /* Add this entry to the end of the queue */
1363 #ifdef UPCALL_TIMING
1375 int (*ip_mforward)(struct ip *, struct ifnet *, struct mbuf *,
1376 struct ip_moptions *) = X_ip_mforward;
1380 * Clean up the cache entry if upcall is not serviced
1383 expire_upcalls(void *unused)
1386 struct mfc *mfc, **nptr;
1391 for (i = 0; i < MFCTBLSIZ; i++) {
1392 if (nexpire[i] == 0)
1394 nptr = &mfctable[i];
1395 for (mfc = *nptr; mfc != NULL; mfc = *nptr) {
1397 * Skip real cache entries
1398 * Make sure it wasn't marked to not expire (shouldn't happen)
1401 if (mfc->mfc_stall != NULL &&
1402 mfc->mfc_expire != 0 &&
1403 --mfc->mfc_expire == 0) {
1404 if (mrtdebug & DEBUG_EXPIRE)
1405 log(LOG_DEBUG, "expire_upcalls: expiring (%lx %lx)\n",
1406 (u_long)ntohl(mfc->mfc_origin.s_addr),
1407 (u_long)ntohl(mfc->mfc_mcastgrp.s_addr));
1409 * drop all the packets
1410 * free the mbuf with the pkt, if, timing info
1412 for (rte = mfc->mfc_stall; rte; ) {
1413 struct rtdetq *n = rte->next;
1416 free(rte, M_MRTABLE);
1419 ++mrtstat.mrts_cache_cleanups;
1422 *nptr = mfc->mfc_next;
1423 free(mfc, M_MRTABLE);
1425 nptr = &mfc->mfc_next;
1430 expire_upcalls_ch = timeout(expire_upcalls, (caddr_t)NULL, EXPIRE_TIMEOUT);
1434 * Packet forwarding routine once entry in the cache is made
1437 ip_mdq(m, ifp, rt, xmt_vif)
1438 register struct mbuf *m;
1439 register struct ifnet *ifp;
1440 register struct mfc *rt;
1441 register vifi_t xmt_vif;
1443 register struct ip *ip = mtod(m, struct ip *);
1444 register vifi_t vifi;
1445 register struct vif *vifp;
1446 register int plen = ip->ip_len;
1449 * Macro to send packet on vif. Since RSVP packets don't get counted on
1450 * input, they shouldn't get counted on output, so statistics keeping is
1453 #define MC_SEND(ip,vifp,m) { \
1454 if ((vifp)->v_flags & VIFF_TUNNEL) \
1455 encap_send((ip), (vifp), (m)); \
1457 phyint_send((ip), (vifp), (m)); \
1461 * If xmt_vif is not -1, send on only the requested vif.
1463 * (since vifi_t is u_short, -1 becomes MAXUSHORT, which > numvifs.)
1465 if (xmt_vif < numvifs) {
1466 MC_SEND(ip, viftable + xmt_vif, m);
1471 * Don't forward if it didn't arrive from the parent vif for its origin.
1473 vifi = rt->mfc_parent;
1474 if ((vifi >= numvifs) || (viftable[vifi].v_ifp != ifp)) {
1475 /* came in the wrong interface */
1476 if (mrtdebug & DEBUG_FORWARD)
1477 log(LOG_DEBUG, "wrong if: ifp %p vifi %d vififp %p\n",
1478 (void *)ifp, vifi, (void *)viftable[vifi].v_ifp);
1479 ++mrtstat.mrts_wrong_if;
1482 * If we are doing PIM assert processing, and we are forwarding
1483 * packets on this interface, and it is a broadcast medium
1484 * interface (and not a tunnel), send a message to the routing daemon.
1486 if (pim_assert && rt->mfc_ttls[vifi] &&
1487 (ifp->if_flags & IFF_BROADCAST) &&
1488 !(viftable[vifi].v_flags & VIFF_TUNNEL)) {
1489 struct sockaddr_in k_igmpsrc;
1492 int hlen = ip->ip_hl << 2;
1494 register u_long delta;
1498 TV_DELTA(rt->mfc_last_assert, now, delta);
1500 if (delta > ASSERT_MSG_TIME) {
1501 mm = m_copy(m, 0, hlen);
1502 if (mm && (M_HASCL(mm) || mm->m_len < hlen))
1503 mm = m_pullup(mm, hlen);
1508 rt->mfc_last_assert = now;
1510 im = mtod(mm, struct igmpmsg *);
1511 im->im_msgtype = IGMPMSG_WRONGVIF;
1515 k_igmpsrc.sin_addr = im->im_src;
1517 socket_send(ip_mrouter, mm, &k_igmpsrc);
1523 /* If I sourced this packet, it counts as output, else it was input. */
1524 if (ip->ip_src.s_addr == viftable[vifi].v_lcl_addr.s_addr) {
1525 viftable[vifi].v_pkt_out++;
1526 viftable[vifi].v_bytes_out += plen;
1528 viftable[vifi].v_pkt_in++;
1529 viftable[vifi].v_bytes_in += plen;
1532 rt->mfc_byte_cnt += plen;
1535 * For each vif, decide if a copy of the packet should be forwarded.
1537 * - the ttl exceeds the vif's threshold
1538 * - there are group members downstream on interface
1540 for (vifp = viftable, vifi = 0; vifi < numvifs; vifp++, vifi++)
1541 if ((rt->mfc_ttls[vifi] > 0) &&
1542 (ip->ip_ttl > rt->mfc_ttls[vifi])) {
1544 vifp->v_bytes_out += plen;
1545 MC_SEND(ip, vifp, m);
1552 * check if a vif number is legal/ok. This is used by ip_output, to export
1556 X_legal_vif_num(vif)
1559 if (vif >= 0 && vif < numvifs)
1566 int (*legal_vif_num)(int) = X_legal_vif_num;
1570 * Return the local address used by this vif
1573 X_ip_mcast_src(vifi)
1576 if (vifi >= 0 && vifi < numvifs)
1577 return viftable[vifi].v_lcl_addr.s_addr;
1583 u_long (*ip_mcast_src)(int) = X_ip_mcast_src;
1587 phyint_send(ip, vifp, m)
1592 register struct mbuf *mb_copy;
1593 register int hlen = ip->ip_hl << 2;
1596 * Make a new reference to the packet; make sure that
1597 * the IP header is actually copied, not just referenced,
1598 * so that ip_output() only scribbles on the copy.
1600 mb_copy = m_copy(m, 0, M_COPYALL);
1601 if (mb_copy && (M_HASCL(mb_copy) || mb_copy->m_len < hlen))
1602 mb_copy = m_pullup(mb_copy, hlen);
1603 if (mb_copy == NULL)
1606 if (vifp->v_rate_limit == 0)
1607 tbf_send_packet(vifp, mb_copy);
1609 tbf_control(vifp, mb_copy, mtod(mb_copy, struct ip *), ip->ip_len);
1613 encap_send(ip, vifp, m)
1614 register struct ip *ip;
1615 register struct vif *vifp;
1616 register struct mbuf *m;
1618 register struct mbuf *mb_copy;
1619 register struct ip *ip_copy;
1620 register int i, len = ip->ip_len;
1623 * copy the old packet & pullup its IP header into the
1624 * new mbuf so we can modify it. Try to fill the new
1625 * mbuf since if we don't the ethernet driver will.
1627 MGETHDR(mb_copy, M_DONTWAIT, MT_HEADER);
1628 if (mb_copy == NULL)
1630 mb_copy->m_data += max_linkhdr;
1631 mb_copy->m_len = sizeof(multicast_encap_iphdr);
1633 if ((mb_copy->m_next = m_copy(m, 0, M_COPYALL)) == NULL) {
1637 i = MHLEN - M_LEADINGSPACE(mb_copy);
1640 mb_copy = m_pullup(mb_copy, i);
1641 if (mb_copy == NULL)
1643 mb_copy->m_pkthdr.len = len + sizeof(multicast_encap_iphdr);
1646 * fill in the encapsulating IP header.
1648 ip_copy = mtod(mb_copy, struct ip *);
1649 *ip_copy = multicast_encap_iphdr;
1651 ip_copy->ip_id = ip_randomid();
1653 ip_copy->ip_id = htons(ip_id++);
1655 ip_copy->ip_len += len;
1656 ip_copy->ip_src = vifp->v_lcl_addr;
1657 ip_copy->ip_dst = vifp->v_rmt_addr;
1660 * turn the encapsulated IP header back into a valid one.
1662 ip = (struct ip *)((caddr_t)ip_copy + sizeof(multicast_encap_iphdr));
1664 ip->ip_len = htons(ip->ip_len);
1665 ip->ip_off = htons(ip->ip_off);
1667 mb_copy->m_data += sizeof(multicast_encap_iphdr);
1668 ip->ip_sum = in_cksum(mb_copy, ip->ip_hl << 2);
1669 mb_copy->m_data -= sizeof(multicast_encap_iphdr);
1671 if (vifp->v_rate_limit == 0)
1672 tbf_send_packet(vifp, mb_copy);
1674 tbf_control(vifp, mb_copy, ip, ip_copy->ip_len);
1678 * Token bucket filter module
1682 tbf_control(vifp, m, ip, p_len)
1683 register struct vif *vifp;
1684 register struct mbuf *m;
1685 register struct ip *ip;
1686 register u_long p_len;
1688 register struct tbf *t = vifp->v_tbf;
1690 if (p_len > MAX_BKT_SIZE) {
1691 /* drop if packet is too large */
1692 mrtstat.mrts_pkt2large++;
1697 tbf_update_tokens(vifp);
1699 /* if there are enough tokens,
1700 * and the queue is empty,
1701 * send this packet out
1704 if (t->tbf_q_len == 0) {
1705 /* queue empty, send packet if enough tokens */
1706 if (p_len <= t->tbf_n_tok) {
1707 t->tbf_n_tok -= p_len;
1708 tbf_send_packet(vifp, m);
1710 /* queue packet and timeout till later */
1712 timeout(tbf_reprocess_q, (caddr_t)vifp, TBF_REPROCESS);
1714 } else if (t->tbf_q_len < t->tbf_max_q_len) {
1715 /* finite queue length, so queue pkts and process queue */
1717 tbf_process_q(vifp);
1719 /* queue length too much, try to dq and queue and process */
1720 if (!tbf_dq_sel(vifp, ip)) {
1721 mrtstat.mrts_q_overflow++;
1726 tbf_process_q(vifp);
1733 * adds a packet to the queue at the interface
1737 register struct vif *vifp;
1738 register struct mbuf *m;
1740 register int s = splnet();
1741 register struct tbf *t = vifp->v_tbf;
1743 if (t->tbf_t == NULL) {
1744 /* Queue was empty */
1747 /* Insert at tail */
1748 t->tbf_t->m_act = m;
1751 /* Set new tail pointer */
1755 /* Make sure we didn't get fed a bogus mbuf */
1757 panic("tbf_queue: m_act");
1768 * processes the queue at the interface
1772 register struct vif *vifp;
1774 register struct mbuf *m;
1776 register int s = splnet();
1777 register struct tbf *t = vifp->v_tbf;
1779 /* loop through the queue at the interface and send as many packets
1782 while (t->tbf_q_len > 0) {
1785 len = mtod(m, struct ip *)->ip_len;
1787 /* determine if the packet can be sent */
1788 if (len <= t->tbf_n_tok) {
1790 * reduce no of tokens, dequeue the packet,
1793 t->tbf_n_tok -= len;
1795 t->tbf_q = m->m_act;
1796 if (--t->tbf_q_len == 0)
1800 tbf_send_packet(vifp, m);
1808 tbf_reprocess_q(xvifp)
1811 register struct vif *vifp = xvifp;
1812 if (ip_mrouter == NULL)
1815 tbf_update_tokens(vifp);
1817 tbf_process_q(vifp);
1819 if (vifp->v_tbf->tbf_q_len)
1820 timeout(tbf_reprocess_q, (caddr_t)vifp, TBF_REPROCESS);
1823 /* function that will selectively discard a member of the queue
1824 * based on the precedence value and the priority
1827 tbf_dq_sel(vifp, ip)
1828 register struct vif *vifp;
1829 register struct ip *ip;
1831 register int s = splnet();
1833 register struct mbuf *m, *last;
1834 register struct mbuf **np;
1835 register struct tbf *t = vifp->v_tbf;
1837 p = priority(vifp, ip);
1841 while ((m = *np) != NULL) {
1842 if (p > priority(vifp, mtod(m, struct ip *))) {
1844 /* If we're removing the last packet, fix the tail pointer */
1848 /* it's impossible for the queue to be empty, but
1849 * we check anyway. */
1850 if (--t->tbf_q_len == 0)
1853 mrtstat.mrts_drop_sel++;
1864 tbf_send_packet(vifp, m)
1865 register struct vif *vifp;
1866 register struct mbuf *m;
1868 struct ip_moptions imo;
1870 static struct route ro;
1873 if (vifp->v_flags & VIFF_TUNNEL) {
1874 /* If tunnel options */
1875 ip_output(m, (struct mbuf *)0, &vifp->v_route,
1876 IP_FORWARDING, (struct ip_moptions *)0);
1878 imo.imo_multicast_ifp = vifp->v_ifp;
1879 imo.imo_multicast_ttl = mtod(m, struct ip *)->ip_ttl - 1;
1880 imo.imo_multicast_loop = 1;
1881 imo.imo_multicast_vif = -1;
1884 * Re-entrancy should not be a problem here, because
1885 * the packets that we send out and are looped back at us
1886 * should get rejected because they appear to come from
1887 * the loopback interface, thus preventing looping.
1889 error = ip_output(m, (struct mbuf *)0, &ro,
1890 IP_FORWARDING, &imo);
1892 if (mrtdebug & DEBUG_XMIT)
1893 log(LOG_DEBUG, "phyint_send on vif %d err %d\n",
1894 vifp - viftable, error);
1899 /* determine the current time and then
1900 * the elapsed time (between the last time and time now)
1901 * in milliseconds & update the no. of tokens in the bucket
1904 tbf_update_tokens(vifp)
1905 register struct vif *vifp;
1909 register int s = splnet();
1910 register struct tbf *t = vifp->v_tbf;
1914 TV_DELTA(tp, t->tbf_last_pkt_t, tm);
1917 * This formula is actually
1918 * "time in seconds" * "bytes/second".
1920 * (tm / 1000000) * (v_rate_limit * 1000 * (1000/1024) / 8)
1922 * The (1000/1024) was introduced in add_vif to optimize
1923 * this divide into a shift.
1925 t->tbf_n_tok += tm * vifp->v_rate_limit / 1024 / 8;
1926 t->tbf_last_pkt_t = tp;
1928 if (t->tbf_n_tok > MAX_BKT_SIZE)
1929 t->tbf_n_tok = MAX_BKT_SIZE;
1936 register struct vif *vifp;
1937 register struct ip *ip;
1941 /* temporary hack; may add general packet classifier some day */
1944 * The UDP port space is divided up into four priority ranges:
1945 * [0, 16384) : unclassified - lowest priority
1946 * [16384, 32768) : audio - highest priority
1947 * [32768, 49152) : whiteboard - medium priority
1948 * [49152, 65536) : video - low priority
1950 if (ip->ip_p == IPPROTO_UDP) {
1951 struct udphdr *udp = (struct udphdr *)(((char *)ip) + (ip->ip_hl << 2));
1952 switch (ntohs(udp->uh_dport) & 0xc000) {
1967 log(LOG_DEBUG, "port %x prio%d\n", ntohs(udp->uh_dport), prio);
1975 * End of token bucket filter modifications
1979 ip_rsvp_vif_init(so, sopt)
1981 struct sockopt *sopt;
1986 printf("ip_rsvp_vif_init: so_type = %d, pr_protocol = %d\n",
1987 so->so_type, so->so_proto->pr_protocol);
1989 if (so->so_type != SOCK_RAW || so->so_proto->pr_protocol != IPPROTO_RSVP)
1993 error = sooptcopyin(sopt, &i, sizeof i, sizeof i);
1998 printf("ip_rsvp_vif_init: vif = %d rsvp_on = %d\n", i, rsvp_on);
2003 if (!legal_vif_num(i)) {
2005 return EADDRNOTAVAIL;
2008 /* Check if socket is available. */
2009 if (viftable[i].v_rsvpd != NULL) {
2014 viftable[i].v_rsvpd = so;
2015 /* This may seem silly, but we need to be sure we don't over-increment
2016 * the RSVP counter, in case something slips up.
2018 if (!viftable[i].v_rsvp_on) {
2019 viftable[i].v_rsvp_on = 1;
2028 ip_rsvp_vif_done(so, sopt)
2030 struct sockopt *sopt;
2035 printf("ip_rsvp_vif_done: so_type = %d, pr_protocol = %d\n",
2036 so->so_type, so->so_proto->pr_protocol);
2038 if (so->so_type != SOCK_RAW ||
2039 so->so_proto->pr_protocol != IPPROTO_RSVP)
2042 error = sooptcopyin(sopt, &i, sizeof i, sizeof i);
2049 if (!legal_vif_num(i)) {
2051 return EADDRNOTAVAIL;
2055 printf("ip_rsvp_vif_done: v_rsvpd = %p so = %p\n",
2056 viftable[i].v_rsvpd, so);
2058 viftable[i].v_rsvpd = NULL;
2060 * This may seem silly, but we need to be sure we don't over-decrement
2061 * the RSVP counter, in case something slips up.
2063 if (viftable[i].v_rsvp_on) {
2064 viftable[i].v_rsvp_on = 0;
2073 ip_rsvp_force_done(so)
2079 /* Don't bother if it is not the right type of socket. */
2080 if (so->so_type != SOCK_RAW || so->so_proto->pr_protocol != IPPROTO_RSVP)
2085 /* The socket may be attached to more than one vif...this
2086 * is perfectly legal.
2088 for (vifi = 0; vifi < numvifs; vifi++) {
2089 if (viftable[vifi].v_rsvpd == so) {
2090 viftable[vifi].v_rsvpd = NULL;
2091 /* This may seem silly, but we need to be sure we don't
2092 * over-decrement the RSVP counter, in case something slips up.
2094 if (viftable[vifi].v_rsvp_on) {
2095 viftable[vifi].v_rsvp_on = 0;
2111 register struct ip *ip = mtod(m, struct ip *);
2112 static struct sockaddr_in rsvp_src = { sizeof rsvp_src, AF_INET };
2117 printf("rsvp_input: rsvp_on %d\n",rsvp_on);
2119 /* Can still get packets with rsvp_on = 0 if there is a local member
2120 * of the group to which the RSVP packet is addressed. But in this
2121 * case we want to throw the packet away.
2131 printf("rsvp_input: check vifs\n");
2134 if (!(m->m_flags & M_PKTHDR))
2135 panic("rsvp_input no hdr");
2138 ifp = m->m_pkthdr.rcvif;
2139 /* Find which vif the packet arrived on. */
2140 for (vifi = 0; vifi < numvifs; vifi++)
2141 if (viftable[vifi].v_ifp == ifp)
2144 if (vifi == numvifs || viftable[vifi].v_rsvpd == NULL) {
2146 * If the old-style non-vif-associated socket is set,
2147 * then use it. Otherwise, drop packet since there
2148 * is no specific socket for this vif.
2150 if (ip_rsvpd != NULL) {
2152 printf("rsvp_input: Sending packet up old-style socket\n");
2153 rip_input(m, off); /* xxx */
2155 if (rsvpdebug && vifi == numvifs)
2156 printf("rsvp_input: Can't find vif for packet.\n");
2157 else if (rsvpdebug && viftable[vifi].v_rsvpd == NULL)
2158 printf("rsvp_input: No socket defined for vif %d\n",vifi);
2164 rsvp_src.sin_addr = ip->ip_src;
2167 printf("rsvp_input: m->m_len = %d, sbspace() = %ld\n",
2168 m->m_len,sbspace(&(viftable[vifi].v_rsvpd->so_rcv)));
2170 if (socket_send(viftable[vifi].v_rsvpd, m, &rsvp_src) < 0) {
2172 printf("rsvp_input: Failed to append to socket\n");
2175 printf("rsvp_input: send packet up\n");
2184 ip_mroute_modevent(module_t mod, int type, void *unused)
2189 static u_long (*old_ip_mcast_src)(int);
2190 static int (*old_ip_mrouter_set)(struct socket *,
2192 static int (*old_ip_mrouter_get)(struct socket *,
2194 static int (*old_ip_mrouter_done)(void);
2195 static int (*old_ip_mforward)(struct ip *, struct ifnet *,
2196 struct mbuf *, struct ip_moptions *);
2197 static int (*old_mrt_ioctl)(int, caddr_t);
2198 static int (*old_legal_vif_num)(int);
2202 /* XXX Protect against multiple loading */
2203 old_ip_mcast_src = ip_mcast_src;
2204 ip_mcast_src = X_ip_mcast_src;
2205 old_ip_mrouter_get = ip_mrouter_get;
2206 ip_mrouter_get = X_ip_mrouter_get;
2207 old_ip_mrouter_set = ip_mrouter_set;
2208 ip_mrouter_set = X_ip_mrouter_set;
2209 old_ip_mrouter_done = ip_mrouter_done;
2210 ip_mrouter_done = X_ip_mrouter_done;
2211 old_ip_mforward = ip_mforward;
2212 ip_mforward = X_ip_mforward;
2213 old_mrt_ioctl = mrt_ioctl;
2214 mrt_ioctl = X_mrt_ioctl;
2215 old_legal_vif_num = legal_vif_num;
2216 legal_vif_num = X_legal_vif_num;
2226 ip_mrouter_get = old_ip_mrouter_get;
2227 ip_mrouter_set = old_ip_mrouter_set;
2228 ip_mrouter_done = old_ip_mrouter_done;
2229 ip_mforward = old_ip_mforward;
2230 mrt_ioctl = old_mrt_ioctl;
2231 legal_vif_num = old_legal_vif_num;
2241 static moduledata_t ip_mroutemod = {
2246 DECLARE_MODULE(ip_mroute, ip_mroutemod, SI_SUB_PSEUDO, SI_ORDER_ANY);
2248 #endif /* MROUTE_KLD */
2249 #endif /* MROUTING */