2 * Copyright (c) 1989 Stephen Deering
3 * Copyright (c) 1992, 1993
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
7 * Stephen Deering of Stanford University.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)ip_mroute.c 8.2 (Berkeley) 11/15/93
37 * IP multicast forwarding procedures
39 * Written by David Waitzman, BBN Labs, August 1988.
40 * Modified by Steve Deering, Stanford, February 1989.
41 * Modified by Mark J. Steiglitz, Stanford, May, 1991
42 * Modified by Van Jacobson, LBL, January 1993
43 * Modified by Ajit Thyagarajan, PARC, August 1993
44 * Modified by Bill Fenner, PARC, April 1995
45 * Modified by Ahmed Helmy, SGI, June 1996
46 * Modified by George Edmond Eddy (Rusty), ISI, February 1998
47 * Modified by Pavlin Radoslavov, USC/ISI, May 1998, August 1999, October 2000
48 * Modified by Hitoshi Asaeda, WIDE, August 2000
49 * Modified by Pavlin Radoslavov, ICSI, October 2002
51 * MROUTING Revision: 3.5
52 * and PIM-SMv2 and PIM-DM support, advanced API support,
53 * bandwidth metering and signaling
59 #include "opt_mrouting.h"
65 #include <sys/param.h>
66 #include <sys/kernel.h>
69 #include <sys/malloc.h>
71 #include <sys/module.h>
72 #include <sys/protosw.h>
73 #include <sys/signalvar.h>
74 #include <sys/socket.h>
75 #include <sys/socketvar.h>
76 #include <sys/sockio.h>
78 #include <sys/sysctl.h>
79 #include <sys/syslog.h>
80 #include <sys/systm.h>
83 #include <net/netisr.h>
84 #include <net/route.h>
85 #include <netinet/in.h>
86 #include <netinet/igmp.h>
87 #include <netinet/in_systm.h>
88 #include <netinet/in_var.h>
89 #include <netinet/ip.h>
90 #include <netinet/ip_encap.h>
91 #include <netinet/ip_mroute.h>
92 #include <netinet/ip_var.h>
94 #include <netinet/pim.h>
95 #include <netinet/pim_var.h>
97 #include <netinet/udp.h>
98 #include <machine/in_cksum.h>
101 * Control debugging code for rsvp and multicast routing code.
102 * Can only set them with the debugger.
104 static u_int rsvpdebug; /* non-zero enables debugging */
106 static u_int mrtdebug; /* any set of the flags below */
107 #define DEBUG_MFC 0x02
108 #define DEBUG_FORWARD 0x04
109 #define DEBUG_EXPIRE 0x08
110 #define DEBUG_XMIT 0x10
111 #define DEBUG_PIM 0x20
113 #define VIFI_INVALID ((vifi_t) -1)
115 #define M_HASCL(m) ((m)->m_flags & M_EXT)
117 static MALLOC_DEFINE(M_MRTABLE, "mroutetbl", "multicast routing tables");
120 * Locking. We use two locks: one for the virtual interface table and
121 * one for the forwarding table. These locks may be nested in which case
122 * the VIF lock must always be taken first. Note that each lock is used
123 * to cover not only the specific data structure but also related data
124 * structures. It may be better to add more fine-grained locking later;
125 * it's not clear how performance-critical this code is.
128 static struct mrtstat mrtstat;
129 SYSCTL_STRUCT(_net_inet_ip, OID_AUTO, mrtstat, CTLFLAG_RW,
131 "Multicast Routing Statistics (struct mrtstat, netinet/ip_mroute.h)");
133 static struct mfc *mfctable[MFCTBLSIZ];
134 SYSCTL_OPAQUE(_net_inet_ip, OID_AUTO, mfctable, CTLFLAG_RD,
135 &mfctable, sizeof(mfctable), "S,*mfc[MFCTBLSIZ]",
136 "Multicast Forwarding Table (struct *mfc[MFCTBLSIZ], netinet/ip_mroute.h)");
138 static struct mtx mfc_mtx;
139 #define MFC_LOCK() mtx_lock(&mfc_mtx)
140 #define MFC_UNLOCK() mtx_unlock(&mfc_mtx)
141 #define MFC_LOCK_ASSERT() do { \
142 mtx_assert(&mfc_mtx, MA_OWNED); \
143 NET_ASSERT_GIANT(); \
145 #define MFC_LOCK_INIT() mtx_init(&mfc_mtx, "mroute mfc table", NULL, MTX_DEF)
146 #define MFC_LOCK_DESTROY() mtx_destroy(&mfc_mtx)
148 static struct vif viftable[MAXVIFS];
149 SYSCTL_OPAQUE(_net_inet_ip, OID_AUTO, viftable, CTLFLAG_RD,
150 &viftable, sizeof(viftable), "S,vif[MAXVIFS]",
151 "Multicast Virtual Interfaces (struct vif[MAXVIFS], netinet/ip_mroute.h)");
153 static struct mtx vif_mtx;
154 #define VIF_LOCK() mtx_lock(&vif_mtx)
155 #define VIF_UNLOCK() mtx_unlock(&vif_mtx)
156 #define VIF_LOCK_ASSERT() mtx_assert(&vif_mtx, MA_OWNED)
157 #define VIF_LOCK_INIT() mtx_init(&vif_mtx, "mroute vif table", NULL, MTX_DEF)
158 #define VIF_LOCK_DESTROY() mtx_destroy(&vif_mtx)
160 static u_char nexpire[MFCTBLSIZ];
162 static struct callout expire_upcalls_ch;
164 #define EXPIRE_TIMEOUT (hz / 4) /* 4x / second */
165 #define UPCALL_EXPIRE 6 /* number of timeouts */
168 * Define the token bucket filter structures
169 * tbftable -> each vif has one of these for storing info
172 static struct tbf tbftable[MAXVIFS];
173 #define TBF_REPROCESS (hz / 100) /* 100x / second */
176 * 'Interfaces' associated with decapsulator (so we can tell
177 * packets that went through it from ones that get reflected
178 * by a broken gateway). These interfaces are never linked into
179 * the system ifnet list & no routes point to them. I.e., packets
180 * can't be sent this way. They only exist as a placeholder for
181 * multicast source verification.
183 static struct ifnet multicast_decap_if[MAXVIFS];
186 #define ENCAP_PROTO IPPROTO_IPIP /* 4 */
188 /* prototype IP hdr for encapsulated packets */
189 static struct ip multicast_encap_iphdr = {
190 #if BYTE_ORDER == LITTLE_ENDIAN
191 sizeof(struct ip) >> 2, IPVERSION,
193 IPVERSION, sizeof(struct ip) >> 2,
196 sizeof(struct ip), /* total length */
199 ENCAP_TTL, ENCAP_PROTO,
204 * Bandwidth meter variables and constants
206 static MALLOC_DEFINE(M_BWMETER, "bwmeter", "multicast upcall bw meters");
208 * Pending timeouts are stored in a hash table, the key being the
209 * expiration time. Periodically, the entries are analysed and processed.
211 #define BW_METER_BUCKETS 1024
212 static struct bw_meter *bw_meter_timers[BW_METER_BUCKETS];
213 static struct callout bw_meter_ch;
214 #define BW_METER_PERIOD (hz) /* periodical handling of bw meters */
217 * Pending upcalls are stored in a vector which is flushed when
218 * full, or periodically
220 static struct bw_upcall bw_upcalls[BW_UPCALLS_MAX];
221 static u_int bw_upcalls_n; /* # of pending upcalls */
222 static struct callout bw_upcalls_ch;
223 #define BW_UPCALLS_PERIOD (hz) /* periodical flush of bw upcalls */
226 static struct pimstat pimstat;
227 SYSCTL_STRUCT(_net_inet_pim, PIMCTL_STATS, stats, CTLFLAG_RD,
229 "PIM Statistics (struct pimstat, netinet/pim_var.h)");
232 * Note: the PIM Register encapsulation adds the following in front of a
235 * struct pim_encap_hdr {
237 * struct pim_encap_pimhdr pim;
242 struct pim_encap_pimhdr {
247 static struct ip pim_encap_iphdr = {
248 #if BYTE_ORDER == LITTLE_ENDIAN
249 sizeof(struct ip) >> 2,
253 sizeof(struct ip) >> 2,
256 sizeof(struct ip), /* total length */
264 static struct pim_encap_pimhdr pim_encap_pimhdr = {
266 PIM_MAKE_VT(PIM_VERSION, PIM_REGISTER), /* PIM vers and message type */
273 static struct ifnet multicast_register_if;
274 static vifi_t reg_vif_num = VIFI_INVALID;
280 static vifi_t numvifs;
281 static const struct encaptab *encap_cookie;
284 * one-back cache used by mroute_encapcheck to locate a tunnel's vif
285 * given a datagram's src ip address.
287 static u_long last_encap_src;
288 static struct vif *last_encap_vif;
291 * Callout for queue processing.
293 static struct callout tbf_reprocess_ch;
295 static u_long X_ip_mcast_src(int vifi);
296 static int X_ip_mforward(struct ip *ip, struct ifnet *ifp,
297 struct mbuf *m, struct ip_moptions *imo);
298 static int X_ip_mrouter_done(void);
299 static int X_ip_mrouter_get(struct socket *so, struct sockopt *m);
300 static int X_ip_mrouter_set(struct socket *so, struct sockopt *m);
301 static int X_legal_vif_num(int vif);
302 static int X_mrt_ioctl(int cmd, caddr_t data);
304 static int get_sg_cnt(struct sioc_sg_req *);
305 static int get_vif_cnt(struct sioc_vif_req *);
306 static int ip_mrouter_init(struct socket *, int);
307 static int add_vif(struct vifctl *);
308 static int del_vif(vifi_t);
309 static int add_mfc(struct mfcctl2 *);
310 static int del_mfc(struct mfcctl2 *);
311 static int set_api_config(uint32_t *); /* chose API capabilities */
312 static int socket_send(struct socket *, struct mbuf *, struct sockaddr_in *);
313 static int set_assert(int);
314 static void expire_upcalls(void *);
315 static int ip_mdq(struct mbuf *, struct ifnet *, struct mfc *, vifi_t);
316 static void phyint_send(struct ip *, struct vif *, struct mbuf *);
317 static void encap_send(struct ip *, struct vif *, struct mbuf *);
318 static void tbf_control(struct vif *, struct mbuf *, struct ip *, u_long);
319 static void tbf_queue(struct vif *, struct mbuf *);
320 static void tbf_process_q(struct vif *);
321 static void tbf_reprocess_q(void *);
322 static int tbf_dq_sel(struct vif *, struct ip *);
323 static void tbf_send_packet(struct vif *, struct mbuf *);
324 static void tbf_update_tokens(struct vif *);
325 static int priority(struct vif *, struct ip *);
328 * Bandwidth monitoring
330 static void free_bw_list(struct bw_meter *list);
331 static int add_bw_upcall(struct bw_upcall *);
332 static int del_bw_upcall(struct bw_upcall *);
333 static void bw_meter_receive_packet(struct bw_meter *x, int plen,
334 struct timeval *nowp);
335 static void bw_meter_prepare_upcall(struct bw_meter *x, struct timeval *nowp);
336 static void bw_upcalls_send(void);
337 static void schedule_bw_meter(struct bw_meter *x, struct timeval *nowp);
338 static void unschedule_bw_meter(struct bw_meter *x);
339 static void bw_meter_process(void);
340 static void expire_bw_upcalls_send(void *);
341 static void expire_bw_meter_process(void *);
344 static int pim_register_send(struct ip *, struct vif *,
345 struct mbuf *, struct mfc *);
346 static int pim_register_send_rp(struct ip *, struct vif *,
347 struct mbuf *, struct mfc *);
348 static int pim_register_send_upcall(struct ip *, struct vif *,
349 struct mbuf *, struct mfc *);
350 static struct mbuf *pim_register_prepare(struct ip *, struct mbuf *);
354 * whether or not special PIM assert processing is enabled.
356 static int pim_assert;
358 * Rate limit for assert notification messages, in usec
360 #define ASSERT_MSG_TIME 3000000
363 * Kernel multicast routing API capabilities and setup.
364 * If more API capabilities are added to the kernel, they should be
365 * recorded in `mrt_api_support'.
367 static const uint32_t mrt_api_support = (MRT_MFC_FLAGS_DISABLE_WRONGVIF |
368 MRT_MFC_FLAGS_BORDER_VIF |
371 static uint32_t mrt_api_config = 0;
374 * Hash function for a source, group entry
376 #define MFCHASH(a, g) MFCHASHMOD(((a) >> 20) ^ ((a) >> 10) ^ (a) ^ \
377 ((g) >> 20) ^ ((g) >> 10) ^ (g))
380 * Find a route for a given origin IP address and Multicast group address
381 * Type of service parameter to be added in the future!!!
382 * Statistics are updated by the caller if needed
383 * (mrtstat.mrts_mfc_lookups and mrtstat.mrts_mfc_misses)
386 mfc_find(in_addr_t o, in_addr_t g)
392 for (rt = mfctable[MFCHASH(o,g)]; rt; rt = rt->mfc_next)
393 if ((rt->mfc_origin.s_addr == o) &&
394 (rt->mfc_mcastgrp.s_addr == g) && (rt->mfc_stall == NULL))
400 * Macros to compute elapsed time efficiently
401 * Borrowed from Van Jacobson's scheduling code
403 #define TV_DELTA(a, b, delta) { \
405 delta = (a).tv_usec - (b).tv_usec; \
406 if ((xxs = (a).tv_sec - (b).tv_sec)) { \
415 delta += (1000000 * xxs); \
420 #define TV_LT(a, b) (((a).tv_usec < (b).tv_usec && \
421 (a).tv_sec <= (b).tv_sec) || (a).tv_sec < (b).tv_sec)
424 * Handle MRT setsockopt commands to modify the multicast routing tables.
427 X_ip_mrouter_set(struct socket *so, struct sockopt *sopt)
433 struct bw_upcall bw_upcall;
436 if (so != ip_mrouter && sopt->sopt_name != MRT_INIT)
440 switch (sopt->sopt_name) {
442 error = sooptcopyin(sopt, &optval, sizeof optval, sizeof optval);
445 error = ip_mrouter_init(so, optval);
449 error = ip_mrouter_done();
453 error = sooptcopyin(sopt, &vifc, sizeof vifc, sizeof vifc);
456 error = add_vif(&vifc);
460 error = sooptcopyin(sopt, &vifi, sizeof vifi, sizeof vifi);
463 error = del_vif(vifi);
469 * select data size depending on API version.
471 if (sopt->sopt_name == MRT_ADD_MFC &&
472 mrt_api_config & MRT_API_FLAGS_ALL) {
473 error = sooptcopyin(sopt, &mfc, sizeof(struct mfcctl2),
474 sizeof(struct mfcctl2));
476 error = sooptcopyin(sopt, &mfc, sizeof(struct mfcctl),
477 sizeof(struct mfcctl));
478 bzero((caddr_t)&mfc + sizeof(struct mfcctl),
479 sizeof(mfc) - sizeof(struct mfcctl));
483 if (sopt->sopt_name == MRT_ADD_MFC)
484 error = add_mfc(&mfc);
486 error = del_mfc(&mfc);
490 error = sooptcopyin(sopt, &optval, sizeof optval, sizeof optval);
497 error = sooptcopyin(sopt, &i, sizeof i, sizeof i);
499 error = set_api_config(&i);
501 error = sooptcopyout(sopt, &i, sizeof i);
504 case MRT_ADD_BW_UPCALL:
505 case MRT_DEL_BW_UPCALL:
506 error = sooptcopyin(sopt, &bw_upcall, sizeof bw_upcall,
510 if (sopt->sopt_name == MRT_ADD_BW_UPCALL)
511 error = add_bw_upcall(&bw_upcall);
513 error = del_bw_upcall(&bw_upcall);
524 * Handle MRT getsockopt commands
527 X_ip_mrouter_get(struct socket *so, struct sockopt *sopt)
530 static int version = 0x0305; /* !!! why is this here? XXX */
532 switch (sopt->sopt_name) {
534 error = sooptcopyout(sopt, &version, sizeof version);
538 error = sooptcopyout(sopt, &pim_assert, sizeof pim_assert);
541 case MRT_API_SUPPORT:
542 error = sooptcopyout(sopt, &mrt_api_support, sizeof mrt_api_support);
546 error = sooptcopyout(sopt, &mrt_api_config, sizeof mrt_api_config);
557 * Handle ioctl commands to obtain information from the cache
560 X_mrt_ioctl(int cmd, caddr_t data)
565 * Currently the only function calling this ioctl routine is rtioctl().
566 * Typically, only root can create the raw socket in order to execute
567 * this ioctl method, however the request might be coming from a prison
569 error = suser(curthread);
573 case (SIOCGETVIFCNT):
574 error = get_vif_cnt((struct sioc_vif_req *)data);
578 error = get_sg_cnt((struct sioc_sg_req *)data);
589 * returns the packet, byte, rpf-failure count for the source group provided
592 get_sg_cnt(struct sioc_sg_req *req)
597 rt = mfc_find(req->src.s_addr, req->grp.s_addr);
600 req->pktcnt = req->bytecnt = req->wrong_if = 0xffffffff;
601 return EADDRNOTAVAIL;
603 req->pktcnt = rt->mfc_pkt_cnt;
604 req->bytecnt = rt->mfc_byte_cnt;
605 req->wrong_if = rt->mfc_wrong_if;
611 * returns the input and output packet and byte counts on the vif provided
614 get_vif_cnt(struct sioc_vif_req *req)
616 vifi_t vifi = req->vifi;
619 if (vifi >= numvifs) {
624 req->icount = viftable[vifi].v_pkt_in;
625 req->ocount = viftable[vifi].v_pkt_out;
626 req->ibytes = viftable[vifi].v_bytes_in;
627 req->obytes = viftable[vifi].v_bytes_out;
634 ip_mrouter_reset(void)
636 bzero((caddr_t)mfctable, sizeof(mfctable));
637 bzero((caddr_t)nexpire, sizeof(nexpire));
642 callout_init(&expire_upcalls_ch, NET_CALLOUT_MPSAFE);
645 bzero((caddr_t)bw_meter_timers, sizeof(bw_meter_timers));
646 callout_init(&bw_upcalls_ch, NET_CALLOUT_MPSAFE);
647 callout_init(&bw_meter_ch, NET_CALLOUT_MPSAFE);
649 callout_init(&tbf_reprocess_ch, NET_CALLOUT_MPSAFE);
652 static struct mtx mrouter_mtx; /* used to synch init/done work */
655 * Enable multicast routing
658 ip_mrouter_init(struct socket *so, int version)
661 log(LOG_DEBUG, "ip_mrouter_init: so_type = %d, pr_protocol = %d\n",
662 so->so_type, so->so_proto->pr_protocol);
664 if (so->so_type != SOCK_RAW || so->so_proto->pr_protocol != IPPROTO_IGMP)
670 mtx_lock(&mrouter_mtx);
672 if (ip_mrouter != NULL) {
673 mtx_unlock(&mrouter_mtx);
677 callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT, expire_upcalls, NULL);
679 callout_reset(&bw_upcalls_ch, BW_UPCALLS_PERIOD,
680 expire_bw_upcalls_send, NULL);
681 callout_reset(&bw_meter_ch, BW_METER_PERIOD, expire_bw_meter_process, NULL);
685 mtx_unlock(&mrouter_mtx);
688 log(LOG_DEBUG, "ip_mrouter_init\n");
694 * Disable multicast routing
697 X_ip_mrouter_done(void)
706 mtx_lock(&mrouter_mtx);
708 if (ip_mrouter == NULL) {
709 mtx_unlock(&mrouter_mtx);
714 * Detach/disable hooks to the reset of the system.
721 const struct encaptab *c = encap_cookie;
727 callout_stop(&tbf_reprocess_ch);
731 * For each phyint in use, disable promiscuous reception of all IP
734 for (vifi = 0; vifi < numvifs; vifi++) {
735 if (viftable[vifi].v_lcl_addr.s_addr != 0 &&
736 !(viftable[vifi].v_flags & (VIFF_TUNNEL | VIFF_REGISTER))) {
737 struct sockaddr_in *so = (struct sockaddr_in *)&(ifr.ifr_addr);
739 so->sin_len = sizeof(struct sockaddr_in);
740 so->sin_family = AF_INET;
741 so->sin_addr.s_addr = INADDR_ANY;
742 ifp = viftable[vifi].v_ifp;
746 bzero((caddr_t)tbftable, sizeof(tbftable));
747 bzero((caddr_t)viftable, sizeof(viftable));
753 * Free all multicast forwarding cache entries.
755 callout_stop(&expire_upcalls_ch);
756 callout_stop(&bw_upcalls_ch);
757 callout_stop(&bw_meter_ch);
760 for (i = 0; i < MFCTBLSIZ; i++) {
761 for (rt = mfctable[i]; rt != NULL; ) {
762 struct mfc *nr = rt->mfc_next;
764 for (rte = rt->mfc_stall; rte != NULL; ) {
765 struct rtdetq *n = rte->next;
768 free(rte, M_MRTABLE);
771 free_bw_list(rt->mfc_bw_meter);
776 bzero((caddr_t)mfctable, sizeof(mfctable));
777 bzero((caddr_t)nexpire, sizeof(nexpire));
779 bzero(bw_meter_timers, sizeof(bw_meter_timers));
783 * Reset de-encapsulation cache
785 last_encap_src = INADDR_ANY;
786 last_encap_vif = NULL;
788 reg_vif_num = VIFI_INVALID;
791 mtx_unlock(&mrouter_mtx);
794 log(LOG_DEBUG, "ip_mrouter_done\n");
800 * Set PIM assert processing global
805 if ((i != 1) && (i != 0))
814 * Configure API capabilities
817 set_api_config(uint32_t *apival)
822 * We can set the API capabilities only if it is the first operation
823 * after MRT_INIT. I.e.:
824 * - there are no vifs installed
825 * - pim_assert is not enabled
826 * - the MFC table is empty
836 for (i = 0; i < MFCTBLSIZ; i++) {
837 if (mfctable[i] != NULL) {
843 mrt_api_config = *apival & mrt_api_support;
844 *apival = mrt_api_config;
850 * Decide if a packet is from a tunnelled peer.
851 * Return 0 if not, 64 if so. XXX yuck.. 64 ???
854 mroute_encapcheck(const struct mbuf *m, int off, int proto, void *arg)
856 struct ip *ip = mtod(m, struct ip *);
857 int hlen = ip->ip_hl << 2;
860 * don't claim the packet if it's not to a multicast destination or if
861 * we don't have an encapsulating tunnel with the source.
862 * Note: This code assumes that the remote site IP address
863 * uniquely identifies the tunnel (i.e., that this site has
864 * at most one tunnel with the remote site).
866 if (!IN_MULTICAST(ntohl(((struct ip *)((char *)ip+hlen))->ip_dst.s_addr)))
868 if (ip->ip_src.s_addr != last_encap_src) {
869 struct vif *vifp = viftable;
870 struct vif *vife = vifp + numvifs;
872 last_encap_src = ip->ip_src.s_addr;
873 last_encap_vif = NULL;
874 for ( ; vifp < vife; ++vifp)
875 if (vifp->v_rmt_addr.s_addr == ip->ip_src.s_addr) {
876 if ((vifp->v_flags & (VIFF_TUNNEL|VIFF_SRCRT)) == VIFF_TUNNEL)
877 last_encap_vif = vifp;
881 if (last_encap_vif == NULL) {
882 last_encap_src = INADDR_ANY;
889 * De-encapsulate a packet and feed it back through ip input (this
890 * routine is called whenever IP gets a packet that mroute_encap_func()
894 mroute_encap_input(struct mbuf *m, int off)
896 struct ip *ip = mtod(m, struct ip *);
897 int hlen = ip->ip_hl << 2;
899 if (hlen > sizeof(struct ip))
900 ip_stripoptions(m, (struct mbuf *) 0);
901 m->m_data += sizeof(struct ip);
902 m->m_len -= sizeof(struct ip);
903 m->m_pkthdr.len -= sizeof(struct ip);
905 m->m_pkthdr.rcvif = last_encap_vif->v_ifp;
907 netisr_queue(NETISR_IP, m); /* mbuf is free'd on failure. */
909 * normally we would need a "schednetisr(NETISR_IP)"
910 * here but we were called by ip_input and it is going
911 * to loop back & try to dequeue the packet we just
912 * queued as soon as we return so we avoid the
913 * unnecessary software interrrupt.
916 * This no longer holds - we may have direct-dispatched the packet,
917 * or there may be a queue processing limit.
921 extern struct domain inetdomain;
922 static struct protosw mroute_encap_protosw =
923 { SOCK_RAW, &inetdomain, IPPROTO_IPV4, PR_ATOMIC|PR_ADDR,
924 mroute_encap_input, 0, 0, rip_ctloutput,
931 * Add a vif to the vif table
934 add_vif(struct vifctl *vifcp)
936 struct vif *vifp = viftable + vifcp->vifc_vifi;
937 struct sockaddr_in sin = {sizeof sin, AF_INET};
941 struct tbf *v_tbf = tbftable + vifcp->vifc_vifi;
944 if (vifcp->vifc_vifi >= MAXVIFS) {
948 if (vifp->v_lcl_addr.s_addr != INADDR_ANY) {
952 if (vifcp->vifc_lcl_addr.s_addr == INADDR_ANY) {
954 return EADDRNOTAVAIL;
957 /* Find the interface with an address in AF_INET family */
959 if (vifcp->vifc_flags & VIFF_REGISTER) {
961 * XXX: Because VIFF_REGISTER does not really need a valid
962 * local interface (e.g. it could be 127.0.0.2), we don't
969 sin.sin_addr = vifcp->vifc_lcl_addr;
970 ifa = ifa_ifwithaddr((struct sockaddr *)&sin);
973 return EADDRNOTAVAIL;
978 if (vifcp->vifc_flags & VIFF_TUNNEL) {
979 if ((vifcp->vifc_flags & VIFF_SRCRT) == 0) {
981 * An encapsulating tunnel is wanted. Tell
982 * mroute_encap_input() to start paying attention
983 * to encapsulated packets.
985 if (encap_cookie == NULL) {
988 encap_cookie = encap_attach_func(AF_INET, IPPROTO_IPV4,
990 (struct protosw *)&mroute_encap_protosw, NULL);
992 if (encap_cookie == NULL) {
993 printf("ip_mroute: unable to attach encap\n");
995 return EIO; /* XXX */
997 for (i = 0; i < MAXVIFS; ++i) {
998 if_initname(&multicast_decap_if[i], "mdecap", i);
1002 * Set interface to fake encapsulator interface
1004 ifp = &multicast_decap_if[vifcp->vifc_vifi];
1006 * Prepare cached route entry
1008 bzero(&vifp->v_route, sizeof(vifp->v_route));
1010 log(LOG_ERR, "source routed tunnels not supported\n");
1015 } else if (vifcp->vifc_flags & VIFF_REGISTER) {
1016 ifp = &multicast_register_if;
1018 log(LOG_DEBUG, "Adding a register vif, ifp: %p\n",
1019 (void *)&multicast_register_if);
1020 if (reg_vif_num == VIFI_INVALID) {
1021 if_initname(&multicast_register_if, "register_vif", 0);
1022 multicast_register_if.if_flags = IFF_LOOPBACK;
1023 bzero(&vifp->v_route, sizeof(vifp->v_route));
1024 reg_vif_num = vifcp->vifc_vifi;
1027 } else { /* Make sure the interface supports multicast */
1028 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
1033 /* Enable promiscuous reception of all IP multicasts from the if */
1034 error = if_allmulti(ifp, 1);
1041 /* define parameters for the tbf structure */
1042 vifp->v_tbf = v_tbf;
1043 GET_TIME(vifp->v_tbf->tbf_last_pkt_t);
1044 vifp->v_tbf->tbf_n_tok = 0;
1045 vifp->v_tbf->tbf_q_len = 0;
1046 vifp->v_tbf->tbf_max_q_len = MAXQSIZE;
1047 vifp->v_tbf->tbf_q = vifp->v_tbf->tbf_t = NULL;
1049 vifp->v_flags = vifcp->vifc_flags;
1050 vifp->v_threshold = vifcp->vifc_threshold;
1051 vifp->v_lcl_addr = vifcp->vifc_lcl_addr;
1052 vifp->v_rmt_addr = vifcp->vifc_rmt_addr;
1054 /* scaling up here allows division by 1024 in critical code */
1055 vifp->v_rate_limit= vifcp->vifc_rate_limit * 1024 / 1000;
1056 vifp->v_rsvp_on = 0;
1057 vifp->v_rsvpd = NULL;
1058 /* initialize per vif pkt counters */
1060 vifp->v_pkt_out = 0;
1061 vifp->v_bytes_in = 0;
1062 vifp->v_bytes_out = 0;
1064 /* Adjust numvifs up if the vifi is higher than numvifs */
1065 if (numvifs <= vifcp->vifc_vifi) numvifs = vifcp->vifc_vifi + 1;
1070 log(LOG_DEBUG, "add_vif #%d, lcladdr %lx, %s %lx, thresh %x, rate %d\n",
1072 (u_long)ntohl(vifcp->vifc_lcl_addr.s_addr),
1073 (vifcp->vifc_flags & VIFF_TUNNEL) ? "rmtaddr" : "mask",
1074 (u_long)ntohl(vifcp->vifc_rmt_addr.s_addr),
1075 vifcp->vifc_threshold,
1076 vifcp->vifc_rate_limit);
1082 * Delete a vif from the vif table
1085 del_vif(vifi_t vifi)
1091 if (vifi >= numvifs) {
1095 vifp = &viftable[vifi];
1096 if (vifp->v_lcl_addr.s_addr == INADDR_ANY) {
1098 return EADDRNOTAVAIL;
1101 if (!(vifp->v_flags & (VIFF_TUNNEL | VIFF_REGISTER)))
1102 if_allmulti(vifp->v_ifp, 0);
1104 if (vifp == last_encap_vif) {
1105 last_encap_vif = NULL;
1106 last_encap_src = INADDR_ANY;
1110 * Free packets queued at the interface
1112 while (vifp->v_tbf->tbf_q) {
1113 struct mbuf *m = vifp->v_tbf->tbf_q;
1115 vifp->v_tbf->tbf_q = m->m_act;
1120 if (vifp->v_flags & VIFF_REGISTER)
1121 reg_vif_num = VIFI_INVALID;
1124 bzero((caddr_t)vifp->v_tbf, sizeof(*(vifp->v_tbf)));
1125 bzero((caddr_t)vifp, sizeof (*vifp));
1128 log(LOG_DEBUG, "del_vif %d, numvifs %d\n", vifi, numvifs);
1130 /* Adjust numvifs down */
1131 for (vifi = numvifs; vifi > 0; vifi--)
1132 if (viftable[vifi-1].v_lcl_addr.s_addr != INADDR_ANY)
1142 * update an mfc entry without resetting counters and S,G addresses.
1145 update_mfc_params(struct mfc *rt, struct mfcctl2 *mfccp)
1149 rt->mfc_parent = mfccp->mfcc_parent;
1150 for (i = 0; i < numvifs; i++) {
1151 rt->mfc_ttls[i] = mfccp->mfcc_ttls[i];
1152 rt->mfc_flags[i] = mfccp->mfcc_flags[i] & mrt_api_config &
1155 /* set the RP address */
1156 if (mrt_api_config & MRT_MFC_RP)
1157 rt->mfc_rp = mfccp->mfcc_rp;
1159 rt->mfc_rp.s_addr = INADDR_ANY;
1163 * fully initialize an mfc entry from the parameter.
1166 init_mfc_params(struct mfc *rt, struct mfcctl2 *mfccp)
1168 rt->mfc_origin = mfccp->mfcc_origin;
1169 rt->mfc_mcastgrp = mfccp->mfcc_mcastgrp;
1171 update_mfc_params(rt, mfccp);
1173 /* initialize pkt counters per src-grp */
1174 rt->mfc_pkt_cnt = 0;
1175 rt->mfc_byte_cnt = 0;
1176 rt->mfc_wrong_if = 0;
1177 rt->mfc_last_assert.tv_sec = rt->mfc_last_assert.tv_usec = 0;
1185 add_mfc(struct mfcctl2 *mfccp)
1195 rt = mfc_find(mfccp->mfcc_origin.s_addr, mfccp->mfcc_mcastgrp.s_addr);
1197 /* If an entry already exists, just update the fields */
1199 if (mrtdebug & DEBUG_MFC)
1200 log(LOG_DEBUG,"add_mfc update o %lx g %lx p %x\n",
1201 (u_long)ntohl(mfccp->mfcc_origin.s_addr),
1202 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
1203 mfccp->mfcc_parent);
1205 update_mfc_params(rt, mfccp);
1212 * Find the entry for which the upcall was made and update
1214 hash = MFCHASH(mfccp->mfcc_origin.s_addr, mfccp->mfcc_mcastgrp.s_addr);
1215 for (rt = mfctable[hash], nstl = 0; rt; rt = rt->mfc_next) {
1217 if ((rt->mfc_origin.s_addr == mfccp->mfcc_origin.s_addr) &&
1218 (rt->mfc_mcastgrp.s_addr == mfccp->mfcc_mcastgrp.s_addr) &&
1219 (rt->mfc_stall != NULL)) {
1222 log(LOG_ERR, "add_mfc %s o %lx g %lx p %x dbx %p\n",
1223 "multiple kernel entries",
1224 (u_long)ntohl(mfccp->mfcc_origin.s_addr),
1225 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
1226 mfccp->mfcc_parent, (void *)rt->mfc_stall);
1228 if (mrtdebug & DEBUG_MFC)
1229 log(LOG_DEBUG,"add_mfc o %lx g %lx p %x dbg %p\n",
1230 (u_long)ntohl(mfccp->mfcc_origin.s_addr),
1231 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
1232 mfccp->mfcc_parent, (void *)rt->mfc_stall);
1234 init_mfc_params(rt, mfccp);
1236 rt->mfc_expire = 0; /* Don't clean this guy up */
1239 /* free packets Qed at the end of this entry */
1240 for (rte = rt->mfc_stall; rte != NULL; ) {
1241 struct rtdetq *n = rte->next;
1243 ip_mdq(rte->m, rte->ifp, rt, -1);
1245 free(rte, M_MRTABLE);
1248 rt->mfc_stall = NULL;
1253 * It is possible that an entry is being inserted without an upcall
1256 if (mrtdebug & DEBUG_MFC)
1257 log(LOG_DEBUG,"add_mfc no upcall h %lu o %lx g %lx p %x\n",
1258 hash, (u_long)ntohl(mfccp->mfcc_origin.s_addr),
1259 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
1260 mfccp->mfcc_parent);
1262 for (rt = mfctable[hash]; rt != NULL; rt = rt->mfc_next) {
1263 if ((rt->mfc_origin.s_addr == mfccp->mfcc_origin.s_addr) &&
1264 (rt->mfc_mcastgrp.s_addr == mfccp->mfcc_mcastgrp.s_addr)) {
1265 init_mfc_params(rt, mfccp);
1272 if (rt == NULL) { /* no upcall, so make a new entry */
1273 rt = (struct mfc *)malloc(sizeof(*rt), M_MRTABLE, M_NOWAIT);
1280 init_mfc_params(rt, mfccp);
1282 rt->mfc_stall = NULL;
1284 rt->mfc_bw_meter = NULL;
1285 /* insert new entry at head of hash chain */
1286 rt->mfc_next = mfctable[hash];
1287 mfctable[hash] = rt;
1296 * Delete an mfc entry
1299 del_mfc(struct mfcctl2 *mfccp)
1301 struct in_addr origin;
1302 struct in_addr mcastgrp;
1306 struct bw_meter *list;
1308 origin = mfccp->mfcc_origin;
1309 mcastgrp = mfccp->mfcc_mcastgrp;
1311 if (mrtdebug & DEBUG_MFC)
1312 log(LOG_DEBUG,"del_mfc orig %lx mcastgrp %lx\n",
1313 (u_long)ntohl(origin.s_addr), (u_long)ntohl(mcastgrp.s_addr));
1317 hash = MFCHASH(origin.s_addr, mcastgrp.s_addr);
1318 for (nptr = &mfctable[hash]; (rt = *nptr) != NULL; nptr = &rt->mfc_next)
1319 if (origin.s_addr == rt->mfc_origin.s_addr &&
1320 mcastgrp.s_addr == rt->mfc_mcastgrp.s_addr &&
1321 rt->mfc_stall == NULL)
1325 return EADDRNOTAVAIL;
1328 *nptr = rt->mfc_next;
1331 * free the bw_meter entries
1333 list = rt->mfc_bw_meter;
1334 rt->mfc_bw_meter = NULL;
1336 free(rt, M_MRTABLE);
1346 * Send a message to mrouted on the multicast routing socket
1349 socket_send(struct socket *s, struct mbuf *mm, struct sockaddr_in *src)
1352 SOCKBUF_LOCK(&s->so_rcv);
1353 if (sbappendaddr_locked(&s->so_rcv, (struct sockaddr *)src, mm,
1355 sorwakeup_locked(s);
1358 SOCKBUF_UNLOCK(&s->so_rcv);
1365 * IP multicast forwarding function. This function assumes that the packet
1366 * pointed to by "ip" has arrived on (or is about to be sent to) the interface
1367 * pointed to by "ifp", and the packet is to be relayed to other networks
1368 * that have members of the packet's destination IP multicast group.
1370 * The packet is returned unscathed to the caller, unless it is
1371 * erroneous, in which case a non-zero return value tells the caller to
1375 #define TUNNEL_LEN 12 /* # bytes of IP option for tunnel encapsulation */
1378 X_ip_mforward(struct ip *ip, struct ifnet *ifp, struct mbuf *m,
1379 struct ip_moptions *imo)
1385 if (mrtdebug & DEBUG_FORWARD)
1386 log(LOG_DEBUG, "ip_mforward: src %lx, dst %lx, ifp %p\n",
1387 (u_long)ntohl(ip->ip_src.s_addr), (u_long)ntohl(ip->ip_dst.s_addr),
1390 if (ip->ip_hl < (sizeof(struct ip) + TUNNEL_LEN) >> 2 ||
1391 ((u_char *)(ip + 1))[1] != IPOPT_LSRR ) {
1393 * Packet arrived via a physical interface or
1394 * an encapsulated tunnel or a register_vif.
1398 * Packet arrived through a source-route tunnel.
1399 * Source-route tunnels are no longer supported.
1401 static int last_log;
1402 if (last_log != time_uptime) {
1403 last_log = time_uptime;
1405 "ip_mforward: received source-routed packet from %lx\n",
1406 (u_long)ntohl(ip->ip_src.s_addr));
1413 if (imo && ((vifi = imo->imo_multicast_vif) < numvifs)) {
1414 if (ip->ip_ttl < 255)
1415 ip->ip_ttl++; /* compensate for -1 in *_send routines */
1416 if (rsvpdebug && ip->ip_p == IPPROTO_RSVP) {
1417 struct vif *vifp = viftable + vifi;
1419 printf("Sending IPPROTO_RSVP from %lx to %lx on vif %d (%s%s)\n",
1420 (long)ntohl(ip->ip_src.s_addr), (long)ntohl(ip->ip_dst.s_addr),
1422 (vifp->v_flags & VIFF_TUNNEL) ? "tunnel on " : "",
1423 vifp->v_ifp->if_xname);
1425 error = ip_mdq(m, ifp, NULL, vifi);
1430 if (rsvpdebug && ip->ip_p == IPPROTO_RSVP) {
1431 printf("Warning: IPPROTO_RSVP from %lx to %lx without vif option\n",
1432 (long)ntohl(ip->ip_src.s_addr), (long)ntohl(ip->ip_dst.s_addr));
1434 printf("In fact, no options were specified at all\n");
1438 * Don't forward a packet with time-to-live of zero or one,
1439 * or a packet destined to a local-only group.
1441 if (ip->ip_ttl <= 1 || ntohl(ip->ip_dst.s_addr) <= INADDR_MAX_LOCAL_GROUP) {
1448 * Determine forwarding vifs from the forwarding cache table
1450 ++mrtstat.mrts_mfc_lookups;
1451 rt = mfc_find(ip->ip_src.s_addr, ip->ip_dst.s_addr);
1453 /* Entry exists, so forward if necessary */
1455 error = ip_mdq(m, ifp, rt, -1);
1461 * If we don't have a route for packet's origin,
1462 * Make a copy of the packet & send message to routing daemon
1468 int hlen = ip->ip_hl << 2;
1470 ++mrtstat.mrts_mfc_misses;
1472 mrtstat.mrts_no_route++;
1473 if (mrtdebug & (DEBUG_FORWARD | DEBUG_MFC))
1474 log(LOG_DEBUG, "ip_mforward: no rte s %lx g %lx\n",
1475 (u_long)ntohl(ip->ip_src.s_addr),
1476 (u_long)ntohl(ip->ip_dst.s_addr));
1479 * Allocate mbufs early so that we don't do extra work if we are
1480 * just going to fail anyway. Make sure to pullup the header so
1481 * that other people can't step on it.
1483 rte = (struct rtdetq *)malloc((sizeof *rte), M_MRTABLE, M_NOWAIT);
1489 mb0 = m_copypacket(m, M_DONTWAIT);
1490 if (mb0 && (M_HASCL(mb0) || mb0->m_len < hlen))
1491 mb0 = m_pullup(mb0, hlen);
1493 free(rte, M_MRTABLE);
1499 /* is there an upcall waiting for this flow ? */
1500 hash = MFCHASH(ip->ip_src.s_addr, ip->ip_dst.s_addr);
1501 for (rt = mfctable[hash]; rt; rt = rt->mfc_next) {
1502 if ((ip->ip_src.s_addr == rt->mfc_origin.s_addr) &&
1503 (ip->ip_dst.s_addr == rt->mfc_mcastgrp.s_addr) &&
1504 (rt->mfc_stall != NULL))
1511 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
1515 * Locate the vifi for the incoming interface for this packet.
1516 * If none found, drop packet.
1518 for (vifi=0; vifi < numvifs && viftable[vifi].v_ifp != ifp; vifi++)
1520 if (vifi >= numvifs) /* vif not found, drop packet */
1523 /* no upcall, so make a new entry */
1524 rt = (struct mfc *)malloc(sizeof(*rt), M_MRTABLE, M_NOWAIT);
1527 /* Make a copy of the header to send to the user level process */
1528 mm = m_copy(mb0, 0, hlen);
1533 * Send message to routing daemon to install
1534 * a route into the kernel table
1537 im = mtod(mm, struct igmpmsg *);
1538 im->im_msgtype = IGMPMSG_NOCACHE;
1542 mrtstat.mrts_upcalls++;
1544 k_igmpsrc.sin_addr = ip->ip_src;
1545 if (socket_send(ip_mrouter, mm, &k_igmpsrc) < 0) {
1546 log(LOG_WARNING, "ip_mforward: ip_mrouter socket queue full\n");
1547 ++mrtstat.mrts_upq_sockfull;
1549 free(rt, M_MRTABLE);
1551 free(rte, M_MRTABLE);
1558 /* insert new entry at head of hash chain */
1559 rt->mfc_origin.s_addr = ip->ip_src.s_addr;
1560 rt->mfc_mcastgrp.s_addr = ip->ip_dst.s_addr;
1561 rt->mfc_expire = UPCALL_EXPIRE;
1563 for (i = 0; i < numvifs; i++) {
1564 rt->mfc_ttls[i] = 0;
1565 rt->mfc_flags[i] = 0;
1567 rt->mfc_parent = -1;
1569 rt->mfc_rp.s_addr = INADDR_ANY; /* clear the RP address */
1571 rt->mfc_bw_meter = NULL;
1573 /* link into table */
1574 rt->mfc_next = mfctable[hash];
1575 mfctable[hash] = rt;
1576 rt->mfc_stall = rte;
1579 /* determine if q has overflowed */
1584 * XXX ouch! we need to append to the list, but we
1585 * only have a pointer to the front, so we have to
1586 * scan the entire list every time.
1588 for (p = &rt->mfc_stall; *p != NULL; p = &(*p)->next)
1591 if (npkts > MAX_UPQ) {
1592 mrtstat.mrts_upq_ovflw++;
1594 free(rte, M_MRTABLE);
1601 /* Add this entry to the end of the queue */
1617 * Clean up the cache entry if upcall is not serviced
1620 expire_upcalls(void *unused)
1623 struct mfc *mfc, **nptr;
1627 for (i = 0; i < MFCTBLSIZ; i++) {
1628 if (nexpire[i] == 0)
1630 nptr = &mfctable[i];
1631 for (mfc = *nptr; mfc != NULL; mfc = *nptr) {
1633 * Skip real cache entries
1634 * Make sure it wasn't marked to not expire (shouldn't happen)
1637 if (mfc->mfc_stall != NULL && mfc->mfc_expire != 0 &&
1638 --mfc->mfc_expire == 0) {
1639 if (mrtdebug & DEBUG_EXPIRE)
1640 log(LOG_DEBUG, "expire_upcalls: expiring (%lx %lx)\n",
1641 (u_long)ntohl(mfc->mfc_origin.s_addr),
1642 (u_long)ntohl(mfc->mfc_mcastgrp.s_addr));
1644 * drop all the packets
1645 * free the mbuf with the pkt, if, timing info
1647 for (rte = mfc->mfc_stall; rte; ) {
1648 struct rtdetq *n = rte->next;
1651 free(rte, M_MRTABLE);
1654 ++mrtstat.mrts_cache_cleanups;
1658 * free the bw_meter entries
1660 while (mfc->mfc_bw_meter != NULL) {
1661 struct bw_meter *x = mfc->mfc_bw_meter;
1663 mfc->mfc_bw_meter = x->bm_mfc_next;
1667 *nptr = mfc->mfc_next;
1668 free(mfc, M_MRTABLE);
1670 nptr = &mfc->mfc_next;
1676 callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT, expire_upcalls, NULL);
1680 * Packet forwarding routine once entry in the cache is made
1683 ip_mdq(struct mbuf *m, struct ifnet *ifp, struct mfc *rt, vifi_t xmt_vif)
1685 struct ip *ip = mtod(m, struct ip *);
1687 int plen = ip->ip_len;
1691 * Macro to send packet on vif. Since RSVP packets don't get counted on
1692 * input, they shouldn't get counted on output, so statistics keeping is
1695 #define MC_SEND(ip,vifp,m) { \
1696 if ((vifp)->v_flags & VIFF_TUNNEL) \
1697 encap_send((ip), (vifp), (m)); \
1699 phyint_send((ip), (vifp), (m)); \
1703 * If xmt_vif is not -1, send on only the requested vif.
1705 * (since vifi_t is u_short, -1 becomes MAXUSHORT, which > numvifs.)
1707 if (xmt_vif < numvifs) {
1709 if (viftable[xmt_vif].v_flags & VIFF_REGISTER)
1710 pim_register_send(ip, viftable + xmt_vif, m, rt);
1713 MC_SEND(ip, viftable + xmt_vif, m);
1718 * Don't forward if it didn't arrive from the parent vif for its origin.
1720 vifi = rt->mfc_parent;
1721 if ((vifi >= numvifs) || (viftable[vifi].v_ifp != ifp)) {
1722 /* came in the wrong interface */
1723 if (mrtdebug & DEBUG_FORWARD)
1724 log(LOG_DEBUG, "wrong if: ifp %p vifi %d vififp %p\n",
1725 (void *)ifp, vifi, (void *)viftable[vifi].v_ifp);
1726 ++mrtstat.mrts_wrong_if;
1729 * If we are doing PIM assert processing, send a message
1730 * to the routing daemon.
1732 * XXX: A PIM-SM router needs the WRONGVIF detection so it
1733 * can complete the SPT switch, regardless of the type
1734 * of the iif (broadcast media, GRE tunnel, etc).
1736 if (pim_assert && (vifi < numvifs) && viftable[vifi].v_ifp) {
1741 if (ifp == &multicast_register_if)
1742 pimstat.pims_rcv_registers_wrongiif++;
1745 /* Get vifi for the incoming packet */
1746 for (vifi=0; vifi < numvifs && viftable[vifi].v_ifp != ifp; vifi++)
1748 if (vifi >= numvifs)
1749 return 0; /* The iif is not found: ignore the packet. */
1751 if (rt->mfc_flags[vifi] & MRT_MFC_FLAGS_DISABLE_WRONGVIF)
1752 return 0; /* WRONGVIF disabled: ignore the packet */
1756 TV_DELTA(rt->mfc_last_assert, now, delta);
1758 if (delta > ASSERT_MSG_TIME) {
1759 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
1761 int hlen = ip->ip_hl << 2;
1762 struct mbuf *mm = m_copy(m, 0, hlen);
1764 if (mm && (M_HASCL(mm) || mm->m_len < hlen))
1765 mm = m_pullup(mm, hlen);
1769 rt->mfc_last_assert = now;
1771 im = mtod(mm, struct igmpmsg *);
1772 im->im_msgtype = IGMPMSG_WRONGVIF;
1776 mrtstat.mrts_upcalls++;
1778 k_igmpsrc.sin_addr = im->im_src;
1779 if (socket_send(ip_mrouter, mm, &k_igmpsrc) < 0) {
1781 "ip_mforward: ip_mrouter socket queue full\n");
1782 ++mrtstat.mrts_upq_sockfull;
1790 /* If I sourced this packet, it counts as output, else it was input. */
1791 if (ip->ip_src.s_addr == viftable[vifi].v_lcl_addr.s_addr) {
1792 viftable[vifi].v_pkt_out++;
1793 viftable[vifi].v_bytes_out += plen;
1795 viftable[vifi].v_pkt_in++;
1796 viftable[vifi].v_bytes_in += plen;
1799 rt->mfc_byte_cnt += plen;
1802 * For each vif, decide if a copy of the packet should be forwarded.
1804 * - the ttl exceeds the vif's threshold
1805 * - there are group members downstream on interface
1807 for (vifi = 0; vifi < numvifs; vifi++)
1808 if ((rt->mfc_ttls[vifi] > 0) && (ip->ip_ttl > rt->mfc_ttls[vifi])) {
1809 viftable[vifi].v_pkt_out++;
1810 viftable[vifi].v_bytes_out += plen;
1812 if (viftable[vifi].v_flags & VIFF_REGISTER)
1813 pim_register_send(ip, viftable + vifi, m, rt);
1816 MC_SEND(ip, viftable+vifi, m);
1820 * Perform upcall-related bw measuring.
1822 if (rt->mfc_bw_meter != NULL) {
1828 for (x = rt->mfc_bw_meter; x != NULL; x = x->bm_mfc_next)
1829 bw_meter_receive_packet(x, plen, &now);
1836 * check if a vif number is legal/ok. This is used by ip_output.
1839 X_legal_vif_num(int vif)
1841 /* XXX unlocked, matter? */
1842 return (vif >= 0 && vif < numvifs);
1846 * Return the local address used by this vif
1849 X_ip_mcast_src(int vifi)
1851 /* XXX unlocked, matter? */
1852 if (vifi >= 0 && vifi < numvifs)
1853 return viftable[vifi].v_lcl_addr.s_addr;
1859 phyint_send(struct ip *ip, struct vif *vifp, struct mbuf *m)
1861 struct mbuf *mb_copy;
1862 int hlen = ip->ip_hl << 2;
1867 * Make a new reference to the packet; make sure that
1868 * the IP header is actually copied, not just referenced,
1869 * so that ip_output() only scribbles on the copy.
1871 mb_copy = m_copypacket(m, M_DONTWAIT);
1872 if (mb_copy && (M_HASCL(mb_copy) || mb_copy->m_len < hlen))
1873 mb_copy = m_pullup(mb_copy, hlen);
1874 if (mb_copy == NULL)
1877 if (vifp->v_rate_limit == 0)
1878 tbf_send_packet(vifp, mb_copy);
1880 tbf_control(vifp, mb_copy, mtod(mb_copy, struct ip *), ip->ip_len);
1884 encap_send(struct ip *ip, struct vif *vifp, struct mbuf *m)
1886 struct mbuf *mb_copy;
1888 int i, len = ip->ip_len;
1892 /* Take care of delayed checksums */
1893 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
1894 in_delayed_cksum(m);
1895 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
1899 * copy the old packet & pullup its IP header into the
1900 * new mbuf so we can modify it. Try to fill the new
1901 * mbuf since if we don't the ethernet driver will.
1903 MGETHDR(mb_copy, M_DONTWAIT, MT_HEADER);
1904 if (mb_copy == NULL)
1907 mac_create_mbuf_multicast_encap(m, vifp->v_ifp, mb_copy);
1909 mb_copy->m_data += max_linkhdr;
1910 mb_copy->m_len = sizeof(multicast_encap_iphdr);
1912 if ((mb_copy->m_next = m_copypacket(m, M_DONTWAIT)) == NULL) {
1916 i = MHLEN - M_LEADINGSPACE(mb_copy);
1919 mb_copy = m_pullup(mb_copy, i);
1920 if (mb_copy == NULL)
1922 mb_copy->m_pkthdr.len = len + sizeof(multicast_encap_iphdr);
1925 * fill in the encapsulating IP header.
1927 ip_copy = mtod(mb_copy, struct ip *);
1928 *ip_copy = multicast_encap_iphdr;
1929 ip_copy->ip_id = ip_newid();
1930 ip_copy->ip_len += len;
1931 ip_copy->ip_src = vifp->v_lcl_addr;
1932 ip_copy->ip_dst = vifp->v_rmt_addr;
1935 * turn the encapsulated IP header back into a valid one.
1937 ip = (struct ip *)((caddr_t)ip_copy + sizeof(multicast_encap_iphdr));
1939 ip->ip_len = htons(ip->ip_len);
1940 ip->ip_off = htons(ip->ip_off);
1942 mb_copy->m_data += sizeof(multicast_encap_iphdr);
1943 ip->ip_sum = in_cksum(mb_copy, ip->ip_hl << 2);
1944 mb_copy->m_data -= sizeof(multicast_encap_iphdr);
1946 if (vifp->v_rate_limit == 0)
1947 tbf_send_packet(vifp, mb_copy);
1949 tbf_control(vifp, mb_copy, ip, ip_copy->ip_len);
1953 * Token bucket filter module
1957 tbf_control(struct vif *vifp, struct mbuf *m, struct ip *ip, u_long p_len)
1959 struct tbf *t = vifp->v_tbf;
1963 if (p_len > MAX_BKT_SIZE) { /* drop if packet is too large */
1964 mrtstat.mrts_pkt2large++;
1969 tbf_update_tokens(vifp);
1971 if (t->tbf_q_len == 0) { /* queue empty... */
1972 if (p_len <= t->tbf_n_tok) { /* send packet if enough tokens */
1973 t->tbf_n_tok -= p_len;
1974 tbf_send_packet(vifp, m);
1975 } else { /* no, queue packet and try later */
1977 callout_reset(&tbf_reprocess_ch, TBF_REPROCESS,
1978 tbf_reprocess_q, vifp);
1980 } else if (t->tbf_q_len < t->tbf_max_q_len) {
1981 /* finite queue length, so queue pkts and process queue */
1983 tbf_process_q(vifp);
1985 /* queue full, try to dq and queue and process */
1986 if (!tbf_dq_sel(vifp, ip)) {
1987 mrtstat.mrts_q_overflow++;
1991 tbf_process_q(vifp);
1997 * adds a packet to the queue at the interface
2000 tbf_queue(struct vif *vifp, struct mbuf *m)
2002 struct tbf *t = vifp->v_tbf;
2006 if (t->tbf_t == NULL) /* Queue was empty */
2008 else /* Insert at tail */
2009 t->tbf_t->m_act = m;
2011 t->tbf_t = m; /* Set new tail pointer */
2014 /* Make sure we didn't get fed a bogus mbuf */
2016 panic("tbf_queue: m_act");
2024 * processes the queue at the interface
2027 tbf_process_q(struct vif *vifp)
2029 struct tbf *t = vifp->v_tbf;
2033 /* loop through the queue at the interface and send as many packets
2036 while (t->tbf_q_len > 0) {
2037 struct mbuf *m = t->tbf_q;
2038 int len = mtod(m, struct ip *)->ip_len;
2040 /* determine if the packet can be sent */
2041 if (len > t->tbf_n_tok) /* not enough tokens, we are done */
2043 /* ok, reduce no of tokens, dequeue and send the packet. */
2044 t->tbf_n_tok -= len;
2046 t->tbf_q = m->m_act;
2047 if (--t->tbf_q_len == 0)
2051 tbf_send_packet(vifp, m);
2056 tbf_reprocess_q(void *xvifp)
2058 struct vif *vifp = xvifp;
2060 if (ip_mrouter == NULL)
2063 tbf_update_tokens(vifp);
2064 tbf_process_q(vifp);
2065 if (vifp->v_tbf->tbf_q_len)
2066 callout_reset(&tbf_reprocess_ch, TBF_REPROCESS, tbf_reprocess_q, vifp);
2070 /* function that will selectively discard a member of the queue
2071 * based on the precedence value and the priority
2074 tbf_dq_sel(struct vif *vifp, struct ip *ip)
2077 struct mbuf *m, *last;
2079 struct tbf *t = vifp->v_tbf;
2083 p = priority(vifp, ip);
2087 while ((m = *np) != NULL) {
2088 if (p > priority(vifp, mtod(m, struct ip *))) {
2090 /* If we're removing the last packet, fix the tail pointer */
2094 /* It's impossible for the queue to be empty, but check anyways. */
2095 if (--t->tbf_q_len == 0)
2097 mrtstat.mrts_drop_sel++;
2107 tbf_send_packet(struct vif *vifp, struct mbuf *m)
2111 if (vifp->v_flags & VIFF_TUNNEL) /* If tunnel options */
2112 ip_output(m, NULL, &vifp->v_route, IP_FORWARDING, NULL, NULL);
2114 struct ip_moptions imo;
2116 static struct route ro; /* XXX check this */
2118 imo.imo_multicast_ifp = vifp->v_ifp;
2119 imo.imo_multicast_ttl = mtod(m, struct ip *)->ip_ttl - 1;
2120 imo.imo_multicast_loop = 1;
2121 imo.imo_multicast_vif = -1;
2124 * Re-entrancy should not be a problem here, because
2125 * the packets that we send out and are looped back at us
2126 * should get rejected because they appear to come from
2127 * the loopback interface, thus preventing looping.
2129 error = ip_output(m, NULL, &ro, IP_FORWARDING, &imo, NULL);
2131 if (mrtdebug & DEBUG_XMIT)
2132 log(LOG_DEBUG, "phyint_send on vif %d err %d\n",
2133 (int)(vifp - viftable), error);
2137 /* determine the current time and then
2138 * the elapsed time (between the last time and time now)
2139 * in milliseconds & update the no. of tokens in the bucket
2142 tbf_update_tokens(struct vif *vifp)
2146 struct tbf *t = vifp->v_tbf;
2152 TV_DELTA(tp, t->tbf_last_pkt_t, tm);
2155 * This formula is actually
2156 * "time in seconds" * "bytes/second".
2158 * (tm / 1000000) * (v_rate_limit * 1000 * (1000/1024) / 8)
2160 * The (1000/1024) was introduced in add_vif to optimize
2161 * this divide into a shift.
2163 t->tbf_n_tok += tm * vifp->v_rate_limit / 1024 / 8;
2164 t->tbf_last_pkt_t = tp;
2166 if (t->tbf_n_tok > MAX_BKT_SIZE)
2167 t->tbf_n_tok = MAX_BKT_SIZE;
2171 priority(struct vif *vifp, struct ip *ip)
2173 int prio = 50; /* the lowest priority -- default case */
2175 /* temporary hack; may add general packet classifier some day */
2178 * The UDP port space is divided up into four priority ranges:
2179 * [0, 16384) : unclassified - lowest priority
2180 * [16384, 32768) : audio - highest priority
2181 * [32768, 49152) : whiteboard - medium priority
2182 * [49152, 65536) : video - low priority
2184 * Everything else gets lowest priority.
2186 if (ip->ip_p == IPPROTO_UDP) {
2187 struct udphdr *udp = (struct udphdr *)(((char *)ip) + (ip->ip_hl << 2));
2188 switch (ntohs(udp->uh_dport) & 0xc000) {
2204 * End of token bucket filter modifications
2208 X_ip_rsvp_vif(struct socket *so, struct sockopt *sopt)
2212 if (so->so_type != SOCK_RAW || so->so_proto->pr_protocol != IPPROTO_RSVP)
2215 error = sooptcopyin(sopt, &vifi, sizeof vifi, sizeof vifi);
2221 if (vifi < 0 || vifi >= numvifs) { /* Error if vif is invalid */
2223 return EADDRNOTAVAIL;
2226 if (sopt->sopt_name == IP_RSVP_VIF_ON) {
2227 /* Check if socket is available. */
2228 if (viftable[vifi].v_rsvpd != NULL) {
2233 viftable[vifi].v_rsvpd = so;
2234 /* This may seem silly, but we need to be sure we don't over-increment
2235 * the RSVP counter, in case something slips up.
2237 if (!viftable[vifi].v_rsvp_on) {
2238 viftable[vifi].v_rsvp_on = 1;
2241 } else { /* must be VIF_OFF */
2243 * XXX as an additional consistency check, one could make sure
2244 * that viftable[vifi].v_rsvpd == so, otherwise passing so as
2245 * first parameter is pretty useless.
2247 viftable[vifi].v_rsvpd = NULL;
2249 * This may seem silly, but we need to be sure we don't over-decrement
2250 * the RSVP counter, in case something slips up.
2252 if (viftable[vifi].v_rsvp_on) {
2253 viftable[vifi].v_rsvp_on = 0;
2262 X_ip_rsvp_force_done(struct socket *so)
2266 /* Don't bother if it is not the right type of socket. */
2267 if (so->so_type != SOCK_RAW || so->so_proto->pr_protocol != IPPROTO_RSVP)
2272 /* The socket may be attached to more than one vif...this
2273 * is perfectly legal.
2275 for (vifi = 0; vifi < numvifs; vifi++) {
2276 if (viftable[vifi].v_rsvpd == so) {
2277 viftable[vifi].v_rsvpd = NULL;
2278 /* This may seem silly, but we need to be sure we don't
2279 * over-decrement the RSVP counter, in case something slips up.
2281 if (viftable[vifi].v_rsvp_on) {
2282 viftable[vifi].v_rsvp_on = 0;
2292 X_rsvp_input(struct mbuf *m, int off)
2295 struct ip *ip = mtod(m, struct ip *);
2296 struct sockaddr_in rsvp_src = { sizeof rsvp_src, AF_INET };
2300 printf("rsvp_input: rsvp_on %d\n",rsvp_on);
2302 /* Can still get packets with rsvp_on = 0 if there is a local member
2303 * of the group to which the RSVP packet is addressed. But in this
2304 * case we want to throw the packet away.
2312 printf("rsvp_input: check vifs\n");
2318 ifp = m->m_pkthdr.rcvif;
2321 /* Find which vif the packet arrived on. */
2322 for (vifi = 0; vifi < numvifs; vifi++)
2323 if (viftable[vifi].v_ifp == ifp)
2326 if (vifi == numvifs || viftable[vifi].v_rsvpd == NULL) {
2328 * Drop the lock here to avoid holding it across rip_input.
2329 * This could make rsvpdebug printfs wrong. If you care,
2330 * record the state of stuff before dropping the lock.
2334 * If the old-style non-vif-associated socket is set,
2335 * then use it. Otherwise, drop packet since there
2336 * is no specific socket for this vif.
2338 if (ip_rsvpd != NULL) {
2340 printf("rsvp_input: Sending packet up old-style socket\n");
2341 rip_input(m, off); /* xxx */
2343 if (rsvpdebug && vifi == numvifs)
2344 printf("rsvp_input: Can't find vif for packet.\n");
2345 else if (rsvpdebug && viftable[vifi].v_rsvpd == NULL)
2346 printf("rsvp_input: No socket defined for vif %d\n",vifi);
2351 rsvp_src.sin_addr = ip->ip_src;
2354 printf("rsvp_input: m->m_len = %d, sbspace() = %ld\n",
2355 m->m_len,sbspace(&(viftable[vifi].v_rsvpd->so_rcv)));
2357 if (socket_send(viftable[vifi].v_rsvpd, m, &rsvp_src) < 0) {
2359 printf("rsvp_input: Failed to append to socket\n");
2362 printf("rsvp_input: send packet up\n");
2368 * Code for bandwidth monitors
2372 * Define common interface for timeval-related methods
2374 #define BW_TIMEVALCMP(tvp, uvp, cmp) timevalcmp((tvp), (uvp), cmp)
2375 #define BW_TIMEVALDECR(vvp, uvp) timevalsub((vvp), (uvp))
2376 #define BW_TIMEVALADD(vvp, uvp) timevaladd((vvp), (uvp))
2379 compute_bw_meter_flags(struct bw_upcall *req)
2383 if (req->bu_flags & BW_UPCALL_UNIT_PACKETS)
2384 flags |= BW_METER_UNIT_PACKETS;
2385 if (req->bu_flags & BW_UPCALL_UNIT_BYTES)
2386 flags |= BW_METER_UNIT_BYTES;
2387 if (req->bu_flags & BW_UPCALL_GEQ)
2388 flags |= BW_METER_GEQ;
2389 if (req->bu_flags & BW_UPCALL_LEQ)
2390 flags |= BW_METER_LEQ;
2396 * Add a bw_meter entry
2399 add_bw_upcall(struct bw_upcall *req)
2402 struct timeval delta = { BW_UPCALL_THRESHOLD_INTERVAL_MIN_SEC,
2403 BW_UPCALL_THRESHOLD_INTERVAL_MIN_USEC };
2408 if (!(mrt_api_config & MRT_MFC_BW_UPCALL))
2411 /* Test if the flags are valid */
2412 if (!(req->bu_flags & (BW_UPCALL_UNIT_PACKETS | BW_UPCALL_UNIT_BYTES)))
2414 if (!(req->bu_flags & (BW_UPCALL_GEQ | BW_UPCALL_LEQ)))
2416 if ((req->bu_flags & (BW_UPCALL_GEQ | BW_UPCALL_LEQ))
2417 == (BW_UPCALL_GEQ | BW_UPCALL_LEQ))
2420 /* Test if the threshold time interval is valid */
2421 if (BW_TIMEVALCMP(&req->bu_threshold.b_time, &delta, <))
2424 flags = compute_bw_meter_flags(req);
2427 * Find if we have already same bw_meter entry
2430 mfc = mfc_find(req->bu_src.s_addr, req->bu_dst.s_addr);
2433 return EADDRNOTAVAIL;
2435 for (x = mfc->mfc_bw_meter; x != NULL; x = x->bm_mfc_next) {
2436 if ((BW_TIMEVALCMP(&x->bm_threshold.b_time,
2437 &req->bu_threshold.b_time, ==)) &&
2438 (x->bm_threshold.b_packets == req->bu_threshold.b_packets) &&
2439 (x->bm_threshold.b_bytes == req->bu_threshold.b_bytes) &&
2440 (x->bm_flags & BW_METER_USER_FLAGS) == flags) {
2442 return 0; /* XXX Already installed */
2446 /* Allocate the new bw_meter entry */
2447 x = (struct bw_meter *)malloc(sizeof(*x), M_BWMETER, M_NOWAIT);
2453 /* Set the new bw_meter entry */
2454 x->bm_threshold.b_time = req->bu_threshold.b_time;
2456 x->bm_start_time = now;
2457 x->bm_threshold.b_packets = req->bu_threshold.b_packets;
2458 x->bm_threshold.b_bytes = req->bu_threshold.b_bytes;
2459 x->bm_measured.b_packets = 0;
2460 x->bm_measured.b_bytes = 0;
2461 x->bm_flags = flags;
2462 x->bm_time_next = NULL;
2463 x->bm_time_hash = BW_METER_BUCKETS;
2465 /* Add the new bw_meter entry to the front of entries for this MFC */
2467 x->bm_mfc_next = mfc->mfc_bw_meter;
2468 mfc->mfc_bw_meter = x;
2469 schedule_bw_meter(x, &now);
2476 free_bw_list(struct bw_meter *list)
2478 while (list != NULL) {
2479 struct bw_meter *x = list;
2481 list = list->bm_mfc_next;
2482 unschedule_bw_meter(x);
2488 * Delete one or multiple bw_meter entries
2491 del_bw_upcall(struct bw_upcall *req)
2496 if (!(mrt_api_config & MRT_MFC_BW_UPCALL))
2500 /* Find the corresponding MFC entry */
2501 mfc = mfc_find(req->bu_src.s_addr, req->bu_dst.s_addr);
2504 return EADDRNOTAVAIL;
2505 } else if (req->bu_flags & BW_UPCALL_DELETE_ALL) {
2507 * Delete all bw_meter entries for this mfc
2509 struct bw_meter *list;
2511 list = mfc->mfc_bw_meter;
2512 mfc->mfc_bw_meter = NULL;
2516 } else { /* Delete a single bw_meter entry */
2517 struct bw_meter *prev;
2520 flags = compute_bw_meter_flags(req);
2522 /* Find the bw_meter entry to delete */
2523 for (prev = NULL, x = mfc->mfc_bw_meter; x != NULL;
2524 prev = x, x = x->bm_mfc_next) {
2525 if ((BW_TIMEVALCMP(&x->bm_threshold.b_time,
2526 &req->bu_threshold.b_time, ==)) &&
2527 (x->bm_threshold.b_packets == req->bu_threshold.b_packets) &&
2528 (x->bm_threshold.b_bytes == req->bu_threshold.b_bytes) &&
2529 (x->bm_flags & BW_METER_USER_FLAGS) == flags)
2532 if (x != NULL) { /* Delete entry from the list for this MFC */
2534 prev->bm_mfc_next = x->bm_mfc_next; /* remove from middle*/
2536 x->bm_mfc->mfc_bw_meter = x->bm_mfc_next;/* new head of list */
2538 unschedule_bw_meter(x);
2540 /* Free the bw_meter entry */
2552 * Perform bandwidth measurement processing that may result in an upcall
2555 bw_meter_receive_packet(struct bw_meter *x, int plen, struct timeval *nowp)
2557 struct timeval delta;
2562 BW_TIMEVALDECR(&delta, &x->bm_start_time);
2564 if (x->bm_flags & BW_METER_GEQ) {
2566 * Processing for ">=" type of bw_meter entry
2568 if (BW_TIMEVALCMP(&delta, &x->bm_threshold.b_time, >)) {
2569 /* Reset the bw_meter entry */
2570 x->bm_start_time = *nowp;
2571 x->bm_measured.b_packets = 0;
2572 x->bm_measured.b_bytes = 0;
2573 x->bm_flags &= ~BW_METER_UPCALL_DELIVERED;
2576 /* Record that a packet is received */
2577 x->bm_measured.b_packets++;
2578 x->bm_measured.b_bytes += plen;
2581 * Test if we should deliver an upcall
2583 if (!(x->bm_flags & BW_METER_UPCALL_DELIVERED)) {
2584 if (((x->bm_flags & BW_METER_UNIT_PACKETS) &&
2585 (x->bm_measured.b_packets >= x->bm_threshold.b_packets)) ||
2586 ((x->bm_flags & BW_METER_UNIT_BYTES) &&
2587 (x->bm_measured.b_bytes >= x->bm_threshold.b_bytes))) {
2588 /* Prepare an upcall for delivery */
2589 bw_meter_prepare_upcall(x, nowp);
2590 x->bm_flags |= BW_METER_UPCALL_DELIVERED;
2593 } else if (x->bm_flags & BW_METER_LEQ) {
2595 * Processing for "<=" type of bw_meter entry
2597 if (BW_TIMEVALCMP(&delta, &x->bm_threshold.b_time, >)) {
2599 * We are behind time with the multicast forwarding table
2600 * scanning for "<=" type of bw_meter entries, so test now
2601 * if we should deliver an upcall.
2603 if (((x->bm_flags & BW_METER_UNIT_PACKETS) &&
2604 (x->bm_measured.b_packets <= x->bm_threshold.b_packets)) ||
2605 ((x->bm_flags & BW_METER_UNIT_BYTES) &&
2606 (x->bm_measured.b_bytes <= x->bm_threshold.b_bytes))) {
2607 /* Prepare an upcall for delivery */
2608 bw_meter_prepare_upcall(x, nowp);
2610 /* Reschedule the bw_meter entry */
2611 unschedule_bw_meter(x);
2612 schedule_bw_meter(x, nowp);
2615 /* Record that a packet is received */
2616 x->bm_measured.b_packets++;
2617 x->bm_measured.b_bytes += plen;
2620 * Test if we should restart the measuring interval
2622 if ((x->bm_flags & BW_METER_UNIT_PACKETS &&
2623 x->bm_measured.b_packets <= x->bm_threshold.b_packets) ||
2624 (x->bm_flags & BW_METER_UNIT_BYTES &&
2625 x->bm_measured.b_bytes <= x->bm_threshold.b_bytes)) {
2626 /* Don't restart the measuring interval */
2628 /* Do restart the measuring interval */
2630 * XXX: note that we don't unschedule and schedule, because this
2631 * might be too much overhead per packet. Instead, when we process
2632 * all entries for a given timer hash bin, we check whether it is
2633 * really a timeout. If not, we reschedule at that time.
2635 x->bm_start_time = *nowp;
2636 x->bm_measured.b_packets = 0;
2637 x->bm_measured.b_bytes = 0;
2638 x->bm_flags &= ~BW_METER_UPCALL_DELIVERED;
2644 * Prepare a bandwidth-related upcall
2647 bw_meter_prepare_upcall(struct bw_meter *x, struct timeval *nowp)
2649 struct timeval delta;
2650 struct bw_upcall *u;
2655 * Compute the measured time interval
2658 BW_TIMEVALDECR(&delta, &x->bm_start_time);
2661 * If there are too many pending upcalls, deliver them now
2663 if (bw_upcalls_n >= BW_UPCALLS_MAX)
2667 * Set the bw_upcall entry
2669 u = &bw_upcalls[bw_upcalls_n++];
2670 u->bu_src = x->bm_mfc->mfc_origin;
2671 u->bu_dst = x->bm_mfc->mfc_mcastgrp;
2672 u->bu_threshold.b_time = x->bm_threshold.b_time;
2673 u->bu_threshold.b_packets = x->bm_threshold.b_packets;
2674 u->bu_threshold.b_bytes = x->bm_threshold.b_bytes;
2675 u->bu_measured.b_time = delta;
2676 u->bu_measured.b_packets = x->bm_measured.b_packets;
2677 u->bu_measured.b_bytes = x->bm_measured.b_bytes;
2679 if (x->bm_flags & BW_METER_UNIT_PACKETS)
2680 u->bu_flags |= BW_UPCALL_UNIT_PACKETS;
2681 if (x->bm_flags & BW_METER_UNIT_BYTES)
2682 u->bu_flags |= BW_UPCALL_UNIT_BYTES;
2683 if (x->bm_flags & BW_METER_GEQ)
2684 u->bu_flags |= BW_UPCALL_GEQ;
2685 if (x->bm_flags & BW_METER_LEQ)
2686 u->bu_flags |= BW_UPCALL_LEQ;
2690 * Send the pending bandwidth-related upcalls
2693 bw_upcalls_send(void)
2696 int len = bw_upcalls_n * sizeof(bw_upcalls[0]);
2697 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
2698 static struct igmpmsg igmpmsg = { 0, /* unused1 */
2700 IGMPMSG_BW_UPCALL,/* im_msgtype */
2705 { 0 } }; /* im_dst */
2709 if (bw_upcalls_n == 0)
2710 return; /* No pending upcalls */
2715 * Allocate a new mbuf, initialize it with the header and
2716 * the payload for the pending calls.
2718 MGETHDR(m, M_DONTWAIT, MT_HEADER);
2720 log(LOG_WARNING, "bw_upcalls_send: cannot allocate mbuf\n");
2724 m->m_len = m->m_pkthdr.len = 0;
2725 m_copyback(m, 0, sizeof(struct igmpmsg), (caddr_t)&igmpmsg);
2726 m_copyback(m, sizeof(struct igmpmsg), len, (caddr_t)&bw_upcalls[0]);
2730 * XXX do we need to set the address in k_igmpsrc ?
2732 mrtstat.mrts_upcalls++;
2733 if (socket_send(ip_mrouter, m, &k_igmpsrc) < 0) {
2734 log(LOG_WARNING, "bw_upcalls_send: ip_mrouter socket queue full\n");
2735 ++mrtstat.mrts_upq_sockfull;
2740 * Compute the timeout hash value for the bw_meter entries
2742 #define BW_METER_TIMEHASH(bw_meter, hash) \
2744 struct timeval next_timeval = (bw_meter)->bm_start_time; \
2746 BW_TIMEVALADD(&next_timeval, &(bw_meter)->bm_threshold.b_time); \
2747 (hash) = next_timeval.tv_sec; \
2748 if (next_timeval.tv_usec) \
2749 (hash)++; /* XXX: make sure we don't timeout early */ \
2750 (hash) %= BW_METER_BUCKETS; \
2754 * Schedule a timer to process periodically bw_meter entry of type "<="
2755 * by linking the entry in the proper hash bucket.
2758 schedule_bw_meter(struct bw_meter *x, struct timeval *nowp)
2764 if (!(x->bm_flags & BW_METER_LEQ))
2765 return; /* XXX: we schedule timers only for "<=" entries */
2768 * Reset the bw_meter entry
2770 x->bm_start_time = *nowp;
2771 x->bm_measured.b_packets = 0;
2772 x->bm_measured.b_bytes = 0;
2773 x->bm_flags &= ~BW_METER_UPCALL_DELIVERED;
2776 * Compute the timeout hash value and insert the entry
2778 BW_METER_TIMEHASH(x, time_hash);
2779 x->bm_time_next = bw_meter_timers[time_hash];
2780 bw_meter_timers[time_hash] = x;
2781 x->bm_time_hash = time_hash;
2785 * Unschedule the periodic timer that processes bw_meter entry of type "<="
2786 * by removing the entry from the proper hash bucket.
2789 unschedule_bw_meter(struct bw_meter *x)
2792 struct bw_meter *prev, *tmp;
2796 if (!(x->bm_flags & BW_METER_LEQ))
2797 return; /* XXX: we schedule timers only for "<=" entries */
2800 * Compute the timeout hash value and delete the entry
2802 time_hash = x->bm_time_hash;
2803 if (time_hash >= BW_METER_BUCKETS)
2804 return; /* Entry was not scheduled */
2806 for (prev = NULL, tmp = bw_meter_timers[time_hash];
2807 tmp != NULL; prev = tmp, tmp = tmp->bm_time_next)
2812 panic("unschedule_bw_meter: bw_meter entry not found");
2815 prev->bm_time_next = x->bm_time_next;
2817 bw_meter_timers[time_hash] = x->bm_time_next;
2819 x->bm_time_next = NULL;
2820 x->bm_time_hash = BW_METER_BUCKETS;
2825 * Process all "<=" type of bw_meter that should be processed now,
2826 * and for each entry prepare an upcall if necessary. Each processed
2827 * entry is rescheduled again for the (periodic) processing.
2829 * This is run periodically (once per second normally). On each round,
2830 * all the potentially matching entries are in the hash slot that we are
2836 static uint32_t last_tv_sec; /* last time we processed this */
2840 struct timeval now, process_endtime;
2843 if (last_tv_sec == now.tv_sec)
2844 return; /* nothing to do */
2846 loops = now.tv_sec - last_tv_sec;
2847 last_tv_sec = now.tv_sec;
2848 if (loops > BW_METER_BUCKETS)
2849 loops = BW_METER_BUCKETS;
2853 * Process all bins of bw_meter entries from the one after the last
2854 * processed to the current one. On entry, i points to the last bucket
2855 * visited, so we need to increment i at the beginning of the loop.
2857 for (i = (now.tv_sec - loops) % BW_METER_BUCKETS; loops > 0; loops--) {
2858 struct bw_meter *x, *tmp_list;
2860 if (++i >= BW_METER_BUCKETS)
2863 /* Disconnect the list of bw_meter entries from the bin */
2864 tmp_list = bw_meter_timers[i];
2865 bw_meter_timers[i] = NULL;
2867 /* Process the list of bw_meter entries */
2868 while (tmp_list != NULL) {
2870 tmp_list = tmp_list->bm_time_next;
2872 /* Test if the time interval is over */
2873 process_endtime = x->bm_start_time;
2874 BW_TIMEVALADD(&process_endtime, &x->bm_threshold.b_time);
2875 if (BW_TIMEVALCMP(&process_endtime, &now, >)) {
2876 /* Not yet: reschedule, but don't reset */
2879 BW_METER_TIMEHASH(x, time_hash);
2880 if (time_hash == i && process_endtime.tv_sec == now.tv_sec) {
2882 * XXX: somehow the bin processing is a bit ahead of time.
2883 * Put the entry in the next bin.
2885 if (++time_hash >= BW_METER_BUCKETS)
2888 x->bm_time_next = bw_meter_timers[time_hash];
2889 bw_meter_timers[time_hash] = x;
2890 x->bm_time_hash = time_hash;
2896 * Test if we should deliver an upcall
2898 if (((x->bm_flags & BW_METER_UNIT_PACKETS) &&
2899 (x->bm_measured.b_packets <= x->bm_threshold.b_packets)) ||
2900 ((x->bm_flags & BW_METER_UNIT_BYTES) &&
2901 (x->bm_measured.b_bytes <= x->bm_threshold.b_bytes))) {
2902 /* Prepare an upcall for delivery */
2903 bw_meter_prepare_upcall(x, &now);
2907 * Reschedule for next processing
2909 schedule_bw_meter(x, &now);
2913 /* Send all upcalls that are pending delivery */
2920 * A periodic function for sending all upcalls that are pending delivery
2923 expire_bw_upcalls_send(void *unused)
2929 callout_reset(&bw_upcalls_ch, BW_UPCALLS_PERIOD,
2930 expire_bw_upcalls_send, NULL);
2934 * A periodic function for periodic scanning of the multicast forwarding
2935 * table for processing all "<=" bw_meter entries.
2938 expire_bw_meter_process(void *unused)
2940 if (mrt_api_config & MRT_MFC_BW_UPCALL)
2943 callout_reset(&bw_meter_ch, BW_METER_PERIOD, expire_bw_meter_process, NULL);
2947 * End of bandwidth monitoring code
2952 * Send the packet up to the user daemon, or eventually do kernel encapsulation
2956 pim_register_send(struct ip *ip, struct vif *vifp,
2957 struct mbuf *m, struct mfc *rt)
2959 struct mbuf *mb_copy, *mm;
2961 if (mrtdebug & DEBUG_PIM)
2962 log(LOG_DEBUG, "pim_register_send: ");
2964 mb_copy = pim_register_prepare(ip, m);
2965 if (mb_copy == NULL)
2969 * Send all the fragments. Note that the mbuf for each fragment
2970 * is freed by the sending machinery.
2972 for (mm = mb_copy; mm; mm = mb_copy) {
2973 mb_copy = mm->m_nextpkt;
2975 mm = m_pullup(mm, sizeof(struct ip));
2977 ip = mtod(mm, struct ip *);
2978 if ((mrt_api_config & MRT_MFC_RP) &&
2979 (rt->mfc_rp.s_addr != INADDR_ANY)) {
2980 pim_register_send_rp(ip, vifp, mm, rt);
2982 pim_register_send_upcall(ip, vifp, mm, rt);
2991 * Return a copy of the data packet that is ready for PIM Register
2993 * XXX: Note that in the returned copy the IP header is a valid one.
2995 static struct mbuf *
2996 pim_register_prepare(struct ip *ip, struct mbuf *m)
2998 struct mbuf *mb_copy = NULL;
3001 /* Take care of delayed checksums */
3002 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
3003 in_delayed_cksum(m);
3004 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
3008 * Copy the old packet & pullup its IP header into the
3009 * new mbuf so we can modify it.
3011 mb_copy = m_copypacket(m, M_DONTWAIT);
3012 if (mb_copy == NULL)
3014 mb_copy = m_pullup(mb_copy, ip->ip_hl << 2);
3015 if (mb_copy == NULL)
3018 /* take care of the TTL */
3019 ip = mtod(mb_copy, struct ip *);
3022 /* Compute the MTU after the PIM Register encapsulation */
3023 mtu = 0xffff - sizeof(pim_encap_iphdr) - sizeof(pim_encap_pimhdr);
3025 if (ip->ip_len <= mtu) {
3026 /* Turn the IP header into a valid one */
3027 ip->ip_len = htons(ip->ip_len);
3028 ip->ip_off = htons(ip->ip_off);
3030 ip->ip_sum = in_cksum(mb_copy, ip->ip_hl << 2);
3032 /* Fragment the packet */
3033 if (ip_fragment(ip, &mb_copy, mtu, 0, CSUM_DELAY_IP) != 0) {
3042 * Send an upcall with the data packet to the user-level process.
3045 pim_register_send_upcall(struct ip *ip, struct vif *vifp,
3046 struct mbuf *mb_copy, struct mfc *rt)
3048 struct mbuf *mb_first;
3049 int len = ntohs(ip->ip_len);
3051 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
3056 * Add a new mbuf with an upcall header
3058 MGETHDR(mb_first, M_DONTWAIT, MT_HEADER);
3059 if (mb_first == NULL) {
3063 mb_first->m_data += max_linkhdr;
3064 mb_first->m_pkthdr.len = len + sizeof(struct igmpmsg);
3065 mb_first->m_len = sizeof(struct igmpmsg);
3066 mb_first->m_next = mb_copy;
3068 /* Send message to routing daemon */
3069 im = mtod(mb_first, struct igmpmsg *);
3070 im->im_msgtype = IGMPMSG_WHOLEPKT;
3072 im->im_vif = vifp - viftable;
3073 im->im_src = ip->ip_src;
3074 im->im_dst = ip->ip_dst;
3076 k_igmpsrc.sin_addr = ip->ip_src;
3078 mrtstat.mrts_upcalls++;
3080 if (socket_send(ip_mrouter, mb_first, &k_igmpsrc) < 0) {
3081 if (mrtdebug & DEBUG_PIM)
3083 "mcast: pim_register_send_upcall: ip_mrouter socket queue full");
3084 ++mrtstat.mrts_upq_sockfull;
3088 /* Keep statistics */
3089 pimstat.pims_snd_registers_msgs++;
3090 pimstat.pims_snd_registers_bytes += len;
3096 * Encapsulate the data packet in PIM Register message and send it to the RP.
3099 pim_register_send_rp(struct ip *ip, struct vif *vifp,
3100 struct mbuf *mb_copy, struct mfc *rt)
3102 struct mbuf *mb_first;
3103 struct ip *ip_outer;
3104 struct pim_encap_pimhdr *pimhdr;
3105 int len = ntohs(ip->ip_len);
3106 vifi_t vifi = rt->mfc_parent;
3110 if ((vifi >= numvifs) || (viftable[vifi].v_lcl_addr.s_addr == 0)) {
3112 return EADDRNOTAVAIL; /* The iif vif is invalid */
3116 * Add a new mbuf with the encapsulating header
3118 MGETHDR(mb_first, M_DONTWAIT, MT_HEADER);
3119 if (mb_first == NULL) {
3123 mb_first->m_data += max_linkhdr;
3124 mb_first->m_len = sizeof(pim_encap_iphdr) + sizeof(pim_encap_pimhdr);
3125 mb_first->m_next = mb_copy;
3127 mb_first->m_pkthdr.len = len + mb_first->m_len;
3130 * Fill in the encapsulating IP and PIM header
3132 ip_outer = mtod(mb_first, struct ip *);
3133 *ip_outer = pim_encap_iphdr;
3134 ip_outer->ip_id = ip_newid();
3135 ip_outer->ip_len = len + sizeof(pim_encap_iphdr) + sizeof(pim_encap_pimhdr);
3136 ip_outer->ip_src = viftable[vifi].v_lcl_addr;
3137 ip_outer->ip_dst = rt->mfc_rp;
3139 * Copy the inner header TOS to the outer header, and take care of the
3142 ip_outer->ip_tos = ip->ip_tos;
3143 if (ntohs(ip->ip_off) & IP_DF)
3144 ip_outer->ip_off |= IP_DF;
3145 pimhdr = (struct pim_encap_pimhdr *)((caddr_t)ip_outer
3146 + sizeof(pim_encap_iphdr));
3147 *pimhdr = pim_encap_pimhdr;
3148 /* If the iif crosses a border, set the Border-bit */
3149 if (rt->mfc_flags[vifi] & MRT_MFC_FLAGS_BORDER_VIF & mrt_api_config)
3150 pimhdr->flags |= htonl(PIM_BORDER_REGISTER);
3152 mb_first->m_data += sizeof(pim_encap_iphdr);
3153 pimhdr->pim.pim_cksum = in_cksum(mb_first, sizeof(pim_encap_pimhdr));
3154 mb_first->m_data -= sizeof(pim_encap_iphdr);
3156 if (vifp->v_rate_limit == 0)
3157 tbf_send_packet(vifp, mb_first);
3159 tbf_control(vifp, mb_first, ip, ip_outer->ip_len);
3161 /* Keep statistics */
3162 pimstat.pims_snd_registers_msgs++;
3163 pimstat.pims_snd_registers_bytes += len;
3169 * PIM-SMv2 and PIM-DM messages processing.
3170 * Receives and verifies the PIM control messages, and passes them
3171 * up to the listening socket, using rip_input().
3172 * The only message with special processing is the PIM_REGISTER message
3173 * (used by PIM-SM): the PIM header is stripped off, and the inner packet
3174 * is passed to if_simloop().
3177 pim_input(struct mbuf *m, int off)
3179 struct ip *ip = mtod(m, struct ip *);
3182 int datalen = ip->ip_len;
3186 /* Keep statistics */
3187 pimstat.pims_rcv_total_msgs++;
3188 pimstat.pims_rcv_total_bytes += datalen;
3193 if (datalen < PIM_MINLEN) {
3194 pimstat.pims_rcv_tooshort++;
3195 log(LOG_ERR, "pim_input: packet size too small %d from %lx\n",
3196 datalen, (u_long)ip->ip_src.s_addr);
3202 * If the packet is at least as big as a REGISTER, go agead
3203 * and grab the PIM REGISTER header size, to avoid another
3204 * possible m_pullup() later.
3206 * PIM_MINLEN == pimhdr + u_int32_t == 4 + 4 = 8
3207 * PIM_REG_MINLEN == pimhdr + reghdr + encap_iphdr == 4 + 4 + 20 = 28
3209 minlen = iphlen + (datalen >= PIM_REG_MINLEN ? PIM_REG_MINLEN : PIM_MINLEN);
3211 * Get the IP and PIM headers in contiguous memory, and
3212 * possibly the PIM REGISTER header.
3214 if ((m->m_flags & M_EXT || m->m_len < minlen) &&
3215 (m = m_pullup(m, minlen)) == 0) {
3216 log(LOG_ERR, "pim_input: m_pullup failure\n");
3219 /* m_pullup() may have given us a new mbuf so reset ip. */
3220 ip = mtod(m, struct ip *);
3221 ip_tos = ip->ip_tos;
3223 /* adjust mbuf to point to the PIM header */
3224 m->m_data += iphlen;
3226 pim = mtod(m, struct pim *);
3229 * Validate checksum. If PIM REGISTER, exclude the data packet.
3231 * XXX: some older PIMv2 implementations don't make this distinction,
3232 * so for compatibility reason perform the checksum over part of the
3233 * message, and if error, then over the whole message.
3235 if (PIM_VT_T(pim->pim_vt) == PIM_REGISTER && in_cksum(m, PIM_MINLEN) == 0) {
3236 /* do nothing, checksum okay */
3237 } else if (in_cksum(m, datalen)) {
3238 pimstat.pims_rcv_badsum++;
3239 if (mrtdebug & DEBUG_PIM)
3240 log(LOG_DEBUG, "pim_input: invalid checksum");
3245 /* PIM version check */
3246 if (PIM_VT_V(pim->pim_vt) < PIM_VERSION) {
3247 pimstat.pims_rcv_badversion++;
3248 log(LOG_ERR, "pim_input: incorrect version %d, expecting %d\n",
3249 PIM_VT_V(pim->pim_vt), PIM_VERSION);
3254 /* restore mbuf back to the outer IP */
3255 m->m_data -= iphlen;
3258 if (PIM_VT_T(pim->pim_vt) == PIM_REGISTER) {
3260 * Since this is a REGISTER, we'll make a copy of the register
3261 * headers ip + pim + u_int32 + encap_ip, to be passed up to the
3264 struct sockaddr_in dst = { sizeof(dst), AF_INET };
3266 struct ip *encap_ip;
3271 if ((reg_vif_num >= numvifs) || (reg_vif_num == VIFI_INVALID)) {
3273 if (mrtdebug & DEBUG_PIM)
3275 "pim_input: register vif not set: %d\n", reg_vif_num);
3279 /* XXX need refcnt? */
3280 vifp = viftable[reg_vif_num].v_ifp;
3286 if (datalen < PIM_REG_MINLEN) {
3287 pimstat.pims_rcv_tooshort++;
3288 pimstat.pims_rcv_badregisters++;
3290 "pim_input: register packet size too small %d from %lx\n",
3291 datalen, (u_long)ip->ip_src.s_addr);
3296 reghdr = (u_int32_t *)(pim + 1);
3297 encap_ip = (struct ip *)(reghdr + 1);
3299 if (mrtdebug & DEBUG_PIM) {
3301 "pim_input[register], encap_ip: %lx -> %lx, encap_ip len %d\n",
3302 (u_long)ntohl(encap_ip->ip_src.s_addr),
3303 (u_long)ntohl(encap_ip->ip_dst.s_addr),
3304 ntohs(encap_ip->ip_len));
3307 /* verify the version number of the inner packet */
3308 if (encap_ip->ip_v != IPVERSION) {
3309 pimstat.pims_rcv_badregisters++;
3310 if (mrtdebug & DEBUG_PIM) {
3311 log(LOG_DEBUG, "pim_input: invalid IP version (%d) "
3312 "of the inner packet\n", encap_ip->ip_v);
3318 /* verify the inner packet is destined to a mcast group */
3319 if (!IN_MULTICAST(ntohl(encap_ip->ip_dst.s_addr))) {
3320 pimstat.pims_rcv_badregisters++;
3321 if (mrtdebug & DEBUG_PIM)
3323 "pim_input: inner packet of register is not "
3325 (u_long)ntohl(encap_ip->ip_dst.s_addr));
3330 /* If a NULL_REGISTER, pass it to the daemon */
3331 if ((ntohl(*reghdr) & PIM_NULL_REGISTER))
3332 goto pim_input_to_daemon;
3335 * Copy the TOS from the outer IP header to the inner IP header.
3337 if (encap_ip->ip_tos != ip_tos) {
3338 /* Outer TOS -> inner TOS */
3339 encap_ip->ip_tos = ip_tos;
3340 /* Recompute the inner header checksum. Sigh... */
3342 /* adjust mbuf to point to the inner IP header */
3343 m->m_data += (iphlen + PIM_MINLEN);
3344 m->m_len -= (iphlen + PIM_MINLEN);
3346 encap_ip->ip_sum = 0;
3347 encap_ip->ip_sum = in_cksum(m, encap_ip->ip_hl << 2);
3349 /* restore mbuf to point back to the outer IP header */
3350 m->m_data -= (iphlen + PIM_MINLEN);
3351 m->m_len += (iphlen + PIM_MINLEN);
3355 * Decapsulate the inner IP packet and loopback to forward it
3356 * as a normal multicast packet. Also, make a copy of the
3357 * outer_iphdr + pimhdr + reghdr + encap_iphdr
3358 * to pass to the daemon later, so it can take the appropriate
3359 * actions (e.g., send back PIM_REGISTER_STOP).
3360 * XXX: here m->m_data points to the outer IP header.
3362 mcp = m_copy(m, 0, iphlen + PIM_REG_MINLEN);
3365 "pim_input: pim register: could not copy register head\n");
3370 /* Keep statistics */
3371 /* XXX: registers_bytes include only the encap. mcast pkt */
3372 pimstat.pims_rcv_registers_msgs++;
3373 pimstat.pims_rcv_registers_bytes += ntohs(encap_ip->ip_len);
3376 * forward the inner ip packet; point m_data at the inner ip.
3378 m_adj(m, iphlen + PIM_MINLEN);
3380 if (mrtdebug & DEBUG_PIM) {
3382 "pim_input: forwarding decapsulated register: "
3383 "src %lx, dst %lx, vif %d\n",
3384 (u_long)ntohl(encap_ip->ip_src.s_addr),
3385 (u_long)ntohl(encap_ip->ip_dst.s_addr),
3388 /* NB: vifp was collected above; can it change on us? */
3389 if_simloop(vifp, m, dst.sin_family, 0);
3391 /* prepare the register head to send to the mrouting daemon */
3395 pim_input_to_daemon:
3397 * Pass the PIM message up to the daemon; if it is a Register message,
3398 * pass the 'head' only up to the daemon. This includes the
3399 * outer IP header, PIM header, PIM-Register header and the
3401 * XXX: the outer IP header pkt size of a Register is not adjust to
3402 * reflect the fact that the inner multicast data is truncated.
3404 rip_input(m, iphlen);
3411 ip_mroute_modevent(module_t mod, int type, void *unused)
3415 mtx_init(&mrouter_mtx, "mrouter initialization", NULL, MTX_DEF);
3419 ip_mcast_src = X_ip_mcast_src;
3420 ip_mforward = X_ip_mforward;
3421 ip_mrouter_done = X_ip_mrouter_done;
3422 ip_mrouter_get = X_ip_mrouter_get;
3423 ip_mrouter_set = X_ip_mrouter_set;
3424 ip_rsvp_force_done = X_ip_rsvp_force_done;
3425 ip_rsvp_vif = X_ip_rsvp_vif;
3426 legal_vif_num = X_legal_vif_num;
3427 mrt_ioctl = X_mrt_ioctl;
3428 rsvp_input_p = X_rsvp_input;
3433 * Typically module unload happens after the user-level
3434 * process has shutdown the kernel services (the check
3435 * below insures someone can't just yank the module out
3436 * from under a running process). But if the module is
3437 * just loaded and then unloaded w/o starting up a user
3438 * process we still need to cleanup.
3443 X_ip_mrouter_done();
3444 ip_mcast_src = NULL;
3446 ip_mrouter_done = NULL;
3447 ip_mrouter_get = NULL;
3448 ip_mrouter_set = NULL;
3449 ip_rsvp_force_done = NULL;
3451 legal_vif_num = NULL;
3453 rsvp_input_p = NULL;
3456 mtx_destroy(&mrouter_mtx);
3464 static moduledata_t ip_mroutemod = {
3469 DECLARE_MODULE(ip_mroute, ip_mroutemod, SI_SUB_PSEUDO, SI_ORDER_ANY);