1 //==========================================================================
3 // src/sys/netinet/ip_mroute.c
5 //==========================================================================
6 //####BSDCOPYRIGHTBEGIN####
8 // -------------------------------------------
10 // Portions of this software may have been derived from OpenBSD,
11 // FreeBSD or other sources, and are covered by the appropriate
12 // copyright disclaimers included herein.
14 // Portions created by Red Hat are
15 // Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
17 // -------------------------------------------
19 //####BSDCOPYRIGHTEND####
20 //==========================================================================
23 * IP multicast forwarding procedures
25 * Written by David Waitzman, BBN Labs, August 1988.
26 * Modified by Steve Deering, Stanford, February 1989.
27 * Modified by Mark J. Steiglitz, Stanford, May, 1991
28 * Modified by Van Jacobson, LBL, January 1993
29 * Modified by Ajit Thyagarajan, PARC, August 1993
30 * Modified by Bill Fenner, PARC, April 1995
32 * MROUTING Revision: 3.5
33 * $FreeBSD: src/sys/netinet/ip_mroute.c,v 1.56.2.2 2001/07/19 06:37:26 kris Exp $
36 #include <sys/param.h>
37 #include <sys/malloc.h>
39 #include <sys/socket.h>
40 #include <sys/socketvar.h>
41 #include <sys/protosw.h>
42 #include <sys/sockio.h>
44 #include <net/route.h>
45 #include <netinet/in.h>
46 #include <netinet/in_systm.h>
47 #include <netinet/in_pcb.h>
48 #include <netinet/ip.h>
49 #include <netinet/ip_var.h>
50 #include <netinet/in_var.h>
51 #include <netinet/igmp.h>
52 #include <netinet/ip_mroute.h>
53 #include <netinet/udp.h>
56 #if BYTE_ORDER != BIG_ENDIAN
57 #define NTOHL(d) ((d) = ntohl((d)))
58 #define NTOHS(d) ((d) = ntohs((u_short)(d)))
59 #define HTONL(d) ((d) = htonl((d)))
60 #define HTONS(d) ((d) = htons((u_short)(d)))
70 extern u_long _ip_mcast_src __P((int vifi));
71 extern int _ip_mforward __P((struct ip *ip, struct ifnet *ifp,
72 struct mbuf *m, struct ip_moptions *imo));
73 extern int _ip_mrouter_done __P((void));
74 extern int _ip_mrouter_get __P((struct socket *so, struct sockopt *sopt));
75 extern int _ip_mrouter_set __P((struct socket *so, struct sockopt *sopt));
76 extern int _mrt_ioctl __P((int req, caddr_t data, struct proc *p));
79 * Dummy routines and globals used when multicast routing is not compiled in.
82 struct socket *ip_mrouter = NULL;
86 _ip_mrouter_set(so, sopt)
93 int (*ip_mrouter_set)(struct socket *, struct sockopt *) = _ip_mrouter_set;
97 _ip_mrouter_get(so, sopt)
104 int (*ip_mrouter_get)(struct socket *, struct sockopt *) = _ip_mrouter_get;
112 int (*ip_mrouter_done)(void) = _ip_mrouter_done;
115 _ip_mforward(ip, ifp, m, imo)
119 struct ip_moptions *imo;
124 int (*ip_mforward)(struct ip *, struct ifnet *, struct mbuf *,
125 struct ip_moptions *) = _ip_mforward;
128 _mrt_ioctl(int req, caddr_t data, struct proc *p)
133 int (*mrt_ioctl)(int, caddr_t, struct proc *) = _mrt_ioctl;
136 rsvp_input(m, off) /* XXX must fixup manually */
140 /* Can still get packets with rsvp_on = 0 if there is a local member
141 * of the group to which the RSVP packet is addressed. But in this
142 * case we want to throw the packet away.
149 if (ip_rsvpd != NULL) {
151 printf("rsvp_input: Sending packet up old-style socket\n");
155 /* Drop the packet */
159 void ipip_input(struct mbuf *m, int off) { /* XXX must fixup manually */
163 int (*legal_vif_num)(int) = 0;
166 * This should never be called, since IP_MULTICAST_VIF should fail, but
167 * just in case it does get called, the code a little lower in ip_output
168 * will assign the packet a local address.
171 _ip_mcast_src(int vifi) { return INADDR_ANY; }
172 u_long (*ip_mcast_src)(int) = _ip_mcast_src;
175 ip_rsvp_vif_init(so, sopt)
177 struct sockopt *sopt;
183 ip_rsvp_vif_done(so, sopt)
185 struct sockopt *sopt;
191 ip_rsvp_force_done(so)
199 #define M_HASCL(m) ((m)->m_flags & M_EXT)
201 #define INSIZ sizeof(struct in_addr)
202 #define same(a1, a2) \
203 (bcmp((caddr_t)(a1), (caddr_t)(a2), INSIZ) == 0)
206 * Globals. All but ip_mrouter and ip_mrtproto could be static,
207 * except for netstat or debugging purposes.
210 struct socket *ip_mrouter = NULL;
211 static struct mrtstat mrtstat;
212 #else /* MROUTE_LKM */
213 extern void X_ipip_input __P((struct mbuf *m, int iphlen));
214 extern struct mrtstat mrtstat;
215 static int ip_mrtproto;
218 #define NO_RTE_FOUND 0x1
219 #define RTE_FOUND 0x2
221 static struct mfc *mfctable[MFCTBLSIZ];
222 static u_char nexpire[MFCTBLSIZ];
223 static struct vif viftable[MAXVIFS];
224 static u_int mrtdebug = 0; /* debug level */
225 #define DEBUG_MFC 0x02
226 #define DEBUG_FORWARD 0x04
227 #define DEBUG_EXPIRE 0x08
228 #define DEBUG_XMIT 0x10
229 static u_int tbfdebug = 0; /* tbf debug level */
230 static u_int rsvpdebug = 0; /* rsvp debug level */
232 static struct callout_handle expire_upcalls_ch;
234 #define EXPIRE_TIMEOUT (hz / 4) /* 4x / second */
235 #define UPCALL_EXPIRE 6 /* number of timeouts */
238 * Define the token bucket filter structures
239 * tbftable -> each vif has one of these for storing info
242 static struct tbf tbftable[MAXVIFS];
243 #define TBF_REPROCESS (hz / 100) /* 100x / second */
246 * 'Interfaces' associated with decapsulator (so we can tell
247 * packets that went through it from ones that get reflected
248 * by a broken gateway). These interfaces are never linked into
249 * the system ifnet list & no routes point to them. I.e., packets
250 * can't be sent this way. They only exist as a placeholder for
251 * multicast source verification.
253 static struct ifnet multicast_decap_if[MAXVIFS];
256 #define ENCAP_PROTO IPPROTO_IPIP /* 4 */
258 /* prototype IP hdr for encapsulated packets */
259 static struct ip multicast_encap_iphdr = {
260 #if BYTE_ORDER == LITTLE_ENDIAN
261 sizeof(struct ip) >> 2, IPVERSION,
263 IPVERSION, sizeof(struct ip) >> 2,
266 sizeof(struct ip), /* total length */
269 ENCAP_TTL, ENCAP_PROTO,
276 static vifi_t numvifs = 0;
277 static int have_encap_tunnel = 0;
280 * one-back cache used by ipip_input to locate a tunnel's vif
281 * given a datagram's src ip address.
283 static u_long last_encap_src;
284 static struct vif *last_encap_vif;
286 static u_long X_ip_mcast_src __P((int vifi));
287 static int X_ip_mforward __P((struct ip *ip, struct ifnet *ifp, struct mbuf *m, struct ip_moptions *imo));
288 static int X_ip_mrouter_done __P((void));
289 static int X_ip_mrouter_get __P((struct socket *so, struct sockopt *m));
290 static int X_ip_mrouter_set __P((struct socket *so, struct sockopt *m));
291 static int X_legal_vif_num __P((int vif));
292 static int X_mrt_ioctl __P((int cmd, caddr_t data));
294 static int get_sg_cnt(struct sioc_sg_req *);
295 static int get_vif_cnt(struct sioc_vif_req *);
296 static int ip_mrouter_init(struct socket *, int);
297 static int add_vif(struct vifctl *);
298 static int del_vif(vifi_t);
299 static int add_mfc(struct mfcctl *);
300 static int del_mfc(struct mfcctl *);
301 static int socket_send(struct socket *, struct mbuf *, struct sockaddr_in *);
302 static int set_assert(int);
303 static void expire_upcalls(void *);
304 static int ip_mdq(struct mbuf *, struct ifnet *, struct mfc *,
306 static void phyint_send(struct ip *, struct vif *, struct mbuf *);
307 static void encap_send(struct ip *, struct vif *, struct mbuf *);
308 static void tbf_control(struct vif *, struct mbuf *, struct ip *, u_long);
309 static void tbf_queue(struct vif *, struct mbuf *);
310 static void tbf_process_q(struct vif *);
311 static void tbf_reprocess_q(void *);
312 static int tbf_dq_sel(struct vif *, struct ip *);
313 static void tbf_send_packet(struct vif *, struct mbuf *);
314 static void tbf_update_tokens(struct vif *);
315 static int priority(struct vif *, struct ip *);
316 void multiencap_decap(struct mbuf *);
319 * whether or not special PIM assert processing is enabled.
321 static int pim_assert;
323 * Rate limit for assert notification messages, in usec
325 #define ASSERT_MSG_TIME 3000000
328 * Hash function for a source, group entry
330 #define MFCHASH(a, g) MFCHASHMOD(((a) >> 20) ^ ((a) >> 10) ^ (a) ^ \
331 ((g) >> 20) ^ ((g) >> 10) ^ (g))
334 * Find a route for a given origin IP address and Multicast group address
335 * Type of service parameter to be added in the future!!!
338 #define MFCFIND(o, g, rt) { \
339 register struct mfc *_rt = mfctable[MFCHASH(o,g)]; \
341 ++mrtstat.mrts_mfc_lookups; \
343 if ((_rt->mfc_origin.s_addr == o) && \
344 (_rt->mfc_mcastgrp.s_addr == g) && \
345 (_rt->mfc_stall == NULL)) { \
349 _rt = _rt->mfc_next; \
352 ++mrtstat.mrts_mfc_misses; \
358 * Macros to compute elapsed time efficiently
359 * Borrowed from Van Jacobson's scheduling code
361 #define TV_DELTA(a, b, delta) { \
364 delta = (a).tv_usec - (b).tv_usec; \
365 if ((xxs = (a).tv_sec - (b).tv_sec)) { \
374 delta += (1000000 * xxs); \
379 #define TV_LT(a, b) (((a).tv_usec < (b).tv_usec && \
380 (a).tv_sec <= (b).tv_sec) || (a).tv_sec < (b).tv_sec)
383 u_long upcall_data[51];
384 static void collate(struct timeval *);
385 #endif /* UPCALL_TIMING */
389 * Handle MRT setsockopt commands to modify the multicast routing tables.
392 X_ip_mrouter_set(so, sopt)
394 struct sockopt *sopt;
401 if (so != ip_mrouter && sopt->sopt_name != MRT_INIT)
405 switch (sopt->sopt_name) {
407 error = sooptcopyin(sopt, &optval, sizeof optval,
411 error = ip_mrouter_init(so, optval);
415 error = ip_mrouter_done();
419 error = sooptcopyin(sopt, &vifc, sizeof vifc, sizeof vifc);
422 error = add_vif(&vifc);
426 error = sooptcopyin(sopt, &vifi, sizeof vifi, sizeof vifi);
429 error = del_vif(vifi);
434 error = sooptcopyin(sopt, &mfc, sizeof mfc, sizeof mfc);
437 if (sopt->sopt_name == MRT_ADD_MFC)
438 error = add_mfc(&mfc);
440 error = del_mfc(&mfc);
444 error = sooptcopyin(sopt, &optval, sizeof optval,
459 int (*ip_mrouter_set)(struct socket *, struct sockopt *) = X_ip_mrouter_set;
463 * Handle MRT getsockopt commands
466 X_ip_mrouter_get(so, sopt)
468 struct sockopt *sopt;
471 static int version = 0x0305; /* !!! why is this here? XXX */
473 switch (sopt->sopt_name) {
475 error = sooptcopyout(sopt, &version, sizeof version);
479 error = sooptcopyout(sopt, &pim_assert, sizeof pim_assert);
489 int (*ip_mrouter_get)(struct socket *, struct sockopt *) = X_ip_mrouter_get;
493 * Handle ioctl commands to obtain information from the cache
496 X_mrt_ioctl(cmd, data)
503 case (SIOCGETVIFCNT):
504 return (get_vif_cnt((struct sioc_vif_req *)data));
507 return (get_sg_cnt((struct sioc_sg_req *)data));
517 int (*mrt_ioctl)(int, caddr_t) = X_mrt_ioctl;
521 * returns the packet, byte, rpf-failure count for the source group provided
525 register struct sioc_sg_req *req;
527 register struct mfc *rt;
531 MFCFIND(req->src.s_addr, req->grp.s_addr, rt);
534 req->pktcnt = rt->mfc_pkt_cnt;
535 req->bytecnt = rt->mfc_byte_cnt;
536 req->wrong_if = rt->mfc_wrong_if;
538 req->pktcnt = req->bytecnt = req->wrong_if = 0xffffffff;
544 * returns the input and output packet and byte counts on the vif provided
548 register struct sioc_vif_req *req;
550 register vifi_t vifi = req->vifi;
552 if (vifi >= numvifs) return EINVAL;
554 req->icount = viftable[vifi].v_pkt_in;
555 req->ocount = viftable[vifi].v_pkt_out;
556 req->ibytes = viftable[vifi].v_bytes_in;
557 req->obytes = viftable[vifi].v_bytes_out;
563 * Enable multicast routing
566 ip_mrouter_init(so, version)
571 log(LOG_DEBUG,"ip_mrouter_init: so_type = %d, pr_protocol = %d\n",
572 so->so_type, so->so_proto->pr_protocol);
574 if (so->so_type != SOCK_RAW ||
575 so->so_proto->pr_protocol != IPPROTO_IGMP) return EOPNOTSUPP;
580 if (ip_mrouter != NULL) return EADDRINUSE;
584 bzero((caddr_t)mfctable, sizeof(mfctable));
585 bzero((caddr_t)nexpire, sizeof(nexpire));
589 expire_upcalls_ch = timeout(expire_upcalls, (caddr_t)NULL, EXPIRE_TIMEOUT);
592 log(LOG_DEBUG, "ip_mrouter_init\n");
598 * Disable multicast routing
614 * For each phyint in use, disable promiscuous reception of all IP
617 for (vifi = 0; vifi < numvifs; vifi++) {
618 if (viftable[vifi].v_lcl_addr.s_addr != 0 &&
619 !(viftable[vifi].v_flags & VIFF_TUNNEL)) {
620 ((struct sockaddr_in *)&(ifr.ifr_addr))->sin_family = AF_INET;
621 ((struct sockaddr_in *)&(ifr.ifr_addr))->sin_addr.s_addr
623 ifp = viftable[vifi].v_ifp;
627 bzero((caddr_t)tbftable, sizeof(tbftable));
628 bzero((caddr_t)viftable, sizeof(viftable));
632 untimeout(expire_upcalls, (caddr_t)NULL, expire_upcalls_ch);
635 * Free all multicast forwarding cache entries.
637 for (i = 0; i < MFCTBLSIZ; i++) {
638 for (rt = mfctable[i]; rt != NULL; ) {
639 struct mfc *nr = rt->mfc_next;
641 for (rte = rt->mfc_stall; rte != NULL; ) {
642 struct rtdetq *n = rte->next;
645 free(rte, M_MRTABLE);
653 bzero((caddr_t)mfctable, sizeof(mfctable));
656 * Reset de-encapsulation cache
659 last_encap_vif = NULL;
660 have_encap_tunnel = 0;
667 log(LOG_DEBUG, "ip_mrouter_done\n");
673 int (*ip_mrouter_done)(void) = X_ip_mrouter_done;
677 * Set PIM assert processing global
683 if ((i != 1) && (i != 0))
692 * Add a vif to the vif table
696 register struct vifctl *vifcp;
698 register struct vif *vifp = viftable + vifcp->vifc_vifi;
699 static struct sockaddr_in sin = {sizeof sin, AF_INET};
703 struct tbf *v_tbf = tbftable + vifcp->vifc_vifi;
705 if (vifcp->vifc_vifi >= MAXVIFS) return EINVAL;
706 if (vifp->v_lcl_addr.s_addr != 0) return EADDRINUSE;
708 /* Find the interface with an address in AF_INET family */
709 sin.sin_addr = vifcp->vifc_lcl_addr;
710 ifa = ifa_ifwithaddr((struct sockaddr *)&sin);
711 if (ifa == 0) return EADDRNOTAVAIL;
714 if (vifcp->vifc_flags & VIFF_TUNNEL) {
715 if ((vifcp->vifc_flags & VIFF_SRCRT) == 0) {
717 * An encapsulating tunnel is wanted. Tell ipip_input() to
718 * start paying attention to encapsulated packets.
720 if (have_encap_tunnel == 0) {
721 have_encap_tunnel = 1;
722 for (s = 0; s < MAXVIFS; ++s) {
723 multicast_decap_if[s].if_name = "mdecap";
724 multicast_decap_if[s].if_unit = s;
728 * Set interface to fake encapsulator interface
730 ifp = &multicast_decap_if[vifcp->vifc_vifi];
732 * Prepare cached route entry
734 bzero(&vifp->v_route, sizeof(vifp->v_route));
736 log(LOG_ERR, "source routed tunnels not supported\n");
740 /* Make sure the interface supports multicast */
741 if ((ifp->if_flags & IFF_MULTICAST) == 0)
744 /* Enable promiscuous reception of all IP multicasts from the if */
746 error = if_allmulti(ifp, 1);
753 /* define parameters for the tbf structure */
755 GET_TIME(vifp->v_tbf->tbf_last_pkt_t);
756 vifp->v_tbf->tbf_n_tok = 0;
757 vifp->v_tbf->tbf_q_len = 0;
758 vifp->v_tbf->tbf_max_q_len = MAXQSIZE;
759 vifp->v_tbf->tbf_q = vifp->v_tbf->tbf_t = NULL;
761 vifp->v_flags = vifcp->vifc_flags;
762 vifp->v_threshold = vifcp->vifc_threshold;
763 vifp->v_lcl_addr = vifcp->vifc_lcl_addr;
764 vifp->v_rmt_addr = vifcp->vifc_rmt_addr;
766 /* scaling up here allows division by 1024 in critical code */
767 vifp->v_rate_limit= vifcp->vifc_rate_limit * 1024 / 1000;
769 vifp->v_rsvpd = NULL;
770 /* initialize per vif pkt counters */
773 vifp->v_bytes_in = 0;
774 vifp->v_bytes_out = 0;
777 /* Adjust numvifs up if the vifi is higher than numvifs */
778 if (numvifs <= vifcp->vifc_vifi) numvifs = vifcp->vifc_vifi + 1;
781 log(LOG_DEBUG, "add_vif #%d, lcladdr %lx, %s %lx, thresh %x, rate %d\n",
783 (u_long)ntohl(vifcp->vifc_lcl_addr.s_addr),
784 (vifcp->vifc_flags & VIFF_TUNNEL) ? "rmtaddr" : "mask",
785 (u_long)ntohl(vifcp->vifc_rmt_addr.s_addr),
786 vifcp->vifc_threshold,
787 vifcp->vifc_rate_limit);
793 * Delete a vif from the vif table
799 register struct vif *vifp = &viftable[vifi];
800 register struct mbuf *m;
805 if (vifi >= numvifs) return EINVAL;
806 if (vifp->v_lcl_addr.s_addr == 0) return EADDRNOTAVAIL;
810 if (!(vifp->v_flags & VIFF_TUNNEL)) {
811 ((struct sockaddr_in *)&(ifr.ifr_addr))->sin_family = AF_INET;
812 ((struct sockaddr_in *)&(ifr.ifr_addr))->sin_addr.s_addr = INADDR_ANY;
817 if (vifp == last_encap_vif) {
823 * Free packets queued at the interface
825 while (vifp->v_tbf->tbf_q) {
826 m = vifp->v_tbf->tbf_q;
827 vifp->v_tbf->tbf_q = m->m_act;
831 bzero((caddr_t)vifp->v_tbf, sizeof(*(vifp->v_tbf)));
832 bzero((caddr_t)vifp, sizeof (*vifp));
835 log(LOG_DEBUG, "del_vif %d, numvifs %d\n", vifi, numvifs);
837 /* Adjust numvifs down */
838 for (vifi = numvifs; vifi > 0; vifi--)
839 if (viftable[vifi-1].v_lcl_addr.s_addr != 0) break;
852 struct mfcctl *mfccp;
857 register u_short nstl;
861 MFCFIND(mfccp->mfcc_origin.s_addr, mfccp->mfcc_mcastgrp.s_addr, rt);
863 /* If an entry already exists, just update the fields */
865 if (mrtdebug & DEBUG_MFC)
866 log(LOG_DEBUG,"add_mfc update o %lx g %lx p %x\n",
867 (u_long)ntohl(mfccp->mfcc_origin.s_addr),
868 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
872 rt->mfc_parent = mfccp->mfcc_parent;
873 for (i = 0; i < numvifs; i++)
874 rt->mfc_ttls[i] = mfccp->mfcc_ttls[i];
880 * Find the entry for which the upcall was made and update
883 hash = MFCHASH(mfccp->mfcc_origin.s_addr, mfccp->mfcc_mcastgrp.s_addr);
884 for (rt = mfctable[hash], nstl = 0; rt; rt = rt->mfc_next) {
886 if ((rt->mfc_origin.s_addr == mfccp->mfcc_origin.s_addr) &&
887 (rt->mfc_mcastgrp.s_addr == mfccp->mfcc_mcastgrp.s_addr) &&
888 (rt->mfc_stall != NULL)) {
891 log(LOG_ERR, "add_mfc %s o %lx g %lx p %x dbx %p\n",
892 "multiple kernel entries",
893 (u_long)ntohl(mfccp->mfcc_origin.s_addr),
894 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
895 mfccp->mfcc_parent, (void *)rt->mfc_stall);
897 if (mrtdebug & DEBUG_MFC)
898 log(LOG_DEBUG,"add_mfc o %lx g %lx p %x dbg %p\n",
899 (u_long)ntohl(mfccp->mfcc_origin.s_addr),
900 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
901 mfccp->mfcc_parent, (void *)rt->mfc_stall);
903 rt->mfc_origin = mfccp->mfcc_origin;
904 rt->mfc_mcastgrp = mfccp->mfcc_mcastgrp;
905 rt->mfc_parent = mfccp->mfcc_parent;
906 for (i = 0; i < numvifs; i++)
907 rt->mfc_ttls[i] = mfccp->mfcc_ttls[i];
908 /* initialize pkt counters per src-grp */
910 rt->mfc_byte_cnt = 0;
911 rt->mfc_wrong_if = 0;
912 rt->mfc_last_assert.tv_sec = rt->mfc_last_assert.tv_usec = 0;
914 rt->mfc_expire = 0; /* Don't clean this guy up */
917 /* free packets Qed at the end of this entry */
918 for (rte = rt->mfc_stall; rte != NULL; ) {
919 struct rtdetq *n = rte->next;
921 ip_mdq(rte->m, rte->ifp, rt, -1);
925 #endif /* UPCALL_TIMING */
926 free(rte, M_MRTABLE);
929 rt->mfc_stall = NULL;
934 * It is possible that an entry is being inserted without an upcall
937 if (mrtdebug & DEBUG_MFC)
938 log(LOG_DEBUG,"add_mfc no upcall h %lu o %lx g %lx p %x\n",
939 hash, (u_long)ntohl(mfccp->mfcc_origin.s_addr),
940 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
943 for (rt = mfctable[hash]; rt != NULL; rt = rt->mfc_next) {
945 if ((rt->mfc_origin.s_addr == mfccp->mfcc_origin.s_addr) &&
946 (rt->mfc_mcastgrp.s_addr == mfccp->mfcc_mcastgrp.s_addr)) {
948 rt->mfc_origin = mfccp->mfcc_origin;
949 rt->mfc_mcastgrp = mfccp->mfcc_mcastgrp;
950 rt->mfc_parent = mfccp->mfcc_parent;
951 for (i = 0; i < numvifs; i++)
952 rt->mfc_ttls[i] = mfccp->mfcc_ttls[i];
953 /* initialize pkt counters per src-grp */
955 rt->mfc_byte_cnt = 0;
956 rt->mfc_wrong_if = 0;
957 rt->mfc_last_assert.tv_sec = rt->mfc_last_assert.tv_usec = 0;
964 /* no upcall, so make a new entry */
965 rt = (struct mfc *)malloc(sizeof(*rt), M_MRTABLE, M_NOWAIT);
971 /* insert new entry at head of hash chain */
972 rt->mfc_origin = mfccp->mfcc_origin;
973 rt->mfc_mcastgrp = mfccp->mfcc_mcastgrp;
974 rt->mfc_parent = mfccp->mfcc_parent;
975 for (i = 0; i < numvifs; i++)
976 rt->mfc_ttls[i] = mfccp->mfcc_ttls[i];
977 /* initialize pkt counters per src-grp */
979 rt->mfc_byte_cnt = 0;
980 rt->mfc_wrong_if = 0;
981 rt->mfc_last_assert.tv_sec = rt->mfc_last_assert.tv_usec = 0;
983 rt->mfc_stall = NULL;
985 /* link into table */
986 rt->mfc_next = mfctable[hash];
996 * collect delay statistics on the upcalls
998 static void collate(t)
999 register struct timeval *t;
1002 register struct timeval tp;
1003 register u_long delta;
1009 TV_DELTA(tp, *t, delta);
1018 #endif /* UPCALL_TIMING */
1021 * Delete an mfc entry
1025 struct mfcctl *mfccp;
1027 struct in_addr origin;
1028 struct in_addr mcastgrp;
1034 origin = mfccp->mfcc_origin;
1035 mcastgrp = mfccp->mfcc_mcastgrp;
1036 hash = MFCHASH(origin.s_addr, mcastgrp.s_addr);
1038 if (mrtdebug & DEBUG_MFC)
1039 log(LOG_DEBUG,"del_mfc orig %lx mcastgrp %lx\n",
1040 (u_long)ntohl(origin.s_addr), (u_long)ntohl(mcastgrp.s_addr));
1044 nptr = &mfctable[hash];
1045 while ((rt = *nptr) != NULL) {
1046 if (origin.s_addr == rt->mfc_origin.s_addr &&
1047 mcastgrp.s_addr == rt->mfc_mcastgrp.s_addr &&
1048 rt->mfc_stall == NULL)
1051 nptr = &rt->mfc_next;
1055 return EADDRNOTAVAIL;
1058 *nptr = rt->mfc_next;
1059 free(rt, M_MRTABLE);
1067 * Send a message to mrouted on the multicast routing socket
1070 socket_send(s, mm, src)
1073 struct sockaddr_in *src;
1076 if (sbappendaddr(&s->so_rcv,
1077 (struct sockaddr *)src,
1078 mm, (struct mbuf *)0) != 0) {
1088 * IP multicast forwarding function. This function assumes that the packet
1089 * pointed to by "ip" has arrived on (or is about to be sent to) the interface
1090 * pointed to by "ifp", and the packet is to be relayed to other networks
1091 * that have members of the packet's destination IP multicast group.
1093 * The packet is returned unscathed to the caller, unless it is
1094 * erroneous, in which case a non-zero return value tells the caller to
1098 #define IP_HDR_LEN 20 /* # bytes of fixed IP header (excluding options) */
1099 #define TUNNEL_LEN 12 /* # bytes of IP option for tunnel encapsulation */
1102 X_ip_mforward(ip, ifp, m, imo)
1103 register struct ip *ip;
1106 struct ip_moptions *imo;
1108 register struct mfc *rt;
1109 register u_char *ipoptions;
1110 static struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
1111 static int srctun = 0;
1112 register struct mbuf *mm;
1117 if (mrtdebug & DEBUG_FORWARD)
1118 log(LOG_DEBUG, "ip_mforward: src %lx, dst %lx, ifp %p\n",
1119 (u_long)ntohl(ip->ip_src.s_addr), (u_long)ntohl(ip->ip_dst.s_addr),
1122 if (ip->ip_hl < (IP_HDR_LEN + TUNNEL_LEN) >> 2 ||
1123 (ipoptions = (u_char *)(ip + 1))[1] != IPOPT_LSRR ) {
1125 * Packet arrived via a physical interface or
1126 * an encapsulated tunnel.
1130 * Packet arrived through a source-route tunnel.
1131 * Source-route tunnels are no longer supported.
1133 if ((srctun++ % 1000) == 0)
1135 "ip_mforward: received source-routed packet from %lx\n",
1136 (u_long)ntohl(ip->ip_src.s_addr));
1141 if ((imo) && ((vifi = imo->imo_multicast_vif) < numvifs)) {
1142 if (ip->ip_ttl < 255)
1143 ip->ip_ttl++; /* compensate for -1 in *_send routines */
1144 if (rsvpdebug && ip->ip_p == IPPROTO_RSVP) {
1145 vifp = viftable + vifi;
1146 printf("Sending IPPROTO_RSVP from %lx to %lx on vif %d (%s%s%d)\n",
1147 ntohl(ip->ip_src.s_addr), ntohl(ip->ip_dst.s_addr), vifi,
1148 (vifp->v_flags & VIFF_TUNNEL) ? "tunnel on " : "",
1149 vifp->v_ifp->if_name, vifp->v_ifp->if_unit);
1151 return (ip_mdq(m, ifp, NULL, vifi));
1153 if (rsvpdebug && ip->ip_p == IPPROTO_RSVP) {
1154 printf("Warning: IPPROTO_RSVP from %lx to %lx without vif option\n",
1155 ntohl(ip->ip_src.s_addr), ntohl(ip->ip_dst.s_addr));
1157 printf("In fact, no options were specified at all\n");
1161 * Don't forward a packet with time-to-live of zero or one,
1162 * or a packet destined to a local-only group.
1164 if (ip->ip_ttl <= 1 ||
1165 ntohl(ip->ip_dst.s_addr) <= INADDR_MAX_LOCAL_GROUP)
1169 * Determine forwarding vifs from the forwarding cache table
1172 MFCFIND(ip->ip_src.s_addr, ip->ip_dst.s_addr, rt);
1174 /* Entry exists, so forward if necessary */
1177 return (ip_mdq(m, ifp, rt, -1));
1180 * If we don't have a route for packet's origin,
1181 * Make a copy of the packet &
1182 * send message to routing daemon
1185 register struct mbuf *mb0;
1186 register struct rtdetq *rte;
1187 register u_long hash;
1188 int hlen = ip->ip_hl << 2;
1189 #ifdef UPCALL_TIMING
1195 mrtstat.mrts_no_route++;
1196 if (mrtdebug & (DEBUG_FORWARD | DEBUG_MFC))
1197 log(LOG_DEBUG, "ip_mforward: no rte s %lx g %lx\n",
1198 (u_long)ntohl(ip->ip_src.s_addr),
1199 (u_long)ntohl(ip->ip_dst.s_addr));
1202 * Allocate mbufs early so that we don't do extra work if we are
1203 * just going to fail anyway. Make sure to pullup the header so
1204 * that other people can't step on it.
1206 rte = (struct rtdetq *)malloc((sizeof *rte), M_MRTABLE, M_NOWAIT);
1211 mb0 = m_copy(m, 0, M_COPYALL);
1212 if (mb0 && (M_HASCL(mb0) || mb0->m_len < hlen))
1213 mb0 = m_pullup(mb0, hlen);
1215 free(rte, M_MRTABLE);
1220 /* is there an upcall waiting for this packet? */
1221 hash = MFCHASH(ip->ip_src.s_addr, ip->ip_dst.s_addr);
1222 for (rt = mfctable[hash]; rt; rt = rt->mfc_next) {
1223 if ((ip->ip_src.s_addr == rt->mfc_origin.s_addr) &&
1224 (ip->ip_dst.s_addr == rt->mfc_mcastgrp.s_addr) &&
1225 (rt->mfc_stall != NULL))
1233 /* no upcall, so make a new entry */
1234 rt = (struct mfc *)malloc(sizeof(*rt), M_MRTABLE, M_NOWAIT);
1236 free(rte, M_MRTABLE);
1241 /* Make a copy of the header to send to the user level process */
1242 mm = m_copy(mb0, 0, hlen);
1244 free(rte, M_MRTABLE);
1246 free(rt, M_MRTABLE);
1252 * Send message to routing daemon to install
1253 * a route into the kernel table
1255 k_igmpsrc.sin_addr = ip->ip_src;
1257 im = mtod(mm, struct igmpmsg *);
1258 im->im_msgtype = IGMPMSG_NOCACHE;
1261 mrtstat.mrts_upcalls++;
1263 if (socket_send(ip_mrouter, mm, &k_igmpsrc) < 0) {
1264 log(LOG_WARNING, "ip_mforward: ip_mrouter socket queue full\n");
1265 ++mrtstat.mrts_upq_sockfull;
1266 free(rte, M_MRTABLE);
1268 free(rt, M_MRTABLE);
1273 /* insert new entry at head of hash chain */
1274 rt->mfc_origin.s_addr = ip->ip_src.s_addr;
1275 rt->mfc_mcastgrp.s_addr = ip->ip_dst.s_addr;
1276 rt->mfc_expire = UPCALL_EXPIRE;
1278 for (i = 0; i < numvifs; i++)
1279 rt->mfc_ttls[i] = 0;
1280 rt->mfc_parent = -1;
1282 /* link into table */
1283 rt->mfc_next = mfctable[hash];
1284 mfctable[hash] = rt;
1285 rt->mfc_stall = rte;
1288 /* determine if q has overflowed */
1292 for (p = &rt->mfc_stall; *p != NULL; p = &(*p)->next)
1295 if (npkts > MAX_UPQ) {
1296 mrtstat.mrts_upq_ovflw++;
1297 free(rte, M_MRTABLE);
1303 /* Add this entry to the end of the queue */
1309 #ifdef UPCALL_TIMING
1321 int (*ip_mforward)(struct ip *, struct ifnet *, struct mbuf *,
1322 struct ip_moptions *) = X_ip_mforward;
1326 * Clean up the cache entry if upcall is not serviced
1329 expire_upcalls(void *unused)
1332 struct mfc *mfc, **nptr;
1337 for (i = 0; i < MFCTBLSIZ; i++) {
1338 if (nexpire[i] == 0)
1340 nptr = &mfctable[i];
1341 for (mfc = *nptr; mfc != NULL; mfc = *nptr) {
1343 * Skip real cache entries
1344 * Make sure it wasn't marked to not expire (shouldn't happen)
1347 if (mfc->mfc_stall != NULL &&
1348 mfc->mfc_expire != 0 &&
1349 --mfc->mfc_expire == 0) {
1350 if (mrtdebug & DEBUG_EXPIRE)
1351 log(LOG_DEBUG, "expire_upcalls: expiring (%lx %lx)\n",
1352 (u_long)ntohl(mfc->mfc_origin.s_addr),
1353 (u_long)ntohl(mfc->mfc_mcastgrp.s_addr));
1355 * drop all the packets
1356 * free the mbuf with the pkt, if, timing info
1358 for (rte = mfc->mfc_stall; rte; ) {
1359 struct rtdetq *n = rte->next;
1362 free(rte, M_MRTABLE);
1365 ++mrtstat.mrts_cache_cleanups;
1368 *nptr = mfc->mfc_next;
1369 free(mfc, M_MRTABLE);
1371 nptr = &mfc->mfc_next;
1376 expire_upcalls_ch = timeout(expire_upcalls, (caddr_t)NULL, EXPIRE_TIMEOUT);
1380 * Packet forwarding routine once entry in the cache is made
1383 ip_mdq(m, ifp, rt, xmt_vif)
1384 register struct mbuf *m;
1385 register struct ifnet *ifp;
1386 register struct mfc *rt;
1387 register vifi_t xmt_vif;
1389 register struct ip *ip = mtod(m, struct ip *);
1390 register vifi_t vifi;
1391 register struct vif *vifp;
1392 register int plen = ip->ip_len;
1395 * Macro to send packet on vif. Since RSVP packets don't get counted on
1396 * input, they shouldn't get counted on output, so statistics keeping is
1399 #define MC_SEND(ip,vifp,m) { \
1400 if ((vifp)->v_flags & VIFF_TUNNEL) \
1401 encap_send((ip), (vifp), (m)); \
1403 phyint_send((ip), (vifp), (m)); \
1407 * If xmt_vif is not -1, send on only the requested vif.
1409 * (since vifi_t is u_short, -1 becomes MAXUSHORT, which > numvifs.)
1411 if (xmt_vif < numvifs) {
1412 MC_SEND(ip, viftable + xmt_vif, m);
1417 * Don't forward if it didn't arrive from the parent vif for its origin.
1419 vifi = rt->mfc_parent;
1420 if ((vifi >= numvifs) || (viftable[vifi].v_ifp != ifp)) {
1421 /* came in the wrong interface */
1422 if (mrtdebug & DEBUG_FORWARD)
1423 log(LOG_DEBUG, "wrong if: ifp %p vifi %d vififp %p\n",
1424 (void *)ifp, vifi, (void *)viftable[vifi].v_ifp);
1425 ++mrtstat.mrts_wrong_if;
1428 * If we are doing PIM assert processing, and we are forwarding
1429 * packets on this interface, and it is a broadcast medium
1430 * interface (and not a tunnel), send a message to the routing daemon.
1432 if (pim_assert && rt->mfc_ttls[vifi] &&
1433 (ifp->if_flags & IFF_BROADCAST) &&
1434 !(viftable[vifi].v_flags & VIFF_TUNNEL)) {
1435 struct sockaddr_in k_igmpsrc;
1438 int hlen = ip->ip_hl << 2;
1440 register u_long delta;
1444 TV_DELTA(rt->mfc_last_assert, now, delta);
1446 if (delta > ASSERT_MSG_TIME) {
1447 mm = m_copy(m, 0, hlen);
1448 if (mm && (M_HASCL(mm) || mm->m_len < hlen))
1449 mm = m_pullup(mm, hlen);
1454 rt->mfc_last_assert = now;
1456 im = mtod(mm, struct igmpmsg *);
1457 im->im_msgtype = IGMPMSG_WRONGVIF;
1461 k_igmpsrc.sin_addr = im->im_src;
1463 socket_send(ip_mrouter, mm, &k_igmpsrc);
1469 /* If I sourced this packet, it counts as output, else it was input. */
1470 if (ip->ip_src.s_addr == viftable[vifi].v_lcl_addr.s_addr) {
1471 viftable[vifi].v_pkt_out++;
1472 viftable[vifi].v_bytes_out += plen;
1474 viftable[vifi].v_pkt_in++;
1475 viftable[vifi].v_bytes_in += plen;
1478 rt->mfc_byte_cnt += plen;
1481 * For each vif, decide if a copy of the packet should be forwarded.
1483 * - the ttl exceeds the vif's threshold
1484 * - there are group members downstream on interface
1486 for (vifp = viftable, vifi = 0; vifi < numvifs; vifp++, vifi++)
1487 if ((rt->mfc_ttls[vifi] > 0) &&
1488 (ip->ip_ttl > rt->mfc_ttls[vifi])) {
1490 vifp->v_bytes_out += plen;
1491 MC_SEND(ip, vifp, m);
1498 * check if a vif number is legal/ok. This is used by ip_output, to export
1502 X_legal_vif_num(vif)
1505 if (vif >= 0 && vif < numvifs)
1512 int (*legal_vif_num)(int) = X_legal_vif_num;
1516 * Return the local address used by this vif
1519 X_ip_mcast_src(vifi)
1522 if (vifi >= 0 && vifi < numvifs)
1523 return viftable[vifi].v_lcl_addr.s_addr;
1529 u_long (*ip_mcast_src)(int) = X_ip_mcast_src;
1533 phyint_send(ip, vifp, m)
1538 register struct mbuf *mb_copy;
1539 register int hlen = ip->ip_hl << 2;
1542 * Make a new reference to the packet; make sure that
1543 * the IP header is actually copied, not just referenced,
1544 * so that ip_output() only scribbles on the copy.
1546 mb_copy = m_copy(m, 0, M_COPYALL);
1547 if (mb_copy && (M_HASCL(mb_copy) || mb_copy->m_len < hlen))
1548 mb_copy = m_pullup(mb_copy, hlen);
1549 if (mb_copy == NULL)
1552 if (vifp->v_rate_limit == 0)
1553 tbf_send_packet(vifp, mb_copy);
1555 tbf_control(vifp, mb_copy, mtod(mb_copy, struct ip *), ip->ip_len);
1559 encap_send(ip, vifp, m)
1560 register struct ip *ip;
1561 register struct vif *vifp;
1562 register struct mbuf *m;
1564 register struct mbuf *mb_copy;
1565 register struct ip *ip_copy;
1566 register int i, len = ip->ip_len;
1569 * copy the old packet & pullup its IP header into the
1570 * new mbuf so we can modify it. Try to fill the new
1571 * mbuf since if we don't the ethernet driver will.
1573 MGETHDR(mb_copy, M_DONTWAIT, MT_HEADER);
1574 if (mb_copy == NULL)
1576 mb_copy->m_data += max_linkhdr;
1577 mb_copy->m_len = sizeof(multicast_encap_iphdr);
1579 if ((mb_copy->m_next = m_copy(m, 0, M_COPYALL)) == NULL) {
1583 i = MHLEN - M_LEADINGSPACE(mb_copy);
1586 mb_copy = m_pullup(mb_copy, i);
1587 if (mb_copy == NULL)
1589 mb_copy->m_pkthdr.len = len + sizeof(multicast_encap_iphdr);
1592 * fill in the encapsulating IP header.
1594 ip_copy = mtod(mb_copy, struct ip *);
1595 *ip_copy = multicast_encap_iphdr;
1597 ip_copy->ip_id = ip_randomid();
1599 ip_copy->ip_id = htons(ip_id++);
1601 ip_copy->ip_len += len;
1602 ip_copy->ip_src = vifp->v_lcl_addr;
1603 ip_copy->ip_dst = vifp->v_rmt_addr;
1606 * turn the encapsulated IP header back into a valid one.
1608 ip = (struct ip *)((caddr_t)ip_copy + sizeof(multicast_encap_iphdr));
1613 mb_copy->m_data += sizeof(multicast_encap_iphdr);
1614 ip->ip_sum = in_cksum(mb_copy, ip->ip_hl << 2);
1615 mb_copy->m_data -= sizeof(multicast_encap_iphdr);
1617 if (vifp->v_rate_limit == 0)
1618 tbf_send_packet(vifp, mb_copy);
1620 tbf_control(vifp, mb_copy, ip, ip_copy->ip_len);
1624 * De-encapsulate a packet and feed it back through ip input (this
1625 * routine is called whenever IP gets a packet with proto type
1626 * ENCAP_PROTO and a local destination address).
1630 X_ipip_input(m, off)
1634 register struct mbuf *m;
1637 struct ifnet *ifp = m->m_pkthdr.rcvif;
1638 register struct ip *ip = mtod(m, struct ip *);
1639 register int hlen = ip->ip_hl << 2;
1641 register struct ifqueue *ifq;
1642 register struct vif *vifp;
1644 if (!have_encap_tunnel) {
1649 * dump the packet if it's not to a multicast destination or if
1650 * we don't have an encapsulating tunnel with the source.
1651 * Note: This code assumes that the remote site IP address
1652 * uniquely identifies the tunnel (i.e., that this site has
1653 * at most one tunnel with the remote site).
1655 if (! IN_MULTICAST(ntohl(((struct ip *)((char *)ip + hlen))->ip_dst.s_addr))) {
1656 ++mrtstat.mrts_bad_tunnel;
1660 if (ip->ip_src.s_addr != last_encap_src) {
1661 register struct vif *vife;
1664 vife = vifp + numvifs;
1665 last_encap_src = ip->ip_src.s_addr;
1667 for ( ; vifp < vife; ++vifp)
1668 if (vifp->v_rmt_addr.s_addr == ip->ip_src.s_addr) {
1669 if ((vifp->v_flags & (VIFF_TUNNEL|VIFF_SRCRT))
1671 last_encap_vif = vifp;
1675 if ((vifp = last_encap_vif) == 0) {
1677 mrtstat.mrts_cant_tunnel++; /*XXX*/
1680 log(LOG_DEBUG, "ip_mforward: no tunnel with %lx\n",
1681 (u_long)ntohl(ip->ip_src.s_addr));
1686 if (hlen > IP_HDR_LEN)
1687 ip_stripoptions(m, (struct mbuf *) 0);
1688 m->m_data += IP_HDR_LEN;
1689 m->m_len -= IP_HDR_LEN;
1690 m->m_pkthdr.len -= IP_HDR_LEN;
1691 m->m_pkthdr.rcvif = ifp;
1695 if (IF_QFULL(ifq)) {
1701 * normally we would need a "schednetisr(NETISR_IP)"
1702 * here but we were called by ip_input and it is going
1703 * to loop back & try to dequeue the packet we just
1704 * queued as soon as we return so we avoid the
1705 * unnecessary software interrrupt.
1712 * Token bucket filter module
1716 tbf_control(vifp, m, ip, p_len)
1717 register struct vif *vifp;
1718 register struct mbuf *m;
1719 register struct ip *ip;
1720 register u_long p_len;
1722 register struct tbf *t = vifp->v_tbf;
1724 if (p_len > MAX_BKT_SIZE) {
1725 /* drop if packet is too large */
1726 mrtstat.mrts_pkt2large++;
1731 tbf_update_tokens(vifp);
1733 /* if there are enough tokens,
1734 * and the queue is empty,
1735 * send this packet out
1738 if (t->tbf_q_len == 0) {
1739 /* queue empty, send packet if enough tokens */
1740 if (p_len <= t->tbf_n_tok) {
1741 t->tbf_n_tok -= p_len;
1742 tbf_send_packet(vifp, m);
1744 /* queue packet and timeout till later */
1746 timeout(tbf_reprocess_q, (caddr_t)vifp, TBF_REPROCESS);
1748 } else if (t->tbf_q_len < t->tbf_max_q_len) {
1749 /* finite queue length, so queue pkts and process queue */
1751 tbf_process_q(vifp);
1753 /* queue length too much, try to dq and queue and process */
1754 if (!tbf_dq_sel(vifp, ip)) {
1755 mrtstat.mrts_q_overflow++;
1760 tbf_process_q(vifp);
1767 * adds a packet to the queue at the interface
1771 register struct vif *vifp;
1772 register struct mbuf *m;
1774 register int s = splnet();
1775 register struct tbf *t = vifp->v_tbf;
1777 if (t->tbf_t == NULL) {
1778 /* Queue was empty */
1781 /* Insert at tail */
1782 t->tbf_t->m_act = m;
1785 /* Set new tail pointer */
1789 /* Make sure we didn't get fed a bogus mbuf */
1791 panic("tbf_queue: m_act");
1802 * processes the queue at the interface
1806 register struct vif *vifp;
1808 register struct mbuf *m;
1810 register int s = splnet();
1811 register struct tbf *t = vifp->v_tbf;
1813 /* loop through the queue at the interface and send as many packets
1816 while (t->tbf_q_len > 0) {
1819 len = mtod(m, struct ip *)->ip_len;
1821 /* determine if the packet can be sent */
1822 if (len <= t->tbf_n_tok) {
1824 * reduce no of tokens, dequeue the packet,
1827 t->tbf_n_tok -= len;
1829 t->tbf_q = m->m_act;
1830 if (--t->tbf_q_len == 0)
1834 tbf_send_packet(vifp, m);
1842 tbf_reprocess_q(xvifp)
1845 register struct vif *vifp = xvifp;
1846 if (ip_mrouter == NULL)
1849 tbf_update_tokens(vifp);
1851 tbf_process_q(vifp);
1853 if (vifp->v_tbf->tbf_q_len)
1854 timeout(tbf_reprocess_q, (caddr_t)vifp, TBF_REPROCESS);
1857 /* function that will selectively discard a member of the queue
1858 * based on the precedence value and the priority
1861 tbf_dq_sel(vifp, ip)
1862 register struct vif *vifp;
1863 register struct ip *ip;
1865 register int s = splnet();
1867 register struct mbuf *m, *last;
1868 register struct mbuf **np;
1869 register struct tbf *t = vifp->v_tbf;
1871 p = priority(vifp, ip);
1875 while ((m = *np) != NULL) {
1876 if (p > priority(vifp, mtod(m, struct ip *))) {
1878 /* If we're removing the last packet, fix the tail pointer */
1882 /* it's impossible for the queue to be empty, but
1883 * we check anyway. */
1884 if (--t->tbf_q_len == 0)
1887 mrtstat.mrts_drop_sel++;
1898 tbf_send_packet(vifp, m)
1899 register struct vif *vifp;
1900 register struct mbuf *m;
1902 struct ip_moptions imo;
1904 static struct route ro;
1907 if (vifp->v_flags & VIFF_TUNNEL) {
1908 /* If tunnel options */
1909 ip_output(m, (struct mbuf *)0, &vifp->v_route,
1910 IP_FORWARDING, (struct ip_moptions *)0);
1912 imo.imo_multicast_ifp = vifp->v_ifp;
1913 imo.imo_multicast_ttl = mtod(m, struct ip *)->ip_ttl - 1;
1914 imo.imo_multicast_loop = 1;
1915 imo.imo_multicast_vif = -1;
1918 * Re-entrancy should not be a problem here, because
1919 * the packets that we send out and are looped back at us
1920 * should get rejected because they appear to come from
1921 * the loopback interface, thus preventing looping.
1923 error = ip_output(m, (struct mbuf *)0, &ro,
1924 IP_FORWARDING, &imo);
1926 if (mrtdebug & DEBUG_XMIT)
1927 log(LOG_DEBUG, "phyint_send on vif %d err %d\n",
1928 vifp - viftable, error);
1933 /* determine the current time and then
1934 * the elapsed time (between the last time and time now)
1935 * in milliseconds & update the no. of tokens in the bucket
1938 tbf_update_tokens(vifp)
1939 register struct vif *vifp;
1943 register int s = splnet();
1944 register struct tbf *t = vifp->v_tbf;
1948 TV_DELTA(tp, t->tbf_last_pkt_t, tm);
1951 * This formula is actually
1952 * "time in seconds" * "bytes/second".
1954 * (tm / 1000000) * (v_rate_limit * 1000 * (1000/1024) / 8)
1956 * The (1000/1024) was introduced in add_vif to optimize
1957 * this divide into a shift.
1959 t->tbf_n_tok += tm * vifp->v_rate_limit / 1024 / 8;
1960 t->tbf_last_pkt_t = tp;
1962 if (t->tbf_n_tok > MAX_BKT_SIZE)
1963 t->tbf_n_tok = MAX_BKT_SIZE;
1970 register struct vif *vifp;
1971 register struct ip *ip;
1975 /* temporary hack; may add general packet classifier some day */
1978 * The UDP port space is divided up into four priority ranges:
1979 * [0, 16384) : unclassified - lowest priority
1980 * [16384, 32768) : audio - highest priority
1981 * [32768, 49152) : whiteboard - medium priority
1982 * [49152, 65536) : video - low priority
1984 if (ip->ip_p == IPPROTO_UDP) {
1985 struct udphdr *udp = (struct udphdr *)(((char *)ip) + (ip->ip_hl << 2));
1986 switch (ntohs(udp->uh_dport) & 0xc000) {
2001 log(LOG_DEBUG, "port %x prio%d\n", ntohs(udp->uh_dport), prio);
2009 * End of token bucket filter modifications
2013 ip_rsvp_vif_init(so, sopt)
2015 struct sockopt *sopt;
2020 printf("ip_rsvp_vif_init: so_type = %d, pr_protocol = %d\n",
2021 so->so_type, so->so_proto->pr_protocol);
2023 if (so->so_type != SOCK_RAW || so->so_proto->pr_protocol != IPPROTO_RSVP)
2027 error = sooptcopyin(sopt, &i, sizeof i, sizeof i);
2032 printf("ip_rsvp_vif_init: vif = %d rsvp_on = %d\n", i, rsvp_on);
2037 if (!legal_vif_num(i)) {
2039 return EADDRNOTAVAIL;
2042 /* Check if socket is available. */
2043 if (viftable[i].v_rsvpd != NULL) {
2048 viftable[i].v_rsvpd = so;
2049 /* This may seem silly, but we need to be sure we don't over-increment
2050 * the RSVP counter, in case something slips up.
2052 if (!viftable[i].v_rsvp_on) {
2053 viftable[i].v_rsvp_on = 1;
2062 ip_rsvp_vif_done(so, sopt)
2064 struct sockopt *sopt;
2069 printf("ip_rsvp_vif_done: so_type = %d, pr_protocol = %d\n",
2070 so->so_type, so->so_proto->pr_protocol);
2072 if (so->so_type != SOCK_RAW ||
2073 so->so_proto->pr_protocol != IPPROTO_RSVP)
2076 error = sooptcopyin(sopt, &i, sizeof i, sizeof i);
2083 if (!legal_vif_num(i)) {
2085 return EADDRNOTAVAIL;
2089 printf("ip_rsvp_vif_done: v_rsvpd = %p so = %p\n",
2090 viftable[i].v_rsvpd, so);
2092 viftable[i].v_rsvpd = NULL;
2094 * This may seem silly, but we need to be sure we don't over-decrement
2095 * the RSVP counter, in case something slips up.
2097 if (viftable[i].v_rsvp_on) {
2098 viftable[i].v_rsvp_on = 0;
2107 ip_rsvp_force_done(so)
2113 /* Don't bother if it is not the right type of socket. */
2114 if (so->so_type != SOCK_RAW || so->so_proto->pr_protocol != IPPROTO_RSVP)
2119 /* The socket may be attached to more than one vif...this
2120 * is perfectly legal.
2122 for (vifi = 0; vifi < numvifs; vifi++) {
2123 if (viftable[vifi].v_rsvpd == so) {
2124 viftable[vifi].v_rsvpd = NULL;
2125 /* This may seem silly, but we need to be sure we don't
2126 * over-decrement the RSVP counter, in case something slips up.
2128 if (viftable[vifi].v_rsvp_on) {
2129 viftable[vifi].v_rsvp_on = 0;
2145 register struct ip *ip = mtod(m, struct ip *);
2146 int proto = ip->ip_p;
2147 static struct sockaddr_in rsvp_src = { sizeof rsvp_src, AF_INET };
2151 /* support IP_RECVIF used by rsvpd rel4.2a1 */
2158 printf("rsvp_input: rsvp_on %d\n",rsvp_on);
2160 /* Can still get packets with rsvp_on = 0 if there is a local member
2161 * of the group to which the RSVP packet is addressed. But in this
2162 * case we want to throw the packet away.
2172 printf("rsvp_input: check vifs\n");
2175 if (!(m->m_flags & M_PKTHDR))
2176 panic("rsvp_input no hdr");
2179 ifp = m->m_pkthdr.rcvif;
2180 /* Find which vif the packet arrived on. */
2181 for (vifi = 0; vifi < numvifs; vifi++)
2182 if (viftable[vifi].v_ifp == ifp)
2186 if (vifi == numvifs || (so = viftable[vifi].v_rsvpd) == NULL) {
2188 if (vifi == numvifs || viftable[vifi].v_rsvpd == NULL) {
2191 * If the old-style non-vif-associated socket is set,
2192 * then use it. Otherwise, drop packet since there
2193 * is no specific socket for this vif.
2195 if (ip_rsvpd != NULL) {
2197 printf("rsvp_input: Sending packet up old-style socket\n");
2198 rip_input(m, off); /* xxx */
2200 if (rsvpdebug && vifi == numvifs)
2201 printf("rsvp_input: Can't find vif for packet.\n");
2202 else if (rsvpdebug && viftable[vifi].v_rsvpd == NULL)
2203 printf("rsvp_input: No socket defined for vif %d\n",vifi);
2209 rsvp_src.sin_addr = ip->ip_src;
2212 printf("rsvp_input: m->m_len = %d, sbspace() = %ld\n",
2213 m->m_len,sbspace(&(viftable[vifi].v_rsvpd->so_rcv)));
2217 inp = (struct inpcb *)so->so_pcb;
2218 if (inp->inp_flags & INP_CONTROLOPTS ||
2219 inp->inp_socket->so_options & SO_TIMESTAMP)
2220 ip_savecontrol(inp, &opts, ip, m);
2221 if (sbappendaddr(&so->so_rcv,
2222 (struct sockaddr *)&rsvp_src,m, opts) == 0) {
2227 printf("rsvp_input: Failed to append to socket\n");
2232 printf("rsvp_input: send packet up\n");
2235 if (socket_send(viftable[vifi].v_rsvpd, m, &rsvp_src) < 0) {
2237 printf("rsvp_input: Failed to append to socket\n");
2240 printf("rsvp_input: send packet up\n");
2248 #include <sys/conf.h>
2249 #include <sys/exec.h>
2250 #include <sys/sysent.h>
2251 #include <sys/lkm.h>
2253 MOD_MISC("ip_mroute_mod")
2256 ip_mroute_mod_handle(struct lkm_table *lkmtp, int cmd)
2259 struct lkm_misc *args = lkmtp->private.lkm_misc;
2263 static int (*old_ip_mrouter_cmd)();
2264 static int (*old_ip_mrouter_done)();
2265 static int (*old_ip_mforward)();
2266 static int (*old_mrt_ioctl)();
2267 static void (*old_proto4_input)();
2268 static int (*old_legal_vif_num)();
2269 extern struct protosw inetsw[];
2272 if(lkmexists(lkmtp) || ip_mrtproto)
2274 old_ip_mrouter_cmd = ip_mrouter_cmd;
2275 ip_mrouter_cmd = X_ip_mrouter_cmd;
2276 old_ip_mrouter_done = ip_mrouter_done;
2277 ip_mrouter_done = X_ip_mrouter_done;
2278 old_ip_mforward = ip_mforward;
2279 ip_mforward = X_ip_mforward;
2280 old_mrt_ioctl = mrt_ioctl;
2281 mrt_ioctl = X_mrt_ioctl;
2282 old_proto4_input = inetsw[ip_protox[ENCAP_PROTO]].pr_input;
2283 inetsw[ip_protox[ENCAP_PROTO]].pr_input = X_ipip_input;
2284 old_legal_vif_num = legal_vif_num;
2285 legal_vif_num = X_legal_vif_num;
2286 ip_mrtproto = IGMP_DVMRP;
2288 printf("\nIP multicast routing loaded\n");
2295 ip_mrouter_cmd = old_ip_mrouter_cmd;
2296 ip_mrouter_done = old_ip_mrouter_done;
2297 ip_mforward = old_ip_mforward;
2298 mrt_ioctl = old_mrt_ioctl;
2299 inetsw[ip_protox[ENCAP_PROTO]].pr_input = old_proto4_input;
2300 legal_vif_num = old_legal_vif_num;
2313 ip_mroute_mod(struct lkm_table *lkmtp, int cmd, int ver) {
2314 DISPATCH(lkmtp, cmd, ver, ip_mroute_mod_handle, ip_mroute_mod_handle,
2318 #endif /* MROUTE_LKM */
2319 #endif /* MROUTING */